Skip to content

Commit 7ec4f60

Browse files
authored
[InstCombine] Infer disjoint flag on Or instructions. (#72912)
The disjoint flag was recently added to IR in #72583 We already set it when we turn an add into an or. This patch sets it on Ors that weren't converted from an Add.
1 parent 0801216 commit 7ec4f60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+317
-307
lines changed

clang/test/Headers/__clang_hip_math.hip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,7 +2451,7 @@ extern "C" __device__ double test_modf(double x, double* y) {
24512451
// CHECK-NEXT: [[RETVAL_0_I_I:%.*]] = phi i64 [ 0, [[CLEANUP_I_I_I]] ], [ [[__R_0_I_I_I]], [[WHILE_COND_I_I_I]] ], [ 0, [[CLEANUP_I36_I_I]] ], [ [[__R_0_I32_I_I]], [[WHILE_COND_I30_I_I]] ], [ 0, [[CLEANUP_I20_I_I]] ], [ [[__R_0_I16_I_I]], [[WHILE_COND_I14_I_I]] ]
24522452
// CHECK-NEXT: [[CONV_I:%.*]] = trunc i64 [[RETVAL_0_I_I]] to i32
24532453
// CHECK-NEXT: [[BF_VALUE_I:%.*]] = and i32 [[CONV_I]], 4194303
2454-
// CHECK-NEXT: [[BF_SET9_I:%.*]] = or i32 [[BF_VALUE_I]], 2143289344
2454+
// CHECK-NEXT: [[BF_SET9_I:%.*]] = or disjoint i32 [[BF_VALUE_I]], 2143289344
24552455
// CHECK-NEXT: [[TMP10:%.*]] = bitcast i32 [[BF_SET9_I]] to float
24562456
// CHECK-NEXT: ret float [[TMP10]]
24572457
//
@@ -2549,7 +2549,7 @@ extern "C" __device__ float test_nanf(const char *tag) {
25492549
// CHECK: _ZL3nanPKc.exit:
25502550
// CHECK-NEXT: [[RETVAL_0_I_I:%.*]] = phi i64 [ 0, [[CLEANUP_I_I_I]] ], [ [[__R_0_I_I_I]], [[WHILE_COND_I_I_I]] ], [ 0, [[CLEANUP_I36_I_I]] ], [ [[__R_0_I32_I_I]], [[WHILE_COND_I30_I_I]] ], [ 0, [[CLEANUP_I20_I_I]] ], [ [[__R_0_I16_I_I]], [[WHILE_COND_I14_I_I]] ]
25512551
// CHECK-NEXT: [[BF_VALUE_I:%.*]] = and i64 [[RETVAL_0_I_I]], 2251799813685247
2552-
// CHECK-NEXT: [[BF_SET9_I:%.*]] = or i64 [[BF_VALUE_I]], 9221120237041090560
2552+
// CHECK-NEXT: [[BF_SET9_I:%.*]] = or disjoint i64 [[BF_VALUE_I]], 9221120237041090560
25532553
// CHECK-NEXT: [[TMP10:%.*]] = bitcast i64 [[BF_SET9_I]] to double
25542554
// CHECK-NEXT: ret double [[TMP10]]
25552555
//

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
264264
if (ShrinkDemandedConstant(I, 1, DemandedMask))
265265
return I;
266266

267+
// Infer disjoint flag if no common bits are set.
268+
if (!cast<PossiblyDisjointInst>(I)->isDisjoint()) {
269+
WithCache<const Value *> LHSCache(I->getOperand(0), LHSKnown),
270+
RHSCache(I->getOperand(1), RHSKnown);
271+
if (haveNoCommonBitsSet(LHSCache, RHSCache, SQ.getWithInstruction(I))) {
272+
cast<PossiblyDisjointInst>(I)->setIsDisjoint(true);
273+
return I;
274+
}
275+
}
276+
267277
break;
268278
}
269279
case Instruction::Xor: {

llvm/test/Transforms/InstCombine/2010-11-01-lshr-mask.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ define i8 @foo(i8 %arg, i8 %arg1) {
3333
; CHECK-NEXT: [[T4:%.*]] = and i8 [[ARG1]], 33
3434
; CHECK-NEXT: [[T5:%.*]] = sub nsw i8 40, [[T2]]
3535
; CHECK-NEXT: [[T6:%.*]] = and i8 [[T5]], 84
36-
; CHECK-NEXT: [[T7:%.*]] = or i8 [[T4]], [[T6]]
36+
; CHECK-NEXT: [[T7:%.*]] = or disjoint i8 [[T4]], [[T6]]
3737
; CHECK-NEXT: [[T8:%.*]] = xor i8 [[T]], [[T3]]
38-
; CHECK-NEXT: [[T9:%.*]] = or i8 [[T7]], [[T8]]
38+
; CHECK-NEXT: [[T9:%.*]] = or disjoint i8 [[T7]], [[T8]]
3939
; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 [[T8]], 2
4040
; CHECK-NEXT: [[T11:%.*]] = and i8 [[TMP1]], 32
4141
; CHECK-NEXT: [[T12:%.*]] = xor i8 [[T11]], [[T9]]

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ define i32 @test29(i32 %x, i32 %y) {
764764
; CHECK-NEXT: [[TMP_2:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
765765
; CHECK-NEXT: [[TMP_7:%.*]] = and i32 [[X]], 63
766766
; CHECK-NEXT: [[TMP_9:%.*]] = and i32 [[TMP_2]], -64
767-
; CHECK-NEXT: [[TMP_10:%.*]] = or i32 [[TMP_7]], [[TMP_9]]
767+
; CHECK-NEXT: [[TMP_10:%.*]] = or disjoint i32 [[TMP_7]], [[TMP_9]]
768768
; CHECK-NEXT: ret i32 [[TMP_10]]
769769
;
770770
%tmp.2 = sub i32 %x, %y
@@ -1499,7 +1499,7 @@ define i8 @add_like_or_n1(i8 %x) {
14991499
define i8 @add_like_or_t2_extrause(i8 %x) {
15001500
; CHECK-LABEL: @add_like_or_t2_extrause(
15011501
; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 4
1502-
; CHECK-NEXT: [[I1:%.*]] = or i8 [[I0]], 15
1502+
; CHECK-NEXT: [[I1:%.*]] = or disjoint i8 [[I0]], 15
15031503
; CHECK-NEXT: call void @use(i8 [[I1]])
15041504
; CHECK-NEXT: [[R:%.*]] = add i8 [[I0]], 57
15051505
; CHECK-NEXT: ret i8 [[R]]
@@ -2361,7 +2361,7 @@ define { i64, i64 } @PR57576(i64 noundef %x, i64 noundef %y, i64 noundef %z, i64
23612361
; CHECK-NEXT: [[ZY:%.*]] = zext i64 [[Y:%.*]] to i128
23622362
; CHECK-NEXT: [[ZZ:%.*]] = zext i64 [[Z:%.*]] to i128
23632363
; CHECK-NEXT: [[SHY:%.*]] = shl nuw i128 [[ZY]], 64
2364-
; CHECK-NEXT: [[XY:%.*]] = or i128 [[SHY]], [[ZX]]
2364+
; CHECK-NEXT: [[XY:%.*]] = or disjoint i128 [[SHY]], [[ZX]]
23652365
; CHECK-NEXT: [[SUB:%.*]] = sub i128 [[XY]], [[ZZ]]
23662366
; CHECK-NEXT: [[T:%.*]] = trunc i128 [[SUB]] to i64
23672367
; CHECK-NEXT: [[TMP1:%.*]] = lshr i128 [[SUB]], 64

llvm/test/Transforms/InstCombine/and-or-not.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ define i32 @or_to_nxor_multiuse(i32 %a, i32 %b) {
553553
; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
554554
; CHECK-NEXT: [[OR:%.*]] = or i32 [[A]], [[B]]
555555
; CHECK-NEXT: [[NOTOR:%.*]] = xor i32 [[OR]], -1
556-
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[AND]], [[NOTOR]]
556+
; CHECK-NEXT: [[OR2:%.*]] = or disjoint i32 [[AND]], [[NOTOR]]
557557
; CHECK-NEXT: [[MUL1:%.*]] = mul i32 [[AND]], [[NOTOR]]
558558
; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL1]], [[OR2]]
559559
; CHECK-NEXT: ret i32 [[MUL2]]

llvm/test/Transforms/InstCombine/and-or.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ define i8 @or_and2_or2(i8 %x) {
217217
; CHECK-NEXT: [[X2:%.*]] = and i8 [[O2]], 66
218218
; CHECK-NEXT: call void @use(i8 [[X2]])
219219
; CHECK-NEXT: [[BITFIELD:%.*]] = and i8 [[X]], -8
220-
; CHECK-NEXT: [[R:%.*]] = or i8 [[BITFIELD]], 3
220+
; CHECK-NEXT: [[R:%.*]] = or disjoint i8 [[BITFIELD]], 3
221221
; CHECK-NEXT: ret i8 [[R]]
222222
;
223223
%o1 = or i8 %x, 1
@@ -243,7 +243,7 @@ define <2 x i8> @or_and2_or2_splat(<2 x i8> %x) {
243243
; CHECK-NEXT: [[X2:%.*]] = and <2 x i8> [[O2]], <i8 66, i8 66>
244244
; CHECK-NEXT: call void @use_vec(<2 x i8> [[X2]])
245245
; CHECK-NEXT: [[BITFIELD:%.*]] = and <2 x i8> [[X]], <i8 -8, i8 -8>
246-
; CHECK-NEXT: [[R:%.*]] = or <2 x i8> [[BITFIELD]], <i8 3, i8 3>
246+
; CHECK-NEXT: [[R:%.*]] = or disjoint <2 x i8> [[BITFIELD]], <i8 3, i8 3>
247247
; CHECK-NEXT: ret <2 x i8> [[R]]
248248
;
249249
%o1 = or <2 x i8> %x, <i8 1, i8 1>
@@ -355,7 +355,7 @@ define i64 @or_or_and_complex(i64 %i) {
355355
; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[I]], 8
356356
; CHECK-NEXT: [[TMP3:%.*]] = and i64 [[TMP1]], 71777214294589695
357357
; CHECK-NEXT: [[TMP4:%.*]] = and i64 [[TMP2]], -71777214294589696
358-
; CHECK-NEXT: [[OR27:%.*]] = or i64 [[TMP3]], [[TMP4]]
358+
; CHECK-NEXT: [[OR27:%.*]] = or disjoint i64 [[TMP3]], [[TMP4]]
359359
; CHECK-NEXT: ret i64 [[OR27]]
360360
;
361361
%1 = lshr i64 %i, 8

llvm/test/Transforms/InstCombine/and.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,7 @@ define i8 @negate_lowbitmask_use2(i8 %x, i8 %y) {
24332433
define i64 @test_and_or_constexpr_infloop() {
24342434
; CHECK-LABEL: @test_and_or_constexpr_infloop(
24352435
; CHECK-NEXT: [[AND:%.*]] = and i64 ptrtoint (ptr @g to i64), -8
2436-
; CHECK-NEXT: [[OR:%.*]] = or i64 [[AND]], 1
2436+
; CHECK-NEXT: [[OR:%.*]] = or disjoint i64 [[AND]], 1
24372437
; CHECK-NEXT: ret i64 [[OR]]
24382438
;
24392439
%and = and i64 ptrtoint (ptr @g to i64), -8

llvm/test/Transforms/InstCombine/apint-shift.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ define i18 @test13(i18 %x) {
273273
define i35 @test14(i35 %A) {
274274
; CHECK-LABEL: @test14(
275275
; CHECK-NEXT: [[B:%.*]] = and i35 [[A:%.*]], -19760
276-
; CHECK-NEXT: [[C:%.*]] = or i35 [[B]], 19744
276+
; CHECK-NEXT: [[C:%.*]] = or disjoint i35 [[B]], 19744
277277
; CHECK-NEXT: ret i35 [[C]]
278278
;
279279
%B = lshr i35 %A, 4

llvm/test/Transforms/InstCombine/binop-and-shifts.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ define i8 @lshr_xor_or_good_mask(i8 %x, i8 %y) {
365365
; CHECK-LABEL: @lshr_xor_or_good_mask(
366366
; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[Y:%.*]], [[X:%.*]]
367367
; CHECK-NEXT: [[TMP2:%.*]] = lshr i8 [[TMP1]], 4
368-
; CHECK-NEXT: [[BW1:%.*]] = or i8 [[TMP2]], 48
368+
; CHECK-NEXT: [[BW1:%.*]] = or disjoint i8 [[TMP2]], 48
369369
; CHECK-NEXT: ret i8 [[BW1]]
370370
;
371371
%shift1 = lshr i8 %x, 4

llvm/test/Transforms/InstCombine/binop-of-displaced-shifts.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ define i8 @mismatched_shifts(i8 %x) {
271271
; CHECK-NEXT: [[SHIFT:%.*]] = shl i8 16, [[X]]
272272
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[X]], 1
273273
; CHECK-NEXT: [[SHIFT2:%.*]] = lshr i8 3, [[ADD]]
274-
; CHECK-NEXT: [[BINOP:%.*]] = or i8 [[SHIFT]], [[SHIFT2]]
274+
; CHECK-NEXT: [[BINOP:%.*]] = or disjoint i8 [[SHIFT]], [[SHIFT2]]
275275
; CHECK-NEXT: ret i8 [[BINOP]]
276276
;
277277
%shift = shl i8 16, %x

0 commit comments

Comments
 (0)