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

Nick Desaulniers via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 14 15:44:59 PST 2019


nickdesaulniers created this revision.
nickdesaulniers added reviewers: jyknight, eli.friedman.
Herald added subscribers: llvm-commits, haicheng, hiraditya, eraman.
Herald added a project: LLVM.

as long as their uses does not contain calls to functions that capture
the argument (potentially allowing the blockaddress to "escape" the
lifetime of the caller).

TODO:

- add more tests
- fix crash in llvm::updateCGAndAnalysisManagerForFunctionPass when invoking Transforms/Inline/blockaddress.ll


Repository:
  rG LLVM Github Monorepo

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,14 @@
   return New;
 }
 
+bool BasicBlock::addressPotentiallyEscapesFunction() {
+  for (const Use& U : BlockAddress::get(this)->uses())
+    if (const CallInst* CI = dyn_cast<CallInst>(U))
+      if (!CI->paramHasAttr(U.getOperandNo(), Attribute::NoCapture))
+        return true;
+  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,11 @@
   /// 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 are call instructions (which may allow the address of this basic
+  /// block to escape).
+  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.186933.patch
Type: text/x-patch
Size: 2274 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190214/0834f182/attachment-0001.bin>


More information about the llvm-commits mailing list