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" |
Devang Patel | 20e367a | 2009-07-27 17:17:04 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/GlobalValue.h" |
| 20 | #include "llvm/Instruction.h" |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Devang Patel | 7c36885 | 2009-07-28 21:49:47 +0000 | [diff] [blame^] | 22 | #include "llvm/Metadata.h" |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 27 | Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | Value *&VMSlot = VM[V]; |
| 29 | if (VMSlot) return VMSlot; // Does it exist in the map yet? |
| 30 | |
| 31 | // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the |
| 32 | // DenseMap. This includes any recursive calls to MapValue. |
| 33 | |
Devang Patel | 20e367a | 2009-07-27 17:17:04 +0000 | [diff] [blame] | 34 | // Global values and metadata do not need to be seeded into the ValueMap if |
| 35 | // they are using the identity mapping. |
| 36 | if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 37 | return VMSlot = const_cast<Value*>(V); |
| 38 | |
| 39 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
| 40 | if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || |
| 41 | isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 42 | isa<UndefValue>(C) || isa<MDString>(C)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | return VMSlot = C; // Primitive constants map directly |
| 44 | else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 45 | for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); |
| 46 | i != e; ++i) { |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 47 | Value *MV = MapValue(*i, VM, Context); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 48 | if (MV != *i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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()); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 54 | for (User::op_iterator j = b; j != i; ++j) |
| 55 | Values.push_back(cast<Constant>(*j)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | Values.push_back(cast<Constant>(MV)); |
| 57 | for (++i; i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 58 | Values.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
Owen Anderson | 7b4f9f8 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 59 | return VM[V] = ConstantArray::get(CA->getType(), Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return VM[V] = C; |
| 63 | |
| 64 | } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 65 | for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); |
| 66 | i != e; ++i) { |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 67 | Value *MV = MapValue(*i, VM, Context); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 68 | if (MV != *i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | // This struct must contain a reference to a global, make a new struct |
| 70 | // and return it. |
| 71 | // |
| 72 | std::vector<Constant*> Values; |
| 73 | Values.reserve(CS->getNumOperands()); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 74 | for (User::op_iterator j = b; j != i; ++j) |
| 75 | Values.push_back(cast<Constant>(*j)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 76 | Values.push_back(cast<Constant>(MV)); |
| 77 | for (++i; i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 78 | Values.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
Owen Anderson | 9c9f10e | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 79 | return VM[V] = ConstantStruct::get(CS->getType(), Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | return VM[V] = C; |
| 83 | |
| 84 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 85 | std::vector<Constant*> Ops; |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 86 | 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] | 87 | Ops.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 88 | return VM[V] = CE->getWithOperands(Ops); |
| 89 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 90 | for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end(); |
| 91 | i != e; ++i) { |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 92 | Value *MV = MapValue(*i, VM, Context); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 93 | if (MV != *i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 94 | // This vector value must contain a reference to a global, make a new |
| 95 | // vector constant and return it. |
| 96 | // |
| 97 | std::vector<Constant*> Values; |
| 98 | Values.reserve(CP->getNumOperands()); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 99 | for (User::op_iterator j = b; j != i; ++j) |
| 100 | Values.push_back(cast<Constant>(*j)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 101 | Values.push_back(cast<Constant>(MV)); |
| 102 | for (++i; i != e; ++i) |
Owen Anderson | a09d234 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 103 | Values.push_back(cast<Constant>(MapValue(*i, VM, Context))); |
Owen Anderson | 2f422e0 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 104 | return VM[V] = ConstantVector::get(Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | return VM[V] = C; |
| 108 | |
| 109 | } else { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 110 | llvm_unreachable("Unknown type of constant!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /// RemapInstruction - Convert the instruction operands from referencing the |
| 117 | /// current values into those specified by ValueMap. |
| 118 | /// |
| 119 | void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 120 | 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] | 121 | Value *V = MapValue(*op, ValueMap, I->getParent()->getContext()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | assert(V && "Referenced value not in value map!"); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 123 | *op = V; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | } |
| 125 | } |