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