Do not copy gigantic switch instructions
authorChris Lattner <sabre@nondot.org>
Tue, 16 Mar 2004 19:45:22 +0000 (19:45 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 16 Mar 2004 19:45:22 +0000 (19:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12441 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/TailDuplication.cpp
lib/Transforms/Utils/SimplifyCFG.cpp

index a3980b342c2ec16aaa0e2a577d5bb748782cb857..00d7f26303bd5dd03ac0885c0708a7c00240f15f 100644 (file)
@@ -91,7 +91,8 @@ bool TailDup::shouldEliminateUnconditionalBranch(TerminatorInst *TI) {
   if (Dest == BI->getParent()) return false;        // Do not loop infinitely!
 
   // Do not inline a block if we will just get another branch to the same block!
-  if (BranchInst *DBI = dyn_cast<BranchInst>(Dest->getTerminator()))
+  TerminatorInst *DTI = Dest->getTerminator();
+  if (BranchInst *DBI = dyn_cast<BranchInst>(DTI))
     if (DBI->isUnconditional() && DBI->getSuccessor(0) == Dest)
       return false;                                 // Do not loop infinitely!
 
@@ -110,6 +111,15 @@ bool TailDup::shouldEliminateUnconditionalBranch(TerminatorInst *TI) {
 
   for (unsigned Size = 0; I != Dest->end(); ++Size, ++I)
     if (Size == 6) return false;  // The block is too large...
+
+  // Do not tail duplicate a block that has thousands of successors into a block
+  // with a single successor if the block has many other predecessors.  This can
+  // cause an N^2 explosion in CFG edges (and PHI node entries), as seen in
+  // cases that have a large number of indirect gotos.
+  if (DTI->getNumSuccessors() > 8)
+    if (std::distance(PI, PE) * DTI->getNumSuccessors() > 128)
+      return false;
+
   return true;  
 }
 
index 089d0ac86e723a4398acc52645ae397bddc850ad..09153ed3bb096d338757a5eaaeeb183c80838d4b 100644 (file)
@@ -331,8 +331,15 @@ static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
 // isValueEqualityComparison - Return true if the specified terminator checks to
 // see if a value is equal to constant integer value.
 static Value *isValueEqualityComparison(TerminatorInst *TI) {
-  if (SwitchInst *SI = dyn_cast<SwitchInst>(TI))
+  if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
+    // Do not permit merging of large switch instructions into their
+    // predecessors unless there is only one predecessor.
+    if (SI->getNumSuccessors() * std::distance(pred_begin(SI->getParent()),
+                                               pred_end(SI->getParent())) > 128)
+      return 0;
+
     return SI->getCondition();
+  }
   if (BranchInst *BI = dyn_cast<BranchInst>(TI))
     if (BI->isConditional() && BI->getCondition()->hasOneUse())
       if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition()))