blob: 9455e532b0e77bec50a1442907af280dbef0db12 [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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
Dan Gohman05ea54e2010-08-24 18:50:07 +000015#include "llvm/Transforms/Utils/ValueMapper.h"
Chris Lattner839a0152010-01-21 21:29:25 +000016#include "llvm/Type.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000017#include "llvm/Constants.h"
Chris Lattner1bb95912009-10-29 00:31:02 +000018#include "llvm/Function.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000019#include "llvm/Metadata.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000020#include "llvm/ADT/SmallVector.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022
Dan Gohman6cb8c232010-08-26 15:41:53 +000023Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM,
24 bool ModuleLevelChanges) {
Rafael Espindola6688c4a2010-10-13 02:08:17 +000025 TrackingVH<Value> &VMSlot = VM[V];
Devang Patel170f06e2010-06-22 23:29:55 +000026 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Chris Lattner5e665f52007-02-03 00:08:31 +000027
Devang Patel170f06e2010-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 Gohman6cb8c232010-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 Dunbar076137c2010-08-26 03:48:11 +000033 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
Dan Gohman6cb8c232010-08-26 15:41:53 +000034 (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal() &&
35 !ModuleLevelChanges))
Devang Patel170f06e2010-06-22 23:29:55 +000036 return VMSlot = const_cast<Value*>(V);
Chris Lattner5f92e2b2003-10-06 15:23:43 +000037
Chris Lattner1b3ef342010-01-21 21:05:54 +000038 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Dan Gohman6cb8c232010-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 Hernandezf58c34d2010-01-20 05:49:59 +000063 }
64
Chris Lattner77488cc2009-10-29 00:28:30 +000065 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
Dan Gohman6cb8c232010-08-26 15:41:53 +000066 if (C == 0)
67 return 0;
Chris Lattner77488cc2009-10-29 00:28:30 +000068
69 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
70 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Devang Patel1192d4d2010-07-02 21:13:23 +000071 isa<UndefValue>(C))
Devang Patel170f06e2010-06-22 23:29:55 +000072 return VMSlot = C; // Primitive constants map directly
Chris Lattner77488cc2009-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 Gohman6cb8c232010-08-26 15:41:53 +000077 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
Chris Lattner77488cc2009-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 Gohman6cb8c232010-08-26 15:41:53 +000088 Values.push_back(cast<Constant>(MapValue(*i, VM,
89 ModuleLevelChanges)));
Chris Lattner77488cc2009-10-29 00:28:30 +000090 return VM[V] = ConstantArray::get(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000091 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000092 }
Chris Lattner77488cc2009-10-29 00:28:30 +000093 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000094 }
Chris Lattner77488cc2009-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 Gohman6cb8c232010-08-26 15:41:53 +000099 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
Chris Lattner77488cc2009-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 Gohman6cb8c232010-08-26 15:41:53 +0000110 Values.push_back(cast<Constant>(MapValue(*i, VM,
111 ModuleLevelChanges)));
Chris Lattner77488cc2009-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 Gohman6cb8c232010-08-26 15:41:53 +0000121 Ops.push_back(cast<Constant>(MapValue(*i, VM, ModuleLevelChanges)));
Chris Lattner77488cc2009-10-29 00:28:30 +0000122 return VM[V] = CE->getWithOperands(Ops);
123 }
124
Chris Lattner1bb95912009-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 Lattner77488cc2009-10-29 00:28:30 +0000127 i != e; ++i) {
Dan Gohman6cb8c232010-08-26 15:41:53 +0000128 Value *MV = MapValue(*i, VM, ModuleLevelChanges);
Chris Lattner77488cc2009-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 Lattner1bb95912009-10-29 00:31:02 +0000134 Values.reserve(CV->getNumOperands());
Chris Lattner77488cc2009-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 Gohman6cb8c232010-08-26 15:41:53 +0000139 Values.push_back(cast<Constant>(MapValue(*i, VM,
140 ModuleLevelChanges)));
Chris Lattner77488cc2009-10-29 00:28:30 +0000141 return VM[V] = ConstantVector::get(Values);
142 }
143 }
144 return VM[V] = C;
Chris Lattner1bb95912009-10-29 00:31:02 +0000145 }
146
Chris Lattner1b3ef342010-01-21 21:05:54 +0000147 BlockAddress *BA = cast<BlockAddress>(C);
Dan Gohman6cb8c232010-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 Lattner1b3ef342010-01-21 21:05:54 +0000152 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000153}
Brian Gaeke6129af32004-05-19 09:08:12 +0000154
155/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +0000156/// current values into those specified by VMap.
Brian Gaeke6129af32004-05-19 09:08:12 +0000157///
Dan Gohman6cb8c232010-08-26 15:41:53 +0000158void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
159 bool ModuleLevelChanges) {
160 // Remap operands.
Gabor Greifba2c4872008-05-30 21:24:22 +0000161 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohman6cb8c232010-08-26 15:41:53 +0000162 Value *V = MapValue(*op, VMap, ModuleLevelChanges);
Brian Gaeke6129af32004-05-19 09:08:12 +0000163 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000164 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000165 }
Daniel Dunbarfd406f12010-08-26 03:48:08 +0000166
Dan Gohman6cb8c232010-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}