LLVM API Documentation
00001 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the MapValue function, which is shared by various parts of 00011 // the lib/Transforms/Utils library. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "ValueMapper.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/GlobalValue.h" 00018 #include "llvm/Instruction.h" 00019 #include <iostream> 00020 00021 using namespace llvm; 00022 00023 Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) { 00024 Value *&VMSlot = VM[V]; 00025 if (VMSlot) return VMSlot; // Does it exist in the map yet? 00026 00027 // Global values do not need to be seeded into the ValueMap if they are using 00028 // the identity mapping. 00029 if (isa<GlobalValue>(V)) 00030 return VMSlot = const_cast<Value*>(V); 00031 00032 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { 00033 if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) || 00034 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || 00035 isa<UndefValue>(C)) 00036 return VMSlot = C; // Primitive constants map directly 00037 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { 00038 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) { 00039 Value *MV = MapValue(CA->getOperand(i), VM); 00040 if (MV != CA->getOperand(i)) { 00041 // This array must contain a reference to a global, make a new array 00042 // and return it. 00043 // 00044 std::vector<Constant*> Values; 00045 Values.reserve(CA->getNumOperands()); 00046 for (unsigned j = 0; j != i; ++j) 00047 Values.push_back(CA->getOperand(j)); 00048 Values.push_back(cast<Constant>(MV)); 00049 for (++i; i != e; ++i) 00050 Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM))); 00051 return VMSlot = ConstantArray::get(CA->getType(), Values); 00052 } 00053 } 00054 return VMSlot = C; 00055 00056 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 00057 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) { 00058 Value *MV = MapValue(CS->getOperand(i), VM); 00059 if (MV != CS->getOperand(i)) { 00060 // This struct must contain a reference to a global, make a new struct 00061 // and return it. 00062 // 00063 std::vector<Constant*> Values; 00064 Values.reserve(CS->getNumOperands()); 00065 for (unsigned j = 0; j != i; ++j) 00066 Values.push_back(CS->getOperand(j)); 00067 Values.push_back(cast<Constant>(MV)); 00068 for (++i; i != e; ++i) 00069 Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM))); 00070 return VMSlot = ConstantStruct::get(CS->getType(), Values); 00071 } 00072 } 00073 return VMSlot = C; 00074 00075 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 00076 if (CE->getOpcode() == Instruction::Cast) { 00077 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM)); 00078 return VMSlot = ConstantExpr::getCast(MV, CE->getType()); 00079 } else if (CE->getOpcode() == Instruction::GetElementPtr) { 00080 std::vector<Constant*> Idx; 00081 Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM)); 00082 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 00083 Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM))); 00084 return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx); 00085 } else if (CE->getOpcode() == Instruction::Select) { 00086 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM)); 00087 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM)); 00088 Constant *MV3 = cast<Constant>(MapValue(CE->getOperand(2), VM)); 00089 return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3); 00090 } else { 00091 assert(CE->getNumOperands() == 2 && "Must be binary operator?"); 00092 Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM)); 00093 Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM)); 00094 return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2); 00095 } 00096 00097 } else { 00098 assert(0 && "Unknown type of constant!"); 00099 } 00100 } 00101 00102 V->dump(); 00103 assert(0 && "Unknown value type: why didn't it get resolved?!"); 00104 return 0; 00105 } 00106 00107 /// RemapInstruction - Convert the instruction operands from referencing the 00108 /// current values into those specified by ValueMap. 00109 /// 00110 void llvm::RemapInstruction(Instruction *I, 00111 std::map<const Value *, Value*> &ValueMap) { 00112 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { 00113 const Value *Op = I->getOperand(op); 00114 Value *V = MapValue(Op, ValueMap); 00115 #ifndef NDEBUG 00116 if (!V) { 00117 std::cerr << "Val = \n" << *Op << "Addr = " << (void*)Op; 00118 std::cerr << "\nInst = " << *I; 00119 } 00120 #endif 00121 assert(V && "Referenced value not in value map!"); 00122 I->setOperand(op, V); 00123 } 00124 }