@@ -67,6 +67,9 @@ using ArrayAccessDesc = llvm::PointerIntPair<ValueBase *, 1, bool>;
67
67
using IndexedArraySet = llvm::DenseSet<std::pair<ValueBase *, ArrayAccessDesc>>;
68
68
using InstructionSet = llvm::SmallPtrSet<SILInstruction *, 16 >;
69
69
70
+ // Should be more than enough to cover "usual" functions.
71
+ static constexpr int maxRecursionDepth = 500 ;
72
+
70
73
// / The effect an instruction can have on array bounds.
71
74
enum class ArrayBoundsEffect {
72
75
kNone = 0 ,
@@ -378,12 +381,17 @@ static bool removeRedundantChecksInBlock(SILBasicBlock &BB, ArraySet &Arrays,
378
381
static bool removeRedundantChecks (DominanceInfoNode *CurBB,
379
382
ABCAnalysis &ABC,
380
383
IndexedArraySet &DominatingSafeChecks,
381
- SILLoop *Loop) {
384
+ SILLoop *Loop,
385
+ int recursionDepth) {
382
386
auto *BB = CurBB->getBlock ();
383
387
if (!Loop->contains (BB))
384
388
return false ;
385
389
bool Changed = false ;
386
390
391
+ // Avoid a stack overflow for very deep dominator trees.
392
+ if (recursionDepth >= maxRecursionDepth)
393
+ return false ;
394
+
387
395
// When we come back from the dominator tree recursion we need to remove
388
396
// checks that we have seen for the first time.
389
397
SmallVector<std::pair<ValueBase *, ArrayAccessDesc>, 8 > SafeChecksToPop;
@@ -436,7 +444,8 @@ static bool removeRedundantChecks(DominanceInfoNode *CurBB,
436
444
// Traverse the children in the dominator tree inside the loop.
437
445
for (auto Child: *CurBB)
438
446
Changed |=
439
- removeRedundantChecks (Child, ABC, DominatingSafeChecks, Loop);
447
+ removeRedundantChecks (Child, ABC, DominatingSafeChecks, Loop,
448
+ recursionDepth + 1 );
440
449
441
450
// Remove checks we have seen for the first time.
442
451
std::for_each (SafeChecksToPop.begin (), SafeChecksToPop.end (),
@@ -930,7 +939,12 @@ static bool hasArrayType(SILValue Value, SILModule &M) {
930
939
static bool hoistChecksInLoop (DominanceInfo *DT, DominanceInfoNode *DTNode,
931
940
ABCAnalysis &ABC, InductionAnalysis &IndVars,
932
941
SILBasicBlock *Preheader, SILBasicBlock *Header,
933
- SILBasicBlock *SingleExitingBlk) {
942
+ SILBasicBlock *SingleExitingBlk,
943
+ int recursionDepth) {
944
+
945
+ // Avoid a stack overflow for very deep dominator trees.
946
+ if (recursionDepth >= maxRecursionDepth)
947
+ return false ;
934
948
935
949
bool Changed = false ;
936
950
auto *CurBB = DTNode->getBlock ();
@@ -1029,7 +1043,7 @@ static bool hoistChecksInLoop(DominanceInfo *DT, DominanceInfoNode *DTNode,
1029
1043
// Traverse the children in the dominator tree.
1030
1044
for (auto Child: *DTNode)
1031
1045
Changed |= hoistChecksInLoop (DT, Child, ABC, IndVars, Preheader,
1032
- Header, SingleExitingBlk);
1046
+ Header, SingleExitingBlk, recursionDepth + 1 );
1033
1047
1034
1048
return Changed;
1035
1049
}
@@ -1127,7 +1141,8 @@ static bool hoistBoundsChecks(SILLoop *Loop, DominanceInfo *DT, SILLoopInfo *LI,
1127
1141
// check for safety outside the loop (with ABCAnalysis).
1128
1142
IndexedArraySet DominatingSafeChecks;
1129
1143
bool Changed = removeRedundantChecks (DT->getNode (Header), ABC,
1130
- DominatingSafeChecks, Loop);
1144
+ DominatingSafeChecks, Loop,
1145
+ /* recursionDepth*/ 0 );
1131
1146
1132
1147
if (!EnableABCHoisting)
1133
1148
return Changed;
@@ -1233,7 +1248,8 @@ static bool hoistBoundsChecks(SILLoop *Loop, DominanceInfo *DT, SILLoopInfo *LI,
1233
1248
1234
1249
// Hoist bounds checks.
1235
1250
Changed |= hoistChecksInLoop (DT, DT->getNode (Header), ABC, IndVars,
1236
- Preheader, Header, SingleExitingBlk);
1251
+ Preheader, Header, SingleExitingBlk,
1252
+ /* recursionDepth*/ 0 );
1237
1253
if (Changed) {
1238
1254
Preheader->getParent ()->verify ();
1239
1255
}
0 commit comments