[PATCH] D116616: [InstSimplify] use knownbits to fold more udiv/urem
Sanjay Patel via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 4 12:02:15 PST 2022
spatel created this revision.
spatel added reviewers: nikic, lebedev.ri.
Herald added subscribers: foad, hiraditya, mcrosier.
spatel requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
We could use knownbits on both operands for even more folds (and there are already tests in place for that), but this is enough to recover the example from:
https://github.com/llvm/llvm-project/issues/51934
(the tests are derived from the code in that example)
I am assuming no noticeable compile-time impact from this because udiv/urem are rare opcodes, but I could check that if there is concern.
https://reviews.llvm.org/D116616
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/div.ll
llvm/test/Transforms/InstSimplify/rem.ll
Index: llvm/test/Transforms/InstSimplify/rem.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/rem.ll
+++ llvm/test/Transforms/InstSimplify/rem.ll
@@ -187,8 +187,7 @@
; CHECK-LABEL: @urem_dividend_known_smaller_than_constant_divisor2(
; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8
; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[T0]], 12
-; CHECK-NEXT: [[R:%.*]] = urem i8 [[XOR]], 14
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 [[XOR]]
;
%t0 = zext i1 %b to i8
%xor = xor i8 %t0, 12
@@ -196,6 +195,8 @@
ret i8 %r
}
+; negative test - dividend can equal 13
+
define i8 @not_urem_dividend_known_smaller_than_constant_divisor2(i1 %b) {
; CHECK-LABEL: @not_urem_dividend_known_smaller_than_constant_divisor2(
; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8
Index: llvm/test/Transforms/InstSimplify/div.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/div.ll
+++ llvm/test/Transforms/InstSimplify/div.ll
@@ -155,10 +155,7 @@
define i8 @udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) {
; CHECK-LABEL: @udiv_dividend_known_smaller_than_constant_divisor2(
-; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8
-; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[T0]], 12
-; CHECK-NEXT: [[R:%.*]] = udiv i8 [[XOR]], 14
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 0
;
%t0 = zext i1 %b to i8
%xor = xor i8 %t0, 12
@@ -166,6 +163,8 @@
ret i8 %r
}
+; negative test - dividend can equal 13
+
define i8 @not_udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) {
; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_constant_divisor2(
; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -1078,6 +1078,18 @@
}
// IsSigned == false.
+
+ // Is the unsigned dividend known to be less than a constant divisor?
+ const APInt *C;
+ if (match(Y, m_APInt(C)) && !C->isZero()) {
+ KnownBits KnownX =
+ computeKnownBits(X, Q.DL, MaxRecurse, Q.AC, Q.CxtI, Q.DT);
+ Optional<bool> IsULT = KnownBits::ult(KnownX, KnownBits::makeConstant(*C));
+ if (IsULT.hasValue() && IsULT.getValue())
+ return true;
+ }
+
+ // Try again for any divisor:
// Is the dividend unsigned less than the divisor?
return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116616.397364.patch
Type: text/x-patch
Size: 2580 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220104/02f6408c/attachment.bin>
More information about the llvm-commits
mailing list