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