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