While folding unconditional return move DbgRegionEndInst into the predecessor, instea...
[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/Constants.h"
17 #include "llvm/GlobalValue.h"
18 #include "llvm/Instruction.h"
19 using namespace llvm;
20
21 Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
22   Value *&VMSlot = VM[V];
23   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
24   
25   // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
26   // DenseMap.  This includes any recursive calls to MapValue.
27
28   // Global values do not need to be seeded into the ValueMap if they are using
29   // the identity mapping.
30   if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
31     return VMSlot = const_cast<Value*>(V);
32
33   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
34     if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
35         isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
36         isa<UndefValue>(C))
37       return VMSlot = C;           // Primitive constants map directly
38     else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
39       for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
40            i != e; ++i) {
41         Value *MV = MapValue(*i, VM);
42         if (MV != *i) {
43           // This array must contain a reference to a global, make a new array
44           // and return it.
45           //
46           std::vector<Constant*> Values;
47           Values.reserve(CA->getNumOperands());
48           for (User::op_iterator j = b; j != i; ++j)
49             Values.push_back(cast<Constant>(*j));
50           Values.push_back(cast<Constant>(MV));
51           for (++i; i != e; ++i)
52             Values.push_back(cast<Constant>(MapValue(*i, VM)));
53           return VM[V] = ConstantArray::get(CA->getType(), Values);
54         }
55       }
56       return VM[V] = C;
57
58     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
59       for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
60            i != e; ++i) {
61         Value *MV = MapValue(*i, VM);
62         if (MV != *i) {
63           // This struct must contain a reference to a global, make a new struct
64           // and return it.
65           //
66           std::vector<Constant*> Values;
67           Values.reserve(CS->getNumOperands());
68           for (User::op_iterator j = b; j != i; ++j)
69             Values.push_back(cast<Constant>(*j));
70           Values.push_back(cast<Constant>(MV));
71           for (++i; i != e; ++i)
72             Values.push_back(cast<Constant>(MapValue(*i, VM)));
73           return VM[V] = ConstantStruct::get(CS->getType(), Values);
74         }
75       }
76       return VM[V] = C;
77
78     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
79       std::vector<Constant*> Ops;
80       for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
81         Ops.push_back(cast<Constant>(MapValue(*i, VM)));
82       return VM[V] = CE->getWithOperands(Ops);
83     } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
84       for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
85            i != e; ++i) {
86         Value *MV = MapValue(*i, VM);
87         if (MV != *i) {
88           // This vector value must contain a reference to a global, make a new
89           // vector constant and return it.
90           //
91           std::vector<Constant*> Values;
92           Values.reserve(CP->getNumOperands());
93           for (User::op_iterator j = b; j != i; ++j)
94             Values.push_back(cast<Constant>(*j));
95           Values.push_back(cast<Constant>(MV));
96           for (++i; i != e; ++i)
97             Values.push_back(cast<Constant>(MapValue(*i, VM)));
98           return VM[V] = ConstantVector::get(Values);
99         }
100       }
101       return VM[V] = C;
102       
103     } else {
104       assert(0 && "Unknown type of constant!");
105     }
106   }
107
108   return 0;
109 }
110
111 /// RemapInstruction - Convert the instruction operands from referencing the
112 /// current values into those specified by ValueMap.
113 ///
114 void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
115   for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
116     Value *V = MapValue(*op, ValueMap);
117     assert(V && "Referenced value not in value map!");
118     *op = V;
119   }
120 }