1 //===------------ FixedLenDecoderEmitter.cpp - Decoder Generator ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // It contains the tablegen backend that emits the decoder functions for
11 // targets with fixed length instruction set.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "decoder-emitter"
17 #include "FixedLenDecoderEmitter.h"
18 #include "CodeGenTarget.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
30 // The set (BIT_TRUE, BIT_FALSE, BIT_UNSET) represents a ternary logic system
33 // BIT_UNFILTERED is used as the init value for a filter position. It is used
34 // only for filter processings.
39 BIT_UNFILTERED
// unfiltered
42 static bool ValueSet(bit_value_t V
) {
43 return (V
== BIT_TRUE
|| V
== BIT_FALSE
);
45 static bool ValueNotSet(bit_value_t V
) {
46 return (V
== BIT_UNSET
);
48 static int Value(bit_value_t V
) {
49 return ValueNotSet(V
) ? -1 : (V
== BIT_FALSE
? 0 : 1);
51 static bit_value_t
bitFromBits(BitsInit
&bits
, unsigned index
) {
52 if (BitInit
*bit
= dynamic_cast<BitInit
*>(bits
.getBit(index
)))
53 return bit
->getValue() ? BIT_TRUE
: BIT_FALSE
;
55 // The bit is uninitialized.
58 // Prints the bit value for each position.
59 static void dumpBits(raw_ostream
&o
, BitsInit
&bits
) {
62 for (index
= bits
.getNumBits(); index
> 0; index
--) {
63 switch (bitFromBits(bits
, index
- 1)) {
74 assert(0 && "unexpected return value from bitFromBits");
79 static BitsInit
&getBitsField(const Record
&def
, const char *str
) {
80 BitsInit
*bits
= def
.getValueAsBitsInit(str
);
84 // Forward declaration.
87 // FIXME: Possibly auto-detected?
90 // Representation of the instruction to work on.
91 typedef bit_value_t insn_t
[BIT_WIDTH
];
93 /// Filter - Filter works with FilterChooser to produce the decoding tree for
96 /// It is useful to think of a Filter as governing the switch stmts of the
97 /// decoding tree in a certain level. Each case stmt delegates to an inferior
98 /// FilterChooser to decide what further decoding logic to employ, or in another
99 /// words, what other remaining bits to look at. The FilterChooser eventually
100 /// chooses a best Filter to do its job.
102 /// This recursive scheme ends when the number of Opcodes assigned to the
103 /// FilterChooser becomes 1 or if there is a conflict. A conflict happens when
104 /// the Filter/FilterChooser combo does not know how to distinguish among the
105 /// Opcodes assigned.
107 /// An example of a conflict is
110 /// 111101000.00........00010000....
111 /// 111101000.00........0001........
112 /// 1111010...00........0001........
113 /// 1111010...00....................
114 /// 1111010.........................
115 /// 1111............................
116 /// ................................
117 /// VST4q8a 111101000_00________00010000____
118 /// VST4q8b 111101000_00________00010000____
120 /// The Debug output shows the path that the decoding tree follows to reach the
121 /// the conclusion that there is a conflict. VST4q8a is a vst4 to double-spaced
122 /// even registers, while VST4q8b is a vst4 to double-spaced odd regsisters.
124 /// The encoding info in the .td files does not specify this meta information,
125 /// which could have been used by the decoder to resolve the conflict. The
126 /// decoder could try to decode the even/odd register numbering and assign to
127 /// VST4q8a or VST4q8b, but for the time being, the decoder chooses the "a"
128 /// version and return the Opcode since the two have the same Asm format string.
131 FilterChooser
*Owner
; // points to the FilterChooser who owns this filter
132 unsigned StartBit
; // the starting bit position
133 unsigned NumBits
; // number of bits to filter
134 bool Mixed
; // a mixed region contains both set and unset bits
136 // Map of well-known segment value to the set of uid's with that value.
137 std::map
<uint64_t, std::vector
<unsigned> > FilteredInstructions
;
139 // Set of uid's with non-constant segment values.
140 std::vector
<unsigned> VariableInstructions
;
142 // Map of well-known segment value to its delegate.
143 std::map
<unsigned, FilterChooser
*> FilterChooserMap
;
145 // Number of instructions which fall under FilteredInstructions category.
146 unsigned NumFiltered
;
148 // Keeps track of the last opcode in the filtered bucket.
149 unsigned LastOpcFiltered
;
151 // Number of instructions which fall under VariableInstructions category.
152 unsigned NumVariable
;
155 unsigned getNumFiltered() { return NumFiltered
; }
156 unsigned getNumVariable() { return NumVariable
; }
157 unsigned getSingletonOpc() {
158 assert(NumFiltered
== 1);
159 return LastOpcFiltered
;
161 // Return the filter chooser for the group of instructions without constant
163 FilterChooser
&getVariableFC() {
164 assert(NumFiltered
== 1);
165 assert(FilterChooserMap
.size() == 1);
166 return *(FilterChooserMap
.find((unsigned)-1)->second
);
169 Filter(const Filter
&f
);
170 Filter(FilterChooser
&owner
, unsigned startBit
, unsigned numBits
, bool mixed
);
174 // Divides the decoding task into sub tasks and delegates them to the
175 // inferior FilterChooser's.
177 // A special case arises when there's only one entry in the filtered
178 // instructions. In order to unambiguously decode the singleton, we need to
179 // match the remaining undecoded encoding bits against the singleton.
182 // Emit code to decode instructions given a segment or segments of bits.
183 void emit(raw_ostream
&o
, unsigned &Indentation
);
185 // Returns the number of fanout produced by the filter. More fanout implies
186 // the filter distinguishes more categories of instructions.
187 unsigned usefulness() const;
188 }; // End of class Filter
190 // These are states of our finite state machines used in FilterChooser's
191 // filterProcessor() which produces the filter candidates to use.
200 /// FilterChooser - FilterChooser chooses the best filter among a set of Filters
201 /// in order to perform the decoding of instructions at the current level.
203 /// Decoding proceeds from the top down. Based on the well-known encoding bits
204 /// of instructions available, FilterChooser builds up the possible Filters that
205 /// can further the task of decoding by distinguishing among the remaining
206 /// candidate instructions.
208 /// Once a filter has been chosen, it is called upon to divide the decoding task
209 /// into sub-tasks and delegates them to its inferior FilterChoosers for further
212 /// It is useful to think of a Filter as governing the switch stmts of the
213 /// decoding tree. And each case is delegated to an inferior FilterChooser to
214 /// decide what further remaining bits to look at.
215 class FilterChooser
{
219 // Vector of codegen instructions to choose our filter.
220 const std::vector
<const CodeGenInstruction
*> &AllInstructions
;
222 // Vector of uid's for this filter chooser to work on.
223 const std::vector
<unsigned> Opcodes
;
225 // Lookup table for the operand decoding of instructions.
226 std::map
<unsigned, std::vector
<OperandInfo
> > &Operands
;
228 // Vector of candidate filters.
229 std::vector
<Filter
> Filters
;
231 // Array of bit values passed down from our parent.
232 // Set to all BIT_UNFILTERED's for Parent == NULL.
233 bit_value_t FilterBitValues
[BIT_WIDTH
];
235 // Links to the FilterChooser above us in the decoding tree.
236 FilterChooser
*Parent
;
238 // Index of the best filter from Filters.
242 FilterChooser(const FilterChooser
&FC
) :
243 AllInstructions(FC
.AllInstructions
), Opcodes(FC
.Opcodes
),
244 Operands(FC
.Operands
), Filters(FC
.Filters
), Parent(FC
.Parent
),
245 BestIndex(FC
.BestIndex
) {
246 memcpy(FilterBitValues
, FC
.FilterBitValues
, sizeof(FilterBitValues
));
249 FilterChooser(const std::vector
<const CodeGenInstruction
*> &Insts
,
250 const std::vector
<unsigned> &IDs
,
251 std::map
<unsigned, std::vector
<OperandInfo
> > &Ops
) :
252 AllInstructions(Insts
), Opcodes(IDs
), Operands(Ops
), Filters(),
253 Parent(NULL
), BestIndex(-1) {
254 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
)
255 FilterBitValues
[i
] = BIT_UNFILTERED
;
260 FilterChooser(const std::vector
<const CodeGenInstruction
*> &Insts
,
261 const std::vector
<unsigned> &IDs
,
262 std::map
<unsigned, std::vector
<OperandInfo
> > &Ops
,
263 bit_value_t (&ParentFilterBitValues
)[BIT_WIDTH
],
264 FilterChooser
&parent
) :
265 AllInstructions(Insts
), Opcodes(IDs
), Operands(Ops
),
266 Filters(), Parent(&parent
), BestIndex(-1) {
267 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
)
268 FilterBitValues
[i
] = ParentFilterBitValues
[i
];
273 // The top level filter chooser has NULL as its parent.
274 bool isTopLevel() { return Parent
== NULL
; }
276 // Emit the top level typedef and decodeInstruction() function.
277 void emitTop(raw_ostream
&o
, unsigned Indentation
);
280 // Populates the insn given the uid.
281 void insnWithID(insn_t
&Insn
, unsigned Opcode
) const {
282 BitsInit
&Bits
= getBitsField(*AllInstructions
[Opcode
]->TheDef
, "Inst");
284 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
)
285 Insn
[i
] = bitFromBits(Bits
, i
);
288 // Returns the record name.
289 const std::string
&nameWithID(unsigned Opcode
) const {
290 return AllInstructions
[Opcode
]->TheDef
->getName();
293 // Populates the field of the insn given the start position and the number of
294 // consecutive bits to scan for.
296 // Returns false if there exists any uninitialized bit value in the range.
297 // Returns true, otherwise.
298 bool fieldFromInsn(uint64_t &Field
, insn_t
&Insn
, unsigned StartBit
,
299 unsigned NumBits
) const;
301 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
302 /// filter array as a series of chars.
303 void dumpFilterArray(raw_ostream
&o
, bit_value_t (&filter
)[BIT_WIDTH
]);
305 /// dumpStack - dumpStack traverses the filter chooser chain and calls
306 /// dumpFilterArray on each filter chooser up to the top level one.
307 void dumpStack(raw_ostream
&o
, const char *prefix
);
309 Filter
&bestFilter() {
310 assert(BestIndex
!= -1 && "BestIndex not set");
311 return Filters
[BestIndex
];
314 // Called from Filter::recurse() when singleton exists. For debug purpose.
315 void SingletonExists(unsigned Opc
);
317 bool PositionFiltered(unsigned i
) {
318 return ValueSet(FilterBitValues
[i
]);
321 // Calculates the island(s) needed to decode the instruction.
322 // This returns a lit of undecoded bits of an instructions, for example,
323 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
324 // decoded bits in order to verify that the instruction matches the Opcode.
325 unsigned getIslands(std::vector
<unsigned> &StartBits
,
326 std::vector
<unsigned> &EndBits
, std::vector
<uint64_t> &FieldVals
,
329 // Emits code to decode the singleton. Return true if we have matched all the
331 bool emitSingletonDecoder(raw_ostream
&o
, unsigned &Indentation
,unsigned Opc
);
333 // Emits code to decode the singleton, and then to decode the rest.
334 void emitSingletonDecoder(raw_ostream
&o
, unsigned &Indentation
,Filter
&Best
);
336 // Assign a single filter and run with it.
337 void runSingleFilter(FilterChooser
&owner
, unsigned startBit
, unsigned numBit
,
340 // reportRegion is a helper function for filterProcessor to mark a region as
341 // eligible for use as a filter region.
342 void reportRegion(bitAttr_t RA
, unsigned StartBit
, unsigned BitIndex
,
345 // FilterProcessor scans the well-known encoding bits of the instructions and
346 // builds up a list of candidate filters. It chooses the best filter and
347 // recursively descends down the decoding tree.
348 bool filterProcessor(bool AllowMixed
, bool Greedy
= true);
350 // Decides on the best configuration of filter(s) to use in order to decode
351 // the instructions. A conflict of instructions may occur, in which case we
352 // dump the conflict set to the standard error.
355 // Emits code to decode our share of instructions. Returns true if the
356 // emitted code causes a return, which occurs if we know how to decode
357 // the instruction at this level or the instruction is not decodeable.
358 bool emit(raw_ostream
&o
, unsigned &Indentation
);
361 ///////////////////////////
363 // Filter Implmenetation //
365 ///////////////////////////
367 Filter::Filter(const Filter
&f
) :
368 Owner(f
.Owner
), StartBit(f
.StartBit
), NumBits(f
.NumBits
), Mixed(f
.Mixed
),
369 FilteredInstructions(f
.FilteredInstructions
),
370 VariableInstructions(f
.VariableInstructions
),
371 FilterChooserMap(f
.FilterChooserMap
), NumFiltered(f
.NumFiltered
),
372 LastOpcFiltered(f
.LastOpcFiltered
), NumVariable(f
.NumVariable
) {
375 Filter::Filter(FilterChooser
&owner
, unsigned startBit
, unsigned numBits
,
376 bool mixed
) : Owner(&owner
), StartBit(startBit
), NumBits(numBits
),
378 assert(StartBit
+ NumBits
- 1 < BIT_WIDTH
);
384 for (unsigned i
= 0, e
= Owner
->Opcodes
.size(); i
!= e
; ++i
) {
387 // Populates the insn given the uid.
388 Owner
->insnWithID(Insn
, Owner
->Opcodes
[i
]);
391 // Scans the segment for possibly well-specified encoding bits.
392 bool ok
= Owner
->fieldFromInsn(Field
, Insn
, StartBit
, NumBits
);
395 // The encoding bits are well-known. Lets add the uid of the
396 // instruction into the bucket keyed off the constant field value.
397 LastOpcFiltered
= Owner
->Opcodes
[i
];
398 FilteredInstructions
[Field
].push_back(LastOpcFiltered
);
401 // Some of the encoding bit(s) are unspecfied. This contributes to
402 // one additional member of "Variable" instructions.
403 VariableInstructions
.push_back(Owner
->Opcodes
[i
]);
408 assert((FilteredInstructions
.size() + VariableInstructions
.size() > 0)
409 && "Filter returns no instruction categories");
413 std::map
<unsigned, FilterChooser
*>::iterator filterIterator
;
414 for (filterIterator
= FilterChooserMap
.begin();
415 filterIterator
!= FilterChooserMap
.end();
417 delete filterIterator
->second
;
421 // Divides the decoding task into sub tasks and delegates them to the
422 // inferior FilterChooser's.
424 // A special case arises when there's only one entry in the filtered
425 // instructions. In order to unambiguously decode the singleton, we need to
426 // match the remaining undecoded encoding bits against the singleton.
427 void Filter::recurse() {
428 std::map
<uint64_t, std::vector
<unsigned> >::const_iterator mapIterator
;
430 bit_value_t BitValueArray
[BIT_WIDTH
];
431 // Starts by inheriting our parent filter chooser's filter bit values.
432 memcpy(BitValueArray
, Owner
->FilterBitValues
, sizeof(BitValueArray
));
436 if (VariableInstructions
.size()) {
437 // Conservatively marks each segment position as BIT_UNSET.
438 for (bitIndex
= 0; bitIndex
< NumBits
; bitIndex
++)
439 BitValueArray
[StartBit
+ bitIndex
] = BIT_UNSET
;
441 // Delegates to an inferior filter chooser for further processing on this
442 // group of instructions whose segment values are variable.
443 FilterChooserMap
.insert(std::pair
<unsigned, FilterChooser
*>(
445 new FilterChooser(Owner
->AllInstructions
,
446 VariableInstructions
,
453 // No need to recurse for a singleton filtered instruction.
454 // See also Filter::emit().
455 if (getNumFiltered() == 1) {
456 //Owner->SingletonExists(LastOpcFiltered);
457 assert(FilterChooserMap
.size() == 1);
461 // Otherwise, create sub choosers.
462 for (mapIterator
= FilteredInstructions
.begin();
463 mapIterator
!= FilteredInstructions
.end();
466 // Marks all the segment positions with either BIT_TRUE or BIT_FALSE.
467 for (bitIndex
= 0; bitIndex
< NumBits
; bitIndex
++) {
468 if (mapIterator
->first
& (1ULL << bitIndex
))
469 BitValueArray
[StartBit
+ bitIndex
] = BIT_TRUE
;
471 BitValueArray
[StartBit
+ bitIndex
] = BIT_FALSE
;
474 // Delegates to an inferior filter chooser for further processing on this
475 // category of instructions.
476 FilterChooserMap
.insert(std::pair
<unsigned, FilterChooser
*>(
478 new FilterChooser(Owner
->AllInstructions
,
487 // Emit code to decode instructions given a segment or segments of bits.
488 void Filter::emit(raw_ostream
&o
, unsigned &Indentation
) {
489 o
.indent(Indentation
) << "// Check Inst{";
492 o
<< (StartBit
+ NumBits
- 1) << '-';
494 o
<< StartBit
<< "} ...\n";
496 o
.indent(Indentation
) << "switch (fieldFromInstruction(insn, "
497 << StartBit
<< ", " << NumBits
<< ")) {\n";
499 std::map
<unsigned, FilterChooser
*>::iterator filterIterator
;
501 bool DefaultCase
= false;
502 for (filterIterator
= FilterChooserMap
.begin();
503 filterIterator
!= FilterChooserMap
.end();
506 // Field value -1 implies a non-empty set of variable instructions.
507 // See also recurse().
508 if (filterIterator
->first
== (unsigned)-1) {
511 o
.indent(Indentation
) << "default:\n";
512 o
.indent(Indentation
) << " break; // fallthrough\n";
514 // Closing curly brace for the switch statement.
515 // This is unconventional because we want the default processing to be
516 // performed for the fallthrough cases as well, i.e., when the "cases"
517 // did not prove a decoded instruction.
518 o
.indent(Indentation
) << "}\n";
521 o
.indent(Indentation
) << "case " << filterIterator
->first
<< ":\n";
523 // We arrive at a category of instructions with the same segment value.
524 // Now delegate to the sub filter chooser for further decodings.
525 // The case may fallthrough, which happens if the remaining well-known
526 // encoding bits do not match exactly.
527 if (!DefaultCase
) { ++Indentation
; ++Indentation
; }
529 bool finished
= filterIterator
->second
->emit(o
, Indentation
);
530 // For top level default case, there's no need for a break statement.
531 if (Owner
->isTopLevel() && DefaultCase
)
534 o
.indent(Indentation
) << "break;\n";
536 if (!DefaultCase
) { --Indentation
; --Indentation
; }
539 // If there is no default case, we still need to supply a closing brace.
541 // Closing curly brace for the switch statement.
542 o
.indent(Indentation
) << "}\n";
546 // Returns the number of fanout produced by the filter. More fanout implies
547 // the filter distinguishes more categories of instructions.
548 unsigned Filter::usefulness() const {
549 if (VariableInstructions
.size())
550 return FilteredInstructions
.size();
552 return FilteredInstructions
.size() + 1;
555 //////////////////////////////////
557 // Filterchooser Implementation //
559 //////////////////////////////////
561 // Emit the top level typedef and decodeInstruction() function.
562 void FilterChooser::emitTop(raw_ostream
&o
, unsigned Indentation
) {
565 o
.indent(Indentation
) << "typedef uint8_t field_t;\n";
568 o
.indent(Indentation
) << "typedef uint16_t field_t;\n";
571 o
.indent(Indentation
) << "typedef uint32_t field_t;\n";
574 o
.indent(Indentation
) << "typedef uint64_t field_t;\n";
577 assert(0 && "Unexpected instruction size!");
582 o
.indent(Indentation
) << "static field_t " <<
583 "fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n";
585 o
.indent(Indentation
) << "{\n";
587 ++Indentation
; ++Indentation
;
588 o
.indent(Indentation
) << "assert(startBit + numBits <= " << BIT_WIDTH
589 << " && \"Instruction field out of bounds!\");\n";
591 o
.indent(Indentation
) << "field_t fieldMask;\n";
593 o
.indent(Indentation
) << "if (numBits == " << BIT_WIDTH
<< ")\n";
595 ++Indentation
; ++Indentation
;
596 o
.indent(Indentation
) << "fieldMask = (field_t)-1;\n";
597 --Indentation
; --Indentation
;
599 o
.indent(Indentation
) << "else\n";
601 ++Indentation
; ++Indentation
;
602 o
.indent(Indentation
) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
603 --Indentation
; --Indentation
;
606 o
.indent(Indentation
) << "return (insn & fieldMask) >> startBit;\n";
607 --Indentation
; --Indentation
;
609 o
.indent(Indentation
) << "}\n";
613 o
.indent(Indentation
) <<
614 "static bool decodeInstruction(MCInst &MI, field_t insn, "
615 "uint64_t Address, const void *Decoder) {\n";
616 o
.indent(Indentation
) << " unsigned tmp = 0;\n";
618 ++Indentation
; ++Indentation
;
619 // Emits code to decode the instructions.
620 emit(o
, Indentation
);
623 o
.indent(Indentation
) << "return false;\n";
624 --Indentation
; --Indentation
;
626 o
.indent(Indentation
) << "}\n";
631 // Populates the field of the insn given the start position and the number of
632 // consecutive bits to scan for.
634 // Returns false if and on the first uninitialized bit value encountered.
635 // Returns true, otherwise.
636 bool FilterChooser::fieldFromInsn(uint64_t &Field
, insn_t
&Insn
,
637 unsigned StartBit
, unsigned NumBits
) const {
640 for (unsigned i
= 0; i
< NumBits
; ++i
) {
641 if (Insn
[StartBit
+ i
] == BIT_UNSET
)
644 if (Insn
[StartBit
+ i
] == BIT_TRUE
)
645 Field
= Field
| (1ULL << i
);
651 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
652 /// filter array as a series of chars.
653 void FilterChooser::dumpFilterArray(raw_ostream
&o
,
654 bit_value_t (&filter
)[BIT_WIDTH
]) {
657 for (bitIndex
= BIT_WIDTH
; bitIndex
> 0; bitIndex
--) {
658 switch (filter
[bitIndex
- 1]) {
675 /// dumpStack - dumpStack traverses the filter chooser chain and calls
676 /// dumpFilterArray on each filter chooser up to the top level one.
677 void FilterChooser::dumpStack(raw_ostream
&o
, const char *prefix
) {
678 FilterChooser
*current
= this;
682 dumpFilterArray(o
, current
->FilterBitValues
);
684 current
= current
->Parent
;
688 // Called from Filter::recurse() when singleton exists. For debug purpose.
689 void FilterChooser::SingletonExists(unsigned Opc
) {
691 insnWithID(Insn0
, Opc
);
693 errs() << "Singleton exists: " << nameWithID(Opc
)
694 << " with its decoding dominating ";
695 for (unsigned i
= 0; i
< Opcodes
.size(); ++i
) {
696 if (Opcodes
[i
] == Opc
) continue;
697 errs() << nameWithID(Opcodes
[i
]) << ' ';
701 dumpStack(errs(), "\t\t");
702 for (unsigned i
= 0; i
< Opcodes
.size(); i
++) {
703 const std::string
&Name
= nameWithID(Opcodes
[i
]);
705 errs() << '\t' << Name
<< " ";
707 getBitsField(*AllInstructions
[Opcodes
[i
]]->TheDef
, "Inst"));
712 // Calculates the island(s) needed to decode the instruction.
713 // This returns a list of undecoded bits of an instructions, for example,
714 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
715 // decoded bits in order to verify that the instruction matches the Opcode.
716 unsigned FilterChooser::getIslands(std::vector
<unsigned> &StartBits
,
717 std::vector
<unsigned> &EndBits
, std::vector
<uint64_t> &FieldVals
,
722 uint64_t FieldVal
= 0;
725 // 1: Water (the bit value does not affect decoding)
726 // 2: Island (well-known bit value needed for decoding)
730 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
) {
731 Val
= Value(Insn
[i
]);
732 bool Filtered
= PositionFiltered(i
);
735 assert(0 && "Unreachable code!");
739 if (Filtered
|| Val
== -1)
740 State
= 1; // Still in Water
742 State
= 2; // Into the Island
744 StartBits
.push_back(i
);
749 if (Filtered
|| Val
== -1) {
750 State
= 1; // Into the Water
751 EndBits
.push_back(i
- 1);
752 FieldVals
.push_back(FieldVal
);
755 State
= 2; // Still in Island
757 FieldVal
= FieldVal
| Val
<< BitNo
;
762 // If we are still in Island after the loop, do some housekeeping.
764 EndBits
.push_back(BIT_WIDTH
- 1);
765 FieldVals
.push_back(FieldVal
);
769 assert(StartBits
.size() == Num
&& EndBits
.size() == Num
&&
770 FieldVals
.size() == Num
);
774 // Emits code to decode the singleton. Return true if we have matched all the
776 bool FilterChooser::emitSingletonDecoder(raw_ostream
&o
, unsigned &Indentation
,
778 std::vector
<unsigned> StartBits
;
779 std::vector
<unsigned> EndBits
;
780 std::vector
<uint64_t> FieldVals
;
782 insnWithID(Insn
, Opc
);
784 // Look for islands of undecoded bits of the singleton.
785 getIslands(StartBits
, EndBits
, FieldVals
, Insn
);
787 unsigned Size
= StartBits
.size();
790 // If we have matched all the well-known bits, just issue a return.
792 o
.indent(Indentation
) << "{\n";
793 o
.indent(Indentation
) << " MI.setOpcode(" << Opc
<< ");\n";
794 std::vector
<OperandInfo
>& InsnOperands
= Operands
[Opc
];
795 for (std::vector
<OperandInfo
>::iterator
796 I
= InsnOperands
.begin(), E
= InsnOperands
.end(); I
!= E
; ++I
) {
797 // If a custom instruction decoder was specified, use that.
798 if (I
->FieldBase
== ~0U && I
->FieldLength
== ~0U) {
799 o
.indent(Indentation
) << " " << I
->Decoder
800 << "(MI, insn, Address, Decoder);\n";
804 o
.indent(Indentation
)
805 << " tmp = fieldFromInstruction(insn, " << I
->FieldBase
806 << ", " << I
->FieldLength
<< ");\n";
807 if (I
->Decoder
!= "") {
808 o
.indent(Indentation
) << " " << I
->Decoder
809 << "(MI, tmp, Address, Decoder);\n";
811 o
.indent(Indentation
)
812 << " MI.addOperand(MCOperand::CreateImm(tmp));\n";
816 o
.indent(Indentation
) << " return true; // " << nameWithID(Opc
)
818 o
.indent(Indentation
) << "}\n";
822 // Otherwise, there are more decodings to be done!
824 // Emit code to match the island(s) for the singleton.
825 o
.indent(Indentation
) << "// Check ";
827 for (I
= Size
; I
!= 0; --I
) {
828 o
<< "Inst{" << EndBits
[I
-1] << '-' << StartBits
[I
-1] << "} ";
832 o
<< "for singleton decoding...\n";
835 o
.indent(Indentation
) << "if (";
837 for (I
= Size
; I
!= 0; --I
) {
838 NumBits
= EndBits
[I
-1] - StartBits
[I
-1] + 1;
839 o
<< "fieldFromInstruction(insn, " << StartBits
[I
-1] << ", " << NumBits
840 << ") == " << FieldVals
[I
-1];
846 o
.indent(Indentation
) << " MI.setOpcode(" << Opc
<< ");\n";
847 std::vector
<OperandInfo
>& InsnOperands
= Operands
[Opc
];
848 for (std::vector
<OperandInfo
>::iterator
849 I
= InsnOperands
.begin(), E
= InsnOperands
.end(); I
!= E
; ++I
) {
850 // If a custom instruction decoder was specified, use that.
851 if (I
->FieldBase
== ~0U && I
->FieldLength
== ~0U) {
852 o
.indent(Indentation
) << " " << I
->Decoder
853 << "(MI, insn, Address, Decoder);\n";
857 o
.indent(Indentation
)
858 << " tmp = fieldFromInstruction(insn, " << I
->FieldBase
859 << ", " << I
->FieldLength
<< ");\n";
860 if (I
->Decoder
!= "") {
861 o
.indent(Indentation
) << " " << I
->Decoder
862 << "(MI, tmp, Address, Decoder);\n";
864 o
.indent(Indentation
)
865 << " MI.addOperand(MCOperand::CreateImm(tmp));\n";
868 o
.indent(Indentation
) << " return true; // " << nameWithID(Opc
)
870 o
.indent(Indentation
) << "}\n";
875 // Emits code to decode the singleton, and then to decode the rest.
876 void FilterChooser::emitSingletonDecoder(raw_ostream
&o
, unsigned &Indentation
,
879 unsigned Opc
= Best
.getSingletonOpc();
881 emitSingletonDecoder(o
, Indentation
, Opc
);
883 // Emit code for the rest.
884 o
.indent(Indentation
) << "else\n";
887 Best
.getVariableFC().emit(o
, Indentation
);
891 // Assign a single filter and run with it. Top level API client can initialize
892 // with a single filter to start the filtering process.
893 void FilterChooser::runSingleFilter(FilterChooser
&owner
, unsigned startBit
,
894 unsigned numBit
, bool mixed
) {
896 Filter
F(*this, startBit
, numBit
, true);
897 Filters
.push_back(F
);
898 BestIndex
= 0; // Sole Filter instance to choose from.
899 bestFilter().recurse();
902 // reportRegion is a helper function for filterProcessor to mark a region as
903 // eligible for use as a filter region.
904 void FilterChooser::reportRegion(bitAttr_t RA
, unsigned StartBit
,
905 unsigned BitIndex
, bool AllowMixed
) {
906 if (RA
== ATTR_MIXED
&& AllowMixed
)
907 Filters
.push_back(Filter(*this, StartBit
, BitIndex
- StartBit
, true));
908 else if (RA
== ATTR_ALL_SET
&& !AllowMixed
)
909 Filters
.push_back(Filter(*this, StartBit
, BitIndex
- StartBit
, false));
912 // FilterProcessor scans the well-known encoding bits of the instructions and
913 // builds up a list of candidate filters. It chooses the best filter and
914 // recursively descends down the decoding tree.
915 bool FilterChooser::filterProcessor(bool AllowMixed
, bool Greedy
) {
918 unsigned numInstructions
= Opcodes
.size();
920 assert(numInstructions
&& "Filter created with no instructions");
922 // No further filtering is necessary.
923 if (numInstructions
== 1)
926 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
927 // instructions is 3.
928 if (AllowMixed
&& !Greedy
) {
929 assert(numInstructions
== 3);
931 for (unsigned i
= 0; i
< Opcodes
.size(); ++i
) {
932 std::vector
<unsigned> StartBits
;
933 std::vector
<unsigned> EndBits
;
934 std::vector
<uint64_t> FieldVals
;
937 insnWithID(Insn
, Opcodes
[i
]);
939 // Look for islands of undecoded bits of any instruction.
940 if (getIslands(StartBits
, EndBits
, FieldVals
, Insn
) > 0) {
941 // Found an instruction with island(s). Now just assign a filter.
942 runSingleFilter(*this, StartBits
[0], EndBits
[0] - StartBits
[0] + 1,
949 unsigned BitIndex
, InsnIndex
;
951 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
952 // The automaton consumes the corresponding bit from each
955 // Input symbols: 0, 1, and _ (unset).
956 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
957 // Initial state: NONE.
959 // (NONE) ------- [01] -> (ALL_SET)
960 // (NONE) ------- _ ----> (ALL_UNSET)
961 // (ALL_SET) ---- [01] -> (ALL_SET)
962 // (ALL_SET) ---- _ ----> (MIXED)
963 // (ALL_UNSET) -- [01] -> (MIXED)
964 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
965 // (MIXED) ------ . ----> (MIXED)
966 // (FILTERED)---- . ----> (FILTERED)
968 bitAttr_t bitAttrs
[BIT_WIDTH
];
970 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
971 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
972 for (BitIndex
= 0; BitIndex
< BIT_WIDTH
; ++BitIndex
)
973 if (FilterBitValues
[BitIndex
] == BIT_TRUE
||
974 FilterBitValues
[BitIndex
] == BIT_FALSE
)
975 bitAttrs
[BitIndex
] = ATTR_FILTERED
;
977 bitAttrs
[BitIndex
] = ATTR_NONE
;
979 for (InsnIndex
= 0; InsnIndex
< numInstructions
; ++InsnIndex
) {
982 insnWithID(insn
, Opcodes
[InsnIndex
]);
984 for (BitIndex
= 0; BitIndex
< BIT_WIDTH
; ++BitIndex
) {
985 switch (bitAttrs
[BitIndex
]) {
987 if (insn
[BitIndex
] == BIT_UNSET
)
988 bitAttrs
[BitIndex
] = ATTR_ALL_UNSET
;
990 bitAttrs
[BitIndex
] = ATTR_ALL_SET
;
993 if (insn
[BitIndex
] == BIT_UNSET
)
994 bitAttrs
[BitIndex
] = ATTR_MIXED
;
997 if (insn
[BitIndex
] != BIT_UNSET
)
998 bitAttrs
[BitIndex
] = ATTR_MIXED
;
1007 // The regionAttr automaton consumes the bitAttrs automatons' state,
1008 // lowest-to-highest.
1010 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1011 // States: NONE, ALL_SET, MIXED
1012 // Initial state: NONE
1014 // (NONE) ----- F --> (NONE)
1015 // (NONE) ----- S --> (ALL_SET) ; and set region start
1016 // (NONE) ----- U --> (NONE)
1017 // (NONE) ----- M --> (MIXED) ; and set region start
1018 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1019 // (ALL_SET) -- S --> (ALL_SET)
1020 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1021 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1022 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1023 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1024 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1025 // (MIXED) ---- M --> (MIXED)
1027 bitAttr_t RA
= ATTR_NONE
;
1028 unsigned StartBit
= 0;
1030 for (BitIndex
= 0; BitIndex
< BIT_WIDTH
; BitIndex
++) {
1031 bitAttr_t bitAttr
= bitAttrs
[BitIndex
];
1033 assert(bitAttr
!= ATTR_NONE
&& "Bit without attributes");
1041 StartBit
= BitIndex
;
1044 case ATTR_ALL_UNSET
:
1047 StartBit
= BitIndex
;
1051 assert(0 && "Unexpected bitAttr!");
1057 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1062 case ATTR_ALL_UNSET
:
1063 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1067 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1068 StartBit
= BitIndex
;
1072 assert(0 && "Unexpected bitAttr!");
1078 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1079 StartBit
= BitIndex
;
1083 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1084 StartBit
= BitIndex
;
1087 case ATTR_ALL_UNSET
:
1088 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1094 assert(0 && "Unexpected bitAttr!");
1097 case ATTR_ALL_UNSET
:
1098 assert(0 && "regionAttr state machine has no ATTR_UNSET state");
1100 assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
1104 // At the end, if we're still in ALL_SET or MIXED states, report a region
1111 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1113 case ATTR_ALL_UNSET
:
1116 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1120 // We have finished with the filter processings. Now it's time to choose
1121 // the best performing filter.
1123 bool AllUseless
= true;
1124 unsigned BestScore
= 0;
1126 for (unsigned i
= 0, e
= Filters
.size(); i
!= e
; ++i
) {
1127 unsigned Usefulness
= Filters
[i
].usefulness();
1132 if (Usefulness
> BestScore
) {
1134 BestScore
= Usefulness
;
1139 bestFilter().recurse();
1142 } // end of FilterChooser::filterProcessor(bool)
1144 // Decides on the best configuration of filter(s) to use in order to decode
1145 // the instructions. A conflict of instructions may occur, in which case we
1146 // dump the conflict set to the standard error.
1147 void FilterChooser::doFilter() {
1148 unsigned Num
= Opcodes
.size();
1149 assert(Num
&& "FilterChooser created with no instructions");
1151 // Try regions of consecutive known bit values first.
1152 if (filterProcessor(false))
1155 // Then regions of mixed bits (both known and unitialized bit values allowed).
1156 if (filterProcessor(true))
1159 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1160 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1161 // well-known encoding pattern. In such case, we backtrack and scan for the
1162 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1163 if (Num
== 3 && filterProcessor(true, false))
1166 // If we come to here, the instruction decoding has failed.
1167 // Set the BestIndex to -1 to indicate so.
1171 // Emits code to decode our share of instructions. Returns true if the
1172 // emitted code causes a return, which occurs if we know how to decode
1173 // the instruction at this level or the instruction is not decodeable.
1174 bool FilterChooser::emit(raw_ostream
&o
, unsigned &Indentation
) {
1175 if (Opcodes
.size() == 1)
1176 // There is only one instruction in the set, which is great!
1177 // Call emitSingletonDecoder() to see whether there are any remaining
1179 return emitSingletonDecoder(o
, Indentation
, Opcodes
[0]);
1181 // Choose the best filter to do the decodings!
1182 if (BestIndex
!= -1) {
1183 Filter
&Best
= bestFilter();
1184 if (Best
.getNumFiltered() == 1)
1185 emitSingletonDecoder(o
, Indentation
, Best
);
1187 bestFilter().emit(o
, Indentation
);
1191 // We don't know how to decode these instructions! Return 0 and dump the
1193 o
.indent(Indentation
) << "return 0;" << " // Conflict set: ";
1194 for (int i
= 0, N
= Opcodes
.size(); i
< N
; ++i
) {
1195 o
<< nameWithID(Opcodes
[i
]);
1202 // Print out useful conflict information for postmortem analysis.
1203 errs() << "Decoding Conflict:\n";
1205 dumpStack(errs(), "\t\t");
1207 for (unsigned i
= 0; i
< Opcodes
.size(); i
++) {
1208 const std::string
&Name
= nameWithID(Opcodes
[i
]);
1210 errs() << '\t' << Name
<< " ";
1212 getBitsField(*AllInstructions
[Opcodes
[i
]]->TheDef
, "Inst"));
1219 bool FixedLenDecoderEmitter::populateInstruction(const CodeGenInstruction
&CGI
,
1221 const Record
&Def
= *CGI
.TheDef
;
1222 // If all the bit positions are not specified; do not decode this instruction.
1223 // We are bound to fail! For proper disassembly, the well-known encoding bits
1224 // of the instruction must be fully specified.
1226 // This also removes pseudo instructions from considerations of disassembly,
1227 // which is a better design and less fragile than the name matchings.
1228 // Ignore "asm parser only" instructions.
1229 if (Def
.getValueAsBit("isAsmParserOnly") ||
1230 Def
.getValueAsBit("isCodeGenOnly"))
1233 BitsInit
&Bits
= getBitsField(Def
, "Inst");
1234 if (Bits
.allInComplete()) return false;
1236 std::vector
<OperandInfo
> InsnOperands
;
1238 // If the instruction has specified a custom decoding hook, use that instead
1239 // of trying to auto-generate the decoder.
1240 std::string InstDecoder
= Def
.getValueAsString("DecoderMethod");
1241 if (InstDecoder
!= "") {
1242 InsnOperands
.push_back(OperandInfo(~0U, ~0U, InstDecoder
));
1243 Operands
[Opc
] = InsnOperands
;
1247 // Generate a description of the operand of the instruction that we know
1248 // how to decode automatically.
1249 // FIXME: We'll need to have a way to manually override this as needed.
1251 // Gather the outputs/inputs of the instruction, so we can find their
1252 // positions in the encoding. This assumes for now that they appear in the
1253 // MCInst in the order that they're listed.
1254 std::vector
<std::pair
<Init
*, std::string
> > InOutOperands
;
1255 DagInit
*Out
= Def
.getValueAsDag("OutOperandList");
1256 DagInit
*In
= Def
.getValueAsDag("InOperandList");
1257 for (unsigned i
= 0; i
< Out
->getNumArgs(); ++i
)
1258 InOutOperands
.push_back(std::make_pair(Out
->getArg(i
), Out
->getArgName(i
)));
1259 for (unsigned i
= 0; i
< In
->getNumArgs(); ++i
)
1260 InOutOperands
.push_back(std::make_pair(In
->getArg(i
), In
->getArgName(i
)));
1262 // For each operand, see if we can figure out where it is encoded.
1263 for (std::vector
<std::pair
<Init
*, std::string
> >::iterator
1264 NI
= InOutOperands
.begin(), NE
= InOutOperands
.end(); NI
!= NE
; ++NI
) {
1265 unsigned PrevBit
= ~0;
1267 unsigned PrevPos
= ~0;
1268 std::string Decoder
= "";
1270 for (unsigned bi
= 0; bi
< Bits
.getNumBits(); ++bi
) {
1271 VarBitInit
*BI
= dynamic_cast<VarBitInit
*>(Bits
.getBit(bi
));
1274 VarInit
*Var
= dynamic_cast<VarInit
*>(BI
->getVariable());
1276 unsigned CurrBit
= BI
->getBitNum();
1277 if (Var
->getName() != NI
->second
) continue;
1279 // Figure out the lowest bit of the value, and the width of the field.
1280 // Deliberately don't try to handle cases where the field is scattered,
1281 // or where not all bits of the the field are explicit.
1282 if (Base
== ~0U && PrevBit
== ~0U && PrevPos
== ~0U) {
1289 if ((PrevPos
!= ~0U && bi
-1 != PrevPos
) ||
1290 (CurrBit
!= ~0U && CurrBit
-1 != PrevBit
)) {
1299 // At this point, we can locate the field, but we need to know how to
1300 // interpret it. As a first step, require the target to provide callbacks
1301 // for decoding register classes.
1302 // FIXME: This need to be extended to handle instructions with custom
1303 // decoder methods, and operands with (simple) MIOperandInfo's.
1304 TypedInit
*TI
= dynamic_cast<TypedInit
*>(NI
->first
);
1305 RecordRecTy
*Type
= dynamic_cast<RecordRecTy
*>(TI
->getType());
1306 Record
*TypeRecord
= Type
->getRecord();
1308 if (TypeRecord
->isSubClassOf("RegisterOperand"))
1309 TypeRecord
= TypeRecord
->getValueAsDef("RegClass");
1310 if (TypeRecord
->isSubClassOf("RegisterClass")) {
1311 Decoder
= "Decode" + TypeRecord
->getName() + "RegisterClass";
1315 RecordVal
*DecoderString
= TypeRecord
->getValue("DecoderMethod");
1316 StringInit
*String
= DecoderString
?
1317 dynamic_cast<StringInit
*>(DecoderString
->getValue()) :
1319 if (!isReg
&& String
&& String
->getValue() != "")
1320 Decoder
= String
->getValue();
1324 InsnOperands
.push_back(OperandInfo(Base
, PrevBit
+1, Decoder
));
1325 DEBUG(errs() << "ENCODED OPERAND: $" << NI
->second
<< " @ ("
1326 << utostr(Base
+PrevBit
) << ", " << utostr(Base
) << ")\n");
1330 Operands
[Opc
] = InsnOperands
;
1335 // Dumps the instruction encoding bits.
1336 dumpBits(errs(), Bits
);
1340 // Dumps the list of operand info.
1341 for (unsigned i
= 0, e
= CGI
.Operands
.size(); i
!= e
; ++i
) {
1342 const CGIOperandList::OperandInfo
&Info
= CGI
.Operands
[i
];
1343 const std::string
&OperandName
= Info
.Name
;
1344 const Record
&OperandDef
= *Info
.Rec
;
1346 errs() << "\t" << OperandName
<< " (" << OperandDef
.getName() << ")\n";
1354 void FixedLenDecoderEmitter::populateInstructions() {
1355 for (unsigned i
= 0, e
= NumberedInstructions
.size(); i
< e
; ++i
) {
1356 Record
*R
= NumberedInstructions
[i
]->TheDef
;
1357 if (R
->getValueAsString("Namespace") == "TargetOpcode" ||
1358 R
->getValueAsBit("isPseudo"))
1361 if (populateInstruction(*NumberedInstructions
[i
], i
))
1362 Opcodes
.push_back(i
);
1366 // Emits disassembler code for instruction decoding.
1367 void FixedLenDecoderEmitter::run(raw_ostream
&o
)
1369 o
<< "#include \"llvm/MC/MCInst.h\"\n";
1370 o
<< "#include \"llvm/Support/DataTypes.h\"\n";
1371 o
<< "#include <assert.h>\n";
1373 o
<< "namespace llvm {\n\n";
1375 NumberedInstructions
= Target
.getInstructionsByEnumValue();
1376 populateInstructions();
1377 FilterChooser
FC(NumberedInstructions
, Opcodes
, Operands
);
1380 o
<< "\n} // End llvm namespace \n";