Skip to content

Commit 90802e6

Browse files
authored
[InstCombine] Handle commuted cases of the fold ((B|C)&A)|B -> B|(A&C) (#76565)
Alive2: https://alive2.llvm.org/ce/z/Qdsqk6 The commit f1eda23 didn't handle other cases that commute operands.
1 parent b6daac0 commit 90802e6

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3513,9 +3513,13 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
35133513
return BinaryOperator::CreateOr(Op0, C);
35143514

35153515
// ((B | C) & A) | B -> B | (A & C)
3516-
if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
3516+
if (match(Op0, m_c_And(m_c_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
35173517
return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
35183518

3519+
// B | ((B | C) & A) -> B | (A & C)
3520+
if (match(Op1, m_c_And(m_c_Or(m_Specific(Op0), m_Value(C)), m_Value(A))))
3521+
return BinaryOperator::CreateOr(Op0, Builder.CreateAnd(A, C));
3522+
35193523
if (Instruction *DeMorgan = matchDeMorgansLaws(I, *this))
35203524
return DeMorgan;
35213525

llvm/test/Transforms/InstCombine/or.ll

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,52 @@ define i32 @test45(i32 %x, i32 %y, i32 %z) {
753753
ret i32 %or1
754754
}
755755

756+
define i32 @test45_commuted1(i32 %x, i32 %y, i32 %z) {
757+
; CHECK-LABEL: @test45_commuted1(
758+
; CHECK-NEXT: [[YY:%.*]] = mul i32 [[Y:%.*]], [[Y]]
759+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], [[Z:%.*]]
760+
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[YY]], [[TMP1]]
761+
; CHECK-NEXT: ret i32 [[OR1]]
762+
;
763+
%yy = mul i32 %y, %y ; thwart complexity-based ordering
764+
%or = or i32 %yy, %z
765+
%and = and i32 %or, %x
766+
%or1 = or i32 %yy, %and
767+
ret i32 %or1
768+
}
769+
770+
define i32 @test45_commuted2(i32 %x, i32 %y, i32 %z) {
771+
; CHECK-LABEL: @test45_commuted2(
772+
; CHECK-NEXT: [[YY:%.*]] = mul i32 [[Y:%.*]], [[Y]]
773+
; CHECK-NEXT: [[XX:%.*]] = mul i32 [[X:%.*]], [[X]]
774+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[XX]], [[Z:%.*]]
775+
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[YY]], [[TMP1]]
776+
; CHECK-NEXT: ret i32 [[OR1]]
777+
;
778+
%yy = mul i32 %y, %y ; thwart complexity-based ordering
779+
%xx = mul i32 %x, %x ; thwart complexity-based ordering
780+
%or = or i32 %yy, %z
781+
%and = and i32 %xx, %or
782+
%or1 = or i32 %and, %yy
783+
ret i32 %or1
784+
}
785+
786+
define i32 @test45_commuted3(i32 %x, i32 %y, i32 %z) {
787+
; CHECK-LABEL: @test45_commuted3(
788+
; CHECK-NEXT: [[YY:%.*]] = mul i32 [[Y:%.*]], [[Y]]
789+
; CHECK-NEXT: [[ZZ:%.*]] = mul i32 [[Z:%.*]], [[Z]]
790+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[ZZ]], [[X:%.*]]
791+
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[YY]], [[TMP1]]
792+
; CHECK-NEXT: ret i32 [[OR1]]
793+
;
794+
%yy = mul i32 %y, %y ; thwart complexity-based ordering
795+
%zz = mul i32 %z, %z ; thwart complexity-based ordering
796+
%or = or i32 %zz, %yy
797+
%and = and i32 %or, %x
798+
%or1 = or i32 %and, %yy
799+
ret i32 %or1
800+
}
801+
756802
define i1 @test46(i8 signext %c) {
757803
; CHECK-LABEL: @test46(
758804
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[C:%.*]], -33
@@ -1213,11 +1259,11 @@ define i32 @PR46712(i1 %x, i1 %y, i1 %b, i64 %z) {
12131259
; CHECK-NEXT: entry:
12141260
; CHECK-NEXT: br i1 [[B:%.*]], label [[TRUE:%.*]], label [[END:%.*]]
12151261
; CHECK: true:
1216-
; CHECK-NEXT: [[BOOL5:%.*]] = icmp eq i64 [[Z:%.*]], 0
1217-
; CHECK-NEXT: [[SEL:%.*]] = zext i1 [[BOOL5]] to i32
1262+
; CHECK-NEXT: [[BOOL5_NOT:%.*]] = icmp eq i64 [[Z:%.*]], 0
1263+
; CHECK-NEXT: [[TMP0:%.*]] = zext i1 [[BOOL5_NOT]] to i32
12181264
; CHECK-NEXT: br label [[END]]
12191265
; CHECK: end:
1220-
; CHECK-NEXT: [[T5:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SEL]], [[TRUE]] ]
1266+
; CHECK-NEXT: [[T5:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[TMP0]], [[TRUE]] ]
12211267
; CHECK-NEXT: ret i32 [[T5]]
12221268
;
12231269
entry:
@@ -1245,11 +1291,11 @@ define i32 @PR46712_logical(i1 %x, i1 %y, i1 %b, i64 %z) {
12451291
; CHECK-NEXT: entry:
12461292
; CHECK-NEXT: br i1 [[B:%.*]], label [[TRUE:%.*]], label [[END:%.*]]
12471293
; CHECK: true:
1248-
; CHECK-NEXT: [[BOOL5:%.*]] = icmp eq i64 [[Z:%.*]], 0
1249-
; CHECK-NEXT: [[SEL:%.*]] = zext i1 [[BOOL5]] to i32
1294+
; CHECK-NEXT: [[BOOL5_NOT:%.*]] = icmp eq i64 [[Z:%.*]], 0
1295+
; CHECK-NEXT: [[TMP0:%.*]] = zext i1 [[BOOL5_NOT]] to i32
12501296
; CHECK-NEXT: br label [[END]]
12511297
; CHECK: end:
1252-
; CHECK-NEXT: [[T5:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SEL]], [[TRUE]] ]
1298+
; CHECK-NEXT: [[T5:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[TMP0]], [[TRUE]] ]
12531299
; CHECK-NEXT: ret i32 [[T5]]
12541300
;
12551301
entry:

0 commit comments

Comments
 (0)