Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 1 | //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the MapValue function, which is shared by various parts of |
| 11 | // the lib/Transforms/Utils library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dan Gohman | 05ea54e | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Chris Lattner | 839a015 | 2010-01-21 21:29:25 +0000 | [diff] [blame] | 16 | #include "llvm/Type.h" |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 1bb9591 | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Devang Patel | 0a9f7b9 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 19 | #include "llvm/Metadata.h" |
Nick Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 22 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 23 | Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, |
| 24 | bool ModuleLevelChanges) { |
Rafael Espindola | 6688c4a | 2010-10-13 02:08:17 +0000 | [diff] [blame^] | 25 | TrackingVH<Value> &VMSlot = VM[V]; |
Devang Patel | 170f06e | 2010-06-22 23:29:55 +0000 | [diff] [blame] | 26 | if (VMSlot) return VMSlot; // Does it exist in the map yet? |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 27 | |
Devang Patel | 170f06e | 2010-06-22 23:29:55 +0000 | [diff] [blame] | 28 | // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the |
| 29 | // DenseMap. This includes any recursive calls to MapValue. |
| 30 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 31 | // Global values do not need to be seeded into the VM if they |
| 32 | // are using the identity mapping. |
Daniel Dunbar | 076137c | 2010-08-26 03:48:11 +0000 | [diff] [blame] | 33 | if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) || |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 34 | (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal() && |
| 35 | !ModuleLevelChanges)) |
Devang Patel | 170f06e | 2010-06-22 23:29:55 +0000 | [diff] [blame] | 36 | return VMSlot = const_cast<Value*>(V); |
Chris Lattner | 5f92e2b | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 1b3ef34 | 2010-01-21 21:05:54 +0000 | [diff] [blame] | 38 | if (const MDNode *MD = dyn_cast<MDNode>(V)) { |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 39 | // Start by assuming that we'll use the identity mapping. |
| 40 | VMSlot = const_cast<Value*>(V); |
| 41 | |
| 42 | // Check all operands to see if any need to be remapped. |
| 43 | for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) { |
| 44 | Value *OP = MD->getOperand(i); |
| 45 | if (!OP || MapValue(OP, VM, ModuleLevelChanges) == OP) continue; |
| 46 | |
| 47 | // Ok, at least one operand needs remapping. |
| 48 | MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0); |
| 49 | VM[V] = Dummy; |
| 50 | SmallVector<Value*, 4> Elts; |
| 51 | Elts.reserve(MD->getNumOperands()); |
| 52 | for (i = 0; i != e; ++i) |
| 53 | Elts.push_back(MD->getOperand(i) ? |
| 54 | MapValue(MD->getOperand(i), VM, ModuleLevelChanges) : 0); |
| 55 | MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size()); |
| 56 | Dummy->replaceAllUsesWith(NewMD); |
| 57 | MDNode::deleteTemporary(Dummy); |
| 58 | return VM[V] = NewMD; |
| 59 | } |
| 60 | |
| 61 | // No operands needed remapping; keep the identity map. |
| 62 | return const_cast<Value*>(V); |
Victor Hernandez | f58c34d | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 65 | Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 66 | if (C == 0) |
| 67 | return 0; |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 68 | |
| 69 | if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || |
| 70 | isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || |
Devang Patel | 1192d4d | 2010-07-02 21:13:23 +0000 | [diff] [blame] | 71 | isa<UndefValue>(C)) |
Devang Patel | 170f06e | 2010-06-22 23:29:55 +0000 | [diff] [blame] | 72 | return VMSlot = C; // Primitive constants map directly |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 73 | |
| 74 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
| 75 | for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); |
| 76 | i != e; ++i) { |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 77 | Value *MV = MapValue(*i, VM, ModuleLevelChanges); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 78 | if (MV != *i) { |
| 79 | // This array must contain a reference to a global, make a new array |
| 80 | // and return it. |
| 81 | // |
| 82 | std::vector<Constant*> Values; |
| 83 | Values.reserve(CA->getNumOperands()); |
| 84 | for (User::op_iterator j = b; j != i; ++j) |
| 85 | Values.push_back(cast<Constant>(*j)); |
| 86 | Values.push_back(cast<Constant>(MV)); |
| 87 | for (++i; i != e; ++i) |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 88 | Values.push_back(cast<Constant>(MapValue(*i, VM, |
| 89 | ModuleLevelChanges))); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 90 | return VM[V] = ConstantArray::get(CA->getType(), Values); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 92 | } |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 93 | return VM[V] = C; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 95 | |
| 96 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 97 | for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); |
| 98 | i != e; ++i) { |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 99 | Value *MV = MapValue(*i, VM, ModuleLevelChanges); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 100 | if (MV != *i) { |
| 101 | // This struct must contain a reference to a global, make a new struct |
| 102 | // and return it. |
| 103 | // |
| 104 | std::vector<Constant*> Values; |
| 105 | Values.reserve(CS->getNumOperands()); |
| 106 | for (User::op_iterator j = b; j != i; ++j) |
| 107 | Values.push_back(cast<Constant>(*j)); |
| 108 | Values.push_back(cast<Constant>(MV)); |
| 109 | for (++i; i != e; ++i) |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 110 | Values.push_back(cast<Constant>(MapValue(*i, VM, |
| 111 | ModuleLevelChanges))); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 112 | return VM[V] = ConstantStruct::get(CS->getType(), Values); |
| 113 | } |
| 114 | } |
| 115 | return VM[V] = C; |
| 116 | } |
| 117 | |
| 118 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 119 | std::vector<Constant*> Ops; |
| 120 | for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 121 | Ops.push_back(cast<Constant>(MapValue(*i, VM, ModuleLevelChanges))); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 122 | return VM[V] = CE->getWithOperands(Ops); |
| 123 | } |
| 124 | |
Chris Lattner | 1bb9591 | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 125 | if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
| 126 | for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end(); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 127 | i != e; ++i) { |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 128 | Value *MV = MapValue(*i, VM, ModuleLevelChanges); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 129 | if (MV != *i) { |
| 130 | // This vector value must contain a reference to a global, make a new |
| 131 | // vector constant and return it. |
| 132 | // |
| 133 | std::vector<Constant*> Values; |
Chris Lattner | 1bb9591 | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 134 | Values.reserve(CV->getNumOperands()); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 135 | for (User::op_iterator j = b; j != i; ++j) |
| 136 | Values.push_back(cast<Constant>(*j)); |
| 137 | Values.push_back(cast<Constant>(MV)); |
| 138 | for (++i; i != e; ++i) |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 139 | Values.push_back(cast<Constant>(MapValue(*i, VM, |
| 140 | ModuleLevelChanges))); |
Chris Lattner | 77488cc | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 141 | return VM[V] = ConstantVector::get(Values); |
| 142 | } |
| 143 | } |
| 144 | return VM[V] = C; |
Chris Lattner | 1bb9591 | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 1b3ef34 | 2010-01-21 21:05:54 +0000 | [diff] [blame] | 147 | BlockAddress *BA = cast<BlockAddress>(C); |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 148 | Function *F = cast<Function>(MapValue(BA->getFunction(), VM, |
| 149 | ModuleLevelChanges)); |
| 150 | BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM, |
| 151 | ModuleLevelChanges)); |
Chris Lattner | 1b3ef34 | 2010-01-21 21:05:54 +0000 | [diff] [blame] | 152 | return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 153 | } |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 154 | |
| 155 | /// RemapInstruction - Convert the instruction operands from referencing the |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 156 | /// current values into those specified by VMap. |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 157 | /// |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 158 | void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap, |
| 159 | bool ModuleLevelChanges) { |
| 160 | // Remap operands. |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 161 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 162 | Value *V = MapValue(*op, VMap, ModuleLevelChanges); |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 163 | assert(V && "Referenced value not in value map!"); |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 164 | *op = V; |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 165 | } |
Daniel Dunbar | fd406f1 | 2010-08-26 03:48:08 +0000 | [diff] [blame] | 166 | |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 167 | // Remap attached metadata. |
| 168 | SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; |
| 169 | I->getAllMetadata(MDs); |
| 170 | for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator |
| 171 | MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) { |
| 172 | Value *Old = MI->second; |
| 173 | Value *New = MapValue(Old, VMap, ModuleLevelChanges); |
| 174 | if (New != Old) |
| 175 | I->setMetadata(MI->first, cast<MDNode>(New)); |
| 176 | } |
| 177 | } |