Factor the logic for testing whether a basic block is viable for code
authorChandler Carruth <chandlerc@gmail.com>
Thu, 3 May 2012 22:26:53 +0000 (22:26 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Thu, 3 May 2012 22:26:53 +0000 (22:26 +0000)
extraction into a public interface. Also clean it up and apply it more
consistently such that we check for landing pads *anywhere* in the
extracted code, not just in single-block extraction.

This will be used to guide decisions in passes that are planning to
eventually perform a round of code extraction.

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

include/llvm/Transforms/Utils/FunctionUtils.h
lib/Transforms/Utils/CodeExtractor.cpp

index 8d71e43aa92169891ff0deadeadd07a1e9fcd173..821a61e99b74577588581f13f58bb811c959bec2 100644 (file)
@@ -23,6 +23,14 @@ namespace llvm {
   class Function;
   class Loop;
 
+  /// \brief Test whether a basic block is a viable candidate for extraction.
+  ///
+  /// This tests whether a particular basic block is viable for extraction into
+  /// a separate function. It can be used to prune code extraction eagerly
+  /// rather than waiting for one of the Extract* methods below to reject
+  /// a request.
+  bool isBlockViableForExtraction(const BasicBlock &BB);
+
   /// ExtractCodeRegion - Rip out a sequence of basic blocks into a new
   /// function.
   ///
index e8c0b80c21269c9b15e4f2331b540165c6ed167c..b8cea45178cc83a862af2b0cfa534641701e6b23 100644 (file)
@@ -756,24 +756,31 @@ ExtractCodeRegion(ArrayRef<BasicBlock*> code) {
 }
 
 bool CodeExtractor::isEligible(ArrayRef<BasicBlock*> code) {
-  // Deny a single basic block that's a landing pad block.
-  if (code.size() == 1 && code[0]->isLandingPad())
-    return false;
+  for (ArrayRef<BasicBlock*>::iterator I = code.begin(), E = code.end();
+       I != E; ++I)
+    if (!isBlockViableForExtraction(**I))
+      return false;
 
-  // Deny code region if it contains allocas or vastarts.
-  for (ArrayRef<BasicBlock*>::iterator BB = code.begin(), e=code.end();
-       BB != e; ++BB)
-    for (BasicBlock::const_iterator I = (*BB)->begin(), Ie = (*BB)->end();
-         I != Ie; ++I)
-      if (isa<AllocaInst>(*I))
-        return false;
-      else if (const CallInst *CI = dyn_cast<CallInst>(I))
-        if (const Function *F = CI->getCalledFunction())
-          if (F->getIntrinsicID() == Intrinsic::vastart)
-            return false;
   return true;
 }
 
+bool llvm::isBlockViableForExtraction(const BasicBlock &BB) {
+  // Landing pads must be in the function where they were inserted for cleanup.
+  if (BB.isLandingPad())
+    return false;
+
+  // Don't hoist code containing allocas, invokes, or vastarts.
+  for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
+    if (isa<AllocaInst>(I) || isa<InvokeInst>(I))
+      return false;
+    if (const CallInst *CI = dyn_cast<CallInst>(I))
+      if (const Function *F = CI->getCalledFunction())
+        if (F->getIntrinsicID() == Intrinsic::vastart)
+          return false;
+  }
+
+  return true;
+}
 
 /// ExtractCodeRegion - Slurp a sequence of basic blocks into a brand new
 /// function.