blob: adf2c5548e88ac5b3713744b557ad9c24e2d04ef [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner51cbcbf2002-11-20 20:47:41 +00009//
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/Instruction.h"
18
19Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
20 Value *&VMSlot = VM[V];
21 if (VMSlot) return VMSlot; // Does it exist in the map yet?
22
Chris Lattner5f92e2b2003-10-06 15:23:43 +000023 // Global values do not need to be seeded into the ValueMap if they are using
24 // the identity mapping.
25 if (isa<GlobalValue>(V))
26 return VMSlot = const_cast<Value*>(V);
27
Chris Lattner15faa842003-04-18 03:49:49 +000028 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000029 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
30 isa<ConstantPointerNull>(C))
31 return VMSlot = C; // Primitive constants map directly
32 else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
33 GlobalValue *MV = cast<GlobalValue>(MapValue((Value*)CPR->getValue(),VM));
34 return VMSlot = ConstantPointerRef::get(MV);
35 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
36 const std::vector<Use> &Vals = CA->getValues();
37 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
38 Value *MV = MapValue(Vals[i], VM);
39 if (MV != Vals[i]) {
40 // This array must contain a reference to a global, make a new array
41 // and return it.
42 //
43 std::vector<Constant*> Values;
44 Values.reserve(Vals.size());
45 for (unsigned j = 0; j != i; ++j)
46 Values.push_back(cast<Constant>(Vals[j]));
47 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000048 for (++i; i != e; ++i)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000049 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
50 return VMSlot = ConstantArray::get(CA->getType(), Values);
51 }
52 }
53 return VMSlot = C;
54
55 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
56 const std::vector<Use> &Vals = CS->getValues();
57 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
58 Value *MV = MapValue(Vals[i], VM);
59 if (MV != Vals[i]) {
60 // This struct must contain a reference to a global, make a new struct
61 // and return it.
62 //
63 std::vector<Constant*> Values;
64 Values.reserve(Vals.size());
65 for (unsigned j = 0; j != i; ++j)
66 Values.push_back(cast<Constant>(Vals[j]));
67 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000068 for (++i; i != e; ++i)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000069 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
70 return VMSlot = ConstantStruct::get(CS->getType(), Values);
71 }
72 }
73 return VMSlot = C;
74
75 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
76 if (CE->getOpcode() == Instruction::Cast) {
77 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
78 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
79 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
80 std::vector<Constant*> Idx;
81 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
82 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
83 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
84 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
85 } else {
86 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
87 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
88 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
89 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
90 }
91
92 } else {
93 assert(0 && "Unknown type of constant!");
94 }
95 }
Chris Lattner3cf5db72003-01-13 00:52:25 +000096
97 V->dump();
Chris Lattner51cbcbf2002-11-20 20:47:41 +000098 assert(0 && "Unknown value type: why didn't it get resolved?!");
99 return 0;
100}
101