[InstCombine] allow more matches for logical-ands --> select
This allows patterns with real 'and' instructions because those are safe to transform: https://alive2.llvm.org/ce/z/7-U_Ak
This commit is contained in:
@@ -2852,18 +2852,21 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
|
||||
|
||||
// (C && A) || (!C && B) --> sel C, A, B
|
||||
// (A && C) || (!C && B) --> sel C, A, B
|
||||
if (match(FalseVal, m_LogicalAnd(m_Not(m_Value(C)), m_Value(B))) &&
|
||||
match(CondVal, m_c_LogicalAnd(m_Specific(C), m_Value(A))))
|
||||
return SelectInst::Create(C, A, B);
|
||||
|
||||
// (C && A) || (B && !C) --> sel C, A, B
|
||||
// TODO: (A && C) || (B && !C) is safe to transform with real 'and' ops.
|
||||
if (match(FalseVal, m_LogicalAnd(m_Value(B), m_Not(m_Value(C)))) &&
|
||||
match(CondVal, m_LogicalAnd(m_Specific(C), m_Value(A))))
|
||||
return SelectInst::Create(C, A, B);
|
||||
// (A && C) || (B && !C) --> sel C, A, B (only with real 'and' ops)
|
||||
if (match(FalseVal, m_c_LogicalAnd(m_Not(m_Value(C)), m_Value(B))) &&
|
||||
match(CondVal, m_c_LogicalAnd(m_Specific(C), m_Value(A)))) {
|
||||
auto *SelCond = dyn_cast<SelectInst>(CondVal);
|
||||
auto *SelFVal = dyn_cast<SelectInst>(FalseVal);
|
||||
if (!SelCond || !SelFVal ||
|
||||
!match(SelFVal->getTrueValue(),
|
||||
m_Not(m_Specific(SelCond->getTrueValue()))))
|
||||
return SelectInst::Create(C, A, B);
|
||||
}
|
||||
|
||||
// (!C && A) || (C && B) --> sel C, B, A
|
||||
// (!C && A) || (B && C) --> sel C, B, A
|
||||
// TODO: Allow more commutes as with the previous fold.
|
||||
if (match(CondVal, m_LogicalAnd(m_Not(m_Value(C)), m_Value(A))) &&
|
||||
match(FalseVal, m_c_LogicalAnd(m_Specific(C), m_Value(B))))
|
||||
return SelectInst::Create(C, B, A);
|
||||
|
||||
@@ -529,6 +529,8 @@ define i1 @bools2_logical_commute2_and1_and2(i1 %a, i1 %c) {
|
||||
ret i1 %or
|
||||
}
|
||||
|
||||
; This is not safe to transform if 'c' could be poison.
|
||||
|
||||
define i1 @bools2_logical_commute3(i1 %a, i1 %b, i1 %c) {
|
||||
; CHECK-LABEL: @bools2_logical_commute3(
|
||||
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
|
||||
@@ -546,10 +548,7 @@ define i1 @bools2_logical_commute3(i1 %a, i1 %b, i1 %c) {
|
||||
|
||||
define i1 @bools2_logical_commute3_and1(i1 %a, i1 %b, i1 %c) {
|
||||
; CHECK-LABEL: @bools2_logical_commute3_and1(
|
||||
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
|
||||
; CHECK-NEXT: [[AND1:%.*]] = and i1 [[A:%.*]], [[C]]
|
||||
; CHECK-NEXT: [[AND2:%.*]] = select i1 [[B:%.*]], i1 [[NOT]], i1 false
|
||||
; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
|
||||
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]
|
||||
; CHECK-NEXT: ret i1 [[OR]]
|
||||
;
|
||||
%not = xor i1 %c, -1
|
||||
@@ -562,10 +561,7 @@ define i1 @bools2_logical_commute3_and1(i1 %a, i1 %b, i1 %c) {
|
||||
define i1 @bools2_logical_commute3_and2(i1 %a, i1 %c) {
|
||||
; CHECK-LABEL: @bools2_logical_commute3_and2(
|
||||
; CHECK-NEXT: [[B:%.*]] = call i1 @gen1()
|
||||
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
|
||||
; CHECK-NEXT: [[AND1:%.*]] = select i1 [[A:%.*]], i1 [[C]], i1 false
|
||||
; CHECK-NEXT: [[AND2:%.*]] = and i1 [[B]], [[NOT]]
|
||||
; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
|
||||
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B]]
|
||||
; CHECK-NEXT: ret i1 [[OR]]
|
||||
;
|
||||
%b = call i1 @gen1()
|
||||
@@ -579,10 +575,7 @@ define i1 @bools2_logical_commute3_and2(i1 %a, i1 %c) {
|
||||
define i1 @bools2_logical_commute3_and1_and2(i1 %a, i1 %c) {
|
||||
; CHECK-LABEL: @bools2_logical_commute3_and1_and2(
|
||||
; CHECK-NEXT: [[B:%.*]] = call i1 @gen1()
|
||||
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
|
||||
; CHECK-NEXT: [[AND1:%.*]] = and i1 [[A:%.*]], [[C]]
|
||||
; CHECK-NEXT: [[AND2:%.*]] = and i1 [[B]], [[NOT]]
|
||||
; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
|
||||
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B]]
|
||||
; CHECK-NEXT: ret i1 [[OR]]
|
||||
;
|
||||
%b = call i1 @gen1()
|
||||
|
||||
Reference in New Issue
Block a user