Reinstate r133513 (reverted in r133700) with an additional fix for a
[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/Type.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Metadata.h"
21 #include "llvm/ADT/SmallVector.h"
22 using namespace llvm;
23
24 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM,
25                       RemapFlags Flags) {
26   ValueToValueMapTy::iterator I = VM.find(V);
27   
28   // If the value already exists in the map, use it.
29   if (I != VM.end() && I->second) return I->second;
30   
31   // Global values do not need to be seeded into the VM if they
32   // are using the identity mapping.
33   if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V))
34     return VM[V] = const_cast<Value*>(V);
35
36   if (const MDNode *MD = dyn_cast<MDNode>(V)) {
37     // If this is a module-level metadata and we know that nothing at the module
38     // level is changing, then use an identity mapping.
39     if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
40       return VM[V] = const_cast<Value*>(V);
41     
42     // Create a dummy node in case we have a metadata cycle.
43     MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef<Value*>());
44     VM[V] = Dummy;
45     
46     // Check all operands to see if any need to be remapped.
47     for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
48       Value *OP = MD->getOperand(i);
49       if (OP == 0 || MapValue(OP, VM, Flags) == OP) continue;
50
51       // Ok, at least one operand needs remapping.  
52       SmallVector<Value*, 4> Elts;
53       Elts.reserve(MD->getNumOperands());
54       for (i = 0; i != e; ++i) {
55         Value *Op = MD->getOperand(i);
56         Elts.push_back(Op ? MapValue(Op, VM, Flags) : 0);
57       }
58       MDNode *NewMD = MDNode::get(V->getContext(), Elts);
59       Dummy->replaceAllUsesWith(NewMD);
60       VM[V] = NewMD;
61       MDNode::deleteTemporary(Dummy);
62       return NewMD;
63     }
64
65     VM[V] = const_cast<Value*>(V);
66     MDNode::deleteTemporary(Dummy);
67
68     // No operands needed remapping.  Use an identity mapping.
69     return const_cast<Value*>(V);
70   }
71
72   // Okay, this either must be a constant (which may or may not be mappable) or
73   // is something that is not in the mapping table.
74   Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
75   if (C == 0)
76     return 0;
77   
78   if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
79     Function *F = cast<Function>(MapValue(BA->getFunction(), VM, Flags));
80     BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
81                                                        Flags));
82     return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
83   }
84   
85   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
86     Value *Op = C->getOperand(i);
87     Value *Mapped = MapValue(Op, VM, Flags);
88     if (Mapped == C) continue;
89     
90     // Okay, the operands don't all match.  We've already processed some or all
91     // of the operands, set them up now.
92     std::vector<Constant*> Ops;
93     Ops.reserve(C->getNumOperands());
94     for (unsigned j = 0; j != i; ++j)
95       Ops.push_back(cast<Constant>(C->getOperand(i)));
96     Ops.push_back(cast<Constant>(Mapped));
97     
98     // Map the rest of the operands that aren't processed yet.
99     for (++i; i != e; ++i)
100       Ops.push_back(cast<Constant>(MapValue(C->getOperand(i), VM, Flags)));
101     
102     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
103       return VM[V] = CE->getWithOperands(Ops);
104     if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
105       return VM[V] = ConstantArray::get(CA->getType(), Ops);
106     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C))
107       return VM[V] = ConstantStruct::get(CS->getType(), Ops);
108     assert(isa<ConstantVector>(C) && "Unknown mapped constant type");
109     return VM[V] = ConstantVector::get(Ops);
110   }
111
112   // If we reach here, all of the operands of the constant match.
113   return VM[V] = C;
114 }
115
116 /// RemapInstruction - Convert the instruction operands from referencing the
117 /// current values into those specified by VMap.
118 ///
119 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
120                             RemapFlags Flags) {
121   // Remap operands.
122   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
123     Value *V = MapValue(*op, VMap, Flags);
124     // If we aren't ignoring missing entries, assert that something happened.
125     if (V != 0)
126       *op = V;
127     else
128       assert((Flags & RF_IgnoreMissingEntries) &&
129              "Referenced value not in value map!");
130   }
131
132   // Remap phi nodes' incoming blocks.
133   if (PHINode *PN = dyn_cast<PHINode>(I)) {
134     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
135       Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
136       // If we aren't ignoring missing entries, assert that something happened.
137       if (V != 0)
138         PN->setIncomingBlock(i, cast<BasicBlock>(V));
139       else
140         assert((Flags & RF_IgnoreMissingEntries) &&
141                "Referenced block not in value map!");
142     }
143   }
144
145   // Remap attached metadata.
146   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
147   I->getAllMetadata(MDs);
148   for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
149        MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
150     Value *Old = MI->second;
151     Value *New = MapValue(Old, VMap, Flags);
152     if (New != Old)
153       I->setMetadata(MI->first, cast<MDNode>(New));
154   }
155 }