Skip to content

Commit 73919a8

Browse files
committed
[InstCombine] try multi-use demanded bits folds for 'add'
This patch enables a multi-use demanded bits fold (motivated by issue llvm#57576): https://alive2.llvm.org/ce/z/DsZakh This mimics transforms that we already do on the single-use path. Originally, this patch did not include the last part to form a constant, but that can be removed independently to reduce risk. It's not clear what the effect of either change will be when viewed end-to-end. This is expected to be neutral or a slight win for compile-time. See the "add-demand2" series for experimental timing results: https://llvm-compile-time-tracker.com/?config=NewPM-O3&stat=instructions&remote=rotateright Differential Revision: https://reviews.llvm.org/D133788
1 parent 796af0c commit 73919a8

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,27 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
10641064

10651065
break;
10661066
}
1067+
case Instruction::Add: {
1068+
unsigned NLZ = DemandedMask.countLeadingZeros();
1069+
APInt DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ);
1070+
1071+
// If an operand adds zeros to every bit below the highest demanded bit,
1072+
// that operand doesn't change the result. Return the other side.
1073+
computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI);
1074+
if (DemandedFromOps.isSubsetOf(RHSKnown.Zero))
1075+
return I->getOperand(0);
1076+
1077+
computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
1078+
if (DemandedFromOps.isSubsetOf(LHSKnown.Zero))
1079+
return I->getOperand(1);
1080+
1081+
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1082+
Known = KnownBits::computeForAddSub(true, NSW, LHSKnown, RHSKnown);
1083+
if (DemandedMask.isSubsetOf(Known.Zero|Known.One))
1084+
return Constant::getIntegerValue(ITy, Known.One);
1085+
1086+
break;
1087+
}
10671088
case Instruction::AShr: {
10681089
// Compute the Known bits to simplify things downstream.
10691090
computeKnownBits(I, Known, Depth, CxtI);

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ define i5 @demand_low_bits_uses(i8 %x, i8 %y) {
21832183
; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], -32
21842184
; CHECK-NEXT: [[A:%.*]] = add i8 [[M]], [[Y:%.*]]
21852185
; CHECK-NEXT: call void @use(i8 [[A]])
2186-
; CHECK-NEXT: [[R:%.*]] = trunc i8 [[A]] to i5
2186+
; CHECK-NEXT: [[R:%.*]] = trunc i8 [[Y]] to i5
21872187
; CHECK-NEXT: ret i5 [[R]]
21882188
;
21892189
%m = mul i8 %x, -32 ; 0xE0
@@ -2193,6 +2193,8 @@ define i5 @demand_low_bits_uses(i8 %x, i8 %y) {
21932193
ret i5 %r
21942194
}
21952195

2196+
; negative test - demands one more bit
2197+
21962198
define i6 @demand_low_bits_uses_extra_bit(i8 %x, i8 %y) {
21972199
; CHECK-LABEL: @demand_low_bits_uses_extra_bit(
21982200
; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], -32
@@ -2214,7 +2216,7 @@ define i8 @demand_low_bits_uses_commute(i8 %x, i8 %p, i8 %z) {
22142216
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], -64
22152217
; CHECK-NEXT: [[A:%.*]] = add i8 [[Y]], [[M]]
22162218
; CHECK-NEXT: call void @use(i8 [[A]])
2217-
; CHECK-NEXT: [[S:%.*]] = sub i8 [[A]], [[Z:%.*]]
2219+
; CHECK-NEXT: [[S:%.*]] = sub i8 [[Y]], [[Z:%.*]]
22182220
; CHECK-NEXT: [[R:%.*]] = shl i8 [[S]], 2
22192221
; CHECK-NEXT: ret i8 [[R]]
22202222
;
@@ -2227,8 +2229,10 @@ define i8 @demand_low_bits_uses_commute(i8 %x, i8 %p, i8 %z) {
22272229
ret i8 %r
22282230
}
22292231

2230-
define i8 @demand_low_bits_uses_commutei_extra_bit(i8 %x, i8 %p, i8 %z) {
2231-
; CHECK-LABEL: @demand_low_bits_uses_commutei_extra_bit(
2232+
; negative test - demands one more bit
2233+
2234+
define i8 @demand_low_bits_uses_commute_extra_bit(i8 %x, i8 %p, i8 %z) {
2235+
; CHECK-LABEL: @demand_low_bits_uses_commute_extra_bit(
22322236
; CHECK-NEXT: [[Y:%.*]] = mul i8 [[P:%.*]], [[P]]
22332237
; CHECK-NEXT: [[M:%.*]] = and i8 [[X:%.*]], -64
22342238
; CHECK-NEXT: [[A:%.*]] = add i8 [[Y]], [[M]]
@@ -2257,7 +2261,7 @@ define { i64, i64 } @PR57576(i64 noundef %x, i64 noundef %y, i64 noundef %z, i64
22572261
; CHECK-NEXT: [[XY:%.*]] = or i128 [[SHY]], [[ZX]]
22582262
; CHECK-NEXT: [[SUB:%.*]] = sub i128 [[XY]], [[ZZ]]
22592263
; CHECK-NEXT: [[ADD:%.*]] = add i128 [[SUB]], [[MW]]
2260-
; CHECK-NEXT: [[T:%.*]] = trunc i128 [[ADD]] to i64
2264+
; CHECK-NEXT: [[T:%.*]] = trunc i128 [[SUB]] to i64
22612265
; CHECK-NEXT: [[H:%.*]] = lshr i128 [[ADD]], 64
22622266
; CHECK-NEXT: [[T2:%.*]] = trunc i128 [[H]] to i64
22632267
; CHECK-NEXT: [[R1:%.*]] = insertvalue { i64, i64 } poison, i64 [[T]], 0

llvm/test/Transforms/InstCombine/shift.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,11 +1750,12 @@ define void @ashr_out_of_range_1(i177* %A) {
17501750
define void @ossfuzz_38078(i32 %arg, i32 %arg1, i32* %ptr, i1* %ptr2, i32* %ptr3, i1* %ptr4, i32* %ptr5, i32* %ptr6, i1* %ptr7) {
17511751
; CHECK-LABEL: @ossfuzz_38078(
17521752
; CHECK-NEXT: bb:
1753+
; CHECK-NEXT: [[I2:%.*]] = add nsw i32 [[ARG:%.*]], [[ARG1:%.*]]
1754+
; CHECK-NEXT: [[B3:%.*]] = or i32 [[I2]], 2147483647
17531755
; CHECK-NEXT: [[G1:%.*]] = getelementptr i32, i32* [[PTR:%.*]], i64 -1
1754-
; CHECK-NEXT: [[I2:%.*]] = sub i32 0, [[ARG1:%.*]]
1755-
; CHECK-NEXT: [[I5:%.*]] = icmp eq i32 [[I2]], [[ARG:%.*]]
1756+
; CHECK-NEXT: [[I5:%.*]] = icmp eq i32 [[I2]], 0
17561757
; CHECK-NEXT: call void @llvm.assume(i1 [[I5]])
1757-
; CHECK-NEXT: store volatile i32 2147483647, i32* [[G1]], align 4
1758+
; CHECK-NEXT: store volatile i32 [[B3]], i32* [[G1]], align 4
17581759
; CHECK-NEXT: br label [[BB:%.*]]
17591760
; CHECK: BB:
17601761
; CHECK-NEXT: unreachable

0 commit comments

Comments
 (0)