blob: 5d1cdf7e98447ae293c0baefdda2b8638b8d3b2b [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"
Devang Patel7c368852009-07-28 21:49:47 +000021#include "llvm/Metadata.h"
Nick Lewycky29aaef82009-05-30 05:06:04 +000022#include "llvm/ADT/SmallVector.h"
Edwin Török675d5622009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Dan Gohmana5b5be12009-10-24 23:37:16 +000026Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027 Value *&VMSlot = VM[V];
28 if (VMSlot) return VMSlot; // Does it exist in the map yet?
29
30 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
31 // DenseMap. This includes any recursive calls to MapValue.
32
Devang Patel20e367a2009-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))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 return VMSlot = const_cast<Value*>(V);
37
38 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
39 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
40 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Nick Lewycky29aaef82009-05-30 05:06:04 +000041 isa<UndefValue>(C) || isa<MDString>(C))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 return VMSlot = C; // Primitive constants map directly
43 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greiff4f8d9d2008-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 Gohmana5b5be12009-10-24 23:37:16 +000046 Value *MV = MapValue(*i, VM);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000047 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 // This array must contain a reference to a global, make a new array
49 // and return it.
50 //
51 std::vector<Constant*> Values;
52 Values.reserve(CA->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000053 for (User::op_iterator j = b; j != i; ++j)
54 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 Values.push_back(cast<Constant>(MV));
56 for (++i; i != e; ++i)
Dan Gohmana5b5be12009-10-24 23:37:16 +000057 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Owen Anderson7b4f9f82009-07-28 18:32:17 +000058 return VM[V] = ConstantArray::get(CA->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 }
60 }
61 return VM[V] = C;
62
63 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greiff4f8d9d2008-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 Gohmana5b5be12009-10-24 23:37:16 +000066 Value *MV = MapValue(*i, VM);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000067 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 // This struct must contain a reference to a global, make a new struct
69 // and return it.
70 //
71 std::vector<Constant*> Values;
72 Values.reserve(CS->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000073 for (User::op_iterator j = b; j != i; ++j)
74 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 Values.push_back(cast<Constant>(MV));
76 for (++i; i != e; ++i)
Dan Gohmana5b5be12009-10-24 23:37:16 +000077 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Owen Anderson9c9f10e2009-07-27 22:29:26 +000078 return VM[V] = ConstantStruct::get(CS->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 }
80 }
81 return VM[V] = C;
82
83 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
84 std::vector<Constant*> Ops;
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000085 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
Dan Gohmana5b5be12009-10-24 23:37:16 +000086 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 return VM[V] = CE->getWithOperands(Ops);
88 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greiff4f8d9d2008-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 Gohmana5b5be12009-10-24 23:37:16 +000091 Value *MV = MapValue(*i, VM);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000092 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 // This vector value must contain a reference to a global, make a new
94 // vector constant and return it.
95 //
96 std::vector<Constant*> Values;
97 Values.reserve(CP->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000098 for (User::op_iterator j = b; j != i; ++j)
99 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 Values.push_back(cast<Constant>(MV));
101 for (++i; i != e; ++i)
Dan Gohmana5b5be12009-10-24 23:37:16 +0000102 Values.push_back(cast<Constant>(MapValue(*i, VM)));
Owen Anderson2f422e02009-07-28 21:19:26 +0000103 return VM[V] = ConstantVector::get(Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 }
105 }
106 return VM[V] = C;
107
108 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000109 llvm_unreachable("Unknown type of constant!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 }
111 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return 0;
113}
114
115/// RemapInstruction - Convert the instruction operands from referencing the
116/// current values into those specified by ValueMap.
117///
118void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000119 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohmana5b5be12009-10-24 23:37:16 +0000120 Value *V = MapValue(*op, ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000122 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 }
124}