[PATCH] D96087: Option to ignore llvm[.compiler].used uses in hasAddressTaken()

Stanislav Mekhanoshin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 4 15:58:29 PST 2021


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

https://reviews.llvm.org/D96087

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


Index: llvm/test/Analysis/CallGraph/ignore-llvm-used.ll
===================================================================
--- /dev/null
+++ llvm/test/Analysis/CallGraph/ignore-llvm-used.ll
@@ -0,0 +1,22 @@
+; 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 'bar'
+; CHECK-EMPTY:
+; CHECK-NEXT:   Call graph node for function: 'bar'<<{{.*}}>>  #uses=1
+; CHECK-EMPTY:
+; CHECK-NEXT:   Call graph node for function: 'foo'<<{{.*}}>>  #uses=0
+; CHECK-EMPTY:
+
+ at llvm.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)]
+ at llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)]
+ at array = appending global [1 x i8*] [i8* bitcast (void ()* @bar to i8*)]
+
+define internal void @foo() {
+entry:
+  ret void
+}
+
+define internal void @bar() {
+entry:
+  ret void
+}
Index: llvm/lib/IR/Function.cpp
===================================================================
--- llvm/lib/IR/Function.cpp
+++ llvm/lib/IR/Function.cpp
@@ -1582,7 +1582,8 @@
 /// uses.
 bool Function::hasAddressTaken(const User **PutOffender,
                                bool IgnoreCallbackUses,
-                               bool IgnoreAssumeLikeCalls) const {
+                               bool IgnoreAssumeLikeCalls,
+                               bool IngoreLLVMUsed) const {
   for (const Use &U : uses()) {
     const User *FU = U.getUser();
     if (isa<BlockAddress>(FU))
@@ -1607,6 +1608,15 @@
             continue;
         }
       }
+      if (IngoreLLVMUsed && FU->hasOneUse() &&
+          llvm::all_of(FU->user_begin()->users(), [](const User *U) {
+            if (const auto *GV = dyn_cast<GlobalVariable>(U))
+              return GV->hasName() && (GV->getName() == "llvm.compiler.used" ||
+                                       GV->getName() == "llvm.used");
+            return false;
+          })) {
+        continue;
+      }
       if (PutOffender)
         *PutOffender = FU;
       return true;
Index: llvm/lib/Analysis/CallGraph.cpp
===================================================================
--- llvm/lib/Analysis/CallGraph.cpp
+++ llvm/lib/Analysis/CallGraph.cpp
@@ -81,7 +81,8 @@
   // it is not a callback, then anything could call it.
   if (!F->hasLocalLinkage() ||
       F->hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
-                         /* IgnoreAssumeLikeCalls */ true))
+                         /* IgnoreAssumeLikeCalls */ true,
+                         /* IngoreLLVMUsed */ true))
     ExternalCallingNode->addCalledFunction(nullptr, Node);
 
   populateCallGraphNode(Node);
Index: llvm/include/llvm/IR/Function.h
===================================================================
--- llvm/include/llvm/IR/Function.h
+++ llvm/include/llvm/IR/Function.h
@@ -877,7 +877,8 @@
   ///
   bool hasAddressTaken(const User ** = nullptr,
                        bool IgnoreCallbackUses = false,
-                       bool IgnoreAssumeLikeCalls = false) const;
+                       bool IgnoreAssumeLikeCalls = false,
+                       bool IngoreLLVMUsed = 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: D96087.321595.patch
Type: text/x-patch
Size: 3342 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210204/5192d367/attachment.bin>


More information about the llvm-commits mailing list