blob: a6e6701d0736535b99d96bc2f64e3502223de3c6 [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
Anton Korobeynikov344ef192007-11-09 12:27:04 +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 Gohman5fa75b02009-10-24 23:37:16 +000023Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000024 Value *&VMSlot = VM[V];
25 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Chris Lattner5e665f52007-02-03 00:08:31 +000026
27 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
28 // DenseMap. This includes any recursive calls to MapValue.
Misha Brukmanfd939082005-04-21 23:48:37 +000029
Victor Hernandezf58c34d2010-01-20 05:49:59 +000030 // Global values and non-function-local metadata do not need to be seeded into
31 // the ValueMap if they are using the identity mapping.
32 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
Chris Lattner1b3ef342010-01-21 21:05:54 +000033 (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal()))
Chris Lattner5f92e2b2003-10-06 15:23:43 +000034 return VMSlot = const_cast<Value*>(V);
35
Chris Lattner1b3ef342010-01-21 21:05:54 +000036 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Victor Hernandezacf18312010-01-20 06:56:16 +000037 SmallVector<Value*, 4> Elts;
Victor Hernandezf58c34d2010-01-20 05:49:59 +000038 for (unsigned i = 0; i != MD->getNumOperands(); i++)
Chris Lattner1b3ef342010-01-21 21:05:54 +000039 Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : 0);
Victor Hernandezf58c34d2010-01-20 05:49:59 +000040 return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size());
41 }
42
Chris Lattner77488cc2009-10-29 00:28:30 +000043 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
44 if (C == 0) return 0;
45
46 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
47 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
48 isa<UndefValue>(C) || isa<MDString>(C))
49 return VMSlot = C; // Primitive constants map directly
50
51 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
52 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
53 i != e; ++i) {
54 Value *MV = MapValue(*i, VM);
55 if (MV != *i) {
56 // This array must contain a reference to a global, make a new array
57 // and return it.
58 //
59 std::vector<Constant*> Values;
60 Values.reserve(CA->getNumOperands());
61 for (User::op_iterator j = b; j != i; ++j)
62 Values.push_back(cast<Constant>(*j));
63 Values.push_back(cast<Constant>(MV));
64 for (++i; i != e; ++i)
65 Values.push_back(cast<Constant>(MapValue(*i, VM)));
66 return VM[V] = ConstantArray::get(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000067 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000068 }
Chris Lattner77488cc2009-10-29 00:28:30 +000069 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000070 }
Chris Lattner77488cc2009-10-29 00:28:30 +000071
72 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
73 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
74 i != e; ++i) {
75 Value *MV = MapValue(*i, VM);
76 if (MV != *i) {
77 // This struct must contain a reference to a global, make a new struct
78 // and return it.
79 //
80 std::vector<Constant*> Values;
81 Values.reserve(CS->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] = ConstantStruct::get(CS->getType(), Values);
88 }
89 }
90 return VM[V] = C;
91 }
92
93 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
94 std::vector<Constant*> Ops;
95 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
96 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
97 return VM[V] = CE->getWithOperands(Ops);
98 }
99
Chris Lattner1bb95912009-10-29 00:31:02 +0000100 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
101 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
Chris Lattner77488cc2009-10-29 00:28:30 +0000102 i != e; ++i) {
103 Value *MV = MapValue(*i, VM);
104 if (MV != *i) {
105 // This vector value must contain a reference to a global, make a new
106 // vector constant and return it.
107 //
108 std::vector<Constant*> Values;
Chris Lattner1bb95912009-10-29 00:31:02 +0000109 Values.reserve(CV->getNumOperands());
Chris Lattner77488cc2009-10-29 00:28:30 +0000110 for (User::op_iterator j = b; j != i; ++j)
111 Values.push_back(cast<Constant>(*j));
112 Values.push_back(cast<Constant>(MV));
113 for (++i; i != e; ++i)
114 Values.push_back(cast<Constant>(MapValue(*i, VM)));
115 return VM[V] = ConstantVector::get(Values);
116 }
117 }
118 return VM[V] = C;
Chris Lattner1bb95912009-10-29 00:31:02 +0000119 }
120
Chris Lattner1b3ef342010-01-21 21:05:54 +0000121 BlockAddress *BA = cast<BlockAddress>(C);
122 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
123 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
124 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000125}
Brian Gaeke6129af32004-05-19 09:08:12 +0000126
127/// RemapInstruction - Convert the instruction operands from referencing the
128/// current values into those specified by ValueMap.
129///
Chris Lattner5e665f52007-02-03 00:08:31 +0000130void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000131 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohman5fa75b02009-10-24 23:37:16 +0000132 Value *V = MapValue(*op, ValueMap);
Brian Gaeke6129af32004-05-19 09:08:12 +0000133 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000134 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000135 }
136}
Devang Patelf9d5c5c2010-01-18 19:52:14 +0000137