Skip to content

Commit 00a1ec2

Browse files
[GlobalISel] Remove references to rhs of shufflevector if rhs is undef
1 parent 97262af commit 00a1ec2

File tree

6 files changed

+196
-263
lines changed

6 files changed

+196
-263
lines changed

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

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

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

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

1579+
def combine_shuffle_undef_rhs : GICombineRule<
1580+
(defs root:$root, build_fn_matchinfo:$matchinfo),
1581+
(match (G_IMPLICIT_DEF $undef),
1582+
(G_SHUFFLE_VECTOR $root, $src1, $undef, $mask):$root,
1583+
[{ return Helper.matchShuffleUndefRHS(*${root}, ${matchinfo}); }]),
1584+
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])
1585+
>;
1586+
15791587
// match_extract_of_element and insert_vector_elt_oob must be the first!
15801588
def vector_ops_combines: GICombineGroup<[
15811589
match_extract_of_element_undef_vector,
@@ -1948,7 +1956,7 @@ def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
19481956
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
19491957
combine_concat_vector, match_addos,
19501958
sext_trunc, zext_trunc, prefer_sign_combines, combine_shuffle_concat,
1951-
combine_use_vector_truncate, merge_combines]>;
1959+
combine_use_vector_truncate, merge_combines, combine_shuffle_undef_rhs]>;
19521960

19531961
// A combine group used to for prelegalizer combiners at -O0. The combines in
19541962
// this group have been selected based on experiments to balance code size and

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7721,3 +7721,32 @@ bool CombinerHelper::matchUnmergeValuesAnyExtBuildVector(const MachineInstr &MI,
77217721

77227722
return false;
77237723
}
7724+
7725+
bool CombinerHelper::matchShuffleUndefRHS(MachineInstr &MI,
7726+
BuildFnTy &MatchInfo) {
7727+
7728+
bool Changed = false;
7729+
ArrayRef<int> OrigMask = MI.getOperand(3).getShuffleMask();
7730+
SmallVector<int, 8> NewMask;
7731+
const LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
7732+
const unsigned NumSrcElems = SrcTy.isVector() ? SrcTy.getNumElements() : 1;
7733+
const unsigned NumDstElts = OrigMask.size();
7734+
for (unsigned i = 0; i != NumDstElts; ++i) {
7735+
int Idx = OrigMask[i];
7736+
if (Idx >= (int)NumSrcElems) {
7737+
Idx = -1;
7738+
Changed = true;
7739+
}
7740+
NewMask.push_back(Idx);
7741+
}
7742+
7743+
if (!Changed)
7744+
return false;
7745+
7746+
MatchInfo = [&, NewMask](MachineIRBuilder &B) {
7747+
B.buildShuffleVector(MI.getOperand(0), MI.getOperand(1), MI.getOperand(2),
7748+
NewMask);
7749+
};
7750+
7751+
return true;
7752+
}

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)