Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 1 | //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the MapValue function, which is shared by various parts of |
| 11 | // the lib/Transforms/Utils library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dan Gohman | a209503 | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/ValueMapper.h" |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 16 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Function.h" |
| 19 | #include "llvm/IR/InlineAsm.h" |
| 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/Metadata.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 23 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 24 | // Out of line method to get vtable etc for class. |
Craig Topper | 2a6a08b | 2012-09-26 06:36:36 +0000 | [diff] [blame] | 25 | void ValueMapTypeRemapper::anchor() {} |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 26 | void ValueMaterializer::anchor() {} |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 27 | |
| 28 | Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 29 | ValueMapTypeRemapper *TypeMapper, |
| 30 | ValueMaterializer *Materializer) { |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 31 | ValueToValueMapTy::iterator I = VM.find(V); |
Chris Lattner | 1bfc7ab | 2007-02-03 00:08:31 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 33 | // If the value already exists in the map, use it. |
| 34 | if (I != VM.end() && I->second) return I->second; |
| 35 | |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 36 | // If we have a materializer and it can materialize a value, use that. |
| 37 | if (Materializer) { |
| 38 | if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V))) |
| 39 | return VM[V] = NewV; |
| 40 | } |
| 41 | |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 42 | // Global values do not need to be seeded into the VM if they |
| 43 | // are using the identity mapping. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 44 | if (isa<GlobalValue>(V)) |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 45 | return VM[V] = const_cast<Value*>(V); |
Chris Lattner | 8b4cf5e | 2011-07-15 23:18:40 +0000 | [diff] [blame] | 46 | |
| 47 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
| 48 | // Inline asm may need *type* remapping. |
| 49 | FunctionType *NewTy = IA->getFunctionType(); |
| 50 | if (TypeMapper) { |
| 51 | NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy)); |
| 52 | |
| 53 | if (NewTy != IA->getFunctionType()) |
| 54 | V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(), |
| 55 | IA->hasSideEffects(), IA->isAlignStack()); |
| 56 | } |
| 57 | |
| 58 | return VM[V] = const_cast<Value*>(V); |
| 59 | } |
Chris Lattner | 6aa34b0 | 2003-10-06 15:23:43 +0000 | [diff] [blame] | 60 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 61 | if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) { |
| 62 | const Metadata *MD = MDV->getMetadata(); |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 63 | // If this is a module-level metadata and we know that nothing at the module |
| 64 | // level is changing, then use an identity mapping. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 65 | if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges)) |
| 66 | return VM[V] = const_cast<Value *>(V); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 67 | |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 68 | auto *MappedMD = MapMetadata(MD, VM, Flags, TypeMapper, Materializer); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 69 | if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries))) |
| 70 | return VM[V] = const_cast<Value *>(V); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 71 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 72 | // FIXME: This assert crashes during bootstrap, but I think it should be |
| 73 | // correct. For now, just match behaviour from before the metadata/value |
| 74 | // split. |
| 75 | // |
| 76 | // assert(MappedMD && "Referenced metadata value not in value map"); |
| 77 | return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD); |
Victor Hernandez | 5fa88d4 | 2010-01-20 05:49:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 80 | // Okay, this either must be a constant (which may or may not be mappable) or |
| 81 | // is something that is not in the mapping table. |
Chris Lattner | cf5a47d | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 82 | Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 83 | if (!C) |
| 84 | return nullptr; |
Chris Lattner | cf5a47d | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 86 | if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 87 | Function *F = |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 88 | cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer)); |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 89 | BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 90 | Flags, TypeMapper, Materializer)); |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 91 | return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock()); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 92 | } |
Chris Lattner | cf5a47d | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 93 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 94 | // Otherwise, we have some other constant to remap. Start by checking to see |
| 95 | // if all operands have an identity remapping. |
| 96 | unsigned OpNo = 0, NumOperands = C->getNumOperands(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 97 | Value *Mapped = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 98 | for (; OpNo != NumOperands; ++OpNo) { |
| 99 | Value *Op = C->getOperand(OpNo); |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 100 | Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 101 | if (Mapped != C) break; |
Chris Lattner | cf5a47d | 2009-10-29 00:28:30 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 103 | |
| 104 | // See if the type mapper wants to remap the type as well. |
| 105 | Type *NewTy = C->getType(); |
| 106 | if (TypeMapper) |
| 107 | NewTy = TypeMapper->remapType(NewTy); |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 108 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 109 | // If the result type and all operands match up, then just insert an identity |
| 110 | // mapping. |
| 111 | if (OpNo == NumOperands && NewTy == C->getType()) |
| 112 | return VM[V] = C; |
| 113 | |
| 114 | // Okay, we need to create a new constant. We've already processed some or |
| 115 | // all of the operands, set them all up now. |
| 116 | SmallVector<Constant*, 8> Ops; |
| 117 | Ops.reserve(NumOperands); |
| 118 | for (unsigned j = 0; j != OpNo; ++j) |
| 119 | Ops.push_back(cast<Constant>(C->getOperand(j))); |
| 120 | |
| 121 | // If one of the operands mismatch, push it and the other mapped operands. |
| 122 | if (OpNo != NumOperands) { |
| 123 | Ops.push_back(cast<Constant>(Mapped)); |
| 124 | |
| 125 | // Map the rest of the operands that aren't processed yet. |
| 126 | for (++OpNo; OpNo != NumOperands; ++OpNo) |
| 127 | Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 128 | Flags, TypeMapper, Materializer)); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 132 | return VM[V] = CE->getWithOperands(Ops, NewTy); |
| 133 | if (isa<ConstantArray>(C)) |
| 134 | return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops); |
| 135 | if (isa<ConstantStruct>(C)) |
| 136 | return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops); |
| 137 | if (isa<ConstantVector>(C)) |
| 138 | return VM[V] = ConstantVector::get(Ops); |
| 139 | // If this is a no-operand constant, it must be because the type was remapped. |
| 140 | if (isa<UndefValue>(C)) |
| 141 | return VM[V] = UndefValue::get(NewTy); |
| 142 | if (isa<ConstantAggregateZero>(C)) |
| 143 | return VM[V] = ConstantAggregateZero::get(NewTy); |
| 144 | assert(isa<ConstantPointerNull>(C)); |
| 145 | return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy)); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 146 | } |
Brian Gaeke | 6182acf | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 147 | |
Kaelyn Takata | 22324f3 | 2014-12-09 23:32:46 +0000 | [diff] [blame] | 148 | static Metadata *mapToMetadata(ValueToValueMapTy &VM, const Metadata *Key, |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 149 | Metadata *Val) { |
| 150 | VM.MD()[Key].reset(Val); |
| 151 | return Val; |
| 152 | } |
| 153 | |
| 154 | static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) { |
Kaelyn Takata | 22324f3 | 2014-12-09 23:32:46 +0000 | [diff] [blame] | 155 | return mapToMetadata(VM, MD, const_cast<Metadata *>(MD)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 158 | static Metadata *MapMetadataImpl(const Metadata *MD, |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 159 | SmallVectorImpl<TrackingMDNodeRef> &Cycles, |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 160 | ValueToValueMapTy &VM, RemapFlags Flags, |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 161 | ValueMapTypeRemapper *TypeMapper, |
Duncan P. N. Exon Smith | 077affd | 2015-01-14 01:01:19 +0000 | [diff] [blame] | 162 | ValueMaterializer *Materializer); |
| 163 | |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 164 | static Metadata *mapMetadataOp(Metadata *Op, |
| 165 | SmallVectorImpl<TrackingMDNodeRef> &Cycles, |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 166 | ValueToValueMapTy &VM, RemapFlags Flags, |
Duncan P. N. Exon Smith | 422e5c7 | 2015-01-19 22:16:01 +0000 | [diff] [blame] | 167 | ValueMapTypeRemapper *TypeMapper, |
| 168 | ValueMaterializer *Materializer) { |
Duncan P. N. Exon Smith | 077affd | 2015-01-14 01:01:19 +0000 | [diff] [blame] | 169 | if (!Op) |
| 170 | return nullptr; |
| 171 | if (Metadata *MappedOp = |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 172 | MapMetadataImpl(Op, Cycles, VM, Flags, TypeMapper, Materializer)) |
Duncan P. N. Exon Smith | 077affd | 2015-01-14 01:01:19 +0000 | [diff] [blame] | 173 | return MappedOp; |
| 174 | // Use identity map if MappedOp is null and we can ignore missing entries. |
| 175 | if (Flags & RF_IgnoreMissingEntries) |
| 176 | return Op; |
| 177 | |
| 178 | // FIXME: This assert crashes during bootstrap, but I think it should be |
| 179 | // correct. For now, just match behaviour from before the metadata/value |
| 180 | // split. |
| 181 | // |
| 182 | // llvm_unreachable("Referenced metadata not in value map!"); |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
Duncan P. N. Exon Smith | 6dc22bf | 2015-01-19 22:44:32 +0000 | [diff] [blame] | 186 | /// \brief Remap nodes. |
| 187 | /// |
| 188 | /// Insert \c NewNode in the value map, and then remap \c OldNode's operands. |
| 189 | /// Assumes that \c NewNode is already a clone of \c OldNode. |
| 190 | /// |
| 191 | /// \pre \c NewNode is a clone of \c OldNode. |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 192 | static bool remap(const MDNode *OldNode, MDNode *NewNode, |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 193 | SmallVectorImpl<TrackingMDNodeRef> &Cycles, |
| 194 | ValueToValueMapTy &VM, RemapFlags Flags, |
| 195 | ValueMapTypeRemapper *TypeMapper, |
Duncan P. N. Exon Smith | 6dc22bf | 2015-01-19 22:44:32 +0000 | [diff] [blame] | 196 | ValueMaterializer *Materializer) { |
| 197 | assert(OldNode->getNumOperands() == NewNode->getNumOperands() && |
| 198 | "Expected nodes to match"); |
| 199 | assert(OldNode->isResolved() && "Expected resolved node"); |
| 200 | assert(!NewNode->isUniqued() && "Expected non-uniqued node"); |
| 201 | |
| 202 | // Map the node upfront so it's available for cyclic references. |
| 203 | mapToMetadata(VM, OldNode, NewNode); |
| 204 | bool AnyChanged = false; |
| 205 | for (unsigned I = 0, E = OldNode->getNumOperands(); I != E; ++I) { |
| 206 | Metadata *Old = OldNode->getOperand(I); |
| 207 | assert(NewNode->getOperand(I) == Old && |
| 208 | "Expected old operands to already be in place"); |
| 209 | |
Duncan P. N. Exon Smith | 0880014 | 2015-08-03 03:24:28 +0000 | [diff] [blame] | 210 | Metadata *New = |
| 211 | mapMetadataOp(Old, Cycles, VM, Flags, TypeMapper, Materializer); |
Duncan P. N. Exon Smith | 6dc22bf | 2015-01-19 22:44:32 +0000 | [diff] [blame] | 212 | if (Old != New) { |
| 213 | AnyChanged = true; |
| 214 | NewNode->replaceOperandWith(I, New); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return AnyChanged; |
| 219 | } |
| 220 | |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 221 | /// Map a distinct MDNode. |
Duncan P. N. Exon Smith | 14cc94c | 2015-01-14 01:03:05 +0000 | [diff] [blame] | 222 | /// |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 223 | /// Whether distinct nodes change is independent of their operands. If \a |
| 224 | /// RF_MoveDistinctMDs, then they are reused, and their operands remapped in |
| 225 | /// place; effectively, they're moved from one graph to another. Otherwise, |
| 226 | /// they're cloned/duplicated, and the new copy's operands are remapped. |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 227 | static Metadata *mapDistinctNode(const MDNode *Node, |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 228 | SmallVectorImpl<TrackingMDNodeRef> &Cycles, |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 229 | ValueToValueMapTy &VM, RemapFlags Flags, |
Duncan P. N. Exon Smith | 14cc94c | 2015-01-14 01:03:05 +0000 | [diff] [blame] | 230 | ValueMapTypeRemapper *TypeMapper, |
| 231 | ValueMaterializer *Materializer) { |
| 232 | assert(Node->isDistinct() && "Expected distinct node"); |
| 233 | |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 234 | MDNode *NewMD; |
| 235 | if (Flags & RF_MoveDistinctMDs) |
| 236 | NewMD = const_cast<MDNode *>(Node); |
| 237 | else |
| 238 | NewMD = MDNode::replaceWithDistinct(Node->clone()); |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 239 | |
Duncan P. N. Exon Smith | 50f8969 | 2015-08-03 03:45:32 +0000 | [diff] [blame] | 240 | // Remap the operands. If any change, track those that could be involved in |
| 241 | // uniquing cycles. |
| 242 | if (remap(Node, NewMD, Cycles, VM, Flags, TypeMapper, Materializer)) |
| 243 | for (Metadata *Op : NewMD->operands()) |
| 244 | if (auto *Node = dyn_cast_or_null<MDNode>(Op)) |
| 245 | if (!Node->isResolved()) |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 246 | Cycles.emplace_back(Node); |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 247 | |
Duncan P. N. Exon Smith | 0dcffe2 | 2015-01-19 22:39:07 +0000 | [diff] [blame] | 248 | return NewMD; |
Duncan P. N. Exon Smith | fb9d128 | 2015-01-14 01:08:47 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Duncan P. N. Exon Smith | b557989 | 2015-01-14 01:06:21 +0000 | [diff] [blame] | 251 | /// \brief Map a uniqued MDNode. |
| 252 | /// |
| 253 | /// Uniqued nodes may not need to be recreated (they may map to themselves). |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 254 | static Metadata *mapUniquedNode(const MDNode *Node, |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 255 | SmallVectorImpl<TrackingMDNodeRef> &Cycles, |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 256 | ValueToValueMapTy &VM, RemapFlags Flags, |
Duncan P. N. Exon Smith | c862be8 | 2015-01-19 22:40:25 +0000 | [diff] [blame] | 257 | ValueMapTypeRemapper *TypeMapper, |
| 258 | ValueMaterializer *Materializer) { |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 259 | assert(Node->isUniqued() && "Expected uniqued node"); |
Duncan P. N. Exon Smith | b557989 | 2015-01-14 01:06:21 +0000 | [diff] [blame] | 260 | |
Duncan P. N. Exon Smith | 0dcffe2 | 2015-01-19 22:39:07 +0000 | [diff] [blame] | 261 | // Create a temporary node upfront in case we have a metadata cycle. |
Duncan P. N. Exon Smith | 03e0583 | 2015-01-20 02:56:57 +0000 | [diff] [blame] | 262 | auto ClonedMD = Node->clone(); |
Duncan P. N. Exon Smith | 1de9ccb | 2015-08-04 13:24:26 +0000 | [diff] [blame^] | 263 | if (!remap(Node, ClonedMD.get(), Cycles, VM, Flags, TypeMapper, |
| 264 | Materializer)) { |
Duncan P. N. Exon Smith | 6dc22bf | 2015-01-19 22:44:32 +0000 | [diff] [blame] | 265 | // No operands changed, so use the identity mapping. |
Duncan P. N. Exon Smith | 706f37e | 2015-08-04 06:42:31 +0000 | [diff] [blame] | 266 | ClonedMD->replaceAllUsesWith(const_cast<MDNode *>(Node)); |
Duncan P. N. Exon Smith | 0dcffe2 | 2015-01-19 22:39:07 +0000 | [diff] [blame] | 267 | return mapToSelf(VM, Node); |
Duncan P. N. Exon Smith | 706f37e | 2015-08-04 06:42:31 +0000 | [diff] [blame] | 268 | } |
Duncan P. N. Exon Smith | 0dcffe2 | 2015-01-19 22:39:07 +0000 | [diff] [blame] | 269 | |
| 270 | // At least one operand has changed, so uniquify the cloned node. |
| 271 | return mapToMetadata(VM, Node, |
| 272 | MDNode::replaceWithUniqued(std::move(ClonedMD))); |
Duncan P. N. Exon Smith | b557989 | 2015-01-14 01:06:21 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 275 | static Metadata *MapMetadataImpl(const Metadata *MD, |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 276 | SmallVectorImpl<TrackingMDNodeRef> &Cycles, |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 277 | ValueToValueMapTy &VM, RemapFlags Flags, |
Duncan P. N. Exon Smith | 077affd | 2015-01-14 01:01:19 +0000 | [diff] [blame] | 278 | ValueMapTypeRemapper *TypeMapper, |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 279 | ValueMaterializer *Materializer) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 280 | // If the value already exists in the map, use it. |
| 281 | if (Metadata *NewMD = VM.MD().lookup(MD).get()) |
| 282 | return NewMD; |
| 283 | |
| 284 | if (isa<MDString>(MD)) |
| 285 | return mapToSelf(VM, MD); |
| 286 | |
| 287 | if (isa<ConstantAsMetadata>(MD)) |
| 288 | if ((Flags & RF_NoModuleLevelChanges)) |
| 289 | return mapToSelf(VM, MD); |
| 290 | |
| 291 | if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) { |
| 292 | Value *MappedV = |
| 293 | MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer); |
| 294 | if (VMD->getValue() == MappedV || |
| 295 | (!MappedV && (Flags & RF_IgnoreMissingEntries))) |
| 296 | return mapToSelf(VM, MD); |
| 297 | |
| 298 | // FIXME: This assert crashes during bootstrap, but I think it should be |
| 299 | // correct. For now, just match behaviour from before the metadata/value |
| 300 | // split. |
| 301 | // |
| 302 | // assert(MappedV && "Referenced metadata not in value map!"); |
| 303 | if (MappedV) |
Kaelyn Takata | 22324f3 | 2014-12-09 23:32:46 +0000 | [diff] [blame] | 304 | return mapToMetadata(VM, MD, ValueAsMetadata::get(MappedV)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 305 | return nullptr; |
| 306 | } |
| 307 | |
Duncan P. N. Exon Smith | 170c26d | 2015-03-17 01:14:40 +0000 | [diff] [blame] | 308 | // Note: this cast precedes the Flags check so we always get its associated |
| 309 | // assertion. |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 310 | const MDNode *Node = cast<MDNode>(MD); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 311 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 312 | // If this is a module-level metadata and we know that nothing at the |
| 313 | // module level is changing, then use an identity mapping. |
| 314 | if (Flags & RF_NoModuleLevelChanges) |
| 315 | return mapToSelf(VM, MD); |
| 316 | |
Duncan P. N. Exon Smith | 170c26d | 2015-03-17 01:14:40 +0000 | [diff] [blame] | 317 | // Require resolved nodes whenever metadata might be remapped. |
| 318 | assert(Node->isResolved() && "Unexpected unresolved node"); |
| 319 | |
Duncan P. N. Exon Smith | 14cc94c | 2015-01-14 01:03:05 +0000 | [diff] [blame] | 320 | if (Node->isDistinct()) |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 321 | return mapDistinctNode(Node, Cycles, VM, Flags, TypeMapper, Materializer); |
Duncan P. N. Exon Smith | 953e1a4 | 2015-01-08 22:42:30 +0000 | [diff] [blame] | 322 | |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 323 | return mapUniquedNode(Node, Cycles, VM, Flags, TypeMapper, Materializer); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 326 | Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, |
| 327 | RemapFlags Flags, ValueMapTypeRemapper *TypeMapper, |
| 328 | ValueMaterializer *Materializer) { |
Duncan P. N. Exon Smith | 5ed90c0 | 2015-08-04 13:23:30 +0000 | [diff] [blame] | 329 | SmallVector<TrackingMDNodeRef, 8> Cycles; |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 330 | Metadata *NewMD = |
| 331 | MapMetadataImpl(MD, Cycles, VM, Flags, TypeMapper, Materializer); |
| 332 | |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 333 | if ((Flags & RF_NoModuleLevelChanges) || |
| 334 | (MD == NewMD && !(Flags & RF_MoveDistinctMDs))) { |
| 335 | assert(Cycles.empty() && "Unresolved cycles without remapping anything?"); |
| 336 | return NewMD; |
Duncan P. N. Exon Smith | 920df5c | 2015-02-04 19:44:34 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Duncan P. N. Exon Smith | 4fb46cb | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 339 | if (auto *N = dyn_cast<MDNode>(NewMD)) |
| 340 | if (!N->isResolved()) |
| 341 | N->resolveCycles(); |
| 342 | |
| 343 | // Resolve cycles underneath MD. |
| 344 | for (MDNode *N : Cycles) |
| 345 | if (!N->isResolved()) |
| 346 | N->resolveCycles(); |
| 347 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 348 | return NewMD; |
| 349 | } |
| 350 | |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 351 | MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM, |
| 352 | RemapFlags Flags, ValueMapTypeRemapper *TypeMapper, |
| 353 | ValueMaterializer *Materializer) { |
| 354 | return cast<MDNode>(MapMetadata(static_cast<const Metadata *>(MD), VM, Flags, |
| 355 | TypeMapper, Materializer)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Brian Gaeke | 6182acf | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 358 | /// RemapInstruction - Convert the instruction operands from referencing the |
Devang Patel | b8f11de | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 359 | /// current values into those specified by VMap. |
Brian Gaeke | 6182acf | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 360 | /// |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 361 | void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap, |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 362 | RemapFlags Flags, ValueMapTypeRemapper *TypeMapper, |
| 363 | ValueMaterializer *Materializer){ |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 364 | // Remap operands. |
Gabor Greif | 5df4326 | 2008-05-30 21:24:22 +0000 | [diff] [blame] | 365 | for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) { |
James Molloy | f6f121e | 2013-05-28 15:17:05 +0000 | [diff] [blame] | 366 | Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer); |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 367 | // If we aren't ignoring missing entries, assert that something happened. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 368 | if (V) |
Chris Lattner | 43f8d16 | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 369 | *op = V; |
| 370 | else |
| 371 | assert((Flags & RF_IgnoreMissingEntries) && |
| 372 | "Referenced value not in value map!"); |
Brian Gaeke | 6182acf | 2004-05-19 09:08:12 +0000 | [diff] [blame] | 373 | } |
Daniel Dunbar | 95fe13c | 2010-08-26 03:48:08 +0000 | [diff] [blame] | 374 | |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 375 | // Remap phi nodes' incoming blocks. |
| 376 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 377 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 378 | Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags); |
| 379 | // If we aren't ignoring missing entries, assert that something happened. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 380 | if (V) |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 381 | PN->setIncomingBlock(i, cast<BasicBlock>(V)); |
| 382 | else |
| 383 | assert((Flags & RF_IgnoreMissingEntries) && |
| 384 | "Referenced block not in value map!"); |
| 385 | } |
| 386 | } |
| 387 | |
Devang Patel | c017404 | 2011-08-04 20:02:18 +0000 | [diff] [blame] | 388 | // Remap attached metadata. |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 389 | SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; |
Devang Patel | c017404 | 2011-08-04 20:02:18 +0000 | [diff] [blame] | 390 | I->getAllMetadata(MDs); |
Duncan P. N. Exon Smith | e08bcbf | 2015-08-03 03:27:12 +0000 | [diff] [blame] | 391 | for (const auto &MI : MDs) { |
| 392 | MDNode *Old = MI.second; |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 393 | MDNode *New = MapMetadata(Old, VMap, Flags, TypeMapper, Materializer); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 394 | if (New != Old) |
Duncan P. N. Exon Smith | e08bcbf | 2015-08-03 03:27:12 +0000 | [diff] [blame] | 395 | I->setMetadata(MI.first, New); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 396 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 397 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 398 | if (!TypeMapper) |
| 399 | return; |
| 400 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 401 | // If the instruction's type is being remapped, do so now. |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 402 | if (auto CS = CallSite(I)) { |
| 403 | SmallVector<Type *, 3> Tys; |
| 404 | FunctionType *FTy = CS.getFunctionType(); |
| 405 | Tys.reserve(FTy->getNumParams()); |
| 406 | for (Type *Ty : FTy->params()) |
| 407 | Tys.push_back(TypeMapper->remapType(Ty)); |
| 408 | CS.mutateFunctionType(FunctionType::get( |
| 409 | TypeMapper->remapType(I->getType()), Tys, FTy->isVarArg())); |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | if (auto *AI = dyn_cast<AllocaInst>(I)) |
| 413 | AI->setAllocatedType(TypeMapper->remapType(AI->getAllocatedType())); |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 414 | if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
David Blaikie | 73cf872 | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 415 | GEP->setSourceElementType( |
| 416 | TypeMapper->remapType(GEP->getSourceElementType())); |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 417 | GEP->setResultElementType( |
| 418 | TypeMapper->remapType(GEP->getResultElementType())); |
| 419 | } |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 420 | I->mutateType(TypeMapper->remapType(I->getType())); |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 421 | } |