[InlineCost] Check that function types match
authorNikita Popov <npopov@redhat.com>
Tue, 12 Apr 2022 09:03:42 +0000 (11:03 +0200)
committerNikita Popov <npopov@redhat.com>
Tue, 12 Apr 2022 09:05:33 +0000 (11:05 +0200)
Retain the behavior we get without opaque pointers: A call to a
known function with different function type is considered an
indirect call.

This fixes the crash reported in https://reviews.llvm.org/D123300#3444772.

llvm/lib/Analysis/InlineCost.cpp
llvm/test/Transforms/Inline/opaque-ptr.ll [new file with mode: 0644]

index dd404fc..033ca9a 100644 (file)
@@ -2136,14 +2136,14 @@ bool CallAnalyzer::visitCallBase(CallBase &Call) {
   if (isa<CallInst>(Call) && cast<CallInst>(Call).cannotDuplicate())
     ContainsNoDuplicateCall = true;
 
-  Value *Callee = Call.getCalledOperand();
-  Function *F = dyn_cast_or_null<Function>(Callee);
+  Function *F = Call.getCalledFunction();
   bool IsIndirectCall = !F;
   if (IsIndirectCall) {
     // Check if this happens to be an indirect function call to a known function
     // in this inline context. If not, we've done all we can.
+    Value *Callee = Call.getCalledOperand();
     F = dyn_cast_or_null<Function>(SimplifiedValues.lookup(Callee));
-    if (!F) {
+    if (!F || F->getFunctionType() != Call.getFunctionType()) {
       onCallArgumentSetup(Call);
 
       if (!Call.onlyReadsMemory())
diff --git a/llvm/test/Transforms/Inline/opaque-ptr.ll b/llvm/test/Transforms/Inline/opaque-ptr.ll
new file mode 100644 (file)
index 0000000..2d22cac
--- /dev/null
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -inline < %s | FileCheck %s
+
+define void @test(ptr %p1, ptr %p2) {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define void @test1() {
+; CHECK-LABEL: @test1(
+; CHECK-NEXT:    [[CALL_I:%.*]] = call i32 @test(ptr null)
+; CHECK-NEXT:    ret void
+;
+  call void @test2(ptr @test)
+  ret void
+}
+
+define void @test2(ptr %i) {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT:    [[CALL:%.*]] = call i32 [[I:%.*]](ptr null)
+; CHECK-NEXT:    ret void
+;
+  %call = call i32 %i(ptr null)
+  ret void
+}