blob: 951d24f121ffd9bb19adb2f28544cb8008662c24 [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"
Owen Anderson0a205a42009-07-05 22:41:43 +000016#include "llvm/BasicBlock.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000017#include "llvm/Constants.h"
Reid Spencer518310c2004-07-18 00:44:37 +000018#include "llvm/GlobalValue.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000019#include "llvm/Instruction.h"
Owen Anderson0a205a42009-07-05 22:41:43 +000020#include "llvm/LLVMContext.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000021#include "llvm/MDNode.h"
22#include "llvm/ADT/SmallVector.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000024using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000025
Owen Anderson07cf79e2009-07-06 23:00:19 +000026Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext *Context) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000027 Value *&VMSlot = VM[V];
28 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Chris Lattner5e665f52007-02-03 00:08:31 +000029
30 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
31 // DenseMap. This includes any recursive calls to MapValue.
Misha Brukmanfd939082005-04-21 23:48:37 +000032
Chris Lattner5f92e2b2003-10-06 15:23:43 +000033 // Global values do not need to be seeded into the ValueMap if they are using
34 // the identity mapping.
Chris Lattner3805dea2006-04-01 23:17:11 +000035 if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
Chris Lattner5f92e2b2003-10-06 15:23:43 +000036 return VMSlot = const_cast<Value*>(V);
37
Chris Lattner15faa842003-04-18 03:49:49 +000038 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000039 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
Chris Lattner82731c72004-10-16 18:10:31 +000040 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Nick Lewycky7a0370f2009-05-30 05:06:04 +000041 isa<UndefValue>(C) || isa<MDString>(C))
Chris Lattner51cbcbf2002-11-20 20:47:41 +000042 return VMSlot = C; // Primitive constants map directly
Reid Spencere95ff9a2004-07-18 08:41:47 +000043 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000044 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
45 i != e; ++i) {
Owen Anderson0a205a42009-07-05 22:41:43 +000046 Value *MV = MapValue(*i, VM, Context);
Gabor Greifba2c4872008-05-30 21:24:22 +000047 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000048 // This array must contain a reference to a global, make a new array
49 // and return it.
50 //
51 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000052 Values.reserve(CA->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000053 for (User::op_iterator j = b; j != i; ++j)
54 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000055 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000056 for (++i; i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +000057 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
58 return VM[V] = Context->getConstantArray(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000059 }
60 }
Chris Lattner5e665f52007-02-03 00:08:31 +000061 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000062
63 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000064 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
65 i != e; ++i) {
Owen Anderson0a205a42009-07-05 22:41:43 +000066 Value *MV = MapValue(*i, VM, Context);
Gabor Greifba2c4872008-05-30 21:24:22 +000067 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000068 // This struct must contain a reference to a global, make a new struct
69 // and return it.
70 //
71 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000072 Values.reserve(CS->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000073 for (User::op_iterator j = b; j != i; ++j)
74 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000075 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000076 for (++i; i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +000077 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
78 return VM[V] = Context->getConstantStruct(CS->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000079 }
80 }
Chris Lattner5e665f52007-02-03 00:08:31 +000081 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000082
83 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner27d67212006-07-14 22:21:31 +000084 std::vector<Constant*> Ops;
Gabor Greifba2c4872008-05-30 21:24:22 +000085 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +000086 Ops.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Chris Lattner5e665f52007-02-03 00:08:31 +000087 return VM[V] = CE->getWithOperands(Ops);
Reid Spencer9d6565a2007-02-15 02:26:10 +000088 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000089 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
90 i != e; ++i) {
Owen Anderson0a205a42009-07-05 22:41:43 +000091 Value *MV = MapValue(*i, VM, Context);
Gabor Greifba2c4872008-05-30 21:24:22 +000092 if (MV != *i) {
Dan Gohman07a96762007-07-16 14:29:03 +000093 // This vector value must contain a reference to a global, make a new
94 // vector constant and return it.
Chris Lattner49aaa6a2006-03-27 05:50:18 +000095 //
96 std::vector<Constant*> Values;
97 Values.reserve(CP->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000098 for (User::op_iterator j = b; j != i; ++j)
99 Values.push_back(cast<Constant>(*j));
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000100 Values.push_back(cast<Constant>(MV));
101 for (++i; i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +0000102 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
103 return VM[V] = Context->getConstantVector(Values);
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000104 }
105 }
Chris Lattnercae0a192007-02-23 19:54:30 +0000106 return VM[V] = C;
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000107
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000108 } else if (MDNode *N = dyn_cast<MDNode>(C)) {
109 for (MDNode::const_elem_iterator b = N->elem_begin(), i = b,
110 e = N->elem_end(); i != e; ++i) {
111 if (!*i) continue;
112
Owen Anderson0a205a42009-07-05 22:41:43 +0000113 Value *MV = MapValue(*i, VM, Context);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000114 if (MV != *i) {
115 // This MDNode must contain a reference to a global, make a new MDNode
116 // and return it.
117 SmallVector<Value*, 8> Values;
118 Values.reserve(N->getNumElements());
119 for (MDNode::const_elem_iterator j = b; j != i; ++j)
120 Values.push_back(*j);
121 Values.push_back(MV);
122 for (++i; i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +0000123 Values.push_back(MapValue(*i, VM, Context));
124 return VM[V] = Context->getMDNode(Values.data(), Values.size());
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000125 }
126 }
127 return VM[V] = C;
128
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000129 } else {
Torok Edwinc25e7582009-07-11 20:10:48 +0000130 LLVM_UNREACHABLE("Unknown type of constant!");
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000131 }
132 }
Chris Lattner3cf5db72003-01-13 00:52:25 +0000133
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000134 return 0;
135}
Brian Gaeke6129af32004-05-19 09:08:12 +0000136
137/// RemapInstruction - Convert the instruction operands from referencing the
138/// current values into those specified by ValueMap.
139///
Chris Lattner5e665f52007-02-03 00:08:31 +0000140void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000141 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Owen Anderson0a205a42009-07-05 22:41:43 +0000142 Value *V = MapValue(*op, ValueMap, I->getParent()->getContext());
Brian Gaeke6129af32004-05-19 09:08:12 +0000143 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000144 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000145 }
146}