blob: 16ab91000dbe539ba369d7d19dac92b85909e9d1 [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
Devang Patel22c961b2010-04-20 22:18:31 +000015#include "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
Devang Pateldc433db2010-04-20 22:24:18 +000023Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM) {
Devang Patelf163e152010-06-22 05:16:56 +000024 ValueToValueMapTy::iterator VMI = VM.find(V);
25 if (VMI != VM.end())
26 return VMI->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027
Victor Hernandez16907c42010-01-20 05:49:59 +000028 // Global values and non-function-local metadata do not need to be seeded into
29 // the ValueMap if they are using the identity mapping.
30 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) ||
Chris Lattner906bd212010-01-21 21:05:54 +000031 (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal()))
Devang Patelf163e152010-06-22 05:16:56 +000032 return VM[V] = const_cast<Value*>(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033
Chris Lattner906bd212010-01-21 21:05:54 +000034 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
Victor Hernandez17a02ae2010-01-20 06:56:16 +000035 SmallVector<Value*, 4> Elts;
Victor Hernandez3e6df522010-01-26 23:29:09 +000036 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Chris Lattner906bd212010-01-21 21:05:54 +000037 Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : 0);
Victor Hernandez16907c42010-01-20 05:49:59 +000038 return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size());
39 }
40
Chris Lattner15513ab2009-10-29 00:28:30 +000041 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
42 if (C == 0) return 0;
43
44 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
45 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
46 isa<UndefValue>(C) || isa<MDString>(C))
Devang Patelf163e152010-06-22 05:16:56 +000047 return VM[V] = C; // Primitive constants map directly
Chris Lattner15513ab2009-10-29 00:28:30 +000048
49 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
50 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
51 i != e; ++i) {
52 Value *MV = MapValue(*i, VM);
53 if (MV != *i) {
54 // This array must contain a reference to a global, make a new array
55 // and return it.
56 //
57 std::vector<Constant*> Values;
58 Values.reserve(CA->getNumOperands());
59 for (User::op_iterator j = b; j != i; ++j)
60 Values.push_back(cast<Constant>(*j));
61 Values.push_back(cast<Constant>(MV));
62 for (++i; i != e; ++i)
63 Values.push_back(cast<Constant>(MapValue(*i, VM)));
64 return VM[V] = ConstantArray::get(CA->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 }
Chris Lattner15513ab2009-10-29 00:28:30 +000067 return VM[V] = C;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 }
Chris Lattner15513ab2009-10-29 00:28:30 +000069
70 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
71 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
72 i != e; ++i) {
73 Value *MV = MapValue(*i, VM);
74 if (MV != *i) {
75 // This struct must contain a reference to a global, make a new struct
76 // and return it.
77 //
78 std::vector<Constant*> Values;
79 Values.reserve(CS->getNumOperands());
80 for (User::op_iterator j = b; j != i; ++j)
81 Values.push_back(cast<Constant>(*j));
82 Values.push_back(cast<Constant>(MV));
83 for (++i; i != e; ++i)
84 Values.push_back(cast<Constant>(MapValue(*i, VM)));
85 return VM[V] = ConstantStruct::get(CS->getType(), Values);
86 }
87 }
88 return VM[V] = C;
89 }
90
91 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
92 std::vector<Constant*> Ops;
93 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
94 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
95 return VM[V] = CE->getWithOperands(Ops);
96 }
97
Chris Lattner52efc3f2009-10-29 00:31:02 +000098 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
99 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
Chris Lattner15513ab2009-10-29 00:28:30 +0000100 i != e; ++i) {
101 Value *MV = MapValue(*i, VM);
102 if (MV != *i) {
103 // This vector value must contain a reference to a global, make a new
104 // vector constant and return it.
105 //
106 std::vector<Constant*> Values;
Chris Lattner52efc3f2009-10-29 00:31:02 +0000107 Values.reserve(CV->getNumOperands());
Chris Lattner15513ab2009-10-29 00:28:30 +0000108 for (User::op_iterator j = b; j != i; ++j)
109 Values.push_back(cast<Constant>(*j));
110 Values.push_back(cast<Constant>(MV));
111 for (++i; i != e; ++i)
112 Values.push_back(cast<Constant>(MapValue(*i, VM)));
113 return VM[V] = ConstantVector::get(Values);
114 }
115 }
116 return VM[V] = C;
Chris Lattner52efc3f2009-10-29 00:31:02 +0000117 }
118
Chris Lattner906bd212010-01-21 21:05:54 +0000119 BlockAddress *BA = cast<BlockAddress>(C);
120 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
121 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
122 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123}
124
125/// RemapInstruction - Convert the instruction operands from referencing the
126/// current values into those specified by ValueMap.
127///
Devang Pateldc433db2010-04-20 22:24:18 +0000128void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &ValueMap) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000129 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohmana5b5be12009-10-24 23:37:16 +0000130 Value *V = MapValue(*op, ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000132 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 }
134}
Devang Patel410b3842010-01-18 19:52:14 +0000135