[PATCH] D136527: [InstCombine] use freeze to enable poison-safe logic->select fold
Sanjay Patel via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 22 06:24:08 PDT 2022
spatel created this revision.
spatel added reviewers: nikic, nlopes, aqjune.
Herald added subscribers: hiraditya, mcrosier.
Herald added a project: All.
spatel requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
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 #58313 <https://github.com/llvm/llvm-project/issues/58313> (although that particular example probably still needs another transform).
There's a sibling transform just below this one that can be updated as a follow-up if this looks right.
https://reviews.llvm.org/D136527
Files:
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/test/Transforms/InstCombine/select-safe-transforms.ll
Index: llvm/test/Transforms/InstCombine/select-safe-transforms.ll
===================================================================
--- llvm/test/Transforms/InstCombine/select-safe-transforms.ll
+++ llvm/test/Transforms/InstCombine/select-safe-transforms.ll
@@ -529,14 +529,12 @@
ret i1 %or
}
-; This is not safe to transform if 'c' could be poison.
+; Freeze 'c' to prevent poison from leaking.
define i1 @bools2_logical_commute3(i1 %a, i1 %b, i1 %c) {
; CHECK-LABEL: @bools2_logical_commute3(
-; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[C:%.*]], true
-; CHECK-NEXT: [[AND1:%.*]] = select i1 [[A:%.*]], i1 [[C]], i1 false
-; CHECK-NEXT: [[AND2:%.*]] = select i1 [[B:%.*]], i1 [[NOT]], 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 [[A:%.*]], i1 [[B:%.*]]
; CHECK-NEXT: ret i1 [[OR]]
;
%not = xor i1 %c, -1
@@ -546,6 +544,9 @@
ret i1 %or
}
+; No freeze needed when 'c' is guaranteed not be poison.
+; Intermediate logic folds may already reduce this.
+
define i1 @bools2_logical_commute3_nopoison(i1 %a, i1 %b, i1 noundef %c) {
; CHECK-LABEL: @bools2_logical_commute3_nopoison(
; CHECK-NEXT: [[OR:%.*]] = select i1 [[C:%.*]], i1 [[A:%.*]], i1 [[B:%.*]]
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2853,15 +2853,17 @@
// (C && A) || (!C && B) --> sel C, A, B
// (A && C) || (!C && B) --> sel C, A, B
// (C && A) || (B && !C) --> sel C, A, B
- // (A && C) || (B && !C) --> sel C, A, B (only with real 'and' ops)
+ // (A && C) || (B && !C) --> sel C, A, B (may require freeze)
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);
+ bool MayNeedFreeze = SelCond && SelFVal &&
+ match(SelFVal->getTrueValue(),
+ m_Not(m_Specific(SelCond->getTrueValue())));
+ if (MayNeedFreeze)
+ C = Builder.CreateFreeze(C);
+ return SelectInst::Create(C, A, B);
}
// (!C && A) || (C && B) --> sel C, B, A
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136527.469889.patch
Type: text/x-patch
Size: 2709 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221022/e7a9bbaa/attachment.bin>
More information about the llvm-commits
mailing list