Do not convert massive blocks on phi nodes into select statements. Instead
authorChris Lattner <sabre@nondot.org>
Sat, 18 Nov 2006 19:19:36 +0000 (19:19 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 18 Nov 2006 19:19:36 +0000 (19:19 +0000)
only do these transformations if there are a small number of phi's.
This speeds up Ptrdist/ks from 2.35s to 2.19s on my mac pro.

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

lib/Transforms/Utils/SimplifyCFG.cpp

index 78f5941b7a15f1b6d50205bba4dd51279a13c388..afb483cc5f6df4f9756058125121db5d7ddebd8b 100644 (file)
@@ -1064,6 +1064,16 @@ static bool FoldTwoEntryPHINode(PHINode *PN) {
   Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
   if (!IfCond) return false;
   
+  // Okay, we found that we can merge this two-entry phi node into a select.
+  // Doing so would require us to fold *all* two entry phi nodes in this block.
+  // At some point this becomes non-profitable (particularly if the target
+  // doesn't support cmov's).  Only do this transformation if there are two or
+  // fewer PHI nodes in this block.
+  unsigned NumPhis = 0;
+  for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
+    if (NumPhis > 2)
+      return false;
+  
   DEBUG(std::cerr << "FOUND IF CONDITION!  " << *IfCond << "  T: "
         << IfTrue->getName() << "  F: " << IfFalse->getName() << "\n");
   
@@ -1552,6 +1562,23 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
               // keep getting unwound.
               if (PBIOp != -1 && PBI->getSuccessor(PBIOp) == BB)
                 PBIOp = BIOp = -1;
+              
+              // Do not perform this transformation if it would require 
+              // insertion of a large number of select instructions. For targets
+              // without predication/cmovs, this is a big pessimization.
+              if (PBIOp != -1) {
+                BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
+           
+                unsigned NumPhis = 0;
+                for (BasicBlock::iterator II = CommonDest->begin();
+                     isa<PHINode>(II); ++II, ++NumPhis) {
+                  if (NumPhis > 2) {
+                    // Disable this xform.
+                    PBIOp = -1;
+                    break;
+                  }
+                }
+              }
 
               // Finally, if everything is ok, fold the branches to logical ops.
               if (PBIOp != -1) {