LLVM  4.0.0
ValueMapper.h
Go to the documentation of this file.
1 //===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MapValue interface which is used by various parts of
11 // the Transforms/Utils library to implement cloning and linking facilities.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
16 #define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
17 
18 #include "llvm/IR/ValueMap.h"
19 
20 namespace llvm {
21 
22 class Value;
25 
26 /// This is a class that can be implemented by clients to remap types when
27 /// cloning constants and instructions.
29  virtual void anchor(); // Out of line method.
30 public:
31  virtual ~ValueMapTypeRemapper() {}
32 
33  /// The client should implement this method if they want to remap types while
34  /// mapping values.
35  virtual Type *remapType(Type *SrcTy) = 0;
36 };
37 
38 /// This is a class that can be implemented by clients to materialize Values on
39 /// demand.
41  virtual void anchor(); // Out of line method.
42 
43 protected:
44  ~ValueMaterializer() = default;
45  ValueMaterializer() = default;
46  ValueMaterializer(const ValueMaterializer &) = default;
47  ValueMaterializer &operator=(const ValueMaterializer &) = default;
48 
49 public:
50  /// This method can be implemented to generate a mapped Value on demand. For
51  /// example, if linking lazily. Returns null if the value is not materialized.
52  virtual Value *materialize(Value *V) = 0;
53 };
54 
55 /// These are flags that the value mapping APIs allow.
56 enum RemapFlags {
57  RF_None = 0,
58 
59  /// If this flag is set, the remapper knows that only local values within a
60  /// function (such as an instruction or argument) are mapped, not global
61  /// values like functions and global metadata.
63 
64  /// If this flag is set, the remapper ignores missing function-local entries
65  /// (Argument, Instruction, BasicBlock) that are not in the value map. If it
66  /// is unset, it aborts if an operand is asked to be remapped which doesn't
67  /// exist in the mapping.
68  ///
69  /// There are no such assertions in MapValue(), whose results are almost
70  /// unchanged by this flag. This flag mainly changes the assertion behaviour
71  /// in RemapInstruction().
72  ///
73  /// Since an Instruction's metadata operands (even that point to SSA values)
74  /// aren't guaranteed to be dominated by their definitions, MapMetadata will
75  /// return "!{}" instead of "null" for \a LocalAsMetadata instances whose SSA
76  /// values are unmapped when this flag is set. Otherwise, \a MapValue()
77  /// completely ignores this flag.
78  ///
79  /// \a MapMetadata() always ignores this flag.
81 
82  /// Instruct the remapper to move distinct metadata instead of duplicating it
83  /// when there are module-level changes.
85 
86  /// Any global values not in value map are mapped to null instead of mapping
87  /// to self. Illegal if RF_IgnoreMissingLocals is also set.
89 };
90 
91 static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
92  return RemapFlags(unsigned(LHS) | unsigned(RHS));
93 }
94 
95 class ValueMapperImpl;
96 
97 /// Context for (re-)mapping values (and metadata).
98 ///
99 /// A shared context used for mapping and remapping of Value and Metadata
100 /// instances using \a ValueToValueMapTy, \a RemapFlags, \a
101 /// ValueMapTypeRemapper, and \a ValueMaterializer.
102 ///
103 /// There are a number of top-level entry points:
104 /// - \a mapValue() (and \a mapConstant());
105 /// - \a mapMetadata() (and \a mapMDNode());
106 /// - \a remapInstruction(); and
107 /// - \a remapFunction().
108 ///
109 /// The \a ValueMaterializer can be used as a callback, but cannot invoke any
110 /// of these top-level functions recursively. Instead, callbacks should use
111 /// one of the following to schedule work lazily in the \a ValueMapper
112 /// instance:
113 /// - \a scheduleMapGlobalInitializer()
114 /// - \a scheduleMapAppendingVariable()
115 /// - \a scheduleMapGlobalAliasee()
116 /// - \a scheduleRemapFunction()
117 ///
118 /// Sometimes a callback needs a diferent mapping context. Such a context can
119 /// be registered using \a registerAlternateMappingContext(), which takes an
120 /// alternate \a ValueToValueMapTy and \a ValueMaterializer and returns a ID to
121 /// pass into the schedule*() functions.
122 ///
123 /// TODO: lib/Linker really doesn't need the \a ValueHandle in the \a
124 /// ValueToValueMapTy. We should template \a ValueMapper (and its
125 /// implementation classes), and explicitly instantiate on two concrete
126 /// instances of \a ValueMap (one as \a ValueToValueMap, and one with raw \a
127 /// Value pointers). It may be viable to do away with \a TrackingMDRef in the
128 /// \a Metadata side map for the lib/Linker case as well, in which case we'll
129 /// need a new template parameter on \a ValueMap.
130 ///
131 /// TODO: Update callers of \a RemapInstruction() and \a MapValue() (etc.) to
132 /// use \a ValueMapper directly.
133 class ValueMapper {
134  void *pImpl;
135 
136  ValueMapper(ValueMapper &&) = delete;
137  ValueMapper(const ValueMapper &) = delete;
138  ValueMapper &operator=(ValueMapper &&) = delete;
139  ValueMapper &operator=(const ValueMapper &) = delete;
140 
141 public:
143  ValueMapTypeRemapper *TypeMapper = nullptr,
144  ValueMaterializer *Materializer = nullptr);
145  ~ValueMapper();
146 
147  /// Register an alternate mapping context.
148  ///
149  /// Returns a MappingContextID that can be used with the various schedule*()
150  /// API to switch in a different value map on-the-fly.
151  unsigned
153  ValueMaterializer *Materializer = nullptr);
154 
155  /// Add to the current \a RemapFlags.
156  ///
157  /// \note Like the top-level mapping functions, \a addFlags() must be called
158  /// at the top level, not during a callback in a \a ValueMaterializer.
159  void addFlags(RemapFlags Flags);
160 
161  Metadata *mapMetadata(const Metadata &MD);
162  MDNode *mapMDNode(const MDNode &N);
163 
164  Value *mapValue(const Value &V);
165  Constant *mapConstant(const Constant &C);
166 
168  void remapFunction(Function &F);
169 
171  unsigned MappingContextID = 0);
173  bool IsOldCtorDtor,
174  ArrayRef<Constant *> NewMembers,
175  unsigned MappingContextID = 0);
176  void scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee,
177  unsigned MappingContextID = 0);
178  void scheduleRemapFunction(Function &F, unsigned MappingContextID = 0);
179 };
180 
181 /// Look up or compute a value in the value map.
182 ///
183 /// Return a mapped value for a function-local value (Argument, Instruction,
184 /// BasicBlock), or compute and memoize a value for a Constant.
185 ///
186 /// 1. If \c V is in VM, return the result.
187 /// 2. Else if \c V can be materialized with \c Materializer, do so, memoize
188 /// it in \c VM, and return it.
189 /// 3. Else if \c V is a function-local value, return nullptr.
190 /// 4. Else if \c V is a \a GlobalValue, return \c nullptr or \c V depending
191 /// on \a RF_NullMapMissingGlobalValues.
192 /// 5. Else if \c V is a \a MetadataAsValue wrapping a LocalAsMetadata,
193 /// recurse on the local SSA value, and return nullptr or "metadata !{}" on
194 /// missing depending on RF_IgnoreMissingValues.
195 /// 6. Else if \c V is a \a MetadataAsValue, rewrap the return of \a
196 /// MapMetadata().
197 /// 7. Else, compute the equivalent constant, and return it.
198 inline Value *MapValue(const Value *V, ValueToValueMapTy &VM,
200  ValueMapTypeRemapper *TypeMapper = nullptr,
201  ValueMaterializer *Materializer = nullptr) {
202  return ValueMapper(VM, Flags, TypeMapper, Materializer).mapValue(*V);
203 }
204 
205 /// Lookup or compute a mapping for a piece of metadata.
206 ///
207 /// Compute and memoize a mapping for \c MD.
208 ///
209 /// 1. If \c MD is mapped, return it.
210 /// 2. Else if \a RF_NoModuleLevelChanges or \c MD is an \a MDString, return
211 /// \c MD.
212 /// 3. Else if \c MD is a \a ConstantAsMetadata, call \a MapValue() and
213 /// re-wrap its return (returning nullptr on nullptr).
214 /// 4. Else, \c MD is an \a MDNode. These are remapped, along with their
215 /// transitive operands. Distinct nodes are duplicated or moved depending
216 /// on \a RF_MoveDistinctNodes. Uniqued nodes are remapped like constants.
217 ///
218 /// \note \a LocalAsMetadata is completely unsupported by \a MapMetadata.
219 /// Instead, use \a MapValue() with its wrapping \a MetadataAsValue instance.
222  ValueMapTypeRemapper *TypeMapper = nullptr,
223  ValueMaterializer *Materializer = nullptr) {
224  return ValueMapper(VM, Flags, TypeMapper, Materializer).mapMetadata(*MD);
225 }
226 
227 /// Version of MapMetadata with type safety for MDNode.
230  ValueMapTypeRemapper *TypeMapper = nullptr,
231  ValueMaterializer *Materializer = nullptr) {
232  return ValueMapper(VM, Flags, TypeMapper, Materializer).mapMDNode(*MD);
233 }
234 
235 /// Convert the instruction operands from referencing the current values into
236 /// those specified by VM.
237 ///
238 /// If \a RF_IgnoreMissingLocals is set and an operand can't be found via \a
239 /// MapValue(), use the old value. Otherwise assert that this doesn't happen.
240 ///
241 /// Note that \a MapValue() only returns \c nullptr for SSA values missing from
242 /// \c VM.
245  ValueMapTypeRemapper *TypeMapper = nullptr,
246  ValueMaterializer *Materializer = nullptr) {
247  ValueMapper(VM, Flags, TypeMapper, Materializer).remapInstruction(*I);
248 }
249 
250 /// Remap the operands, metadata, arguments, and instructions of a function.
251 ///
252 /// Calls \a MapValue() on prefix data, prologue data, and personality
253 /// function; calls \a MapMetadata() on each attached MDNode; remaps the
254 /// argument types using the provided \c TypeMapper; and calls \a
255 /// RemapInstruction() on every instruction.
258  ValueMapTypeRemapper *TypeMapper = nullptr,
259  ValueMaterializer *Materializer = nullptr) {
260  ValueMapper(VM, Flags, TypeMapper, Materializer).remapFunction(F);
261 }
262 
263 /// Version of MapValue with type safety for Constant.
266  ValueMapTypeRemapper *TypeMapper = nullptr,
267  ValueMaterializer *Materializer = nullptr) {
268  return ValueMapper(VM, Flags, TypeMapper, Materializer).mapConstant(*V);
269 }
270 
271 } // End llvm namespace
272 
273 #endif
void RemapFunction(Function &F, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Remap the operands, metadata, arguments, and instructions of a function.
Definition: ValueMapper.h:256
Any global values not in value map are mapped to null instead of mapping to self. ...
Definition: ValueMapper.h:88
Metadata node.
Definition: Metadata.h:830
Context for (re-)mapping values (and metadata).
Definition: ValueMapper.h:133
void scheduleRemapFunction(Function &F, unsigned MappingContextID=0)
Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Lookup or compute a mapping for a piece of metadata.
Definition: ValueMapper.h:220
struct fuzzer::@269 Flags
void remapInstruction(Instruction &I)
Constant * mapConstant(const Constant &C)
SmallBitVector operator|(const SmallBitVector &LHS, const SmallBitVector &RHS)
Metadata * mapMetadata(const Metadata &MD)
Instruct the remapper to move distinct metadata instead of duplicating it when there are module-level...
Definition: ValueMapper.h:84
#define F(x, y, z)
Definition: MD5.cpp:51
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:62
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:40
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:42
void scheduleMapGlobalInitializer(GlobalVariable &GV, Constant &Init, unsigned MappingContextID=0)
ValueMap< const Value *, WeakVH > ValueToValueMapTy
Definition: ValueMapper.h:23
RemapFlags
These are flags that the value mapping APIs allow.
Definition: ValueMapper.h:56
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Look up or compute a value in the value map.
Definition: ValueMapper.h:198
ValueMaterializer & operator=(const ValueMaterializer &)=default
void scheduleMapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix, bool IsOldCtorDtor, ArrayRef< Constant * > NewMembers, unsigned MappingContextID=0)
void remapFunction(Function &F)
void scheduleMapGlobalAliasee(GlobalAlias &GA, Constant &Aliasee, unsigned MappingContextID=0)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM...
Definition: ValueMapper.h:243
This is a class that can be implemented by clients to remap types when cloning constants and instruct...
Definition: ValueMapper.h:28
If this flag is set, the remapper ignores missing function-local entries (Argument, Instruction, BasicBlock) that are not in the value map.
Definition: ValueMapper.h:80
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
virtual Value * materialize(Value *V)=0
This method can be implemented to generate a mapped Value on demand.
void addFlags(RemapFlags Flags)
Add to the current RemapFlags.
Value * mapValue(const Value &V)
LLVM Value Representation.
Definition: Value.h:71
unsigned registerAlternateMappingContext(ValueToValueMapTy &VM, ValueMaterializer *Materializer=nullptr)
Register an alternate mapping context.
virtual Type * remapType(Type *SrcTy)=0
The client should implement this method if they want to remap types while mapping values...
Root of the metadata hierarchy.
Definition: Metadata.h:55
MDNode * mapMDNode(const MDNode &N)