1 //===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
16 #define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
18 #include "llvm/IR/ValueMap.h"
23 typedef ValueMap<const Value *, WeakVH> ValueToValueMapTy;
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.
30 virtual ~ValueMapTypeRemapper() {}
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;
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.
43 ~ValueMaterializer() = default;
44 ValueMaterializer() = default;
45 ValueMaterializer(const ValueMaterializer&) = default;
46 ValueMaterializer &operator=(const ValueMaterializer&) = default;
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;
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
57 virtual void materializeInitFor(GlobalValue *New, GlobalValue *Old);
60 /// RemapFlags - These are flags that the value mapping APIs allow.
64 /// RF_NoModuleLevelChanges - If this flag is set, the remapper knows that
65 /// only local values within a function (such as an instruction or argument)
66 /// are mapped, not global values like functions and global metadata.
67 RF_NoModuleLevelChanges = 1,
69 /// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores
70 /// entries that are not in the value map. If it is unset, it aborts if an
71 /// operand is asked to be remapped which doesn't exist in the mapping.
72 RF_IgnoreMissingEntries = 2,
74 /// Instruct the remapper to move distinct metadata instead of duplicating
75 /// it when there are module-level changes.
76 RF_MoveDistinctMDs = 4,
78 /// Any global values not in value map are mapped to null instead of
79 /// mapping to self. Illegal if RF_IgnoreMissingEntries is also set.
80 RF_NullMapMissingGlobalValues = 8,
83 static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
84 return RemapFlags(unsigned(LHS)|unsigned(RHS));
87 Value *MapValue(const Value *V, ValueToValueMapTy &VM,
88 RemapFlags Flags = RF_None,
89 ValueMapTypeRemapper *TypeMapper = nullptr,
90 ValueMaterializer *Materializer = nullptr);
92 Metadata *MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
93 RemapFlags Flags = RF_None,
94 ValueMapTypeRemapper *TypeMapper = nullptr,
95 ValueMaterializer *Materializer = nullptr);
97 /// MapMetadata - provide versions that preserve type safety for MDNodes.
98 MDNode *MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
99 RemapFlags Flags = RF_None,
100 ValueMapTypeRemapper *TypeMapper = nullptr,
101 ValueMaterializer *Materializer = nullptr);
103 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
104 RemapFlags Flags = RF_None,
105 ValueMapTypeRemapper *TypeMapper = nullptr,
106 ValueMaterializer *Materializer = nullptr);
108 /// MapValue - provide versions that preserve type safety for Constants.
109 inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,
110 RemapFlags Flags = RF_None,
111 ValueMapTypeRemapper *TypeMapper = nullptr,
112 ValueMaterializer *Materializer = nullptr) {
113 return cast<Constant>(MapValue((const Value*)V, VM, Flags, TypeMapper,
117 } // End llvm namespace