Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 1 | //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 22 | |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 23 | Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 24 | Value *&VMSlot = VM[V]; |
| 25 | if (VMSlot) return VMSlot; // Does it exist in the map yet? |
| 26 | |
Chris Lattner | 5f92e2b | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 27 | // Global values do not need to be seeded into the ValueMap if they are using |
| 28 | // the identity mapping. |
| 29 | if (isa<GlobalValue>(V)) |
| 30 | return VMSlot = const_cast<Value*>(V); |
| 31 | |
Chris Lattner | 15faa84 | 2003-04-18 03:49:49 +0000 | [diff] [blame] | 32 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 33 | if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) || |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 34 | isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C)) |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 35 | return VMSlot = C; // Primitive constants map directly |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame^] | 36 | else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 37 | return VMSlot = GV; |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 38 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
| 39 | const std::vector<Use> &Vals = CA->getValues(); |
| 40 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 41 | Value *MV = MapValue(Vals[i], VM); |
| 42 | if (MV != Vals[i]) { |
| 43 | // This array must contain a reference to a global, make a new array |
| 44 | // and return it. |
| 45 | // |
| 46 | std::vector<Constant*> Values; |
| 47 | Values.reserve(Vals.size()); |
| 48 | for (unsigned j = 0; j != i; ++j) |
| 49 | Values.push_back(cast<Constant>(Vals[j])); |
| 50 | Values.push_back(cast<Constant>(MV)); |
Chris Lattner | ac8d4d9 | 2002-12-07 21:27:16 +0000 | [diff] [blame] | 51 | for (++i; i != e; ++i) |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 52 | Values.push_back(cast<Constant>(MapValue(Vals[i], VM))); |
| 53 | return VMSlot = ConstantArray::get(CA->getType(), Values); |
| 54 | } |
| 55 | } |
| 56 | return VMSlot = C; |
| 57 | |
| 58 | } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 59 | const std::vector<Use> &Vals = CS->getValues(); |
| 60 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 61 | Value *MV = MapValue(Vals[i], VM); |
| 62 | if (MV != Vals[i]) { |
| 63 | // This struct must contain a reference to a global, make a new struct |
| 64 | // and return it. |
| 65 | // |
| 66 | std::vector<Constant*> Values; |
| 67 | Values.reserve(Vals.size()); |
| 68 | for (unsigned j = 0; j != i; ++j) |
| 69 | Values.push_back(cast<Constant>(Vals[j])); |
| 70 | Values.push_back(cast<Constant>(MV)); |
Chris Lattner | ac8d4d9 | 2002-12-07 21:27:16 +0000 | [diff] [blame] | 71 | for (++i; i != e; ++i) |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 72 | Values.push_back(cast<Constant>(MapValue(Vals[i], VM))); |
| 73 | return VMSlot = ConstantStruct::get(CS->getType(), Values); |
| 74 | } |
| 75 | } |
| 76 | return VMSlot = C; |
| 77 | |
| 78 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 79 | if (CE->getOpcode() == Instruction::Cast) { |
| 80 | Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM)); |
| 81 | return VMSlot = ConstantExpr::getCast(MV, CE->getType()); |
| 82 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 83 | std::vector<Constant*> Idx; |
| 84 | Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM)); |
| 85 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 86 | Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM))); |
| 87 | return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx); |
| 88 | } else { |
| 89 | assert(CE->getNumOperands() == 2 && "Must be binary operator?"); |
| 90 | Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM)); |
| 91 | Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM)); |
| 92 | return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2); |
| 93 | } |
| 94 | |
| 95 | } else { |
| 96 | assert(0 && "Unknown type of constant!"); |
| 97 | } |
| 98 | } |
Chris Lattner | 3cf5db7 | 2003-01-13 00:52:25 +0000 | [diff] [blame] | 99 | |
| 100 | V->dump(); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 101 | assert(0 && "Unknown value type: why didn't it get resolved?!"); |
| 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); |
| 113 | #ifndef NDEBUG |
| 114 | if (!V) { |
Chris Lattner | 30b4344 | 2004-07-15 02:06:12 +0000 | [diff] [blame] | 115 | std::cerr << "Val = \n" << *Op << "Addr = " << (void*)Op; |
| 116 | std::cerr << "\nInst = " << *I; |
Brian Gaeke | 6129af3 | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 117 | } |
| 118 | #endif |
| 119 | assert(V && "Referenced value not in value map!"); |
| 120 | I->setOperand(op, V); |
| 121 | } |
| 122 | } |