1 //===---------------- DecoderEmitter.cpp - Decoder Generator --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // It contains the tablegen backend that emits the decoder functions for
10 // targets with fixed/variable length instruction set.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenInstruction.h"
15 #include "CodeGenTarget.h"
16 #include "InfoByHwMode.h"
17 #include "VarLenCodeEmitterGen.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/CachedHashString.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/MC/MCDecoderOps.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/LEB128.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/TableGen/Error.h"
35 #include "llvm/TableGen/Record.h"
36 #include <algorithm>
37 #include <cassert>
38 #include <cstddef>
39 #include <cstdint>
40 #include <map>
41 #include <memory>
42 #include <set>
43 #include <string>
44 #include <utility>
45 #include <vector>
46 
47 using namespace llvm;
48 
49 #define DEBUG_TYPE "decoder-emitter"
50 
51 namespace {
52 
53 STATISTIC(NumEncodings, "Number of encodings considered");
54 STATISTIC(NumEncodingsLackingDisasm, "Number of encodings without disassembler info");
55 STATISTIC(NumInstructions, "Number of instructions considered");
56 STATISTIC(NumEncodingsSupported, "Number of encodings supported");
57 STATISTIC(NumEncodingsOmitted, "Number of encodings omitted");
58 
59 struct EncodingField {
60   unsigned Base, Width, Offset;
61   EncodingField(unsigned B, unsigned W, unsigned O)
62     : Base(B), Width(W), Offset(O) { }
63 };
64 
65 struct OperandInfo {
66   std::vector<EncodingField> Fields;
67   std::string Decoder;
68   bool HasCompleteDecoder;
69   uint64_t InitValue;
70 
71   OperandInfo(std::string D, bool HCD)
72       : Decoder(std::move(D)), HasCompleteDecoder(HCD), InitValue(0) {}
73 
74   void addField(unsigned Base, unsigned Width, unsigned Offset) {
75     Fields.push_back(EncodingField(Base, Width, Offset));
76   }
77 
78   unsigned numFields() const { return Fields.size(); }
79 
80   typedef std::vector<EncodingField>::const_iterator const_iterator;
81 
82   const_iterator begin() const { return Fields.begin(); }
83   const_iterator end() const   { return Fields.end();   }
84 };
85 
86 typedef std::vector<uint8_t> DecoderTable;
87 typedef uint32_t DecoderFixup;
88 typedef std::vector<DecoderFixup> FixupList;
89 typedef std::vector<FixupList> FixupScopeList;
90 typedef SmallSetVector<CachedHashString, 16> PredicateSet;
91 typedef SmallSetVector<CachedHashString, 16> DecoderSet;
92 struct DecoderTableInfo {
93   DecoderTable Table;
94   FixupScopeList FixupStack;
95   PredicateSet Predicates;
96   DecoderSet Decoders;
97 };
98 
99 struct EncodingAndInst {
100   const Record *EncodingDef;
101   const CodeGenInstruction *Inst;
102   StringRef HwModeName;
103 
104   EncodingAndInst(const Record *EncodingDef, const CodeGenInstruction *Inst,
105                   StringRef HwModeName = "")
106       : EncodingDef(EncodingDef), Inst(Inst), HwModeName(HwModeName) {}
107 };
108 
109 struct EncodingIDAndOpcode {
110   unsigned EncodingID;
111   unsigned Opcode;
112 
113   EncodingIDAndOpcode() : EncodingID(0), Opcode(0) {}
114   EncodingIDAndOpcode(unsigned EncodingID, unsigned Opcode)
115       : EncodingID(EncodingID), Opcode(Opcode) {}
116 };
117 
118 raw_ostream &operator<<(raw_ostream &OS, const EncodingAndInst &Value) {
119   if (Value.EncodingDef != Value.Inst->TheDef)
120     OS << Value.EncodingDef->getName() << ":";
121   OS << Value.Inst->TheDef->getName();
122   return OS;
123 }
124 
125 class DecoderEmitter {
126   RecordKeeper &RK;
127   std::vector<EncodingAndInst> NumberedEncodings;
128 
129 public:
130   // Defaults preserved here for documentation, even though they aren't
131   // strictly necessary given the way that this is currently being called.
132   DecoderEmitter(RecordKeeper &R, std::string PredicateNamespace,
133                  std::string GPrefix = "if (",
134                  std::string GPostfix = " == MCDisassembler::Fail)",
135                  std::string ROK = "MCDisassembler::Success",
136                  std::string RFail = "MCDisassembler::Fail", std::string L = "")
137       : RK(R), Target(R), PredicateNamespace(std::move(PredicateNamespace)),
138         GuardPrefix(std::move(GPrefix)), GuardPostfix(std::move(GPostfix)),
139         ReturnOK(std::move(ROK)), ReturnFail(std::move(RFail)),
140         Locals(std::move(L)) {}
141 
142   // Emit the decoder state machine table.
143   void emitTable(formatted_raw_ostream &o, DecoderTable &Table,
144                  unsigned Indentation, unsigned BitWidth,
145                  StringRef Namespace) const;
146   void emitInstrLenTable(formatted_raw_ostream &OS,
147                          std::vector<unsigned> &InstrLen) const;
148   void emitPredicateFunction(formatted_raw_ostream &OS,
149                              PredicateSet &Predicates,
150                              unsigned Indentation) const;
151   void emitDecoderFunction(formatted_raw_ostream &OS,
152                            DecoderSet &Decoders,
153                            unsigned Indentation) const;
154 
155   // run - Output the code emitter
156   void run(raw_ostream &o);
157 
158 private:
159   CodeGenTarget Target;
160 
161 public:
162   std::string PredicateNamespace;
163   std::string GuardPrefix, GuardPostfix;
164   std::string ReturnOK, ReturnFail;
165   std::string Locals;
166 };
167 
168 } // end anonymous namespace
169 
170 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
171 // for a bit value.
172 //
173 // BIT_UNFILTERED is used as the init value for a filter position.  It is used
174 // only for filter processings.
175 typedef enum {
176   BIT_TRUE,      // '1'
177   BIT_FALSE,     // '0'
178   BIT_UNSET,     // '?'
179   BIT_UNFILTERED // unfiltered
180 } bit_value_t;
181 
182 static bool ValueSet(bit_value_t V) {
183   return (V == BIT_TRUE || V == BIT_FALSE);
184 }
185 
186 static bool ValueNotSet(bit_value_t V) {
187   return (V == BIT_UNSET);
188 }
189 
190 static int Value(bit_value_t V) {
191   return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
192 }
193 
194 static bit_value_t bitFromBits(const BitsInit &bits, unsigned index) {
195   if (BitInit *bit = dyn_cast<BitInit>(bits.getBit(index)))
196     return bit->getValue() ? BIT_TRUE : BIT_FALSE;
197 
198   // The bit is uninitialized.
199   return BIT_UNSET;
200 }
201 
202 // Prints the bit value for each position.
203 static void dumpBits(raw_ostream &o, const BitsInit &bits) {
204   for (unsigned index = bits.getNumBits(); index > 0; --index) {
205     switch (bitFromBits(bits, index - 1)) {
206     case BIT_TRUE:
207       o << "1";
208       break;
209     case BIT_FALSE:
210       o << "0";
211       break;
212     case BIT_UNSET:
213       o << "_";
214       break;
215     default:
216       llvm_unreachable("unexpected return value from bitFromBits");
217     }
218   }
219 }
220 
221 static BitsInit &getBitsField(const Record &def, StringRef str) {
222   const RecordVal *RV = def.getValue(str);
223   if (BitsInit *Bits = dyn_cast<BitsInit>(RV->getValue()))
224     return *Bits;
225 
226   // variable length instruction
227   VarLenInst VLI = VarLenInst(cast<DagInit>(RV->getValue()), RV);
228   SmallVector<Init *, 16> Bits;
229 
230   for (auto &SI : VLI) {
231     if (const BitsInit *BI = dyn_cast<BitsInit>(SI.Value)) {
232       for (unsigned Idx = 0U; Idx < BI->getNumBits(); ++Idx) {
233         Bits.push_back(BI->getBit(Idx));
234       }
235     } else if (const BitInit *BI = dyn_cast<BitInit>(SI.Value)) {
236       Bits.push_back(const_cast<BitInit *>(BI));
237     } else {
238       for (unsigned Idx = 0U; Idx < SI.BitWidth; ++Idx)
239         Bits.push_back(UnsetInit::get(def.getRecords()));
240     }
241   }
242 
243   return *BitsInit::get(def.getRecords(), Bits);
244 }
245 
246 // Representation of the instruction to work on.
247 typedef std::vector<bit_value_t> insn_t;
248 
249 namespace {
250 
251 static const uint64_t NO_FIXED_SEGMENTS_SENTINEL = -1ULL;
252 
253 class FilterChooser;
254 
255 /// Filter - Filter works with FilterChooser to produce the decoding tree for
256 /// the ISA.
257 ///
258 /// It is useful to think of a Filter as governing the switch stmts of the
259 /// decoding tree in a certain level.  Each case stmt delegates to an inferior
260 /// FilterChooser to decide what further decoding logic to employ, or in another
261 /// words, what other remaining bits to look at.  The FilterChooser eventually
262 /// chooses a best Filter to do its job.
263 ///
264 /// This recursive scheme ends when the number of Opcodes assigned to the
265 /// FilterChooser becomes 1 or if there is a conflict.  A conflict happens when
266 /// the Filter/FilterChooser combo does not know how to distinguish among the
267 /// Opcodes assigned.
268 ///
269 /// An example of a conflict is
270 ///
271 /// Conflict:
272 ///                     111101000.00........00010000....
273 ///                     111101000.00........0001........
274 ///                     1111010...00........0001........
275 ///                     1111010...00....................
276 ///                     1111010.........................
277 ///                     1111............................
278 ///                     ................................
279 ///     VST4q8a         111101000_00________00010000____
280 ///     VST4q8b         111101000_00________00010000____
281 ///
282 /// The Debug output shows the path that the decoding tree follows to reach the
283 /// the conclusion that there is a conflict.  VST4q8a is a vst4 to double-spaced
284 /// even registers, while VST4q8b is a vst4 to double-spaced odd registers.
285 ///
286 /// The encoding info in the .td files does not specify this meta information,
287 /// which could have been used by the decoder to resolve the conflict.  The
288 /// decoder could try to decode the even/odd register numbering and assign to
289 /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
290 /// version and return the Opcode since the two have the same Asm format string.
291 class Filter {
292 protected:
293   const FilterChooser *Owner;// points to the FilterChooser who owns this filter
294   unsigned StartBit; // the starting bit position
295   unsigned NumBits; // number of bits to filter
296   bool Mixed; // a mixed region contains both set and unset bits
297 
298   // Map of well-known segment value to the set of uid's with that value.
299   std::map<uint64_t, std::vector<EncodingIDAndOpcode>>
300       FilteredInstructions;
301 
302   // Set of uid's with non-constant segment values.
303   std::vector<EncodingIDAndOpcode> VariableInstructions;
304 
305   // Map of well-known segment value to its delegate.
306   std::map<uint64_t, std::unique_ptr<const FilterChooser>> FilterChooserMap;
307 
308   // Number of instructions which fall under FilteredInstructions category.
309   unsigned NumFiltered;
310 
311   // Keeps track of the last opcode in the filtered bucket.
312   EncodingIDAndOpcode LastOpcFiltered;
313 
314 public:
315   Filter(Filter &&f);
316   Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
317 
318   ~Filter() = default;
319 
320   unsigned getNumFiltered() const { return NumFiltered; }
321 
322   EncodingIDAndOpcode getSingletonOpc() const {
323     assert(NumFiltered == 1);
324     return LastOpcFiltered;
325   }
326 
327   // Return the filter chooser for the group of instructions without constant
328   // segment values.
329   const FilterChooser &getVariableFC() const {
330     assert(NumFiltered == 1);
331     assert(FilterChooserMap.size() == 1);
332     return *(FilterChooserMap.find(NO_FIXED_SEGMENTS_SENTINEL)->second);
333   }
334 
335   // Divides the decoding task into sub tasks and delegates them to the
336   // inferior FilterChooser's.
337   //
338   // A special case arises when there's only one entry in the filtered
339   // instructions.  In order to unambiguously decode the singleton, we need to
340   // match the remaining undecoded encoding bits against the singleton.
341   void recurse();
342 
343   // Emit table entries to decode instructions given a segment or segments of
344   // bits.
345   void emitTableEntry(DecoderTableInfo &TableInfo) const;
346 
347   // Returns the number of fanout produced by the filter.  More fanout implies
348   // the filter distinguishes more categories of instructions.
349   unsigned usefulness() const;
350 }; // end class Filter
351 
352 } // end anonymous namespace
353 
354 // These are states of our finite state machines used in FilterChooser's
355 // filterProcessor() which produces the filter candidates to use.
356 typedef enum {
357   ATTR_NONE,
358   ATTR_FILTERED,
359   ATTR_ALL_SET,
360   ATTR_ALL_UNSET,
361   ATTR_MIXED
362 } bitAttr_t;
363 
364 /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
365 /// in order to perform the decoding of instructions at the current level.
366 ///
367 /// Decoding proceeds from the top down.  Based on the well-known encoding bits
368 /// of instructions available, FilterChooser builds up the possible Filters that
369 /// can further the task of decoding by distinguishing among the remaining
370 /// candidate instructions.
371 ///
372 /// Once a filter has been chosen, it is called upon to divide the decoding task
373 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
374 /// processings.
375 ///
376 /// It is useful to think of a Filter as governing the switch stmts of the
377 /// decoding tree.  And each case is delegated to an inferior FilterChooser to
378 /// decide what further remaining bits to look at.
379 namespace {
380 
381 class FilterChooser {
382 protected:
383   friend class Filter;
384 
385   // Vector of codegen instructions to choose our filter.
386   ArrayRef<EncodingAndInst> AllInstructions;
387 
388   // Vector of uid's for this filter chooser to work on.
389   // The first member of the pair is the opcode id being decoded, the second is
390   // the opcode id that should be emitted.
391   const std::vector<EncodingIDAndOpcode> &Opcodes;
392 
393   // Lookup table for the operand decoding of instructions.
394   const std::map<unsigned, std::vector<OperandInfo>> &Operands;
395 
396   // Vector of candidate filters.
397   std::vector<Filter> Filters;
398 
399   // Array of bit values passed down from our parent.
400   // Set to all BIT_UNFILTERED's for Parent == NULL.
401   std::vector<bit_value_t> FilterBitValues;
402 
403   // Links to the FilterChooser above us in the decoding tree.
404   const FilterChooser *Parent;
405 
406   // Index of the best filter from Filters.
407   int BestIndex;
408 
409   // Width of instructions
410   unsigned BitWidth;
411 
412   // Parent emitter
413   const DecoderEmitter *Emitter;
414 
415 public:
416   FilterChooser(ArrayRef<EncodingAndInst> Insts,
417                 const std::vector<EncodingIDAndOpcode> &IDs,
418                 const std::map<unsigned, std::vector<OperandInfo>> &Ops,
419                 unsigned BW, const DecoderEmitter *E)
420       : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
421         FilterBitValues(BW, BIT_UNFILTERED), Parent(nullptr), BestIndex(-1),
422         BitWidth(BW), Emitter(E) {
423     doFilter();
424   }
425 
426   FilterChooser(ArrayRef<EncodingAndInst> Insts,
427                 const std::vector<EncodingIDAndOpcode> &IDs,
428                 const std::map<unsigned, std::vector<OperandInfo>> &Ops,
429                 const std::vector<bit_value_t> &ParentFilterBitValues,
430                 const FilterChooser &parent)
431       : AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
432         FilterBitValues(ParentFilterBitValues), Parent(&parent), BestIndex(-1),
433         BitWidth(parent.BitWidth), Emitter(parent.Emitter) {
434     doFilter();
435   }
436 
437   FilterChooser(const FilterChooser &) = delete;
438   void operator=(const FilterChooser &) = delete;
439 
440   unsigned getBitWidth() const { return BitWidth; }
441 
442 protected:
443   // Populates the insn given the uid.
444   void insnWithID(insn_t &Insn, unsigned Opcode) const {
445     BitsInit &Bits = getBitsField(*AllInstructions[Opcode].EncodingDef, "Inst");
446     Insn.resize(BitWidth > Bits.getNumBits() ? BitWidth : Bits.getNumBits(),
447                 BIT_UNSET);
448     // We may have a SoftFail bitmask, which specifies a mask where an encoding
449     // may differ from the value in "Inst" and yet still be valid, but the
450     // disassembler should return SoftFail instead of Success.
451     //
452     // This is used for marking UNPREDICTABLE instructions in the ARM world.
453     const RecordVal *RV =
454         AllInstructions[Opcode].EncodingDef->getValue("SoftFail");
455     const BitsInit *SFBits = RV ? dyn_cast<BitsInit>(RV->getValue()) : nullptr;
456     for (unsigned i = 0; i < Bits.getNumBits(); ++i) {
457       if (SFBits && bitFromBits(*SFBits, i) == BIT_TRUE)
458         Insn[i] = BIT_UNSET;
459       else
460         Insn[i] = bitFromBits(Bits, i);
461     }
462   }
463 
464   // Emit the name of the encoding/instruction pair.
465   void emitNameWithID(raw_ostream &OS, unsigned Opcode) const {
466     const Record *EncodingDef = AllInstructions[Opcode].EncodingDef;
467     const Record *InstDef = AllInstructions[Opcode].Inst->TheDef;
468     if (EncodingDef != InstDef)
469       OS << EncodingDef->getName() << ":";
470     OS << InstDef->getName();
471   }
472 
473   // Populates the field of the insn given the start position and the number of
474   // consecutive bits to scan for.
475   //
476   // Returns false if there exists any uninitialized bit value in the range.
477   // Returns true, otherwise.
478   bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
479                      unsigned NumBits) const;
480 
481   /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
482   /// filter array as a series of chars.
483   void dumpFilterArray(raw_ostream &o,
484                        const std::vector<bit_value_t> & filter) const;
485 
486   /// dumpStack - dumpStack traverses the filter chooser chain and calls
487   /// dumpFilterArray on each filter chooser up to the top level one.
488   void dumpStack(raw_ostream &o, const char *prefix) const;
489 
490   Filter &bestFilter() {
491     assert(BestIndex != -1 && "BestIndex not set");
492     return Filters[BestIndex];
493   }
494 
495   bool PositionFiltered(unsigned i) const {
496     return ValueSet(FilterBitValues[i]);
497   }
498 
499   // Calculates the island(s) needed to decode the instruction.
500   // This returns a lit of undecoded bits of an instructions, for example,
501   // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
502   // decoded bits in order to verify that the instruction matches the Opcode.
503   unsigned getIslands(std::vector<unsigned> &StartBits,
504                       std::vector<unsigned> &EndBits,
505                       std::vector<uint64_t> &FieldVals,
506                       const insn_t &Insn) const;
507 
508   // Emits code to check the Predicates member of an instruction are true.
509   // Returns true if predicate matches were emitted, false otherwise.
510   bool emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
511                           unsigned Opc) const;
512   bool emitPredicateMatchAux(const Init &Val, bool ParenIfBinOp,
513                              raw_ostream &OS) const;
514 
515   bool doesOpcodeNeedPredicate(unsigned Opc) const;
516   unsigned getPredicateIndex(DecoderTableInfo &TableInfo, StringRef P) const;
517   void emitPredicateTableEntry(DecoderTableInfo &TableInfo,
518                                unsigned Opc) const;
519 
520   void emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
521                               unsigned Opc) const;
522 
523   // Emits table entries to decode the singleton.
524   void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
525                                EncodingIDAndOpcode Opc) const;
526 
527   // Emits code to decode the singleton, and then to decode the rest.
528   void emitSingletonTableEntry(DecoderTableInfo &TableInfo,
529                                const Filter &Best) const;
530 
531   void emitBinaryParser(raw_ostream &o, unsigned &Indentation,
532                         const OperandInfo &OpInfo,
533                         bool &OpHasCompleteDecoder) const;
534 
535   void emitDecoder(raw_ostream &OS, unsigned Indentation, unsigned Opc,
536                    bool &HasCompleteDecoder) const;
537   unsigned getDecoderIndex(DecoderSet &Decoders, unsigned Opc,
538                            bool &HasCompleteDecoder) const;
539 
540   // Assign a single filter and run with it.
541   void runSingleFilter(unsigned startBit, unsigned numBit, bool mixed);
542 
543   // reportRegion is a helper function for filterProcessor to mark a region as
544   // eligible for use as a filter region.
545   void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
546                     bool AllowMixed);
547 
548   // FilterProcessor scans the well-known encoding bits of the instructions and
549   // builds up a list of candidate filters.  It chooses the best filter and
550   // recursively descends down the decoding tree.
551   bool filterProcessor(bool AllowMixed, bool Greedy = true);
552 
553   // Decides on the best configuration of filter(s) to use in order to decode
554   // the instructions.  A conflict of instructions may occur, in which case we
555   // dump the conflict set to the standard error.
556   void doFilter();
557 
558 public:
559   // emitTableEntries - Emit state machine entries to decode our share of
560   // instructions.
561   void emitTableEntries(DecoderTableInfo &TableInfo) const;
562 };
563 
564 } // end anonymous namespace
565 
566 ///////////////////////////
567 //                       //
568 // Filter Implementation //
569 //                       //
570 ///////////////////////////
571 
572 Filter::Filter(Filter &&f)
573   : Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
574     FilteredInstructions(std::move(f.FilteredInstructions)),
575     VariableInstructions(std::move(f.VariableInstructions)),
576     FilterChooserMap(std::move(f.FilterChooserMap)), NumFiltered(f.NumFiltered),
577     LastOpcFiltered(f.LastOpcFiltered) {
578 }
579 
580 Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
581                bool mixed)
582   : Owner(&owner), StartBit(startBit), NumBits(numBits), Mixed(mixed) {
583   assert(StartBit + NumBits - 1 < Owner->BitWidth);
584 
585   NumFiltered = 0;
586   LastOpcFiltered = {0, 0};
587 
588   for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
589     insn_t Insn;
590 
591     // Populates the insn given the uid.
592     Owner->insnWithID(Insn, Owner->Opcodes[i].EncodingID);
593 
594     uint64_t Field;
595     // Scans the segment for possibly well-specified encoding bits.
596     bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
597 
598     if (ok) {
599       // The encoding bits are well-known.  Lets add the uid of the
600       // instruction into the bucket keyed off the constant field value.
601       LastOpcFiltered = Owner->Opcodes[i];
602       FilteredInstructions[Field].push_back(LastOpcFiltered);
603       ++NumFiltered;
604     } else {
605       // Some of the encoding bit(s) are unspecified.  This contributes to
606       // one additional member of "Variable" instructions.
607       VariableInstructions.push_back(Owner->Opcodes[i]);
608     }
609   }
610 
611   assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
612          && "Filter returns no instruction categories");
613 }
614 
615 // Divides the decoding task into sub tasks and delegates them to the
616 // inferior FilterChooser's.
617 //
618 // A special case arises when there's only one entry in the filtered
619 // instructions.  In order to unambiguously decode the singleton, we need to
620 // match the remaining undecoded encoding bits against the singleton.
621 void Filter::recurse() {
622   // Starts by inheriting our parent filter chooser's filter bit values.
623   std::vector<bit_value_t> BitValueArray(Owner->FilterBitValues);
624 
625   if (!VariableInstructions.empty()) {
626     // Conservatively marks each segment position as BIT_UNSET.
627     for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex)
628       BitValueArray[StartBit + bitIndex] = BIT_UNSET;
629 
630     // Delegates to an inferior filter chooser for further processing on this
631     // group of instructions whose segment values are variable.
632     FilterChooserMap.insert(std::make_pair(NO_FIXED_SEGMENTS_SENTINEL,
633         std::make_unique<FilterChooser>(Owner->AllInstructions,
634             VariableInstructions, Owner->Operands, BitValueArray, *Owner)));
635   }
636 
637   // No need to recurse for a singleton filtered instruction.
638   // See also Filter::emit*().
639   if (getNumFiltered() == 1) {
640     assert(FilterChooserMap.size() == 1);
641     return;
642   }
643 
644   // Otherwise, create sub choosers.
645   for (const auto &Inst : FilteredInstructions) {
646 
647     // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
648     for (unsigned bitIndex = 0; bitIndex < NumBits; ++bitIndex) {
649       if (Inst.first & (1ULL << bitIndex))
650         BitValueArray[StartBit + bitIndex] = BIT_TRUE;
651       else
652         BitValueArray[StartBit + bitIndex] = BIT_FALSE;
653     }
654 
655     // Delegates to an inferior filter chooser for further processing on this
656     // category of instructions.
657     FilterChooserMap.insert(std::make_pair(
658         Inst.first, std::make_unique<FilterChooser>(
659                                 Owner->AllInstructions, Inst.second,
660                                 Owner->Operands, BitValueArray, *Owner)));
661   }
662 }
663 
664 static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
665                                uint32_t DestIdx) {
666   // Any NumToSkip fixups in the current scope can resolve to the
667   // current location.
668   for (FixupList::const_reverse_iterator I = Fixups.rbegin(),
669                                          E = Fixups.rend();
670        I != E; ++I) {
671     // Calculate the distance from the byte following the fixup entry byte
672     // to the destination. The Target is calculated from after the 16-bit
673     // NumToSkip entry itself, so subtract two  from the displacement here
674     // to account for that.
675     uint32_t FixupIdx = *I;
676     uint32_t Delta = DestIdx - FixupIdx - 3;
677     // Our NumToSkip entries are 24-bits. Make sure our table isn't too
678     // big.
679     assert(Delta < (1u << 24));
680     Table[FixupIdx] = (uint8_t)Delta;
681     Table[FixupIdx + 1] = (uint8_t)(Delta >> 8);
682     Table[FixupIdx + 2] = (uint8_t)(Delta >> 16);
683   }
684 }
685 
686 // Emit table entries to decode instructions given a segment or segments
687 // of bits.
688 void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
689   TableInfo.Table.push_back(MCD::OPC_ExtractField);
690   TableInfo.Table.push_back(StartBit);
691   TableInfo.Table.push_back(NumBits);
692 
693   // A new filter entry begins a new scope for fixup resolution.
694   TableInfo.FixupStack.emplace_back();
695 
696   DecoderTable &Table = TableInfo.Table;
697 
698   size_t PrevFilter = 0;
699   bool HasFallthrough = false;
700   for (auto &Filter : FilterChooserMap) {
701     // Field value -1 implies a non-empty set of variable instructions.
702     // See also recurse().
703     if (Filter.first == NO_FIXED_SEGMENTS_SENTINEL) {
704       HasFallthrough = true;
705 
706       // Each scope should always have at least one filter value to check
707       // for.
708       assert(PrevFilter != 0 && "empty filter set!");
709       FixupList &CurScope = TableInfo.FixupStack.back();
710       // Resolve any NumToSkip fixups in the current scope.
711       resolveTableFixups(Table, CurScope, Table.size());
712       CurScope.clear();
713       PrevFilter = 0;  // Don't re-process the filter's fallthrough.
714     } else {
715       Table.push_back(MCD::OPC_FilterValue);
716       // Encode and emit the value to filter against.
717       uint8_t Buffer[16];
718       unsigned Len = encodeULEB128(Filter.first, Buffer);
719       Table.insert(Table.end(), Buffer, Buffer + Len);
720       // Reserve space for the NumToSkip entry. We'll backpatch the value
721       // later.
722       PrevFilter = Table.size();
723       Table.push_back(0);
724       Table.push_back(0);
725       Table.push_back(0);
726     }
727 
728     // We arrive at a category of instructions with the same segment value.
729     // Now delegate to the sub filter chooser for further decodings.
730     // The case may fallthrough, which happens if the remaining well-known
731     // encoding bits do not match exactly.
732     Filter.second->emitTableEntries(TableInfo);
733 
734     // Now that we've emitted the body of the handler, update the NumToSkip
735     // of the filter itself to be able to skip forward when false. Subtract
736     // two as to account for the width of the NumToSkip field itself.
737     if (PrevFilter) {
738       uint32_t NumToSkip = Table.size() - PrevFilter - 3;
739       assert(NumToSkip < (1u << 24) && "disassembler decoding table too large!");
740       Table[PrevFilter] = (uint8_t)NumToSkip;
741       Table[PrevFilter + 1] = (uint8_t)(NumToSkip >> 8);
742       Table[PrevFilter + 2] = (uint8_t)(NumToSkip >> 16);
743     }
744   }
745 
746   // Any remaining unresolved fixups bubble up to the parent fixup scope.
747   assert(TableInfo.FixupStack.size() > 1 && "fixup stack underflow!");
748   FixupScopeList::iterator Source = TableInfo.FixupStack.end() - 1;
749   FixupScopeList::iterator Dest = Source - 1;
750   llvm::append_range(*Dest, *Source);
751   TableInfo.FixupStack.pop_back();
752 
753   // If there is no fallthrough, then the final filter should get fixed
754   // up according to the enclosing scope rather than the current position.
755   if (!HasFallthrough)
756     TableInfo.FixupStack.back().push_back(PrevFilter);
757 }
758 
759 // Returns the number of fanout produced by the filter.  More fanout implies
760 // the filter distinguishes more categories of instructions.
761 unsigned Filter::usefulness() const {
762   if (!VariableInstructions.empty())
763     return FilteredInstructions.size();
764   else
765     return FilteredInstructions.size() + 1;
766 }
767 
768 //////////////////////////////////
769 //                              //
770 // Filterchooser Implementation //
771 //                              //
772 //////////////////////////////////
773 
774 // Emit the decoder state machine table.
775 void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
776                                unsigned Indentation, unsigned BitWidth,
777                                StringRef Namespace) const {
778   OS.indent(Indentation) << "static const uint8_t DecoderTable" << Namespace
779     << BitWidth << "[] = {\n";
780 
781   Indentation += 2;
782 
783   // FIXME: We may be able to use the NumToSkip values to recover
784   // appropriate indentation levels.
785   DecoderTable::const_iterator I = Table.begin();
786   DecoderTable::const_iterator E = Table.end();
787   while (I != E) {
788     assert (I < E && "incomplete decode table entry!");
789 
790     uint64_t Pos = I - Table.begin();
791     OS << "/* " << Pos << " */";
792     OS.PadToColumn(12);
793 
794     switch (*I) {
795     default:
796       PrintFatalError("invalid decode table opcode");
797     case MCD::OPC_ExtractField: {
798       ++I;
799       unsigned Start = *I++;
800       unsigned Len = *I++;
801       OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
802         << Len << ",  // Inst{";
803       if (Len > 1)
804         OS << (Start + Len - 1) << "-";
805       OS << Start << "} ...\n";
806       break;
807     }
808     case MCD::OPC_FilterValue: {
809       ++I;
810       OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
811       // The filter value is ULEB128 encoded.
812       while (*I >= 128)
813         OS << (unsigned)*I++ << ", ";
814       OS << (unsigned)*I++ << ", ";
815 
816       // 24-bit numtoskip value.
817       uint8_t Byte = *I++;
818       uint32_t NumToSkip = Byte;
819       OS << (unsigned)Byte << ", ";
820       Byte = *I++;
821       OS << (unsigned)Byte << ", ";
822       NumToSkip |= Byte << 8;
823       Byte = *I++;
824       OS << utostr(Byte) << ", ";
825       NumToSkip |= Byte << 16;
826       OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
827       break;
828     }
829     case MCD::OPC_CheckField: {
830       ++I;
831       unsigned Start = *I++;
832       unsigned Len = *I++;
833       OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
834         << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
835       // ULEB128 encoded field value.
836       for (; *I >= 128; ++I)
837         OS << (unsigned)*I << ", ";
838       OS << (unsigned)*I++ << ", ";
839       // 24-bit numtoskip value.
840       uint8_t Byte = *I++;
841       uint32_t NumToSkip = Byte;
842       OS << (unsigned)Byte << ", ";
843       Byte = *I++;
844       OS << (unsigned)Byte << ", ";
845       NumToSkip |= Byte << 8;
846       Byte = *I++;
847       OS << utostr(Byte) << ", ";
848       NumToSkip |= Byte << 16;
849       OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
850       break;
851     }
852     case MCD::OPC_CheckPredicate: {
853       ++I;
854       OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
855       for (; *I >= 128; ++I)
856         OS << (unsigned)*I << ", ";
857       OS << (unsigned)*I++ << ", ";
858 
859       // 24-bit numtoskip value.
860       uint8_t Byte = *I++;
861       uint32_t NumToSkip = Byte;
862       OS << (unsigned)Byte << ", ";
863       Byte = *I++;
864       OS << (unsigned)Byte << ", ";
865       NumToSkip |= Byte << 8;
866       Byte = *I++;
867       OS << utostr(Byte) << ", ";
868       NumToSkip |= Byte << 16;
869       OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
870       break;
871     }
872     case MCD::OPC_Decode:
873     case MCD::OPC_TryDecode: {
874       bool IsTry = *I == MCD::OPC_TryDecode;
875       ++I;
876       // Extract the ULEB128 encoded Opcode to a buffer.
877       uint8_t Buffer[16], *p = Buffer;
878       while ((*p++ = *I++) >= 128)
879         assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
880                && "ULEB128 value too large!");
881       // Decode the Opcode value.
882       unsigned Opc = decodeULEB128(Buffer);
883       OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
884         << "Decode, ";
885       for (p = Buffer; *p >= 128; ++p)
886         OS << (unsigned)*p << ", ";
887       OS << (unsigned)*p << ", ";
888 
889       // Decoder index.
890       for (; *I >= 128; ++I)
891         OS << (unsigned)*I << ", ";
892       OS << (unsigned)*I++ << ", ";
893 
894       if (!IsTry) {
895         OS << "// Opcode: " << NumberedEncodings[Opc] << "\n";
896         break;
897       }
898 
899       // Fallthrough for OPC_TryDecode.
900 
901       // 24-bit numtoskip value.
902       uint8_t Byte = *I++;
903       uint32_t NumToSkip = Byte;
904       OS << (unsigned)Byte << ", ";
905       Byte = *I++;
906       OS << (unsigned)Byte << ", ";
907       NumToSkip |= Byte << 8;
908       Byte = *I++;
909       OS << utostr(Byte) << ", ";
910       NumToSkip |= Byte << 16;
911 
912       OS << "// Opcode: " << NumberedEncodings[Opc]
913          << ", skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
914       break;
915     }
916     case MCD::OPC_SoftFail: {
917       ++I;
918       OS.indent(Indentation) << "MCD::OPC_SoftFail";
919       // Positive mask
920       uint64_t Value = 0;
921       unsigned Shift = 0;
922       do {
923         OS << ", " << (unsigned)*I;
924         Value += (*I & 0x7f) << Shift;
925         Shift += 7;
926       } while (*I++ >= 128);
927       if (Value > 127) {
928         OS << " /* 0x";
929         OS.write_hex(Value);
930         OS << " */";
931       }
932       // Negative mask
933       Value = 0;
934       Shift = 0;
935       do {
936         OS << ", " << (unsigned)*I;
937         Value += (*I & 0x7f) << Shift;
938         Shift += 7;
939       } while (*I++ >= 128);
940       if (Value > 127) {
941         OS << " /* 0x";
942         OS.write_hex(Value);
943         OS << " */";
944       }
945       OS << ",\n";
946       break;
947     }
948     case MCD::OPC_Fail: {
949       ++I;
950       OS.indent(Indentation) << "MCD::OPC_Fail,\n";
951       break;
952     }
953     }
954   }
955   OS.indent(Indentation) << "0\n";
956 
957   Indentation -= 2;
958 
959   OS.indent(Indentation) << "};\n\n";
960 }
961 
962 void DecoderEmitter::emitInstrLenTable(formatted_raw_ostream &OS,
963                                        std::vector<unsigned> &InstrLen) const {
964   OS << "static const uint8_t InstrLenTable[] = {\n";
965   for (unsigned &Len : InstrLen) {
966     OS << Len << ",\n";
967   }
968   OS << "};\n\n";
969 }
970 
971 void DecoderEmitter::emitPredicateFunction(formatted_raw_ostream &OS,
972                                            PredicateSet &Predicates,
973                                            unsigned Indentation) const {
974   // The predicate function is just a big switch statement based on the
975   // input predicate index.
976   OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
977     << "const FeatureBitset &Bits) {\n";
978   Indentation += 2;
979   if (!Predicates.empty()) {
980     OS.indent(Indentation) << "switch (Idx) {\n";
981     OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
982     unsigned Index = 0;
983     for (const auto &Predicate : Predicates) {
984       OS.indent(Indentation) << "case " << Index++ << ":\n";
985       OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
986     }
987     OS.indent(Indentation) << "}\n";
988   } else {
989     // No case statement to emit
990     OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
991   }
992   Indentation -= 2;
993   OS.indent(Indentation) << "}\n\n";
994 }
995 
996 void DecoderEmitter::emitDecoderFunction(formatted_raw_ostream &OS,
997                                          DecoderSet &Decoders,
998                                          unsigned Indentation) const {
999   // The decoder function is just a big switch statement based on the
1000   // input decoder index.
1001   OS.indent(Indentation) << "template <typename InsnType>\n";
1002   OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
1003     << " unsigned Idx, InsnType insn, MCInst &MI,\n";
1004   OS.indent(Indentation)
1005       << "                                   uint64_t "
1006       << "Address, const MCDisassembler *Decoder, bool &DecodeComplete) {\n";
1007   Indentation += 2;
1008   OS.indent(Indentation) << "DecodeComplete = true;\n";
1009   // TODO: When InsnType is large, using uint64_t limits all fields to 64 bits
1010   // It would be better for emitBinaryParser to use a 64-bit tmp whenever
1011   // possible but fall back to an InsnType-sized tmp for truly large fields.
1012   OS.indent(Indentation) << "using TmpType = "
1013                             "std::conditional_t<std::is_integral<InsnType>::"
1014                             "value, InsnType, uint64_t>;\n";
1015   OS.indent(Indentation) << "TmpType tmp;\n";
1016   OS.indent(Indentation) << "switch (Idx) {\n";
1017   OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
1018   unsigned Index = 0;
1019   for (const auto &Decoder : Decoders) {
1020     OS.indent(Indentation) << "case " << Index++ << ":\n";
1021     OS << Decoder;
1022     OS.indent(Indentation+2) << "return S;\n";
1023   }
1024   OS.indent(Indentation) << "}\n";
1025   Indentation -= 2;
1026   OS.indent(Indentation) << "}\n\n";
1027 }
1028 
1029 // Populates the field of the insn given the start position and the number of
1030 // consecutive bits to scan for.
1031 //
1032 // Returns false if and on the first uninitialized bit value encountered.
1033 // Returns true, otherwise.
1034 bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
1035                                   unsigned StartBit, unsigned NumBits) const {
1036   Field = 0;
1037 
1038   for (unsigned i = 0; i < NumBits; ++i) {
1039     if (Insn[StartBit + i] == BIT_UNSET)
1040       return false;
1041 
1042     if (Insn[StartBit + i] == BIT_TRUE)
1043       Field = Field | (1ULL << i);
1044   }
1045 
1046   return true;
1047 }
1048 
1049 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
1050 /// filter array as a series of chars.
1051 void FilterChooser::dumpFilterArray(raw_ostream &o,
1052                                  const std::vector<bit_value_t> &filter) const {
1053   for (unsigned bitIndex = BitWidth; bitIndex > 0; bitIndex--) {
1054     switch (filter[bitIndex - 1]) {
1055     case BIT_UNFILTERED:
1056       o << ".";
1057       break;
1058     case BIT_UNSET:
1059       o << "_";
1060       break;
1061     case BIT_TRUE:
1062       o << "1";
1063       break;
1064     case BIT_FALSE:
1065       o << "0";
1066       break;
1067     }
1068   }
1069 }
1070 
1071 /// dumpStack - dumpStack traverses the filter chooser chain and calls
1072 /// dumpFilterArray on each filter chooser up to the top level one.
1073 void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) const {
1074   const FilterChooser *current = this;
1075 
1076   while (current) {
1077     o << prefix;
1078     dumpFilterArray(o, current->FilterBitValues);
1079     o << '\n';
1080     current = current->Parent;
1081   }
1082 }
1083 
1084 // Calculates the island(s) needed to decode the instruction.
1085 // This returns a list of undecoded bits of an instructions, for example,
1086 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
1087 // decoded bits in order to verify that the instruction matches the Opcode.
1088 unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
1089                                    std::vector<unsigned> &EndBits,
1090                                    std::vector<uint64_t> &FieldVals,
1091                                    const insn_t &Insn) const {
1092   unsigned Num, BitNo;
1093   Num = BitNo = 0;
1094 
1095   uint64_t FieldVal = 0;
1096 
1097   // 0: Init
1098   // 1: Water (the bit value does not affect decoding)
1099   // 2: Island (well-known bit value needed for decoding)
1100   int State = 0;
1101 
1102   for (unsigned i = 0; i < BitWidth; ++i) {
1103     int64_t Val = Value(Insn[i]);
1104     bool Filtered = PositionFiltered(i);
1105     switch (State) {
1106     default: llvm_unreachable("Unreachable code!");
1107     case 0:
1108     case 1:
1109       if (Filtered || Val == -1)
1110         State = 1; // Still in Water
1111       else {
1112         State = 2; // Into the Island
1113         BitNo = 0;
1114         StartBits.push_back(i);
1115         FieldVal = Val;
1116       }
1117       break;
1118     case 2:
1119       if (Filtered || Val == -1) {
1120         State = 1; // Into the Water
1121         EndBits.push_back(i - 1);
1122         FieldVals.push_back(FieldVal);
1123         ++Num;
1124       } else {
1125         State = 2; // Still in Island
1126         ++BitNo;
1127         FieldVal = FieldVal | Val << BitNo;
1128       }
1129       break;
1130     }
1131   }
1132   // If we are still in Island after the loop, do some housekeeping.
1133   if (State == 2) {
1134     EndBits.push_back(BitWidth - 1);
1135     FieldVals.push_back(FieldVal);
1136     ++Num;
1137   }
1138 
1139   assert(StartBits.size() == Num && EndBits.size() == Num &&
1140          FieldVals.size() == Num);
1141   return Num;
1142 }
1143 
1144 void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
1145                                      const OperandInfo &OpInfo,
1146                                      bool &OpHasCompleteDecoder) const {
1147   const std::string &Decoder = OpInfo.Decoder;
1148 
1149   bool UseInsertBits = OpInfo.numFields() != 1 || OpInfo.InitValue != 0;
1150 
1151   if (UseInsertBits) {
1152     o.indent(Indentation) << "tmp = 0x";
1153     o.write_hex(OpInfo.InitValue);
1154     o << ";\n";
1155   }
1156 
1157   for (const EncodingField &EF : OpInfo) {
1158     o.indent(Indentation);
1159     if (UseInsertBits)
1160       o << "insertBits(tmp, ";
1161     else
1162       o << "tmp = ";
1163     o << "fieldFromInstruction(insn, " << EF.Base << ", " << EF.Width << ')';
1164     if (UseInsertBits)
1165       o << ", " << EF.Offset << ", " << EF.Width << ')';
1166     else if (EF.Offset != 0)
1167       o << " << " << EF.Offset;
1168     o << ";\n";
1169   }
1170 
1171   if (Decoder != "") {
1172     OpHasCompleteDecoder = OpInfo.HasCompleteDecoder;
1173     o.indent(Indentation) << Emitter->GuardPrefix << Decoder
1174       << "(MI, tmp, Address, Decoder)"
1175       << Emitter->GuardPostfix
1176       << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
1177       << "return MCDisassembler::Fail; }\n";
1178   } else {
1179     OpHasCompleteDecoder = true;
1180     o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
1181   }
1182 }
1183 
1184 void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
1185                                 unsigned Opc, bool &HasCompleteDecoder) const {
1186   HasCompleteDecoder = true;
1187 
1188   for (const auto &Op : Operands.find(Opc)->second) {
1189     // If a custom instruction decoder was specified, use that.
1190     if (Op.numFields() == 0 && !Op.Decoder.empty()) {
1191       HasCompleteDecoder = Op.HasCompleteDecoder;
1192       OS.indent(Indentation) << Emitter->GuardPrefix << Op.Decoder
1193         << "(MI, insn, Address, Decoder)"
1194         << Emitter->GuardPostfix
1195         << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
1196         << "return MCDisassembler::Fail; }\n";
1197       break;
1198     }
1199 
1200     bool OpHasCompleteDecoder;
1201     emitBinaryParser(OS, Indentation, Op, OpHasCompleteDecoder);
1202     if (!OpHasCompleteDecoder)
1203       HasCompleteDecoder = false;
1204   }
1205 }
1206 
1207 unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
1208                                         unsigned Opc,
1209                                         bool &HasCompleteDecoder) const {
1210   // Build up the predicate string.
1211   SmallString<256> Decoder;
1212   // FIXME: emitDecoder() function can take a buffer directly rather than
1213   // a stream.
1214   raw_svector_ostream S(Decoder);
1215   unsigned I = 4;
1216   emitDecoder(S, I, Opc, HasCompleteDecoder);
1217 
1218   // Using the full decoder string as the key value here is a bit
1219   // heavyweight, but is effective. If the string comparisons become a
1220   // performance concern, we can implement a mangling of the predicate
1221   // data easily enough with a map back to the actual string. That's
1222   // overkill for now, though.
1223 
1224   // Make sure the predicate is in the table.
1225   Decoders.insert(CachedHashString(Decoder));
1226   // Now figure out the index for when we write out the table.
1227   DecoderSet::const_iterator P = find(Decoders, Decoder.str());
1228   return (unsigned)(P - Decoders.begin());
1229 }
1230 
1231 // If ParenIfBinOp is true, print a surrounding () if Val uses && or ||.
1232 bool FilterChooser::emitPredicateMatchAux(const Init &Val, bool ParenIfBinOp,
1233                                           raw_ostream &OS) const {
1234   if (auto *D = dyn_cast<DefInit>(&Val)) {
1235     if (!D->getDef()->isSubClassOf("SubtargetFeature"))
1236       return true;
1237     OS << "Bits[" << Emitter->PredicateNamespace << "::" << D->getAsString()
1238        << "]";
1239     return false;
1240   }
1241   if (auto *D = dyn_cast<DagInit>(&Val)) {
1242     std::string Op = D->getOperator()->getAsString();
1243     if (Op == "not" && D->getNumArgs() == 1) {
1244       OS << '!';
1245       return emitPredicateMatchAux(*D->getArg(0), true, OS);
1246     }
1247     if ((Op == "any_of" || Op == "all_of") && D->getNumArgs() > 0) {
1248       bool Paren = D->getNumArgs() > 1 && std::exchange(ParenIfBinOp, true);
1249       if (Paren)
1250         OS << '(';
1251       ListSeparator LS(Op == "any_of" ? " || " : " && ");
1252       for (auto *Arg : D->getArgs()) {
1253         OS << LS;
1254         if (emitPredicateMatchAux(*Arg, ParenIfBinOp, OS))
1255           return true;
1256       }
1257       if (Paren)
1258         OS << ')';
1259       return false;
1260     }
1261   }
1262   return true;
1263 }
1264 
1265 bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
1266                                        unsigned Opc) const {
1267   ListInit *Predicates =
1268       AllInstructions[Opc].EncodingDef->getValueAsListInit("Predicates");
1269   bool IsFirstEmission = true;
1270   for (unsigned i = 0; i < Predicates->size(); ++i) {
1271     Record *Pred = Predicates->getElementAsRecord(i);
1272     if (!Pred->getValue("AssemblerMatcherPredicate"))
1273       continue;
1274 
1275     if (!isa<DagInit>(Pred->getValue("AssemblerCondDag")->getValue()))
1276       continue;
1277 
1278     if (!IsFirstEmission)
1279       o << " && ";
1280     if (emitPredicateMatchAux(*Pred->getValueAsDag("AssemblerCondDag"),
1281                               Predicates->size() > 1, o))
1282       PrintFatalError(Pred->getLoc(), "Invalid AssemblerCondDag!");
1283     IsFirstEmission = false;
1284   }
1285   return !Predicates->empty();
1286 }
1287 
1288 bool FilterChooser::doesOpcodeNeedPredicate(unsigned Opc) const {
1289   ListInit *Predicates =
1290       AllInstructions[Opc].EncodingDef->getValueAsListInit("Predicates");
1291   for (unsigned i = 0; i < Predicates->size(); ++i) {
1292     Record *Pred = Predicates->getElementAsRecord(i);
1293     if (!Pred->getValue("AssemblerMatcherPredicate"))
1294       continue;
1295 
1296     if (isa<DagInit>(Pred->getValue("AssemblerCondDag")->getValue()))
1297       return true;
1298   }
1299   return false;
1300 }
1301 
1302 unsigned FilterChooser::getPredicateIndex(DecoderTableInfo &TableInfo,
1303                                           StringRef Predicate) const {
1304   // Using the full predicate string as the key value here is a bit
1305   // heavyweight, but is effective. If the string comparisons become a
1306   // performance concern, we can implement a mangling of the predicate
1307   // data easily enough with a map back to the actual string. That's
1308   // overkill for now, though.
1309 
1310   // Make sure the predicate is in the table.
1311   TableInfo.Predicates.insert(CachedHashString(Predicate));
1312   // Now figure out the index for when we write out the table.
1313   PredicateSet::const_iterator P = find(TableInfo.Predicates, Predicate);
1314   return (unsigned)(P - TableInfo.Predicates.begin());
1315 }
1316 
1317 void FilterChooser::emitPredicateTableEntry(DecoderTableInfo &TableInfo,
1318                                             unsigned Opc) const {
1319   if (!doesOpcodeNeedPredicate(Opc))
1320     return;
1321 
1322   // Build up the predicate string.
1323   SmallString<256> Predicate;
1324   // FIXME: emitPredicateMatch() functions can take a buffer directly rather
1325   // than a stream.
1326   raw_svector_ostream PS(Predicate);
1327   unsigned I = 0;
1328   emitPredicateMatch(PS, I, Opc);
1329 
1330   // Figure out the index into the predicate table for the predicate just
1331   // computed.
1332   unsigned PIdx = getPredicateIndex(TableInfo, PS.str());
1333   SmallString<16> PBytes;
1334   raw_svector_ostream S(PBytes);
1335   encodeULEB128(PIdx, S);
1336 
1337   TableInfo.Table.push_back(MCD::OPC_CheckPredicate);
1338   // Predicate index
1339   for (unsigned i = 0, e = PBytes.size(); i != e; ++i)
1340     TableInfo.Table.push_back(PBytes[i]);
1341   // Push location for NumToSkip backpatching.
1342   TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1343   TableInfo.Table.push_back(0);
1344   TableInfo.Table.push_back(0);
1345   TableInfo.Table.push_back(0);
1346 }
1347 
1348 void FilterChooser::emitSoftFailTableEntry(DecoderTableInfo &TableInfo,
1349                                            unsigned Opc) const {
1350   const RecordVal *RV = AllInstructions[Opc].EncodingDef->getValue("SoftFail");
1351   BitsInit *SFBits = RV ? dyn_cast<BitsInit>(RV->getValue()) : nullptr;
1352 
1353   if (!SFBits) return;
1354   BitsInit *InstBits =
1355       AllInstructions[Opc].EncodingDef->getValueAsBitsInit("Inst");
1356 
1357   APInt PositiveMask(BitWidth, 0ULL);
1358   APInt NegativeMask(BitWidth, 0ULL);
1359   for (unsigned i = 0; i < BitWidth; ++i) {
1360     bit_value_t B = bitFromBits(*SFBits, i);
1361     bit_value_t IB = bitFromBits(*InstBits, i);
1362 
1363     if (B != BIT_TRUE) continue;
1364 
1365     switch (IB) {
1366     case BIT_FALSE:
1367       // The bit is meant to be false, so emit a check to see if it is true.
1368       PositiveMask.setBit(i);
1369       break;
1370     case BIT_TRUE:
1371       // The bit is meant to be true, so emit a check to see if it is false.
1372       NegativeMask.setBit(i);
1373       break;
1374     default:
1375       // The bit is not set; this must be an error!
1376       errs() << "SoftFail Conflict: bit SoftFail{" << i << "} in "
1377              << AllInstructions[Opc] << " is set but Inst{" << i
1378              << "} is unset!\n"
1379              << "  - You can only mark a bit as SoftFail if it is fully defined"
1380              << " (1/0 - not '?') in Inst\n";
1381       return;
1382     }
1383   }
1384 
1385   bool NeedPositiveMask = PositiveMask.getBoolValue();
1386   bool NeedNegativeMask = NegativeMask.getBoolValue();
1387 
1388   if (!NeedPositiveMask && !NeedNegativeMask)
1389     return;
1390 
1391   TableInfo.Table.push_back(MCD::OPC_SoftFail);
1392 
1393   SmallString<16> MaskBytes;
1394   raw_svector_ostream S(MaskBytes);
1395   if (NeedPositiveMask) {
1396     encodeULEB128(PositiveMask.getZExtValue(), S);
1397     for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1398       TableInfo.Table.push_back(MaskBytes[i]);
1399   } else
1400     TableInfo.Table.push_back(0);
1401   if (NeedNegativeMask) {
1402     MaskBytes.clear();
1403     encodeULEB128(NegativeMask.getZExtValue(), S);
1404     for (unsigned i = 0, e = MaskBytes.size(); i != e; ++i)
1405       TableInfo.Table.push_back(MaskBytes[i]);
1406   } else
1407     TableInfo.Table.push_back(0);
1408 }
1409 
1410 // Emits table entries to decode the singleton.
1411 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1412                                             EncodingIDAndOpcode Opc) const {
1413   std::vector<unsigned> StartBits;
1414   std::vector<unsigned> EndBits;
1415   std::vector<uint64_t> FieldVals;
1416   insn_t Insn;
1417   insnWithID(Insn, Opc.EncodingID);
1418 
1419   // Look for islands of undecoded bits of the singleton.
1420   getIslands(StartBits, EndBits, FieldVals, Insn);
1421 
1422   unsigned Size = StartBits.size();
1423 
1424   // Emit the predicate table entry if one is needed.
1425   emitPredicateTableEntry(TableInfo, Opc.EncodingID);
1426 
1427   // Check any additional encoding fields needed.
1428   for (unsigned I = Size; I != 0; --I) {
1429     unsigned NumBits = EndBits[I-1] - StartBits[I-1] + 1;
1430     TableInfo.Table.push_back(MCD::OPC_CheckField);
1431     TableInfo.Table.push_back(StartBits[I-1]);
1432     TableInfo.Table.push_back(NumBits);
1433     uint8_t Buffer[16], *p;
1434     encodeULEB128(FieldVals[I-1], Buffer);
1435     for (p = Buffer; *p >= 128 ; ++p)
1436       TableInfo.Table.push_back(*p);
1437     TableInfo.Table.push_back(*p);
1438     // Push location for NumToSkip backpatching.
1439     TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1440     // The fixup is always 24-bits, so go ahead and allocate the space
1441     // in the table so all our relative position calculations work OK even
1442     // before we fully resolve the real value here.
1443     TableInfo.Table.push_back(0);
1444     TableInfo.Table.push_back(0);
1445     TableInfo.Table.push_back(0);
1446   }
1447 
1448   // Check for soft failure of the match.
1449   emitSoftFailTableEntry(TableInfo, Opc.EncodingID);
1450 
1451   bool HasCompleteDecoder;
1452   unsigned DIdx =
1453       getDecoderIndex(TableInfo.Decoders, Opc.EncodingID, HasCompleteDecoder);
1454 
1455   // Produce OPC_Decode or OPC_TryDecode opcode based on the information
1456   // whether the instruction decoder is complete or not. If it is complete
1457   // then it handles all possible values of remaining variable/unfiltered bits
1458   // and for any value can determine if the bitpattern is a valid instruction
1459   // or not. This means OPC_Decode will be the final step in the decoding
1460   // process. If it is not complete, then the Fail return code from the
1461   // decoder method indicates that additional processing should be done to see
1462   // if there is any other instruction that also matches the bitpattern and
1463   // can decode it.
1464   TableInfo.Table.push_back(HasCompleteDecoder ? MCD::OPC_Decode :
1465       MCD::OPC_TryDecode);
1466   NumEncodingsSupported++;
1467   uint8_t Buffer[16], *p;
1468   encodeULEB128(Opc.Opcode, Buffer);
1469   for (p = Buffer; *p >= 128 ; ++p)
1470     TableInfo.Table.push_back(*p);
1471   TableInfo.Table.push_back(*p);
1472 
1473   SmallString<16> Bytes;
1474   raw_svector_ostream S(Bytes);
1475   encodeULEB128(DIdx, S);
1476 
1477   // Decoder index
1478   for (unsigned i = 0, e = Bytes.size(); i != e; ++i)
1479     TableInfo.Table.push_back(Bytes[i]);
1480 
1481   if (!HasCompleteDecoder) {
1482     // Push location for NumToSkip backpatching.
1483     TableInfo.FixupStack.back().push_back(TableInfo.Table.size());
1484     // Allocate the space for the fixup.
1485     TableInfo.Table.push_back(0);
1486     TableInfo.Table.push_back(0);
1487     TableInfo.Table.push_back(0);
1488   }
1489 }
1490 
1491 // Emits table entries to decode the singleton, and then to decode the rest.
1492 void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
1493                                             const Filter &Best) const {
1494   EncodingIDAndOpcode Opc = Best.getSingletonOpc();
1495 
1496   // complex singletons need predicate checks from the first singleton
1497   // to refer forward to the variable filterchooser that follows.
1498   TableInfo.FixupStack.emplace_back();
1499 
1500   emitSingletonTableEntry(TableInfo, Opc);
1501 
1502   resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
1503                      TableInfo.Table.size());
1504   TableInfo.FixupStack.pop_back();
1505 
1506   Best.getVariableFC().emitTableEntries(TableInfo);
1507 }
1508 
1509 // Assign a single filter and run with it.  Top level API client can initialize
1510 // with a single filter to start the filtering process.
1511 void FilterChooser::runSingleFilter(unsigned startBit, unsigned numBit,
1512                                     bool mixed) {
1513   Filters.clear();
1514   Filters.emplace_back(*this, startBit, numBit, true);
1515   BestIndex = 0; // Sole Filter instance to choose from.
1516   bestFilter().recurse();
1517 }
1518 
1519 // reportRegion is a helper function for filterProcessor to mark a region as
1520 // eligible for use as a filter region.
1521 void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
1522                                  unsigned BitIndex, bool AllowMixed) {
1523   if (RA == ATTR_MIXED && AllowMixed)
1524     Filters.emplace_back(*this, StartBit, BitIndex - StartBit, true);
1525   else if (RA == ATTR_ALL_SET && !AllowMixed)
1526     Filters.emplace_back(*this, StartBit, BitIndex - StartBit, false);
1527 }
1528 
1529 // FilterProcessor scans the well-known encoding bits of the instructions and
1530 // builds up a list of candidate filters.  It chooses the best filter and
1531 // recursively descends down the decoding tree.
1532 bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
1533   Filters.clear();
1534   BestIndex = -1;
1535   unsigned numInstructions = Opcodes.size();
1536 
1537   assert(numInstructions && "Filter created with no instructions");
1538 
1539   // No further filtering is necessary.
1540   if (numInstructions == 1)
1541     return true;
1542 
1543   // Heuristics.  See also doFilter()'s "Heuristics" comment when num of
1544   // instructions is 3.
1545   if (AllowMixed && !Greedy) {
1546     assert(numInstructions == 3);
1547 
1548     for (auto Opcode : Opcodes) {
1549       std::vector<unsigned> StartBits;
1550       std::vector<unsigned> EndBits;
1551       std::vector<uint64_t> FieldVals;
1552       insn_t Insn;
1553 
1554       insnWithID(Insn, Opcode.EncodingID);
1555 
1556       // Look for islands of undecoded bits of any instruction.
1557       if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
1558         // Found an instruction with island(s).  Now just assign a filter.
1559         runSingleFilter(StartBits[0], EndBits[0] - StartBits[0] + 1, true);
1560         return true;
1561       }
1562     }
1563   }
1564 
1565   unsigned BitIndex;
1566 
1567   // We maintain BIT_WIDTH copies of the bitAttrs automaton.
1568   // The automaton consumes the corresponding bit from each
1569   // instruction.
1570   //
1571   //   Input symbols: 0, 1, and _ (unset).
1572   //   States:        NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
1573   //   Initial state: NONE.
1574   //
1575   // (NONE) ------- [01] -> (ALL_SET)
1576   // (NONE) ------- _ ----> (ALL_UNSET)
1577   // (ALL_SET) ---- [01] -> (ALL_SET)
1578   // (ALL_SET) ---- _ ----> (MIXED)
1579   // (ALL_UNSET) -- [01] -> (MIXED)
1580   // (ALL_UNSET) -- _ ----> (ALL_UNSET)
1581   // (MIXED) ------ . ----> (MIXED)
1582   // (FILTERED)---- . ----> (FILTERED)
1583 
1584   std::vector<bitAttr_t> bitAttrs;
1585 
1586   // FILTERED bit positions provide no entropy and are not worthy of pursuing.
1587   // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
1588   for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex)
1589     if (FilterBitValues[BitIndex] == BIT_TRUE ||
1590         FilterBitValues[BitIndex] == BIT_FALSE)
1591       bitAttrs.push_back(ATTR_FILTERED);
1592     else
1593       bitAttrs.push_back(ATTR_NONE);
1594 
1595   for (unsigned InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
1596     insn_t insn;
1597 
1598     insnWithID(insn, Opcodes[InsnIndex].EncodingID);
1599 
1600     for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1601       switch (bitAttrs[BitIndex]) {
1602       case ATTR_NONE:
1603         if (insn[BitIndex] == BIT_UNSET)
1604           bitAttrs[BitIndex] = ATTR_ALL_UNSET;
1605         else
1606           bitAttrs[BitIndex] = ATTR_ALL_SET;
1607         break;
1608       case ATTR_ALL_SET:
1609         if (insn[BitIndex] == BIT_UNSET)
1610           bitAttrs[BitIndex] = ATTR_MIXED;
1611         break;
1612       case ATTR_ALL_UNSET:
1613         if (insn[BitIndex] != BIT_UNSET)
1614           bitAttrs[BitIndex] = ATTR_MIXED;
1615         break;
1616       case ATTR_MIXED:
1617       case ATTR_FILTERED:
1618         break;
1619       }
1620     }
1621   }
1622 
1623   // The regionAttr automaton consumes the bitAttrs automatons' state,
1624   // lowest-to-highest.
1625   //
1626   //   Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1627   //   States:        NONE, ALL_SET, MIXED
1628   //   Initial state: NONE
1629   //
1630   // (NONE) ----- F --> (NONE)
1631   // (NONE) ----- S --> (ALL_SET)     ; and set region start
1632   // (NONE) ----- U --> (NONE)
1633   // (NONE) ----- M --> (MIXED)       ; and set region start
1634   // (ALL_SET) -- F --> (NONE)        ; and report an ALL_SET region
1635   // (ALL_SET) -- S --> (ALL_SET)
1636   // (ALL_SET) -- U --> (NONE)        ; and report an ALL_SET region
1637   // (ALL_SET) -- M --> (MIXED)       ; and report an ALL_SET region
1638   // (MIXED) ---- F --> (NONE)        ; and report a MIXED region
1639   // (MIXED) ---- S --> (ALL_SET)     ; and report a MIXED region
1640   // (MIXED) ---- U --> (NONE)        ; and report a MIXED region
1641   // (MIXED) ---- M --> (MIXED)
1642 
1643   bitAttr_t RA = ATTR_NONE;
1644   unsigned StartBit = 0;
1645 
1646   for (BitIndex = 0; BitIndex < BitWidth; ++BitIndex) {
1647     bitAttr_t bitAttr = bitAttrs[BitIndex];
1648 
1649     assert(bitAttr != ATTR_NONE && "Bit without attributes");
1650 
1651     switch (RA) {
1652     case ATTR_NONE:
1653       switch (bitAttr) {
1654       case ATTR_FILTERED:
1655         break;
1656       case ATTR_ALL_SET:
1657         StartBit = BitIndex;
1658         RA = ATTR_ALL_SET;
1659         break;
1660       case ATTR_ALL_UNSET:
1661         break;
1662       case ATTR_MIXED:
1663         StartBit = BitIndex;
1664         RA = ATTR_MIXED;
1665         break;
1666       default:
1667         llvm_unreachable("Unexpected bitAttr!");
1668       }
1669       break;
1670     case ATTR_ALL_SET:
1671       switch (bitAttr) {
1672       case ATTR_FILTERED:
1673         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1674         RA = ATTR_NONE;
1675         break;
1676       case ATTR_ALL_SET:
1677         break;
1678       case ATTR_ALL_UNSET:
1679         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1680         RA = ATTR_NONE;
1681         break;
1682       case ATTR_MIXED:
1683         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1684         StartBit = BitIndex;
1685         RA = ATTR_MIXED;
1686         break;
1687       default:
1688         llvm_unreachable("Unexpected bitAttr!");
1689       }
1690       break;
1691     case ATTR_MIXED:
1692       switch (bitAttr) {
1693       case ATTR_FILTERED:
1694         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1695         StartBit = BitIndex;
1696         RA = ATTR_NONE;
1697         break;
1698       case ATTR_ALL_SET:
1699         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1700         StartBit = BitIndex;
1701         RA = ATTR_ALL_SET;
1702         break;
1703       case ATTR_ALL_UNSET:
1704         reportRegion(RA, StartBit, BitIndex, AllowMixed);
1705         RA = ATTR_NONE;
1706         break;
1707       case ATTR_MIXED:
1708         break;
1709       default:
1710         llvm_unreachable("Unexpected bitAttr!");
1711       }
1712       break;
1713     case ATTR_ALL_UNSET:
1714       llvm_unreachable("regionAttr state machine has no ATTR_UNSET state");
1715     case ATTR_FILTERED:
1716       llvm_unreachable("regionAttr state machine has no ATTR_FILTERED state");
1717     }
1718   }
1719 
1720   // At the end, if we're still in ALL_SET or MIXED states, report a region
1721   switch (RA) {
1722   case ATTR_NONE:
1723     break;
1724   case ATTR_FILTERED:
1725     break;
1726   case ATTR_ALL_SET:
1727     reportRegion(RA, StartBit, BitIndex, AllowMixed);
1728     break;
1729   case ATTR_ALL_UNSET:
1730     break;
1731   case ATTR_MIXED:
1732     reportRegion(RA, StartBit, BitIndex, AllowMixed);
1733     break;
1734   }
1735 
1736   // We have finished with the filter processings.  Now it's time to choose
1737   // the best performing filter.
1738   BestIndex = 0;
1739   bool AllUseless = true;
1740   unsigned BestScore = 0;
1741 
1742   for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
1743     unsigned Usefulness = Filters[i].usefulness();
1744 
1745     if (Usefulness)
1746       AllUseless = false;
1747 
1748     if (Usefulness > BestScore) {
1749       BestIndex = i;
1750       BestScore = Usefulness;
1751     }
1752   }
1753 
1754   if (!AllUseless)
1755     bestFilter().recurse();
1756 
1757   return !AllUseless;
1758 } // end of FilterChooser::filterProcessor(bool)
1759 
1760 // Decides on the best configuration of filter(s) to use in order to decode
1761 // the instructions.  A conflict of instructions may occur, in which case we
1762 // dump the conflict set to the standard error.
1763 void FilterChooser::doFilter() {
1764   unsigned Num = Opcodes.size();
1765   assert(Num && "FilterChooser created with no instructions");
1766 
1767   // Try regions of consecutive known bit values first.
1768   if (filterProcessor(false))
1769     return;
1770 
1771   // Then regions of mixed bits (both known and unitialized bit values allowed).
1772   if (filterProcessor(true))
1773     return;
1774 
1775   // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1776   // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1777   // well-known encoding pattern.  In such case, we backtrack and scan for the
1778   // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1779   if (Num == 3 && filterProcessor(true, false))
1780     return;
1781 
1782   // If we come to here, the instruction decoding has failed.
1783   // Set the BestIndex to -1 to indicate so.
1784   BestIndex = -1;
1785 }
1786 
1787 // emitTableEntries - Emit state machine entries to decode our share of
1788 // instructions.
1789 void FilterChooser::emitTableEntries(DecoderTableInfo &TableInfo) const {
1790   if (Opcodes.size() == 1) {
1791     // There is only one instruction in the set, which is great!
1792     // Call emitSingletonDecoder() to see whether there are any remaining
1793     // encodings bits.
1794     emitSingletonTableEntry(TableInfo, Opcodes[0]);
1795     return;
1796   }
1797 
1798   // Choose the best filter to do the decodings!
1799   if (BestIndex != -1) {
1800     const Filter &Best = Filters[BestIndex];
1801     if (Best.getNumFiltered() == 1)
1802       emitSingletonTableEntry(TableInfo, Best);
1803     else
1804       Best.emitTableEntry(TableInfo);
1805     return;
1806   }
1807 
1808   // We don't know how to decode these instructions!  Dump the
1809   // conflict set and bail.
1810 
1811   // Print out useful conflict information for postmortem analysis.
1812   errs() << "Decoding Conflict:\n";
1813 
1814   dumpStack(errs(), "\t\t");
1815 
1816   for (auto Opcode : Opcodes) {
1817     errs() << '\t';
1818     emitNameWithID(errs(), Opcode.EncodingID);
1819     errs() << " ";
1820     dumpBits(
1821         errs(),
1822         getBitsField(*AllInstructions[Opcode.EncodingID].EncodingDef, "Inst"));
1823     errs() << '\n';
1824   }
1825 }
1826 
1827 static std::string findOperandDecoderMethod(Record *Record) {
1828   std::string Decoder;
1829 
1830   RecordVal *DecoderString = Record->getValue("DecoderMethod");
1831   StringInit *String = DecoderString ?
1832     dyn_cast<StringInit>(DecoderString->getValue()) : nullptr;
1833   if (String) {
1834     Decoder = std::string(String->getValue());
1835     if (!Decoder.empty())
1836       return Decoder;
1837   }
1838 
1839   if (Record->isSubClassOf("RegisterOperand"))
1840     Record = Record->getValueAsDef("RegClass");
1841 
1842   if (Record->isSubClassOf("RegisterClass")) {
1843     Decoder = "Decode" + Record->getName().str() + "RegisterClass";
1844   } else if (Record->isSubClassOf("PointerLikeRegClass")) {
1845     Decoder = "DecodePointerLikeRegClass" +
1846       utostr(Record->getValueAsInt("RegClassKind"));
1847   }
1848 
1849   return Decoder;
1850 }
1851 
1852 OperandInfo getOpInfo(Record *TypeRecord) {
1853   std::string Decoder = findOperandDecoderMethod(TypeRecord);
1854 
1855   RecordVal *HasCompleteDecoderVal = TypeRecord->getValue("hasCompleteDecoder");
1856   BitInit *HasCompleteDecoderBit =
1857       HasCompleteDecoderVal
1858           ? dyn_cast<BitInit>(HasCompleteDecoderVal->getValue())
1859           : nullptr;
1860   bool HasCompleteDecoder =
1861       HasCompleteDecoderBit ? HasCompleteDecoderBit->getValue() : true;
1862 
1863   return OperandInfo(Decoder, HasCompleteDecoder);
1864 }
1865 
1866 void parseVarLenInstOperand(const Record &Def,
1867                             std::vector<OperandInfo> &Operands,
1868                             const CodeGenInstruction &CGI) {
1869 
1870   const RecordVal *RV = Def.getValue("Inst");
1871   VarLenInst VLI(cast<DagInit>(RV->getValue()), RV);
1872   SmallVector<int> TiedTo;
1873 
1874   for (unsigned Idx = 0; Idx < CGI.Operands.size(); ++Idx) {
1875     auto &Op = CGI.Operands[Idx];
1876     if (Op.MIOperandInfo && Op.MIOperandInfo->getNumArgs() > 0)
1877       for (auto *Arg : Op.MIOperandInfo->getArgs())
1878         Operands.push_back(getOpInfo(cast<DefInit>(Arg)->getDef()));
1879     else
1880       Operands.push_back(getOpInfo(Op.Rec));
1881 
1882     int TiedReg = Op.getTiedRegister();
1883     TiedTo.push_back(-1);
1884     if (TiedReg != -1) {
1885       TiedTo[Idx] = TiedReg;
1886       TiedTo[TiedReg] = Idx;
1887     }
1888   }
1889 
1890   unsigned CurrBitPos = 0;
1891   for (auto &EncodingSegment : VLI) {
1892     unsigned Offset = 0;
1893     StringRef OpName;
1894 
1895     if (const StringInit *SI = dyn_cast<StringInit>(EncodingSegment.Value)) {
1896       OpName = SI->getValue();
1897     } else if (const DagInit *DI = dyn_cast<DagInit>(EncodingSegment.Value)) {
1898       OpName = cast<StringInit>(DI->getArg(0))->getValue();
1899       Offset = cast<IntInit>(DI->getArg(2))->getValue();
1900     }
1901 
1902     if (!OpName.empty()) {
1903       auto OpSubOpPair =
1904           const_cast<CodeGenInstruction &>(CGI).Operands.ParseOperandName(
1905               OpName);
1906       unsigned OpIdx = CGI.Operands.getFlattenedOperandNumber(OpSubOpPair);
1907       Operands[OpIdx].addField(CurrBitPos, EncodingSegment.BitWidth, Offset);
1908 
1909       int TiedReg = TiedTo[OpSubOpPair.first];
1910       if (TiedReg != -1) {
1911         unsigned OpIdx = CGI.Operands.getFlattenedOperandNumber(
1912             std::make_pair(TiedReg, OpSubOpPair.second));
1913         Operands[OpIdx].addField(CurrBitPos, EncodingSegment.BitWidth, Offset);
1914       }
1915     }
1916 
1917     CurrBitPos += EncodingSegment.BitWidth;
1918   }
1919 }
1920 
1921 static unsigned
1922 populateInstruction(CodeGenTarget &Target, const Record &EncodingDef,
1923                     const CodeGenInstruction &CGI, unsigned Opc,
1924                     std::map<unsigned, std::vector<OperandInfo>> &Operands,
1925                     bool IsVarLenInst) {
1926   const Record &Def = *CGI.TheDef;
1927   // If all the bit positions are not specified; do not decode this instruction.
1928   // We are bound to fail!  For proper disassembly, the well-known encoding bits
1929   // of the instruction must be fully specified.
1930 
1931   BitsInit &Bits = getBitsField(EncodingDef, "Inst");
1932   if (Bits.allInComplete())
1933     return 0;
1934 
1935   std::vector<OperandInfo> InsnOperands;
1936 
1937   // If the instruction has specified a custom decoding hook, use that instead
1938   // of trying to auto-generate the decoder.
1939   StringRef InstDecoder = EncodingDef.getValueAsString("DecoderMethod");
1940   if (InstDecoder != "") {
1941     bool HasCompleteInstDecoder = EncodingDef.getValueAsBit("hasCompleteDecoder");
1942     InsnOperands.push_back(
1943         OperandInfo(std::string(InstDecoder), HasCompleteInstDecoder));
1944     Operands[Opc] = InsnOperands;
1945     return Bits.getNumBits();
1946   }
1947 
1948   // Generate a description of the operand of the instruction that we know
1949   // how to decode automatically.
1950   // FIXME: We'll need to have a way to manually override this as needed.
1951 
1952   // Gather the outputs/inputs of the instruction, so we can find their
1953   // positions in the encoding.  This assumes for now that they appear in the
1954   // MCInst in the order that they're listed.
1955   std::vector<std::pair<Init*, StringRef>> InOutOperands;
1956   DagInit *Out  = Def.getValueAsDag("OutOperandList");
1957   DagInit *In  = Def.getValueAsDag("InOperandList");
1958   for (unsigned i = 0; i < Out->getNumArgs(); ++i)
1959     InOutOperands.push_back(
1960         std::make_pair(Out->getArg(i), Out->getArgNameStr(i)));
1961   for (unsigned i = 0; i < In->getNumArgs(); ++i)
1962     InOutOperands.push_back(
1963         std::make_pair(In->getArg(i), In->getArgNameStr(i)));
1964 
1965   // Search for tied operands, so that we can correctly instantiate
1966   // operands that are not explicitly represented in the encoding.
1967   std::map<std::string, std::string> TiedNames;
1968   for (unsigned i = 0; i < CGI.Operands.size(); ++i) {
1969     int tiedTo = CGI.Operands[i].getTiedRegister();
1970     if (tiedTo != -1) {
1971       std::pair<unsigned, unsigned> SO =
1972         CGI.Operands.getSubOperandNumber(tiedTo);
1973       TiedNames[std::string(InOutOperands[i].second)] =
1974           std::string(InOutOperands[SO.first].second);
1975       TiedNames[std::string(InOutOperands[SO.first].second)] =
1976           std::string(InOutOperands[i].second);
1977     }
1978   }
1979 
1980   if (IsVarLenInst) {
1981     parseVarLenInstOperand(EncodingDef, InsnOperands, CGI);
1982   } else {
1983     std::map<std::string, std::vector<OperandInfo>> NumberedInsnOperands;
1984     std::set<std::string> NumberedInsnOperandsNoTie;
1985     if (Target.getInstructionSet()->getValueAsBit(
1986             "decodePositionallyEncodedOperands")) {
1987       const std::vector<RecordVal> &Vals = Def.getValues();
1988       unsigned NumberedOp = 0;
1989 
1990       std::set<unsigned> NamedOpIndices;
1991       if (Target.getInstructionSet()->getValueAsBit(
1992               "noNamedPositionallyEncodedOperands"))
1993         // Collect the set of operand indices that might correspond to named
1994         // operand, and skip these when assigning operands based on position.
1995         for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1996           unsigned OpIdx;
1997           if (!CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
1998             continue;
1999 
2000           NamedOpIndices.insert(OpIdx);
2001         }
2002 
2003       for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2004         // Ignore fixed fields in the record, we're looking for values like:
2005         //    bits<5> RST = { ?, ?, ?, ?, ? };
2006         if (Vals[i].isNonconcreteOK() || Vals[i].getValue()->isComplete())
2007           continue;
2008 
2009         // Determine if Vals[i] actually contributes to the Inst encoding.
2010         unsigned bi = 0;
2011         for (; bi < Bits.getNumBits(); ++bi) {
2012           VarInit *Var = nullptr;
2013           VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
2014           if (BI)
2015             Var = dyn_cast<VarInit>(BI->getBitVar());
2016           else
2017             Var = dyn_cast<VarInit>(Bits.getBit(bi));
2018 
2019           if (Var && Var->getName() == Vals[i].getName())
2020             break;
2021         }
2022 
2023         if (bi == Bits.getNumBits())
2024           continue;
2025 
2026         // Skip variables that correspond to explicitly-named operands.
2027         unsigned OpIdx;
2028         if (CGI.Operands.hasOperandNamed(Vals[i].getName(), OpIdx))
2029           continue;
2030 
2031         // Get the bit range for this operand:
2032         unsigned bitStart = bi++, bitWidth = 1;
2033         for (; bi < Bits.getNumBits(); ++bi) {
2034           VarInit *Var = nullptr;
2035           VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
2036           if (BI)
2037             Var = dyn_cast<VarInit>(BI->getBitVar());
2038           else
2039             Var = dyn_cast<VarInit>(Bits.getBit(bi));
2040 
2041           if (!Var)
2042             break;
2043 
2044           if (Var->getName() != Vals[i].getName())
2045             break;
2046 
2047           ++bitWidth;
2048         }
2049 
2050         unsigned NumberOps = CGI.Operands.size();
2051         while (NumberedOp < NumberOps &&
2052                (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
2053                 (!NamedOpIndices.empty() &&
2054                  NamedOpIndices.count(
2055                      CGI.Operands.getSubOperandNumber(NumberedOp).first))))
2056           ++NumberedOp;
2057 
2058         OpIdx = NumberedOp++;
2059 
2060         // OpIdx now holds the ordered operand number of Vals[i].
2061         std::pair<unsigned, unsigned> SO =
2062             CGI.Operands.getSubOperandNumber(OpIdx);
2063         const std::string &Name = CGI.Operands[SO.first].Name;
2064 
2065         LLVM_DEBUG(dbgs() << "Numbered operand mapping for " << Def.getName()
2066                           << ": " << Name << "(" << SO.first << ", "
2067                           << SO.second << ") => " << Vals[i].getName() << "\n");
2068 
2069         std::string Decoder;
2070         Record *TypeRecord = CGI.Operands[SO.first].Rec;
2071 
2072         RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
2073         StringInit *String =
2074             DecoderString ? dyn_cast<StringInit>(DecoderString->getValue())
2075                           : nullptr;
2076         if (String && String->getValue() != "")
2077           Decoder = std::string(String->getValue());
2078 
2079         if (Decoder == "" && CGI.Operands[SO.first].MIOperandInfo &&
2080             CGI.Operands[SO.first].MIOperandInfo->getNumArgs()) {
2081           Init *Arg = CGI.Operands[SO.first].MIOperandInfo->getArg(SO.second);
2082           if (DefInit *DI = cast<DefInit>(Arg))
2083             TypeRecord = DI->getDef();
2084         }
2085 
2086         bool isReg = false;
2087         if (TypeRecord->isSubClassOf("RegisterOperand"))
2088           TypeRecord = TypeRecord->getValueAsDef("RegClass");
2089         if (TypeRecord->isSubClassOf("RegisterClass")) {
2090           Decoder = "Decode" + TypeRecord->getName().str() + "RegisterClass";
2091           isReg = true;
2092         } else if (TypeRecord->isSubClassOf("PointerLikeRegClass")) {
2093           Decoder = "DecodePointerLikeRegClass" +
2094                     utostr(TypeRecord->getValueAsInt("RegClassKind"));
2095           isReg = true;
2096         }
2097 
2098         DecoderString = TypeRecord->getValue("DecoderMethod");
2099         String = DecoderString ? dyn_cast<StringInit>(DecoderString->getValue())
2100                                : nullptr;
2101         if (!isReg && String && String->getValue() != "")
2102           Decoder = std::string(String->getValue());
2103 
2104         RecordVal *HasCompleteDecoderVal =
2105             TypeRecord->getValue("hasCompleteDecoder");
2106         BitInit *HasCompleteDecoderBit =
2107             HasCompleteDecoderVal
2108                 ? dyn_cast<BitInit>(HasCompleteDecoderVal->getValue())
2109                 : nullptr;
2110         bool HasCompleteDecoder =
2111             HasCompleteDecoderBit ? HasCompleteDecoderBit->getValue() : true;
2112 
2113         OperandInfo OpInfo(Decoder, HasCompleteDecoder);
2114         OpInfo.addField(bitStart, bitWidth, 0);
2115 
2116         NumberedInsnOperands[Name].push_back(OpInfo);
2117 
2118         // FIXME: For complex operands with custom decoders we can't handle tied
2119         // sub-operands automatically. Skip those here and assume that this is
2120         // fixed up elsewhere.
2121         if (CGI.Operands[SO.first].MIOperandInfo &&
2122             CGI.Operands[SO.first].MIOperandInfo->getNumArgs() > 1 && String &&
2123             String->getValue() != "")
2124           NumberedInsnOperandsNoTie.insert(Name);
2125       }
2126     }
2127 
2128     // For each operand, see if we can figure out where it is encoded.
2129     for (const auto &Op : InOutOperands) {
2130       if (!NumberedInsnOperands[std::string(Op.second)].empty()) {
2131         llvm::append_range(InsnOperands,
2132                            NumberedInsnOperands[std::string(Op.second)]);
2133         continue;
2134       }
2135       if (!NumberedInsnOperands[TiedNames[std::string(Op.second)]].empty()) {
2136         if (!NumberedInsnOperandsNoTie.count(
2137                 TiedNames[std::string(Op.second)])) {
2138           // Figure out to which (sub)operand we're tied.
2139           unsigned i =
2140               CGI.Operands.getOperandNamed(TiedNames[std::string(Op.second)]);
2141           int tiedTo = CGI.Operands[i].getTiedRegister();
2142           if (tiedTo == -1) {
2143             i = CGI.Operands.getOperandNamed(Op.second);
2144             tiedTo = CGI.Operands[i].getTiedRegister();
2145           }
2146 
2147           if (tiedTo != -1) {
2148             std::pair<unsigned, unsigned> SO =
2149                 CGI.Operands.getSubOperandNumber(tiedTo);
2150 
2151             InsnOperands.push_back(
2152                 NumberedInsnOperands[TiedNames[std::string(Op.second)]]
2153                                     [SO.second]);
2154           }
2155         }
2156         continue;
2157       }
2158 
2159       // At this point, we can locate the decoder field, but we need to know how
2160       // to interpret it.  As a first step, require the target to provide
2161       // callbacks for decoding register classes.
2162 
2163       OperandInfo OpInfo = getOpInfo(cast<DefInit>(Op.first)->getDef());
2164 
2165       // Some bits of the operand may be required to be 1 depending on the
2166       // instruction's encoding. Collect those bits.
2167       if (const RecordVal *EncodedValue = EncodingDef.getValue(Op.second))
2168         if (const BitsInit *OpBits =
2169                 dyn_cast<BitsInit>(EncodedValue->getValue()))
2170           for (unsigned I = 0; I < OpBits->getNumBits(); ++I)
2171             if (const BitInit *OpBit = dyn_cast<BitInit>(OpBits->getBit(I)))
2172               if (OpBit->getValue())
2173                 OpInfo.InitValue |= 1ULL << I;
2174 
2175       unsigned Base = ~0U;
2176       unsigned Width = 0;
2177       unsigned Offset = 0;
2178 
2179       for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
2180         VarInit *Var = nullptr;
2181         VarBitInit *BI = dyn_cast<VarBitInit>(Bits.getBit(bi));
2182         if (BI)
2183           Var = dyn_cast<VarInit>(BI->getBitVar());
2184         else
2185           Var = dyn_cast<VarInit>(Bits.getBit(bi));
2186 
2187         if (!Var) {
2188           if (Base != ~0U) {
2189             OpInfo.addField(Base, Width, Offset);
2190             Base = ~0U;
2191             Width = 0;
2192             Offset = 0;
2193           }
2194           continue;
2195         }
2196 
2197         if ((Var->getName() != Op.second &&
2198              Var->getName() != TiedNames[std::string(Op.second)])) {
2199           if (Base != ~0U) {
2200             OpInfo.addField(Base, Width, Offset);
2201             Base = ~0U;
2202             Width = 0;
2203             Offset = 0;
2204           }
2205           continue;
2206         }
2207 
2208         if (Base == ~0U) {
2209           Base = bi;
2210           Width = 1;
2211           Offset = BI ? BI->getBitNum() : 0;
2212         } else if (BI && BI->getBitNum() != Offset + Width) {
2213           OpInfo.addField(Base, Width, Offset);
2214           Base = bi;
2215           Width = 1;
2216           Offset = BI->getBitNum();
2217         } else {
2218           ++Width;
2219         }
2220       }
2221 
2222       if (Base != ~0U)
2223         OpInfo.addField(Base, Width, Offset);
2224 
2225       if (OpInfo.numFields() > 0)
2226         InsnOperands.push_back(OpInfo);
2227     }
2228   }
2229 
2230   Operands[Opc] = InsnOperands;
2231 
2232 #if 0
2233   LLVM_DEBUG({
2234       // Dumps the instruction encoding bits.
2235       dumpBits(errs(), Bits);
2236 
2237       errs() << '\n';
2238 
2239       // Dumps the list of operand info.
2240       for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
2241         const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
2242         const std::string &OperandName = Info.Name;
2243         const Record &OperandDef = *Info.Rec;
2244 
2245         errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
2246       }
2247     });
2248 #endif
2249 
2250   return Bits.getNumBits();
2251 }
2252 
2253 // emitFieldFromInstruction - Emit the templated helper function
2254 // fieldFromInstruction().
2255 // On Windows we make sure that this function is not inlined when
2256 // using the VS compiler. It has a bug which causes the function
2257 // to be optimized out in some circustances. See llvm.org/pr38292
2258 static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
2259   OS << "// Helper functions for extracting fields from encoded instructions.\n"
2260      << "// InsnType must either be integral or an APInt-like object that "
2261         "must:\n"
2262      << "// * be default-constructible and copy-constructible\n"
2263      << "// * be constructible from an APInt (this can be private)\n"
2264      << "// * Support insertBits(bits, startBit, numBits)\n"
2265      << "// * Support extractBitsAsZExtValue(numBits, startBit)\n"
2266      << "// * Support the ~, &, ==, and != operators with other objects of "
2267         "the same type\n"
2268      << "// * Support the != and bitwise & with uint64_t\n"
2269      << "// * Support put (<<) to raw_ostream&\n"
2270      << "template <typename InsnType>\n"
2271      << "#if defined(_MSC_VER) && !defined(__clang__)\n"
2272      << "__declspec(noinline)\n"
2273      << "#endif\n"
2274      << "static std::enable_if_t<std::is_integral<InsnType>::value, InsnType>\n"
2275      << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n"
2276      << "                     unsigned numBits) {\n"
2277      << "  assert(startBit + numBits <= 64 && \"Cannot support >64-bit "
2278         "extractions!\");\n"
2279      << "  assert(startBit + numBits <= (sizeof(InsnType) * 8) &&\n"
2280      << "         \"Instruction field out of bounds!\");\n"
2281      << "  InsnType fieldMask;\n"
2282      << "  if (numBits == sizeof(InsnType) * 8)\n"
2283      << "    fieldMask = (InsnType)(-1LL);\n"
2284      << "  else\n"
2285      << "    fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
2286      << "  return (insn & fieldMask) >> startBit;\n"
2287      << "}\n"
2288      << "\n"
2289      << "template <typename InsnType>\n"
2290      << "static std::enable_if_t<!std::is_integral<InsnType>::value, "
2291         "uint64_t>\n"
2292      << "fieldFromInstruction(const InsnType &insn, unsigned startBit,\n"
2293      << "                     unsigned numBits) {\n"
2294      << "  return insn.extractBitsAsZExtValue(numBits, startBit);\n"
2295      << "}\n\n";
2296 }
2297 
2298 // emitInsertBits - Emit the templated helper function insertBits().
2299 static void emitInsertBits(formatted_raw_ostream &OS) {
2300   OS << "// Helper function for inserting bits extracted from an encoded "
2301         "instruction into\n"
2302      << "// a field.\n"
2303      << "template <typename InsnType>\n"
2304      << "static std::enable_if_t<std::is_integral<InsnType>::value>\n"
2305      << "insertBits(InsnType &field, InsnType bits, unsigned startBit, "
2306         "unsigned numBits) {\n"
2307      << "  assert(startBit + numBits <= sizeof field * 8);\n"
2308      << "  field |= (InsnType)bits << startBit;\n"
2309      << "}\n"
2310      << "\n"
2311      << "template <typename InsnType>\n"
2312      << "static std::enable_if_t<!std::is_integral<InsnType>::value>\n"
2313      << "insertBits(InsnType &field, uint64_t bits, unsigned startBit, "
2314         "unsigned numBits) {\n"
2315      << "  field.insertBits(bits, startBit, numBits);\n"
2316      << "}\n\n";
2317 }
2318 
2319 // emitDecodeInstruction - Emit the templated helper function
2320 // decodeInstruction().
2321 static void emitDecodeInstruction(formatted_raw_ostream &OS,
2322                                   bool IsVarLenInst) {
2323   OS << "template <typename InsnType>\n"
2324      << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2325         "MCInst &MI,\n"
2326      << "                                      InsnType insn, uint64_t "
2327         "Address,\n"
2328      << "                                      const MCDisassembler *DisAsm,\n"
2329      << "                                      const MCSubtargetInfo &STI";
2330   if (IsVarLenInst) {
2331     OS << ",\n"
2332        << "                                      llvm::function_ref<void(APInt "
2333           "&,"
2334        << " uint64_t)> makeUp";
2335   }
2336   OS << ") {\n"
2337      << "  const FeatureBitset &Bits = STI.getFeatureBits();\n"
2338      << "\n"
2339      << "  const uint8_t *Ptr = DecodeTable;\n"
2340      << "  uint64_t CurFieldValue = 0;\n"
2341      << "  DecodeStatus S = MCDisassembler::Success;\n"
2342      << "  while (true) {\n"
2343      << "    ptrdiff_t Loc = Ptr - DecodeTable;\n"
2344      << "    switch (*Ptr) {\n"
2345      << "    default:\n"
2346      << "      errs() << Loc << \": Unexpected decode table opcode!\\n\";\n"
2347      << "      return MCDisassembler::Fail;\n"
2348      << "    case MCD::OPC_ExtractField: {\n"
2349      << "      unsigned Start = *++Ptr;\n"
2350      << "      unsigned Len = *++Ptr;\n"
2351      << "      ++Ptr;\n";
2352   if (IsVarLenInst)
2353     OS << "      makeUp(insn, Start + Len);\n";
2354   OS << "      CurFieldValue = fieldFromInstruction(insn, Start, Len);\n"
2355      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_ExtractField(\" << Start << "
2356         "\", \"\n"
2357      << "                   << Len << \"): \" << CurFieldValue << \"\\n\");\n"
2358      << "      break;\n"
2359      << "    }\n"
2360      << "    case MCD::OPC_FilterValue: {\n"
2361      << "      // Decode the field value.\n"
2362      << "      unsigned Len;\n"
2363      << "      uint64_t Val = decodeULEB128(++Ptr, &Len);\n"
2364      << "      Ptr += Len;\n"
2365      << "      // NumToSkip is a plain 24-bit integer.\n"
2366      << "      unsigned NumToSkip = *Ptr++;\n"
2367      << "      NumToSkip |= (*Ptr++) << 8;\n"
2368      << "      NumToSkip |= (*Ptr++) << 16;\n"
2369      << "\n"
2370      << "      // Perform the filter operation.\n"
2371      << "      if (Val != CurFieldValue)\n"
2372      << "        Ptr += NumToSkip;\n"
2373      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_FilterValue(\" << Val << "
2374         "\", \" << NumToSkip\n"
2375      << "                   << \"): \" << ((Val != CurFieldValue) ? \"FAIL:\" "
2376         ": \"PASS:\")\n"
2377      << "                   << \" continuing at \" << (Ptr - DecodeTable) << "
2378         "\"\\n\");\n"
2379      << "\n"
2380      << "      break;\n"
2381      << "    }\n"
2382      << "    case MCD::OPC_CheckField: {\n"
2383      << "      unsigned Start = *++Ptr;\n"
2384      << "      unsigned Len = *++Ptr;\n";
2385   if (IsVarLenInst)
2386     OS << "      makeUp(insn, Start + Len);\n";
2387   OS << "      uint64_t FieldValue = fieldFromInstruction(insn, Start, Len);\n"
2388      << "      // Decode the field value.\n"
2389      << "      unsigned PtrLen = 0;\n"
2390      << "      uint64_t ExpectedValue = decodeULEB128(++Ptr, &PtrLen);\n"
2391      << "      Ptr += PtrLen;\n"
2392      << "      // NumToSkip is a plain 24-bit integer.\n"
2393      << "      unsigned NumToSkip = *Ptr++;\n"
2394      << "      NumToSkip |= (*Ptr++) << 8;\n"
2395      << "      NumToSkip |= (*Ptr++) << 16;\n"
2396      << "\n"
2397      << "      // If the actual and expected values don't match, skip.\n"
2398      << "      if (ExpectedValue != FieldValue)\n"
2399      << "        Ptr += NumToSkip;\n"
2400      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckField(\" << Start << "
2401         "\", \"\n"
2402      << "                   << Len << \", \" << ExpectedValue << \", \" << "
2403         "NumToSkip\n"
2404      << "                   << \"): FieldValue = \" << FieldValue << \", "
2405         "ExpectedValue = \"\n"
2406      << "                   << ExpectedValue << \": \"\n"
2407      << "                   << ((ExpectedValue == FieldValue) ? \"PASS\\n\" : "
2408         "\"FAIL\\n\"));\n"
2409      << "      break;\n"
2410      << "    }\n"
2411      << "    case MCD::OPC_CheckPredicate: {\n"
2412      << "      unsigned Len;\n"
2413      << "      // Decode the Predicate Index value.\n"
2414      << "      unsigned PIdx = decodeULEB128(++Ptr, &Len);\n"
2415      << "      Ptr += Len;\n"
2416      << "      // NumToSkip is a plain 24-bit integer.\n"
2417      << "      unsigned NumToSkip = *Ptr++;\n"
2418      << "      NumToSkip |= (*Ptr++) << 8;\n"
2419      << "      NumToSkip |= (*Ptr++) << 16;\n"
2420      << "      // Check the predicate.\n"
2421      << "      bool Pred;\n"
2422      << "      if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\n"
2423      << "        Ptr += NumToSkip;\n"
2424      << "      (void)Pred;\n"
2425      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_CheckPredicate(\" << PIdx "
2426         "<< \"): \"\n"
2427      << "            << (Pred ? \"PASS\\n\" : \"FAIL\\n\"));\n"
2428      << "\n"
2429      << "      break;\n"
2430      << "    }\n"
2431      << "    case MCD::OPC_Decode: {\n"
2432      << "      unsigned Len;\n"
2433      << "      // Decode the Opcode value.\n"
2434      << "      unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2435      << "      Ptr += Len;\n"
2436      << "      unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2437      << "      Ptr += Len;\n"
2438      << "\n"
2439      << "      MI.clear();\n"
2440      << "      MI.setOpcode(Opc);\n"
2441      << "      bool DecodeComplete;\n";
2442   if (IsVarLenInst) {
2443     OS << "      Len = InstrLenTable[Opc];\n"
2444        << "      makeUp(insn, Len);\n";
2445   }
2446   OS << "      S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
2447         "DecodeComplete);\n"
2448      << "      assert(DecodeComplete);\n"
2449      << "\n"
2450      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_Decode: opcode \" << Opc\n"
2451      << "                   << \", using decoder \" << DecodeIdx << \": \"\n"
2452      << "                   << (S != MCDisassembler::Fail ? \"PASS\" : "
2453         "\"FAIL\") << \"\\n\");\n"
2454      << "      return S;\n"
2455      << "    }\n"
2456      << "    case MCD::OPC_TryDecode: {\n"
2457      << "      unsigned Len;\n"
2458      << "      // Decode the Opcode value.\n"
2459      << "      unsigned Opc = decodeULEB128(++Ptr, &Len);\n"
2460      << "      Ptr += Len;\n"
2461      << "      unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\n"
2462      << "      Ptr += Len;\n"
2463      << "      // NumToSkip is a plain 24-bit integer.\n"
2464      << "      unsigned NumToSkip = *Ptr++;\n"
2465      << "      NumToSkip |= (*Ptr++) << 8;\n"
2466      << "      NumToSkip |= (*Ptr++) << 16;\n"
2467      << "\n"
2468      << "      // Perform the decode operation.\n"
2469      << "      MCInst TmpMI;\n"
2470      << "      TmpMI.setOpcode(Opc);\n"
2471      << "      bool DecodeComplete;\n"
2472      << "      S = decodeToMCInst(S, DecodeIdx, insn, TmpMI, Address, DisAsm, "
2473         "DecodeComplete);\n"
2474      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_TryDecode: opcode \" << "
2475         "Opc\n"
2476      << "                   << \", using decoder \" << DecodeIdx << \": \");\n"
2477      << "\n"
2478      << "      if (DecodeComplete) {\n"
2479      << "        // Decoding complete.\n"
2480      << "        LLVM_DEBUG(dbgs() << (S != MCDisassembler::Fail ? \"PASS\" : "
2481         "\"FAIL\") << \"\\n\");\n"
2482      << "        MI = TmpMI;\n"
2483      << "        return S;\n"
2484      << "      } else {\n"
2485      << "        assert(S == MCDisassembler::Fail);\n"
2486      << "        // If the decoding was incomplete, skip.\n"
2487      << "        Ptr += NumToSkip;\n"
2488      << "        LLVM_DEBUG(dbgs() << \"FAIL: continuing at \" << (Ptr - "
2489         "DecodeTable) << \"\\n\");\n"
2490      << "        // Reset decode status. This also drops a SoftFail status "
2491         "that could be\n"
2492      << "        // set before the decode attempt.\n"
2493      << "        S = MCDisassembler::Success;\n"
2494      << "      }\n"
2495      << "      break;\n"
2496      << "    }\n"
2497      << "    case MCD::OPC_SoftFail: {\n"
2498      << "      // Decode the mask values.\n"
2499      << "      unsigned Len;\n"
2500      << "      uint64_t PositiveMask = decodeULEB128(++Ptr, &Len);\n"
2501      << "      Ptr += Len;\n"
2502      << "      uint64_t NegativeMask = decodeULEB128(Ptr, &Len);\n"
2503      << "      Ptr += Len;\n"
2504      << "      bool Fail = (insn & PositiveMask) != 0 || (~insn & "
2505         "NegativeMask) != 0;\n"
2506      << "      if (Fail)\n"
2507      << "        S = MCDisassembler::SoftFail;\n"
2508      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_SoftFail: \" << (Fail ? "
2509         "\"FAIL\\n\" : \"PASS\\n\"));\n"
2510      << "      break;\n"
2511      << "    }\n"
2512      << "    case MCD::OPC_Fail: {\n"
2513      << "      LLVM_DEBUG(dbgs() << Loc << \": OPC_Fail\\n\");\n"
2514      << "      return MCDisassembler::Fail;\n"
2515      << "    }\n"
2516      << "    }\n"
2517      << "  }\n"
2518      << "  llvm_unreachable(\"bogosity detected in disassembler state "
2519         "machine!\");\n"
2520      << "}\n\n";
2521 }
2522 
2523 // Emits disassembler code for instruction decoding.
2524 void DecoderEmitter::run(raw_ostream &o) {
2525   formatted_raw_ostream OS(o);
2526   OS << "#include \"llvm/MC/MCInst.h\"\n";
2527   OS << "#include \"llvm/MC/MCSubtargetInfo.h\"\n";
2528   OS << "#include \"llvm/MC/SubtargetFeature.h\"\n";
2529   OS << "#include \"llvm/Support/DataTypes.h\"\n";
2530   OS << "#include \"llvm/Support/Debug.h\"\n";
2531   OS << "#include \"llvm/Support/LEB128.h\"\n";
2532   OS << "#include \"llvm/Support/raw_ostream.h\"\n";
2533   OS << "#include <assert.h>\n";
2534   OS << '\n';
2535   OS << "namespace llvm {\n\n";
2536 
2537   emitFieldFromInstruction(OS);
2538   emitInsertBits(OS);
2539 
2540   Target.reverseBitsForLittleEndianEncoding();
2541 
2542   // Parameterize the decoders based on namespace and instruction width.
2543   std::set<StringRef> HwModeNames;
2544   const auto &NumberedInstructions = Target.getInstructionsByEnumValue();
2545   NumberedEncodings.reserve(NumberedInstructions.size());
2546   DenseMap<Record *, unsigned> IndexOfInstruction;
2547   // First, collect all HwModes referenced by the target.
2548   for (const auto &NumberedInstruction : NumberedInstructions) {
2549     IndexOfInstruction[NumberedInstruction->TheDef] = NumberedEncodings.size();
2550 
2551     if (const RecordVal *RV =
2552             NumberedInstruction->TheDef->getValue("EncodingInfos")) {
2553       if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
2554         const CodeGenHwModes &HWM = Target.getHwModes();
2555         EncodingInfoByHwMode EBM(DI->getDef(), HWM);
2556         for (auto &KV : EBM)
2557           HwModeNames.insert(HWM.getMode(KV.first).Name);
2558       }
2559     }
2560   }
2561 
2562   // If HwModeNames is empty, add the empty string so we always have one HwMode.
2563   if (HwModeNames.empty())
2564     HwModeNames.insert("");
2565 
2566   for (const auto &NumberedInstruction : NumberedInstructions) {
2567     IndexOfInstruction[NumberedInstruction->TheDef] = NumberedEncodings.size();
2568 
2569     if (const RecordVal *RV =
2570             NumberedInstruction->TheDef->getValue("EncodingInfos")) {
2571       if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
2572         const CodeGenHwModes &HWM = Target.getHwModes();
2573         EncodingInfoByHwMode EBM(DI->getDef(), HWM);
2574         for (auto &KV : EBM) {
2575           NumberedEncodings.emplace_back(KV.second, NumberedInstruction,
2576                                          HWM.getMode(KV.first).Name);
2577           HwModeNames.insert(HWM.getMode(KV.first).Name);
2578         }
2579         continue;
2580       }
2581     }
2582     // This instruction is encoded the same on all HwModes. Emit it for all
2583     // HwModes.
2584     for (StringRef HwModeName : HwModeNames)
2585       NumberedEncodings.emplace_back(NumberedInstruction->TheDef,
2586                                      NumberedInstruction, HwModeName);
2587   }
2588   for (const auto &NumberedAlias : RK.getAllDerivedDefinitions("AdditionalEncoding"))
2589     NumberedEncodings.emplace_back(
2590         NumberedAlias,
2591         &Target.getInstruction(NumberedAlias->getValueAsDef("AliasOf")));
2592 
2593   std::map<std::pair<std::string, unsigned>, std::vector<EncodingIDAndOpcode>>
2594       OpcMap;
2595   std::map<unsigned, std::vector<OperandInfo>> Operands;
2596   std::vector<unsigned> InstrLen;
2597 
2598   bool IsVarLenInst =
2599       any_of(NumberedInstructions, [](const CodeGenInstruction *CGI) {
2600         RecordVal *RV = CGI->TheDef->getValue("Inst");
2601         return RV && isa<DagInit>(RV->getValue());
2602       });
2603   unsigned MaxInstLen = 0;
2604 
2605   for (unsigned i = 0; i < NumberedEncodings.size(); ++i) {
2606     const Record *EncodingDef = NumberedEncodings[i].EncodingDef;
2607     const CodeGenInstruction *Inst = NumberedEncodings[i].Inst;
2608     const Record *Def = Inst->TheDef;
2609     unsigned Size = EncodingDef->getValueAsInt("Size");
2610     if (Def->getValueAsString("Namespace") == "TargetOpcode" ||
2611         Def->getValueAsBit("isPseudo") ||
2612         Def->getValueAsBit("isAsmParserOnly") ||
2613         Def->getValueAsBit("isCodeGenOnly")) {
2614       NumEncodingsLackingDisasm++;
2615       continue;
2616     }
2617 
2618     if (i < NumberedInstructions.size())
2619       NumInstructions++;
2620     NumEncodings++;
2621 
2622     if (!Size && !IsVarLenInst)
2623       continue;
2624 
2625     if (IsVarLenInst)
2626       InstrLen.resize(NumberedInstructions.size(), 0);
2627 
2628     if (unsigned Len = populateInstruction(Target, *EncodingDef, *Inst, i,
2629                                            Operands, IsVarLenInst)) {
2630       if (IsVarLenInst) {
2631         MaxInstLen = std::max(MaxInstLen, Len);
2632         InstrLen[i] = Len;
2633       }
2634       std::string DecoderNamespace =
2635           std::string(EncodingDef->getValueAsString("DecoderNamespace"));
2636       if (!NumberedEncodings[i].HwModeName.empty())
2637         DecoderNamespace +=
2638             std::string("_") + NumberedEncodings[i].HwModeName.str();
2639       OpcMap[std::make_pair(DecoderNamespace, Size)].emplace_back(
2640           i, IndexOfInstruction.find(Def)->second);
2641     } else {
2642       NumEncodingsOmitted++;
2643     }
2644   }
2645 
2646   DecoderTableInfo TableInfo;
2647   for (const auto &Opc : OpcMap) {
2648     // Emit the decoder for this namespace+width combination.
2649     ArrayRef<EncodingAndInst> NumberedEncodingsRef(
2650         NumberedEncodings.data(), NumberedEncodings.size());
2651     FilterChooser FC(NumberedEncodingsRef, Opc.second, Operands,
2652                      IsVarLenInst ? MaxInstLen : 8 * Opc.first.second, this);
2653 
2654     // The decode table is cleared for each top level decoder function. The
2655     // predicates and decoders themselves, however, are shared across all
2656     // decoders to give more opportunities for uniqueing.
2657     TableInfo.Table.clear();
2658     TableInfo.FixupStack.clear();
2659     TableInfo.Table.reserve(16384);
2660     TableInfo.FixupStack.emplace_back();
2661     FC.emitTableEntries(TableInfo);
2662     // Any NumToSkip fixups in the top level scope can resolve to the
2663     // OPC_Fail at the end of the table.
2664     assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2665     // Resolve any NumToSkip fixups in the current scope.
2666     resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2667                        TableInfo.Table.size());
2668     TableInfo.FixupStack.clear();
2669 
2670     TableInfo.Table.push_back(MCD::OPC_Fail);
2671 
2672     // Print the table to the output stream.
2673     emitTable(OS, TableInfo.Table, 0, FC.getBitWidth(), Opc.first.first);
2674     OS.flush();
2675   }
2676 
2677   // For variable instruction, we emit a instruction length table
2678   // to let the decoder know how long the instructions are.
2679   // You can see example usage in M68k's disassembler.
2680   if (IsVarLenInst)
2681     emitInstrLenTable(OS, InstrLen);
2682   // Emit the predicate function.
2683   emitPredicateFunction(OS, TableInfo.Predicates, 0);
2684 
2685   // Emit the decoder function.
2686   emitDecoderFunction(OS, TableInfo.Decoders, 0);
2687 
2688   // Emit the main entry point for the decoder, decodeInstruction().
2689   emitDecodeInstruction(OS, IsVarLenInst);
2690 
2691   OS << "\n} // end namespace llvm\n";
2692 }
2693 
2694 namespace llvm {
2695 
2696 void EmitDecoder(RecordKeeper &RK, raw_ostream &OS,
2697                  const std::string &PredicateNamespace,
2698                  const std::string &GPrefix, const std::string &GPostfix,
2699                  const std::string &ROK, const std::string &RFail,
2700                  const std::string &L) {
2701   DecoderEmitter(RK, PredicateNamespace, GPrefix, GPostfix, ROK, RFail, L)
2702       .run(OS);
2703 }
2704 
2705 } // end namespace llvm
2706