blob: 2d8332f5252ab9592b9946cb19e49dfcaaa46258 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MapValue function, which is shared by various parts of
11// the lib/Transforms/Utils library.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikovad7ea242007-11-09 12:27:04 +000015#include "llvm/Transforms/Utils/ValueMapper.h"
Owen Andersona09d2342009-07-05 22:41:43 +000016#include "llvm/BasicBlock.h"
Devang Patel20e367a2009-07-27 17:17:04 +000017#include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Constants.h"
19#include "llvm/GlobalValue.h"
20#include "llvm/Instruction.h"
Owen Andersona09d2342009-07-05 22:41:43 +000021#include "llvm/LLVMContext.h"
Devang Patel7c368852009-07-28 21:49:47 +000022#include "llvm/Metadata.h"
Nick Lewycky29aaef82009-05-30 05:06:04 +000023#include "llvm/ADT/SmallVector.h"
Edwin Török675d5622009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025using namespace llvm;
26
Owen Anderson175b6542009-07-22 00:24:57 +000027Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028 Value *&VMSlot = VM[V];
29 if (VMSlot) return VMSlot; // Does it exist in the map yet?
30
31 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
32 // DenseMap. This includes any recursive calls to MapValue.
33
Devang Patel20e367a2009-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))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 return VMSlot = const_cast<Value*>(V);
38
39 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
40 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
41 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Nick Lewycky29aaef82009-05-30 05:06:04 +000042 isa<UndefValue>(C) || isa<MDString>(C))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 return VMSlot = C; // Primitive constants map directly
44 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greiff4f8d9d2008-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 Andersona09d2342009-07-05 22:41:43 +000047 Value *MV = MapValue(*i, VM, Context);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000048 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 // This array must contain a reference to a global, make a new array
50 // and return it.
51 //
52 std::vector<Constant*> Values;
53 Values.reserve(CA->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000054 for (User::op_iterator j = b; j != i; ++j)
55 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 Values.push_back(cast<Constant>(MV));
57 for (++i; i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +000058 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Owen Anderson7b4f9f82009-07-28 18:32:17 +000059 return VM[V] = ConstantArray::get(CA->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 }
61 }
62 return VM[V] = C;
63
64 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greiff4f8d9d2008-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 Andersona09d2342009-07-05 22:41:43 +000067 Value *MV = MapValue(*i, VM, Context);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000068 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 // This struct must contain a reference to a global, make a new struct
70 // and return it.
71 //
72 std::vector<Constant*> Values;
73 Values.reserve(CS->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000074 for (User::op_iterator j = b; j != i; ++j)
75 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 Values.push_back(cast<Constant>(MV));
77 for (++i; i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +000078 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Owen Anderson9c9f10e2009-07-27 22:29:26 +000079 return VM[V] = ConstantStruct::get(CS->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 }
81 }
82 return VM[V] = C;
83
84 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
85 std::vector<Constant*> Ops;
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000086 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +000087 Ops.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 return VM[V] = CE->getWithOperands(Ops);
89 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greiff4f8d9d2008-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 Andersona09d2342009-07-05 22:41:43 +000092 Value *MV = MapValue(*i, VM, Context);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000093 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 // This vector value must contain a reference to a global, make a new
95 // vector constant and return it.
96 //
97 std::vector<Constant*> Values;
98 Values.reserve(CP->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000099 for (User::op_iterator j = b; j != i; ++j)
100 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 Values.push_back(cast<Constant>(MV));
102 for (++i; i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +0000103 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Owen Anderson2f422e02009-07-28 21:19:26 +0000104 return VM[V] = ConstantVector::get(Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 }
106 }
107 return VM[V] = C;
108
109 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000110 llvm_unreachable("Unknown type of constant!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 }
112 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return 0;
114}
115
116/// RemapInstruction - Convert the instruction operands from referencing the
117/// current values into those specified by ValueMap.
118///
119void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000120 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Owen Andersona09d2342009-07-05 22:41:43 +0000121 Value *V = MapValue(*op, ValueMap, I->getParent()->getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000123 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 }
125}