@@ -112,6 +112,11 @@ static cl::opt<unsigned> PHICSENumPHISmallSize(
112
112
" When the basic block contains not more than this number of PHI nodes, "
113
113
" perform a (faster!) exhaustive search instead of set-driven one." ));
114
114
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
+
115
120
// Max recursion depth for collectBitParts used when detecting bswap and
116
121
// bitreverse idioms.
117
122
static const unsigned BitPartRecursionMaxDepth = 48 ;
@@ -1040,6 +1045,42 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
1040
1045
return true ;
1041
1046
}
1042
1047
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
+
1043
1084
// / Replace a value flowing from a block to a phi with
1044
1085
// / potentially multiple instances of that value flowing from the
1045
1086
// / block's predecessors to the phi.
@@ -1139,7 +1180,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
1139
1180
BBKillable ||
1140
1181
CanRedirectPredsOfEmptyBBToSucc (BB, Succ, BBPreds, SuccPreds, CommonPred);
1141
1182
1142
- if (!BBKillable && !BBPhisMergeable)
1183
+ if (( !BBKillable && !BBPhisMergeable) || IntroduceTooComplexPhi (BB) )
1143
1184
return false ;
1144
1185
1145
1186
// Check to see if merging these blocks/phis would cause conflicts for any of
0 commit comments