Skip to content

Commit bf90ffb

Browse files
committed
[SVE][InstCombine] Delete redundante sel instructions with ptrue
svsel(pture, x, y) => x. depend on D121792 Reviewed By: paulwalker-arm, david-arm
1 parent 127cf4e commit bf90ffb

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,31 @@ instCombineConvertFromSVBool(InstCombiner &IC, IntrinsicInst &II) {
798798
return IC.replaceInstUsesWith(II, EarliestReplacement);
799799
}
800800

801+
static bool isAllActivePredicate(Value *Pred) {
802+
// Look through convert.from.svbool(convert.to.svbool(...) chain.
803+
Value *UncastedPred;
804+
if (match(Pred, m_Intrinsic<Intrinsic::aarch64_sve_convert_from_svbool>(
805+
m_Intrinsic<Intrinsic::aarch64_sve_convert_to_svbool>(
806+
m_Value(UncastedPred)))))
807+
// If the predicate has the same or less lanes than the uncasted
808+
// predicate then we know the casting has no effect.
809+
if (cast<ScalableVectorType>(Pred->getType())->getMinNumElements() <=
810+
cast<ScalableVectorType>(UncastedPred->getType())->getMinNumElements())
811+
Pred = UncastedPred;
812+
813+
return match(Pred, m_Intrinsic<Intrinsic::aarch64_sve_ptrue>(
814+
m_ConstantInt<AArch64SVEPredPattern::all>()));
815+
}
816+
801817
static std::optional<Instruction *> instCombineSVESel(InstCombiner &IC,
802818
IntrinsicInst &II) {
803-
auto Select = IC.Builder.CreateSelect(II.getOperand(0), II.getOperand(1),
804-
II.getOperand(2));
819+
// svsel(ptrue, x, y) => x
820+
auto *OpPredicate = II.getOperand(0);
821+
if (isAllActivePredicate(OpPredicate))
822+
return IC.replaceInstUsesWith(II, II.getOperand(1));
823+
824+
auto Select =
825+
IC.Builder.CreateSelect(OpPredicate, II.getOperand(1), II.getOperand(2));
805826
return IC.replaceInstUsesWith(II, Select);
806827
}
807828

@@ -1200,22 +1221,6 @@ instCombineSVEVectorFuseMulAddSub(InstCombiner &IC, IntrinsicInst &II,
12001221
return IC.replaceInstUsesWith(II, Res);
12011222
}
12021223

1203-
static bool isAllActivePredicate(Value *Pred) {
1204-
// Look through convert.from.svbool(convert.to.svbool(...) chain.
1205-
Value *UncastedPred;
1206-
if (match(Pred, m_Intrinsic<Intrinsic::aarch64_sve_convert_from_svbool>(
1207-
m_Intrinsic<Intrinsic::aarch64_sve_convert_to_svbool>(
1208-
m_Value(UncastedPred)))))
1209-
// If the predicate has the same or less lanes than the uncasted
1210-
// predicate then we know the casting has no effect.
1211-
if (cast<ScalableVectorType>(Pred->getType())->getMinNumElements() <=
1212-
cast<ScalableVectorType>(UncastedPred->getType())->getMinNumElements())
1213-
Pred = UncastedPred;
1214-
1215-
return match(Pred, m_Intrinsic<Intrinsic::aarch64_sve_ptrue>(
1216-
m_ConstantInt<AArch64SVEPredPattern::all>()));
1217-
}
1218-
12191224
static std::optional<Instruction *>
12201225
instCombineSVELD1(InstCombiner &IC, IntrinsicInst &II, const DataLayout &DL) {
12211226
Value *Pred = II.getOperand(0);

llvm/test/Transforms/InstCombine/AArch64/sve-intrinsic-sel.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ define <vscale x 4 x i32> @replace_sel_intrinsic(<vscale x 4 x i1> %p, <vscale x
1414

1515
define <vscale x 4 x i32> @sel_ptrue(<vscale x 4 x i32> %a, <vscale x 4 x i32> %b) {
1616
; CHECK-LABEL: @sel_ptrue(
17-
; CHECK-NEXT: [[PRED:%.*]] = call <vscale x 4 x i1> @llvm.aarch64.sve.ptrue.nxv4i1(i32 31)
18-
; CHECK-NEXT: [[RES:%.*]] = select <vscale x 4 x i1> [[PRED]], <vscale x 4 x i32> [[A:%.*]], <vscale x 4 x i32> [[B:%.*]]
19-
; CHECK-NEXT: ret <vscale x 4 x i32> [[RES]]
17+
; CHECK-NEXT: ret <vscale x 4 x i32> [[A:%.*]]
2018
;
2119
%pred = call <vscale x 4 x i1> @llvm.aarch64.sve.ptrue.nxv4i1(i32 31)
2220
%res = call <vscale x 4 x i32> @llvm.aarch64.sve.sel.nxv4i32(<vscale x 4 x i1> %pred, <vscale x 4 x i32> %a, <vscale x 4 x i32> %b)

0 commit comments

Comments
 (0)