@@ -112,6 +112,11 @@ static cl::opt<unsigned> PHICSENumPHISmallSize(
112112 " When the basic block contains not more than this number of PHI nodes, "
113113 " perform a (faster!) exhaustive search instead of set-driven one." ));
114114
115+ static cl::opt<unsigned > MaxPhiEntriesAfterRemovingEmptyBlock (
116+ " max-phi-entries-after-removing-empty-block" , cl::init(100 ), cl::Hidden,
117+ cl::desc(" Stop removing an empty block if removing it will make a PHI have "
118+ " more than this number of incoming entries." ));
119+
115120// Max recursion depth for collectBitParts used when detecting bswap and
116121// bitreverse idioms.
117122static const unsigned BitPartRecursionMaxDepth = 48 ;
@@ -1040,6 +1045,42 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
10401045 return true ;
10411046}
10421047
1048+ // Check whether removing BB will make the phis in its Succ will have too
1049+ // many incoming entries. This function does not check whether BB is foldable
1050+ // or not.
1051+ static bool introduceTooComplexPhi (BasicBlock *BB) {
1052+ // Check BB only has phi and an unconditional branch
1053+ BranchInst *Branch = dyn_cast<BranchInst>(BB->getFirstNonPHIOrDbg (true ));
1054+ assert (Branch && Branch->isUnconditional () && " BB is not an empty block" );
1055+
1056+ // If BB only has one predecessor, then removing it will not introduce more
1057+ // incoming edges for phis.
1058+ if (BB->hasNPredecessors (1 ))
1059+ return false ;
1060+ int NumPreds = pred_size (BB);
1061+ auto *Succ = BB->getTerminator ()->getSuccessor (0 );
1062+ for (auto &Phi : Succ->phis ()) {
1063+ auto BlockIdx = Phi.getBasicBlockIndex (BB);
1064+ if (BlockIdx >= 0 ) {
1065+ // If the incoming value is a phi and the phi is defined in BB,
1066+ // then removing BB will not increase the total phi entries of the ir.
1067+ if (PHINode *IncomingPhi =
1068+ dyn_cast<PHINode>(Phi.getIncomingValue (BlockIdx)))
1069+ if (IncomingPhi->getParent () == BB)
1070+ continue ;
1071+ // Otherwise, we need to add (NumPreds - 1) entries to the phi node.
1072+ // If removing BB makes the phi have more than
1073+ // MaxPhiEntriesAfterRemovingEmptyBlock incoming values, then it will be
1074+ // considered as introducing too complex phi to the ir.
1075+ // The default threshold is 100.
1076+ if ((NumPreds - 1 ) + Phi.getNumIncomingValues () >
1077+ MaxPhiEntriesAfterRemovingEmptyBlock)
1078+ return true ;
1079+ }
1080+ }
1081+ return false ;
1082+ }
1083+
10431084// / Replace a value flowing from a block to a phi with
10441085// / potentially multiple instances of that value flowing from the
10451086// / block's predecessors to the phi.
@@ -1139,7 +1180,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11391180 BBKillable ||
11401181 CanRedirectPredsOfEmptyBBToSucc (BB, Succ, BBPreds, SuccPreds, CommonPred);
11411182
1142- if (!BBKillable && !BBPhisMergeable)
1183+ if (( !BBKillable && !BBPhisMergeable) || IntroduceTooComplexPhi (BB) )
11431184 return false ;
11441185
11451186 // Check to see if merging these blocks/phis would cause conflicts for any of
0 commit comments