llvm.org GIT mirror llvm / 11ef6e1
Allow null-valued function operands in getCalledFunction() Summary: Change the dynamic cast in CallBase::getCalledFunction() to allow null-valued function operands. This patch fixes a crash that occurred when a funtion operand of a call instruction was dropped, and later on a metadata-carrying instruction was printed out. When allocating the metadata slot numbers, getCalledFunction() would be invoked on the call with the dropped operand, resulting in a failed non-null assertion in isa<>. This fixes PR38924, in which a printout in DBCE crashed due to this. This aligns getCalledFunction() with getCalledValue(), as the latter allows the operand to be null. Reviewers: vsk, dexonsmith, hfinkel Reviewed By: dexonsmith Subscribers: hfinkel, llvm-commits Differential Revision: https://reviews.llvm.org/D52537 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@345966 91177308-0d34-0410-b5e6-96231b3b80d8 David Stenberg 11 months ago
2 changed file(s) with 22 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
15221522 /// indirect function invocation.
15231523 ///
15241524 Function *getCalledFunction() const {
1525 return dyn_cast(Op<-InstTy::ArgOffset>());
1525 return dyn_cast_or_null(Op<-InstTy::ArgOffset>());
15261526 }
15271527
15281528 /// Determine whether this call has the given attribute.
400400 EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
401401 EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
402402 EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
403 }
404
405 TEST_F(MDNodeTest, PrintWithDroppedCallOperand) {
406 Module M("test", Context);
407
408 auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
409 auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
410 auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
411 auto *BB0 = BasicBlock::Create(Context, "entry", F0);
412
413 CallInst *CI0 = CallInst::Create(F1, "", BB0);
414 CI0->dropAllReferences();
415
416 auto *R0 = ReturnInst::Create(Context, BB0);
417 auto *N0 = MDNode::getDistinct(Context, None);
418 R0->setMetadata("md", N0);
419
420 // Printing the metadata node would previously result in a failed assertion
421 // due to the call instruction's dropped function operand.
422 ModuleSlotTracker MST(&M);
423 EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
403424 }
404425 #undef EXPECT_PRINTER_EQ
405426