xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/LegalizerInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- lib/CodeGen/GlobalISel/LegalizerInfo.cpp - Legalizer ---------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implement an interface to specify and query how an illegal operation on a
100b57cec5SDimitry Andric // given type should be expanded.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
20*0fca6ea1SDimitry Andric #include "llvm/CodeGenTypes/LowLevelType.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
230b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
240b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
250b57cec5SDimitry Andric #include <algorithm>
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric using namespace LegalizeActions;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric #define DEBUG_TYPE "legalizer-info"
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric cl::opt<bool> llvm::DisableGISelLegalityCheck(
330b57cec5SDimitry Andric     "disable-gisel-legality-check",
340b57cec5SDimitry Andric     cl::desc("Don't verify that MIR is fully legal between GlobalISel passes"),
350b57cec5SDimitry Andric     cl::Hidden);
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS, LegalizeAction Action) {
380b57cec5SDimitry Andric   switch (Action) {
390b57cec5SDimitry Andric   case Legal:
400b57cec5SDimitry Andric     OS << "Legal";
410b57cec5SDimitry Andric     break;
420b57cec5SDimitry Andric   case NarrowScalar:
430b57cec5SDimitry Andric     OS << "NarrowScalar";
440b57cec5SDimitry Andric     break;
450b57cec5SDimitry Andric   case WidenScalar:
460b57cec5SDimitry Andric     OS << "WidenScalar";
470b57cec5SDimitry Andric     break;
480b57cec5SDimitry Andric   case FewerElements:
490b57cec5SDimitry Andric     OS << "FewerElements";
500b57cec5SDimitry Andric     break;
510b57cec5SDimitry Andric   case MoreElements:
520b57cec5SDimitry Andric     OS << "MoreElements";
530b57cec5SDimitry Andric     break;
545ffd83dbSDimitry Andric   case Bitcast:
555ffd83dbSDimitry Andric     OS << "Bitcast";
565ffd83dbSDimitry Andric     break;
570b57cec5SDimitry Andric   case Lower:
580b57cec5SDimitry Andric     OS << "Lower";
590b57cec5SDimitry Andric     break;
600b57cec5SDimitry Andric   case Libcall:
610b57cec5SDimitry Andric     OS << "Libcall";
620b57cec5SDimitry Andric     break;
630b57cec5SDimitry Andric   case Custom:
640b57cec5SDimitry Andric     OS << "Custom";
650b57cec5SDimitry Andric     break;
660b57cec5SDimitry Andric   case Unsupported:
670b57cec5SDimitry Andric     OS << "Unsupported";
680b57cec5SDimitry Andric     break;
690b57cec5SDimitry Andric   case NotFound:
700b57cec5SDimitry Andric     OS << "NotFound";
710b57cec5SDimitry Andric     break;
720b57cec5SDimitry Andric   case UseLegacyRules:
730b57cec5SDimitry Andric     OS << "UseLegacyRules";
740b57cec5SDimitry Andric     break;
750b57cec5SDimitry Andric   }
760b57cec5SDimitry Andric   return OS;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric raw_ostream &LegalityQuery::print(raw_ostream &OS) const {
805f757f3fSDimitry Andric   OS << "Opcode=" << Opcode << ", Tys={";
810b57cec5SDimitry Andric   for (const auto &Type : Types) {
820b57cec5SDimitry Andric     OS << Type << ", ";
830b57cec5SDimitry Andric   }
845f757f3fSDimitry Andric   OS << "}, MMOs={";
850b57cec5SDimitry Andric   for (const auto &MMODescr : MMODescrs) {
86fe6060f1SDimitry Andric     OS << MMODescr.MemoryTy << ", ";
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric   OS << "}";
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   return OS;
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric #ifndef NDEBUG
940b57cec5SDimitry Andric // Make sure the rule won't (trivially) loop forever.
950b57cec5SDimitry Andric static bool hasNoSimpleLoops(const LegalizeRule &Rule, const LegalityQuery &Q,
960b57cec5SDimitry Andric                              const std::pair<unsigned, LLT> &Mutation) {
970b57cec5SDimitry Andric   switch (Rule.getAction()) {
98e8d8bef9SDimitry Andric   case Legal:
990b57cec5SDimitry Andric   case Custom:
1000b57cec5SDimitry Andric   case Lower:
1010b57cec5SDimitry Andric   case MoreElements:
1020b57cec5SDimitry Andric   case FewerElements:
1035f757f3fSDimitry Andric   case Libcall:
1040b57cec5SDimitry Andric     break;
1050b57cec5SDimitry Andric   default:
1060b57cec5SDimitry Andric     return Q.Types[Mutation.first] != Mutation.second;
1070b57cec5SDimitry Andric   }
1080b57cec5SDimitry Andric   return true;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric // Make sure the returned mutation makes sense for the match type.
1120b57cec5SDimitry Andric static bool mutationIsSane(const LegalizeRule &Rule,
1130b57cec5SDimitry Andric                            const LegalityQuery &Q,
1140b57cec5SDimitry Andric                            std::pair<unsigned, LLT> Mutation) {
1150b57cec5SDimitry Andric   // If the user wants a custom mutation, then we can't really say much about
1160b57cec5SDimitry Andric   // it. Return true, and trust that they're doing the right thing.
117e8d8bef9SDimitry Andric   if (Rule.getAction() == Custom || Rule.getAction() == Legal)
1180b57cec5SDimitry Andric     return true;
1190b57cec5SDimitry Andric 
1205f757f3fSDimitry Andric   // Skip null mutation.
1215f757f3fSDimitry Andric   if (!Mutation.second.isValid())
1225f757f3fSDimitry Andric     return true;
1235f757f3fSDimitry Andric 
1240b57cec5SDimitry Andric   const unsigned TypeIdx = Mutation.first;
1250b57cec5SDimitry Andric   const LLT OldTy = Q.Types[TypeIdx];
1260b57cec5SDimitry Andric   const LLT NewTy = Mutation.second;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   switch (Rule.getAction()) {
1290b57cec5SDimitry Andric   case FewerElements:
1300b57cec5SDimitry Andric     if (!OldTy.isVector())
1310b57cec5SDimitry Andric       return false;
132bdd1243dSDimitry Andric     [[fallthrough]];
133480093f4SDimitry Andric   case MoreElements: {
134480093f4SDimitry Andric     // MoreElements can go from scalar to vector.
13581ad6265SDimitry Andric     const ElementCount OldElts = OldTy.isVector() ?
13681ad6265SDimitry Andric       OldTy.getElementCount() : ElementCount::getFixed(1);
1370b57cec5SDimitry Andric     if (NewTy.isVector()) {
1380b57cec5SDimitry Andric       if (Rule.getAction() == FewerElements) {
1390b57cec5SDimitry Andric         // Make sure the element count really decreased.
14081ad6265SDimitry Andric         if (ElementCount::isKnownGE(NewTy.getElementCount(), OldElts))
1410b57cec5SDimitry Andric           return false;
1420b57cec5SDimitry Andric       } else {
1430b57cec5SDimitry Andric         // Make sure the element count really increased.
14481ad6265SDimitry Andric         if (ElementCount::isKnownLE(NewTy.getElementCount(), OldElts))
1450b57cec5SDimitry Andric           return false;
1460b57cec5SDimitry Andric       }
147e8d8bef9SDimitry Andric     } else if (Rule.getAction() == MoreElements)
148e8d8bef9SDimitry Andric       return false;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric     // Make sure the element type didn't change.
151480093f4SDimitry Andric     return NewTy.getScalarType() == OldTy.getScalarType();
1520b57cec5SDimitry Andric   }
1530b57cec5SDimitry Andric   case NarrowScalar:
1540b57cec5SDimitry Andric   case WidenScalar: {
1550b57cec5SDimitry Andric     if (OldTy.isVector()) {
1560b57cec5SDimitry Andric       // Number of elements should not change.
157*0fca6ea1SDimitry Andric       if (!NewTy.isVector() ||
158*0fca6ea1SDimitry Andric           OldTy.getElementCount() != NewTy.getElementCount())
1590b57cec5SDimitry Andric         return false;
1600b57cec5SDimitry Andric     } else {
1610b57cec5SDimitry Andric       // Both types must be vectors
1620b57cec5SDimitry Andric       if (NewTy.isVector())
1630b57cec5SDimitry Andric         return false;
1640b57cec5SDimitry Andric     }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric     if (Rule.getAction() == NarrowScalar)  {
1670b57cec5SDimitry Andric       // Make sure the size really decreased.
1680b57cec5SDimitry Andric       if (NewTy.getScalarSizeInBits() >= OldTy.getScalarSizeInBits())
1690b57cec5SDimitry Andric         return false;
1700b57cec5SDimitry Andric     } else {
1710b57cec5SDimitry Andric       // Make sure the size really increased.
1720b57cec5SDimitry Andric       if (NewTy.getScalarSizeInBits() <= OldTy.getScalarSizeInBits())
1730b57cec5SDimitry Andric         return false;
1740b57cec5SDimitry Andric     }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric     return true;
1770b57cec5SDimitry Andric   }
1785ffd83dbSDimitry Andric   case Bitcast: {
1795ffd83dbSDimitry Andric     return OldTy != NewTy && OldTy.getSizeInBits() == NewTy.getSizeInBits();
1805ffd83dbSDimitry Andric   }
1810b57cec5SDimitry Andric   default:
1820b57cec5SDimitry Andric     return true;
1830b57cec5SDimitry Andric   }
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric #endif
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric LegalizeActionStep LegalizeRuleSet::apply(const LegalityQuery &Query) const {
1880b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Applying legalizer ruleset to: "; Query.print(dbgs());
1890b57cec5SDimitry Andric              dbgs() << "\n");
1900b57cec5SDimitry Andric   if (Rules.empty()) {
1910b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. fallback to legacy rules (no rules defined)\n");
1920b57cec5SDimitry Andric     return {LegalizeAction::UseLegacyRules, 0, LLT{}};
1930b57cec5SDimitry Andric   }
1940b57cec5SDimitry Andric   for (const LegalizeRule &Rule : Rules) {
1950b57cec5SDimitry Andric     if (Rule.match(Query)) {
1960b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ".. match\n");
1970b57cec5SDimitry Andric       std::pair<unsigned, LLT> Mutation = Rule.determineMutation(Query);
1980b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ".. .. " << Rule.getAction() << ", "
1990b57cec5SDimitry Andric                         << Mutation.first << ", " << Mutation.second << "\n");
2000b57cec5SDimitry Andric       assert(mutationIsSane(Rule, Query, Mutation) &&
2010b57cec5SDimitry Andric              "legality mutation invalid for match");
2020b57cec5SDimitry Andric       assert(hasNoSimpleLoops(Rule, Query, Mutation) && "Simple loop detected");
2030b57cec5SDimitry Andric       return {Rule.getAction(), Mutation.first, Mutation.second};
2040b57cec5SDimitry Andric     } else
2050b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ".. no match\n");
2060b57cec5SDimitry Andric   }
2070b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ".. unsupported\n");
2080b57cec5SDimitry Andric   return {LegalizeAction::Unsupported, 0, LLT{}};
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric bool LegalizeRuleSet::verifyTypeIdxsCoverage(unsigned NumTypeIdxs) const {
2120b57cec5SDimitry Andric #ifndef NDEBUG
2130b57cec5SDimitry Andric   if (Rules.empty()) {
2140b57cec5SDimitry Andric     LLVM_DEBUG(
2150b57cec5SDimitry Andric         dbgs() << ".. type index coverage check SKIPPED: no rules defined\n");
2160b57cec5SDimitry Andric     return true;
2170b57cec5SDimitry Andric   }
2180b57cec5SDimitry Andric   const int64_t FirstUncovered = TypeIdxsCovered.find_first_unset();
2190b57cec5SDimitry Andric   if (FirstUncovered < 0) {
2200b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. type index coverage check SKIPPED:"
2210b57cec5SDimitry Andric                          " user-defined predicate detected\n");
2220b57cec5SDimitry Andric     return true;
2230b57cec5SDimitry Andric   }
2240b57cec5SDimitry Andric   const bool AllCovered = (FirstUncovered >= NumTypeIdxs);
2258bcb0991SDimitry Andric   if (NumTypeIdxs > 0)
2260b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. the first uncovered type index: " << FirstUncovered
2270b57cec5SDimitry Andric                       << ", " << (AllCovered ? "OK" : "FAIL") << "\n");
2280b57cec5SDimitry Andric   return AllCovered;
2290b57cec5SDimitry Andric #else
2300b57cec5SDimitry Andric   return true;
2310b57cec5SDimitry Andric #endif
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
2348bcb0991SDimitry Andric bool LegalizeRuleSet::verifyImmIdxsCoverage(unsigned NumImmIdxs) const {
2358bcb0991SDimitry Andric #ifndef NDEBUG
2368bcb0991SDimitry Andric   if (Rules.empty()) {
2378bcb0991SDimitry Andric     LLVM_DEBUG(
2388bcb0991SDimitry Andric         dbgs() << ".. imm index coverage check SKIPPED: no rules defined\n");
2398bcb0991SDimitry Andric     return true;
2408bcb0991SDimitry Andric   }
2418bcb0991SDimitry Andric   const int64_t FirstUncovered = ImmIdxsCovered.find_first_unset();
2428bcb0991SDimitry Andric   if (FirstUncovered < 0) {
2438bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << ".. imm index coverage check SKIPPED:"
2448bcb0991SDimitry Andric                          " user-defined predicate detected\n");
2458bcb0991SDimitry Andric     return true;
2468bcb0991SDimitry Andric   }
2478bcb0991SDimitry Andric   const bool AllCovered = (FirstUncovered >= NumImmIdxs);
2488bcb0991SDimitry Andric   LLVM_DEBUG(dbgs() << ".. the first uncovered imm index: " << FirstUncovered
2498bcb0991SDimitry Andric                     << ", " << (AllCovered ? "OK" : "FAIL") << "\n");
2508bcb0991SDimitry Andric   return AllCovered;
2518bcb0991SDimitry Andric #else
2528bcb0991SDimitry Andric   return true;
2538bcb0991SDimitry Andric #endif
2548bcb0991SDimitry Andric }
2558bcb0991SDimitry Andric 
2560b57cec5SDimitry Andric /// Helper function to get LLT for the given type index.
2570b57cec5SDimitry Andric static LLT getTypeFromTypeIdx(const MachineInstr &MI,
2580b57cec5SDimitry Andric                               const MachineRegisterInfo &MRI, unsigned OpIdx,
2590b57cec5SDimitry Andric                               unsigned TypeIdx) {
2600b57cec5SDimitry Andric   assert(TypeIdx < MI.getNumOperands() && "Unexpected TypeIdx");
2610b57cec5SDimitry Andric   // G_UNMERGE_VALUES has variable number of operands, but there is only
2620b57cec5SDimitry Andric   // one source type and one destination type as all destinations must be the
2630b57cec5SDimitry Andric   // same type. So, get the last operand if TypeIdx == 1.
2640b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES && TypeIdx == 1)
2650b57cec5SDimitry Andric     return MRI.getType(MI.getOperand(MI.getNumOperands() - 1).getReg());
2660b57cec5SDimitry Andric   return MRI.getType(MI.getOperand(OpIdx).getReg());
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric unsigned LegalizerInfo::getOpcodeIdxForOpcode(unsigned Opcode) const {
2700b57cec5SDimitry Andric   assert(Opcode >= FirstOp && Opcode <= LastOp && "Unsupported opcode");
2710b57cec5SDimitry Andric   return Opcode - FirstOp;
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric unsigned LegalizerInfo::getActionDefinitionsIdx(unsigned Opcode) const {
2750b57cec5SDimitry Andric   unsigned OpcodeIdx = getOpcodeIdxForOpcode(Opcode);
2760b57cec5SDimitry Andric   if (unsigned Alias = RulesForOpcode[OpcodeIdx].getAlias()) {
2770b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. opcode " << Opcode << " is aliased to " << Alias
2780b57cec5SDimitry Andric                       << "\n");
2790b57cec5SDimitry Andric     OpcodeIdx = getOpcodeIdxForOpcode(Alias);
2800b57cec5SDimitry Andric     assert(RulesForOpcode[OpcodeIdx].getAlias() == 0 && "Cannot chain aliases");
2810b57cec5SDimitry Andric   }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   return OpcodeIdx;
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric const LegalizeRuleSet &
2870b57cec5SDimitry Andric LegalizerInfo::getActionDefinitions(unsigned Opcode) const {
2880b57cec5SDimitry Andric   unsigned OpcodeIdx = getActionDefinitionsIdx(Opcode);
2890b57cec5SDimitry Andric   return RulesForOpcode[OpcodeIdx];
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric LegalizeRuleSet &LegalizerInfo::getActionDefinitionsBuilder(unsigned Opcode) {
2930b57cec5SDimitry Andric   unsigned OpcodeIdx = getActionDefinitionsIdx(Opcode);
2940b57cec5SDimitry Andric   auto &Result = RulesForOpcode[OpcodeIdx];
2950b57cec5SDimitry Andric   assert(!Result.isAliasedByAnother() && "Modifying this opcode will modify aliases");
2960b57cec5SDimitry Andric   return Result;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric LegalizeRuleSet &LegalizerInfo::getActionDefinitionsBuilder(
3000b57cec5SDimitry Andric     std::initializer_list<unsigned> Opcodes) {
3010b57cec5SDimitry Andric   unsigned Representative = *Opcodes.begin();
3020b57cec5SDimitry Andric 
303bdd1243dSDimitry Andric   assert(Opcodes.size() >= 2 &&
3040b57cec5SDimitry Andric          "Initializer list must have at least two opcodes");
3050b57cec5SDimitry Andric 
306fe6060f1SDimitry Andric   for (unsigned Op : llvm::drop_begin(Opcodes))
307fe6060f1SDimitry Andric     aliasActionDefinitions(Representative, Op);
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   auto &Return = getActionDefinitionsBuilder(Representative);
3100b57cec5SDimitry Andric   Return.setIsAliasedByAnother();
3110b57cec5SDimitry Andric   return Return;
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric void LegalizerInfo::aliasActionDefinitions(unsigned OpcodeTo,
3150b57cec5SDimitry Andric                                            unsigned OpcodeFrom) {
3160b57cec5SDimitry Andric   assert(OpcodeTo != OpcodeFrom && "Cannot alias to self");
3170b57cec5SDimitry Andric   assert(OpcodeTo >= FirstOp && OpcodeTo <= LastOp && "Unsupported opcode");
3180b57cec5SDimitry Andric   const unsigned OpcodeFromIdx = getOpcodeIdxForOpcode(OpcodeFrom);
3190b57cec5SDimitry Andric   RulesForOpcode[OpcodeFromIdx].aliasTo(OpcodeTo);
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric LegalizeActionStep
3230b57cec5SDimitry Andric LegalizerInfo::getAction(const LegalityQuery &Query) const {
3240b57cec5SDimitry Andric   LegalizeActionStep Step = getActionDefinitions(Query.Opcode).apply(Query);
3250b57cec5SDimitry Andric   if (Step.Action != LegalizeAction::UseLegacyRules) {
3260b57cec5SDimitry Andric     return Step;
3270b57cec5SDimitry Andric   }
3280b57cec5SDimitry Andric 
329fe6060f1SDimitry Andric   return getLegacyLegalizerInfo().getAction(Query);
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric LegalizeActionStep
3330b57cec5SDimitry Andric LegalizerInfo::getAction(const MachineInstr &MI,
3340b57cec5SDimitry Andric                          const MachineRegisterInfo &MRI) const {
335fe6060f1SDimitry Andric   SmallVector<LLT, 8> Types;
3360b57cec5SDimitry Andric   SmallBitVector SeenTypes(8);
337bdd1243dSDimitry Andric   ArrayRef<MCOperandInfo> OpInfo = MI.getDesc().operands();
3380b57cec5SDimitry Andric   // FIXME: probably we'll need to cache the results here somehow?
3390b57cec5SDimitry Andric   for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
3400b57cec5SDimitry Andric     if (!OpInfo[i].isGenericType())
3410b57cec5SDimitry Andric       continue;
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric     // We must only record actions once for each TypeIdx; otherwise we'd
3440b57cec5SDimitry Andric     // try to legalize operands multiple times down the line.
3450b57cec5SDimitry Andric     unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
3460b57cec5SDimitry Andric     if (SeenTypes[TypeIdx])
3470b57cec5SDimitry Andric       continue;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric     SeenTypes.set(TypeIdx);
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric     LLT Ty = getTypeFromTypeIdx(MI, MRI, i, TypeIdx);
3520b57cec5SDimitry Andric     Types.push_back(Ty);
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   SmallVector<LegalityQuery::MemDesc, 2> MemDescrs;
3560b57cec5SDimitry Andric   for (const auto &MMO : MI.memoperands())
357349cc55cSDimitry Andric     MemDescrs.push_back({*MMO});
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   return getAction({MI.getOpcode(), Types, MemDescrs});
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric bool LegalizerInfo::isLegal(const MachineInstr &MI,
3630b57cec5SDimitry Andric                             const MachineRegisterInfo &MRI) const {
3640b57cec5SDimitry Andric   return getAction(MI, MRI).Action == Legal;
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric bool LegalizerInfo::isLegalOrCustom(const MachineInstr &MI,
3680b57cec5SDimitry Andric                                     const MachineRegisterInfo &MRI) const {
3690b57cec5SDimitry Andric   auto Action = getAction(MI, MRI).Action;
3700b57cec5SDimitry Andric   // If the action is custom, it may not necessarily modify the instruction,
3710b57cec5SDimitry Andric   // so we have to assume it's legal.
3720b57cec5SDimitry Andric   return Action == Legal || Action == Custom;
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric 
375480093f4SDimitry Andric unsigned LegalizerInfo::getExtOpcodeForWideningConstant(LLT SmallTy) const {
376480093f4SDimitry Andric   return SmallTy.isByteSized() ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT;
377480093f4SDimitry Andric }
378480093f4SDimitry Andric 
3790b57cec5SDimitry Andric /// \pre Type indices of every opcode form a dense set starting from 0.
3800b57cec5SDimitry Andric void LegalizerInfo::verify(const MCInstrInfo &MII) const {
3810b57cec5SDimitry Andric #ifndef NDEBUG
3820b57cec5SDimitry Andric   std::vector<unsigned> FailedOpcodes;
3830b57cec5SDimitry Andric   for (unsigned Opcode = FirstOp; Opcode <= LastOp; ++Opcode) {
3840b57cec5SDimitry Andric     const MCInstrDesc &MCID = MII.get(Opcode);
3850b57cec5SDimitry Andric     const unsigned NumTypeIdxs = std::accumulate(
386bdd1243dSDimitry Andric         MCID.operands().begin(), MCID.operands().end(), 0U,
3870b57cec5SDimitry Andric         [](unsigned Acc, const MCOperandInfo &OpInfo) {
3880b57cec5SDimitry Andric           return OpInfo.isGenericType()
3890b57cec5SDimitry Andric                      ? std::max(OpInfo.getGenericTypeIndex() + 1U, Acc)
3900b57cec5SDimitry Andric                      : Acc;
3910b57cec5SDimitry Andric         });
3928bcb0991SDimitry Andric     const unsigned NumImmIdxs = std::accumulate(
393bdd1243dSDimitry Andric         MCID.operands().begin(), MCID.operands().end(), 0U,
3948bcb0991SDimitry Andric         [](unsigned Acc, const MCOperandInfo &OpInfo) {
3958bcb0991SDimitry Andric           return OpInfo.isGenericImm()
3968bcb0991SDimitry Andric                      ? std::max(OpInfo.getGenericImmIndex() + 1U, Acc)
3978bcb0991SDimitry Andric                      : Acc;
3988bcb0991SDimitry Andric         });
3990b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << MII.getName(Opcode) << " (opcode " << Opcode
4000b57cec5SDimitry Andric                       << "): " << NumTypeIdxs << " type ind"
4018bcb0991SDimitry Andric                       << (NumTypeIdxs == 1 ? "ex" : "ices") << ", "
4028bcb0991SDimitry Andric                       << NumImmIdxs << " imm ind"
4038bcb0991SDimitry Andric                       << (NumImmIdxs == 1 ? "ex" : "ices") << "\n");
4040b57cec5SDimitry Andric     const LegalizeRuleSet &RuleSet = getActionDefinitions(Opcode);
4050b57cec5SDimitry Andric     if (!RuleSet.verifyTypeIdxsCoverage(NumTypeIdxs))
4060b57cec5SDimitry Andric       FailedOpcodes.push_back(Opcode);
4078bcb0991SDimitry Andric     else if (!RuleSet.verifyImmIdxsCoverage(NumImmIdxs))
4088bcb0991SDimitry Andric       FailedOpcodes.push_back(Opcode);
4090b57cec5SDimitry Andric   }
4100b57cec5SDimitry Andric   if (!FailedOpcodes.empty()) {
4110b57cec5SDimitry Andric     errs() << "The following opcodes have ill-defined legalization rules:";
4120b57cec5SDimitry Andric     for (unsigned Opcode : FailedOpcodes)
4130b57cec5SDimitry Andric       errs() << " " << MII.getName(Opcode);
4140b57cec5SDimitry Andric     errs() << "\n";
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric     report_fatal_error("ill-defined LegalizerInfo"
4170b57cec5SDimitry Andric                        ", try -debug-only=legalizer-info for details");
4180b57cec5SDimitry Andric   }
4190b57cec5SDimitry Andric #endif
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric #ifndef NDEBUG
4230b57cec5SDimitry Andric // FIXME: This should be in the MachineVerifier, but it can't use the
4240b57cec5SDimitry Andric // LegalizerInfo as it's currently in the separate GlobalISel library.
4250b57cec5SDimitry Andric // Note that RegBankSelected property already checked in the verifier
4260b57cec5SDimitry Andric // has the same layering problem, but we only use inline methods so
4270b57cec5SDimitry Andric // end up not needing to link against the GlobalISel library.
4280b57cec5SDimitry Andric const MachineInstr *llvm::machineFunctionIsIllegal(const MachineFunction &MF) {
4290b57cec5SDimitry Andric   if (const LegalizerInfo *MLI = MF.getSubtarget().getLegalizerInfo()) {
4300b57cec5SDimitry Andric     const MachineRegisterInfo &MRI = MF.getRegInfo();
4310b57cec5SDimitry Andric     for (const MachineBasicBlock &MBB : MF)
4320b57cec5SDimitry Andric       for (const MachineInstr &MI : MBB)
4330b57cec5SDimitry Andric         if (isPreISelGenericOpcode(MI.getOpcode()) &&
4340b57cec5SDimitry Andric             !MLI->isLegalOrCustom(MI, MRI))
4350b57cec5SDimitry Andric           return &MI;
4360b57cec5SDimitry Andric   }
4370b57cec5SDimitry Andric   return nullptr;
4380b57cec5SDimitry Andric }
4390b57cec5SDimitry Andric #endif
440