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