[PATCH] D118429: [ScalarEvolution] Mark a loop as finite if in a willreturn function

William Moses via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 28 10:58:52 PST 2022


wsmoses updated this revision to Diff 404096.
wsmoses added a comment.

Add test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118429/new/

https://reviews.llvm.org/D118429

Files:
  llvm/include/llvm/Analysis/LoopInfo.h
  llvm/lib/Analysis/LoopInfo.cpp
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/lt-overflow.ll


Index: llvm/test/Analysis/ScalarEvolution/lt-overflow.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/lt-overflow.ll
+++ llvm/test/Analysis/ScalarEvolution/lt-overflow.ll
@@ -28,6 +28,8 @@
 ; CHECK: Loop %for.body: <multiple exits> Unpredictable backedge-taken count.
 ; CHECK: Determining loop execution counts for: @test_gt
 ; CHECK: Loop %for.body: Unpredictable backedge-taken count.
+; CHECK: Determining loop execution counts for: @test_willreturn
+; CHECK: Loop %for.body: backedge-taken count is ((-1 + (1024 umax %N)) /u 1024)
 
 define void @test(i32 %N) mustprogress {
 entry:
@@ -198,3 +200,20 @@
 for.cond.cleanup:
   ret void
 }
+
+declare void @sideeffect()
+
+define void @test_willreturn(i32 %S, i32 %N) willreturn {
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i32 [ %iv.next, %for.body ], [ 0, %entry ]
+  %iv.next = add i32 %iv, 1024
+  call void @sideeffect() nounwind willreturn
+  %cmp = icmp ult i32 %iv.next, %N
+  br i1 %cmp, label %for.body, label %for.cond.cleanup
+
+for.cond.cleanup:
+  ret void
+}
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -7017,7 +7017,7 @@
   // A mustprogress loop without side effects must be finite.
   // TODO: The check used here is very conservative.  It's only *specific*
   // side effects which are well defined in infinite loops.
-  return isMustProgress(L) && loopHasNoSideEffects(L);
+  return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L));
 }
 
 const SCEV *ScalarEvolution::createSCEV(Value *V) {
Index: llvm/lib/Analysis/LoopInfo.cpp
===================================================================
--- llvm/lib/Analysis/LoopInfo.cpp
+++ llvm/lib/Analysis/LoopInfo.cpp
@@ -1107,6 +1107,10 @@
   return getOptionalIntLoopAttribute(TheLoop, Name).getValueOr(Default);
 }
 
+bool llvm::isFinite(const Loop *L) {
+  return L->getHeader()->getParent()->willReturn();
+}
+
 static const char *LLVMLoopMustProgress = "llvm.loop.mustprogress";
 
 bool llvm::hasMustProgress(const Loop *L) {
Index: llvm/include/llvm/Analysis/LoopInfo.h
===================================================================
--- llvm/include/llvm/Analysis/LoopInfo.h
+++ llvm/include/llvm/Analysis/LoopInfo.h
@@ -1336,6 +1336,10 @@
 /// be infinite without side effects without also being undefined)
 bool isMustProgress(const Loop *L);
 
+/// Return true if this loop can be assumed to run for a finite number of
+/// iterations.
+bool isFinite(const Loop *L);
+
 /// Return whether an MDNode might represent an access group.
 ///
 /// Access group metadata nodes have to be distinct and empty. Being


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118429.404096.patch
Type: text/x-patch
Size: 2788 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220128/03d9f423/attachment.bin>


More information about the llvm-commits mailing list