ValueMapper.cpp revision e7a6663eb155eb8eb5de24cf22f632790485e5f9
1//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// 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//===----------------------------------------------------------------------===// 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" 17#include "llvm/Instruction.h" 18 19Value *MapValue(const Value *V, std::map<const Value*, Value*> &VM) { 20 Value *&VMSlot = VM[V]; 21 if (VMSlot) return VMSlot; // Does it exist in the map yet? 22 23 // Global values do not need to be seeded into the ValueMap if they are using 24 // the identity mapping. 25 if (isa<GlobalValue>(V)) 26 return VMSlot = const_cast<Value*>(V); 27 28 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { 29 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) || 30 isa<ConstantPointerNull>(C)) 31 return VMSlot = C; // Primitive constants map directly 32 else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) { 33 GlobalValue *MV = cast<GlobalValue>(MapValue((Value*)CPR->getValue(),VM)); 34 return VMSlot = ConstantPointerRef::get(MV); 35 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { 36 const std::vector<Use> &Vals = CA->getValues(); 37 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 38 Value *MV = MapValue(Vals[i], VM); 39 if (MV != Vals[i]) { 40 // This array must contain a reference to a global, make a new array 41 // and return it. 42 // 43 std::vector<Constant*> Values; 44 Values.reserve(Vals.size()); 45 for (unsigned j = 0; j != i; ++j) 46 Values.push_back(cast<Constant>(Vals[j])); 47 Values.push_back(cast<Constant>(MV)); 48 for (++i; i != e; ++i) 49 Values.push_back(cast<Constant>(MapValue(Vals[i], VM))); 50 return VMSlot = ConstantArray::get(CA->getType(), Values); 51 } 52 } 53 return VMSlot = C; 54 55 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 56 const std::vector<Use> &Vals = CS->getValues(); 57 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 58 Value *MV = MapValue(Vals[i], VM); 59 if (MV != Vals[i]) { 60 // This struct must contain a reference to a global, make a new struct 61 // and return it. 62 // 63 std::vector<Constant*> Values; 64 Values.reserve(Vals.size()); 65 for (unsigned j = 0; j != i; ++j) 66 Values.push_back(cast<Constant>(Vals[j])); 67 Values.push_back(cast<Constant>(MV)); 68 for (++i; i != e; ++i) 69 Values.push_back(cast<Constant>(MapValue(Vals[i], VM))); 70 return VMSlot = ConstantStruct::get(CS->getType(), Values); 71 } 72 } 73 return VMSlot = C; 74 75 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 76 if (CE->getOpcode() == Instruction::Cast) { 77 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM)); 78 return VMSlot = ConstantExpr::getCast(MV, CE->getType()); 79 } else if (CE->getOpcode() == Instruction::GetElementPtr) { 80 std::vector<Constant*> Idx; 81 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM)); 82 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 83 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM))); 84 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx); 85 } else if (CE->getOpcode() == Instruction::Shl || 86 CE->getOpcode() == Instruction::Shr) { 87 assert(CE->getNumOperands() == 2 && "Must be a shift!"); 88 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM)); 89 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM)); 90 return VMSlot = ConstantExpr::getShift(CE->getOpcode(), MV1, MV2); 91 } else { 92 assert(CE->getNumOperands() == 2 && "Must be binary operator?"); 93 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM)); 94 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM)); 95 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2); 96 } 97 98 } else { 99 assert(0 && "Unknown type of constant!"); 100 } 101 } 102 103 V->dump(); 104 assert(0 && "Unknown value type: why didn't it get resolved?!"); 105 return 0; 106} 107 108