[llvm] 9bdb19c - [SCEV] (udiv X, Y) * Y is always NUW

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 15 11:35:01 PDT 2021


Author: Philip Reames
Date: 2021-09-15T11:34:50-07:00
New Revision: 9bdb19cca292f7a8335148e5c1b85f7e9120a1a5

URL: https://github.com/llvm/llvm-project/commit/9bdb19cca292f7a8335148e5c1b85f7e9120a1a5
DIFF: https://github.com/llvm/llvm-project/commit/9bdb19cca292f7a8335148e5c1b85f7e9120a1a5.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp
    llvm/test/Analysis/ScalarEvolution/extract-highbits-variablemask.ll
    llvm/test/Analysis/ScalarEvolution/max-be-count-not-constant.ll
    llvm/test/Analysis/ScalarEvolution/mul.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 60912adfd5a2..0af91d3851af 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -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;
 }
 

diff  --git a/llvm/test/Analysis/ScalarEvolution/extract-highbits-variablemask.ll b/llvm/test/Analysis/ScalarEvolution/extract-highbits-variablemask.ll
index ef79c9253503..bd191c42ca4c 100644
--- a/llvm/test/Analysis/ScalarEvolution/extract-highbits-variablemask.ll
+++ b/llvm/test/Analysis/ScalarEvolution/extract-highbits-variablemask.ll
@@ -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

diff  --git a/llvm/test/Analysis/ScalarEvolution/max-be-count-not-constant.ll b/llvm/test/Analysis/ScalarEvolution/max-be-count-not-constant.ll
index f757e064fcb2..008359e4b6a1 100644
--- a/llvm/test/Analysis/ScalarEvolution/max-be-count-not-constant.ll
+++ b/llvm/test/Analysis/ScalarEvolution/max-be-count-not-constant.ll
@@ -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

diff  --git a/llvm/test/Analysis/ScalarEvolution/mul.ll b/llvm/test/Analysis/ScalarEvolution/mul.ll
index 193c9527891f..0a29516526ce 100644
--- a/llvm/test/Analysis/ScalarEvolution/mul.ll
+++ b/llvm/test/Analysis/ScalarEvolution/mul.ll
@@ -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


        


More information about the llvm-commits mailing list