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" |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 16 | #include "llvm/BasicBlock.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/GlobalValue.h" |
| 19 | #include "llvm/Instruction.h" |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 20 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 21 | #include "llvm/MDNode.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 26 | Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext *Context) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | 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 | |
| 33 | // Global values do not need to be seeded into the ValueMap if they are using |
| 34 | // the identity mapping. |
| 35 | if (isa<GlobalValue>(V) || isa<InlineAsm>(V)) |
| 36 | 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 Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 41 | isa<UndefValue>(C) || isa<MDString>(C)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 42 | return VMSlot = C; // Primitive constants map directly |
| 43 | else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 44 | for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); |
| 45 | i != e; ++i) { |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 46 | Value *MV = MapValue(*i, VM, Context); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 47 | if (MV != *i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | // 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 Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 53 | for (User::op_iterator j = b; j != i; ++j) |
| 54 | Values.push_back(cast<Constant>(*j)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | Values.push_back(cast<Constant>(MV)); |
| 56 | for (++i; i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 57 | Values.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
| 58 | return VM[V] = Context->getConstantArray(CA->getType(), Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | return VM[V] = C; |
| 62 | |
| 63 | } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 64 | for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); |
| 65 | i != e; ++i) { |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 66 | Value *MV = MapValue(*i, VM, Context); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 67 | if (MV != *i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 68 | // 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 Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 73 | for (User::op_iterator j = b; j != i; ++j) |
| 74 | Values.push_back(cast<Constant>(*j)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | Values.push_back(cast<Constant>(MV)); |
| 76 | for (++i; i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 77 | Values.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
| 78 | return VM[V] = Context->getConstantStruct(CS->getType(), Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | return VM[V] = C; |
| 82 | |
| 83 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 84 | std::vector<Constant*> Ops; |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 85 | for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 86 | Ops.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | return VM[V] = CE->getWithOperands(Ops); |
| 88 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 89 | for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end(); |
| 90 | i != e; ++i) { |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 91 | Value *MV = MapValue(*i, VM, Context); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 92 | if (MV != *i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 93 | // 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 Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 98 | for (User::op_iterator j = b; j != i; ++j) |
| 99 | Values.push_back(cast<Constant>(*j)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | Values.push_back(cast<Constant>(MV)); |
| 101 | for (++i; i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 102 | Values.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
| 103 | return VM[V] = Context->getConstantVector(Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | return VM[V] = C; |
| 107 | |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 108 | } else if (MDNode *N = dyn_cast<MDNode>(C)) { |
| 109 | for (MDNode::const_elem_iterator b = N->elem_begin(), i = b, |
| 110 | e = N->elem_end(); i != e; ++i) { |
| 111 | if (!*i) continue; |
| 112 | |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 113 | Value *MV = MapValue(*i, VM, Context); |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 114 | if (MV != *i) { |
| 115 | // This MDNode must contain a reference to a global, make a new MDNode |
| 116 | // and return it. |
| 117 | SmallVector<Value*, 8> Values; |
| 118 | Values.reserve(N->getNumElements()); |
| 119 | for (MDNode::const_elem_iterator j = b; j != i; ++j) |
| 120 | Values.push_back(*j); |
| 121 | Values.push_back(MV); |
| 122 | for (++i; i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 123 | Values.push_back(MapValue(*i, VM, Context)); |
| 124 | return VM[V] = Context->getMDNode(Values.data(), Values.size()); |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | return VM[V] = C; |
| 128 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | } else { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 130 | llvm_unreachable("Unknown type of constant!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /// RemapInstruction - Convert the instruction operands from referencing the |
| 138 | /// current values into those specified by ValueMap. |
| 139 | /// |
| 140 | void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 141 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 142 | Value *V = MapValue(*op, ValueMap, I->getParent()->getContext()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 143 | assert(V && "Referenced value not in value map!"); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 144 | *op = V; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 145 | } |
| 146 | } |