[PATCH] D58260: [INLINER] allow inlining of address taken blocks

Nick Desaulniers via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 15 15:09:12 PST 2019


nickdesaulniers updated this revision to Diff 187098.
nickdesaulniers added a comment.

- improve method comment and check the non-global constant case


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58260/new/

https://reviews.llvm.org/D58260

Files:
  llvm/include/llvm/IR/BasicBlock.h
  llvm/lib/Analysis/InlineCost.cpp
  llvm/lib/IR/BasicBlock.cpp


Index: llvm/lib/IR/BasicBlock.cpp
===================================================================
--- llvm/lib/IR/BasicBlock.cpp
+++ llvm/lib/IR/BasicBlock.cpp
@@ -442,6 +442,33 @@
   return New;
 }
 
+bool BasicBlock::addressPotentiallyEscapesFunction() {
+  SmallVector<const Use*, 16> Worklist;
+  for (auto &U : BlockAddress::get(this)->uses()) {
+    Worklist.push_back(&U);
+  }
+  while (!Worklist.empty()) {
+    const Use* U = Worklist.pop_back_val();
+    // If the use is a Constant and not a GlobalValue, check their uses.
+    if (isa<Constant>(U) && !isa<GlobalValue>(U)) {
+     for (auto &UU : U->getUser()->uses()) {
+       Worklist.push_back(&UU);
+     }
+     continue;
+    // If the use is a nocapture paramter, this is ok.
+    } else if (const CallInst* CI = dyn_cast<CallInst>(U)) {
+      if (CI->paramHasAttr(U->getOperandNo(), Attribute::NoCapture)) {
+        continue;
+      }
+    // Otherwise, conservatively return true.
+    } else {
+      return true;
+    }
+  }
+  // Use list was empty, or contained no faulty cases.
+  return false;
+}
+
 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
   Instruction *TI = getTerminator();
   if (!TI)
Index: llvm/lib/Analysis/InlineCost.cpp
===================================================================
--- llvm/lib/Analysis/InlineCost.cpp
+++ llvm/lib/Analysis/InlineCost.cpp
@@ -1832,7 +1832,7 @@
     // see an indirect branch that ends up being dead code at a particular call
     // site. If the blockaddress escapes the function, e.g., via a global
     // variable, inlining may lead to an invalid cross-function reference.
-    if (BB->hasAddressTaken())
+    if (BB->hasAddressTaken() && BB->addressPotentiallyEscapesFunction())
       return "blockaddress";
 
     // Analyze the cost of this block. If we blow through the threshold, this
@@ -2082,7 +2082,7 @@
     if (isa<IndirectBrInst>(BI->getTerminator()))
       return "contains indirect branches";
 
-    if (BI->hasAddressTaken())
+    if (BI->hasAddressTaken() && BI->addressPotentiallyEscapesFunction())
       return "uses block address";
 
     for (auto &II : *BI) {
Index: llvm/include/llvm/IR/BasicBlock.h
===================================================================
--- llvm/include/llvm/IR/BasicBlock.h
+++ llvm/include/llvm/IR/BasicBlock.h
@@ -390,6 +390,12 @@
   /// direct branches, switches, etc. to it.
   bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
 
+  /// Returns true if there are any uses of the address of this basic block
+  /// that may escape the function.  At the momement, this is a very
+  /// conservative approximation, and treats ANY use (except as a "noescape"
+  /// parameter to a call as potentially escaping.
+  bool addressPotentiallyEscapesFunction();
+
   /// Update all phi nodes in this basic block's successors to refer to basic
   /// block \p New instead of to it.
   void replaceSuccessorsPhiUsesWith(BasicBlock *New);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58260.187098.patch
Type: text/x-patch
Size: 2972 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190215/a554e7b8/attachment.bin>


More information about the llvm-commits mailing list