Skip to content

Commit cbfe87c

Browse files
[GlobalISel] Remove references to rhs of shufflevector if rhs is undef (#115076)
1 parent 5348a30 commit cbfe87c

File tree

6 files changed

+200
-263
lines changed

6 files changed

+200
-263
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,10 @@ class CombinerHelper {
867867
/// register and different indices.
868868
bool matchExtractVectorElementWithDifferentIndices(const MachineOperand &MO,
869869
BuildFnTy &MatchInfo);
870+
871+
/// Remove references to rhs if it is undef
872+
bool matchShuffleUndefRHS(MachineInstr &MI, BuildFnTy &MatchInfo);
873+
870874
/// Use a function which takes in a MachineIRBuilder to perform a combine.
871875
/// By default, it erases the instruction def'd on \p MO from the function.
872876
void applyBuildFnMO(const MachineOperand &MO, BuildFnTy &MatchInfo);

llvm/include/llvm/Target/GlobalISel/Combine.td

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,14 @@ def expand_const_fpowi : GICombineRule<
15681568
[{ return Helper.matchFPowIExpansion(*${root}, ${imm}.getCImm()->getSExtValue()); }]),
15691569
(apply [{ Helper.applyExpandFPowI(*${root}, ${imm}.getCImm()->getSExtValue()); }])>;
15701570

1571+
def combine_shuffle_undef_rhs : GICombineRule<
1572+
(defs root:$root, build_fn_matchinfo:$matchinfo),
1573+
(match (G_IMPLICIT_DEF $undef),
1574+
(G_SHUFFLE_VECTOR $root, $src1, $undef, $mask):$root,
1575+
[{ return Helper.matchShuffleUndefRHS(*${root}, ${matchinfo}); }]),
1576+
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])
1577+
>;
1578+
15711579
// match_extract_of_element and insert_vector_elt_oob must be the first!
15721580
def vector_ops_combines: GICombineGroup<[
15731581
match_extract_of_element_undef_vector,
@@ -1917,6 +1925,9 @@ def constant_fold_binops : GICombineGroup<[constant_fold_binop,
19171925

19181926
def prefer_sign_combines : GICombineGroup<[nneg_zext]>;
19191927

1928+
def shuffle_combines : GICombineGroup<[combine_shuffle_concat,
1929+
combine_shuffle_undef_rhs]>;
1930+
19201931
def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
19211932
vector_ops_combines, freeze_combines, cast_combines,
19221933
insert_vec_elt_combines, extract_vec_elt_combines, combines_for_extload,
@@ -1938,7 +1949,7 @@ def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
19381949
sub_add_reg, select_to_minmax,
19391950
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
19401951
combine_concat_vector, match_addos,
1941-
sext_trunc, zext_trunc, prefer_sign_combines, combine_shuffle_concat,
1952+
sext_trunc, zext_trunc, prefer_sign_combines, shuffle_combines,
19421953
combine_use_vector_truncate, merge_combines]>;
19431954

19441955
// A combine group used to for prelegalizer combiners at -O0. The combines in

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7696,3 +7696,33 @@ bool CombinerHelper::matchUnmergeValuesAnyExtBuildVector(const MachineInstr &MI,
76967696

76977697
return false;
76987698
}
7699+
7700+
bool CombinerHelper::matchShuffleUndefRHS(MachineInstr &MI,
7701+
BuildFnTy &MatchInfo) {
7702+
7703+
bool Changed = false;
7704+
auto &Shuffle = cast<GShuffleVector>(MI);
7705+
ArrayRef<int> OrigMask = Shuffle.getMask();
7706+
SmallVector<int, 16> NewMask;
7707+
const LLT SrcTy = MRI.getType(Shuffle.getSrc1Reg());
7708+
const unsigned NumSrcElems = SrcTy.isVector() ? SrcTy.getNumElements() : 1;
7709+
const unsigned NumDstElts = OrigMask.size();
7710+
for (unsigned i = 0; i != NumDstElts; ++i) {
7711+
int Idx = OrigMask[i];
7712+
if (Idx >= (int)NumSrcElems) {
7713+
Idx = -1;
7714+
Changed = true;
7715+
}
7716+
NewMask.push_back(Idx);
7717+
}
7718+
7719+
if (!Changed)
7720+
return false;
7721+
7722+
MatchInfo = [&, NewMask](MachineIRBuilder &B) {
7723+
B.buildShuffleVector(MI.getOperand(0), MI.getOperand(1), MI.getOperand(2),
7724+
NewMask);
7725+
};
7726+
7727+
return true;
7728+
}

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -772,13 +772,13 @@ MachineInstrBuilder MachineIRBuilder::buildShuffleVector(const DstOp &Res,
772772
LLT DstTy = Res.getLLTTy(*getMRI());
773773
LLT Src1Ty = Src1.getLLTTy(*getMRI());
774774
LLT Src2Ty = Src2.getLLTTy(*getMRI());
775-
assert((size_t)(Src1Ty.getNumElements() + Src2Ty.getNumElements()) >=
776-
Mask.size());
777-
assert(DstTy.getElementType() == Src1Ty.getElementType() &&
778-
DstTy.getElementType() == Src2Ty.getElementType());
779-
(void)DstTy;
780-
(void)Src1Ty;
781-
(void)Src2Ty;
775+
const LLT DstElemTy = DstTy.isVector() ? DstTy.getElementType() : DstTy;
776+
const LLT ElemTy1 = Src1Ty.isVector() ? Src1Ty.getElementType() : Src1Ty;
777+
const LLT ElemTy2 = Src2Ty.isVector() ? Src2Ty.getElementType() : Src2Ty;
778+
assert(DstElemTy == ElemTy1 && DstElemTy == ElemTy2);
779+
(void)DstElemTy;
780+
(void)ElemTy1;
781+
(void)ElemTy2;
782782
ArrayRef<int> MaskAlloc = getMF().allocateShuffleMask(Mask);
783783
return buildInstr(TargetOpcode::G_SHUFFLE_VECTOR, {Res}, {Src1, Src2})
784784
.addShuffleMask(MaskAlloc);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s
3+
4+
---
5+
name: shuffle_vector_undef_rhs
6+
tracksRegLiveness: true
7+
body: |
8+
bb.1:
9+
liveins: $d0
10+
11+
; CHECK-LABEL: name: shuffle_vector_undef_rhs
12+
; CHECK: liveins: $d0
13+
; CHECK-NEXT: {{ $}}
14+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s32>) = COPY $d0
15+
; CHECK-NEXT: [[DEF:%[0-9]+]]:_(<2 x s32>) = G_IMPLICIT_DEF
16+
; CHECK-NEXT: [[SHUF:%[0-9]+]]:_(<4 x s32>) = G_SHUFFLE_VECTOR [[COPY]](<2 x s32>), [[DEF]], shufflemask(0, undef, 1, undef)
17+
; CHECK-NEXT: RET_ReallyLR implicit [[SHUF]](<4 x s32>)
18+
%0:_(<2 x s32>) = COPY $d0
19+
%1:_(<2 x s32>) = G_IMPLICIT_DEF
20+
%2:_(<4 x s32>) = G_SHUFFLE_VECTOR %0(<2 x s32>), %1(<2 x s32>), shufflemask(0, 2, 1, 3)
21+
RET_ReallyLR implicit %2
22+
...
23+
24+
---
25+
name: shuffle_vector_undef_rhs_scalar
26+
tracksRegLiveness: true
27+
body: |
28+
bb.1:
29+
liveins: $x0
30+
31+
; CHECK-LABEL: name: shuffle_vector_undef_rhs_scalar
32+
; CHECK: liveins: $x0
33+
; CHECK-NEXT: {{ $}}
34+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
35+
; CHECK-NEXT: [[DEF:%[0-9]+]]:_(s64) = G_IMPLICIT_DEF
36+
; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x s64>) = G_BUILD_VECTOR [[COPY]](s64), [[DEF]](s64)
37+
; CHECK-NEXT: RET_ReallyLR implicit [[BUILD_VECTOR]](<2 x s64>)
38+
%0:_(s64) = COPY $x0
39+
%1:_(s64) = G_IMPLICIT_DEF
40+
%2:_(<2 x s64>) = G_SHUFFLE_VECTOR %0(s64), %1(s64), shufflemask(0, 1)
41+
RET_ReallyLR implicit %2
42+
...

0 commit comments

Comments
 (0)