xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/GlobalISelEmitter.cpp (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This tablegen backend emits code for use by the GlobalISel instruction
11 /// selector. See include/llvm/CodeGen/TargetGlobalISel.td.
12 ///
13 /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen
14 /// backend, filters out the ones that are unsupported, maps
15 /// SelectionDAG-specific constructs to their GlobalISel counterpart
16 /// (when applicable: MVT to LLT;  SDNode to generic Instruction).
17 ///
18 /// Not all patterns are supported: pass the tablegen invocation
19 /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped,
20 /// as well as why.
21 ///
22 /// The generated file defines a single method:
23 ///     bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const;
24 /// intended to be used in InstructionSelector::select as the first-step
25 /// selector for the patterns that don't require complex C++.
26 ///
27 /// FIXME: We'll probably want to eventually define a base
28 /// "TargetGenInstructionSelector" class.
29 ///
30 //===----------------------------------------------------------------------===//
31 
32 #include "CodeGenDAGPatterns.h"
33 #include "CodeGenInstruction.h"
34 #include "SubtargetFeatureInfo.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/Support/CodeGenCoverage.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/Error.h"
39 #include "llvm/Support/LowLevelTypeImpl.h"
40 #include "llvm/Support/MachineValueType.h"
41 #include "llvm/Support/ScopedPrinter.h"
42 #include "llvm/TableGen/Error.h"
43 #include "llvm/TableGen/Record.h"
44 #include "llvm/TableGen/TableGenBackend.h"
45 #include <numeric>
46 #include <string>
47 using namespace llvm;
48 
49 #define DEBUG_TYPE "gisel-emitter"
50 
51 STATISTIC(NumPatternTotal, "Total number of patterns");
52 STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG");
53 STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped");
54 STATISTIC(NumPatternsTested, "Number of patterns executed according to coverage information");
55 STATISTIC(NumPatternEmitted, "Number of patterns emitted");
56 
57 cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel");
58 
59 static cl::opt<bool> WarnOnSkippedPatterns(
60     "warn-on-skipped-patterns",
61     cl::desc("Explain why a pattern was skipped for inclusion "
62              "in the GlobalISel selector"),
63     cl::init(false), cl::cat(GlobalISelEmitterCat));
64 
65 static cl::opt<bool> GenerateCoverage(
66     "instrument-gisel-coverage",
67     cl::desc("Generate coverage instrumentation for GlobalISel"),
68     cl::init(false), cl::cat(GlobalISelEmitterCat));
69 
70 static cl::opt<std::string> UseCoverageFile(
71     "gisel-coverage-file", cl::init(""),
72     cl::desc("Specify file to retrieve coverage information from"),
73     cl::cat(GlobalISelEmitterCat));
74 
75 static cl::opt<bool> OptimizeMatchTable(
76     "optimize-match-table",
77     cl::desc("Generate an optimized version of the match table"),
78     cl::init(true), cl::cat(GlobalISelEmitterCat));
79 
80 namespace {
81 //===- Helper functions ---------------------------------------------------===//
82 
83 /// Get the name of the enum value used to number the predicate function.
84 std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) {
85   if (Predicate.hasGISelPredicateCode())
86     return "GIPFP_MI_" + Predicate.getFnName();
87   return "GIPFP_" + Predicate.getImmTypeIdentifier().str() + "_" +
88          Predicate.getFnName();
89 }
90 
91 /// Get the opcode used to check this predicate.
92 std::string getMatchOpcodeForImmPredicate(const TreePredicateFn &Predicate) {
93   return "GIM_Check" + Predicate.getImmTypeIdentifier().str() + "ImmPredicate";
94 }
95 
96 /// This class stands in for LLT wherever we want to tablegen-erate an
97 /// equivalent at compiler run-time.
98 class LLTCodeGen {
99 private:
100   LLT Ty;
101 
102 public:
103   LLTCodeGen() = default;
104   LLTCodeGen(const LLT &Ty) : Ty(Ty) {}
105 
106   std::string getCxxEnumValue() const {
107     std::string Str;
108     raw_string_ostream OS(Str);
109 
110     emitCxxEnumValue(OS);
111     return Str;
112   }
113 
114   void emitCxxEnumValue(raw_ostream &OS) const {
115     if (Ty.isScalar()) {
116       OS << "GILLT_s" << Ty.getSizeInBits();
117       return;
118     }
119     if (Ty.isVector()) {
120       OS << (Ty.isScalable() ? "GILLT_nxv" : "GILLT_v")
121          << Ty.getElementCount().getKnownMinValue() << "s"
122          << Ty.getScalarSizeInBits();
123       return;
124     }
125     if (Ty.isPointer()) {
126       OS << "GILLT_p" << Ty.getAddressSpace();
127       if (Ty.getSizeInBits() > 0)
128         OS << "s" << Ty.getSizeInBits();
129       return;
130     }
131     llvm_unreachable("Unhandled LLT");
132   }
133 
134   void emitCxxConstructorCall(raw_ostream &OS) const {
135     if (Ty.isScalar()) {
136       OS << "LLT::scalar(" << Ty.getSizeInBits() << ")";
137       return;
138     }
139     if (Ty.isVector()) {
140       OS << "LLT::vector("
141          << (Ty.isScalable() ? "ElementCount::getScalable("
142                              : "ElementCount::getFixed(")
143          << Ty.getElementCount().getKnownMinValue() << "), "
144          << Ty.getScalarSizeInBits() << ")";
145       return;
146     }
147     if (Ty.isPointer() && Ty.getSizeInBits() > 0) {
148       OS << "LLT::pointer(" << Ty.getAddressSpace() << ", "
149          << Ty.getSizeInBits() << ")";
150       return;
151     }
152     llvm_unreachable("Unhandled LLT");
153   }
154 
155   const LLT &get() const { return Ty; }
156 
157   /// This ordering is used for std::unique() and llvm::sort(). There's no
158   /// particular logic behind the order but either A < B or B < A must be
159   /// true if A != B.
160   bool operator<(const LLTCodeGen &Other) const {
161     if (Ty.isValid() != Other.Ty.isValid())
162       return Ty.isValid() < Other.Ty.isValid();
163     if (!Ty.isValid())
164       return false;
165 
166     if (Ty.isVector() != Other.Ty.isVector())
167       return Ty.isVector() < Other.Ty.isVector();
168     if (Ty.isScalar() != Other.Ty.isScalar())
169       return Ty.isScalar() < Other.Ty.isScalar();
170     if (Ty.isPointer() != Other.Ty.isPointer())
171       return Ty.isPointer() < Other.Ty.isPointer();
172 
173     if (Ty.isPointer() && Ty.getAddressSpace() != Other.Ty.getAddressSpace())
174       return Ty.getAddressSpace() < Other.Ty.getAddressSpace();
175 
176     if (Ty.isVector() && Ty.getElementCount() != Other.Ty.getElementCount())
177       return std::make_tuple(Ty.isScalable(),
178                              Ty.getElementCount().getKnownMinValue()) <
179              std::make_tuple(Other.Ty.isScalable(),
180                              Other.Ty.getElementCount().getKnownMinValue());
181 
182     assert((!Ty.isVector() || Ty.isScalable() == Other.Ty.isScalable()) &&
183            "Unexpected mismatch of scalable property");
184     return Ty.isVector()
185                ? std::make_tuple(Ty.isScalable(),
186                                  Ty.getSizeInBits().getKnownMinValue()) <
187                      std::make_tuple(
188                          Other.Ty.isScalable(),
189                          Other.Ty.getSizeInBits().getKnownMinValue())
190                : Ty.getSizeInBits().getFixedValue() <
191                      Other.Ty.getSizeInBits().getFixedValue();
192   }
193 
194   bool operator==(const LLTCodeGen &B) const { return Ty == B.Ty; }
195 };
196 
197 // Track all types that are used so we can emit the corresponding enum.
198 std::set<LLTCodeGen> KnownTypes;
199 
200 class InstructionMatcher;
201 /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for
202 /// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...).
203 static std::optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) {
204   MVT VT(SVT);
205 
206   if (VT.isVector() && !VT.getVectorElementCount().isScalar())
207     return LLTCodeGen(
208         LLT::vector(VT.getVectorElementCount(), VT.getScalarSizeInBits()));
209 
210   if (VT.isInteger() || VT.isFloatingPoint())
211     return LLTCodeGen(LLT::scalar(VT.getSizeInBits()));
212 
213   return std::nullopt;
214 }
215 
216 static std::string explainPredicates(const TreePatternNode *N) {
217   std::string Explanation;
218   StringRef Separator = "";
219   for (const TreePredicateCall &Call : N->getPredicateCalls()) {
220     const TreePredicateFn &P = Call.Fn;
221     Explanation +=
222         (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str();
223     Separator = ", ";
224 
225     if (P.isAlwaysTrue())
226       Explanation += " always-true";
227     if (P.isImmediatePattern())
228       Explanation += " immediate";
229 
230     if (P.isUnindexed())
231       Explanation += " unindexed";
232 
233     if (P.isNonExtLoad())
234       Explanation += " non-extload";
235     if (P.isAnyExtLoad())
236       Explanation += " extload";
237     if (P.isSignExtLoad())
238       Explanation += " sextload";
239     if (P.isZeroExtLoad())
240       Explanation += " zextload";
241 
242     if (P.isNonTruncStore())
243       Explanation += " non-truncstore";
244     if (P.isTruncStore())
245       Explanation += " truncstore";
246 
247     if (Record *VT = P.getMemoryVT())
248       Explanation += (" MemVT=" + VT->getName()).str();
249     if (Record *VT = P.getScalarMemoryVT())
250       Explanation += (" ScalarVT(MemVT)=" + VT->getName()).str();
251 
252     if (ListInit *AddrSpaces = P.getAddressSpaces()) {
253       raw_string_ostream OS(Explanation);
254       OS << " AddressSpaces=[";
255 
256       StringRef AddrSpaceSeparator;
257       for (Init *Val : AddrSpaces->getValues()) {
258         IntInit *IntVal = dyn_cast<IntInit>(Val);
259         if (!IntVal)
260           continue;
261 
262         OS << AddrSpaceSeparator << IntVal->getValue();
263         AddrSpaceSeparator = ", ";
264       }
265 
266       OS << ']';
267     }
268 
269     int64_t MinAlign = P.getMinAlignment();
270     if (MinAlign > 0)
271       Explanation += " MinAlign=" + utostr(MinAlign);
272 
273     if (P.isAtomicOrderingMonotonic())
274       Explanation += " monotonic";
275     if (P.isAtomicOrderingAcquire())
276       Explanation += " acquire";
277     if (P.isAtomicOrderingRelease())
278       Explanation += " release";
279     if (P.isAtomicOrderingAcquireRelease())
280       Explanation += " acq_rel";
281     if (P.isAtomicOrderingSequentiallyConsistent())
282       Explanation += " seq_cst";
283     if (P.isAtomicOrderingAcquireOrStronger())
284       Explanation += " >=acquire";
285     if (P.isAtomicOrderingWeakerThanAcquire())
286       Explanation += " <acquire";
287     if (P.isAtomicOrderingReleaseOrStronger())
288       Explanation += " >=release";
289     if (P.isAtomicOrderingWeakerThanRelease())
290       Explanation += " <release";
291   }
292   return Explanation;
293 }
294 
295 std::string explainOperator(Record *Operator) {
296   if (Operator->isSubClassOf("SDNode"))
297     return (" (" + Operator->getValueAsString("Opcode") + ")").str();
298 
299   if (Operator->isSubClassOf("Intrinsic"))
300     return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str();
301 
302   if (Operator->isSubClassOf("ComplexPattern"))
303     return (" (Operator is an unmapped ComplexPattern, " + Operator->getName() +
304             ")")
305         .str();
306 
307   if (Operator->isSubClassOf("SDNodeXForm"))
308     return (" (Operator is an unmapped SDNodeXForm, " + Operator->getName() +
309             ")")
310         .str();
311 
312   return (" (Operator " + Operator->getName() + " not understood)").str();
313 }
314 
315 /// Helper function to let the emitter report skip reason error messages.
316 static Error failedImport(const Twine &Reason) {
317   return make_error<StringError>(Reason, inconvertibleErrorCode());
318 }
319 
320 static Error isTrivialOperatorNode(const TreePatternNode *N) {
321   std::string Explanation;
322   std::string Separator;
323 
324   bool HasUnsupportedPredicate = false;
325   for (const TreePredicateCall &Call : N->getPredicateCalls()) {
326     const TreePredicateFn &Predicate = Call.Fn;
327 
328     if (Predicate.isAlwaysTrue())
329       continue;
330 
331     if (Predicate.isImmediatePattern())
332       continue;
333 
334     if (Predicate.hasNoUse())
335       continue;
336 
337     if (Predicate.isNonExtLoad() || Predicate.isAnyExtLoad() ||
338         Predicate.isSignExtLoad() || Predicate.isZeroExtLoad())
339       continue;
340 
341     if (Predicate.isNonTruncStore() || Predicate.isTruncStore())
342       continue;
343 
344     if (Predicate.isLoad() && Predicate.getMemoryVT())
345       continue;
346 
347     if (Predicate.isLoad() || Predicate.isStore()) {
348       if (Predicate.isUnindexed())
349         continue;
350     }
351 
352     if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) {
353       const ListInit *AddrSpaces = Predicate.getAddressSpaces();
354       if (AddrSpaces && !AddrSpaces->empty())
355         continue;
356 
357       if (Predicate.getMinAlignment() > 0)
358         continue;
359     }
360 
361     if (Predicate.isAtomic() && Predicate.getMemoryVT())
362       continue;
363 
364     if (Predicate.isAtomic() &&
365         (Predicate.isAtomicOrderingMonotonic() ||
366          Predicate.isAtomicOrderingAcquire() ||
367          Predicate.isAtomicOrderingRelease() ||
368          Predicate.isAtomicOrderingAcquireRelease() ||
369          Predicate.isAtomicOrderingSequentiallyConsistent() ||
370          Predicate.isAtomicOrderingAcquireOrStronger() ||
371          Predicate.isAtomicOrderingWeakerThanAcquire() ||
372          Predicate.isAtomicOrderingReleaseOrStronger() ||
373          Predicate.isAtomicOrderingWeakerThanRelease()))
374       continue;
375 
376     if (Predicate.hasGISelPredicateCode())
377       continue;
378 
379     HasUnsupportedPredicate = true;
380     Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")";
381     Separator = ", ";
382     Explanation += (Separator + "first-failing:" +
383                     Predicate.getOrigPatFragRecord()->getRecord()->getName())
384                        .str();
385     break;
386   }
387 
388   if (!HasUnsupportedPredicate)
389     return Error::success();
390 
391   return failedImport(Explanation);
392 }
393 
394 static Record *getInitValueAsRegClass(Init *V) {
395   if (DefInit *VDefInit = dyn_cast<DefInit>(V)) {
396     if (VDefInit->getDef()->isSubClassOf("RegisterOperand"))
397       return VDefInit->getDef()->getValueAsDef("RegClass");
398     if (VDefInit->getDef()->isSubClassOf("RegisterClass"))
399       return VDefInit->getDef();
400   }
401   return nullptr;
402 }
403 
404 std::string
405 getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) {
406   std::string Name = "GIFBS";
407   for (const auto &Feature : FeatureBitset)
408     Name += ("_" + Feature->getName()).str();
409   return Name;
410 }
411 
412 static std::string getScopedName(unsigned Scope, const std::string &Name) {
413   return ("pred:" + Twine(Scope) + ":" + Name).str();
414 }
415 
416 //===- MatchTable Helpers -------------------------------------------------===//
417 
418 class MatchTable;
419 
420 /// A record to be stored in a MatchTable.
421 ///
422 /// This class represents any and all output that may be required to emit the
423 /// MatchTable. Instances  are most often configured to represent an opcode or
424 /// value that will be emitted to the table with some formatting but it can also
425 /// represent commas, comments, and other formatting instructions.
426 struct MatchTableRecord {
427   enum RecordFlagsBits {
428     MTRF_None = 0x0,
429     /// Causes EmitStr to be formatted as comment when emitted.
430     MTRF_Comment = 0x1,
431     /// Causes the record value to be followed by a comma when emitted.
432     MTRF_CommaFollows = 0x2,
433     /// Causes the record value to be followed by a line break when emitted.
434     MTRF_LineBreakFollows = 0x4,
435     /// Indicates that the record defines a label and causes an additional
436     /// comment to be emitted containing the index of the label.
437     MTRF_Label = 0x8,
438     /// Causes the record to be emitted as the index of the label specified by
439     /// LabelID along with a comment indicating where that label is.
440     MTRF_JumpTarget = 0x10,
441     /// Causes the formatter to add a level of indentation before emitting the
442     /// record.
443     MTRF_Indent = 0x20,
444     /// Causes the formatter to remove a level of indentation after emitting the
445     /// record.
446     MTRF_Outdent = 0x40,
447   };
448 
449   /// When MTRF_Label or MTRF_JumpTarget is used, indicates a label id to
450   /// reference or define.
451   unsigned LabelID;
452   /// The string to emit. Depending on the MTRF_* flags it may be a comment, a
453   /// value, a label name.
454   std::string EmitStr;
455 
456 private:
457   /// The number of MatchTable elements described by this record. Comments are 0
458   /// while values are typically 1. Values >1 may occur when we need to emit
459   /// values that exceed the size of a MatchTable element.
460   unsigned NumElements;
461 
462 public:
463   /// A bitfield of RecordFlagsBits flags.
464   unsigned Flags;
465 
466   /// The actual run-time value, if known
467   int64_t RawValue;
468 
469   MatchTableRecord(std::optional<unsigned> LabelID_, StringRef EmitStr,
470                    unsigned NumElements, unsigned Flags,
471                    int64_t RawValue = std::numeric_limits<int64_t>::min())
472       : LabelID(LabelID_.value_or(~0u)), EmitStr(EmitStr),
473         NumElements(NumElements), Flags(Flags), RawValue(RawValue) {
474     assert((!LabelID_ || LabelID != ~0u) &&
475            "This value is reserved for non-labels");
476   }
477   MatchTableRecord(const MatchTableRecord &Other) = default;
478   MatchTableRecord(MatchTableRecord &&Other) = default;
479 
480   /// Useful if a Match Table Record gets optimized out
481   void turnIntoComment() {
482     Flags |= MTRF_Comment;
483     Flags &= ~MTRF_CommaFollows;
484     NumElements = 0;
485   }
486 
487   /// For Jump Table generation purposes
488   bool operator<(const MatchTableRecord &Other) const {
489     return RawValue < Other.RawValue;
490   }
491   int64_t getRawValue() const { return RawValue; }
492 
493   void emit(raw_ostream &OS, bool LineBreakNextAfterThis,
494             const MatchTable &Table) const;
495   unsigned size() const { return NumElements; }
496 };
497 
498 class Matcher;
499 
500 /// Holds the contents of a generated MatchTable to enable formatting and the
501 /// necessary index tracking needed to support GIM_Try.
502 class MatchTable {
503   /// An unique identifier for the table. The generated table will be named
504   /// MatchTable${ID}.
505   unsigned ID;
506   /// The records that make up the table. Also includes comments describing the
507   /// values being emitted and line breaks to format it.
508   std::vector<MatchTableRecord> Contents;
509   /// The currently defined labels.
510   DenseMap<unsigned, unsigned> LabelMap;
511   /// Tracks the sum of MatchTableRecord::NumElements as the table is built.
512   unsigned CurrentSize = 0;
513   /// A unique identifier for a MatchTable label.
514   unsigned CurrentLabelID = 0;
515   /// Determines if the table should be instrumented for rule coverage tracking.
516   bool IsWithCoverage;
517 
518 public:
519   static MatchTableRecord LineBreak;
520   static MatchTableRecord Comment(StringRef Comment) {
521     return MatchTableRecord(std::nullopt, Comment, 0,
522                             MatchTableRecord::MTRF_Comment);
523   }
524   static MatchTableRecord Opcode(StringRef Opcode, int IndentAdjust = 0) {
525     unsigned ExtraFlags = 0;
526     if (IndentAdjust > 0)
527       ExtraFlags |= MatchTableRecord::MTRF_Indent;
528     if (IndentAdjust < 0)
529       ExtraFlags |= MatchTableRecord::MTRF_Outdent;
530 
531     return MatchTableRecord(std::nullopt, Opcode, 1,
532                             MatchTableRecord::MTRF_CommaFollows | ExtraFlags);
533   }
534   static MatchTableRecord NamedValue(StringRef NamedValue) {
535     return MatchTableRecord(std::nullopt, NamedValue, 1,
536                             MatchTableRecord::MTRF_CommaFollows);
537   }
538   static MatchTableRecord NamedValue(StringRef NamedValue, int64_t RawValue) {
539     return MatchTableRecord(std::nullopt, NamedValue, 1,
540                             MatchTableRecord::MTRF_CommaFollows, RawValue);
541   }
542   static MatchTableRecord NamedValue(StringRef Namespace,
543                                      StringRef NamedValue) {
544     return MatchTableRecord(std::nullopt, (Namespace + "::" + NamedValue).str(),
545                             1, MatchTableRecord::MTRF_CommaFollows);
546   }
547   static MatchTableRecord NamedValue(StringRef Namespace, StringRef NamedValue,
548                                      int64_t RawValue) {
549     return MatchTableRecord(std::nullopt, (Namespace + "::" + NamedValue).str(),
550                             1, MatchTableRecord::MTRF_CommaFollows, RawValue);
551   }
552   static MatchTableRecord IntValue(int64_t IntValue) {
553     return MatchTableRecord(std::nullopt, llvm::to_string(IntValue), 1,
554                             MatchTableRecord::MTRF_CommaFollows);
555   }
556   static MatchTableRecord Label(unsigned LabelID) {
557     return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 0,
558                             MatchTableRecord::MTRF_Label |
559                                 MatchTableRecord::MTRF_Comment |
560                                 MatchTableRecord::MTRF_LineBreakFollows);
561   }
562   static MatchTableRecord JumpTarget(unsigned LabelID) {
563     return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 1,
564                             MatchTableRecord::MTRF_JumpTarget |
565                                 MatchTableRecord::MTRF_Comment |
566                                 MatchTableRecord::MTRF_CommaFollows);
567   }
568 
569   static MatchTable buildTable(ArrayRef<Matcher *> Rules, bool WithCoverage);
570 
571   MatchTable(bool WithCoverage, unsigned ID = 0)
572       : ID(ID), IsWithCoverage(WithCoverage) {}
573 
574   bool isWithCoverage() const { return IsWithCoverage; }
575 
576   void push_back(const MatchTableRecord &Value) {
577     if (Value.Flags & MatchTableRecord::MTRF_Label)
578       defineLabel(Value.LabelID);
579     Contents.push_back(Value);
580     CurrentSize += Value.size();
581   }
582 
583   unsigned allocateLabelID() { return CurrentLabelID++; }
584 
585   void defineLabel(unsigned LabelID) {
586     LabelMap.insert(std::make_pair(LabelID, CurrentSize));
587   }
588 
589   unsigned getLabelIndex(unsigned LabelID) const {
590     const auto I = LabelMap.find(LabelID);
591     assert(I != LabelMap.end() && "Use of undeclared label");
592     return I->second;
593   }
594 
595   void emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; }
596 
597   void emitDeclaration(raw_ostream &OS) const {
598     unsigned Indentation = 4;
599     OS << "  constexpr static int64_t MatchTable" << ID << "[] = {";
600     LineBreak.emit(OS, true, *this);
601     OS << std::string(Indentation, ' ');
602 
603     for (auto I = Contents.begin(), E = Contents.end(); I != E;
604          ++I) {
605       bool LineBreakIsNext = false;
606       const auto &NextI = std::next(I);
607 
608       if (NextI != E) {
609         if (NextI->EmitStr == "" &&
610             NextI->Flags == MatchTableRecord::MTRF_LineBreakFollows)
611           LineBreakIsNext = true;
612       }
613 
614       if (I->Flags & MatchTableRecord::MTRF_Indent)
615         Indentation += 2;
616 
617       I->emit(OS, LineBreakIsNext, *this);
618       if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows)
619         OS << std::string(Indentation, ' ');
620 
621       if (I->Flags & MatchTableRecord::MTRF_Outdent)
622         Indentation -= 2;
623     }
624     OS << "};\n";
625   }
626 };
627 
628 MatchTableRecord MatchTable::LineBreak = {
629     std::nullopt, "" /* Emit String */, 0 /* Elements */,
630     MatchTableRecord::MTRF_LineBreakFollows};
631 
632 void MatchTableRecord::emit(raw_ostream &OS, bool LineBreakIsNextAfterThis,
633                             const MatchTable &Table) const {
634   bool UseLineComment =
635       LineBreakIsNextAfterThis || (Flags & MTRF_LineBreakFollows);
636   if (Flags & (MTRF_JumpTarget | MTRF_CommaFollows))
637     UseLineComment = false;
638 
639   if (Flags & MTRF_Comment)
640     OS << (UseLineComment ? "// " : "/*");
641 
642   OS << EmitStr;
643   if (Flags & MTRF_Label)
644     OS << ": @" << Table.getLabelIndex(LabelID);
645 
646   if ((Flags & MTRF_Comment) && !UseLineComment)
647     OS << "*/";
648 
649   if (Flags & MTRF_JumpTarget) {
650     if (Flags & MTRF_Comment)
651       OS << " ";
652     OS << Table.getLabelIndex(LabelID);
653   }
654 
655   if (Flags & MTRF_CommaFollows) {
656     OS << ",";
657     if (!LineBreakIsNextAfterThis && !(Flags & MTRF_LineBreakFollows))
658       OS << " ";
659   }
660 
661   if (Flags & MTRF_LineBreakFollows)
662     OS << "\n";
663 }
664 
665 MatchTable &operator<<(MatchTable &Table, const MatchTableRecord &Value) {
666   Table.push_back(Value);
667   return Table;
668 }
669 
670 //===- Matchers -----------------------------------------------------------===//
671 
672 class OperandMatcher;
673 class MatchAction;
674 class PredicateMatcher;
675 
676 class Matcher {
677 public:
678   virtual ~Matcher() = default;
679   virtual void optimize() {}
680   virtual void emit(MatchTable &Table) = 0;
681 
682   virtual bool hasFirstCondition() const = 0;
683   virtual const PredicateMatcher &getFirstCondition() const = 0;
684   virtual std::unique_ptr<PredicateMatcher> popFirstCondition() = 0;
685 };
686 
687 MatchTable MatchTable::buildTable(ArrayRef<Matcher *> Rules,
688                                   bool WithCoverage) {
689   MatchTable Table(WithCoverage);
690   for (Matcher *Rule : Rules)
691     Rule->emit(Table);
692 
693   return Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak;
694 }
695 
696 class GroupMatcher final : public Matcher {
697   /// Conditions that form a common prefix of all the matchers contained.
698   SmallVector<std::unique_ptr<PredicateMatcher>, 1> Conditions;
699 
700   /// All the nested matchers, sharing a common prefix.
701   std::vector<Matcher *> Matchers;
702 
703   /// An owning collection for any auxiliary matchers created while optimizing
704   /// nested matchers contained.
705   std::vector<std::unique_ptr<Matcher>> MatcherStorage;
706 
707 public:
708   /// Add a matcher to the collection of nested matchers if it meets the
709   /// requirements, and return true. If it doesn't, do nothing and return false.
710   ///
711   /// Expected to preserve its argument, so it could be moved out later on.
712   bool addMatcher(Matcher &Candidate);
713 
714   /// Mark the matcher as fully-built and ensure any invariants expected by both
715   /// optimize() and emit(...) methods. Generally, both sequences of calls
716   /// are expected to lead to a sensible result:
717   ///
718   /// addMatcher(...)*; finalize(); optimize(); emit(...); and
719   /// addMatcher(...)*; finalize(); emit(...);
720   ///
721   /// or generally
722   ///
723   /// addMatcher(...)*; finalize(); { optimize()*; emit(...); }*
724   ///
725   /// Multiple calls to optimize() are expected to be handled gracefully, though
726   /// optimize() is not expected to be idempotent. Multiple calls to finalize()
727   /// aren't generally supported. emit(...) is expected to be non-mutating and
728   /// producing the exact same results upon repeated calls.
729   ///
730   /// addMatcher() calls after the finalize() call are not supported.
731   ///
732   /// finalize() and optimize() are both allowed to mutate the contained
733   /// matchers, so moving them out after finalize() is not supported.
734   void finalize();
735   void optimize() override;
736   void emit(MatchTable &Table) override;
737 
738   /// Could be used to move out the matchers added previously, unless finalize()
739   /// has been already called. If any of the matchers are moved out, the group
740   /// becomes safe to destroy, but not safe to re-use for anything else.
741   iterator_range<std::vector<Matcher *>::iterator> matchers() {
742     return make_range(Matchers.begin(), Matchers.end());
743   }
744   size_t size() const { return Matchers.size(); }
745   bool empty() const { return Matchers.empty(); }
746 
747   std::unique_ptr<PredicateMatcher> popFirstCondition() override {
748     assert(!Conditions.empty() &&
749            "Trying to pop a condition from a condition-less group");
750     std::unique_ptr<PredicateMatcher> P = std::move(Conditions.front());
751     Conditions.erase(Conditions.begin());
752     return P;
753   }
754   const PredicateMatcher &getFirstCondition() const override {
755     assert(!Conditions.empty() &&
756            "Trying to get a condition from a condition-less group");
757     return *Conditions.front();
758   }
759   bool hasFirstCondition() const override { return !Conditions.empty(); }
760 
761 private:
762   /// See if a candidate matcher could be added to this group solely by
763   /// analyzing its first condition.
764   bool candidateConditionMatches(const PredicateMatcher &Predicate) const;
765 };
766 
767 class SwitchMatcher : public Matcher {
768   /// All the nested matchers, representing distinct switch-cases. The first
769   /// conditions (as Matcher::getFirstCondition() reports) of all the nested
770   /// matchers must share the same type and path to a value they check, in other
771   /// words, be isIdenticalDownToValue, but have different values they check
772   /// against.
773   std::vector<Matcher *> Matchers;
774 
775   /// The representative condition, with a type and a path (InsnVarID and OpIdx
776   /// in most cases)  shared by all the matchers contained.
777   std::unique_ptr<PredicateMatcher> Condition = nullptr;
778 
779   /// Temporary set used to check that the case values don't repeat within the
780   /// same switch.
781   std::set<MatchTableRecord> Values;
782 
783   /// An owning collection for any auxiliary matchers created while optimizing
784   /// nested matchers contained.
785   std::vector<std::unique_ptr<Matcher>> MatcherStorage;
786 
787 public:
788   bool addMatcher(Matcher &Candidate);
789 
790   void finalize();
791   void emit(MatchTable &Table) override;
792 
793   iterator_range<std::vector<Matcher *>::iterator> matchers() {
794     return make_range(Matchers.begin(), Matchers.end());
795   }
796   size_t size() const { return Matchers.size(); }
797   bool empty() const { return Matchers.empty(); }
798 
799   std::unique_ptr<PredicateMatcher> popFirstCondition() override {
800     // SwitchMatcher doesn't have a common first condition for its cases, as all
801     // the cases only share a kind of a value (a type and a path to it) they
802     // match, but deliberately differ in the actual value they match.
803     llvm_unreachable("Trying to pop a condition from a condition-less group");
804   }
805   const PredicateMatcher &getFirstCondition() const override {
806     llvm_unreachable("Trying to pop a condition from a condition-less group");
807   }
808   bool hasFirstCondition() const override { return false; }
809 
810 private:
811   /// See if the predicate type has a Switch-implementation for it.
812   static bool isSupportedPredicateType(const PredicateMatcher &Predicate);
813 
814   bool candidateConditionMatches(const PredicateMatcher &Predicate) const;
815 
816   /// emit()-helper
817   static void emitPredicateSpecificOpcodes(const PredicateMatcher &P,
818                                            MatchTable &Table);
819 };
820 
821 /// Generates code to check that a match rule matches.
822 class RuleMatcher : public Matcher {
823 public:
824   using ActionList = std::list<std::unique_ptr<MatchAction>>;
825   using action_iterator = ActionList::iterator;
826 
827 protected:
828   /// A list of matchers that all need to succeed for the current rule to match.
829   /// FIXME: This currently supports a single match position but could be
830   /// extended to support multiple positions to support div/rem fusion or
831   /// load-multiple instructions.
832   using MatchersTy = std::vector<std::unique_ptr<InstructionMatcher>> ;
833   MatchersTy Matchers;
834 
835   /// A list of actions that need to be taken when all predicates in this rule
836   /// have succeeded.
837   ActionList Actions;
838 
839   using DefinedInsnVariablesMap = std::map<InstructionMatcher *, unsigned>;
840 
841   /// A map of instruction matchers to the local variables
842   DefinedInsnVariablesMap InsnVariableIDs;
843 
844   using MutatableInsnSet = SmallPtrSet<InstructionMatcher *, 4>;
845 
846   // The set of instruction matchers that have not yet been claimed for mutation
847   // by a BuildMI.
848   MutatableInsnSet MutatableInsns;
849 
850   /// A map of named operands defined by the matchers that may be referenced by
851   /// the renderers.
852   StringMap<OperandMatcher *> DefinedOperands;
853 
854   /// A map of anonymous physical register operands defined by the matchers that
855   /// may be referenced by the renderers.
856   DenseMap<Record *, OperandMatcher *> PhysRegOperands;
857 
858   /// ID for the next instruction variable defined with implicitlyDefineInsnVar()
859   unsigned NextInsnVarID;
860 
861   /// ID for the next output instruction allocated with allocateOutputInsnID()
862   unsigned NextOutputInsnID;
863 
864   /// ID for the next temporary register ID allocated with allocateTempRegID()
865   unsigned NextTempRegID;
866 
867   std::vector<Record *> RequiredFeatures;
868   std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers;
869 
870   ArrayRef<SMLoc> SrcLoc;
871 
872   typedef std::tuple<Record *, unsigned, unsigned>
873       DefinedComplexPatternSubOperand;
874   typedef StringMap<DefinedComplexPatternSubOperand>
875       DefinedComplexPatternSubOperandMap;
876   /// A map of Symbolic Names to ComplexPattern sub-operands.
877   DefinedComplexPatternSubOperandMap ComplexSubOperands;
878   /// A map used to for multiple referenced error check of ComplexSubOperand.
879   /// ComplexSubOperand can't be referenced multiple from different operands,
880   /// however multiple references from same operand are allowed since that is
881   /// how 'same operand checks' are generated.
882   StringMap<std::string> ComplexSubOperandsParentName;
883 
884   uint64_t RuleID;
885   static uint64_t NextRuleID;
886 
887 public:
888   RuleMatcher(ArrayRef<SMLoc> SrcLoc)
889       : NextInsnVarID(0), NextOutputInsnID(0), NextTempRegID(0), SrcLoc(SrcLoc),
890         RuleID(NextRuleID++) {}
891   RuleMatcher(RuleMatcher &&Other) = default;
892   RuleMatcher &operator=(RuleMatcher &&Other) = default;
893 
894   uint64_t getRuleID() const { return RuleID; }
895 
896   InstructionMatcher &addInstructionMatcher(StringRef SymbolicName);
897   void addRequiredFeature(Record *Feature);
898   const std::vector<Record *> &getRequiredFeatures() const;
899 
900   template <class Kind, class... Args> Kind &addAction(Args &&... args);
901   template <class Kind, class... Args>
902   action_iterator insertAction(action_iterator InsertPt, Args &&... args);
903 
904   /// Define an instruction without emitting any code to do so.
905   unsigned implicitlyDefineInsnVar(InstructionMatcher &Matcher);
906 
907   unsigned getInsnVarID(InstructionMatcher &InsnMatcher) const;
908   DefinedInsnVariablesMap::const_iterator defined_insn_vars_begin() const {
909     return InsnVariableIDs.begin();
910   }
911   DefinedInsnVariablesMap::const_iterator defined_insn_vars_end() const {
912     return InsnVariableIDs.end();
913   }
914   iterator_range<typename DefinedInsnVariablesMap::const_iterator>
915   defined_insn_vars() const {
916     return make_range(defined_insn_vars_begin(), defined_insn_vars_end());
917   }
918 
919   MutatableInsnSet::const_iterator mutatable_insns_begin() const {
920     return MutatableInsns.begin();
921   }
922   MutatableInsnSet::const_iterator mutatable_insns_end() const {
923     return MutatableInsns.end();
924   }
925   iterator_range<typename MutatableInsnSet::const_iterator>
926   mutatable_insns() const {
927     return make_range(mutatable_insns_begin(), mutatable_insns_end());
928   }
929   void reserveInsnMatcherForMutation(InstructionMatcher *InsnMatcher) {
930     bool R = MutatableInsns.erase(InsnMatcher);
931     assert(R && "Reserving a mutatable insn that isn't available");
932     (void)R;
933   }
934 
935   action_iterator actions_begin() { return Actions.begin(); }
936   action_iterator actions_end() { return Actions.end(); }
937   iterator_range<action_iterator> actions() {
938     return make_range(actions_begin(), actions_end());
939   }
940 
941   void defineOperand(StringRef SymbolicName, OperandMatcher &OM);
942 
943   void definePhysRegOperand(Record *Reg, OperandMatcher &OM);
944 
945   Error defineComplexSubOperand(StringRef SymbolicName, Record *ComplexPattern,
946                                 unsigned RendererID, unsigned SubOperandID,
947                                 StringRef ParentSymbolicName) {
948     std::string ParentName(ParentSymbolicName);
949     if (ComplexSubOperands.count(SymbolicName)) {
950       const std::string &RecordedParentName =
951           ComplexSubOperandsParentName[SymbolicName];
952       if (RecordedParentName != ParentName)
953         return failedImport("Error: Complex suboperand " + SymbolicName +
954                             " referenced by different operands: " +
955                             RecordedParentName + " and " + ParentName + ".");
956       // Complex suboperand referenced more than once from same the operand is
957       // used to generate 'same operand check'. Emitting of
958       // GIR_ComplexSubOperandRenderer for them is already handled.
959       return Error::success();
960     }
961 
962     ComplexSubOperands[SymbolicName] =
963         std::make_tuple(ComplexPattern, RendererID, SubOperandID);
964     ComplexSubOperandsParentName[SymbolicName] = ParentName;
965 
966     return Error::success();
967   }
968 
969   std::optional<DefinedComplexPatternSubOperand>
970   getComplexSubOperand(StringRef SymbolicName) const {
971     const auto &I = ComplexSubOperands.find(SymbolicName);
972     if (I == ComplexSubOperands.end())
973       return std::nullopt;
974     return I->second;
975   }
976 
977   InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const;
978   const OperandMatcher &getOperandMatcher(StringRef Name) const;
979   const OperandMatcher &getPhysRegOperandMatcher(Record *) const;
980 
981   void optimize() override;
982   void emit(MatchTable &Table) override;
983 
984   /// Compare the priority of this object and B.
985   ///
986   /// Returns true if this object is more important than B.
987   bool isHigherPriorityThan(const RuleMatcher &B) const;
988 
989   /// Report the maximum number of temporary operands needed by the rule
990   /// matcher.
991   unsigned countRendererFns() const;
992 
993   std::unique_ptr<PredicateMatcher> popFirstCondition() override;
994   const PredicateMatcher &getFirstCondition() const override;
995   LLTCodeGen getFirstConditionAsRootType();
996   bool hasFirstCondition() const override;
997   unsigned getNumOperands() const;
998   StringRef getOpcode() const;
999 
1000   // FIXME: Remove this as soon as possible
1001   InstructionMatcher &insnmatchers_front() const { return *Matchers.front(); }
1002 
1003   unsigned allocateOutputInsnID() { return NextOutputInsnID++; }
1004   unsigned allocateTempRegID() { return NextTempRegID++; }
1005 
1006   iterator_range<MatchersTy::iterator> insnmatchers() {
1007     return make_range(Matchers.begin(), Matchers.end());
1008   }
1009   bool insnmatchers_empty() const { return Matchers.empty(); }
1010   void insnmatchers_pop_front() { Matchers.erase(Matchers.begin()); }
1011 };
1012 
1013 uint64_t RuleMatcher::NextRuleID = 0;
1014 
1015 using action_iterator = RuleMatcher::action_iterator;
1016 
1017 template <class PredicateTy> class PredicateListMatcher {
1018 private:
1019   /// Template instantiations should specialize this to return a string to use
1020   /// for the comment emitted when there are no predicates.
1021   std::string getNoPredicateComment() const;
1022 
1023 protected:
1024   using PredicatesTy = std::deque<std::unique_ptr<PredicateTy>>;
1025   PredicatesTy Predicates;
1026 
1027   /// Track if the list of predicates was manipulated by one of the optimization
1028   /// methods.
1029   bool Optimized = false;
1030 
1031 public:
1032   typename PredicatesTy::iterator predicates_begin() {
1033     return Predicates.begin();
1034   }
1035   typename PredicatesTy::iterator predicates_end() {
1036     return Predicates.end();
1037   }
1038   iterator_range<typename PredicatesTy::iterator> predicates() {
1039     return make_range(predicates_begin(), predicates_end());
1040   }
1041   typename PredicatesTy::size_type predicates_size() const {
1042     return Predicates.size();
1043   }
1044   bool predicates_empty() const { return Predicates.empty(); }
1045 
1046   std::unique_ptr<PredicateTy> predicates_pop_front() {
1047     std::unique_ptr<PredicateTy> Front = std::move(Predicates.front());
1048     Predicates.pop_front();
1049     Optimized = true;
1050     return Front;
1051   }
1052 
1053   void prependPredicate(std::unique_ptr<PredicateTy> &&Predicate) {
1054     Predicates.push_front(std::move(Predicate));
1055   }
1056 
1057   void eraseNullPredicates() {
1058     const auto NewEnd =
1059         std::stable_partition(Predicates.begin(), Predicates.end(),
1060                               std::logical_not<std::unique_ptr<PredicateTy>>());
1061     if (NewEnd != Predicates.begin()) {
1062       Predicates.erase(Predicates.begin(), NewEnd);
1063       Optimized = true;
1064     }
1065   }
1066 
1067   /// Emit MatchTable opcodes that tests whether all the predicates are met.
1068   template <class... Args>
1069   void emitPredicateListOpcodes(MatchTable &Table, Args &&... args) {
1070     if (Predicates.empty() && !Optimized) {
1071       Table << MatchTable::Comment(getNoPredicateComment())
1072             << MatchTable::LineBreak;
1073       return;
1074     }
1075 
1076     for (const auto &Predicate : predicates())
1077       Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...);
1078   }
1079 
1080   /// Provide a function to avoid emitting certain predicates. This is used to
1081   /// defer some predicate checks until after others
1082   using PredicateFilterFunc = std::function<bool(const PredicateTy&)>;
1083 
1084   /// Emit MatchTable opcodes for predicates which satisfy \p
1085   /// ShouldEmitPredicate. This should be called multiple times to ensure all
1086   /// predicates are eventually added to the match table.
1087   template <class... Args>
1088   void emitFilteredPredicateListOpcodes(PredicateFilterFunc ShouldEmitPredicate,
1089                                         MatchTable &Table, Args &&... args) {
1090     if (Predicates.empty() && !Optimized) {
1091       Table << MatchTable::Comment(getNoPredicateComment())
1092             << MatchTable::LineBreak;
1093       return;
1094     }
1095 
1096     for (const auto &Predicate : predicates()) {
1097       if (ShouldEmitPredicate(*Predicate))
1098         Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...);
1099     }
1100   }
1101 };
1102 
1103 class PredicateMatcher {
1104 public:
1105   /// This enum is used for RTTI and also defines the priority that is given to
1106   /// the predicate when generating the matcher code. Kinds with higher priority
1107   /// must be tested first.
1108   ///
1109   /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
1110   /// but OPM_Int must have priority over OPM_RegBank since constant integers
1111   /// are represented by a virtual register defined by a G_CONSTANT instruction.
1112   ///
1113   /// Note: The relative priority between IPM_ and OPM_ does not matter, they
1114   /// are currently not compared between each other.
1115   enum PredicateKind {
1116     IPM_Opcode,
1117     IPM_NumOperands,
1118     IPM_ImmPredicate,
1119     IPM_Imm,
1120     IPM_AtomicOrderingMMO,
1121     IPM_MemoryLLTSize,
1122     IPM_MemoryVsLLTSize,
1123     IPM_MemoryAddressSpace,
1124     IPM_MemoryAlignment,
1125     IPM_VectorSplatImm,
1126     IPM_NoUse,
1127     IPM_GenericPredicate,
1128     OPM_SameOperand,
1129     OPM_ComplexPattern,
1130     OPM_IntrinsicID,
1131     OPM_CmpPredicate,
1132     OPM_Instruction,
1133     OPM_Int,
1134     OPM_LiteralInt,
1135     OPM_LLT,
1136     OPM_PointerToAny,
1137     OPM_RegBank,
1138     OPM_MBB,
1139     OPM_RecordNamedOperand,
1140   };
1141 
1142 protected:
1143   PredicateKind Kind;
1144   unsigned InsnVarID;
1145   unsigned OpIdx;
1146 
1147 public:
1148   PredicateMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx = ~0)
1149       : Kind(Kind), InsnVarID(InsnVarID), OpIdx(OpIdx) {}
1150 
1151   unsigned getInsnVarID() const { return InsnVarID; }
1152   unsigned getOpIdx() const { return OpIdx; }
1153 
1154   virtual ~PredicateMatcher() = default;
1155   /// Emit MatchTable opcodes that check the predicate for the given operand.
1156   virtual void emitPredicateOpcodes(MatchTable &Table,
1157                                     RuleMatcher &Rule) const = 0;
1158 
1159   PredicateKind getKind() const { return Kind; }
1160 
1161   bool dependsOnOperands() const {
1162     // Custom predicates really depend on the context pattern of the
1163     // instruction, not just the individual instruction. This therefore
1164     // implicitly depends on all other pattern constraints.
1165     return Kind == IPM_GenericPredicate;
1166   }
1167 
1168   virtual bool isIdentical(const PredicateMatcher &B) const {
1169     return B.getKind() == getKind() && InsnVarID == B.InsnVarID &&
1170            OpIdx == B.OpIdx;
1171   }
1172 
1173   virtual bool isIdenticalDownToValue(const PredicateMatcher &B) const {
1174     return hasValue() && PredicateMatcher::isIdentical(B);
1175   }
1176 
1177   virtual MatchTableRecord getValue() const {
1178     assert(hasValue() && "Can not get a value of a value-less predicate!");
1179     llvm_unreachable("Not implemented yet");
1180   }
1181   virtual bool hasValue() const { return false; }
1182 
1183   /// Report the maximum number of temporary operands needed by the predicate
1184   /// matcher.
1185   virtual unsigned countRendererFns() const { return 0; }
1186 };
1187 
1188 /// Generates code to check a predicate of an operand.
1189 ///
1190 /// Typical predicates include:
1191 /// * Operand is a particular register.
1192 /// * Operand is assigned a particular register bank.
1193 /// * Operand is an MBB.
1194 class OperandPredicateMatcher : public PredicateMatcher {
1195 public:
1196   OperandPredicateMatcher(PredicateKind Kind, unsigned InsnVarID,
1197                           unsigned OpIdx)
1198       : PredicateMatcher(Kind, InsnVarID, OpIdx) {}
1199   virtual ~OperandPredicateMatcher() {}
1200 
1201   /// Compare the priority of this object and B.
1202   ///
1203   /// Returns true if this object is more important than B.
1204   virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const;
1205 };
1206 
1207 template <>
1208 std::string
1209 PredicateListMatcher<OperandPredicateMatcher>::getNoPredicateComment() const {
1210   return "No operand predicates";
1211 }
1212 
1213 /// Generates code to check that a register operand is defined by the same exact
1214 /// one as another.
1215 class SameOperandMatcher : public OperandPredicateMatcher {
1216   std::string MatchingName;
1217   unsigned OrigOpIdx;
1218 
1219 public:
1220   SameOperandMatcher(unsigned InsnVarID, unsigned OpIdx, StringRef MatchingName,
1221                      unsigned OrigOpIdx)
1222       : OperandPredicateMatcher(OPM_SameOperand, InsnVarID, OpIdx),
1223         MatchingName(MatchingName), OrigOpIdx(OrigOpIdx) {}
1224 
1225   static bool classof(const PredicateMatcher *P) {
1226     return P->getKind() == OPM_SameOperand;
1227   }
1228 
1229   void emitPredicateOpcodes(MatchTable &Table,
1230                             RuleMatcher &Rule) const override;
1231 
1232   bool isIdentical(const PredicateMatcher &B) const override {
1233     return OperandPredicateMatcher::isIdentical(B) &&
1234            OrigOpIdx == cast<SameOperandMatcher>(&B)->OrigOpIdx &&
1235            MatchingName == cast<SameOperandMatcher>(&B)->MatchingName;
1236   }
1237 };
1238 
1239 /// Generates code to check that an operand is a particular LLT.
1240 class LLTOperandMatcher : public OperandPredicateMatcher {
1241 protected:
1242   LLTCodeGen Ty;
1243 
1244 public:
1245   static std::map<LLTCodeGen, unsigned> TypeIDValues;
1246 
1247   static void initTypeIDValuesMap() {
1248     TypeIDValues.clear();
1249 
1250     unsigned ID = 0;
1251     for (const LLTCodeGen &LLTy : KnownTypes)
1252       TypeIDValues[LLTy] = ID++;
1253   }
1254 
1255   LLTOperandMatcher(unsigned InsnVarID, unsigned OpIdx, const LLTCodeGen &Ty)
1256       : OperandPredicateMatcher(OPM_LLT, InsnVarID, OpIdx), Ty(Ty) {
1257     KnownTypes.insert(Ty);
1258   }
1259 
1260   static bool classof(const PredicateMatcher *P) {
1261     return P->getKind() == OPM_LLT;
1262   }
1263   bool isIdentical(const PredicateMatcher &B) const override {
1264     return OperandPredicateMatcher::isIdentical(B) &&
1265            Ty == cast<LLTOperandMatcher>(&B)->Ty;
1266   }
1267   MatchTableRecord getValue() const override {
1268     const auto VI = TypeIDValues.find(Ty);
1269     if (VI == TypeIDValues.end())
1270       return MatchTable::NamedValue(getTy().getCxxEnumValue());
1271     return MatchTable::NamedValue(getTy().getCxxEnumValue(), VI->second);
1272   }
1273   bool hasValue() const override {
1274     if (TypeIDValues.size() != KnownTypes.size())
1275       initTypeIDValuesMap();
1276     return TypeIDValues.count(Ty);
1277   }
1278 
1279   LLTCodeGen getTy() const { return Ty; }
1280 
1281   void emitPredicateOpcodes(MatchTable &Table,
1282                             RuleMatcher &Rule) const override {
1283     Table << MatchTable::Opcode("GIM_CheckType") << MatchTable::Comment("MI")
1284           << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op")
1285           << MatchTable::IntValue(OpIdx) << MatchTable::Comment("Type")
1286           << getValue() << MatchTable::LineBreak;
1287   }
1288 };
1289 
1290 std::map<LLTCodeGen, unsigned> LLTOperandMatcher::TypeIDValues;
1291 
1292 /// Generates code to check that an operand is a pointer to any address space.
1293 ///
1294 /// In SelectionDAG, the types did not describe pointers or address spaces. As a
1295 /// result, iN is used to describe a pointer of N bits to any address space and
1296 /// PatFrag predicates are typically used to constrain the address space. There's
1297 /// no reliable means to derive the missing type information from the pattern so
1298 /// imported rules must test the components of a pointer separately.
1299 ///
1300 /// If SizeInBits is zero, then the pointer size will be obtained from the
1301 /// subtarget.
1302 class PointerToAnyOperandMatcher : public OperandPredicateMatcher {
1303 protected:
1304   unsigned SizeInBits;
1305 
1306 public:
1307   PointerToAnyOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1308                              unsigned SizeInBits)
1309       : OperandPredicateMatcher(OPM_PointerToAny, InsnVarID, OpIdx),
1310         SizeInBits(SizeInBits) {}
1311 
1312   static bool classof(const PredicateMatcher *P) {
1313     return P->getKind() == OPM_PointerToAny;
1314   }
1315 
1316   bool isIdentical(const PredicateMatcher &B) const override {
1317     return OperandPredicateMatcher::isIdentical(B) &&
1318            SizeInBits == cast<PointerToAnyOperandMatcher>(&B)->SizeInBits;
1319   }
1320 
1321   void emitPredicateOpcodes(MatchTable &Table,
1322                             RuleMatcher &Rule) const override {
1323     Table << MatchTable::Opcode("GIM_CheckPointerToAny")
1324           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1325           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1326           << MatchTable::Comment("SizeInBits")
1327           << MatchTable::IntValue(SizeInBits) << MatchTable::LineBreak;
1328   }
1329 };
1330 
1331 /// Generates code to record named operand in RecordedOperands list at StoreIdx.
1332 /// Predicates with 'let PredicateCodeUsesOperands = 1' get RecordedOperands as
1333 /// an argument to predicate's c++ code once all operands have been matched.
1334 class RecordNamedOperandMatcher : public OperandPredicateMatcher {
1335 protected:
1336   unsigned StoreIdx;
1337   std::string Name;
1338 
1339 public:
1340   RecordNamedOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1341                             unsigned StoreIdx, StringRef Name)
1342       : OperandPredicateMatcher(OPM_RecordNamedOperand, InsnVarID, OpIdx),
1343         StoreIdx(StoreIdx), Name(Name) {}
1344 
1345   static bool classof(const PredicateMatcher *P) {
1346     return P->getKind() == OPM_RecordNamedOperand;
1347   }
1348 
1349   bool isIdentical(const PredicateMatcher &B) const override {
1350     return OperandPredicateMatcher::isIdentical(B) &&
1351            StoreIdx == cast<RecordNamedOperandMatcher>(&B)->StoreIdx &&
1352            Name == cast<RecordNamedOperandMatcher>(&B)->Name;
1353   }
1354 
1355   void emitPredicateOpcodes(MatchTable &Table,
1356                             RuleMatcher &Rule) const override {
1357     Table << MatchTable::Opcode("GIM_RecordNamedOperand")
1358           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1359           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1360           << MatchTable::Comment("StoreIdx") << MatchTable::IntValue(StoreIdx)
1361           << MatchTable::Comment("Name : " + Name) << MatchTable::LineBreak;
1362   }
1363 };
1364 
1365 /// Generates code to check that an operand is a particular target constant.
1366 class ComplexPatternOperandMatcher : public OperandPredicateMatcher {
1367 protected:
1368   const OperandMatcher &Operand;
1369   const Record &TheDef;
1370 
1371   unsigned getAllocatedTemporariesBaseID() const;
1372 
1373 public:
1374   bool isIdentical(const PredicateMatcher &B) const override { return false; }
1375 
1376   ComplexPatternOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1377                                const OperandMatcher &Operand,
1378                                const Record &TheDef)
1379       : OperandPredicateMatcher(OPM_ComplexPattern, InsnVarID, OpIdx),
1380         Operand(Operand), TheDef(TheDef) {}
1381 
1382   static bool classof(const PredicateMatcher *P) {
1383     return P->getKind() == OPM_ComplexPattern;
1384   }
1385 
1386   void emitPredicateOpcodes(MatchTable &Table,
1387                             RuleMatcher &Rule) const override {
1388     unsigned ID = getAllocatedTemporariesBaseID();
1389     Table << MatchTable::Opcode("GIM_CheckComplexPattern")
1390           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1391           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1392           << MatchTable::Comment("Renderer") << MatchTable::IntValue(ID)
1393           << MatchTable::NamedValue(("GICP_" + TheDef.getName()).str())
1394           << MatchTable::LineBreak;
1395   }
1396 
1397   unsigned countRendererFns() const override {
1398     return 1;
1399   }
1400 };
1401 
1402 /// Generates code to check that an operand is in a particular register bank.
1403 class RegisterBankOperandMatcher : public OperandPredicateMatcher {
1404 protected:
1405   const CodeGenRegisterClass &RC;
1406 
1407 public:
1408   RegisterBankOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1409                              const CodeGenRegisterClass &RC)
1410       : OperandPredicateMatcher(OPM_RegBank, InsnVarID, OpIdx), RC(RC) {}
1411 
1412   bool isIdentical(const PredicateMatcher &B) const override {
1413     return OperandPredicateMatcher::isIdentical(B) &&
1414            RC.getDef() == cast<RegisterBankOperandMatcher>(&B)->RC.getDef();
1415   }
1416 
1417   static bool classof(const PredicateMatcher *P) {
1418     return P->getKind() == OPM_RegBank;
1419   }
1420 
1421   void emitPredicateOpcodes(MatchTable &Table,
1422                             RuleMatcher &Rule) const override {
1423     Table << MatchTable::Opcode("GIM_CheckRegBankForClass")
1424           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1425           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1426           << MatchTable::Comment("RC")
1427           << MatchTable::NamedValue(RC.getQualifiedName() + "RegClassID")
1428           << MatchTable::LineBreak;
1429   }
1430 };
1431 
1432 /// Generates code to check that an operand is a basic block.
1433 class MBBOperandMatcher : public OperandPredicateMatcher {
1434 public:
1435   MBBOperandMatcher(unsigned InsnVarID, unsigned OpIdx)
1436       : OperandPredicateMatcher(OPM_MBB, InsnVarID, OpIdx) {}
1437 
1438   static bool classof(const PredicateMatcher *P) {
1439     return P->getKind() == OPM_MBB;
1440   }
1441 
1442   void emitPredicateOpcodes(MatchTable &Table,
1443                             RuleMatcher &Rule) const override {
1444     Table << MatchTable::Opcode("GIM_CheckIsMBB") << MatchTable::Comment("MI")
1445           << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op")
1446           << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak;
1447   }
1448 };
1449 
1450 class ImmOperandMatcher : public OperandPredicateMatcher {
1451 public:
1452   ImmOperandMatcher(unsigned InsnVarID, unsigned OpIdx)
1453       : OperandPredicateMatcher(IPM_Imm, InsnVarID, OpIdx) {}
1454 
1455   static bool classof(const PredicateMatcher *P) {
1456     return P->getKind() == IPM_Imm;
1457   }
1458 
1459   void emitPredicateOpcodes(MatchTable &Table,
1460                             RuleMatcher &Rule) const override {
1461     Table << MatchTable::Opcode("GIM_CheckIsImm") << MatchTable::Comment("MI")
1462           << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op")
1463           << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak;
1464   }
1465 };
1466 
1467 /// Generates code to check that an operand is a G_CONSTANT with a particular
1468 /// int.
1469 class ConstantIntOperandMatcher : public OperandPredicateMatcher {
1470 protected:
1471   int64_t Value;
1472 
1473 public:
1474   ConstantIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value)
1475       : OperandPredicateMatcher(OPM_Int, InsnVarID, OpIdx), Value(Value) {}
1476 
1477   bool isIdentical(const PredicateMatcher &B) const override {
1478     return OperandPredicateMatcher::isIdentical(B) &&
1479            Value == cast<ConstantIntOperandMatcher>(&B)->Value;
1480   }
1481 
1482   static bool classof(const PredicateMatcher *P) {
1483     return P->getKind() == OPM_Int;
1484   }
1485 
1486   void emitPredicateOpcodes(MatchTable &Table,
1487                             RuleMatcher &Rule) const override {
1488     Table << MatchTable::Opcode("GIM_CheckConstantInt")
1489           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1490           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1491           << MatchTable::IntValue(Value) << MatchTable::LineBreak;
1492   }
1493 };
1494 
1495 /// Generates code to check that an operand is a raw int (where MO.isImm() or
1496 /// MO.isCImm() is true).
1497 class LiteralIntOperandMatcher : public OperandPredicateMatcher {
1498 protected:
1499   int64_t Value;
1500 
1501 public:
1502   LiteralIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value)
1503       : OperandPredicateMatcher(OPM_LiteralInt, InsnVarID, OpIdx),
1504         Value(Value) {}
1505 
1506   bool isIdentical(const PredicateMatcher &B) const override {
1507     return OperandPredicateMatcher::isIdentical(B) &&
1508            Value == cast<LiteralIntOperandMatcher>(&B)->Value;
1509   }
1510 
1511   static bool classof(const PredicateMatcher *P) {
1512     return P->getKind() == OPM_LiteralInt;
1513   }
1514 
1515   void emitPredicateOpcodes(MatchTable &Table,
1516                             RuleMatcher &Rule) const override {
1517     Table << MatchTable::Opcode("GIM_CheckLiteralInt")
1518           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1519           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1520           << MatchTable::IntValue(Value) << MatchTable::LineBreak;
1521   }
1522 };
1523 
1524 /// Generates code to check that an operand is an CmpInst predicate
1525 class CmpPredicateOperandMatcher : public OperandPredicateMatcher {
1526 protected:
1527   std::string PredName;
1528 
1529 public:
1530   CmpPredicateOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1531                              std::string P)
1532     : OperandPredicateMatcher(OPM_CmpPredicate, InsnVarID, OpIdx), PredName(P) {}
1533 
1534   bool isIdentical(const PredicateMatcher &B) const override {
1535     return OperandPredicateMatcher::isIdentical(B) &&
1536            PredName == cast<CmpPredicateOperandMatcher>(&B)->PredName;
1537   }
1538 
1539   static bool classof(const PredicateMatcher *P) {
1540     return P->getKind() == OPM_CmpPredicate;
1541   }
1542 
1543   void emitPredicateOpcodes(MatchTable &Table,
1544                             RuleMatcher &Rule) const override {
1545     Table << MatchTable::Opcode("GIM_CheckCmpPredicate")
1546           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1547           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1548           << MatchTable::Comment("Predicate")
1549           << MatchTable::NamedValue("CmpInst", PredName)
1550           << MatchTable::LineBreak;
1551   }
1552 };
1553 
1554 /// Generates code to check that an operand is an intrinsic ID.
1555 class IntrinsicIDOperandMatcher : public OperandPredicateMatcher {
1556 protected:
1557   const CodeGenIntrinsic *II;
1558 
1559 public:
1560   IntrinsicIDOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1561                             const CodeGenIntrinsic *II)
1562       : OperandPredicateMatcher(OPM_IntrinsicID, InsnVarID, OpIdx), II(II) {}
1563 
1564   bool isIdentical(const PredicateMatcher &B) const override {
1565     return OperandPredicateMatcher::isIdentical(B) &&
1566            II == cast<IntrinsicIDOperandMatcher>(&B)->II;
1567   }
1568 
1569   static bool classof(const PredicateMatcher *P) {
1570     return P->getKind() == OPM_IntrinsicID;
1571   }
1572 
1573   void emitPredicateOpcodes(MatchTable &Table,
1574                             RuleMatcher &Rule) const override {
1575     Table << MatchTable::Opcode("GIM_CheckIntrinsicID")
1576           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1577           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
1578           << MatchTable::NamedValue("Intrinsic::" + II->EnumName)
1579           << MatchTable::LineBreak;
1580   }
1581 };
1582 
1583 /// Generates code to check that this operand is an immediate whose value meets
1584 /// an immediate predicate.
1585 class OperandImmPredicateMatcher : public OperandPredicateMatcher {
1586 protected:
1587   TreePredicateFn Predicate;
1588 
1589 public:
1590   OperandImmPredicateMatcher(unsigned InsnVarID, unsigned OpIdx,
1591                              const TreePredicateFn &Predicate)
1592       : OperandPredicateMatcher(IPM_ImmPredicate, InsnVarID, OpIdx),
1593         Predicate(Predicate) {}
1594 
1595   bool isIdentical(const PredicateMatcher &B) const override {
1596     return OperandPredicateMatcher::isIdentical(B) &&
1597            Predicate.getOrigPatFragRecord() ==
1598                cast<OperandImmPredicateMatcher>(&B)
1599                    ->Predicate.getOrigPatFragRecord();
1600   }
1601 
1602   static bool classof(const PredicateMatcher *P) {
1603     return P->getKind() == IPM_ImmPredicate;
1604   }
1605 
1606   void emitPredicateOpcodes(MatchTable &Table,
1607                             RuleMatcher &Rule) const override {
1608     Table << MatchTable::Opcode("GIM_CheckImmOperandPredicate")
1609           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1610           << MatchTable::Comment("MO") << MatchTable::IntValue(OpIdx)
1611           << MatchTable::Comment("Predicate")
1612           << MatchTable::NamedValue(getEnumNameForPredicate(Predicate))
1613           << MatchTable::LineBreak;
1614   }
1615 };
1616 
1617 /// Generates code to check that a set of predicates match for a particular
1618 /// operand.
1619 class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> {
1620 protected:
1621   InstructionMatcher &Insn;
1622   unsigned OpIdx;
1623   std::string SymbolicName;
1624 
1625   /// The index of the first temporary variable allocated to this operand. The
1626   /// number of allocated temporaries can be found with
1627   /// countRendererFns().
1628   unsigned AllocatedTemporariesBaseID;
1629 
1630 public:
1631   OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx,
1632                  const std::string &SymbolicName,
1633                  unsigned AllocatedTemporariesBaseID)
1634       : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName),
1635         AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {}
1636 
1637   bool hasSymbolicName() const { return !SymbolicName.empty(); }
1638   StringRef getSymbolicName() const { return SymbolicName; }
1639   void setSymbolicName(StringRef Name) {
1640     assert(SymbolicName.empty() && "Operand already has a symbolic name");
1641     SymbolicName = std::string(Name);
1642   }
1643 
1644   /// Construct a new operand predicate and add it to the matcher.
1645   template <class Kind, class... Args>
1646   std::optional<Kind *> addPredicate(Args &&...args) {
1647     if (isSameAsAnotherOperand())
1648       return std::nullopt;
1649     Predicates.emplace_back(std::make_unique<Kind>(
1650         getInsnVarID(), getOpIdx(), std::forward<Args>(args)...));
1651     return static_cast<Kind *>(Predicates.back().get());
1652   }
1653 
1654   unsigned getOpIdx() const { return OpIdx; }
1655   unsigned getInsnVarID() const;
1656 
1657   std::string getOperandExpr(unsigned InsnVarID) const {
1658     return "State.MIs[" + llvm::to_string(InsnVarID) + "]->getOperand(" +
1659            llvm::to_string(OpIdx) + ")";
1660   }
1661 
1662   InstructionMatcher &getInstructionMatcher() const { return Insn; }
1663 
1664   Error addTypeCheckPredicate(const TypeSetByHwMode &VTy,
1665                               bool OperandIsAPointer);
1666 
1667   /// Emit MatchTable opcodes that test whether the instruction named in
1668   /// InsnVarID matches all the predicates and all the operands.
1669   void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) {
1670     if (!Optimized) {
1671       std::string Comment;
1672       raw_string_ostream CommentOS(Comment);
1673       CommentOS << "MIs[" << getInsnVarID() << "] ";
1674       if (SymbolicName.empty())
1675         CommentOS << "Operand " << OpIdx;
1676       else
1677         CommentOS << SymbolicName;
1678       Table << MatchTable::Comment(Comment) << MatchTable::LineBreak;
1679     }
1680 
1681     emitPredicateListOpcodes(Table, Rule);
1682   }
1683 
1684   /// Compare the priority of this object and B.
1685   ///
1686   /// Returns true if this object is more important than B.
1687   bool isHigherPriorityThan(OperandMatcher &B) {
1688     // Operand matchers involving more predicates have higher priority.
1689     if (predicates_size() > B.predicates_size())
1690       return true;
1691     if (predicates_size() < B.predicates_size())
1692       return false;
1693 
1694     // This assumes that predicates are added in a consistent order.
1695     for (auto &&Predicate : zip(predicates(), B.predicates())) {
1696       if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
1697         return true;
1698       if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
1699         return false;
1700     }
1701 
1702     return false;
1703   };
1704 
1705   /// Report the maximum number of temporary operands needed by the operand
1706   /// matcher.
1707   unsigned countRendererFns() {
1708     return std::accumulate(
1709         predicates().begin(), predicates().end(), 0,
1710         [](unsigned A,
1711            const std::unique_ptr<OperandPredicateMatcher> &Predicate) {
1712           return A + Predicate->countRendererFns();
1713         });
1714   }
1715 
1716   unsigned getAllocatedTemporariesBaseID() const {
1717     return AllocatedTemporariesBaseID;
1718   }
1719 
1720   bool isSameAsAnotherOperand() {
1721     for (const auto &Predicate : predicates())
1722       if (isa<SameOperandMatcher>(Predicate))
1723         return true;
1724     return false;
1725   }
1726 };
1727 
1728 Error OperandMatcher::addTypeCheckPredicate(const TypeSetByHwMode &VTy,
1729                                             bool OperandIsAPointer) {
1730   if (!VTy.isMachineValueType())
1731     return failedImport("unsupported typeset");
1732 
1733   if (VTy.getMachineValueType() == MVT::iPTR && OperandIsAPointer) {
1734     addPredicate<PointerToAnyOperandMatcher>(0);
1735     return Error::success();
1736   }
1737 
1738   auto OpTyOrNone = MVTToLLT(VTy.getMachineValueType().SimpleTy);
1739   if (!OpTyOrNone)
1740     return failedImport("unsupported type");
1741 
1742   if (OperandIsAPointer)
1743     addPredicate<PointerToAnyOperandMatcher>(OpTyOrNone->get().getSizeInBits());
1744   else if (VTy.isPointer())
1745     addPredicate<LLTOperandMatcher>(LLT::pointer(VTy.getPtrAddrSpace(),
1746                                                  OpTyOrNone->get().getSizeInBits()));
1747   else
1748     addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1749   return Error::success();
1750 }
1751 
1752 unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const {
1753   return Operand.getAllocatedTemporariesBaseID();
1754 }
1755 
1756 /// Generates code to check a predicate on an instruction.
1757 ///
1758 /// Typical predicates include:
1759 /// * The opcode of the instruction is a particular value.
1760 /// * The nsw/nuw flag is/isn't set.
1761 class InstructionPredicateMatcher : public PredicateMatcher {
1762 public:
1763   InstructionPredicateMatcher(PredicateKind Kind, unsigned InsnVarID)
1764       : PredicateMatcher(Kind, InsnVarID) {}
1765   virtual ~InstructionPredicateMatcher() {}
1766 
1767   /// Compare the priority of this object and B.
1768   ///
1769   /// Returns true if this object is more important than B.
1770   virtual bool
1771   isHigherPriorityThan(const InstructionPredicateMatcher &B) const {
1772     return Kind < B.Kind;
1773   };
1774 };
1775 
1776 template <>
1777 std::string
1778 PredicateListMatcher<PredicateMatcher>::getNoPredicateComment() const {
1779   return "No instruction predicates";
1780 }
1781 
1782 /// Generates code to check the opcode of an instruction.
1783 class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
1784 protected:
1785   // Allow matching one to several, similar opcodes that share properties. This
1786   // is to handle patterns where one SelectionDAG operation maps to multiple
1787   // GlobalISel ones (e.g. G_BUILD_VECTOR and G_BUILD_VECTOR_TRUNC). The first
1788   // is treated as the canonical opcode.
1789   SmallVector<const CodeGenInstruction *, 2> Insts;
1790 
1791   static DenseMap<const CodeGenInstruction *, unsigned> OpcodeValues;
1792 
1793 
1794   MatchTableRecord getInstValue(const CodeGenInstruction *I) const {
1795     const auto VI = OpcodeValues.find(I);
1796     if (VI != OpcodeValues.end())
1797       return MatchTable::NamedValue(I->Namespace, I->TheDef->getName(),
1798                                     VI->second);
1799     return MatchTable::NamedValue(I->Namespace, I->TheDef->getName());
1800   }
1801 
1802 public:
1803   static void initOpcodeValuesMap(const CodeGenTarget &Target) {
1804     OpcodeValues.clear();
1805 
1806     unsigned OpcodeValue = 0;
1807     for (const CodeGenInstruction *I : Target.getInstructionsByEnumValue())
1808       OpcodeValues[I] = OpcodeValue++;
1809   }
1810 
1811   InstructionOpcodeMatcher(unsigned InsnVarID,
1812                            ArrayRef<const CodeGenInstruction *> I)
1813       : InstructionPredicateMatcher(IPM_Opcode, InsnVarID),
1814         Insts(I.begin(), I.end()) {
1815     assert((Insts.size() == 1 || Insts.size() == 2) &&
1816            "unexpected number of opcode alternatives");
1817   }
1818 
1819   static bool classof(const PredicateMatcher *P) {
1820     return P->getKind() == IPM_Opcode;
1821   }
1822 
1823   bool isIdentical(const PredicateMatcher &B) const override {
1824     return InstructionPredicateMatcher::isIdentical(B) &&
1825            Insts == cast<InstructionOpcodeMatcher>(&B)->Insts;
1826   }
1827 
1828   bool hasValue() const override {
1829     return Insts.size() == 1 && OpcodeValues.count(Insts[0]);
1830   }
1831 
1832   // TODO: This is used for the SwitchMatcher optimization. We should be able to
1833   // return a list of the opcodes to match.
1834   MatchTableRecord getValue() const override {
1835     assert(Insts.size() == 1);
1836 
1837     const CodeGenInstruction *I = Insts[0];
1838     const auto VI = OpcodeValues.find(I);
1839     if (VI != OpcodeValues.end())
1840       return MatchTable::NamedValue(I->Namespace, I->TheDef->getName(),
1841                                     VI->second);
1842     return MatchTable::NamedValue(I->Namespace, I->TheDef->getName());
1843   }
1844 
1845   void emitPredicateOpcodes(MatchTable &Table,
1846                             RuleMatcher &Rule) const override {
1847     StringRef CheckType = Insts.size() == 1 ?
1848                           "GIM_CheckOpcode" : "GIM_CheckOpcodeIsEither";
1849     Table << MatchTable::Opcode(CheckType) << MatchTable::Comment("MI")
1850           << MatchTable::IntValue(InsnVarID);
1851 
1852     for (const CodeGenInstruction *I : Insts)
1853       Table << getInstValue(I);
1854     Table << MatchTable::LineBreak;
1855   }
1856 
1857   /// Compare the priority of this object and B.
1858   ///
1859   /// Returns true if this object is more important than B.
1860   bool
1861   isHigherPriorityThan(const InstructionPredicateMatcher &B) const override {
1862     if (InstructionPredicateMatcher::isHigherPriorityThan(B))
1863       return true;
1864     if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this))
1865       return false;
1866 
1867     // Prioritize opcodes for cosmetic reasons in the generated source. Although
1868     // this is cosmetic at the moment, we may want to drive a similar ordering
1869     // using instruction frequency information to improve compile time.
1870     if (const InstructionOpcodeMatcher *BO =
1871             dyn_cast<InstructionOpcodeMatcher>(&B))
1872       return Insts[0]->TheDef->getName() < BO->Insts[0]->TheDef->getName();
1873 
1874     return false;
1875   };
1876 
1877   bool isConstantInstruction() const {
1878     return Insts.size() == 1 && Insts[0]->TheDef->getName() == "G_CONSTANT";
1879   }
1880 
1881   // The first opcode is the canonical opcode, and later are alternatives.
1882   StringRef getOpcode() const {
1883     return Insts[0]->TheDef->getName();
1884   }
1885 
1886   ArrayRef<const CodeGenInstruction *> getAlternativeOpcodes() {
1887     return Insts;
1888   }
1889 
1890   bool isVariadicNumOperands() const {
1891     // If one is variadic, they all should be.
1892     return Insts[0]->Operands.isVariadic;
1893   }
1894 
1895   StringRef getOperandType(unsigned OpIdx) const {
1896     // Types expected to be uniform for all alternatives.
1897     return Insts[0]->Operands[OpIdx].OperandType;
1898   }
1899 };
1900 
1901 DenseMap<const CodeGenInstruction *, unsigned>
1902     InstructionOpcodeMatcher::OpcodeValues;
1903 
1904 class InstructionNumOperandsMatcher final : public InstructionPredicateMatcher {
1905   unsigned NumOperands = 0;
1906 
1907 public:
1908   InstructionNumOperandsMatcher(unsigned InsnVarID, unsigned NumOperands)
1909       : InstructionPredicateMatcher(IPM_NumOperands, InsnVarID),
1910         NumOperands(NumOperands) {}
1911 
1912   static bool classof(const PredicateMatcher *P) {
1913     return P->getKind() == IPM_NumOperands;
1914   }
1915 
1916   bool isIdentical(const PredicateMatcher &B) const override {
1917     return InstructionPredicateMatcher::isIdentical(B) &&
1918            NumOperands == cast<InstructionNumOperandsMatcher>(&B)->NumOperands;
1919   }
1920 
1921   void emitPredicateOpcodes(MatchTable &Table,
1922                             RuleMatcher &Rule) const override {
1923     Table << MatchTable::Opcode("GIM_CheckNumOperands")
1924           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1925           << MatchTable::Comment("Expected")
1926           << MatchTable::IntValue(NumOperands) << MatchTable::LineBreak;
1927   }
1928 };
1929 
1930 /// Generates code to check that this instruction is a constant whose value
1931 /// meets an immediate predicate.
1932 ///
1933 /// Immediates are slightly odd since they are typically used like an operand
1934 /// but are represented as an operator internally. We typically write simm8:$src
1935 /// in a tablegen pattern, but this is just syntactic sugar for
1936 /// (imm:i32)<<P:Predicate_simm8>>:$imm which more directly describes the nodes
1937 /// that will be matched and the predicate (which is attached to the imm
1938 /// operator) that will be tested. In SelectionDAG this describes a
1939 /// ConstantSDNode whose internal value will be tested using the simm8 predicate.
1940 ///
1941 /// The corresponding GlobalISel representation is %1 = G_CONSTANT iN Value. In
1942 /// this representation, the immediate could be tested with an
1943 /// InstructionMatcher, InstructionOpcodeMatcher, OperandMatcher, and a
1944 /// OperandPredicateMatcher-subclass to check the Value meets the predicate but
1945 /// there are two implementation issues with producing that matcher
1946 /// configuration from the SelectionDAG pattern:
1947 /// * ImmLeaf is a PatFrag whose root is an InstructionMatcher. This means that
1948 ///   were we to sink the immediate predicate to the operand we would have to
1949 ///   have two partial implementations of PatFrag support, one for immediates
1950 ///   and one for non-immediates.
1951 /// * At the point we handle the predicate, the OperandMatcher hasn't been
1952 ///   created yet. If we were to sink the predicate to the OperandMatcher we
1953 ///   would also have to complicate (or duplicate) the code that descends and
1954 ///   creates matchers for the subtree.
1955 /// Overall, it's simpler to handle it in the place it was found.
1956 class InstructionImmPredicateMatcher : public InstructionPredicateMatcher {
1957 protected:
1958   TreePredicateFn Predicate;
1959 
1960 public:
1961   InstructionImmPredicateMatcher(unsigned InsnVarID,
1962                                  const TreePredicateFn &Predicate)
1963       : InstructionPredicateMatcher(IPM_ImmPredicate, InsnVarID),
1964         Predicate(Predicate) {}
1965 
1966   bool isIdentical(const PredicateMatcher &B) const override {
1967     return InstructionPredicateMatcher::isIdentical(B) &&
1968            Predicate.getOrigPatFragRecord() ==
1969                cast<InstructionImmPredicateMatcher>(&B)
1970                    ->Predicate.getOrigPatFragRecord();
1971   }
1972 
1973   static bool classof(const PredicateMatcher *P) {
1974     return P->getKind() == IPM_ImmPredicate;
1975   }
1976 
1977   void emitPredicateOpcodes(MatchTable &Table,
1978                             RuleMatcher &Rule) const override {
1979     Table << MatchTable::Opcode(getMatchOpcodeForImmPredicate(Predicate))
1980           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
1981           << MatchTable::Comment("Predicate")
1982           << MatchTable::NamedValue(getEnumNameForPredicate(Predicate))
1983           << MatchTable::LineBreak;
1984   }
1985 };
1986 
1987 /// Generates code to check that a memory instruction has a atomic ordering
1988 /// MachineMemoryOperand.
1989 class AtomicOrderingMMOPredicateMatcher : public InstructionPredicateMatcher {
1990 public:
1991   enum AOComparator {
1992     AO_Exactly,
1993     AO_OrStronger,
1994     AO_WeakerThan,
1995   };
1996 
1997 protected:
1998   StringRef Order;
1999   AOComparator Comparator;
2000 
2001 public:
2002   AtomicOrderingMMOPredicateMatcher(unsigned InsnVarID, StringRef Order,
2003                                     AOComparator Comparator = AO_Exactly)
2004       : InstructionPredicateMatcher(IPM_AtomicOrderingMMO, InsnVarID),
2005         Order(Order), Comparator(Comparator) {}
2006 
2007   static bool classof(const PredicateMatcher *P) {
2008     return P->getKind() == IPM_AtomicOrderingMMO;
2009   }
2010 
2011   bool isIdentical(const PredicateMatcher &B) const override {
2012     if (!InstructionPredicateMatcher::isIdentical(B))
2013       return false;
2014     const auto &R = *cast<AtomicOrderingMMOPredicateMatcher>(&B);
2015     return Order == R.Order && Comparator == R.Comparator;
2016   }
2017 
2018   void emitPredicateOpcodes(MatchTable &Table,
2019                             RuleMatcher &Rule) const override {
2020     StringRef Opcode = "GIM_CheckAtomicOrdering";
2021 
2022     if (Comparator == AO_OrStronger)
2023       Opcode = "GIM_CheckAtomicOrderingOrStrongerThan";
2024     if (Comparator == AO_WeakerThan)
2025       Opcode = "GIM_CheckAtomicOrderingWeakerThan";
2026 
2027     Table << MatchTable::Opcode(Opcode) << MatchTable::Comment("MI")
2028           << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Order")
2029           << MatchTable::NamedValue(("(int64_t)AtomicOrdering::" + Order).str())
2030           << MatchTable::LineBreak;
2031   }
2032 };
2033 
2034 /// Generates code to check that the size of an MMO is exactly N bytes.
2035 class MemorySizePredicateMatcher : public InstructionPredicateMatcher {
2036 protected:
2037   unsigned MMOIdx;
2038   uint64_t Size;
2039 
2040 public:
2041   MemorySizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, unsigned Size)
2042       : InstructionPredicateMatcher(IPM_MemoryLLTSize, InsnVarID),
2043         MMOIdx(MMOIdx), Size(Size) {}
2044 
2045   static bool classof(const PredicateMatcher *P) {
2046     return P->getKind() == IPM_MemoryLLTSize;
2047   }
2048   bool isIdentical(const PredicateMatcher &B) const override {
2049     return InstructionPredicateMatcher::isIdentical(B) &&
2050            MMOIdx == cast<MemorySizePredicateMatcher>(&B)->MMOIdx &&
2051            Size == cast<MemorySizePredicateMatcher>(&B)->Size;
2052   }
2053 
2054   void emitPredicateOpcodes(MatchTable &Table,
2055                             RuleMatcher &Rule) const override {
2056     Table << MatchTable::Opcode("GIM_CheckMemorySizeEqualTo")
2057           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
2058           << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx)
2059           << MatchTable::Comment("Size") << MatchTable::IntValue(Size)
2060           << MatchTable::LineBreak;
2061   }
2062 };
2063 
2064 class MemoryAddressSpacePredicateMatcher : public InstructionPredicateMatcher {
2065 protected:
2066   unsigned MMOIdx;
2067   SmallVector<unsigned, 4> AddrSpaces;
2068 
2069 public:
2070   MemoryAddressSpacePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx,
2071                                      ArrayRef<unsigned> AddrSpaces)
2072       : InstructionPredicateMatcher(IPM_MemoryAddressSpace, InsnVarID),
2073         MMOIdx(MMOIdx), AddrSpaces(AddrSpaces.begin(), AddrSpaces.end()) {}
2074 
2075   static bool classof(const PredicateMatcher *P) {
2076     return P->getKind() == IPM_MemoryAddressSpace;
2077   }
2078   bool isIdentical(const PredicateMatcher &B) const override {
2079     if (!InstructionPredicateMatcher::isIdentical(B))
2080       return false;
2081     auto *Other = cast<MemoryAddressSpacePredicateMatcher>(&B);
2082     return MMOIdx == Other->MMOIdx && AddrSpaces == Other->AddrSpaces;
2083   }
2084 
2085   void emitPredicateOpcodes(MatchTable &Table,
2086                             RuleMatcher &Rule) const override {
2087     Table << MatchTable::Opcode("GIM_CheckMemoryAddressSpace")
2088           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
2089           << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx)
2090         // Encode number of address spaces to expect.
2091           << MatchTable::Comment("NumAddrSpace")
2092           << MatchTable::IntValue(AddrSpaces.size());
2093     for (unsigned AS : AddrSpaces)
2094       Table << MatchTable::Comment("AddrSpace") << MatchTable::IntValue(AS);
2095 
2096     Table << MatchTable::LineBreak;
2097   }
2098 };
2099 
2100 class MemoryAlignmentPredicateMatcher : public InstructionPredicateMatcher {
2101 protected:
2102   unsigned MMOIdx;
2103   int MinAlign;
2104 
2105 public:
2106   MemoryAlignmentPredicateMatcher(unsigned InsnVarID, unsigned MMOIdx,
2107                                   int MinAlign)
2108       : InstructionPredicateMatcher(IPM_MemoryAlignment, InsnVarID),
2109         MMOIdx(MMOIdx), MinAlign(MinAlign) {
2110     assert(MinAlign > 0);
2111   }
2112 
2113   static bool classof(const PredicateMatcher *P) {
2114     return P->getKind() == IPM_MemoryAlignment;
2115   }
2116 
2117   bool isIdentical(const PredicateMatcher &B) const override {
2118     if (!InstructionPredicateMatcher::isIdentical(B))
2119       return false;
2120     auto *Other = cast<MemoryAlignmentPredicateMatcher>(&B);
2121     return MMOIdx == Other->MMOIdx && MinAlign == Other->MinAlign;
2122   }
2123 
2124   void emitPredicateOpcodes(MatchTable &Table,
2125                             RuleMatcher &Rule) const override {
2126     Table << MatchTable::Opcode("GIM_CheckMemoryAlignment")
2127           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
2128           << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx)
2129           << MatchTable::Comment("MinAlign") << MatchTable::IntValue(MinAlign)
2130           << MatchTable::LineBreak;
2131   }
2132 };
2133 
2134 /// Generates code to check that the size of an MMO is less-than, equal-to, or
2135 /// greater than a given LLT.
2136 class MemoryVsLLTSizePredicateMatcher : public InstructionPredicateMatcher {
2137 public:
2138   enum RelationKind {
2139     GreaterThan,
2140     EqualTo,
2141     LessThan,
2142   };
2143 
2144 protected:
2145   unsigned MMOIdx;
2146   RelationKind Relation;
2147   unsigned OpIdx;
2148 
2149 public:
2150   MemoryVsLLTSizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx,
2151                                   enum RelationKind Relation,
2152                                   unsigned OpIdx)
2153       : InstructionPredicateMatcher(IPM_MemoryVsLLTSize, InsnVarID),
2154         MMOIdx(MMOIdx), Relation(Relation), OpIdx(OpIdx) {}
2155 
2156   static bool classof(const PredicateMatcher *P) {
2157     return P->getKind() == IPM_MemoryVsLLTSize;
2158   }
2159   bool isIdentical(const PredicateMatcher &B) const override {
2160     return InstructionPredicateMatcher::isIdentical(B) &&
2161            MMOIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->MMOIdx &&
2162            Relation == cast<MemoryVsLLTSizePredicateMatcher>(&B)->Relation &&
2163            OpIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->OpIdx;
2164   }
2165 
2166   void emitPredicateOpcodes(MatchTable &Table,
2167                             RuleMatcher &Rule) const override {
2168     Table << MatchTable::Opcode(Relation == EqualTo
2169                                     ? "GIM_CheckMemorySizeEqualToLLT"
2170                                     : Relation == GreaterThan
2171                                           ? "GIM_CheckMemorySizeGreaterThanLLT"
2172                                           : "GIM_CheckMemorySizeLessThanLLT")
2173           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
2174           << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx)
2175           << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx)
2176           << MatchTable::LineBreak;
2177   }
2178 };
2179 
2180 // Matcher for immAllOnesV/immAllZerosV
2181 class VectorSplatImmPredicateMatcher : public InstructionPredicateMatcher {
2182 public:
2183   enum SplatKind {
2184     AllZeros,
2185     AllOnes
2186   };
2187 
2188 private:
2189   SplatKind Kind;
2190 
2191 public:
2192   VectorSplatImmPredicateMatcher(unsigned InsnVarID, SplatKind K)
2193       : InstructionPredicateMatcher(IPM_VectorSplatImm, InsnVarID), Kind(K) {}
2194 
2195   static bool classof(const PredicateMatcher *P) {
2196     return P->getKind() == IPM_VectorSplatImm;
2197   }
2198 
2199   bool isIdentical(const PredicateMatcher &B) const override {
2200     return InstructionPredicateMatcher::isIdentical(B) &&
2201            Kind == static_cast<const VectorSplatImmPredicateMatcher &>(B).Kind;
2202   }
2203 
2204   void emitPredicateOpcodes(MatchTable &Table,
2205                             RuleMatcher &Rule) const override {
2206     if (Kind == AllOnes)
2207       Table << MatchTable::Opcode("GIM_CheckIsBuildVectorAllOnes");
2208     else
2209       Table << MatchTable::Opcode("GIM_CheckIsBuildVectorAllZeros");
2210 
2211     Table << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID);
2212     Table << MatchTable::LineBreak;
2213   }
2214 };
2215 
2216 /// Generates code to check an arbitrary C++ instruction predicate.
2217 class GenericInstructionPredicateMatcher : public InstructionPredicateMatcher {
2218 protected:
2219   TreePredicateFn Predicate;
2220 
2221 public:
2222   GenericInstructionPredicateMatcher(unsigned InsnVarID,
2223                                      TreePredicateFn Predicate)
2224       : InstructionPredicateMatcher(IPM_GenericPredicate, InsnVarID),
2225         Predicate(Predicate) {}
2226 
2227   static bool classof(const InstructionPredicateMatcher *P) {
2228     return P->getKind() == IPM_GenericPredicate;
2229   }
2230   bool isIdentical(const PredicateMatcher &B) const override {
2231     return InstructionPredicateMatcher::isIdentical(B) &&
2232            Predicate ==
2233                static_cast<const GenericInstructionPredicateMatcher &>(B)
2234                    .Predicate;
2235   }
2236   void emitPredicateOpcodes(MatchTable &Table,
2237                             RuleMatcher &Rule) const override {
2238     Table << MatchTable::Opcode("GIM_CheckCxxInsnPredicate")
2239           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
2240           << MatchTable::Comment("FnId")
2241           << MatchTable::NamedValue(getEnumNameForPredicate(Predicate))
2242           << MatchTable::LineBreak;
2243   }
2244 };
2245 
2246 /// Generates code to check for the absence of use of the result.
2247 // TODO? Generalize this to support checking for one use.
2248 class NoUsePredicateMatcher : public InstructionPredicateMatcher {
2249 public:
2250   NoUsePredicateMatcher(unsigned InsnVarID)
2251       : InstructionPredicateMatcher(IPM_NoUse, InsnVarID) {}
2252 
2253   static bool classof(const PredicateMatcher *P) {
2254     return P->getKind() == IPM_NoUse;
2255   }
2256 
2257   bool isIdentical(const PredicateMatcher &B) const override {
2258     return InstructionPredicateMatcher::isIdentical(B);
2259   }
2260 
2261   void emitPredicateOpcodes(MatchTable &Table,
2262                             RuleMatcher &Rule) const override {
2263     Table << MatchTable::Opcode("GIM_CheckHasNoUse")
2264           << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
2265           << MatchTable::LineBreak;
2266   }
2267 };
2268 
2269 /// Generates code to check that a set of predicates and operands match for a
2270 /// particular instruction.
2271 ///
2272 /// Typical predicates include:
2273 /// * Has a specific opcode.
2274 /// * Has an nsw/nuw flag or doesn't.
2275 class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> {
2276 protected:
2277   typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec;
2278 
2279   RuleMatcher &Rule;
2280 
2281   /// The operands to match. All rendered operands must be present even if the
2282   /// condition is always true.
2283   OperandVec Operands;
2284   bool NumOperandsCheck = true;
2285 
2286   std::string SymbolicName;
2287   unsigned InsnVarID;
2288 
2289   /// PhysRegInputs - List list has an entry for each explicitly specified
2290   /// physreg input to the pattern.  The first elt is the Register node, the
2291   /// second is the recorded slot number the input pattern match saved it in.
2292   SmallVector<std::pair<Record *, unsigned>, 2> PhysRegInputs;
2293 
2294 public:
2295   InstructionMatcher(RuleMatcher &Rule, StringRef SymbolicName,
2296                      bool NumOpsCheck = true)
2297       : Rule(Rule), NumOperandsCheck(NumOpsCheck), SymbolicName(SymbolicName) {
2298     // We create a new instruction matcher.
2299     // Get a new ID for that instruction.
2300     InsnVarID = Rule.implicitlyDefineInsnVar(*this);
2301   }
2302 
2303   /// Construct a new instruction predicate and add it to the matcher.
2304   template <class Kind, class... Args>
2305   std::optional<Kind *> addPredicate(Args &&...args) {
2306     Predicates.emplace_back(
2307         std::make_unique<Kind>(getInsnVarID(), std::forward<Args>(args)...));
2308     return static_cast<Kind *>(Predicates.back().get());
2309   }
2310 
2311   RuleMatcher &getRuleMatcher() const { return Rule; }
2312 
2313   unsigned getInsnVarID() const { return InsnVarID; }
2314 
2315   /// Add an operand to the matcher.
2316   OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName,
2317                              unsigned AllocatedTemporariesBaseID) {
2318     Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName,
2319                                              AllocatedTemporariesBaseID));
2320     if (!SymbolicName.empty())
2321       Rule.defineOperand(SymbolicName, *Operands.back());
2322 
2323     return *Operands.back();
2324   }
2325 
2326   OperandMatcher &getOperand(unsigned OpIdx) {
2327     auto I = llvm::find_if(Operands,
2328                            [&OpIdx](const std::unique_ptr<OperandMatcher> &X) {
2329                              return X->getOpIdx() == OpIdx;
2330                            });
2331     if (I != Operands.end())
2332       return **I;
2333     llvm_unreachable("Failed to lookup operand");
2334   }
2335 
2336   OperandMatcher &addPhysRegInput(Record *Reg, unsigned OpIdx,
2337                                   unsigned TempOpIdx) {
2338     assert(SymbolicName.empty());
2339     OperandMatcher *OM = new OperandMatcher(*this, OpIdx, "", TempOpIdx);
2340     Operands.emplace_back(OM);
2341     Rule.definePhysRegOperand(Reg, *OM);
2342     PhysRegInputs.emplace_back(Reg, OpIdx);
2343     return *OM;
2344   }
2345 
2346   ArrayRef<std::pair<Record *, unsigned>> getPhysRegInputs() const {
2347     return PhysRegInputs;
2348   }
2349 
2350   StringRef getSymbolicName() const { return SymbolicName; }
2351   unsigned getNumOperands() const { return Operands.size(); }
2352   OperandVec::iterator operands_begin() { return Operands.begin(); }
2353   OperandVec::iterator operands_end() { return Operands.end(); }
2354   iterator_range<OperandVec::iterator> operands() {
2355     return make_range(operands_begin(), operands_end());
2356   }
2357   OperandVec::const_iterator operands_begin() const { return Operands.begin(); }
2358   OperandVec::const_iterator operands_end() const { return Operands.end(); }
2359   iterator_range<OperandVec::const_iterator> operands() const {
2360     return make_range(operands_begin(), operands_end());
2361   }
2362   bool operands_empty() const { return Operands.empty(); }
2363 
2364   void pop_front() { Operands.erase(Operands.begin()); }
2365 
2366   void optimize();
2367 
2368   /// Emit MatchTable opcodes that test whether the instruction named in
2369   /// InsnVarName matches all the predicates and all the operands.
2370   void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) {
2371     if (NumOperandsCheck)
2372       InstructionNumOperandsMatcher(InsnVarID, getNumOperands())
2373           .emitPredicateOpcodes(Table, Rule);
2374 
2375     // First emit all instruction level predicates need to be verified before we
2376     // can verify operands.
2377     emitFilteredPredicateListOpcodes(
2378       [](const PredicateMatcher &P) {
2379         return !P.dependsOnOperands();
2380       }, Table, Rule);
2381 
2382     // Emit all operand constraints.
2383     for (const auto &Operand : Operands)
2384       Operand->emitPredicateOpcodes(Table, Rule);
2385 
2386     // All of the tablegen defined predicates should now be matched. Now emit
2387     // any custom predicates that rely on all generated checks.
2388     emitFilteredPredicateListOpcodes(
2389       [](const PredicateMatcher &P) {
2390         return P.dependsOnOperands();
2391       }, Table, Rule);
2392   }
2393 
2394   /// Compare the priority of this object and B.
2395   ///
2396   /// Returns true if this object is more important than B.
2397   bool isHigherPriorityThan(InstructionMatcher &B) {
2398     // Instruction matchers involving more operands have higher priority.
2399     if (Operands.size() > B.Operands.size())
2400       return true;
2401     if (Operands.size() < B.Operands.size())
2402       return false;
2403 
2404     for (auto &&P : zip(predicates(), B.predicates())) {
2405       auto L = static_cast<InstructionPredicateMatcher *>(std::get<0>(P).get());
2406       auto R = static_cast<InstructionPredicateMatcher *>(std::get<1>(P).get());
2407       if (L->isHigherPriorityThan(*R))
2408         return true;
2409       if (R->isHigherPriorityThan(*L))
2410         return false;
2411     }
2412 
2413     for (auto Operand : zip(Operands, B.Operands)) {
2414       if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand)))
2415         return true;
2416       if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand)))
2417         return false;
2418     }
2419 
2420     return false;
2421   };
2422 
2423   /// Report the maximum number of temporary operands needed by the instruction
2424   /// matcher.
2425   unsigned countRendererFns() {
2426     return std::accumulate(
2427                predicates().begin(), predicates().end(), 0,
2428                [](unsigned A,
2429                   const std::unique_ptr<PredicateMatcher> &Predicate) {
2430                  return A + Predicate->countRendererFns();
2431                }) +
2432            std::accumulate(
2433                Operands.begin(), Operands.end(), 0,
2434                [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) {
2435                  return A + Operand->countRendererFns();
2436                });
2437   }
2438 
2439   InstructionOpcodeMatcher &getOpcodeMatcher() {
2440     for (auto &P : predicates())
2441       if (auto *OpMatcher = dyn_cast<InstructionOpcodeMatcher>(P.get()))
2442         return *OpMatcher;
2443     llvm_unreachable("Didn't find an opcode matcher");
2444   }
2445 
2446   bool isConstantInstruction() {
2447     return getOpcodeMatcher().isConstantInstruction();
2448   }
2449 
2450   StringRef getOpcode() { return getOpcodeMatcher().getOpcode(); }
2451 };
2452 
2453 StringRef RuleMatcher::getOpcode() const {
2454   return Matchers.front()->getOpcode();
2455 }
2456 
2457 unsigned RuleMatcher::getNumOperands() const {
2458   return Matchers.front()->getNumOperands();
2459 }
2460 
2461 LLTCodeGen RuleMatcher::getFirstConditionAsRootType() {
2462   InstructionMatcher &InsnMatcher = *Matchers.front();
2463   if (!InsnMatcher.predicates_empty())
2464     if (const auto *TM =
2465             dyn_cast<LLTOperandMatcher>(&**InsnMatcher.predicates_begin()))
2466       if (TM->getInsnVarID() == 0 && TM->getOpIdx() == 0)
2467         return TM->getTy();
2468   return {};
2469 }
2470 
2471 /// Generates code to check that the operand is a register defined by an
2472 /// instruction that matches the given instruction matcher.
2473 ///
2474 /// For example, the pattern:
2475 ///   (set $dst, (G_MUL (G_ADD $src1, $src2), $src3))
2476 /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match
2477 /// the:
2478 ///   (G_ADD $src1, $src2)
2479 /// subpattern.
2480 class InstructionOperandMatcher : public OperandPredicateMatcher {
2481 protected:
2482   std::unique_ptr<InstructionMatcher> InsnMatcher;
2483 
2484 public:
2485   InstructionOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
2486                             RuleMatcher &Rule, StringRef SymbolicName,
2487                             bool NumOpsCheck = true)
2488       : OperandPredicateMatcher(OPM_Instruction, InsnVarID, OpIdx),
2489         InsnMatcher(new InstructionMatcher(Rule, SymbolicName, NumOpsCheck)) {}
2490 
2491   static bool classof(const PredicateMatcher *P) {
2492     return P->getKind() == OPM_Instruction;
2493   }
2494 
2495   InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; }
2496 
2497   void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule) const {
2498     const unsigned NewInsnVarID = InsnMatcher->getInsnVarID();
2499     Table << MatchTable::Opcode("GIM_RecordInsn")
2500           << MatchTable::Comment("DefineMI")
2501           << MatchTable::IntValue(NewInsnVarID) << MatchTable::Comment("MI")
2502           << MatchTable::IntValue(getInsnVarID())
2503           << MatchTable::Comment("OpIdx") << MatchTable::IntValue(getOpIdx())
2504           << MatchTable::Comment("MIs[" + llvm::to_string(NewInsnVarID) + "]")
2505           << MatchTable::LineBreak;
2506   }
2507 
2508   void emitPredicateOpcodes(MatchTable &Table,
2509                             RuleMatcher &Rule) const override {
2510     emitCaptureOpcodes(Table, Rule);
2511     InsnMatcher->emitPredicateOpcodes(Table, Rule);
2512   }
2513 
2514   bool isHigherPriorityThan(const OperandPredicateMatcher &B) const override {
2515     if (OperandPredicateMatcher::isHigherPriorityThan(B))
2516       return true;
2517     if (B.OperandPredicateMatcher::isHigherPriorityThan(*this))
2518       return false;
2519 
2520     if (const InstructionOperandMatcher *BP =
2521             dyn_cast<InstructionOperandMatcher>(&B))
2522       if (InsnMatcher->isHigherPriorityThan(*BP->InsnMatcher))
2523         return true;
2524     return false;
2525   }
2526 
2527   /// Report the maximum number of temporary operands needed by the predicate
2528   /// matcher.
2529   unsigned countRendererFns() const override {
2530     return InsnMatcher->countRendererFns();
2531   }
2532 };
2533 
2534 void InstructionMatcher::optimize() {
2535   SmallVector<std::unique_ptr<PredicateMatcher>, 8> Stash;
2536   const auto &OpcMatcher = getOpcodeMatcher();
2537 
2538   Stash.push_back(predicates_pop_front());
2539   if (Stash.back().get() == &OpcMatcher) {
2540     if (NumOperandsCheck && OpcMatcher.isVariadicNumOperands())
2541       Stash.emplace_back(
2542           new InstructionNumOperandsMatcher(InsnVarID, getNumOperands()));
2543     NumOperandsCheck = false;
2544 
2545     for (auto &OM : Operands)
2546       for (auto &OP : OM->predicates())
2547         if (isa<IntrinsicIDOperandMatcher>(OP)) {
2548           Stash.push_back(std::move(OP));
2549           OM->eraseNullPredicates();
2550           break;
2551         }
2552   }
2553 
2554   if (InsnVarID > 0) {
2555     assert(!Operands.empty() && "Nested instruction is expected to def a vreg");
2556     for (auto &OP : Operands[0]->predicates())
2557       OP.reset();
2558     Operands[0]->eraseNullPredicates();
2559   }
2560   for (auto &OM : Operands) {
2561     for (auto &OP : OM->predicates())
2562       if (isa<LLTOperandMatcher>(OP))
2563         Stash.push_back(std::move(OP));
2564     OM->eraseNullPredicates();
2565   }
2566   while (!Stash.empty())
2567     prependPredicate(Stash.pop_back_val());
2568 }
2569 
2570 //===- Actions ------------------------------------------------------------===//
2571 class OperandRenderer {
2572 public:
2573   enum RendererKind {
2574     OR_Copy,
2575     OR_CopyOrAddZeroReg,
2576     OR_CopySubReg,
2577     OR_CopyPhysReg,
2578     OR_CopyConstantAsImm,
2579     OR_CopyFConstantAsFPImm,
2580     OR_Imm,
2581     OR_SubRegIndex,
2582     OR_Register,
2583     OR_TempRegister,
2584     OR_ComplexPattern,
2585     OR_Custom,
2586     OR_CustomOperand
2587   };
2588 
2589 protected:
2590   RendererKind Kind;
2591 
2592 public:
2593   OperandRenderer(RendererKind Kind) : Kind(Kind) {}
2594   virtual ~OperandRenderer() {}
2595 
2596   RendererKind getKind() const { return Kind; }
2597 
2598   virtual void emitRenderOpcodes(MatchTable &Table,
2599                                  RuleMatcher &Rule) const = 0;
2600 };
2601 
2602 /// A CopyRenderer emits code to copy a single operand from an existing
2603 /// instruction to the one being built.
2604 class CopyRenderer : public OperandRenderer {
2605 protected:
2606   unsigned NewInsnID;
2607   /// The name of the operand.
2608   const StringRef SymbolicName;
2609 
2610 public:
2611   CopyRenderer(unsigned NewInsnID, StringRef SymbolicName)
2612       : OperandRenderer(OR_Copy), NewInsnID(NewInsnID),
2613         SymbolicName(SymbolicName) {
2614     assert(!SymbolicName.empty() && "Cannot copy from an unspecified source");
2615   }
2616 
2617   static bool classof(const OperandRenderer *R) {
2618     return R->getKind() == OR_Copy;
2619   }
2620 
2621   StringRef getSymbolicName() const { return SymbolicName; }
2622 
2623   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2624     const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName);
2625     unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher());
2626     Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID")
2627           << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID")
2628           << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx")
2629           << MatchTable::IntValue(Operand.getOpIdx())
2630           << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
2631   }
2632 };
2633 
2634 /// A CopyRenderer emits code to copy a virtual register to a specific physical
2635 /// register.
2636 class CopyPhysRegRenderer : public OperandRenderer {
2637 protected:
2638   unsigned NewInsnID;
2639   Record *PhysReg;
2640 
2641 public:
2642   CopyPhysRegRenderer(unsigned NewInsnID, Record *Reg)
2643       : OperandRenderer(OR_CopyPhysReg), NewInsnID(NewInsnID),
2644         PhysReg(Reg) {
2645     assert(PhysReg);
2646   }
2647 
2648   static bool classof(const OperandRenderer *R) {
2649     return R->getKind() == OR_CopyPhysReg;
2650   }
2651 
2652   Record *getPhysReg() const { return PhysReg; }
2653 
2654   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2655     const OperandMatcher &Operand = Rule.getPhysRegOperandMatcher(PhysReg);
2656     unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher());
2657     Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID")
2658           << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID")
2659           << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx")
2660           << MatchTable::IntValue(Operand.getOpIdx())
2661           << MatchTable::Comment(PhysReg->getName())
2662           << MatchTable::LineBreak;
2663   }
2664 };
2665 
2666 /// A CopyOrAddZeroRegRenderer emits code to copy a single operand from an
2667 /// existing instruction to the one being built. If the operand turns out to be
2668 /// a 'G_CONSTANT 0' then it replaces the operand with a zero register.
2669 class CopyOrAddZeroRegRenderer : public OperandRenderer {
2670 protected:
2671   unsigned NewInsnID;
2672   /// The name of the operand.
2673   const StringRef SymbolicName;
2674   const Record *ZeroRegisterDef;
2675 
2676 public:
2677   CopyOrAddZeroRegRenderer(unsigned NewInsnID,
2678                            StringRef SymbolicName, Record *ZeroRegisterDef)
2679       : OperandRenderer(OR_CopyOrAddZeroReg), NewInsnID(NewInsnID),
2680         SymbolicName(SymbolicName), ZeroRegisterDef(ZeroRegisterDef) {
2681     assert(!SymbolicName.empty() && "Cannot copy from an unspecified source");
2682   }
2683 
2684   static bool classof(const OperandRenderer *R) {
2685     return R->getKind() == OR_CopyOrAddZeroReg;
2686   }
2687 
2688   StringRef getSymbolicName() const { return SymbolicName; }
2689 
2690   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2691     const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName);
2692     unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher());
2693     Table << MatchTable::Opcode("GIR_CopyOrAddZeroReg")
2694           << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID)
2695           << MatchTable::Comment("OldInsnID")
2696           << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx")
2697           << MatchTable::IntValue(Operand.getOpIdx())
2698           << MatchTable::NamedValue(
2699                  (ZeroRegisterDef->getValue("Namespace")
2700                       ? ZeroRegisterDef->getValueAsString("Namespace")
2701                       : ""),
2702                  ZeroRegisterDef->getName())
2703           << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
2704   }
2705 };
2706 
2707 /// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to
2708 /// an extended immediate operand.
2709 class CopyConstantAsImmRenderer : public OperandRenderer {
2710 protected:
2711   unsigned NewInsnID;
2712   /// The name of the operand.
2713   const std::string SymbolicName;
2714   bool Signed;
2715 
2716 public:
2717   CopyConstantAsImmRenderer(unsigned NewInsnID, StringRef SymbolicName)
2718       : OperandRenderer(OR_CopyConstantAsImm), NewInsnID(NewInsnID),
2719         SymbolicName(SymbolicName), Signed(true) {}
2720 
2721   static bool classof(const OperandRenderer *R) {
2722     return R->getKind() == OR_CopyConstantAsImm;
2723   }
2724 
2725   StringRef getSymbolicName() const { return SymbolicName; }
2726 
2727   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2728     InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName);
2729     unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher);
2730     Table << MatchTable::Opcode(Signed ? "GIR_CopyConstantAsSImm"
2731                                        : "GIR_CopyConstantAsUImm")
2732           << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID)
2733           << MatchTable::Comment("OldInsnID")
2734           << MatchTable::IntValue(OldInsnVarID)
2735           << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
2736   }
2737 };
2738 
2739 /// A CopyFConstantAsFPImmRenderer emits code to render a G_FCONSTANT
2740 /// instruction to an extended immediate operand.
2741 class CopyFConstantAsFPImmRenderer : public OperandRenderer {
2742 protected:
2743   unsigned NewInsnID;
2744   /// The name of the operand.
2745   const std::string SymbolicName;
2746 
2747 public:
2748   CopyFConstantAsFPImmRenderer(unsigned NewInsnID, StringRef SymbolicName)
2749       : OperandRenderer(OR_CopyFConstantAsFPImm), NewInsnID(NewInsnID),
2750         SymbolicName(SymbolicName) {}
2751 
2752   static bool classof(const OperandRenderer *R) {
2753     return R->getKind() == OR_CopyFConstantAsFPImm;
2754   }
2755 
2756   StringRef getSymbolicName() const { return SymbolicName; }
2757 
2758   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2759     InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName);
2760     unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher);
2761     Table << MatchTable::Opcode("GIR_CopyFConstantAsFPImm")
2762           << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID)
2763           << MatchTable::Comment("OldInsnID")
2764           << MatchTable::IntValue(OldInsnVarID)
2765           << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
2766   }
2767 };
2768 
2769 /// A CopySubRegRenderer emits code to copy a single register operand from an
2770 /// existing instruction to the one being built and indicate that only a
2771 /// subregister should be copied.
2772 class CopySubRegRenderer : public OperandRenderer {
2773 protected:
2774   unsigned NewInsnID;
2775   /// The name of the operand.
2776   const StringRef SymbolicName;
2777   /// The subregister to extract.
2778   const CodeGenSubRegIndex *SubReg;
2779 
2780 public:
2781   CopySubRegRenderer(unsigned NewInsnID, StringRef SymbolicName,
2782                      const CodeGenSubRegIndex *SubReg)
2783       : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID),
2784         SymbolicName(SymbolicName), SubReg(SubReg) {}
2785 
2786   static bool classof(const OperandRenderer *R) {
2787     return R->getKind() == OR_CopySubReg;
2788   }
2789 
2790   StringRef getSymbolicName() const { return SymbolicName; }
2791 
2792   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2793     const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName);
2794     unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher());
2795     Table << MatchTable::Opcode("GIR_CopySubReg")
2796           << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID)
2797           << MatchTable::Comment("OldInsnID")
2798           << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx")
2799           << MatchTable::IntValue(Operand.getOpIdx())
2800           << MatchTable::Comment("SubRegIdx")
2801           << MatchTable::IntValue(SubReg->EnumValue)
2802           << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
2803   }
2804 };
2805 
2806 /// Adds a specific physical register to the instruction being built.
2807 /// This is typically useful for WZR/XZR on AArch64.
2808 class AddRegisterRenderer : public OperandRenderer {
2809 protected:
2810   unsigned InsnID;
2811   const Record *RegisterDef;
2812   bool IsDef;
2813   const CodeGenTarget &Target;
2814 
2815 public:
2816   AddRegisterRenderer(unsigned InsnID, const CodeGenTarget &Target,
2817                       const Record *RegisterDef, bool IsDef = false)
2818       : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef),
2819         IsDef(IsDef), Target(Target) {}
2820 
2821   static bool classof(const OperandRenderer *R) {
2822     return R->getKind() == OR_Register;
2823   }
2824 
2825   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2826     Table << MatchTable::Opcode("GIR_AddRegister")
2827           << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID);
2828     if (RegisterDef->getName() != "zero_reg") {
2829       Table << MatchTable::NamedValue(
2830                    (RegisterDef->getValue("Namespace")
2831                         ? RegisterDef->getValueAsString("Namespace")
2832                         : ""),
2833                    RegisterDef->getName());
2834     } else {
2835       Table << MatchTable::NamedValue(Target.getRegNamespace(), "NoRegister");
2836     }
2837     Table << MatchTable::Comment("AddRegisterRegFlags");
2838 
2839     // TODO: This is encoded as a 64-bit element, but only 16 or 32-bits are
2840     // really needed for a physical register reference. We can pack the
2841     // register and flags in a single field.
2842     if (IsDef)
2843       Table << MatchTable::NamedValue("RegState::Define");
2844     else
2845       Table << MatchTable::IntValue(0);
2846     Table << MatchTable::LineBreak;
2847   }
2848 };
2849 
2850 /// Adds a specific temporary virtual register to the instruction being built.
2851 /// This is used to chain instructions together when emitting multiple
2852 /// instructions.
2853 class TempRegRenderer : public OperandRenderer {
2854 protected:
2855   unsigned InsnID;
2856   unsigned TempRegID;
2857   const CodeGenSubRegIndex *SubRegIdx;
2858   bool IsDef;
2859   bool IsDead;
2860 
2861 public:
2862   TempRegRenderer(unsigned InsnID, unsigned TempRegID, bool IsDef = false,
2863                   const CodeGenSubRegIndex *SubReg = nullptr,
2864                   bool IsDead = false)
2865       : OperandRenderer(OR_Register), InsnID(InsnID), TempRegID(TempRegID),
2866         SubRegIdx(SubReg), IsDef(IsDef), IsDead(IsDead) {}
2867 
2868   static bool classof(const OperandRenderer *R) {
2869     return R->getKind() == OR_TempRegister;
2870   }
2871 
2872   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2873     if (SubRegIdx) {
2874       assert(!IsDef);
2875       Table << MatchTable::Opcode("GIR_AddTempSubRegister");
2876     } else
2877       Table << MatchTable::Opcode("GIR_AddTempRegister");
2878 
2879     Table << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
2880           << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID)
2881           << MatchTable::Comment("TempRegFlags");
2882 
2883     if (IsDef) {
2884       SmallString<32> RegFlags;
2885       RegFlags += "RegState::Define";
2886       if (IsDead)
2887         RegFlags += "|RegState::Dead";
2888       Table << MatchTable::NamedValue(RegFlags);
2889     } else
2890       Table << MatchTable::IntValue(0);
2891 
2892     if (SubRegIdx)
2893       Table << MatchTable::NamedValue(SubRegIdx->getQualifiedName());
2894     Table << MatchTable::LineBreak;
2895   }
2896 };
2897 
2898 /// Adds a specific immediate to the instruction being built.
2899 class ImmRenderer : public OperandRenderer {
2900 protected:
2901   unsigned InsnID;
2902   int64_t Imm;
2903 
2904 public:
2905   ImmRenderer(unsigned InsnID, int64_t Imm)
2906       : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {}
2907 
2908   static bool classof(const OperandRenderer *R) {
2909     return R->getKind() == OR_Imm;
2910   }
2911 
2912   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2913     Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID")
2914           << MatchTable::IntValue(InsnID) << MatchTable::Comment("Imm")
2915           << MatchTable::IntValue(Imm) << MatchTable::LineBreak;
2916   }
2917 };
2918 
2919 /// Adds an enum value for a subreg index to the instruction being built.
2920 class SubRegIndexRenderer : public OperandRenderer {
2921 protected:
2922   unsigned InsnID;
2923   const CodeGenSubRegIndex *SubRegIdx;
2924 
2925 public:
2926   SubRegIndexRenderer(unsigned InsnID, const CodeGenSubRegIndex *SRI)
2927       : OperandRenderer(OR_SubRegIndex), InsnID(InsnID), SubRegIdx(SRI) {}
2928 
2929   static bool classof(const OperandRenderer *R) {
2930     return R->getKind() == OR_SubRegIndex;
2931   }
2932 
2933   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2934     Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID")
2935           << MatchTable::IntValue(InsnID) << MatchTable::Comment("SubRegIndex")
2936           << MatchTable::IntValue(SubRegIdx->EnumValue)
2937           << MatchTable::LineBreak;
2938   }
2939 };
2940 
2941 /// Adds operands by calling a renderer function supplied by the ComplexPattern
2942 /// matcher function.
2943 class RenderComplexPatternOperand : public OperandRenderer {
2944 private:
2945   unsigned InsnID;
2946   const Record &TheDef;
2947   /// The name of the operand.
2948   const StringRef SymbolicName;
2949   /// The renderer number. This must be unique within a rule since it's used to
2950   /// identify a temporary variable to hold the renderer function.
2951   unsigned RendererID;
2952   /// When provided, this is the suboperand of the ComplexPattern operand to
2953   /// render. Otherwise all the suboperands will be rendered.
2954   std::optional<unsigned> SubOperand;
2955 
2956   unsigned getNumOperands() const {
2957     return TheDef.getValueAsDag("Operands")->getNumArgs();
2958   }
2959 
2960 public:
2961   RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef,
2962                               StringRef SymbolicName, unsigned RendererID,
2963                               std::optional<unsigned> SubOperand = std::nullopt)
2964       : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef),
2965         SymbolicName(SymbolicName), RendererID(RendererID),
2966         SubOperand(SubOperand) {}
2967 
2968   static bool classof(const OperandRenderer *R) {
2969     return R->getKind() == OR_ComplexPattern;
2970   }
2971 
2972   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
2973     Table << MatchTable::Opcode(SubOperand ? "GIR_ComplexSubOperandRenderer"
2974                                            : "GIR_ComplexRenderer")
2975           << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
2976           << MatchTable::Comment("RendererID")
2977           << MatchTable::IntValue(RendererID);
2978     if (SubOperand)
2979       Table << MatchTable::Comment("SubOperand")
2980             << MatchTable::IntValue(*SubOperand);
2981     Table << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
2982   }
2983 };
2984 
2985 class CustomRenderer : public OperandRenderer {
2986 protected:
2987   unsigned InsnID;
2988   const Record &Renderer;
2989   /// The name of the operand.
2990   const std::string SymbolicName;
2991 
2992 public:
2993   CustomRenderer(unsigned InsnID, const Record &Renderer,
2994                  StringRef SymbolicName)
2995       : OperandRenderer(OR_Custom), InsnID(InsnID), Renderer(Renderer),
2996         SymbolicName(SymbolicName) {}
2997 
2998   static bool classof(const OperandRenderer *R) {
2999     return R->getKind() == OR_Custom;
3000   }
3001 
3002   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
3003     InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName);
3004     unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher);
3005     Table << MatchTable::Opcode("GIR_CustomRenderer")
3006           << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3007           << MatchTable::Comment("OldInsnID")
3008           << MatchTable::IntValue(OldInsnVarID)
3009           << MatchTable::Comment("Renderer")
3010           << MatchTable::NamedValue(
3011                  "GICR_" + Renderer.getValueAsString("RendererFn").str())
3012           << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
3013   }
3014 };
3015 
3016 class CustomOperandRenderer : public OperandRenderer {
3017 protected:
3018   unsigned InsnID;
3019   const Record &Renderer;
3020   /// The name of the operand.
3021   const std::string SymbolicName;
3022 
3023 public:
3024   CustomOperandRenderer(unsigned InsnID, const Record &Renderer,
3025                         StringRef SymbolicName)
3026       : OperandRenderer(OR_CustomOperand), InsnID(InsnID), Renderer(Renderer),
3027         SymbolicName(SymbolicName) {}
3028 
3029   static bool classof(const OperandRenderer *R) {
3030     return R->getKind() == OR_CustomOperand;
3031   }
3032 
3033   void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
3034     const OperandMatcher &OpdMatcher = Rule.getOperandMatcher(SymbolicName);
3035     Table << MatchTable::Opcode("GIR_CustomOperandRenderer")
3036           << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3037           << MatchTable::Comment("OldInsnID")
3038           << MatchTable::IntValue(OpdMatcher.getInsnVarID())
3039           << MatchTable::Comment("OpIdx")
3040           << MatchTable::IntValue(OpdMatcher.getOpIdx())
3041           << MatchTable::Comment("OperandRenderer")
3042           << MatchTable::NamedValue(
3043             "GICR_" + Renderer.getValueAsString("RendererFn").str())
3044           << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak;
3045   }
3046 };
3047 
3048 /// An action taken when all Matcher predicates succeeded for a parent rule.
3049 ///
3050 /// Typical actions include:
3051 /// * Changing the opcode of an instruction.
3052 /// * Adding an operand to an instruction.
3053 class MatchAction {
3054 public:
3055   virtual ~MatchAction() {}
3056 
3057   /// Emit the MatchTable opcodes to implement the action.
3058   virtual void emitActionOpcodes(MatchTable &Table,
3059                                  RuleMatcher &Rule) const = 0;
3060 };
3061 
3062 /// Generates a comment describing the matched rule being acted upon.
3063 class DebugCommentAction : public MatchAction {
3064 private:
3065   std::string S;
3066 
3067 public:
3068   DebugCommentAction(StringRef S) : S(std::string(S)) {}
3069 
3070   void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
3071     Table << MatchTable::Comment(S) << MatchTable::LineBreak;
3072   }
3073 };
3074 
3075 /// Generates code to build an instruction or mutate an existing instruction
3076 /// into the desired instruction when this is possible.
3077 class BuildMIAction : public MatchAction {
3078 private:
3079   unsigned InsnID;
3080   const CodeGenInstruction *I;
3081   InstructionMatcher *Matched;
3082   std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers;
3083 
3084   /// True if the instruction can be built solely by mutating the opcode.
3085   bool canMutate(RuleMatcher &Rule, const InstructionMatcher *Insn) const {
3086     if (!Insn)
3087       return false;
3088 
3089     if (OperandRenderers.size() != Insn->getNumOperands())
3090       return false;
3091 
3092     for (const auto &Renderer : enumerate(OperandRenderers)) {
3093       if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) {
3094         const OperandMatcher &OM = Rule.getOperandMatcher(Copy->getSymbolicName());
3095         if (Insn != &OM.getInstructionMatcher() ||
3096             OM.getOpIdx() != Renderer.index())
3097           return false;
3098       } else
3099         return false;
3100     }
3101 
3102     return true;
3103   }
3104 
3105 public:
3106   BuildMIAction(unsigned InsnID, const CodeGenInstruction *I)
3107       : InsnID(InsnID), I(I), Matched(nullptr) {}
3108 
3109   unsigned getInsnID() const { return InsnID; }
3110   const CodeGenInstruction *getCGI() const { return I; }
3111 
3112   void chooseInsnToMutate(RuleMatcher &Rule) {
3113     for (auto *MutateCandidate : Rule.mutatable_insns()) {
3114       if (canMutate(Rule, MutateCandidate)) {
3115         // Take the first one we're offered that we're able to mutate.
3116         Rule.reserveInsnMatcherForMutation(MutateCandidate);
3117         Matched = MutateCandidate;
3118         return;
3119       }
3120     }
3121   }
3122 
3123   template <class Kind, class... Args>
3124   Kind &addRenderer(Args&&... args) {
3125     OperandRenderers.emplace_back(
3126         std::make_unique<Kind>(InsnID, std::forward<Args>(args)...));
3127     return *static_cast<Kind *>(OperandRenderers.back().get());
3128   }
3129 
3130   void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
3131     if (Matched) {
3132       assert(canMutate(Rule, Matched) &&
3133              "Arranged to mutate an insn that isn't mutatable");
3134 
3135       unsigned RecycleInsnID = Rule.getInsnVarID(*Matched);
3136       Table << MatchTable::Opcode("GIR_MutateOpcode")
3137             << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3138             << MatchTable::Comment("RecycleInsnID")
3139             << MatchTable::IntValue(RecycleInsnID)
3140             << MatchTable::Comment("Opcode")
3141             << MatchTable::NamedValue(I->Namespace, I->TheDef->getName())
3142             << MatchTable::LineBreak;
3143 
3144       if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) {
3145         for (auto *Def : I->ImplicitDefs) {
3146           auto Namespace = Def->getValue("Namespace")
3147                                ? Def->getValueAsString("Namespace")
3148                                : "";
3149           Table << MatchTable::Opcode("GIR_AddImplicitDef")
3150                 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3151                 << MatchTable::NamedValue(Namespace, Def->getName())
3152                 << MatchTable::LineBreak;
3153         }
3154         for (auto *Use : I->ImplicitUses) {
3155           auto Namespace = Use->getValue("Namespace")
3156                                ? Use->getValueAsString("Namespace")
3157                                : "";
3158           Table << MatchTable::Opcode("GIR_AddImplicitUse")
3159                 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3160                 << MatchTable::NamedValue(Namespace, Use->getName())
3161                 << MatchTable::LineBreak;
3162         }
3163       }
3164       return;
3165     }
3166 
3167     // TODO: Simple permutation looks like it could be almost as common as
3168     //       mutation due to commutative operations.
3169 
3170     Table << MatchTable::Opcode("GIR_BuildMI") << MatchTable::Comment("InsnID")
3171           << MatchTable::IntValue(InsnID) << MatchTable::Comment("Opcode")
3172           << MatchTable::NamedValue(I->Namespace, I->TheDef->getName())
3173           << MatchTable::LineBreak;
3174     for (const auto &Renderer : OperandRenderers)
3175       Renderer->emitRenderOpcodes(Table, Rule);
3176 
3177     if (I->mayLoad || I->mayStore) {
3178       Table << MatchTable::Opcode("GIR_MergeMemOperands")
3179             << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3180             << MatchTable::Comment("MergeInsnID's");
3181       // Emit the ID's for all the instructions that are matched by this rule.
3182       // TODO: Limit this to matched instructions that mayLoad/mayStore or have
3183       //       some other means of having a memoperand. Also limit this to
3184       //       emitted instructions that expect to have a memoperand too. For
3185       //       example, (G_SEXT (G_LOAD x)) that results in separate load and
3186       //       sign-extend instructions shouldn't put the memoperand on the
3187       //       sign-extend since it has no effect there.
3188       std::vector<unsigned> MergeInsnIDs;
3189       for (const auto &IDMatcherPair : Rule.defined_insn_vars())
3190         MergeInsnIDs.push_back(IDMatcherPair.second);
3191       llvm::sort(MergeInsnIDs);
3192       for (const auto &MergeInsnID : MergeInsnIDs)
3193         Table << MatchTable::IntValue(MergeInsnID);
3194       Table << MatchTable::NamedValue("GIU_MergeMemOperands_EndOfList")
3195             << MatchTable::LineBreak;
3196     }
3197 
3198     // FIXME: This is a hack but it's sufficient for ISel. We'll need to do
3199     //        better for combines. Particularly when there are multiple match
3200     //        roots.
3201     if (InsnID == 0)
3202       Table << MatchTable::Opcode("GIR_EraseFromParent")
3203             << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3204             << MatchTable::LineBreak;
3205   }
3206 };
3207 
3208 /// Generates code to constrain the operands of an output instruction to the
3209 /// register classes specified by the definition of that instruction.
3210 class ConstrainOperandsToDefinitionAction : public MatchAction {
3211   unsigned InsnID;
3212 
3213 public:
3214   ConstrainOperandsToDefinitionAction(unsigned InsnID) : InsnID(InsnID) {}
3215 
3216   void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
3217     Table << MatchTable::Opcode("GIR_ConstrainSelectedInstOperands")
3218           << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3219           << MatchTable::LineBreak;
3220   }
3221 };
3222 
3223 /// Generates code to constrain the specified operand of an output instruction
3224 /// to the specified register class.
3225 class ConstrainOperandToRegClassAction : public MatchAction {
3226   unsigned InsnID;
3227   unsigned OpIdx;
3228   const CodeGenRegisterClass &RC;
3229 
3230 public:
3231   ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx,
3232                                    const CodeGenRegisterClass &RC)
3233       : InsnID(InsnID), OpIdx(OpIdx), RC(RC) {}
3234 
3235   void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
3236     Table << MatchTable::Opcode("GIR_ConstrainOperandRC")
3237           << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3238           << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx)
3239           << MatchTable::NamedValue(RC.getQualifiedName() + "RegClassID")
3240           << MatchTable::LineBreak;
3241   }
3242 };
3243 
3244 /// Generates code to create a temporary register which can be used to chain
3245 /// instructions together.
3246 class MakeTempRegisterAction : public MatchAction {
3247 private:
3248   LLTCodeGen Ty;
3249   unsigned TempRegID;
3250 
3251 public:
3252   MakeTempRegisterAction(const LLTCodeGen &Ty, unsigned TempRegID)
3253       : Ty(Ty), TempRegID(TempRegID) {
3254     KnownTypes.insert(Ty);
3255   }
3256 
3257   void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override {
3258     Table << MatchTable::Opcode("GIR_MakeTempReg")
3259           << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID)
3260           << MatchTable::Comment("TypeID")
3261           << MatchTable::NamedValue(Ty.getCxxEnumValue())
3262           << MatchTable::LineBreak;
3263   }
3264 };
3265 
3266 InstructionMatcher &RuleMatcher::addInstructionMatcher(StringRef SymbolicName) {
3267   Matchers.emplace_back(new InstructionMatcher(*this, SymbolicName));
3268   MutatableInsns.insert(Matchers.back().get());
3269   return *Matchers.back();
3270 }
3271 
3272 void RuleMatcher::addRequiredFeature(Record *Feature) {
3273   RequiredFeatures.push_back(Feature);
3274 }
3275 
3276 const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const {
3277   return RequiredFeatures;
3278 }
3279 
3280 // Emplaces an action of the specified Kind at the end of the action list.
3281 //
3282 // Returns a reference to the newly created action.
3283 //
3284 // Like std::vector::emplace_back(), may invalidate all iterators if the new
3285 // size exceeds the capacity. Otherwise, only invalidates the past-the-end
3286 // iterator.
3287 template <class Kind, class... Args>
3288 Kind &RuleMatcher::addAction(Args &&... args) {
3289   Actions.emplace_back(std::make_unique<Kind>(std::forward<Args>(args)...));
3290   return *static_cast<Kind *>(Actions.back().get());
3291 }
3292 
3293 // Emplaces an action of the specified Kind before the given insertion point.
3294 //
3295 // Returns an iterator pointing at the newly created instruction.
3296 //
3297 // Like std::vector::insert(), may invalidate all iterators if the new size
3298 // exceeds the capacity. Otherwise, only invalidates the iterators from the
3299 // insertion point onwards.
3300 template <class Kind, class... Args>
3301 action_iterator RuleMatcher::insertAction(action_iterator InsertPt,
3302                                           Args &&... args) {
3303   return Actions.emplace(InsertPt,
3304                          std::make_unique<Kind>(std::forward<Args>(args)...));
3305 }
3306 
3307 unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher &Matcher) {
3308   unsigned NewInsnVarID = NextInsnVarID++;
3309   InsnVariableIDs[&Matcher] = NewInsnVarID;
3310   return NewInsnVarID;
3311 }
3312 
3313 unsigned RuleMatcher::getInsnVarID(InstructionMatcher &InsnMatcher) const {
3314   const auto &I = InsnVariableIDs.find(&InsnMatcher);
3315   if (I != InsnVariableIDs.end())
3316     return I->second;
3317   llvm_unreachable("Matched Insn was not captured in a local variable");
3318 }
3319 
3320 void RuleMatcher::defineOperand(StringRef SymbolicName, OperandMatcher &OM) {
3321   if (DefinedOperands.find(SymbolicName) == DefinedOperands.end()) {
3322     DefinedOperands[SymbolicName] = &OM;
3323     return;
3324   }
3325 
3326   // If the operand is already defined, then we must ensure both references in
3327   // the matcher have the exact same node.
3328   OM.addPredicate<SameOperandMatcher>(
3329       OM.getSymbolicName(), getOperandMatcher(OM.getSymbolicName()).getOpIdx());
3330 }
3331 
3332 void RuleMatcher::definePhysRegOperand(Record *Reg, OperandMatcher &OM) {
3333   if (PhysRegOperands.find(Reg) == PhysRegOperands.end()) {
3334     PhysRegOperands[Reg] = &OM;
3335     return;
3336   }
3337 }
3338 
3339 InstructionMatcher &
3340 RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const {
3341   for (const auto &I : InsnVariableIDs)
3342     if (I.first->getSymbolicName() == SymbolicName)
3343       return *I.first;
3344   llvm_unreachable(
3345       ("Failed to lookup instruction " + SymbolicName).str().c_str());
3346 }
3347 
3348 const OperandMatcher &
3349 RuleMatcher::getPhysRegOperandMatcher(Record *Reg) const {
3350   const auto &I = PhysRegOperands.find(Reg);
3351 
3352   if (I == PhysRegOperands.end()) {
3353     PrintFatalError(SrcLoc, "Register " + Reg->getName() +
3354                     " was not declared in matcher");
3355   }
3356 
3357   return *I->second;
3358 }
3359 
3360 const OperandMatcher &
3361 RuleMatcher::getOperandMatcher(StringRef Name) const {
3362   const auto &I = DefinedOperands.find(Name);
3363 
3364   if (I == DefinedOperands.end())
3365     PrintFatalError(SrcLoc, "Operand " + Name + " was not declared in matcher");
3366 
3367   return *I->second;
3368 }
3369 
3370 void RuleMatcher::emit(MatchTable &Table) {
3371   if (Matchers.empty())
3372     llvm_unreachable("Unexpected empty matcher!");
3373 
3374   // The representation supports rules that require multiple roots such as:
3375   //    %ptr(p0) = ...
3376   //    %elt0(s32) = G_LOAD %ptr
3377   //    %1(p0) = G_ADD %ptr, 4
3378   //    %elt1(s32) = G_LOAD p0 %1
3379   // which could be usefully folded into:
3380   //    %ptr(p0) = ...
3381   //    %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
3382   // on some targets but we don't need to make use of that yet.
3383   assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
3384 
3385   unsigned LabelID = Table.allocateLabelID();
3386   Table << MatchTable::Opcode("GIM_Try", +1)
3387         << MatchTable::Comment("On fail goto")
3388         << MatchTable::JumpTarget(LabelID)
3389         << MatchTable::Comment(("Rule ID " + Twine(RuleID) + " //").str())
3390         << MatchTable::LineBreak;
3391 
3392   if (!RequiredFeatures.empty()) {
3393     Table << MatchTable::Opcode("GIM_CheckFeatures")
3394           << MatchTable::NamedValue(getNameForFeatureBitset(RequiredFeatures))
3395           << MatchTable::LineBreak;
3396   }
3397 
3398   Matchers.front()->emitPredicateOpcodes(Table, *this);
3399 
3400   // We must also check if it's safe to fold the matched instructions.
3401   if (InsnVariableIDs.size() >= 2) {
3402     // Invert the map to create stable ordering (by var names)
3403     SmallVector<unsigned, 2> InsnIDs;
3404     for (const auto &Pair : InsnVariableIDs) {
3405       // Skip the root node since it isn't moving anywhere. Everything else is
3406       // sinking to meet it.
3407       if (Pair.first == Matchers.front().get())
3408         continue;
3409 
3410       InsnIDs.push_back(Pair.second);
3411     }
3412     llvm::sort(InsnIDs);
3413 
3414     for (const auto &InsnID : InsnIDs) {
3415       // Reject the difficult cases until we have a more accurate check.
3416       Table << MatchTable::Opcode("GIM_CheckIsSafeToFold")
3417             << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID)
3418             << MatchTable::LineBreak;
3419 
3420       // FIXME: Emit checks to determine it's _actually_ safe to fold and/or
3421       //        account for unsafe cases.
3422       //
3423       //        Example:
3424       //          MI1--> %0 = ...
3425       //                 %1 = ... %0
3426       //          MI0--> %2 = ... %0
3427       //          It's not safe to erase MI1. We currently handle this by not
3428       //          erasing %0 (even when it's dead).
3429       //
3430       //        Example:
3431       //          MI1--> %0 = load volatile @a
3432       //                 %1 = load volatile @a
3433       //          MI0--> %2 = ... %0
3434       //          It's not safe to sink %0's def past %1. We currently handle
3435       //          this by rejecting all loads.
3436       //
3437       //        Example:
3438       //          MI1--> %0 = load @a
3439       //                 %1 = store @a
3440       //          MI0--> %2 = ... %0
3441       //          It's not safe to sink %0's def past %1. We currently handle
3442       //          this by rejecting all loads.
3443       //
3444       //        Example:
3445       //                   G_CONDBR %cond, @BB1
3446       //                 BB0:
3447       //          MI1-->   %0 = load @a
3448       //                   G_BR @BB1
3449       //                 BB1:
3450       //          MI0-->   %2 = ... %0
3451       //          It's not always safe to sink %0 across control flow. In this
3452       //          case it may introduce a memory fault. We currentl handle this
3453       //          by rejecting all loads.
3454     }
3455   }
3456 
3457   for (const auto &PM : EpilogueMatchers)
3458     PM->emitPredicateOpcodes(Table, *this);
3459 
3460   for (const auto &MA : Actions)
3461     MA->emitActionOpcodes(Table, *this);
3462 
3463   if (Table.isWithCoverage())
3464     Table << MatchTable::Opcode("GIR_Coverage") << MatchTable::IntValue(RuleID)
3465           << MatchTable::LineBreak;
3466   else
3467     Table << MatchTable::Comment(("GIR_Coverage, " + Twine(RuleID) + ",").str())
3468           << MatchTable::LineBreak;
3469 
3470   Table << MatchTable::Opcode("GIR_Done", -1) << MatchTable::LineBreak
3471         << MatchTable::Label(LabelID);
3472   ++NumPatternEmitted;
3473 }
3474 
3475 bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const {
3476   // Rules involving more match roots have higher priority.
3477   if (Matchers.size() > B.Matchers.size())
3478     return true;
3479   if (Matchers.size() < B.Matchers.size())
3480     return false;
3481 
3482   for (auto Matcher : zip(Matchers, B.Matchers)) {
3483     if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher)))
3484       return true;
3485     if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher)))
3486       return false;
3487   }
3488 
3489   return false;
3490 }
3491 
3492 unsigned RuleMatcher::countRendererFns() const {
3493   return std::accumulate(
3494       Matchers.begin(), Matchers.end(), 0,
3495       [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) {
3496         return A + Matcher->countRendererFns();
3497       });
3498 }
3499 
3500 bool OperandPredicateMatcher::isHigherPriorityThan(
3501     const OperandPredicateMatcher &B) const {
3502   // Generally speaking, an instruction is more important than an Int or a
3503   // LiteralInt because it can cover more nodes but theres an exception to
3504   // this. G_CONSTANT's are less important than either of those two because they
3505   // are more permissive.
3506 
3507   const InstructionOperandMatcher *AOM =
3508       dyn_cast<InstructionOperandMatcher>(this);
3509   const InstructionOperandMatcher *BOM =
3510       dyn_cast<InstructionOperandMatcher>(&B);
3511   bool AIsConstantInsn = AOM && AOM->getInsnMatcher().isConstantInstruction();
3512   bool BIsConstantInsn = BOM && BOM->getInsnMatcher().isConstantInstruction();
3513 
3514   if (AOM && BOM) {
3515     // The relative priorities between a G_CONSTANT and any other instruction
3516     // don't actually matter but this code is needed to ensure a strict weak
3517     // ordering. This is particularly important on Windows where the rules will
3518     // be incorrectly sorted without it.
3519     if (AIsConstantInsn != BIsConstantInsn)
3520       return AIsConstantInsn < BIsConstantInsn;
3521     return false;
3522   }
3523 
3524   if (AOM && AIsConstantInsn && (B.Kind == OPM_Int || B.Kind == OPM_LiteralInt))
3525     return false;
3526   if (BOM && BIsConstantInsn && (Kind == OPM_Int || Kind == OPM_LiteralInt))
3527     return true;
3528 
3529   return Kind < B.Kind;
3530 }
3531 
3532 void SameOperandMatcher::emitPredicateOpcodes(MatchTable &Table,
3533                                               RuleMatcher &Rule) const {
3534   const OperandMatcher &OtherOM = Rule.getOperandMatcher(MatchingName);
3535   unsigned OtherInsnVarID = Rule.getInsnVarID(OtherOM.getInstructionMatcher());
3536   assert(OtherInsnVarID == OtherOM.getInstructionMatcher().getInsnVarID());
3537 
3538   Table << MatchTable::Opcode("GIM_CheckIsSameOperand")
3539         << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID)
3540         << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx)
3541         << MatchTable::Comment("OtherMI")
3542         << MatchTable::IntValue(OtherInsnVarID)
3543         << MatchTable::Comment("OtherOpIdx")
3544         << MatchTable::IntValue(OtherOM.getOpIdx())
3545         << MatchTable::LineBreak;
3546 }
3547 
3548 //===- GlobalISelEmitter class --------------------------------------------===//
3549 
3550 static Expected<LLTCodeGen> getInstResultType(const TreePatternNode *Dst) {
3551   ArrayRef<TypeSetByHwMode> ChildTypes = Dst->getExtTypes();
3552   if (ChildTypes.size() != 1)
3553     return failedImport("Dst pattern child has multiple results");
3554 
3555   std::optional<LLTCodeGen> MaybeOpTy;
3556   if (ChildTypes.front().isMachineValueType()) {
3557     MaybeOpTy =
3558       MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy);
3559   }
3560 
3561   if (!MaybeOpTy)
3562     return failedImport("Dst operand has an unsupported type");
3563   return *MaybeOpTy;
3564 }
3565 
3566 class GlobalISelEmitter {
3567 public:
3568   explicit GlobalISelEmitter(RecordKeeper &RK);
3569   void run(raw_ostream &OS);
3570 
3571 private:
3572   const RecordKeeper &RK;
3573   const CodeGenDAGPatterns CGP;
3574   const CodeGenTarget &Target;
3575   CodeGenRegBank &CGRegs;
3576 
3577   /// Keep track of the equivalence between SDNodes and Instruction by mapping
3578   /// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to
3579   /// check for attributes on the relation such as CheckMMOIsNonAtomic.
3580   /// This is defined using 'GINodeEquiv' in the target description.
3581   DenseMap<Record *, Record *> NodeEquivs;
3582 
3583   /// Keep track of the equivalence between ComplexPattern's and
3584   /// GIComplexOperandMatcher. Map entries are specified by subclassing
3585   /// GIComplexPatternEquiv.
3586   DenseMap<const Record *, const Record *> ComplexPatternEquivs;
3587 
3588   /// Keep track of the equivalence between SDNodeXForm's and
3589   /// GICustomOperandRenderer. Map entries are specified by subclassing
3590   /// GISDNodeXFormEquiv.
3591   DenseMap<const Record *, const Record *> SDNodeXFormEquivs;
3592 
3593   /// Keep track of Scores of PatternsToMatch similar to how the DAG does.
3594   /// This adds compatibility for RuleMatchers to use this for ordering rules.
3595   DenseMap<uint64_t, int> RuleMatcherScores;
3596 
3597   // Map of predicates to their subtarget features.
3598   SubtargetFeatureInfoMap SubtargetFeatures;
3599 
3600   // Rule coverage information.
3601   std::optional<CodeGenCoverage> RuleCoverage;
3602 
3603   /// Variables used to help with collecting of named operands for predicates
3604   /// with 'let PredicateCodeUsesOperands = 1'. WaitingForNamedOperands is set
3605   /// to the number of named operands that predicate expects. Store locations in
3606   /// StoreIdxForName correspond to the order in which operand names appear in
3607   /// predicate's argument list.
3608   /// When we visit named leaf operand and WaitingForNamedOperands is not zero,
3609   /// add matcher that will record operand and decrease counter.
3610   unsigned WaitingForNamedOperands = 0;
3611   StringMap<unsigned> StoreIdxForName;
3612 
3613   void gatherOpcodeValues();
3614   void gatherTypeIDValues();
3615   void gatherNodeEquivs();
3616 
3617   Record *findNodeEquiv(Record *N) const;
3618   const CodeGenInstruction *getEquivNode(Record &Equiv,
3619                                          const TreePatternNode *N) const;
3620 
3621   Error importRulePredicates(RuleMatcher &M, ArrayRef<Record *> Predicates);
3622   Expected<InstructionMatcher &>
3623   createAndImportSelDAGMatcher(RuleMatcher &Rule,
3624                                InstructionMatcher &InsnMatcher,
3625                                const TreePatternNode *Src, unsigned &TempOpIdx);
3626   Error importComplexPatternOperandMatcher(OperandMatcher &OM, Record *R,
3627                                            unsigned &TempOpIdx) const;
3628   Error importChildMatcher(RuleMatcher &Rule, InstructionMatcher &InsnMatcher,
3629                            const TreePatternNode *SrcChild,
3630                            bool OperandIsAPointer, bool OperandIsImmArg,
3631                            unsigned OpIdx, unsigned &TempOpIdx);
3632 
3633   Expected<BuildMIAction &> createAndImportInstructionRenderer(
3634       RuleMatcher &M, InstructionMatcher &InsnMatcher,
3635       const TreePatternNode *Src, const TreePatternNode *Dst);
3636   Expected<action_iterator> createAndImportSubInstructionRenderer(
3637       action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst,
3638       unsigned TempReg);
3639   Expected<action_iterator>
3640   createInstructionRenderer(action_iterator InsertPt, RuleMatcher &M,
3641                             const TreePatternNode *Dst);
3642 
3643   Expected<action_iterator>
3644   importExplicitDefRenderers(action_iterator InsertPt, RuleMatcher &M,
3645                              BuildMIAction &DstMIBuilder,
3646                              const TreePatternNode *Dst);
3647 
3648   Expected<action_iterator>
3649   importExplicitUseRenderers(action_iterator InsertPt, RuleMatcher &M,
3650                              BuildMIAction &DstMIBuilder,
3651                              const llvm::TreePatternNode *Dst);
3652   Expected<action_iterator>
3653   importExplicitUseRenderer(action_iterator InsertPt, RuleMatcher &Rule,
3654                             BuildMIAction &DstMIBuilder,
3655                             TreePatternNode *DstChild);
3656   Error importDefaultOperandRenderers(action_iterator InsertPt, RuleMatcher &M,
3657                                       BuildMIAction &DstMIBuilder,
3658                                       DagInit *DefaultOps) const;
3659   Error
3660   importImplicitDefRenderers(BuildMIAction &DstMIBuilder,
3661                              const std::vector<Record *> &ImplicitDefs) const;
3662 
3663   void emitCxxPredicateFns(raw_ostream &OS, StringRef CodeFieldName,
3664                            StringRef TypeIdentifier, StringRef ArgType,
3665                            StringRef ArgName, StringRef AdditionalArgs,
3666                            StringRef AdditionalDeclarations,
3667                            std::function<bool(const Record *R)> Filter);
3668   void emitImmPredicateFns(raw_ostream &OS, StringRef TypeIdentifier,
3669                            StringRef ArgType,
3670                            std::function<bool(const Record *R)> Filter);
3671   void emitMIPredicateFns(raw_ostream &OS);
3672 
3673   /// Analyze pattern \p P, returning a matcher for it if possible.
3674   /// Otherwise, return an Error explaining why we don't support it.
3675   Expected<RuleMatcher> runOnPattern(const PatternToMatch &P);
3676 
3677   void declareSubtargetFeature(Record *Predicate);
3678 
3679   MatchTable buildMatchTable(MutableArrayRef<RuleMatcher> Rules, bool Optimize,
3680                              bool WithCoverage);
3681 
3682   /// Infer a CodeGenRegisterClass for the type of \p SuperRegNode. The returned
3683   /// CodeGenRegisterClass will support the CodeGenRegisterClass of
3684   /// \p SubRegNode, and the subregister index defined by \p SubRegIdxNode.
3685   /// If no register class is found, return std::nullopt.
3686   std::optional<const CodeGenRegisterClass *>
3687   inferSuperRegisterClassForNode(const TypeSetByHwMode &Ty,
3688                                  TreePatternNode *SuperRegNode,
3689                                  TreePatternNode *SubRegIdxNode);
3690   std::optional<CodeGenSubRegIndex *>
3691   inferSubRegIndexForNode(TreePatternNode *SubRegIdxNode);
3692 
3693   /// Infer a CodeGenRegisterClass which suppoorts \p Ty and \p SubRegIdxNode.
3694   /// Return std::nullopt if no such class exists.
3695   std::optional<const CodeGenRegisterClass *>
3696   inferSuperRegisterClass(const TypeSetByHwMode &Ty,
3697                           TreePatternNode *SubRegIdxNode);
3698 
3699   /// Return the CodeGenRegisterClass associated with \p Leaf if it has one.
3700   std::optional<const CodeGenRegisterClass *>
3701   getRegClassFromLeaf(TreePatternNode *Leaf);
3702 
3703   /// Return a CodeGenRegisterClass for \p N if one can be found. Return
3704   /// std::nullopt otherwise.
3705   std::optional<const CodeGenRegisterClass *>
3706   inferRegClassFromPattern(TreePatternNode *N);
3707 
3708   /// Return the size of the MemoryVT in this predicate, if possible.
3709   std::optional<unsigned>
3710   getMemSizeBitsFromPredicate(const TreePredicateFn &Predicate);
3711 
3712   // Add builtin predicates.
3713   Expected<InstructionMatcher &>
3714   addBuiltinPredicates(const Record *SrcGIEquivOrNull,
3715                        const TreePredicateFn &Predicate,
3716                        InstructionMatcher &InsnMatcher, bool &HasAddedMatcher);
3717 
3718 public:
3719   /// Takes a sequence of \p Rules and group them based on the predicates
3720   /// they share. \p MatcherStorage is used as a memory container
3721   /// for the group that are created as part of this process.
3722   ///
3723   /// What this optimization does looks like if GroupT = GroupMatcher:
3724   /// Output without optimization:
3725   /// \verbatim
3726   /// # R1
3727   ///  # predicate A
3728   ///  # predicate B
3729   ///  ...
3730   /// # R2
3731   ///  # predicate A // <-- effectively this is going to be checked twice.
3732   ///                //     Once in R1 and once in R2.
3733   ///  # predicate C
3734   /// \endverbatim
3735   /// Output with optimization:
3736   /// \verbatim
3737   /// # Group1_2
3738   ///  # predicate A // <-- Check is now shared.
3739   ///  # R1
3740   ///   # predicate B
3741   ///  # R2
3742   ///   # predicate C
3743   /// \endverbatim
3744   template <class GroupT>
3745   static std::vector<Matcher *> optimizeRules(
3746       ArrayRef<Matcher *> Rules,
3747       std::vector<std::unique_ptr<Matcher>> &MatcherStorage);
3748 };
3749 
3750 void GlobalISelEmitter::gatherOpcodeValues() {
3751   InstructionOpcodeMatcher::initOpcodeValuesMap(Target);
3752 }
3753 
3754 void GlobalISelEmitter::gatherTypeIDValues() {
3755   LLTOperandMatcher::initTypeIDValuesMap();
3756 }
3757 
3758 void GlobalISelEmitter::gatherNodeEquivs() {
3759   assert(NodeEquivs.empty());
3760   for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
3761     NodeEquivs[Equiv->getValueAsDef("Node")] = Equiv;
3762 
3763   assert(ComplexPatternEquivs.empty());
3764   for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
3765     Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
3766     if (!SelDAGEquiv)
3767       continue;
3768     ComplexPatternEquivs[SelDAGEquiv] = Equiv;
3769  }
3770 
3771  assert(SDNodeXFormEquivs.empty());
3772  for (Record *Equiv : RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) {
3773    Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
3774    if (!SelDAGEquiv)
3775      continue;
3776    SDNodeXFormEquivs[SelDAGEquiv] = Equiv;
3777  }
3778 }
3779 
3780 Record *GlobalISelEmitter::findNodeEquiv(Record *N) const {
3781   return NodeEquivs.lookup(N);
3782 }
3783 
3784 const CodeGenInstruction *
3785 GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode *N) const {
3786   if (N->getNumChildren() >= 1) {
3787     // setcc operation maps to two different G_* instructions based on the type.
3788     if (!Equiv.isValueUnset("IfFloatingPoint") &&
3789         MVT(N->getChild(0)->getSimpleType(0)).isFloatingPoint())
3790       return &Target.getInstruction(Equiv.getValueAsDef("IfFloatingPoint"));
3791   }
3792 
3793   for (const TreePredicateCall &Call : N->getPredicateCalls()) {
3794     const TreePredicateFn &Predicate = Call.Fn;
3795     if (!Equiv.isValueUnset("IfSignExtend") &&
3796         (Predicate.isLoad() || Predicate.isAtomic()) &&
3797         Predicate.isSignExtLoad())
3798       return &Target.getInstruction(Equiv.getValueAsDef("IfSignExtend"));
3799     if (!Equiv.isValueUnset("IfZeroExtend") &&
3800         (Predicate.isLoad() || Predicate.isAtomic()) &&
3801         Predicate.isZeroExtLoad())
3802       return &Target.getInstruction(Equiv.getValueAsDef("IfZeroExtend"));
3803   }
3804 
3805   return &Target.getInstruction(Equiv.getValueAsDef("I"));
3806 }
3807 
3808 GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
3809     : RK(RK), CGP(RK), Target(CGP.getTargetInfo()),
3810       CGRegs(Target.getRegBank()) {}
3811 
3812 //===- Emitter ------------------------------------------------------------===//
3813 
3814 Error GlobalISelEmitter::importRulePredicates(RuleMatcher &M,
3815                                               ArrayRef<Record *> Predicates) {
3816   for (Record *Pred : Predicates) {
3817     if (Pred->getValueAsString("CondString").empty())
3818       continue;
3819     declareSubtargetFeature(Pred);
3820     M.addRequiredFeature(Pred);
3821   }
3822 
3823   return Error::success();
3824 }
3825 
3826 std::optional<unsigned> GlobalISelEmitter::getMemSizeBitsFromPredicate(
3827     const TreePredicateFn &Predicate) {
3828   std::optional<LLTCodeGen> MemTyOrNone =
3829       MVTToLLT(getValueType(Predicate.getMemoryVT()));
3830 
3831   if (!MemTyOrNone)
3832     return std::nullopt;
3833 
3834   // Align so unusual types like i1 don't get rounded down.
3835   return llvm::alignTo(
3836       static_cast<unsigned>(MemTyOrNone->get().getSizeInBits()), 8);
3837 }
3838 
3839 Expected<InstructionMatcher &> GlobalISelEmitter::addBuiltinPredicates(
3840     const Record *SrcGIEquivOrNull, const TreePredicateFn &Predicate,
3841     InstructionMatcher &InsnMatcher, bool &HasAddedMatcher) {
3842   if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) {
3843     if (const ListInit *AddrSpaces = Predicate.getAddressSpaces()) {
3844       SmallVector<unsigned, 4> ParsedAddrSpaces;
3845 
3846       for (Init *Val : AddrSpaces->getValues()) {
3847         IntInit *IntVal = dyn_cast<IntInit>(Val);
3848         if (!IntVal)
3849           return failedImport("Address space is not an integer");
3850         ParsedAddrSpaces.push_back(IntVal->getValue());
3851       }
3852 
3853       if (!ParsedAddrSpaces.empty()) {
3854         InsnMatcher.addPredicate<MemoryAddressSpacePredicateMatcher>(
3855             0, ParsedAddrSpaces);
3856         return InsnMatcher;
3857       }
3858     }
3859 
3860     int64_t MinAlign = Predicate.getMinAlignment();
3861     if (MinAlign > 0) {
3862       InsnMatcher.addPredicate<MemoryAlignmentPredicateMatcher>(0, MinAlign);
3863       return InsnMatcher;
3864     }
3865   }
3866 
3867   // G_LOAD is used for both non-extending and any-extending loads.
3868   if (Predicate.isLoad() && Predicate.isNonExtLoad()) {
3869     InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>(
3870         0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0);
3871     return InsnMatcher;
3872   }
3873   if (Predicate.isLoad() && Predicate.isAnyExtLoad()) {
3874     InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>(
3875         0, MemoryVsLLTSizePredicateMatcher::LessThan, 0);
3876     return InsnMatcher;
3877   }
3878 
3879   if (Predicate.isStore()) {
3880     if (Predicate.isTruncStore()) {
3881       if (Predicate.getMemoryVT() != nullptr) {
3882         // FIXME: If MemoryVT is set, we end up with 2 checks for the MMO size.
3883         auto MemSizeInBits = getMemSizeBitsFromPredicate(Predicate);
3884         if (!MemSizeInBits)
3885           return failedImport("MemVT could not be converted to LLT");
3886 
3887         InsnMatcher.addPredicate<MemorySizePredicateMatcher>(0, *MemSizeInBits /
3888                                                                     8);
3889       } else {
3890         InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>(
3891             0, MemoryVsLLTSizePredicateMatcher::LessThan, 0);
3892       }
3893       return InsnMatcher;
3894     }
3895     if (Predicate.isNonTruncStore()) {
3896       // We need to check the sizes match here otherwise we could incorrectly
3897       // match truncating stores with non-truncating ones.
3898       InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>(
3899           0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0);
3900     }
3901   }
3902 
3903   // No check required. We already did it by swapping the opcode.
3904   if (!SrcGIEquivOrNull->isValueUnset("IfSignExtend") &&
3905       Predicate.isSignExtLoad())
3906     return InsnMatcher;
3907 
3908   // No check required. We already did it by swapping the opcode.
3909   if (!SrcGIEquivOrNull->isValueUnset("IfZeroExtend") &&
3910       Predicate.isZeroExtLoad())
3911     return InsnMatcher;
3912 
3913   // No check required. G_STORE by itself is a non-extending store.
3914   if (Predicate.isNonTruncStore())
3915     return InsnMatcher;
3916 
3917   if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) {
3918     if (Predicate.getMemoryVT() != nullptr) {
3919       auto MemSizeInBits = getMemSizeBitsFromPredicate(Predicate);
3920       if (!MemSizeInBits)
3921         return failedImport("MemVT could not be converted to LLT");
3922 
3923       InsnMatcher.addPredicate<MemorySizePredicateMatcher>(0,
3924                                                            *MemSizeInBits / 8);
3925       return InsnMatcher;
3926     }
3927   }
3928 
3929   if (Predicate.isLoad() || Predicate.isStore()) {
3930     // No check required. A G_LOAD/G_STORE is an unindexed load.
3931     if (Predicate.isUnindexed())
3932       return InsnMatcher;
3933   }
3934 
3935   if (Predicate.isAtomic()) {
3936     if (Predicate.isAtomicOrderingMonotonic()) {
3937       InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Monotonic");
3938       return InsnMatcher;
3939     }
3940     if (Predicate.isAtomicOrderingAcquire()) {
3941       InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Acquire");
3942       return InsnMatcher;
3943     }
3944     if (Predicate.isAtomicOrderingRelease()) {
3945       InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Release");
3946       return InsnMatcher;
3947     }
3948     if (Predicate.isAtomicOrderingAcquireRelease()) {
3949       InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>(
3950           "AcquireRelease");
3951       return InsnMatcher;
3952     }
3953     if (Predicate.isAtomicOrderingSequentiallyConsistent()) {
3954       InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>(
3955           "SequentiallyConsistent");
3956       return InsnMatcher;
3957     }
3958   }
3959 
3960   if (Predicate.isAtomicOrderingAcquireOrStronger()) {
3961     InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>(
3962         "Acquire", AtomicOrderingMMOPredicateMatcher::AO_OrStronger);
3963     return InsnMatcher;
3964   }
3965   if (Predicate.isAtomicOrderingWeakerThanAcquire()) {
3966     InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>(
3967         "Acquire", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan);
3968     return InsnMatcher;
3969   }
3970 
3971   if (Predicate.isAtomicOrderingReleaseOrStronger()) {
3972     InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>(
3973         "Release", AtomicOrderingMMOPredicateMatcher::AO_OrStronger);
3974     return InsnMatcher;
3975   }
3976   if (Predicate.isAtomicOrderingWeakerThanRelease()) {
3977     InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>(
3978         "Release", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan);
3979     return InsnMatcher;
3980   }
3981   HasAddedMatcher = false;
3982   return InsnMatcher;
3983 }
3984 
3985 Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
3986     RuleMatcher &Rule, InstructionMatcher &InsnMatcher,
3987     const TreePatternNode *Src, unsigned &TempOpIdx) {
3988   Record *SrcGIEquivOrNull = nullptr;
3989   const CodeGenInstruction *SrcGIOrNull = nullptr;
3990 
3991   // Start with the defined operands (i.e., the results of the root operator).
3992   if (Src->getExtTypes().size() > 1)
3993     return failedImport("Src pattern has multiple results");
3994 
3995   if (Src->isLeaf()) {
3996     Init *SrcInit = Src->getLeafValue();
3997     if (isa<IntInit>(SrcInit)) {
3998       InsnMatcher.addPredicate<InstructionOpcodeMatcher>(
3999           &Target.getInstruction(RK.getDef("G_CONSTANT")));
4000     } else
4001       return failedImport(
4002           "Unable to deduce gMIR opcode to handle Src (which is a leaf)");
4003   } else {
4004     SrcGIEquivOrNull = findNodeEquiv(Src->getOperator());
4005     if (!SrcGIEquivOrNull)
4006       return failedImport("Pattern operator lacks an equivalent Instruction" +
4007                           explainOperator(Src->getOperator()));
4008     SrcGIOrNull = getEquivNode(*SrcGIEquivOrNull, Src);
4009 
4010     // The operators look good: match the opcode
4011     InsnMatcher.addPredicate<InstructionOpcodeMatcher>(SrcGIOrNull);
4012   }
4013 
4014   unsigned OpIdx = 0;
4015   for (const TypeSetByHwMode &VTy : Src->getExtTypes()) {
4016     // Results don't have a name unless they are the root node. The caller will
4017     // set the name if appropriate.
4018     const bool OperandIsAPointer =
4019         SrcGIOrNull && SrcGIOrNull->isOutOperandAPointer(OpIdx);
4020     OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx);
4021     if (auto Error = OM.addTypeCheckPredicate(VTy, OperandIsAPointer))
4022       return failedImport(toString(std::move(Error)) +
4023                           " for result of Src pattern operator");
4024   }
4025 
4026   for (const TreePredicateCall &Call : Src->getPredicateCalls()) {
4027     const TreePredicateFn &Predicate = Call.Fn;
4028     bool HasAddedBuiltinMatcher = true;
4029     if (Predicate.isAlwaysTrue())
4030       continue;
4031 
4032     if (Predicate.isImmediatePattern()) {
4033       InsnMatcher.addPredicate<InstructionImmPredicateMatcher>(Predicate);
4034       continue;
4035     }
4036 
4037     auto InsnMatcherOrError = addBuiltinPredicates(
4038         SrcGIEquivOrNull, Predicate, InsnMatcher, HasAddedBuiltinMatcher);
4039     if (auto Error = InsnMatcherOrError.takeError())
4040       return std::move(Error);
4041 
4042     // FIXME: This should be part of addBuiltinPredicates(). If we add this at
4043     // the start of addBuiltinPredicates() without returning, then there might
4044     // be cases where we hit the last return before which the
4045     // HasAddedBuiltinMatcher will be set to false. The predicate could be
4046     // missed if we add it in the middle or at the end due to return statements
4047     // after the addPredicate<>() calls.
4048     if (Predicate.hasNoUse()) {
4049       InsnMatcher.addPredicate<NoUsePredicateMatcher>();
4050       HasAddedBuiltinMatcher = true;
4051     }
4052 
4053     if (Predicate.hasGISelPredicateCode()) {
4054       if (Predicate.usesOperands()) {
4055         assert(WaitingForNamedOperands == 0 &&
4056                "previous predicate didn't find all operands or "
4057                "nested predicate that uses operands");
4058         TreePattern *TP = Predicate.getOrigPatFragRecord();
4059         WaitingForNamedOperands = TP->getNumArgs();
4060         for (unsigned i = 0; i < WaitingForNamedOperands; ++i)
4061           StoreIdxForName[getScopedName(Call.Scope, TP->getArgName(i))] = i;
4062       }
4063       InsnMatcher.addPredicate<GenericInstructionPredicateMatcher>(Predicate);
4064       continue;
4065     }
4066     if (!HasAddedBuiltinMatcher) {
4067       return failedImport("Src pattern child has predicate (" +
4068                           explainPredicates(Src) + ")");
4069     }
4070   }
4071 
4072   bool IsAtomic = false;
4073   if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsNonAtomic"))
4074     InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("NotAtomic");
4075   else if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsAtomic")) {
4076     IsAtomic = true;
4077     InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>(
4078       "Unordered", AtomicOrderingMMOPredicateMatcher::AO_OrStronger);
4079   }
4080 
4081   if (Src->isLeaf()) {
4082     Init *SrcInit = Src->getLeafValue();
4083     if (IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) {
4084       OperandMatcher &OM =
4085           InsnMatcher.addOperand(OpIdx++, Src->getName(), TempOpIdx);
4086       OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue());
4087     } else
4088       return failedImport(
4089           "Unable to deduce gMIR opcode to handle Src (which is a leaf)");
4090   } else {
4091     assert(SrcGIOrNull &&
4092            "Expected to have already found an equivalent Instruction");
4093     if (SrcGIOrNull->TheDef->getName() == "G_CONSTANT" ||
4094         SrcGIOrNull->TheDef->getName() == "G_FCONSTANT") {
4095       // imm/fpimm still have operands but we don't need to do anything with it
4096       // here since we don't support ImmLeaf predicates yet. However, we still
4097       // need to note the hidden operand to get GIM_CheckNumOperands correct.
4098       InsnMatcher.addOperand(OpIdx++, "", TempOpIdx);
4099       return InsnMatcher;
4100     }
4101 
4102     // Special case because the operand order is changed from setcc. The
4103     // predicate operand needs to be swapped from the last operand to the first
4104     // source.
4105 
4106     unsigned NumChildren = Src->getNumChildren();
4107     bool IsFCmp = SrcGIOrNull->TheDef->getName() == "G_FCMP";
4108 
4109     if (IsFCmp || SrcGIOrNull->TheDef->getName() == "G_ICMP") {
4110       TreePatternNode *SrcChild = Src->getChild(NumChildren - 1);
4111       if (SrcChild->isLeaf()) {
4112         DefInit *DI = dyn_cast<DefInit>(SrcChild->getLeafValue());
4113         Record *CCDef = DI ? DI->getDef() : nullptr;
4114         if (!CCDef || !CCDef->isSubClassOf("CondCode"))
4115           return failedImport("Unable to handle CondCode");
4116 
4117         OperandMatcher &OM =
4118           InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx);
4119         StringRef PredType = IsFCmp ? CCDef->getValueAsString("FCmpPredicate") :
4120                                       CCDef->getValueAsString("ICmpPredicate");
4121 
4122         if (!PredType.empty()) {
4123           OM.addPredicate<CmpPredicateOperandMatcher>(std::string(PredType));
4124           // Process the other 2 operands normally.
4125           --NumChildren;
4126         }
4127       }
4128     }
4129 
4130     // Hack around an unfortunate mistake in how atomic store (and really
4131     // atomicrmw in general) operands were ordered. A ISD::STORE used the order
4132     // <stored value>, <pointer> order. ISD::ATOMIC_STORE used the opposite,
4133     // <pointer>, <stored value>. In GlobalISel there's just the one store
4134     // opcode, so we need to swap the operands here to get the right type check.
4135     if (IsAtomic && SrcGIOrNull->TheDef->getName() == "G_STORE") {
4136       assert(NumChildren == 2 && "wrong operands for atomic store");
4137 
4138       TreePatternNode *PtrChild = Src->getChild(0);
4139       TreePatternNode *ValueChild = Src->getChild(1);
4140 
4141       if (auto Error = importChildMatcher(Rule, InsnMatcher, PtrChild, true,
4142                                           false, 1, TempOpIdx))
4143         return std::move(Error);
4144 
4145       if (auto Error = importChildMatcher(Rule, InsnMatcher, ValueChild, false,
4146                                           false, 0, TempOpIdx))
4147         return std::move(Error);
4148       return InsnMatcher;
4149     }
4150 
4151     // Match the used operands (i.e. the children of the operator).
4152     bool IsIntrinsic =
4153         SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" ||
4154         SrcGIOrNull->TheDef->getName() == "G_INTRINSIC_W_SIDE_EFFECTS";
4155     const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP);
4156     if (IsIntrinsic && !II)
4157       return failedImport("Expected IntInit containing intrinsic ID)");
4158 
4159     for (unsigned i = 0; i != NumChildren; ++i) {
4160       TreePatternNode *SrcChild = Src->getChild(i);
4161 
4162       // We need to determine the meaning of a literal integer based on the
4163       // context. If this is a field required to be an immediate (such as an
4164       // immarg intrinsic argument), the required predicates are different than
4165       // a constant which may be materialized in a register. If we have an
4166       // argument that is required to be an immediate, we should not emit an LLT
4167       // type check, and should not be looking for a G_CONSTANT defined
4168       // register.
4169       bool OperandIsImmArg = SrcGIOrNull->isInOperandImmArg(i);
4170 
4171       // SelectionDAG allows pointers to be represented with iN since it doesn't
4172       // distinguish between pointers and integers but they are different types in GlobalISel.
4173       // Coerce integers to pointers to address space 0 if the context indicates a pointer.
4174       //
4175       bool OperandIsAPointer = SrcGIOrNull->isInOperandAPointer(i);
4176 
4177       if (IsIntrinsic) {
4178         // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately
4179         // following the defs is an intrinsic ID.
4180         if (i == 0) {
4181           OperandMatcher &OM =
4182               InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx);
4183           OM.addPredicate<IntrinsicIDOperandMatcher>(II);
4184           continue;
4185         }
4186 
4187         // We have to check intrinsics for llvm_anyptr_ty and immarg parameters.
4188         //
4189         // Note that we have to look at the i-1th parameter, because we don't
4190         // have the intrinsic ID in the intrinsic's parameter list.
4191         OperandIsAPointer |= II->isParamAPointer(i - 1);
4192         OperandIsImmArg |= II->isParamImmArg(i - 1);
4193       }
4194 
4195       if (auto Error =
4196               importChildMatcher(Rule, InsnMatcher, SrcChild, OperandIsAPointer,
4197                                  OperandIsImmArg, OpIdx++, TempOpIdx))
4198         return std::move(Error);
4199     }
4200   }
4201 
4202   return InsnMatcher;
4203 }
4204 
4205 Error GlobalISelEmitter::importComplexPatternOperandMatcher(
4206     OperandMatcher &OM, Record *R, unsigned &TempOpIdx) const {
4207   const auto &ComplexPattern = ComplexPatternEquivs.find(R);
4208   if (ComplexPattern == ComplexPatternEquivs.end())
4209     return failedImport("SelectionDAG ComplexPattern (" + R->getName() +
4210                         ") not mapped to GlobalISel");
4211 
4212   OM.addPredicate<ComplexPatternOperandMatcher>(OM, *ComplexPattern->second);
4213   TempOpIdx++;
4214   return Error::success();
4215 }
4216 
4217 // Get the name to use for a pattern operand. For an anonymous physical register
4218 // input, this should use the register name.
4219 static StringRef getSrcChildName(const TreePatternNode *SrcChild,
4220                                  Record *&PhysReg) {
4221   StringRef SrcChildName = SrcChild->getName();
4222   if (SrcChildName.empty() && SrcChild->isLeaf()) {
4223     if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
4224       auto *ChildRec = ChildDefInit->getDef();
4225       if (ChildRec->isSubClassOf("Register")) {
4226         SrcChildName = ChildRec->getName();
4227         PhysReg = ChildRec;
4228       }
4229     }
4230   }
4231 
4232   return SrcChildName;
4233 }
4234 
4235 Error GlobalISelEmitter::importChildMatcher(
4236     RuleMatcher &Rule, InstructionMatcher &InsnMatcher,
4237     const TreePatternNode *SrcChild, bool OperandIsAPointer,
4238     bool OperandIsImmArg, unsigned OpIdx, unsigned &TempOpIdx) {
4239 
4240   Record *PhysReg = nullptr;
4241   std::string SrcChildName = std::string(getSrcChildName(SrcChild, PhysReg));
4242   if (!SrcChild->isLeaf() &&
4243       SrcChild->getOperator()->isSubClassOf("ComplexPattern")) {
4244     // The "name" of a non-leaf complex pattern (MY_PAT $op1, $op2) is
4245     // "MY_PAT:op1:op2" and the ones with same "name" represent same operand.
4246     std::string PatternName = std::string(SrcChild->getOperator()->getName());
4247     for (unsigned i = 0; i < SrcChild->getNumChildren(); ++i) {
4248       PatternName += ":";
4249       PatternName += SrcChild->getChild(i)->getName();
4250     }
4251     SrcChildName = PatternName;
4252   }
4253 
4254   OperandMatcher &OM =
4255       PhysReg ? InsnMatcher.addPhysRegInput(PhysReg, OpIdx, TempOpIdx)
4256               : InsnMatcher.addOperand(OpIdx, SrcChildName, TempOpIdx);
4257   if (OM.isSameAsAnotherOperand())
4258     return Error::success();
4259 
4260   ArrayRef<TypeSetByHwMode> ChildTypes = SrcChild->getExtTypes();
4261   if (ChildTypes.size() != 1)
4262     return failedImport("Src pattern child has multiple results");
4263 
4264   // Check MBB's before the type check since they are not a known type.
4265   if (!SrcChild->isLeaf()) {
4266     if (SrcChild->getOperator()->isSubClassOf("SDNode")) {
4267       auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator());
4268       if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
4269         OM.addPredicate<MBBOperandMatcher>();
4270         return Error::success();
4271       }
4272       if (SrcChild->getOperator()->getName() == "timm") {
4273         OM.addPredicate<ImmOperandMatcher>();
4274 
4275         // Add predicates, if any
4276         for (const TreePredicateCall &Call : SrcChild->getPredicateCalls()) {
4277           const TreePredicateFn &Predicate = Call.Fn;
4278 
4279           // Only handle immediate patterns for now
4280           if (Predicate.isImmediatePattern()) {
4281             OM.addPredicate<OperandImmPredicateMatcher>(Predicate);
4282           }
4283         }
4284 
4285         return Error::success();
4286       }
4287     }
4288   }
4289 
4290   // Immediate arguments have no meaningful type to check as they don't have
4291   // registers.
4292   if (!OperandIsImmArg) {
4293     if (auto Error =
4294             OM.addTypeCheckPredicate(ChildTypes.front(), OperandIsAPointer))
4295       return failedImport(toString(std::move(Error)) + " for Src operand (" +
4296                           to_string(*SrcChild) + ")");
4297   }
4298 
4299   // Check for nested instructions.
4300   if (!SrcChild->isLeaf()) {
4301     if (SrcChild->getOperator()->isSubClassOf("ComplexPattern")) {
4302       // When a ComplexPattern is used as an operator, it should do the same
4303       // thing as when used as a leaf. However, the children of the operator
4304       // name the sub-operands that make up the complex operand and we must
4305       // prepare to reference them in the renderer too.
4306       unsigned RendererID = TempOpIdx;
4307       if (auto Error = importComplexPatternOperandMatcher(
4308               OM, SrcChild->getOperator(), TempOpIdx))
4309         return Error;
4310 
4311       for (unsigned i = 0, e = SrcChild->getNumChildren(); i != e; ++i) {
4312         auto *SubOperand = SrcChild->getChild(i);
4313         if (!SubOperand->getName().empty()) {
4314           if (auto Error = Rule.defineComplexSubOperand(
4315                   SubOperand->getName(), SrcChild->getOperator(), RendererID, i,
4316                   SrcChildName))
4317             return Error;
4318         }
4319       }
4320 
4321       return Error::success();
4322     }
4323 
4324     auto MaybeInsnOperand = OM.addPredicate<InstructionOperandMatcher>(
4325         InsnMatcher.getRuleMatcher(), SrcChild->getName());
4326     if (!MaybeInsnOperand) {
4327       // This isn't strictly true. If the user were to provide exactly the same
4328       // matchers as the original operand then we could allow it. However, it's
4329       // simpler to not permit the redundant specification.
4330       return failedImport("Nested instruction cannot be the same as another operand");
4331     }
4332 
4333     // Map the node to a gMIR instruction.
4334     InstructionOperandMatcher &InsnOperand = **MaybeInsnOperand;
4335     auto InsnMatcherOrError = createAndImportSelDAGMatcher(
4336         Rule, InsnOperand.getInsnMatcher(), SrcChild, TempOpIdx);
4337     if (auto Error = InsnMatcherOrError.takeError())
4338       return Error;
4339 
4340     return Error::success();
4341   }
4342 
4343   if (SrcChild->hasAnyPredicate())
4344     return failedImport("Src pattern child has unsupported predicate");
4345 
4346   // Check for constant immediates.
4347   if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) {
4348     if (OperandIsImmArg) {
4349       // Checks for argument directly in operand list
4350       OM.addPredicate<LiteralIntOperandMatcher>(ChildInt->getValue());
4351     } else {
4352       // Checks for materialized constant
4353       OM.addPredicate<ConstantIntOperandMatcher>(ChildInt->getValue());
4354     }
4355     return Error::success();
4356   }
4357 
4358   // Check for def's like register classes or ComplexPattern's.
4359   if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
4360     auto *ChildRec = ChildDefInit->getDef();
4361 
4362     if (WaitingForNamedOperands) {
4363       auto PA = SrcChild->getNamesAsPredicateArg().begin();
4364       std::string Name = getScopedName(PA->getScope(), PA->getIdentifier());
4365       OM.addPredicate<RecordNamedOperandMatcher>(StoreIdxForName[Name], Name);
4366       --WaitingForNamedOperands;
4367     }
4368 
4369     // Check for register classes.
4370     if (ChildRec->isSubClassOf("RegisterClass") ||
4371         ChildRec->isSubClassOf("RegisterOperand")) {
4372       OM.addPredicate<RegisterBankOperandMatcher>(
4373           Target.getRegisterClass(getInitValueAsRegClass(ChildDefInit)));
4374       return Error::success();
4375     }
4376 
4377     if (ChildRec->isSubClassOf("Register")) {
4378       // This just be emitted as a copy to the specific register.
4379       ValueTypeByHwMode VT = ChildTypes.front().getValueTypeByHwMode();
4380       const CodeGenRegisterClass *RC
4381         = CGRegs.getMinimalPhysRegClass(ChildRec, &VT);
4382       if (!RC) {
4383         return failedImport(
4384           "Could not determine physical register class of pattern source");
4385       }
4386 
4387       OM.addPredicate<RegisterBankOperandMatcher>(*RC);
4388       return Error::success();
4389     }
4390 
4391     // Check for ValueType.
4392     if (ChildRec->isSubClassOf("ValueType")) {
4393       // We already added a type check as standard practice so this doesn't need
4394       // to do anything.
4395       return Error::success();
4396     }
4397 
4398     // Check for ComplexPattern's.
4399     if (ChildRec->isSubClassOf("ComplexPattern"))
4400       return importComplexPatternOperandMatcher(OM, ChildRec, TempOpIdx);
4401 
4402     if (ChildRec->isSubClassOf("ImmLeaf")) {
4403       return failedImport(
4404           "Src pattern child def is an unsupported tablegen class (ImmLeaf)");
4405     }
4406 
4407     // Place holder for SRCVALUE nodes. Nothing to do here.
4408     if (ChildRec->getName() == "srcvalue")
4409       return Error::success();
4410 
4411     const bool ImmAllOnesV = ChildRec->getName() == "immAllOnesV";
4412     if (ImmAllOnesV || ChildRec->getName() == "immAllZerosV") {
4413       auto MaybeInsnOperand = OM.addPredicate<InstructionOperandMatcher>(
4414           InsnMatcher.getRuleMatcher(), SrcChild->getName(), false);
4415       InstructionOperandMatcher &InsnOperand = **MaybeInsnOperand;
4416 
4417       ValueTypeByHwMode VTy = ChildTypes.front().getValueTypeByHwMode();
4418 
4419       const CodeGenInstruction &BuildVector
4420         = Target.getInstruction(RK.getDef("G_BUILD_VECTOR"));
4421       const CodeGenInstruction &BuildVectorTrunc
4422         = Target.getInstruction(RK.getDef("G_BUILD_VECTOR_TRUNC"));
4423 
4424       // Treat G_BUILD_VECTOR as the canonical opcode, and G_BUILD_VECTOR_TRUNC
4425       // as an alternative.
4426       InsnOperand.getInsnMatcher().addPredicate<InstructionOpcodeMatcher>(
4427           ArrayRef({&BuildVector, &BuildVectorTrunc}));
4428 
4429       // TODO: Handle both G_BUILD_VECTOR and G_BUILD_VECTOR_TRUNC We could
4430       // theoretically not emit any opcode check, but getOpcodeMatcher currently
4431       // has to succeed.
4432       OperandMatcher &OM =
4433           InsnOperand.getInsnMatcher().addOperand(0, "", TempOpIdx);
4434       if (auto Error =
4435               OM.addTypeCheckPredicate(VTy, false /* OperandIsAPointer */))
4436         return failedImport(toString(std::move(Error)) +
4437                             " for result of Src pattern operator");
4438 
4439       InsnOperand.getInsnMatcher().addPredicate<VectorSplatImmPredicateMatcher>(
4440           ImmAllOnesV ? VectorSplatImmPredicateMatcher::AllOnes
4441                       : VectorSplatImmPredicateMatcher::AllZeros);
4442       return Error::success();
4443     }
4444 
4445     return failedImport(
4446         "Src pattern child def is an unsupported tablegen class");
4447   }
4448 
4449   return failedImport("Src pattern child is an unsupported kind");
4450 }
4451 
4452 Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer(
4453     action_iterator InsertPt, RuleMatcher &Rule, BuildMIAction &DstMIBuilder,
4454     TreePatternNode *DstChild) {
4455 
4456   const auto &SubOperand = Rule.getComplexSubOperand(DstChild->getName());
4457   if (SubOperand) {
4458     DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
4459         *std::get<0>(*SubOperand), DstChild->getName(),
4460         std::get<1>(*SubOperand), std::get<2>(*SubOperand));
4461     return InsertPt;
4462   }
4463 
4464   if (!DstChild->isLeaf()) {
4465     if (DstChild->getOperator()->isSubClassOf("SDNodeXForm")) {
4466       auto Child = DstChild->getChild(0);
4467       auto I = SDNodeXFormEquivs.find(DstChild->getOperator());
4468       if (I != SDNodeXFormEquivs.end()) {
4469         Record *XFormOpc = DstChild->getOperator()->getValueAsDef("Opcode");
4470         if (XFormOpc->getName() == "timm") {
4471           // If this is a TargetConstant, there won't be a corresponding
4472           // instruction to transform. Instead, this will refer directly to an
4473           // operand in an instruction's operand list.
4474           DstMIBuilder.addRenderer<CustomOperandRenderer>(*I->second,
4475                                                           Child->getName());
4476         } else {
4477           DstMIBuilder.addRenderer<CustomRenderer>(*I->second,
4478                                                    Child->getName());
4479         }
4480 
4481         return InsertPt;
4482       }
4483       return failedImport("SDNodeXForm " + Child->getName() +
4484                           " has no custom renderer");
4485     }
4486 
4487     // We accept 'bb' here. It's an operator because BasicBlockSDNode isn't
4488     // inline, but in MI it's just another operand.
4489     if (DstChild->getOperator()->isSubClassOf("SDNode")) {
4490       auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator());
4491       if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
4492         DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName());
4493         return InsertPt;
4494       }
4495     }
4496 
4497     // Similarly, imm is an operator in TreePatternNode's view but must be
4498     // rendered as operands.
4499     // FIXME: The target should be able to choose sign-extended when appropriate
4500     //        (e.g. on Mips).
4501     if (DstChild->getOperator()->getName() == "timm") {
4502       DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName());
4503       return InsertPt;
4504     } else if (DstChild->getOperator()->getName() == "imm") {
4505       DstMIBuilder.addRenderer<CopyConstantAsImmRenderer>(DstChild->getName());
4506       return InsertPt;
4507     } else if (DstChild->getOperator()->getName() == "fpimm") {
4508       DstMIBuilder.addRenderer<CopyFConstantAsFPImmRenderer>(
4509           DstChild->getName());
4510       return InsertPt;
4511     }
4512 
4513     if (DstChild->getOperator()->isSubClassOf("Instruction")) {
4514       auto OpTy = getInstResultType(DstChild);
4515       if (!OpTy)
4516         return OpTy.takeError();
4517 
4518       unsigned TempRegID = Rule.allocateTempRegID();
4519       InsertPt = Rule.insertAction<MakeTempRegisterAction>(
4520           InsertPt, *OpTy, TempRegID);
4521       DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID);
4522 
4523       auto InsertPtOrError = createAndImportSubInstructionRenderer(
4524           ++InsertPt, Rule, DstChild, TempRegID);
4525       if (auto Error = InsertPtOrError.takeError())
4526         return std::move(Error);
4527       return InsertPtOrError.get();
4528     }
4529 
4530     return failedImport("Dst pattern child isn't a leaf node or an MBB" + llvm::to_string(*DstChild));
4531   }
4532 
4533   // It could be a specific immediate in which case we should just check for
4534   // that immediate.
4535   if (const IntInit *ChildIntInit =
4536           dyn_cast<IntInit>(DstChild->getLeafValue())) {
4537     DstMIBuilder.addRenderer<ImmRenderer>(ChildIntInit->getValue());
4538     return InsertPt;
4539   }
4540 
4541   // Otherwise, we're looking for a bog-standard RegisterClass operand.
4542   if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) {
4543     auto *ChildRec = ChildDefInit->getDef();
4544 
4545     ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes();
4546     if (ChildTypes.size() != 1)
4547       return failedImport("Dst pattern child has multiple results");
4548 
4549     std::optional<LLTCodeGen> OpTyOrNone;
4550     if (ChildTypes.front().isMachineValueType())
4551       OpTyOrNone = MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy);
4552     if (!OpTyOrNone)
4553       return failedImport("Dst operand has an unsupported type");
4554 
4555     if (ChildRec->isSubClassOf("Register")) {
4556       DstMIBuilder.addRenderer<AddRegisterRenderer>(Target, ChildRec);
4557       return InsertPt;
4558     }
4559 
4560     if (ChildRec->isSubClassOf("RegisterClass") ||
4561         ChildRec->isSubClassOf("RegisterOperand") ||
4562         ChildRec->isSubClassOf("ValueType")) {
4563       if (ChildRec->isSubClassOf("RegisterOperand") &&
4564           !ChildRec->isValueUnset("GIZeroRegister")) {
4565         DstMIBuilder.addRenderer<CopyOrAddZeroRegRenderer>(
4566             DstChild->getName(), ChildRec->getValueAsDef("GIZeroRegister"));
4567         return InsertPt;
4568       }
4569 
4570       DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName());
4571       return InsertPt;
4572     }
4573 
4574     if (ChildRec->isSubClassOf("SubRegIndex")) {
4575       CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(ChildRec);
4576       DstMIBuilder.addRenderer<ImmRenderer>(SubIdx->EnumValue);
4577       return InsertPt;
4578     }
4579 
4580     if (ChildRec->isSubClassOf("ComplexPattern")) {
4581       const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
4582       if (ComplexPattern == ComplexPatternEquivs.end())
4583         return failedImport(
4584             "SelectionDAG ComplexPattern not mapped to GlobalISel");
4585 
4586       const OperandMatcher &OM = Rule.getOperandMatcher(DstChild->getName());
4587       DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
4588           *ComplexPattern->second, DstChild->getName(),
4589           OM.getAllocatedTemporariesBaseID());
4590       return InsertPt;
4591     }
4592 
4593     return failedImport(
4594         "Dst pattern child def is an unsupported tablegen class");
4595   }
4596   return failedImport("Dst pattern child is an unsupported kind");
4597 }
4598 
4599 Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer(
4600     RuleMatcher &M, InstructionMatcher &InsnMatcher, const TreePatternNode *Src,
4601     const TreePatternNode *Dst) {
4602   auto InsertPtOrError = createInstructionRenderer(M.actions_end(), M, Dst);
4603   if (auto Error = InsertPtOrError.takeError())
4604     return std::move(Error);
4605 
4606   action_iterator InsertPt = InsertPtOrError.get();
4607   BuildMIAction &DstMIBuilder = *static_cast<BuildMIAction *>(InsertPt->get());
4608 
4609   for (auto PhysInput : InsnMatcher.getPhysRegInputs()) {
4610     InsertPt = M.insertAction<BuildMIAction>(
4611         InsertPt, M.allocateOutputInsnID(),
4612         &Target.getInstruction(RK.getDef("COPY")));
4613     BuildMIAction &CopyToPhysRegMIBuilder =
4614         *static_cast<BuildMIAction *>(InsertPt->get());
4615     CopyToPhysRegMIBuilder.addRenderer<AddRegisterRenderer>(Target,
4616                                                             PhysInput.first,
4617                                                             true);
4618     CopyToPhysRegMIBuilder.addRenderer<CopyPhysRegRenderer>(PhysInput.first);
4619   }
4620 
4621   if (auto Error = importExplicitDefRenderers(InsertPt, M, DstMIBuilder, Dst)
4622                        .takeError())
4623     return std::move(Error);
4624 
4625   if (auto Error = importExplicitUseRenderers(InsertPt, M, DstMIBuilder, Dst)
4626                        .takeError())
4627     return std::move(Error);
4628 
4629   return DstMIBuilder;
4630 }
4631 
4632 Expected<action_iterator>
4633 GlobalISelEmitter::createAndImportSubInstructionRenderer(
4634     const action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst,
4635     unsigned TempRegID) {
4636   auto InsertPtOrError = createInstructionRenderer(InsertPt, M, Dst);
4637 
4638   // TODO: Assert there's exactly one result.
4639 
4640   if (auto Error = InsertPtOrError.takeError())
4641     return std::move(Error);
4642 
4643   BuildMIAction &DstMIBuilder =
4644       *static_cast<BuildMIAction *>(InsertPtOrError.get()->get());
4645 
4646   // Assign the result to TempReg.
4647   DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID, true);
4648 
4649   InsertPtOrError =
4650       importExplicitUseRenderers(InsertPtOrError.get(), M, DstMIBuilder, Dst);
4651   if (auto Error = InsertPtOrError.takeError())
4652     return std::move(Error);
4653 
4654   // We need to make sure that when we import an INSERT_SUBREG as a
4655   // subinstruction that it ends up being constrained to the correct super
4656   // register and subregister classes.
4657   auto OpName = Target.getInstruction(Dst->getOperator()).TheDef->getName();
4658   if (OpName == "INSERT_SUBREG") {
4659     auto SubClass = inferRegClassFromPattern(Dst->getChild(1));
4660     if (!SubClass)
4661       return failedImport(
4662           "Cannot infer register class from INSERT_SUBREG operand #1");
4663     std::optional<const CodeGenRegisterClass *> SuperClass =
4664         inferSuperRegisterClassForNode(Dst->getExtType(0), Dst->getChild(0),
4665                                        Dst->getChild(2));
4666     if (!SuperClass)
4667       return failedImport(
4668           "Cannot infer register class for INSERT_SUBREG operand #0");
4669     // The destination and the super register source of an INSERT_SUBREG must
4670     // be the same register class.
4671     M.insertAction<ConstrainOperandToRegClassAction>(
4672         InsertPt, DstMIBuilder.getInsnID(), 0, **SuperClass);
4673     M.insertAction<ConstrainOperandToRegClassAction>(
4674         InsertPt, DstMIBuilder.getInsnID(), 1, **SuperClass);
4675     M.insertAction<ConstrainOperandToRegClassAction>(
4676         InsertPt, DstMIBuilder.getInsnID(), 2, **SubClass);
4677     return InsertPtOrError.get();
4678   }
4679 
4680   if (OpName == "EXTRACT_SUBREG") {
4681     // EXTRACT_SUBREG selects into a subregister COPY but unlike most
4682     // instructions, the result register class is controlled by the
4683     // subregisters of the operand. As a result, we must constrain the result
4684     // class rather than check that it's already the right one.
4685     auto SuperClass = inferRegClassFromPattern(Dst->getChild(0));
4686     if (!SuperClass)
4687       return failedImport(
4688         "Cannot infer register class from EXTRACT_SUBREG operand #0");
4689 
4690     auto SubIdx = inferSubRegIndexForNode(Dst->getChild(1));
4691     if (!SubIdx)
4692       return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
4693 
4694     const auto SrcRCDstRCPair =
4695       (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx);
4696     assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass");
4697     M.insertAction<ConstrainOperandToRegClassAction>(
4698       InsertPt, DstMIBuilder.getInsnID(), 0, *SrcRCDstRCPair->second);
4699     M.insertAction<ConstrainOperandToRegClassAction>(
4700       InsertPt, DstMIBuilder.getInsnID(), 1, *SrcRCDstRCPair->first);
4701 
4702     // We're done with this pattern!  It's eligible for GISel emission; return
4703     // it.
4704     return InsertPtOrError.get();
4705   }
4706 
4707   // Similar to INSERT_SUBREG, we also have to handle SUBREG_TO_REG as a
4708   // subinstruction.
4709   if (OpName == "SUBREG_TO_REG") {
4710     auto SubClass = inferRegClassFromPattern(Dst->getChild(1));
4711     if (!SubClass)
4712       return failedImport(
4713         "Cannot infer register class from SUBREG_TO_REG child #1");
4714     auto SuperClass = inferSuperRegisterClass(Dst->getExtType(0),
4715                                               Dst->getChild(2));
4716     if (!SuperClass)
4717       return failedImport(
4718         "Cannot infer register class for SUBREG_TO_REG operand #0");
4719     M.insertAction<ConstrainOperandToRegClassAction>(
4720       InsertPt, DstMIBuilder.getInsnID(), 0, **SuperClass);
4721     M.insertAction<ConstrainOperandToRegClassAction>(
4722       InsertPt, DstMIBuilder.getInsnID(), 2, **SubClass);
4723     return InsertPtOrError.get();
4724   }
4725 
4726   if (OpName == "REG_SEQUENCE") {
4727     auto SuperClass = inferRegClassFromPattern(Dst->getChild(0));
4728     M.insertAction<ConstrainOperandToRegClassAction>(
4729       InsertPt, DstMIBuilder.getInsnID(), 0, **SuperClass);
4730 
4731     unsigned Num = Dst->getNumChildren();
4732     for (unsigned I = 1; I != Num; I += 2) {
4733       TreePatternNode *SubRegChild = Dst->getChild(I + 1);
4734 
4735       auto SubIdx = inferSubRegIndexForNode(SubRegChild);
4736       if (!SubIdx)
4737         return failedImport("REG_SEQUENCE child is not a subreg index");
4738 
4739       const auto SrcRCDstRCPair =
4740         (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx);
4741       assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass");
4742       M.insertAction<ConstrainOperandToRegClassAction>(
4743         InsertPt, DstMIBuilder.getInsnID(), I, *SrcRCDstRCPair->second);
4744     }
4745 
4746     return InsertPtOrError.get();
4747   }
4748 
4749   M.insertAction<ConstrainOperandsToDefinitionAction>(InsertPt,
4750                                                       DstMIBuilder.getInsnID());
4751   return InsertPtOrError.get();
4752 }
4753 
4754 Expected<action_iterator> GlobalISelEmitter::createInstructionRenderer(
4755     action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst) {
4756   Record *DstOp = Dst->getOperator();
4757   if (!DstOp->isSubClassOf("Instruction")) {
4758     if (DstOp->isSubClassOf("ValueType"))
4759       return failedImport(
4760           "Pattern operator isn't an instruction (it's a ValueType)");
4761     return failedImport("Pattern operator isn't an instruction");
4762   }
4763   CodeGenInstruction *DstI = &Target.getInstruction(DstOp);
4764 
4765   // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction
4766   // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy.
4767   StringRef Name = DstI->TheDef->getName();
4768   if (Name == "COPY_TO_REGCLASS" || Name == "EXTRACT_SUBREG")
4769     DstI = &Target.getInstruction(RK.getDef("COPY"));
4770 
4771   return M.insertAction<BuildMIAction>(InsertPt, M.allocateOutputInsnID(),
4772                                        DstI);
4773 }
4774 
4775 Expected<action_iterator> GlobalISelEmitter::importExplicitDefRenderers(
4776     action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder,
4777     const TreePatternNode *Dst) {
4778   const CodeGenInstruction *DstI = DstMIBuilder.getCGI();
4779   const unsigned NumDefs = DstI->Operands.NumDefs;
4780   if (NumDefs == 0)
4781     return InsertPt;
4782 
4783   DstMIBuilder.addRenderer<CopyRenderer>(DstI->Operands[0].Name);
4784 
4785   // Some instructions have multiple defs, but are missing a type entry
4786   // (e.g. s_cc_out operands).
4787   if (Dst->getExtTypes().size() < NumDefs)
4788     return failedImport("unhandled discarded def");
4789 
4790   // Patterns only handle a single result, so any result after the first is an
4791   // implicitly dead def.
4792   for (unsigned I = 1; I < NumDefs; ++I) {
4793     const TypeSetByHwMode &ExtTy = Dst->getExtType(I);
4794     if (!ExtTy.isMachineValueType())
4795       return failedImport("unsupported typeset");
4796 
4797     auto OpTy = MVTToLLT(ExtTy.getMachineValueType().SimpleTy);
4798     if (!OpTy)
4799       return failedImport("unsupported type");
4800 
4801     unsigned TempRegID = M.allocateTempRegID();
4802     InsertPt =
4803       M.insertAction<MakeTempRegisterAction>(InsertPt, *OpTy, TempRegID);
4804     DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID, true, nullptr, true);
4805   }
4806 
4807   return InsertPt;
4808 }
4809 
4810 Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderers(
4811     action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder,
4812     const llvm::TreePatternNode *Dst) {
4813   const CodeGenInstruction *DstI = DstMIBuilder.getCGI();
4814   CodeGenInstruction *OrigDstI = &Target.getInstruction(Dst->getOperator());
4815 
4816   StringRef Name = OrigDstI->TheDef->getName();
4817   unsigned ExpectedDstINumUses = Dst->getNumChildren();
4818 
4819   // EXTRACT_SUBREG needs to use a subregister COPY.
4820   if (Name == "EXTRACT_SUBREG") {
4821     if (!Dst->getChild(1)->isLeaf())
4822       return failedImport("EXTRACT_SUBREG child #1 is not a leaf");
4823     DefInit *SubRegInit = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue());
4824     if (!SubRegInit)
4825       return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
4826 
4827     CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef());
4828     TreePatternNode *ValChild = Dst->getChild(0);
4829     if (!ValChild->isLeaf()) {
4830       // We really have to handle the source instruction, and then insert a
4831       // copy from the subregister.
4832       auto ExtractSrcTy = getInstResultType(ValChild);
4833       if (!ExtractSrcTy)
4834         return ExtractSrcTy.takeError();
4835 
4836       unsigned TempRegID = M.allocateTempRegID();
4837       InsertPt = M.insertAction<MakeTempRegisterAction>(
4838         InsertPt, *ExtractSrcTy, TempRegID);
4839 
4840       auto InsertPtOrError = createAndImportSubInstructionRenderer(
4841         ++InsertPt, M, ValChild, TempRegID);
4842       if (auto Error = InsertPtOrError.takeError())
4843         return std::move(Error);
4844 
4845       DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID, false, SubIdx);
4846       return InsertPt;
4847     }
4848 
4849     // If this is a source operand, this is just a subregister copy.
4850     Record *RCDef = getInitValueAsRegClass(ValChild->getLeafValue());
4851     if (!RCDef)
4852       return failedImport("EXTRACT_SUBREG child #0 could not "
4853                           "be coerced to a register class");
4854 
4855     CodeGenRegisterClass *RC = CGRegs.getRegClass(RCDef);
4856 
4857     const auto SrcRCDstRCPair =
4858       RC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx);
4859     if (SrcRCDstRCPair) {
4860       assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass");
4861       if (SrcRCDstRCPair->first != RC)
4862         return failedImport("EXTRACT_SUBREG requires an additional COPY");
4863     }
4864 
4865     DstMIBuilder.addRenderer<CopySubRegRenderer>(Dst->getChild(0)->getName(),
4866                                                  SubIdx);
4867     return InsertPt;
4868   }
4869 
4870   if (Name == "REG_SEQUENCE") {
4871     if (!Dst->getChild(0)->isLeaf())
4872       return failedImport("REG_SEQUENCE child #0 is not a leaf");
4873 
4874     Record *RCDef = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue());
4875     if (!RCDef)
4876       return failedImport("REG_SEQUENCE child #0 could not "
4877                           "be coerced to a register class");
4878 
4879     if ((ExpectedDstINumUses - 1) % 2 != 0)
4880       return failedImport("Malformed REG_SEQUENCE");
4881 
4882     for (unsigned I = 1; I != ExpectedDstINumUses; I += 2) {
4883       TreePatternNode *ValChild = Dst->getChild(I);
4884       TreePatternNode *SubRegChild = Dst->getChild(I + 1);
4885 
4886       if (DefInit *SubRegInit =
4887               dyn_cast<DefInit>(SubRegChild->getLeafValue())) {
4888         CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef());
4889 
4890         auto InsertPtOrError =
4891             importExplicitUseRenderer(InsertPt, M, DstMIBuilder, ValChild);
4892         if (auto Error = InsertPtOrError.takeError())
4893           return std::move(Error);
4894         InsertPt = InsertPtOrError.get();
4895         DstMIBuilder.addRenderer<SubRegIndexRenderer>(SubIdx);
4896       }
4897     }
4898 
4899     return InsertPt;
4900   }
4901 
4902   // Render the explicit uses.
4903   unsigned DstINumUses = OrigDstI->Operands.size() - OrigDstI->Operands.NumDefs;
4904   if (Name == "COPY_TO_REGCLASS") {
4905     DstINumUses--; // Ignore the class constraint.
4906     ExpectedDstINumUses--;
4907   }
4908 
4909   // NumResults - This is the number of results produced by the instruction in
4910   // the "outs" list.
4911   unsigned NumResults = OrigDstI->Operands.NumDefs;
4912 
4913   // Number of operands we know the output instruction must have. If it is
4914   // variadic, we could have more operands.
4915   unsigned NumFixedOperands = DstI->Operands.size();
4916 
4917   // Loop over all of the fixed operands of the instruction pattern, emitting
4918   // code to fill them all in. The node 'N' usually has number children equal to
4919   // the number of input operands of the instruction.  However, in cases where
4920   // there are predicate operands for an instruction, we need to fill in the
4921   // 'execute always' values. Match up the node operands to the instruction
4922   // operands to do this.
4923   unsigned Child = 0;
4924 
4925   // Similarly to the code in TreePatternNode::ApplyTypeConstraints, count the
4926   // number of operands at the end of the list which have default values.
4927   // Those can come from the pattern if it provides enough arguments, or be
4928   // filled in with the default if the pattern hasn't provided them. But any
4929   // operand with a default value _before_ the last mandatory one will be
4930   // filled in with their defaults unconditionally.
4931   unsigned NonOverridableOperands = NumFixedOperands;
4932   while (NonOverridableOperands > NumResults &&
4933          CGP.operandHasDefault(DstI->Operands[NonOverridableOperands - 1].Rec))
4934     --NonOverridableOperands;
4935 
4936   unsigned NumDefaultOps = 0;
4937   for (unsigned I = 0; I != DstINumUses; ++I) {
4938     unsigned InstOpNo = DstI->Operands.NumDefs + I;
4939 
4940     // Determine what to emit for this operand.
4941     Record *OperandNode = DstI->Operands[InstOpNo].Rec;
4942 
4943     // If the operand has default values, introduce them now.
4944     if (CGP.operandHasDefault(OperandNode) &&
4945         (InstOpNo < NonOverridableOperands || Child >= Dst->getNumChildren())) {
4946       // This is a predicate or optional def operand which the pattern has not
4947       // overridden, or which we aren't letting it override; emit the 'default
4948       // ops' operands.
4949 
4950       const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[InstOpNo];
4951       DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps");
4952       if (auto Error = importDefaultOperandRenderers(
4953             InsertPt, M, DstMIBuilder, DefaultOps))
4954         return std::move(Error);
4955       ++NumDefaultOps;
4956       continue;
4957     }
4958 
4959     auto InsertPtOrError = importExplicitUseRenderer(InsertPt, M, DstMIBuilder,
4960                                                      Dst->getChild(Child));
4961     if (auto Error = InsertPtOrError.takeError())
4962       return std::move(Error);
4963     InsertPt = InsertPtOrError.get();
4964     ++Child;
4965   }
4966 
4967   if (NumDefaultOps + ExpectedDstINumUses != DstINumUses)
4968     return failedImport("Expected " + llvm::to_string(DstINumUses) +
4969                         " used operands but found " +
4970                         llvm::to_string(ExpectedDstINumUses) +
4971                         " explicit ones and " + llvm::to_string(NumDefaultOps) +
4972                         " default ones");
4973 
4974   return InsertPt;
4975 }
4976 
4977 Error GlobalISelEmitter::importDefaultOperandRenderers(
4978     action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder,
4979     DagInit *DefaultOps) const {
4980   for (const auto *DefaultOp : DefaultOps->getArgs()) {
4981     std::optional<LLTCodeGen> OpTyOrNone;
4982 
4983     // Look through ValueType operators.
4984     if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) {
4985       if (const DefInit *DefaultDagOperator =
4986               dyn_cast<DefInit>(DefaultDagOp->getOperator())) {
4987         if (DefaultDagOperator->getDef()->isSubClassOf("ValueType")) {
4988           OpTyOrNone = MVTToLLT(getValueType(
4989                                   DefaultDagOperator->getDef()));
4990           DefaultOp = DefaultDagOp->getArg(0);
4991         }
4992       }
4993     }
4994 
4995     if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) {
4996       auto Def = DefaultDefOp->getDef();
4997       if (Def->getName() == "undef_tied_input") {
4998         unsigned TempRegID = M.allocateTempRegID();
4999         M.insertAction<MakeTempRegisterAction>(InsertPt, *OpTyOrNone,
5000                                                TempRegID);
5001         InsertPt = M.insertAction<BuildMIAction>(
5002           InsertPt, M.allocateOutputInsnID(),
5003           &Target.getInstruction(RK.getDef("IMPLICIT_DEF")));
5004         BuildMIAction &IDMIBuilder = *static_cast<BuildMIAction *>(
5005           InsertPt->get());
5006         IDMIBuilder.addRenderer<TempRegRenderer>(TempRegID);
5007         DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID);
5008       } else {
5009         DstMIBuilder.addRenderer<AddRegisterRenderer>(Target, Def);
5010       }
5011       continue;
5012     }
5013 
5014     if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) {
5015       DstMIBuilder.addRenderer<ImmRenderer>(DefaultIntOp->getValue());
5016       continue;
5017     }
5018 
5019     return failedImport("Could not add default op");
5020   }
5021 
5022   return Error::success();
5023 }
5024 
5025 Error GlobalISelEmitter::importImplicitDefRenderers(
5026     BuildMIAction &DstMIBuilder,
5027     const std::vector<Record *> &ImplicitDefs) const {
5028   if (!ImplicitDefs.empty())
5029     return failedImport("Pattern defines a physical register");
5030   return Error::success();
5031 }
5032 
5033 std::optional<const CodeGenRegisterClass *>
5034 GlobalISelEmitter::getRegClassFromLeaf(TreePatternNode *Leaf) {
5035   assert(Leaf && "Expected node?");
5036   assert(Leaf->isLeaf() && "Expected leaf?");
5037   Record *RCRec = getInitValueAsRegClass(Leaf->getLeafValue());
5038   if (!RCRec)
5039     return std::nullopt;
5040   CodeGenRegisterClass *RC = CGRegs.getRegClass(RCRec);
5041   if (!RC)
5042     return std::nullopt;
5043   return RC;
5044 }
5045 
5046 std::optional<const CodeGenRegisterClass *>
5047 GlobalISelEmitter::inferRegClassFromPattern(TreePatternNode *N) {
5048   if (!N)
5049     return std::nullopt;
5050 
5051   if (N->isLeaf())
5052     return getRegClassFromLeaf(N);
5053 
5054   // We don't have a leaf node, so we have to try and infer something. Check
5055   // that we have an instruction that we an infer something from.
5056 
5057   // Only handle things that produce a single type.
5058   if (N->getNumTypes() != 1)
5059     return std::nullopt;
5060   Record *OpRec = N->getOperator();
5061 
5062   // We only want instructions.
5063   if (!OpRec->isSubClassOf("Instruction"))
5064     return std::nullopt;
5065 
5066   // Don't want to try and infer things when there could potentially be more
5067   // than one candidate register class.
5068   auto &Inst = Target.getInstruction(OpRec);
5069   if (Inst.Operands.NumDefs > 1)
5070     return std::nullopt;
5071 
5072   // Handle any special-case instructions which we can safely infer register
5073   // classes from.
5074   StringRef InstName = Inst.TheDef->getName();
5075   bool IsRegSequence = InstName == "REG_SEQUENCE";
5076   if (IsRegSequence || InstName == "COPY_TO_REGCLASS") {
5077     // If we have a COPY_TO_REGCLASS, then we need to handle it specially. It
5078     // has the desired register class as the first child.
5079     TreePatternNode *RCChild = N->getChild(IsRegSequence ? 0 : 1);
5080     if (!RCChild->isLeaf())
5081       return std::nullopt;
5082     return getRegClassFromLeaf(RCChild);
5083   }
5084   if (InstName == "INSERT_SUBREG") {
5085     TreePatternNode *Child0 = N->getChild(0);
5086     assert(Child0->getNumTypes() == 1 && "Unexpected number of types!");
5087     const TypeSetByHwMode &VTy = Child0->getExtType(0);
5088     return inferSuperRegisterClassForNode(VTy, Child0, N->getChild(2));
5089   }
5090   if (InstName == "EXTRACT_SUBREG") {
5091     assert(N->getNumTypes() == 1 && "Unexpected number of types!");
5092     const TypeSetByHwMode &VTy = N->getExtType(0);
5093     return inferSuperRegisterClass(VTy, N->getChild(1));
5094   }
5095 
5096   // Handle destination record types that we can safely infer a register class
5097   // from.
5098   const auto &DstIOperand = Inst.Operands[0];
5099   Record *DstIOpRec = DstIOperand.Rec;
5100   if (DstIOpRec->isSubClassOf("RegisterOperand")) {
5101     DstIOpRec = DstIOpRec->getValueAsDef("RegClass");
5102     const CodeGenRegisterClass &RC = Target.getRegisterClass(DstIOpRec);
5103     return &RC;
5104   }
5105 
5106   if (DstIOpRec->isSubClassOf("RegisterClass")) {
5107     const CodeGenRegisterClass &RC = Target.getRegisterClass(DstIOpRec);
5108     return &RC;
5109   }
5110 
5111   return std::nullopt;
5112 }
5113 
5114 std::optional<const CodeGenRegisterClass *>
5115 GlobalISelEmitter::inferSuperRegisterClass(const TypeSetByHwMode &Ty,
5116                                            TreePatternNode *SubRegIdxNode) {
5117   assert(SubRegIdxNode && "Expected subregister index node!");
5118   // We need a ValueTypeByHwMode for getSuperRegForSubReg.
5119   if (!Ty.isValueTypeByHwMode(false))
5120     return std::nullopt;
5121   if (!SubRegIdxNode->isLeaf())
5122     return std::nullopt;
5123   DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode->getLeafValue());
5124   if (!SubRegInit)
5125     return std::nullopt;
5126   CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef());
5127 
5128   // Use the information we found above to find a minimal register class which
5129   // supports the subregister and type we want.
5130   auto RC =
5131       Target.getSuperRegForSubReg(Ty.getValueTypeByHwMode(), CGRegs, SubIdx,
5132                                   /* MustBeAllocatable */ true);
5133   if (!RC)
5134     return std::nullopt;
5135   return *RC;
5136 }
5137 
5138 std::optional<const CodeGenRegisterClass *>
5139 GlobalISelEmitter::inferSuperRegisterClassForNode(
5140     const TypeSetByHwMode &Ty, TreePatternNode *SuperRegNode,
5141     TreePatternNode *SubRegIdxNode) {
5142   assert(SuperRegNode && "Expected super register node!");
5143   // Check if we already have a defined register class for the super register
5144   // node. If we do, then we should preserve that rather than inferring anything
5145   // from the subregister index node. We can assume that whoever wrote the
5146   // pattern in the first place made sure that the super register and
5147   // subregister are compatible.
5148   if (std::optional<const CodeGenRegisterClass *> SuperRegisterClass =
5149           inferRegClassFromPattern(SuperRegNode))
5150     return *SuperRegisterClass;
5151   return inferSuperRegisterClass(Ty, SubRegIdxNode);
5152 }
5153 
5154 std::optional<CodeGenSubRegIndex *>
5155 GlobalISelEmitter::inferSubRegIndexForNode(TreePatternNode *SubRegIdxNode) {
5156   if (!SubRegIdxNode->isLeaf())
5157     return std::nullopt;
5158 
5159   DefInit *SubRegInit = dyn_cast<DefInit>(SubRegIdxNode->getLeafValue());
5160   if (!SubRegInit)
5161     return std::nullopt;
5162   return CGRegs.getSubRegIdx(SubRegInit->getDef());
5163 }
5164 
5165 Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
5166   // Keep track of the matchers and actions to emit.
5167   int Score = P.getPatternComplexity(CGP);
5168   RuleMatcher M(P.getSrcRecord()->getLoc());
5169   RuleMatcherScores[M.getRuleID()] = Score;
5170   M.addAction<DebugCommentAction>(llvm::to_string(*P.getSrcPattern()) +
5171                                   "  =>  " +
5172                                   llvm::to_string(*P.getDstPattern()));
5173 
5174   SmallVector<Record *, 4> Predicates;
5175   P.getPredicateRecords(Predicates);
5176   if (auto Error = importRulePredicates(M, Predicates))
5177     return std::move(Error);
5178 
5179   // Next, analyze the pattern operators.
5180   TreePatternNode *Src = P.getSrcPattern();
5181   TreePatternNode *Dst = P.getDstPattern();
5182 
5183   // If the root of either pattern isn't a simple operator, ignore it.
5184   if (auto Err = isTrivialOperatorNode(Dst))
5185     return failedImport("Dst pattern root isn't a trivial operator (" +
5186                         toString(std::move(Err)) + ")");
5187   if (auto Err = isTrivialOperatorNode(Src))
5188     return failedImport("Src pattern root isn't a trivial operator (" +
5189                         toString(std::move(Err)) + ")");
5190 
5191   // The different predicates and matchers created during
5192   // addInstructionMatcher use the RuleMatcher M to set up their
5193   // instruction ID (InsnVarID) that are going to be used when
5194   // M is going to be emitted.
5195   // However, the code doing the emission still relies on the IDs
5196   // returned during that process by the RuleMatcher when issuing
5197   // the recordInsn opcodes.
5198   // Because of that:
5199   // 1. The order in which we created the predicates
5200   //    and such must be the same as the order in which we emit them,
5201   //    and
5202   // 2. We need to reset the generation of the IDs in M somewhere between
5203   //    addInstructionMatcher and emit
5204   //
5205   // FIXME: Long term, we don't want to have to rely on this implicit
5206   // naming being the same. One possible solution would be to have
5207   // explicit operator for operation capture and reference those.
5208   // The plus side is that it would expose opportunities to share
5209   // the capture accross rules. The downside is that it would
5210   // introduce a dependency between predicates (captures must happen
5211   // before their first use.)
5212   InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher(Src->getName());
5213   unsigned TempOpIdx = 0;
5214   auto InsnMatcherOrError =
5215       createAndImportSelDAGMatcher(M, InsnMatcherTemp, Src, TempOpIdx);
5216   if (auto Error = InsnMatcherOrError.takeError())
5217     return std::move(Error);
5218   InstructionMatcher &InsnMatcher = InsnMatcherOrError.get();
5219 
5220   if (Dst->isLeaf()) {
5221     Record *RCDef = getInitValueAsRegClass(Dst->getLeafValue());
5222     if (RCDef) {
5223       const CodeGenRegisterClass &RC = Target.getRegisterClass(RCDef);
5224 
5225       // We need to replace the def and all its uses with the specified
5226       // operand. However, we must also insert COPY's wherever needed.
5227       // For now, emit a copy and let the register allocator clean up.
5228       auto &DstI = Target.getInstruction(RK.getDef("COPY"));
5229       const auto &DstIOperand = DstI.Operands[0];
5230 
5231       OperandMatcher &OM0 = InsnMatcher.getOperand(0);
5232       OM0.setSymbolicName(DstIOperand.Name);
5233       M.defineOperand(OM0.getSymbolicName(), OM0);
5234       OM0.addPredicate<RegisterBankOperandMatcher>(RC);
5235 
5236       auto &DstMIBuilder =
5237           M.addAction<BuildMIAction>(M.allocateOutputInsnID(), &DstI);
5238       DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name);
5239       DstMIBuilder.addRenderer<CopyRenderer>(Dst->getName());
5240       M.addAction<ConstrainOperandToRegClassAction>(0, 0, RC);
5241 
5242       // We're done with this pattern!  It's eligible for GISel emission; return
5243       // it.
5244       ++NumPatternImported;
5245       return std::move(M);
5246     }
5247 
5248     return failedImport("Dst pattern root isn't a known leaf");
5249   }
5250 
5251   // Start with the defined operands (i.e., the results of the root operator).
5252   Record *DstOp = Dst->getOperator();
5253   if (!DstOp->isSubClassOf("Instruction"))
5254     return failedImport("Pattern operator isn't an instruction");
5255 
5256   auto &DstI = Target.getInstruction(DstOp);
5257   StringRef DstIName = DstI.TheDef->getName();
5258 
5259   unsigned DstNumDefs = DstI.Operands.NumDefs,
5260            SrcNumDefs = Src->getExtTypes().size();
5261   if (DstNumDefs < SrcNumDefs) {
5262     if (DstNumDefs != 0)
5263       return failedImport("Src pattern result has more defs than dst MI (" +
5264                           to_string(SrcNumDefs) + " def(s) vs " +
5265                           to_string(DstNumDefs) + " def(s))");
5266 
5267     bool FoundNoUsePred = false;
5268     for (const auto &Pred : InsnMatcher.predicates()) {
5269       if ((FoundNoUsePred = isa<NoUsePredicateMatcher>(Pred.get())))
5270         break;
5271     }
5272     if (!FoundNoUsePred)
5273       return failedImport("Src pattern result has " + to_string(SrcNumDefs) +
5274                           " def(s) without the HasNoUse predicate set to true "
5275                           "but Dst MI has no def");
5276   }
5277 
5278   // The root of the match also has constraints on the register bank so that it
5279   // matches the result instruction.
5280   unsigned OpIdx = 0;
5281   unsigned N = std::min(DstNumDefs, SrcNumDefs);
5282   for (unsigned I = 0; I < N; ++I) {
5283     const TypeSetByHwMode &VTy = Src->getExtType(I);
5284 
5285     const auto &DstIOperand = DstI.Operands[OpIdx];
5286     Record *DstIOpRec = DstIOperand.Rec;
5287     if (DstIName == "COPY_TO_REGCLASS") {
5288       DstIOpRec = getInitValueAsRegClass(Dst->getChild(1)->getLeafValue());
5289 
5290       if (DstIOpRec == nullptr)
5291         return failedImport(
5292             "COPY_TO_REGCLASS operand #1 isn't a register class");
5293     } else if (DstIName == "REG_SEQUENCE") {
5294       DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue());
5295       if (DstIOpRec == nullptr)
5296         return failedImport("REG_SEQUENCE operand #0 isn't a register class");
5297     } else if (DstIName == "EXTRACT_SUBREG") {
5298       auto InferredClass = inferRegClassFromPattern(Dst->getChild(0));
5299       if (!InferredClass)
5300         return failedImport("Could not infer class for EXTRACT_SUBREG operand #0");
5301 
5302       // We can assume that a subregister is in the same bank as it's super
5303       // register.
5304       DstIOpRec = (*InferredClass)->getDef();
5305     } else if (DstIName == "INSERT_SUBREG") {
5306       auto MaybeSuperClass = inferSuperRegisterClassForNode(
5307           VTy, Dst->getChild(0), Dst->getChild(2));
5308       if (!MaybeSuperClass)
5309         return failedImport(
5310             "Cannot infer register class for INSERT_SUBREG operand #0");
5311       // Move to the next pattern here, because the register class we found
5312       // doesn't necessarily have a record associated with it. So, we can't
5313       // set DstIOpRec using this.
5314       OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
5315       OM.setSymbolicName(DstIOperand.Name);
5316       M.defineOperand(OM.getSymbolicName(), OM);
5317       OM.addPredicate<RegisterBankOperandMatcher>(**MaybeSuperClass);
5318       ++OpIdx;
5319       continue;
5320     } else if (DstIName == "SUBREG_TO_REG") {
5321       auto MaybeRegClass = inferSuperRegisterClass(VTy, Dst->getChild(2));
5322       if (!MaybeRegClass)
5323         return failedImport(
5324             "Cannot infer register class for SUBREG_TO_REG operand #0");
5325       OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
5326       OM.setSymbolicName(DstIOperand.Name);
5327       M.defineOperand(OM.getSymbolicName(), OM);
5328       OM.addPredicate<RegisterBankOperandMatcher>(**MaybeRegClass);
5329       ++OpIdx;
5330       continue;
5331     } else if (DstIOpRec->isSubClassOf("RegisterOperand"))
5332       DstIOpRec = DstIOpRec->getValueAsDef("RegClass");
5333     else if (!DstIOpRec->isSubClassOf("RegisterClass"))
5334       return failedImport("Dst MI def isn't a register class" +
5335                           to_string(*Dst));
5336 
5337     OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
5338     OM.setSymbolicName(DstIOperand.Name);
5339     M.defineOperand(OM.getSymbolicName(), OM);
5340     OM.addPredicate<RegisterBankOperandMatcher>(
5341         Target.getRegisterClass(DstIOpRec));
5342     ++OpIdx;
5343   }
5344 
5345   auto DstMIBuilderOrError =
5346       createAndImportInstructionRenderer(M, InsnMatcher, Src, Dst);
5347   if (auto Error = DstMIBuilderOrError.takeError())
5348     return std::move(Error);
5349   BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get();
5350 
5351   // Render the implicit defs.
5352   // These are only added to the root of the result.
5353   if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs()))
5354     return std::move(Error);
5355 
5356   DstMIBuilder.chooseInsnToMutate(M);
5357 
5358   // Constrain the registers to classes. This is normally derived from the
5359   // emitted instruction but a few instructions require special handling.
5360   if (DstIName == "COPY_TO_REGCLASS") {
5361     // COPY_TO_REGCLASS does not provide operand constraints itself but the
5362     // result is constrained to the class given by the second child.
5363     Record *DstIOpRec =
5364         getInitValueAsRegClass(Dst->getChild(1)->getLeafValue());
5365 
5366     if (DstIOpRec == nullptr)
5367       return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class");
5368 
5369     M.addAction<ConstrainOperandToRegClassAction>(
5370         0, 0, Target.getRegisterClass(DstIOpRec));
5371 
5372     // We're done with this pattern!  It's eligible for GISel emission; return
5373     // it.
5374     ++NumPatternImported;
5375     return std::move(M);
5376   }
5377 
5378   if (DstIName == "EXTRACT_SUBREG") {
5379     auto SuperClass = inferRegClassFromPattern(Dst->getChild(0));
5380     if (!SuperClass)
5381       return failedImport(
5382         "Cannot infer register class from EXTRACT_SUBREG operand #0");
5383 
5384     auto SubIdx = inferSubRegIndexForNode(Dst->getChild(1));
5385     if (!SubIdx)
5386       return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
5387 
5388     // It would be nice to leave this constraint implicit but we're required
5389     // to pick a register class so constrain the result to a register class
5390     // that can hold the correct MVT.
5391     //
5392     // FIXME: This may introduce an extra copy if the chosen class doesn't
5393     //        actually contain the subregisters.
5394     assert(Src->getExtTypes().size() == 1 &&
5395              "Expected Src of EXTRACT_SUBREG to have one result type");
5396 
5397     const auto SrcRCDstRCPair =
5398       (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx);
5399     if (!SrcRCDstRCPair) {
5400       return failedImport("subreg index is incompatible "
5401                           "with inferred reg class");
5402     }
5403 
5404     assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass");
5405     M.addAction<ConstrainOperandToRegClassAction>(0, 0, *SrcRCDstRCPair->second);
5406     M.addAction<ConstrainOperandToRegClassAction>(0, 1, *SrcRCDstRCPair->first);
5407 
5408     // We're done with this pattern!  It's eligible for GISel emission; return
5409     // it.
5410     ++NumPatternImported;
5411     return std::move(M);
5412   }
5413 
5414   if (DstIName == "INSERT_SUBREG") {
5415     assert(Src->getExtTypes().size() == 1 &&
5416            "Expected Src of INSERT_SUBREG to have one result type");
5417     // We need to constrain the destination, a super regsister source, and a
5418     // subregister source.
5419     auto SubClass = inferRegClassFromPattern(Dst->getChild(1));
5420     if (!SubClass)
5421       return failedImport(
5422           "Cannot infer register class from INSERT_SUBREG operand #1");
5423     auto SuperClass = inferSuperRegisterClassForNode(
5424         Src->getExtType(0), Dst->getChild(0), Dst->getChild(2));
5425     if (!SuperClass)
5426       return failedImport(
5427           "Cannot infer register class for INSERT_SUBREG operand #0");
5428     M.addAction<ConstrainOperandToRegClassAction>(0, 0, **SuperClass);
5429     M.addAction<ConstrainOperandToRegClassAction>(0, 1, **SuperClass);
5430     M.addAction<ConstrainOperandToRegClassAction>(0, 2, **SubClass);
5431     ++NumPatternImported;
5432     return std::move(M);
5433   }
5434 
5435   if (DstIName == "SUBREG_TO_REG") {
5436     // We need to constrain the destination and subregister source.
5437     assert(Src->getExtTypes().size() == 1 &&
5438            "Expected Src of SUBREG_TO_REG to have one result type");
5439 
5440     // Attempt to infer the subregister source from the first child. If it has
5441     // an explicitly given register class, we'll use that. Otherwise, we will
5442     // fail.
5443     auto SubClass = inferRegClassFromPattern(Dst->getChild(1));
5444     if (!SubClass)
5445       return failedImport(
5446           "Cannot infer register class from SUBREG_TO_REG child #1");
5447     // We don't have a child to look at that might have a super register node.
5448     auto SuperClass =
5449         inferSuperRegisterClass(Src->getExtType(0), Dst->getChild(2));
5450     if (!SuperClass)
5451       return failedImport(
5452           "Cannot infer register class for SUBREG_TO_REG operand #0");
5453     M.addAction<ConstrainOperandToRegClassAction>(0, 0, **SuperClass);
5454     M.addAction<ConstrainOperandToRegClassAction>(0, 2, **SubClass);
5455     ++NumPatternImported;
5456     return std::move(M);
5457   }
5458 
5459   if (DstIName == "REG_SEQUENCE") {
5460     auto SuperClass = inferRegClassFromPattern(Dst->getChild(0));
5461 
5462     M.addAction<ConstrainOperandToRegClassAction>(0, 0, **SuperClass);
5463 
5464     unsigned Num = Dst->getNumChildren();
5465     for (unsigned I = 1; I != Num; I += 2) {
5466       TreePatternNode *SubRegChild = Dst->getChild(I + 1);
5467 
5468       auto SubIdx = inferSubRegIndexForNode(SubRegChild);
5469       if (!SubIdx)
5470         return failedImport("REG_SEQUENCE child is not a subreg index");
5471 
5472       const auto SrcRCDstRCPair =
5473         (*SuperClass)->getMatchingSubClassWithSubRegs(CGRegs, *SubIdx);
5474 
5475       M.addAction<ConstrainOperandToRegClassAction>(0, I,
5476                                                     *SrcRCDstRCPair->second);
5477     }
5478 
5479     ++NumPatternImported;
5480     return std::move(M);
5481   }
5482 
5483   M.addAction<ConstrainOperandsToDefinitionAction>(0);
5484 
5485   // We're done with this pattern!  It's eligible for GISel emission; return it.
5486   ++NumPatternImported;
5487   return std::move(M);
5488 }
5489 
5490 // Emit imm predicate table and an enum to reference them with.
5491 // The 'Predicate_' part of the name is redundant but eliminating it is more
5492 // trouble than it's worth.
5493 void GlobalISelEmitter::emitCxxPredicateFns(
5494     raw_ostream &OS, StringRef CodeFieldName, StringRef TypeIdentifier,
5495     StringRef ArgType, StringRef ArgName, StringRef AdditionalArgs,
5496     StringRef AdditionalDeclarations,
5497     std::function<bool(const Record *R)> Filter) {
5498   std::vector<const Record *> MatchedRecords;
5499   const auto &Defs = RK.getAllDerivedDefinitions("PatFrags");
5500   std::copy_if(Defs.begin(), Defs.end(), std::back_inserter(MatchedRecords),
5501                [&](Record *Record) {
5502                  return !Record->getValueAsString(CodeFieldName).empty() &&
5503                         Filter(Record);
5504                });
5505 
5506   if (!MatchedRecords.empty()) {
5507     OS << "// PatFrag predicates.\n"
5508        << "enum {\n";
5509     std::string EnumeratorSeparator =
5510         (" = GIPFP_" + TypeIdentifier + "_Invalid + 1,\n").str();
5511     for (const auto *Record : MatchedRecords) {
5512       OS << "  GIPFP_" << TypeIdentifier << "_Predicate_" << Record->getName()
5513          << EnumeratorSeparator;
5514       EnumeratorSeparator = ",\n";
5515     }
5516     OS << "};\n";
5517   }
5518 
5519   OS << "bool " << Target.getName() << "InstructionSelector::test" << ArgName
5520      << "Predicate_" << TypeIdentifier << "(unsigned PredicateID, " << ArgType << " "
5521      << ArgName << AdditionalArgs <<") const {\n"
5522      << AdditionalDeclarations;
5523   if (!AdditionalDeclarations.empty())
5524     OS << "\n";
5525   if (!MatchedRecords.empty())
5526     OS << "  switch (PredicateID) {\n";
5527   for (const auto *Record : MatchedRecords) {
5528     OS << "  case GIPFP_" << TypeIdentifier << "_Predicate_"
5529        << Record->getName() << ": {\n"
5530        << "    " << Record->getValueAsString(CodeFieldName) << "\n"
5531        << "    llvm_unreachable(\"" << CodeFieldName
5532        << " should have returned\");\n"
5533        << "    return false;\n"
5534        << "  }\n";
5535   }
5536   if (!MatchedRecords.empty())
5537     OS << "  }\n";
5538   OS << "  llvm_unreachable(\"Unknown predicate\");\n"
5539      << "  return false;\n"
5540      << "}\n";
5541 }
5542 
5543 void GlobalISelEmitter::emitImmPredicateFns(
5544     raw_ostream &OS, StringRef TypeIdentifier, StringRef ArgType,
5545     std::function<bool(const Record *R)> Filter) {
5546   return emitCxxPredicateFns(OS, "ImmediateCode", TypeIdentifier, ArgType,
5547                              "Imm", "", "", Filter);
5548 }
5549 
5550 void GlobalISelEmitter::emitMIPredicateFns(raw_ostream &OS) {
5551   return emitCxxPredicateFns(
5552       OS, "GISelPredicateCode", "MI", "const MachineInstr &", "MI",
5553       ", const std::array<const MachineOperand *, 3> &Operands",
5554       "  const MachineFunction &MF = *MI.getParent()->getParent();\n"
5555       "  const MachineRegisterInfo &MRI = MF.getRegInfo();\n"
5556       "  (void)MRI;",
5557       [](const Record *R) { return true; });
5558 }
5559 
5560 template <class GroupT>
5561 std::vector<Matcher *> GlobalISelEmitter::optimizeRules(
5562     ArrayRef<Matcher *> Rules,
5563     std::vector<std::unique_ptr<Matcher>> &MatcherStorage) {
5564 
5565   std::vector<Matcher *> OptRules;
5566   std::unique_ptr<GroupT> CurrentGroup = std::make_unique<GroupT>();
5567   assert(CurrentGroup->empty() && "Newly created group isn't empty!");
5568   unsigned NumGroups = 0;
5569 
5570   auto ProcessCurrentGroup = [&]() {
5571     if (CurrentGroup->empty())
5572       // An empty group is good to be reused:
5573       return;
5574 
5575     // If the group isn't large enough to provide any benefit, move all the
5576     // added rules out of it and make sure to re-create the group to properly
5577     // re-initialize it:
5578     if (CurrentGroup->size() < 2)
5579       append_range(OptRules, CurrentGroup->matchers());
5580     else {
5581       CurrentGroup->finalize();
5582       OptRules.push_back(CurrentGroup.get());
5583       MatcherStorage.emplace_back(std::move(CurrentGroup));
5584       ++NumGroups;
5585     }
5586     CurrentGroup = std::make_unique<GroupT>();
5587   };
5588   for (Matcher *Rule : Rules) {
5589     // Greedily add as many matchers as possible to the current group:
5590     if (CurrentGroup->addMatcher(*Rule))
5591       continue;
5592 
5593     ProcessCurrentGroup();
5594     assert(CurrentGroup->empty() && "A group wasn't properly re-initialized");
5595 
5596     // Try to add the pending matcher to a newly created empty group:
5597     if (!CurrentGroup->addMatcher(*Rule))
5598       // If we couldn't add the matcher to an empty group, that group type
5599       // doesn't support that kind of matchers at all, so just skip it:
5600       OptRules.push_back(Rule);
5601   }
5602   ProcessCurrentGroup();
5603 
5604   LLVM_DEBUG(dbgs() << "NumGroups: " << NumGroups << "\n");
5605   (void) NumGroups;
5606   assert(CurrentGroup->empty() && "The last group wasn't properly processed");
5607   return OptRules;
5608 }
5609 
5610 MatchTable
5611 GlobalISelEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules,
5612                                    bool Optimize, bool WithCoverage) {
5613   std::vector<Matcher *> InputRules;
5614   for (Matcher &Rule : Rules)
5615     InputRules.push_back(&Rule);
5616 
5617   if (!Optimize)
5618     return MatchTable::buildTable(InputRules, WithCoverage);
5619 
5620   unsigned CurrentOrdering = 0;
5621   StringMap<unsigned> OpcodeOrder;
5622   for (RuleMatcher &Rule : Rules) {
5623     const StringRef Opcode = Rule.getOpcode();
5624     assert(!Opcode.empty() && "Didn't expect an undefined opcode");
5625     if (OpcodeOrder.count(Opcode) == 0)
5626       OpcodeOrder[Opcode] = CurrentOrdering++;
5627   }
5628 
5629   llvm::stable_sort(InputRules, [&OpcodeOrder](const Matcher *A,
5630                                                const Matcher *B) {
5631     auto *L = static_cast<const RuleMatcher *>(A);
5632     auto *R = static_cast<const RuleMatcher *>(B);
5633     return std::make_tuple(OpcodeOrder[L->getOpcode()], L->getNumOperands()) <
5634            std::make_tuple(OpcodeOrder[R->getOpcode()], R->getNumOperands());
5635   });
5636 
5637   for (Matcher *Rule : InputRules)
5638     Rule->optimize();
5639 
5640   std::vector<std::unique_ptr<Matcher>> MatcherStorage;
5641   std::vector<Matcher *> OptRules =
5642       optimizeRules<GroupMatcher>(InputRules, MatcherStorage);
5643 
5644   for (Matcher *Rule : OptRules)
5645     Rule->optimize();
5646 
5647   OptRules = optimizeRules<SwitchMatcher>(OptRules, MatcherStorage);
5648 
5649   return MatchTable::buildTable(OptRules, WithCoverage);
5650 }
5651 
5652 void GroupMatcher::optimize() {
5653   // Make sure we only sort by a specific predicate within a range of rules that
5654   // all have that predicate checked against a specific value (not a wildcard):
5655   auto F = Matchers.begin();
5656   auto T = F;
5657   auto E = Matchers.end();
5658   while (T != E) {
5659     while (T != E) {
5660       auto *R = static_cast<RuleMatcher *>(*T);
5661       if (!R->getFirstConditionAsRootType().get().isValid())
5662         break;
5663       ++T;
5664     }
5665     std::stable_sort(F, T, [](Matcher *A, Matcher *B) {
5666       auto *L = static_cast<RuleMatcher *>(A);
5667       auto *R = static_cast<RuleMatcher *>(B);
5668       return L->getFirstConditionAsRootType() <
5669              R->getFirstConditionAsRootType();
5670     });
5671     if (T != E)
5672       F = ++T;
5673   }
5674   GlobalISelEmitter::optimizeRules<GroupMatcher>(Matchers, MatcherStorage)
5675       .swap(Matchers);
5676   GlobalISelEmitter::optimizeRules<SwitchMatcher>(Matchers, MatcherStorage)
5677       .swap(Matchers);
5678 }
5679 
5680 void GlobalISelEmitter::run(raw_ostream &OS) {
5681   if (!UseCoverageFile.empty()) {
5682     RuleCoverage = CodeGenCoverage();
5683     auto RuleCoverageBufOrErr = MemoryBuffer::getFile(UseCoverageFile);
5684     if (!RuleCoverageBufOrErr) {
5685       PrintWarning(SMLoc(), "Missing rule coverage data");
5686       RuleCoverage = std::nullopt;
5687     } else {
5688       if (!RuleCoverage->parse(*RuleCoverageBufOrErr.get(), Target.getName())) {
5689         PrintWarning(SMLoc(), "Ignoring invalid or missing rule coverage data");
5690         RuleCoverage = std::nullopt;
5691       }
5692     }
5693   }
5694 
5695   // Track the run-time opcode values
5696   gatherOpcodeValues();
5697   // Track the run-time LLT ID values
5698   gatherTypeIDValues();
5699 
5700   // Track the GINodeEquiv definitions.
5701   gatherNodeEquivs();
5702 
5703   emitSourceFileHeader(("Global Instruction Selector for the " +
5704                        Target.getName() + " target").str(), OS);
5705   std::vector<RuleMatcher> Rules;
5706   // Look through the SelectionDAG patterns we found, possibly emitting some.
5707   for (const PatternToMatch &Pat : CGP.ptms()) {
5708     ++NumPatternTotal;
5709 
5710     auto MatcherOrErr = runOnPattern(Pat);
5711 
5712     // The pattern analysis can fail, indicating an unsupported pattern.
5713     // Report that if we've been asked to do so.
5714     if (auto Err = MatcherOrErr.takeError()) {
5715       if (WarnOnSkippedPatterns) {
5716         PrintWarning(Pat.getSrcRecord()->getLoc(),
5717                      "Skipped pattern: " + toString(std::move(Err)));
5718       } else {
5719         consumeError(std::move(Err));
5720       }
5721       ++NumPatternImportsSkipped;
5722       continue;
5723     }
5724 
5725     if (RuleCoverage) {
5726       if (RuleCoverage->isCovered(MatcherOrErr->getRuleID()))
5727         ++NumPatternsTested;
5728       else
5729         PrintWarning(Pat.getSrcRecord()->getLoc(),
5730                      "Pattern is not covered by a test");
5731     }
5732     Rules.push_back(std::move(MatcherOrErr.get()));
5733   }
5734 
5735   // Comparison function to order records by name.
5736   auto orderByName = [](const Record *A, const Record *B) {
5737     return A->getName() < B->getName();
5738   };
5739 
5740   std::vector<Record *> ComplexPredicates =
5741       RK.getAllDerivedDefinitions("GIComplexOperandMatcher");
5742   llvm::sort(ComplexPredicates, orderByName);
5743 
5744   std::vector<StringRef> CustomRendererFns;
5745   transform(RK.getAllDerivedDefinitions("GICustomOperandRenderer"),
5746             std::back_inserter(CustomRendererFns), [](const auto &Record) {
5747               return Record->getValueAsString("RendererFn");
5748             });
5749   // Sort and remove duplicates to get a list of unique renderer functions, in
5750   // case some were mentioned more than once.
5751   llvm::sort(CustomRendererFns);
5752   CustomRendererFns.erase(
5753       std::unique(CustomRendererFns.begin(), CustomRendererFns.end()),
5754       CustomRendererFns.end());
5755 
5756   unsigned MaxTemporaries = 0;
5757   for (const auto &Rule : Rules)
5758     MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns());
5759 
5760   OS << "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n"
5761      << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size()
5762      << ";\n"
5763      << "using PredicateBitset = "
5764         "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n"
5765      << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n";
5766 
5767   OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n"
5768      << "  mutable MatcherState State;\n"
5769      << "  typedef "
5770         "ComplexRendererFns("
5771      << Target.getName()
5772      << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n"
5773 
5774      << "  typedef void(" << Target.getName()
5775      << "InstructionSelector::*CustomRendererFn)(MachineInstrBuilder &, const "
5776         "MachineInstr &, int) "
5777         "const;\n"
5778      << "  const ISelInfoTy<PredicateBitset, ComplexMatcherMemFn, "
5779         "CustomRendererFn> "
5780         "ISelInfo;\n";
5781   OS << "  static " << Target.getName()
5782      << "InstructionSelector::ComplexMatcherMemFn ComplexPredicateFns[];\n"
5783      << "  static " << Target.getName()
5784      << "InstructionSelector::CustomRendererFn CustomRenderers[];\n"
5785      << "  bool testImmPredicate_I64(unsigned PredicateID, int64_t Imm) const "
5786         "override;\n"
5787      << "  bool testImmPredicate_APInt(unsigned PredicateID, const APInt &Imm) "
5788         "const override;\n"
5789      << "  bool testImmPredicate_APFloat(unsigned PredicateID, const APFloat "
5790         "&Imm) const override;\n"
5791      << "  const int64_t *getMatchTable() const override;\n"
5792      << "  bool testMIPredicate_MI(unsigned PredicateID, const MachineInstr &MI"
5793         ", const std::array<const MachineOperand *, 3> &Operands) "
5794         "const override;\n"
5795      << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
5796 
5797   OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n"
5798      << ", State(" << MaxTemporaries << "),\n"
5799      << "ISelInfo(TypeObjects, NumTypeObjects, FeatureBitsets"
5800      << ", ComplexPredicateFns, CustomRenderers)\n"
5801      << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
5802 
5803   OS << "#ifdef GET_GLOBALISEL_IMPL\n";
5804   SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures,
5805                                                            OS);
5806 
5807   // Separate subtarget features by how often they must be recomputed.
5808   SubtargetFeatureInfoMap ModuleFeatures;
5809   std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(),
5810                std::inserter(ModuleFeatures, ModuleFeatures.end()),
5811                [](const SubtargetFeatureInfoMap::value_type &X) {
5812                  return !X.second.mustRecomputePerFunction();
5813                });
5814   SubtargetFeatureInfoMap FunctionFeatures;
5815   std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(),
5816                std::inserter(FunctionFeatures, FunctionFeatures.end()),
5817                [](const SubtargetFeatureInfoMap::value_type &X) {
5818                  return X.second.mustRecomputePerFunction();
5819                });
5820 
5821   SubtargetFeatureInfo::emitComputeAvailableFeatures(
5822     Target.getName(), "InstructionSelector", "computeAvailableModuleFeatures",
5823       ModuleFeatures, OS);
5824 
5825 
5826   OS << "void " << Target.getName() << "InstructionSelector"
5827     "::setupGeneratedPerFunctionState(MachineFunction &MF) {\n"
5828     "  AvailableFunctionFeatures = computeAvailableFunctionFeatures("
5829     "(const " << Target.getName() << "Subtarget *)&MF.getSubtarget(), &MF);\n"
5830     "}\n";
5831 
5832   SubtargetFeatureInfo::emitComputeAvailableFeatures(
5833       Target.getName(), "InstructionSelector",
5834       "computeAvailableFunctionFeatures", FunctionFeatures, OS,
5835       "const MachineFunction *MF");
5836 
5837   // Emit a table containing the LLT objects needed by the matcher and an enum
5838   // for the matcher to reference them with.
5839   std::vector<LLTCodeGen> TypeObjects;
5840   append_range(TypeObjects, KnownTypes);
5841   llvm::sort(TypeObjects);
5842   OS << "// LLT Objects.\n"
5843      << "enum {\n";
5844   for (const auto &TypeObject : TypeObjects) {
5845     OS << "  ";
5846     TypeObject.emitCxxEnumValue(OS);
5847     OS << ",\n";
5848   }
5849   OS << "};\n";
5850   OS << "const static size_t NumTypeObjects = " << TypeObjects.size() << ";\n"
5851      << "const static LLT TypeObjects[] = {\n";
5852   for (const auto &TypeObject : TypeObjects) {
5853     OS << "  ";
5854     TypeObject.emitCxxConstructorCall(OS);
5855     OS << ",\n";
5856   }
5857   OS << "};\n\n";
5858 
5859   // Emit a table containing the PredicateBitsets objects needed by the matcher
5860   // and an enum for the matcher to reference them with.
5861   std::vector<std::vector<Record *>> FeatureBitsets;
5862   FeatureBitsets.reserve(Rules.size());
5863   for (auto &Rule : Rules)
5864     FeatureBitsets.push_back(Rule.getRequiredFeatures());
5865   llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A,
5866                                  const std::vector<Record *> &B) {
5867     if (A.size() < B.size())
5868       return true;
5869     if (A.size() > B.size())
5870       return false;
5871     for (auto Pair : zip(A, B)) {
5872       if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName())
5873         return true;
5874       if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName())
5875         return false;
5876     }
5877     return false;
5878   });
5879   FeatureBitsets.erase(
5880       std::unique(FeatureBitsets.begin(), FeatureBitsets.end()),
5881       FeatureBitsets.end());
5882   OS << "// Feature bitsets.\n"
5883      << "enum {\n"
5884      << "  GIFBS_Invalid,\n";
5885   for (const auto &FeatureBitset : FeatureBitsets) {
5886     if (FeatureBitset.empty())
5887       continue;
5888     OS << "  " << getNameForFeatureBitset(FeatureBitset) << ",\n";
5889   }
5890   OS << "};\n"
5891      << "const static PredicateBitset FeatureBitsets[] {\n"
5892      << "  {}, // GIFBS_Invalid\n";
5893   for (const auto &FeatureBitset : FeatureBitsets) {
5894     if (FeatureBitset.empty())
5895       continue;
5896     OS << "  {";
5897     for (const auto &Feature : FeatureBitset) {
5898       const auto &I = SubtargetFeatures.find(Feature);
5899       assert(I != SubtargetFeatures.end() && "Didn't import predicate?");
5900       OS << I->second.getEnumBitName() << ", ";
5901     }
5902     OS << "},\n";
5903   }
5904   OS << "};\n\n";
5905 
5906   // Emit complex predicate table and an enum to reference them with.
5907   OS << "// ComplexPattern predicates.\n"
5908      << "enum {\n"
5909      << "  GICP_Invalid,\n";
5910   for (const auto &Record : ComplexPredicates)
5911     OS << "  GICP_" << Record->getName() << ",\n";
5912   OS << "};\n"
5913      << "// See constructor for table contents\n\n";
5914 
5915   emitImmPredicateFns(OS, "I64", "int64_t", [](const Record *R) {
5916     bool Unset;
5917     return !R->getValueAsBitOrUnset("IsAPFloat", Unset) &&
5918            !R->getValueAsBit("IsAPInt");
5919   });
5920   emitImmPredicateFns(OS, "APFloat", "const APFloat &", [](const Record *R) {
5921     bool Unset;
5922     return R->getValueAsBitOrUnset("IsAPFloat", Unset);
5923   });
5924   emitImmPredicateFns(OS, "APInt", "const APInt &", [](const Record *R) {
5925     return R->getValueAsBit("IsAPInt");
5926   });
5927   emitMIPredicateFns(OS);
5928   OS << "\n";
5929 
5930   OS << Target.getName() << "InstructionSelector::ComplexMatcherMemFn\n"
5931      << Target.getName() << "InstructionSelector::ComplexPredicateFns[] = {\n"
5932      << "  nullptr, // GICP_Invalid\n";
5933   for (const auto &Record : ComplexPredicates)
5934     OS << "  &" << Target.getName()
5935        << "InstructionSelector::" << Record->getValueAsString("MatcherFn")
5936        << ", // " << Record->getName() << "\n";
5937   OS << "};\n\n";
5938 
5939   OS << "// Custom renderers.\n"
5940      << "enum {\n"
5941      << "  GICR_Invalid,\n";
5942   for (const auto &Fn : CustomRendererFns)
5943     OS << "  GICR_" << Fn << ",\n";
5944   OS << "};\n";
5945 
5946   OS << Target.getName() << "InstructionSelector::CustomRendererFn\n"
5947      << Target.getName() << "InstructionSelector::CustomRenderers[] = {\n"
5948      << "  nullptr, // GICR_Invalid\n";
5949   for (const auto &Fn : CustomRendererFns)
5950     OS << "  &" << Target.getName() << "InstructionSelector::" << Fn << ",\n";
5951   OS << "};\n\n";
5952 
5953   llvm::stable_sort(Rules, [&](const RuleMatcher &A, const RuleMatcher &B) {
5954     int ScoreA = RuleMatcherScores[A.getRuleID()];
5955     int ScoreB = RuleMatcherScores[B.getRuleID()];
5956     if (ScoreA > ScoreB)
5957       return true;
5958     if (ScoreB > ScoreA)
5959       return false;
5960     if (A.isHigherPriorityThan(B)) {
5961       assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
5962                                            "and less important at "
5963                                            "the same time");
5964       return true;
5965     }
5966     return false;
5967   });
5968 
5969   OS << "bool " << Target.getName()
5970      << "InstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage "
5971         "&CoverageInfo) const {\n"
5972      << "  MachineFunction &MF = *I.getParent()->getParent();\n"
5973      << "  MachineRegisterInfo &MRI = MF.getRegInfo();\n"
5974      << "  const PredicateBitset AvailableFeatures = getAvailableFeatures();\n"
5975      << "  NewMIVector OutMIs;\n"
5976      << "  State.MIs.clear();\n"
5977      << "  State.MIs.push_back(&I);\n\n"
5978      << "  if (executeMatchTable(*this, OutMIs, State, ISelInfo"
5979      << ", getMatchTable(), TII, MRI, TRI, RBI, AvailableFeatures"
5980      << ", CoverageInfo)) {\n"
5981      << "    return true;\n"
5982      << "  }\n\n"
5983      << "  return false;\n"
5984      << "}\n\n";
5985 
5986   const MatchTable Table =
5987       buildMatchTable(Rules, OptimizeMatchTable, GenerateCoverage);
5988   OS << "const int64_t *" << Target.getName()
5989      << "InstructionSelector::getMatchTable() const {\n";
5990   Table.emitDeclaration(OS);
5991   OS << "  return ";
5992   Table.emitUse(OS);
5993   OS << ";\n}\n";
5994   OS << "#endif // ifdef GET_GLOBALISEL_IMPL\n";
5995 
5996   OS << "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n"
5997      << "PredicateBitset AvailableModuleFeatures;\n"
5998      << "mutable PredicateBitset AvailableFunctionFeatures;\n"
5999      << "PredicateBitset getAvailableFeatures() const {\n"
6000      << "  return AvailableModuleFeatures | AvailableFunctionFeatures;\n"
6001      << "}\n"
6002      << "PredicateBitset\n"
6003      << "computeAvailableModuleFeatures(const " << Target.getName()
6004      << "Subtarget *Subtarget) const;\n"
6005      << "PredicateBitset\n"
6006      << "computeAvailableFunctionFeatures(const " << Target.getName()
6007      << "Subtarget *Subtarget,\n"
6008      << "                                 const MachineFunction *MF) const;\n"
6009      << "void setupGeneratedPerFunctionState(MachineFunction &MF) override;\n"
6010      << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n";
6011 
6012   OS << "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n"
6013      << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n"
6014      << "AvailableFunctionFeatures()\n"
6015      << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n";
6016 }
6017 
6018 void GlobalISelEmitter::declareSubtargetFeature(Record *Predicate) {
6019   if (SubtargetFeatures.count(Predicate) == 0)
6020     SubtargetFeatures.emplace(
6021         Predicate, SubtargetFeatureInfo(Predicate, SubtargetFeatures.size()));
6022 }
6023 
6024 void RuleMatcher::optimize() {
6025   for (auto &Item : InsnVariableIDs) {
6026     InstructionMatcher &InsnMatcher = *Item.first;
6027     for (auto &OM : InsnMatcher.operands()) {
6028       // Complex Patterns are usually expensive and they relatively rarely fail
6029       // on their own: more often we end up throwing away all the work done by a
6030       // matching part of a complex pattern because some other part of the
6031       // enclosing pattern didn't match. All of this makes it beneficial to
6032       // delay complex patterns until the very end of the rule matching,
6033       // especially for targets having lots of complex patterns.
6034       for (auto &OP : OM->predicates())
6035         if (isa<ComplexPatternOperandMatcher>(OP))
6036           EpilogueMatchers.emplace_back(std::move(OP));
6037       OM->eraseNullPredicates();
6038     }
6039     InsnMatcher.optimize();
6040   }
6041   llvm::sort(EpilogueMatchers, [](const std::unique_ptr<PredicateMatcher> &L,
6042                                   const std::unique_ptr<PredicateMatcher> &R) {
6043     return std::make_tuple(L->getKind(), L->getInsnVarID(), L->getOpIdx()) <
6044            std::make_tuple(R->getKind(), R->getInsnVarID(), R->getOpIdx());
6045   });
6046 }
6047 
6048 bool RuleMatcher::hasFirstCondition() const {
6049   if (insnmatchers_empty())
6050     return false;
6051   InstructionMatcher &Matcher = insnmatchers_front();
6052   if (!Matcher.predicates_empty())
6053     return true;
6054   for (auto &OM : Matcher.operands())
6055     for (auto &OP : OM->predicates())
6056       if (!isa<InstructionOperandMatcher>(OP))
6057         return true;
6058   return false;
6059 }
6060 
6061 const PredicateMatcher &RuleMatcher::getFirstCondition() const {
6062   assert(!insnmatchers_empty() &&
6063          "Trying to get a condition from an empty RuleMatcher");
6064 
6065   InstructionMatcher &Matcher = insnmatchers_front();
6066   if (!Matcher.predicates_empty())
6067     return **Matcher.predicates_begin();
6068   // If there is no more predicate on the instruction itself, look at its
6069   // operands.
6070   for (auto &OM : Matcher.operands())
6071     for (auto &OP : OM->predicates())
6072       if (!isa<InstructionOperandMatcher>(OP))
6073         return *OP;
6074 
6075   llvm_unreachable("Trying to get a condition from an InstructionMatcher with "
6076                    "no conditions");
6077 }
6078 
6079 std::unique_ptr<PredicateMatcher> RuleMatcher::popFirstCondition() {
6080   assert(!insnmatchers_empty() &&
6081          "Trying to pop a condition from an empty RuleMatcher");
6082 
6083   InstructionMatcher &Matcher = insnmatchers_front();
6084   if (!Matcher.predicates_empty())
6085     return Matcher.predicates_pop_front();
6086   // If there is no more predicate on the instruction itself, look at its
6087   // operands.
6088   for (auto &OM : Matcher.operands())
6089     for (auto &OP : OM->predicates())
6090       if (!isa<InstructionOperandMatcher>(OP)) {
6091         std::unique_ptr<PredicateMatcher> Result = std::move(OP);
6092         OM->eraseNullPredicates();
6093         return Result;
6094       }
6095 
6096   llvm_unreachable("Trying to pop a condition from an InstructionMatcher with "
6097                    "no conditions");
6098 }
6099 
6100 bool GroupMatcher::candidateConditionMatches(
6101     const PredicateMatcher &Predicate) const {
6102 
6103   if (empty()) {
6104     // Sharing predicates for nested instructions is not supported yet as we
6105     // currently don't hoist the GIM_RecordInsn's properly, therefore we can
6106     // only work on the original root instruction (InsnVarID == 0):
6107     if (Predicate.getInsnVarID() != 0)
6108       return false;
6109     // ... otherwise an empty group can handle any predicate with no specific
6110     // requirements:
6111     return true;
6112   }
6113 
6114   const Matcher &Representative = **Matchers.begin();
6115   const auto &RepresentativeCondition = Representative.getFirstCondition();
6116   // ... if not empty, the group can only accomodate matchers with the exact
6117   // same first condition:
6118   return Predicate.isIdentical(RepresentativeCondition);
6119 }
6120 
6121 bool GroupMatcher::addMatcher(Matcher &Candidate) {
6122   if (!Candidate.hasFirstCondition())
6123     return false;
6124 
6125   const PredicateMatcher &Predicate = Candidate.getFirstCondition();
6126   if (!candidateConditionMatches(Predicate))
6127     return false;
6128 
6129   Matchers.push_back(&Candidate);
6130   return true;
6131 }
6132 
6133 void GroupMatcher::finalize() {
6134   assert(Conditions.empty() && "Already finalized?");
6135   if (empty())
6136     return;
6137 
6138   Matcher &FirstRule = **Matchers.begin();
6139   for (;;) {
6140     // All the checks are expected to succeed during the first iteration:
6141     for (const auto &Rule : Matchers)
6142       if (!Rule->hasFirstCondition())
6143         return;
6144     const auto &FirstCondition = FirstRule.getFirstCondition();
6145     for (unsigned I = 1, E = Matchers.size(); I < E; ++I)
6146       if (!Matchers[I]->getFirstCondition().isIdentical(FirstCondition))
6147         return;
6148 
6149     Conditions.push_back(FirstRule.popFirstCondition());
6150     for (unsigned I = 1, E = Matchers.size(); I < E; ++I)
6151       Matchers[I]->popFirstCondition();
6152   }
6153 }
6154 
6155 void GroupMatcher::emit(MatchTable &Table) {
6156   unsigned LabelID = ~0U;
6157   if (!Conditions.empty()) {
6158     LabelID = Table.allocateLabelID();
6159     Table << MatchTable::Opcode("GIM_Try", +1)
6160           << MatchTable::Comment("On fail goto")
6161           << MatchTable::JumpTarget(LabelID) << MatchTable::LineBreak;
6162   }
6163   for (auto &Condition : Conditions)
6164     Condition->emitPredicateOpcodes(
6165         Table, *static_cast<RuleMatcher *>(*Matchers.begin()));
6166 
6167   for (const auto &M : Matchers)
6168     M->emit(Table);
6169 
6170   // Exit the group
6171   if (!Conditions.empty())
6172     Table << MatchTable::Opcode("GIM_Reject", -1) << MatchTable::LineBreak
6173           << MatchTable::Label(LabelID);
6174 }
6175 
6176 bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher &P) {
6177   return isa<InstructionOpcodeMatcher>(P) || isa<LLTOperandMatcher>(P);
6178 }
6179 
6180 bool SwitchMatcher::candidateConditionMatches(
6181     const PredicateMatcher &Predicate) const {
6182 
6183   if (empty()) {
6184     // Sharing predicates for nested instructions is not supported yet as we
6185     // currently don't hoist the GIM_RecordInsn's properly, therefore we can
6186     // only work on the original root instruction (InsnVarID == 0):
6187     if (Predicate.getInsnVarID() != 0)
6188       return false;
6189     // ... while an attempt to add even a root matcher to an empty SwitchMatcher
6190     // could fail as not all the types of conditions are supported:
6191     if (!isSupportedPredicateType(Predicate))
6192       return false;
6193     // ... or the condition might not have a proper implementation of
6194     // getValue() / isIdenticalDownToValue() yet:
6195     if (!Predicate.hasValue())
6196       return false;
6197     // ... otherwise an empty Switch can accomodate the condition with no
6198     // further requirements:
6199     return true;
6200   }
6201 
6202   const Matcher &CaseRepresentative = **Matchers.begin();
6203   const auto &RepresentativeCondition = CaseRepresentative.getFirstCondition();
6204   // Switch-cases must share the same kind of condition and path to the value it
6205   // checks:
6206   if (!Predicate.isIdenticalDownToValue(RepresentativeCondition))
6207     return false;
6208 
6209   const auto Value = Predicate.getValue();
6210   // ... but be unique with respect to the actual value they check:
6211   return Values.count(Value) == 0;
6212 }
6213 
6214 bool SwitchMatcher::addMatcher(Matcher &Candidate) {
6215   if (!Candidate.hasFirstCondition())
6216     return false;
6217 
6218   const PredicateMatcher &Predicate = Candidate.getFirstCondition();
6219   if (!candidateConditionMatches(Predicate))
6220     return false;
6221   const auto Value = Predicate.getValue();
6222   Values.insert(Value);
6223 
6224   Matchers.push_back(&Candidate);
6225   return true;
6226 }
6227 
6228 void SwitchMatcher::finalize() {
6229   assert(Condition == nullptr && "Already finalized");
6230   assert(Values.size() == Matchers.size() && "Broken SwitchMatcher");
6231   if (empty())
6232     return;
6233 
6234   llvm::stable_sort(Matchers, [](const Matcher *L, const Matcher *R) {
6235     return L->getFirstCondition().getValue() <
6236            R->getFirstCondition().getValue();
6237   });
6238   Condition = Matchers[0]->popFirstCondition();
6239   for (unsigned I = 1, E = Values.size(); I < E; ++I)
6240     Matchers[I]->popFirstCondition();
6241 }
6242 
6243 void SwitchMatcher::emitPredicateSpecificOpcodes(const PredicateMatcher &P,
6244                                                  MatchTable &Table) {
6245   assert(isSupportedPredicateType(P) && "Predicate type is not supported");
6246 
6247   if (const auto *Condition = dyn_cast<InstructionOpcodeMatcher>(&P)) {
6248     Table << MatchTable::Opcode("GIM_SwitchOpcode") << MatchTable::Comment("MI")
6249           << MatchTable::IntValue(Condition->getInsnVarID());
6250     return;
6251   }
6252   if (const auto *Condition = dyn_cast<LLTOperandMatcher>(&P)) {
6253     Table << MatchTable::Opcode("GIM_SwitchType") << MatchTable::Comment("MI")
6254           << MatchTable::IntValue(Condition->getInsnVarID())
6255           << MatchTable::Comment("Op")
6256           << MatchTable::IntValue(Condition->getOpIdx());
6257     return;
6258   }
6259 
6260   llvm_unreachable("emitPredicateSpecificOpcodes is broken: can not handle a "
6261                    "predicate type that is claimed to be supported");
6262 }
6263 
6264 void SwitchMatcher::emit(MatchTable &Table) {
6265   assert(Values.size() == Matchers.size() && "Broken SwitchMatcher");
6266   if (empty())
6267     return;
6268   assert(Condition != nullptr &&
6269          "Broken SwitchMatcher, hasn't been finalized?");
6270 
6271   std::vector<unsigned> LabelIDs(Values.size());
6272   std::generate(LabelIDs.begin(), LabelIDs.end(),
6273                 [&Table]() { return Table.allocateLabelID(); });
6274   const unsigned Default = Table.allocateLabelID();
6275 
6276   const int64_t LowerBound = Values.begin()->getRawValue();
6277   const int64_t UpperBound = Values.rbegin()->getRawValue() + 1;
6278 
6279   emitPredicateSpecificOpcodes(*Condition, Table);
6280 
6281   Table << MatchTable::Comment("[") << MatchTable::IntValue(LowerBound)
6282         << MatchTable::IntValue(UpperBound) << MatchTable::Comment(")")
6283         << MatchTable::Comment("default:") << MatchTable::JumpTarget(Default);
6284 
6285   int64_t J = LowerBound;
6286   auto VI = Values.begin();
6287   for (unsigned I = 0, E = Values.size(); I < E; ++I) {
6288     auto V = *VI++;
6289     while (J++ < V.getRawValue())
6290       Table << MatchTable::IntValue(0);
6291     V.turnIntoComment();
6292     Table << MatchTable::LineBreak << V << MatchTable::JumpTarget(LabelIDs[I]);
6293   }
6294   Table << MatchTable::LineBreak;
6295 
6296   for (unsigned I = 0, E = Values.size(); I < E; ++I) {
6297     Table << MatchTable::Label(LabelIDs[I]);
6298     Matchers[I]->emit(Table);
6299     Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak;
6300   }
6301   Table << MatchTable::Label(Default);
6302 }
6303 
6304 unsigned OperandMatcher::getInsnVarID() const { return Insn.getInsnVarID(); }
6305 
6306 } // end anonymous namespace
6307 
6308 //===----------------------------------------------------------------------===//
6309 
6310 namespace llvm {
6311 void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) {
6312   GlobalISelEmitter(RK).run(OS);
6313 }
6314 } // End llvm namespace
6315