Skip to content

Commit

Permalink
Implement legalization for some operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobly0 committed Nov 16, 2023
1 parent efc2b24 commit af8e15d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
20 changes: 13 additions & 7 deletions llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,17 @@ class LegalizeRuleSet {
changeTo(typeIdx(TypeIdx), Ty));
}

/// Conditionally limit the minimum size of the scalar.
LegalizeRuleSet &minScalarIf(LegalityPredicate Predicate, unsigned TypeIdx,
const LLT Ty) {
using namespace LegalityPredicates;
using namespace LegalizeMutations;
return actionIf(
LegalizeAction::WidenScalar,
all(scalarNarrowerThan(TypeIdx, Ty.getSizeInBits()), Predicate),
changeTo(typeIdx(TypeIdx), Ty));
}

/// Ensure the scalar is at most as wide as Ty.
LegalizeRuleSet &maxScalarOrElt(unsigned TypeIdx, const LLT Ty) {
using namespace LegalityPredicates;
Expand Down Expand Up @@ -977,13 +988,8 @@ class LegalizeRuleSet {
using namespace LegalizeMutations;
return actionIf(
LegalizeAction::NarrowScalar,
[=](const LegalityQuery &Query) {
const LLT QueryTy = Query.Types[TypeIdx];
return QueryTy.isScalar() &&
QueryTy.getSizeInBits() > Ty.getSizeInBits() &&
Predicate(Query);
},
changeElementTo(typeIdx(TypeIdx), Ty));
all(scalarWiderThan(TypeIdx, Ty.getSizeInBits()), Predicate),
changeTo(typeIdx(TypeIdx), Ty));
}

/// Limit the range of scalar sizes to MinTy and MaxTy.
Expand Down
30 changes: 22 additions & 8 deletions llvm/lib/Target/Z80/GISel/Z80LegalizerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ using namespace MIPatternMatch;
Z80LegalizerInfo::Z80LegalizerInfo(const Z80Subtarget &STI,
const Z80TargetMachine &TM)
: Subtarget(STI), TM(TM) {
using namespace LegalityPredicates;
using namespace LegalizeMutations;

bool Is24Bit = Subtarget.is24Bit();
LegalityPredicate pred24Bit = [=](const LegalityQuery &) { return Is24Bit; };

LegalityPredicate predZ180Ops = [this](const LegalityQuery &) { return Subtarget.hasZ180Ops(); };

std::array<LLT, 5> p;
for (int AddrSpace = 0; AddrSpace != p.size(); ++AddrSpace)
Expand All @@ -39,6 +45,7 @@ Z80LegalizerInfo::Z80LegalizerInfo(const Z80Subtarget &STI,
LLT s16 = LLT::scalar(16);
LLT s24 = LLT::scalar(24);
LLT s32 = LLT::scalar(32);
LLT s48 = LLT::scalar(48);
LLT s64 = LLT::scalar(64);
LLT sMax = Is24Bit ? s24 : s16;
LLT sOther = Is24Bit ? s16 : s24;
Expand Down Expand Up @@ -159,13 +166,15 @@ Z80LegalizerInfo::Z80LegalizerInfo(const Z80Subtarget &STI,
.legalForCartesianProduct(LegalScalars, {s1})
.clampScalar(0, s8, sMax);

{
auto &&Mul = getActionDefinitionsBuilder(G_MUL);
if (Subtarget.hasZ180Ops())
Mul.legalFor({s8});
Mul.libcallFor(LegalLibcallScalars)
.clampScalar(0, s8, s32);
}
getActionDefinitionsBuilder(G_MUL)
.legalIf(all(predZ180Ops, typeIs(0, s8)))
.libcallFor(LegalLibcallScalars)
.minScalar(0, s8)
.minScalar(0, s16)
.minScalarIf(pred24Bit, 0, s24)
.minScalar(0, s32)
.minScalar(0, s64)
.maxScalar(0, s64);

getActionDefinitionsBuilder({G_SDIV, G_UDIV, G_SREM, G_UREM})
.libcallFor(LegalLibcallScalars)
Expand All @@ -179,7 +188,12 @@ Z80LegalizerInfo::Z80LegalizerInfo(const Z80Subtarget &STI,
getActionDefinitionsBuilder({G_SHL, G_LSHR, G_ASHR})
.customForCartesianProduct(LegalLibcallScalars, {s8})
.clampScalar(1, s8, s8)
.clampScalar(0, s8, s64);
.minScalar(0, s8)
.minScalar(0, s16)
.minScalarIf(pred24Bit, 0, s24)
.minScalar(0, s32)
.minScalar(0, s64)
.maxScalar(0, s64);

getActionDefinitionsBuilder({G_FSHL, G_FSHR, G_ROTR, G_ROTL, G_UMULO,
G_UMULFIX, G_SMULFIX, G_SMULFIXSAT, G_UMULFIXSAT,
Expand Down

0 comments on commit af8e15d

Please sign in to comment.