blob: a6e6701d0736535b99d96bc2f64e3502223de3c6 [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"
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 Gohmana5b5be12009-10-24 23:37:16 +000023Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024 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
Victor Hernandez16907c42010-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 Lattner906bd212010-01-21 21:05:54 +000033 (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 return VMSlot = const_cast<Value*>(V);
35
Chris Lattner906bd212010-01-21 21:05:54 +000036 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Victor Hernandez17a02ae2010-01-20 06:56:16 +000037 SmallVector<Value*, 4> Elts;
Victor Hernandez16907c42010-01-20 05:49:59 +000038 for (unsigned i = 0; i != MD->getNumOperands(); i++)
Chris Lattner906bd212010-01-21 21:05:54 +000039 Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : 0);
Victor Hernandez16907c42010-01-20 05:49:59 +000040 return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size());
41 }
42
Chris Lattner15513ab2009-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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 }
Chris Lattner15513ab2009-10-29 00:28:30 +000069 return VM[V] = C;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 }
Chris Lattner15513ab2009-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 Lattner52efc3f2009-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 Lattner15513ab2009-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 Lattner52efc3f2009-10-29 00:31:02 +0000109 Values.reserve(CV->getNumOperands());
Chris Lattner15513ab2009-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 Lattner52efc3f2009-10-29 00:31:02 +0000119 }
120
Chris Lattner906bd212010-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());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125}
126
127/// RemapInstruction - Convert the instruction operands from referencing the
128/// current values into those specified by ValueMap.
129///
130void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000131 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohmana5b5be12009-10-24 23:37:16 +0000132 Value *V = MapValue(*op, ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000134 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 }
136}
Devang Patel410b3842010-01-18 19:52:14 +0000137