Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 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 Korobeynikov | ad7ea24 | 2007-11-09 12:27:04 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | 52efc3f | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Devang Patel | 7c36885 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 18 | #include "llvm/Metadata.h" |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Dan Gohman | a5b5be1 | 2009-10-24 23:37:16 +0000 | [diff] [blame] | 22 | Value *llvm::MapValue(const Value *V, ValueMapTy &VM) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | Value *&VMSlot = VM[V]; |
| 24 | if (VMSlot) return VMSlot; // Does it exist in the map yet? |
| 25 | |
| 26 | // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the |
| 27 | // DenseMap. This includes any recursive calls to MapValue. |
| 28 | |
Victor Hernandez | 16907c4 | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 29 | // Global values and non-function-local metadata do not need to be seeded into |
| 30 | // the ValueMap if they are using the identity mapping. |
| 31 | if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) || |
Chris Lattner | 906bd21 | 2010-01-21 21:05:54 +0000 | [diff] [blame^] | 32 | (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | return VMSlot = const_cast<Value*>(V); |
| 34 | |
Chris Lattner | 906bd21 | 2010-01-21 21:05:54 +0000 | [diff] [blame^] | 35 | if (const MDNode *MD = dyn_cast<MDNode>(V)) { |
Victor Hernandez | 17a02ae | 2010-01-20 06:56:16 +0000 | [diff] [blame] | 36 | SmallVector<Value*, 4> Elts; |
Victor Hernandez | 16907c4 | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 37 | for (unsigned i = 0; i != MD->getNumOperands(); i++) |
Chris Lattner | 906bd21 | 2010-01-21 21:05:54 +0000 | [diff] [blame^] | 38 | Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : 0); |
Victor Hernandez | 16907c4 | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 39 | return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size()); |
| 40 | } |
| 41 | |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 42 | Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); |
| 43 | if (C == 0) return 0; |
| 44 | |
| 45 | if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || |
| 46 | isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || |
| 47 | isa<UndefValue>(C) || isa<MDString>(C)) |
| 48 | return VMSlot = C; // Primitive constants map directly |
| 49 | |
| 50 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
| 51 | for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); |
| 52 | i != e; ++i) { |
| 53 | Value *MV = MapValue(*i, VM); |
| 54 | if (MV != *i) { |
| 55 | // This array must contain a reference to a global, make a new array |
| 56 | // and return it. |
| 57 | // |
| 58 | std::vector<Constant*> Values; |
| 59 | Values.reserve(CA->getNumOperands()); |
| 60 | for (User::op_iterator j = b; j != i; ++j) |
| 61 | Values.push_back(cast<Constant>(*j)); |
| 62 | Values.push_back(cast<Constant>(MV)); |
| 63 | for (++i; i != e; ++i) |
| 64 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
| 65 | return VM[V] = ConstantArray::get(CA->getType(), Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 67 | } |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 68 | return VM[V] = C; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 70 | |
| 71 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 72 | for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); |
| 73 | i != e; ++i) { |
| 74 | Value *MV = MapValue(*i, VM); |
| 75 | if (MV != *i) { |
| 76 | // This struct must contain a reference to a global, make a new struct |
| 77 | // and return it. |
| 78 | // |
| 79 | std::vector<Constant*> Values; |
| 80 | Values.reserve(CS->getNumOperands()); |
| 81 | for (User::op_iterator j = b; j != i; ++j) |
| 82 | Values.push_back(cast<Constant>(*j)); |
| 83 | Values.push_back(cast<Constant>(MV)); |
| 84 | for (++i; i != e; ++i) |
| 85 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
| 86 | return VM[V] = ConstantStruct::get(CS->getType(), Values); |
| 87 | } |
| 88 | } |
| 89 | return VM[V] = C; |
| 90 | } |
| 91 | |
| 92 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 93 | std::vector<Constant*> Ops; |
| 94 | for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) |
| 95 | Ops.push_back(cast<Constant>(MapValue(*i, VM))); |
| 96 | return VM[V] = CE->getWithOperands(Ops); |
| 97 | } |
| 98 | |
Chris Lattner | 52efc3f | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 99 | if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
| 100 | for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end(); |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 101 | i != e; ++i) { |
| 102 | Value *MV = MapValue(*i, VM); |
| 103 | if (MV != *i) { |
| 104 | // This vector value must contain a reference to a global, make a new |
| 105 | // vector constant and return it. |
| 106 | // |
| 107 | std::vector<Constant*> Values; |
Chris Lattner | 52efc3f | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 108 | Values.reserve(CV->getNumOperands()); |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 109 | for (User::op_iterator j = b; j != i; ++j) |
| 110 | Values.push_back(cast<Constant>(*j)); |
| 111 | Values.push_back(cast<Constant>(MV)); |
| 112 | for (++i; i != e; ++i) |
| 113 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
| 114 | return VM[V] = ConstantVector::get(Values); |
| 115 | } |
| 116 | } |
| 117 | return VM[V] = C; |
Chris Lattner | 52efc3f | 2009-10-29 00:31:02 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 906bd21 | 2010-01-21 21:05:54 +0000 | [diff] [blame^] | 120 | BlockAddress *BA = cast<BlockAddress>(C); |
| 121 | Function *F = cast<Function>(MapValue(BA->getFunction(), VM)); |
| 122 | BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM)); |
| 123 | return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /// RemapInstruction - Convert the instruction operands from referencing the |
| 127 | /// current values into those specified by ValueMap. |
| 128 | /// |
| 129 | void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 130 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
Dan Gohman | a5b5be1 | 2009-10-24 23:37:16 +0000 | [diff] [blame] | 131 | Value *V = MapValue(*op, ValueMap); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 132 | assert(V && "Referenced value not in value map!"); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 133 | *op = V; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
Devang Patel | 410b384 | 2010-01-18 19:52:14 +0000 | [diff] [blame] | 136 | |