blob: 21b3a220c30a841f8c42e93d0c3c6932173ed475 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Constants.h"
18#include "llvm/GlobalValue.h"
19#include "llvm/Instruction.h"
Owen Andersona09d2342009-07-05 22:41:43 +000020#include "llvm/LLVMContext.h"
Nick Lewycky29aaef82009-05-30 05:06:04 +000021#include "llvm/MDNode.h"
22#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
Owen Anderson5349f052009-07-06 23:00:19 +000025Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext *Context) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026 Value *&VMSlot = VM[V];
27 if (VMSlot) return VMSlot; // Does it exist in the map yet?
28
29 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
30 // DenseMap. This includes any recursive calls to MapValue.
31
32 // Global values do not need to be seeded into the ValueMap if they are using
33 // the identity mapping.
34 if (isa<GlobalValue>(V) || isa<InlineAsm>(V))
35 return VMSlot = const_cast<Value*>(V);
36
37 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
38 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
39 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
Nick Lewycky29aaef82009-05-30 05:06:04 +000040 isa<UndefValue>(C) || isa<MDString>(C))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 return VMSlot = C; // Primitive constants map directly
42 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000043 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
44 i != e; ++i) {
Owen Andersona09d2342009-07-05 22:41:43 +000045 Value *MV = MapValue(*i, VM, Context);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000046 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 // This array must contain a reference to a global, make a new array
48 // and return it.
49 //
50 std::vector<Constant*> Values;
51 Values.reserve(CA->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000052 for (User::op_iterator j = b; j != i; ++j)
53 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 Values.push_back(cast<Constant>(MV));
55 for (++i; i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +000056 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
57 return VM[V] = Context->getConstantArray(CA->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 }
59 }
60 return VM[V] = C;
61
62 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000063 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
64 i != e; ++i) {
Owen Andersona09d2342009-07-05 22:41:43 +000065 Value *MV = MapValue(*i, VM, Context);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000066 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 // This struct must contain a reference to a global, make a new struct
68 // and return it.
69 //
70 std::vector<Constant*> Values;
71 Values.reserve(CS->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000072 for (User::op_iterator j = b; j != i; ++j)
73 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 Values.push_back(cast<Constant>(MV));
75 for (++i; i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +000076 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
77 return VM[V] = Context->getConstantStruct(CS->getType(), Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 }
79 }
80 return VM[V] = C;
81
82 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
83 std::vector<Constant*> Ops;
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000084 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +000085 Ops.push_back(cast<Constant>(MapValue(*i, VM, Context)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 return VM[V] = CE->getWithOperands(Ops);
87 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000088 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end();
89 i != e; ++i) {
Owen Andersona09d2342009-07-05 22:41:43 +000090 Value *MV = MapValue(*i, VM, Context);
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000091 if (MV != *i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 // This vector value must contain a reference to a global, make a new
93 // vector constant and return it.
94 //
95 std::vector<Constant*> Values;
96 Values.reserve(CP->getNumOperands());
Gabor Greiff4f8d9d2008-05-30 21:24:22 +000097 for (User::op_iterator j = b; j != i; ++j)
98 Values.push_back(cast<Constant>(*j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 Values.push_back(cast<Constant>(MV));
100 for (++i; i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +0000101 Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
102 return VM[V] = Context->getConstantVector(Values);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 }
104 }
105 return VM[V] = C;
106
Nick Lewycky29aaef82009-05-30 05:06:04 +0000107 } else if (MDNode *N = dyn_cast<MDNode>(C)) {
108 for (MDNode::const_elem_iterator b = N->elem_begin(), i = b,
109 e = N->elem_end(); i != e; ++i) {
110 if (!*i) continue;
111
Owen Andersona09d2342009-07-05 22:41:43 +0000112 Value *MV = MapValue(*i, VM, Context);
Nick Lewycky29aaef82009-05-30 05:06:04 +0000113 if (MV != *i) {
114 // This MDNode must contain a reference to a global, make a new MDNode
115 // and return it.
116 SmallVector<Value*, 8> Values;
117 Values.reserve(N->getNumElements());
118 for (MDNode::const_elem_iterator j = b; j != i; ++j)
119 Values.push_back(*j);
120 Values.push_back(MV);
121 for (++i; i != e; ++i)
Owen Andersona09d2342009-07-05 22:41:43 +0000122 Values.push_back(MapValue(*i, VM, Context));
123 return VM[V] = Context->getMDNode(Values.data(), Values.size());
Nick Lewycky29aaef82009-05-30 05:06:04 +0000124 }
125 }
126 return VM[V] = C;
127
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 } else {
129 assert(0 && "Unknown type of constant!");
130 }
131 }
132
133 return 0;
134}
135
136/// RemapInstruction - Convert the instruction operands from referencing the
137/// current values into those specified by ValueMap.
138///
139void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000140 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Owen Andersona09d2342009-07-05 22:41:43 +0000141 Value *V = MapValue(*op, ValueMap, I->getParent()->getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 assert(V && "Referenced value not in value map!");
Gabor Greiff4f8d9d2008-05-30 21:24:22 +0000143 *op = V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 }
145}