blob: 39331d78169a36585b73dcac0ad049413f43ac13 [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"
Devang Patel41d89c22009-07-27 17:17:04 +000016#include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty)
Chris Lattner51cbcbf2002-11-20 20:47:41 +000017#include "llvm/Constants.h"
Chris Lattner1bb95912009-10-29 00:31:02 +000018#include "llvm/Function.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000019#include "llvm/Metadata.h"
Nick Lewycky7a0370f2009-05-30 05:06:04 +000020#include "llvm/ADT/SmallVector.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000022using namespace llvm;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000023
Dan Gohman5fa75b02009-10-24 23:37:16 +000024Value *llvm::MapValue(const Value *V, ValueMapTy &VM) {
Chris Lattner51cbcbf2002-11-20 20:47:41 +000025 Value *&VMSlot = VM[V];
26 if (VMSlot) return VMSlot; // Does it exist in the map yet?
Chris Lattner5e665f52007-02-03 00:08:31 +000027
28 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the
29 // DenseMap. This includes any recursive calls to MapValue.
Misha Brukmanfd939082005-04-21 23:48:37 +000030
Devang Patel41d89c22009-07-27 17:17:04 +000031 // Global values and metadata do not need to be seeded into the ValueMap if
32 // they are using the identity mapping.
33 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V))
Chris Lattner5f92e2b2003-10-06 15:23:43 +000034 return VMSlot = const_cast<Value*>(V);
35
Chris Lattner77488cc2009-10-29 00:28:30 +000036 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
37 if (C == 0) return 0;
38
39 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) ||
40 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) ||
41 isa<UndefValue>(C) || isa<MDString>(C))
42 return VMSlot = C; // Primitive constants map directly
43
44 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
45 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end();
46 i != e; ++i) {
47 Value *MV = MapValue(*i, VM);
48 if (MV != *i) {
49 // 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());
54 for (User::op_iterator j = b; j != i; ++j)
55 Values.push_back(cast<Constant>(*j));
56 Values.push_back(cast<Constant>(MV));
57 for (++i; i != e; ++i)
58 Values.push_back(cast<Constant>(MapValue(*i, VM)));
59 return VM[V] = ConstantArray::get(CA->getType(), Values);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000060 }
Chris Lattner51cbcbf2002-11-20 20:47:41 +000061 }
Chris Lattner77488cc2009-10-29 00:28:30 +000062 return VM[V] = C;
Chris Lattner51cbcbf2002-11-20 20:47:41 +000063 }
Chris Lattner77488cc2009-10-29 00:28:30 +000064
65 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
66 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end();
67 i != e; ++i) {
68 Value *MV = MapValue(*i, VM);
69 if (MV != *i) {
70 // This struct must contain a reference to a global, make a new struct
71 // and return it.
72 //
73 std::vector<Constant*> Values;
74 Values.reserve(CS->getNumOperands());
75 for (User::op_iterator j = b; j != i; ++j)
76 Values.push_back(cast<Constant>(*j));
77 Values.push_back(cast<Constant>(MV));
78 for (++i; i != e; ++i)
79 Values.push_back(cast<Constant>(MapValue(*i, VM)));
80 return VM[V] = ConstantStruct::get(CS->getType(), Values);
81 }
82 }
83 return VM[V] = C;
84 }
85
86 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
87 std::vector<Constant*> Ops;
88 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
89 Ops.push_back(cast<Constant>(MapValue(*i, VM)));
90 return VM[V] = CE->getWithOperands(Ops);
91 }
92
Chris Lattner1bb95912009-10-29 00:31:02 +000093 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
94 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end();
Chris Lattner77488cc2009-10-29 00:28:30 +000095 i != e; ++i) {
96 Value *MV = MapValue(*i, VM);
97 if (MV != *i) {
98 // This vector value must contain a reference to a global, make a new
99 // vector constant and return it.
100 //
101 std::vector<Constant*> Values;
Chris Lattner1bb95912009-10-29 00:31:02 +0000102 Values.reserve(CV->getNumOperands());
Chris Lattner77488cc2009-10-29 00:28:30 +0000103 for (User::op_iterator j = b; j != i; ++j)
104 Values.push_back(cast<Constant>(*j));
105 Values.push_back(cast<Constant>(MV));
106 for (++i; i != e; ++i)
107 Values.push_back(cast<Constant>(MapValue(*i, VM)));
108 return VM[V] = ConstantVector::get(Values);
109 }
110 }
111 return VM[V] = C;
Chris Lattner1bb95912009-10-29 00:31:02 +0000112 }
113
114 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
115 Function *F = cast<Function>(MapValue(BA->getFunction(), VM));
Chris Lattnercdfc9402009-11-01 01:27:45 +0000116 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM));
117 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
Chris Lattner77488cc2009-10-29 00:28:30 +0000118 }
119
120 llvm_unreachable("Unknown type of constant!");
Chris Lattner51cbcbf2002-11-20 20:47:41 +0000121 return 0;
122}
Brian Gaeke6129af32004-05-19 09:08:12 +0000123
124/// RemapInstruction - Convert the instruction operands from referencing the
125/// current values into those specified by ValueMap.
126///
Chris Lattner5e665f52007-02-03 00:08:31 +0000127void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) {
Gabor Greifba2c4872008-05-30 21:24:22 +0000128 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
Dan Gohman5fa75b02009-10-24 23:37:16 +0000129 Value *V = MapValue(*op, ValueMap);
Brian Gaeke6129af32004-05-19 09:08:12 +0000130 assert(V && "Referenced value not in value map!");
Gabor Greifba2c4872008-05-30 21:24:22 +0000131 *op = V;
Brian Gaeke6129af32004-05-19 09:08:12 +0000132 }
133}