blob: 20b676d0fb8deb6579492792f4b2a1a30eeb7ed8 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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
Anton Korobeynikovad7ea242007-11-09 12:27:04 +000015#include "llvm/Transforms/Utils/ValueMapper.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/Constants.h"
17#include "llvm/GlobalValue.h"
18#include "llvm/Instruction.h"
Nick Lewycky29aaef82009-05-30 05:06:04 +000019#include "llvm/MDNode.h"
20#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
23Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
24 Value *&VMSlot = VM[V];
25 if (VMSlot) return VMSlot; // Does it exist in the map yet?
26
27 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
28 // DenseMap. This includes any recursive calls to MapValue.
29
30 // Global values do not need to be seeded into the ValueMap if they are using
31 // the identity mapping.
32 if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
33 return VMSlot = const_cast<Value*>(V);
34
35 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
36 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
37 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Nick Lewycky29aaef82009-05-30 05:06:04 +000038 isa<UndefValue>(C) || isa<MDString>(C))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 return VMSlot = C; // Primitive constants map directly
40 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000041 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
42 i != e; ++i) {
43 Value *MV = MapValue(*i, VM);
44 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 // This array must contain a reference to a global, make a new array
46 // and return it.
47 //
48 std::vector<Constant*> Values;
49 Values.reserve(CA->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000050 for (User::op_iterator j = b; j != i; ++j)
51 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 Values.push_back(cast<Constant>(MV));
53 for (++i; i != e; ++i)
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000054 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 return VM[V] = ConstantArray::get(CA->getType(), Values);
56 }
57 }
58 return VM[V] = C;
59
60 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000061 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
62 i != e; ++i) {
63 Value *MV = MapValue(*i, VM);
64 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 // This struct must contain a reference to a global, make a new struct
66 // and return it.
67 //
68 std::vector<Constant*> Values;
69 Values.reserve(CS->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000070 for (User::op_iterator j = b; j != i; ++j)
71 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 Values.push_back(cast<Constant>(MV));
73 for (++i; i != e; ++i)
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000074 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 return VM[V] = ConstantStruct::get(CS->getType(), Values);
76 }
77 }
78 return VM[V] = C;
79
80 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
81 std::vector<Constant*> Ops;
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000082 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
83 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 return VM[V] = CE->getWithOperands(Ops);
85 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000086 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
87 i != e; ++i) {
88 Value *MV = MapValue(*i, VM);
89 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 // This vector value must contain a reference to a global, make a new
91 // vector constant and return it.
92 //
93 std::vector<Constant*> Values;
94 Values.reserve(CP->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000095 for (User::op_iterator j = b; j != i; ++j)
96 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 Values.push_back(cast<Constant>(MV));
98 for (++i; i != e; ++i)
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000099 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 return VM[V] = ConstantVector::get(Values);
101 }
102 }
103 return VM[V] = C;
104
Nick Lewycky29aaef82009-05-30 05:06:04 +0000105 } else if (MDNode *N = dyn_cast<MDNode>(C)) {
106 for (MDNode::const_elem_iterator b = N->elem_begin(), i = b,
107 e = N->elem_end(); i != e; ++i) {
108 if (!*i) continue;
109
110 Value *MV = MapValue(*i, VM);
111 if (MV != *i) {
112 // This MDNode must contain a reference to a global, make a new MDNode
113 // and return it.
114 SmallVector<Value*, 8> Values;
115 Values.reserve(N->getNumElements());
116 for (MDNode::const_elem_iterator j = b; j != i; ++j)
117 Values.push_back(*j);
118 Values.push_back(MV);
119 for (++i; i != e; ++i)
120 Values.push_back(MapValue(*i, VM));
121 return VM[V] = MDNode::get(Values.data(), Values.size());
122 }
123 }
124 return VM[V] = C;
125
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 } else {
127 assert(0 && "Unknown type of constant!");
128 }
129 }
130
131 return 0;
132}
133
134/// RemapInstruction - Convert the instruction operands from referencing the
135/// current values into those specified by ValueMap.
136///
137void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000138 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
139 Value *V = MapValue(*op, ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000141 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 }
143}