blob: 1cd5cca9b6e5d56b3f6cd5f3cc7598d6ad33166e [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
Devang Patelcd9b4922010-04-20 22:18:31 +000015#include "ValueMapper.h"
Chris Lattner839a0152010-01-21 21:29:25 +000016#include "llvm/Type.h"
Devang Patelbd68d422010-07-22 16:35:00 +000017#include "llvm/GlobalAlias.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000018#include "llvm/Constants.h"
Chris Lattner1bb95912009-10-29 00:31:02 +000019#include "llvm/Function.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000020#include "llvm/Metadata.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000021#include "llvm/ADT/SmallVector.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000022using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000023
Devang Patel39430842010-04-20 22:24:18 +000024Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM) {
Devang Patel170f06e2010-06-22 23:29:55 +000025 Value *&VMSlot = VM[V];
26 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
31 // Global values and non-function-local metadata do not need to be seeded into
Devang Patel29d3dd82010-06-23 23:55:51 +000032 // the VM if they are using the identity mapping.
Devang Patelbd68d422010-07-22 16:35:00 +000033 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V))
Devang Patel170f06e2010-06-22 23:29:55 +000034 return VMSlot = const_cast<Value*>(V);
Chris Lattner5f92e2b2003-10-06 15:23:43 +000035
Chris Lattner1b3ef342010-01-21 21:05:54 +000036 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Devang Patelbd68d422010-07-22 16:35:00 +000037 Value *Dummy = new GlobalAlias(V->getType(), GlobalValue::ExternalLinkage);
38 VMSlot = Dummy;
39 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
40 Value *OP = MD->getOperand(i);
41 if (!OP) continue;
42 Value *MV = MapValue(OP, VM);
43 if (MV != OP) {
44 // This MDNode contain a reference to mapped value. Make a new
45 // MDNode and return it.
46 SmallVector<Value*, 4> Elts;
47 Elts.reserve(MD->getNumOperands());
48 for (unsigned j = 0; j != i; ++j)
49 Elts.push_back(MD->getOperand(j));
50 Elts.push_back(MV);
51 for (++i; i != e; ++i)
52 Elts.push_back(MD->getOperand(i) ?
53 MapValue(MD->getOperand(i), VM) : 0);
54 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
55 Dummy->uncheckedReplaceAllUsesWith(NewMD);
56 delete Dummy;
57 return VM[V] = NewMD;
58 }
59 }
60 delete Dummy;
61 return VM[V] = const_cast<Value*>(V);
Victor Hernandezf58c34d2010-01-20 05:49:59 +000062 }
63
Chris Lattner77488cc2009-10-29 00:28:30 +000064 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
65 if (C == 0) return 0;
66
67 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
68 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Devang Patel1192d4d2010-07-02 21:13:23 +000069 isa<UndefValue>(C))
Devang Patel170f06e2010-06-22 23:29:55 +000070 return VMSlot = C; // Primitive constants map directly
Chris Lattner77488cc2009-10-29 00:28:30 +000071
72 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
73 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
74 i != e; ++i) {
75 Value *MV = MapValue(*i, VM);
76 if (MV != *i) {
77 // This array must contain a reference to a global, make a new array
78 // and return it.
79 //
80 std::vector<Constant*> Values;
81 Values.reserve(CA->getNumOperands());
82 for (User::op_iterator j = b; j != i; ++j)
83 Values.push_back(cast<Constant>(*j));
84 Values.push_back(cast<Constant>(MV));
85 for (++i; i != e; ++i)
86 Values.push_back(cast<Constant>(MapValue(*i, VM)));
87 return VM[V] = ConstantArray::get(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000088 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000089 }
Chris Lattner77488cc2009-10-29 00:28:30 +000090 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000091 }
Chris Lattner77488cc2009-10-29 00:28:30 +000092
93 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
94 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
95 i != e; ++i) {
96 Value *MV = MapValue(*i, VM);
97 if (MV != *i) {
98 // This struct must contain a reference to a global, make a new struct
99 // and return it.
100 //
101 std::vector<Constant*> Values;
102 Values.reserve(CS->getNumOperands());
103 for (User::op_iterator j = b; j != i; ++j)
104 Values.push_back(cast<Constant>(*j));
105 Values.push_back(cast<Constant>(MV));
106 for (++i; i != e; ++i)
107 Values.push_back(cast<Constant>(MapValue(*i, VM)));
108 return VM[V] = ConstantStruct::get(CS->getType(), Values);
109 }
110 }
111 return VM[V] = C;
112 }
113
114 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
115 std::vector<Constant*> Ops;
116 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
117 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
118 return VM[V] = CE->getWithOperands(Ops);
119 }
120
Chris Lattner1bb95912009-10-29 00:31:02 +0000121 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
122 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
Chris Lattner77488cc2009-10-29 00:28:30 +0000123 i != e; ++i) {
124 Value *MV = MapValue(*i, VM);
125 if (MV != *i) {
126 // This vector value must contain a reference to a global, make a new
127 // vector constant and return it.
128 //
129 std::vector<Constant*> Values;
Chris Lattner1bb95912009-10-29 00:31:02 +0000130 Values.reserve(CV->getNumOperands());
Chris Lattner77488cc2009-10-29 00:28:30 +0000131 for (User::op_iterator j = b; j != i; ++j)
132 Values.push_back(cast<Constant>(*j));
133 Values.push_back(cast<Constant>(MV));
134 for (++i; i != e; ++i)
135 Values.push_back(cast<Constant>(MapValue(*i, VM)));
136 return VM[V] = ConstantVector::get(Values);
137 }
138 }
139 return VM[V] = C;
Chris Lattner1bb95912009-10-29 00:31:02 +0000140 }
141
Chris Lattner1b3ef342010-01-21 21:05:54 +0000142 BlockAddress *BA = cast<BlockAddress>(C);
143 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
144 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
145 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000146}
Brian Gaeke6129af32004-05-19 09:08:12 +0000147
148/// RemapInstruction - Convert the instruction operands from referencing the
Devang Patel29d3dd82010-06-23 23:55:51 +0000149/// current values into those specified by VMap.
Brian Gaeke6129af32004-05-19 09:08:12 +0000150///
Devang Patel29d3dd82010-06-23 23:55:51 +0000151void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000152 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Devang Patel29d3dd82010-06-23 23:55:51 +0000153 Value *V = MapValue(*op, VMap);
Brian Gaeke6129af32004-05-19 09:08:12 +0000154 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000155 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000156 }
157}
Devang Patelf9d5c5c2010-01-18 19:52:14 +0000158