xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/GlobalISel/LegalizerInfo.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
200b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
220b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
230b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
240b57cec5SDimitry Andric #include "llvm/Support/LowLevelTypeImpl.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 {
800b57cec5SDimitry Andric   OS << Opcode << ", Tys={";
810b57cec5SDimitry Andric   for (const auto &Type : Types) {
820b57cec5SDimitry Andric     OS << Type << ", ";
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric   OS << "}, Opcode=";
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   OS << Opcode << ", MMOs={";
870b57cec5SDimitry Andric   for (const auto &MMODescr : MMODescrs) {
88fe6060f1SDimitry Andric     OS << MMODescr.MemoryTy << ", ";
890b57cec5SDimitry Andric   }
900b57cec5SDimitry Andric   OS << "}";
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   return OS;
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric #ifndef NDEBUG
960b57cec5SDimitry Andric // Make sure the rule won't (trivially) loop forever.
970b57cec5SDimitry Andric static bool hasNoSimpleLoops(const LegalizeRule &Rule, const LegalityQuery &Q,
980b57cec5SDimitry Andric                              const std::pair<unsigned, LLT> &Mutation) {
990b57cec5SDimitry Andric   switch (Rule.getAction()) {
100e8d8bef9SDimitry Andric   case Legal:
1010b57cec5SDimitry Andric   case Custom:
1020b57cec5SDimitry Andric   case Lower:
1030b57cec5SDimitry Andric   case MoreElements:
1040b57cec5SDimitry Andric   case FewerElements:
1050b57cec5SDimitry Andric     break;
1060b57cec5SDimitry Andric   default:
1070b57cec5SDimitry Andric     return Q.Types[Mutation.first] != Mutation.second;
1080b57cec5SDimitry Andric   }
1090b57cec5SDimitry Andric   return true;
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric // Make sure the returned mutation makes sense for the match type.
1130b57cec5SDimitry Andric static bool mutationIsSane(const LegalizeRule &Rule,
1140b57cec5SDimitry Andric                            const LegalityQuery &Q,
1150b57cec5SDimitry Andric                            std::pair<unsigned, LLT> Mutation) {
1160b57cec5SDimitry Andric   // If the user wants a custom mutation, then we can't really say much about
1170b57cec5SDimitry Andric   // it. Return true, and trust that they're doing the right thing.
118e8d8bef9SDimitry Andric   if (Rule.getAction() == Custom || Rule.getAction() == Legal)
1190b57cec5SDimitry Andric     return true;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   const unsigned TypeIdx = Mutation.first;
1220b57cec5SDimitry Andric   const LLT OldTy = Q.Types[TypeIdx];
1230b57cec5SDimitry Andric   const LLT NewTy = Mutation.second;
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   switch (Rule.getAction()) {
1260b57cec5SDimitry Andric   case FewerElements:
1270b57cec5SDimitry Andric     if (!OldTy.isVector())
1280b57cec5SDimitry Andric       return false;
129*bdd1243dSDimitry Andric     [[fallthrough]];
130480093f4SDimitry Andric   case MoreElements: {
131480093f4SDimitry Andric     // MoreElements can go from scalar to vector.
13281ad6265SDimitry Andric     const ElementCount OldElts = OldTy.isVector() ?
13381ad6265SDimitry Andric       OldTy.getElementCount() : ElementCount::getFixed(1);
1340b57cec5SDimitry Andric     if (NewTy.isVector()) {
1350b57cec5SDimitry Andric       if (Rule.getAction() == FewerElements) {
1360b57cec5SDimitry Andric         // Make sure the element count really decreased.
13781ad6265SDimitry Andric         if (ElementCount::isKnownGE(NewTy.getElementCount(), OldElts))
1380b57cec5SDimitry Andric           return false;
1390b57cec5SDimitry Andric       } else {
1400b57cec5SDimitry Andric         // Make sure the element count really increased.
14181ad6265SDimitry Andric         if (ElementCount::isKnownLE(NewTy.getElementCount(), OldElts))
1420b57cec5SDimitry Andric           return false;
1430b57cec5SDimitry Andric       }
144e8d8bef9SDimitry Andric     } else if (Rule.getAction() == MoreElements)
145e8d8bef9SDimitry Andric       return false;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric     // Make sure the element type didn't change.
148480093f4SDimitry Andric     return NewTy.getScalarType() == OldTy.getScalarType();
1490b57cec5SDimitry Andric   }
1500b57cec5SDimitry Andric   case NarrowScalar:
1510b57cec5SDimitry Andric   case WidenScalar: {
1520b57cec5SDimitry Andric     if (OldTy.isVector()) {
1530b57cec5SDimitry Andric       // Number of elements should not change.
1540b57cec5SDimitry Andric       if (!NewTy.isVector() || OldTy.getNumElements() != NewTy.getNumElements())
1550b57cec5SDimitry Andric         return false;
1560b57cec5SDimitry Andric     } else {
1570b57cec5SDimitry Andric       // Both types must be vectors
1580b57cec5SDimitry Andric       if (NewTy.isVector())
1590b57cec5SDimitry Andric         return false;
1600b57cec5SDimitry Andric     }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric     if (Rule.getAction() == NarrowScalar)  {
1630b57cec5SDimitry Andric       // Make sure the size really decreased.
1640b57cec5SDimitry Andric       if (NewTy.getScalarSizeInBits() >= OldTy.getScalarSizeInBits())
1650b57cec5SDimitry Andric         return false;
1660b57cec5SDimitry Andric     } else {
1670b57cec5SDimitry Andric       // Make sure the size really increased.
1680b57cec5SDimitry Andric       if (NewTy.getScalarSizeInBits() <= OldTy.getScalarSizeInBits())
1690b57cec5SDimitry Andric         return false;
1700b57cec5SDimitry Andric     }
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric     return true;
1730b57cec5SDimitry Andric   }
1745ffd83dbSDimitry Andric   case Bitcast: {
1755ffd83dbSDimitry Andric     return OldTy != NewTy && OldTy.getSizeInBits() == NewTy.getSizeInBits();
1765ffd83dbSDimitry Andric   }
1770b57cec5SDimitry Andric   default:
1780b57cec5SDimitry Andric     return true;
1790b57cec5SDimitry Andric   }
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric #endif
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric LegalizeActionStep LegalizeRuleSet::apply(const LegalityQuery &Query) const {
1840b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Applying legalizer ruleset to: "; Query.print(dbgs());
1850b57cec5SDimitry Andric              dbgs() << "\n");
1860b57cec5SDimitry Andric   if (Rules.empty()) {
1870b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. fallback to legacy rules (no rules defined)\n");
1880b57cec5SDimitry Andric     return {LegalizeAction::UseLegacyRules, 0, LLT{}};
1890b57cec5SDimitry Andric   }
1900b57cec5SDimitry Andric   for (const LegalizeRule &Rule : Rules) {
1910b57cec5SDimitry Andric     if (Rule.match(Query)) {
1920b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ".. match\n");
1930b57cec5SDimitry Andric       std::pair<unsigned, LLT> Mutation = Rule.determineMutation(Query);
1940b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ".. .. " << Rule.getAction() << ", "
1950b57cec5SDimitry Andric                         << Mutation.first << ", " << Mutation.second << "\n");
1960b57cec5SDimitry Andric       assert(mutationIsSane(Rule, Query, Mutation) &&
1970b57cec5SDimitry Andric              "legality mutation invalid for match");
1980b57cec5SDimitry Andric       assert(hasNoSimpleLoops(Rule, Query, Mutation) && "Simple loop detected");
1990b57cec5SDimitry Andric       return {Rule.getAction(), Mutation.first, Mutation.second};
2000b57cec5SDimitry Andric     } else
2010b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << ".. no match\n");
2020b57cec5SDimitry Andric   }
2030b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << ".. unsupported\n");
2040b57cec5SDimitry Andric   return {LegalizeAction::Unsupported, 0, LLT{}};
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric bool LegalizeRuleSet::verifyTypeIdxsCoverage(unsigned NumTypeIdxs) const {
2080b57cec5SDimitry Andric #ifndef NDEBUG
2090b57cec5SDimitry Andric   if (Rules.empty()) {
2100b57cec5SDimitry Andric     LLVM_DEBUG(
2110b57cec5SDimitry Andric         dbgs() << ".. type index coverage check SKIPPED: no rules defined\n");
2120b57cec5SDimitry Andric     return true;
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric   const int64_t FirstUncovered = TypeIdxsCovered.find_first_unset();
2150b57cec5SDimitry Andric   if (FirstUncovered < 0) {
2160b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. type index coverage check SKIPPED:"
2170b57cec5SDimitry Andric                          " user-defined predicate detected\n");
2180b57cec5SDimitry Andric     return true;
2190b57cec5SDimitry Andric   }
2200b57cec5SDimitry Andric   const bool AllCovered = (FirstUncovered >= NumTypeIdxs);
2218bcb0991SDimitry Andric   if (NumTypeIdxs > 0)
2220b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. the first uncovered type index: " << FirstUncovered
2230b57cec5SDimitry Andric                       << ", " << (AllCovered ? "OK" : "FAIL") << "\n");
2240b57cec5SDimitry Andric   return AllCovered;
2250b57cec5SDimitry Andric #else
2260b57cec5SDimitry Andric   return true;
2270b57cec5SDimitry Andric #endif
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
2308bcb0991SDimitry Andric bool LegalizeRuleSet::verifyImmIdxsCoverage(unsigned NumImmIdxs) const {
2318bcb0991SDimitry Andric #ifndef NDEBUG
2328bcb0991SDimitry Andric   if (Rules.empty()) {
2338bcb0991SDimitry Andric     LLVM_DEBUG(
2348bcb0991SDimitry Andric         dbgs() << ".. imm index coverage check SKIPPED: no rules defined\n");
2358bcb0991SDimitry Andric     return true;
2368bcb0991SDimitry Andric   }
2378bcb0991SDimitry Andric   const int64_t FirstUncovered = ImmIdxsCovered.find_first_unset();
2388bcb0991SDimitry Andric   if (FirstUncovered < 0) {
2398bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << ".. imm index coverage check SKIPPED:"
2408bcb0991SDimitry Andric                          " user-defined predicate detected\n");
2418bcb0991SDimitry Andric     return true;
2428bcb0991SDimitry Andric   }
2438bcb0991SDimitry Andric   const bool AllCovered = (FirstUncovered >= NumImmIdxs);
2448bcb0991SDimitry Andric   LLVM_DEBUG(dbgs() << ".. the first uncovered imm index: " << FirstUncovered
2458bcb0991SDimitry Andric                     << ", " << (AllCovered ? "OK" : "FAIL") << "\n");
2468bcb0991SDimitry Andric   return AllCovered;
2478bcb0991SDimitry Andric #else
2488bcb0991SDimitry Andric   return true;
2498bcb0991SDimitry Andric #endif
2508bcb0991SDimitry Andric }
2518bcb0991SDimitry Andric 
2520b57cec5SDimitry Andric /// Helper function to get LLT for the given type index.
2530b57cec5SDimitry Andric static LLT getTypeFromTypeIdx(const MachineInstr &MI,
2540b57cec5SDimitry Andric                               const MachineRegisterInfo &MRI, unsigned OpIdx,
2550b57cec5SDimitry Andric                               unsigned TypeIdx) {
2560b57cec5SDimitry Andric   assert(TypeIdx < MI.getNumOperands() && "Unexpected TypeIdx");
2570b57cec5SDimitry Andric   // G_UNMERGE_VALUES has variable number of operands, but there is only
2580b57cec5SDimitry Andric   // one source type and one destination type as all destinations must be the
2590b57cec5SDimitry Andric   // same type. So, get the last operand if TypeIdx == 1.
2600b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES && TypeIdx == 1)
2610b57cec5SDimitry Andric     return MRI.getType(MI.getOperand(MI.getNumOperands() - 1).getReg());
2620b57cec5SDimitry Andric   return MRI.getType(MI.getOperand(OpIdx).getReg());
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric unsigned LegalizerInfo::getOpcodeIdxForOpcode(unsigned Opcode) const {
2660b57cec5SDimitry Andric   assert(Opcode >= FirstOp && Opcode <= LastOp && "Unsupported opcode");
2670b57cec5SDimitry Andric   return Opcode - FirstOp;
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric unsigned LegalizerInfo::getActionDefinitionsIdx(unsigned Opcode) const {
2710b57cec5SDimitry Andric   unsigned OpcodeIdx = getOpcodeIdxForOpcode(Opcode);
2720b57cec5SDimitry Andric   if (unsigned Alias = RulesForOpcode[OpcodeIdx].getAlias()) {
2730b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << ".. opcode " << Opcode << " is aliased to " << Alias
2740b57cec5SDimitry Andric                       << "\n");
2750b57cec5SDimitry Andric     OpcodeIdx = getOpcodeIdxForOpcode(Alias);
2760b57cec5SDimitry Andric     assert(RulesForOpcode[OpcodeIdx].getAlias() == 0 && "Cannot chain aliases");
2770b57cec5SDimitry Andric   }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   return OpcodeIdx;
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric const LegalizeRuleSet &
2830b57cec5SDimitry Andric LegalizerInfo::getActionDefinitions(unsigned Opcode) const {
2840b57cec5SDimitry Andric   unsigned OpcodeIdx = getActionDefinitionsIdx(Opcode);
2850b57cec5SDimitry Andric   return RulesForOpcode[OpcodeIdx];
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric LegalizeRuleSet &LegalizerInfo::getActionDefinitionsBuilder(unsigned Opcode) {
2890b57cec5SDimitry Andric   unsigned OpcodeIdx = getActionDefinitionsIdx(Opcode);
2900b57cec5SDimitry Andric   auto &Result = RulesForOpcode[OpcodeIdx];
2910b57cec5SDimitry Andric   assert(!Result.isAliasedByAnother() && "Modifying this opcode will modify aliases");
2920b57cec5SDimitry Andric   return Result;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric LegalizeRuleSet &LegalizerInfo::getActionDefinitionsBuilder(
2960b57cec5SDimitry Andric     std::initializer_list<unsigned> Opcodes) {
2970b57cec5SDimitry Andric   unsigned Representative = *Opcodes.begin();
2980b57cec5SDimitry Andric 
299*bdd1243dSDimitry Andric   assert(Opcodes.size() >= 2 &&
3000b57cec5SDimitry Andric          "Initializer list must have at least two opcodes");
3010b57cec5SDimitry Andric 
302fe6060f1SDimitry Andric   for (unsigned Op : llvm::drop_begin(Opcodes))
303fe6060f1SDimitry Andric     aliasActionDefinitions(Representative, Op);
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   auto &Return = getActionDefinitionsBuilder(Representative);
3060b57cec5SDimitry Andric   Return.setIsAliasedByAnother();
3070b57cec5SDimitry Andric   return Return;
3080b57cec5SDimitry Andric }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric void LegalizerInfo::aliasActionDefinitions(unsigned OpcodeTo,
3110b57cec5SDimitry Andric                                            unsigned OpcodeFrom) {
3120b57cec5SDimitry Andric   assert(OpcodeTo != OpcodeFrom && "Cannot alias to self");
3130b57cec5SDimitry Andric   assert(OpcodeTo >= FirstOp && OpcodeTo <= LastOp && "Unsupported opcode");
3140b57cec5SDimitry Andric   const unsigned OpcodeFromIdx = getOpcodeIdxForOpcode(OpcodeFrom);
3150b57cec5SDimitry Andric   RulesForOpcode[OpcodeFromIdx].aliasTo(OpcodeTo);
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric LegalizeActionStep
3190b57cec5SDimitry Andric LegalizerInfo::getAction(const LegalityQuery &Query) const {
3200b57cec5SDimitry Andric   LegalizeActionStep Step = getActionDefinitions(Query.Opcode).apply(Query);
3210b57cec5SDimitry Andric   if (Step.Action != LegalizeAction::UseLegacyRules) {
3220b57cec5SDimitry Andric     return Step;
3230b57cec5SDimitry Andric   }
3240b57cec5SDimitry Andric 
325fe6060f1SDimitry Andric   return getLegacyLegalizerInfo().getAction(Query);
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric LegalizeActionStep
3290b57cec5SDimitry Andric LegalizerInfo::getAction(const MachineInstr &MI,
3300b57cec5SDimitry Andric                          const MachineRegisterInfo &MRI) const {
331fe6060f1SDimitry Andric   SmallVector<LLT, 8> Types;
3320b57cec5SDimitry Andric   SmallBitVector SeenTypes(8);
333*bdd1243dSDimitry Andric   ArrayRef<MCOperandInfo> OpInfo = MI.getDesc().operands();
3340b57cec5SDimitry Andric   // FIXME: probably we'll need to cache the results here somehow?
3350b57cec5SDimitry Andric   for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
3360b57cec5SDimitry Andric     if (!OpInfo[i].isGenericType())
3370b57cec5SDimitry Andric       continue;
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric     // We must only record actions once for each TypeIdx; otherwise we'd
3400b57cec5SDimitry Andric     // try to legalize operands multiple times down the line.
3410b57cec5SDimitry Andric     unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
3420b57cec5SDimitry Andric     if (SeenTypes[TypeIdx])
3430b57cec5SDimitry Andric       continue;
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric     SeenTypes.set(TypeIdx);
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     LLT Ty = getTypeFromTypeIdx(MI, MRI, i, TypeIdx);
3480b57cec5SDimitry Andric     Types.push_back(Ty);
3490b57cec5SDimitry Andric   }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   SmallVector<LegalityQuery::MemDesc, 2> MemDescrs;
3520b57cec5SDimitry Andric   for (const auto &MMO : MI.memoperands())
353349cc55cSDimitry Andric     MemDescrs.push_back({*MMO});
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   return getAction({MI.getOpcode(), Types, MemDescrs});
3560b57cec5SDimitry Andric }
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric bool LegalizerInfo::isLegal(const MachineInstr &MI,
3590b57cec5SDimitry Andric                             const MachineRegisterInfo &MRI) const {
3600b57cec5SDimitry Andric   return getAction(MI, MRI).Action == Legal;
3610b57cec5SDimitry Andric }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric bool LegalizerInfo::isLegalOrCustom(const MachineInstr &MI,
3640b57cec5SDimitry Andric                                     const MachineRegisterInfo &MRI) const {
3650b57cec5SDimitry Andric   auto Action = getAction(MI, MRI).Action;
3660b57cec5SDimitry Andric   // If the action is custom, it may not necessarily modify the instruction,
3670b57cec5SDimitry Andric   // so we have to assume it's legal.
3680b57cec5SDimitry Andric   return Action == Legal || Action == Custom;
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
371480093f4SDimitry Andric unsigned LegalizerInfo::getExtOpcodeForWideningConstant(LLT SmallTy) const {
372480093f4SDimitry Andric   return SmallTy.isByteSized() ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT;
373480093f4SDimitry Andric }
374480093f4SDimitry Andric 
3750b57cec5SDimitry Andric /// \pre Type indices of every opcode form a dense set starting from 0.
3760b57cec5SDimitry Andric void LegalizerInfo::verify(const MCInstrInfo &MII) const {
3770b57cec5SDimitry Andric #ifndef NDEBUG
3780b57cec5SDimitry Andric   std::vector<unsigned> FailedOpcodes;
3790b57cec5SDimitry Andric   for (unsigned Opcode = FirstOp; Opcode <= LastOp; ++Opcode) {
3800b57cec5SDimitry Andric     const MCInstrDesc &MCID = MII.get(Opcode);
3810b57cec5SDimitry Andric     const unsigned NumTypeIdxs = std::accumulate(
382*bdd1243dSDimitry Andric         MCID.operands().begin(), MCID.operands().end(), 0U,
3830b57cec5SDimitry Andric         [](unsigned Acc, const MCOperandInfo &OpInfo) {
3840b57cec5SDimitry Andric           return OpInfo.isGenericType()
3850b57cec5SDimitry Andric                      ? std::max(OpInfo.getGenericTypeIndex() + 1U, Acc)
3860b57cec5SDimitry Andric                      : Acc;
3870b57cec5SDimitry Andric         });
3888bcb0991SDimitry Andric     const unsigned NumImmIdxs = std::accumulate(
389*bdd1243dSDimitry Andric         MCID.operands().begin(), MCID.operands().end(), 0U,
3908bcb0991SDimitry Andric         [](unsigned Acc, const MCOperandInfo &OpInfo) {
3918bcb0991SDimitry Andric           return OpInfo.isGenericImm()
3928bcb0991SDimitry Andric                      ? std::max(OpInfo.getGenericImmIndex() + 1U, Acc)
3938bcb0991SDimitry Andric                      : Acc;
3948bcb0991SDimitry Andric         });
3950b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << MII.getName(Opcode) << " (opcode " << Opcode
3960b57cec5SDimitry Andric                       << "): " << NumTypeIdxs << " type ind"
3978bcb0991SDimitry Andric                       << (NumTypeIdxs == 1 ? "ex" : "ices") << ", "
3988bcb0991SDimitry Andric                       << NumImmIdxs << " imm ind"
3998bcb0991SDimitry Andric                       << (NumImmIdxs == 1 ? "ex" : "ices") << "\n");
4000b57cec5SDimitry Andric     const LegalizeRuleSet &RuleSet = getActionDefinitions(Opcode);
4010b57cec5SDimitry Andric     if (!RuleSet.verifyTypeIdxsCoverage(NumTypeIdxs))
4020b57cec5SDimitry Andric       FailedOpcodes.push_back(Opcode);
4038bcb0991SDimitry Andric     else if (!RuleSet.verifyImmIdxsCoverage(NumImmIdxs))
4048bcb0991SDimitry Andric       FailedOpcodes.push_back(Opcode);
4050b57cec5SDimitry Andric   }
4060b57cec5SDimitry Andric   if (!FailedOpcodes.empty()) {
4070b57cec5SDimitry Andric     errs() << "The following opcodes have ill-defined legalization rules:";
4080b57cec5SDimitry Andric     for (unsigned Opcode : FailedOpcodes)
4090b57cec5SDimitry Andric       errs() << " " << MII.getName(Opcode);
4100b57cec5SDimitry Andric     errs() << "\n";
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric     report_fatal_error("ill-defined LegalizerInfo"
4130b57cec5SDimitry Andric                        ", try -debug-only=legalizer-info for details");
4140b57cec5SDimitry Andric   }
4150b57cec5SDimitry Andric #endif
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric #ifndef NDEBUG
4190b57cec5SDimitry Andric // FIXME: This should be in the MachineVerifier, but it can't use the
4200b57cec5SDimitry Andric // LegalizerInfo as it's currently in the separate GlobalISel library.
4210b57cec5SDimitry Andric // Note that RegBankSelected property already checked in the verifier
4220b57cec5SDimitry Andric // has the same layering problem, but we only use inline methods so
4230b57cec5SDimitry Andric // end up not needing to link against the GlobalISel library.
4240b57cec5SDimitry Andric const MachineInstr *llvm::machineFunctionIsIllegal(const MachineFunction &MF) {
4250b57cec5SDimitry Andric   if (const LegalizerInfo *MLI = MF.getSubtarget().getLegalizerInfo()) {
4260b57cec5SDimitry Andric     const MachineRegisterInfo &MRI = MF.getRegInfo();
4270b57cec5SDimitry Andric     for (const MachineBasicBlock &MBB : MF)
4280b57cec5SDimitry Andric       for (const MachineInstr &MI : MBB)
4290b57cec5SDimitry Andric         if (isPreISelGenericOpcode(MI.getOpcode()) &&
4300b57cec5SDimitry Andric             !MLI->isLegalOrCustom(MI, MRI))
4310b57cec5SDimitry Andric           return &MI;
4320b57cec5SDimitry Andric   }
4330b57cec5SDimitry Andric   return nullptr;
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric #endif
436