ValueMapper.cpp revision 82731c793a9fbe53d8c5e784e3638cd3ad09d5ca
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" 19#include <iostream> 20 21using namespace llvm; 22 23Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) { 24 Value *&VMSlot = VM[V]; 25 if (VMSlot) return VMSlot; // Does it exist in the map yet? 26 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 32 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { 33 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) || 34 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || 35 isa<UndefValue>(C)) 36 return VMSlot = C; // Primitive constants map directly 37 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { 38 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { 39 Value *MV = MapValue(CA->getOperand(i), VM); 40 if (MV != CA->getOperand(i)) { 41 // This array must contain a reference to a global, make a new array 42 // and return it. 43 // 44 std::vector<Constant*> Values; 45 Values.reserve(CA->getNumOperands()); 46 for (unsigned j = 0; j != i; ++j) 47 Values.push_back(CA->getOperand(j)); 48 Values.push_back(cast<Constant>(MV)); 49 for (++i; i != e; ++i) 50 Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM))); 51 return VMSlot = ConstantArray::get(CA->getType(), Values); 52 } 53 } 54 return VMSlot = C; 55 56 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 57 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { 58 Value *MV = MapValue(CS->getOperand(i), VM); 59 if (MV != CS->getOperand(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(CS->getNumOperands()); 65 for (unsigned j = 0; j != i; ++j) 66 Values.push_back(CS->getOperand(j)); 67 Values.push_back(cast<Constant>(MV)); 68 for (++i; i != e; ++i) 69 Values.push_back(cast<Constant>(MapValue(CS->getOperand(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::Select) { 86 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM)); 87 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM)); 88 Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM)); 89 return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3); 90 } else { 91 assert(CE->getNumOperands() == 2 && "Must be binary operator?"); 92 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM)); 93 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM)); 94 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2); 95 } 96 97 } else { 98 assert(0 && "Unknown type of constant!"); 99 } 100 } 101 102 V->dump(); 103 assert(0 && "Unknown value type: why didn't it get resolved?!"); 104 return 0; 105} 106 107/// RemapInstruction - Convert the instruction operands from referencing the 108/// current values into those specified by ValueMap. 109/// 110void llvm::RemapInstruction(Instruction *I, 111 std::map<const Value *, Value*> &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#ifndef NDEBUG 116 if (!V) { 117 std::cerr << "Val = \n" << *Op << "Addr = " << (void*)Op; 118 std::cerr << "\nInst = " << *I; 119 } 120#endif 121 assert(V && "Referenced value not in value map!"); 122 I->setOperand(op, V); 123 } 124} 125