blob: d79c21f474ebf31a3a3d42b7a0f76419d4d45f3b [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
2//
3// This file defines the MapValue function, which is shared by various parts of
4// the lib/Transforms/Utils library.
5//
6//===----------------------------------------------------------------------===//
7
8#include "ValueMapper.h"
9#include "llvm/Constants.h"
10#include "llvm/Instruction.h"
11
12Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
13 Value *&VMSlot = VM[V];
14 if (VMSlot) return VMSlot; // Does it exist in the map yet?
15
Chris Lattner5f92e2b2003-10-06 15:23:43 +000016 // Global values do not need to be seeded into the ValueMap if they are using
17 // the identity mapping.
18 if (isa<GlobalValue>(V))
19 return VMSlot = const_cast<Value*>(V);
20
Chris Lattner15faa842003-04-18 03:49:49 +000021 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
23 isa<ConstantPointerNull>(C))
24 return VMSlot = C; // Primitive constants map directly
25 else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
26 GlobalValue *MV = cast<GlobalValue>(MapValue((Value*)CPR->getValue(),VM));
27 return VMSlot = ConstantPointerRef::get(MV);
28 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
29 const std::vector<Use> &Vals = CA->getValues();
30 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
31 Value *MV = MapValue(Vals[i], VM);
32 if (MV != Vals[i]) {
33 // This array must contain a reference to a global, make a new array
34 // and return it.
35 //
36 std::vector<Constant*> Values;
37 Values.reserve(Vals.size());
38 for (unsigned j = 0; j != i; ++j)
39 Values.push_back(cast<Constant>(Vals[j]));
40 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000041 for (++i; i != e; ++i)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000042 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
43 return VMSlot = ConstantArray::get(CA->getType(), Values);
44 }
45 }
46 return VMSlot = C;
47
48 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
49 const std::vector<Use> &Vals = CS->getValues();
50 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
51 Value *MV = MapValue(Vals[i], VM);
52 if (MV != Vals[i]) {
53 // This struct must contain a reference to a global, make a new struct
54 // and return it.
55 //
56 std::vector<Constant*> Values;
57 Values.reserve(Vals.size());
58 for (unsigned j = 0; j != i; ++j)
59 Values.push_back(cast<Constant>(Vals[j]));
60 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000061 for (++i; i != e; ++i)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000062 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
63 return VMSlot = ConstantStruct::get(CS->getType(), Values);
64 }
65 }
66 return VMSlot = C;
67
68 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
69 if (CE->getOpcode() == Instruction::Cast) {
70 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
71 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
72 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
73 std::vector<Constant*> Idx;
74 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
75 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
76 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
77 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
78 } else {
79 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
80 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
81 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
82 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
83 }
84
85 } else {
86 assert(0 && "Unknown type of constant!");
87 }
88 }
Chris Lattner3cf5db72003-01-13 00:52:25 +000089
90 V->dump();
Chris Lattner51cbcbf2002-11-20 20:47:41 +000091 assert(0 && "Unknown value type: why didn't it get resolved?!");
92 return 0;
93}
94