From aaaeb4b160fe94e0ad3bcd6073eea4807f84a33a Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Thu, 10 Jun 2021 13:16:50 -0700 Subject: [PATCH] [SCEV] Use mustprogress flag on loops (in addition to function attribute) This addresses a performance regression reported against 3c6e4191. That change (correctly) limited a transform based on assumed finiteness to mustprogress loops, but the previous change (38540d7) which introduced the mustprogress check utility only handled function attributes, not the loop metadata form. It turns out that clang uses the function attribute form for C++, and the loop metadata form for C. As a result, 3c6e4191 ended up being a large regression in practice for C code as loops weren't being considered mustprogress despite the language semantics. --- llvm/lib/Analysis/ScalarEvolution.cpp | 4 ++-- .../ScalarEvolution/trip-count-unknown-stride.ll | 26 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 70191f1..06aa6cb 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -6579,8 +6579,8 @@ ScalarEvolution::getLoopProperties(const Loop *L) { } bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) { - // TODO: Use the loop metadata form of mustprogress as well. - if (!L->getHeader()->getParent()->mustProgress()) + if (!L->getHeader()->getParent()->mustProgress() && + !hasMustProgress(L)) return false; // A loop without side effects must be finite. diff --git a/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll b/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll index 7d41a1f..6c59910 100644 --- a/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll +++ b/llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll @@ -82,3 +82,29 @@ for.body: ; preds = %entry, %for.body for.end: ; preds = %for.body, %entry ret void } + +; Same as foo2, but with mustprogress on loop, not function +; CHECK: Determining loop execution counts for: @foo4 +; CHECK: backedge-taken count is ((-1 + (%n smax %s)) /u %s) +; CHECK: max backedge-taken count is -1 + +define void @foo4(i32* nocapture %A, i32 %n, i32 %s) { +entry: + br label %for.body + +for.body: ; preds = %entry, %for.body + %i.05 = phi i32 [ %add, %for.body ], [ 0, %entry ] + %arrayidx = getelementptr inbounds i32, i32* %A, i32 %i.05 + %0 = load i32, i32* %arrayidx, align 4 + %inc = add nsw i32 %0, 1 + store i32 %inc, i32* %arrayidx, align 4 + %add = add nsw i32 %i.05, %s + %cmp = icmp slt i32 %add, %n + br i1 %cmp, label %for.body, label %for.end, !llvm.loop !8 + +for.end: ; preds = %for.body, %entry + ret void +} + +!8 = distinct !{!8, !9} +!9 = !{!"llvm.loop.mustprogress"} -- 2.7.4