@@ -864,6 +864,10 @@ static void reportBoundsChecks(SILFunction *F) {
864864#endif
865865
866866namespace {
867+
868+ // Should be more than enough to cover "usual" functions.
869+ static constexpr int maxRecursionDepth = 500 ;
870+
867871// / Remove redundant checks in basic blocks and hoist redundant checks out of
868872// / loops.
869873class ABCOpt : public SILFunctionTransform {
@@ -885,13 +889,15 @@ class ABCOpt : public SILFunctionTransform {
885889 // / Walk down the dominator tree inside the loop, removing redundant checks.
886890 bool removeRedundantChecksInLoop (DominanceInfoNode *CurBB, ABCAnalysis &ABC,
887891 IndexedArraySet &DominatingSafeChecks,
888- SILLoop *Loop);
892+ SILLoop *Loop,
893+ int recursionDepth);
889894 // / Analyze the loop for arrays that are not modified and perform dominator
890895 // / tree based redundant bounds check removal.
891896 bool hoistChecksInLoop (DominanceInfoNode *DTNode, ABCAnalysis &ABC,
892897 InductionAnalysis &IndVars, SILBasicBlock *Preheader,
893898 SILBasicBlock *Header,
894- SILBasicBlock *SingleExitingBlk);
899+ SILBasicBlock *SingleExitingBlk,
900+ int recursionDepth);
895901
896902public:
897903 void run () override {
@@ -1049,10 +1055,16 @@ bool ABCOpt::removeRedundantChecksInBlock(SILBasicBlock &BB) {
10491055bool ABCOpt::removeRedundantChecksInLoop (DominanceInfoNode *CurBB,
10501056 ABCAnalysis &ABC,
10511057 IndexedArraySet &DominatingSafeChecks,
1052- SILLoop *Loop) {
1058+ SILLoop *Loop,
1059+ int recursionDepth) {
10531060 auto *BB = CurBB->getBlock ();
10541061 if (!Loop->contains (BB))
10551062 return false ;
1063+
1064+ // Avoid a stack overflow for very deep dominator trees.
1065+ if (recursionDepth >= maxRecursionDepth)
1066+ return false ;
1067+
10561068 bool Changed = false ;
10571069
10581070 // When we come back from the dominator tree recursion we need to remove
@@ -1106,7 +1118,8 @@ bool ABCOpt::removeRedundantChecksInLoop(DominanceInfoNode *CurBB,
11061118 // Traverse the children in the dominator tree inside the loop.
11071119 for (auto Child : *CurBB)
11081120 Changed |=
1109- removeRedundantChecksInLoop (Child, ABC, DominatingSafeChecks, Loop);
1121+ removeRedundantChecksInLoop (Child, ABC, DominatingSafeChecks, Loop,
1122+ recursionDepth + 1 );
11101123
11111124 // Remove checks we have seen for the first time.
11121125 std::for_each (SafeChecksToPop.begin (), SafeChecksToPop.end (),
@@ -1149,7 +1162,8 @@ bool ABCOpt::processLoop(SILLoop *Loop) {
11491162 // check for safety outside the loop (with ABCAnalysis).
11501163 IndexedArraySet DominatingSafeChecks;
11511164 bool Changed = removeRedundantChecksInLoop (DT->getNode (Header), ABC,
1152- DominatingSafeChecks, Loop);
1165+ DominatingSafeChecks, Loop,
1166+ /* recursionDepth*/ 0 );
11531167
11541168 if (!EnableABCHoisting)
11551169 return Changed;
@@ -1255,7 +1269,7 @@ bool ABCOpt::processLoop(SILLoop *Loop) {
12551269
12561270 // Hoist bounds checks.
12571271 Changed |= hoistChecksInLoop (DT->getNode (Header), ABC, IndVars, Preheader,
1258- Header, SingleExitingBlk);
1272+ Header, SingleExitingBlk, /* recursionDepth */ 0 );
12591273 if (Changed) {
12601274 Preheader->getParent ()->verify ();
12611275 }
@@ -1265,7 +1279,12 @@ bool ABCOpt::processLoop(SILLoop *Loop) {
12651279bool ABCOpt::hoistChecksInLoop (DominanceInfoNode *DTNode, ABCAnalysis &ABC,
12661280 InductionAnalysis &IndVars,
12671281 SILBasicBlock *Preheader, SILBasicBlock *Header,
1268- SILBasicBlock *SingleExitingBlk) {
1282+ SILBasicBlock *SingleExitingBlk,
1283+ int recursionDepth) {
1284+ // Avoid a stack overflow for very deep dominator trees.
1285+ if (recursionDepth >= maxRecursionDepth)
1286+ return false ;
1287+
12691288 bool Changed = false ;
12701289 auto *CurBB = DTNode->getBlock ();
12711290 bool blockAlwaysExecutes =
@@ -1364,7 +1383,7 @@ bool ABCOpt::hoistChecksInLoop(DominanceInfoNode *DTNode, ABCAnalysis &ABC,
13641383 // Traverse the children in the dominator tree.
13651384 for (auto Child : *DTNode)
13661385 Changed |= hoistChecksInLoop (Child, ABC, IndVars, Preheader, Header,
1367- SingleExitingBlk);
1386+ SingleExitingBlk, recursionDepth + 1 );
13681387
13691388 return Changed;
13701389}
0 commit comments