For PR1043:
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "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, std::map<const Value*, Value*> &VM) {
22   Value *&VMSlot = VM[V];
23   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
24
25   // Global values do not need to be seeded into the ValueMap if they are using
26   // the identity mapping.
27   if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
28     return VMSlot = const_cast<Value*>(V);
29
30   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
31     if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
32         isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
33         isa<UndefValue>(C))
34       return VMSlot = C;           // Primitive constants map directly
35     else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
36       for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
37         Value *MV = MapValue(CA->getOperand(i), VM);
38         if (MV != CA->getOperand(i)) {
39           // This array must contain a reference to a global, make a new array
40           // and return it.
41           //
42           std::vector<Constant*> Values;
43           Values.reserve(CA->getNumOperands());
44           for (unsigned j = 0; j != i; ++j)
45             Values.push_back(CA->getOperand(j));
46           Values.push_back(cast<Constant>(MV));
47           for (++i; i != e; ++i)
48             Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
49           return VMSlot = ConstantArray::get(CA->getType(), Values);
50         }
51       }
52       return VMSlot = C;
53
54     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
55       for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
56         Value *MV = MapValue(CS->getOperand(i), VM);
57         if (MV != CS->getOperand(i)) {
58           // This struct must contain a reference to a global, make a new struct
59           // and return it.
60           //
61           std::vector<Constant*> Values;
62           Values.reserve(CS->getNumOperands());
63           for (unsigned j = 0; j != i; ++j)
64             Values.push_back(CS->getOperand(j));
65           Values.push_back(cast<Constant>(MV));
66           for (++i; i != e; ++i)
67             Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
68           return VMSlot = ConstantStruct::get(CS->getType(), Values);
69         }
70       }
71       return VMSlot = C;
72
73     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
74       std::vector<Constant*> Ops;
75       for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
76         Ops.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
77       return VMSlot = CE->getWithOperands(Ops);
78     } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
79       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
80         Value *MV = MapValue(CP->getOperand(i), VM);
81         if (MV != CP->getOperand(i)) {
82           // This packed value must contain a reference to a global, make a new
83           // packed constant and return it.
84           //
85           std::vector<Constant*> Values;
86           Values.reserve(CP->getNumOperands());
87           for (unsigned j = 0; j != i; ++j)
88             Values.push_back(CP->getOperand(j));
89           Values.push_back(cast<Constant>(MV));
90           for (++i; i != e; ++i)
91             Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM)));
92           return VMSlot = ConstantPacked::get(Values);
93         }
94       }
95       return VMSlot = C;
96       
97     } else {
98       assert(0 && "Unknown type of constant!");
99     }
100   }
101
102   return 0;
103 }
104
105 /// RemapInstruction - Convert the instruction operands from referencing the
106 /// current values into those specified by ValueMap.
107 ///
108 void llvm::RemapInstruction(Instruction *I,
109                             std::map<const Value *, Value*> &ValueMap) {
110   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
111     const Value *Op = I->getOperand(op);
112     Value *V = MapValue(Op, ValueMap);
113     assert(V && "Referenced value not in value map!");
114     I->setOperand(op, V);
115   }
116 }