When extending a liveinterval by commuting, don't throw away the live ranges that...
[oota-llvm.git] / lib / CodeGen / BranchFolding.cpp
index a8fee1c9ac360f6ca51720c0db37a86c9c8472ad..e67339ab3860066f1d86587615c327b36a7cb7ac 100644 (file)
@@ -38,13 +38,13 @@ STATISTIC(NumBranchOpts, "Number of branches optimized");
 STATISTIC(NumTailMerge , "Number of block tails merged");
 static cl::opt<cl::boolOrDefault> FlagEnableTailMerge("enable-tail-merge", 
                               cl::init(cl::BOU_UNSET), cl::Hidden);
-namespace {
-  // Throttle for huge numbers of predecessors (compile speed problems)
-  static cl::opt<unsigned>
-  TailMergeThreshold("tail-merge-threshold", 
-            cl::desc("Max number of predecessors to consider tail merging"),
-            cl::init(100), cl::Hidden);
+// Throttle for huge numbers of predecessors (compile speed problems)
+static cl::opt<unsigned>
+TailMergeThreshold("tail-merge-threshold", 
+          cl::desc("Max number of predecessors to consider tail merging"),
+          cl::init(100), cl::Hidden);
 
+namespace {
   struct VISIBILITY_HIDDEN BranchFolder : public MachineFunctionPass {
     static char ID;
     explicit BranchFolder(bool defaultEnableTailMerge) : 
@@ -495,7 +495,18 @@ unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
                                         CurMPIter->second,
                                         I->second,
                                         TrialBBI1, TrialBBI2);
-      if (CommonTailLen >= minCommonTailLength) {
+      // If we will have to split a block, there should be at least
+      // minCommonTailLength instructions in common; if not, at worst
+      // we will be replacing a fallthrough into the common tail with a
+      // branch, which at worst breaks even with falling through into
+      // the duplicated common tail, so 1 instruction in common is enough.
+      // We will always pick a block we do not have to split as the common
+      // tail if there is one.
+      // (Empty blocks will get forwarded and need not be considered.)
+      if (CommonTailLen >= minCommonTailLength ||
+          (CommonTailLen > 0 &&
+           (TrialBBI1==CurMPIter->second->begin() ||
+            TrialBBI2==I->second->begin()))) {
         if (CommonTailLen > maxCommonTailLength) {
           SameTails.clear();
           maxCommonTailLength = CommonTailLen;
@@ -518,18 +529,20 @@ unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
 void BranchFolder::RemoveBlocksWithHash(unsigned CurHash, 
                                         MachineBasicBlock* SuccBB,
                                         MachineBasicBlock* PredBB) {
-  for (MPIterator CurMPIter = prior(MergePotentials.end()),
-                  B = MergePotentials.begin(); 
+  MPIterator CurMPIter, B;
+  for (CurMPIter = prior(MergePotentials.end()), B = MergePotentials.begin(); 
        CurMPIter->first==CurHash;
        --CurMPIter) {
     // Put the unconditional branch back, if we need one.
     MachineBasicBlock *CurMBB = CurMPIter->second;
     if (SuccBB && CurMBB != PredBB)
       FixTail(CurMBB, SuccBB, TII);
-    MergePotentials.erase(CurMPIter);
-    if (CurMPIter==B) 
+    if (CurMPIter==B)
       break;
   }
+  if (CurMPIter->first!=CurHash)
+    CurMPIter++;
+  MergePotentials.erase(CurMPIter, MergePotentials.end());
 }
 
 /// CreateCommonTailOnlyBlock - None of the blocks to be tail-merged consist