Skip to content

Commit 639cafd

Browse files
authored
[RISCV][GISel] Use boolean predicated legalization action methods to remove a custom lambda. (#115628)
1 parent 3cdd86b commit 639cafd

File tree

3 files changed

+83
-82
lines changed

3 files changed

+83
-82
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,13 @@ class LegalizeRuleSet {
11021102
return minScalar(TypeIdx, MinTy).maxScalar(TypeIdx, MaxTy);
11031103
}
11041104

1105+
LegalizeRuleSet &clampScalar(bool Pred, unsigned TypeIdx, const LLT MinTy,
1106+
const LLT MaxTy) {
1107+
if (!Pred)
1108+
return *this;
1109+
return clampScalar(TypeIdx, MinTy, MaxTy);
1110+
}
1111+
11051112
/// Limit the range of scalar sizes to MinTy and MaxTy.
11061113
LegalizeRuleSet &clampScalarOrElt(unsigned TypeIdx, const LLT MinTy,
11071114
const LLT MaxTy) {

llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ using namespace llvm;
3030
using namespace LegalityPredicates;
3131
using namespace LegalizeMutations;
3232

33-
// Is this type supported by scalar FP arithmetic operations given the current
34-
// subtarget.
35-
static LegalityPredicate typeIsScalarFPArith(unsigned TypeIdx,
36-
const RISCVSubtarget &ST) {
37-
return [=, &ST](const LegalityQuery &Query) {
38-
return Query.Types[TypeIdx].isScalar() &&
39-
((ST.hasStdExtZfh() && Query.Types[TypeIdx].getSizeInBits() == 16) ||
40-
(ST.hasStdExtF() && Query.Types[TypeIdx].getSizeInBits() == 32) ||
41-
(ST.hasStdExtD() && Query.Types[TypeIdx].getSizeInBits() == 64));
42-
};
43-
}
44-
4533
static LegalityPredicate
4634
typeIsLegalIntOrFPVec(unsigned TypeIdx,
4735
std::initializer_list<LLT> IntOrFPVecTys,
@@ -498,59 +486,65 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST)
498486

499487
getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FMA, G_FNEG,
500488
G_FABS, G_FSQRT, G_FMAXNUM, G_FMINNUM})
501-
.legalIf(typeIsScalarFPArith(0, ST));
489+
.legalFor(ST.hasStdExtF(), {s32})
490+
.legalFor(ST.hasStdExtD(), {s64})
491+
.legalFor(ST.hasStdExtZfh(), {s16});
502492

503493
getActionDefinitionsBuilder(G_FREM)
504494
.libcallFor({s32, s64})
505495
.minScalar(0, s32)
506496
.scalarize(0);
507497

508498
getActionDefinitionsBuilder(G_FCOPYSIGN)
509-
.legalIf(all(typeIsScalarFPArith(0, ST), typeIsScalarFPArith(1, ST)));
499+
.legalFor(ST.hasStdExtF(), {{s32, s32}})
500+
.legalFor(ST.hasStdExtD(), {{s64, s64}, {s32, s64}, {s64, s32}})
501+
.legalFor(ST.hasStdExtZfh(), {{s16, s16}, {s16, s32}, {s32, s16}})
502+
.legalFor(ST.hasStdExtZfh() && ST.hasStdExtD(), {{s16, s64}, {s64, s16}});
510503

511504
// FIXME: Use Zfhmin.
512-
getActionDefinitionsBuilder(G_FPTRUNC).legalIf(
513-
[=, &ST](const LegalityQuery &Query) -> bool {
514-
return (ST.hasStdExtD() && typeIs(0, s32)(Query) &&
515-
typeIs(1, s64)(Query)) ||
516-
(ST.hasStdExtZfh() && typeIs(0, s16)(Query) &&
517-
typeIs(1, s32)(Query)) ||
518-
(ST.hasStdExtZfh() && ST.hasStdExtD() && typeIs(0, s16)(Query) &&
519-
typeIs(1, s64)(Query));
520-
});
521-
getActionDefinitionsBuilder(G_FPEXT).legalIf(
522-
[=, &ST](const LegalityQuery &Query) -> bool {
523-
return (ST.hasStdExtD() && typeIs(0, s64)(Query) &&
524-
typeIs(1, s32)(Query)) ||
525-
(ST.hasStdExtZfh() && typeIs(0, s32)(Query) &&
526-
typeIs(1, s16)(Query)) ||
527-
(ST.hasStdExtZfh() && ST.hasStdExtD() && typeIs(0, s64)(Query) &&
528-
typeIs(1, s16)(Query));
529-
});
505+
getActionDefinitionsBuilder(G_FPTRUNC)
506+
.legalFor(ST.hasStdExtD(), {{s32, s64}})
507+
.legalFor(ST.hasStdExtZfh(), {{s16, s32}})
508+
.legalFor(ST.hasStdExtZfh() && ST.hasStdExtD(), {{s16, s64}});
509+
getActionDefinitionsBuilder(G_FPEXT)
510+
.legalFor(ST.hasStdExtD(), {{s64, s32}})
511+
.legalFor(ST.hasStdExtZfh(), {{s32, s16}})
512+
.legalFor(ST.hasStdExtZfh() && ST.hasStdExtD(), {{s64, s16}});
530513

531514
getActionDefinitionsBuilder(G_FCMP)
532-
.legalIf(all(typeIs(0, sXLen), typeIsScalarFPArith(1, ST)))
533-
.clampScalar(0, sXLen, sXLen);
515+
.legalFor(ST.hasStdExtF(), {{sXLen, s32}})
516+
.legalFor(ST.hasStdExtD(), {{sXLen, s64}})
517+
.legalFor(ST.hasStdExtZfh(), {{sXLen, s16}})
518+
.clampScalar(ST.hasStdExtF(), 0, sXLen, sXLen);
534519

535520
// TODO: Support vector version of G_IS_FPCLASS.
536521
getActionDefinitionsBuilder(G_IS_FPCLASS)
537-
.customIf(all(typeIs(0, s1), typeIsScalarFPArith(1, ST)));
522+
.customFor(ST.hasStdExtF(), {{s1, s32}})
523+
.customFor(ST.hasStdExtD(), {{s1, s64}})
524+
.customFor(ST.hasStdExtZfh(), {{s1, s16}});
538525

539526
getActionDefinitionsBuilder(G_FCONSTANT)
540-
.legalIf(typeIsScalarFPArith(0, ST))
527+
.legalFor(ST.hasStdExtF(), {s32})
528+
.legalFor(ST.hasStdExtD(), {s64})
529+
.legalFor(ST.hasStdExtZfh(), {s16})
541530
.lowerFor({s32, s64});
542531

543-
auto &FPToIActions = getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI});
544-
FPToIActions.legalIf(all(typeInSet(0, {sXLen}), typeIsScalarFPArith(1, ST)));
545-
if (ST.is64Bit())
546-
FPToIActions.customIf(all(typeInSet(0, {s32}), typeIsScalarFPArith(1, ST)));
547-
FPToIActions.widenScalarToNextPow2(0)
532+
getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
533+
.legalFor(ST.hasStdExtF(), {{sXLen, s32}})
534+
.legalFor(ST.hasStdExtD(), {{sXLen, s64}})
535+
.legalFor(ST.hasStdExtZfh(), {{sXLen, s16}})
536+
.customFor(ST.is64Bit() && ST.hasStdExtF(), {{s32, s32}})
537+
.customFor(ST.is64Bit() && ST.hasStdExtD(), {{s32, s64}})
538+
.customFor(ST.is64Bit() && ST.hasStdExtZfh(), {{s32, s16}})
539+
.widenScalarToNextPow2(0)
548540
.minScalar(0, s32)
549541
.libcallFor({{s32, s32}, {s64, s32}, {s32, s64}, {s64, s64}})
550542
.libcallFor(ST.is64Bit(), {{s128, s32}, {s128, s64}});
551543

552544
getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
553-
.legalIf(all(typeIsScalarFPArith(0, ST), typeInSet(1, {sXLen})))
545+
.legalFor(ST.hasStdExtF(), {{s32, sXLen}})
546+
.legalFor(ST.hasStdExtD(), {{s64, sXLen}})
547+
.legalFor(ST.hasStdExtZfh(), {{s16, sXLen}})
554548
.widenScalarToNextPow2(1)
555549
.minScalar(1, sXLen)
556550
.libcallFor({{s32, s32}, {s64, s32}, {s32, s64}, {s64, s64}})

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

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@
307307
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
308308
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
309309
# DEBUG-NEXT: G_FCONSTANT (opcode {{[0-9]+}}): 1 type index, 0 imm indices
310-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
311-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
310+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
311+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
312312
# DEBUG-NEXT: G_VASTART (opcode {{[0-9]+}}): 1 type index, 0 imm indices
313313
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
314314
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
@@ -354,8 +354,8 @@
354354
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
355355
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
356356
# DEBUG-NEXT: G_FCMP (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
357-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
358-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
357+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
358+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
359359
# DEBUG-NEXT: G_SCMP (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
360360
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
361361
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
@@ -453,27 +453,27 @@
453453
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
454454
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
455455
# DEBUG-NEXT: G_FADD (opcode {{[0-9]+}}): 1 type index, 0 imm indices
456-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
457-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
456+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
457+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
458458
# DEBUG-NEXT: G_FSUB (opcode {{[0-9]+}}): 1 type index, 0 imm indices
459459
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
460-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
461-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
460+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
461+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
462462
# DEBUG-NEXT: G_FMUL (opcode {{[0-9]+}}): 1 type index, 0 imm indices
463463
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
464-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
465-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
464+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
465+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
466466
# DEBUG-NEXT: G_FMA (opcode {{[0-9]+}}): 1 type index, 0 imm indices
467467
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
468-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
469-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
468+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
469+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
470470
# DEBUG-NEXT: G_FMAD (opcode {{[0-9]+}}): 1 type index, 0 imm indices
471471
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
472472
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
473473
# DEBUG-NEXT: G_FDIV (opcode {{[0-9]+}}): 1 type index, 0 imm indices
474474
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
475-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
476-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
475+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
476+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
477477
# DEBUG-NEXT: G_FREM (opcode {{[0-9]+}}): 1 type index, 0 imm indices
478478
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
479479
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
@@ -509,28 +509,28 @@
509509
# DEBUG-NEXT:.. imm index coverage check SKIPPED: no rules defined
510510
# DEBUG-NEXT: G_FNEG (opcode {{[0-9]+}}): 1 type index, 0 imm indices
511511
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
512-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
513-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
512+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
513+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
514514
# DEBUG-NEXT: G_FPEXT (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
515-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
516-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
515+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
516+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
517517
# DEBUG-NEXT: G_FPTRUNC (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
518-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
519-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
518+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
519+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
520520
# DEBUG-NEXT: G_FPTOSI (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
521-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
522-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
521+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
522+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
523523
# DEBUG-NEXT: G_FPTOUI (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
524524
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
525-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
526-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
525+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
526+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
527527
# DEBUG-NEXT: G_SITOFP (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
528-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
529-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
528+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
529+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
530530
# DEBUG-NEXT: G_UITOFP (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
531531
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
532-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
533-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
532+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
533+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
534534
# DEBUG-NEXT: G_FPTOSI_SAT (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
535535
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
536536
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
@@ -539,25 +539,25 @@
539539
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
540540
# DEBUG-NEXT: G_FABS (opcode {{[0-9]+}}): 1 type index, 0 imm indices
541541
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
542-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
543-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
542+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
543+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
544544
# DEBUG-NEXT: G_FCOPYSIGN (opcode {{[0-9]+}}): 2 type indices
545-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
546-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
545+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
546+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
547547
# DEBUG-NEXT: G_IS_FPCLASS (opcode {{[0-9]+}}): 2 type indices, 0 imm indices
548-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
549-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
548+
# DEBUG-NEXT: .. the first uncovered type index: 2, OK
549+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
550550
# DEBUG-NEXT: G_FCANONICALIZE (opcode {{[0-9]+}}): 1 type index, 0 imm indices
551551
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
552552
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
553553
# DEBUG-NEXT: G_FMINNUM (opcode {{[0-9]+}}): 1 type index
554554
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
555-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
556-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
555+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
556+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
557557
# DEBUG-NEXT: G_FMAXNUM (opcode {{[0-9]+}}): 1 type index
558558
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
559-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
560-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
559+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
560+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
561561
# DEBUG-NEXT: G_FMINNUM_IEEE (opcode {{[0-9]+}}): 1 type index
562562
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
563563
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
@@ -710,8 +710,8 @@
710710
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
711711
# DEBUG-NEXT: G_FSQRT (opcode {{[0-9]+}}): 1 type index, 0 imm indices
712712
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
713-
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
714-
# DEBUG-NEXT: .. imm index coverage check SKIPPED: user-defined predicate detected
713+
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
714+
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
715715
# DEBUG-NEXT: G_FFLOOR (opcode {{[0-9]+}}): 1 type index, 0 imm indices
716716
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
717717
# DEBUG-NEXT: .. the first uncovered type index: 1, OK

0 commit comments

Comments
 (0)