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