From 5b96b13fdffebc1e81cdca3466e75b0d0e584c27 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Mon, 10 Apr 2023 16:37:10 +0700 Subject: [PATCH] [SCEV] Improve AddRecs' range computation in Expensive Range Sharpening mode Apply loop guards to AddRec's start in range computation for non-self-wrapping AddRecs. According to CT measurements, this has a wide negative compile time impact, so we hold it in expensive range sharpening mode where it's not so critical. However, we need to find a way to share benefits of this mode with default mode. Patch by Aleksandr Popov! Differential Revision: https://reviews.llvm.org/D147557 Reviewed By: mkazantsev --- llvm/lib/Analysis/ScalarEvolution.cpp | 2 +- llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index a2f0aa4..174eea8 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -7003,7 +7003,7 @@ ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR( // outside and inside the range [Min(Start, End), Max(Start, End)]. Using that // knowledge, let's try to prove that we are dealing with Case 1. It is so if // Start <= End and step is positive, or Start >= End and step is negative. - const SCEV *Start = AddRec->getStart(); + const SCEV *Start = applyLoopGuards(AddRec->getStart(), AddRec->getLoop()); ConstantRange StartRange = getRangeRef(Start, SignHint); ConstantRange EndRange = getRangeRef(End, SignHint); ConstantRange RangeBetween = StartRange.unionWith(EndRange); diff --git a/llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll b/llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll index e445332..3757ffc 100644 --- a/llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll +++ b/llvm/test/Analysis/ScalarEvolution/decrementing_addrecs.ll @@ -23,8 +23,8 @@ ; FIXME: c's AddRec is expected to be no-sign-wrap ; i is expected to be non-negative ; j is expected to be non-negative -; FIXME: a is expected to be positive -; FIXME: b is expected to be non-negative +; a is expected to be positive +; b is expected to be non-negative ; c is expected to be positive define i32 @test_step_1_flags(i32 %n) { ; DEFAULT-LABEL: 'test_step_1_flags' @@ -60,17 +60,17 @@ define i32 @test_step_1_flags(i32 %n) { ; EXPENSIVE_SHARPENING-NEXT: %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] ; EXPENSIVE_SHARPENING-NEXT: --> {0,+,1}<%loop> U: [0,2147483647) S: [0,2147483647) Exits: (-1 + %n) LoopDispositions: { %loop: Computable } ; EXPENSIVE_SHARPENING-NEXT: %j = phi i32 [ %n.minus.1, %entry ], [ %j.next, %loop ] -; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable } +; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<%loop> U: [0,2147483647) S: [0,2147483647) Exits: 0 LoopDispositions: { %loop: Computable } ; EXPENSIVE_SHARPENING-NEXT: %a = sub i32 %n, %i -; EXPENSIVE_SHARPENING-NEXT: --> {%n,+,-1}<%loop> U: full-set S: full-set Exits: 1 LoopDispositions: { %loop: Computable } +; EXPENSIVE_SHARPENING-NEXT: --> {%n,+,-1}<%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: 1 LoopDispositions: { %loop: Computable } ; EXPENSIVE_SHARPENING-NEXT: %b = sub i32 %n.minus.1, %i -; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<%loop> U: full-set S: full-set Exits: 0 LoopDispositions: { %loop: Computable } +; EXPENSIVE_SHARPENING-NEXT: --> {(-1 + %n),+,-1}<%loop> U: [0,2147483647) S: [0,2147483647) Exits: 0 LoopDispositions: { %loop: Computable } ; EXPENSIVE_SHARPENING-NEXT: %c = sub i32 2147483647, %i ; EXPENSIVE_SHARPENING-NEXT: --> {2147483647,+,-1}<%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: (-2147483648 + (-1 * %n)) LoopDispositions: { %loop: Computable } ; EXPENSIVE_SHARPENING-NEXT: %i.next = add nuw nsw i32 %i, 1 ; EXPENSIVE_SHARPENING-NEXT: --> {1,+,1}<%loop> U: [1,-2147483648) S: [1,-2147483648) Exits: %n LoopDispositions: { %loop: Computable } ; EXPENSIVE_SHARPENING-NEXT: %j.next = add nsw i32 %j, -1 -; EXPENSIVE_SHARPENING-NEXT: --> {(-2 + %n),+,-1}<%loop> U: full-set S: full-set Exits: -1 LoopDispositions: { %loop: Computable } +; EXPENSIVE_SHARPENING-NEXT: --> {(-2 + %n),+,-1}<%loop> U: full-set S: [-1,2147483646) Exits: -1 LoopDispositions: { %loop: Computable } ; EXPENSIVE_SHARPENING-NEXT: Determining loop execution counts for: @test_step_1_flags ; EXPENSIVE_SHARPENING-NEXT: Loop %loop: backedge-taken count is (-1 + %n) ; EXPENSIVE_SHARPENING-NEXT: Loop %loop: constant max backedge-taken count is 2147483646 -- 2.7.4