llvm.org GIT mirror llvm / f9eaea7
IR: Return unique_ptr from MDNode::getTemporary() Change `MDTuple::getTemporary()` and `MDLocation::getTemporary()` to return (effectively) `std::unique_ptr<T, MDNode::deleteTemporary>`, and clean up call sites. (For now, `DIBuilder` call sites just call `release()` immediately.) There's an accompanying change in each of clang and polly to use the new API. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226504 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
10 changed file(s) with 81 addition(s) and 85 deletion(s). Raw diff Collapse all Expand all
6565
6666 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
6767 unsigned Count) {
68 return wrap(MDNode::getTemporary(*unwrap(C),
69 ArrayRef(unwrap(MDs), Count)));
68 return wrap(MDTuple::getTemporary(*unwrap(C),
69 ArrayRef(unwrap(MDs), Count))
70 .release());
7071 }
7172
7273 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
650650 }
651651 };
652652
653 template
654 struct TempMDNodeDeleter {
655 inline void operator()(T *Node) const;
656 };
657
658 #define HANDLE_UNIQUABLE_LEAF(CLASS) \
659 typedef std::unique_ptr> Temp##CLASS;
660 #include "llvm/IR/Metadata.def"
661
653662 //===----------------------------------------------------------------------===//
654663 /// \brief Tuple of metadata.
655664 class MDNode : public Metadata {
696705 ArrayRef MDs);
697706 static inline MDTuple *getDistinct(LLVMContext &Context,
698707 ArrayRef MDs);
699 static inline MDTuple *getTemporary(LLVMContext &Context,
700 ArrayRef MDs);
708 static inline TempMDTuple getTemporary(LLVMContext &Context,
709 ArrayRef MDs);
701710
702711 /// \brief Deallocate a node created by getTemporary.
703712 ///
883892 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
884893 /// not uniqued, may be RAUW'd, and must be manually deleted with
885894 /// deleteTemporary.
886 static MDTuple *getTemporary(LLVMContext &Context, ArrayRef MDs) {
887 return getImpl(Context, MDs, Temporary);
895 static TempMDTuple getTemporary(LLVMContext &Context,
896 ArrayRef MDs) {
897 return TempMDTuple(getImpl(Context, MDs, Temporary));
888898 }
889899
890900 static bool classof(const Metadata *MD) {
905915 MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef MDs) {
906916 return MDTuple::getDistinct(Context, MDs);
907917 }
908 MDTuple *MDNode::getTemporary(LLVMContext &Context, ArrayRef MDs) {
918 TempMDTuple MDNode::getTemporary(LLVMContext &Context,
919 ArrayRef MDs) {
909920 return MDTuple::getTemporary(Context, MDs);
921 }
922 template
923 void TempMDNodeDeleter::operator()(T *Node) const {
924 MDNode::deleteTemporary(Node);
910925 }
911926
912927 /// \brief Debug location.
944959 Metadata *InlinedAt = nullptr) {
945960 return getImpl(Context, Line, Column, Scope, InlinedAt, Distinct);
946961 }
947 static MDLocation *getTemporary(LLVMContext &Context, unsigned Line,
948 unsigned Column, Metadata *Scope,
949 Metadata *InlinedAt = nullptr) {
950 return getImpl(Context, Line, Column, Scope, InlinedAt, Temporary);
962 static TempMDLocation getTemporary(LLVMContext &Context, unsigned Line,
963 unsigned Column, Metadata *Scope,
964 Metadata *InlinedAt = nullptr) {
965 return TempMDLocation(
966 getImpl(Context, Line, Column, Scope, InlinedAt, Temporary));
951967 }
952968
953969 unsigned getLine() const { return MDNodeSubclassData; }
530530 }
531531
532532 // Otherwise, create MDNode forward reference.
533 MDTuple *FwdNode = MDTuple::getTemporary(Context, None);
534 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
533 auto &FwdRef = ForwardRefMDNodes[MID];
534 FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), Lex.getLoc());
535535
536536 if (NumberedMetadata.size() <= MID)
537537 NumberedMetadata.resize(MID+1);
538 NumberedMetadata[MID].reset(FwdNode);
539 Result = FwdNode;
538 Result = FwdRef.first.get();
539 NumberedMetadata[MID].reset(Result);
540540 return false;
541541 }
542542
596596 // See if this was forward referenced, if so, handle it.
597597 auto FI = ForwardRefMDNodes.find(MetadataID);
598598 if (FI != ForwardRefMDNodes.end()) {
599 MDTuple *Temp = FI->second.first;
600 Temp->replaceAllUsesWith(Init);
601 MDNode::deleteTemporary(Temp);
599 FI->second.first->replaceAllUsesWith(Init);
602600 ForwardRefMDNodes.erase(FI);
603601
604602 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
135135 std::vector > NumberedTypes;
136136
137137 std::vector NumberedMetadata;
138 std::mapMDTuple *, LocTy>> ForwardRefMDNodes;
138 std::mapTempMDTuple, LocTy>> ForwardRefMDNodes;
139139
140140 // Global Value reference information.
141141 std::map > ForwardRefVals;
540540 }
541541
542542 // If there was a forward reference to this value, replace it.
543 MDTuple *PrevMD = cast(OldMD.get());
543 TempMDTuple PrevMD(cast(OldMD.get()));
544544 PrevMD->replaceAllUsesWith(MD);
545 MDNode::deleteTemporary(PrevMD);
546545 --NumFwdRefs;
547546 }
548547
556555 // Create and return a placeholder, which will later be RAUW'd.
557556 AnyFwdRefs = true;
558557 ++NumFwdRefs;
559 Metadata *MD = MDNode::getTemporary(Context, None);
558 Metadata *MD = MDNode::getTemporary(Context, None).release();
560559 MDValuePtrs[Idx].reset(MD);
561560 return MD;
562561 }
141141 assert(!Filename.empty() &&
142142 "Unable to create compile unit without filename");
143143 Metadata *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)};
144 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
145
146 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
147
148 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
149
150 TempGVs = MDNode::getTemporary(VMContext, TElts);
151
152 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
144 TempEnumTypes = MDNode::getTemporary(VMContext, TElts).release();
145
146 TempRetainTypes = MDNode::getTemporary(VMContext, TElts).release();
147
148 TempSubprograms = MDNode::getTemporary(VMContext, TElts).release();
149
150 TempGVs = MDNode::getTemporary(VMContext, TElts).release();
151
152 TempImportedModules = MDNode::getTemporary(VMContext, TElts).release();
153153
154154 Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit)
155155 .concat(Lang)
824824 nullptr, // TemplateParams
825825 UniqueIdentifier.empty() ? nullptr
826826 : MDString::get(VMContext, UniqueIdentifier)};
827 DICompositeType RetTy(MDNode::getTemporary(VMContext, Elts));
827 DICompositeType RetTy(MDNode::getTemporary(VMContext, Elts).release());
828828 assert(RetTy.isCompositeType() &&
829829 "createReplaceableForwardDecl result should be a DIType");
830830 if (!UniqueIdentifier.empty())
902902 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
903903 LineNumber, Ty, isLocalToUnit, Val, Decl,
904904 false, [&](ArrayRef Elts) {
905 return MDNode::getTemporary(VMContext, Elts);
905 return MDNode::getTemporary(VMContext, Elts).release();
906906 });
907907 }
908908
10061006 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
10071007 LineNo, Ty, isLocalToUnit, isDefinition,
10081008 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
1009 MDNode::getTemporary(VMContext, None),
1009 MDNode::getTemporary(VMContext, None).release(),
10101010 [&](ArrayRef Elts) -> MDNode *{
10111011 MDNode *Node = MDNode::get(VMContext, Elts);
10121012 // Create a named metadata so that we
10291029 LineNo, Ty, isLocalToUnit, isDefinition,
10301030 ScopeLine, Flags, isOptimized, Fn, TParams, Decl,
10311031 nullptr, [&](ArrayRef Elts) {
1032 return MDNode::getTemporary(VMContext, Elts);
1032 return MDNode::getTemporary(VMContext, Elts).release();
10331033 });
10341034 }
10351035
6767
6868 MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
6969 // To ensure uniqueness the root node is self-referential.
70 MDNode *Dummy = MDNode::getTemporary(Context, None);
70 auto Dummy = MDNode::getTemporary(Context, None);
7171
72 SmallVector Args(1, Dummy);
72 SmallVector Args(1, Dummy.get());
7373 if (Extra)
7474 Args.push_back(Extra);
7575 if (!Name.empty())
8181 // !1 = metadata !{metadata !0} <- root
8282 // Replace the dummy operand with the root node itself and delete the dummy.
8383 Root->replaceOperandWith(0, Root);
84 MDNode::deleteTemporary(Dummy);
84
8585 // We now have
8686 // !1 = metadata !{metadata !1} <- self-referential root
8787 return Root;
318318
319319 // Now we have a complete set of all metadata in the chains used to specify
320320 // the noalias scopes and the lists of those scopes.
321 SmallVector<MDTuple *, 16> DummyNodes;
321 SmallVector<TempMDTuple, 16> DummyNodes;
322322 DenseMap MDMap;
323323 for (SetVector::iterator I = MD.begin(), IE = MD.end();
324324 I != IE; ++I) {
325 MDTuple *Dummy = MDTuple::getTemporary(CalledFunc->getContext(), None);
326 DummyNodes.push_back(Dummy);
327 MDMap[*I].reset(Dummy);
325 DummyNodes.push_back(MDTuple::getTemporary(CalledFunc->getContext(), None));
326 MDMap[*I].reset(DummyNodes.back().get());
328327 }
329328
330329 // Create new metadata nodes to replace the dummy nodes, replacing old
388387 NI->setMetadata(LLVMContext::MD_noalias, M);
389388 }
390389 }
391
392 // Now that everything has been replaced, delete the dummy nodes.
393 for (unsigned i = 0, ie = DummyNodes.size(); i != ie; ++i)
394 MDNode::deleteTemporary(DummyNodes[i]);
395390 }
396391
397392 /// AddAliasScopeMetadata - If the inlined function has noalias arguments, then
248248
249249 // In general we need a dummy node, since whether the operands are null can
250250 // affect the size of the node.
251 MDTuple *Dummy = MDTuple::getTemporary(Node->getContext(), None);
252 mapToMetadata(VM, Node, Dummy);
251 auto Dummy = MDTuple::getTemporary(Node->getContext(), None);
252 mapToMetadata(VM, Node, Dummy.get());
253253 Metadata *NewMD = cloneMDNode(Node, VM, Flags, TypeMapper, Materializer,
254254 /* IsDistinct */ true);
255255 Dummy->replaceAllUsesWith(NewMD);
256 MDNode::deleteTemporary(Dummy);
257256 return mapToMetadata(VM, Node, NewMD);
258257 }
259258
284283 assert(Node->isUniqued() && "Expected uniqued node");
285284
286285 // Create a dummy node in case we have a metadata cycle.
287 MDTuple *Dummy = MDTuple::getTemporary(Node->getContext(), None);
288 mapToMetadata(VM, Node, Dummy);
286 auto Dummy = MDTuple::getTemporary(Node->getContext(), None);
287 mapToMetadata(VM, Node, Dummy.get());
289288
290289 // Check all operands to see if any need to be remapped.
291290 if (!shouldRemapUniquedNode(Node, VM, Flags, TypeMapper, Materializer)) {
292291 // Use an identity mapping.
293292 mapToSelf(VM, Node);
294 MDNode::deleteTemporary(Dummy);
295293 return const_cast(static_cast(Node));
296294 }
297295
299297 Metadata *NewMD = cloneMDNode(Node, VM, Flags, TypeMapper, Materializer,
300298 /* IsDistinct */ false);
301299 Dummy->replaceAllUsesWith(NewMD);
302 MDNode::deleteTemporary(Dummy);
303300 return mapToMetadata(VM, Node, NewMD);
304301 }
305302
170170 // !0 = !{!0}
171171 // !1 = !{!0}
172172 {
173 MDNode *Temp = MDNode::getTemporary(Context, None);
174 Metadata *Args[] = {Temp};
173 auto Temp = MDNode::getTemporary(Context, None);
174 Metadata *Args[] = {Temp.get()};
175175 MDNode *Self = MDNode::get(Context, Args);
176176 Self->replaceOperandWith(0, Self);
177 MDNode::deleteTemporary(Temp);
178177 ASSERT_EQ(Self, Self->getOperand(0));
179178
180179 // Self-references should be distinct, so MDNode::get() should grab a
189188 // !0 = !{!0, !{}}
190189 // !1 = !{!0, !{}}
191190 {
192 MDNode *Temp = MDNode::getTemporary(Context, None);
193 Metadata *Args[] = {Temp, MDNode::get(Context, None)};
191 auto Temp = MDNode::getTemporary(Context, None);
192 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
194193 MDNode *Self = MDNode::get(Context, Args);
195194 Self->replaceOperandWith(0, Self);
196 MDNode::deleteTemporary(Temp);
197195 ASSERT_EQ(Self, Self->getOperand(0));
198196
199197 // Self-references should be distinct, so MDNode::get() should grab a
309307 TEST_F(MDNodeTest, isUniqued) {
310308 MDNode *U = MDTuple::get(Context, None);
311309 MDNode *D = MDTuple::getDistinct(Context, None);
312 MDNode *T = MDTuple::getTemporary(Context, None);
310 auto T = MDTuple::getTemporary(Context, None);
313311 EXPECT_TRUE(U->isUniqued());
314312 EXPECT_FALSE(D->isUniqued());
315313 EXPECT_FALSE(T->isUniqued());
316 MDNode::deleteTemporary(T);
317314 }
318315
319316 TEST_F(MDNodeTest, isDistinct) {
320317 MDNode *U = MDTuple::get(Context, None);
321318 MDNode *D = MDTuple::getDistinct(Context, None);
322 MDNode *T = MDTuple::getTemporary(Context, None);
319 auto T = MDTuple::getTemporary(Context, None);
323320 EXPECT_FALSE(U->isDistinct());
324321 EXPECT_TRUE(D->isDistinct());
325322 EXPECT_FALSE(T->isDistinct());
326 MDNode::deleteTemporary(T);
327323 }
328324
329325 TEST_F(MDNodeTest, isTemporary) {
330326 MDNode *U = MDTuple::get(Context, None);
331327 MDNode *D = MDTuple::getDistinct(Context, None);
332 MDNode *T = MDTuple::getTemporary(Context, None);
328 auto T = MDTuple::getTemporary(Context, None);
333329 EXPECT_FALSE(U->isTemporary());
334330 EXPECT_FALSE(D->isTemporary());
335331 EXPECT_TRUE(T->isTemporary());
336 MDNode::deleteTemporary(T);
337332 }
338333
339334 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
340335 // temporary !{}
341 MDTuple *Temp = MDTuple::getTemporary(Context, None);
336 auto Temp = MDTuple::getTemporary(Context, None);
342337 ASSERT_FALSE(Temp->isResolved());
343338
344339 // distinct !{temporary !{}}
345 Metadata *Ops[] = {Temp};
340 Metadata *Ops[] = {Temp.get()};
346341 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
347342 EXPECT_TRUE(Distinct->isResolved());
348 EXPECT_EQ(Temp, Distinct->getOperand(0));
343 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
349344
350345 // temporary !{} => !{}
351346 MDNode *Empty = MDNode::get(Context, None);
352347 Temp->replaceAllUsesWith(Empty);
353 MDNode::deleteTemporary(Temp);
354348 EXPECT_EQ(Empty, Distinct->getOperand(0));
355349 }
356350
359353 MDNode *N0 = MDNode::get(Context, None);
360354
361355 // !1 = !{!3, null}
362 MDTuple *Temp3 = MDTuple::getTemporary(Context, None);
363 Metadata *Ops1[] = {Temp3, nullptr};
356 auto Temp3 = MDTuple::getTemporary(Context, None);
357 Metadata *Ops1[] = {Temp3.get(), nullptr};
364358 MDNode *N1 = MDNode::get(Context, Ops1);
365359
366360 // !2 = !{!3, !0}
367 Metadata *Ops2[] = {Temp3, N0};
361 Metadata *Ops2[] = {Temp3.get(), N0};
368362 MDNode *N2 = MDNode::get(Context, Ops2);
369363
370364 // !3 = !{!2}
371365 Metadata *Ops3[] = {N2};
372366 MDNode *N3 = MDNode::get(Context, Ops3);
373367 Temp3->replaceAllUsesWith(N3);
374 MDNode::deleteTemporary(Temp3);
375368
376369 // !4 = !{!1}
377370 Metadata *Ops4[] = {N1};
424417 // a global value that gets RAUW'ed.
425418 //
426419 // Use a temporary node to keep N from being resolved.
427 MDTuple *Temp = MDTuple::getTemporary(Context, None);
428 Metadata *Ops[] = {nullptr, Temp};
420 auto Temp = MDTuple::getTemporary(Context, None);
421 Metadata *Ops[] = {nullptr, Temp.get()};
429422
430423 MDNode *Empty = MDTuple::get(Context, ArrayRef());
431424 MDNode *N = MDTuple::get(Context, Ops);
437430 EXPECT_EQ(Empty, N->getOperand(0));
438431
439432 // Check code for adding another unresolved operand.
440 N->replaceOperandWith(0, Temp);
441 EXPECT_EQ(Temp, N->getOperand(0));
433 N->replaceOperandWith(0, Temp.get());
434 EXPECT_EQ(Temp.get(), N->getOperand(0));
442435
443436 // Remove the references to Temp; required for teardown.
444437 Temp->replaceAllUsesWith(nullptr);
445 MDNode::deleteTemporary(Temp);
446438 }
447439
448440 typedef MetadataTest MDLocationTest;
484476
485477 TEST_F(MDLocationTest, getTemporary) {
486478 MDNode *N = MDNode::get(Context, None);
487 MDLocation *L = MDLocation::getTemporary(Context, 2, 7, N);
479 auto L = MDLocation::getTemporary(Context, 2, 7, N);
488480 EXPECT_TRUE(L->isTemporary());
489481 EXPECT_FALSE(L->isResolved());
490 MDNode::deleteTemporary(L);
491482 }
492483
493484 typedef MetadataTest MetadataAsValueTest;
556547 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
557548
558549 // Create a temporary to prevent nodes from resolving.
559 MDTuple *Temp = MDTuple::getTemporary(Context, None);
550 auto Temp = MDTuple::getTemporary(Context, None);
560551
561552 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
562 Metadata *Ops1[] = {CI, CI, Temp};
563 Metadata *Ops2[] = {nullptr, CI, Temp};
553 Metadata *Ops1[] = {CI, CI, Temp.get()};
554 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
564555
565556 auto *N1 = MDTuple::get(Context, Ops1);
566557 auto *N2 = MDTuple::get(Context, Ops2);
572563 ValueAsMetadata::handleDeletion(CI->getValue());
573564 EXPECT_EQ(nullptr, N2->getOperand(0));
574565 EXPECT_EQ(nullptr, N2->getOperand(1));
575 EXPECT_EQ(Temp, N2->getOperand(2));
566 EXPECT_EQ(Temp.get(), N2->getOperand(2));
576567
577568 // Clean up Temp for teardown.
578569 Temp->replaceAllUsesWith(nullptr);
579 MDNode::deleteTemporary(Temp);
580570 }
581571
582572 typedef MetadataTest TrackingMDRefTest;