[llvm] ee5d5e1 - [funcattrs] Use callsite param attributes from indirect calls when inferring access attributes

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 22 18:22:58 PST 2021


Author: Philip Reames
Date: 2021-12-22T18:21:59-08:00
New Revision: ee5d5e19f99db863fc3037eb125d8cfce3b2c5d8

URL: https://github.com/llvm/llvm-project/commit/ee5d5e19f99db863fc3037eb125d8cfce3b2c5d8
DIFF: https://github.com/llvm/llvm-project/commit/ee5d5e19f99db863fc3037eb125d8cfce3b2c5d8.diff

LOG: [funcattrs] Use callsite param attributes from indirect calls when inferring access attributes

Arguments to an indirect call is by definition outside the SCC, but there's no reason we can't use locally defined facts on the call site. This also has the nice effect of further simplifying the code.

Differential Revision: https://reviews.llvm.org/D116118

Added: 
    

Modified: 
    llvm/lib/Transforms/IPO/FunctionAttrs.cpp
    llvm/test/Transforms/FunctionAttrs/readattrs.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index 516d4006894ce..321d4a19a5855 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -710,22 +710,13 @@ determinePointerAccessAttrs(Argument *A,
       if (CB.doesNotAccessMemory())
         continue;
 
-      Function *F = CB.getCalledFunction();
-      if (!F) {
-        if (CB.onlyReadsMemory()) {
-          IsRead = true;
-          continue;
-        }
-        return Attribute::None;
-      }
-
-      if (CB.isArgOperand(U) && UseIndex < F->arg_size() &&
-          SCCNodes.count(F->getArg(UseIndex))) {
-        // This is an argument which is part of the speculative SCC.  Note that
-        // only operands corresponding to formal arguments of the callee can
-        // participate in the speculation.
-        break;
-      }
+      if (Function *F = CB.getCalledFunction())
+        if (CB.isArgOperand(U) && UseIndex < F->arg_size() &&
+            SCCNodes.count(F->getArg(UseIndex)))
+          // This is an argument which is part of the speculative SCC.  Note
+          // that only operands corresponding to formal arguments of the callee
+          // can participate in the speculation.
+          break;
 
       // The accessors used on call site here do the right thing for calls and
       // invokes with operand bundles.

diff  --git a/llvm/test/Transforms/FunctionAttrs/readattrs.ll b/llvm/test/Transforms/FunctionAttrs/readattrs.ll
index 3ca0bc4382277..8ea47cf430707 100644
--- a/llvm/test/Transforms/FunctionAttrs/readattrs.ll
+++ b/llvm/test/Transforms/FunctionAttrs/readattrs.ll
@@ -146,3 +146,42 @@ define void @unsound_readonly(i8* %ignored, i8* %escaped_then_written) {
   store i8 0, i8* %addr.ld
   ret void
 }
+
+
+; CHECK: define void @fptr_test1a(i8* nocapture readnone %p, void (i8*)* nocapture readonly %f)
+define void @fptr_test1a(i8* %p, void (i8*)* %f) {
+  call void %f(i8* nocapture readnone %p)
+  ret void
+}
+
+; CHECK: define void @fptr_test1b(i8* %p, void (i8*)* nocapture readonly %f)
+define void @fptr_test1b(i8* %p, void (i8*)* %f) {
+  ; Can't infer readnone here because call might capture %p
+  call void %f(i8* readnone %p)
+  ret void
+}
+
+; CHECK: define void @fptr_test1c(i8* readnone %p, void (i8*)* nocapture readonly %f)
+define void @fptr_test1c(i8* %p, void (i8*)* %f) {
+  call void %f(i8* readnone %p) readonly
+  ret void
+}
+
+; CHECK: define void @fptr_test2a(i8* nocapture readonly %p, void (i8*)* nocapture readonly %f)
+define void @fptr_test2a(i8* %p, void (i8*)* %f) {
+  call void %f(i8* nocapture readonly %p)
+  ret void
+}
+
+; CHECK: define void @fptr_test2b(i8* %p, void (i8*)* nocapture readonly %f)
+define void @fptr_test2b(i8* %p, void (i8*)* %f) {
+  ; Can't infer readonly here because call might capture %p
+  call void %f(i8* readonly %p)
+  ret void
+}
+
+; CHECK: define void @fptr_test2c(i8* readonly %p, void (i8*)* nocapture readonly %f)
+define void @fptr_test2c(i8* %p, void (i8*)* %f) {
+  call void %f(i8* readonly %p) readonly
+  ret void
+}


        


More information about the llvm-commits mailing list