xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===- GlobalISelCombinerMatchTableEmitter.cpp - --------------------------===//
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 Generate a combiner implementation for GlobalISel from a declarative
10 /// syntax using GlobalISelMatchTable.
11 ///
12 /// Usually, TableGen backends use "assert is an error" as a means to report
13 /// invalid input. They try to diagnose common case but don't try very hard and
14 /// crashes can be common. This backend aims to behave closer to how a language
15 /// compiler frontend would behave: we try extra hard to diagnose invalid inputs
16 /// early, and any crash should be considered a bug (= a feature or diagnostic
17 /// is missing).
18 ///
19 /// While this can make the backend a bit more complex than it needs to be, it
20 /// pays off because MIR patterns can get complicated. Giving useful error
21 /// messages to combine writers can help boost their productivity.
22 ///
23 /// As with anything, a good balance has to be found. We also don't want to
24 /// write hundreds of lines of code to detect edge cases. In practice, crashing
25 /// very occasionally, or giving poor errors in some rare instances, is fine.
26 ///
27 //===----------------------------------------------------------------------===//
28 
29 #include "CodeGenInstruction.h"
30 #include "CodeGenTarget.h"
31 #include "GlobalISel/CXXPredicates.h"
32 #include "GlobalISel/CodeExpander.h"
33 #include "GlobalISel/CodeExpansions.h"
34 #include "GlobalISel/CombinerUtils.h"
35 #include "GlobalISel/MatchDataInfo.h"
36 #include "GlobalISel/Patterns.h"
37 #include "GlobalISelMatchTable.h"
38 #include "GlobalISelMatchTableExecutorEmitter.h"
39 #include "SubtargetFeatureInfo.h"
40 #include "llvm/ADT/APInt.h"
41 #include "llvm/ADT/EquivalenceClasses.h"
42 #include "llvm/ADT/Hashing.h"
43 #include "llvm/ADT/MapVector.h"
44 #include "llvm/ADT/SetVector.h"
45 #include "llvm/ADT/Statistic.h"
46 #include "llvm/ADT/StringSet.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/PrettyStackTrace.h"
50 #include "llvm/Support/ScopedPrinter.h"
51 #include "llvm/TableGen/Error.h"
52 #include "llvm/TableGen/Record.h"
53 #include "llvm/TableGen/StringMatcher.h"
54 #include "llvm/TableGen/TableGenBackend.h"
55 #include <cstdint>
56 
57 using namespace llvm;
58 using namespace llvm::gi;
59 
60 #define DEBUG_TYPE "gicombiner-emitter"
61 
62 namespace {
63 cl::OptionCategory
64     GICombinerEmitterCat("Options for -gen-global-isel-combiner");
65 cl::opt<bool> StopAfterParse(
66     "gicombiner-stop-after-parse",
67     cl::desc("Stop processing after parsing rules and dump state"),
68     cl::cat(GICombinerEmitterCat));
69 cl::list<std::string>
70     SelectedCombiners("combiners", cl::desc("Emit the specified combiners"),
71                       cl::cat(GICombinerEmitterCat), cl::CommaSeparated);
72 cl::opt<bool> DebugCXXPreds(
73     "gicombiner-debug-cxxpreds",
74     cl::desc("Add Contextual/Debug comments to all C++ predicates"),
75     cl::cat(GICombinerEmitterCat));
76 cl::opt<bool> DebugTypeInfer("gicombiner-debug-typeinfer",
77                              cl::desc("Print type inference debug logs"),
78                              cl::cat(GICombinerEmitterCat));
79 
80 constexpr StringLiteral CXXApplyPrefix = "GICXXCustomAction_CombineApply";
81 constexpr StringLiteral CXXPredPrefix = "GICXXPred_MI_Predicate_";
82 constexpr StringLiteral MIFlagsEnumClassName = "MIFlagEnum";
83 
84 //===- CodeExpansions Helpers  --------------------------------------------===//
85 
86 void declareInstExpansion(CodeExpansions &CE, const InstructionMatcher &IM,
87                           StringRef Name) {
88   CE.declare(Name, "State.MIs[" + to_string(IM.getInsnVarID()) + "]");
89 }
90 
91 void declareInstExpansion(CodeExpansions &CE, const BuildMIAction &A,
92                           StringRef Name) {
93   // Note: we use redeclare here because this may overwrite a matcher inst
94   // expansion.
95   CE.redeclare(Name, "OutMIs[" + to_string(A.getInsnID()) + "]");
96 }
97 
98 void declareOperandExpansion(CodeExpansions &CE, const OperandMatcher &OM,
99                              StringRef Name) {
100   CE.declare(Name, "State.MIs[" + to_string(OM.getInsnVarID()) +
101                        "]->getOperand(" + to_string(OM.getOpIdx()) + ")");
102 }
103 
104 void declareTempRegExpansion(CodeExpansions &CE, unsigned TempRegID,
105                              StringRef Name) {
106   CE.declare(Name, "State.TempRegisters[" + to_string(TempRegID) + "]");
107 }
108 
109 //===- Misc. Helpers  -----------------------------------------------------===//
110 
111 /// Copies a StringRef into a static pool to preserve it.
112 /// Most Pattern classes use StringRef so we need this.
113 StringRef insertStrRef(StringRef S) {
114   if (S.empty())
115     return {};
116 
117   static StringSet<> Pool;
118   auto [It, Inserted] = Pool.insert(S);
119   return It->getKey();
120 }
121 
122 template <typename Container> auto keys(Container &&C) {
123   return map_range(C, [](auto &Entry) -> auto & { return Entry.first; });
124 }
125 
126 template <typename Container> auto values(Container &&C) {
127   return map_range(C, [](auto &Entry) -> auto & { return Entry.second; });
128 }
129 
130 std::string getIsEnabledPredicateEnumName(unsigned CombinerRuleID) {
131   return "GICXXPred_Simple_IsRule" + to_string(CombinerRuleID) + "Enabled";
132 }
133 
134 //===- MatchTable Helpers  ------------------------------------------------===//
135 
136 LLTCodeGen getLLTCodeGen(const PatternType &PT) {
137   return *MVTToLLT(getValueType(PT.getLLTRecord()));
138 }
139 
140 LLTCodeGenOrTempType getLLTCodeGenOrTempType(const PatternType &PT,
141                                              RuleMatcher &RM) {
142   assert(!PT.isNone());
143 
144   if (PT.isLLT())
145     return getLLTCodeGen(PT);
146 
147   assert(PT.isTypeOf());
148   auto &OM = RM.getOperandMatcher(PT.getTypeOfOpName());
149   return OM.getTempTypeIdx(RM);
150 }
151 
152 //===- PrettyStackTrace Helpers  ------------------------------------------===//
153 
154 class PrettyStackTraceParse : public PrettyStackTraceEntry {
155   const Record &Def;
156 
157 public:
158   PrettyStackTraceParse(const Record &Def) : Def(Def) {}
159 
160   void print(raw_ostream &OS) const override {
161     if (Def.isSubClassOf("GICombineRule"))
162       OS << "Parsing GICombineRule '" << Def.getName() << "'";
163     else if (Def.isSubClassOf(PatFrag::ClassName))
164       OS << "Parsing " << PatFrag::ClassName << " '" << Def.getName() << "'";
165     else
166       OS << "Parsing '" << Def.getName() << "'";
167     OS << '\n';
168   }
169 };
170 
171 class PrettyStackTraceEmit : public PrettyStackTraceEntry {
172   const Record &Def;
173   const Pattern *Pat = nullptr;
174 
175 public:
176   PrettyStackTraceEmit(const Record &Def, const Pattern *Pat = nullptr)
177       : Def(Def), Pat(Pat) {}
178 
179   void print(raw_ostream &OS) const override {
180     if (Def.isSubClassOf("GICombineRule"))
181       OS << "Emitting GICombineRule '" << Def.getName() << "'";
182     else if (Def.isSubClassOf(PatFrag::ClassName))
183       OS << "Emitting " << PatFrag::ClassName << " '" << Def.getName() << "'";
184     else
185       OS << "Emitting '" << Def.getName() << "'";
186 
187     if (Pat)
188       OS << " [" << Pat->getKindName() << " '" << Pat->getName() << "']";
189     OS << '\n';
190   }
191 };
192 
193 //===- CombineRuleOperandTypeChecker --------------------------------------===//
194 
195 /// This is a wrapper around OperandTypeChecker specialized for Combiner Rules.
196 /// On top of doing the same things as OperandTypeChecker, this also attempts to
197 /// infer as many types as possible for temporary register defs & immediates in
198 /// apply patterns.
199 ///
200 /// The inference is trivial and leverages the MCOI OperandTypes encoded in
201 /// CodeGenInstructions to infer types across patterns in a CombineRule. It's
202 /// thus very limited and only supports CodeGenInstructions (but that's the main
203 /// use case so it's fine).
204 ///
205 /// We only try to infer untyped operands in apply patterns when they're temp
206 /// reg defs, or immediates. Inference always outputs a `TypeOf<$x>` where $x is
207 /// a named operand from a match pattern.
208 class CombineRuleOperandTypeChecker : private OperandTypeChecker {
209 public:
210   CombineRuleOperandTypeChecker(const Record &RuleDef,
211                                 const OperandTable &MatchOpTable)
212       : OperandTypeChecker(RuleDef.getLoc()), RuleDef(RuleDef),
213         MatchOpTable(MatchOpTable) {}
214 
215   /// Records and checks a 'match' pattern.
216   bool processMatchPattern(InstructionPattern &P);
217 
218   /// Records and checks an 'apply' pattern.
219   bool processApplyPattern(InstructionPattern &P);
220 
221   /// Propagates types, then perform type inference and do a second round of
222   /// propagation in the apply patterns only if any types were inferred.
223   void propagateAndInferTypes();
224 
225 private:
226   /// TypeEquivalenceClasses are groups of operands of an instruction that share
227   /// a common type.
228   ///
229   /// e.g. [[a, b], [c, d]] means a and b have the same type, and c and
230   /// d have the same type too. b/c and a/d don't have to have the same type,
231   /// though.
232   using TypeEquivalenceClasses = EquivalenceClasses<StringRef>;
233 
234   /// \returns true for `OPERAND_GENERIC_` 0 through 5.
235   /// These are the MCOI types that can be registers. The other MCOI types are
236   /// either immediates, or fancier operands used only post-ISel, so we don't
237   /// care about them for combiners.
238   static bool canMCOIOperandTypeBeARegister(StringRef MCOIType) {
239     // Assume OPERAND_GENERIC_0 through 5 can be registers. The other MCOI
240     // OperandTypes are either never used in gMIR, or not relevant (e.g.
241     // OPERAND_GENERIC_IMM, which is definitely never a register).
242     return MCOIType.drop_back(1).ends_with("OPERAND_GENERIC_");
243   }
244 
245   /// Finds the "MCOI::"" operand types for each operand of \p CGP.
246   ///
247   /// This is a bit trickier than it looks because we need to handle variadic
248   /// in/outs.
249   ///
250   /// e.g. for
251   ///   (G_BUILD_VECTOR $vec, $x, $y) ->
252   ///   [MCOI::OPERAND_GENERIC_0, MCOI::OPERAND_GENERIC_1,
253   ///    MCOI::OPERAND_GENERIC_1]
254   ///
255   /// For unknown types (which can happen in variadics where varargs types are
256   /// inconsistent), a unique name is given, e.g. "unknown_type_0".
257   static std::vector<std::string>
258   getMCOIOperandTypes(const CodeGenInstructionPattern &CGP);
259 
260   /// Adds the TypeEquivalenceClasses for \p P in \p OutTECs.
261   void getInstEqClasses(const InstructionPattern &P,
262                         TypeEquivalenceClasses &OutTECs) const;
263 
264   /// Calls `getInstEqClasses` on all patterns of the rule to produce the whole
265   /// rule's TypeEquivalenceClasses.
266   TypeEquivalenceClasses getRuleEqClasses() const;
267 
268   /// Tries to infer the type of the \p ImmOpIdx -th operand of \p IP using \p
269   /// TECs.
270   ///
271   /// This is achieved by trying to find a named operand in \p IP that shares
272   /// the same type as \p ImmOpIdx, and using \ref inferNamedOperandType on that
273   /// operand instead.
274   ///
275   /// \returns the inferred type or an empty PatternType if inference didn't
276   /// succeed.
277   PatternType inferImmediateType(const InstructionPattern &IP,
278                                  unsigned ImmOpIdx,
279                                  const TypeEquivalenceClasses &TECs) const;
280 
281   /// Looks inside \p TECs to infer \p OpName's type.
282   ///
283   /// \returns the inferred type or an empty PatternType if inference didn't
284   /// succeed.
285   PatternType inferNamedOperandType(const InstructionPattern &IP,
286                                     StringRef OpName,
287                                     const TypeEquivalenceClasses &TECs,
288                                     bool AllowSelf = false) const;
289 
290   const Record &RuleDef;
291   SmallVector<InstructionPattern *, 8> MatchPats;
292   SmallVector<InstructionPattern *, 8> ApplyPats;
293 
294   const OperandTable &MatchOpTable;
295 };
296 
297 bool CombineRuleOperandTypeChecker::processMatchPattern(InstructionPattern &P) {
298   MatchPats.push_back(&P);
299   return check(P, /*CheckTypeOf*/ [](const auto &) {
300     // GITypeOf in 'match' is currently always rejected by the
301     // CombineRuleBuilder after inference is done.
302     return true;
303   });
304 }
305 
306 bool CombineRuleOperandTypeChecker::processApplyPattern(InstructionPattern &P) {
307   ApplyPats.push_back(&P);
308   return check(P, /*CheckTypeOf*/ [&](const PatternType &Ty) {
309     // GITypeOf<"$x"> can only be used if "$x" is a matched operand.
310     const auto OpName = Ty.getTypeOfOpName();
311     if (MatchOpTable.lookup(OpName).Found)
312       return true;
313 
314     PrintError(RuleDef.getLoc(), "'" + OpName + "' ('" + Ty.str() +
315                                      "') does not refer to a matched operand!");
316     return false;
317   });
318 }
319 
320 void CombineRuleOperandTypeChecker::propagateAndInferTypes() {
321   /// First step here is to propagate types using the OperandTypeChecker. That
322   /// way we ensure all uses of a given register have consistent types.
323   propagateTypes();
324 
325   /// Build the TypeEquivalenceClasses for the whole rule.
326   const TypeEquivalenceClasses TECs = getRuleEqClasses();
327 
328   /// Look at the apply patterns and find operands that need to be
329   /// inferred. We then try to find an equivalence class that they're a part of
330   /// and select the best operand to use for the `GITypeOf` type. We prioritize
331   /// defs of matched instructions because those are guaranteed to be registers.
332   bool InferredAny = false;
333   for (auto *Pat : ApplyPats) {
334     for (unsigned K = 0; K < Pat->operands_size(); ++K) {
335       auto &Op = Pat->getOperand(K);
336 
337       // We only want to take a look at untyped defs or immediates.
338       if ((!Op.isDef() && !Op.hasImmValue()) || Op.getType())
339         continue;
340 
341       // Infer defs & named immediates.
342       if (Op.isDef() || Op.isNamedImmediate()) {
343         // Check it's not a redefinition of a matched operand.
344         // In such cases, inference is not necessary because we just copy
345         // operands and don't create temporary registers.
346         if (MatchOpTable.lookup(Op.getOperandName()).Found)
347           continue;
348 
349         // Inference is needed here, so try to do it.
350         if (PatternType Ty =
351                 inferNamedOperandType(*Pat, Op.getOperandName(), TECs)) {
352           if (DebugTypeInfer)
353             errs() << "INFER: " << Op.describe() << " -> " << Ty.str() << '\n';
354           Op.setType(Ty);
355           InferredAny = true;
356         }
357 
358         continue;
359       }
360 
361       // Infer immediates
362       if (Op.hasImmValue()) {
363         if (PatternType Ty = inferImmediateType(*Pat, K, TECs)) {
364           if (DebugTypeInfer)
365             errs() << "INFER: " << Op.describe() << " -> " << Ty.str() << '\n';
366           Op.setType(Ty);
367           InferredAny = true;
368         }
369         continue;
370       }
371     }
372   }
373 
374   // If we've inferred any types, we want to propagate them across the apply
375   // patterns. Type inference only adds GITypeOf types that point to Matched
376   // operands, so we definitely don't want to propagate types into the match
377   // patterns as well, otherwise bad things happen.
378   if (InferredAny) {
379     OperandTypeChecker OTC(RuleDef.getLoc());
380     for (auto *Pat : ApplyPats) {
381       if (!OTC.check(*Pat, [&](const auto &) { return true; }))
382         PrintFatalError(RuleDef.getLoc(),
383                         "OperandTypeChecker unexpectedly failed on '" +
384                             Pat->getName() + "' during Type Inference");
385     }
386     OTC.propagateTypes();
387 
388     if (DebugTypeInfer) {
389       errs() << "Apply patterns for rule " << RuleDef.getName()
390              << " after inference:\n";
391       for (auto *Pat : ApplyPats) {
392         errs() << "  ";
393         Pat->print(errs(), /*PrintName*/ true);
394         errs() << '\n';
395       }
396       errs() << '\n';
397     }
398   }
399 }
400 
401 PatternType CombineRuleOperandTypeChecker::inferImmediateType(
402     const InstructionPattern &IP, unsigned ImmOpIdx,
403     const TypeEquivalenceClasses &TECs) const {
404   // We can only infer CGPs.
405   const auto *CGP = dyn_cast<CodeGenInstructionPattern>(&IP);
406   if (!CGP)
407     return {};
408 
409   // For CGPs, we try to infer immediates by trying to infer another named
410   // operand that shares its type.
411   //
412   // e.g.
413   //    Pattern: G_BUILD_VECTOR $x, $y, 0
414   //    MCOIs:   [MCOI::OPERAND_GENERIC_0, MCOI::OPERAND_GENERIC_1,
415   //              MCOI::OPERAND_GENERIC_1]
416   //    $y has the same type as 0, so we can infer $y and get the type 0 should
417   //    have.
418 
419   // We infer immediates by looking for a named operand that shares the same
420   // MCOI type.
421   const auto MCOITypes = getMCOIOperandTypes(*CGP);
422   StringRef ImmOpTy = MCOITypes[ImmOpIdx];
423 
424   for (const auto &[Idx, Ty] : enumerate(MCOITypes)) {
425     if (Idx != ImmOpIdx && Ty == ImmOpTy) {
426       const auto &Op = IP.getOperand(Idx);
427       if (!Op.isNamedOperand())
428         continue;
429 
430       // Named operand with the same name, try to infer that.
431       if (PatternType InferTy = inferNamedOperandType(IP, Op.getOperandName(),
432                                                       TECs, /*AllowSelf=*/true))
433         return InferTy;
434     }
435   }
436 
437   return {};
438 }
439 
440 PatternType CombineRuleOperandTypeChecker::inferNamedOperandType(
441     const InstructionPattern &IP, StringRef OpName,
442     const TypeEquivalenceClasses &TECs, bool AllowSelf) const {
443   // This is the simplest possible case, we just need to find a TEC that
444   // contains OpName. Look at all operands in equivalence class and try to
445   // find a suitable one. If `AllowSelf` is true, the operand itself is also
446   // considered suitable.
447 
448   // Check for a def of a matched pattern. This is guaranteed to always
449   // be a register so we can blindly use that.
450   StringRef GoodOpName;
451   for (auto It = TECs.findLeader(OpName); It != TECs.member_end(); ++It) {
452     if (!AllowSelf && *It == OpName)
453       continue;
454 
455     const auto LookupRes = MatchOpTable.lookup(*It);
456     if (LookupRes.Def) // Favor defs
457       return PatternType::getTypeOf(*It);
458 
459     // Otherwise just save this in case we don't find any def.
460     if (GoodOpName.empty() && LookupRes.Found)
461       GoodOpName = *It;
462   }
463 
464   if (!GoodOpName.empty())
465     return PatternType::getTypeOf(GoodOpName);
466 
467   // No good operand found, give up.
468   return {};
469 }
470 
471 std::vector<std::string> CombineRuleOperandTypeChecker::getMCOIOperandTypes(
472     const CodeGenInstructionPattern &CGP) {
473   // FIXME?: Should we cache this? We call it twice when inferring immediates.
474 
475   static unsigned UnknownTypeIdx = 0;
476 
477   std::vector<std::string> OpTypes;
478   auto &CGI = CGP.getInst();
479   Record *VarArgsTy = CGI.TheDef->isSubClassOf("GenericInstruction")
480                           ? CGI.TheDef->getValueAsOptionalDef("variadicOpsType")
481                           : nullptr;
482   std::string VarArgsTyName =
483       VarArgsTy ? ("MCOI::" + VarArgsTy->getValueAsString("OperandType")).str()
484                 : ("unknown_type_" + Twine(UnknownTypeIdx++)).str();
485 
486   // First, handle defs.
487   for (unsigned K = 0; K < CGI.Operands.NumDefs; ++K)
488     OpTypes.push_back(CGI.Operands[K].OperandType);
489 
490   // Then, handle variadic defs if there are any.
491   if (CGP.hasVariadicDefs()) {
492     for (unsigned K = CGI.Operands.NumDefs; K < CGP.getNumInstDefs(); ++K)
493       OpTypes.push_back(VarArgsTyName);
494   }
495 
496   // If we had variadic defs, the op idx in the pattern won't match the op idx
497   // in the CGI anymore.
498   int CGIOpOffset = int(CGI.Operands.NumDefs) - CGP.getNumInstDefs();
499   assert(CGP.hasVariadicDefs() ? (CGIOpOffset <= 0) : (CGIOpOffset == 0));
500 
501   // Handle all remaining use operands, including variadic ones.
502   for (unsigned K = CGP.getNumInstDefs(); K < CGP.getNumInstOperands(); ++K) {
503     unsigned CGIOpIdx = K + CGIOpOffset;
504     if (CGIOpIdx >= CGI.Operands.size()) {
505       assert(CGP.isVariadic());
506       OpTypes.push_back(VarArgsTyName);
507     } else {
508       OpTypes.push_back(CGI.Operands[CGIOpIdx].OperandType);
509     }
510   }
511 
512   assert(OpTypes.size() == CGP.operands_size());
513   return OpTypes;
514 }
515 
516 void CombineRuleOperandTypeChecker::getInstEqClasses(
517     const InstructionPattern &P, TypeEquivalenceClasses &OutTECs) const {
518   // Determine the TypeEquivalenceClasses by:
519   //    - Getting the MCOI Operand Types.
520   //    - Creating a Map of MCOI Type -> [Operand Indexes]
521   //    - Iterating over the map, filtering types we don't like, and just adding
522   //      the array of Operand Indexes to \p OutTECs.
523 
524   // We can only do this on CodeGenInstructions. Other InstructionPatterns have
525   // no type inference information associated with them.
526   // TODO: Could we add some inference information to builtins at least? e.g.
527   // ReplaceReg should always replace with a reg of the same type, for instance.
528   // Though, those patterns are often used alone so it might not be worth the
529   // trouble to infer their types.
530   auto *CGP = dyn_cast<CodeGenInstructionPattern>(&P);
531   if (!CGP)
532     return;
533 
534   const auto MCOITypes = getMCOIOperandTypes(*CGP);
535   assert(MCOITypes.size() == P.operands_size());
536 
537   DenseMap<StringRef, std::vector<unsigned>> TyToOpIdx;
538   for (const auto &[Idx, Ty] : enumerate(MCOITypes))
539     TyToOpIdx[Ty].push_back(Idx);
540 
541   if (DebugTypeInfer)
542     errs() << "\tGroups for " << P.getName() << ":\t";
543 
544   for (const auto &[Ty, Idxs] : TyToOpIdx) {
545     if (!canMCOIOperandTypeBeARegister(Ty))
546       continue;
547 
548     if (DebugTypeInfer)
549       errs() << '[';
550     StringRef Sep = "";
551 
552     // We only collect named operands.
553     StringRef Leader;
554     for (unsigned Idx : Idxs) {
555       const auto &Op = P.getOperand(Idx);
556       if (!Op.isNamedOperand())
557         continue;
558 
559       const auto OpName = Op.getOperandName();
560       if (DebugTypeInfer) {
561         errs() << Sep << OpName;
562         Sep = ", ";
563       }
564 
565       if (Leader.empty())
566         OutTECs.insert((Leader = OpName));
567       else
568         OutTECs.unionSets(Leader, OpName);
569     }
570 
571     if (DebugTypeInfer)
572       errs() << "] ";
573   }
574 
575   if (DebugTypeInfer)
576     errs() << '\n';
577 }
578 
579 CombineRuleOperandTypeChecker::TypeEquivalenceClasses
580 CombineRuleOperandTypeChecker::getRuleEqClasses() const {
581   StringMap<unsigned> OpNameToEqClassIdx;
582   TypeEquivalenceClasses TECs;
583 
584   if (DebugTypeInfer)
585     errs() << "Rule Operand Type Equivalence Classes for " << RuleDef.getName()
586            << ":\n";
587 
588   for (const auto *Pat : MatchPats)
589     getInstEqClasses(*Pat, TECs);
590   for (const auto *Pat : ApplyPats)
591     getInstEqClasses(*Pat, TECs);
592 
593   if (DebugTypeInfer) {
594     errs() << "Final Type Equivalence Classes: ";
595     for (auto ClassIt = TECs.begin(); ClassIt != TECs.end(); ++ClassIt) {
596       // only print non-empty classes.
597       if (auto MembIt = TECs.member_begin(ClassIt);
598           MembIt != TECs.member_end()) {
599         errs() << '[';
600         StringRef Sep = "";
601         for (; MembIt != TECs.member_end(); ++MembIt) {
602           errs() << Sep << *MembIt;
603           Sep = ", ";
604         }
605         errs() << "] ";
606       }
607     }
608     errs() << '\n';
609   }
610 
611   return TECs;
612 }
613 
614 //===- CombineRuleBuilder -------------------------------------------------===//
615 
616 /// Parses combine rule and builds a small intermediate representation to tie
617 /// patterns together and emit RuleMatchers to match them. This may emit more
618 /// than one RuleMatcher, e.g. for `wip_match_opcode`.
619 ///
620 /// Memory management for `Pattern` objects is done through `std::unique_ptr`.
621 /// In most cases, there are two stages to a pattern's lifetime:
622 ///   - Creation in a `parse` function
623 ///     - The unique_ptr is stored in a variable, and may be destroyed if the
624 ///       pattern is found to be semantically invalid.
625 ///   - Ownership transfer into a `PatternMap`
626 ///     - Once a pattern is moved into either the map of Match or Apply
627 ///       patterns, it is known to be valid and it never moves back.
628 class CombineRuleBuilder {
629 public:
630   using PatternMap = MapVector<StringRef, std::unique_ptr<Pattern>>;
631   using PatternAlternatives = DenseMap<const Pattern *, unsigned>;
632 
633   CombineRuleBuilder(const CodeGenTarget &CGT,
634                      SubtargetFeatureInfoMap &SubtargetFeatures,
635                      Record &RuleDef, unsigned ID,
636                      std::vector<RuleMatcher> &OutRMs)
637       : CGT(CGT), SubtargetFeatures(SubtargetFeatures), RuleDef(RuleDef),
638         RuleID(ID), OutRMs(OutRMs) {}
639 
640   /// Parses all fields in the RuleDef record.
641   bool parseAll();
642 
643   /// Emits all RuleMatchers into the vector of RuleMatchers passed in the
644   /// constructor.
645   bool emitRuleMatchers();
646 
647   void print(raw_ostream &OS) const;
648   void dump() const { print(dbgs()); }
649 
650   /// Debug-only verification of invariants.
651 #ifndef NDEBUG
652   void verify() const;
653 #endif
654 
655 private:
656   const CodeGenInstruction &getGConstant() const {
657     return CGT.getInstruction(RuleDef.getRecords().getDef("G_CONSTANT"));
658   }
659 
660   void PrintError(Twine Msg) const { ::PrintError(&RuleDef, Msg); }
661   void PrintWarning(Twine Msg) const { ::PrintWarning(RuleDef.getLoc(), Msg); }
662   void PrintNote(Twine Msg) const { ::PrintNote(RuleDef.getLoc(), Msg); }
663 
664   void print(raw_ostream &OS, const PatternAlternatives &Alts) const;
665 
666   bool addApplyPattern(std::unique_ptr<Pattern> Pat);
667   bool addMatchPattern(std::unique_ptr<Pattern> Pat);
668 
669   /// Adds the expansions from \see MatchDatas to \p CE.
670   void declareAllMatchDatasExpansions(CodeExpansions &CE) const;
671 
672   /// Adds a matcher \p P to \p IM, expanding its code using \p CE.
673   /// Note that the predicate is added on the last InstructionMatcher.
674   ///
675   /// \p Alts is only used if DebugCXXPreds is enabled.
676   void addCXXPredicate(RuleMatcher &M, const CodeExpansions &CE,
677                        const CXXPattern &P, const PatternAlternatives &Alts);
678 
679   /// Adds an apply \p P to \p IM, expanding its code using \p CE.
680   void addCXXAction(RuleMatcher &M, const CodeExpansions &CE,
681                     const CXXPattern &P);
682 
683   bool hasOnlyCXXApplyPatterns() const;
684   bool hasEraseRoot() const;
685 
686   // Infer machine operand types and check their consistency.
687   bool typecheckPatterns();
688 
689   /// For all PatFragPatterns, add a new entry in PatternAlternatives for each
690   /// PatternList it contains. This is multiplicative, so if we have 2
691   /// PatFrags with 3 alternatives each, we get 2*3 permutations added to
692   /// PermutationsToEmit. The "MaxPermutations" field controls how many
693   /// permutations are allowed before an error is emitted and this function
694   /// returns false. This is a simple safeguard to prevent combination of
695   /// PatFrags from generating enormous amounts of rules.
696   bool buildPermutationsToEmit();
697 
698   /// Checks additional semantics of the Patterns.
699   bool checkSemantics();
700 
701   /// Creates a new RuleMatcher with some boilerplate
702   /// settings/actions/predicates, and and adds it to \p OutRMs.
703   /// \see addFeaturePredicates too.
704   ///
705   /// \param Alts Current set of alternatives, for debug comment.
706   /// \param AdditionalComment Comment string to be added to the
707   ///        `DebugCommentAction`.
708   RuleMatcher &addRuleMatcher(const PatternAlternatives &Alts,
709                               Twine AdditionalComment = "");
710   bool addFeaturePredicates(RuleMatcher &M);
711 
712   bool findRoots();
713   bool buildRuleOperandsTable();
714 
715   bool parseDefs(const DagInit &Def);
716   bool
717   parsePatternList(const DagInit &List,
718                    function_ref<bool(std::unique_ptr<Pattern>)> ParseAction,
719                    StringRef Operator, ArrayRef<SMLoc> DiagLoc,
720                    StringRef AnonPatNamePrefix) const;
721 
722   std::unique_ptr<Pattern> parseInstructionPattern(const Init &Arg,
723                                                    StringRef PatName) const;
724   std::unique_ptr<Pattern> parseWipMatchOpcodeMatcher(const Init &Arg,
725                                                       StringRef PatName) const;
726   bool parseInstructionPatternOperand(InstructionPattern &IP,
727                                       const Init *OpInit,
728                                       const StringInit *OpName) const;
729   bool parseInstructionPatternMIFlags(InstructionPattern &IP,
730                                       const DagInit *Op) const;
731   std::unique_ptr<PatFrag> parsePatFragImpl(const Record *Def) const;
732   bool parsePatFragParamList(
733       ArrayRef<SMLoc> DiagLoc, const DagInit &OpsList,
734       function_ref<bool(StringRef, PatFrag::ParamKind)> ParseAction) const;
735   const PatFrag *parsePatFrag(const Record *Def) const;
736 
737   bool emitMatchPattern(CodeExpansions &CE, const PatternAlternatives &Alts,
738                         const InstructionPattern &IP);
739   bool emitMatchPattern(CodeExpansions &CE, const PatternAlternatives &Alts,
740                         const AnyOpcodePattern &AOP);
741 
742   bool emitPatFragMatchPattern(CodeExpansions &CE,
743                                const PatternAlternatives &Alts, RuleMatcher &RM,
744                                InstructionMatcher *IM,
745                                const PatFragPattern &PFP,
746                                DenseSet<const Pattern *> &SeenPats);
747 
748   bool emitApplyPatterns(CodeExpansions &CE, RuleMatcher &M);
749 
750   // Recursively visits InstructionPatterns from P to build up the
751   // RuleMatcher actions.
752   bool emitInstructionApplyPattern(CodeExpansions &CE, RuleMatcher &M,
753                                    const InstructionPattern &P,
754                                    DenseSet<const Pattern *> &SeenPats,
755                                    StringMap<unsigned> &OperandToTempRegID);
756 
757   bool emitCodeGenInstructionApplyImmOperand(RuleMatcher &M,
758                                              BuildMIAction &DstMI,
759                                              const CodeGenInstructionPattern &P,
760                                              const InstructionOperand &O);
761 
762   bool emitBuiltinApplyPattern(CodeExpansions &CE, RuleMatcher &M,
763                                const BuiltinPattern &P,
764                                StringMap<unsigned> &OperandToTempRegID);
765 
766   // Recursively visits CodeGenInstructionPattern from P to build up the
767   // RuleMatcher/InstructionMatcher. May create new InstructionMatchers as
768   // needed.
769   using OperandMapperFnRef =
770       function_ref<InstructionOperand(const InstructionOperand &)>;
771   using OperandDefLookupFn =
772       function_ref<const InstructionPattern *(StringRef)>;
773   bool emitCodeGenInstructionMatchPattern(
774       CodeExpansions &CE, const PatternAlternatives &Alts, RuleMatcher &M,
775       InstructionMatcher &IM, const CodeGenInstructionPattern &P,
776       DenseSet<const Pattern *> &SeenPats, OperandDefLookupFn LookupOperandDef,
777       OperandMapperFnRef OperandMapper = [](const auto &O) { return O; });
778 
779   const CodeGenTarget &CGT;
780   SubtargetFeatureInfoMap &SubtargetFeatures;
781   Record &RuleDef;
782   const unsigned RuleID;
783   std::vector<RuleMatcher> &OutRMs;
784 
785   // For InstructionMatcher::addOperand
786   unsigned AllocatedTemporariesBaseID = 0;
787 
788   /// The root of the pattern.
789   StringRef RootName;
790 
791   /// These maps have ownership of the actual Pattern objects.
792   /// They both map a Pattern's name to the Pattern instance.
793   PatternMap MatchPats;
794   PatternMap ApplyPats;
795 
796   /// Operand tables to tie match/apply patterns together.
797   OperandTable MatchOpTable;
798   OperandTable ApplyOpTable;
799 
800   /// Set by findRoots.
801   Pattern *MatchRoot = nullptr;
802   SmallDenseSet<InstructionPattern *, 2> ApplyRoots;
803 
804   SmallVector<MatchDataInfo, 2> MatchDatas;
805   SmallVector<PatternAlternatives, 1> PermutationsToEmit;
806 
807   // print()/debug-only members.
808   mutable SmallPtrSet<const PatFrag *, 2> SeenPatFrags;
809 };
810 
811 bool CombineRuleBuilder::parseAll() {
812   auto StackTrace = PrettyStackTraceParse(RuleDef);
813 
814   if (!parseDefs(*RuleDef.getValueAsDag("Defs")))
815     return false;
816 
817   if (!parsePatternList(
818           *RuleDef.getValueAsDag("Match"),
819           [this](auto Pat) { return addMatchPattern(std::move(Pat)); }, "match",
820           RuleDef.getLoc(), (RuleDef.getName() + "_match").str()))
821     return false;
822 
823   if (!parsePatternList(
824           *RuleDef.getValueAsDag("Apply"),
825           [this](auto Pat) { return addApplyPattern(std::move(Pat)); }, "apply",
826           RuleDef.getLoc(), (RuleDef.getName() + "_apply").str()))
827     return false;
828 
829   if (!buildRuleOperandsTable() || !typecheckPatterns() || !findRoots() ||
830       !checkSemantics() || !buildPermutationsToEmit())
831     return false;
832   LLVM_DEBUG(verify());
833   return true;
834 }
835 
836 bool CombineRuleBuilder::emitRuleMatchers() {
837   auto StackTrace = PrettyStackTraceEmit(RuleDef);
838 
839   assert(MatchRoot);
840   CodeExpansions CE;
841   declareAllMatchDatasExpansions(CE);
842 
843   assert(!PermutationsToEmit.empty());
844   for (const auto &Alts : PermutationsToEmit) {
845     switch (MatchRoot->getKind()) {
846     case Pattern::K_AnyOpcode: {
847       if (!emitMatchPattern(CE, Alts, *cast<AnyOpcodePattern>(MatchRoot)))
848         return false;
849       break;
850     }
851     case Pattern::K_PatFrag:
852     case Pattern::K_Builtin:
853     case Pattern::K_CodeGenInstruction:
854       if (!emitMatchPattern(CE, Alts, *cast<InstructionPattern>(MatchRoot)))
855         return false;
856       break;
857     case Pattern::K_CXX:
858       PrintError("C++ code cannot be the root of a rule!");
859       return false;
860     default:
861       llvm_unreachable("unknown pattern kind!");
862     }
863   }
864 
865   return true;
866 }
867 
868 void CombineRuleBuilder::print(raw_ostream &OS) const {
869   OS << "(CombineRule name:" << RuleDef.getName() << " id:" << RuleID
870      << " root:" << RootName << '\n';
871 
872   if (!MatchDatas.empty()) {
873     OS << "  (MatchDatas\n";
874     for (const auto &MD : MatchDatas) {
875       OS << "    ";
876       MD.print(OS);
877       OS << '\n';
878     }
879     OS << "  )\n";
880   }
881 
882   if (!SeenPatFrags.empty()) {
883     OS << "  (PatFrags\n";
884     for (const auto *PF : SeenPatFrags) {
885       PF->print(OS, /*Indent=*/"    ");
886       OS << '\n';
887     }
888     OS << "  )\n";
889   }
890 
891   const auto DumpPats = [&](StringRef Name, const PatternMap &Pats) {
892     OS << "  (" << Name << " ";
893     if (Pats.empty()) {
894       OS << "<empty>)\n";
895       return;
896     }
897 
898     OS << '\n';
899     for (const auto &[Name, Pat] : Pats) {
900       OS << "    ";
901       if (Pat.get() == MatchRoot)
902         OS << "<match_root>";
903       if (isa<InstructionPattern>(Pat.get()) &&
904           ApplyRoots.contains(cast<InstructionPattern>(Pat.get())))
905         OS << "<apply_root>";
906       OS << Name << ":";
907       Pat->print(OS, /*PrintName=*/false);
908       OS << '\n';
909     }
910     OS << "  )\n";
911   };
912 
913   DumpPats("MatchPats", MatchPats);
914   DumpPats("ApplyPats", ApplyPats);
915 
916   MatchOpTable.print(OS, "MatchPats", /*Indent*/ "  ");
917   ApplyOpTable.print(OS, "ApplyPats", /*Indent*/ "  ");
918 
919   if (PermutationsToEmit.size() > 1) {
920     OS << "  (PermutationsToEmit\n";
921     for (const auto &Perm : PermutationsToEmit) {
922       OS << "    ";
923       print(OS, Perm);
924       OS << ",\n";
925     }
926     OS << "  )\n";
927   }
928 
929   OS << ")\n";
930 }
931 
932 #ifndef NDEBUG
933 void CombineRuleBuilder::verify() const {
934   const auto VerifyPats = [&](const PatternMap &Pats) {
935     for (const auto &[Name, Pat] : Pats) {
936       if (!Pat)
937         PrintFatalError("null pattern in pattern map!");
938 
939       if (Name != Pat->getName()) {
940         Pat->dump();
941         PrintFatalError("Pattern name mismatch! Map name: " + Name +
942                         ", Pat name: " + Pat->getName());
943       }
944 
945       // Sanity check: the map should point to the same data as the Pattern.
946       // Both strings are allocated in the pool using insertStrRef.
947       if (Name.data() != Pat->getName().data()) {
948         dbgs() << "Map StringRef: '" << Name << "' @ "
949                << (const void *)Name.data() << '\n';
950         dbgs() << "Pat String: '" << Pat->getName() << "' @ "
951                << (const void *)Pat->getName().data() << '\n';
952         PrintFatalError("StringRef stored in the PatternMap is not referencing "
953                         "the same string as its Pattern!");
954       }
955     }
956   };
957 
958   VerifyPats(MatchPats);
959   VerifyPats(ApplyPats);
960 
961   // Check there are no wip_match_opcode patterns in the "apply" patterns.
962   if (any_of(ApplyPats,
963              [&](auto &E) { return isa<AnyOpcodePattern>(E.second.get()); })) {
964     dump();
965     PrintFatalError(
966         "illegal wip_match_opcode pattern in the 'apply' patterns!");
967   }
968 
969   // Check there are no nullptrs in ApplyRoots.
970   if (ApplyRoots.contains(nullptr)) {
971     PrintFatalError(
972         "CombineRuleBuilder's ApplyRoots set contains a null pointer!");
973   }
974 }
975 #endif
976 
977 void CombineRuleBuilder::print(raw_ostream &OS,
978                                const PatternAlternatives &Alts) const {
979   SmallVector<std::string, 1> Strings(
980       map_range(Alts, [](const auto &PatAndPerm) {
981         return PatAndPerm.first->getName().str() + "[" +
982                to_string(PatAndPerm.second) + "]";
983       }));
984   // Sort so output is deterministic for tests. Otherwise it's sorted by pointer
985   // values.
986   sort(Strings);
987   OS << "[" << join(Strings, ", ") << "]";
988 }
989 
990 bool CombineRuleBuilder::addApplyPattern(std::unique_ptr<Pattern> Pat) {
991   StringRef Name = Pat->getName();
992   if (ApplyPats.contains(Name)) {
993     PrintError("'" + Name + "' apply pattern defined more than once!");
994     return false;
995   }
996 
997   if (isa<AnyOpcodePattern>(Pat.get())) {
998     PrintError("'" + Name +
999                "': wip_match_opcode is not supported in apply patterns");
1000     return false;
1001   }
1002 
1003   if (isa<PatFragPattern>(Pat.get())) {
1004     PrintError("'" + Name + "': using " + PatFrag::ClassName +
1005                " is not supported in apply patterns");
1006     return false;
1007   }
1008 
1009   if (auto *CXXPat = dyn_cast<CXXPattern>(Pat.get()))
1010     CXXPat->setIsApply();
1011 
1012   ApplyPats[Name] = std::move(Pat);
1013   return true;
1014 }
1015 
1016 bool CombineRuleBuilder::addMatchPattern(std::unique_ptr<Pattern> Pat) {
1017   StringRef Name = Pat->getName();
1018   if (MatchPats.contains(Name)) {
1019     PrintError("'" + Name + "' match pattern defined more than once!");
1020     return false;
1021   }
1022 
1023   // For now, none of the builtins can appear in 'match'.
1024   if (const auto *BP = dyn_cast<BuiltinPattern>(Pat.get())) {
1025     PrintError("'" + BP->getInstName() +
1026                "' cannot be used in a 'match' pattern");
1027     return false;
1028   }
1029 
1030   MatchPats[Name] = std::move(Pat);
1031   return true;
1032 }
1033 
1034 void CombineRuleBuilder::declareAllMatchDatasExpansions(
1035     CodeExpansions &CE) const {
1036   for (const auto &MD : MatchDatas)
1037     CE.declare(MD.getPatternSymbol(), MD.getQualifiedVariableName());
1038 }
1039 
1040 void CombineRuleBuilder::addCXXPredicate(RuleMatcher &M,
1041                                          const CodeExpansions &CE,
1042                                          const CXXPattern &P,
1043                                          const PatternAlternatives &Alts) {
1044   // FIXME: Hack so C++ code is executed last. May not work for more complex
1045   // patterns.
1046   auto &IM = *std::prev(M.insnmatchers().end());
1047   auto Loc = RuleDef.getLoc();
1048   const auto AddComment = [&](raw_ostream &OS) {
1049     OS << "// Pattern Alternatives: ";
1050     print(OS, Alts);
1051     OS << '\n';
1052   };
1053   const auto &ExpandedCode =
1054       DebugCXXPreds ? P.expandCode(CE, Loc, AddComment) : P.expandCode(CE, Loc);
1055   IM->addPredicate<GenericInstructionPredicateMatcher>(
1056       ExpandedCode.getEnumNameWithPrefix(CXXPredPrefix));
1057 }
1058 
1059 void CombineRuleBuilder::addCXXAction(RuleMatcher &M, const CodeExpansions &CE,
1060                                       const CXXPattern &P) {
1061   const auto &ExpandedCode = P.expandCode(CE, RuleDef.getLoc());
1062   M.addAction<CustomCXXAction>(
1063       ExpandedCode.getEnumNameWithPrefix(CXXApplyPrefix));
1064 }
1065 
1066 bool CombineRuleBuilder::hasOnlyCXXApplyPatterns() const {
1067   return all_of(ApplyPats, [&](auto &Entry) {
1068     return isa<CXXPattern>(Entry.second.get());
1069   });
1070 }
1071 
1072 bool CombineRuleBuilder::hasEraseRoot() const {
1073   return any_of(ApplyPats, [&](auto &Entry) {
1074     if (const auto *BP = dyn_cast<BuiltinPattern>(Entry.second.get()))
1075       return BP->getBuiltinKind() == BI_EraseRoot;
1076     return false;
1077   });
1078 }
1079 
1080 bool CombineRuleBuilder::typecheckPatterns() {
1081   CombineRuleOperandTypeChecker OTC(RuleDef, MatchOpTable);
1082 
1083   for (auto &Pat : values(MatchPats)) {
1084     if (auto *IP = dyn_cast<InstructionPattern>(Pat.get())) {
1085       if (!OTC.processMatchPattern(*IP))
1086         return false;
1087     }
1088   }
1089 
1090   for (auto &Pat : values(ApplyPats)) {
1091     if (auto *IP = dyn_cast<InstructionPattern>(Pat.get())) {
1092       if (!OTC.processApplyPattern(*IP))
1093         return false;
1094     }
1095   }
1096 
1097   OTC.propagateAndInferTypes();
1098 
1099   // Always check this after in case inference adds some special types to the
1100   // match patterns.
1101   for (auto &Pat : values(MatchPats)) {
1102     if (auto *IP = dyn_cast<InstructionPattern>(Pat.get())) {
1103       if (IP->diagnoseAllSpecialTypes(
1104               RuleDef.getLoc(), PatternType::SpecialTyClassName +
1105                                     " is not supported in 'match' patterns")) {
1106         return false;
1107       }
1108     }
1109   }
1110   return true;
1111 }
1112 
1113 bool CombineRuleBuilder::buildPermutationsToEmit() {
1114   PermutationsToEmit.clear();
1115 
1116   // Start with one empty set of alternatives.
1117   PermutationsToEmit.emplace_back();
1118   for (const auto &Pat : values(MatchPats)) {
1119     unsigned NumAlts = 0;
1120     // Note: technically, AnyOpcodePattern also needs permutations, but:
1121     //    - We only allow a single one of them in the root.
1122     //    - They cannot be mixed with any other pattern other than C++ code.
1123     // So we don't really need to take them into account here. We could, but
1124     // that pattern is a hack anyway and the less it's involved, the better.
1125     if (const auto *PFP = dyn_cast<PatFragPattern>(Pat.get()))
1126       NumAlts = PFP->getPatFrag().num_alternatives();
1127     else
1128       continue;
1129 
1130     // For each pattern that needs permutations, multiply the current set of
1131     // alternatives.
1132     auto CurPerms = PermutationsToEmit;
1133     PermutationsToEmit.clear();
1134 
1135     for (const auto &Perm : CurPerms) {
1136       assert(!Perm.count(Pat.get()) && "Pattern already emitted?");
1137       for (unsigned K = 0; K < NumAlts; ++K) {
1138         PatternAlternatives NewPerm = Perm;
1139         NewPerm[Pat.get()] = K;
1140         PermutationsToEmit.emplace_back(std::move(NewPerm));
1141       }
1142     }
1143   }
1144 
1145   if (int64_t MaxPerms = RuleDef.getValueAsInt("MaxPermutations");
1146       MaxPerms > 0) {
1147     if ((int64_t)PermutationsToEmit.size() > MaxPerms) {
1148       PrintError("cannot emit rule '" + RuleDef.getName() + "'; " +
1149                  Twine(PermutationsToEmit.size()) +
1150                  " permutations would be emitted, but the max is " +
1151                  Twine(MaxPerms));
1152       return false;
1153     }
1154   }
1155 
1156   // Ensure we always have a single empty entry, it simplifies the emission
1157   // logic so it doesn't need to handle the case where there are no perms.
1158   if (PermutationsToEmit.empty()) {
1159     PermutationsToEmit.emplace_back();
1160     return true;
1161   }
1162 
1163   return true;
1164 }
1165 
1166 bool CombineRuleBuilder::checkSemantics() {
1167   assert(MatchRoot && "Cannot call this before findRoots()");
1168 
1169   bool UsesWipMatchOpcode = false;
1170   for (const auto &Match : MatchPats) {
1171     const auto *Pat = Match.second.get();
1172 
1173     if (const auto *CXXPat = dyn_cast<CXXPattern>(Pat)) {
1174       if (!CXXPat->getRawCode().contains("return "))
1175         PrintWarning("'match' C++ code does not seem to return!");
1176       continue;
1177     }
1178 
1179     // MIFlags in match cannot use the following syntax: (MIFlags $mi)
1180     if (const auto *CGP = dyn_cast<CodeGenInstructionPattern>(Pat)) {
1181       if (auto *FI = CGP->getMIFlagsInfo()) {
1182         if (!FI->copy_flags().empty()) {
1183           PrintError(
1184               "'match' patterns cannot refer to flags from other instructions");
1185           PrintNote("MIFlags in '" + CGP->getName() +
1186                     "' refer to: " + join(FI->copy_flags(), ", "));
1187           return false;
1188         }
1189       }
1190     }
1191 
1192     const auto *AOP = dyn_cast<AnyOpcodePattern>(Pat);
1193     if (!AOP)
1194       continue;
1195 
1196     if (UsesWipMatchOpcode) {
1197       PrintError("wip_opcode_match can only be present once");
1198       return false;
1199     }
1200 
1201     UsesWipMatchOpcode = true;
1202   }
1203 
1204   for (const auto &Apply : ApplyPats) {
1205     assert(Apply.second.get());
1206     const auto *IP = dyn_cast<InstructionPattern>(Apply.second.get());
1207     if (!IP)
1208       continue;
1209 
1210     if (UsesWipMatchOpcode) {
1211       PrintError("cannot use wip_match_opcode in combination with apply "
1212                  "instruction patterns!");
1213       return false;
1214     }
1215 
1216     // Check that the insts mentioned in copy_flags exist.
1217     if (const auto *CGP = dyn_cast<CodeGenInstructionPattern>(IP)) {
1218       if (auto *FI = CGP->getMIFlagsInfo()) {
1219         for (auto InstName : FI->copy_flags()) {
1220           auto It = MatchPats.find(InstName);
1221           if (It == MatchPats.end()) {
1222             PrintError("unknown instruction '$" + InstName +
1223                        "' referenced in MIFlags of '" + CGP->getName() + "'");
1224             return false;
1225           }
1226 
1227           if (!isa<CodeGenInstructionPattern>(It->second.get())) {
1228             PrintError(
1229                 "'$" + InstName +
1230                 "' does not refer to a CodeGenInstruction in MIFlags of '" +
1231                 CGP->getName() + "'");
1232             return false;
1233           }
1234         }
1235       }
1236     }
1237 
1238     const auto *BIP = dyn_cast<BuiltinPattern>(IP);
1239     if (!BIP)
1240       continue;
1241     StringRef Name = BIP->getInstName();
1242 
1243     // (GIEraseInst) has to be the only apply pattern, or it can not be used at
1244     // all. The root cannot have any defs either.
1245     switch (BIP->getBuiltinKind()) {
1246     case BI_EraseRoot: {
1247       if (ApplyPats.size() > 1) {
1248         PrintError(Name + " must be the only 'apply' pattern");
1249         return false;
1250       }
1251 
1252       const auto *IRoot = dyn_cast<CodeGenInstructionPattern>(MatchRoot);
1253       if (!IRoot) {
1254         PrintError(Name +
1255                    " can only be used if the root is a CodeGenInstruction");
1256         return false;
1257       }
1258 
1259       if (IRoot->getNumInstDefs() != 0) {
1260         PrintError(Name + " can only be used if on roots that do "
1261                           "not have any output operand");
1262         PrintNote("'" + IRoot->getInstName() + "' has " +
1263                   Twine(IRoot->getNumInstDefs()) + " output operands");
1264         return false;
1265       }
1266       break;
1267     }
1268     case BI_ReplaceReg: {
1269       // (GIReplaceReg can only be used on the root instruction)
1270       // TODO: When we allow rewriting non-root instructions, also allow this.
1271       StringRef OldRegName = BIP->getOperand(0).getOperandName();
1272       auto *Def = MatchOpTable.getDef(OldRegName);
1273       if (!Def) {
1274         PrintError(Name + " cannot find a matched pattern that defines '" +
1275                    OldRegName + "'");
1276         return false;
1277       }
1278       if (MatchOpTable.getDef(OldRegName) != MatchRoot) {
1279         PrintError(Name + " cannot replace '" + OldRegName +
1280                    "': this builtin can only replace a register defined by the "
1281                    "match root");
1282         return false;
1283       }
1284       break;
1285     }
1286     }
1287   }
1288 
1289   return true;
1290 }
1291 
1292 RuleMatcher &CombineRuleBuilder::addRuleMatcher(const PatternAlternatives &Alts,
1293                                                 Twine AdditionalComment) {
1294   auto &RM = OutRMs.emplace_back(RuleDef.getLoc());
1295   addFeaturePredicates(RM);
1296   RM.setPermanentGISelFlags(GISF_IgnoreCopies);
1297   RM.addRequiredSimplePredicate(getIsEnabledPredicateEnumName(RuleID));
1298 
1299   std::string Comment;
1300   raw_string_ostream CommentOS(Comment);
1301   CommentOS << "Combiner Rule #" << RuleID << ": " << RuleDef.getName();
1302   if (!Alts.empty()) {
1303     CommentOS << " @ ";
1304     print(CommentOS, Alts);
1305   }
1306   if (!AdditionalComment.isTriviallyEmpty())
1307     CommentOS << "; " << AdditionalComment;
1308   RM.addAction<DebugCommentAction>(Comment);
1309   return RM;
1310 }
1311 
1312 bool CombineRuleBuilder::addFeaturePredicates(RuleMatcher &M) {
1313   if (!RuleDef.getValue("Predicates"))
1314     return true;
1315 
1316   ListInit *Preds = RuleDef.getValueAsListInit("Predicates");
1317   for (Init *PI : Preds->getValues()) {
1318     DefInit *Pred = dyn_cast<DefInit>(PI);
1319     if (!Pred)
1320       continue;
1321 
1322     Record *Def = Pred->getDef();
1323     if (!Def->isSubClassOf("Predicate")) {
1324       ::PrintError(Def, "Unknown 'Predicate' Type");
1325       return false;
1326     }
1327 
1328     if (Def->getValueAsString("CondString").empty())
1329       continue;
1330 
1331     if (SubtargetFeatures.count(Def) == 0) {
1332       SubtargetFeatures.emplace(
1333           Def, SubtargetFeatureInfo(Def, SubtargetFeatures.size()));
1334     }
1335 
1336     M.addRequiredFeature(Def);
1337   }
1338 
1339   return true;
1340 }
1341 
1342 bool CombineRuleBuilder::findRoots() {
1343   const auto Finish = [&]() {
1344     assert(MatchRoot);
1345 
1346     if (hasOnlyCXXApplyPatterns() || hasEraseRoot())
1347       return true;
1348 
1349     auto *IPRoot = dyn_cast<InstructionPattern>(MatchRoot);
1350     if (!IPRoot)
1351       return true;
1352 
1353     if (IPRoot->getNumInstDefs() == 0) {
1354       // No defs to work with -> find the root using the pattern name.
1355       auto It = ApplyPats.find(RootName);
1356       if (It == ApplyPats.end()) {
1357         PrintError("Cannot find root '" + RootName + "' in apply patterns!");
1358         return false;
1359       }
1360 
1361       auto *ApplyRoot = dyn_cast<InstructionPattern>(It->second.get());
1362       if (!ApplyRoot) {
1363         PrintError("apply pattern root '" + RootName +
1364                    "' must be an instruction pattern");
1365         return false;
1366       }
1367 
1368       ApplyRoots.insert(ApplyRoot);
1369       return true;
1370     }
1371 
1372     // Collect all redefinitions of the MatchRoot's defs and put them in
1373     // ApplyRoots.
1374     const auto DefsNeeded = IPRoot->getApplyDefsNeeded();
1375     for (auto &Op : DefsNeeded) {
1376       assert(Op.isDef() && Op.isNamedOperand());
1377       StringRef Name = Op.getOperandName();
1378 
1379       auto *ApplyRedef = ApplyOpTable.getDef(Name);
1380       if (!ApplyRedef) {
1381         PrintError("'" + Name + "' must be redefined in the 'apply' pattern");
1382         return false;
1383       }
1384 
1385       ApplyRoots.insert((InstructionPattern *)ApplyRedef);
1386     }
1387 
1388     if (auto It = ApplyPats.find(RootName); It != ApplyPats.end()) {
1389       if (find(ApplyRoots, It->second.get()) == ApplyRoots.end()) {
1390         PrintError("apply pattern '" + RootName +
1391                    "' is supposed to be a root but it does not redefine any of "
1392                    "the defs of the match root");
1393         return false;
1394       }
1395     }
1396 
1397     return true;
1398   };
1399 
1400   // Look by pattern name, e.g.
1401   //    (G_FNEG $x, $y):$root
1402   if (auto MatchPatIt = MatchPats.find(RootName);
1403       MatchPatIt != MatchPats.end()) {
1404     MatchRoot = MatchPatIt->second.get();
1405     return Finish();
1406   }
1407 
1408   // Look by def:
1409   //    (G_FNEG $root, $y)
1410   auto LookupRes = MatchOpTable.lookup(RootName);
1411   if (!LookupRes.Found) {
1412     PrintError("Cannot find root '" + RootName + "' in match patterns!");
1413     return false;
1414   }
1415 
1416   MatchRoot = LookupRes.Def;
1417   if (!MatchRoot) {
1418     PrintError("Cannot use live-in operand '" + RootName +
1419                "' as match pattern root!");
1420     return false;
1421   }
1422 
1423   return Finish();
1424 }
1425 
1426 bool CombineRuleBuilder::buildRuleOperandsTable() {
1427   const auto DiagnoseRedefMatch = [&](StringRef OpName) {
1428     PrintError("Operand '" + OpName +
1429                "' is defined multiple times in the 'match' patterns");
1430   };
1431 
1432   const auto DiagnoseRedefApply = [&](StringRef OpName) {
1433     PrintError("Operand '" + OpName +
1434                "' is defined multiple times in the 'apply' patterns");
1435   };
1436 
1437   for (auto &Pat : values(MatchPats)) {
1438     auto *IP = dyn_cast<InstructionPattern>(Pat.get());
1439     if (IP && !MatchOpTable.addPattern(IP, DiagnoseRedefMatch))
1440       return false;
1441   }
1442 
1443   for (auto &Pat : values(ApplyPats)) {
1444     auto *IP = dyn_cast<InstructionPattern>(Pat.get());
1445     if (IP && !ApplyOpTable.addPattern(IP, DiagnoseRedefApply))
1446       return false;
1447   }
1448 
1449   return true;
1450 }
1451 
1452 bool CombineRuleBuilder::parseDefs(const DagInit &Def) {
1453   if (Def.getOperatorAsDef(RuleDef.getLoc())->getName() != "defs") {
1454     PrintError("Expected defs operator");
1455     return false;
1456   }
1457 
1458   SmallVector<StringRef> Roots;
1459   for (unsigned I = 0, E = Def.getNumArgs(); I < E; ++I) {
1460     if (isSpecificDef(*Def.getArg(I), "root")) {
1461       Roots.emplace_back(Def.getArgNameStr(I));
1462       continue;
1463     }
1464 
1465     // Subclasses of GIDefMatchData should declare that this rule needs to pass
1466     // data from the match stage to the apply stage, and ensure that the
1467     // generated matcher has a suitable variable for it to do so.
1468     if (Record *MatchDataRec =
1469             getDefOfSubClass(*Def.getArg(I), "GIDefMatchData")) {
1470       MatchDatas.emplace_back(Def.getArgNameStr(I),
1471                               MatchDataRec->getValueAsString("Type"));
1472       continue;
1473     }
1474 
1475     // Otherwise emit an appropriate error message.
1476     if (getDefOfSubClass(*Def.getArg(I), "GIDefKind"))
1477       PrintError("This GIDefKind not implemented in tablegen");
1478     else if (getDefOfSubClass(*Def.getArg(I), "GIDefKindWithArgs"))
1479       PrintError("This GIDefKindWithArgs not implemented in tablegen");
1480     else
1481       PrintError("Expected a subclass of GIDefKind or a sub-dag whose "
1482                  "operator is of type GIDefKindWithArgs");
1483     return false;
1484   }
1485 
1486   if (Roots.size() != 1) {
1487     PrintError("Combine rules must have exactly one root");
1488     return false;
1489   }
1490 
1491   RootName = Roots.front();
1492 
1493   // Assign variables to all MatchDatas.
1494   AssignMatchDataVariables(MatchDatas);
1495   return true;
1496 }
1497 
1498 bool CombineRuleBuilder::parsePatternList(
1499     const DagInit &List,
1500     function_ref<bool(std::unique_ptr<Pattern>)> ParseAction,
1501     StringRef Operator, ArrayRef<SMLoc> DiagLoc,
1502     StringRef AnonPatNamePrefix) const {
1503   if (List.getOperatorAsDef(RuleDef.getLoc())->getName() != Operator) {
1504     ::PrintError(DiagLoc, "Expected " + Operator + " operator");
1505     return false;
1506   }
1507 
1508   if (List.getNumArgs() == 0) {
1509     ::PrintError(DiagLoc, Operator + " pattern list is empty");
1510     return false;
1511   }
1512 
1513   // The match section consists of a list of matchers and predicates. Parse each
1514   // one and add the equivalent GIMatchDag nodes, predicates, and edges.
1515   for (unsigned I = 0; I < List.getNumArgs(); ++I) {
1516     Init *Arg = List.getArg(I);
1517     std::string Name = List.getArgName(I)
1518                            ? List.getArgName(I)->getValue().str()
1519                            : ("__" + AnonPatNamePrefix + "_" + Twine(I)).str();
1520 
1521     if (auto Pat = parseInstructionPattern(*Arg, Name)) {
1522       if (!ParseAction(std::move(Pat)))
1523         return false;
1524       continue;
1525     }
1526 
1527     if (auto Pat = parseWipMatchOpcodeMatcher(*Arg, Name)) {
1528       if (!ParseAction(std::move(Pat)))
1529         return false;
1530       continue;
1531     }
1532 
1533     // Parse arbitrary C++ code
1534     if (const auto *StringI = dyn_cast<StringInit>(Arg)) {
1535       auto CXXPat = std::make_unique<CXXPattern>(*StringI, insertStrRef(Name));
1536       if (!ParseAction(std::move(CXXPat)))
1537         return false;
1538       continue;
1539     }
1540 
1541     ::PrintError(DiagLoc,
1542                  "Failed to parse pattern: '" + Arg->getAsString() + "'");
1543     return false;
1544   }
1545 
1546   return true;
1547 }
1548 
1549 std::unique_ptr<Pattern>
1550 CombineRuleBuilder::parseInstructionPattern(const Init &Arg,
1551                                             StringRef Name) const {
1552   const DagInit *DagPat = dyn_cast<DagInit>(&Arg);
1553   if (!DagPat)
1554     return nullptr;
1555 
1556   std::unique_ptr<InstructionPattern> Pat;
1557   if (const DagInit *IP = getDagWithOperatorOfSubClass(Arg, "Instruction")) {
1558     auto &Instr = CGT.getInstruction(IP->getOperatorAsDef(RuleDef.getLoc()));
1559     Pat =
1560         std::make_unique<CodeGenInstructionPattern>(Instr, insertStrRef(Name));
1561   } else if (const DagInit *PFP =
1562                  getDagWithOperatorOfSubClass(Arg, PatFrag::ClassName)) {
1563     const Record *Def = PFP->getOperatorAsDef(RuleDef.getLoc());
1564     const PatFrag *PF = parsePatFrag(Def);
1565     if (!PF)
1566       return nullptr; // Already diagnosed by parsePatFrag
1567     Pat = std::make_unique<PatFragPattern>(*PF, insertStrRef(Name));
1568   } else if (const DagInit *BP =
1569                  getDagWithOperatorOfSubClass(Arg, BuiltinPattern::ClassName)) {
1570     Pat = std::make_unique<BuiltinPattern>(
1571         *BP->getOperatorAsDef(RuleDef.getLoc()), insertStrRef(Name));
1572   } else {
1573     return nullptr;
1574   }
1575 
1576   for (unsigned K = 0; K < DagPat->getNumArgs(); ++K) {
1577     Init *Arg = DagPat->getArg(K);
1578     if (auto *DagArg = getDagWithSpecificOperator(*Arg, "MIFlags")) {
1579       if (!parseInstructionPatternMIFlags(*Pat, DagArg))
1580         return nullptr;
1581       continue;
1582     }
1583 
1584     if (!parseInstructionPatternOperand(*Pat, Arg, DagPat->getArgName(K)))
1585       return nullptr;
1586   }
1587 
1588   if (!Pat->checkSemantics(RuleDef.getLoc()))
1589     return nullptr;
1590 
1591   return std::move(Pat);
1592 }
1593 
1594 std::unique_ptr<Pattern>
1595 CombineRuleBuilder::parseWipMatchOpcodeMatcher(const Init &Arg,
1596                                                StringRef Name) const {
1597   const DagInit *Matcher = getDagWithSpecificOperator(Arg, "wip_match_opcode");
1598   if (!Matcher)
1599     return nullptr;
1600 
1601   if (Matcher->getNumArgs() == 0) {
1602     PrintError("Empty wip_match_opcode");
1603     return nullptr;
1604   }
1605 
1606   // Each argument is an opcode that can match.
1607   auto Result = std::make_unique<AnyOpcodePattern>(insertStrRef(Name));
1608   for (const auto &Arg : Matcher->getArgs()) {
1609     Record *OpcodeDef = getDefOfSubClass(*Arg, "Instruction");
1610     if (OpcodeDef) {
1611       Result->addOpcode(&CGT.getInstruction(OpcodeDef));
1612       continue;
1613     }
1614 
1615     PrintError("Arguments to wip_match_opcode must be instructions");
1616     return nullptr;
1617   }
1618 
1619   return std::move(Result);
1620 }
1621 
1622 bool CombineRuleBuilder::parseInstructionPatternOperand(
1623     InstructionPattern &IP, const Init *OpInit,
1624     const StringInit *OpName) const {
1625   const auto ParseErr = [&]() {
1626     PrintError("cannot parse operand '" + OpInit->getAsUnquotedString() + "' ");
1627     if (OpName)
1628       PrintNote("operand name is '" + OpName->getAsUnquotedString() + "'");
1629     return false;
1630   };
1631 
1632   // untyped immediate, e.g. 0
1633   if (const auto *IntImm = dyn_cast<IntInit>(OpInit)) {
1634     std::string Name = OpName ? OpName->getAsUnquotedString() : "";
1635     IP.addOperand(IntImm->getValue(), insertStrRef(Name), PatternType());
1636     return true;
1637   }
1638 
1639   // typed immediate, e.g. (i32 0)
1640   if (const auto *DagOp = dyn_cast<DagInit>(OpInit)) {
1641     if (DagOp->getNumArgs() != 1)
1642       return ParseErr();
1643 
1644     const Record *TyDef = DagOp->getOperatorAsDef(RuleDef.getLoc());
1645     auto ImmTy = PatternType::get(RuleDef.getLoc(), TyDef,
1646                                   "cannot parse immediate '" +
1647                                       DagOp->getAsUnquotedString() + "'");
1648     if (!ImmTy)
1649       return false;
1650 
1651     if (!IP.hasAllDefs()) {
1652       PrintError("out operand of '" + IP.getInstName() +
1653                  "' cannot be an immediate");
1654       return false;
1655     }
1656 
1657     const auto *Val = dyn_cast<IntInit>(DagOp->getArg(0));
1658     if (!Val)
1659       return ParseErr();
1660 
1661     std::string Name = OpName ? OpName->getAsUnquotedString() : "";
1662     IP.addOperand(Val->getValue(), insertStrRef(Name), *ImmTy);
1663     return true;
1664   }
1665 
1666   // Typed operand e.g. $x/$z in (G_FNEG $x, $z)
1667   if (auto *DefI = dyn_cast<DefInit>(OpInit)) {
1668     if (!OpName) {
1669       PrintError("expected an operand name after '" + OpInit->getAsString() +
1670                  "'");
1671       return false;
1672     }
1673     const Record *Def = DefI->getDef();
1674     auto Ty =
1675         PatternType::get(RuleDef.getLoc(), Def, "cannot parse operand type");
1676     if (!Ty)
1677       return false;
1678     IP.addOperand(insertStrRef(OpName->getAsUnquotedString()), *Ty);
1679     return true;
1680   }
1681 
1682   // Untyped operand e.g. $x/$z in (G_FNEG $x, $z)
1683   if (isa<UnsetInit>(OpInit)) {
1684     assert(OpName && "Unset w/ no OpName?");
1685     IP.addOperand(insertStrRef(OpName->getAsUnquotedString()), PatternType());
1686     return true;
1687   }
1688 
1689   return ParseErr();
1690 }
1691 
1692 bool CombineRuleBuilder::parseInstructionPatternMIFlags(
1693     InstructionPattern &IP, const DagInit *Op) const {
1694   auto *CGIP = dyn_cast<CodeGenInstructionPattern>(&IP);
1695   if (!CGIP) {
1696     PrintError("matching/writing MIFlags is only allowed on CodeGenInstruction "
1697                "patterns");
1698     return false;
1699   }
1700 
1701   const auto CheckFlagEnum = [&](const Record *R) {
1702     if (!R->isSubClassOf(MIFlagsEnumClassName)) {
1703       PrintError("'" + R->getName() + "' is not a subclass of '" +
1704                  MIFlagsEnumClassName + "'");
1705       return false;
1706     }
1707 
1708     return true;
1709   };
1710 
1711   if (CGIP->getMIFlagsInfo()) {
1712     PrintError("MIFlags can only be present once on an instruction");
1713     return false;
1714   }
1715 
1716   auto &FI = CGIP->getOrCreateMIFlagsInfo();
1717   for (unsigned K = 0; K < Op->getNumArgs(); ++K) {
1718     const Init *Arg = Op->getArg(K);
1719 
1720     // Match/set a flag: (MIFlags FmNoNans)
1721     if (const auto *Def = dyn_cast<DefInit>(Arg)) {
1722       const Record *R = Def->getDef();
1723       if (!CheckFlagEnum(R))
1724         return false;
1725 
1726       FI.addSetFlag(R);
1727       continue;
1728     }
1729 
1730     // Do not match a flag/unset a flag: (MIFlags (not FmNoNans))
1731     if (const DagInit *NotDag = getDagWithSpecificOperator(*Arg, "not")) {
1732       for (const Init *NotArg : NotDag->getArgs()) {
1733         const DefInit *DefArg = dyn_cast<DefInit>(NotArg);
1734         if (!DefArg) {
1735           PrintError("cannot parse '" + NotArg->getAsUnquotedString() +
1736                      "': expected a '" + MIFlagsEnumClassName + "'");
1737           return false;
1738         }
1739 
1740         const Record *R = DefArg->getDef();
1741         if (!CheckFlagEnum(R))
1742           return false;
1743 
1744         FI.addUnsetFlag(R);
1745         continue;
1746       }
1747 
1748       continue;
1749     }
1750 
1751     // Copy flags from a matched instruction: (MIFlags $mi)
1752     if (isa<UnsetInit>(Arg)) {
1753       FI.addCopyFlag(insertStrRef(Op->getArgName(K)->getAsUnquotedString()));
1754       continue;
1755     }
1756   }
1757 
1758   return true;
1759 }
1760 
1761 std::unique_ptr<PatFrag>
1762 CombineRuleBuilder::parsePatFragImpl(const Record *Def) const {
1763   auto StackTrace = PrettyStackTraceParse(*Def);
1764   if (!Def->isSubClassOf(PatFrag::ClassName))
1765     return nullptr;
1766 
1767   const DagInit *Ins = Def->getValueAsDag("InOperands");
1768   if (Ins->getOperatorAsDef(Def->getLoc())->getName() != "ins") {
1769     ::PrintError(Def, "expected 'ins' operator for " + PatFrag::ClassName +
1770                           " in operands list");
1771     return nullptr;
1772   }
1773 
1774   const DagInit *Outs = Def->getValueAsDag("OutOperands");
1775   if (Outs->getOperatorAsDef(Def->getLoc())->getName() != "outs") {
1776     ::PrintError(Def, "expected 'outs' operator for " + PatFrag::ClassName +
1777                           " out operands list");
1778     return nullptr;
1779   }
1780 
1781   auto Result = std::make_unique<PatFrag>(*Def);
1782   if (!parsePatFragParamList(Def->getLoc(), *Outs,
1783                              [&](StringRef Name, PatFrag::ParamKind Kind) {
1784                                Result->addOutParam(insertStrRef(Name), Kind);
1785                                return true;
1786                              }))
1787     return nullptr;
1788 
1789   if (!parsePatFragParamList(Def->getLoc(), *Ins,
1790                              [&](StringRef Name, PatFrag::ParamKind Kind) {
1791                                Result->addInParam(insertStrRef(Name), Kind);
1792                                return true;
1793                              }))
1794     return nullptr;
1795 
1796   const ListInit *Alts = Def->getValueAsListInit("Alternatives");
1797   unsigned AltIdx = 0;
1798   for (const Init *Alt : *Alts) {
1799     const auto *PatDag = dyn_cast<DagInit>(Alt);
1800     if (!PatDag) {
1801       ::PrintError(Def, "expected dag init for PatFrag pattern alternative");
1802       return nullptr;
1803     }
1804 
1805     PatFrag::Alternative &A = Result->addAlternative();
1806     const auto AddPat = [&](std::unique_ptr<Pattern> Pat) {
1807       A.Pats.push_back(std::move(Pat));
1808       return true;
1809     };
1810 
1811     if (!parsePatternList(
1812             *PatDag, AddPat, "pattern", Def->getLoc(),
1813             /*AnonPatPrefix*/
1814             (Def->getName() + "_alt" + Twine(AltIdx++) + "_pattern").str()))
1815       return nullptr;
1816   }
1817 
1818   if (!Result->buildOperandsTables() || !Result->checkSemantics())
1819     return nullptr;
1820 
1821   return Result;
1822 }
1823 
1824 bool CombineRuleBuilder::parsePatFragParamList(
1825     ArrayRef<SMLoc> DiagLoc, const DagInit &OpsList,
1826     function_ref<bool(StringRef, PatFrag::ParamKind)> ParseAction) const {
1827   for (unsigned K = 0; K < OpsList.getNumArgs(); ++K) {
1828     const StringInit *Name = OpsList.getArgName(K);
1829     const Init *Ty = OpsList.getArg(K);
1830 
1831     if (!Name) {
1832       ::PrintError(DiagLoc, "all operands must be named'");
1833       return false;
1834     }
1835     const std::string NameStr = Name->getAsUnquotedString();
1836 
1837     PatFrag::ParamKind OpKind;
1838     if (isSpecificDef(*Ty, "gi_imm"))
1839       OpKind = PatFrag::PK_Imm;
1840     else if (isSpecificDef(*Ty, "root"))
1841       OpKind = PatFrag::PK_Root;
1842     else if (isa<UnsetInit>(Ty) ||
1843              isSpecificDef(*Ty, "gi_mo")) // no type = gi_mo.
1844       OpKind = PatFrag::PK_MachineOperand;
1845     else {
1846       ::PrintError(
1847           DiagLoc,
1848           "'" + NameStr +
1849               "' operand type was expected to be 'root', 'gi_imm' or 'gi_mo'");
1850       return false;
1851     }
1852 
1853     if (!ParseAction(NameStr, OpKind))
1854       return false;
1855   }
1856 
1857   return true;
1858 }
1859 
1860 const PatFrag *CombineRuleBuilder::parsePatFrag(const Record *Def) const {
1861   // Cache already parsed PatFrags to avoid doing extra work.
1862   static DenseMap<const Record *, std::unique_ptr<PatFrag>> ParsedPatFrags;
1863 
1864   auto It = ParsedPatFrags.find(Def);
1865   if (It != ParsedPatFrags.end()) {
1866     SeenPatFrags.insert(It->second.get());
1867     return It->second.get();
1868   }
1869 
1870   std::unique_ptr<PatFrag> NewPatFrag = parsePatFragImpl(Def);
1871   if (!NewPatFrag) {
1872     ::PrintError(Def, "Could not parse " + PatFrag::ClassName + " '" +
1873                           Def->getName() + "'");
1874     // Put a nullptr in the map so we don't attempt parsing this again.
1875     ParsedPatFrags[Def] = nullptr;
1876     return nullptr;
1877   }
1878 
1879   const auto *Res = NewPatFrag.get();
1880   ParsedPatFrags[Def] = std::move(NewPatFrag);
1881   SeenPatFrags.insert(Res);
1882   return Res;
1883 }
1884 
1885 bool CombineRuleBuilder::emitMatchPattern(CodeExpansions &CE,
1886                                           const PatternAlternatives &Alts,
1887                                           const InstructionPattern &IP) {
1888   auto StackTrace = PrettyStackTraceEmit(RuleDef, &IP);
1889 
1890   auto &M = addRuleMatcher(Alts);
1891   InstructionMatcher &IM = M.addInstructionMatcher(IP.getName());
1892   declareInstExpansion(CE, IM, IP.getName());
1893 
1894   DenseSet<const Pattern *> SeenPats;
1895 
1896   const auto FindOperandDef = [&](StringRef Op) -> InstructionPattern * {
1897     return MatchOpTable.getDef(Op);
1898   };
1899 
1900   if (const auto *CGP = dyn_cast<CodeGenInstructionPattern>(&IP)) {
1901     if (!emitCodeGenInstructionMatchPattern(CE, Alts, M, IM, *CGP, SeenPats,
1902                                             FindOperandDef))
1903       return false;
1904   } else if (const auto *PFP = dyn_cast<PatFragPattern>(&IP)) {
1905     if (!PFP->getPatFrag().canBeMatchRoot()) {
1906       PrintError("cannot use '" + PFP->getInstName() + " as match root");
1907       return false;
1908     }
1909 
1910     if (!emitPatFragMatchPattern(CE, Alts, M, &IM, *PFP, SeenPats))
1911       return false;
1912   } else if (isa<BuiltinPattern>(&IP)) {
1913     llvm_unreachable("No match builtins known!");
1914   } else
1915     llvm_unreachable("Unknown kind of InstructionPattern!");
1916 
1917   // Emit remaining patterns
1918   for (auto &Pat : values(MatchPats)) {
1919     if (SeenPats.contains(Pat.get()))
1920       continue;
1921 
1922     switch (Pat->getKind()) {
1923     case Pattern::K_AnyOpcode:
1924       PrintError("wip_match_opcode can not be used with instruction patterns!");
1925       return false;
1926     case Pattern::K_PatFrag: {
1927       if (!emitPatFragMatchPattern(CE, Alts, M, /*IM*/ nullptr,
1928                                    *cast<PatFragPattern>(Pat.get()), SeenPats))
1929         return false;
1930       continue;
1931     }
1932     case Pattern::K_Builtin:
1933       PrintError("No known match builtins");
1934       return false;
1935     case Pattern::K_CodeGenInstruction:
1936       cast<InstructionPattern>(Pat.get())->reportUnreachable(RuleDef.getLoc());
1937       return false;
1938     case Pattern::K_CXX: {
1939       addCXXPredicate(M, CE, *cast<CXXPattern>(Pat.get()), Alts);
1940       continue;
1941     }
1942     default:
1943       llvm_unreachable("unknown pattern kind!");
1944     }
1945   }
1946 
1947   return emitApplyPatterns(CE, M);
1948 }
1949 
1950 bool CombineRuleBuilder::emitMatchPattern(CodeExpansions &CE,
1951                                           const PatternAlternatives &Alts,
1952                                           const AnyOpcodePattern &AOP) {
1953   auto StackTrace = PrettyStackTraceEmit(RuleDef, &AOP);
1954 
1955   for (const CodeGenInstruction *CGI : AOP.insts()) {
1956     auto &M = addRuleMatcher(Alts, "wip_match_opcode '" +
1957                                        CGI->TheDef->getName() + "'");
1958 
1959     InstructionMatcher &IM = M.addInstructionMatcher(AOP.getName());
1960     declareInstExpansion(CE, IM, AOP.getName());
1961     // declareInstExpansion needs to be identical, otherwise we need to create a
1962     // CodeExpansions object here instead.
1963     assert(IM.getInsnVarID() == 0);
1964 
1965     IM.addPredicate<InstructionOpcodeMatcher>(CGI);
1966 
1967     // Emit remaining patterns.
1968     for (auto &Pat : values(MatchPats)) {
1969       if (Pat.get() == &AOP)
1970         continue;
1971 
1972       switch (Pat->getKind()) {
1973       case Pattern::K_AnyOpcode:
1974         PrintError("wip_match_opcode can only be present once!");
1975         return false;
1976       case Pattern::K_PatFrag: {
1977         DenseSet<const Pattern *> SeenPats;
1978         if (!emitPatFragMatchPattern(CE, Alts, M, /*IM*/ nullptr,
1979                                      *cast<PatFragPattern>(Pat.get()),
1980                                      SeenPats))
1981           return false;
1982         continue;
1983       }
1984       case Pattern::K_Builtin:
1985         PrintError("No known match builtins");
1986         return false;
1987       case Pattern::K_CodeGenInstruction:
1988         cast<InstructionPattern>(Pat.get())->reportUnreachable(
1989             RuleDef.getLoc());
1990         return false;
1991       case Pattern::K_CXX: {
1992         addCXXPredicate(M, CE, *cast<CXXPattern>(Pat.get()), Alts);
1993         break;
1994       }
1995       default:
1996         llvm_unreachable("unknown pattern kind!");
1997       }
1998     }
1999 
2000     if (!emitApplyPatterns(CE, M))
2001       return false;
2002   }
2003 
2004   return true;
2005 }
2006 
2007 bool CombineRuleBuilder::emitPatFragMatchPattern(
2008     CodeExpansions &CE, const PatternAlternatives &Alts, RuleMatcher &RM,
2009     InstructionMatcher *IM, const PatFragPattern &PFP,
2010     DenseSet<const Pattern *> &SeenPats) {
2011   auto StackTrace = PrettyStackTraceEmit(RuleDef, &PFP);
2012 
2013   if (SeenPats.contains(&PFP))
2014     return true;
2015   SeenPats.insert(&PFP);
2016 
2017   const auto &PF = PFP.getPatFrag();
2018 
2019   if (!IM) {
2020     // When we don't have an IM, this means this PatFrag isn't reachable from
2021     // the root. This is only acceptable if it doesn't define anything (e.g. a
2022     // pure C++ PatFrag).
2023     if (PF.num_out_params() != 0) {
2024       PFP.reportUnreachable(RuleDef.getLoc());
2025       return false;
2026     }
2027   } else {
2028     // When an IM is provided, this is reachable from the root, and we're
2029     // expecting to have output operands.
2030     // TODO: If we want to allow for multiple roots we'll need a map of IMs
2031     // then, and emission becomes a bit more complicated.
2032     assert(PF.num_roots() == 1);
2033   }
2034 
2035   CodeExpansions PatFragCEs;
2036   if (!PFP.mapInputCodeExpansions(CE, PatFragCEs, RuleDef.getLoc()))
2037     return false;
2038 
2039   // List of {ParamName, ArgName}.
2040   // When all patterns have been emitted, find expansions in PatFragCEs named
2041   // ArgName and add their expansion to CE using ParamName as the key.
2042   SmallVector<std::pair<std::string, std::string>, 4> CEsToImport;
2043 
2044   // Map parameter names to the actual argument.
2045   const auto OperandMapper =
2046       [&](const InstructionOperand &O) -> InstructionOperand {
2047     if (!O.isNamedOperand())
2048       return O;
2049 
2050     StringRef ParamName = O.getOperandName();
2051 
2052     // Not sure what to do with those tbh. They should probably never be here.
2053     assert(!O.isNamedImmediate() && "TODO: handle named imms");
2054     unsigned PIdx = PF.getParamIdx(ParamName);
2055 
2056     // Map parameters to the argument values.
2057     if (PIdx == (unsigned)-1) {
2058       // This is a temp of the PatFragPattern, prefix the name to avoid
2059       // conflicts.
2060       return O.withNewName(
2061           insertStrRef((PFP.getName() + "." + ParamName).str()));
2062     }
2063 
2064     // The operand will be added to PatFragCEs's code expansions using the
2065     // parameter's name. If it's bound to some operand during emission of the
2066     // patterns, we'll want to add it to CE.
2067     auto ArgOp = PFP.getOperand(PIdx);
2068     if (ArgOp.isNamedOperand())
2069       CEsToImport.emplace_back(ArgOp.getOperandName().str(), ParamName);
2070 
2071     if (ArgOp.getType() && O.getType() && ArgOp.getType() != O.getType()) {
2072       StringRef PFName = PF.getName();
2073       PrintWarning("impossible type constraints: operand " + Twine(PIdx) +
2074                    " of '" + PFP.getName() + "' has type '" +
2075                    ArgOp.getType().str() + "', but '" + PFName +
2076                    "' constrains it to '" + O.getType().str() + "'");
2077       if (ArgOp.isNamedOperand())
2078         PrintNote("operand " + Twine(PIdx) + " of '" + PFP.getName() +
2079                   "' is '" + ArgOp.getOperandName() + "'");
2080       if (O.isNamedOperand())
2081         PrintNote("argument " + Twine(PIdx) + " of '" + PFName + "' is '" +
2082                   ParamName + "'");
2083     }
2084 
2085     return ArgOp;
2086   };
2087 
2088   // PatFragPatterns are only made of InstructionPatterns or CXXPatterns.
2089   // Emit instructions from the root.
2090   const auto &FragAlt = PF.getAlternative(Alts.lookup(&PFP));
2091   const auto &FragAltOT = FragAlt.OpTable;
2092   const auto LookupOperandDef =
2093       [&](StringRef Op) -> const InstructionPattern * {
2094     return FragAltOT.getDef(Op);
2095   };
2096 
2097   DenseSet<const Pattern *> PatFragSeenPats;
2098   for (const auto &[Idx, InOp] : enumerate(PF.out_params())) {
2099     if (InOp.Kind != PatFrag::PK_Root)
2100       continue;
2101 
2102     StringRef ParamName = InOp.Name;
2103     const auto *Def = FragAltOT.getDef(ParamName);
2104     assert(Def && "PatFrag::checkSemantics should have emitted an error if "
2105                   "an out operand isn't defined!");
2106     assert(isa<CodeGenInstructionPattern>(Def) &&
2107            "Nested PatFrags not supported yet");
2108 
2109     if (!emitCodeGenInstructionMatchPattern(
2110             PatFragCEs, Alts, RM, *IM, *cast<CodeGenInstructionPattern>(Def),
2111             PatFragSeenPats, LookupOperandDef, OperandMapper))
2112       return false;
2113   }
2114 
2115   // Emit leftovers.
2116   for (const auto &Pat : FragAlt.Pats) {
2117     if (PatFragSeenPats.contains(Pat.get()))
2118       continue;
2119 
2120     if (const auto *CXXPat = dyn_cast<CXXPattern>(Pat.get())) {
2121       addCXXPredicate(RM, PatFragCEs, *CXXPat, Alts);
2122       continue;
2123     }
2124 
2125     if (const auto *IP = dyn_cast<InstructionPattern>(Pat.get())) {
2126       IP->reportUnreachable(PF.getLoc());
2127       return false;
2128     }
2129 
2130     llvm_unreachable("Unexpected pattern kind in PatFrag");
2131   }
2132 
2133   for (const auto &[ParamName, ArgName] : CEsToImport) {
2134     // Note: we're find if ParamName already exists. It just means it's been
2135     // bound before, so we prefer to keep the first binding.
2136     CE.declare(ParamName, PatFragCEs.lookup(ArgName));
2137   }
2138 
2139   return true;
2140 }
2141 
2142 bool CombineRuleBuilder::emitApplyPatterns(CodeExpansions &CE, RuleMatcher &M) {
2143   if (hasOnlyCXXApplyPatterns()) {
2144     for (auto &Pat : values(ApplyPats))
2145       addCXXAction(M, CE, *cast<CXXPattern>(Pat.get()));
2146     return true;
2147   }
2148 
2149   DenseSet<const Pattern *> SeenPats;
2150   StringMap<unsigned> OperandToTempRegID;
2151 
2152   for (auto *ApplyRoot : ApplyRoots) {
2153     assert(isa<InstructionPattern>(ApplyRoot) &&
2154            "Root can only be a InstructionPattern!");
2155     if (!emitInstructionApplyPattern(CE, M,
2156                                      cast<InstructionPattern>(*ApplyRoot),
2157                                      SeenPats, OperandToTempRegID))
2158       return false;
2159   }
2160 
2161   for (auto &Pat : values(ApplyPats)) {
2162     if (SeenPats.contains(Pat.get()))
2163       continue;
2164 
2165     switch (Pat->getKind()) {
2166     case Pattern::K_AnyOpcode:
2167       llvm_unreachable("Unexpected pattern in apply!");
2168     case Pattern::K_PatFrag:
2169       // TODO: We could support pure C++ PatFrags as a temporary thing.
2170       llvm_unreachable("Unexpected pattern in apply!");
2171     case Pattern::K_Builtin:
2172       if (!emitInstructionApplyPattern(CE, M, cast<BuiltinPattern>(*Pat),
2173                                        SeenPats, OperandToTempRegID))
2174         return false;
2175       break;
2176     case Pattern::K_CodeGenInstruction:
2177       cast<CodeGenInstructionPattern>(*Pat).reportUnreachable(RuleDef.getLoc());
2178       return false;
2179     case Pattern::K_CXX: {
2180       addCXXAction(M, CE, *cast<CXXPattern>(Pat.get()));
2181       continue;
2182     }
2183     default:
2184       llvm_unreachable("unknown pattern kind!");
2185     }
2186   }
2187 
2188   return true;
2189 }
2190 
2191 bool CombineRuleBuilder::emitInstructionApplyPattern(
2192     CodeExpansions &CE, RuleMatcher &M, const InstructionPattern &P,
2193     DenseSet<const Pattern *> &SeenPats,
2194     StringMap<unsigned> &OperandToTempRegID) {
2195   auto StackTrace = PrettyStackTraceEmit(RuleDef, &P);
2196 
2197   if (SeenPats.contains(&P))
2198     return true;
2199 
2200   SeenPats.insert(&P);
2201 
2202   // First, render the uses.
2203   for (auto &Op : P.named_operands()) {
2204     if (Op.isDef())
2205       continue;
2206 
2207     StringRef OpName = Op.getOperandName();
2208     if (const auto *DefPat = ApplyOpTable.getDef(OpName)) {
2209       if (!emitInstructionApplyPattern(CE, M, *DefPat, SeenPats,
2210                                        OperandToTempRegID))
2211         return false;
2212     } else {
2213       // If we have no def, check this exists in the MatchRoot.
2214       if (!Op.isNamedImmediate() && !MatchOpTable.lookup(OpName).Found) {
2215         PrintError("invalid output operand '" + OpName +
2216                    "': operand is not a live-in of the match pattern, and it "
2217                    "has no definition");
2218         return false;
2219       }
2220     }
2221   }
2222 
2223   if (const auto *BP = dyn_cast<BuiltinPattern>(&P))
2224     return emitBuiltinApplyPattern(CE, M, *BP, OperandToTempRegID);
2225 
2226   if (isa<PatFragPattern>(&P))
2227     llvm_unreachable("PatFragPatterns is not supported in 'apply'!");
2228 
2229   auto &CGIP = cast<CodeGenInstructionPattern>(P);
2230 
2231   // Now render this inst.
2232   auto &DstMI =
2233       M.addAction<BuildMIAction>(M.allocateOutputInsnID(), &CGIP.getInst());
2234 
2235   for (auto &Op : P.operands()) {
2236     if (Op.isNamedImmediate()) {
2237       PrintError("invalid output operand '" + Op.getOperandName() +
2238                  "': output immediates cannot be named");
2239       PrintNote("while emitting pattern '" + P.getName() + "' (" +
2240                 P.getInstName() + ")");
2241       return false;
2242     }
2243 
2244     if (Op.hasImmValue()) {
2245       if (!emitCodeGenInstructionApplyImmOperand(M, DstMI, CGIP, Op))
2246         return false;
2247       continue;
2248     }
2249 
2250     StringRef OpName = Op.getOperandName();
2251 
2252     // Uses of operand.
2253     if (!Op.isDef()) {
2254       if (auto It = OperandToTempRegID.find(OpName);
2255           It != OperandToTempRegID.end()) {
2256         assert(!MatchOpTable.lookup(OpName).Found &&
2257                "Temp reg is also from match pattern?");
2258         DstMI.addRenderer<TempRegRenderer>(It->second);
2259       } else {
2260         // This should be a match live in or a redef of a matched instr.
2261         // If it's a use of a temporary register, then we messed up somewhere -
2262         // the previous condition should have passed.
2263         assert(MatchOpTable.lookup(OpName).Found &&
2264                !ApplyOpTable.getDef(OpName) && "Temp reg not emitted yet!");
2265         DstMI.addRenderer<CopyRenderer>(OpName);
2266       }
2267       continue;
2268     }
2269 
2270     // Determine what we're dealing with. Are we replace a matched instruction?
2271     // Creating a new one?
2272     auto OpLookupRes = MatchOpTable.lookup(OpName);
2273     if (OpLookupRes.Found) {
2274       if (OpLookupRes.isLiveIn()) {
2275         // live-in of the match pattern.
2276         PrintError("Cannot define live-in operand '" + OpName +
2277                    "' in the 'apply' pattern");
2278         return false;
2279       }
2280       assert(OpLookupRes.Def);
2281 
2282       // TODO: Handle this. We need to mutate the instr, or delete the old
2283       // one.
2284       //       Likewise, we also need to ensure we redef everything, if the
2285       //       instr has more than one def, we need to redef all or nothing.
2286       if (OpLookupRes.Def != MatchRoot) {
2287         PrintError("redefining an instruction other than the root is not "
2288                    "supported (operand '" +
2289                    OpName + "')");
2290         return false;
2291       }
2292       // redef of a match
2293       DstMI.addRenderer<CopyRenderer>(OpName);
2294       continue;
2295     }
2296 
2297     // Define a new register unique to the apply patterns (AKA a "temp"
2298     // register).
2299     unsigned TempRegID;
2300     if (auto It = OperandToTempRegID.find(OpName);
2301         It != OperandToTempRegID.end()) {
2302       TempRegID = It->second;
2303     } else {
2304       // This is a brand new register.
2305       TempRegID = M.allocateTempRegID();
2306       OperandToTempRegID[OpName] = TempRegID;
2307       const auto Ty = Op.getType();
2308       if (!Ty) {
2309         PrintError("def of a new register '" + OpName +
2310                    "' in the apply patterns must have a type");
2311         return false;
2312       }
2313 
2314       declareTempRegExpansion(CE, TempRegID, OpName);
2315       // Always insert the action at the beginning, otherwise we may end up
2316       // using the temp reg before it's available.
2317       M.insertAction<MakeTempRegisterAction>(
2318           M.actions_begin(), getLLTCodeGenOrTempType(Ty, M), TempRegID);
2319     }
2320 
2321     DstMI.addRenderer<TempRegRenderer>(TempRegID);
2322   }
2323 
2324   // Render MIFlags
2325   if (const auto *FI = CGIP.getMIFlagsInfo()) {
2326     for (StringRef InstName : FI->copy_flags())
2327       DstMI.addCopiedMIFlags(M.getInstructionMatcher(InstName));
2328     for (StringRef F : FI->set_flags())
2329       DstMI.addSetMIFlags(F);
2330     for (StringRef F : FI->unset_flags())
2331       DstMI.addUnsetMIFlags(F);
2332   }
2333 
2334   // Don't allow mutating opcodes for GISel combiners. We want a more precise
2335   // handling of MIFlags so we require them to be explicitly preserved.
2336   //
2337   // TODO: We don't mutate very often, if at all in combiners, but it'd be nice
2338   // to re-enable this. We'd then need to always clear MIFlags when mutating
2339   // opcodes, and never mutate an inst that we copy flags from.
2340   // DstMI.chooseInsnToMutate(M);
2341   declareInstExpansion(CE, DstMI, P.getName());
2342 
2343   return true;
2344 }
2345 
2346 bool CombineRuleBuilder::emitCodeGenInstructionApplyImmOperand(
2347     RuleMatcher &M, BuildMIAction &DstMI, const CodeGenInstructionPattern &P,
2348     const InstructionOperand &O) {
2349   // If we have a type, we implicitly emit a G_CONSTANT, except for G_CONSTANT
2350   // itself where we emit a CImm.
2351   //
2352   // No type means we emit a simple imm.
2353   // G_CONSTANT is a special case and needs a CImm though so this is likely a
2354   // mistake.
2355   const bool isGConstant = P.is("G_CONSTANT");
2356   const auto Ty = O.getType();
2357   if (!Ty) {
2358     if (isGConstant) {
2359       PrintError("'G_CONSTANT' immediate must be typed!");
2360       PrintNote("while emitting pattern '" + P.getName() + "' (" +
2361                 P.getInstName() + ")");
2362       return false;
2363     }
2364 
2365     DstMI.addRenderer<ImmRenderer>(O.getImmValue());
2366     return true;
2367   }
2368 
2369   auto ImmTy = getLLTCodeGenOrTempType(Ty, M);
2370 
2371   if (isGConstant) {
2372     DstMI.addRenderer<ImmRenderer>(O.getImmValue(), ImmTy);
2373     return true;
2374   }
2375 
2376   unsigned TempRegID = M.allocateTempRegID();
2377   // Ensure MakeTempReg & the BuildConstantAction occur at the beginning.
2378   auto InsertIt = M.insertAction<MakeTempRegisterAction>(M.actions_begin(),
2379                                                          ImmTy, TempRegID);
2380   M.insertAction<BuildConstantAction>(++InsertIt, TempRegID, O.getImmValue());
2381   DstMI.addRenderer<TempRegRenderer>(TempRegID);
2382   return true;
2383 }
2384 
2385 bool CombineRuleBuilder::emitBuiltinApplyPattern(
2386     CodeExpansions &CE, RuleMatcher &M, const BuiltinPattern &P,
2387     StringMap<unsigned> &OperandToTempRegID) {
2388   const auto Error = [&](Twine Reason) {
2389     PrintError("cannot emit '" + P.getInstName() + "' builtin: " + Reason);
2390     return false;
2391   };
2392 
2393   switch (P.getBuiltinKind()) {
2394   case BI_EraseRoot: {
2395     // Root is always inst 0.
2396     M.addAction<EraseInstAction>(/*InsnID*/ 0);
2397     return true;
2398   }
2399   case BI_ReplaceReg: {
2400     StringRef Old = P.getOperand(0).getOperandName();
2401     StringRef New = P.getOperand(1).getOperandName();
2402 
2403     if (!ApplyOpTable.lookup(New).Found && !MatchOpTable.lookup(New).Found)
2404       return Error("unknown operand '" + Old + "'");
2405 
2406     auto &OldOM = M.getOperandMatcher(Old);
2407     if (auto It = OperandToTempRegID.find(New);
2408         It != OperandToTempRegID.end()) {
2409       // Replace with temp reg.
2410       M.addAction<ReplaceRegAction>(OldOM.getInsnVarID(), OldOM.getOpIdx(),
2411                                     It->second);
2412     } else {
2413       // Replace with matched reg.
2414       auto &NewOM = M.getOperandMatcher(New);
2415       M.addAction<ReplaceRegAction>(OldOM.getInsnVarID(), OldOM.getOpIdx(),
2416                                     NewOM.getInsnVarID(), NewOM.getOpIdx());
2417     }
2418     // checkSemantics should have ensured that we can only rewrite the root.
2419     // Ensure we're deleting it.
2420     assert(MatchOpTable.getDef(Old) == MatchRoot);
2421     // TODO: We could avoid adding the action again if it's already in. The
2422     // MatchTable is smart enough to only emit one opcode even if
2423     // EraseInstAction is present multiple times. I think searching for a copy
2424     // is more expensive than just blindly adding it though.
2425     M.addAction<EraseInstAction>(/*InsnID*/ 0);
2426 
2427     return true;
2428   }
2429   }
2430 
2431   llvm_unreachable("Unknown BuiltinKind!");
2432 }
2433 
2434 bool isLiteralImm(const InstructionPattern &P, unsigned OpIdx) {
2435   if (const auto *CGP = dyn_cast<CodeGenInstructionPattern>(&P)) {
2436     StringRef InstName = CGP->getInst().TheDef->getName();
2437     return (InstName == "G_CONSTANT" || InstName == "G_FCONSTANT") &&
2438            OpIdx == 1;
2439   }
2440 
2441   llvm_unreachable("TODO");
2442 }
2443 
2444 bool CombineRuleBuilder::emitCodeGenInstructionMatchPattern(
2445     CodeExpansions &CE, const PatternAlternatives &Alts, RuleMatcher &M,
2446     InstructionMatcher &IM, const CodeGenInstructionPattern &P,
2447     DenseSet<const Pattern *> &SeenPats, OperandDefLookupFn LookupOperandDef,
2448     OperandMapperFnRef OperandMapper) {
2449   auto StackTrace = PrettyStackTraceEmit(RuleDef, &P);
2450 
2451   if (SeenPats.contains(&P))
2452     return true;
2453 
2454   SeenPats.insert(&P);
2455 
2456   IM.addPredicate<InstructionOpcodeMatcher>(&P.getInst());
2457   declareInstExpansion(CE, IM, P.getName());
2458 
2459   // Check flags if needed.
2460   if (const auto *FI = P.getMIFlagsInfo()) {
2461     assert(FI->copy_flags().empty());
2462 
2463     if (const auto &SetF = FI->set_flags(); !SetF.empty())
2464       IM.addPredicate<MIFlagsInstructionPredicateMatcher>(SetF.getArrayRef());
2465     if (const auto &UnsetF = FI->unset_flags(); !UnsetF.empty())
2466       IM.addPredicate<MIFlagsInstructionPredicateMatcher>(UnsetF.getArrayRef(),
2467                                                           /*CheckNot=*/true);
2468   }
2469 
2470   for (const auto &[Idx, OriginalO] : enumerate(P.operands())) {
2471     // Remap the operand. This is used when emitting InstructionPatterns inside
2472     // PatFrags, so it can remap them to the arguments passed to the pattern.
2473     //
2474     // We use the remapped operand to emit immediates, and for the symbolic
2475     // operand names (in IM.addOperand). CodeExpansions and OperandTable lookups
2476     // still use the original name.
2477     //
2478     // The "def" flag on the remapped operand is always ignored.
2479     auto RemappedO = OperandMapper(OriginalO);
2480     assert(RemappedO.isNamedOperand() == OriginalO.isNamedOperand() &&
2481            "Cannot remap an unnamed operand to a named one!");
2482 
2483     const auto OpName =
2484         RemappedO.isNamedOperand() ? RemappedO.getOperandName().str() : "";
2485     OperandMatcher &OM =
2486         IM.addOperand(Idx, OpName, AllocatedTemporariesBaseID++);
2487     if (!OpName.empty())
2488       declareOperandExpansion(CE, OM, OriginalO.getOperandName());
2489 
2490     // Handle immediates.
2491     if (RemappedO.hasImmValue()) {
2492       if (isLiteralImm(P, Idx))
2493         OM.addPredicate<LiteralIntOperandMatcher>(RemappedO.getImmValue());
2494       else
2495         OM.addPredicate<ConstantIntOperandMatcher>(RemappedO.getImmValue());
2496     }
2497 
2498     // Handle typed operands, but only bother to check if it hasn't been done
2499     // before.
2500     //
2501     // getOperandMatcher will always return the first OM to have been created
2502     // for that Operand. "OM" here is always a new OperandMatcher.
2503     //
2504     // Always emit a check for unnamed operands.
2505     if (OpName.empty() ||
2506         !M.getOperandMatcher(OpName).contains<LLTOperandMatcher>()) {
2507       if (const auto Ty = RemappedO.getType()) {
2508         // TODO: We could support GITypeOf here on the condition that the
2509         // OperandMatcher exists already. Though it's clunky to make this work
2510         // and isn't all that useful so it's just rejected in typecheckPatterns
2511         // at this time.
2512         assert(Ty.isLLT() && "Only LLTs are supported in match patterns!");
2513         OM.addPredicate<LLTOperandMatcher>(getLLTCodeGen(Ty));
2514       }
2515     }
2516 
2517     // Stop here if the operand is a def, or if it had no name.
2518     if (OriginalO.isDef() || !OriginalO.isNamedOperand())
2519       continue;
2520 
2521     const auto *DefPat = LookupOperandDef(OriginalO.getOperandName());
2522     if (!DefPat)
2523       continue;
2524 
2525     if (OriginalO.hasImmValue()) {
2526       assert(!OpName.empty());
2527       // This is a named immediate that also has a def, that's not okay.
2528       // e.g.
2529       //    (G_SEXT $y, (i32 0))
2530       //    (COPY $x, 42:$y)
2531       PrintError("'" + OpName +
2532                  "' is a named immediate, it cannot be defined by another "
2533                  "instruction");
2534       PrintNote("'" + OpName + "' is defined by '" + DefPat->getName() + "'");
2535       return false;
2536     }
2537 
2538     // From here we know that the operand defines an instruction, and we need to
2539     // emit it.
2540     auto InstOpM =
2541         OM.addPredicate<InstructionOperandMatcher>(M, DefPat->getName());
2542     if (!InstOpM) {
2543       // TODO: copy-pasted from GlobalISelEmitter.cpp. Is it still relevant
2544       // here?
2545       PrintError("Nested instruction '" + DefPat->getName() +
2546                  "' cannot be the same as another operand '" +
2547                  OriginalO.getOperandName() + "'");
2548       return false;
2549     }
2550 
2551     auto &IM = (*InstOpM)->getInsnMatcher();
2552     if (const auto *CGIDef = dyn_cast<CodeGenInstructionPattern>(DefPat)) {
2553       if (!emitCodeGenInstructionMatchPattern(CE, Alts, M, IM, *CGIDef,
2554                                               SeenPats, LookupOperandDef,
2555                                               OperandMapper))
2556         return false;
2557       continue;
2558     }
2559 
2560     if (const auto *PFPDef = dyn_cast<PatFragPattern>(DefPat)) {
2561       if (!emitPatFragMatchPattern(CE, Alts, M, &IM, *PFPDef, SeenPats))
2562         return false;
2563       continue;
2564     }
2565 
2566     llvm_unreachable("unknown type of InstructionPattern");
2567   }
2568 
2569   return true;
2570 }
2571 
2572 //===- GICombinerEmitter --------------------------------------------------===//
2573 
2574 /// Main implementation class. This emits the tablegenerated output.
2575 ///
2576 /// It collects rules, uses `CombineRuleBuilder` to parse them and accumulate
2577 /// RuleMatchers, then takes all the necessary state/data from the various
2578 /// static storage pools and wires them together to emit the match table &
2579 /// associated function/data structures.
2580 class GICombinerEmitter final : public GlobalISelMatchTableExecutorEmitter {
2581   RecordKeeper &Records;
2582   StringRef Name;
2583   const CodeGenTarget &Target;
2584   Record *Combiner;
2585   unsigned NextRuleID = 0;
2586 
2587   // List all combine rules (ID, name) imported.
2588   // Note that the combiner rule ID is different from the RuleMatcher ID. The
2589   // latter is internal to the MatchTable, the former is the canonical ID of the
2590   // combine rule used to disable/enable it.
2591   std::vector<std::pair<unsigned, std::string>> AllCombineRules;
2592 
2593   // Keep track of all rules we've seen so far to ensure we don't process
2594   // the same rule twice.
2595   StringSet<> RulesSeen;
2596 
2597   MatchTable buildMatchTable(MutableArrayRef<RuleMatcher> Rules);
2598 
2599   void emitRuleConfigImpl(raw_ostream &OS);
2600 
2601   void emitAdditionalImpl(raw_ostream &OS) override;
2602 
2603   void emitMIPredicateFns(raw_ostream &OS) override;
2604   void emitI64ImmPredicateFns(raw_ostream &OS) override;
2605   void emitAPFloatImmPredicateFns(raw_ostream &OS) override;
2606   void emitAPIntImmPredicateFns(raw_ostream &OS) override;
2607   void emitTestSimplePredicate(raw_ostream &OS) override;
2608   void emitRunCustomAction(raw_ostream &OS) override;
2609 
2610   void emitAdditionalTemporariesDecl(raw_ostream &OS,
2611                                      StringRef Indent) override;
2612 
2613   const CodeGenTarget &getTarget() const override { return Target; }
2614   StringRef getClassName() const override {
2615     return Combiner->getValueAsString("Classname");
2616   }
2617 
2618   StringRef getCombineAllMethodName() const {
2619     return Combiner->getValueAsString("CombineAllMethodName");
2620   }
2621 
2622   std::string getRuleConfigClassName() const {
2623     return getClassName().str() + "RuleConfig";
2624   }
2625 
2626   void gatherRules(std::vector<RuleMatcher> &Rules,
2627                    const std::vector<Record *> &&RulesAndGroups);
2628 
2629 public:
2630   explicit GICombinerEmitter(RecordKeeper &RK, const CodeGenTarget &Target,
2631                              StringRef Name, Record *Combiner);
2632   ~GICombinerEmitter() {}
2633 
2634   void run(raw_ostream &OS);
2635 };
2636 
2637 void GICombinerEmitter::emitRuleConfigImpl(raw_ostream &OS) {
2638   OS << "struct " << getRuleConfigClassName() << " {\n"
2639      << "  SparseBitVector<> DisabledRules;\n\n"
2640      << "  bool isRuleEnabled(unsigned RuleID) const;\n"
2641      << "  bool parseCommandLineOption();\n"
2642      << "  bool setRuleEnabled(StringRef RuleIdentifier);\n"
2643      << "  bool setRuleDisabled(StringRef RuleIdentifier);\n"
2644      << "};\n\n";
2645 
2646   std::vector<std::pair<std::string, std::string>> Cases;
2647   Cases.reserve(AllCombineRules.size());
2648 
2649   for (const auto &[ID, Name] : AllCombineRules)
2650     Cases.emplace_back(Name, "return " + to_string(ID) + ";\n");
2651 
2652   OS << "static std::optional<uint64_t> getRuleIdxForIdentifier(StringRef "
2653         "RuleIdentifier) {\n"
2654      << "  uint64_t I;\n"
2655      << "  // getAtInteger(...) returns false on success\n"
2656      << "  bool Parsed = !RuleIdentifier.getAsInteger(0, I);\n"
2657      << "  if (Parsed)\n"
2658      << "    return I;\n\n"
2659      << "#ifndef NDEBUG\n";
2660   StringMatcher Matcher("RuleIdentifier", Cases, OS);
2661   Matcher.Emit();
2662   OS << "#endif // ifndef NDEBUG\n\n"
2663      << "  return std::nullopt;\n"
2664      << "}\n";
2665 
2666   OS << "static std::optional<std::pair<uint64_t, uint64_t>> "
2667         "getRuleRangeForIdentifier(StringRef RuleIdentifier) {\n"
2668      << "  std::pair<StringRef, StringRef> RangePair = "
2669         "RuleIdentifier.split('-');\n"
2670      << "  if (!RangePair.second.empty()) {\n"
2671      << "    const auto First = "
2672         "getRuleIdxForIdentifier(RangePair.first);\n"
2673      << "    const auto Last = "
2674         "getRuleIdxForIdentifier(RangePair.second);\n"
2675      << "    if (!First || !Last)\n"
2676      << "      return std::nullopt;\n"
2677      << "    if (First >= Last)\n"
2678      << "      report_fatal_error(\"Beginning of range should be before "
2679         "end of range\");\n"
2680      << "    return {{*First, *Last + 1}};\n"
2681      << "  }\n"
2682      << "  if (RangePair.first == \"*\") {\n"
2683      << "    return {{0, " << AllCombineRules.size() << "}};\n"
2684      << "  }\n"
2685      << "  const auto I = getRuleIdxForIdentifier(RangePair.first);\n"
2686      << "  if (!I)\n"
2687      << "    return std::nullopt;\n"
2688      << "  return {{*I, *I + 1}};\n"
2689      << "}\n\n";
2690 
2691   for (bool Enabled : {true, false}) {
2692     OS << "bool " << getRuleConfigClassName() << "::setRule"
2693        << (Enabled ? "Enabled" : "Disabled") << "(StringRef RuleIdentifier) {\n"
2694        << "  auto MaybeRange = getRuleRangeForIdentifier(RuleIdentifier);\n"
2695        << "  if (!MaybeRange)\n"
2696        << "    return false;\n"
2697        << "  for (auto I = MaybeRange->first; I < MaybeRange->second; ++I)\n"
2698        << "    DisabledRules." << (Enabled ? "reset" : "set") << "(I);\n"
2699        << "  return true;\n"
2700        << "}\n\n";
2701   }
2702 
2703   OS << "static std::vector<std::string> " << Name << "Option;\n"
2704      << "static cl::list<std::string> " << Name << "DisableOption(\n"
2705      << "    \"" << Name.lower() << "-disable-rule\",\n"
2706      << "    cl::desc(\"Disable one or more combiner rules temporarily in "
2707      << "the " << Name << " pass\"),\n"
2708      << "    cl::CommaSeparated,\n"
2709      << "    cl::Hidden,\n"
2710      << "    cl::cat(GICombinerOptionCategory),\n"
2711      << "    cl::callback([](const std::string &Str) {\n"
2712      << "      " << Name << "Option.push_back(Str);\n"
2713      << "    }));\n"
2714      << "static cl::list<std::string> " << Name << "OnlyEnableOption(\n"
2715      << "    \"" << Name.lower() << "-only-enable-rule\",\n"
2716      << "    cl::desc(\"Disable all rules in the " << Name
2717      << " pass then re-enable the specified ones\"),\n"
2718      << "    cl::Hidden,\n"
2719      << "    cl::cat(GICombinerOptionCategory),\n"
2720      << "    cl::callback([](const std::string &CommaSeparatedArg) {\n"
2721      << "      StringRef Str = CommaSeparatedArg;\n"
2722      << "      " << Name << "Option.push_back(\"*\");\n"
2723      << "      do {\n"
2724      << "        auto X = Str.split(\",\");\n"
2725      << "        " << Name << "Option.push_back((\"!\" + X.first).str());\n"
2726      << "        Str = X.second;\n"
2727      << "      } while (!Str.empty());\n"
2728      << "    }));\n"
2729      << "\n\n"
2730      << "bool " << getRuleConfigClassName()
2731      << "::isRuleEnabled(unsigned RuleID) const {\n"
2732      << "    return  !DisabledRules.test(RuleID);\n"
2733      << "}\n"
2734      << "bool " << getRuleConfigClassName() << "::parseCommandLineOption() {\n"
2735      << "  for (StringRef Identifier : " << Name << "Option) {\n"
2736      << "    bool Enabled = Identifier.consume_front(\"!\");\n"
2737      << "    if (Enabled && !setRuleEnabled(Identifier))\n"
2738      << "      return false;\n"
2739      << "    if (!Enabled && !setRuleDisabled(Identifier))\n"
2740      << "      return false;\n"
2741      << "  }\n"
2742      << "  return true;\n"
2743      << "}\n\n";
2744 }
2745 
2746 void GICombinerEmitter::emitAdditionalImpl(raw_ostream &OS) {
2747   OS << "bool " << getClassName() << "::" << getCombineAllMethodName()
2748      << "(MachineInstr &I) const {\n"
2749      << "  const TargetSubtargetInfo &ST = MF.getSubtarget();\n"
2750      << "  const PredicateBitset AvailableFeatures = "
2751         "getAvailableFeatures();\n"
2752      << "  B.setInstrAndDebugLoc(I);\n"
2753      << "  State.MIs.clear();\n"
2754      << "  State.MIs.push_back(&I);\n"
2755      << "  " << MatchDataInfo::StructName << " = "
2756      << MatchDataInfo::StructTypeName << "();\n\n"
2757      << "  if (executeMatchTable(*this, State, ExecInfo, B"
2758      << ", getMatchTable(), *ST.getInstrInfo(), MRI, "
2759         "*MRI.getTargetRegisterInfo(), *ST.getRegBankInfo(), AvailableFeatures"
2760      << ", /*CoverageInfo*/ nullptr)) {\n"
2761      << "    return true;\n"
2762      << "  }\n\n"
2763      << "  return false;\n"
2764      << "}\n\n";
2765 }
2766 
2767 void GICombinerEmitter::emitMIPredicateFns(raw_ostream &OS) {
2768   auto MatchCode = CXXPredicateCode::getAllMatchCode();
2769   emitMIPredicateFnsImpl<const CXXPredicateCode *>(
2770       OS, "", ArrayRef<const CXXPredicateCode *>(MatchCode),
2771       [](const CXXPredicateCode *C) -> StringRef { return C->BaseEnumName; },
2772       [](const CXXPredicateCode *C) -> StringRef { return C->Code; });
2773 }
2774 
2775 void GICombinerEmitter::emitI64ImmPredicateFns(raw_ostream &OS) {
2776   // Unused, but still needs to be called.
2777   emitImmPredicateFnsImpl<unsigned>(
2778       OS, "I64", "int64_t", {}, [](unsigned) { return ""; },
2779       [](unsigned) { return ""; });
2780 }
2781 
2782 void GICombinerEmitter::emitAPFloatImmPredicateFns(raw_ostream &OS) {
2783   // Unused, but still needs to be called.
2784   emitImmPredicateFnsImpl<unsigned>(
2785       OS, "APFloat", "const APFloat &", {}, [](unsigned) { return ""; },
2786       [](unsigned) { return ""; });
2787 }
2788 
2789 void GICombinerEmitter::emitAPIntImmPredicateFns(raw_ostream &OS) {
2790   // Unused, but still needs to be called.
2791   emitImmPredicateFnsImpl<unsigned>(
2792       OS, "APInt", "const APInt &", {}, [](unsigned) { return ""; },
2793       [](unsigned) { return ""; });
2794 }
2795 
2796 void GICombinerEmitter::emitTestSimplePredicate(raw_ostream &OS) {
2797   if (!AllCombineRules.empty()) {
2798     OS << "enum {\n";
2799     std::string EnumeratorSeparator = " = GICXXPred_Invalid + 1,\n";
2800     // To avoid emitting a switch, we expect that all those rules are in order.
2801     // That way we can just get the RuleID from the enum by subtracting
2802     // (GICXXPred_Invalid + 1).
2803     unsigned ExpectedID = 0;
2804     (void)ExpectedID;
2805     for (const auto &ID : keys(AllCombineRules)) {
2806       assert(ExpectedID++ == ID && "combine rules are not ordered!");
2807       OS << "  " << getIsEnabledPredicateEnumName(ID) << EnumeratorSeparator;
2808       EnumeratorSeparator = ",\n";
2809     }
2810     OS << "};\n\n";
2811   }
2812 
2813   OS << "bool " << getClassName()
2814      << "::testSimplePredicate(unsigned Predicate) const {\n"
2815      << "    return RuleConfig.isRuleEnabled(Predicate - "
2816         "GICXXPred_Invalid - "
2817         "1);\n"
2818      << "}\n";
2819 }
2820 
2821 void GICombinerEmitter::emitRunCustomAction(raw_ostream &OS) {
2822   const auto ApplyCode = CXXPredicateCode::getAllApplyCode();
2823 
2824   if (!ApplyCode.empty()) {
2825     OS << "enum {\n";
2826     std::string EnumeratorSeparator = " = GICXXCustomAction_Invalid + 1,\n";
2827     for (const auto &Apply : ApplyCode) {
2828       OS << "  " << Apply->getEnumNameWithPrefix(CXXApplyPrefix)
2829          << EnumeratorSeparator;
2830       EnumeratorSeparator = ",\n";
2831     }
2832     OS << "};\n";
2833   }
2834 
2835   OS << "void " << getClassName()
2836      << "::runCustomAction(unsigned ApplyID, const MatcherState &State, "
2837         "NewMIVector &OutMIs) const "
2838         "{\n";
2839   if (!ApplyCode.empty()) {
2840     OS << "  switch(ApplyID) {\n";
2841     for (const auto &Apply : ApplyCode) {
2842       OS << "  case " << Apply->getEnumNameWithPrefix(CXXApplyPrefix) << ":{\n"
2843          << "    " << join(split(Apply->Code, '\n'), "\n    ") << '\n'
2844          << "    return;\n";
2845       OS << "  }\n";
2846     }
2847     OS << "}\n";
2848   }
2849   OS << "  llvm_unreachable(\"Unknown Apply Action\");\n"
2850      << "}\n";
2851 }
2852 
2853 void GICombinerEmitter::emitAdditionalTemporariesDecl(raw_ostream &OS,
2854                                                       StringRef Indent) {
2855   OS << Indent << "struct " << MatchDataInfo::StructTypeName << " {\n";
2856   for (const auto &[Type, VarNames] : AllMatchDataVars) {
2857     assert(!VarNames.empty() && "Cannot have no vars for this type!");
2858     OS << Indent << "  " << Type << " " << join(VarNames, ", ") << ";\n";
2859   }
2860   OS << Indent << "};\n"
2861      << Indent << "mutable " << MatchDataInfo::StructTypeName << " "
2862      << MatchDataInfo::StructName << ";\n\n";
2863 }
2864 
2865 GICombinerEmitter::GICombinerEmitter(RecordKeeper &RK,
2866                                      const CodeGenTarget &Target,
2867                                      StringRef Name, Record *Combiner)
2868     : Records(RK), Name(Name), Target(Target), Combiner(Combiner) {}
2869 
2870 MatchTable
2871 GICombinerEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules) {
2872   std::vector<Matcher *> InputRules;
2873   for (Matcher &Rule : Rules)
2874     InputRules.push_back(&Rule);
2875 
2876   unsigned CurrentOrdering = 0;
2877   StringMap<unsigned> OpcodeOrder;
2878   for (RuleMatcher &Rule : Rules) {
2879     const StringRef Opcode = Rule.getOpcode();
2880     assert(!Opcode.empty() && "Didn't expect an undefined opcode");
2881     if (OpcodeOrder.count(Opcode) == 0)
2882       OpcodeOrder[Opcode] = CurrentOrdering++;
2883   }
2884 
2885   llvm::stable_sort(InputRules, [&OpcodeOrder](const Matcher *A,
2886                                                const Matcher *B) {
2887     auto *L = static_cast<const RuleMatcher *>(A);
2888     auto *R = static_cast<const RuleMatcher *>(B);
2889     return std::make_tuple(OpcodeOrder[L->getOpcode()], L->getNumOperands()) <
2890            std::make_tuple(OpcodeOrder[R->getOpcode()], R->getNumOperands());
2891   });
2892 
2893   for (Matcher *Rule : InputRules)
2894     Rule->optimize();
2895 
2896   std::vector<std::unique_ptr<Matcher>> MatcherStorage;
2897   std::vector<Matcher *> OptRules =
2898       optimizeRules<GroupMatcher>(InputRules, MatcherStorage);
2899 
2900   for (Matcher *Rule : OptRules)
2901     Rule->optimize();
2902 
2903   OptRules = optimizeRules<SwitchMatcher>(OptRules, MatcherStorage);
2904 
2905   return MatchTable::buildTable(OptRules, /*WithCoverage*/ false,
2906                                 /*IsCombiner*/ true);
2907 }
2908 
2909 /// Recurse into GICombineGroup's and flatten the ruleset into a simple list.
2910 void GICombinerEmitter::gatherRules(
2911     std::vector<RuleMatcher> &ActiveRules,
2912     const std::vector<Record *> &&RulesAndGroups) {
2913   for (Record *Rec : RulesAndGroups) {
2914     if (!Rec->isValueUnset("Rules")) {
2915       gatherRules(ActiveRules, Rec->getValueAsListOfDefs("Rules"));
2916       continue;
2917     }
2918 
2919     StringRef RuleName = Rec->getName();
2920     if (!RulesSeen.insert(RuleName).second) {
2921       PrintWarning(Rec->getLoc(),
2922                    "skipping rule '" + Rec->getName() +
2923                        "' because it has already been processed");
2924       continue;
2925     }
2926 
2927     AllCombineRules.emplace_back(NextRuleID, Rec->getName().str());
2928     CombineRuleBuilder CRB(Target, SubtargetFeatures, *Rec, NextRuleID++,
2929                            ActiveRules);
2930 
2931     if (!CRB.parseAll()) {
2932       assert(ErrorsPrinted && "Parsing failed without errors!");
2933       continue;
2934     }
2935 
2936     if (StopAfterParse) {
2937       CRB.print(outs());
2938       continue;
2939     }
2940 
2941     if (!CRB.emitRuleMatchers()) {
2942       assert(ErrorsPrinted && "Emission failed without errors!");
2943       continue;
2944     }
2945   }
2946 }
2947 
2948 void GICombinerEmitter::run(raw_ostream &OS) {
2949   InstructionOpcodeMatcher::initOpcodeValuesMap(Target);
2950   LLTOperandMatcher::initTypeIDValuesMap();
2951 
2952   Records.startTimer("Gather rules");
2953   std::vector<RuleMatcher> Rules;
2954   gatherRules(Rules, Combiner->getValueAsListOfDefs("Rules"));
2955   if (ErrorsPrinted)
2956     PrintFatalError(Combiner->getLoc(), "Failed to parse one or more rules");
2957 
2958   if (StopAfterParse)
2959     return;
2960 
2961   Records.startTimer("Creating Match Table");
2962   unsigned MaxTemporaries = 0;
2963   for (const auto &Rule : Rules)
2964     MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns());
2965 
2966   llvm::stable_sort(Rules, [&](const RuleMatcher &A, const RuleMatcher &B) {
2967     if (A.isHigherPriorityThan(B)) {
2968       assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
2969                                            "and less important at "
2970                                            "the same time");
2971       return true;
2972     }
2973     return false;
2974   });
2975 
2976   const MatchTable Table = buildMatchTable(Rules);
2977 
2978   Records.startTimer("Emit combiner");
2979 
2980   emitSourceFileHeader(getClassName().str() + " Combiner Match Table", OS);
2981 
2982   // Unused
2983   std::vector<StringRef> CustomRendererFns;
2984   // Unused
2985   std::vector<Record *> ComplexPredicates;
2986 
2987   SmallVector<LLTCodeGen, 16> TypeObjects;
2988   append_range(TypeObjects, KnownTypes);
2989   llvm::sort(TypeObjects);
2990 
2991   // Hack: Avoid empty declarator.
2992   if (TypeObjects.empty())
2993     TypeObjects.push_back(LLT::scalar(1));
2994 
2995   // GET_GICOMBINER_DEPS, which pulls in extra dependencies.
2996   OS << "#ifdef GET_GICOMBINER_DEPS\n"
2997      << "#include \"llvm/ADT/SparseBitVector.h\"\n"
2998      << "namespace llvm {\n"
2999      << "extern cl::OptionCategory GICombinerOptionCategory;\n"
3000      << "} // end namespace llvm\n"
3001      << "#endif // ifdef GET_GICOMBINER_DEPS\n\n";
3002 
3003   // GET_GICOMBINER_TYPES, which needs to be included before the declaration of
3004   // the class.
3005   OS << "#ifdef GET_GICOMBINER_TYPES\n";
3006   emitRuleConfigImpl(OS);
3007   OS << "#endif // ifdef GET_GICOMBINER_TYPES\n\n";
3008   emitPredicateBitset(OS, "GET_GICOMBINER_TYPES");
3009 
3010   // GET_GICOMBINER_CLASS_MEMBERS, which need to be included inside the class.
3011   emitPredicatesDecl(OS, "GET_GICOMBINER_CLASS_MEMBERS");
3012   emitTemporariesDecl(OS, "GET_GICOMBINER_CLASS_MEMBERS");
3013 
3014   // GET_GICOMBINER_IMPL, which needs to be included outside the class.
3015   emitExecutorImpl(OS, Table, TypeObjects, Rules, ComplexPredicates,
3016                    CustomRendererFns, "GET_GICOMBINER_IMPL");
3017 
3018   // GET_GICOMBINER_CONSTRUCTOR_INITS, which are in the constructor's
3019   // initializer list.
3020   emitPredicatesInit(OS, "GET_GICOMBINER_CONSTRUCTOR_INITS");
3021   emitTemporariesInit(OS, MaxTemporaries, "GET_GICOMBINER_CONSTRUCTOR_INITS");
3022 }
3023 
3024 } // end anonymous namespace
3025 
3026 //===----------------------------------------------------------------------===//
3027 
3028 static void EmitGICombiner(RecordKeeper &RK, raw_ostream &OS) {
3029   EnablePrettyStackTrace();
3030   CodeGenTarget Target(RK);
3031 
3032   if (SelectedCombiners.empty())
3033     PrintFatalError("No combiners selected with -combiners");
3034   for (const auto &Combiner : SelectedCombiners) {
3035     Record *CombinerDef = RK.getDef(Combiner);
3036     if (!CombinerDef)
3037       PrintFatalError("Could not find " + Combiner);
3038     GICombinerEmitter(RK, Target, Combiner, CombinerDef).run(OS);
3039   }
3040 }
3041 
3042 static TableGen::Emitter::Opt X("gen-global-isel-combiner", EmitGICombiner,
3043                                 "Generate GlobalISel Combiner");
3044