[SCEV] Use mustprogress flag on loops (in addition to function attribute)
authorPhilip Reames <listmail@philipreames.com>
Thu, 10 Jun 2021 20:16:50 +0000 (13:16 -0700)
committerPhilip Reames <listmail@philipreames.com>
Thu, 10 Jun 2021 20:20:28 +0000 (13:20 -0700)
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
llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll

index 70191f1..06aa6cb 100644 (file)
@@ -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.
index 7d41a1f..6c59910 100644 (file)
@@ -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"}