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/Type.h" 17 #include "llvm/Constants.h" 18 #include "llvm/Function.h" 19 #include "llvm/Metadata.h" 20 #include "llvm/ADT/SmallVector.h" 21 using namespace llvm; 22 23 Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, 24 bool ModuleLevelChanges) { 25 TrackingVH<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 do not need to be seeded into the VM if they 32 // are using the identity mapping. 33 if (isa<GlobalValue>(V) || isa<InlineAsm>(V) || isa<MDString>(V) || 34 (isa<MDNode>(V) && !cast<MDNode>(V)->isFunctionLocal() && 35 !ModuleLevelChanges)) 36 return VMSlot = const_cast<Value*>(V); 37 38 if (const MDNode *MD = dyn_cast<MDNode>(V)) { 39 // Start by assuming that we'll use the identity mapping. 40 VMSlot = const_cast<Value*>(V); 41 42 // Check all operands to see if any need to be remapped. 43 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) { 44 Value *OP = MD->getOperand(i); 45 if (!OP || MapValue(OP, VM, ModuleLevelChanges) == OP) continue; 46 47 // Ok, at least one operand needs remapping. 48 MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0); 49 VM[V] = Dummy; 50 SmallVector<Value*, 4> Elts; 51 Elts.reserve(MD->getNumOperands()); 52 for (i = 0; i != e; ++i) 53 Elts.push_back(MD->getOperand(i) ? 54 MapValue(MD->getOperand(i), VM, ModuleLevelChanges) : 0); 55 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size()); 56 Dummy->replaceAllUsesWith(NewMD); 57 MDNode::deleteTemporary(Dummy); 58 return VM[V] = NewMD; 59 } 60 61 // No operands needed remapping; keep the identity map. 62 return const_cast<Value*>(V); 63 } 64 65 Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); 66 if (C == 0) 67 return 0; 68 69 if (isa<ConstantInt>(C) || isa<ConstantFP>(C) || 70 isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C) || 71 isa<UndefValue>(C)) 72 return VMSlot = C; // Primitive constants map directly 73 74 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { 75 for (User::op_iterator b = CA->op_begin(), i = b, e = CA->op_end(); 76 i != e; ++i) { 77 Value *MV = MapValue(*i, VM, ModuleLevelChanges); 78 if (MV != *i) { 79 // This array must contain a reference to a global, make a new array 80 // and return it. 81 // 82 std::vector<Constant*> Values; 83 Values.reserve(CA->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 ModuleLevelChanges))); 90 return VM[V] = ConstantArray::get(CA->getType(), Values); 91 } 92 } 93 return VM[V] = C; 94 } 95 96 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 97 for (User::op_iterator b = CS->op_begin(), i = b, e = CS->op_end(); 98 i != e; ++i) { 99 Value *MV = MapValue(*i, VM, ModuleLevelChanges); 100 if (MV != *i) { 101 // This struct must contain a reference to a global, make a new struct 102 // and return it. 103 // 104 std::vector<Constant*> Values; 105 Values.reserve(CS->getNumOperands()); 106 for (User::op_iterator j = b; j != i; ++j) 107 Values.push_back(cast<Constant>(*j)); 108 Values.push_back(cast<Constant>(MV)); 109 for (++i; i != e; ++i) 110 Values.push_back(cast<Constant>(MapValue(*i, VM, 111 ModuleLevelChanges))); 112 return VM[V] = ConstantStruct::get(CS->getType(), Values); 113 } 114 } 115 return VM[V] = C; 116 } 117 118 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 119 std::vector<Constant*> Ops; 120 for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) 121 Ops.push_back(cast<Constant>(MapValue(*i, VM, ModuleLevelChanges))); 122 return VM[V] = CE->getWithOperands(Ops); 123 } 124 125 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) { 126 for (User::op_iterator b = CV->op_begin(), i = b, e = CV->op_end(); 127 i != e; ++i) { 128 Value *MV = MapValue(*i, VM, ModuleLevelChanges); 129 if (MV != *i) { 130 // This vector value must contain a reference to a global, make a new 131 // vector constant and return it. 132 // 133 std::vector<Constant*> Values; 134 Values.reserve(CV->getNumOperands()); 135 for (User::op_iterator j = b; j != i; ++j) 136 Values.push_back(cast<Constant>(*j)); 137 Values.push_back(cast<Constant>(MV)); 138 for (++i; i != e; ++i) 139 Values.push_back(cast<Constant>(MapValue(*i, VM, 140 ModuleLevelChanges))); 141 return VM[V] = ConstantVector::get(Values); 142 } 143 } 144 return VM[V] = C; 145 } 146 147 BlockAddress *BA = cast<BlockAddress>(C); 148 Function *F = cast<Function>(MapValue(BA->getFunction(), VM, 149 ModuleLevelChanges)); 150 BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(),VM, 151 ModuleLevelChanges)); 152 return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock()); 153 } 154 155 /// RemapInstruction - Convert the instruction operands from referencing the 156 /// current values into those specified by VMap. 157 /// 158 void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap, 159 bool ModuleLevelChanges) { 160 // Remap operands. 161 for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { 162 Value *V = MapValue(*op, VMap, ModuleLevelChanges); 163 assert(V && "Referenced value not in value map!"); 164 *op = V; 165 } 166 167 // Remap attached metadata. 168 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 169 I->getAllMetadata(MDs); 170 for (SmallVectorImpl<std::pair<unsigned, MDNode *> >::iterator 171 MI = MDs.begin(), ME = MDs.end(); MI != ME; ++MI) { 172 Value *Old = MI->second; 173 Value *New = MapValue(Old, VMap, ModuleLevelChanges); 174 if (New != Old) 175 I->setMetadata(MI->first, cast<MDNode>(New)); 176 } 177 } 178