factor this code better.
[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/DerivedTypes.h"  // For getNullValue(Type::Int32Ty)
17 #include "llvm/Constants.h"
18 #include "llvm/Function.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Metadata.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/ErrorHandling.h"
23 using namespace llvm;
24
25 Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
26   Value *&VMSlot = VM[V];
27   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
28   
29   // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
30   // DenseMap.  This includes any recursive calls to MapValue.
31
32   // Global values and metadata do not need to be seeded into the ValueMap if 
33   // they are using the identity mapping.
34   if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V))
35     return VMSlot = const_cast<Value*>(V);
36
37   Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
38   if (C == 0) return 0;
39   
40   if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
41       isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
42       isa<UndefValue>(C) || isa<MDString>(C))
43     return VMSlot = C;           // Primitive constants map directly
44   
45   if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
46     for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
47          i != e; ++i) {
48       Value *MV = MapValue(*i, VM);
49       if (MV != *i) {
50         // This array must contain a reference to a global, make a new array
51         // and return it.
52         //
53         std::vector<Constant*> Values;
54         Values.reserve(CA->getNumOperands());
55         for (User::op_iterator j = b; j != i; ++j)
56           Values.push_back(cast<Constant>(*j));
57         Values.push_back(cast<Constant>(MV));
58         for (++i; i != e; ++i)
59           Values.push_back(cast<Constant>(MapValue(*i, VM)));
60         return VM[V] = ConstantArray::get(CA->getType(), Values);
61       }
62     }
63     return VM[V] = C;
64   }
65   
66   if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
67     for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
68          i != e; ++i) {
69       Value *MV = MapValue(*i, VM);
70       if (MV != *i) {
71         // This struct must contain a reference to a global, make a new struct
72         // and return it.
73         //
74         std::vector<Constant*> Values;
75         Values.reserve(CS->getNumOperands());
76         for (User::op_iterator j = b; j != i; ++j)
77           Values.push_back(cast<Constant>(*j));
78         Values.push_back(cast<Constant>(MV));
79         for (++i; i != e; ++i)
80           Values.push_back(cast<Constant>(MapValue(*i, VM)));
81         return VM[V] = ConstantStruct::get(CS->getType(), Values);
82       }
83     }
84     return VM[V] = C;
85   }
86   
87   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
88     std::vector<Constant*> Ops;
89     for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
90       Ops.push_back(cast<Constant>(MapValue(*i, VM)));
91     return VM[V] = CE->getWithOperands(Ops);
92   }
93   
94   if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
95     for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
96          i != e; ++i) {
97       Value *MV = MapValue(*i, VM);
98       if (MV != *i) {
99         // This vector value must contain a reference to a global, make a new
100         // vector constant and return it.
101         //
102         std::vector<Constant*> Values;
103         Values.reserve(CV->getNumOperands());
104         for (User::op_iterator j = b; j != i; ++j)
105           Values.push_back(cast<Constant>(*j));
106         Values.push_back(cast<Constant>(MV));
107         for (++i; i != e; ++i)
108           Values.push_back(cast<Constant>(MapValue(*i, VM)));
109         return VM[V] = ConstantVector::get(Values);
110       }
111     }
112     return VM[V] = C;
113   }
114   
115   if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
116     Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
117     BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
118     return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
119   }
120   
121   llvm_unreachable("Unknown type of constant!");
122   return 0;
123 }
124
125 /// RemapInstruction - Convert the instruction operands from referencing the
126 /// current values into those specified by ValueMap.
127 ///
128 void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
129   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
130     Value *V = MapValue(*op, ValueMap);
131     assert(V && "Referenced value not in value map!");
132     *op = V;
133   }
134
135   // Map llvm.dbg.declare instruction's first operand, which points to
136   // alloca instruction through MDNode. Since MDNodes are not counted as normal
137   // uses, this will fall through cracks otherwise.
138   const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I);
139   if (!DDI) return;
140   
141   Value *AddrInsn = DDI->getAddress();
142   if (!AddrInsn) return;
143   
144   ValueMapTy::iterator VMI = ValueMap.find(AddrInsn);
145   if (VMI == ValueMap.end()) return;
146   
147   Value *Elts[] =  { VMI->second };
148   MDNode *NewAddr = MDNode::get(AddrInsn->getContext(), Elts, 1);
149   I->setOperand(1, NewAddr);
150 }
151