[llvm] 64d3091 - [InstCombine] try multi-use demanded bits fold for 'sub'
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 21 11:14:55 PDT 2022
Author: Sanjay Patel
Date: 2022-09-21T14:13:05-04:00
New Revision: 64d309131a873c6705903b95ec7669ce7e99e439
URL: https://github.com/llvm/llvm-project/commit/64d309131a873c6705903b95ec7669ce7e99e439
DIFF: https://github.com/llvm/llvm-project/commit/64d309131a873c6705903b95ec7669ce7e99e439.diff
LOG: [InstCombine] try multi-use demanded bits fold for 'sub'
This is similar to D133788 / 73919a87e9a6, but for sub
the transform is valid only for low zeros in operand 1.
https://alive2.llvm.org/ce/z/EmRsXC
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
llvm/test/Transforms/InstCombine/sub.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 6cc8b2aff9811..d682f3bfb2803 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -1080,6 +1080,18 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
break;
}
+ case Instruction::Sub: {
+ unsigned NLZ = DemandedMask.countLeadingZeros();
+ APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
+
+ // If an operand subtracts zeros from every bit below the highest demanded
+ // bit, that operand doesn't change the result. Return the other side.
+ computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
+ if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
+ return I->getOperand(0);
+
+ break;
+ }
case Instruction::AShr: {
// Compute the Known bits to simplify things downstream.
computeKnownBits(I, Known, Depth, CxtI);
diff --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 0c016e8f3a050..cad15fd994c9d 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -2074,7 +2074,7 @@ define i5 @demand_low_bits_uses(i8 %x, i8 %y) {
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], 96
; CHECK-NEXT: [[A:%.*]] = sub i8 [[Y:%.*]], [[M]]
; CHECK-NEXT: call void @use8(i8 [[A]])
-; CHECK-NEXT: [[R:%.*]] = trunc i8 [[A]] to i5
+; CHECK-NEXT: [[R:%.*]] = trunc i8 [[Y]] to i5
; CHECK-NEXT: ret i5 [[R]]
;
%m = and i8 %x, 96 ; 0x60
@@ -2101,6 +2101,8 @@ define i6 @demand_low_bits_uses_extra_bit(i8 %x, i8 %y) {
ret i6 %r
}
+; negative test - must be operand 1
+
define i8 @demand_low_bits_uses_commute(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @demand_low_bits_uses_commute(
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], -64
More information about the llvm-commits
mailing list