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