blob: 5d1cdf7e98447ae293c0baefdda2b8638b8d3b2b [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"
Devang Patel41d89c22009-07-27 17:17:04 +000017#include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000018#include "llvm/Constants.h"
Reid Spencer518310c2004-07-18 00:44:37 +000019#include "llvm/GlobalValue.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000020#include "llvm/Instruction.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000021#include "llvm/Metadata.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000022#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
Dan Gohman5fa75b02009-10-24 23:37:16 +000026Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
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
Devang Patel41d89c22009-07-27 17:17:04 +000033 // Global values and metadata do not need to be seeded into the ValueMap if
34 // they are using the identity mapping.
35 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(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) {
Dan Gohman5fa75b02009-10-24 23:37:16 +000046 Value *MV = MapValue(*i, VM);
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)
Dan Gohman5fa75b02009-10-24 23:37:16 +000057 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Owen Anderson1fd70962009-07-28 18:32:17 +000058 return VM[V] = ConstantArray::get(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) {
Dan Gohman5fa75b02009-10-24 23:37:16 +000066 Value *MV = MapValue(*i, VM);
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)
Dan Gohman5fa75b02009-10-24 23:37:16 +000077 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Owen Anderson8fa33382009-07-27 22:29:26 +000078 return VM[V] = ConstantStruct::get(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)
Dan Gohman5fa75b02009-10-24 23:37:16 +000086 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
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) {
Dan Gohman5fa75b02009-10-24 23:37:16 +000091 Value *MV = MapValue(*i, VM);
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)
Dan Gohman5fa75b02009-10-24 23:37:16 +0000102 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Owen Andersonaf7ec972009-07-28 21:19:26 +0000103 return VM[V] = ConstantVector::get(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
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000108 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000109 llvm_unreachable("Unknown type of constant!");
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000110 }
111 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000112 return 0;
113}
Brian Gaeke6129af32004-05-19 09:08:12 +0000114
115/// RemapInstruction - Convert the instruction operands from referencing the
116/// current values into those specified by ValueMap.
117///
Chris Lattner5e665f52007-02-03 00:08:31 +0000118void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000119 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohman5fa75b02009-10-24 23:37:16 +0000120 Value *V = MapValue(*op, ValueMap);
Brian Gaeke6129af32004-05-19 09:08:12 +0000121 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000122 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000123 }
124}