blob: fc4bde77d4f95229d77a12a2e8c982461a04baa9 [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
Dan Gohman65f6c9d2010-08-24 18:50:07 +000015#include "llvm/Transforms/Utils/ValueMapper.h"
Chris Lattner6fac4282010-01-21 21:29:25 +000016#include "llvm/Type.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Constants.h"
Chris Lattner52efc3f2009-10-29 00:31:02 +000018#include "llvm/Function.h"
Devang Patel7c368852009-07-28 21:49:47 +000019#include "llvm/Metadata.h"
Nick Lewycky29aaef82009-05-30 05:06:04 +000020#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
Dan Gohmanac8e12c2010-08-26 15:41:53 +000023Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM,
24 bool ModuleLevelChanges) {
Devang Patel77867f52010-06-22 23:29:55 +000025 Value *&VMSlot = VM[V];
26 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027
Devang Patel77867f52010-06-22 23:29:55 +000028 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
29 // DenseMap. This includes any recursive calls to MapValue.
30
Dan Gohmanac8e12c2010-08-26 15:41:53 +000031 // Global values do not need to be seeded into the VM if they
32 // are using the identity mapping.
Daniel Dunbara4d552f2010-08-26 03:48:11 +000033 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
Dan Gohmanac8e12c2010-08-26 15:41:53 +000034 (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal() &&
35 !ModuleLevelChanges))
Devang Patel77867f52010-06-22 23:29:55 +000036 return VMSlot = const_cast<Value*>(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
Chris Lattner906bd212010-01-21 21:05:54 +000038 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Dan Gohmanac8e12c2010-08-26 15:41:53 +000039 // Start by assuming that we'll use the identity mapping.
40 VMSlot = const_cast<Value*>(V);
41
42 // Check all operands to see if any need to be remapped.
43 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
44 Value *OP = MD->getOperand(i);
45 if (!OP || MapValue(OP, VM, ModuleLevelChanges) == OP) continue;
46
47 // Ok, at least one operand needs remapping.
48 MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0);
49 VM[V] = Dummy;
50 SmallVector<Value*, 4> Elts;
51 Elts.reserve(MD->getNumOperands());
52 for (i = 0; i != e; ++i)
53 Elts.push_back(MD->getOperand(i) ?
54 MapValue(MD->getOperand(i), VM, ModuleLevelChanges) : 0);
55 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
56 Dummy->replaceAllUsesWith(NewMD);
57 MDNode::deleteTemporary(Dummy);
58 return VM[V] = NewMD;
59 }
60
61 // No operands needed remapping; keep the identity map.
62 return const_cast<Value*>(V);
Victor Hernandez16907c42010-01-20 05:49:59 +000063 }
64
Chris Lattner15513ab2009-10-29 00:28:30 +000065 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Dan Gohmanac8e12c2010-08-26 15:41:53 +000066 if (C == 0)
67 return 0;
Chris Lattner15513ab2009-10-29 00:28:30 +000068
69 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
70 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Devang Patelfb6f8272010-07-02 21:13:23 +000071 isa<UndefValue>(C))
Devang Patel77867f52010-06-22 23:29:55 +000072 return VMSlot = C; // Primitive constants map directly
Chris Lattner15513ab2009-10-29 00:28:30 +000073
74 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
75 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
76 i != e; ++i) {
Dan Gohmanac8e12c2010-08-26 15:41:53 +000077 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
Chris Lattner15513ab2009-10-29 00:28:30 +000078 if (MV != *i) {
79 // This array must contain a reference to a global, make a new array
80 // and return it.
81 //
82 std::vector<Constant*> Values;
83 Values.reserve(CA->getNumOperands());
84 for (User::op_iterator j = b; j != i; ++j)
85 Values.push_back(cast<Constant>(*j));
86 Values.push_back(cast<Constant>(MV));
87 for (++i; i != e; ++i)
Dan Gohmanac8e12c2010-08-26 15:41:53 +000088 Values.push_back(cast<Constant>(MapValue(*i, VM,
89 ModuleLevelChanges)));
Chris Lattner15513ab2009-10-29 00:28:30 +000090 return VM[V] = ConstantArray::get(CA->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 }
Chris Lattner15513ab2009-10-29 00:28:30 +000093 return VM[V] = C;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 }
Chris Lattner15513ab2009-10-29 00:28:30 +000095
96 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
97 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
98 i != e; ++i) {
Dan Gohmanac8e12c2010-08-26 15:41:53 +000099 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
Chris Lattner15513ab2009-10-29 00:28:30 +0000100 if (MV != *i) {
101 // This struct must contain a reference to a global, make a new struct
102 // and return it.
103 //
104 std::vector<Constant*> Values;
105 Values.reserve(CS->getNumOperands());
106 for (User::op_iterator j = b; j != i; ++j)
107 Values.push_back(cast<Constant>(*j));
108 Values.push_back(cast<Constant>(MV));
109 for (++i; i != e; ++i)
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000110 Values.push_back(cast<Constant>(MapValue(*i, VM,
111 ModuleLevelChanges)));
Chris Lattner15513ab2009-10-29 00:28:30 +0000112 return VM[V] = ConstantStruct::get(CS->getType(), Values);
113 }
114 }
115 return VM[V] = C;
116 }
117
118 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
119 std::vector<Constant*> Ops;
120 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000121 Ops.push_back(cast<Constant>(MapValue(*i, VM, ModuleLevelChanges)));
Chris Lattner15513ab2009-10-29 00:28:30 +0000122 return VM[V] = CE->getWithOperands(Ops);
123 }
124
Chris Lattner52efc3f2009-10-29 00:31:02 +0000125 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
126 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
Chris Lattner15513ab2009-10-29 00:28:30 +0000127 i != e; ++i) {
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000128 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
Chris Lattner15513ab2009-10-29 00:28:30 +0000129 if (MV != *i) {
130 // This vector value must contain a reference to a global, make a new
131 // vector constant and return it.
132 //
133 std::vector<Constant*> Values;
Chris Lattner52efc3f2009-10-29 00:31:02 +0000134 Values.reserve(CV->getNumOperands());
Chris Lattner15513ab2009-10-29 00:28:30 +0000135 for (User::op_iterator j = b; j != i; ++j)
136 Values.push_back(cast<Constant>(*j));
137 Values.push_back(cast<Constant>(MV));
138 for (++i; i != e; ++i)
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000139 Values.push_back(cast<Constant>(MapValue(*i, VM,
140 ModuleLevelChanges)));
Chris Lattner15513ab2009-10-29 00:28:30 +0000141 return VM[V] = ConstantVector::get(Values);
142 }
143 }
144 return VM[V] = C;
Chris Lattner52efc3f2009-10-29 00:31:02 +0000145 }
146
Chris Lattner906bd212010-01-21 21:05:54 +0000147 BlockAddress *BA = cast<BlockAddress>(C);
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000148 Function *F = cast<Function>(MapValue(BA->getFunction(), VM,
149 ModuleLevelChanges));
150 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM,
151 ModuleLevelChanges));
Chris Lattner906bd212010-01-21 21:05:54 +0000152 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153}
154
155/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel24bbd612010-06-23 23:55:51 +0000156/// current values into those specified by VMap.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157///
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000158void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
159 bool ModuleLevelChanges) {
160 // Remap operands.
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000161 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000162 Value *V = MapValue(*op, VMap, ModuleLevelChanges);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000164 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 }
Daniel Dunbare8106f52010-08-26 03:48:08 +0000166
Dan Gohmanac8e12c2010-08-26 15:41:53 +0000167 // Remap attached metadata.
168 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
169 I->getAllMetadata(MDs);
170 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator
171 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) {
172 Value *Old = MI->second;
173 Value *New = MapValue(Old, VMap, ModuleLevelChanges);
174 if (New != Old)
175 I->setMetadata(MI->first, cast<MDNode>(New));
176 }
177}