[PATCH] D126502: [SCEV] Apply conditions involving constants first in applyLoopGuards.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu May 26 14:30:21 PDT 2022
fhahn created this revision.
fhahn added reviewers: efriedma, nikic, reames, mkazantsev.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.
In some cases the order conditions are applied in can pessimize results.
In general, applying conditions with constant operands should lead to
better results. I think ideally we would apply conditions with operands
that are not involved in any other condition first, but processing
conditions with constants should be a good start.
Alternatively we could re-write all collected expressions after
collecting them. But that's likely more expensive.
Depends on D126460 <https://reviews.llvm.org/D126460>.
Fixes #55645.
Repository:
rG LLVM Github Monorepo
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
@@ -109,7 +109,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 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,23 @@
LoopEntryPredicate->getSuccessor(0) == Pair.second);
}
+ // Sort conditions so that conditions with constant operands 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 true;
+
+ bool ConstA = isa<SCEVConstant>(getSCEV(CmpA->getOperand(0))) ||
+ isa<SCEVConstant>(getSCEV(CmpA->getOperand(1)));
+ bool ConstB = isa<SCEVConstant>(getSCEV(CmpB->getOperand(0))) ||
+ isa<SCEVConstant>(getSCEV(CmpB->getOperand(1)));
+ return ConstA != ConstB;
+ });
+
// 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.432385.patch
Type: text/x-patch
Size: 3079 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220526/be15fa89/attachment.bin>
More information about the llvm-commits
mailing list