LLVM 20.0.0git
ValueMapper.h
Go to the documentation of this file.
1//===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the MapValue interface which is used by various parts of
10// the Transforms/Utils library to implement cloning and linking facilities.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
15#define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
16
17#include "llvm/ADT/ArrayRef.h"
20#include "llvm/IR/ValueHandle.h"
21#include "llvm/IR/ValueMap.h"
22
23namespace llvm {
24
25class Constant;
26class DIBuilder;
27class DbgRecord;
28class Function;
29class GlobalVariable;
30class Instruction;
31class MDNode;
32class Metadata;
33class Module;
34class Type;
35class Value;
36
37using ValueToValueMapTy = ValueMap<const Value *, WeakTrackingVH>;
40
41/// This is a class that can be implemented by clients to remap types when
42/// cloning constants and instructions.
44 virtual void anchor(); // Out of line method.
45
46public:
47 virtual ~ValueMapTypeRemapper() = default;
48
49 /// The client should implement this method if they want to remap types while
50 /// mapping values.
51 virtual Type *remapType(Type *SrcTy) = 0;
52};
53
54/// This is a class that can be implemented by clients to materialize Values on
55/// demand.
57 virtual void anchor(); // Out of line method.
58
59protected:
60 ValueMaterializer() = default;
63 ~ValueMaterializer() = default;
64
65public:
66 /// This method can be implemented to generate a mapped Value on demand. For
67 /// example, if linking lazily. Returns null if the value is not materialized.
68 virtual Value *materialize(Value *V) = 0;
69};
70
71/// These are flags that the value mapping APIs allow.
74
75 /// If this flag is set, the remapper knows that only local values within a
76 /// function (such as an instruction or argument) are mapped, not global
77 /// values like functions and global metadata.
79
80 /// If this flag is set, the remapper ignores missing function-local entries
81 /// (Argument, Instruction, BasicBlock) that are not in the value map. If it
82 /// is unset, it aborts if an operand is asked to be remapped which doesn't
83 /// exist in the mapping.
84 ///
85 /// There are no such assertions in MapValue(), whose results are almost
86 /// unchanged by this flag. This flag mainly changes the assertion behaviour
87 /// in RemapInstruction().
88 ///
89 /// Since an Instruction's metadata operands (even that point to SSA values)
90 /// aren't guaranteed to be dominated by their definitions, MapMetadata will
91 /// return "!{}" instead of "null" for \a LocalAsMetadata instances whose SSA
92 /// values are unmapped when this flag is set. Otherwise, \a MapValue()
93 /// completely ignores this flag.
94 ///
95 /// \a MapMetadata() always ignores this flag.
97
98 /// Instruct the remapper to reuse and mutate distinct metadata (remapping
99 /// them in place) instead of cloning remapped copies. This flag has no
100 /// effect when RF_NoModuleLevelChanges, since that implies an identity
101 /// mapping.
103
104 /// Any global values not in value map are mapped to null instead of mapping
105 /// to self. Illegal if RF_IgnoreMissingLocals is also set.
107};
108
110 return RemapFlags(unsigned(LHS) | unsigned(RHS));
111}
112
113/// Context for (re-)mapping values (and metadata).
114///
115/// A shared context used for mapping and remapping of Value and Metadata
116/// instances using \a ValueToValueMapTy, \a RemapFlags, \a
117/// ValueMapTypeRemapper, \a ValueMaterializer, and \a IdentityMD.
118///
119/// There are a number of top-level entry points:
120/// - \a mapValue() (and \a mapConstant());
121/// - \a mapMetadata() (and \a mapMDNode());
122/// - \a remapInstruction();
123/// - \a remapFunction(); and
124/// - \a remapGlobalObjectMetadata().
125///
126/// The \a ValueMaterializer can be used as a callback, but cannot invoke any
127/// of these top-level functions recursively. Instead, callbacks should use
128/// one of the following to schedule work lazily in the \a ValueMapper
129/// instance:
130/// - \a scheduleMapGlobalInitializer()
131/// - \a scheduleMapAppendingVariable()
132/// - \a scheduleMapGlobalAlias()
133/// - \a scheduleMapGlobalIFunc()
134/// - \a scheduleRemapFunction()
135///
136/// Sometimes a callback needs a different mapping context. Such a context can
137/// be registered using \a registerAlternateMappingContext(), which takes an
138/// alternate \a ValueToValueMapTy and \a ValueMaterializer and returns a ID to
139/// pass into the schedule*() functions.
140///
141/// If an \a IdentityMD set is optionally provided, \a Metadata inside this set
142/// will be mapped onto itself in \a VM on first use.
143///
144/// TODO: lib/Linker really doesn't need the \a ValueHandle in the \a
145/// ValueToValueMapTy. We should template \a ValueMapper (and its
146/// implementation classes), and explicitly instantiate on two concrete
147/// instances of \a ValueMap (one as \a ValueToValueMap, and one with raw \a
148/// Value pointers). It may be viable to do away with \a TrackingMDRef in the
149/// \a Metadata side map for the lib/Linker case as well, in which case we'll
150/// need a new template parameter on \a ValueMap.
151///
152/// TODO: Update callers of \a RemapInstruction() and \a MapValue() (etc.) to
153/// use \a ValueMapper directly.
155 void *pImpl;
156
157public:
159 ValueMapTypeRemapper *TypeMapper = nullptr,
160 ValueMaterializer *Materializer = nullptr,
161 const MetadataSetTy *IdentityMD = nullptr);
163 ValueMapper(const ValueMapper &) = delete;
166 ~ValueMapper();
167
168 /// Register an alternate mapping context.
169 ///
170 /// Returns a MappingContextID that can be used with the various schedule*()
171 /// API to switch in a different value map on-the-fly.
172 unsigned
174 ValueMaterializer *Materializer = nullptr);
175
176 /// Add to the current \a RemapFlags.
177 ///
178 /// \note Like the top-level mapping functions, \a addFlags() must be called
179 /// at the top level, not during a callback in a \a ValueMaterializer.
180 void addFlags(RemapFlags Flags);
181
182 Metadata *mapMetadata(const Metadata &MD);
183 MDNode *mapMDNode(const MDNode &N);
184
185 Value *mapValue(const Value &V);
187
189 void remapDbgRecord(Module *M, DbgRecord &V);
191 void remapFunction(Function &F);
193
195 unsigned MappingContextID = 0);
197 bool IsOldCtorDtor,
198 ArrayRef<Constant *> NewMembers,
199 unsigned MappingContextID = 0);
201 unsigned MappingContextID = 0);
203 unsigned MappingContextID = 0);
204 void scheduleRemapFunction(Function &F, unsigned MappingContextID = 0);
205};
206
207/// Look up or compute a value in the value map.
208///
209/// Return a mapped value for a function-local value (Argument, Instruction,
210/// BasicBlock), or compute and memoize a value for a Constant.
211///
212/// 1. If \c V is in VM, return the result.
213/// 2. Else if \c V can be materialized with \c Materializer, do so, memoize
214/// it in \c VM, and return it.
215/// 3. Else if \c V is a function-local value, return nullptr.
216/// 4. Else if \c V is a \a GlobalValue, return \c nullptr or \c V depending
217/// on \a RF_NullMapMissingGlobalValues.
218/// 5. Else if \c V is a \a MetadataAsValue wrapping a LocalAsMetadata,
219/// recurse on the local SSA value, and return nullptr or "metadata !{}" on
220/// missing depending on RF_IgnoreMissingValues.
221/// 6. Else if \c V is a \a MetadataAsValue, rewrap the return of \a
222/// MapMetadata().
223/// 7. Else, compute the equivalent constant, and return it.
224inline Value *MapValue(const Value *V, ValueToValueMapTy &VM,
225 RemapFlags Flags = RF_None,
226 ValueMapTypeRemapper *TypeMapper = nullptr,
227 ValueMaterializer *Materializer = nullptr,
228 const MetadataSetTy *IdentityMD = nullptr) {
229 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD)
230 .mapValue(*V);
231}
232
233/// Lookup or compute a mapping for a piece of metadata.
234///
235/// Compute and memoize a mapping for \c MD.
236///
237/// 1. If \c MD is mapped, return it.
238/// 2. Else if \a RF_NoModuleLevelChanges or \c MD is an \a MDString, return
239/// \c MD.
240/// 3. Else if \c MD is a \a ConstantAsMetadata, call \a MapValue() and
241/// re-wrap its return (returning nullptr on nullptr).
242/// 4. Else if \c MD is in \c IdentityMD then add an identity mapping for it
243/// and return it.
244/// 5. Else, \c MD is an \a MDNode. These are remapped, along with their
245/// transitive operands. Distinct nodes are duplicated or moved depending
246/// on \a RF_MoveDistinctNodes. Uniqued nodes are remapped like constants.
247///
248/// \note \a LocalAsMetadata is completely unsupported by \a MapMetadata.
249/// Instead, use \a MapValue() with its wrapping \a MetadataAsValue instance.
251 RemapFlags Flags = RF_None,
252 ValueMapTypeRemapper *TypeMapper = nullptr,
253 ValueMaterializer *Materializer = nullptr,
254 const MetadataSetTy *IdentityMD = nullptr) {
255 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD)
256 .mapMetadata(*MD);
257}
258
259/// Version of MapMetadata with type safety for MDNode.
261 RemapFlags Flags = RF_None,
262 ValueMapTypeRemapper *TypeMapper = nullptr,
263 ValueMaterializer *Materializer = nullptr,
264 const MetadataSetTy *IdentityMD = nullptr) {
265 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD)
266 .mapMDNode(*MD);
267}
268
269/// Convert the instruction operands from referencing the current values into
270/// those specified by VM.
271///
272/// If \a RF_IgnoreMissingLocals is set and an operand can't be found via \a
273/// MapValue(), use the old value. Otherwise assert that this doesn't happen.
274///
275/// Note that \a MapValue() only returns \c nullptr for SSA values missing from
276/// \c VM.
278 RemapFlags Flags = RF_None,
279 ValueMapTypeRemapper *TypeMapper = nullptr,
280 ValueMaterializer *Materializer = nullptr,
281 const MetadataSetTy *IdentityMD = nullptr) {
282 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD)
284}
285
286/// Remap the Values used in the DbgRecord \a DR using the value map \a
287/// VM.
289 RemapFlags Flags = RF_None,
290 ValueMapTypeRemapper *TypeMapper = nullptr,
291 ValueMaterializer *Materializer = nullptr,
292 const MetadataSetTy *IdentityMD = nullptr) {
293 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD)
294 .remapDbgRecord(M, *DR);
295}
296
297/// Remap the Values used in the DbgRecords \a Range using the value map \a
298/// VM.
302 RemapFlags Flags = RF_None,
303 ValueMapTypeRemapper *TypeMapper = nullptr,
304 ValueMaterializer *Materializer = nullptr,
305 const MetadataSetTy *IdentityMD = nullptr) {
306 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD)
308}
309
310/// Remap the operands, metadata, arguments, and instructions of a function.
311///
312/// Calls \a MapValue() on prefix data, prologue data, and personality
313/// function; calls \a MapMetadata() on each attached MDNode; remaps the
314/// argument types using the provided \c TypeMapper; and calls \a
315/// RemapInstruction() on every instruction.
317 RemapFlags Flags = RF_None,
318 ValueMapTypeRemapper *TypeMapper = nullptr,
319 ValueMaterializer *Materializer = nullptr,
320 const MetadataSetTy *IdentityMD = nullptr) {
321 ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD).remapFunction(F);
322}
323
324/// Version of MapValue with type safety for Constant.
326 RemapFlags Flags = RF_None,
327 ValueMapTypeRemapper *TypeMapper = nullptr,
328 ValueMaterializer *Materializer = nullptr,
329 const MetadataSetTy *IdentityMD = nullptr) {
330 return ValueMapper(VM, Flags, TypeMapper, Materializer, IdentityMD)
331 .mapConstant(*V);
332}
333
334} // end namespace llvm
335
336#endif // LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
RelocType Type
Definition: COFFYAML.cpp:410
dxil translate DXIL Translate Metadata
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Machine Check Debug Module
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file defines the SmallPtrSet class.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This is an important base class in LLVM.
Definition: Constant.h:42
Base class for non-instruction debug metadata records that have positions within IR.
Metadata node.
Definition: Metadata.h:1073
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2148
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This is a class that can be implemented by clients to remap types when cloning constants and instruct...
Definition: ValueMapper.h:43
virtual ~ValueMapTypeRemapper()=default
virtual Type * remapType(Type *SrcTy)=0
The client should implement this method if they want to remap types while mapping values.
Context for (re-)mapping values (and metadata).
Definition: ValueMapper.h:154
void remapDbgRecord(Module *M, DbgRecord &V)
void remapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range)
MDNode * mapMDNode(const MDNode &N)
Metadata * mapMetadata(const Metadata &MD)
ValueMapper & operator=(const ValueMapper &)=delete
void remapInstruction(Instruction &I)
ValueMapper(ValueMapper &&)=delete
void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MappingContextID=0)
void scheduleRemapFunction(Function &F, unsigned MappingContextID=0)
void scheduleMapGlobalIFunc(GlobalIFunc &GI, Constant &Resolver, unsigned MappingContextID=0)
unsigned registerAlternateMappingContext(ValueToValueMapTy &VM, ValueMaterializer *Materializer=nullptr)
Register an alternate mapping context.
ValueMapper(const ValueMapper &)=delete
void remapFunction(Function &F)
Constant * mapConstant(const Constant &C)
ValueMapper & operator=(ValueMapper &&)=delete
void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix, bool IsOldCtorDtor, ArrayRef< Constant * > NewMembers, unsigned MappingContextID=0)
void scheduleMapGlobalAlias(GlobalAlias &GA, Constant &Aliasee, unsigned MappingContextID=0)
void remapGlobalObjectMetadata(GlobalObject &GO)
Value * mapValue(const Value &V)
void addFlags(RemapFlags Flags)
Add to the current RemapFlags.
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:56
ValueMaterializer & operator=(const ValueMaterializer &)=default
virtual Value * materialize(Value *V)=0
This method can be implemented to generate a mapped Value on demand.
ValueMaterializer(const ValueMaterializer &)=default
LLVM Value Representation.
Definition: Value.h:74
A range adaptor for a pair of iterators.
typename ilist_select_iterator_type< OptionsT::has_iterator_bits, OptionsT, false, false >::type iterator
Definition: simple_ilist.h:97
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void RemapFunction(Function &F, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Remap the operands, metadata, arguments, and instructions of a function.
Definition: ValueMapper.h:316
simple_ilist< DbgRecord >::iterator DbgRecordIterator
Definition: ValueMapper.h:38
Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Lookup or compute a mapping for a piece of metadata.
Definition: ValueMapper.h:250
void RemapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Remap the Values used in the DbgRecords Range using the value map VM.
Definition: ValueMapper.h:299
RemapFlags
These are flags that the value mapping APIs allow.
Definition: ValueMapper.h:72
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:96
@ RF_NullMapMissingGlobalValues
Any global values not in value map are mapped to null instead of mapping to self.
Definition: ValueMapper.h:106
@ RF_None
Definition: ValueMapper.h:73
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:78
@ RF_ReuseAndMutateDistinctMDs
Instruct the remapper to reuse and mutate distinct metadata (remapping them in place) instead of clon...
Definition: ValueMapper.h:102
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Look up or compute a value in the value map.
Definition: ValueMapper.h:224
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
Definition: ValueMapper.h:277
void RemapDbgRecord(Module *M, DbgRecord *DR, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Remap the Values used in the DbgRecord DR using the value map VM.
Definition: ValueMapper.h:288
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
APInt operator|(APInt a, const APInt &b)
Definition: APInt.h:2112
#define N