ValueMapper.cpp revision cae0a193e310050b6bb9cc4628b7151997126f60
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/GlobalValue.h" 18#include "llvm/Instruction.h" 19using namespace llvm; 20 21Value *llvm::MapValue(const Value *V, ValueMapTy &VM) { 22 Value *&VMSlot = VM[V]; 23 if (VMSlot) return VMSlot; // Does it exist in the map yet? 24 25 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the 26 // DenseMap. This includes any recursive calls to MapValue. 27 28 // Global values do not need to be seeded into the ValueMap if they are using 29 // the identity mapping. 30 if (isa<GlobalValue>(V) || isa<InlineAsm>(V)) 31 return VMSlot = const_cast<Value*>(V); 32 33 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { 34 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || 35 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || 36 isa<UndefValue>(C)) 37 return VMSlot = C; // Primitive constants map directly 38 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { 39 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { 40 Value *MV = MapValue(CA->getOperand(i), VM); 41 if (MV != CA->getOperand(i)) { 42 // This array must contain a reference to a global, make a new array 43 // and return it. 44 // 45 std::vector<Constant*> Values; 46 Values.reserve(CA->getNumOperands()); 47 for (unsigned j = 0; j != i; ++j) 48 Values.push_back(CA->getOperand(j)); 49 Values.push_back(cast<Constant>(MV)); 50 for (++i; i != e; ++i) 51 Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM))); 52 return VM[V] = ConstantArray::get(CA->getType(), Values); 53 } 54 } 55 return VM[V] = C; 56 57 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 58 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { 59 Value *MV = MapValue(CS->getOperand(i), VM); 60 if (MV != CS->getOperand(i)) { 61 // This struct must contain a reference to a global, make a new struct 62 // and return it. 63 // 64 std::vector<Constant*> Values; 65 Values.reserve(CS->getNumOperands()); 66 for (unsigned j = 0; j != i; ++j) 67 Values.push_back(CS->getOperand(j)); 68 Values.push_back(cast<Constant>(MV)); 69 for (++i; i != e; ++i) 70 Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM))); 71 return VM[V] = ConstantStruct::get(CS->getType(), Values); 72 } 73 } 74 return VM[V] = C; 75 76 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 77 std::vector<Constant*> Ops; 78 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) 79 Ops.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM))); 80 return VM[V] = CE->getWithOperands(Ops); 81 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { 82 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { 83 Value *MV = MapValue(CP->getOperand(i), VM); 84 if (MV != CP->getOperand(i)) { 85 // This packed value must contain a reference to a global, make a new 86 // packed constant and return it. 87 // 88 std::vector<Constant*> Values; 89 Values.reserve(CP->getNumOperands()); 90 for (unsigned j = 0; j != i; ++j) 91 Values.push_back(CP->getOperand(j)); 92 Values.push_back(cast<Constant>(MV)); 93 for (++i; i != e; ++i) 94 Values.push_back(cast<Constant>(MapValue(CP->getOperand(i), VM))); 95 return VM[V] = ConstantVector::get(Values); 96 } 97 } 98 return VM[V] = C; 99 100 } else { 101 assert(0 && "Unknown type of constant!"); 102 } 103 } 104 105 return 0; 106} 107 108/// RemapInstruction - Convert the instruction operands from referencing the 109/// current values into those specified by ValueMap. 110/// 111void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { 112 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { 113 const Value *Op = I->getOperand(op); 114 Value *V = MapValue(Op, ValueMap); 115 assert(V && "Referenced value not in value map!"); 116 I->setOperand(op, V); 117 } 118} 119