blob: a8f0f86ddf5757ff2df8532142692d18afca8294 [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"
Owen Anderson0a205a42009-07-05 22:41:43 +000021#include "llvm/LLVMContext.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000022#include "llvm/MDNode.h"
23#include "llvm/ADT/SmallVector.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000025using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000026
Owen Andersone922c022009-07-22 00:24:57 +000027Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000028 Value *&VMSlot = VM[V];
29 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Chris Lattner5e665f52007-02-03 00:08:31 +000030
31 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
32 // DenseMap. This includes any recursive calls to MapValue.
Misha Brukmanfd939082005-04-21 23:48:37 +000033
Devang Patel41d89c22009-07-27 17:17:04 +000034 // Global values and metadata do not need to be seeded into the ValueMap if
35 // they are using the identity mapping.
36 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V))
Chris Lattner5f92e2b2003-10-06 15:23:43 +000037 return VMSlot = const_cast<Value*>(V);
38
Chris Lattner15faa842003-04-18 03:49:49 +000039 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000040 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
Chris Lattner82731c72004-10-16 18:10:31 +000041 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Nick Lewycky7a0370f2009-05-30 05:06:04 +000042 isa<UndefValue>(C) || isa<MDString>(C))
Chris Lattner51cbcbf2002-11-20 20:47:41 +000043 return VMSlot = C; // Primitive constants map directly
Reid Spencere95ff9a2004-07-18 08:41:47 +000044 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000045 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
46 i != e; ++i) {
Owen Anderson0a205a42009-07-05 22:41:43 +000047 Value *MV = MapValue(*i, VM, Context);
Gabor Greifba2c4872008-05-30 21:24:22 +000048 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000049 // This array must contain a reference to a global, make a new array
50 // and return it.
51 //
52 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000053 Values.reserve(CA->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000054 for (User::op_iterator j = b; j != i; ++j)
55 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000056 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000057 for (++i; i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +000058 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Owen Andersone922c022009-07-22 00:24:57 +000059 return VM[V] = Context.getConstantArray(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000060 }
61 }
Chris Lattner5e665f52007-02-03 00:08:31 +000062 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000063
64 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000065 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
66 i != e; ++i) {
Owen Anderson0a205a42009-07-05 22:41:43 +000067 Value *MV = MapValue(*i, VM, Context);
Gabor Greifba2c4872008-05-30 21:24:22 +000068 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000069 // This struct must contain a reference to a global, make a new struct
70 // and return it.
71 //
72 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000073 Values.reserve(CS->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000074 for (User::op_iterator j = b; j != i; ++j)
75 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000076 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000077 for (++i; i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +000078 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Owen Andersone922c022009-07-22 00:24:57 +000079 return VM[V] = Context.getConstantStruct(CS->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000080 }
81 }
Chris Lattner5e665f52007-02-03 00:08:31 +000082 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000083
84 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner27d67212006-07-14 22:21:31 +000085 std::vector<Constant*> Ops;
Gabor Greifba2c4872008-05-30 21:24:22 +000086 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +000087 Ops.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Chris Lattner5e665f52007-02-03 00:08:31 +000088 return VM[V] = CE->getWithOperands(Ops);
Reid Spencer9d6565a2007-02-15 02:26:10 +000089 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000090 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
91 i != e; ++i) {
Owen Anderson0a205a42009-07-05 22:41:43 +000092 Value *MV = MapValue(*i, VM, Context);
Gabor Greifba2c4872008-05-30 21:24:22 +000093 if (MV != *i) {
Dan Gohman07a96762007-07-16 14:29:03 +000094 // This vector value must contain a reference to a global, make a new
95 // vector constant and return it.
Chris Lattner49aaa6a2006-03-27 05:50:18 +000096 //
97 std::vector<Constant*> Values;
98 Values.reserve(CP->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000099 for (User::op_iterator j = b; j != i; ++j)
100 Values.push_back(cast<Constant>(*j));
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000101 Values.push_back(cast<Constant>(MV));
102 for (++i; i != e; ++i)
Owen Anderson0a205a42009-07-05 22:41:43 +0000103 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Owen Andersone922c022009-07-22 00:24:57 +0000104 return VM[V] = Context.getConstantVector(Values);
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000105 }
106 }
Chris Lattnercae0a192007-02-23 19:54:30 +0000107 return VM[V] = C;
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000108
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000109 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000110 llvm_unreachable("Unknown type of constant!");
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000111 }
112 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000113 return 0;
114}
Brian Gaeke6129af32004-05-19 09:08:12 +0000115
116/// RemapInstruction - Convert the instruction operands from referencing the
117/// current values into those specified by ValueMap.
118///
Chris Lattner5e665f52007-02-03 00:08:31 +0000119void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000120 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Owen Anderson0a205a42009-07-05 22:41:43 +0000121 Value *V = MapValue(*op, ValueMap, I->getParent()->getContext());
Brian Gaeke6129af32004-05-19 09:08:12 +0000122 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000123 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000124 }
125}