Revert r173946. This breaks compilation of googletest with Clang
[oota-llvm.git] / lib / Transforms / Utils / ValueMapper.cpp
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
15 #include "llvm/Transforms/Utils/ValueMapper.h"
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
26 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
27                       ValueMapTypeRemapper *TypeMapper) {
28   ValueToValueMapTy::iterator I = VM.find(V);
29   
30   // If the value already exists in the map, use it.
31   if (I != VM.end() && I->second) return I->second;
32   
33   // Global values do not need to be seeded into the VM if they
34   // are using the identity mapping.
35   if (isa<GlobalValue>(V) || isa<MDString>(V))
36     return VM[V] = const_cast<Value*>(V);
37   
38   if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
39     // Inline asm may need *type* remapping.
40     FunctionType *NewTy = IA->getFunctionType();
41     if (TypeMapper) {
42       NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
43
44       if (NewTy != IA->getFunctionType())
45         V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
46                            IA->hasSideEffects(), IA->isAlignStack());
47     }
48     
49     return VM[V] = const_cast<Value*>(V);
50   }
51   
52
53   if (const MDNode *MD = dyn_cast<MDNode>(V)) {
54     // If this is a module-level metadata and we know that nothing at the module
55     // level is changing, then use an identity mapping.
56     if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
57       return VM[V] = const_cast<Value*>(V);
58     
59     // Create a dummy node in case we have a metadata cycle.
60     MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef<Value*>());
61     VM[V] = Dummy;
62     
63     // Check all operands to see if any need to be remapped.
64     for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
65       Value *OP = MD->getOperand(i);
66       if (OP == 0 || MapValue(OP, VM, Flags, TypeMapper) == OP) continue;
67
68       // Ok, at least one operand needs remapping.  
69       SmallVector<Value*, 4> Elts;
70       Elts.reserve(MD->getNumOperands());
71       for (i = 0; i != e; ++i) {
72         Value *Op = MD->getOperand(i);
73         Elts.push_back(Op ? MapValue(Op, VM, Flags, TypeMapper) : 0);
74       }
75       MDNode *NewMD = MDNode::get(V->getContext(), Elts);
76       Dummy->replaceAllUsesWith(NewMD);
77       VM[V] = NewMD;
78       MDNode::deleteTemporary(Dummy);
79       return NewMD;
80     }
81
82     VM[V] = const_cast<Value*>(V);
83     MDNode::deleteTemporary(Dummy);
84
85     // No operands needed remapping.  Use an identity mapping.
86     return const_cast<Value*>(V);
87   }
88
89   // Okay, this either must be a constant (which may or may not be mappable) or
90   // is something that is not in the mapping table.
91   Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
92   if (C == 0)
93     return 0;
94   
95   if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
96     Function *F = 
97       cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper));
98     BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
99                                                        Flags, TypeMapper));
100     return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
101   }
102   
103   // Otherwise, we have some other constant to remap.  Start by checking to see
104   // if all operands have an identity remapping.
105   unsigned OpNo = 0, NumOperands = C->getNumOperands();
106   Value *Mapped = 0;
107   for (; OpNo != NumOperands; ++OpNo) {
108     Value *Op = C->getOperand(OpNo);
109     Mapped = MapValue(Op, VM, Flags, TypeMapper);
110     if (Mapped != C) break;
111   }
112   
113   // See if the type mapper wants to remap the type as well.
114   Type *NewTy = C->getType();
115   if (TypeMapper)
116     NewTy = TypeMapper->remapType(NewTy);
117
118   // If the result type and all operands match up, then just insert an identity
119   // mapping.
120   if (OpNo == NumOperands && NewTy == C->getType())
121     return VM[V] = C;
122   
123   // Okay, we need to create a new constant.  We've already processed some or
124   // all of the operands, set them all up now.
125   SmallVector<Constant*, 8> Ops;
126   Ops.reserve(NumOperands);
127   for (unsigned j = 0; j != OpNo; ++j)
128     Ops.push_back(cast<Constant>(C->getOperand(j)));
129   
130   // If one of the operands mismatch, push it and the other mapped operands.
131   if (OpNo != NumOperands) {
132     Ops.push_back(cast<Constant>(Mapped));
133   
134     // Map the rest of the operands that aren't processed yet.
135     for (++OpNo; OpNo != NumOperands; ++OpNo)
136       Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
137                              Flags, TypeMapper));
138   }
139   
140   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
141     return VM[V] = CE->getWithOperands(Ops, NewTy);
142   if (isa<ConstantArray>(C))
143     return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
144   if (isa<ConstantStruct>(C))
145     return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
146   if (isa<ConstantVector>(C))
147     return VM[V] = ConstantVector::get(Ops);
148   // If this is a no-operand constant, it must be because the type was remapped.
149   if (isa<UndefValue>(C))
150     return VM[V] = UndefValue::get(NewTy);
151   if (isa<ConstantAggregateZero>(C))
152     return VM[V] = ConstantAggregateZero::get(NewTy);
153   assert(isa<ConstantPointerNull>(C));
154   return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
155 }
156
157 /// RemapInstruction - Convert the instruction operands from referencing the
158 /// current values into those specified by VMap.
159 ///
160 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
161                             RemapFlags Flags, ValueMapTypeRemapper *TypeMapper){
162   // Remap operands.
163   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
164     Value *V = MapValue(*op, VMap, Flags, TypeMapper);
165     // If we aren't ignoring missing entries, assert that something happened.
166     if (V != 0)
167       *op = V;
168     else
169       assert((Flags & RF_IgnoreMissingEntries) &&
170              "Referenced value not in value map!");
171   }
172
173   // Remap phi nodes' incoming blocks.
174   if (PHINode *PN = dyn_cast<PHINode>(I)) {
175     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
176       Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
177       // If we aren't ignoring missing entries, assert that something happened.
178       if (V != 0)
179         PN->setIncomingBlock(i, cast<BasicBlock>(V));
180       else
181         assert((Flags & RF_IgnoreMissingEntries) &&
182                "Referenced block not in value map!");
183     }
184   }
185
186   // Remap attached metadata.
187   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
188   I->getAllMetadata(MDs);
189   for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
190        MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
191     MDNode *Old = MI->second;
192     MDNode *New = MapValue(Old, VMap, Flags, TypeMapper);
193     if (New != Old)
194       I->setMetadata(MI->first, New);
195   }
196   
197   // If the instruction's type is being remapped, do so now.
198   if (TypeMapper)
199     I->mutateType(TypeMapper->remapType(I->getType()));
200 }