Skip to content

Commit 128c0da

Browse files
committed
[InstCombine] Support trunc to i1 in foldSelectICmpAnd
1 parent 3836559 commit 128c0da

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static Instruction *foldSelectBinOpIdentity(SelectInst &Sel,
119119
/// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC
120120
/// With some variations depending if FC is larger than TC, or the shift
121121
/// isn't needed, or the bit widths don't match.
122-
static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
122+
static Value *foldSelectICmpAnd(SelectInst &Sel, Value *CondVal,
123123
InstCombiner::BuilderTy &Builder) {
124124
const APInt *SelTC, *SelFC;
125125
if (!match(Sel.getTrueValue(), m_APInt(SelTC)) ||
@@ -128,33 +128,42 @@ static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
128128

129129
// If this is a vector select, we need a vector compare.
130130
Type *SelType = Sel.getType();
131-
if (SelType->isVectorTy() != Cmp->getType()->isVectorTy())
131+
if (SelType->isVectorTy() != CondVal->getType()->isVectorTy())
132132
return nullptr;
133133

134134
Value *V;
135135
APInt AndMask;
136136
bool CreateAnd = false;
137-
ICmpInst::Predicate Pred = Cmp->getPredicate();
138-
if (ICmpInst::isEquality(Pred)) {
139-
if (!match(Cmp->getOperand(1), m_Zero()))
140-
return nullptr;
137+
CmpPredicate Pred;
138+
Value *CmpLHS, *CmpRHS;
141139

142-
V = Cmp->getOperand(0);
143-
const APInt *AndRHS;
144-
if (!match(V, m_And(m_Value(), m_Power2(AndRHS))))
145-
return nullptr;
140+
if (match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
141+
if (ICmpInst::isEquality(Pred)) {
142+
if (!match(CmpRHS, m_Zero()))
143+
return nullptr;
146144

147-
AndMask = *AndRHS;
148-
} else if (auto Res = decomposeBitTestICmp(Cmp->getOperand(0),
149-
Cmp->getOperand(1), Pred)) {
150-
assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
151-
if (!Res->Mask.isPowerOf2())
152-
return nullptr;
145+
V = CmpLHS;
146+
const APInt *AndRHS;
147+
if (!match(V, m_And(m_Value(), m_Power2(AndRHS))))
148+
return nullptr;
149+
150+
AndMask = *AndRHS;
151+
} else {
152+
auto Res = decomposeBitTestICmp(CmpLHS, CmpRHS, Pred);
153+
if (!Res || !Res->Mask.isPowerOf2())
154+
return nullptr;
155+
assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
153156

154-
V = Res->X;
155-
AndMask = Res->Mask;
156-
Pred = Res->Pred;
157-
CreateAnd = true;
157+
V = Res->X;
158+
AndMask = Res->Mask;
159+
Pred = Res->Pred;
160+
CreateAnd = true;
161+
}
162+
} else if (auto *Trunc = dyn_cast<TruncInst>(CondVal)) {
163+
V = Trunc->getOperand(0);
164+
AndMask = APInt(V->getType()->getScalarSizeInBits(), 1);
165+
Pred = ICmpInst::ICMP_NE;
166+
CreateAnd = !Trunc->hasNoUnsignedWrap();
158167
} else {
159168
return nullptr;
160169
}
@@ -172,7 +181,7 @@ static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
172181
return nullptr;
173182
// If we have to create an 'and', then we must kill the cmp to not
174183
// increase the instruction count.
175-
if (CreateAnd && !Cmp->hasOneUse())
184+
if (CreateAnd && !CondVal->hasOneUse())
176185
return nullptr;
177186

178187
// (V & AndMaskC) == 0 ? TC : FC --> TC | (V & AndMaskC)
@@ -213,7 +222,7 @@ static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
213222
// a 'select' + 'icmp', then this transformation would result in more
214223
// instructions and potentially interfere with other folding.
215224
if (CreateAnd + ShouldNotVal + NeedShift + NeedZExtTrunc >
216-
1 + Cmp->hasOneUse())
225+
1 + CondVal->hasOneUse())
217226
return nullptr;
218227

219228
// Insert the 'and' instruction on the input to the truncate.
@@ -1955,9 +1964,6 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19551964
tryToReuseConstantFromSelectInComparison(SI, *ICI, *this))
19561965
return NewSel;
19571966

1958-
if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
1959-
return replaceInstUsesWith(SI, V);
1960-
19611967
// NOTE: if we wanted to, this is where to detect integer MIN/MAX
19621968
bool Changed = false;
19631969
Value *TrueVal = SI.getTrueValue();
@@ -3955,6 +3961,9 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
39553961
if (Instruction *Result = foldSelectInstWithICmp(SI, ICI))
39563962
return Result;
39573963

3964+
if (Value *V = foldSelectICmpAnd(SI, CondVal, Builder))
3965+
return replaceInstUsesWith(SI, V);
3966+
39583967
if (Value *V = foldSelectICmpAndBinOp(CondVal, TrueVal, FalseVal, Builder))
39593968
return replaceInstUsesWith(SI, V);
39603969

llvm/test/Transforms/InstCombine/select-icmp-and.ll

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,8 @@ define i8 @select_bittest_to_xor(i8 %x) {
809809

810810
define i8 @select_trunc_bittest_to_sub(i8 %x) {
811811
; CHECK-LABEL: @select_trunc_bittest_to_sub(
812-
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i8 [[X:%.*]] to i1
813-
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 3, i8 4
812+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 1
813+
; CHECK-NEXT: [[RET:%.*]] = sub nuw nsw i8 4, [[TMP1]]
814814
; CHECK-NEXT: ret i8 [[RET]]
815815
;
816816
%trunc = trunc i8 %x to i1
@@ -820,8 +820,7 @@ define i8 @select_trunc_bittest_to_sub(i8 %x) {
820820

821821
define i8 @select_trunc_nuw_bittest_to_sub(i8 %x) {
822822
; CHECK-LABEL: @select_trunc_nuw_bittest_to_sub(
823-
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
824-
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 3, i8 4
823+
; CHECK-NEXT: [[RET:%.*]] = sub i8 4, [[X:%.*]]
825824
; CHECK-NEXT: ret i8 [[RET]]
826825
;
827826
%trunc = trunc nuw i8 %x to i1
@@ -831,8 +830,8 @@ define i8 @select_trunc_nuw_bittest_to_sub(i8 %x) {
831830

832831
define i8 @select_trunc_nsw_bittest_to_sub(i8 %x) {
833832
; CHECK-LABEL: @select_trunc_nsw_bittest_to_sub(
834-
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nsw i8 [[X:%.*]] to i1
835-
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 3, i8 4
833+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 1
834+
; CHECK-NEXT: [[RET:%.*]] = sub nuw nsw i8 4, [[TMP1]]
836835
; CHECK-NEXT: ret i8 [[RET]]
837836
;
838837
%trunc = trunc nsw i8 %x to i1
@@ -844,7 +843,7 @@ define i8 @select_trunc_nuw_bittest_to_sub_extra_use(i8 %x) {
844843
; CHECK-LABEL: @select_trunc_nuw_bittest_to_sub_extra_use(
845844
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
846845
; CHECK-NEXT: call void @use1(i1 [[TRUNC]])
847-
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 3, i8 4
846+
; CHECK-NEXT: [[RET:%.*]] = sub i8 4, [[X]]
848847
; CHECK-NEXT: ret i8 [[RET]]
849848
;
850849
%trunc = trunc nuw i8 %x to i1
@@ -868,8 +867,8 @@ define i8 @neg_select_trunc_bittest_to_sub_extra_use(i8 %x) {
868867

869868
define i8 @select_trunc_nuw_bittest_to_shl_not(i8 %x) {
870869
; CHECK-LABEL: @select_trunc_nuw_bittest_to_shl_not(
871-
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
872-
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 0, i8 4
870+
; CHECK-NEXT: [[TMP1:%.*]] = shl i8 [[X:%.*]], 2
871+
; CHECK-NEXT: [[RET:%.*]] = xor i8 [[TMP1]], 4
873872
; CHECK-NEXT: ret i8 [[RET]]
874873
;
875874
%trunc = trunc nuw i8 %x to i1
@@ -879,8 +878,8 @@ define i8 @select_trunc_nuw_bittest_to_shl_not(i8 %x) {
879878

880879
define i8 @select_trunc_bittest_to_shl(i8 %x) {
881880
; CHECK-LABEL: @select_trunc_bittest_to_shl(
882-
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i8 [[X:%.*]] to i1
883-
; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 4, i8 0
881+
; CHECK-NEXT: [[TMP1:%.*]] = shl i8 [[X:%.*]], 2
882+
; CHECK-NEXT: [[RET:%.*]] = and i8 [[TMP1]], 4
884883
; CHECK-NEXT: ret i8 [[RET]]
885884
;
886885
%trunc = trunc i8 %x to i1
@@ -903,8 +902,9 @@ define i8 @neg_select_trunc_bittest_to_shl_extra_use(i8 %x) {
903902

904903
define i16 @select_trunc_nuw_bittest_or(i8 %x) {
905904
; CHECK-LABEL: @select_trunc_nuw_bittest_or(
906-
; CHECK-NEXT: [[TMP1:%.*]] = trunc nuw i8 [[X:%.*]] to i1
907-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[TMP1]], i16 20, i16 4
905+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[X:%.*]] to i16
906+
; CHECK-NEXT: [[SELECT:%.*]] = shl nuw nsw i16 [[TMP1]], 4
907+
; CHECK-NEXT: [[RES:%.*]] = or disjoint i16 [[SELECT]], 4
908908
; CHECK-NEXT: ret i16 [[RES]]
909909
;
910910
%trunc = trunc nuw i8 %x to i1

0 commit comments

Comments
 (0)