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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
| 15 | #include "ValueMapper.h" |
| 16 | #include "llvm/Constants.h" |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalValue.h" |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 18 | #include "llvm/Instruction.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 20 | |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 21 | Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 22 | Value *&VMSlot = VM[V]; |
| 23 | if (VMSlot) return VMSlot; // Does it exist in the map yet? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 5f92e2b | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 25 | // Global values do not need to be seeded into the ValueMap if they are using |
| 26 | // the identity mapping. |
Chris Lattner | 3805dea | 2006-04-01 23:17:11 +0000 | [diff] [blame] | 27 | if (isa<GlobalValue>(V) || isa<InlineAsm>(V)) |
Chris Lattner | 5f92e2b | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 28 | return VMSlot = const_cast<Value*>(V); |
| 29 | |
Chris Lattner | 15faa84 | 2003-04-18 03:49:49 +0000 | [diff] [blame] | 30 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame^] | 31 | if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || |
Chris Lattner | 82731c7 | 2004-10-16 18:10:31 +0000 | [diff] [blame] | 32 | isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || |
Chris Lattner | 83f03bf | 2006-05-27 01:22:24 +0000 | [diff] [blame] | 33 | isa<UndefValue>(C)) |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 34 | return VMSlot = C; // Primitive constants map directly |
Reid Spencer | e95ff9a | 2004-07-18 08:41:47 +0000 | [diff] [blame] | 35 | else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 36 | for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { |
| 37 | Value *MV = MapValue(CA->getOperand(i), VM); |
| 38 | if (MV != CA->getOperand(i)) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 39 | // This array must contain a reference to a global, make a new array |
| 40 | // and return it. |
| 41 | // |
| 42 | std::vector<Constant*> Values; |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 43 | Values.reserve(CA->getNumOperands()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 44 | for (unsigned j = 0; j != i; ++j) |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 45 | Values.push_back(CA->getOperand(j)); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 46 | Values.push_back(cast<Constant>(MV)); |
Chris Lattner | ac8d4d9 | 2002-12-07 21:27:16 +0000 | [diff] [blame] | 47 | for (++i; i != e; ++i) |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 48 | Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM))); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 49 | return VMSlot = ConstantArray::get(CA->getType(), Values); |
| 50 | } |
| 51 | } |
| 52 | return VMSlot = C; |
| 53 | |
| 54 | } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 55 | for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { |
| 56 | Value *MV = MapValue(CS->getOperand(i), VM); |
| 57 | if (MV != CS->getOperand(i)) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 58 | // This struct must contain a reference to a global, make a new struct |
| 59 | // and return it. |
| 60 | // |
| 61 | std::vector<Constant*> Values; |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 62 | Values.reserve(CS->getNumOperands()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 63 | for (unsigned j = 0; j != i; ++j) |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 64 | Values.push_back(CS->getOperand(j)); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 65 | Values.push_back(cast<Constant>(MV)); |
Chris Lattner | ac8d4d9 | 2002-12-07 21:27:16 +0000 | [diff] [blame] | 66 | for (++i; i != e; ++i) |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 67 | Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM))); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 68 | return VMSlot = ConstantStruct::get(CS->getType(), Values); |
| 69 | } |
| 70 | } |
| 71 | return VMSlot = C; |
| 72 | |
| 73 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | 27d6721 | 2006-07-14 22:21:31 +0000 | [diff] [blame] | 74 | std::vector<Constant*> Ops; |
| 75 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) |
| 76 | Ops.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM))); |
| 77 | return VMSlot = CE->getWithOperands(Ops); |
Chris Lattner | 49aaa6a | 2006-03-27 05:50:18 +0000 | [diff] [blame] | 78 | } else if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) { |
| 79 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { |
| 80 | Value *MV = MapValue(CP->getOperand(i), VM); |
| 81 | if (MV != CP->getOperand(i)) { |
| 82 | // This packed value must contain a reference to a global, make a new |
| 83 | // packed constant and return it. |
| 84 | // |
| 85 | std::vector<Constant*> Values; |
| 86 | Values.reserve(CP->getNumOperands()); |
| 87 | for (unsigned j = 0; j != i; ++j) |
| 88 | Values.push_back(CP->getOperand(j)); |
| 89 | Values.push_back(cast<Constant>(MV)); |
| 90 | for (++i; i != e; ++i) |
| 91 | Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM))); |
| 92 | return VMSlot = ConstantPacked::get(Values); |
| 93 | } |
| 94 | } |
| 95 | return VMSlot = C; |
| 96 | |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 97 | } else { |
| 98 | assert(0 && "Unknown type of constant!"); |
| 99 | } |
| 100 | } |
Chris Lattner | 3cf5db7 | 2003-01-13 00:52:25 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 104 | |
| 105 | /// RemapInstruction - Convert the instruction operands from referencing the |
| 106 | /// current values into those specified by ValueMap. |
| 107 | /// |
| 108 | void llvm::RemapInstruction(Instruction *I, |
| 109 | std::map<const Value *, Value*> &ValueMap) { |
| 110 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 111 | const Value *Op = I->getOperand(op); |
| 112 | Value *V = MapValue(Op, ValueMap); |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 113 | assert(V && "Referenced value not in value map!"); |
| 114 | I->setOperand(op, V); |
| 115 | } |
| 116 | } |