]> granicus.if.org Git - llvm/commitdiff
[INLINER] allow inlining of address taken blocks
authorNick Desaulniers <ndesaulniers@google.com>
Thu, 14 Feb 2019 23:35:53 +0000 (23:35 +0000)
committerNick Desaulniers <ndesaulniers@google.com>
Thu, 14 Feb 2019 23:35:53 +0000 (23:35 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354079 91177308-0d34-0410-b5e6-96231b3b80d8

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

index fd9efb8417ea9a7fe27bea4473e9e6251fd6c825..076adad7cf73ec1ad06ace2b3fa81b6cb9a00654 100644 (file)
@@ -390,6 +390,11 @@ public:
   /// 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);
index 574a1b6c8410a8d4ec29fd6edc877e374cea354a..2f5aa4a25766bd82e8208eefde85922780f2d5f8 100644 (file)
@@ -1832,7 +1832,7 @@ InlineResult CallAnalyzer::analyzeCall(CallSite CS) {
     // 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 @@ InlineResult llvm::isInlineViable(Function &F) {
     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 18e2fd898f72b3a7c75e8eb21eb83b70dc548993..7f2e762c29a60dcc627a97da9c4a3b729821f9b0 100644 (file)
@@ -442,6 +442,14 @@ BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) {
   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)