1 //===- lib/CodeGen/GlobalISel/LegalizerInfo.cpp - Legalizer ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement an interface to specify and query how an illegal operation on a
11 // given type should be expanded.
12 //
13 // Issues to be resolved:
14 //   + Make it fast.
15 //   + Support weird types like i3, <7 x i3>, ...
16 //   + Operations with more than one type (ICMP, CMPXCHG, intrinsics, ...)
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
21 #include "llvm/ADT/SmallBitVector.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/TargetOpcodes.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/LowLevelTypeImpl.h"
30 #include "llvm/Support/MathExtras.h"
31 #include <algorithm>
32 #include <map>
33 
34 using namespace llvm;
35 using namespace LegalizeActions;
36 
37 #define DEBUG_TYPE "legalizer-info"
38 
39 cl::opt<bool> llvm::DisableGISelLegalityCheck(
40     "disable-gisel-legality-check",
41     cl::desc("Don't verify that MIR is fully legal between GlobalISel passes"),
42     cl::Hidden);
43 
44 raw_ostream &LegalityQuery::print(raw_ostream &OS) const {
45   OS << Opcode << ", {";
46   for (const auto &Type : Types) {
47     OS << Type << ", ";
48   }
49   OS << "}";
50   return OS;
51 }
52 
53 LegalizeActionStep LegalizeRuleSet::apply(const LegalityQuery &Query) const {
54   DEBUG(dbgs() << "Applying legalizer ruleset to: "; Query.print(dbgs());
55         dbgs() << "\n");
56   if (Rules.empty()) {
57     DEBUG(dbgs() << ".. fallback to legacy rules (no rules defined)\n");
58     return {LegalizeAction::UseLegacyRules, 0, LLT{}};
59   }
60   for (const auto &Rule : Rules) {
61     if (Rule.match(Query)) {
62       DEBUG(dbgs() << ".. match\n");
63       std::pair<unsigned, LLT> Mutation = Rule.determineMutation(Query);
64       DEBUG(dbgs() << ".. .. " << (unsigned)Rule.getAction() << ", "
65                    << Mutation.first << ", " << Mutation.second << "\n");
66       assert((Query.Types[Mutation.first] != Mutation.second ||
67               Rule.getAction() == MoreElements ||
68               Rule.getAction() == FewerElements) &&
69              "Simple loop detected");
70       return {Rule.getAction(), Mutation.first, Mutation.second};
71     } else
72       DEBUG(dbgs() << ".. no match\n");
73   }
74   DEBUG(dbgs() << ".. unsupported\n");
75   return {LegalizeAction::Unsupported, 0, LLT{}};
76 }
77 
78 LegalizerInfo::LegalizerInfo() : TablesInitialized(false) {
79   // Set defaults.
80   // FIXME: these two (G_ANYEXT and G_TRUNC?) can be legalized to the
81   // fundamental load/store Jakob proposed. Once loads & stores are supported.
82   setScalarAction(TargetOpcode::G_ANYEXT, 1, {{1, Legal}});
83   setScalarAction(TargetOpcode::G_ZEXT, 1, {{1, Legal}});
84   setScalarAction(TargetOpcode::G_SEXT, 1, {{1, Legal}});
85   setScalarAction(TargetOpcode::G_TRUNC, 0, {{1, Legal}});
86   setScalarAction(TargetOpcode::G_TRUNC, 1, {{1, Legal}});
87 
88   setScalarAction(TargetOpcode::G_INTRINSIC, 0, {{1, Legal}});
89   setScalarAction(TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS, 0, {{1, Legal}});
90 
91   setLegalizeScalarToDifferentSizeStrategy(
92       TargetOpcode::G_IMPLICIT_DEF, 0, narrowToSmallerAndUnsupportedIfTooSmall);
93   setLegalizeScalarToDifferentSizeStrategy(
94       TargetOpcode::G_ADD, 0, widenToLargerTypesAndNarrowToLargest);
95   setLegalizeScalarToDifferentSizeStrategy(
96       TargetOpcode::G_OR, 0, widenToLargerTypesAndNarrowToLargest);
97   setLegalizeScalarToDifferentSizeStrategy(
98       TargetOpcode::G_LOAD, 0, narrowToSmallerAndUnsupportedIfTooSmall);
99   setLegalizeScalarToDifferentSizeStrategy(
100       TargetOpcode::G_STORE, 0, narrowToSmallerAndUnsupportedIfTooSmall);
101 
102   setLegalizeScalarToDifferentSizeStrategy(
103       TargetOpcode::G_BRCOND, 0, widenToLargerTypesUnsupportedOtherwise);
104   setLegalizeScalarToDifferentSizeStrategy(
105       TargetOpcode::G_INSERT, 0, narrowToSmallerAndUnsupportedIfTooSmall);
106   setLegalizeScalarToDifferentSizeStrategy(
107       TargetOpcode::G_EXTRACT, 0, narrowToSmallerAndUnsupportedIfTooSmall);
108   setLegalizeScalarToDifferentSizeStrategy(
109       TargetOpcode::G_EXTRACT, 1, narrowToSmallerAndUnsupportedIfTooSmall);
110   setScalarAction(TargetOpcode::G_FNEG, 0, {{1, Lower}});
111 }
112 
113 void LegalizerInfo::computeTables() {
114   assert(TablesInitialized == false);
115 
116   for (unsigned OpcodeIdx = 0; OpcodeIdx <= LastOp - FirstOp; ++OpcodeIdx) {
117     const unsigned Opcode = FirstOp + OpcodeIdx;
118     for (unsigned TypeIdx = 0; TypeIdx != SpecifiedActions[OpcodeIdx].size();
119          ++TypeIdx) {
120       // 0. Collect information specified through the setAction API, i.e.
121       // for specific bit sizes.
122       // For scalar types:
123       SizeAndActionsVec ScalarSpecifiedActions;
124       // For pointer types:
125       std::map<uint16_t, SizeAndActionsVec> AddressSpace2SpecifiedActions;
126       // For vector types:
127       std::map<uint16_t, SizeAndActionsVec> ElemSize2SpecifiedActions;
128       for (auto LLT2Action : SpecifiedActions[OpcodeIdx][TypeIdx]) {
129         const LLT Type = LLT2Action.first;
130         const LegalizeAction Action = LLT2Action.second;
131 
132         auto SizeAction = std::make_pair(Type.getSizeInBits(), Action);
133         if (Type.isPointer())
134           AddressSpace2SpecifiedActions[Type.getAddressSpace()].push_back(
135               SizeAction);
136         else if (Type.isVector())
137           ElemSize2SpecifiedActions[Type.getElementType().getSizeInBits()]
138               .push_back(SizeAction);
139         else
140           ScalarSpecifiedActions.push_back(SizeAction);
141       }
142 
143       // 1. Handle scalar types
144       {
145         // Decide how to handle bit sizes for which no explicit specification
146         // was given.
147         SizeChangeStrategy S = &unsupportedForDifferentSizes;
148         if (TypeIdx < ScalarSizeChangeStrategies[OpcodeIdx].size() &&
149             ScalarSizeChangeStrategies[OpcodeIdx][TypeIdx] != nullptr)
150           S = ScalarSizeChangeStrategies[OpcodeIdx][TypeIdx];
151         std::sort(ScalarSpecifiedActions.begin(), ScalarSpecifiedActions.end());
152         checkPartialSizeAndActionsVector(ScalarSpecifiedActions);
153         setScalarAction(Opcode, TypeIdx, S(ScalarSpecifiedActions));
154       }
155 
156       // 2. Handle pointer types
157       for (auto PointerSpecifiedActions : AddressSpace2SpecifiedActions) {
158         std::sort(PointerSpecifiedActions.second.begin(),
159                   PointerSpecifiedActions.second.end());
160         checkPartialSizeAndActionsVector(PointerSpecifiedActions.second);
161         // For pointer types, we assume that there isn't a meaningfull way
162         // to change the number of bits used in the pointer.
163         setPointerAction(
164             Opcode, TypeIdx, PointerSpecifiedActions.first,
165             unsupportedForDifferentSizes(PointerSpecifiedActions.second));
166       }
167 
168       // 3. Handle vector types
169       SizeAndActionsVec ElementSizesSeen;
170       for (auto VectorSpecifiedActions : ElemSize2SpecifiedActions) {
171         std::sort(VectorSpecifiedActions.second.begin(),
172                   VectorSpecifiedActions.second.end());
173         const uint16_t ElementSize = VectorSpecifiedActions.first;
174         ElementSizesSeen.push_back({ElementSize, Legal});
175         checkPartialSizeAndActionsVector(VectorSpecifiedActions.second);
176         // For vector types, we assume that the best way to adapt the number
177         // of elements is to the next larger number of elements type for which
178         // the vector type is legal, unless there is no such type. In that case,
179         // legalize towards a vector type with a smaller number of elements.
180         SizeAndActionsVec NumElementsActions;
181         for (SizeAndAction BitsizeAndAction : VectorSpecifiedActions.second) {
182           assert(BitsizeAndAction.first % ElementSize == 0);
183           const uint16_t NumElements = BitsizeAndAction.first / ElementSize;
184           NumElementsActions.push_back({NumElements, BitsizeAndAction.second});
185         }
186         setVectorNumElementAction(
187             Opcode, TypeIdx, ElementSize,
188             moreToWiderTypesAndLessToWidest(NumElementsActions));
189       }
190       std::sort(ElementSizesSeen.begin(), ElementSizesSeen.end());
191       SizeChangeStrategy VectorElementSizeChangeStrategy =
192           &unsupportedForDifferentSizes;
193       if (TypeIdx < VectorElementSizeChangeStrategies[OpcodeIdx].size() &&
194           VectorElementSizeChangeStrategies[OpcodeIdx][TypeIdx] != nullptr)
195         VectorElementSizeChangeStrategy =
196             VectorElementSizeChangeStrategies[OpcodeIdx][TypeIdx];
197       setScalarInVectorAction(
198           Opcode, TypeIdx, VectorElementSizeChangeStrategy(ElementSizesSeen));
199     }
200   }
201 
202   TablesInitialized = true;
203 }
204 
205 // FIXME: inefficient implementation for now. Without ComputeValueVTs we're
206 // probably going to need specialized lookup structures for various types before
207 // we have any hope of doing well with something like <13 x i3>. Even the common
208 // cases should do better than what we have now.
209 std::pair<LegalizeAction, LLT>
210 LegalizerInfo::getAspectAction(const InstrAspect &Aspect) const {
211   assert(TablesInitialized && "backend forgot to call computeTables");
212   // These *have* to be implemented for now, they're the fundamental basis of
213   // how everything else is transformed.
214   if (Aspect.Type.isScalar() || Aspect.Type.isPointer())
215     return findScalarLegalAction(Aspect);
216   assert(Aspect.Type.isVector());
217   return findVectorLegalAction(Aspect);
218 }
219 
220 /// Helper function to get LLT for the given type index.
221 static LLT getTypeFromTypeIdx(const MachineInstr &MI,
222                               const MachineRegisterInfo &MRI, unsigned OpIdx,
223                               unsigned TypeIdx) {
224   assert(TypeIdx < MI.getNumOperands() && "Unexpected TypeIdx");
225   // G_UNMERGE_VALUES has variable number of operands, but there is only
226   // one source type and one destination type as all destinations must be the
227   // same type. So, get the last operand if TypeIdx == 1.
228   if (MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES && TypeIdx == 1)
229     return MRI.getType(MI.getOperand(MI.getNumOperands() - 1).getReg());
230   return MRI.getType(MI.getOperand(OpIdx).getReg());
231 }
232 
233 unsigned LegalizerInfo::getOpcodeIdxForOpcode(unsigned Opcode) const {
234   assert(Opcode >= FirstOp && Opcode <= LastOp && "Unsupported opcode");
235   return Opcode - FirstOp;
236 }
237 
238 unsigned LegalizerInfo::getActionDefinitionsIdx(unsigned Opcode) const {
239   unsigned OpcodeIdx = getOpcodeIdxForOpcode(Opcode);
240   if (unsigned Alias = RulesForOpcode[OpcodeIdx].getAlias()) {
241     DEBUG(dbgs() << ".. opcode " << Opcode << " is aliased to " << Alias
242                  << "\n");
243     OpcodeIdx = getOpcodeIdxForOpcode(Alias);
244     DEBUG(dbgs() << ".. opcode " << Alias << " is aliased to "
245                  << RulesForOpcode[OpcodeIdx].getAlias() << "\n");
246     assert(RulesForOpcode[OpcodeIdx].getAlias() == 0 && "Cannot chain aliases");
247   }
248 
249   return OpcodeIdx;
250 }
251 
252 const LegalizeRuleSet &
253 LegalizerInfo::getActionDefinitions(unsigned Opcode) const {
254   unsigned OpcodeIdx = getActionDefinitionsIdx(Opcode);
255   return RulesForOpcode[OpcodeIdx];
256 }
257 
258 LegalizeRuleSet &LegalizerInfo::getActionDefinitionsBuilder(unsigned Opcode) {
259   unsigned OpcodeIdx = getActionDefinitionsIdx(Opcode);
260   auto &Result = RulesForOpcode[OpcodeIdx];
261   assert(!Result.isAliasedByAnother() && "Modifying this opcode will modify aliases");
262   return Result;
263 }
264 
265 LegalizeRuleSet &LegalizerInfo::getActionDefinitionsBuilder(
266     std::initializer_list<unsigned> Opcodes) {
267   unsigned Representative = *Opcodes.begin();
268 
269   assert(Opcodes.begin() != Opcodes.end() &&
270          Opcodes.begin() + 1 != Opcodes.end() &&
271          "Initializer list must have at least two opcodes");
272 
273   for (auto I = Opcodes.begin() + 1, E = Opcodes.end(); I != E; ++I)
274     aliasActionDefinitions(Representative, *I);
275 
276   auto &Return = getActionDefinitionsBuilder(Representative);
277   Return.setIsAliasedByAnother();
278   return Return;
279 }
280 
281 void LegalizerInfo::aliasActionDefinitions(unsigned OpcodeTo,
282                                            unsigned OpcodeFrom) {
283   assert(OpcodeTo != OpcodeFrom && "Cannot alias to self");
284   assert(OpcodeTo >= FirstOp && OpcodeTo <= LastOp && "Unsupported opcode");
285   const unsigned OpcodeFromIdx = getOpcodeIdxForOpcode(OpcodeFrom);
286   RulesForOpcode[OpcodeFromIdx].aliasTo(OpcodeTo);
287 }
288 
289 LegalizeActionStep
290 LegalizerInfo::getAction(const LegalityQuery &Query) const {
291   LegalizeActionStep Step = getActionDefinitions(Query.Opcode).apply(Query);
292   if (Step.Action != LegalizeAction::UseLegacyRules) {
293     return Step;
294   }
295 
296   for (unsigned i = 0; i < Query.Types.size(); ++i) {
297     auto Action = getAspectAction({Query.Opcode, i, Query.Types[i]});
298     if (Action.first != Legal) {
299       DEBUG(dbgs() << ".. (legacy) Type " << i << " Action="
300                    << (unsigned)Action.first << ", " << Action.second << "\n");
301       return {Action.first, i, Action.second};
302     } else
303       DEBUG(dbgs() << ".. (legacy) Type " << i << " Legal\n");
304   }
305   DEBUG(dbgs() << ".. (legacy) Legal\n");
306   return {Legal, 0, LLT{}};
307 }
308 
309 LegalizeActionStep
310 LegalizerInfo::getAction(const MachineInstr &MI,
311                          const MachineRegisterInfo &MRI) const {
312   SmallVector<LLT, 2> Types;
313   SmallBitVector SeenTypes(8);
314   const MCOperandInfo *OpInfo = MI.getDesc().OpInfo;
315   // FIXME: probably we'll need to cache the results here somehow?
316   for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
317     if (!OpInfo[i].isGenericType())
318       continue;
319 
320     // We must only record actions once for each TypeIdx; otherwise we'd
321     // try to legalize operands multiple times down the line.
322     unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
323     if (SeenTypes[TypeIdx])
324       continue;
325 
326     SeenTypes.set(TypeIdx);
327 
328     LLT Ty = getTypeFromTypeIdx(MI, MRI, i, TypeIdx);
329     Types.push_back(Ty);
330   }
331   return getAction({MI.getOpcode(), Types});
332 }
333 
334 bool LegalizerInfo::isLegal(const MachineInstr &MI,
335                             const MachineRegisterInfo &MRI) const {
336   return getAction(MI, MRI).Action == Legal;
337 }
338 
339 bool LegalizerInfo::legalizeCustom(MachineInstr &MI, MachineRegisterInfo &MRI,
340                                    MachineIRBuilder &MIRBuilder) const {
341   return false;
342 }
343 
344 LegalizerInfo::SizeAndActionsVec
345 LegalizerInfo::increaseToLargerTypesAndDecreaseToLargest(
346     const SizeAndActionsVec &v, LegalizeAction IncreaseAction,
347     LegalizeAction DecreaseAction) {
348   SizeAndActionsVec result;
349   unsigned LargestSizeSoFar = 0;
350   if (v.size() >= 1 && v[0].first != 1)
351     result.push_back({1, IncreaseAction});
352   for (size_t i = 0; i < v.size(); ++i) {
353     result.push_back(v[i]);
354     LargestSizeSoFar = v[i].first;
355     if (i + 1 < v.size() && v[i + 1].first != v[i].first + 1) {
356       result.push_back({LargestSizeSoFar + 1, IncreaseAction});
357       LargestSizeSoFar = v[i].first + 1;
358     }
359   }
360   result.push_back({LargestSizeSoFar + 1, DecreaseAction});
361   return result;
362 }
363 
364 LegalizerInfo::SizeAndActionsVec
365 LegalizerInfo::decreaseToSmallerTypesAndIncreaseToSmallest(
366     const SizeAndActionsVec &v, LegalizeAction DecreaseAction,
367     LegalizeAction IncreaseAction) {
368   SizeAndActionsVec result;
369   if (v.size() == 0 || v[0].first != 1)
370     result.push_back({1, IncreaseAction});
371   for (size_t i = 0; i < v.size(); ++i) {
372     result.push_back(v[i]);
373     if (i + 1 == v.size() || v[i + 1].first != v[i].first + 1) {
374       result.push_back({v[i].first + 1, DecreaseAction});
375     }
376   }
377   return result;
378 }
379 
380 LegalizerInfo::SizeAndAction
381 LegalizerInfo::findAction(const SizeAndActionsVec &Vec, const uint32_t Size) {
382   assert(Size >= 1);
383   // Find the last element in Vec that has a bitsize equal to or smaller than
384   // the requested bit size.
385   // That is the element just before the first element that is bigger than Size.
386   auto VecIt = std::upper_bound(
387       Vec.begin(), Vec.end(), Size,
388       [](const uint32_t Size, const SizeAndAction lhs) -> bool {
389         return Size < lhs.first;
390       });
391   assert(VecIt != Vec.begin() && "Does Vec not start with size 1?");
392   --VecIt;
393   int VecIdx = VecIt - Vec.begin();
394 
395   LegalizeAction Action = Vec[VecIdx].second;
396   switch (Action) {
397   case Legal:
398   case Lower:
399   case Libcall:
400   case Custom:
401     return {Size, Action};
402   case FewerElements:
403     // FIXME: is this special case still needed and correct?
404     // Special case for scalarization:
405     if (Vec == SizeAndActionsVec({{1, FewerElements}}))
406       return {1, FewerElements};
407     LLVM_FALLTHROUGH;
408   case NarrowScalar: {
409     // The following needs to be a loop, as for now, we do allow needing to
410     // go over "Unsupported" bit sizes before finding a legalizable bit size.
411     // e.g. (s8, WidenScalar), (s9, Unsupported), (s32, Legal). if Size==8,
412     // we need to iterate over s9, and then to s32 to return (s32, Legal).
413     // If we want to get rid of the below loop, we should have stronger asserts
414     // when building the SizeAndActionsVecs, probably not allowing
415     // "Unsupported" unless at the ends of the vector.
416     for (int i = VecIdx - 1; i >= 0; --i)
417       if (!needsLegalizingToDifferentSize(Vec[i].second) &&
418           Vec[i].second != Unsupported)
419         return {Vec[i].first, Action};
420     llvm_unreachable("");
421   }
422   case WidenScalar:
423   case MoreElements: {
424     // See above, the following needs to be a loop, at least for now.
425     for (std::size_t i = VecIdx + 1; i < Vec.size(); ++i)
426       if (!needsLegalizingToDifferentSize(Vec[i].second) &&
427           Vec[i].second != Unsupported)
428         return {Vec[i].first, Action};
429     llvm_unreachable("");
430   }
431   case Unsupported:
432     return {Size, Unsupported};
433   case NotFound:
434   case UseLegacyRules:
435     llvm_unreachable("NotFound");
436   }
437   llvm_unreachable("Action has an unknown enum value");
438 }
439 
440 std::pair<LegalizeAction, LLT>
441 LegalizerInfo::findScalarLegalAction(const InstrAspect &Aspect) const {
442   assert(Aspect.Type.isScalar() || Aspect.Type.isPointer());
443   if (Aspect.Opcode < FirstOp || Aspect.Opcode > LastOp)
444     return {NotFound, LLT()};
445   const unsigned OpcodeIdx = getOpcodeIdxForOpcode(Aspect.Opcode);
446   if (Aspect.Type.isPointer() &&
447       AddrSpace2PointerActions[OpcodeIdx].find(Aspect.Type.getAddressSpace()) ==
448           AddrSpace2PointerActions[OpcodeIdx].end()) {
449     return {NotFound, LLT()};
450   }
451   const SmallVector<SizeAndActionsVec, 1> &Actions =
452       Aspect.Type.isPointer()
453           ? AddrSpace2PointerActions[OpcodeIdx]
454                 .find(Aspect.Type.getAddressSpace())
455                 ->second
456           : ScalarActions[OpcodeIdx];
457   if (Aspect.Idx >= Actions.size())
458     return {NotFound, LLT()};
459   const SizeAndActionsVec &Vec = Actions[Aspect.Idx];
460   // FIXME: speed up this search, e.g. by using a results cache for repeated
461   // queries?
462   auto SizeAndAction = findAction(Vec, Aspect.Type.getSizeInBits());
463   return {SizeAndAction.second,
464           Aspect.Type.isScalar() ? LLT::scalar(SizeAndAction.first)
465                                  : LLT::pointer(Aspect.Type.getAddressSpace(),
466                                                 SizeAndAction.first)};
467 }
468 
469 std::pair<LegalizeAction, LLT>
470 LegalizerInfo::findVectorLegalAction(const InstrAspect &Aspect) const {
471   assert(Aspect.Type.isVector());
472   // First legalize the vector element size, then legalize the number of
473   // lanes in the vector.
474   if (Aspect.Opcode < FirstOp || Aspect.Opcode > LastOp)
475     return {NotFound, Aspect.Type};
476   const unsigned OpcodeIdx = getOpcodeIdxForOpcode(Aspect.Opcode);
477   const unsigned TypeIdx = Aspect.Idx;
478   if (TypeIdx >= ScalarInVectorActions[OpcodeIdx].size())
479     return {NotFound, Aspect.Type};
480   const SizeAndActionsVec &ElemSizeVec =
481       ScalarInVectorActions[OpcodeIdx][TypeIdx];
482 
483   LLT IntermediateType;
484   auto ElementSizeAndAction =
485       findAction(ElemSizeVec, Aspect.Type.getScalarSizeInBits());
486   IntermediateType =
487       LLT::vector(Aspect.Type.getNumElements(), ElementSizeAndAction.first);
488   if (ElementSizeAndAction.second != Legal)
489     return {ElementSizeAndAction.second, IntermediateType};
490 
491   auto i = NumElements2Actions[OpcodeIdx].find(
492       IntermediateType.getScalarSizeInBits());
493   if (i == NumElements2Actions[OpcodeIdx].end()) {
494     return {NotFound, IntermediateType};
495   }
496   const SizeAndActionsVec &NumElementsVec = (*i).second[TypeIdx];
497   auto NumElementsAndAction =
498       findAction(NumElementsVec, IntermediateType.getNumElements());
499   return {NumElementsAndAction.second,
500           LLT::vector(NumElementsAndAction.first,
501                       IntermediateType.getScalarSizeInBits())};
502 }
503 
504 #ifndef NDEBUG
505 // FIXME: This should be in the MachineVerifier, but it can't use the
506 // LegalizerInfo as it's currently in the separate GlobalISel library.
507 // Note that RegBankSelected property already checked in the verifier
508 // has the same layering problem, but we only use inline methods so
509 // end up not needing to link against the GlobalISel library.
510 const MachineInstr *llvm::machineFunctionIsIllegal(const MachineFunction &MF) {
511   if (const LegalizerInfo *MLI = MF.getSubtarget().getLegalizerInfo()) {
512     const MachineRegisterInfo &MRI = MF.getRegInfo();
513     for (const MachineBasicBlock &MBB : MF)
514       for (const MachineInstr &MI : MBB)
515         if (isPreISelGenericOpcode(MI.getOpcode()) && !MLI->isLegal(MI, MRI))
516 	  return &MI;
517   }
518   return nullptr;
519 }
520 #endif
521