1 //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// This tablegen backend emits code for use by the GlobalISel instruction
12 /// selector. See include/llvm/CodeGen/TargetGlobalISel.td.
13 ///
14 /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen
15 /// backend, filters out the ones that are unsupported, maps
16 /// SelectionDAG-specific constructs to their GlobalISel counterpart
17 /// (when applicable: MVT to LLT;  SDNode to generic Instruction).
18 ///
19 /// Not all patterns are supported: pass the tablegen invocation
20 /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped,
21 /// as well as why.
22 ///
23 /// The generated file defines a single method:
24 ///     bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const;
25 /// intended to be used in InstructionSelector::select as the first-step
26 /// selector for the patterns that don't require complex C++.
27 ///
28 /// FIXME: We'll probably want to eventually define a base
29 /// "TargetGenInstructionSelector" class.
30 ///
31 //===----------------------------------------------------------------------===//
32 
33 #include "CodeGenDAGPatterns.h"
34 #include "llvm/ADT/Optional.h"
35 #include "llvm/ADT/SmallSet.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/CodeGen/MachineValueType.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Error.h"
40 #include "llvm/Support/LowLevelTypeImpl.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 <string>
46 #include <numeric>
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(NumPatternEmitted, "Number of patterns emitted");
55 
56 cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel");
57 
58 static cl::opt<bool> WarnOnSkippedPatterns(
59     "warn-on-skipped-patterns",
60     cl::desc("Explain why a pattern was skipped for inclusion "
61              "in the GlobalISel selector"),
62     cl::init(false), cl::cat(GlobalISelEmitterCat));
63 
64 namespace {
65 //===- Helper functions ---------------------------------------------------===//
66 
67 /// This class stands in for LLT wherever we want to tablegen-erate an
68 /// equivalent at compiler run-time.
69 class LLTCodeGen {
70 private:
71   LLT Ty;
72 
73 public:
74   LLTCodeGen(const LLT &Ty) : Ty(Ty) {}
75 
76   void emitCxxConstructorCall(raw_ostream &OS) const {
77     if (Ty.isScalar()) {
78       OS << "LLT::scalar(" << Ty.getSizeInBits() << ")";
79       return;
80     }
81     if (Ty.isVector()) {
82       OS << "LLT::vector(" << Ty.getNumElements() << ", " << Ty.getSizeInBits()
83          << ")";
84       return;
85     }
86     llvm_unreachable("Unhandled LLT");
87   }
88 
89   const LLT &get() const { return Ty; }
90 };
91 
92 class InstructionMatcher;
93 class OperandPlaceholder {
94 private:
95   enum PlaceholderKind {
96     OP_MatchReference,
97     OP_Temporary,
98   } Kind;
99 
100   struct MatchReferenceData {
101     InstructionMatcher *InsnMatcher;
102     StringRef InsnVarName;
103     StringRef SymbolicName;
104   };
105 
106   struct TemporaryData {
107     unsigned OpIdx;
108   };
109 
110   union {
111     struct MatchReferenceData MatchReference;
112     struct TemporaryData Temporary;
113   };
114 
115   OperandPlaceholder(PlaceholderKind Kind) : Kind(Kind) {}
116 
117 public:
118   ~OperandPlaceholder() {}
119 
120   static OperandPlaceholder
121   CreateMatchReference(InstructionMatcher *InsnMatcher,
122                        StringRef InsnVarName, StringRef SymbolicName) {
123     OperandPlaceholder Result(OP_MatchReference);
124     Result.MatchReference.InsnMatcher = InsnMatcher;
125     Result.MatchReference.InsnVarName = InsnVarName;
126     Result.MatchReference.SymbolicName = SymbolicName;
127     return Result;
128   }
129 
130   static OperandPlaceholder CreateTemporary(unsigned OpIdx) {
131     OperandPlaceholder Result(OP_Temporary);
132     Result.Temporary.OpIdx = OpIdx;
133     return Result;
134   }
135 
136   void emitCxxValueExpr(raw_ostream &OS) const;
137 };
138 
139 /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for
140 /// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...).
141 static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) {
142   MVT VT(SVT);
143   if (VT.isVector() && VT.getVectorNumElements() != 1)
144     return LLTCodeGen(LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits()));
145   if (VT.isInteger() || VT.isFloatingPoint())
146     return LLTCodeGen(LLT::scalar(VT.getSizeInBits()));
147   return None;
148 }
149 
150 static std::string explainPredicates(const TreePatternNode *N) {
151   std::string Explanation = "";
152   StringRef Separator = "";
153   for (const auto &P : N->getPredicateFns()) {
154     Explanation +=
155         (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str();
156     if (P.isAlwaysTrue())
157       Explanation += " always-true";
158     if (P.isImmediatePattern())
159       Explanation += " immediate";
160   }
161   return Explanation;
162 }
163 
164 static std::string explainRulePredicates(const ArrayRef<Init *> Predicates) {
165   std::string Explanation = "";
166   StringRef Separator = "";
167   for (const auto *P : Predicates) {
168     Explanation += Separator;
169 
170     if (const DefInit *PDef = dyn_cast<DefInit>(P)) {
171       Explanation += PDef->getDef()->getName();
172     } else
173       Explanation += "<unknown>";
174   }
175   return Explanation;
176 }
177 
178 std::string explainOperator(Record *Operator) {
179   if (Operator->isSubClassOf("SDNode"))
180     return " (" + Operator->getValueAsString("Opcode") + ")";
181 
182   if (Operator->isSubClassOf("Intrinsic"))
183     return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str();
184 
185   return " (Operator not understood)";
186 }
187 
188 /// Helper function to let the emitter report skip reason error messages.
189 static Error failedImport(const Twine &Reason) {
190   return make_error<StringError>(Reason, inconvertibleErrorCode());
191 }
192 
193 static Error isTrivialOperatorNode(const TreePatternNode *N) {
194   std::string Explanation = "";
195   std::string Separator = "";
196   if (N->isLeaf()) {
197     Explanation = "Is a leaf";
198     Separator = ", ";
199   }
200 
201   if (N->hasAnyPredicate()) {
202     Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")";
203     Separator = ", ";
204   }
205 
206   if (N->getTransformFn()) {
207     Explanation += Separator + "Has a transform function";
208     Separator = ", ";
209   }
210 
211   if (!N->isLeaf() && !N->hasAnyPredicate() && !N->getTransformFn())
212     return Error::success();
213 
214   return failedImport(Explanation);
215 }
216 
217 //===- Matchers -----------------------------------------------------------===//
218 
219 class OperandMatcher;
220 class MatchAction;
221 
222 /// Generates code to check that a match rule matches.
223 class RuleMatcher {
224   /// A list of matchers that all need to succeed for the current rule to match.
225   /// FIXME: This currently supports a single match position but could be
226   /// extended to support multiple positions to support div/rem fusion or
227   /// load-multiple instructions.
228   std::vector<std::unique_ptr<InstructionMatcher>> Matchers;
229 
230   /// A list of actions that need to be taken when all predicates in this rule
231   /// have succeeded.
232   std::vector<std::unique_ptr<MatchAction>> Actions;
233 
234   /// A map of instruction matchers to the local variables created by
235   /// emitCxxCaptureStmts().
236   std::map<const InstructionMatcher *, std::string> InsnVariableNames;
237 
238   /// ID for the next instruction variable defined with defineInsnVar()
239   unsigned NextInsnVarID;
240 
241 public:
242   RuleMatcher()
243       : Matchers(), Actions(), InsnVariableNames(), NextInsnVarID(0) {}
244   RuleMatcher(RuleMatcher &&Other) = default;
245   RuleMatcher &operator=(RuleMatcher &&Other) = default;
246 
247   InstructionMatcher &addInstructionMatcher();
248 
249   template <class Kind, class... Args> Kind &addAction(Args &&... args);
250 
251   std::string defineInsnVar(raw_ostream &OS, const InstructionMatcher &Matcher,
252                             StringRef Value);
253   StringRef getInsnVarName(const InstructionMatcher &InsnMatcher) const;
254 
255   void emitCxxCapturedInsnList(raw_ostream &OS);
256   void emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr);
257 
258   void emit(raw_ostream &OS);
259 
260   /// Compare the priority of this object and B.
261   ///
262   /// Returns true if this object is more important than B.
263   bool isHigherPriorityThan(const RuleMatcher &B) const;
264 
265   /// Report the maximum number of temporary operands needed by the rule
266   /// matcher.
267   unsigned countTemporaryOperands() const;
268 };
269 
270 template <class PredicateTy> class PredicateListMatcher {
271 private:
272   typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec;
273   PredicateVec Predicates;
274 
275 public:
276   /// Construct a new operand predicate and add it to the matcher.
277   template <class Kind, class... Args>
278   Kind &addPredicate(Args&&... args) {
279     Predicates.emplace_back(
280         llvm::make_unique<Kind>(std::forward<Args>(args)...));
281     return *static_cast<Kind *>(Predicates.back().get());
282   }
283 
284   typename PredicateVec::const_iterator predicates_begin() const { return Predicates.begin(); }
285   typename PredicateVec::const_iterator predicates_end() const { return Predicates.end(); }
286   iterator_range<typename PredicateVec::const_iterator> predicates() const {
287     return make_range(predicates_begin(), predicates_end());
288   }
289   typename PredicateVec::size_type predicates_size() const { return Predicates.size(); }
290 
291   /// Emit a C++ expression that tests whether all the predicates are met.
292   template <class... Args>
293   void emitCxxPredicateListExpr(raw_ostream &OS, Args &&... args) const {
294     if (Predicates.empty()) {
295       OS << "true";
296       return;
297     }
298 
299     StringRef Separator = "";
300     for (const auto &Predicate : predicates()) {
301       OS << Separator << "(";
302       Predicate->emitCxxPredicateExpr(OS, std::forward<Args>(args)...);
303       OS << ")";
304       Separator = " &&\n";
305     }
306   }
307 };
308 
309 /// Generates code to check a predicate of an operand.
310 ///
311 /// Typical predicates include:
312 /// * Operand is a particular register.
313 /// * Operand is assigned a particular register bank.
314 /// * Operand is an MBB.
315 class OperandPredicateMatcher {
316 public:
317   /// This enum is used for RTTI and also defines the priority that is given to
318   /// the predicate when generating the matcher code. Kinds with higher priority
319   /// must be tested first.
320   ///
321   /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
322   /// but OPM_Int must have priority over OPM_RegBank since constant integers
323   /// are represented by a virtual register defined by a G_CONSTANT instruction.
324   enum PredicateKind {
325     OPM_ComplexPattern,
326     OPM_Instruction,
327     OPM_Int,
328     OPM_LLT,
329     OPM_RegBank,
330     OPM_MBB,
331   };
332 
333 protected:
334   PredicateKind Kind;
335 
336 public:
337   OperandPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
338   virtual ~OperandPredicateMatcher() {}
339 
340   PredicateKind getKind() const { return Kind; }
341 
342   /// Return the OperandMatcher for the specified operand or nullptr if there
343   /// isn't one by that name in this operand predicate matcher.
344   ///
345   /// InstructionOperandMatcher is the only subclass that can return non-null
346   /// for this.
347   virtual Optional<const OperandMatcher *>
348   getOptionalOperand(StringRef SymbolicName) const {
349     assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
350     return None;
351   }
352 
353   /// Emit C++ statements to capture instructions into local variables.
354   ///
355   /// Only InstructionOperandMatcher needs to do anything for this method.
356   virtual void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule,
357                                    StringRef Expr) const {}
358 
359   /// Emit a C++ expression that checks the predicate for the given operand.
360   virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
361                                     StringRef OperandExpr) const = 0;
362 
363   /// Compare the priority of this object and B.
364   ///
365   /// Returns true if this object is more important than B.
366   virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const {
367     return Kind < B.Kind;
368   };
369 
370   /// Report the maximum number of temporary operands needed by the predicate
371   /// matcher.
372   virtual unsigned countTemporaryOperands() const { return 0; }
373 };
374 
375 /// Generates code to check that an operand is a particular LLT.
376 class LLTOperandMatcher : public OperandPredicateMatcher {
377 protected:
378   LLTCodeGen Ty;
379 
380 public:
381   LLTOperandMatcher(const LLTCodeGen &Ty)
382       : OperandPredicateMatcher(OPM_LLT), Ty(Ty) {}
383 
384   static bool classof(const OperandPredicateMatcher *P) {
385     return P->getKind() == OPM_LLT;
386   }
387 
388   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
389                             StringRef OperandExpr) const override {
390     OS << "MRI.getType(" << OperandExpr << ".getReg()) == (";
391     Ty.emitCxxConstructorCall(OS);
392     OS << ")";
393   }
394 };
395 
396 /// Generates code to check that an operand is a particular target constant.
397 class ComplexPatternOperandMatcher : public OperandPredicateMatcher {
398 protected:
399   const OperandMatcher &Operand;
400   const Record &TheDef;
401 
402   unsigned getNumOperands() const {
403     return TheDef.getValueAsDag("Operands")->getNumArgs();
404   }
405 
406   unsigned getAllocatedTemporariesBaseID() const;
407 
408 public:
409   ComplexPatternOperandMatcher(const OperandMatcher &Operand,
410                                const Record &TheDef)
411       : OperandPredicateMatcher(OPM_ComplexPattern), Operand(Operand),
412         TheDef(TheDef) {}
413 
414   static bool classof(const OperandPredicateMatcher *P) {
415     return P->getKind() == OPM_ComplexPattern;
416   }
417 
418   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
419                             StringRef OperandExpr) const override {
420     OS << TheDef.getValueAsString("MatcherFn") << "(" << OperandExpr;
421     for (unsigned I = 0; I < getNumOperands(); ++I) {
422       OS << ", ";
423       OperandPlaceholder::CreateTemporary(getAllocatedTemporariesBaseID() + I)
424           .emitCxxValueExpr(OS);
425     }
426     OS << ")";
427   }
428 
429   unsigned countTemporaryOperands() const override {
430     return getNumOperands();
431   }
432 };
433 
434 /// Generates code to check that an operand is in a particular register bank.
435 class RegisterBankOperandMatcher : public OperandPredicateMatcher {
436 protected:
437   const CodeGenRegisterClass &RC;
438 
439 public:
440   RegisterBankOperandMatcher(const CodeGenRegisterClass &RC)
441       : OperandPredicateMatcher(OPM_RegBank), RC(RC) {}
442 
443   static bool classof(const OperandPredicateMatcher *P) {
444     return P->getKind() == OPM_RegBank;
445   }
446 
447   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
448                             StringRef OperandExpr) const override {
449     OS << "(&RBI.getRegBankFromRegClass(" << RC.getQualifiedName()
450        << "RegClass) == RBI.getRegBank(" << OperandExpr
451        << ".getReg(), MRI, TRI))";
452   }
453 };
454 
455 /// Generates code to check that an operand is a basic block.
456 class MBBOperandMatcher : public OperandPredicateMatcher {
457 public:
458   MBBOperandMatcher() : OperandPredicateMatcher(OPM_MBB) {}
459 
460   static bool classof(const OperandPredicateMatcher *P) {
461     return P->getKind() == OPM_MBB;
462   }
463 
464   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
465                             StringRef OperandExpr) const override {
466     OS << OperandExpr << ".isMBB()";
467   }
468 };
469 
470 /// Generates code to check that an operand is a particular int.
471 class IntOperandMatcher : public OperandPredicateMatcher {
472 protected:
473   int64_t Value;
474 
475 public:
476   IntOperandMatcher(int64_t Value)
477       : OperandPredicateMatcher(OPM_Int), Value(Value) {}
478 
479   static bool classof(const OperandPredicateMatcher *P) {
480     return P->getKind() == OPM_Int;
481   }
482 
483   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
484                             StringRef OperandExpr) const override {
485     OS << "isOperandImmEqual(" << OperandExpr << ", " << Value << ", MRI)";
486   }
487 };
488 
489 /// Generates code to check that a set of predicates match for a particular
490 /// operand.
491 class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> {
492 protected:
493   InstructionMatcher &Insn;
494   unsigned OpIdx;
495   std::string SymbolicName;
496 
497   /// The index of the first temporary variable allocated to this operand. The
498   /// number of allocated temporaries can be found with
499   /// countTemporaryOperands().
500   unsigned AllocatedTemporariesBaseID;
501 
502 public:
503   OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx,
504                  const std::string &SymbolicName,
505                  unsigned AllocatedTemporariesBaseID)
506       : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName),
507         AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {}
508 
509   bool hasSymbolicName() const { return !SymbolicName.empty(); }
510   const StringRef getSymbolicName() const { return SymbolicName; }
511   void setSymbolicName(StringRef Name) {
512     assert(SymbolicName.empty() && "Operand already has a symbolic name");
513     SymbolicName = Name;
514   }
515   unsigned getOperandIndex() const { return OpIdx; }
516 
517   std::string getOperandExpr(StringRef InsnVarName) const {
518     return (InsnVarName + ".getOperand(" + llvm::to_string(OpIdx) + ")").str();
519   }
520 
521   Optional<const OperandMatcher *>
522   getOptionalOperand(StringRef DesiredSymbolicName) const {
523     assert(!DesiredSymbolicName.empty() && "Cannot lookup unnamed operand");
524     if (DesiredSymbolicName == SymbolicName)
525       return this;
526     for (const auto &OP : predicates()) {
527       const auto &MaybeOperand = OP->getOptionalOperand(DesiredSymbolicName);
528       if (MaybeOperand.hasValue())
529         return MaybeOperand.getValue();
530     }
531     return None;
532   }
533 
534   InstructionMatcher &getInstructionMatcher() const { return Insn; }
535 
536   /// Emit C++ statements to capture instructions into local variables.
537   void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule,
538                            StringRef OperandExpr) const {
539     for (const auto &Predicate : predicates())
540       Predicate->emitCxxCaptureStmts(OS, Rule, OperandExpr);
541   }
542 
543   /// Emit a C++ expression that tests whether the instruction named in
544   /// InsnVarName matches all the predicate and all the operands.
545   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
546                             StringRef InsnVarName) const {
547     OS << "(/* ";
548     if (SymbolicName.empty())
549       OS << "Operand " << OpIdx;
550     else
551       OS << SymbolicName;
552     OS << " */ ";
553     emitCxxPredicateListExpr(OS, Rule, getOperandExpr(InsnVarName));
554     OS << ")";
555   }
556 
557   /// Compare the priority of this object and B.
558   ///
559   /// Returns true if this object is more important than B.
560   bool isHigherPriorityThan(const OperandMatcher &B) const {
561     // Operand matchers involving more predicates have higher priority.
562     if (predicates_size() > B.predicates_size())
563       return true;
564     if (predicates_size() < B.predicates_size())
565       return false;
566 
567     // This assumes that predicates are added in a consistent order.
568     for (const auto &Predicate : zip(predicates(), B.predicates())) {
569       if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
570         return true;
571       if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
572         return false;
573     }
574 
575     return false;
576   };
577 
578   /// Report the maximum number of temporary operands needed by the operand
579   /// matcher.
580   unsigned countTemporaryOperands() const {
581     return std::accumulate(
582         predicates().begin(), predicates().end(), 0,
583         [](unsigned A,
584            const std::unique_ptr<OperandPredicateMatcher> &Predicate) {
585           return A + Predicate->countTemporaryOperands();
586         });
587   }
588 
589   unsigned getAllocatedTemporariesBaseID() const {
590     return AllocatedTemporariesBaseID;
591   }
592 };
593 
594 unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const {
595   return Operand.getAllocatedTemporariesBaseID();
596 }
597 
598 /// Generates code to check a predicate on an instruction.
599 ///
600 /// Typical predicates include:
601 /// * The opcode of the instruction is a particular value.
602 /// * The nsw/nuw flag is/isn't set.
603 class InstructionPredicateMatcher {
604 protected:
605   /// This enum is used for RTTI and also defines the priority that is given to
606   /// the predicate when generating the matcher code. Kinds with higher priority
607   /// must be tested first.
608   enum PredicateKind {
609     IPM_Opcode,
610   };
611 
612   PredicateKind Kind;
613 
614 public:
615   InstructionPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
616   virtual ~InstructionPredicateMatcher() {}
617 
618   PredicateKind getKind() const { return Kind; }
619 
620   /// Emit a C++ expression that tests whether the instruction named in
621   /// InsnVarName matches the predicate.
622   virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
623                                     StringRef InsnVarName) const = 0;
624 
625   /// Compare the priority of this object and B.
626   ///
627   /// Returns true if this object is more important than B.
628   virtual bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const {
629     return Kind < B.Kind;
630   };
631 
632   /// Report the maximum number of temporary operands needed by the predicate
633   /// matcher.
634   virtual unsigned countTemporaryOperands() const { return 0; }
635 };
636 
637 /// Generates code to check the opcode of an instruction.
638 class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
639 protected:
640   const CodeGenInstruction *I;
641 
642 public:
643   InstructionOpcodeMatcher(const CodeGenInstruction *I)
644       : InstructionPredicateMatcher(IPM_Opcode), I(I) {}
645 
646   static bool classof(const InstructionPredicateMatcher *P) {
647     return P->getKind() == IPM_Opcode;
648   }
649 
650   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
651                             StringRef InsnVarName) const override {
652     OS << InsnVarName << ".getOpcode() == " << I->Namespace
653        << "::" << I->TheDef->getName();
654   }
655 
656   /// Compare the priority of this object and B.
657   ///
658   /// Returns true if this object is more important than B.
659   bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const override {
660     if (InstructionPredicateMatcher::isHigherPriorityThan(B))
661       return true;
662     if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this))
663       return false;
664 
665     // Prioritize opcodes for cosmetic reasons in the generated source. Although
666     // this is cosmetic at the moment, we may want to drive a similar ordering
667     // using instruction frequency information to improve compile time.
668     if (const InstructionOpcodeMatcher *BO =
669             dyn_cast<InstructionOpcodeMatcher>(&B))
670       return I->TheDef->getName() < BO->I->TheDef->getName();
671 
672     return false;
673   };
674 };
675 
676 /// Generates code to check that a set of predicates and operands match for a
677 /// particular instruction.
678 ///
679 /// Typical predicates include:
680 /// * Has a specific opcode.
681 /// * Has an nsw/nuw flag or doesn't.
682 class InstructionMatcher
683     : public PredicateListMatcher<InstructionPredicateMatcher> {
684 protected:
685   typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec;
686 
687   /// The operands to match. All rendered operands must be present even if the
688   /// condition is always true.
689   OperandVec Operands;
690 
691 public:
692   /// Add an operand to the matcher.
693   OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName,
694                              unsigned AllocatedTemporariesBaseID) {
695     Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName,
696                                              AllocatedTemporariesBaseID));
697     return *Operands.back();
698   }
699 
700   OperandMatcher &getOperand(unsigned OpIdx) {
701     auto I = std::find_if(Operands.begin(), Operands.end(),
702                           [&OpIdx](const std::unique_ptr<OperandMatcher> &X) {
703                             return X->getOperandIndex() == OpIdx;
704                           });
705     if (I != Operands.end())
706       return **I;
707     llvm_unreachable("Failed to lookup operand");
708   }
709 
710   Optional<const OperandMatcher *>
711   getOptionalOperand(StringRef SymbolicName) const {
712     assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
713     for (const auto &Operand : Operands) {
714       const auto &OM = Operand->getOptionalOperand(SymbolicName);
715       if (OM.hasValue())
716         return OM.getValue();
717     }
718     return None;
719   }
720 
721   const OperandMatcher &getOperand(StringRef SymbolicName) const {
722     Optional<const OperandMatcher *>OM = getOptionalOperand(SymbolicName);
723     if (OM.hasValue())
724       return *OM.getValue();
725     llvm_unreachable("Failed to lookup operand");
726   }
727 
728   unsigned getNumOperands() const { return Operands.size(); }
729   OperandVec::iterator operands_begin() { return Operands.begin(); }
730   OperandVec::iterator operands_end() { return Operands.end(); }
731   iterator_range<OperandVec::iterator> operands() {
732     return make_range(operands_begin(), operands_end());
733   }
734   OperandVec::const_iterator operands_begin() const { return Operands.begin(); }
735   OperandVec::const_iterator operands_end() const { return Operands.end(); }
736   iterator_range<OperandVec::const_iterator> operands() const {
737     return make_range(operands_begin(), operands_end());
738   }
739 
740   /// Emit C++ statements to check the shape of the match and capture
741   /// instructions into local variables.
742   void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule, StringRef Expr) {
743     OS << "if (" << Expr << ".getNumOperands() < " << getNumOperands() << ")\n"
744        << "  return false;\n";
745     for (const auto &Operand : Operands) {
746       Operand->emitCxxCaptureStmts(OS, Rule, Operand->getOperandExpr(Expr));
747     }
748   }
749 
750   /// Emit a C++ expression that tests whether the instruction named in
751   /// InsnVarName matches all the predicates and all the operands.
752   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
753                             StringRef InsnVarName) const {
754     emitCxxPredicateListExpr(OS, Rule, InsnVarName);
755     for (const auto &Operand : Operands) {
756       OS << " &&\n(";
757       Operand->emitCxxPredicateExpr(OS, Rule, InsnVarName);
758       OS << ")";
759     }
760   }
761 
762   /// Compare the priority of this object and B.
763   ///
764   /// Returns true if this object is more important than B.
765   bool isHigherPriorityThan(const InstructionMatcher &B) const {
766     // Instruction matchers involving more operands have higher priority.
767     if (Operands.size() > B.Operands.size())
768       return true;
769     if (Operands.size() < B.Operands.size())
770       return false;
771 
772     for (const auto &Predicate : zip(predicates(), B.predicates())) {
773       if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
774         return true;
775       if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
776         return false;
777     }
778 
779     for (const auto &Operand : zip(Operands, B.Operands)) {
780       if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand)))
781         return true;
782       if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand)))
783         return false;
784     }
785 
786     return false;
787   };
788 
789   /// Report the maximum number of temporary operands needed by the instruction
790   /// matcher.
791   unsigned countTemporaryOperands() const {
792     return std::accumulate(predicates().begin(), predicates().end(), 0,
793                            [](unsigned A,
794                               const std::unique_ptr<InstructionPredicateMatcher>
795                                   &Predicate) {
796                              return A + Predicate->countTemporaryOperands();
797                            }) +
798            std::accumulate(
799                Operands.begin(), Operands.end(), 0,
800                [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) {
801                  return A + Operand->countTemporaryOperands();
802                });
803   }
804 };
805 
806 /// Generates code to check that the operand is a register defined by an
807 /// instruction that matches the given instruction matcher.
808 ///
809 /// For example, the pattern:
810 ///   (set $dst, (G_MUL (G_ADD $src1, $src2), $src3))
811 /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match
812 /// the:
813 ///   (G_ADD $src1, $src2)
814 /// subpattern.
815 class InstructionOperandMatcher : public OperandPredicateMatcher {
816 protected:
817   std::unique_ptr<InstructionMatcher> InsnMatcher;
818 
819 public:
820   InstructionOperandMatcher()
821       : OperandPredicateMatcher(OPM_Instruction),
822         InsnMatcher(new InstructionMatcher()) {}
823 
824   static bool classof(const OperandPredicateMatcher *P) {
825     return P->getKind() == OPM_Instruction;
826   }
827 
828   InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; }
829 
830   Optional<const OperandMatcher *>
831   getOptionalOperand(StringRef SymbolicName) const override {
832     assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
833     return InsnMatcher->getOptionalOperand(SymbolicName);
834   }
835 
836   void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule,
837                            StringRef OperandExpr) const override {
838     OS << "if (!" << OperandExpr + ".isReg())\n"
839        << "  return false;\n";
840     std::string InsnVarName = Rule.defineInsnVar(
841         OS, *InsnMatcher,
842         ("*MRI.getVRegDef(" + OperandExpr + ".getReg())").str());
843     InsnMatcher->emitCxxCaptureStmts(OS, Rule, InsnVarName);
844   }
845 
846   void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
847                             StringRef OperandExpr) const override {
848     OperandExpr = Rule.getInsnVarName(*InsnMatcher);
849     OS << "(";
850     InsnMatcher->emitCxxPredicateExpr(OS, Rule, OperandExpr);
851     OS << ")\n";
852   }
853 };
854 
855 //===- Actions ------------------------------------------------------------===//
856 void OperandPlaceholder::emitCxxValueExpr(raw_ostream &OS) const {
857   switch (Kind) {
858   case OP_MatchReference:
859     OS << MatchReference.InsnMatcher->getOperand(MatchReference.SymbolicName)
860               .getOperandExpr(MatchReference.InsnVarName);
861     break;
862   case OP_Temporary:
863     OS << "TempOp" << Temporary.OpIdx;
864     break;
865   }
866 }
867 
868 class OperandRenderer {
869 public:
870   enum RendererKind { OR_Copy, OR_Imm, OR_Register, OR_ComplexPattern };
871 
872 protected:
873   RendererKind Kind;
874 
875 public:
876   OperandRenderer(RendererKind Kind) : Kind(Kind) {}
877   virtual ~OperandRenderer() {}
878 
879   RendererKind getKind() const { return Kind; }
880 
881   virtual void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const = 0;
882 };
883 
884 /// A CopyRenderer emits code to copy a single operand from an existing
885 /// instruction to the one being built.
886 class CopyRenderer : public OperandRenderer {
887 protected:
888   /// The matcher for the instruction that this operand is copied from.
889   /// This provides the facility for looking up an a operand by it's name so
890   /// that it can be used as a source for the instruction being built.
891   const InstructionMatcher &Matched;
892   /// The name of the operand.
893   const StringRef SymbolicName;
894 
895 public:
896   CopyRenderer(const InstructionMatcher &Matched, StringRef SymbolicName)
897       : OperandRenderer(OR_Copy), Matched(Matched), SymbolicName(SymbolicName) {
898   }
899 
900   static bool classof(const OperandRenderer *R) {
901     return R->getKind() == OR_Copy;
902   }
903 
904   const StringRef getSymbolicName() const { return SymbolicName; }
905 
906   void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
907     const OperandMatcher &Operand = Matched.getOperand(SymbolicName);
908     StringRef InsnVarName =
909         Rule.getInsnVarName(Operand.getInstructionMatcher());
910     std::string OperandExpr = Operand.getOperandExpr(InsnVarName);
911     OS << "    MIB.add(" << OperandExpr << "/*" << SymbolicName << "*/);\n";
912   }
913 };
914 
915 /// Adds a specific physical register to the instruction being built.
916 /// This is typically useful for WZR/XZR on AArch64.
917 class AddRegisterRenderer : public OperandRenderer {
918 protected:
919   const Record *RegisterDef;
920 
921 public:
922   AddRegisterRenderer(const Record *RegisterDef)
923       : OperandRenderer(OR_Register), RegisterDef(RegisterDef) {}
924 
925   static bool classof(const OperandRenderer *R) {
926     return R->getKind() == OR_Register;
927   }
928 
929   void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
930     OS << "    MIB.addReg(" << RegisterDef->getValueAsString("Namespace")
931        << "::" << RegisterDef->getName() << ");\n";
932   }
933 };
934 
935 /// Adds a specific immediate to the instruction being built.
936 class ImmRenderer : public OperandRenderer {
937 protected:
938   int64_t Imm;
939 
940 public:
941   ImmRenderer(int64_t Imm)
942       : OperandRenderer(OR_Imm), Imm(Imm) {}
943 
944   static bool classof(const OperandRenderer *R) {
945     return R->getKind() == OR_Imm;
946   }
947 
948   void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
949     OS << "    MIB.addImm(" << Imm << ");\n";
950   }
951 };
952 
953 class RenderComplexPatternOperand : public OperandRenderer {
954 private:
955   const Record &TheDef;
956   std::vector<OperandPlaceholder> Sources;
957 
958   unsigned getNumOperands() const {
959     return TheDef.getValueAsDag("Operands")->getNumArgs();
960   }
961 
962 public:
963   RenderComplexPatternOperand(const Record &TheDef,
964                               const ArrayRef<OperandPlaceholder> Sources)
965       : OperandRenderer(OR_ComplexPattern), TheDef(TheDef), Sources(Sources) {}
966 
967   static bool classof(const OperandRenderer *R) {
968     return R->getKind() == OR_ComplexPattern;
969   }
970 
971   void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
972     assert(Sources.size() == getNumOperands() && "Inconsistent number of operands");
973     for (const auto &Source : Sources) {
974       OS << "MIB.add(";
975       Source.emitCxxValueExpr(OS);
976       OS << ");\n";
977     }
978   }
979 };
980 
981 /// An action taken when all Matcher predicates succeeded for a parent rule.
982 ///
983 /// Typical actions include:
984 /// * Changing the opcode of an instruction.
985 /// * Adding an operand to an instruction.
986 class MatchAction {
987 public:
988   virtual ~MatchAction() {}
989 
990   /// Emit the C++ statements to implement the action.
991   ///
992   /// \param RecycleVarName If given, it's an instruction to recycle. The
993   ///                       requirements on the instruction vary from action to
994   ///                       action.
995   virtual void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
996                                   StringRef RecycleVarName) const = 0;
997 };
998 
999 /// Generates a comment describing the matched rule being acted upon.
1000 class DebugCommentAction : public MatchAction {
1001 private:
1002   const PatternToMatch &P;
1003 
1004 public:
1005   DebugCommentAction(const PatternToMatch &P) : P(P) {}
1006 
1007   void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
1008                           StringRef RecycleVarName) const override {
1009     OS << "// " << *P.getSrcPattern() << "  =>  " << *P.getDstPattern() << "\n";
1010   }
1011 };
1012 
1013 /// Generates code to build an instruction or mutate an existing instruction
1014 /// into the desired instruction when this is possible.
1015 class BuildMIAction : public MatchAction {
1016 private:
1017   const CodeGenInstruction *I;
1018   const InstructionMatcher &Matched;
1019   std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers;
1020 
1021   /// True if the instruction can be built solely by mutating the opcode.
1022   bool canMutate() const {
1023     for (const auto &Renderer : enumerate(OperandRenderers)) {
1024       if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) {
1025         if (Matched.getOperand(Copy->getSymbolicName()).getOperandIndex() !=
1026             Renderer.index())
1027           return false;
1028       } else
1029         return false;
1030     }
1031 
1032     return true;
1033   }
1034 
1035 public:
1036   BuildMIAction(const CodeGenInstruction *I, const InstructionMatcher &Matched)
1037       : I(I), Matched(Matched) {}
1038 
1039   template <class Kind, class... Args>
1040   Kind &addRenderer(Args&&... args) {
1041     OperandRenderers.emplace_back(
1042         llvm::make_unique<Kind>(std::forward<Args>(args)...));
1043     return *static_cast<Kind *>(OperandRenderers.back().get());
1044   }
1045 
1046   void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
1047                           StringRef RecycleVarName) const override {
1048     if (canMutate()) {
1049       OS << "    " << RecycleVarName << ".setDesc(TII.get(" << I->Namespace
1050          << "::" << I->TheDef->getName() << "));\n";
1051 
1052       if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) {
1053         OS << "    auto MIB = MachineInstrBuilder(MF, &" << RecycleVarName
1054            << ");\n";
1055 
1056         for (auto Def : I->ImplicitDefs) {
1057           auto Namespace = Def->getValueAsString("Namespace");
1058           OS << "    MIB.addDef(" << Namespace << "::" << Def->getName()
1059              << ", RegState::Implicit);\n";
1060         }
1061         for (auto Use : I->ImplicitUses) {
1062           auto Namespace = Use->getValueAsString("Namespace");
1063           OS << "    MIB.addUse(" << Namespace << "::" << Use->getName()
1064              << ", RegState::Implicit);\n";
1065         }
1066       }
1067 
1068       OS << "    MachineInstr &NewI = " << RecycleVarName << ";\n";
1069       return;
1070     }
1071 
1072     // TODO: Simple permutation looks like it could be almost as common as
1073     //       mutation due to commutative operations.
1074 
1075     OS << "MachineInstrBuilder MIB = BuildMI(*I.getParent(), I, "
1076           "I.getDebugLoc(), TII.get("
1077        << I->Namespace << "::" << I->TheDef->getName() << "));\n";
1078     for (const auto &Renderer : OperandRenderers)
1079       Renderer->emitCxxRenderStmts(OS, Rule);
1080     OS << "    for (const auto *FromMI : ";
1081     Rule.emitCxxCapturedInsnList(OS);
1082     OS << ")\n";
1083     OS << "      for (const auto &MMO : FromMI->memoperands())\n";
1084     OS << "        MIB.addMemOperand(MMO);\n";
1085     OS << "    " << RecycleVarName << ".eraseFromParent();\n";
1086     OS << "    MachineInstr &NewI = *MIB;\n";
1087   }
1088 };
1089 
1090 InstructionMatcher &RuleMatcher::addInstructionMatcher() {
1091   Matchers.emplace_back(new InstructionMatcher());
1092   return *Matchers.back();
1093 }
1094 
1095 template <class Kind, class... Args>
1096 Kind &RuleMatcher::addAction(Args &&... args) {
1097   Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...));
1098   return *static_cast<Kind *>(Actions.back().get());
1099 }
1100 
1101 std::string RuleMatcher::defineInsnVar(raw_ostream &OS,
1102                                        const InstructionMatcher &Matcher,
1103                                        StringRef Value) {
1104   std::string InsnVarName = "MI" + llvm::to_string(NextInsnVarID++);
1105   OS << "MachineInstr &" << InsnVarName << " = " << Value << ";\n";
1106   InsnVariableNames[&Matcher] = InsnVarName;
1107   return InsnVarName;
1108 }
1109 
1110 StringRef RuleMatcher::getInsnVarName(const InstructionMatcher &InsnMatcher) const {
1111   const auto &I = InsnVariableNames.find(&InsnMatcher);
1112   if (I != InsnVariableNames.end())
1113     return I->second;
1114   llvm_unreachable("Matched Insn was not captured in a local variable");
1115 }
1116 
1117 /// Emit a C++ initializer_list containing references to every matched instruction.
1118 void RuleMatcher::emitCxxCapturedInsnList(raw_ostream &OS) {
1119   SmallVector<StringRef, 2> Names;
1120   for (const auto &Pair : InsnVariableNames)
1121     Names.push_back(Pair.second);
1122   std::sort(Names.begin(), Names.end());
1123 
1124   OS << "{";
1125   for (const auto &Name : Names)
1126     OS << "&" << Name << ", ";
1127   OS << "}";
1128 }
1129 
1130 /// Emit C++ statements to check the shape of the match and capture
1131 /// instructions into local variables.
1132 void RuleMatcher::emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr) {
1133   assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
1134   std::string InsnVarName = defineInsnVar(OS, *Matchers.front(), Expr);
1135   Matchers.front()->emitCxxCaptureStmts(OS, *this, InsnVarName);
1136 }
1137 
1138 void RuleMatcher::emit(raw_ostream &OS) {
1139   if (Matchers.empty())
1140     llvm_unreachable("Unexpected empty matcher!");
1141 
1142   // The representation supports rules that require multiple roots such as:
1143   //    %ptr(p0) = ...
1144   //    %elt0(s32) = G_LOAD %ptr
1145   //    %1(p0) = G_ADD %ptr, 4
1146   //    %elt1(s32) = G_LOAD p0 %1
1147   // which could be usefully folded into:
1148   //    %ptr(p0) = ...
1149   //    %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
1150   // on some targets but we don't need to make use of that yet.
1151   assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
1152   OS << "if ([&]() {\n";
1153 
1154   emitCxxCaptureStmts(OS, "I");
1155 
1156   OS << "    if (";
1157   Matchers.front()->emitCxxPredicateExpr(OS, *this,
1158                                          getInsnVarName(*Matchers.front()));
1159   OS << ") {\n";
1160 
1161   // We must also check if it's safe to fold the matched instructions.
1162   if (InsnVariableNames.size() >= 2) {
1163     for (const auto &Pair : InsnVariableNames) {
1164       // Skip the root node since it isn't moving anywhere. Everything else is
1165       // sinking to meet it.
1166       if (Pair.first == Matchers.front().get())
1167         continue;
1168 
1169       // Reject the difficult cases until we have a more accurate check.
1170       OS << "      if (!isObviouslySafeToFold(" << Pair.second
1171          << ")) return false;\n";
1172 
1173       // FIXME: Emit checks to determine it's _actually_ safe to fold and/or
1174       //        account for unsafe cases.
1175       //
1176       //        Example:
1177       //          MI1--> %0 = ...
1178       //                 %1 = ... %0
1179       //          MI0--> %2 = ... %0
1180       //          It's not safe to erase MI1. We currently handle this by not
1181       //          erasing %0 (even when it's dead).
1182       //
1183       //        Example:
1184       //          MI1--> %0 = load volatile @a
1185       //                 %1 = load volatile @a
1186       //          MI0--> %2 = ... %0
1187       //          It's not safe to sink %0's def past %1. We currently handle
1188       //          this by rejecting all loads.
1189       //
1190       //        Example:
1191       //          MI1--> %0 = load @a
1192       //                 %1 = store @a
1193       //          MI0--> %2 = ... %0
1194       //          It's not safe to sink %0's def past %1. We currently handle
1195       //          this by rejecting all loads.
1196       //
1197       //        Example:
1198       //                   G_CONDBR %cond, @BB1
1199       //                 BB0:
1200       //          MI1-->   %0 = load @a
1201       //                   G_BR @BB1
1202       //                 BB1:
1203       //          MI0-->   %2 = ... %0
1204       //          It's not always safe to sink %0 across control flow. In this
1205       //          case it may introduce a memory fault. We currentl handle this
1206       //          by rejecting all loads.
1207     }
1208   }
1209 
1210   for (const auto &MA : Actions) {
1211     MA->emitCxxActionStmts(OS, *this, "I");
1212   }
1213 
1214   OS << "      constrainSelectedInstRegOperands(NewI, TII, TRI, RBI);\n";
1215   OS << "      return true;\n";
1216   OS << "    }\n";
1217   OS << "    return false;\n";
1218   OS << "  }()) { return true; }\n\n";
1219 }
1220 
1221 bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const {
1222   // Rules involving more match roots have higher priority.
1223   if (Matchers.size() > B.Matchers.size())
1224     return true;
1225   if (Matchers.size() < B.Matchers.size())
1226     return false;
1227 
1228   for (const auto &Matcher : zip(Matchers, B.Matchers)) {
1229     if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher)))
1230       return true;
1231     if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher)))
1232       return false;
1233   }
1234 
1235   return false;
1236 }
1237 
1238 unsigned RuleMatcher::countTemporaryOperands() const {
1239   return std::accumulate(
1240       Matchers.begin(), Matchers.end(), 0,
1241       [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) {
1242         return A + Matcher->countTemporaryOperands();
1243       });
1244 }
1245 
1246 //===- GlobalISelEmitter class --------------------------------------------===//
1247 
1248 class GlobalISelEmitter {
1249 public:
1250   explicit GlobalISelEmitter(RecordKeeper &RK);
1251   void run(raw_ostream &OS);
1252 
1253 private:
1254   const RecordKeeper &RK;
1255   const CodeGenDAGPatterns CGP;
1256   const CodeGenTarget &Target;
1257 
1258   /// Keep track of the equivalence between SDNodes and Instruction.
1259   /// This is defined using 'GINodeEquiv' in the target description.
1260   DenseMap<Record *, const CodeGenInstruction *> NodeEquivs;
1261 
1262   /// Keep track of the equivalence between ComplexPattern's and
1263   /// GIComplexOperandMatcher. Map entries are specified by subclassing
1264   /// GIComplexPatternEquiv.
1265   DenseMap<const Record *, const Record *> ComplexPatternEquivs;
1266 
1267   void gatherNodeEquivs();
1268   const CodeGenInstruction *findNodeEquiv(Record *N) const;
1269 
1270   Error importRulePredicates(RuleMatcher &M, ArrayRef<Init *> Predicates) const;
1271   Expected<InstructionMatcher &>
1272   createAndImportSelDAGMatcher(InstructionMatcher &InsnMatcher,
1273                                const TreePatternNode *Src) const;
1274   Error importChildMatcher(InstructionMatcher &InsnMatcher,
1275                            TreePatternNode *SrcChild, unsigned OpIdx,
1276                            unsigned &TempOpIdx) const;
1277   Expected<BuildMIAction &> createAndImportInstructionRenderer(
1278       RuleMatcher &M, const TreePatternNode *Dst,
1279       const InstructionMatcher &InsnMatcher) const;
1280   Error importExplicitUseRenderer(BuildMIAction &DstMIBuilder,
1281                                   TreePatternNode *DstChild,
1282                                   const InstructionMatcher &InsnMatcher) const;
1283   Error
1284   importImplicitDefRenderers(BuildMIAction &DstMIBuilder,
1285                              const std::vector<Record *> &ImplicitDefs) const;
1286 
1287   /// Analyze pattern \p P, returning a matcher for it if possible.
1288   /// Otherwise, return an Error explaining why we don't support it.
1289   Expected<RuleMatcher> runOnPattern(const PatternToMatch &P);
1290 };
1291 
1292 void GlobalISelEmitter::gatherNodeEquivs() {
1293   assert(NodeEquivs.empty());
1294   for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
1295     NodeEquivs[Equiv->getValueAsDef("Node")] =
1296         &Target.getInstruction(Equiv->getValueAsDef("I"));
1297 
1298   assert(ComplexPatternEquivs.empty());
1299   for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
1300     Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
1301     if (!SelDAGEquiv)
1302       continue;
1303     ComplexPatternEquivs[SelDAGEquiv] = Equiv;
1304  }
1305 }
1306 
1307 const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const {
1308   return NodeEquivs.lookup(N);
1309 }
1310 
1311 GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
1312     : RK(RK), CGP(RK), Target(CGP.getTargetInfo()) {}
1313 
1314 //===- Emitter ------------------------------------------------------------===//
1315 
1316 Error
1317 GlobalISelEmitter::importRulePredicates(RuleMatcher &M,
1318                                         ArrayRef<Init *> Predicates) const {
1319   if (!Predicates.empty())
1320     return failedImport("Pattern has a rule predicate (" +
1321                         explainRulePredicates(Predicates) + ")");
1322   return Error::success();
1323 }
1324 
1325 Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
1326     InstructionMatcher &InsnMatcher, const TreePatternNode *Src) const {
1327   // Start with the defined operands (i.e., the results of the root operator).
1328   if (Src->getExtTypes().size() > 1)
1329     return failedImport("Src pattern has multiple results");
1330 
1331   auto SrcGIOrNull = findNodeEquiv(Src->getOperator());
1332   if (!SrcGIOrNull)
1333     return failedImport("Pattern operator lacks an equivalent Instruction" +
1334                         explainOperator(Src->getOperator()));
1335   auto &SrcGI = *SrcGIOrNull;
1336 
1337   // The operators look good: match the opcode and mutate it to the new one.
1338   InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI);
1339 
1340   unsigned OpIdx = 0;
1341   unsigned TempOpIdx = 0;
1342   for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
1343     auto OpTyOrNone = MVTToLLT(Ty.getConcrete());
1344 
1345     if (!OpTyOrNone)
1346       return failedImport(
1347           "Result of Src pattern operator has an unsupported type");
1348 
1349     // Results don't have a name unless they are the root node. The caller will
1350     // set the name if appropriate.
1351     OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx);
1352     OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1353   }
1354 
1355   // Match the used operands (i.e. the children of the operator).
1356   for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) {
1357     if (auto Error = importChildMatcher(InsnMatcher, Src->getChild(i), OpIdx++,
1358                                         TempOpIdx))
1359       return std::move(Error);
1360   }
1361 
1362   return InsnMatcher;
1363 }
1364 
1365 Error GlobalISelEmitter::importChildMatcher(InstructionMatcher &InsnMatcher,
1366                                             TreePatternNode *SrcChild,
1367                                             unsigned OpIdx,
1368                                             unsigned &TempOpIdx) const {
1369   OperandMatcher &OM =
1370       InsnMatcher.addOperand(OpIdx, SrcChild->getName(), TempOpIdx);
1371 
1372   if (SrcChild->hasAnyPredicate())
1373     return failedImport("Src pattern child has predicate (" +
1374                         explainPredicates(SrcChild) + ")");
1375 
1376   ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes();
1377   if (ChildTypes.size() != 1)
1378     return failedImport("Src pattern child has multiple results");
1379 
1380   // Check MBB's before the type check since they are not a known type.
1381   if (!SrcChild->isLeaf()) {
1382     if (SrcChild->getOperator()->isSubClassOf("SDNode")) {
1383       auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator());
1384       if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1385         OM.addPredicate<MBBOperandMatcher>();
1386         return Error::success();
1387       }
1388     }
1389   }
1390 
1391   auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1392   if (!OpTyOrNone)
1393     return failedImport("Src operand has an unsupported type");
1394   OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1395 
1396   // Check for nested instructions.
1397   if (!SrcChild->isLeaf()) {
1398     // Map the node to a gMIR instruction.
1399     InstructionOperandMatcher &InsnOperand =
1400         OM.addPredicate<InstructionOperandMatcher>();
1401     auto InsnMatcherOrError =
1402         createAndImportSelDAGMatcher(InsnOperand.getInsnMatcher(), SrcChild);
1403     if (auto Error = InsnMatcherOrError.takeError())
1404       return Error;
1405 
1406     return Error::success();
1407   }
1408 
1409   // Check for constant immediates.
1410   if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) {
1411     OM.addPredicate<IntOperandMatcher>(ChildInt->getValue());
1412     return Error::success();
1413   }
1414 
1415   // Check for def's like register classes or ComplexPattern's.
1416   if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
1417     auto *ChildRec = ChildDefInit->getDef();
1418 
1419     // Check for register classes.
1420     if (ChildRec->isSubClassOf("RegisterClass")) {
1421       OM.addPredicate<RegisterBankOperandMatcher>(
1422           Target.getRegisterClass(ChildRec));
1423       return Error::success();
1424     }
1425 
1426     // Check for ComplexPattern's.
1427     if (ChildRec->isSubClassOf("ComplexPattern")) {
1428       const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1429       if (ComplexPattern == ComplexPatternEquivs.end())
1430         return failedImport("SelectionDAG ComplexPattern (" +
1431                             ChildRec->getName() + ") not mapped to GlobalISel");
1432 
1433       const auto &Predicate = OM.addPredicate<ComplexPatternOperandMatcher>(
1434           OM, *ComplexPattern->second);
1435       TempOpIdx += Predicate.countTemporaryOperands();
1436       return Error::success();
1437     }
1438 
1439     if (ChildRec->isSubClassOf("ImmLeaf")) {
1440       return failedImport(
1441           "Src pattern child def is an unsupported tablegen class (ImmLeaf)");
1442     }
1443 
1444     return failedImport(
1445         "Src pattern child def is an unsupported tablegen class");
1446   }
1447 
1448   return failedImport("Src pattern child is an unsupported kind");
1449 }
1450 
1451 Error GlobalISelEmitter::importExplicitUseRenderer(
1452     BuildMIAction &DstMIBuilder, TreePatternNode *DstChild,
1453     const InstructionMatcher &InsnMatcher) const {
1454   // The only non-leaf child we accept is 'bb': it's an operator because
1455   // BasicBlockSDNode isn't inline, but in MI it's just another operand.
1456   if (!DstChild->isLeaf()) {
1457     if (DstChild->getOperator()->isSubClassOf("SDNode")) {
1458       auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator());
1459       if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1460         DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher,
1461                                                DstChild->getName());
1462         return Error::success();
1463       }
1464     }
1465     return failedImport("Dst pattern child isn't a leaf node or an MBB");
1466   }
1467 
1468   // Otherwise, we're looking for a bog-standard RegisterClass operand.
1469   if (DstChild->hasAnyPredicate())
1470     return failedImport("Dst pattern child has predicate (" +
1471                         explainPredicates(DstChild) + ")");
1472 
1473   if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) {
1474     auto *ChildRec = ChildDefInit->getDef();
1475 
1476     ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes();
1477     if (ChildTypes.size() != 1)
1478       return failedImport("Dst pattern child has multiple results");
1479 
1480     auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1481     if (!OpTyOrNone)
1482       return failedImport("Dst operand has an unsupported type");
1483 
1484     if (ChildRec->isSubClassOf("Register")) {
1485       DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec);
1486       return Error::success();
1487     }
1488 
1489     if (ChildRec->isSubClassOf("RegisterClass")) {
1490       DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstChild->getName());
1491       return Error::success();
1492     }
1493 
1494     if (ChildRec->isSubClassOf("ComplexPattern")) {
1495       const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1496       if (ComplexPattern == ComplexPatternEquivs.end())
1497         return failedImport(
1498             "SelectionDAG ComplexPattern not mapped to GlobalISel");
1499 
1500       SmallVector<OperandPlaceholder, 2> RenderedOperands;
1501       const OperandMatcher &OM = InsnMatcher.getOperand(DstChild->getName());
1502       for (unsigned I = 0; I < OM.countTemporaryOperands(); ++I)
1503         RenderedOperands.push_back(OperandPlaceholder::CreateTemporary(
1504             OM.getAllocatedTemporariesBaseID() + I));
1505       DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
1506           *ComplexPattern->second, RenderedOperands);
1507       return Error::success();
1508     }
1509 
1510     if (ChildRec->isSubClassOf("SDNodeXForm"))
1511       return failedImport("Dst pattern child def is an unsupported tablegen "
1512                           "class (SDNodeXForm)");
1513 
1514     return failedImport(
1515         "Dst pattern child def is an unsupported tablegen class");
1516   }
1517 
1518   return failedImport("Dst pattern child is an unsupported kind");
1519 }
1520 
1521 Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer(
1522     RuleMatcher &M, const TreePatternNode *Dst,
1523     const InstructionMatcher &InsnMatcher) const {
1524   Record *DstOp = Dst->getOperator();
1525   if (!DstOp->isSubClassOf("Instruction")) {
1526     if (DstOp->isSubClassOf("ValueType"))
1527       return failedImport(
1528           "Pattern operator isn't an instruction (it's a ValueType)");
1529     return failedImport("Pattern operator isn't an instruction");
1530   }
1531   auto &DstI = Target.getInstruction(DstOp);
1532 
1533   auto &DstMIBuilder = M.addAction<BuildMIAction>(&DstI, InsnMatcher);
1534 
1535   // Render the explicit defs.
1536   for (unsigned I = 0; I < DstI.Operands.NumDefs; ++I) {
1537     const auto &DstIOperand = DstI.Operands[I];
1538     DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstIOperand.Name);
1539   }
1540 
1541   // Figure out which operands need defaults inserted. Operands that subclass
1542   // OperandWithDefaultOps are considered from left to right until we have
1543   // enough operands to render the instruction.
1544   SmallSet<unsigned, 2> DefaultOperands;
1545   unsigned DstINumUses = DstI.Operands.size() - DstI.Operands.NumDefs;
1546   unsigned NumDefaultOperands = 0;
1547   for (unsigned I = 0; I < DstINumUses &&
1548                        DstINumUses > Dst->getNumChildren() + NumDefaultOperands;
1549        ++I) {
1550     const auto &DstIOperand = DstI.Operands[DstI.Operands.NumDefs + I];
1551     if (DstIOperand.Rec->isSubClassOf("OperandWithDefaultOps")) {
1552       DefaultOperands.insert(I);
1553       NumDefaultOperands +=
1554           DstIOperand.Rec->getValueAsDag("DefaultOps")->getNumArgs();
1555     }
1556   }
1557   if (DstINumUses > Dst->getNumChildren() + DefaultOperands.size())
1558     return failedImport("Insufficient operands supplied and default ops "
1559                         "couldn't make up the shortfall");
1560   if (DstINumUses < Dst->getNumChildren() + DefaultOperands.size())
1561     return failedImport("Too many operands supplied");
1562 
1563   // Render the explicit uses.
1564   unsigned Child = 0;
1565   for (unsigned I = 0; I != DstINumUses; ++I) {
1566     // If we need to insert default ops here, then do so.
1567     if (DefaultOperands.count(I)) {
1568       const auto &DstIOperand = DstI.Operands[DstI.Operands.NumDefs + I];
1569 
1570       DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps");
1571       for (const auto *DefaultOp : DefaultOps->args()) {
1572         // Look through ValueType operators.
1573         if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) {
1574           if (const DefInit *DefaultDagOperator =
1575                   dyn_cast<DefInit>(DefaultDagOp->getOperator())) {
1576             if (DefaultDagOperator->getDef()->isSubClassOf("ValueType"))
1577               DefaultOp = DefaultDagOp->getArg(0);
1578           }
1579         }
1580 
1581         if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) {
1582           DstMIBuilder.addRenderer<AddRegisterRenderer>(DefaultDefOp->getDef());
1583           continue;
1584         }
1585 
1586         if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) {
1587           DstMIBuilder.addRenderer<ImmRenderer>(DefaultIntOp->getValue());
1588           continue;
1589         }
1590 
1591         return failedImport("Could not add default op");
1592       }
1593 
1594       continue;
1595     }
1596 
1597     if (auto Error = importExplicitUseRenderer(
1598             DstMIBuilder, Dst->getChild(Child), InsnMatcher))
1599       return std::move(Error);
1600     ++Child;
1601   }
1602 
1603   return DstMIBuilder;
1604 }
1605 
1606 Error GlobalISelEmitter::importImplicitDefRenderers(
1607     BuildMIAction &DstMIBuilder,
1608     const std::vector<Record *> &ImplicitDefs) const {
1609   if (!ImplicitDefs.empty())
1610     return failedImport("Pattern defines a physical register");
1611   return Error::success();
1612 }
1613 
1614 Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
1615   // Keep track of the matchers and actions to emit.
1616   RuleMatcher M;
1617   M.addAction<DebugCommentAction>(P);
1618 
1619   if (auto Error = importRulePredicates(M, P.getPredicates()->getValues()))
1620     return std::move(Error);
1621 
1622   // Next, analyze the pattern operators.
1623   TreePatternNode *Src = P.getSrcPattern();
1624   TreePatternNode *Dst = P.getDstPattern();
1625 
1626   // If the root of either pattern isn't a simple operator, ignore it.
1627   if (auto Err = isTrivialOperatorNode(Dst))
1628     return failedImport("Dst pattern root isn't a trivial operator (" +
1629                         toString(std::move(Err)) + ")");
1630   if (auto Err = isTrivialOperatorNode(Src))
1631     return failedImport("Src pattern root isn't a trivial operator (" +
1632                         toString(std::move(Err)) + ")");
1633 
1634   // Start with the defined operands (i.e., the results of the root operator).
1635   Record *DstOp = Dst->getOperator();
1636   if (!DstOp->isSubClassOf("Instruction"))
1637     return failedImport("Pattern operator isn't an instruction");
1638 
1639   auto &DstI = Target.getInstruction(DstOp);
1640   if (DstI.Operands.NumDefs != Src->getExtTypes().size())
1641     return failedImport("Src pattern results and dst MI defs are different (" +
1642                         to_string(Src->getExtTypes().size()) + " def(s) vs " +
1643                         to_string(DstI.Operands.NumDefs) + " def(s))");
1644 
1645   InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher();
1646   auto InsnMatcherOrError = createAndImportSelDAGMatcher(InsnMatcherTemp, Src);
1647   if (auto Error = InsnMatcherOrError.takeError())
1648     return std::move(Error);
1649   InstructionMatcher &InsnMatcher = InsnMatcherOrError.get();
1650 
1651   // The root of the match also has constraints on the register bank so that it
1652   // matches the result instruction.
1653   unsigned OpIdx = 0;
1654   for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
1655     (void)Ty;
1656 
1657     const auto &DstIOperand = DstI.Operands[OpIdx];
1658     Record *DstIOpRec = DstIOperand.Rec;
1659     if (!DstIOpRec->isSubClassOf("RegisterClass"))
1660       return failedImport("Dst MI def isn't a register class");
1661 
1662     OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
1663     OM.setSymbolicName(DstIOperand.Name);
1664     OM.addPredicate<RegisterBankOperandMatcher>(
1665         Target.getRegisterClass(DstIOpRec));
1666     ++OpIdx;
1667   }
1668 
1669   auto DstMIBuilderOrError =
1670       createAndImportInstructionRenderer(M, Dst, InsnMatcher);
1671   if (auto Error = DstMIBuilderOrError.takeError())
1672     return std::move(Error);
1673   BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get();
1674 
1675   // Render the implicit defs.
1676   // These are only added to the root of the result.
1677   if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs()))
1678     return std::move(Error);
1679 
1680   // We're done with this pattern!  It's eligible for GISel emission; return it.
1681   ++NumPatternImported;
1682   return std::move(M);
1683 }
1684 
1685 void GlobalISelEmitter::run(raw_ostream &OS) {
1686   // Track the GINodeEquiv definitions.
1687   gatherNodeEquivs();
1688 
1689   emitSourceFileHeader(("Global Instruction Selector for the " +
1690                        Target.getName() + " target").str(), OS);
1691   std::vector<RuleMatcher> Rules;
1692   // Look through the SelectionDAG patterns we found, possibly emitting some.
1693   for (const PatternToMatch &Pat : CGP.ptms()) {
1694     ++NumPatternTotal;
1695     auto MatcherOrErr = runOnPattern(Pat);
1696 
1697     // The pattern analysis can fail, indicating an unsupported pattern.
1698     // Report that if we've been asked to do so.
1699     if (auto Err = MatcherOrErr.takeError()) {
1700       if (WarnOnSkippedPatterns) {
1701         PrintWarning(Pat.getSrcRecord()->getLoc(),
1702                      "Skipped pattern: " + toString(std::move(Err)));
1703       } else {
1704         consumeError(std::move(Err));
1705       }
1706       ++NumPatternImportsSkipped;
1707       continue;
1708     }
1709 
1710     Rules.push_back(std::move(MatcherOrErr.get()));
1711   }
1712 
1713   std::stable_sort(Rules.begin(), Rules.end(),
1714             [&](const RuleMatcher &A, const RuleMatcher &B) {
1715               if (A.isHigherPriorityThan(B)) {
1716                 assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
1717                                                      "and less important at "
1718                                                      "the same time");
1719                 return true;
1720               }
1721               return false;
1722             });
1723 
1724   unsigned MaxTemporaries = 0;
1725   for (const auto &Rule : Rules)
1726     MaxTemporaries = std::max(MaxTemporaries, Rule.countTemporaryOperands());
1727 
1728   OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n";
1729   for (unsigned I = 0; I < MaxTemporaries; ++I)
1730     OS << "  mutable MachineOperand TempOp" << I << ";\n";
1731   OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
1732 
1733   OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n";
1734   for (unsigned I = 0; I < MaxTemporaries; ++I)
1735     OS << ", TempOp" << I << "(MachineOperand::CreatePlaceholder())\n";
1736   OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
1737 
1738   OS << "#ifdef GET_GLOBALISEL_IMPL\n"
1739      << "bool " << Target.getName()
1740      << "InstructionSelector::selectImpl(MachineInstr &I) const {\n"
1741      << "  MachineFunction &MF = *I.getParent()->getParent();\n"
1742      << "  const MachineRegisterInfo &MRI = MF.getRegInfo();\n";
1743 
1744   for (auto &Rule : Rules) {
1745     Rule.emit(OS);
1746     ++NumPatternEmitted;
1747   }
1748 
1749   OS << "  return false;\n"
1750      << "}\n"
1751      << "#endif // ifdef GET_GLOBALISEL_IMPL\n";
1752 }
1753 
1754 } // end anonymous namespace
1755 
1756 //===----------------------------------------------------------------------===//
1757 
1758 namespace llvm {
1759 void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) {
1760   GlobalISelEmitter(RK).run(OS);
1761 }
1762 } // End llvm namespace
1763