blob: 2cb6a9d221d4a9cf704de74d47b662b629e732b5 [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"
Chris Lattnerf7703df2004-01-09 06:12:26 +000018using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000019
Chris Lattnerf7703df2004-01-09 06:12:26 +000020Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000021 Value *&VMSlot = VM[V];
22 if (VMSlot) return VMSlot; // Does it exist in the map yet?
23
Chris Lattner5f92e2b2003-10-06 15:23:43 +000024 // Global values do not need to be seeded into the ValueMap if they are using
25 // the identity mapping.
26 if (isa<GlobalValue>(V))
27 return VMSlot = const_cast<Value*>(V);
28
Chris Lattner15faa842003-04-18 03:49:49 +000029 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000030 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
Chris Lattnerde512b52004-02-15 05:55:15 +000031 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C))
Chris Lattner51cbcbf2002-11-20 20:47:41 +000032 return VMSlot = C; // Primitive constants map directly
33 else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
34 GlobalValue *MV = cast<GlobalValue>(MapValue((Value*)CPR->getValue(),VM));
35 return VMSlot = ConstantPointerRef::get(MV);
36 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
37 const std::vector<Use> &Vals = CA->getValues();
38 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
39 Value *MV = MapValue(Vals[i], VM);
40 if (MV != Vals[i]) {
41 // This array must contain a reference to a global, make a new array
42 // and return it.
43 //
44 std::vector<Constant*> Values;
45 Values.reserve(Vals.size());
46 for (unsigned j = 0; j != i; ++j)
47 Values.push_back(cast<Constant>(Vals[j]));
48 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000049 for (++i; i != e; ++i)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000050 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
51 return VMSlot = ConstantArray::get(CA->getType(), Values);
52 }
53 }
54 return VMSlot = C;
55
56 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
57 const std::vector<Use> &Vals = CS->getValues();
58 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
59 Value *MV = MapValue(Vals[i], VM);
60 if (MV != Vals[i]) {
61 // This struct must contain a reference to a global, make a new struct
62 // and return it.
63 //
64 std::vector<Constant*> Values;
65 Values.reserve(Vals.size());
66 for (unsigned j = 0; j != i; ++j)
67 Values.push_back(cast<Constant>(Vals[j]));
68 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000069 for (++i; i != e; ++i)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000070 Values.push_back(cast<Constant>(MapValue(Vals[i], VM)));
71 return VMSlot = ConstantStruct::get(CS->getType(), Values);
72 }
73 }
74 return VMSlot = C;
75
76 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
77 if (CE->getOpcode() == Instruction::Cast) {
78 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
79 return VMSlot = ConstantExpr::getCast(MV, CE->getType());
80 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
81 std::vector<Constant*> Idx;
82 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
83 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
84 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
85 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
86 } else {
87 assert(CE->getNumOperands() == 2 && "Must be binary operator?");
88 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
89 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
90 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
91 }
92
93 } else {
94 assert(0 && "Unknown type of constant!");
95 }
96 }
Chris Lattner3cf5db72003-01-13 00:52:25 +000097
98 V->dump();
Chris Lattner51cbcbf2002-11-20 20:47:41 +000099 assert(0 && "Unknown value type: why didn't it get resolved?!");
100 return 0;
101}