[SCEV] ] If RHS >= Start, simplify (Start smax RHS) to RHS for trip counts.

This is the max version of D85046.

This change causes binary changes in 44 out of 237 benchmarks (out of
MultiSource/SPEC2000/SPEC2006)

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D85189
This commit is contained in:
Florian Hahn 2020-08-11 13:05:04 +01:00
parent 026e0bf984
commit 3483c28c5b
2 changed files with 11 additions and 5 deletions
llvm
lib/Analysis
test/Analysis/ScalarEvolution

View File

@ -10561,7 +10561,13 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
if (isLoopEntryGuardedByCond(L, Cond, getMinusSCEV(Start, Stride), RHS))
BECount = BECountIfBackedgeTaken;
else {
End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start);
// If we know that RHS >= Start in the context of loop, then we know that
// max(RHS, Start) = RHS at this point.
if (isLoopEntryGuardedByCond(
L, IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, RHS, Start))
End = RHS;
else
End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start);
BECount = computeBECount(getMinusSCEV(End, Start), Stride, false);
}

View File

@ -98,13 +98,13 @@ define void @smax_simplify_with_guard(i32 %start, i32 %n) {
; CHECK-LABEL: 'smax_simplify_with_guard'
; CHECK-NEXT: Classifying expressions for: @smax_simplify_with_guard
; CHECK-NEXT: %k.0.i26 = phi i32 [ %start, %loop.ph ], [ %inc.i, %loop ]
; CHECK-NEXT: --> {%start,+,1}<nsw><%loop> U: full-set S: full-set Exits: (%start smax %n) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: --> {%start,+,1}<nsw><%loop> U: full-set S: full-set Exits: %n LoopDispositions: { %loop: Computable }
; CHECK-NEXT: %inc.i = add nsw i32 %k.0.i26, 1
; CHECK-NEXT: --> {(1 + %start),+,1}<nw><%loop> U: full-set S: full-set Exits: (1 + (%start smax %n)) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: --> {(1 + %start),+,1}<nw><%loop> U: full-set S: full-set Exits: (1 + %n) LoopDispositions: { %loop: Computable }
; CHECK-NEXT: Determining loop execution counts for: @smax_simplify_with_guard
; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 * %start) + (%start smax %n))
; CHECK-NEXT: Loop %loop: backedge-taken count is ((-1 * %start) + %n)
; CHECK-NEXT: Loop %loop: max backedge-taken count is -1
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 * %start) + (%start smax %n))
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is ((-1 * %start) + %n)
; CHECK-NEXT: Predicates:
; CHECK: Loop %loop: Trip multiple is 1
entry: