blob: 841e661b3748b8ebb46306f0f0d5925f94d4a7e2 [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"
Chris Lattnerf7703df2004-01-09 06:12:26 +000019using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000020
Chris Lattner5e665f52007-02-03 00:08:31 +000021Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000022 Value *&VMSlot = VM[V];
23 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Chris Lattner5e665f52007-02-03 00:08:31 +000024
25 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
26 // DenseMap. This includes any recursive calls to MapValue.
Misha Brukmanfd939082005-04-21 23:48:37 +000027
Chris Lattner5f92e2b2003-10-06 15:23:43 +000028 // Global values do not need to be seeded into the ValueMap if they are using
29 // the identity mapping.
Chris Lattner3805dea2006-04-01 23:17:11 +000030 if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
Chris Lattner5f92e2b2003-10-06 15:23:43 +000031 return VMSlot = const_cast<Value*>(V);
32
Chris Lattner15faa842003-04-18 03:49:49 +000033 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000034 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
Chris Lattner82731c72004-10-16 18:10:31 +000035 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Chris Lattner83f03bf2006-05-27 01:22:24 +000036 isa<UndefValue>(C))
Chris Lattner51cbcbf2002-11-20 20:47:41 +000037 return VMSlot = C; // Primitive constants map directly
Reid Spencere95ff9a2004-07-18 08:41:47 +000038 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000039 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
40 i != e; ++i) {
41 Value *MV = MapValue(*i, VM);
42 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000043 // This array must contain a reference to a global, make a new array
44 // and return it.
45 //
46 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000047 Values.reserve(CA->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000048 for (User::op_iterator j = b; j != i; ++j)
49 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000050 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000051 for (++i; i != e; ++i)
Gabor Greifba2c4872008-05-30 21:24:22 +000052 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Chris Lattner5e665f52007-02-03 00:08:31 +000053 return VM[V] = ConstantArray::get(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000054 }
55 }
Chris Lattner5e665f52007-02-03 00:08:31 +000056 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000057
58 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000059 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
60 i != e; ++i) {
61 Value *MV = MapValue(*i, VM);
62 if (MV != *i) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000063 // This struct must contain a reference to a global, make a new struct
64 // and return it.
65 //
66 std::vector<Constant*> Values;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +000067 Values.reserve(CS->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000068 for (User::op_iterator j = b; j != i; ++j)
69 Values.push_back(cast<Constant>(*j));
Chris Lattner51cbcbf2002-11-20 20:47:41 +000070 Values.push_back(cast<Constant>(MV));
Chris Lattnerac8d4d92002-12-07 21:27:16 +000071 for (++i; i != e; ++i)
Gabor Greifba2c4872008-05-30 21:24:22 +000072 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Chris Lattner5e665f52007-02-03 00:08:31 +000073 return VM[V] = ConstantStruct::get(CS->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000074 }
75 }
Chris Lattner5e665f52007-02-03 00:08:31 +000076 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000077
78 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner27d67212006-07-14 22:21:31 +000079 std::vector<Constant*> Ops;
Gabor Greifba2c4872008-05-30 21:24:22 +000080 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
81 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
Chris Lattner5e665f52007-02-03 00:08:31 +000082 return VM[V] = CE->getWithOperands(Ops);
Reid Spencer9d6565a2007-02-15 02:26:10 +000083 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greifba2c4872008-05-30 21:24:22 +000084 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
85 i != e; ++i) {
86 Value *MV = MapValue(*i, VM);
87 if (MV != *i) {
Dan Gohman07a96762007-07-16 14:29:03 +000088 // This vector value must contain a reference to a global, make a new
89 // vector constant and return it.
Chris Lattner49aaa6a2006-03-27 05:50:18 +000090 //
91 std::vector<Constant*> Values;
92 Values.reserve(CP->getNumOperands());
Gabor Greifba2c4872008-05-30 21:24:22 +000093 for (User::op_iterator j = b; j != i; ++j)
94 Values.push_back(cast<Constant>(*j));
Chris Lattner49aaa6a2006-03-27 05:50:18 +000095 Values.push_back(cast<Constant>(MV));
96 for (++i; i != e; ++i)
Gabor Greifba2c4872008-05-30 21:24:22 +000097 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Reid Spencer9d6565a2007-02-15 02:26:10 +000098 return VM[V] = ConstantVector::get(Values);
Chris Lattner49aaa6a2006-03-27 05:50:18 +000099 }
100 }
Chris Lattnercae0a192007-02-23 19:54:30 +0000101 return VM[V] = C;
Chris Lattner49aaa6a2006-03-27 05:50:18 +0000102
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000103 } else {
104 assert(0 && "Unknown type of constant!");
105 }
106 }
Chris Lattner3cf5db72003-01-13 00:52:25 +0000107
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000108 return 0;
109}
Brian Gaeke6129af32004-05-19 09:08:12 +0000110
111/// RemapInstruction - Convert the instruction operands from referencing the
112/// current values into those specified by ValueMap.
113///
Chris Lattner5e665f52007-02-03 00:08:31 +0000114void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000115 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
116 Value *V = MapValue(*op, ValueMap);
Brian Gaeke6129af32004-05-19 09:08:12 +0000117 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000118 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000119 }
120}