LLVM API Documentation
00001 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // 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 "llvm/Transforms/Utils/ValueMapper.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/GlobalValue.h" 00018 #include "llvm/Instruction.h" 00019 using namespace llvm; 00020 00021 Value *llvm::MapValue(const Value *V, ValueMapTy &VM) { 00022 Value *&VMSlot = VM[V]; 00023 if (VMSlot) return VMSlot; // Does it exist in the map yet? 00024 00025 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the 00026 // DenseMap. This includes any recursive calls to MapValue. 00027 00028 // Global values do not need to be seeded into the ValueMap if they are using 00029 // the identity mapping. 00030 if (isa<GlobalValue>(V) || isa<InlineAsm>(V)) 00031 return VMSlot = const_cast<Value*>(V); 00032 00033 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) { 00034 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || 00035 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || 00036 isa<UndefValue>(C)) 00037 return VMSlot = C; // Primitive constants map directly 00038 else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { 00039 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); 00040 i != e; ++i) { 00041 Value *MV = MapValue(*i, VM); 00042 if (MV != *i) { 00043 // This array must contain a reference to a global, make a new array 00044 // and return it. 00045 // 00046 std::vector<Constant*> Values; 00047 Values.reserve(CA->getNumOperands()); 00048 for (User::op_iterator j = b; j != i; ++j) 00049 Values.push_back(cast<Constant>(*j)); 00050 Values.push_back(cast<Constant>(MV)); 00051 for (++i; i != e; ++i) 00052 Values.push_back(cast<Constant>(MapValue(*i, VM))); 00053 return VM[V] = ConstantArray::get(CA->getType(), Values); 00054 } 00055 } 00056 return VM[V] = C; 00057 00058 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 00059 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); 00060 i != e; ++i) { 00061 Value *MV = MapValue(*i, VM); 00062 if (MV != *i) { 00063 // This struct must contain a reference to a global, make a new struct 00064 // and return it. 00065 // 00066 std::vector<Constant*> Values; 00067 Values.reserve(CS->getNumOperands()); 00068 for (User::op_iterator j = b; j != i; ++j) 00069 Values.push_back(cast<Constant>(*j)); 00070 Values.push_back(cast<Constant>(MV)); 00071 for (++i; i != e; ++i) 00072 Values.push_back(cast<Constant>(MapValue(*i, VM))); 00073 return VM[V] = ConstantStruct::get(CS->getType(), Values); 00074 } 00075 } 00076 return VM[V] = C; 00077 00078 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 00079 std::vector<Constant*> Ops; 00080 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) 00081 Ops.push_back(cast<Constant>(MapValue(*i, VM))); 00082 return VM[V] = CE->getWithOperands(Ops); 00083 } else if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) { 00084 for (User::op_iterator b = CP->op_begin(), i = b, e = CP->op_end(); 00085 i != e; ++i) { 00086 Value *MV = MapValue(*i, VM); 00087 if (MV != *i) { 00088 // This vector value must contain a reference to a global, make a new 00089 // vector constant and return it. 00090 // 00091 std::vector<Constant*> Values; 00092 Values.reserve(CP->getNumOperands()); 00093 for (User::op_iterator j = b; j != i; ++j) 00094 Values.push_back(cast<Constant>(*j)); 00095 Values.push_back(cast<Constant>(MV)); 00096 for (++i; i != e; ++i) 00097 Values.push_back(cast<Constant>(MapValue(*i, VM))); 00098 return VM[V] = ConstantVector::get(Values); 00099 } 00100 } 00101 return VM[V] = C; 00102 00103 } else { 00104 assert(0 && "Unknown type of constant!"); 00105 } 00106 } 00107 00108 return 0; 00109 } 00110 00111 /// RemapInstruction - Convert the instruction operands from referencing the 00112 /// current values into those specified by ValueMap. 00113 /// 00114 void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { 00115 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { 00116 Value *V = MapValue(*op, ValueMap); 00117 assert(V && "Referenced value not in value map!"); 00118 *op = V; 00119 } 00120 }