LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ValueMapper.cpp
Go to the documentation of this file.
1 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
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 function, which is shared by various parts of
11 // the lib/Transforms/Utils library.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/InlineAsm.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Metadata.h"
21 using namespace llvm;
22 
23 // Out of line method to get vtable etc for class.
24 void ValueMapTypeRemapper::anchor() {}
25 void ValueMaterializer::anchor() {}
26 
28  ValueMapTypeRemapper *TypeMapper,
29  ValueMaterializer *Materializer) {
31 
32  // If the value already exists in the map, use it.
33  if (I != VM.end() && I->second) return I->second;
34 
35  // If we have a materializer and it can materialize a value, use that.
36  if (Materializer) {
37  if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V)))
38  return VM[V] = NewV;
39  }
40 
41  // Global values do not need to be seeded into the VM if they
42  // are using the identity mapping.
43  if (isa<GlobalValue>(V) || isa<MDString>(V))
44  return VM[V] = const_cast<Value*>(V);
45 
46  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
47  // Inline asm may need *type* remapping.
48  FunctionType *NewTy = IA->getFunctionType();
49  if (TypeMapper) {
50  NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
51 
52  if (NewTy != IA->getFunctionType())
53  V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
54  IA->hasSideEffects(), IA->isAlignStack());
55  }
56 
57  return VM[V] = const_cast<Value*>(V);
58  }
59 
60 
61  if (const MDNode *MD = dyn_cast<MDNode>(V)) {
62  // If this is a module-level metadata and we know that nothing at the module
63  // level is changing, then use an identity mapping.
64  if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
65  return VM[V] = const_cast<Value*>(V);
66 
67  // Create a dummy node in case we have a metadata cycle.
69  VM[V] = Dummy;
70 
71  // Check all operands to see if any need to be remapped.
72  for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
73  Value *OP = MD->getOperand(i);
74  if (OP == 0) continue;
75  Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper, Materializer);
76  // Use identity map if Mapped_Op is null and we can ignore missing
77  // entries.
78  if (Mapped_OP == OP ||
79  (Mapped_OP == 0 && (Flags & RF_IgnoreMissingEntries)))
80  continue;
81 
82  // Ok, at least one operand needs remapping.
84  Elts.reserve(MD->getNumOperands());
85  for (i = 0; i != e; ++i) {
86  Value *Op = MD->getOperand(i);
87  if (Op == 0)
88  Elts.push_back(0);
89  else {
90  Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper, Materializer);
91  // Use identity map if Mapped_Op is null and we can ignore missing
92  // entries.
93  if (Mapped_Op == 0 && (Flags & RF_IgnoreMissingEntries))
94  Mapped_Op = Op;
95  Elts.push_back(Mapped_Op);
96  }
97  }
98  MDNode *NewMD = MDNode::get(V->getContext(), Elts);
99  Dummy->replaceAllUsesWith(NewMD);
100  VM[V] = NewMD;
102  return NewMD;
103  }
104 
105  VM[V] = const_cast<Value*>(V);
107 
108  // No operands needed remapping. Use an identity mapping.
109  return const_cast<Value*>(V);
110  }
111 
112  // Okay, this either must be a constant (which may or may not be mappable) or
113  // is something that is not in the mapping table.
114  Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
115  if (C == 0)
116  return 0;
117 
118  if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
119  Function *F =
120  cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
121  BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
122  Flags, TypeMapper, Materializer));
123  return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
124  }
125 
126  // Otherwise, we have some other constant to remap. Start by checking to see
127  // if all operands have an identity remapping.
128  unsigned OpNo = 0, NumOperands = C->getNumOperands();
129  Value *Mapped = 0;
130  for (; OpNo != NumOperands; ++OpNo) {
131  Value *Op = C->getOperand(OpNo);
132  Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
133  if (Mapped != C) break;
134  }
135 
136  // See if the type mapper wants to remap the type as well.
137  Type *NewTy = C->getType();
138  if (TypeMapper)
139  NewTy = TypeMapper->remapType(NewTy);
140 
141  // If the result type and all operands match up, then just insert an identity
142  // mapping.
143  if (OpNo == NumOperands && NewTy == C->getType())
144  return VM[V] = C;
145 
146  // Okay, we need to create a new constant. We've already processed some or
147  // all of the operands, set them all up now.
149  Ops.reserve(NumOperands);
150  for (unsigned j = 0; j != OpNo; ++j)
151  Ops.push_back(cast<Constant>(C->getOperand(j)));
152 
153  // If one of the operands mismatch, push it and the other mapped operands.
154  if (OpNo != NumOperands) {
155  Ops.push_back(cast<Constant>(Mapped));
156 
157  // Map the rest of the operands that aren't processed yet.
158  for (++OpNo; OpNo != NumOperands; ++OpNo)
159  Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
160  Flags, TypeMapper, Materializer));
161  }
162 
163  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
164  return VM[V] = CE->getWithOperands(Ops, NewTy);
165  if (isa<ConstantArray>(C))
166  return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
167  if (isa<ConstantStruct>(C))
168  return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
169  if (isa<ConstantVector>(C))
170  return VM[V] = ConstantVector::get(Ops);
171  // If this is a no-operand constant, it must be because the type was remapped.
172  if (isa<UndefValue>(C))
173  return VM[V] = UndefValue::get(NewTy);
174  if (isa<ConstantAggregateZero>(C))
175  return VM[V] = ConstantAggregateZero::get(NewTy);
176  assert(isa<ConstantPointerNull>(C));
177  return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
178 }
179 
180 /// RemapInstruction - Convert the instruction operands from referencing the
181 /// current values into those specified by VMap.
182 ///
185  ValueMaterializer *Materializer){
186  // Remap operands.
187  for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
188  Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
189  // If we aren't ignoring missing entries, assert that something happened.
190  if (V != 0)
191  *op = V;
192  else
193  assert((Flags & RF_IgnoreMissingEntries) &&
194  "Referenced value not in value map!");
195  }
196 
197  // Remap phi nodes' incoming blocks.
198  if (PHINode *PN = dyn_cast<PHINode>(I)) {
199  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
200  Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
201  // If we aren't ignoring missing entries, assert that something happened.
202  if (V != 0)
203  PN->setIncomingBlock(i, cast<BasicBlock>(V));
204  else
205  assert((Flags & RF_IgnoreMissingEntries) &&
206  "Referenced block not in value map!");
207  }
208  }
209 
210  // Remap attached metadata.
212  I->getAllMetadata(MDs);
213  for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
214  MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
215  MDNode *Old = MI->second;
216  MDNode *New = MapValue(Old, VMap, Flags, TypeMapper, Materializer);
217  if (New != Old)
218  I->setMetadata(MI->first, New);
219  }
220 
221  // If the instruction's type is being remapped, do so now.
222  if (TypeMapper)
223  I->mutateType(TypeMapper->remapType(I->getType()));
224 }
static void deleteTemporary(MDNode *N)
Definition: Metadata.cpp:292
static MDNode * getTemporary(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:282
void reserve(unsigned N)
Definition: SmallVector.h:425
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
unsigned getNumOperands() const
Definition: User.h:108
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1231
MDNode - a tuple of other values.
Definition: Metadata.h:69
F(f)
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
op_iterator op_begin()
Definition: User.h:116
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
Definition: ValueMapper.cpp:27
static MDNode * get(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:268
Definition: Use.h:60
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:923
iterator find(const KeyT &Val)
Definition: ValueMap.h:116
void replaceAllUsesWith(Value *V)
Definition: Value.cpp:303
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Definition: Instruction.h:155
* if(!EatIfPresent(lltok::kw_thread_local)) return false
static ConstantPointerNull * get(PointerType *T)
get() - Static factory methods - Return objects of the specified value
Definition: Constants.cpp:1314
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
static BlockAddress * get(Function *F, BasicBlock *BB)
get - Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1358
LLVM Constant Representation.
Definition: Constant.h:41
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:745
op_iterator op_end()
Definition: User.h:118
Value * getOperand(unsigned i) const
Definition: User.h:88
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:874
virtual Value * materializeValueFor(Value *V)=0
static UndefValue * get(Type *T)
Definition: Constants.cpp:1334
RemapFlags
RemapFlags - These are flags that the value mapping APIs allow.
Definition: ValueMapper.h:51
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:517
void setMetadata(unsigned KindID, MDNode *Node)
Definition: Metadata.cpp:589
iterator end()
Definition: ValueMap.h:99
See the file comment.
Definition: ValueMap.h:75
Type * getType() const
Definition: Value.h:111
#define I(x, y, z)
Definition: MD5.cpp:54
static InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT)
Definition: InlineAsm.cpp:28
void mutateType(Type *Ty)
Definition: Value.h:342
LLVM Value Representation.
Definition: Value.h:66
virtual Type * remapType(Type *SrcTy)=0
#define OP(n)
Definition: regex2.h:67