Skip to content

Commit 1786361

Browse files
committed
[InstCombine] Fold select -> and/or using impliesPoison
We can fold a ? b : false to a & b if is_poison(b) implies that is_poison(a), at which point we're able to reuse all the usual fold on ands. In particular, this covers the very common case of icmp X, C && icmp X, C'. The same applies to ors. This currently only has an effect if the -instcombine-unsafe-select-transform=0 option is set. Differential Revision: https://reviews.llvm.org/D94550
1 parent e433ca2 commit 1786361

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

‎llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,11 +2572,13 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
25722572

25732573
if (SelType->isIntOrIntVectorTy(1) &&
25742574
TrueVal->getType() == CondVal->getType()) {
2575-
if (EnableUnsafeSelectTransform && match(TrueVal, m_One())) {
2575+
if (match(TrueVal, m_One()) &&
2576+
(EnableUnsafeSelectTransform || impliesPoison(FalseVal, CondVal))) {
25762577
// Change: A = select B, true, C --> A = or B, C
25772578
return BinaryOperator::CreateOr(CondVal, FalseVal);
25782579
}
2579-
if (EnableUnsafeSelectTransform && match(FalseVal, m_Zero())) {
2580+
if (match(FalseVal, m_Zero()) &&
2581+
(EnableUnsafeSelectTransform || impliesPoison(TrueVal, CondVal))) {
25802582
// Change: A = select B, C, false --> A = and B, C
25812583
return BinaryOperator::CreateAnd(CondVal, TrueVal);
25822584
}

‎llvm/test/Transforms/InstCombine/select-and-or.ll‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ define i1 @logical_or_implies(i32 %x) {
9191
; CHECK-LABEL: @logical_or_implies(
9292
; CHECK-NEXT: [[C1:%.*]] = icmp eq i32 [[X:%.*]], 0
9393
; CHECK-NEXT: [[C2:%.*]] = icmp eq i32 [[X]], 42
94-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i1 true, i1 [[C2]]
94+
; CHECK-NEXT: [[RES:%.*]] = or i1 [[C1]], [[C2]]
9595
; CHECK-NEXT: ret i1 [[RES]]
9696
;
9797
%c1 = icmp eq i32 %x, 0
@@ -103,10 +103,7 @@ define i1 @logical_or_implies(i32 %x) {
103103
; Will fold after conversion to or.
104104
define i1 @logical_or_implies_folds(i32 %x) {
105105
; CHECK-LABEL: @logical_or_implies_folds(
106-
; CHECK-NEXT: [[C1:%.*]] = icmp slt i32 [[X:%.*]], 0
107-
; CHECK-NEXT: [[C2:%.*]] = icmp sgt i32 [[X]], -1
108-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i1 true, i1 [[C2]]
109-
; CHECK-NEXT: ret i1 [[RES]]
106+
; CHECK-NEXT: ret i1 true
110107
;
111108
%c1 = icmp slt i32 %x, 0
112109
%c2 = icmp sge i32 %x, 0
@@ -119,7 +116,7 @@ define i1 @logical_and_implies(i32 %x) {
119116
; CHECK-LABEL: @logical_and_implies(
120117
; CHECK-NEXT: [[C1:%.*]] = icmp ne i32 [[X:%.*]], 0
121118
; CHECK-NEXT: [[C2:%.*]] = icmp ne i32 [[X]], 42
122-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i1 [[C2]], i1 false
119+
; CHECK-NEXT: [[RES:%.*]] = and i1 [[C1]], [[C2]]
123120
; CHECK-NEXT: ret i1 [[RES]]
124121
;
125122
%c1 = icmp ne i32 %x, 0
@@ -132,9 +129,7 @@ define i1 @logical_and_implies(i32 %x) {
132129
define i1 @logical_and_implies_folds(i32 %x) {
133130
; CHECK-LABEL: @logical_and_implies_folds(
134131
; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[X:%.*]], 42
135-
; CHECK-NEXT: [[C2:%.*]] = icmp ne i32 [[X]], 0
136-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C1]], i1 [[C2]], i1 false
137-
; CHECK-NEXT: ret i1 [[RES]]
132+
; CHECK-NEXT: ret i1 [[C1]]
138133
;
139134
%c1 = icmp ugt i32 %x, 42
140135
%c2 = icmp ne i32 %x, 0
@@ -153,6 +148,7 @@ define i1 @logical_or_noundef_a(i1 noundef %a, i1 %b) {
153148
}
154149

155150
; Noundef on false value allows conversion to or.
151+
; TODO: impliesPoison doesn't handle this yet.
156152
define i1 @logical_or_noundef_b(i1 %a, i1 noundef %b) {
157153
; CHECK-LABEL: @logical_or_noundef_b(
158154
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]]
@@ -173,6 +169,7 @@ define i1 @logical_and_noundef_a(i1 noundef %a, i1 %b) {
173169
}
174170

175171
; Noundef on false value allows conversion to and.
172+
; TODO: impliesPoison doesn't handle this yet.
176173
define i1 @logical_and_noundef_b(i1 %a, i1 noundef %b) {
177174
; CHECK-LABEL: @logical_and_noundef_b(
178175
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false

0 commit comments

Comments
 (0)