diff --git a/llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h b/llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h --- a/llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h +++ b/llvm/include/llvm/CodeGen/GlobalISel/LegalizerInfo.h @@ -950,6 +950,22 @@ changeTo(typeIdx(TypeIdx), Ty)); } + /// Ensure the scalar is at least as wide as Ty if condition is met. + LegalizeRuleSet &minScalarIf(LegalityPredicate Predicate, unsigned TypeIdx, + const LLT Ty) { + using namespace LegalityPredicates; + using namespace LegalizeMutations; + return actionIf( + LegalizeAction::WidenScalar, + [=](const LegalityQuery &Query) { + const LLT QueryTy = Query.Types[TypeIdx]; + return QueryTy.isScalar() && + QueryTy.getSizeInBits() < Ty.getSizeInBits() && + Predicate(Query); + }, + changeTo(typeIdx(TypeIdx), Ty)); + } + /// Ensure the scalar is at most as wide as Ty. LegalizeRuleSet &maxScalarOrElt(unsigned TypeIdx, const LLT Ty) { using namespace LegalityPredicates; diff --git a/llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp b/llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp --- a/llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp +++ b/llvm/unittests/CodeGen/GlobalISel/LegalizerInfoTest.cpp @@ -321,6 +321,27 @@ EXPECT_ACTION(Unsupported, 0, LLT(), LegalityQuery(G_OR, {v2s16})); } + // Test minScalarIf + { + bool IfCond = true; + LegalizerInfo LI; + auto &LegacyInfo = LI.getLegacyLegalizerInfo(); + LI.getActionDefinitionsBuilder(G_OR) + .legalFor({s32}) + .minScalarIf([&](const LegalityQuery &Query) { + return IfCond; + }, 0, s32); + LegacyInfo.computeTables(); + + // Only handle scalars, ignore vectors. + EXPECT_ACTION(WidenScalar, 0, s32, LegalityQuery(G_OR, {s16})); + EXPECT_ACTION(Unsupported, 0, LLT(), LegalityQuery(G_OR, {v2s16})); + + IfCond = false; + EXPECT_ACTION(Unsupported, 0, LLT(), LegalityQuery(G_OR, {s16})); + EXPECT_ACTION(Unsupported, 0, LLT(), LegalityQuery(G_OR, {v2s16})); + } + // Test maxScalar { LegalizerInfo LI;