[SCEV] (udiv X, Y) * Y is always NUW

Motivated by the removal done in D109782. This implements the correct flag part generically.

Differential Revision: https://reviews.llvm.org/D109786
This commit is contained in:
Philip Reames 2021-09-15 11:33:31 -07:00
parent 1b79efdc72
commit 9bdb19cca2
4 changed files with 16 additions and 5 deletions

View File

@ -2397,6 +2397,17 @@ StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
Ops[0]->isZero() && IsKnownNonNegative(Ops[1]))
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
// both (udiv X, Y) * Y and Y * (udiv X, Y) are always NUW
if (Type == scMulExpr && !ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) &&
Ops.size() == 2) {
if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[0]))
if (UDiv->getOperand(1) == Ops[1])
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[1]))
if (UDiv->getOperand(1) == Ops[0])
Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
}
return Flags;
}

View File

@ -10,7 +10,7 @@ define i32 @div(i32 %val, i32 %num) nounwind {
; CHECK-NEXT: %tmp1 = udiv i32 %val, %num
; CHECK-NEXT: --> (%val /u %num) U: full-set S: full-set
; CHECK-NEXT: %tmp2 = mul i32 %tmp1, %num
; CHECK-NEXT: --> ((%val /u %num) * %num) U: full-set S: full-set
; CHECK-NEXT: --> ((%val /u %num) * %num)<nuw> U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @div
;
%tmp1 = udiv i32 %val, %num

View File

@ -16,9 +16,9 @@ define void @pluto(i32 %arg) {
; CHECK-NEXT: %tmp1 = add nsw i32 %tmp, 2
; CHECK-NEXT: --> (2 + %tmp)<nsw> U: [1,3) S: [1,3)
; CHECK-NEXT: %tmp3 = phi i32 [ 0, %bb ], [ %tmp4, %bb2 ]
; CHECK-NEXT: --> {0,+,(2 + %tmp)<nsw>}<nuw><nsw><%bb2> U: [0,3) S: [0,3) Exits: ((2 + %tmp)<nsw> * (1 /u (2 + %tmp)<nsw>)) LoopDispositions: { %bb2: Computable }
; CHECK-NEXT: --> {0,+,(2 + %tmp)<nsw>}<nuw><nsw><%bb2> U: [0,3) S: [0,3) Exits: ((2 + %tmp)<nsw> * (1 /u (2 + %tmp)<nsw>))<nuw> LoopDispositions: { %bb2: Computable }
; CHECK-NEXT: %tmp4 = add nuw nsw i32 %tmp1, %tmp3
; CHECK-NEXT: --> {(2 + %tmp)<nsw>,+,(2 + %tmp)<nsw>}<nuw><nsw><%bb2> U: [1,5) S: [1,5) Exits: (2 + ((2 + %tmp)<nsw> * (1 /u (2 + %tmp)<nsw>)) + %tmp) LoopDispositions: { %bb2: Computable }
; CHECK-NEXT: --> {(2 + %tmp)<nsw>,+,(2 + %tmp)<nsw>}<nuw><nsw><%bb2> U: [1,5) S: [1,5) Exits: (2 + ((2 + %tmp)<nsw> * (1 /u (2 + %tmp)<nsw>))<nuw> + %tmp) LoopDispositions: { %bb2: Computable }
; CHECK-NEXT: Determining loop execution counts for: @pluto
; CHECK-NEXT: Loop %bb2: backedge-taken count is (1 /u (2 + %tmp)<nsw>)
; CHECK-NEXT: Loop %bb2: max backedge-taken count is 1

View File

@ -7,7 +7,7 @@ define i8 @test(i8 %x, i8 %y) {
; CHECK-NEXT: %udiv = udiv i8 %x, %y
; CHECK-NEXT: --> (%x /u %y) U: full-set S: full-set
; CHECK-NEXT: %res = mul i8 %udiv, %y
; CHECK-NEXT: --> ((%x /u %y) * %y) U: full-set S: full-set
; CHECK-NEXT: --> ((%x /u %y) * %y)<nuw> U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @test
;
%udiv = udiv i8 %x, %y
@ -65,7 +65,7 @@ define i8 @test5(i8 %x, i32 %y32) {
; CHECK-NEXT: %udiv = udiv i8 %x, %y
; CHECK-NEXT: --> (%x /u (trunc i32 %y32 to i8)) U: full-set S: full-set
; CHECK-NEXT: %res = mul i8 %udiv, %y
; CHECK-NEXT: --> ((trunc i32 %y32 to i8) * (%x /u (trunc i32 %y32 to i8))) U: full-set S: full-set
; CHECK-NEXT: --> ((trunc i32 %y32 to i8) * (%x /u (trunc i32 %y32 to i8)))<nuw> U: full-set S: full-set
; CHECK-NEXT: Determining loop execution counts for: @test5
;
%y = trunc i32 %y32 to i8