Skip to content

Commit 1badf7c

Browse files
committed
[InstComine] Forego of one-use check in (X - (X & Y)) --> (X & ~Y) if Y is a constant
Summary: This is potentially more friendly for further optimizations, analysies, e.g.: https://godbolt.org/z/G24anE This resolves phase-ordering bug that was introduced in D75145 for https://godbolt.org/z/2gBwF2 https://godbolt.org/z/XvgSua Reviewers: spatel, nikic, dmgreen, xbolva00 Reviewed By: nikic, xbolva00 Subscribers: hiraditya, zzheng, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D75757
1 parent dcf6494 commit 1badf7c

File tree

6 files changed

+16
-15
lines changed

6 files changed

+16
-15
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,12 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
19171917
return NewSel;
19181918
}
19191919

1920+
// (X - (X & Y)) --> (X & ~Y)
1921+
if (match(Op1, m_c_And(m_Specific(Op0), m_Value(Y))) &&
1922+
(Op1->hasOneUse() || isa<Constant>(Y)))
1923+
return BinaryOperator::CreateAnd(
1924+
Op0, Builder.CreateNot(Y, Y->getName() + ".not"));
1925+
19201926
if (Op1->hasOneUse()) {
19211927
Value *Y = nullptr, *Z = nullptr;
19221928
Constant *C = nullptr;
@@ -1926,11 +1932,6 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
19261932
return BinaryOperator::CreateAdd(Op0,
19271933
Builder.CreateSub(Z, Y, Op1->getName()));
19281934

1929-
// (X - (X & Y)) --> (X & ~Y)
1930-
if (match(Op1, m_c_And(m_Value(Y), m_Specific(Op0))))
1931-
return BinaryOperator::CreateAnd(Op0,
1932-
Builder.CreateNot(Y, Y->getName() + ".not"));
1933-
19341935
// Subtracting -1/0 is the same as adding 1/0:
19351936
// sub [nsw] Op0, sext(bool Y) -> add [nsw] Op0, zext(bool Y)
19361937
// 'nuw' is dropped in favor of the canonical form.

llvm/test/Transforms/InstCombine/sub.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ define i8 @test75(i8 %x) {
15311531
; CHECK-LABEL: @test75(
15321532
; CHECK-NEXT: [[T0:%.*]] = and i8 [[X:%.*]], -8
15331533
; CHECK-NEXT: call void @use8(i8 [[T0]])
1534-
; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X]], [[T0]]
1534+
; CHECK-NEXT: [[T1:%.*]] = and i8 [[X]], 7
15351535
; CHECK-NEXT: ret i8 [[T1]]
15361536
;
15371537
%t0 = and i8 %x, -8

llvm/test/Transforms/LoopUnroll/runtime-loop-multiple-exits.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ define void @test1(i64 %trip, i1 %cond) {
1919
; EPILOG-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], 7
2020
; EPILOG-NEXT: br i1 [[TMP1]], label %exit2.loopexit.unr-lcssa, label [[ENTRY_NEW:%.*]]
2121
; EPILOG: entry.new:
22-
; EPILOG-NEXT: [[UNROLL_ITER:%.*]] = sub i64 [[TRIP]], [[XTRAITER]]
22+
; EPILOG-NEXT: [[UNROLL_ITER:%.*]] = and i64 [[TRIP]], -8
2323
; EPILOG-NEXT: br label [[LOOP_HEADER:%.*]]
2424
; EPILOG: loop_latch.epil:
2525
; EPILOG-NEXT: %epil.iter.sub = add i64 %epil.iter, -1
@@ -147,7 +147,7 @@ define void @test3(i64 %trip, i64 %add) {
147147
; EPILOG-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], 7
148148
; EPILOG-NEXT: br i1 [[TMP1]], label %exit2.loopexit.unr-lcssa, label [[ENTRY_NEW:%.*]]
149149
; EPILOG: entry.new:
150-
; EPILOG-NEXT: %unroll_iter = sub i64 [[TRIP]], [[XTRAITER]]
150+
; EPILOG-NEXT: %unroll_iter = and i64 [[TRIP]], -8
151151
; EPILOG-NEXT: br label [[LOOP_HEADER:%.*]]
152152
; EPILOG: loop_header:
153153
; EPILOG-NEXT: %sum = phi i64 [ 0, %entry.new ], [ %sum.next.7, %loop_latch.7 ]

llvm/test/Transforms/LoopUnroll/runtime-unroll-remainder.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ for.cond.cleanup:
2121
; CHECK: br i1 [[CMP]], label %[[CLEANUP:.*]], label %for.body.lr.ph.new
2222

2323
; CHECK-LABEL: for.body.lr.ph.new:
24-
; CHECK: %unroll_iter = sub nsw i64 %wide.trip.count, %xtraiter
24+
; CHECK: %unroll_iter = and i64 %wide.trip.count, 4294967292
2525
; CHECK: br label %for.body
2626

2727
; CHECK: [[CLEANUP]]:

llvm/test/Transforms/LoopVectorize/X86/float-induction-x86.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ define void @fp_iv_loop1(float* noalias nocapture %A, i32 %N) #0 {
3535
; AUTO_VEC-NEXT: [[TMP4:%.*]] = icmp ult i64 [[TMP1]], 96
3636
; AUTO_VEC-NEXT: br i1 [[TMP4]], label [[MIDDLE_BLOCK_UNR_LCSSA:%.*]], label [[VECTOR_PH_NEW:%.*]]
3737
; AUTO_VEC: vector.ph.new:
38-
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = sub nsw i64 [[TMP3]], [[XTRAITER]]
38+
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = and i64 [[TMP3]], 1152921504606846972
3939
; AUTO_VEC-NEXT: br label [[VECTOR_BODY:%.*]]
4040
; AUTO_VEC: vector.body:
4141
; AUTO_VEC-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH_NEW]] ], [ [[INDEX_NEXT_3:%.*]], [[VECTOR_BODY]] ]
@@ -203,7 +203,7 @@ define void @fp_iv_loop2(float* noalias nocapture %A, i32 %N) #1 {
203203
; AUTO_VEC-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], 7
204204
; AUTO_VEC-NEXT: br i1 [[TMP1]], label [[FOR_END_LOOPEXIT_UNR_LCSSA:%.*]], label [[FOR_BODY_PREHEADER_NEW:%.*]]
205205
; AUTO_VEC: for.body.preheader.new:
206-
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = sub nsw i64 [[ZEXT]], [[XTRAITER]]
206+
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = and i64 [[ZEXT]], 4294967288
207207
; AUTO_VEC-NEXT: br label [[FOR_BODY:%.*]]
208208
; AUTO_VEC: for.body:
209209
; AUTO_VEC-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_BODY_PREHEADER_NEW]] ], [ [[INDVARS_IV_NEXT_7:%.*]], [[FOR_BODY]] ]
@@ -306,7 +306,7 @@ define double @external_use_with_fast_math(double* %a, i64 %n) {
306306
; AUTO_VEC-NEXT: [[TMP5:%.*]] = icmp ult i64 [[TMP2]], 48
307307
; AUTO_VEC-NEXT: br i1 [[TMP5]], label [[MIDDLE_BLOCK_UNR_LCSSA:%.*]], label [[VECTOR_PH_NEW:%.*]]
308308
; AUTO_VEC: vector.ph.new:
309-
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = sub nsw i64 [[TMP4]], [[XTRAITER]]
309+
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = and i64 [[TMP4]], 2305843009213693948
310310
; AUTO_VEC-NEXT: br label [[VECTOR_BODY:%.*]]
311311
; AUTO_VEC: vector.body:
312312
; AUTO_VEC-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH_NEW]] ], [ [[INDEX_NEXT_3:%.*]], [[VECTOR_BODY]] ]
@@ -459,7 +459,7 @@ define double @external_use_without_fast_math(double* %a, i64 %n) {
459459
; AUTO_VEC-NEXT: [[TMP2:%.*]] = icmp ult i64 [[TMP1]], 7
460460
; AUTO_VEC-NEXT: br i1 [[TMP2]], label [[FOR_END_UNR_LCSSA:%.*]], label [[ENTRY_NEW:%.*]]
461461
; AUTO_VEC: entry.new:
462-
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = sub nsw i64 [[SMAX]], [[XTRAITER]]
462+
; AUTO_VEC-NEXT: [[UNROLL_ITER:%.*]] = and i64 [[SMAX]], 9223372036854775800
463463
; AUTO_VEC-NEXT: br label [[FOR_BODY:%.*]]
464464
; AUTO_VEC: for.body:
465465
; AUTO_VEC-NEXT: [[I:%.*]] = phi i64 [ 0, [[ENTRY_NEW]] ], [ [[I_NEXT_7:%.*]], [[FOR_BODY]] ]

llvm/test/Transforms/PhaseOrdering/reassociate-after-unroll.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ define dso_local i64 @func(i64 %blah, i64 %limit) #0 {
1919
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], 7
2020
; CHECK-NEXT: br i1 [[TMP1]], label [[FOR_COND_CLEANUP_LOOPEXIT_UNR_LCSSA:%.*]], label [[FOR_BODY_LR_PH_NEW:%.*]]
2121
; CHECK: for.body.lr.ph.new:
22-
; CHECK-NEXT: [[UNROLL_ITER:%.*]] = sub i64 [[LIMIT]], [[XTRAITER]]
22+
; CHECK-NEXT: [[UNROLL_ITER:%.*]] = and i64 [[LIMIT]], -8
2323
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
2424
; CHECK: for.cond.cleanup.loopexit.unr-lcssa:
2525
; CHECK-NEXT: [[ADD_LCSSA_PH:%.*]] = phi i64 [ undef, [[FOR_BODY_LR_PH]] ], [ [[ADD_7:%.*]], [[FOR_BODY]] ]
@@ -66,7 +66,7 @@ define dso_local i64 @func(i64 %blah, i64 %limit) #0 {
6666
; NPM-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], 7
6767
; NPM-NEXT: br i1 [[TMP1]], label [[FOR_COND_CLEANUP_LOOPEXIT_UNR_LCSSA:%.*]], label [[FOR_BODY_LR_PH_NEW:%.*]]
6868
; NPM: for.body.lr.ph.new:
69-
; NPM-NEXT: [[UNROLL_ITER:%.*]] = sub i64 [[LIMIT]], [[XTRAITER]]
69+
; NPM-NEXT: [[UNROLL_ITER:%.*]] = and i64 [[LIMIT]], -8
7070
; NPM-NEXT: [[AND_0:%.*]] = and i64 [[CONV]], 1
7171
; NPM-NEXT: br label [[FOR_BODY:%.*]]
7272
; NPM: for.cond.cleanup.loopexit.unr-lcssa:

0 commit comments

Comments
 (0)