1 //===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits a target specifier matcher for converting parsed
11 // assembly operands in the MCInst structures. It also emits a matcher for
12 // custom operand parsing.
13 //
14 // Converting assembly operands into MCInst structures
15 // ---------------------------------------------------
16 //
17 // The input to the target specific matcher is a list of literal tokens and
18 // operands. The target specific parser should generally eliminate any syntax
19 // which is not relevant for matching; for example, comma tokens should have
20 // already been consumed and eliminated by the parser. Most instructions will
21 // end up with a single literal token (the instruction name) and some number of
22 // operands.
23 //
24 // Some example inputs, for X86:
25 //   'addl' (immediate ...) (register ...)
26 //   'add' (immediate ...) (memory ...)
27 //   'call' '*' %epc
28 //
29 // The assembly matcher is responsible for converting this input into a precise
30 // machine instruction (i.e., an instruction with a well defined encoding). This
31 // mapping has several properties which complicate matching:
32 //
33 //  - It may be ambiguous; many architectures can legally encode particular
34 //    variants of an instruction in different ways (for example, using a smaller
35 //    encoding for small immediates). Such ambiguities should never be
36 //    arbitrarily resolved by the assembler, the assembler is always responsible
37 //    for choosing the "best" available instruction.
38 //
39 //  - It may depend on the subtarget or the assembler context. Instructions
40 //    which are invalid for the current mode, but otherwise unambiguous (e.g.,
41 //    an SSE instruction in a file being assembled for i486) should be accepted
42 //    and rejected by the assembler front end. However, if the proper encoding
43 //    for an instruction is dependent on the assembler context then the matcher
44 //    is responsible for selecting the correct machine instruction for the
45 //    current mode.
46 //
47 // The core matching algorithm attempts to exploit the regularity in most
48 // instruction sets to quickly determine the set of possibly matching
49 // instructions, and the simplify the generated code. Additionally, this helps
50 // to ensure that the ambiguities are intentionally resolved by the user.
51 //
52 // The matching is divided into two distinct phases:
53 //
54 //   1. Classification: Each operand is mapped to the unique set which (a)
55 //      contains it, and (b) is the largest such subset for which a single
56 //      instruction could match all members.
57 //
58 //      For register classes, we can generate these subgroups automatically. For
59 //      arbitrary operands, we expect the user to define the classes and their
60 //      relations to one another (for example, 8-bit signed immediates as a
61 //      subset of 32-bit immediates).
62 //
63 //      By partitioning the operands in this way, we guarantee that for any
64 //      tuple of classes, any single instruction must match either all or none
65 //      of the sets of operands which could classify to that tuple.
66 //
67 //      In addition, the subset relation amongst classes induces a partial order
68 //      on such tuples, which we use to resolve ambiguities.
69 //
70 //   2. The input can now be treated as a tuple of classes (static tokens are
71 //      simple singleton sets). Each such tuple should generally map to a single
72 //      instruction (we currently ignore cases where this isn't true, whee!!!),
73 //      which we can emit a simple matcher for.
74 //
75 // Custom Operand Parsing
76 // ----------------------
77 //
78 //  Some targets need a custom way to parse operands, some specific instructions
79 //  can contain arguments that can represent processor flags and other kinds of
80 //  identifiers that need to be mapped to specific values in the final encoded
81 //  instructions. The target specific custom operand parsing works in the
82 //  following way:
83 //
84 //   1. A operand match table is built, each entry contains a mnemonic, an
85 //      operand class, a mask for all operand positions for that same
86 //      class/mnemonic and target features to be checked while trying to match.
87 //
88 //   2. The operand matcher will try every possible entry with the same
89 //      mnemonic and will check if the target feature for this mnemonic also
90 //      matches. After that, if the operand to be matched has its index
91 //      present in the mask, a successful match occurs. Otherwise, fallback
92 //      to the regular operand parsing.
93 //
94 //   3. For a match success, each operand class that has a 'ParserMethod'
95 //      becomes part of a switch from where the custom method is called.
96 //
97 //===----------------------------------------------------------------------===//
98 
99 #include "CodeGenTarget.h"
100 #include "llvm/ADT/CachedHashString.h"
101 #include "llvm/ADT/PointerUnion.h"
102 #include "llvm/ADT/STLExtras.h"
103 #include "llvm/ADT/SmallPtrSet.h"
104 #include "llvm/ADT/SmallVector.h"
105 #include "llvm/ADT/StringExtras.h"
106 #include "llvm/Support/CommandLine.h"
107 #include "llvm/Support/Debug.h"
108 #include "llvm/Support/ErrorHandling.h"
109 #include "llvm/TableGen/Error.h"
110 #include "llvm/TableGen/Record.h"
111 #include "llvm/TableGen/StringMatcher.h"
112 #include "llvm/TableGen/StringToOffsetTable.h"
113 #include "llvm/TableGen/TableGenBackend.h"
114 #include <cassert>
115 #include <cctype>
116 #include <forward_list>
117 #include <map>
118 #include <set>
119 
120 using namespace llvm;
121 
122 #define DEBUG_TYPE "asm-matcher-emitter"
123 
124 static cl::opt<std::string>
125 MatchPrefix("match-prefix", cl::init(""),
126             cl::desc("Only match instructions with the given prefix"));
127 
128 namespace {
129 class AsmMatcherInfo;
130 struct SubtargetFeatureInfo;
131 
132 // Register sets are used as keys in some second-order sets TableGen creates
133 // when generating its data structures. This means that the order of two
134 // RegisterSets can be seen in the outputted AsmMatcher tables occasionally, and
135 // can even affect compiler output (at least seen in diagnostics produced when
136 // all matches fail). So we use a type that sorts them consistently.
137 typedef std::set<Record*, LessRecordByID> RegisterSet;
138 
139 class AsmMatcherEmitter {
140   RecordKeeper &Records;
141 public:
142   AsmMatcherEmitter(RecordKeeper &R) : Records(R) {}
143 
144   void run(raw_ostream &o);
145 };
146 
147 /// ClassInfo - Helper class for storing the information about a particular
148 /// class of operands which can be matched.
149 struct ClassInfo {
150   enum ClassInfoKind {
151     /// Invalid kind, for use as a sentinel value.
152     Invalid = 0,
153 
154     /// The class for a particular token.
155     Token,
156 
157     /// The (first) register class, subsequent register classes are
158     /// RegisterClass0+1, and so on.
159     RegisterClass0,
160 
161     /// The (first) user defined class, subsequent user defined classes are
162     /// UserClass0+1, and so on.
163     UserClass0 = 1<<16
164   };
165 
166   /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
167   /// N) for the Nth user defined class.
168   unsigned Kind;
169 
170   /// SuperClasses - The super classes of this class. Note that for simplicities
171   /// sake user operands only record their immediate super class, while register
172   /// operands include all superclasses.
173   std::vector<ClassInfo*> SuperClasses;
174 
175   /// Name - The full class name, suitable for use in an enum.
176   std::string Name;
177 
178   /// ClassName - The unadorned generic name for this class (e.g., Token).
179   std::string ClassName;
180 
181   /// ValueName - The name of the value this class represents; for a token this
182   /// is the literal token string, for an operand it is the TableGen class (or
183   /// empty if this is a derived class).
184   std::string ValueName;
185 
186   /// PredicateMethod - The name of the operand method to test whether the
187   /// operand matches this class; this is not valid for Token or register kinds.
188   std::string PredicateMethod;
189 
190   /// RenderMethod - The name of the operand method to add this operand to an
191   /// MCInst; this is not valid for Token or register kinds.
192   std::string RenderMethod;
193 
194   /// ParserMethod - The name of the operand method to do a target specific
195   /// parsing on the operand.
196   std::string ParserMethod;
197 
198   /// For register classes: the records for all the registers in this class.
199   RegisterSet Registers;
200 
201   /// For custom match classes: the diagnostic kind for when the predicate fails.
202   std::string DiagnosticType;
203 
204   /// Is this operand optional and not always required.
205   bool IsOptional;
206 
207   /// DefaultMethod - The name of the method that returns the default operand
208   /// for optional operand
209   std::string DefaultMethod;
210 
211 public:
212   /// isRegisterClass() - Check if this is a register class.
213   bool isRegisterClass() const {
214     return Kind >= RegisterClass0 && Kind < UserClass0;
215   }
216 
217   /// isUserClass() - Check if this is a user defined class.
218   bool isUserClass() const {
219     return Kind >= UserClass0;
220   }
221 
222   /// isRelatedTo - Check whether this class is "related" to \p RHS. Classes
223   /// are related if they are in the same class hierarchy.
224   bool isRelatedTo(const ClassInfo &RHS) const {
225     // Tokens are only related to tokens.
226     if (Kind == Token || RHS.Kind == Token)
227       return Kind == Token && RHS.Kind == Token;
228 
229     // Registers classes are only related to registers classes, and only if
230     // their intersection is non-empty.
231     if (isRegisterClass() || RHS.isRegisterClass()) {
232       if (!isRegisterClass() || !RHS.isRegisterClass())
233         return false;
234 
235       RegisterSet Tmp;
236       std::insert_iterator<RegisterSet> II(Tmp, Tmp.begin());
237       std::set_intersection(Registers.begin(), Registers.end(),
238                             RHS.Registers.begin(), RHS.Registers.end(),
239                             II, LessRecordByID());
240 
241       return !Tmp.empty();
242     }
243 
244     // Otherwise we have two users operands; they are related if they are in the
245     // same class hierarchy.
246     //
247     // FIXME: This is an oversimplification, they should only be related if they
248     // intersect, however we don't have that information.
249     assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
250     const ClassInfo *Root = this;
251     while (!Root->SuperClasses.empty())
252       Root = Root->SuperClasses.front();
253 
254     const ClassInfo *RHSRoot = &RHS;
255     while (!RHSRoot->SuperClasses.empty())
256       RHSRoot = RHSRoot->SuperClasses.front();
257 
258     return Root == RHSRoot;
259   }
260 
261   /// isSubsetOf - Test whether this class is a subset of \p RHS.
262   bool isSubsetOf(const ClassInfo &RHS) const {
263     // This is a subset of RHS if it is the same class...
264     if (this == &RHS)
265       return true;
266 
267     // ... or if any of its super classes are a subset of RHS.
268     for (const ClassInfo *CI : SuperClasses)
269       if (CI->isSubsetOf(RHS))
270         return true;
271 
272     return false;
273   }
274 
275   int getTreeDepth() const {
276     int Depth = 0;
277     const ClassInfo *Root = this;
278     while (!Root->SuperClasses.empty()) {
279       Depth++;
280       Root = Root->SuperClasses.front();
281     }
282     return Depth;
283   }
284 
285   const ClassInfo *findRoot() const {
286     const ClassInfo *Root = this;
287     while (!Root->SuperClasses.empty())
288       Root = Root->SuperClasses.front();
289     return Root;
290   }
291 
292   /// Compare two classes. This does not produce a total ordering, but does
293   /// guarantee that subclasses are sorted before their parents, and that the
294   /// ordering is transitive.
295   bool operator<(const ClassInfo &RHS) const {
296     if (this == &RHS)
297       return false;
298 
299     // First, enforce the ordering between the three different types of class.
300     // Tokens sort before registers, which sort before user classes.
301     if (Kind == Token) {
302       if (RHS.Kind != Token)
303         return true;
304       assert(RHS.Kind == Token);
305     } else if (isRegisterClass()) {
306       if (RHS.Kind == Token)
307         return false;
308       else if (RHS.isUserClass())
309         return true;
310       assert(RHS.isRegisterClass());
311     } else if (isUserClass()) {
312       if (!RHS.isUserClass())
313         return false;
314       assert(RHS.isUserClass());
315     } else {
316       llvm_unreachable("Unknown ClassInfoKind");
317     }
318 
319     if (Kind == Token || isUserClass()) {
320       // Related tokens and user classes get sorted by depth in the inheritence
321       // tree (so that subclasses are before their parents).
322       if (isRelatedTo(RHS)) {
323         if (getTreeDepth() > RHS.getTreeDepth())
324           return true;
325         if (getTreeDepth() < RHS.getTreeDepth())
326           return false;
327       } else {
328         // Unrelated tokens and user classes are ordered by the name of their
329         // root nodes, so that there is a consistent ordering between
330         // unconnected trees.
331         return findRoot()->ValueName < RHS.findRoot()->ValueName;
332       }
333     } else if (isRegisterClass()) {
334       // For register sets, sort by number of registers. This guarantees that
335       // a set will always sort before all of it's strict supersets.
336       if (Registers.size() != RHS.Registers.size())
337         return Registers.size() < RHS.Registers.size();
338     } else {
339       llvm_unreachable("Unknown ClassInfoKind");
340     }
341 
342     // FIXME: We should be able to just return false here, as we only need a
343     // partial order (we use stable sorts, so this is deterministic) and the
344     // name of a class shouldn't be significant. However, some of the backends
345     // accidentally rely on this behaviour, so it will have to stay like this
346     // until they are fixed.
347     return ValueName < RHS.ValueName;
348   }
349 };
350 
351 class AsmVariantInfo {
352 public:
353   std::string RegisterPrefix;
354   std::string TokenizingCharacters;
355   std::string SeparatorCharacters;
356   std::string BreakCharacters;
357   std::string Name;
358   int AsmVariantNo;
359 };
360 
361 /// MatchableInfo - Helper class for storing the necessary information for an
362 /// instruction or alias which is capable of being matched.
363 struct MatchableInfo {
364   struct AsmOperand {
365     /// Token - This is the token that the operand came from.
366     StringRef Token;
367 
368     /// The unique class instance this operand should match.
369     ClassInfo *Class;
370 
371     /// The operand name this is, if anything.
372     StringRef SrcOpName;
373 
374     /// The suboperand index within SrcOpName, or -1 for the entire operand.
375     int SubOpIdx;
376 
377     /// Whether the token is "isolated", i.e., it is preceded and followed
378     /// by separators.
379     bool IsIsolatedToken;
380 
381     /// Register record if this token is singleton register.
382     Record *SingletonReg;
383 
384     explicit AsmOperand(bool IsIsolatedToken, StringRef T)
385         : Token(T), Class(nullptr), SubOpIdx(-1),
386           IsIsolatedToken(IsIsolatedToken), SingletonReg(nullptr) {}
387   };
388 
389   /// ResOperand - This represents a single operand in the result instruction
390   /// generated by the match.  In cases (like addressing modes) where a single
391   /// assembler operand expands to multiple MCOperands, this represents the
392   /// single assembler operand, not the MCOperand.
393   struct ResOperand {
394     enum {
395       /// RenderAsmOperand - This represents an operand result that is
396       /// generated by calling the render method on the assembly operand.  The
397       /// corresponding AsmOperand is specified by AsmOperandNum.
398       RenderAsmOperand,
399 
400       /// TiedOperand - This represents a result operand that is a duplicate of
401       /// a previous result operand.
402       TiedOperand,
403 
404       /// ImmOperand - This represents an immediate value that is dumped into
405       /// the operand.
406       ImmOperand,
407 
408       /// RegOperand - This represents a fixed register that is dumped in.
409       RegOperand
410     } Kind;
411 
412     union {
413       /// This is the operand # in the AsmOperands list that this should be
414       /// copied from.
415       unsigned AsmOperandNum;
416 
417       /// TiedOperandNum - This is the (earlier) result operand that should be
418       /// copied from.
419       unsigned TiedOperandNum;
420 
421       /// ImmVal - This is the immediate value added to the instruction.
422       int64_t ImmVal;
423 
424       /// Register - This is the register record.
425       Record *Register;
426     };
427 
428     /// MINumOperands - The number of MCInst operands populated by this
429     /// operand.
430     unsigned MINumOperands;
431 
432     static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
433       ResOperand X;
434       X.Kind = RenderAsmOperand;
435       X.AsmOperandNum = AsmOpNum;
436       X.MINumOperands = NumOperands;
437       return X;
438     }
439 
440     static ResOperand getTiedOp(unsigned TiedOperandNum) {
441       ResOperand X;
442       X.Kind = TiedOperand;
443       X.TiedOperandNum = TiedOperandNum;
444       X.MINumOperands = 1;
445       return X;
446     }
447 
448     static ResOperand getImmOp(int64_t Val) {
449       ResOperand X;
450       X.Kind = ImmOperand;
451       X.ImmVal = Val;
452       X.MINumOperands = 1;
453       return X;
454     }
455 
456     static ResOperand getRegOp(Record *Reg) {
457       ResOperand X;
458       X.Kind = RegOperand;
459       X.Register = Reg;
460       X.MINumOperands = 1;
461       return X;
462     }
463   };
464 
465   /// AsmVariantID - Target's assembly syntax variant no.
466   int AsmVariantID;
467 
468   /// AsmString - The assembly string for this instruction (with variants
469   /// removed), e.g. "movsx $src, $dst".
470   std::string AsmString;
471 
472   /// TheDef - This is the definition of the instruction or InstAlias that this
473   /// matchable came from.
474   Record *const TheDef;
475 
476   /// DefRec - This is the definition that it came from.
477   PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
478 
479   const CodeGenInstruction *getResultInst() const {
480     if (DefRec.is<const CodeGenInstruction*>())
481       return DefRec.get<const CodeGenInstruction*>();
482     return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
483   }
484 
485   /// ResOperands - This is the operand list that should be built for the result
486   /// MCInst.
487   SmallVector<ResOperand, 8> ResOperands;
488 
489   /// Mnemonic - This is the first token of the matched instruction, its
490   /// mnemonic.
491   StringRef Mnemonic;
492 
493   /// AsmOperands - The textual operands that this instruction matches,
494   /// annotated with a class and where in the OperandList they were defined.
495   /// This directly corresponds to the tokenized AsmString after the mnemonic is
496   /// removed.
497   SmallVector<AsmOperand, 8> AsmOperands;
498 
499   /// Predicates - The required subtarget features to match this instruction.
500   SmallVector<const SubtargetFeatureInfo *, 4> RequiredFeatures;
501 
502   /// ConversionFnKind - The enum value which is passed to the generated
503   /// convertToMCInst to convert parsed operands into an MCInst for this
504   /// function.
505   std::string ConversionFnKind;
506 
507   /// If this instruction is deprecated in some form.
508   bool HasDeprecation;
509 
510   /// If this is an alias, this is use to determine whether or not to using
511   /// the conversion function defined by the instruction's AsmMatchConverter
512   /// or to use the function generated by the alias.
513   bool UseInstAsmMatchConverter;
514 
515   MatchableInfo(const CodeGenInstruction &CGI)
516     : AsmVariantID(0), AsmString(CGI.AsmString), TheDef(CGI.TheDef), DefRec(&CGI),
517       UseInstAsmMatchConverter(true) {
518   }
519 
520   MatchableInfo(std::unique_ptr<const CodeGenInstAlias> Alias)
521     : AsmVariantID(0), AsmString(Alias->AsmString), TheDef(Alias->TheDef),
522       DefRec(Alias.release()),
523       UseInstAsmMatchConverter(
524         TheDef->getValueAsBit("UseInstAsmMatchConverter")) {
525   }
526 
527   // Could remove this and the dtor if PointerUnion supported unique_ptr
528   // elements with a dynamic failure/assertion (like the one below) in the case
529   // where it was copied while being in an owning state.
530   MatchableInfo(const MatchableInfo &RHS)
531       : AsmVariantID(RHS.AsmVariantID), AsmString(RHS.AsmString),
532         TheDef(RHS.TheDef), DefRec(RHS.DefRec), ResOperands(RHS.ResOperands),
533         Mnemonic(RHS.Mnemonic), AsmOperands(RHS.AsmOperands),
534         RequiredFeatures(RHS.RequiredFeatures),
535         ConversionFnKind(RHS.ConversionFnKind),
536         HasDeprecation(RHS.HasDeprecation),
537         UseInstAsmMatchConverter(RHS.UseInstAsmMatchConverter) {
538     assert(!DefRec.is<const CodeGenInstAlias *>());
539   }
540 
541   ~MatchableInfo() {
542     delete DefRec.dyn_cast<const CodeGenInstAlias*>();
543   }
544 
545   // Two-operand aliases clone from the main matchable, but mark the second
546   // operand as a tied operand of the first for purposes of the assembler.
547   void formTwoOperandAlias(StringRef Constraint);
548 
549   void initialize(const AsmMatcherInfo &Info,
550                   SmallPtrSetImpl<Record*> &SingletonRegisters,
551                   AsmVariantInfo const &Variant,
552                   bool HasMnemonicFirst);
553 
554   /// validate - Return true if this matchable is a valid thing to match against
555   /// and perform a bunch of validity checking.
556   bool validate(StringRef CommentDelimiter, bool Hack) const;
557 
558   /// findAsmOperand - Find the AsmOperand with the specified name and
559   /// suboperand index.
560   int findAsmOperand(StringRef N, int SubOpIdx) const {
561     auto I = find_if(AsmOperands, [&](const AsmOperand &Op) {
562       return Op.SrcOpName == N && Op.SubOpIdx == SubOpIdx;
563     });
564     return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1;
565   }
566 
567   /// findAsmOperandNamed - Find the first AsmOperand with the specified name.
568   /// This does not check the suboperand index.
569   int findAsmOperandNamed(StringRef N) const {
570     auto I = find_if(AsmOperands,
571                      [&](const AsmOperand &Op) { return Op.SrcOpName == N; });
572     return (I != AsmOperands.end()) ? I - AsmOperands.begin() : -1;
573   }
574 
575   void buildInstructionResultOperands();
576   void buildAliasResultOperands();
577 
578   /// operator< - Compare two matchables.
579   bool operator<(const MatchableInfo &RHS) const {
580     // The primary comparator is the instruction mnemonic.
581     if (int Cmp = Mnemonic.compare(RHS.Mnemonic))
582       return Cmp == -1;
583 
584     if (AsmOperands.size() != RHS.AsmOperands.size())
585       return AsmOperands.size() < RHS.AsmOperands.size();
586 
587     // Compare lexicographically by operand. The matcher validates that other
588     // orderings wouldn't be ambiguous using \see couldMatchAmbiguouslyWith().
589     for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
590       if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
591         return true;
592       if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
593         return false;
594     }
595 
596     // Give matches that require more features higher precedence. This is useful
597     // because we cannot define AssemblerPredicates with the negation of
598     // processor features. For example, ARM v6 "nop" may be either a HINT or
599     // MOV. With v6, we want to match HINT. The assembler has no way to
600     // predicate MOV under "NoV6", but HINT will always match first because it
601     // requires V6 while MOV does not.
602     if (RequiredFeatures.size() != RHS.RequiredFeatures.size())
603       return RequiredFeatures.size() > RHS.RequiredFeatures.size();
604 
605     return false;
606   }
607 
608   /// couldMatchAmbiguouslyWith - Check whether this matchable could
609   /// ambiguously match the same set of operands as \p RHS (without being a
610   /// strictly superior match).
611   bool couldMatchAmbiguouslyWith(const MatchableInfo &RHS) const {
612     // The primary comparator is the instruction mnemonic.
613     if (Mnemonic != RHS.Mnemonic)
614       return false;
615 
616     // The number of operands is unambiguous.
617     if (AsmOperands.size() != RHS.AsmOperands.size())
618       return false;
619 
620     // Otherwise, make sure the ordering of the two instructions is unambiguous
621     // by checking that either (a) a token or operand kind discriminates them,
622     // or (b) the ordering among equivalent kinds is consistent.
623 
624     // Tokens and operand kinds are unambiguous (assuming a correct target
625     // specific parser).
626     for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
627       if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
628           AsmOperands[i].Class->Kind == ClassInfo::Token)
629         if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
630             *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
631           return false;
632 
633     // Otherwise, this operand could commute if all operands are equivalent, or
634     // there is a pair of operands that compare less than and a pair that
635     // compare greater than.
636     bool HasLT = false, HasGT = false;
637     for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
638       if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
639         HasLT = true;
640       if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
641         HasGT = true;
642     }
643 
644     return HasLT == HasGT;
645   }
646 
647   void dump() const;
648 
649 private:
650   void tokenizeAsmString(AsmMatcherInfo const &Info,
651                          AsmVariantInfo const &Variant);
652   void addAsmOperand(StringRef Token, bool IsIsolatedToken = false);
653 };
654 
655 /// SubtargetFeatureInfo - Helper class for storing information on a subtarget
656 /// feature which participates in instruction matching.
657 struct SubtargetFeatureInfo {
658   /// \brief The predicate record for this feature.
659   Record *TheDef;
660 
661   /// \brief An unique index assigned to represent this feature.
662   uint64_t Index;
663 
664   SubtargetFeatureInfo(Record *D, uint64_t Idx) : TheDef(D), Index(Idx) {}
665 
666   /// \brief The name of the enumerated constant identifying this feature.
667   std::string getEnumName() const {
668     return "Feature_" + TheDef->getName();
669   }
670 
671   void dump() const {
672     errs() << getEnumName() << " " << Index << "\n";
673     TheDef->dump();
674   }
675 };
676 
677 struct OperandMatchEntry {
678   unsigned OperandMask;
679   const MatchableInfo* MI;
680   ClassInfo *CI;
681 
682   static OperandMatchEntry create(const MatchableInfo *mi, ClassInfo *ci,
683                                   unsigned opMask) {
684     OperandMatchEntry X;
685     X.OperandMask = opMask;
686     X.CI = ci;
687     X.MI = mi;
688     return X;
689   }
690 };
691 
692 class AsmMatcherInfo {
693 public:
694   /// Tracked Records
695   RecordKeeper &Records;
696 
697   /// The tablegen AsmParser record.
698   Record *AsmParser;
699 
700   /// Target - The target information.
701   CodeGenTarget &Target;
702 
703   /// The classes which are needed for matching.
704   std::forward_list<ClassInfo> Classes;
705 
706   /// The information on the matchables to match.
707   std::vector<std::unique_ptr<MatchableInfo>> Matchables;
708 
709   /// Info for custom matching operands by user defined methods.
710   std::vector<OperandMatchEntry> OperandMatchInfo;
711 
712   /// Map of Register records to their class information.
713   typedef std::map<Record*, ClassInfo*, LessRecordByID> RegisterClassesTy;
714   RegisterClassesTy RegisterClasses;
715 
716   /// Map of Predicate records to their subtarget information.
717   std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures;
718 
719   /// Map of AsmOperandClass records to their class information.
720   std::map<Record*, ClassInfo*> AsmOperandClasses;
721 
722 private:
723   /// Map of token to class information which has already been constructed.
724   std::map<std::string, ClassInfo*> TokenClasses;
725 
726   /// Map of RegisterClass records to their class information.
727   std::map<Record*, ClassInfo*> RegisterClassClasses;
728 
729 private:
730   /// getTokenClass - Lookup or create the class for the given token.
731   ClassInfo *getTokenClass(StringRef Token);
732 
733   /// getOperandClass - Lookup or create the class for the given operand.
734   ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
735                              int SubOpIdx);
736   ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
737 
738   /// buildRegisterClasses - Build the ClassInfo* instances for register
739   /// classes.
740   void buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters);
741 
742   /// buildOperandClasses - Build the ClassInfo* instances for user defined
743   /// operand classes.
744   void buildOperandClasses();
745 
746   void buildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
747                                         unsigned AsmOpIdx);
748   void buildAliasOperandReference(MatchableInfo *II, StringRef OpName,
749                                   MatchableInfo::AsmOperand &Op);
750 
751 public:
752   AsmMatcherInfo(Record *AsmParser,
753                  CodeGenTarget &Target,
754                  RecordKeeper &Records);
755 
756   /// buildInfo - Construct the various tables used during matching.
757   void buildInfo();
758 
759   /// buildOperandMatchInfo - Build the necessary information to handle user
760   /// defined operand parsing methods.
761   void buildOperandMatchInfo();
762 
763   /// getSubtargetFeature - Lookup or create the subtarget feature info for the
764   /// given operand.
765   const SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
766     assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
767     const auto &I = SubtargetFeatures.find(Def);
768     return I == SubtargetFeatures.end() ? nullptr : &I->second;
769   }
770 
771   RecordKeeper &getRecords() const {
772     return Records;
773   }
774 
775   bool hasOptionalOperands() const {
776     return find_if(Classes, [](const ClassInfo &Class) {
777              return Class.IsOptional;
778            }) != Classes.end();
779   }
780 };
781 
782 } // end anonymous namespace
783 
784 void MatchableInfo::dump() const {
785   errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
786 
787   for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
788     const AsmOperand &Op = AsmOperands[i];
789     errs() << "  op[" << i << "] = " << Op.Class->ClassName << " - ";
790     errs() << '\"' << Op.Token << "\"\n";
791   }
792 }
793 
794 static std::pair<StringRef, StringRef>
795 parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
796   // Split via the '='.
797   std::pair<StringRef, StringRef> Ops = S.split('=');
798   if (Ops.second == "")
799     PrintFatalError(Loc, "missing '=' in two-operand alias constraint");
800   // Trim whitespace and the leading '$' on the operand names.
801   size_t start = Ops.first.find_first_of('$');
802   if (start == std::string::npos)
803     PrintFatalError(Loc, "expected '$' prefix on asm operand name");
804   Ops.first = Ops.first.slice(start + 1, std::string::npos);
805   size_t end = Ops.first.find_last_of(" \t");
806   Ops.first = Ops.first.slice(0, end);
807   // Now the second operand.
808   start = Ops.second.find_first_of('$');
809   if (start == std::string::npos)
810     PrintFatalError(Loc, "expected '$' prefix on asm operand name");
811   Ops.second = Ops.second.slice(start + 1, std::string::npos);
812   end = Ops.second.find_last_of(" \t");
813   Ops.first = Ops.first.slice(0, end);
814   return Ops;
815 }
816 
817 void MatchableInfo::formTwoOperandAlias(StringRef Constraint) {
818   // Figure out which operands are aliased and mark them as tied.
819   std::pair<StringRef, StringRef> Ops =
820     parseTwoOperandConstraint(Constraint, TheDef->getLoc());
821 
822   // Find the AsmOperands that refer to the operands we're aliasing.
823   int SrcAsmOperand = findAsmOperandNamed(Ops.first);
824   int DstAsmOperand = findAsmOperandNamed(Ops.second);
825   if (SrcAsmOperand == -1)
826     PrintFatalError(TheDef->getLoc(),
827                     "unknown source two-operand alias operand '" + Ops.first +
828                     "'.");
829   if (DstAsmOperand == -1)
830     PrintFatalError(TheDef->getLoc(),
831                     "unknown destination two-operand alias operand '" +
832                     Ops.second + "'.");
833 
834   // Find the ResOperand that refers to the operand we're aliasing away
835   // and update it to refer to the combined operand instead.
836   for (ResOperand &Op : ResOperands) {
837     if (Op.Kind == ResOperand::RenderAsmOperand &&
838         Op.AsmOperandNum == (unsigned)SrcAsmOperand) {
839       Op.AsmOperandNum = DstAsmOperand;
840       break;
841     }
842   }
843   // Remove the AsmOperand for the alias operand.
844   AsmOperands.erase(AsmOperands.begin() + SrcAsmOperand);
845   // Adjust the ResOperand references to any AsmOperands that followed
846   // the one we just deleted.
847   for (ResOperand &Op : ResOperands) {
848     switch(Op.Kind) {
849     default:
850       // Nothing to do for operands that don't reference AsmOperands.
851       break;
852     case ResOperand::RenderAsmOperand:
853       if (Op.AsmOperandNum > (unsigned)SrcAsmOperand)
854         --Op.AsmOperandNum;
855       break;
856     case ResOperand::TiedOperand:
857       if (Op.TiedOperandNum > (unsigned)SrcAsmOperand)
858         --Op.TiedOperandNum;
859       break;
860     }
861   }
862 }
863 
864 /// extractSingletonRegisterForAsmOperand - Extract singleton register,
865 /// if present, from specified token.
866 static void
867 extractSingletonRegisterForAsmOperand(MatchableInfo::AsmOperand &Op,
868                                       const AsmMatcherInfo &Info,
869                                       StringRef RegisterPrefix) {
870   StringRef Tok = Op.Token;
871 
872   // If this token is not an isolated token, i.e., it isn't separated from
873   // other tokens (e.g. with whitespace), don't interpret it as a register name.
874   if (!Op.IsIsolatedToken)
875     return;
876 
877   if (RegisterPrefix.empty()) {
878     std::string LoweredTok = Tok.lower();
879     if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok))
880       Op.SingletonReg = Reg->TheDef;
881     return;
882   }
883 
884   if (!Tok.startswith(RegisterPrefix))
885     return;
886 
887   StringRef RegName = Tok.substr(RegisterPrefix.size());
888   if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
889     Op.SingletonReg = Reg->TheDef;
890 
891   // If there is no register prefix (i.e. "%" in "%eax"), then this may
892   // be some random non-register token, just ignore it.
893 }
894 
895 void MatchableInfo::initialize(const AsmMatcherInfo &Info,
896                                SmallPtrSetImpl<Record*> &SingletonRegisters,
897                                AsmVariantInfo const &Variant,
898                                bool HasMnemonicFirst) {
899   AsmVariantID = Variant.AsmVariantNo;
900   AsmString =
901     CodeGenInstruction::FlattenAsmStringVariants(AsmString,
902                                                  Variant.AsmVariantNo);
903 
904   tokenizeAsmString(Info, Variant);
905 
906   // The first token of the instruction is the mnemonic, which must be a
907   // simple string, not a $foo variable or a singleton register.
908   if (AsmOperands.empty())
909     PrintFatalError(TheDef->getLoc(),
910                   "Instruction '" + TheDef->getName() + "' has no tokens");
911 
912   assert(!AsmOperands[0].Token.empty());
913   if (HasMnemonicFirst) {
914     Mnemonic = AsmOperands[0].Token;
915     if (Mnemonic[0] == '$')
916       PrintFatalError(TheDef->getLoc(),
917                       "Invalid instruction mnemonic '" + Mnemonic + "'!");
918 
919     // Remove the first operand, it is tracked in the mnemonic field.
920     AsmOperands.erase(AsmOperands.begin());
921   } else if (AsmOperands[0].Token[0] != '$')
922     Mnemonic = AsmOperands[0].Token;
923 
924   // Compute the require features.
925   for (Record *Predicate : TheDef->getValueAsListOfDefs("Predicates"))
926     if (const SubtargetFeatureInfo *Feature =
927             Info.getSubtargetFeature(Predicate))
928       RequiredFeatures.push_back(Feature);
929 
930   // Collect singleton registers, if used.
931   for (MatchableInfo::AsmOperand &Op : AsmOperands) {
932     extractSingletonRegisterForAsmOperand(Op, Info, Variant.RegisterPrefix);
933     if (Record *Reg = Op.SingletonReg)
934       SingletonRegisters.insert(Reg);
935   }
936 
937   const RecordVal *DepMask = TheDef->getValue("DeprecatedFeatureMask");
938   if (!DepMask)
939     DepMask = TheDef->getValue("ComplexDeprecationPredicate");
940 
941   HasDeprecation =
942       DepMask ? !DepMask->getValue()->getAsUnquotedString().empty() : false;
943 }
944 
945 /// Append an AsmOperand for the given substring of AsmString.
946 void MatchableInfo::addAsmOperand(StringRef Token, bool IsIsolatedToken) {
947   AsmOperands.push_back(AsmOperand(IsIsolatedToken, Token));
948 }
949 
950 /// tokenizeAsmString - Tokenize a simplified assembly string.
951 void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info,
952                                       AsmVariantInfo const &Variant) {
953   StringRef String = AsmString;
954   size_t Prev = 0;
955   bool InTok = false;
956   bool IsIsolatedToken = true;
957   for (size_t i = 0, e = String.size(); i != e; ++i) {
958     char Char = String[i];
959     if (Variant.BreakCharacters.find(Char) != std::string::npos) {
960       if (InTok) {
961         addAsmOperand(String.slice(Prev, i), false);
962         Prev = i;
963         IsIsolatedToken = false;
964       }
965       InTok = true;
966       continue;
967     }
968     if (Variant.TokenizingCharacters.find(Char) != std::string::npos) {
969       if (InTok) {
970         addAsmOperand(String.slice(Prev, i), IsIsolatedToken);
971         InTok = false;
972         IsIsolatedToken = false;
973       }
974       addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
975       Prev = i + 1;
976       IsIsolatedToken = true;
977       continue;
978     }
979     if (Variant.SeparatorCharacters.find(Char) != std::string::npos) {
980       if (InTok) {
981         addAsmOperand(String.slice(Prev, i), IsIsolatedToken);
982         InTok = false;
983       }
984       Prev = i + 1;
985       IsIsolatedToken = true;
986       continue;
987     }
988 
989     switch (Char) {
990     case '\\':
991       if (InTok) {
992         addAsmOperand(String.slice(Prev, i), false);
993         InTok = false;
994         IsIsolatedToken = false;
995       }
996       ++i;
997       assert(i != String.size() && "Invalid quoted character");
998       addAsmOperand(String.slice(i, i + 1), IsIsolatedToken);
999       Prev = i + 1;
1000       IsIsolatedToken = false;
1001       break;
1002 
1003     case '$': {
1004       if (InTok) {
1005         addAsmOperand(String.slice(Prev, i), false);
1006         InTok = false;
1007         IsIsolatedToken = false;
1008       }
1009 
1010       // If this isn't "${", start new identifier looking like "$xxx"
1011       if (i + 1 == String.size() || String[i + 1] != '{') {
1012         Prev = i;
1013         break;
1014       }
1015 
1016       size_t EndPos = String.find('}', i);
1017       assert(EndPos != StringRef::npos &&
1018              "Missing brace in operand reference!");
1019       addAsmOperand(String.slice(i, EndPos+1), IsIsolatedToken);
1020       Prev = EndPos + 1;
1021       i = EndPos;
1022       IsIsolatedToken = false;
1023       break;
1024     }
1025 
1026     default:
1027       InTok = true;
1028       break;
1029     }
1030   }
1031   if (InTok && Prev != String.size())
1032     addAsmOperand(String.substr(Prev), IsIsolatedToken);
1033 }
1034 
1035 bool MatchableInfo::validate(StringRef CommentDelimiter, bool Hack) const {
1036   // Reject matchables with no .s string.
1037   if (AsmString.empty())
1038     PrintFatalError(TheDef->getLoc(), "instruction with empty asm string");
1039 
1040   // Reject any matchables with a newline in them, they should be marked
1041   // isCodeGenOnly if they are pseudo instructions.
1042   if (AsmString.find('\n') != std::string::npos)
1043     PrintFatalError(TheDef->getLoc(),
1044                   "multiline instruction is not valid for the asmparser, "
1045                   "mark it isCodeGenOnly");
1046 
1047   // Remove comments from the asm string.  We know that the asmstring only
1048   // has one line.
1049   if (!CommentDelimiter.empty() &&
1050       StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
1051     PrintFatalError(TheDef->getLoc(),
1052                   "asmstring for instruction has comment character in it, "
1053                   "mark it isCodeGenOnly");
1054 
1055   // Reject matchables with operand modifiers, these aren't something we can
1056   // handle, the target should be refactored to use operands instead of
1057   // modifiers.
1058   //
1059   // Also, check for instructions which reference the operand multiple times;
1060   // this implies a constraint we would not honor.
1061   std::set<std::string> OperandNames;
1062   for (const AsmOperand &Op : AsmOperands) {
1063     StringRef Tok = Op.Token;
1064     if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
1065       PrintFatalError(TheDef->getLoc(),
1066                       "matchable with operand modifier '" + Tok +
1067                       "' not supported by asm matcher.  Mark isCodeGenOnly!");
1068 
1069     // Verify that any operand is only mentioned once.
1070     // We reject aliases and ignore instructions for now.
1071     if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
1072       if (!Hack)
1073         PrintFatalError(TheDef->getLoc(),
1074                         "ERROR: matchable with tied operand '" + Tok +
1075                         "' can never be matched!");
1076       // FIXME: Should reject these.  The ARM backend hits this with $lane in a
1077       // bunch of instructions.  It is unclear what the right answer is.
1078       DEBUG({
1079         errs() << "warning: '" << TheDef->getName() << "': "
1080                << "ignoring instruction with tied operand '"
1081                << Tok << "'\n";
1082       });
1083       return false;
1084     }
1085   }
1086 
1087   return true;
1088 }
1089 
1090 static std::string getEnumNameForToken(StringRef Str) {
1091   std::string Res;
1092 
1093   for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
1094     switch (*it) {
1095     case '*': Res += "_STAR_"; break;
1096     case '%': Res += "_PCT_"; break;
1097     case ':': Res += "_COLON_"; break;
1098     case '!': Res += "_EXCLAIM_"; break;
1099     case '.': Res += "_DOT_"; break;
1100     case '<': Res += "_LT_"; break;
1101     case '>': Res += "_GT_"; break;
1102     case '-': Res += "_MINUS_"; break;
1103     default:
1104       if ((*it >= 'A' && *it <= 'Z') ||
1105           (*it >= 'a' && *it <= 'z') ||
1106           (*it >= '0' && *it <= '9'))
1107         Res += *it;
1108       else
1109         Res += "_" + utostr((unsigned) *it) + "_";
1110     }
1111   }
1112 
1113   return Res;
1114 }
1115 
1116 ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
1117   ClassInfo *&Entry = TokenClasses[Token];
1118 
1119   if (!Entry) {
1120     Classes.emplace_front();
1121     Entry = &Classes.front();
1122     Entry->Kind = ClassInfo::Token;
1123     Entry->ClassName = "Token";
1124     Entry->Name = "MCK_" + getEnumNameForToken(Token);
1125     Entry->ValueName = Token;
1126     Entry->PredicateMethod = "<invalid>";
1127     Entry->RenderMethod = "<invalid>";
1128     Entry->ParserMethod = "";
1129     Entry->DiagnosticType = "";
1130     Entry->IsOptional = false;
1131     Entry->DefaultMethod = "<invalid>";
1132   }
1133 
1134   return Entry;
1135 }
1136 
1137 ClassInfo *
1138 AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
1139                                 int SubOpIdx) {
1140   Record *Rec = OI.Rec;
1141   if (SubOpIdx != -1)
1142     Rec = cast<DefInit>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
1143   return getOperandClass(Rec, SubOpIdx);
1144 }
1145 
1146 ClassInfo *
1147 AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
1148   if (Rec->isSubClassOf("RegisterOperand")) {
1149     // RegisterOperand may have an associated ParserMatchClass. If it does,
1150     // use it, else just fall back to the underlying register class.
1151     const RecordVal *R = Rec->getValue("ParserMatchClass");
1152     if (!R || !R->getValue())
1153       PrintFatalError("Record `" + Rec->getName() +
1154         "' does not have a ParserMatchClass!\n");
1155 
1156     if (DefInit *DI= dyn_cast<DefInit>(R->getValue())) {
1157       Record *MatchClass = DI->getDef();
1158       if (ClassInfo *CI = AsmOperandClasses[MatchClass])
1159         return CI;
1160     }
1161 
1162     // No custom match class. Just use the register class.
1163     Record *ClassRec = Rec->getValueAsDef("RegClass");
1164     if (!ClassRec)
1165       PrintFatalError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
1166                     "' has no associated register class!\n");
1167     if (ClassInfo *CI = RegisterClassClasses[ClassRec])
1168       return CI;
1169     PrintFatalError(Rec->getLoc(), "register class has no class info!");
1170   }
1171 
1172   if (Rec->isSubClassOf("RegisterClass")) {
1173     if (ClassInfo *CI = RegisterClassClasses[Rec])
1174       return CI;
1175     PrintFatalError(Rec->getLoc(), "register class has no class info!");
1176   }
1177 
1178   if (!Rec->isSubClassOf("Operand"))
1179     PrintFatalError(Rec->getLoc(), "Operand `" + Rec->getName() +
1180                   "' does not derive from class Operand!\n");
1181   Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1182   if (ClassInfo *CI = AsmOperandClasses[MatchClass])
1183     return CI;
1184 
1185   PrintFatalError(Rec->getLoc(), "operand has no match class!");
1186 }
1187 
1188 struct LessRegisterSet {
1189   bool operator() (const RegisterSet &LHS, const RegisterSet & RHS) const {
1190     // std::set<T> defines its own compariso "operator<", but it
1191     // performs a lexicographical comparison by T's innate comparison
1192     // for some reason. We don't want non-deterministic pointer
1193     // comparisons so use this instead.
1194     return std::lexicographical_compare(LHS.begin(), LHS.end(),
1195                                         RHS.begin(), RHS.end(),
1196                                         LessRecordByID());
1197   }
1198 };
1199 
1200 void AsmMatcherInfo::
1201 buildRegisterClasses(SmallPtrSetImpl<Record*> &SingletonRegisters) {
1202   const auto &Registers = Target.getRegBank().getRegisters();
1203   auto &RegClassList = Target.getRegBank().getRegClasses();
1204 
1205   typedef std::set<RegisterSet, LessRegisterSet> RegisterSetSet;
1206 
1207   // The register sets used for matching.
1208   RegisterSetSet RegisterSets;
1209 
1210   // Gather the defined sets.
1211   for (const CodeGenRegisterClass &RC : RegClassList)
1212     RegisterSets.insert(
1213         RegisterSet(RC.getOrder().begin(), RC.getOrder().end()));
1214 
1215   // Add any required singleton sets.
1216   for (Record *Rec : SingletonRegisters) {
1217     RegisterSets.insert(RegisterSet(&Rec, &Rec + 1));
1218   }
1219 
1220   // Introduce derived sets where necessary (when a register does not determine
1221   // a unique register set class), and build the mapping of registers to the set
1222   // they should classify to.
1223   std::map<Record*, RegisterSet> RegisterMap;
1224   for (const CodeGenRegister &CGR : Registers) {
1225     // Compute the intersection of all sets containing this register.
1226     RegisterSet ContainingSet;
1227 
1228     for (const RegisterSet &RS : RegisterSets) {
1229       if (!RS.count(CGR.TheDef))
1230         continue;
1231 
1232       if (ContainingSet.empty()) {
1233         ContainingSet = RS;
1234         continue;
1235       }
1236 
1237       RegisterSet Tmp;
1238       std::swap(Tmp, ContainingSet);
1239       std::insert_iterator<RegisterSet> II(ContainingSet,
1240                                            ContainingSet.begin());
1241       std::set_intersection(Tmp.begin(), Tmp.end(), RS.begin(), RS.end(), II,
1242                             LessRecordByID());
1243     }
1244 
1245     if (!ContainingSet.empty()) {
1246       RegisterSets.insert(ContainingSet);
1247       RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
1248     }
1249   }
1250 
1251   // Construct the register classes.
1252   std::map<RegisterSet, ClassInfo*, LessRegisterSet> RegisterSetClasses;
1253   unsigned Index = 0;
1254   for (const RegisterSet &RS : RegisterSets) {
1255     Classes.emplace_front();
1256     ClassInfo *CI = &Classes.front();
1257     CI->Kind = ClassInfo::RegisterClass0 + Index;
1258     CI->ClassName = "Reg" + utostr(Index);
1259     CI->Name = "MCK_Reg" + utostr(Index);
1260     CI->ValueName = "";
1261     CI->PredicateMethod = ""; // unused
1262     CI->RenderMethod = "addRegOperands";
1263     CI->Registers = RS;
1264     // FIXME: diagnostic type.
1265     CI->DiagnosticType = "";
1266     CI->IsOptional = false;
1267     CI->DefaultMethod = ""; // unused
1268     RegisterSetClasses.insert(std::make_pair(RS, CI));
1269     ++Index;
1270   }
1271 
1272   // Find the superclasses; we could compute only the subgroup lattice edges,
1273   // but there isn't really a point.
1274   for (const RegisterSet &RS : RegisterSets) {
1275     ClassInfo *CI = RegisterSetClasses[RS];
1276     for (const RegisterSet &RS2 : RegisterSets)
1277       if (RS != RS2 &&
1278           std::includes(RS2.begin(), RS2.end(), RS.begin(), RS.end(),
1279                         LessRecordByID()))
1280         CI->SuperClasses.push_back(RegisterSetClasses[RS2]);
1281   }
1282 
1283   // Name the register classes which correspond to a user defined RegisterClass.
1284   for (const CodeGenRegisterClass &RC : RegClassList) {
1285     // Def will be NULL for non-user defined register classes.
1286     Record *Def = RC.getDef();
1287     if (!Def)
1288       continue;
1289     ClassInfo *CI = RegisterSetClasses[RegisterSet(RC.getOrder().begin(),
1290                                                    RC.getOrder().end())];
1291     if (CI->ValueName.empty()) {
1292       CI->ClassName = RC.getName();
1293       CI->Name = "MCK_" + RC.getName();
1294       CI->ValueName = RC.getName();
1295     } else
1296       CI->ValueName = CI->ValueName + "," + RC.getName();
1297 
1298     RegisterClassClasses.insert(std::make_pair(Def, CI));
1299   }
1300 
1301   // Populate the map for individual registers.
1302   for (std::map<Record*, RegisterSet>::iterator it = RegisterMap.begin(),
1303          ie = RegisterMap.end(); it != ie; ++it)
1304     RegisterClasses[it->first] = RegisterSetClasses[it->second];
1305 
1306   // Name the register classes which correspond to singleton registers.
1307   for (Record *Rec : SingletonRegisters) {
1308     ClassInfo *CI = RegisterClasses[Rec];
1309     assert(CI && "Missing singleton register class info!");
1310 
1311     if (CI->ValueName.empty()) {
1312       CI->ClassName = Rec->getName();
1313       CI->Name = "MCK_" + Rec->getName();
1314       CI->ValueName = Rec->getName();
1315     } else
1316       CI->ValueName = CI->ValueName + "," + Rec->getName();
1317   }
1318 }
1319 
1320 void AsmMatcherInfo::buildOperandClasses() {
1321   std::vector<Record*> AsmOperands =
1322     Records.getAllDerivedDefinitions("AsmOperandClass");
1323 
1324   // Pre-populate AsmOperandClasses map.
1325   for (Record *Rec : AsmOperands) {
1326     Classes.emplace_front();
1327     AsmOperandClasses[Rec] = &Classes.front();
1328   }
1329 
1330   unsigned Index = 0;
1331   for (Record *Rec : AsmOperands) {
1332     ClassInfo *CI = AsmOperandClasses[Rec];
1333     CI->Kind = ClassInfo::UserClass0 + Index;
1334 
1335     ListInit *Supers = Rec->getValueAsListInit("SuperClasses");
1336     for (Init *I : Supers->getValues()) {
1337       DefInit *DI = dyn_cast<DefInit>(I);
1338       if (!DI) {
1339         PrintError(Rec->getLoc(), "Invalid super class reference!");
1340         continue;
1341       }
1342 
1343       ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1344       if (!SC)
1345         PrintError(Rec->getLoc(), "Invalid super class reference!");
1346       else
1347         CI->SuperClasses.push_back(SC);
1348     }
1349     CI->ClassName = Rec->getValueAsString("Name");
1350     CI->Name = "MCK_" + CI->ClassName;
1351     CI->ValueName = Rec->getName();
1352 
1353     // Get or construct the predicate method name.
1354     Init *PMName = Rec->getValueInit("PredicateMethod");
1355     if (StringInit *SI = dyn_cast<StringInit>(PMName)) {
1356       CI->PredicateMethod = SI->getValue();
1357     } else {
1358       assert(isa<UnsetInit>(PMName) && "Unexpected PredicateMethod field!");
1359       CI->PredicateMethod = "is" + CI->ClassName;
1360     }
1361 
1362     // Get or construct the render method name.
1363     Init *RMName = Rec->getValueInit("RenderMethod");
1364     if (StringInit *SI = dyn_cast<StringInit>(RMName)) {
1365       CI->RenderMethod = SI->getValue();
1366     } else {
1367       assert(isa<UnsetInit>(RMName) && "Unexpected RenderMethod field!");
1368       CI->RenderMethod = "add" + CI->ClassName + "Operands";
1369     }
1370 
1371     // Get the parse method name or leave it as empty.
1372     Init *PRMName = Rec->getValueInit("ParserMethod");
1373     if (StringInit *SI = dyn_cast<StringInit>(PRMName))
1374       CI->ParserMethod = SI->getValue();
1375 
1376     // Get the diagnostic type or leave it as empty.
1377     // Get the parse method name or leave it as empty.
1378     Init *DiagnosticType = Rec->getValueInit("DiagnosticType");
1379     if (StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
1380       CI->DiagnosticType = SI->getValue();
1381 
1382     Init *IsOptional = Rec->getValueInit("IsOptional");
1383     if (BitInit *BI = dyn_cast<BitInit>(IsOptional))
1384       CI->IsOptional = BI->getValue();
1385 
1386     // Get or construct the default method name.
1387     Init *DMName = Rec->getValueInit("DefaultMethod");
1388     if (StringInit *SI = dyn_cast<StringInit>(DMName)) {
1389       CI->DefaultMethod = SI->getValue();
1390     } else {
1391       assert(isa<UnsetInit>(DMName) && "Unexpected DefaultMethod field!");
1392       CI->DefaultMethod = "default" + CI->ClassName + "Operands";
1393     }
1394 
1395     ++Index;
1396   }
1397 }
1398 
1399 AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1400                                CodeGenTarget &target,
1401                                RecordKeeper &records)
1402   : Records(records), AsmParser(asmParser), Target(target) {
1403 }
1404 
1405 /// buildOperandMatchInfo - Build the necessary information to handle user
1406 /// defined operand parsing methods.
1407 void AsmMatcherInfo::buildOperandMatchInfo() {
1408 
1409   /// Map containing a mask with all operands indices that can be found for
1410   /// that class inside a instruction.
1411   typedef std::map<ClassInfo *, unsigned, less_ptr<ClassInfo>> OpClassMaskTy;
1412   OpClassMaskTy OpClassMask;
1413 
1414   for (const auto &MI : Matchables) {
1415     OpClassMask.clear();
1416 
1417     // Keep track of all operands of this instructions which belong to the
1418     // same class.
1419     for (unsigned i = 0, e = MI->AsmOperands.size(); i != e; ++i) {
1420       const MatchableInfo::AsmOperand &Op = MI->AsmOperands[i];
1421       if (Op.Class->ParserMethod.empty())
1422         continue;
1423       unsigned &OperandMask = OpClassMask[Op.Class];
1424       OperandMask |= (1 << i);
1425     }
1426 
1427     // Generate operand match info for each mnemonic/operand class pair.
1428     for (const auto &OCM : OpClassMask) {
1429       unsigned OpMask = OCM.second;
1430       ClassInfo *CI = OCM.first;
1431       OperandMatchInfo.push_back(OperandMatchEntry::create(MI.get(), CI,
1432                                                            OpMask));
1433     }
1434   }
1435 }
1436 
1437 void AsmMatcherInfo::buildInfo() {
1438   // Build information about all of the AssemblerPredicates.
1439   std::vector<Record*> AllPredicates =
1440     Records.getAllDerivedDefinitions("Predicate");
1441   for (Record *Pred : AllPredicates) {
1442     // Ignore predicates that are not intended for the assembler.
1443     if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1444       continue;
1445 
1446     if (Pred->getName().empty())
1447       PrintFatalError(Pred->getLoc(), "Predicate has no name!");
1448 
1449     SubtargetFeatures.insert(std::make_pair(
1450         Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size())));
1451     DEBUG(SubtargetFeatures.find(Pred)->second.dump());
1452     assert(SubtargetFeatures.size() <= 64 && "Too many subtarget features!");
1453   }
1454 
1455   bool HasMnemonicFirst = AsmParser->getValueAsBit("HasMnemonicFirst");
1456 
1457   // Parse the instructions; we need to do this first so that we can gather the
1458   // singleton register classes.
1459   SmallPtrSet<Record*, 16> SingletonRegisters;
1460   unsigned VariantCount = Target.getAsmParserVariantCount();
1461   for (unsigned VC = 0; VC != VariantCount; ++VC) {
1462     Record *AsmVariant = Target.getAsmParserVariant(VC);
1463     std::string CommentDelimiter =
1464       AsmVariant->getValueAsString("CommentDelimiter");
1465     AsmVariantInfo Variant;
1466     Variant.RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
1467     Variant.TokenizingCharacters =
1468         AsmVariant->getValueAsString("TokenizingCharacters");
1469     Variant.SeparatorCharacters =
1470         AsmVariant->getValueAsString("SeparatorCharacters");
1471     Variant.BreakCharacters =
1472         AsmVariant->getValueAsString("BreakCharacters");
1473     Variant.Name = AsmVariant->getValueAsString("Name");
1474     Variant.AsmVariantNo = AsmVariant->getValueAsInt("Variant");
1475 
1476     for (const CodeGenInstruction *CGI : Target.getInstructionsByEnumValue()) {
1477 
1478       // If the tblgen -match-prefix option is specified (for tblgen hackers),
1479       // filter the set of instructions we consider.
1480       if (!StringRef(CGI->TheDef->getName()).startswith(MatchPrefix))
1481         continue;
1482 
1483       // Ignore "codegen only" instructions.
1484       if (CGI->TheDef->getValueAsBit("isCodeGenOnly"))
1485         continue;
1486 
1487       // Ignore instructions for different instructions
1488       const std::string V = CGI->TheDef->getValueAsString("AsmVariantName");
1489       if (!V.empty() && V != Variant.Name)
1490         continue;
1491 
1492       auto II = llvm::make_unique<MatchableInfo>(*CGI);
1493 
1494       II->initialize(*this, SingletonRegisters, Variant, HasMnemonicFirst);
1495 
1496       // Ignore instructions which shouldn't be matched and diagnose invalid
1497       // instruction definitions with an error.
1498       if (!II->validate(CommentDelimiter, true))
1499         continue;
1500 
1501       Matchables.push_back(std::move(II));
1502     }
1503 
1504     // Parse all of the InstAlias definitions and stick them in the list of
1505     // matchables.
1506     std::vector<Record*> AllInstAliases =
1507       Records.getAllDerivedDefinitions("InstAlias");
1508     for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
1509       auto Alias = llvm::make_unique<CodeGenInstAlias>(AllInstAliases[i],
1510                                                        Variant.AsmVariantNo,
1511                                                        Target);
1512 
1513       // If the tblgen -match-prefix option is specified (for tblgen hackers),
1514       // filter the set of instruction aliases we consider, based on the target
1515       // instruction.
1516       if (!StringRef(Alias->ResultInst->TheDef->getName())
1517             .startswith( MatchPrefix))
1518         continue;
1519 
1520       const std::string V = Alias->TheDef->getValueAsString("AsmVariantName");
1521       if (!V.empty() && V != Variant.Name)
1522         continue;
1523 
1524       auto II = llvm::make_unique<MatchableInfo>(std::move(Alias));
1525 
1526       II->initialize(*this, SingletonRegisters, Variant, HasMnemonicFirst);
1527 
1528       // Validate the alias definitions.
1529       II->validate(CommentDelimiter, false);
1530 
1531       Matchables.push_back(std::move(II));
1532     }
1533   }
1534 
1535   // Build info for the register classes.
1536   buildRegisterClasses(SingletonRegisters);
1537 
1538   // Build info for the user defined assembly operand classes.
1539   buildOperandClasses();
1540 
1541   // Build the information about matchables, now that we have fully formed
1542   // classes.
1543   std::vector<std::unique_ptr<MatchableInfo>> NewMatchables;
1544   for (auto &II : Matchables) {
1545     // Parse the tokens after the mnemonic.
1546     // Note: buildInstructionOperandReference may insert new AsmOperands, so
1547     // don't precompute the loop bound.
1548     for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
1549       MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
1550       StringRef Token = Op.Token;
1551 
1552       // Check for singleton registers.
1553       if (Record *RegRecord = Op.SingletonReg) {
1554         Op.Class = RegisterClasses[RegRecord];
1555         assert(Op.Class && Op.Class->Registers.size() == 1 &&
1556                "Unexpected class for singleton register");
1557         continue;
1558       }
1559 
1560       // Check for simple tokens.
1561       if (Token[0] != '$') {
1562         Op.Class = getTokenClass(Token);
1563         continue;
1564       }
1565 
1566       if (Token.size() > 1 && isdigit(Token[1])) {
1567         Op.Class = getTokenClass(Token);
1568         continue;
1569       }
1570 
1571       // Otherwise this is an operand reference.
1572       StringRef OperandName;
1573       if (Token[1] == '{')
1574         OperandName = Token.substr(2, Token.size() - 3);
1575       else
1576         OperandName = Token.substr(1);
1577 
1578       if (II->DefRec.is<const CodeGenInstruction*>())
1579         buildInstructionOperandReference(II.get(), OperandName, i);
1580       else
1581         buildAliasOperandReference(II.get(), OperandName, Op);
1582     }
1583 
1584     if (II->DefRec.is<const CodeGenInstruction*>()) {
1585       II->buildInstructionResultOperands();
1586       // If the instruction has a two-operand alias, build up the
1587       // matchable here. We'll add them in bulk at the end to avoid
1588       // confusing this loop.
1589       std::string Constraint =
1590         II->TheDef->getValueAsString("TwoOperandAliasConstraint");
1591       if (Constraint != "") {
1592         // Start by making a copy of the original matchable.
1593         auto AliasII = llvm::make_unique<MatchableInfo>(*II);
1594 
1595         // Adjust it to be a two-operand alias.
1596         AliasII->formTwoOperandAlias(Constraint);
1597 
1598         // Add the alias to the matchables list.
1599         NewMatchables.push_back(std::move(AliasII));
1600       }
1601     } else
1602       II->buildAliasResultOperands();
1603   }
1604   if (!NewMatchables.empty())
1605     Matchables.insert(Matchables.end(),
1606                       std::make_move_iterator(NewMatchables.begin()),
1607                       std::make_move_iterator(NewMatchables.end()));
1608 
1609   // Process token alias definitions and set up the associated superclass
1610   // information.
1611   std::vector<Record*> AllTokenAliases =
1612     Records.getAllDerivedDefinitions("TokenAlias");
1613   for (Record *Rec : AllTokenAliases) {
1614     ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1615     ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
1616     if (FromClass == ToClass)
1617       PrintFatalError(Rec->getLoc(),
1618                     "error: Destination value identical to source value.");
1619     FromClass->SuperClasses.push_back(ToClass);
1620   }
1621 
1622   // Reorder classes so that classes precede super classes.
1623   Classes.sort();
1624 
1625 #ifndef NDEBUG
1626   // Verify that the table is now sorted
1627   for (auto I = Classes.begin(), E = Classes.end(); I != E; ++I) {
1628     for (auto J = I; J != E; ++J) {
1629       assert(!(*J < *I));
1630       assert(I == J || !J->isSubsetOf(*I));
1631     }
1632   }
1633 #endif // NDEBUG
1634 }
1635 
1636 /// buildInstructionOperandReference - The specified operand is a reference to a
1637 /// named operand such as $src.  Resolve the Class and OperandInfo pointers.
1638 void AsmMatcherInfo::
1639 buildInstructionOperandReference(MatchableInfo *II,
1640                                  StringRef OperandName,
1641                                  unsigned AsmOpIdx) {
1642   const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1643   const CGIOperandList &Operands = CGI.Operands;
1644   MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
1645 
1646   // Map this token to an operand.
1647   unsigned Idx;
1648   if (!Operands.hasOperandNamed(OperandName, Idx))
1649     PrintFatalError(II->TheDef->getLoc(),
1650                     "error: unable to find operand: '" + OperandName + "'");
1651 
1652   // If the instruction operand has multiple suboperands, but the parser
1653   // match class for the asm operand is still the default "ImmAsmOperand",
1654   // then handle each suboperand separately.
1655   if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1656     Record *Rec = Operands[Idx].Rec;
1657     assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1658     Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1659     if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1660       // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1661       StringRef Token = Op->Token; // save this in case Op gets moved
1662       for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1663         MatchableInfo::AsmOperand NewAsmOp(/*IsIsolatedToken=*/true, Token);
1664         NewAsmOp.SubOpIdx = SI;
1665         II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1666       }
1667       // Replace Op with first suboperand.
1668       Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1669       Op->SubOpIdx = 0;
1670     }
1671   }
1672 
1673   // Set up the operand class.
1674   Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
1675 
1676   // If the named operand is tied, canonicalize it to the untied operand.
1677   // For example, something like:
1678   //   (outs GPR:$dst), (ins GPR:$src)
1679   // with an asmstring of
1680   //   "inc $src"
1681   // we want to canonicalize to:
1682   //   "inc $dst"
1683   // so that we know how to provide the $dst operand when filling in the result.
1684   int OITied = -1;
1685   if (Operands[Idx].MINumOperands == 1)
1686     OITied = Operands[Idx].getTiedRegister();
1687   if (OITied != -1) {
1688     // The tied operand index is an MIOperand index, find the operand that
1689     // contains it.
1690     std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1691     OperandName = Operands[Idx.first].Name;
1692     Op->SubOpIdx = Idx.second;
1693   }
1694 
1695   Op->SrcOpName = OperandName;
1696 }
1697 
1698 /// buildAliasOperandReference - When parsing an operand reference out of the
1699 /// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1700 /// operand reference is by looking it up in the result pattern definition.
1701 void AsmMatcherInfo::buildAliasOperandReference(MatchableInfo *II,
1702                                                 StringRef OperandName,
1703                                                 MatchableInfo::AsmOperand &Op) {
1704   const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
1705 
1706   // Set up the operand class.
1707   for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
1708     if (CGA.ResultOperands[i].isRecord() &&
1709         CGA.ResultOperands[i].getName() == OperandName) {
1710       // It's safe to go with the first one we find, because CodeGenInstAlias
1711       // validates that all operands with the same name have the same record.
1712       Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
1713       // Use the match class from the Alias definition, not the
1714       // destination instruction, as we may have an immediate that's
1715       // being munged by the match class.
1716       Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
1717                                  Op.SubOpIdx);
1718       Op.SrcOpName = OperandName;
1719       return;
1720     }
1721 
1722   PrintFatalError(II->TheDef->getLoc(),
1723                   "error: unable to find operand: '" + OperandName + "'");
1724 }
1725 
1726 void MatchableInfo::buildInstructionResultOperands() {
1727   const CodeGenInstruction *ResultInst = getResultInst();
1728 
1729   // Loop over all operands of the result instruction, determining how to
1730   // populate them.
1731   for (const CGIOperandList::OperandInfo &OpInfo : ResultInst->Operands) {
1732     // If this is a tied operand, just copy from the previously handled operand.
1733     int TiedOp = -1;
1734     if (OpInfo.MINumOperands == 1)
1735       TiedOp = OpInfo.getTiedRegister();
1736     if (TiedOp != -1) {
1737       ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
1738       continue;
1739     }
1740 
1741     // Find out what operand from the asmparser this MCInst operand comes from.
1742     int SrcOperand = findAsmOperandNamed(OpInfo.Name);
1743     if (OpInfo.Name.empty() || SrcOperand == -1) {
1744       // This may happen for operands that are tied to a suboperand of a
1745       // complex operand.  Simply use a dummy value here; nobody should
1746       // use this operand slot.
1747       // FIXME: The long term goal is for the MCOperand list to not contain
1748       // tied operands at all.
1749       ResOperands.push_back(ResOperand::getImmOp(0));
1750       continue;
1751     }
1752 
1753     // Check if the one AsmOperand populates the entire operand.
1754     unsigned NumOperands = OpInfo.MINumOperands;
1755     if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1756       ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
1757       continue;
1758     }
1759 
1760     // Add a separate ResOperand for each suboperand.
1761     for (unsigned AI = 0; AI < NumOperands; ++AI) {
1762       assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1763              AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1764              "unexpected AsmOperands for suboperands");
1765       ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1766     }
1767   }
1768 }
1769 
1770 void MatchableInfo::buildAliasResultOperands() {
1771   const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1772   const CodeGenInstruction *ResultInst = getResultInst();
1773 
1774   // Loop over all operands of the result instruction, determining how to
1775   // populate them.
1776   unsigned AliasOpNo = 0;
1777   unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
1778   for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1779     const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
1780 
1781     // If this is a tied operand, just copy from the previously handled operand.
1782     int TiedOp = -1;
1783     if (OpInfo->MINumOperands == 1)
1784       TiedOp = OpInfo->getTiedRegister();
1785     if (TiedOp != -1) {
1786       ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
1787       continue;
1788     }
1789 
1790     // Handle all the suboperands for this operand.
1791     const std::string &OpName = OpInfo->Name;
1792     for ( ; AliasOpNo <  LastOpNo &&
1793             CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1794       int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1795 
1796       // Find out what operand from the asmparser that this MCInst operand
1797       // comes from.
1798       switch (CGA.ResultOperands[AliasOpNo].Kind) {
1799       case CodeGenInstAlias::ResultOperand::K_Record: {
1800         StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1801         int SrcOperand = findAsmOperand(Name, SubIdx);
1802         if (SrcOperand == -1)
1803           PrintFatalError(TheDef->getLoc(), "Instruction '" +
1804                         TheDef->getName() + "' has operand '" + OpName +
1805                         "' that doesn't appear in asm string!");
1806         unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1807         ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1808                                                         NumOperands));
1809         break;
1810       }
1811       case CodeGenInstAlias::ResultOperand::K_Imm: {
1812         int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1813         ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1814         break;
1815       }
1816       case CodeGenInstAlias::ResultOperand::K_Reg: {
1817         Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1818         ResOperands.push_back(ResOperand::getRegOp(Reg));
1819         break;
1820       }
1821       }
1822     }
1823   }
1824 }
1825 
1826 static unsigned
1827 getConverterOperandID(const std::string &Name,
1828                       SmallSetVector<CachedHashString, 16> &Table,
1829                       bool &IsNew) {
1830   IsNew = Table.insert(CachedHashString(Name));
1831 
1832   unsigned ID = IsNew ? Table.size() - 1 : find(Table, Name) - Table.begin();
1833 
1834   assert(ID < Table.size());
1835 
1836   return ID;
1837 }
1838 
1839 static void emitConvertFuncs(CodeGenTarget &Target, StringRef ClassName,
1840                              std::vector<std::unique_ptr<MatchableInfo>> &Infos,
1841                              bool HasMnemonicFirst, bool HasOptionalOperands,
1842                              raw_ostream &OS) {
1843   SmallSetVector<CachedHashString, 16> OperandConversionKinds;
1844   SmallSetVector<CachedHashString, 16> InstructionConversionKinds;
1845   std::vector<std::vector<uint8_t> > ConversionTable;
1846   size_t MaxRowLength = 2; // minimum is custom converter plus terminator.
1847 
1848   // TargetOperandClass - This is the target's operand class, like X86Operand.
1849   std::string TargetOperandClass = Target.getName() + "Operand";
1850 
1851   // Write the convert function to a separate stream, so we can drop it after
1852   // the enum. We'll build up the conversion handlers for the individual
1853   // operand types opportunistically as we encounter them.
1854   std::string ConvertFnBody;
1855   raw_string_ostream CvtOS(ConvertFnBody);
1856   // Start the unified conversion function.
1857   if (HasOptionalOperands) {
1858     CvtOS << "void " << Target.getName() << ClassName << "::\n"
1859           << "convertToMCInst(unsigned Kind, MCInst &Inst, "
1860           << "unsigned Opcode,\n"
1861           << "                const OperandVector &Operands,\n"
1862           << "                const SmallBitVector &OptionalOperandsMask) {\n";
1863   } else {
1864     CvtOS << "void " << Target.getName() << ClassName << "::\n"
1865           << "convertToMCInst(unsigned Kind, MCInst &Inst, "
1866           << "unsigned Opcode,\n"
1867           << "                const OperandVector &Operands) {\n";
1868   }
1869   CvtOS << "  assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n";
1870   CvtOS << "  const uint8_t *Converter = ConversionTable[Kind];\n";
1871   if (HasOptionalOperands) {
1872     CvtOS << "  unsigned NumDefaults = 0;\n";
1873   }
1874   CvtOS << "  unsigned OpIdx;\n";
1875   CvtOS << "  Inst.setOpcode(Opcode);\n";
1876   CvtOS << "  for (const uint8_t *p = Converter; *p; p+= 2) {\n";
1877   if (HasOptionalOperands) {
1878     CvtOS << "    OpIdx = *(p + 1) - NumDefaults;\n";
1879   } else {
1880     CvtOS << "    OpIdx = *(p + 1);\n";
1881   }
1882   CvtOS << "    switch (*p) {\n";
1883   CvtOS << "    default: llvm_unreachable(\"invalid conversion entry!\");\n";
1884   CvtOS << "    case CVT_Reg:\n";
1885   CvtOS << "      static_cast<" << TargetOperandClass
1886         << "&>(*Operands[OpIdx]).addRegOperands(Inst, 1);\n";
1887   CvtOS << "      break;\n";
1888   CvtOS << "    case CVT_Tied:\n";
1889   CvtOS << "      Inst.addOperand(Inst.getOperand(OpIdx));\n";
1890   CvtOS << "      break;\n";
1891 
1892   std::string OperandFnBody;
1893   raw_string_ostream OpOS(OperandFnBody);
1894   // Start the operand number lookup function.
1895   OpOS << "void " << Target.getName() << ClassName << "::\n"
1896        << "convertToMapAndConstraints(unsigned Kind,\n";
1897   OpOS.indent(27);
1898   OpOS << "const OperandVector &Operands) {\n"
1899        << "  assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"
1900        << "  unsigned NumMCOperands = 0;\n"
1901        << "  const uint8_t *Converter = ConversionTable[Kind];\n"
1902        << "  for (const uint8_t *p = Converter; *p; p+= 2) {\n"
1903        << "    switch (*p) {\n"
1904        << "    default: llvm_unreachable(\"invalid conversion entry!\");\n"
1905        << "    case CVT_Reg:\n"
1906        << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
1907        << "      Operands[*(p + 1)]->setConstraint(\"r\");\n"
1908        << "      ++NumMCOperands;\n"
1909        << "      break;\n"
1910        << "    case CVT_Tied:\n"
1911        << "      ++NumMCOperands;\n"
1912        << "      break;\n";
1913 
1914   // Pre-populate the operand conversion kinds with the standard always
1915   // available entries.
1916   OperandConversionKinds.insert(CachedHashString("CVT_Done"));
1917   OperandConversionKinds.insert(CachedHashString("CVT_Reg"));
1918   OperandConversionKinds.insert(CachedHashString("CVT_Tied"));
1919   enum { CVT_Done, CVT_Reg, CVT_Tied };
1920 
1921   for (auto &II : Infos) {
1922     // Check if we have a custom match function.
1923     std::string AsmMatchConverter =
1924       II->getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
1925     if (!AsmMatchConverter.empty() && II->UseInstAsmMatchConverter) {
1926       std::string Signature = "ConvertCustom_" + AsmMatchConverter;
1927       II->ConversionFnKind = Signature;
1928 
1929       // Check if we have already generated this signature.
1930       if (!InstructionConversionKinds.insert(CachedHashString(Signature)))
1931         continue;
1932 
1933       // Remember this converter for the kind enum.
1934       unsigned KindID = OperandConversionKinds.size();
1935       OperandConversionKinds.insert(
1936           CachedHashString("CVT_" + getEnumNameForToken(AsmMatchConverter)));
1937 
1938       // Add the converter row for this instruction.
1939       ConversionTable.emplace_back();
1940       ConversionTable.back().push_back(KindID);
1941       ConversionTable.back().push_back(CVT_Done);
1942 
1943       // Add the handler to the conversion driver function.
1944       CvtOS << "    case CVT_"
1945             << getEnumNameForToken(AsmMatchConverter) << ":\n"
1946             << "      " << AsmMatchConverter << "(Inst, Operands);\n"
1947             << "      break;\n";
1948 
1949       // FIXME: Handle the operand number lookup for custom match functions.
1950       continue;
1951     }
1952 
1953     // Build the conversion function signature.
1954     std::string Signature = "Convert";
1955 
1956     std::vector<uint8_t> ConversionRow;
1957 
1958     // Compute the convert enum and the case body.
1959     MaxRowLength = std::max(MaxRowLength, II->ResOperands.size()*2 + 1 );
1960 
1961     for (unsigned i = 0, e = II->ResOperands.size(); i != e; ++i) {
1962       const MatchableInfo::ResOperand &OpInfo = II->ResOperands[i];
1963 
1964       // Generate code to populate each result operand.
1965       switch (OpInfo.Kind) {
1966       case MatchableInfo::ResOperand::RenderAsmOperand: {
1967         // This comes from something we parsed.
1968         const MatchableInfo::AsmOperand &Op =
1969           II->AsmOperands[OpInfo.AsmOperandNum];
1970 
1971         // Registers are always converted the same, don't duplicate the
1972         // conversion function based on them.
1973         Signature += "__";
1974         std::string Class;
1975         Class = Op.Class->isRegisterClass() ? "Reg" : Op.Class->ClassName;
1976         Signature += Class;
1977         Signature += utostr(OpInfo.MINumOperands);
1978         Signature += "_" + itostr(OpInfo.AsmOperandNum);
1979 
1980         // Add the conversion kind, if necessary, and get the associated ID
1981         // the index of its entry in the vector).
1982         std::string Name = "CVT_" + (Op.Class->isRegisterClass() ? "Reg" :
1983                                      Op.Class->RenderMethod);
1984         if (Op.Class->IsOptional) {
1985           // For optional operands we must also care about DefaultMethod
1986           assert(HasOptionalOperands);
1987           Name += "_" + Op.Class->DefaultMethod;
1988         }
1989         Name = getEnumNameForToken(Name);
1990 
1991         bool IsNewConverter = false;
1992         unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
1993                                             IsNewConverter);
1994 
1995         // Add the operand entry to the instruction kind conversion row.
1996         ConversionRow.push_back(ID);
1997         ConversionRow.push_back(OpInfo.AsmOperandNum + HasMnemonicFirst);
1998 
1999         if (!IsNewConverter)
2000           break;
2001 
2002         // This is a new operand kind. Add a handler for it to the
2003         // converter driver.
2004         CvtOS << "    case " << Name << ":\n";
2005         if (Op.Class->IsOptional) {
2006           // If optional operand is not present in actual instruction then we
2007           // should call its DefaultMethod before RenderMethod
2008           assert(HasOptionalOperands);
2009           CvtOS << "      if (OptionalOperandsMask[*(p + 1) - 1]) {\n"
2010                 << "        " << Op.Class->DefaultMethod << "()"
2011                 << "->" << Op.Class->RenderMethod << "(Inst, "
2012                 << OpInfo.MINumOperands << ");\n"
2013                 << "        ++NumDefaults;\n"
2014                 << "      } else {\n"
2015                 << "        static_cast<" << TargetOperandClass
2016                 << "&>(*Operands[OpIdx])." << Op.Class->RenderMethod
2017                 << "(Inst, " << OpInfo.MINumOperands << ");\n"
2018                 << "      }\n";
2019         } else {
2020           CvtOS << "      static_cast<" << TargetOperandClass
2021                 << "&>(*Operands[OpIdx])." << Op.Class->RenderMethod
2022                 << "(Inst, " << OpInfo.MINumOperands << ");\n";
2023         }
2024         CvtOS << "      break;\n";
2025 
2026         // Add a handler for the operand number lookup.
2027         OpOS << "    case " << Name << ":\n"
2028              << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n";
2029 
2030         if (Op.Class->isRegisterClass())
2031           OpOS << "      Operands[*(p + 1)]->setConstraint(\"r\");\n";
2032         else
2033           OpOS << "      Operands[*(p + 1)]->setConstraint(\"m\");\n";
2034         OpOS << "      NumMCOperands += " << OpInfo.MINumOperands << ";\n"
2035              << "      break;\n";
2036         break;
2037       }
2038       case MatchableInfo::ResOperand::TiedOperand: {
2039         // If this operand is tied to a previous one, just copy the MCInst
2040         // operand from the earlier one.We can only tie single MCOperand values.
2041         assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
2042         unsigned TiedOp = OpInfo.TiedOperandNum;
2043         assert(i > TiedOp && "Tied operand precedes its target!");
2044         Signature += "__Tie" + utostr(TiedOp);
2045         ConversionRow.push_back(CVT_Tied);
2046         ConversionRow.push_back(TiedOp);
2047         break;
2048       }
2049       case MatchableInfo::ResOperand::ImmOperand: {
2050         int64_t Val = OpInfo.ImmVal;
2051         std::string Ty = "imm_" + itostr(Val);
2052         Ty = getEnumNameForToken(Ty);
2053         Signature += "__" + Ty;
2054 
2055         std::string Name = "CVT_" + Ty;
2056         bool IsNewConverter = false;
2057         unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
2058                                             IsNewConverter);
2059         // Add the operand entry to the instruction kind conversion row.
2060         ConversionRow.push_back(ID);
2061         ConversionRow.push_back(0);
2062 
2063         if (!IsNewConverter)
2064           break;
2065 
2066         CvtOS << "    case " << Name << ":\n"
2067               << "      Inst.addOperand(MCOperand::createImm(" << Val << "));\n"
2068               << "      break;\n";
2069 
2070         OpOS << "    case " << Name << ":\n"
2071              << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2072              << "      Operands[*(p + 1)]->setConstraint(\"\");\n"
2073              << "      ++NumMCOperands;\n"
2074              << "      break;\n";
2075         break;
2076       }
2077       case MatchableInfo::ResOperand::RegOperand: {
2078         std::string Reg, Name;
2079         if (!OpInfo.Register) {
2080           Name = "reg0";
2081           Reg = "0";
2082         } else {
2083           Reg = getQualifiedName(OpInfo.Register);
2084           Name = "reg" + OpInfo.Register->getName();
2085         }
2086         Signature += "__" + Name;
2087         Name = "CVT_" + Name;
2088         bool IsNewConverter = false;
2089         unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
2090                                             IsNewConverter);
2091         // Add the operand entry to the instruction kind conversion row.
2092         ConversionRow.push_back(ID);
2093         ConversionRow.push_back(0);
2094 
2095         if (!IsNewConverter)
2096           break;
2097         CvtOS << "    case " << Name << ":\n"
2098               << "      Inst.addOperand(MCOperand::createReg(" << Reg << "));\n"
2099               << "      break;\n";
2100 
2101         OpOS << "    case " << Name << ":\n"
2102              << "      Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
2103              << "      Operands[*(p + 1)]->setConstraint(\"m\");\n"
2104              << "      ++NumMCOperands;\n"
2105              << "      break;\n";
2106       }
2107       }
2108     }
2109 
2110     // If there were no operands, add to the signature to that effect
2111     if (Signature == "Convert")
2112       Signature += "_NoOperands";
2113 
2114     II->ConversionFnKind = Signature;
2115 
2116     // Save the signature. If we already have it, don't add a new row
2117     // to the table.
2118     if (!InstructionConversionKinds.insert(CachedHashString(Signature)))
2119       continue;
2120 
2121     // Add the row to the table.
2122     ConversionTable.push_back(std::move(ConversionRow));
2123   }
2124 
2125   // Finish up the converter driver function.
2126   CvtOS << "    }\n  }\n}\n\n";
2127 
2128   // Finish up the operand number lookup function.
2129   OpOS << "    }\n  }\n}\n\n";
2130 
2131   OS << "namespace {\n";
2132 
2133   // Output the operand conversion kind enum.
2134   OS << "enum OperatorConversionKind {\n";
2135   for (const auto &Converter : OperandConversionKinds)
2136     OS << "  " << Converter << ",\n";
2137   OS << "  CVT_NUM_CONVERTERS\n";
2138   OS << "};\n\n";
2139 
2140   // Output the instruction conversion kind enum.
2141   OS << "enum InstructionConversionKind {\n";
2142   for (const auto &Signature : InstructionConversionKinds)
2143     OS << "  " << Signature << ",\n";
2144   OS << "  CVT_NUM_SIGNATURES\n";
2145   OS << "};\n\n";
2146 
2147   OS << "} // end anonymous namespace\n\n";
2148 
2149   // Output the conversion table.
2150   OS << "static const uint8_t ConversionTable[CVT_NUM_SIGNATURES]["
2151      << MaxRowLength << "] = {\n";
2152 
2153   for (unsigned Row = 0, ERow = ConversionTable.size(); Row != ERow; ++Row) {
2154     assert(ConversionTable[Row].size() % 2 == 0 && "bad conversion row!");
2155     OS << "  // " << InstructionConversionKinds[Row] << "\n";
2156     OS << "  { ";
2157     for (unsigned i = 0, e = ConversionTable[Row].size(); i != e; i += 2)
2158       OS << OperandConversionKinds[ConversionTable[Row][i]] << ", "
2159          << (unsigned)(ConversionTable[Row][i + 1]) << ", ";
2160     OS << "CVT_Done },\n";
2161   }
2162 
2163   OS << "};\n\n";
2164 
2165   // Spit out the conversion driver function.
2166   OS << CvtOS.str();
2167 
2168   // Spit out the operand number lookup function.
2169   OS << OpOS.str();
2170 }
2171 
2172 /// emitMatchClassEnumeration - Emit the enumeration for match class kinds.
2173 static void emitMatchClassEnumeration(CodeGenTarget &Target,
2174                                       std::forward_list<ClassInfo> &Infos,
2175                                       raw_ostream &OS) {
2176   OS << "namespace {\n\n";
2177 
2178   OS << "/// MatchClassKind - The kinds of classes which participate in\n"
2179      << "/// instruction matching.\n";
2180   OS << "enum MatchClassKind {\n";
2181   OS << "  InvalidMatchClass = 0,\n";
2182   OS << "  OptionalMatchClass = 1,\n";
2183   for (const auto &CI : Infos) {
2184     OS << "  " << CI.Name << ", // ";
2185     if (CI.Kind == ClassInfo::Token) {
2186       OS << "'" << CI.ValueName << "'\n";
2187     } else if (CI.isRegisterClass()) {
2188       if (!CI.ValueName.empty())
2189         OS << "register class '" << CI.ValueName << "'\n";
2190       else
2191         OS << "derived register class\n";
2192     } else {
2193       OS << "user defined class '" << CI.ValueName << "'\n";
2194     }
2195   }
2196   OS << "  NumMatchClassKinds\n";
2197   OS << "};\n\n";
2198 
2199   OS << "}\n\n";
2200 }
2201 
2202 /// emitValidateOperandClass - Emit the function to validate an operand class.
2203 static void emitValidateOperandClass(AsmMatcherInfo &Info,
2204                                      raw_ostream &OS) {
2205   OS << "static unsigned validateOperandClass(MCParsedAsmOperand &GOp, "
2206      << "MatchClassKind Kind) {\n";
2207   OS << "  " << Info.Target.getName() << "Operand &Operand = ("
2208      << Info.Target.getName() << "Operand&)GOp;\n";
2209 
2210   // The InvalidMatchClass is not to match any operand.
2211   OS << "  if (Kind == InvalidMatchClass)\n";
2212   OS << "    return MCTargetAsmParser::Match_InvalidOperand;\n\n";
2213 
2214   // Check for Token operands first.
2215   // FIXME: Use a more specific diagnostic type.
2216   OS << "  if (Operand.isToken())\n";
2217   OS << "    return isSubclass(matchTokenString(Operand.getToken()), Kind) ?\n"
2218      << "             MCTargetAsmParser::Match_Success :\n"
2219      << "             MCTargetAsmParser::Match_InvalidOperand;\n\n";
2220 
2221   // Check the user classes. We don't care what order since we're only
2222   // actually matching against one of them.
2223   OS << "  switch (Kind) {\n"
2224         "  default: break;\n";
2225   for (const auto &CI : Info.Classes) {
2226     if (!CI.isUserClass())
2227       continue;
2228 
2229     OS << "  // '" << CI.ClassName << "' class\n";
2230     OS << "  case " << CI.Name << ":\n";
2231     OS << "    if (Operand." << CI.PredicateMethod << "())\n";
2232     OS << "      return MCTargetAsmParser::Match_Success;\n";
2233     if (!CI.DiagnosticType.empty())
2234       OS << "    return " << Info.Target.getName() << "AsmParser::Match_"
2235          << CI.DiagnosticType << ";\n";
2236     else
2237       OS << "    break;\n";
2238   }
2239   OS << "  } // end switch (Kind)\n\n";
2240 
2241   // Check for register operands, including sub-classes.
2242   OS << "  if (Operand.isReg()) {\n";
2243   OS << "    MatchClassKind OpKind;\n";
2244   OS << "    switch (Operand.getReg()) {\n";
2245   OS << "    default: OpKind = InvalidMatchClass; break;\n";
2246   for (const auto &RC : Info.RegisterClasses)
2247     OS << "    case " << Info.Target.getName() << "::"
2248        << RC.first->getName() << ": OpKind = " << RC.second->Name
2249        << "; break;\n";
2250   OS << "    }\n";
2251   OS << "    return isSubclass(OpKind, Kind) ? "
2252      << "MCTargetAsmParser::Match_Success :\n                             "
2253      << "         MCTargetAsmParser::Match_InvalidOperand;\n  }\n\n";
2254 
2255   // Generic fallthrough match failure case for operands that don't have
2256   // specialized diagnostic types.
2257   OS << "  return MCTargetAsmParser::Match_InvalidOperand;\n";
2258   OS << "}\n\n";
2259 }
2260 
2261 /// emitIsSubclass - Emit the subclass predicate function.
2262 static void emitIsSubclass(CodeGenTarget &Target,
2263                            std::forward_list<ClassInfo> &Infos,
2264                            raw_ostream &OS) {
2265   OS << "/// isSubclass - Compute whether \\p A is a subclass of \\p B.\n";
2266   OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
2267   OS << "  if (A == B)\n";
2268   OS << "    return true;\n\n";
2269 
2270   bool EmittedSwitch = false;
2271   for (const auto &A : Infos) {
2272     std::vector<StringRef> SuperClasses;
2273     if (A.IsOptional)
2274       SuperClasses.push_back("OptionalMatchClass");
2275     for (const auto &B : Infos) {
2276       if (&A != &B && A.isSubsetOf(B))
2277         SuperClasses.push_back(B.Name);
2278     }
2279 
2280     if (SuperClasses.empty())
2281       continue;
2282 
2283     // If this is the first SuperClass, emit the switch header.
2284     if (!EmittedSwitch) {
2285       OS << "  switch (A) {\n";
2286       OS << "  default:\n";
2287       OS << "    return false;\n";
2288       EmittedSwitch = true;
2289     }
2290 
2291     OS << "\n  case " << A.Name << ":\n";
2292 
2293     if (SuperClasses.size() == 1) {
2294       OS << "    return B == " << SuperClasses.back() << ";\n";
2295       continue;
2296     }
2297 
2298     if (!SuperClasses.empty()) {
2299       OS << "    switch (B) {\n";
2300       OS << "    default: return false;\n";
2301       for (StringRef SC : SuperClasses)
2302         OS << "    case " << SC << ": return true;\n";
2303       OS << "    }\n";
2304     } else {
2305       // No case statement to emit
2306       OS << "    return false;\n";
2307     }
2308   }
2309 
2310   // If there were case statements emitted into the string stream write the
2311   // default.
2312   if (EmittedSwitch)
2313     OS << "  }\n";
2314   else
2315     OS << "  return false;\n";
2316 
2317   OS << "}\n\n";
2318 }
2319 
2320 /// emitMatchTokenString - Emit the function to match a token string to the
2321 /// appropriate match class value.
2322 static void emitMatchTokenString(CodeGenTarget &Target,
2323                                  std::forward_list<ClassInfo> &Infos,
2324                                  raw_ostream &OS) {
2325   // Construct the match list.
2326   std::vector<StringMatcher::StringPair> Matches;
2327   for (const auto &CI : Infos) {
2328     if (CI.Kind == ClassInfo::Token)
2329       Matches.emplace_back(CI.ValueName, "return " + CI.Name + ";");
2330   }
2331 
2332   OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
2333 
2334   StringMatcher("Name", Matches, OS).Emit();
2335 
2336   OS << "  return InvalidMatchClass;\n";
2337   OS << "}\n\n";
2338 }
2339 
2340 /// emitMatchRegisterName - Emit the function to match a string to the target
2341 /// specific register enum.
2342 static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
2343                                   raw_ostream &OS) {
2344   // Construct the match list.
2345   std::vector<StringMatcher::StringPair> Matches;
2346   const auto &Regs = Target.getRegBank().getRegisters();
2347   for (const CodeGenRegister &Reg : Regs) {
2348     if (Reg.TheDef->getValueAsString("AsmName").empty())
2349       continue;
2350 
2351     Matches.emplace_back(Reg.TheDef->getValueAsString("AsmName"),
2352                          "return " + utostr(Reg.EnumValue) + ";");
2353   }
2354 
2355   OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
2356 
2357   StringMatcher("Name", Matches, OS).Emit();
2358 
2359   OS << "  return 0;\n";
2360   OS << "}\n\n";
2361 }
2362 
2363 /// Emit the function to match a string to the target
2364 /// specific register enum.
2365 static void emitMatchRegisterAltName(CodeGenTarget &Target, Record *AsmParser,
2366                                      raw_ostream &OS) {
2367   // Construct the match list.
2368   std::vector<StringMatcher::StringPair> Matches;
2369   const auto &Regs = Target.getRegBank().getRegisters();
2370   for (const CodeGenRegister &Reg : Regs) {
2371 
2372     auto AltNames = Reg.TheDef->getValueAsListOfStrings("AltNames");
2373 
2374     for (auto AltName : AltNames) {
2375       AltName = StringRef(AltName).trim();
2376 
2377       // don't handle empty alternative names
2378       if (AltName.empty())
2379         continue;
2380 
2381       Matches.emplace_back(AltName,
2382                            "return " + utostr(Reg.EnumValue) + ";");
2383     }
2384   }
2385 
2386   OS << "static unsigned MatchRegisterAltName(StringRef Name) {\n";
2387 
2388   StringMatcher("Name", Matches, OS).Emit();
2389 
2390   OS << "  return 0;\n";
2391   OS << "}\n\n";
2392 }
2393 
2394 static const char *getMinimalTypeForRange(uint64_t Range) {
2395   assert(Range <= 0xFFFFFFFFFFFFFFFFULL && "Enum too large");
2396   if (Range > 0xFFFFFFFFULL)
2397     return "uint64_t";
2398   if (Range > 0xFFFF)
2399     return "uint32_t";
2400   if (Range > 0xFF)
2401     return "uint16_t";
2402   return "uint8_t";
2403 }
2404 
2405 static const char *getMinimalRequiredFeaturesType(const AsmMatcherInfo &Info) {
2406   uint64_t MaxIndex = Info.SubtargetFeatures.size();
2407   if (MaxIndex > 0)
2408     MaxIndex--;
2409   return getMinimalTypeForRange(1ULL << MaxIndex);
2410 }
2411 
2412 /// emitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
2413 /// definitions.
2414 static void emitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
2415                                                 raw_ostream &OS) {
2416   OS << "// Flags for subtarget features that participate in "
2417      << "instruction matching.\n";
2418   OS << "enum SubtargetFeatureFlag : " << getMinimalRequiredFeaturesType(Info)
2419      << " {\n";
2420   for (const auto &SF : Info.SubtargetFeatures) {
2421     const SubtargetFeatureInfo &SFI = SF.second;
2422     OS << "  " << SFI.getEnumName() << " = (1ULL << " << SFI.Index << "),\n";
2423   }
2424   OS << "  Feature_None = 0\n";
2425   OS << "};\n\n";
2426 }
2427 
2428 /// emitOperandDiagnosticTypes - Emit the operand matching diagnostic types.
2429 static void emitOperandDiagnosticTypes(AsmMatcherInfo &Info, raw_ostream &OS) {
2430   // Get the set of diagnostic types from all of the operand classes.
2431   std::set<StringRef> Types;
2432   for (const auto &OpClassEntry : Info.AsmOperandClasses) {
2433     if (!OpClassEntry.second->DiagnosticType.empty())
2434       Types.insert(OpClassEntry.second->DiagnosticType);
2435   }
2436 
2437   if (Types.empty()) return;
2438 
2439   // Now emit the enum entries.
2440   for (StringRef Type : Types)
2441     OS << "  Match_" << Type << ",\n";
2442   OS << "  END_OPERAND_DIAGNOSTIC_TYPES\n";
2443 }
2444 
2445 /// emitGetSubtargetFeatureName - Emit the helper function to get the
2446 /// user-level name for a subtarget feature.
2447 static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) {
2448   OS << "// User-level names for subtarget features that participate in\n"
2449      << "// instruction matching.\n"
2450      << "static const char *getSubtargetFeatureName(uint64_t Val) {\n";
2451   if (!Info.SubtargetFeatures.empty()) {
2452     OS << "  switch(Val) {\n";
2453     for (const auto &SF : Info.SubtargetFeatures) {
2454       const SubtargetFeatureInfo &SFI = SF.second;
2455       // FIXME: Totally just a placeholder name to get the algorithm working.
2456       OS << "  case " << SFI.getEnumName() << ": return \""
2457          << SFI.TheDef->getValueAsString("PredicateName") << "\";\n";
2458     }
2459     OS << "  default: return \"(unknown)\";\n";
2460     OS << "  }\n";
2461   } else {
2462     // Nothing to emit, so skip the switch
2463     OS << "  return \"(unknown)\";\n";
2464   }
2465   OS << "}\n\n";
2466 }
2467 
2468 /// emitComputeAvailableFeatures - Emit the function to compute the list of
2469 /// available features given a subtarget.
2470 static void emitComputeAvailableFeatures(AsmMatcherInfo &Info,
2471                                          raw_ostream &OS) {
2472   std::string ClassName =
2473     Info.AsmParser->getValueAsString("AsmParserClassName");
2474 
2475   OS << "uint64_t " << Info.Target.getName() << ClassName << "::\n"
2476      << "ComputeAvailableFeatures(const FeatureBitset& FB) const {\n";
2477   OS << "  uint64_t Features = 0;\n";
2478   for (const auto &SF : Info.SubtargetFeatures) {
2479     const SubtargetFeatureInfo &SFI = SF.second;
2480 
2481     OS << "  if (";
2482     std::string CondStorage =
2483       SFI.TheDef->getValueAsString("AssemblerCondString");
2484     StringRef Conds = CondStorage;
2485     std::pair<StringRef,StringRef> Comma = Conds.split(',');
2486     bool First = true;
2487     do {
2488       if (!First)
2489         OS << " && ";
2490 
2491       bool Neg = false;
2492       StringRef Cond = Comma.first;
2493       if (Cond[0] == '!') {
2494         Neg = true;
2495         Cond = Cond.substr(1);
2496       }
2497 
2498       OS << "(";
2499       if (Neg)
2500         OS << "!";
2501       OS << "FB[" << Info.Target.getName() << "::" << Cond << "])";
2502 
2503       if (Comma.second.empty())
2504         break;
2505 
2506       First = false;
2507       Comma = Comma.second.split(',');
2508     } while (true);
2509 
2510     OS << ")\n";
2511     OS << "    Features |= " << SFI.getEnumName() << ";\n";
2512   }
2513   OS << "  return Features;\n";
2514   OS << "}\n\n";
2515 }
2516 
2517 static std::string GetAliasRequiredFeatures(Record *R,
2518                                             const AsmMatcherInfo &Info) {
2519   std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
2520   std::string Result;
2521   unsigned NumFeatures = 0;
2522   for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
2523     const SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
2524 
2525     if (!F)
2526       PrintFatalError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
2527                     "' is not marked as an AssemblerPredicate!");
2528 
2529     if (NumFeatures)
2530       Result += '|';
2531 
2532     Result += F->getEnumName();
2533     ++NumFeatures;
2534   }
2535 
2536   if (NumFeatures > 1)
2537     Result = '(' + Result + ')';
2538   return Result;
2539 }
2540 
2541 static void emitMnemonicAliasVariant(raw_ostream &OS,const AsmMatcherInfo &Info,
2542                                      std::vector<Record*> &Aliases,
2543                                      unsigned Indent = 0,
2544                                   StringRef AsmParserVariantName = StringRef()){
2545   // Keep track of all the aliases from a mnemonic.  Use an std::map so that the
2546   // iteration order of the map is stable.
2547   std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
2548 
2549   for (Record *R : Aliases) {
2550     // FIXME: Allow AssemblerVariantName to be a comma separated list.
2551     std::string AsmVariantName = R->getValueAsString("AsmVariantName");
2552     if (AsmVariantName != AsmParserVariantName)
2553       continue;
2554     AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
2555   }
2556   if (AliasesFromMnemonic.empty())
2557     return;
2558 
2559   // Process each alias a "from" mnemonic at a time, building the code executed
2560   // by the string remapper.
2561   std::vector<StringMatcher::StringPair> Cases;
2562   for (const auto &AliasEntry : AliasesFromMnemonic) {
2563     const std::vector<Record*> &ToVec = AliasEntry.second;
2564 
2565     // Loop through each alias and emit code that handles each case.  If there
2566     // are two instructions without predicates, emit an error.  If there is one,
2567     // emit it last.
2568     std::string MatchCode;
2569     int AliasWithNoPredicate = -1;
2570 
2571     for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
2572       Record *R = ToVec[i];
2573       std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
2574 
2575       // If this unconditionally matches, remember it for later and diagnose
2576       // duplicates.
2577       if (FeatureMask.empty()) {
2578         if (AliasWithNoPredicate != -1) {
2579           // We can't have two aliases from the same mnemonic with no predicate.
2580           PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
2581                      "two MnemonicAliases with the same 'from' mnemonic!");
2582           PrintFatalError(R->getLoc(), "this is the other MnemonicAlias.");
2583         }
2584 
2585         AliasWithNoPredicate = i;
2586         continue;
2587       }
2588       if (R->getValueAsString("ToMnemonic") == AliasEntry.first)
2589         PrintFatalError(R->getLoc(), "MnemonicAlias to the same string");
2590 
2591       if (!MatchCode.empty())
2592         MatchCode += "else ";
2593       MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
2594       MatchCode += "  Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
2595     }
2596 
2597     if (AliasWithNoPredicate != -1) {
2598       Record *R = ToVec[AliasWithNoPredicate];
2599       if (!MatchCode.empty())
2600         MatchCode += "else\n  ";
2601       MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
2602     }
2603 
2604     MatchCode += "return;";
2605 
2606     Cases.push_back(std::make_pair(AliasEntry.first, MatchCode));
2607   }
2608   StringMatcher("Mnemonic", Cases, OS).Emit(Indent);
2609 }
2610 
2611 /// emitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
2612 /// emit a function for them and return true, otherwise return false.
2613 static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info,
2614                                 CodeGenTarget &Target) {
2615   // Ignore aliases when match-prefix is set.
2616   if (!MatchPrefix.empty())
2617     return false;
2618 
2619   std::vector<Record*> Aliases =
2620     Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
2621   if (Aliases.empty()) return false;
2622 
2623   OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
2624     "uint64_t Features, unsigned VariantID) {\n";
2625   OS << "  switch (VariantID) {\n";
2626   unsigned VariantCount = Target.getAsmParserVariantCount();
2627   for (unsigned VC = 0; VC != VariantCount; ++VC) {
2628     Record *AsmVariant = Target.getAsmParserVariant(VC);
2629     int AsmParserVariantNo = AsmVariant->getValueAsInt("Variant");
2630     std::string AsmParserVariantName = AsmVariant->getValueAsString("Name");
2631     OS << "    case " << AsmParserVariantNo << ":\n";
2632     emitMnemonicAliasVariant(OS, Info, Aliases, /*Indent=*/2,
2633                              AsmParserVariantName);
2634     OS << "    break;\n";
2635   }
2636   OS << "  }\n";
2637 
2638   // Emit aliases that apply to all variants.
2639   emitMnemonicAliasVariant(OS, Info, Aliases);
2640 
2641   OS << "}\n\n";
2642 
2643   return true;
2644 }
2645 
2646 static void emitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
2647                               const AsmMatcherInfo &Info, StringRef ClassName,
2648                               StringToOffsetTable &StringTable,
2649                               unsigned MaxMnemonicIndex, bool HasMnemonicFirst) {
2650   unsigned MaxMask = 0;
2651   for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) {
2652     MaxMask |= OMI.OperandMask;
2653   }
2654 
2655   // Emit the static custom operand parsing table;
2656   OS << "namespace {\n";
2657   OS << "  struct OperandMatchEntry {\n";
2658   OS << "    " << getMinimalRequiredFeaturesType(Info)
2659                << " RequiredFeatures;\n";
2660   OS << "    " << getMinimalTypeForRange(MaxMnemonicIndex)
2661                << " Mnemonic;\n";
2662   OS << "    " << getMinimalTypeForRange(std::distance(
2663                       Info.Classes.begin(), Info.Classes.end())) << " Class;\n";
2664   OS << "    " << getMinimalTypeForRange(MaxMask)
2665                << " OperandMask;\n\n";
2666   OS << "    StringRef getMnemonic() const {\n";
2667   OS << "      return StringRef(MnemonicTable + Mnemonic + 1,\n";
2668   OS << "                       MnemonicTable[Mnemonic]);\n";
2669   OS << "    }\n";
2670   OS << "  };\n\n";
2671 
2672   OS << "  // Predicate for searching for an opcode.\n";
2673   OS << "  struct LessOpcodeOperand {\n";
2674   OS << "    bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
2675   OS << "      return LHS.getMnemonic()  < RHS;\n";
2676   OS << "    }\n";
2677   OS << "    bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
2678   OS << "      return LHS < RHS.getMnemonic();\n";
2679   OS << "    }\n";
2680   OS << "    bool operator()(const OperandMatchEntry &LHS,";
2681   OS << " const OperandMatchEntry &RHS) {\n";
2682   OS << "      return LHS.getMnemonic() < RHS.getMnemonic();\n";
2683   OS << "    }\n";
2684   OS << "  };\n";
2685 
2686   OS << "} // end anonymous namespace.\n\n";
2687 
2688   OS << "static const OperandMatchEntry OperandMatchTable["
2689      << Info.OperandMatchInfo.size() << "] = {\n";
2690 
2691   OS << "  /* Operand List Mask, Mnemonic, Operand Class, Features */\n";
2692   for (const OperandMatchEntry &OMI : Info.OperandMatchInfo) {
2693     const MatchableInfo &II = *OMI.MI;
2694 
2695     OS << "  { ";
2696 
2697     // Write the required features mask.
2698     if (!II.RequiredFeatures.empty()) {
2699       for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2700         if (i) OS << "|";
2701         OS << II.RequiredFeatures[i]->getEnumName();
2702       }
2703     } else
2704       OS << "0";
2705 
2706     // Store a pascal-style length byte in the mnemonic.
2707     std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str();
2708     OS << ", " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
2709        << " /* " << II.Mnemonic << " */, ";
2710 
2711     OS << OMI.CI->Name;
2712 
2713     OS << ", " << OMI.OperandMask;
2714     OS << " /* ";
2715     bool printComma = false;
2716     for (int i = 0, e = 31; i !=e; ++i)
2717       if (OMI.OperandMask & (1 << i)) {
2718         if (printComma)
2719           OS << ", ";
2720         OS << i;
2721         printComma = true;
2722       }
2723     OS << " */";
2724 
2725     OS << " },\n";
2726   }
2727   OS << "};\n\n";
2728 
2729   // Emit the operand class switch to call the correct custom parser for
2730   // the found operand class.
2731   OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2732      << Target.getName() << ClassName << "::\n"
2733      << "tryCustomParseOperand(OperandVector"
2734      << " &Operands,\n                      unsigned MCK) {\n\n"
2735      << "  switch(MCK) {\n";
2736 
2737   for (const auto &CI : Info.Classes) {
2738     if (CI.ParserMethod.empty())
2739       continue;
2740     OS << "  case " << CI.Name << ":\n"
2741        << "    return " << CI.ParserMethod << "(Operands);\n";
2742   }
2743 
2744   OS << "  default:\n";
2745   OS << "    return MatchOperand_NoMatch;\n";
2746   OS << "  }\n";
2747   OS << "  return MatchOperand_NoMatch;\n";
2748   OS << "}\n\n";
2749 
2750   // Emit the static custom operand parser. This code is very similar with
2751   // the other matcher. Also use MatchResultTy here just in case we go for
2752   // a better error handling.
2753   OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2754      << Target.getName() << ClassName << "::\n"
2755      << "MatchOperandParserImpl(OperandVector"
2756      << " &Operands,\n                       StringRef Mnemonic) {\n";
2757 
2758   // Emit code to get the available features.
2759   OS << "  // Get the current feature set.\n";
2760   OS << "  uint64_t AvailableFeatures = getAvailableFeatures();\n\n";
2761 
2762   OS << "  // Get the next operand index.\n";
2763   OS << "  unsigned NextOpNum = Operands.size()"
2764      << (HasMnemonicFirst ? " - 1" : "") << ";\n";
2765 
2766   // Emit code to search the table.
2767   OS << "  // Search the table.\n";
2768   if (HasMnemonicFirst) {
2769     OS << "  auto MnemonicRange =\n";
2770     OS << "    std::equal_range(std::begin(OperandMatchTable), "
2771           "std::end(OperandMatchTable),\n";
2772     OS << "                     Mnemonic, LessOpcodeOperand());\n\n";
2773   } else {
2774     OS << "  auto MnemonicRange = std::make_pair(std::begin(OperandMatchTable),"
2775           " std::end(OperandMatchTable));\n";
2776     OS << "  if (!Mnemonic.empty())\n";
2777     OS << "    MnemonicRange =\n";
2778     OS << "      std::equal_range(std::begin(OperandMatchTable), "
2779           "std::end(OperandMatchTable),\n";
2780     OS << "                       Mnemonic, LessOpcodeOperand());\n\n";
2781   }
2782 
2783   OS << "  if (MnemonicRange.first == MnemonicRange.second)\n";
2784   OS << "    return MatchOperand_NoMatch;\n\n";
2785 
2786   OS << "  for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2787      << "       *ie = MnemonicRange.second; it != ie; ++it) {\n";
2788 
2789   OS << "    // equal_range guarantees that instruction mnemonic matches.\n";
2790   OS << "    assert(Mnemonic == it->getMnemonic());\n\n";
2791 
2792   // Emit check that the required features are available.
2793   OS << "    // check if the available features match\n";
2794   OS << "    if ((AvailableFeatures & it->RequiredFeatures) "
2795      << "!= it->RequiredFeatures) {\n";
2796   OS << "      continue;\n";
2797   OS << "    }\n\n";
2798 
2799   // Emit check to ensure the operand number matches.
2800   OS << "    // check if the operand in question has a custom parser.\n";
2801   OS << "    if (!(it->OperandMask & (1 << NextOpNum)))\n";
2802   OS << "      continue;\n\n";
2803 
2804   // Emit call to the custom parser method
2805   OS << "    // call custom parse method to handle the operand\n";
2806   OS << "    OperandMatchResultTy Result = ";
2807   OS << "tryCustomParseOperand(Operands, it->Class);\n";
2808   OS << "    if (Result != MatchOperand_NoMatch)\n";
2809   OS << "      return Result;\n";
2810   OS << "  }\n\n";
2811 
2812   OS << "  // Okay, we had no match.\n";
2813   OS << "  return MatchOperand_NoMatch;\n";
2814   OS << "}\n\n";
2815 }
2816 
2817 void AsmMatcherEmitter::run(raw_ostream &OS) {
2818   CodeGenTarget Target(Records);
2819   Record *AsmParser = Target.getAsmParser();
2820   std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2821 
2822   // Compute the information on the instructions to match.
2823   AsmMatcherInfo Info(AsmParser, Target, Records);
2824   Info.buildInfo();
2825 
2826   // Sort the instruction table using the partial order on classes. We use
2827   // stable_sort to ensure that ambiguous instructions are still
2828   // deterministically ordered.
2829   std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2830                    [](const std::unique_ptr<MatchableInfo> &a,
2831                       const std::unique_ptr<MatchableInfo> &b){
2832                      return *a < *b;});
2833 
2834 #ifndef NDEBUG
2835   // Verify that the table is now sorted
2836   for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E;
2837        ++I) {
2838     for (auto J = I; J != E; ++J) {
2839       assert(!(**J < **I));
2840     }
2841   }
2842 #endif // NDEBUG
2843 
2844   DEBUG_WITH_TYPE("instruction_info", {
2845       for (const auto &MI : Info.Matchables)
2846         MI->dump();
2847     });
2848 
2849   // Check for ambiguous matchables.
2850   DEBUG_WITH_TYPE("ambiguous_instrs", {
2851     unsigned NumAmbiguous = 0;
2852     for (auto I = Info.Matchables.begin(), E = Info.Matchables.end(); I != E;
2853          ++I) {
2854       for (auto J = std::next(I); J != E; ++J) {
2855         const MatchableInfo &A = **I;
2856         const MatchableInfo &B = **J;
2857 
2858         if (A.couldMatchAmbiguouslyWith(B)) {
2859           errs() << "warning: ambiguous matchables:\n";
2860           A.dump();
2861           errs() << "\nis incomparable with:\n";
2862           B.dump();
2863           errs() << "\n\n";
2864           ++NumAmbiguous;
2865         }
2866       }
2867     }
2868     if (NumAmbiguous)
2869       errs() << "warning: " << NumAmbiguous
2870              << " ambiguous matchables!\n";
2871   });
2872 
2873   // Compute the information on the custom operand parsing.
2874   Info.buildOperandMatchInfo();
2875 
2876   bool HasMnemonicFirst = AsmParser->getValueAsBit("HasMnemonicFirst");
2877   bool HasOptionalOperands = Info.hasOptionalOperands();
2878 
2879   // Write the output.
2880 
2881   // Information for the class declaration.
2882   OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2883   OS << "#undef GET_ASSEMBLER_HEADER\n";
2884   OS << "  // This should be included into the middle of the declaration of\n";
2885   OS << "  // your subclasses implementation of MCTargetAsmParser.\n";
2886   OS << "  uint64_t ComputeAvailableFeatures(const FeatureBitset& FB) const;\n";
2887   if (HasOptionalOperands) {
2888     OS << "  void convertToMCInst(unsigned Kind, MCInst &Inst, "
2889        << "unsigned Opcode,\n"
2890        << "                       const OperandVector &Operands,\n"
2891        << "                       const SmallBitVector &OptionalOperandsMask);\n";
2892   } else {
2893     OS << "  void convertToMCInst(unsigned Kind, MCInst &Inst, "
2894        << "unsigned Opcode,\n"
2895        << "                       const OperandVector &Operands);\n";
2896   }
2897   OS << "  void convertToMapAndConstraints(unsigned Kind,\n                ";
2898   OS << "           const OperandVector &Operands) override;\n";
2899   if (HasMnemonicFirst)
2900     OS << "  bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID);\n";
2901   OS << "  unsigned MatchInstructionImpl(const OperandVector &Operands,\n"
2902      << "                                MCInst &Inst,\n"
2903      << "                                uint64_t &ErrorInfo,"
2904      << " bool matchingInlineAsm,\n"
2905      << "                                unsigned VariantID = 0);\n";
2906 
2907   if (!Info.OperandMatchInfo.empty()) {
2908     OS << "\n  enum OperandMatchResultTy {\n";
2909     OS << "    MatchOperand_Success,    // operand matched successfully\n";
2910     OS << "    MatchOperand_NoMatch,    // operand did not match\n";
2911     OS << "    MatchOperand_ParseFail   // operand matched but had errors\n";
2912     OS << "  };\n";
2913     OS << "  OperandMatchResultTy MatchOperandParserImpl(\n";
2914     OS << "    OperandVector &Operands,\n";
2915     OS << "    StringRef Mnemonic);\n";
2916 
2917     OS << "  OperandMatchResultTy tryCustomParseOperand(\n";
2918     OS << "    OperandVector &Operands,\n";
2919     OS << "    unsigned MCK);\n\n";
2920   }
2921 
2922   OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2923 
2924   // Emit the operand match diagnostic enum names.
2925   OS << "\n#ifdef GET_OPERAND_DIAGNOSTIC_TYPES\n";
2926   OS << "#undef GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
2927   emitOperandDiagnosticTypes(Info, OS);
2928   OS << "#endif // GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
2929 
2930   OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2931   OS << "#undef GET_REGISTER_MATCHER\n\n";
2932 
2933   // Emit the subtarget feature enumeration.
2934   emitSubtargetFeatureFlagEnumeration(Info, OS);
2935 
2936   // Emit the function to match a register name to number.
2937   // This should be omitted for Mips target
2938   if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterName"))
2939     emitMatchRegisterName(Target, AsmParser, OS);
2940 
2941   if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterAltName"))
2942     emitMatchRegisterAltName(Target, AsmParser, OS);
2943 
2944   OS << "#endif // GET_REGISTER_MATCHER\n\n";
2945 
2946   OS << "\n#ifdef GET_SUBTARGET_FEATURE_NAME\n";
2947   OS << "#undef GET_SUBTARGET_FEATURE_NAME\n\n";
2948 
2949   // Generate the helper function to get the names for subtarget features.
2950   emitGetSubtargetFeatureName(Info, OS);
2951 
2952   OS << "#endif // GET_SUBTARGET_FEATURE_NAME\n\n";
2953 
2954   OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2955   OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
2956 
2957   // Generate the function that remaps for mnemonic aliases.
2958   bool HasMnemonicAliases = emitMnemonicAliases(OS, Info, Target);
2959 
2960   // Generate the convertToMCInst function to convert operands into an MCInst.
2961   // Also, generate the convertToMapAndConstraints function for MS-style inline
2962   // assembly.  The latter doesn't actually generate a MCInst.
2963   emitConvertFuncs(Target, ClassName, Info.Matchables, HasMnemonicFirst,
2964                    HasOptionalOperands, OS);
2965 
2966   // Emit the enumeration for classes which participate in matching.
2967   emitMatchClassEnumeration(Target, Info.Classes, OS);
2968 
2969   // Emit the routine to match token strings to their match class.
2970   emitMatchTokenString(Target, Info.Classes, OS);
2971 
2972   // Emit the subclass predicate routine.
2973   emitIsSubclass(Target, Info.Classes, OS);
2974 
2975   // Emit the routine to validate an operand against a match class.
2976   emitValidateOperandClass(Info, OS);
2977 
2978   // Emit the available features compute function.
2979   emitComputeAvailableFeatures(Info, OS);
2980 
2981   StringToOffsetTable StringTable;
2982 
2983   size_t MaxNumOperands = 0;
2984   unsigned MaxMnemonicIndex = 0;
2985   bool HasDeprecation = false;
2986   for (const auto &MI : Info.Matchables) {
2987     MaxNumOperands = std::max(MaxNumOperands, MI->AsmOperands.size());
2988     HasDeprecation |= MI->HasDeprecation;
2989 
2990     // Store a pascal-style length byte in the mnemonic.
2991     std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.str();
2992     MaxMnemonicIndex = std::max(MaxMnemonicIndex,
2993                         StringTable.GetOrAddStringOffset(LenMnemonic, false));
2994   }
2995 
2996   OS << "static const char *const MnemonicTable =\n";
2997   StringTable.EmitString(OS);
2998   OS << ";\n\n";
2999 
3000   // Emit the static match table; unused classes get initalized to 0 which is
3001   // guaranteed to be InvalidMatchClass.
3002   //
3003   // FIXME: We can reduce the size of this table very easily. First, we change
3004   // it so that store the kinds in separate bit-fields for each index, which
3005   // only needs to be the max width used for classes at that index (we also need
3006   // to reject based on this during classification). If we then make sure to
3007   // order the match kinds appropriately (putting mnemonics last), then we
3008   // should only end up using a few bits for each class, especially the ones
3009   // following the mnemonic.
3010   OS << "namespace {\n";
3011   OS << "  struct MatchEntry {\n";
3012   OS << "    " << getMinimalTypeForRange(MaxMnemonicIndex)
3013                << " Mnemonic;\n";
3014   OS << "    uint16_t Opcode;\n";
3015   OS << "    " << getMinimalTypeForRange(Info.Matchables.size())
3016                << " ConvertFn;\n";
3017   OS << "    " << getMinimalRequiredFeaturesType(Info)
3018                << " RequiredFeatures;\n";
3019   OS << "    " << getMinimalTypeForRange(
3020                       std::distance(Info.Classes.begin(), Info.Classes.end()))
3021      << " Classes[" << MaxNumOperands << "];\n";
3022   OS << "    StringRef getMnemonic() const {\n";
3023   OS << "      return StringRef(MnemonicTable + Mnemonic + 1,\n";
3024   OS << "                       MnemonicTable[Mnemonic]);\n";
3025   OS << "    }\n";
3026   OS << "  };\n\n";
3027 
3028   OS << "  // Predicate for searching for an opcode.\n";
3029   OS << "  struct LessOpcode {\n";
3030   OS << "    bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
3031   OS << "      return LHS.getMnemonic() < RHS;\n";
3032   OS << "    }\n";
3033   OS << "    bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
3034   OS << "      return LHS < RHS.getMnemonic();\n";
3035   OS << "    }\n";
3036   OS << "    bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
3037   OS << "      return LHS.getMnemonic() < RHS.getMnemonic();\n";
3038   OS << "    }\n";
3039   OS << "  };\n";
3040 
3041   OS << "} // end anonymous namespace.\n\n";
3042 
3043   unsigned VariantCount = Target.getAsmParserVariantCount();
3044   for (unsigned VC = 0; VC != VariantCount; ++VC) {
3045     Record *AsmVariant = Target.getAsmParserVariant(VC);
3046     int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
3047 
3048     OS << "static const MatchEntry MatchTable" << VC << "[] = {\n";
3049 
3050     for (const auto &MI : Info.Matchables) {
3051       if (MI->AsmVariantID != AsmVariantNo)
3052         continue;
3053 
3054       // Store a pascal-style length byte in the mnemonic.
3055       std::string LenMnemonic = char(MI->Mnemonic.size()) + MI->Mnemonic.str();
3056       OS << "  { " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
3057          << " /* " << MI->Mnemonic << " */, "
3058          << Target.getName() << "::"
3059          << MI->getResultInst()->TheDef->getName() << ", "
3060          << MI->ConversionFnKind << ", ";
3061 
3062       // Write the required features mask.
3063       if (!MI->RequiredFeatures.empty()) {
3064         for (unsigned i = 0, e = MI->RequiredFeatures.size(); i != e; ++i) {
3065           if (i) OS << "|";
3066           OS << MI->RequiredFeatures[i]->getEnumName();
3067         }
3068       } else
3069         OS << "0";
3070 
3071       OS << ", { ";
3072       for (unsigned i = 0, e = MI->AsmOperands.size(); i != e; ++i) {
3073         const MatchableInfo::AsmOperand &Op = MI->AsmOperands[i];
3074 
3075         if (i) OS << ", ";
3076         OS << Op.Class->Name;
3077       }
3078       OS << " }, },\n";
3079     }
3080 
3081     OS << "};\n\n";
3082   }
3083 
3084   // A method to determine if a mnemonic is in the list.
3085   if (HasMnemonicFirst) {
3086     OS << "bool " << Target.getName() << ClassName << "::\n"
3087        << "mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) {\n";
3088     OS << "  // Find the appropriate table for this asm variant.\n";
3089     OS << "  const MatchEntry *Start, *End;\n";
3090     OS << "  switch (VariantID) {\n";
3091     OS << "  default: llvm_unreachable(\"invalid variant!\");\n";
3092     for (unsigned VC = 0; VC != VariantCount; ++VC) {
3093       Record *AsmVariant = Target.getAsmParserVariant(VC);
3094       int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
3095       OS << "  case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3096          << "); End = std::end(MatchTable" << VC << "); break;\n";
3097     }
3098     OS << "  }\n";
3099     OS << "  // Search the table.\n";
3100     OS << "  auto MnemonicRange = ";
3101     OS << "std::equal_range(Start, End, Mnemonic, LessOpcode());\n";
3102     OS << "  return MnemonicRange.first != MnemonicRange.second;\n";
3103     OS << "}\n\n";
3104   }
3105 
3106   // Finally, build the match function.
3107   OS << "unsigned " << Target.getName() << ClassName << "::\n"
3108      << "MatchInstructionImpl(const OperandVector &Operands,\n";
3109   OS << "                     MCInst &Inst, uint64_t &ErrorInfo,\n"
3110      << "                     bool matchingInlineAsm, unsigned VariantID) {\n";
3111 
3112   OS << "  // Eliminate obvious mismatches.\n";
3113   OS << "  if (Operands.size() > "
3114      << (MaxNumOperands + HasMnemonicFirst) << ") {\n";
3115   OS << "    ErrorInfo = "
3116      << (MaxNumOperands + HasMnemonicFirst) << ";\n";
3117   OS << "    return Match_InvalidOperand;\n";
3118   OS << "  }\n\n";
3119 
3120   // Emit code to get the available features.
3121   OS << "  // Get the current feature set.\n";
3122   OS << "  uint64_t AvailableFeatures = getAvailableFeatures();\n\n";
3123 
3124   OS << "  // Get the instruction mnemonic, which is the first token.\n";
3125   if (HasMnemonicFirst) {
3126     OS << "  StringRef Mnemonic = ((" << Target.getName()
3127        << "Operand&)*Operands[0]).getToken();\n\n";
3128   } else {
3129     OS << "  StringRef Mnemonic;\n";
3130     OS << "  if (Operands[0]->isToken())\n";
3131     OS << "    Mnemonic = ((" << Target.getName()
3132        << "Operand&)*Operands[0]).getToken();\n\n";
3133   }
3134 
3135   if (HasMnemonicAliases) {
3136     OS << "  // Process all MnemonicAliases to remap the mnemonic.\n";
3137     OS << "  applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);\n\n";
3138   }
3139 
3140   // Emit code to compute the class list for this operand vector.
3141   OS << "  // Some state to try to produce better error messages.\n";
3142   OS << "  bool HadMatchOtherThanFeatures = false;\n";
3143   OS << "  bool HadMatchOtherThanPredicate = false;\n";
3144   OS << "  unsigned RetCode = Match_InvalidOperand;\n";
3145   OS << "  uint64_t MissingFeatures = ~0ULL;\n";
3146   if (HasOptionalOperands) {
3147     OS << "  SmallBitVector OptionalOperandsMask(" << MaxNumOperands << ");\n";
3148   }
3149   OS << "  // Set ErrorInfo to the operand that mismatches if it is\n";
3150   OS << "  // wrong for all instances of the instruction.\n";
3151   OS << "  ErrorInfo = ~0ULL;\n";
3152 
3153   // Emit code to search the table.
3154   OS << "  // Find the appropriate table for this asm variant.\n";
3155   OS << "  const MatchEntry *Start, *End;\n";
3156   OS << "  switch (VariantID) {\n";
3157   OS << "  default: llvm_unreachable(\"invalid variant!\");\n";
3158   for (unsigned VC = 0; VC != VariantCount; ++VC) {
3159     Record *AsmVariant = Target.getAsmParserVariant(VC);
3160     int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
3161     OS << "  case " << AsmVariantNo << ": Start = std::begin(MatchTable" << VC
3162        << "); End = std::end(MatchTable" << VC << "); break;\n";
3163   }
3164   OS << "  }\n";
3165 
3166   OS << "  // Search the table.\n";
3167   if (HasMnemonicFirst) {
3168     OS << "  auto MnemonicRange = "
3169           "std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n";
3170   } else {
3171     OS << "  auto MnemonicRange = std::make_pair(Start, End);\n";
3172     OS << "  unsigned SIndex = Mnemonic.empty() ? 0 : 1;\n";
3173     OS << "  if (!Mnemonic.empty())\n";
3174     OS << "    MnemonicRange = "
3175           "std::equal_range(Start, End, Mnemonic.lower(), LessOpcode());\n\n";
3176   }
3177 
3178   OS << "  // Return a more specific error code if no mnemonics match.\n";
3179   OS << "  if (MnemonicRange.first == MnemonicRange.second)\n";
3180   OS << "    return Match_MnemonicFail;\n\n";
3181 
3182   OS << "  for (const MatchEntry *it = MnemonicRange.first, "
3183      << "*ie = MnemonicRange.second;\n";
3184   OS << "       it != ie; ++it) {\n";
3185 
3186   if (HasMnemonicFirst) {
3187     OS << "    // equal_range guarantees that instruction mnemonic matches.\n";
3188     OS << "    assert(Mnemonic == it->getMnemonic());\n";
3189   }
3190 
3191   // Emit check that the subclasses match.
3192   OS << "    bool OperandsValid = true;\n";
3193   if (HasOptionalOperands) {
3194     OS << "    OptionalOperandsMask.reset(0, " << MaxNumOperands << ");\n";
3195   }
3196   OS << "    for (unsigned FormalIdx = " << (HasMnemonicFirst ? "0" : "SIndex")
3197      << ", ActualIdx = " << (HasMnemonicFirst ? "1" : "SIndex")
3198      << "; FormalIdx != " << MaxNumOperands << "; ++FormalIdx) {\n";
3199   OS << "      auto Formal = "
3200      << "static_cast<MatchClassKind>(it->Classes[FormalIdx]);\n";
3201   OS << "      if (ActualIdx >= Operands.size()) {\n";
3202   OS << "        OperandsValid = (Formal == " <<"InvalidMatchClass) || "
3203                                  "isSubclass(Formal, OptionalMatchClass);\n";
3204   OS << "        if (!OperandsValid) ErrorInfo = ActualIdx;\n";
3205   if (HasOptionalOperands) {
3206     OS << "        OptionalOperandsMask.set(FormalIdx, " << MaxNumOperands
3207        << ");\n";
3208   }
3209   OS << "        break;\n";
3210   OS << "      }\n";
3211   OS << "      MCParsedAsmOperand &Actual = *Operands[ActualIdx];\n";
3212   OS << "      unsigned Diag = validateOperandClass(Actual, Formal);\n";
3213   OS << "      if (Diag == Match_Success) {\n";
3214   OS << "        ++ActualIdx;\n";
3215   OS << "        continue;\n";
3216   OS << "      }\n";
3217   OS << "      // If the generic handler indicates an invalid operand\n";
3218   OS << "      // failure, check for a special case.\n";
3219   OS << "      if (Diag == Match_InvalidOperand) {\n";
3220   OS << "        Diag = validateTargetOperandClass(Actual, Formal);\n";
3221   OS << "        if (Diag == Match_Success) {\n";
3222   OS << "          ++ActualIdx;\n";
3223   OS << "          continue;\n";
3224   OS << "        }\n";
3225   OS << "      }\n";
3226   OS << "      // If current formal operand wasn't matched and it is optional\n"
3227      << "      // then try to match next formal operand\n";
3228   OS << "      if (Diag == Match_InvalidOperand "
3229      << "&& isSubclass(Formal, OptionalMatchClass)) {\n";
3230   if (HasOptionalOperands) {
3231     OS << "        OptionalOperandsMask.set(FormalIdx);\n";
3232   }
3233   OS << "        continue;\n";
3234   OS << "      }\n";
3235   OS << "      // If this operand is broken for all of the instances of this\n";
3236   OS << "      // mnemonic, keep track of it so we can report loc info.\n";
3237   OS << "      // If we already had a match that only failed due to a\n";
3238   OS << "      // target predicate, that diagnostic is preferred.\n";
3239   OS << "      if (!HadMatchOtherThanPredicate &&\n";
3240   OS << "          (it == MnemonicRange.first || ErrorInfo <= ActualIdx)) {\n";
3241   OS << "        ErrorInfo = ActualIdx;\n";
3242   OS << "        // InvalidOperand is the default. Prefer specificity.\n";
3243   OS << "        if (Diag != Match_InvalidOperand)\n";
3244   OS << "          RetCode = Diag;\n";
3245   OS << "      }\n";
3246   OS << "      // Otherwise, just reject this instance of the mnemonic.\n";
3247   OS << "      OperandsValid = false;\n";
3248   OS << "      break;\n";
3249   OS << "    }\n\n";
3250 
3251   OS << "    if (!OperandsValid) continue;\n";
3252 
3253   // Emit check that the required features are available.
3254   OS << "    if ((AvailableFeatures & it->RequiredFeatures) "
3255      << "!= it->RequiredFeatures) {\n";
3256   OS << "      HadMatchOtherThanFeatures = true;\n";
3257   OS << "      uint64_t NewMissingFeatures = it->RequiredFeatures & "
3258         "~AvailableFeatures;\n";
3259   OS << "      if (countPopulation(NewMissingFeatures) <=\n"
3260         "          countPopulation(MissingFeatures))\n";
3261   OS << "        MissingFeatures = NewMissingFeatures;\n";
3262   OS << "      continue;\n";
3263   OS << "    }\n";
3264   OS << "\n";
3265   OS << "    Inst.clear();\n\n";
3266   OS << "    Inst.setOpcode(it->Opcode);\n";
3267   // Verify the instruction with the target-specific match predicate function.
3268   OS << "    // We have a potential match but have not rendered the operands.\n"
3269      << "    // Check the target predicate to handle any context sensitive\n"
3270         "    // constraints.\n"
3271      << "    // For example, Ties that are referenced multiple times must be\n"
3272         "    // checked here to ensure the input is the same for each match\n"
3273         "    // constraints. If we leave it any later the ties will have been\n"
3274         "    // canonicalized\n"
3275      << "    unsigned MatchResult;\n"
3276      << "    if ((MatchResult = checkEarlyTargetMatchPredicate(Inst, "
3277         "Operands)) != Match_Success) {\n"
3278      << "      Inst.clear();\n"
3279      << "      RetCode = MatchResult;\n"
3280      << "      HadMatchOtherThanPredicate = true;\n"
3281      << "      continue;\n"
3282      << "    }\n\n";
3283   OS << "    if (matchingInlineAsm) {\n";
3284   OS << "      convertToMapAndConstraints(it->ConvertFn, Operands);\n";
3285   OS << "      return Match_Success;\n";
3286   OS << "    }\n\n";
3287   OS << "    // We have selected a definite instruction, convert the parsed\n"
3288      << "    // operands into the appropriate MCInst.\n";
3289   if (HasOptionalOperands) {
3290     OS << "    convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands,\n"
3291        << "                    OptionalOperandsMask);\n";
3292   } else {
3293     OS << "    convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
3294   }
3295   OS << "\n";
3296 
3297   // Verify the instruction with the target-specific match predicate function.
3298   OS << "    // We have a potential match. Check the target predicate to\n"
3299      << "    // handle any context sensitive constraints.\n"
3300      << "    if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
3301      << " Match_Success) {\n"
3302      << "      Inst.clear();\n"
3303      << "      RetCode = MatchResult;\n"
3304      << "      HadMatchOtherThanPredicate = true;\n"
3305      << "      continue;\n"
3306      << "    }\n\n";
3307 
3308   // Call the post-processing function, if used.
3309   std::string InsnCleanupFn =
3310     AsmParser->getValueAsString("AsmParserInstCleanup");
3311   if (!InsnCleanupFn.empty())
3312     OS << "    " << InsnCleanupFn << "(Inst);\n";
3313 
3314   if (HasDeprecation) {
3315     OS << "    std::string Info;\n";
3316     OS << "    if (MII.get(Inst.getOpcode()).getDeprecatedInfo(Inst, getSTI(), Info)) {\n";
3317     OS << "      SMLoc Loc = ((" << Target.getName()
3318        << "Operand&)*Operands[0]).getStartLoc();\n";
3319     OS << "      getParser().Warning(Loc, Info, None);\n";
3320     OS << "    }\n";
3321   }
3322 
3323   OS << "    return Match_Success;\n";
3324   OS << "  }\n\n";
3325 
3326   OS << "  // Okay, we had no match.  Try to return a useful error code.\n";
3327   OS << "  if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)\n";
3328   OS << "    return RetCode;\n\n";
3329   OS << "  // Missing feature matches return which features were missing\n";
3330   OS << "  ErrorInfo = MissingFeatures;\n";
3331   OS << "  return Match_MissingFeature;\n";
3332   OS << "}\n\n";
3333 
3334   if (!Info.OperandMatchInfo.empty())
3335     emitCustomOperandParsing(OS, Target, Info, ClassName, StringTable,
3336                              MaxMnemonicIndex, HasMnemonicFirst);
3337 
3338   OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
3339 }
3340 
3341 namespace llvm {
3342 
3343 void EmitAsmMatcher(RecordKeeper &RK, raw_ostream &OS) {
3344   emitSourceFileHeader("Assembly Matcher Source Fragment", OS);
3345   AsmMatcherEmitter(RK).run(OS);
3346 }
3347 
3348 } // end namespace llvm
3349