Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GlobalISel] Remove references to rhs of shufflevector if rhs is undef #115076

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,10 @@ class CombinerHelper {
/// register and different indices.
bool matchExtractVectorElementWithDifferentIndices(const MachineOperand &MO,
BuildFnTy &MatchInfo);

/// Remove references to rhs if it is undef
bool matchShuffleUndefRHS(MachineInstr &MI, BuildFnTy &MatchInfo);

/// Use a function which takes in a MachineIRBuilder to perform a combine.
/// By default, it erases the instruction def'd on \p MO from the function.
void applyBuildFnMO(const MachineOperand &MO, BuildFnTy &MatchInfo);
Expand Down
13 changes: 12 additions & 1 deletion llvm/include/llvm/Target/GlobalISel/Combine.td
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,14 @@ def expand_const_fpowi : GICombineRule<
[{ return Helper.matchFPowIExpansion(*${root}, ${imm}.getCImm()->getSExtValue()); }]),
(apply [{ Helper.applyExpandFPowI(*${root}, ${imm}.getCImm()->getSExtValue()); }])>;

def combine_shuffle_undef_rhs : GICombineRule<
(defs root:$root, build_fn_matchinfo:$matchinfo),
(match (G_IMPLICIT_DEF $undef),
(G_SHUFFLE_VECTOR $root, $src1, $undef, $mask):$root,
[{ return Helper.matchShuffleUndefRHS(*${root}, ${matchinfo}); }]),
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])
>;

// match_extract_of_element and insert_vector_elt_oob must be the first!
def vector_ops_combines: GICombineGroup<[
match_extract_of_element_undef_vector,
Expand Down Expand Up @@ -1917,6 +1925,9 @@ def constant_fold_binops : GICombineGroup<[constant_fold_binop,

def prefer_sign_combines : GICombineGroup<[nneg_zext]>;

def shuffle_combines : GICombineGroup<[combine_shuffle_concat,
combine_shuffle_undef_rhs]>;

def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
vector_ops_combines, freeze_combines, cast_combines,
insert_vec_elt_combines, extract_vec_elt_combines, combines_for_extload,
Expand All @@ -1938,7 +1949,7 @@ def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
sub_add_reg, select_to_minmax,
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
combine_concat_vector, match_addos,
sext_trunc, zext_trunc, prefer_sign_combines, combine_shuffle_concat,
sext_trunc, zext_trunc, prefer_sign_combines, shuffle_combines,
combine_use_vector_truncate, merge_combines]>;

// A combine group used to for prelegalizer combiners at -O0. The combines in
Expand Down
30 changes: 30 additions & 0 deletions llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7696,3 +7696,33 @@ bool CombinerHelper::matchUnmergeValuesAnyExtBuildVector(const MachineInstr &MI,

return false;
}

bool CombinerHelper::matchShuffleUndefRHS(MachineInstr &MI,
BuildFnTy &MatchInfo) {

bool Changed = false;
auto &Shuffle = cast<GShuffleVector>(MI);
ArrayRef<int> OrigMask = Shuffle.getMask();
SmallVector<int, 16> NewMask;
const LLT SrcTy = MRI.getType(Shuffle.getSrc1Reg());
const unsigned NumSrcElems = SrcTy.isVector() ? SrcTy.getNumElements() : 1;
const unsigned NumDstElts = OrigMask.size();
for (unsigned i = 0; i != NumDstElts; ++i) {
int Idx = OrigMask[i];
if (Idx >= (int)NumSrcElems) {
Idx = -1;
Changed = true;
}
NewMask.push_back(Idx);
}

if (!Changed)
return false;

MatchInfo = [&, NewMask](MachineIRBuilder &B) {
B.buildShuffleVector(MI.getOperand(0), MI.getOperand(1), MI.getOperand(2),
NewMask);
};

return true;
}
14 changes: 7 additions & 7 deletions llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,13 +772,13 @@ MachineInstrBuilder MachineIRBuilder::buildShuffleVector(const DstOp &Res,
LLT DstTy = Res.getLLTTy(*getMRI());
LLT Src1Ty = Src1.getLLTTy(*getMRI());
LLT Src2Ty = Src2.getLLTTy(*getMRI());
assert((size_t)(Src1Ty.getNumElements() + Src2Ty.getNumElements()) >=
Mask.size());
assert(DstTy.getElementType() == Src1Ty.getElementType() &&
DstTy.getElementType() == Src2Ty.getElementType());
(void)DstTy;
(void)Src1Ty;
(void)Src2Ty;
const LLT DstElemTy = DstTy.isVector() ? DstTy.getElementType() : DstTy;
const LLT ElemTy1 = Src1Ty.isVector() ? Src1Ty.getElementType() : Src1Ty;
const LLT ElemTy2 = Src2Ty.isVector() ? Src2Ty.getElementType() : Src2Ty;
assert(DstElemTy == ElemTy1 && DstElemTy == ElemTy2);
(void)DstElemTy;
(void)ElemTy1;
(void)ElemTy2;
ArrayRef<int> MaskAlloc = getMF().allocateShuffleMask(Mask);
return buildInstr(TargetOpcode::G_SHUFFLE_VECTOR, {Res}, {Src1, Src2})
.addShuffleMask(MaskAlloc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s

---
name: shuffle_vector_undef_rhs
tracksRegLiveness: true
body: |
bb.1:
liveins: $d0

; CHECK-LABEL: name: shuffle_vector_undef_rhs
; CHECK: liveins: $d0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s32>) = COPY $d0
; CHECK-NEXT: [[DEF:%[0-9]+]]:_(<2 x s32>) = G_IMPLICIT_DEF
; CHECK-NEXT: [[SHUF:%[0-9]+]]:_(<4 x s32>) = G_SHUFFLE_VECTOR [[COPY]](<2 x s32>), [[DEF]], shufflemask(0, undef, 1, undef)
; CHECK-NEXT: RET_ReallyLR implicit [[SHUF]](<4 x s32>)
%0:_(<2 x s32>) = COPY $d0
%1:_(<2 x s32>) = G_IMPLICIT_DEF
%2:_(<4 x s32>) = G_SHUFFLE_VECTOR %0(<2 x s32>), %1(<2 x s32>), shufflemask(0, 2, 1, 3)
RET_ReallyLR implicit %2
...

---
name: shuffle_vector_undef_rhs_scalar
tracksRegLiveness: true
body: |
bb.1:
liveins: $x0

; CHECK-LABEL: name: shuffle_vector_undef_rhs_scalar
; CHECK: liveins: $x0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
; CHECK-NEXT: [[DEF:%[0-9]+]]:_(s64) = G_IMPLICIT_DEF
; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x s64>) = G_BUILD_VECTOR [[COPY]](s64), [[DEF]](s64)
; CHECK-NEXT: RET_ReallyLR implicit [[BUILD_VECTOR]](<2 x s64>)
%0:_(s64) = COPY $x0
%1:_(s64) = G_IMPLICIT_DEF
%2:_(<2 x s64>) = G_SHUFFLE_VECTOR %0(s64), %1(s64), shufflemask(0, 1)
RET_ReallyLR implicit %2
...
Loading
Loading