blob: 20b676d0fb8deb6579492792f4b2a1a30eeb7ed8 [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 Lattner51cbcbf2002-11-20 20:47:41 +000016#include "llvm/Constants.h"
Reid Spencer518310c2004-07-18 00:44:37 +000017#include "llvm/GlobalValue.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000018#include "llvm/Instruction.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000019#include "llvm/MDNode.h"
20#include "llvm/ADT/SmallVector.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000021using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022
Chris Lattner5e665f52007-02-03 00:08:31 +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
Chris Lattner5f92e2b2003-10-06 15:23:43 +000030 // Global values do not need to be seeded into the ValueMap if they are using
31 // the identity mapping.
Chris Lattner3805dea2006-04-01 23:17:11 +000032 if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
Chris Lattner5f92e2b2003-10-06 15:23:43 +000033 return VMSlot = const_cast<Value*>(V);
34
Chris Lattner15faa842003-04-18 03:49:49 +000035 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000036 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
Chris Lattner82731c72004-10-16 18:10:31 +000037 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Nick Lewycky7a0370f2009-05-30 05:06:04 +000038 isa<UndefValue>(C) || isa<MDString>(C))
Chris Lattner51cbcbf2002-11-20 20:47:41 +000039 return VMSlot = C; // Primitive constants map directly
Reid Spencere95ff9a2004-07-18 08:41:47 +000040 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000041 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
42 i != e; ++i) {
43 Value *MV = MapValue(*i, VM);
44 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000045 // This array must contain a reference to a global, make a new array
46 // and return it.
47 //
48 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000049 Values.reserve(CA->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000050 for (User::op_iterator j = b; j != i; ++j)
51 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000052 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000053 for (++i; i != e; ++i)
Gabor Greifba2c4872008-05-30 21:24:22 +000054 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Chris Lattner5e665f52007-02-03 00:08:31 +000055 return VM[V] = ConstantArray::get(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000056 }
57 }
Chris Lattner5e665f52007-02-03 00:08:31 +000058 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000059
60 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000061 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
62 i != e; ++i) {
63 Value *MV = MapValue(*i, VM);
64 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000065 // This struct must contain a reference to a global, make a new struct
66 // and return it.
67 //
68 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000069 Values.reserve(CS->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000070 for (User::op_iterator j = b; j != i; ++j)
71 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000072 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000073 for (++i; i != e; ++i)
Gabor Greifba2c4872008-05-30 21:24:22 +000074 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Chris Lattner5e665f52007-02-03 00:08:31 +000075 return VM[V] = ConstantStruct::get(CS->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000076 }
77 }
Chris Lattner5e665f52007-02-03 00:08:31 +000078 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000079
80 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner27d67212006-07-14 22:21:31 +000081 std::vector<Constant*> Ops;
Gabor Greifba2c4872008-05-30 21:24:22 +000082 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
83 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
Chris Lattner5e665f52007-02-03 00:08:31 +000084 return VM[V] = CE->getWithOperands(Ops);
Reid Spencer9d6565a2007-02-15 02:26:10 +000085 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000086 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
87 i != e; ++i) {
88 Value *MV = MapValue(*i, VM);
89 if (MV != *i) {
Dan Gohman07a96762007-07-16 14:29:03 +000090 // This vector value must contain a reference to a global, make a new
91 // vector constant and return it.
Chris Lattner49aaa6a2006-03-27 05:50:18 +000092 //
93 std::vector<Constant*> Values;
94 Values.reserve(CP->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000095 for (User::op_iterator j = b; j != i; ++j)
96 Values.push_back(cast<Constant>(*j));
Chris Lattner49aaa6a2006-03-27 05:50:18 +000097 Values.push_back(cast<Constant>(MV));
98 for (++i; i != e; ++i)
Gabor Greifba2c4872008-05-30 21:24:22 +000099 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000100 return VM[V] = ConstantVector::get(Values);
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000101 }
102 }
Chris Lattnercae0a192007-02-23 19:54:30 +0000103 return VM[V] = C;
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000104
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000105 } else if (MDNode *N = dyn_cast<MDNode>(C)) {
106 for (MDNode::const_elem_iterator b = N->elem_begin(), i = b,
107 e = N->elem_end(); i != e; ++i) {
108 if (!*i) continue;
109
110 Value *MV = MapValue(*i, VM);
111 if (MV != *i) {
112 // This MDNode must contain a reference to a global, make a new MDNode
113 // and return it.
114 SmallVector<Value*, 8> Values;
115 Values.reserve(N->getNumElements());
116 for (MDNode::const_elem_iterator j = b; j != i; ++j)
117 Values.push_back(*j);
118 Values.push_back(MV);
119 for (++i; i != e; ++i)
120 Values.push_back(MapValue(*i, VM));
121 return VM[V] = MDNode::get(Values.data(), Values.size());
122 }
123 }
124 return VM[V] = C;
125
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000126 } else {
127 assert(0 && "Unknown type of constant!");
128 }
129 }
Chris Lattner3cf5db72003-01-13 00:52:25 +0000130
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000131 return 0;
132}
Brian Gaeke6129af32004-05-19 09:08:12 +0000133
134/// RemapInstruction - Convert the instruction operands from referencing the
135/// current values into those specified by ValueMap.
136///
Chris Lattner5e665f52007-02-03 00:08:31 +0000137void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000138 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
139 Value *V = MapValue(*op, ValueMap);
Brian Gaeke6129af32004-05-19 09:08:12 +0000140 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000141 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000142 }
143}