17ce4f4ab73dda73284d7ecf19bc86da5f26ee4d
[oota-llvm.git] / include / llvm / Transforms / Utils / ValueMapper.h
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:
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     /// materializeValueFor - The client should implement this method if they
50     /// want to generate a mapped Value on demand. For example, if linking
51     /// lazily.
52     virtual Value *materializeValueFor(Value *V) = 0;
53   };
54
55   /// RemapFlags - These are flags that the value mapping APIs allow.
56   enum RemapFlags {
57     RF_None = 0,
58
59     /// RF_NoModuleLevelChanges - If this flag is set, the remapper knows that
60     /// only local values within a function (such as an instruction or argument)
61     /// are mapped, not global values like functions and global metadata.
62     RF_NoModuleLevelChanges = 1,
63
64     /// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores
65     /// entries that are not in the value map.  If it is unset, it aborts if an
66     /// operand is asked to be remapped which doesn't exist in the mapping.
67     RF_IgnoreMissingEntries = 2,
68
69     /// Instruct the remapper to move distinct metadata instead of duplicating
70     /// it when there are module-level changes.
71     RF_MoveDistinctMDs = 4,
72
73     /// Any global values not in value map are mapped to null instead of
74     /// mapping to self. Illegal if RF_IgnoreMissingEntries is also set.
75     RF_NullMapMissingGlobalValues = 8,
76   };
77
78   static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
79     return RemapFlags(unsigned(LHS)|unsigned(RHS));
80   }
81
82   Value *MapValue(const Value *V, ValueToValueMapTy &VM,
83                   RemapFlags Flags = RF_None,
84                   ValueMapTypeRemapper *TypeMapper = nullptr,
85                   ValueMaterializer *Materializer = nullptr);
86
87   Metadata *MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
88                         RemapFlags Flags = RF_None,
89                         ValueMapTypeRemapper *TypeMapper = nullptr,
90                         ValueMaterializer *Materializer = nullptr);
91
92   /// MapMetadata - provide versions that preserve type safety for MDNodes.
93   MDNode *MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
94                       RemapFlags Flags = RF_None,
95                       ValueMapTypeRemapper *TypeMapper = nullptr,
96                       ValueMaterializer *Materializer = nullptr);
97
98   void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
99                         RemapFlags Flags = RF_None,
100                         ValueMapTypeRemapper *TypeMapper = nullptr,
101                         ValueMaterializer *Materializer = nullptr);
102
103   /// MapValue - provide versions that preserve type safety for Constants.
104   inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,
105                             RemapFlags Flags = RF_None,
106                             ValueMapTypeRemapper *TypeMapper = nullptr,
107                             ValueMaterializer *Materializer = nullptr) {
108     return cast<Constant>(MapValue((const Value*)V, VM, Flags, TypeMapper,
109                                    Materializer));
110   }
111
112 } // End llvm namespace
113
114 #endif