[PATCH] D126502: [SCEV] Apply conditions involving constants first in applyLoopGuards.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 27 04:52:14 PDT 2022


fhahn updated this revision to Diff 432523.
fhahn added a comment.

Update sort order to apply conditions that constraint ranges using max/min (predicates != EQ & != NE).

This ensures we rewrite conditons involving URem checks last, which can lead to better results.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126502

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Analysis/ScalarEvolution/trip-multiple-guard-info.ll


Index: llvm/test/Analysis/ScalarEvolution/trip-multiple-guard-info.ll
===================================================================
--- llvm/test/Analysis/ScalarEvolution/trip-multiple-guard-info.ll
+++ llvm/test/Analysis/ScalarEvolution/trip-multiple-guard-info.ll
@@ -34,7 +34,7 @@
 ; CHECK:       Loop %for.body: backedge-taken count is (-1 + %num)
 ; CHECK-NEXT:  Loop %for.body: max backedge-taken count is -2
 ; CHECK-NEXT:  Loop %for.body: Predicated backedge-taken count is (-1 + %num)
-; CHECK:       Loop %for.body: Trip multiple is 2
+; CHECK:       Loop %for.body: Trip multiple is 4
 ;
 entry:
   %u = urem i32 %num, 4
@@ -59,7 +59,7 @@
 ; CHECK:       Loop %for.body: backedge-taken count is (-1 + %num)
 ; CHECK-NEXT:  Loop %for.body: max backedge-taken count is 2147483646
 ; CHECK-NEXT:  Loop %for.body: Predicated backedge-taken count is (-1 + %num)
-; CHECK:       Loop %for.body: Trip multiple is 2
+; CHECK:       Loop %for.body: Trip multiple is 4
 ;
 entry:
   %u = urem i32 %num, 4
@@ -84,7 +84,7 @@
 ; CHECK:       Loop %for.body: backedge-taken count is (-1 + %num)
 ; CHECK-NEXT:  Loop %for.body: max backedge-taken count is -2
 ; CHECK-NEXT:  Loop %for.body: Predicated backedge-taken count is (-1 + %num)
-; CHECK:       Loop %for.body: Trip multiple is 1
+; CHECK:       Loop %for.body: Trip multiple is 4
 ;
 entry:
   %u = urem i32 %num, 4
@@ -104,12 +104,37 @@
   ret void
 }
 
+define void @test_trip_multiple_4_uge_5_order_swapped(i32 %num) {
+; CHECK-LABEL: @test_trip_multiple_4_uge_5_order_swapped
+; CHECK:       Loop %for.body: backedge-taken count is (-1 + %num)
+; CHECK-NEXT:  Loop %for.body: max backedge-taken count is -2
+; CHECK-NEXT:  Loop %for.body: Predicated backedge-taken count is (-1 + %num)
+; CHECK:       Loop %for.body: Trip multiple is 4
+;
+entry:
+  %u = urem i32 %num, 4
+  %cmp = icmp eq i32 %u, 0
+  %cmp.1 = icmp uge i32 %num, 5
+  tail call void @llvm.assume(i1 %cmp.1)
+  tail call void @llvm.assume(i1 %cmp)
+  br label %for.body
+
+for.body:
+  %i.010 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+  %inc = add nuw nsw i32 %i.010, 1
+  %cmp2 = icmp ult i32 %inc, %num
+  br i1 %cmp2, label %for.body, label %exit
+
+exit:
+  ret void
+}
+
 define void @test_trip_multiple_4_sge_5(i32 %num) {
 ; CHECK-LABEL: @test_trip_multiple_4_sge_5
 ; CHECK:       Loop %for.body: backedge-taken count is (-1 + %num)
 ; CHECK-NEXT:  Loop %for.body: max backedge-taken count is 2147483646
 ; CHECK-NEXT:  Loop %for.body: Predicated backedge-taken count is (-1 + %num)
-; CHECK:       Loop %for.body: Trip multiple is 1
+; CHECK:       Loop %for.body: Trip multiple is 4
 ;
 entry:
   %u = urem i32 %num, 4
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -14548,6 +14548,22 @@
                        LoopEntryPredicate->getSuccessor(0) == Pair.second);
   }
 
+  // Sort conditions so that conditions that tighten ranges of values come last.
+  // This allows processing them first in the loop below, which can lead to more
+  // effective expression rewriting.
+  stable_sort(Terms, [this](const std::pair<Value *, bool> &A,
+                            const std::pair<Value *, bool> &B) {
+    auto *CmpA = dyn_cast<ICmpInst>(A.first);
+    auto *CmpB = dyn_cast<ICmpInst>(B.first);
+    if (!CmpA || !CmpB)
+      return false;
+    bool IsRangeA = CmpA->getPredicate() != CmpInst::ICMP_NE &&
+                    CmpA->getPredicate() != CmpInst::ICMP_EQ;
+    bool IsRangeB = CmpB->getPredicate() != CmpInst::ICMP_NE &&
+                    CmpB->getPredicate() != CmpInst::ICMP_EQ;
+    return IsRangeA < IsRangeB;
+  });
+
   // Now apply the information from the collected conditions to RewriteMap.
   // Conditions are processed in reverse order, so the earliest conditions is
   // processed first. This ensures the SCEVs with the shortest dependency chains


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126502.432523.patch
Type: text/x-patch
Size: 3981 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220527/ed9f19a7/attachment-0001.bin>


More information about the llvm-commits mailing list