ValueMapper.cpp revision 589423cd634b0836c0f2ec4c8dbe28b8807916f1
1//===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 "llvm/Transforms/Utils/ValueMapper.h" 16#include "llvm/DerivedTypes.h" // For getNullValue(Type::Int32Ty) 17#include "llvm/Constants.h" 18#include "llvm/Function.h" 19#include "llvm/Metadata.h" 20#include "llvm/ADT/SmallVector.h" 21#include "llvm/Support/ErrorHandling.h" 22using namespace llvm; 23 24Value *llvm::MapValue(const Value *V, ValueMapTy &VM) { 25 Value *&VMSlot = VM[V]; 26 if (VMSlot) return VMSlot; // Does it exist in the map yet? 27 28 // NOTE: VMSlot can be invalidated by any reference to VM, which can grow the 29 // DenseMap. This includes any recursive calls to MapValue. 30 31 // Global values and non-function-local metadata do not need to be seeded into 32 // the ValueMap if they are using the identity mapping. 33 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) || 34 (isa<MDNode>(V) && !dyn_cast<MDNode>(V)->isFunctionLocal())) 35 return VMSlot = const_cast<Value*>(V); 36 37 if (isa<MDNode>(V)) { 38 const MDNode *MD = dyn_cast<MDNode>(V); 39 SmallVector<Value*, 4> Elts; 40 for (unsigned i = 0; i != MD->getNumOperands(); i++) 41 Elts.push_back(MD->getOperand(i) ? MapValue(MD->getOperand(i), VM) : NULL); 42 return VM[V] = MDNode::get(V->getContext(), Elts.data(), Elts.size()); 43 } 44 45 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); 46 if (C == 0) return 0; 47 48 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || 49 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || 50 isa<UndefValue>(C) || isa<MDString>(C)) 51 return VMSlot = C; // Primitive constants map directly 52 53 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { 54 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); 55 i != e; ++i) { 56 Value *MV = MapValue(*i, VM); 57 if (MV != *i) { 58 // This array must contain a reference to a global, make a new array 59 // and return it. 60 // 61 std::vector<Constant*> Values; 62 Values.reserve(CA->getNumOperands()); 63 for (User::op_iterator j = b; j != i; ++j) 64 Values.push_back(cast<Constant>(*j)); 65 Values.push_back(cast<Constant>(MV)); 66 for (++i; i != e; ++i) 67 Values.push_back(cast<Constant>(MapValue(*i, VM))); 68 return VM[V] = ConstantArray::get(CA->getType(), Values); 69 } 70 } 71 return VM[V] = C; 72 } 73 74 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 75 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); 76 i != e; ++i) { 77 Value *MV = MapValue(*i, VM); 78 if (MV != *i) { 79 // This struct must contain a reference to a global, make a new struct 80 // and return it. 81 // 82 std::vector<Constant*> Values; 83 Values.reserve(CS->getNumOperands()); 84 for (User::op_iterator j = b; j != i; ++j) 85 Values.push_back(cast<Constant>(*j)); 86 Values.push_back(cast<Constant>(MV)); 87 for (++i; i != e; ++i) 88 Values.push_back(cast<Constant>(MapValue(*i, VM))); 89 return VM[V] = ConstantStruct::get(CS->getType(), Values); 90 } 91 } 92 return VM[V] = C; 93 } 94 95 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 96 std::vector<Constant*> Ops; 97 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) 98 Ops.push_back(cast<Constant>(MapValue(*i, VM))); 99 return VM[V] = CE->getWithOperands(Ops); 100 } 101 102 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) { 103 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end(); 104 i != e; ++i) { 105 Value *MV = MapValue(*i, VM); 106 if (MV != *i) { 107 // This vector value must contain a reference to a global, make a new 108 // vector constant and return it. 109 // 110 std::vector<Constant*> Values; 111 Values.reserve(CV->getNumOperands()); 112 for (User::op_iterator j = b; j != i; ++j) 113 Values.push_back(cast<Constant>(*j)); 114 Values.push_back(cast<Constant>(MV)); 115 for (++i; i != e; ++i) 116 Values.push_back(cast<Constant>(MapValue(*i, VM))); 117 return VM[V] = ConstantVector::get(Values); 118 } 119 } 120 return VM[V] = C; 121 } 122 123 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 124 Function *F = cast<Function>(MapValue(BA->getFunction(), VM)); 125 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM)); 126 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock()); 127 } 128 129 llvm_unreachable("Unknown type of constant!"); 130 return 0; 131} 132 133/// RemapInstruction - Convert the instruction operands from referencing the 134/// current values into those specified by ValueMap. 135/// 136void llvm::RemapInstruction(Instruction *I, ValueMapTy &ValueMap) { 137 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { 138 Value *V = MapValue(*op, ValueMap); 139 assert(V && "Referenced value not in value map!"); 140 *op = V; 141 } 142} 143 144