Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3599,6 +3599,21 @@ Instruction *InstCombinerImpl::foldSelectOfBools(SelectInst &SI) {
m_Not(m_Specific(SelCond->getTrueValue())));
if (MayNeedFreeze)
C = Builder.CreateFreeze(C);
if (!ProfcheckDisableMetadataFixes) {
Value *C2 = nullptr, *A2 = nullptr, *B2 = nullptr;
if (match(CondVal, m_LogicalAnd(m_Specific(C), m_Value(A2))) &&
SelCond) {
return SelectInst::Create(C, A, B, "", nullptr, SelCond);
} else if (match(FalseVal,
m_LogicalAnd(m_Not(m_Value(C2)), m_Value(B2))) &&
SelFVal) {
SelectInst *NewSI = SelectInst::Create(C, A, B, "", nullptr, SelFVal);
NewSI->swapProfMetadata();
return NewSI;
} else {
return createSelectInstWithUnknownProfile(C, A, B);
}
}
return SelectInst::Create(C, A, B);
}

Expand All @@ -3615,6 +3630,20 @@ Instruction *InstCombinerImpl::foldSelectOfBools(SelectInst &SI) {
m_Not(m_Specific(SelFVal->getTrueValue())));
if (MayNeedFreeze)
C = Builder.CreateFreeze(C);
if (!ProfcheckDisableMetadataFixes) {
Value *C2 = nullptr, *A2 = nullptr, *B2 = nullptr;
if (match(CondVal, m_LogicalAnd(m_Not(m_Value(C2)), m_Value(A2))) &&
SelCond) {
SelectInst *NewSI = SelectInst::Create(C, B, A, "", nullptr, SelCond);
NewSI->swapProfMetadata();
return NewSI;
} else if (match(FalseVal, m_LogicalAnd(m_Specific(C), m_Value(B2))) &&
SelFVal) {
return SelectInst::Create(C, B, A, "", nullptr, SelFVal);
} else {
return createSelectInstWithUnknownProfile(C, B, A);
}
}
return SelectInst::Create(C, B, A);
}
}
Expand Down
51 changes: 27 additions & 24 deletions llvm/test/Transforms/InstCombine/select-safe-transforms.ll
Original file line number Diff line number Diff line change
Expand Up @@ -256,27 +256,27 @@ define <2 x i1> @not_logical_or2(i1 %b, <2 x i32> %a) {
ret <2 x i1> %and
}

define i1 @bools_logical_commute0(i1 %a, i1 %b, i1 %c) {
define i1 @bools_logical_commute0(i1 %a, i1 %b, i1 %c) !prof !0 {
; CHECK-LABEL: @bools_logical_commute0(
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]]
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]], !prof [[PROF2]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
%and1 = select i1 %not, i1 %a, i1 false
%and2 = select i1 %c, i1 %b, i1 false
%or = select i1 %and1, i1 true, i1 %and2
%and1 = select i1 %not, i1 %a, i1 false, !prof!1
%and2 = select i1 %c, i1 %b, i1 false, !prof !2
%or = select i1 %and1, i1 true, i1 %and2, !prof !3
ret i1 %or
}

define i1 @bools_logical_commute0_and1(i1 %a, i1 %b, i1 %c) {
define i1 @bools_logical_commute0_and1(i1 %a, i1 %b, i1 %c) !prof !0 {
; CHECK-LABEL: @bools_logical_commute0_and1(
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]]
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]], !prof [[PROF1]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
%and1 = and i1 %not, %a
%and2 = select i1 %c, i1 %b, i1 false
%or = select i1 %and1, i1 true, i1 %and2
%and2 = select i1 %c, i1 %b, i1 false, !prof !1
%or = select i1 %and1, i1 true, i1 %and2, !prof !2
ret i1 %or
}

Expand All @@ -292,15 +292,15 @@ define i1 @bools_logical_commute0_and2(i1 %a, i1 %b, i1 %c) {
ret i1 %or
}

define i1 @bools_logical_commute0_and1_and2(i1 %a, i1 %b, i1 %c) {
define i1 @bools_logical_commute0_and1_and2(i1 %a, i1 %b, i1 %c) !prof !0 {
; CHECK-LABEL: @bools_logical_commute0_and1_and2(
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]]
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[B:%.*]], i1 [[A:%.*]], !prof [[PROF3:![0-9]+]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
%and1 = and i1 %not, %a
%and2 = and i1 %c, %b
%or = select i1 %and1, i1 true, i1 %and2
%or = select i1 %and1, i1 true, i1 %and2, !prof !1
ret i1 %or
}

Expand Down Expand Up @@ -457,27 +457,27 @@ define i1 @bools_logical_commute3_and1_and2(i1 %b, i1 %c) {
ret i1 %or
}

define i1 @bools2_logical_commute0(i1 %a, i1 %b, i1 %c) {
define i1 @bools2_logical_commute0(i1 %a, i1 %b, i1 %c) !prof !0 {
; CHECK-LABEL: @bools2_logical_commute0(
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]], !prof [[PROF1]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
%and1 = select i1 %c, i1 %a, i1 false
%and2 = select i1 %not, i1 %b, i1 false
%or = select i1 %and1, i1 true, i1 %and2
%and1 = select i1 %c, i1 %a, i1 false, !prof !1
%and2 = select i1 %not, i1 %b, i1 false, !prof !2
%or = select i1 %and1, i1 true, i1 %and2, !prof !3
ret i1 %or
}

define i1 @bools2_logical_commute0_and1(i1 %a, i1 %b, i1 %c) {
define i1 @bools2_logical_commute0_and1(i1 %a, i1 %b, i1 %c) !prof !0 {
; CHECK-LABEL: @bools2_logical_commute0_and1(
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]], !prof [[PROF2]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
%and1 = and i1 %c, %a
%and2 = select i1 %not, i1 %b, i1 false
%or = select i1 %and1, i1 true, i1 %and2
%and2 = select i1 %not, i1 %b, i1 false, !prof !1
%or = select i1 %and1, i1 true, i1 %and2, !prof !2
ret i1 %or
}

Expand All @@ -493,15 +493,15 @@ define i1 @bools2_logical_commute0_and2(i1 %a, i1 %b, i1 %c) {
ret i1 %or
}

define i1 @bools2_logical_commute0_and1_and2(i1 %a, i1 %b, i1 %c) {
define i1 @bools2_logical_commute0_and1_and2(i1 %a, i1 %b, i1 %c) !prof !0 {
; CHECK-LABEL: @bools2_logical_commute0_and1_and2(
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]], !prof [[PROF3]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
%and1 = and i1 %c, %a
%and2 = and i1 %not, %b
%or = select i1 %and1, i1 true, i1 %and2
%or = select i1 %and1, i1 true, i1 %and2, !prof !1
ret i1 %or
}

Expand Down Expand Up @@ -799,8 +799,11 @@ define <2 x i1> @not_logical_and2(i1 %b, <2 x i32> %a) {

!0 = !{!"function_entry_count", i64 1000}
!1 = !{!"branch_weights", i32 2, i32 3}
!2 = !{!"branch_weights", i32 5, i32 7}
!3 = !{!"branch_weights", i32 11, i32 13}
;.
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i64 1000}
; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
; CHECK: [[PROF2]] = !{!"branch_weights", i32 3, i32 2}
; CHECK: [[PROF3]] = !{!"unknown", !"instcombine"}
;.
1 change: 0 additions & 1 deletion llvm/utils/profcheck-xfail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,6 @@ Transforms/InstCombine/select_frexp.ll
Transforms/InstCombine/select.ll
Transforms/InstCombine/select-min-max.ll
Transforms/InstCombine/select-of-symmetric-selects.ll
Transforms/InstCombine/select-safe-transforms.ll
Transforms/InstCombine/select-select.ll
Transforms/InstCombine/select-with-extreme-eq-cond.ll
Transforms/InstCombine/shift.ll
Expand Down