Skip to content

Commit f6f8e20

Browse files
authored
[AArch64][GlobalISel] Refactor Combine G_CONCAT_VECTOR (#80866)
The combine now works using tablegen and checks if new instruction is legal before creating it.
1 parent c6a7c4d commit f6f8e20

File tree

10 files changed

+506
-508
lines changed

10 files changed

+506
-508
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,18 @@ class CombinerHelper {
224224
/// - concat_vector(undef, undef) => undef
225225
/// - concat_vector(build_vector(A, B), build_vector(C, D)) =>
226226
/// build_vector(A, B, C, D)
227-
///
228-
/// \pre MI.getOpcode() == G_CONCAT_VECTORS.
229-
bool tryCombineConcatVectors(MachineInstr &MI);
227+
/// ==========================================================
230228
/// Check if the G_CONCAT_VECTORS \p MI is undef or if it
231229
/// can be flattened into a build_vector.
232-
/// In the first case \p IsUndef will be true.
233-
/// In the second case \p Ops will contain the operands needed
234-
/// to produce the flattened build_vector.
230+
/// In the first case \p Ops will be empty
231+
/// In the second case \p Ops will contain the operands
232+
/// needed to produce the flattened build_vector.
235233
///
236234
/// \pre MI.getOpcode() == G_CONCAT_VECTORS.
237-
bool matchCombineConcatVectors(MachineInstr &MI, bool &IsUndef,
238-
SmallVectorImpl<Register> &Ops);
239-
/// Replace \p MI with a flattened build_vector with \p Ops or an
240-
/// implicit_def if IsUndef is true.
241-
void applyCombineConcatVectors(MachineInstr &MI, bool IsUndef,
242-
const ArrayRef<Register> Ops);
235+
bool matchCombineConcatVectors(MachineInstr &MI, SmallVector<Register> &Ops);
236+
/// Replace \p MI with a flattened build_vector with \p Ops
237+
/// or an implicit_def if \p Ops is empty.
238+
void applyCombineConcatVectors(MachineInstr &MI, SmallVector<Register> &Ops);
243239

244240
/// Try to combine G_SHUFFLE_VECTOR into G_CONCAT_VECTORS.
245241
/// Returns true if MI changed.

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,14 @@ def match_ors : GICombineRule<
12531253
[{ return Helper.matchOr(*${root}, ${matchinfo}); }]),
12541254
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
12551255

1256+
// Combines concat operations
1257+
def concat_matchinfo : GIDefMatchData<"SmallVector<Register>">;
1258+
def combine_concat_vector : GICombineRule<
1259+
(defs root:$root, concat_matchinfo:$matchinfo),
1260+
(match (wip_match_opcode G_CONCAT_VECTORS):$root,
1261+
[{ return Helper.matchCombineConcatVectors(*${root}, ${matchinfo}); }]),
1262+
(apply [{ Helper.applyCombineConcatVectors(*${root}, ${matchinfo}); }])>;
1263+
12561264
// FIXME: These should use the custom predicate feature once it lands.
12571265
def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
12581266
undef_to_negative_one,
@@ -1326,11 +1334,12 @@ def all_combines : GICombineGroup<[trivial_combines, insert_vec_elt_combines,
13261334
intdiv_combines, mulh_combines, redundant_neg_operands,
13271335
and_or_disjoint_mask, fma_combines, fold_binop_into_select,
13281336
sub_add_reg, select_to_minmax, redundant_binop_in_equality,
1329-
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors]>;
1337+
fsub_to_fneg, commute_constant_to_rhs, match_ands, match_ors,
1338+
combine_concat_vector]>;
13301339

13311340
// A combine group used to for prelegalizer combiners at -O0. The combines in
13321341
// this group have been selected based on experiments to balance code size and
13331342
// compile time performance.
13341343
def optnone_combines : GICombineGroup<[trivial_combines,
13351344
ptr_add_immed_chain, combines_for_extload,
1336-
not_cmp_fold, opt_brcond_by_inverting_cond]>;
1345+
not_cmp_fold, opt_brcond_by_inverting_cond, combine_concat_vector]>;

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,11 @@ void CombinerHelper::applyCombineCopy(MachineInstr &MI) {
222222
replaceRegWith(MRI, DstReg, SrcReg);
223223
}
224224

225-
bool CombinerHelper::tryCombineConcatVectors(MachineInstr &MI) {
226-
bool IsUndef = false;
227-
SmallVector<Register, 4> Ops;
228-
if (matchCombineConcatVectors(MI, IsUndef, Ops)) {
229-
applyCombineConcatVectors(MI, IsUndef, Ops);
230-
return true;
231-
}
232-
return false;
233-
}
234-
235-
bool CombinerHelper::matchCombineConcatVectors(MachineInstr &MI, bool &IsUndef,
236-
SmallVectorImpl<Register> &Ops) {
225+
bool CombinerHelper::matchCombineConcatVectors(MachineInstr &MI,
226+
SmallVector<Register> &Ops) {
237227
assert(MI.getOpcode() == TargetOpcode::G_CONCAT_VECTORS &&
238228
"Invalid instruction");
239-
IsUndef = true;
229+
bool IsUndef = true;
240230
MachineInstr *Undef = nullptr;
241231

242232
// Walk over all the operands of concat vectors and check if they are
@@ -246,6 +236,8 @@ bool CombinerHelper::matchCombineConcatVectors(MachineInstr &MI, bool &IsUndef,
246236
Register Reg = MO.getReg();
247237
MachineInstr *Def = MRI.getVRegDef(Reg);
248238
assert(Def && "Operand not defined");
239+
if (!MRI.hasOneNonDBGUse(Reg))
240+
return false;
249241
switch (Def->getOpcode()) {
250242
case TargetOpcode::G_BUILD_VECTOR:
251243
IsUndef = false;
@@ -275,10 +267,21 @@ bool CombinerHelper::matchCombineConcatVectors(MachineInstr &MI, bool &IsUndef,
275267
return false;
276268
}
277269
}
270+
271+
// Check if the combine is illegal
272+
LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
273+
if (!isLegalOrBeforeLegalizer(
274+
{TargetOpcode::G_BUILD_VECTOR, {DstTy, MRI.getType(Ops[0])}})) {
275+
return false;
276+
}
277+
278+
if (IsUndef)
279+
Ops.clear();
280+
278281
return true;
279282
}
280-
void CombinerHelper::applyCombineConcatVectors(
281-
MachineInstr &MI, bool IsUndef, const ArrayRef<Register> Ops) {
283+
void CombinerHelper::applyCombineConcatVectors(MachineInstr &MI,
284+
SmallVector<Register> &Ops) {
282285
// We determined that the concat_vectors can be flatten.
283286
// Generate the flattened build_vector.
284287
Register DstReg = MI.getOperand(0).getReg();
@@ -289,9 +292,9 @@ void CombinerHelper::applyCombineConcatVectors(
289292
// checking that at all Ops are undef. Alternatively, we could have
290293
// generate a build_vector of undefs and rely on another combine to
291294
// clean that up. For now, given we already gather this information
292-
// in tryCombineConcatVectors, just save compile time and issue the
295+
// in matchCombineConcatVectors, just save compile time and issue the
293296
// right thing.
294-
if (IsUndef)
297+
if (Ops.empty())
295298
Builder.buildUndef(NewDstReg);
296299
else
297300
Builder.buildBuildVector(NewDstReg, Ops);

llvm/lib/Target/AArch64/AArch64Combine.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,6 @@ def AArch64PostLegalizerCombiner
288288
constant_fold_binops, identity_combines,
289289
ptr_add_immed_chain, overlapping_and,
290290
split_store_zero_128, undef_combines,
291-
select_to_minmax, or_to_bsp,
291+
select_to_minmax, or_to_bsp, combine_concat_vector,
292292
commute_constant_to_rhs]> {
293293
}

llvm/lib/Target/AArch64/GISel/AArch64O0PreLegalizerCombiner.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ bool AArch64O0PreLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
9191

9292
unsigned Opc = MI.getOpcode();
9393
switch (Opc) {
94-
case TargetOpcode::G_CONCAT_VECTORS:
95-
return Helper.tryCombineConcatVectors(MI);
9694
case TargetOpcode::G_SHUFFLE_VECTOR:
9795
return Helper.tryCombineShuffleVector(MI);
9896
case TargetOpcode::G_MEMCPY_INLINE:

llvm/lib/Target/AArch64/GISel/AArch64PreLegalizerCombiner.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,6 @@ bool AArch64PreLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
720720

721721
unsigned Opc = MI.getOpcode();
722722
switch (Opc) {
723-
case TargetOpcode::G_CONCAT_VECTORS:
724-
return Helper.tryCombineConcatVectors(MI);
725723
case TargetOpcode::G_SHUFFLE_VECTOR:
726724
return Helper.tryCombineShuffleVector(MI);
727725
case TargetOpcode::G_UADDO:

llvm/lib/Target/AMDGPU/AMDGPUPreLegalizerCombiner.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ bool AMDGPUPreLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
106106
return true;
107107

108108
switch (MI.getOpcode()) {
109-
case TargetOpcode::G_CONCAT_VECTORS:
110-
return Helper.tryCombineConcatVectors(MI);
111109
case TargetOpcode::G_SHUFFLE_VECTOR:
112110
return Helper.tryCombineShuffleVector(MI);
113111
}

0 commit comments

Comments
 (0)