[PATCH] D52537: Allow null-valued function operands in getCalledFunction()

David Stenberg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 2 04:48:49 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL345966: Allow null-valued function operands in getCalledFunction() (authored by dstenb, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D52537?vs=167728&id=172338#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D52537

Files:
  llvm/trunk/include/llvm/IR/Instructions.h
  llvm/trunk/unittests/IR/MetadataTest.cpp


Index: llvm/trunk/include/llvm/IR/Instructions.h
===================================================================
--- llvm/trunk/include/llvm/IR/Instructions.h
+++ llvm/trunk/include/llvm/IR/Instructions.h
@@ -1523,7 +1523,7 @@
   /// indirect function invocation.
   ///
   Function *getCalledFunction() const {
-    return dyn_cast<Function>(Op<-InstTy::ArgOffset>());
+    return dyn_cast_or_null<Function>(Op<-InstTy::ArgOffset>());
   }
 
   /// Determine whether this call has the given attribute.
Index: llvm/trunk/unittests/IR/MetadataTest.cpp
===================================================================
--- llvm/trunk/unittests/IR/MetadataTest.cpp
+++ llvm/trunk/unittests/IR/MetadataTest.cpp
@@ -402,6 +402,27 @@
   EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
   EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
 }
+
+TEST_F(MDNodeTest, PrintWithDroppedCallOperand) {
+  Module M("test", Context);
+
+  auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
+  auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
+  auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
+  auto *BB0 = BasicBlock::Create(Context, "entry", F0);
+
+  CallInst *CI0 = CallInst::Create(F1, "", BB0);
+  CI0->dropAllReferences();
+
+  auto *R0 = ReturnInst::Create(Context, BB0);
+  auto *N0 = MDNode::getDistinct(Context, None);
+  R0->setMetadata("md", N0);
+
+  // Printing the metadata node would previously result in a failed assertion
+  // due to the call instruction's dropped function operand.
+  ModuleSlotTracker MST(&M);
+  EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
+}
 #undef EXPECT_PRINTER_EQ
 
 TEST_F(MDNodeTest, NullOperand) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52537.172338.patch
Type: text/x-patch
Size: 1761 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181102/07832992/attachment.bin>


More information about the llvm-commits mailing list