Skip to content

Commit 9df7ca1

Browse files
authored
[GlobalISel] Legalize Saturated Truncate instructions and intrinsics (llvm#154340)
Adds legalization support for `G_TRUNC_SSAT_S`, `G_TRUNC_SSAT_S`, `G_TRUNC_USAT_U` instructions for GlobalISel.
1 parent 5f9630b commit 9df7ca1

File tree

4 files changed

+139
-103
lines changed

4 files changed

+139
-103
lines changed

llvm/include/llvm/Target/GlobalISel/SelectionDAGCompat.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def : GINodeEquiv<G_SEXT, sext>;
5151
def : GINodeEquiv<G_SEXT_INREG, sext_inreg>;
5252
def : GINodeEquiv<G_ZEXT, zext>;
5353
def : GINodeEquiv<G_TRUNC, trunc>;
54+
def : GINodeEquiv<G_TRUNC_SSAT_S, truncssat_s>;
55+
def : GINodeEquiv<G_TRUNC_SSAT_U, truncssat_u>;
56+
def : GINodeEquiv<G_TRUNC_USAT_U, truncusat_u>;
5457
def : GINodeEquiv<G_BITCAST, bitconvert>;
5558
// G_INTTOPTR - SelectionDAG has no equivalent.
5659
// G_PTRTOINT - SelectionDAG has no equivalent.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,9 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST)
797797
.clampMinNumElements(0, s16, 4)
798798
.alwaysLegal();
799799

800+
getActionDefinitionsBuilder({G_TRUNC_SSAT_S, G_TRUNC_SSAT_U, G_TRUNC_USAT_U})
801+
.legalFor({{v8s8, v8s16}, {v4s16, v4s32}, {v2s32, v2s64}});
802+
800803
getActionDefinitionsBuilder(G_SEXT_INREG)
801804
.legalFor({s32, s64})
802805
.legalFor(PackedVectorAllTypeList)
@@ -1644,6 +1647,11 @@ bool AArch64LegalizerInfo::legalizeIntrinsic(LegalizerHelper &Helper,
16441647
MachineIRBuilder &MIB = Helper.MIRBuilder;
16451648
MachineRegisterInfo &MRI = *MIB.getMRI();
16461649

1650+
auto LowerUnaryOp = [&MI, &MIB](unsigned Opcode) {
1651+
MIB.buildInstr(Opcode, {MI.getOperand(0)}, {MI.getOperand(2)});
1652+
MI.eraseFromParent();
1653+
return true;
1654+
};
16471655
auto LowerBinOp = [&MI, &MIB](unsigned Opcode) {
16481656
MIB.buildInstr(Opcode, {MI.getOperand(0)},
16491657
{MI.getOperand(2), MI.getOperand(3)});
@@ -1838,6 +1846,12 @@ bool AArch64LegalizerInfo::legalizeIntrinsic(LegalizerHelper &Helper,
18381846
return LowerTriOp(AArch64::G_UDOT);
18391847
case Intrinsic::aarch64_neon_sdot:
18401848
return LowerTriOp(AArch64::G_SDOT);
1849+
case Intrinsic::aarch64_neon_sqxtn:
1850+
return LowerUnaryOp(AArch64::G_TRUNC_SSAT_S);
1851+
case Intrinsic::aarch64_neon_sqxtun:
1852+
return LowerUnaryOp(AArch64::G_TRUNC_SSAT_U);
1853+
case Intrinsic::aarch64_neon_uqxtn:
1854+
return LowerUnaryOp(AArch64::G_TRUNC_USAT_U);
18411855

18421856
case Intrinsic::vector_reverse:
18431857
// TODO: Add support for vector_reverse

llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,16 @@
321321
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
322322
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
323323
# DEBUG-NEXT: G_TRUNC_SSAT_S (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
324-
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
325-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
324+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
325+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
326326
# DEBUG-NEXT: G_TRUNC_SSAT_U (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
327-
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
328-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
327+
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
328+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
329+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
329330
# DEBUG-NEXT: G_TRUNC_USAT_U (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
330-
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
331-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
331+
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
332+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
333+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
332334
# DEBUG-NEXT: G_CONSTANT (opcode {{[0-9]+}}): 1 type index, 0 imm indices
333335
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
334336
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK

0 commit comments

Comments
 (0)