[PATCH] D149571: [ValueTracking] Use `isKnownNonZero` for checking if `sdiv`/`srem` are speculatable

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 30 22:01:47 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, spatel, StephenFan.
Herald added subscribers: asbirlea, hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Previously was using `KnownBits.isNonZero()` which is weaker the
`isKnownNonZero`. `KnownBits.isNonZero()` requires us to actually know
a bit which is often not the case even when we know non-zero.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149571

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Transforms/InstCombine/binop-select.ll
  llvm/test/Transforms/LICM/speculate-div.ll


Index: llvm/test/Transforms/LICM/speculate-div.ll
===================================================================
--- llvm/test/Transforms/LICM/speculate-div.ll
+++ llvm/test/Transforms/LICM/speculate-div.ll
@@ -97,10 +97,10 @@
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[N:%.*]] = and i16 [[NN:%.*]], 123
 ; CHECK-NEXT:    [[X:%.*]] = add nuw nsw i16 [[XX:%.*]], 1
+; CHECK-NEXT:    [[DIV:%.*]] = srem i16 [[N]], [[X]]
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
 ; CHECK-NEXT:    call void @maythrow()
-; CHECK-NEXT:    [[DIV:%.*]] = srem i16 [[N]], [[X]]
 ; CHECK-NEXT:    call void @use(i16 [[DIV]])
 ; CHECK-NEXT:    br label [[LOOP]]
 ;
Index: llvm/test/Transforms/InstCombine/binop-select.ll
===================================================================
--- llvm/test/Transforms/InstCombine/binop-select.ll
+++ llvm/test/Transforms/InstCombine/binop-select.ll
@@ -479,8 +479,8 @@
 define <2 x i32> @test_sdiv_to_const_Csdiv_todo_no_common_bit(<2 x i32> %x) {
 ; CHECK-LABEL: @test_sdiv_to_const_Csdiv_todo_no_common_bit(
 ; CHECK-NEXT:    [[C_NOT:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 90, i32 91>
-; CHECK-NEXT:    [[COND:%.*]] = select <2 x i1> [[C_NOT]], <2 x i32> <i32 16, i32 4>, <2 x i32> <i32 7, i32 9>
-; CHECK-NEXT:    [[DIV:%.*]] = sdiv <2 x i32> [[X]], [[COND]]
+; CHECK-NEXT:    [[TMP1:%.*]] = sdiv <2 x i32> [[X]], <i32 7, i32 9>
+; CHECK-NEXT:    [[DIV:%.*]] = select <2 x i1> [[C_NOT]], <2 x i32> <i32 5, i32 22>, <2 x i32> [[TMP1]]
 ; CHECK-NEXT:    ret <2 x i32> [[DIV]]
 ;
   %c = icmp ne <2 x i32> %x, <i32 90, i32 91>
@@ -505,8 +505,8 @@
 define i32 @test_srem_to_const_Csrem_todo_no_common_bits(i32 %x) {
 ; CHECK-LABEL: @test_srem_to_const_Csrem_todo_no_common_bits(
 ; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[X:%.*]], 24
-; CHECK-NEXT:    [[COND:%.*]] = select i1 [[C]], i32 7, i32 16
-; CHECK-NEXT:    [[DIV:%.*]] = srem i32 [[X]], [[COND]]
+; CHECK-NEXT:    [[TMP1:%.*]] = srem i32 [[X]], 16
+; CHECK-NEXT:    [[DIV:%.*]] = select i1 [[C]], i32 3, i32 [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[DIV]]
 ;
   %c = icmp eq i32 %x, 24
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -6083,12 +6083,13 @@
   case Instruction::SRem: {
     // x / y is undefined if y == 0 or x == INT_MIN and y == -1
     const DataLayout &DL = Inst->getModule()->getDataLayout();
-    KnownBits KnownDenominator =
-        computeKnownBits(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
     // We cannot hoist this division if the denominator is 0.
-    if (!KnownDenominator.isNonZero())
+    if (!isKnownNonZero(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT))
       return false;
 
+    KnownBits KnownDenominator =
+        computeKnownBits(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT);
+
     // It's safe to hoist if the denominator is not 0 or -1.
     if (!KnownDenominator.Zero.isZero())
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149571.518398.patch
Type: text/x-patch
Size: 3017 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230501/a31daca7/attachment.bin>


More information about the llvm-commits mailing list