[PATCH] D156777: [SelectionDAG] Add/Improve cases in `isKnownNeverZero`
Noah Goldstein via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 2 09:35:04 PDT 2023
goldstein.w.n updated this revision to Diff 546512.
goldstein.w.n marked an inline comment as done.
goldstein.w.n added a comment.
Remove any_extend, add other casts back to main switch
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D156777/new/
https://reviews.llvm.org/D156777
Files:
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
llvm/test/CodeGen/X86/known-never-zero.ll
Index: llvm/test/CodeGen/X86/known-never-zero.ll
===================================================================
--- llvm/test/CodeGen/X86/known-never-zero.ll
+++ llvm/test/CodeGen/X86/known-never-zero.ll
@@ -258,9 +258,7 @@
; CHECK-NEXT: addl $4, %esi
; CHECK-NEXT: cmpl %esi, %eax
; CHECK-NEXT: cmovll %eax, %esi
-; CHECK-NEXT: bsfl %esi, %ecx
-; CHECK-NEXT: movl $32, %eax
-; CHECK-NEXT: cmovnel %ecx, %eax
+; CHECK-NEXT: rep bsfl %esi, %eax
; CHECK-NEXT: retq
%x = shl nuw i32 4, %xx
%y = add nuw nsw i32 %yy, 4
@@ -298,9 +296,7 @@
; CHECK-NEXT: addl $4, %esi
; CHECK-NEXT: cmpl %esi, %eax
; CHECK-NEXT: cmovgl %eax, %esi
-; CHECK-NEXT: bsfl %esi, %ecx
-; CHECK-NEXT: movl $32, %eax
-; CHECK-NEXT: cmovnel %ecx, %eax
+; CHECK-NEXT: rep bsfl %esi, %eax
; CHECK-NEXT: retq
%x = shl nuw i32 4, %xx
%y = add nuw nsw i32 %yy, 4
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -5106,21 +5106,27 @@
return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
isKnownNeverZero(Op.getOperand(2), Depth + 1);
- case ISD::SHL:
+ case ISD::SHL: {
if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
return isKnownNeverZero(Op.getOperand(0), Depth + 1);
-
- // 1 << X is never zero. TODO: This can be expanded if we can bound X.
- // The expression is really !Known.One[BitWidth-MaxLog2(Known):0].isZero()
- if (computeKnownBits(Op.getOperand(0), Depth + 1).One[0])
+ KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
+ // 1 << X is never zero.
+ if (ValKnown.One[0])
+ return true;
+ // If max shift cnt of known ones is non-zero, result is non-zero.
+ APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
+ if (MaxCnt.ule(ValKnown.getBitWidth()) &&
+ !ValKnown.One.shl(MaxCnt).isZero())
return true;
break;
-
+ }
case ISD::UADDSAT:
case ISD::UMAX:
return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
isKnownNeverZero(Op.getOperand(0), Depth + 1);
+ case ISD::SMAX:
+ case ISD::SMIN:
case ISD::UMIN:
return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
isKnownNeverZero(Op.getOperand(0), Depth + 1);
@@ -5134,16 +5140,19 @@
return isKnownNeverZero(Op.getOperand(0), Depth + 1);
case ISD::SRA:
- case ISD::SRL:
+ case ISD::SRL: {
if (Op->getFlags().hasExact())
return isKnownNeverZero(Op.getOperand(0), Depth + 1);
- // Signed >> X is never zero. TODO: This can be expanded if we can bound X.
- // The expression is really
- // !Known.One[SignBit:SignBit-(BitWidth-MaxLog2(Known))].isZero()
- if (computeKnownBits(Op.getOperand(0), Depth + 1).isNegative())
+ KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
+ if (ValKnown.isNegative())
+ return true;
+ // If max shift cnt of known ones is non-zero, result is non-zero.
+ APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
+ if (MaxCnt.ule(ValKnown.getBitWidth()) &&
+ !ValKnown.One.lshr(MaxCnt).isZero())
return true;
break;
-
+ }
case ISD::UDIV:
case ISD::SDIV:
// div exact can only produce a zero if the dividend is zero.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156777.546512.patch
Type: text/x-patch
Size: 3450 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230802/f093a9ba/attachment.bin>
More information about the llvm-commits
mailing list