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 | |
Anton Korobeynikov | 344ef19 | 2007-11-09 12:27:04 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 16 | #include "llvm/BasicBlock.h" |
Devang Patel | 41d89c2 | 2009-07-27 17:17:04 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty) |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalValue.h" |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 20 | #include "llvm/Instruction.h" |
Devang Patel | 0a9f7b9 | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 21 | #include "llvm/Metadata.h" |
Nick Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 25 | |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 26 | Value *llvm::MapValue(const Value *V, ValueMapTy &VM) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 27 | Value *&VMSlot = VM[V]; |
| 28 | if (VMSlot) return VMSlot; // Does it exist in the map yet? |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 32 | |
Devang Patel | 41d89c2 | 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)) |
Chris Lattner | 5f92e2b | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 36 | return VMSlot = const_cast<Value*>(V); |
| 37 | |
Chris Lattner | 15faa84 | 2003-04-18 03:49:49 +0000 | [diff] [blame] | 38 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 39 | if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || |
Chris Lattner | 82731c7 | 2004-10-16 18:10:31 +0000 | [diff] [blame] | 40 | isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || |
Nick Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 41 | isa<UndefValue>(C) || isa<MDString>(C)) |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 42 | return VMSlot = C; // Primitive constants map directly |
Reid Spencer | e95ff9a | 2004-07-18 08:41:47 +0000 | [diff] [blame] | 43 | else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
Gabor Greif | ba2c487 | 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) { |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 46 | Value *MV = MapValue(*i, VM); |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 47 | if (MV != *i) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +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; |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 52 | Values.reserve(CA->getNumOperands()); |
Gabor Greif | ba2c487 | 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)); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 55 | Values.push_back(cast<Constant>(MV)); |
Chris Lattner | ac8d4d9 | 2002-12-07 21:27:16 +0000 | [diff] [blame] | 56 | for (++i; i != e; ++i) |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 57 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 58 | return VM[V] = ConstantArray::get(CA->getType(), Values); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 61 | return VM[V] = C; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 62 | |
| 63 | } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Gabor Greif | ba2c487 | 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) { |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 66 | Value *MV = MapValue(*i, VM); |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 67 | if (MV != *i) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +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; |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 72 | Values.reserve(CS->getNumOperands()); |
Gabor Greif | ba2c487 | 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)); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 75 | Values.push_back(cast<Constant>(MV)); |
Chris Lattner | ac8d4d9 | 2002-12-07 21:27:16 +0000 | [diff] [blame] | 76 | for (++i; i != e; ++i) |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 77 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
Owen Anderson | 8fa3338 | 2009-07-27 22:29:26 +0000 | [diff] [blame] | 78 | return VM[V] = ConstantStruct::get(CS->getType(), Values); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 81 | return VM[V] = C; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 82 | |
| 83 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | 27d6721 | 2006-07-14 22:21:31 +0000 | [diff] [blame] | 84 | std::vector<Constant*> Ops; |
Gabor Greif | ba2c487 | 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) |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 86 | Ops.push_back(cast<Constant>(MapValue(*i, VM))); |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 87 | return VM[V] = CE->getWithOperands(Ops); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 88 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { |
Gabor Greif | ba2c487 | 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) { |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 91 | Value *MV = MapValue(*i, VM); |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 92 | if (MV != *i) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 93 | // This vector value must contain a reference to a global, make a new |
| 94 | // vector constant and return it. |
Chris Lattner | 49aaa6a | 2006-03-27 05:50:18 +0000 | [diff] [blame] | 95 | // |
| 96 | std::vector<Constant*> Values; |
| 97 | Values.reserve(CP->getNumOperands()); |
Gabor Greif | ba2c487 | 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)); |
Chris Lattner | 49aaa6a | 2006-03-27 05:50:18 +0000 | [diff] [blame] | 100 | Values.push_back(cast<Constant>(MV)); |
| 101 | for (++i; i != e; ++i) |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 102 | Values.push_back(cast<Constant>(MapValue(*i, VM))); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 103 | return VM[V] = ConstantVector::get(Values); |
Chris Lattner | 49aaa6a | 2006-03-27 05:50:18 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Chris Lattner | cae0a19 | 2007-02-23 19:54:30 +0000 | [diff] [blame] | 106 | return VM[V] = C; |
Chris Lattner | 49aaa6a | 2006-03-27 05:50:18 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 108 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 109 | llvm_unreachable("Unknown type of constant!"); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 114 | |
| 115 | /// RemapInstruction - Convert the instruction operands from referencing the |
| 116 | /// current values into those specified by ValueMap. |
| 117 | /// |
Chris Lattner | 5e665f5 | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 118 | void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 119 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
Dan Gohman | 5fa75b0 | 2009-10-24 23:37:16 +0000 | [diff] [blame^] | 120 | Value *V = MapValue(*op, ValueMap); |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 121 | assert(V && "Referenced value not in value map!"); |
Gabor Greif | ba2c487 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 122 | *op = V; |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 123 | } |
| 124 | } |