blob: e3ef79facc73465e851b727fef3621b16162260f [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"
Devang Patel20e367a2009-07-27 17:17:04 +000016#include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty)
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 Patel410b3842010-01-18 19:52:14 +000019#include "llvm/IntrinsicInst.h"
Devang Patel7c368852009-07-28 21:49:47 +000020#include "llvm/Metadata.h"
Nick Lewycky29aaef82009-05-30 05:06:04 +000021#include "llvm/ADT/SmallVector.h"
Edwin Török675d5622009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
Dan Gohmana5b5be12009-10-24 23:37:16 +000025Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026 Value *&VMSlot = VM[V];
27 if (VMSlot) return VMSlot; // Does it exist in the map yet?
28
29 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
30 // DenseMap. This includes any recursive calls to MapValue.
31
Victor Hernandez16907c42010-01-20 05:49:59 +000032 // Global values and non-function-local metadata do not need to be seeded into
33 // the ValueMap if they are using the identity mapping.
34 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
35 (isa<MDNode>(V) && !dyn_cast<MDNode>(V)->isFunctionLocal()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 return VMSlot = const_cast<Value*>(V);
37
Victor Hernandez16907c42010-01-20 05:49:59 +000038 if (isa<MDNode>(V)) {
39 const MDNode *MD = dyn_cast<MDNode>(V);
Victor Hernandez17a02ae2010-01-20 06:56:16 +000040 SmallVector<Value*, 4> Elts;
Victor Hernandez16907c42010-01-20 05:49:59 +000041 for (unsigned i = 0; i != MD->getNumOperands(); i++)
42 Elts.push_back(MapValue(MD->getOperand(i), VM));
43 return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size());
44 }
45
Chris Lattner15513ab2009-10-29 00:28:30 +000046 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
47 if (C == 0) return 0;
48
49 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
50 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
51 isa<UndefValue>(C) || isa<MDString>(C))
52 return VMSlot = C; // Primitive constants map directly
53
54 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
55 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
56 i != e; ++i) {
57 Value *MV = MapValue(*i, VM);
58 if (MV != *i) {
59 // This array must contain a reference to a global, make a new array
60 // and return it.
61 //
62 std::vector<Constant*> Values;
63 Values.reserve(CA->getNumOperands());
64 for (User::op_iterator j = b; j != i; ++j)
65 Values.push_back(cast<Constant>(*j));
66 Values.push_back(cast<Constant>(MV));
67 for (++i; i != e; ++i)
68 Values.push_back(cast<Constant>(MapValue(*i, VM)));
69 return VM[V] = ConstantArray::get(CA->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 }
Chris Lattner15513ab2009-10-29 00:28:30 +000072 return VM[V] = C;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 }
Chris Lattner15513ab2009-10-29 00:28:30 +000074
75 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
76 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
77 i != e; ++i) {
78 Value *MV = MapValue(*i, VM);
79 if (MV != *i) {
80 // This struct must contain a reference to a global, make a new struct
81 // and return it.
82 //
83 std::vector<Constant*> Values;
84 Values.reserve(CS->getNumOperands());
85 for (User::op_iterator j = b; j != i; ++j)
86 Values.push_back(cast<Constant>(*j));
87 Values.push_back(cast<Constant>(MV));
88 for (++i; i != e; ++i)
89 Values.push_back(cast<Constant>(MapValue(*i, VM)));
90 return VM[V] = ConstantStruct::get(CS->getType(), Values);
91 }
92 }
93 return VM[V] = C;
94 }
95
96 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
97 std::vector<Constant*> Ops;
98 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
99 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
100 return VM[V] = CE->getWithOperands(Ops);
101 }
102
Chris Lattner52efc3f2009-10-29 00:31:02 +0000103 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
104 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
Chris Lattner15513ab2009-10-29 00:28:30 +0000105 i != e; ++i) {
106 Value *MV = MapValue(*i, VM);
107 if (MV != *i) {
108 // This vector value must contain a reference to a global, make a new
109 // vector constant and return it.
110 //
111 std::vector<Constant*> Values;
Chris Lattner52efc3f2009-10-29 00:31:02 +0000112 Values.reserve(CV->getNumOperands());
Chris Lattner15513ab2009-10-29 00:28:30 +0000113 for (User::op_iterator j = b; j != i; ++j)
114 Values.push_back(cast<Constant>(*j));
115 Values.push_back(cast<Constant>(MV));
116 for (++i; i != e; ++i)
117 Values.push_back(cast<Constant>(MapValue(*i, VM)));
118 return VM[V] = ConstantVector::get(Values);
119 }
120 }
121 return VM[V] = C;
Chris Lattner52efc3f2009-10-29 00:31:02 +0000122 }
123
124 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
125 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
Chris Lattner620cead2009-11-01 01:27:45 +0000126 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
127 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner15513ab2009-10-29 00:28:30 +0000128 }
129
130 llvm_unreachable("Unknown type of constant!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 return 0;
132}
133
134/// RemapInstruction - Convert the instruction operands from referencing the
135/// current values into those specified by ValueMap.
136///
137void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000138 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohmana5b5be12009-10-24 23:37:16 +0000139 Value *V = MapValue(*op, ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000141 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 }
143}
Devang Patel410b3842010-01-18 19:52:14 +0000144