[PATCH] D98884: [IR] Add opt-in parameter to hasAddressTaken() to ignore bitcasts callee in callbase instruction

Madhur Amilkanthwar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 18 12:14:23 PDT 2021


madhur13490 created this revision.
madhur13490 added reviewers: arsenm, rampitec, chandlerc, ggeorgakoudis.
Herald added subscribers: dexonsmith, hiraditya.
madhur13490 requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

This patch adds an optional parameter to
hasAddressTaken() to ignore bitcasts as a
callee in callbase instruction. Address taken for such
bitcast callees in callbase instructions is not assigned to a
value and thus used later meaningfully.

This patch introduces an new optional parameter called
IgnoreBitCastCallees. Its default value is false to
retian current behaviour. When provided, it will
ignore if all users of bitcast operator are callbase instructions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98884

Files:
  llvm/include/llvm/IR/Function.h
  llvm/lib/IR/Function.cpp
  llvm/test/Analysis/CallGraph/ignore-bitcast-callees.ll


Index: llvm/test/Analysis/CallGraph/ignore-bitcast-callees.ll
===================================================================
--- /dev/null
+++ llvm/test/Analysis/CallGraph/ignore-bitcast-callees.ll
@@ -0,0 +1,24 @@
+; RUN:  opt < %s -print-callgraph -disable-output 2>&1 | FileCheck %s
+; CHECK: Call graph node <<null function>><<{{.*}}>>  #uses=0
+; CHECK-NEXT:   CS<None> calls function 'foo'
+; CHECK-EMPTY:
+; CHECK-NEXT: Call graph node for function: 'foo'<<{{.*}}>>  #uses=1
+; CHECK-EMPTY:
+; CHECK-NEXT: Call graph node for function: 'test_bitcast_callees'<<{{.*}}>>  #uses=0
+; CHECK-NEXT:   CS<{{.*}}> calls external node
+; CHECK-NEXT:   CS<{{.*}}> calls external node
+
+define internal i32 @foo() #1 {
+entry:
+    ret i32 5
+}
+
+define internal float @test_bitcast_callees() #1 {
+  %v1 = call float bitcast (i32()* @foo to float()*)()
+  %v2 = fadd float %v1, 1.0
+  %v3 = call i8 bitcast (i32()* @foo to i8()*)()
+  %v4 = uitofp i8 %v3 to float
+  %v5 = fadd float %v2, %v4
+  ret float %v5
+}
+
Index: llvm/lib/IR/Function.cpp
===================================================================
--- llvm/lib/IR/Function.cpp
+++ llvm/lib/IR/Function.cpp
@@ -1584,8 +1584,8 @@
 /// and llvm.compiler.used variables.
 bool Function::hasAddressTaken(const User **PutOffender,
                                bool IgnoreCallbackUses,
-                               bool IgnoreAssumeLikeCalls,
-                               bool IgnoreLLVMUsed) const {
+                               bool IgnoreAssumeLikeCalls, bool IgnoreLLVMUsed,
+                               bool IgnoreBitCastCallees) const {
   for (const Use &U : uses()) {
     const User *FU = U.getUser();
     if (isa<BlockAddress>(FU))
@@ -1597,6 +1597,13 @@
         continue;
     }
 
+    if (IgnoreBitCastCallees) {
+      if (isa<BitCastOperator>(FU) &&
+          llvm::all_of(FU->users(),
+                       [](const User *U) { return isa<CallBase>(U); }))
+        continue;
+    }
+
     const auto *Call = dyn_cast<CallBase>(FU);
     if (!Call) {
       if (IgnoreAssumeLikeCalls) {
Index: llvm/include/llvm/IR/Function.h
===================================================================
--- llvm/include/llvm/IR/Function.h
+++ llvm/include/llvm/IR/Function.h
@@ -876,10 +876,10 @@
   /// ignores callback uses, assume like pointer annotation calls, and
   /// references in llvm.used and llvm.compiler.used variables.
   ///
-  bool hasAddressTaken(const User ** = nullptr,
-                       bool IgnoreCallbackUses = false,
+  bool hasAddressTaken(const User ** = nullptr, bool IgnoreCallbackUses = false,
                        bool IgnoreAssumeLikeCalls = false,
-                       bool IngoreLLVMUsed = false) const;
+                       bool IngoreLLVMUsed = false,
+                       bool IgnoreBitCastCallees = 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: D98884.331651.patch
Type: text/x-patch
Size: 3010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210318/b2d13090/attachment.bin>


More information about the llvm-commits mailing list