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 class Value; 22 class Instruction; 23 typedef ValueMap<const Value *, WeakVH> ValueToValueMapTy; 24 25 /// ValueMapTypeRemapper - This is a class that can be implemented by clients 26 /// to remap types when cloning constants and instructions. 27 class ValueMapTypeRemapper { 28 virtual void anchor(); // Out of line method. 29 public: ~ValueMapTypeRemapper()30 virtual ~ValueMapTypeRemapper() {} 31 32 /// remapType - The client should implement this method if they want to 33 /// remap types while mapping values. 34 virtual Type *remapType(Type *SrcTy) = 0; 35 }; 36 37 /// ValueMaterializer - This is a class that can be implemented by clients 38 /// to materialize Values on demand. 39 class ValueMaterializer { 40 virtual void anchor(); // Out of line method. 41 42 protected: 43 ~ValueMaterializer() = default; 44 ValueMaterializer() = default; 45 ValueMaterializer(const ValueMaterializer&) = default; 46 ValueMaterializer &operator=(const ValueMaterializer&) = default; 47 48 public: 49 /// The client should implement this method if they want to generate a 50 /// mapped Value on demand. For example, if linking lazily. 51 virtual Value *materializeDeclFor(Value *V) = 0; 52 53 /// If the data being mapped is recursive, the above function can map 54 /// just the declaration and this is called to compute the initializer. 55 /// It is called after the mapping is recorded, so it doesn't need to worry 56 /// about recursion. 57 virtual void materializeInitFor(GlobalValue *New, GlobalValue *Old); 58 59 /// If the client needs to handle temporary metadata it must implement 60 /// these methods. mapTemporaryMetadata(Metadata * MD)61 virtual Metadata *mapTemporaryMetadata(Metadata *MD) { return nullptr; } replaceTemporaryMetadata(const Metadata * OrigMD,Metadata * NewMD)62 virtual void replaceTemporaryMetadata(const Metadata *OrigMD, 63 Metadata *NewMD) {} 64 65 /// The client should implement this method if some metadata need 66 /// not be mapped, for example DISubprogram metadata for functions not 67 /// linked into the destination module. isMetadataNeeded(Metadata * MD)68 virtual bool isMetadataNeeded(Metadata *MD) { return true; } 69 }; 70 71 /// RemapFlags - These are flags that the value mapping APIs allow. 72 enum RemapFlags { 73 RF_None = 0, 74 75 /// RF_NoModuleLevelChanges - If this flag is set, the remapper knows that 76 /// only local values within a function (such as an instruction or argument) 77 /// are mapped, not global values like functions and global metadata. 78 RF_NoModuleLevelChanges = 1, 79 80 /// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores 81 /// entries that are not in the value map. If it is unset, it aborts if an 82 /// operand is asked to be remapped which doesn't exist in the mapping. 83 RF_IgnoreMissingEntries = 2, 84 85 /// Instruct the remapper to move distinct metadata instead of duplicating 86 /// it when there are module-level changes. 87 RF_MoveDistinctMDs = 4, 88 89 /// Any global values not in value map are mapped to null instead of 90 /// mapping to self. Illegal if RF_IgnoreMissingEntries is also set. 91 RF_NullMapMissingGlobalValues = 8, 92 93 /// Set when there is still temporary metadata that must be handled, 94 /// such as when we are doing function importing and will materialize 95 /// and link metadata as a postpass. 96 RF_HaveUnmaterializedMetadata = 16, 97 }; 98 99 static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) { 100 return RemapFlags(unsigned(LHS)|unsigned(RHS)); 101 } 102 103 Value *MapValue(const Value *V, ValueToValueMapTy &VM, 104 RemapFlags Flags = RF_None, 105 ValueMapTypeRemapper *TypeMapper = nullptr, 106 ValueMaterializer *Materializer = nullptr); 107 108 Metadata *MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, 109 RemapFlags Flags = RF_None, 110 ValueMapTypeRemapper *TypeMapper = nullptr, 111 ValueMaterializer *Materializer = nullptr); 112 113 /// MapMetadata - provide versions that preserve type safety for MDNodes. 114 MDNode *MapMetadata(const MDNode *MD, ValueToValueMapTy &VM, 115 RemapFlags Flags = RF_None, 116 ValueMapTypeRemapper *TypeMapper = nullptr, 117 ValueMaterializer *Materializer = nullptr); 118 119 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, 120 RemapFlags Flags = RF_None, 121 ValueMapTypeRemapper *TypeMapper = nullptr, 122 ValueMaterializer *Materializer = nullptr); 123 124 /// MapValue - provide versions that preserve type safety for Constants. 125 inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM, 126 RemapFlags Flags = RF_None, 127 ValueMapTypeRemapper *TypeMapper = nullptr, 128 ValueMaterializer *Materializer = nullptr) { 129 return cast<Constant>(MapValue((const Value*)V, VM, Flags, TypeMapper, 130 Materializer)); 131 } 132 133 } // End llvm namespace 134 135 #endif 136