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/ADT/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.
42 virtual ~ValueMaterializer() {}
44 /// materializeValueFor - The client should implement this method if they
45 /// want to generate a mapped Value on demand. For example, if linking
47 virtual Value *materializeValueFor(Value *V) = 0;
50 /// RemapFlags - These are flags that the value mapping APIs allow.
54 /// RF_NoModuleLevelChanges - If this flag is set, the remapper knows that
55 /// only local values within a function (such as an instruction or argument)
56 /// are mapped, not global values like functions and global metadata.
57 RF_NoModuleLevelChanges = 1,
59 /// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores
60 /// entries that are not in the value map. If it is unset, it aborts if an
61 /// operand is asked to be remapped which doesn't exist in the mapping.
62 RF_IgnoreMissingEntries = 2
65 static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
66 return RemapFlags(unsigned(LHS)|unsigned(RHS));
69 Value *MapValue(const Value *V, ValueToValueMapTy &VM,
70 RemapFlags Flags = RF_None,
71 ValueMapTypeRemapper *TypeMapper = 0,
72 ValueMaterializer *Materializer = 0);
74 void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
75 RemapFlags Flags = RF_None,
76 ValueMapTypeRemapper *TypeMapper = 0,
77 ValueMaterializer *Materializer = 0);
79 /// MapValue - provide versions that preserve type safety for MDNode and
81 inline MDNode *MapValue(const MDNode *V, ValueToValueMapTy &VM,
82 RemapFlags Flags = RF_None,
83 ValueMapTypeRemapper *TypeMapper = 0,
84 ValueMaterializer *Materializer = 0) {
85 return cast<MDNode>(MapValue((const Value*)V, VM, Flags, TypeMapper,
88 inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,
89 RemapFlags Flags = RF_None,
90 ValueMapTypeRemapper *TypeMapper = 0,
91 ValueMaterializer *Materializer = 0) {
92 return cast<Constant>(MapValue((const Value*)V, VM, Flags, TypeMapper,
97 } // End llvm namespace