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" |
Devang Patel | 7c36885 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 21 | #include "llvm/Metadata.h" |
Nick Lewycky | 29aaef8 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 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 | |
Dan Gohman | a5b5be1 | 2009-10-24 23:37:16 +0000 | [diff] [blame] | 26 | Value *llvm::MapValue(const Value *V, ValueMapTy &VM) { |
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 | |
Devang Patel | 20e367a | 2009-07-27 17:17:04 +0000 | [diff] [blame] | 33 | // Global values and metadata do not need to be seeded into the ValueMap if |
| 34 | // they are using the identity mapping. |
| 35 | if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MetadataBase>(V)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | return VMSlot = const_cast<Value*>(V); |
| 37 | |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame^] | 38 | Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); |
| 39 | if (C == 0) return 0; |
| 40 | |
| 41 | if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || |
| 42 | isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || |
| 43 | isa<UndefValue>(C) || isa<MDString>(C)) |
| 44 | return VMSlot = C; // Primitive constants map directly |
| 45 | |
| 46 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
| 47 | for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); |
| 48 | i != e; ++i) { |
| 49 | Value *MV = MapValue(*i, VM); |
| 50 | if (MV != *i) { |
| 51 | // This array must contain a reference to a global, make a new array |
| 52 | // and return it. |
| 53 | // |
| 54 | std::vector<Constant*> Values; |
| 55 | Values.reserve(CA->getNumOperands()); |
| 56 | for (User::op_iterator j = b; j != i; ++j) |
| 57 | Values.push_back(cast<Constant>(*j)); |
| 58 | Values.push_back(cast<Constant>(MV)); |
| 59 | for (++i; i != e; ++i) |
| 60 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
| 61 | return VM[V] = ConstantArray::get(CA->getType(), Values); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame^] | 64 | return VM[V] = C; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 65 | } |
Chris Lattner | 15513ab | 2009-10-29 00:28:30 +0000 | [diff] [blame^] | 66 | |
| 67 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 68 | for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); |
| 69 | i != e; ++i) { |
| 70 | Value *MV = MapValue(*i, VM); |
| 71 | if (MV != *i) { |
| 72 | // This struct must contain a reference to a global, make a new struct |
| 73 | // and return it. |
| 74 | // |
| 75 | std::vector<Constant*> Values; |
| 76 | Values.reserve(CS->getNumOperands()); |
| 77 | for (User::op_iterator j = b; j != i; ++j) |
| 78 | Values.push_back(cast<Constant>(*j)); |
| 79 | Values.push_back(cast<Constant>(MV)); |
| 80 | for (++i; i != e; ++i) |
| 81 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
| 82 | return VM[V] = ConstantStruct::get(CS->getType(), Values); |
| 83 | } |
| 84 | } |
| 85 | return VM[V] = C; |
| 86 | } |
| 87 | |
| 88 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 89 | std::vector<Constant*> Ops; |
| 90 | for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) |
| 91 | Ops.push_back(cast<Constant>(MapValue(*i, VM))); |
| 92 | return VM[V] = CE->getWithOperands(Ops); |
| 93 | } |
| 94 | |
| 95 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
| 96 | for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end(); |
| 97 | i != e; ++i) { |
| 98 | Value *MV = MapValue(*i, VM); |
| 99 | if (MV != *i) { |
| 100 | // This vector value must contain a reference to a global, make a new |
| 101 | // vector constant and return it. |
| 102 | // |
| 103 | std::vector<Constant*> Values; |
| 104 | Values.reserve(CP->getNumOperands()); |
| 105 | for (User::op_iterator j = b; j != i; ++j) |
| 106 | Values.push_back(cast<Constant>(*j)); |
| 107 | Values.push_back(cast<Constant>(MV)); |
| 108 | for (++i; i != e; ++i) |
| 109 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
| 110 | return VM[V] = ConstantVector::get(Values); |
| 111 | } |
| 112 | } |
| 113 | return VM[V] = C; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | llvm_unreachable("Unknown type of constant!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /// RemapInstruction - Convert the instruction operands from referencing the |
| 122 | /// current values into those specified by ValueMap. |
| 123 | /// |
| 124 | void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 125 | 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] | 126 | Value *V = MapValue(*op, ValueMap); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | assert(V && "Referenced value not in value map!"); |
Gabor Greif | f4f8d9d | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 128 | *op = V; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | } |
| 130 | } |