[SCEV] Cap the number of instructions scanned when infering flags
authorPhilip Reames <listmail@philipreames.com>
Sun, 3 Oct 2021 23:14:06 +0000 (16:14 -0700)
committerPhilip Reames <listmail@philipreames.com>
Sun, 3 Oct 2021 23:14:06 +0000 (16:14 -0700)
This addresses a comment from review on D109845.  The concern was raised that an unbounded scan would be expensive.  Long term plan is to cache this search - likely reusing the existing mechanism for loop side effects - but let's be simple and conservative for now.

llvm/lib/Analysis/ScalarEvolution.cpp

index f9d7ff2..b51e8a7 100644 (file)
@@ -6597,9 +6597,19 @@ const Instruction *ScalarEvolution::getDefiningScopeBound(const SCEV *S) {
 static bool
 isGuaranteedToTransferExecutionToSuccessor(BasicBlock::const_iterator Begin,
                                            BasicBlock::const_iterator End) {
-  return llvm::all_of( make_range(Begin, End), [](const Instruction &I) {
-    return isGuaranteedToTransferExecutionToSuccessor(&I);
-  });
+  // Limit number of instructions we look at, to avoid scanning through large
+  // blocks. The current limit is chosen arbitrarily.
+  unsigned ScanLimit = 32;
+  for (const Instruction &I : make_range(Begin, End)) {
+    if (isa<DbgInfoIntrinsic>(I))
+        continue;
+    if (--ScanLimit == 0)
+      return false;
+
+    if (!isGuaranteedToTransferExecutionToSuccessor(&I))
+      return false;
+  }
+  return true;
 }
 
 bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A,