}
// (!C && A) || (C && B) --> sel C, B, A
+ // (A && !C) || (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))))
+ // (A && !C) || (B && C) --> sel C, B, A (may require freeze)
+ if (match(CondVal, m_c_LogicalAnd(m_Not(m_Value(C)), m_Value(A))) &&
+ match(FalseVal, m_c_LogicalAnd(m_Specific(C), m_Value(B)))) {
+ auto *SelCond = dyn_cast<SelectInst>(CondVal);
+ auto *SelFVal = dyn_cast<SelectInst>(FalseVal);
+ bool MayNeedFreeze = SelCond && SelFVal &&
+ match(SelCond->getTrueValue(),
+ m_Not(m_Specific(SelFVal->getTrueValue())));
+ if (MayNeedFreeze)
+ C = Builder.CreateFreeze(C);
return SelectInst::Create(C, B, A);
+ }
}
}
define i1 @bools_logical_commute1(i1 %a, i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute1(
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = select i1 [[A:%.*]], i1 [[NOT]], i1 false
-; CHECK-NEXT: [[AND2:%.*]] = select i1 [[C]], i1 [[B:%.*]], i1 false
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
define i1 @bools_logical_commute1_and1(i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute1_and1(
; CHECK-NEXT: [[A:%.*]] = call i1 @gen1()
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = and i1 [[A]], [[NOT]]
-; CHECK-NEXT: [[AND2:%.*]] = select i1 [[C]], i1 [[B:%.*]], i1 false
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A]]
; CHECK-NEXT: ret i1 [[OR]]
;
%a = call i1 @gen1()
define i1 @bools_logical_commute1_and2(i1 %a, i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute1_and2(
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = select i1 [[A:%.*]], i1 [[NOT]], i1 false
-; CHECK-NEXT: [[AND2:%.*]] = and i1 [[C]], [[B:%.*]]
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
define i1 @bools_logical_commute1_and1_and2(i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute1_and1_and2(
; CHECK-NEXT: [[A:%.*]] = call i1 @gen1()
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = and i1 [[A]], [[NOT]]
-; CHECK-NEXT: [[AND2:%.*]] = and i1 [[C]], [[B:%.*]]
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A]]
; CHECK-NEXT: ret i1 [[OR]]
;
%a = call i1 @gen1()
define i1 @bools_logical_commute3(i1 %a, i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute3(
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = select i1 [[A:%.*]], i1 [[NOT]], i1 false
-; CHECK-NEXT: [[AND2:%.*]] = select i1 [[B:%.*]], i1 [[C]], i1 false
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[C:%.*]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i1 [[B:%.*]], i1 [[A:%.*]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
define i1 @bools_logical_commute3_and1(i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute3_and1(
; CHECK-NEXT: [[A:%.*]] = call i1 @gen1()
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = and i1 [[A]], [[NOT]]
-; CHECK-NEXT: [[AND2:%.*]] = select i1 [[B:%.*]], i1 [[C]], i1 false
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A]]
; CHECK-NEXT: ret i1 [[OR]]
;
%a = call i1 @gen1()
define i1 @bools_logical_commute3_and2(i1 %a, i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute3_and2(
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = select i1 [[A:%.*]], i1 [[NOT]], i1 false
-; CHECK-NEXT: [[AND2:%.*]] = and i1 [[B:%.*]], [[C]]
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
define i1 @bools_logical_commute3_and1_and2(i1 %b, i1 %c) {
; CHECK-LABEL: @bools_logical_commute3_and1_and2(
; CHECK-NEXT: [[A:%.*]] = call i1 @gen1()
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = and i1 [[A]], [[NOT]]
-; CHECK-NEXT: [[AND2:%.*]] = and i1 [[B:%.*]], [[C]]
-; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
+; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A]]
; CHECK-NEXT: ret i1 [[OR]]
;
%a = call i1 @gen1()