Skip to content

Commit 8628e6d

Browse files
committed
[InstCombine] use freeze to enable poison-safe logic->select fold
Without a freeze, this transform can leak poison to the output: https://alive2.llvm.org/ce/z/GJuF9i This makes the transform as uniform as possible, and it can help reduce patterns like issue llvm#58313 (although that particular example probably still needs another transform). Differential Revision: https://reviews.llvm.org/D136527
1 parent db40f9b commit 8628e6d

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,15 +2853,17 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
28532853
// (C && A) || (!C && B) --> sel C, A, B
28542854
// (A && C) || (!C && B) --> sel C, A, B
28552855
// (C && A) || (B && !C) --> sel C, A, B
2856-
// (A && C) || (B && !C) --> sel C, A, B (only with real 'and' ops)
2856+
// (A && C) || (B && !C) --> sel C, A, B (may require freeze)
28572857
if (match(FalseVal, m_c_LogicalAnd(m_Not(m_Value(C)), m_Value(B))) &&
28582858
match(CondVal, m_c_LogicalAnd(m_Specific(C), m_Value(A)))) {
28592859
auto *SelCond = dyn_cast<SelectInst>(CondVal);
28602860
auto *SelFVal = dyn_cast<SelectInst>(FalseVal);
2861-
if (!SelCond || !SelFVal ||
2862-
!match(SelFVal->getTrueValue(),
2863-
m_Not(m_Specific(SelCond->getTrueValue()))))
2864-
return SelectInst::Create(C, A, B);
2861+
bool MayNeedFreeze = SelCond && SelFVal &&
2862+
match(SelFVal->getTrueValue(),
2863+
m_Not(m_Specific(SelCond->getTrueValue())));
2864+
if (MayNeedFreeze)
2865+
C = Builder.CreateFreeze(C);
2866+
return SelectInst::Create(C, A, B);
28652867
}
28662868

28672869
// (!C && A) || (C && B) --> sel C, B, A

llvm/test/Transforms/InstCombine/select-safe-transforms.ll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,12 @@ define i1 @bools2_logical_commute2_and1_and2(i1 %a, i1 %c) {
529529
ret i1 %or
530530
}
531531

532-
; This is not safe to transform if 'c' could be poison.
532+
; Freeze 'c' to prevent poison from leaking.
533533

534534
define i1 @bools2_logical_commute3(i1 %a, i1 %b, i1 %c) {
535535
; CHECK-LABEL: @bools2_logical_commute3(
536-
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
537-
; CHECK-NEXT: [[AND1:%.*]] = select i1 [[A:%.*]], i1 [[C]], i1 false
538-
; CHECK-NEXT: [[AND2:%.*]] = select i1 [[B:%.*]], i1 [[NOT]], i1 false
539-
; CHECK-NEXT: [[OR:%.*]] = select i1 [[AND1]], i1 true, i1 [[AND2]]
536+
; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[C:%.*]]
537+
; CHECK-NEXT: [[OR:%.*]] = select i1 [[TMP1]], i1 [[A:%.*]], i1 [[B:%.*]]
540538
; CHECK-NEXT: ret i1 [[OR]]
541539
;
542540
%not = xor i1 %c, -1
@@ -546,6 +544,9 @@ define i1 @bools2_logical_commute3(i1 %a, i1 %b, i1 %c) {
546544
ret i1 %or
547545
}
548546

547+
; No freeze needed when 'c' is guaranteed not be poison.
548+
; Intermediate logic folds may already reduce this.
549+
549550
define i1 @bools2_logical_commute3_nopoison(i1 %a, i1 %b, i1 noundef %c) {
550551
; CHECK-LABEL: @bools2_logical_commute3_nopoison(
551552
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]

0 commit comments

Comments
 (0)