[PATCH] D159149: [Core] Allow `hasAddressTaken` to ignore "casted direct calls"

Johannes Doerfert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 29 16:59:27 PDT 2023


jdoerfert created this revision.
jdoerfert added reviewers: nikic, fhahn, arsenm.
Herald added subscribers: hoy, ormris, StephenFan, okura, kuter, bollu, hiraditya.
Herald added a project: All.
jdoerfert requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a reviewer: sstefan1.
Herald added a project: LLVM.

A direct call to a function casted to a different type is still not
really an address taken event. We allow the user to opt out of these
now.


https://reviews.llvm.org/D159149

Files:
  llvm/include/llvm/IR/Function.h
  llvm/lib/IR/Function.cpp
  llvm/lib/Transforms/IPO/Attributor.cpp
  llvm/test/Transforms/Attributor/callgraph.ll


Index: llvm/test/Transforms/Attributor/callgraph.ll
===================================================================
--- llvm/test/Transforms/Attributor/callgraph.ll
+++ llvm/test/Transforms/Attributor/callgraph.ll
@@ -425,6 +425,16 @@
   ret i32 %call
 }
 
+declare void @usedOnlyInCastedDirectCall(i32)
+define void @usedOnlyInCastedDirectCallCaller() {
+; CHECK-LABEL: @usedOnlyInCastedDirectCallCaller(
+; CHECK-NEXT:    call void @usedOnlyInCastedDirectCall()
+; CHECK-NEXT:    ret void
+;
+  call void @usedOnlyInCastedDirectCall()
+  ret void
+}
+
 define void @broker(ptr %unknown) !callback !0 {
 ; OWRDL-LABEL: @broker(
 ; OWRDL-NEXT:    call void [[UNKNOWN:%.*]]()
Index: llvm/lib/Transforms/IPO/Attributor.cpp
===================================================================
--- llvm/lib/Transforms/IPO/Attributor.cpp
+++ llvm/lib/Transforms/IPO/Attributor.cpp
@@ -1072,7 +1072,9 @@
     if (Fn->hasAddressTaken(/*PutOffender=*/nullptr,
                             /*IgnoreCallbackUses=*/false,
                             /*IgnoreAssumeLikeCalls=*/true,
-                            /*IgnoreLLVMUsed=*/true))
+                            /*IgnoreLLVMUsed=*/true,
+                            /*IgnoreARCAttachedCall=*/false,
+                            /*IgnoreCastedDirectCall=*/true))
       InfoCache.IndirectlyCallableFunctions.push_back(Fn);
 }
 
Index: llvm/lib/IR/Function.cpp
===================================================================
--- llvm/lib/IR/Function.cpp
+++ llvm/lib/IR/Function.cpp
@@ -1752,7 +1752,8 @@
 bool Function::hasAddressTaken(const User **PutOffender,
                                bool IgnoreCallbackUses,
                                bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed,
-                               bool IgnoreARCAttachedCall) const {
+                               bool IgnoreARCAttachedCall,
+                               bool IgnoreCastedDirectCall) const {
   for (const Use &U : uses()) {
     const User *FU = U.getUser();
     if (isa<BlockAddress>(FU))
@@ -1801,7 +1802,8 @@
           continue;
     }
 
-    if (!Call->isCallee(&U) || Call->getFunctionType() != getFunctionType()) {
+    if (!Call->isCallee(&U) || (!IgnoreCastedDirectCall &&
+                                Call->getFunctionType() != getFunctionType())) {
       if (IgnoreARCAttachedCall &&
           Call->isOperandBundleOfType(LLVMContext::OB_clang_arc_attachedcall,
                                       U.getOperandNo()))
Index: llvm/include/llvm/IR/Function.h
===================================================================
--- llvm/include/llvm/IR/Function.h
+++ llvm/include/llvm/IR/Function.h
@@ -888,13 +888,14 @@
   /// other than direct calls or invokes to it, or blockaddress expressions.
   /// Optionally passes back an offending user for diagnostic purposes,
   /// ignores callback uses, assume like pointer annotation calls, references in
-  /// llvm.used and llvm.compiler.used variables, and operand bundle
-  /// "clang.arc.attachedcall".
-  bool hasAddressTaken(const User ** = nullptr,
-                       bool IgnoreCallbackUses = false,
+  /// llvm.used and llvm.compiler.used variables, operand bundle
+  /// "clang.arc.attachedcall", and direct calls with a different call site
+  /// signature (the function is implicitly casted).
+  bool hasAddressTaken(const User ** = nullptr, bool IgnoreCallbackUses = false,
                        bool IgnoreAssumeLikeCalls = true,
                        bool IngoreLLVMUsed = false,
-                       bool IgnoreARCAttachedCall = false) const;
+                       bool IgnoreARCAttachedCall = false,
+                       bool IgnoreCastedDirectCall = false) const;
 
   /// isDefTriviallyDead - Return true if it is trivially safe to remove
   /// this function definition from the module (because it isn't externally


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159149.554527.patch
Type: text/x-patch
Size: 3876 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230829/7366fc66/attachment.bin>


More information about the llvm-commits mailing list