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 futher 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 futher 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) {\n";
615 o
.indent(Indentation
) << " unsigned tmp = 0;\n";
617 ++Indentation
; ++Indentation
;
618 // Emits code to decode the instructions.
619 emit(o
, Indentation
);
622 o
.indent(Indentation
) << "return false;\n";
623 --Indentation
; --Indentation
;
625 o
.indent(Indentation
) << "}\n";
630 // Populates the field of the insn given the start position and the number of
631 // consecutive bits to scan for.
633 // Returns false if and on the first uninitialized bit value encountered.
634 // Returns true, otherwise.
635 bool FilterChooser::fieldFromInsn(uint64_t &Field
, insn_t
&Insn
,
636 unsigned StartBit
, unsigned NumBits
) const {
639 for (unsigned i
= 0; i
< NumBits
; ++i
) {
640 if (Insn
[StartBit
+ i
] == BIT_UNSET
)
643 if (Insn
[StartBit
+ i
] == BIT_TRUE
)
644 Field
= Field
| (1ULL << i
);
650 /// dumpFilterArray - dumpFilterArray prints out debugging info for the given
651 /// filter array as a series of chars.
652 void FilterChooser::dumpFilterArray(raw_ostream
&o
,
653 bit_value_t (&filter
)[BIT_WIDTH
]) {
656 for (bitIndex
= BIT_WIDTH
; bitIndex
> 0; bitIndex
--) {
657 switch (filter
[bitIndex
- 1]) {
674 /// dumpStack - dumpStack traverses the filter chooser chain and calls
675 /// dumpFilterArray on each filter chooser up to the top level one.
676 void FilterChooser::dumpStack(raw_ostream
&o
, const char *prefix
) {
677 FilterChooser
*current
= this;
681 dumpFilterArray(o
, current
->FilterBitValues
);
683 current
= current
->Parent
;
687 // Called from Filter::recurse() when singleton exists. For debug purpose.
688 void FilterChooser::SingletonExists(unsigned Opc
) {
690 insnWithID(Insn0
, Opc
);
692 errs() << "Singleton exists: " << nameWithID(Opc
)
693 << " with its decoding dominating ";
694 for (unsigned i
= 0; i
< Opcodes
.size(); ++i
) {
695 if (Opcodes
[i
] == Opc
) continue;
696 errs() << nameWithID(Opcodes
[i
]) << ' ';
700 dumpStack(errs(), "\t\t");
701 for (unsigned i
= 0; i
< Opcodes
.size(); i
++) {
702 const std::string
&Name
= nameWithID(Opcodes
[i
]);
704 errs() << '\t' << Name
<< " ";
706 getBitsField(*AllInstructions
[Opcodes
[i
]]->TheDef
, "Inst"));
711 // Calculates the island(s) needed to decode the instruction.
712 // This returns a list of undecoded bits of an instructions, for example,
713 // Inst{20} = 1 && Inst{3-0} == 0b1111 represents two islands of yet-to-be
714 // decoded bits in order to verify that the instruction matches the Opcode.
715 unsigned FilterChooser::getIslands(std::vector
<unsigned> &StartBits
,
716 std::vector
<unsigned> &EndBits
, std::vector
<uint64_t> &FieldVals
,
721 uint64_t FieldVal
= 0;
724 // 1: Water (the bit value does not affect decoding)
725 // 2: Island (well-known bit value needed for decoding)
729 for (unsigned i
= 0; i
< BIT_WIDTH
; ++i
) {
730 Val
= Value(Insn
[i
]);
731 bool Filtered
= PositionFiltered(i
);
734 assert(0 && "Unreachable code!");
738 if (Filtered
|| Val
== -1)
739 State
= 1; // Still in Water
741 State
= 2; // Into the Island
743 StartBits
.push_back(i
);
748 if (Filtered
|| Val
== -1) {
749 State
= 1; // Into the Water
750 EndBits
.push_back(i
- 1);
751 FieldVals
.push_back(FieldVal
);
754 State
= 2; // Still in Island
756 FieldVal
= FieldVal
| Val
<< BitNo
;
761 // If we are still in Island after the loop, do some housekeeping.
763 EndBits
.push_back(BIT_WIDTH
- 1);
764 FieldVals
.push_back(FieldVal
);
768 assert(StartBits
.size() == Num
&& EndBits
.size() == Num
&&
769 FieldVals
.size() == Num
);
773 // Emits code to decode the singleton. Return true if we have matched all the
775 bool FilterChooser::emitSingletonDecoder(raw_ostream
&o
, unsigned &Indentation
,
777 std::vector
<unsigned> StartBits
;
778 std::vector
<unsigned> EndBits
;
779 std::vector
<uint64_t> FieldVals
;
781 insnWithID(Insn
, Opc
);
783 // Look for islands of undecoded bits of the singleton.
784 getIslands(StartBits
, EndBits
, FieldVals
, Insn
);
786 unsigned Size
= StartBits
.size();
789 // If we have matched all the well-known bits, just issue a return.
791 o
.indent(Indentation
) << "{\n";
792 o
.indent(Indentation
) << " MI.setOpcode(" << Opc
<< ");\n";
793 std::vector
<OperandInfo
>& InsnOperands
= Operands
[Opc
];
794 for (std::vector
<OperandInfo
>::iterator
795 I
= InsnOperands
.begin(), E
= InsnOperands
.end(); I
!= E
; ++I
) {
796 // If a custom instruction decoder was specified, use that.
797 if (I
->FieldBase
== ~0U && I
->FieldLength
== ~0U) {
798 o
.indent(Indentation
) << " " << I
->Decoder
<< "(MI, insn);\n";
802 o
.indent(Indentation
)
803 << " tmp = fieldFromInstruction(insn, " << I
->FieldBase
804 << ", " << I
->FieldLength
<< ");\n";
805 if (I
->Decoder
!= "") {
806 o
.indent(Indentation
) << " " << I
->Decoder
<< "(MI, tmp);\n";
808 o
.indent(Indentation
)
809 << " MI.addOperand(MCOperand::CreateImm(tmp));\n";
813 o
.indent(Indentation
) << " return true; // " << nameWithID(Opc
)
815 o
.indent(Indentation
) << "}\n";
819 // Otherwise, there are more decodings to be done!
821 // Emit code to match the island(s) for the singleton.
822 o
.indent(Indentation
) << "// Check ";
824 for (I
= Size
; I
!= 0; --I
) {
825 o
<< "Inst{" << EndBits
[I
-1] << '-' << StartBits
[I
-1] << "} ";
829 o
<< "for singleton decoding...\n";
832 o
.indent(Indentation
) << "if (";
834 for (I
= Size
; I
!= 0; --I
) {
835 NumBits
= EndBits
[I
-1] - StartBits
[I
-1] + 1;
836 o
<< "fieldFromInstruction(insn, " << StartBits
[I
-1] << ", " << NumBits
837 << ") == " << FieldVals
[I
-1];
843 o
.indent(Indentation
) << " MI.setOpcode(" << Opc
<< ");\n";
844 std::vector
<OperandInfo
>& InsnOperands
= Operands
[Opc
];
845 for (std::vector
<OperandInfo
>::iterator
846 I
= InsnOperands
.begin(), E
= InsnOperands
.end(); I
!= E
; ++I
) {
847 // If a custom instruction decoder was specified, use that.
848 if (I
->FieldBase
== ~0U && I
->FieldLength
== ~0U) {
849 o
.indent(Indentation
) << " " << I
->Decoder
<< "(MI, insn);\n";
853 o
.indent(Indentation
)
854 << " tmp = fieldFromInstruction(insn, " << I
->FieldBase
855 << ", " << I
->FieldLength
<< ");\n";
856 if (I
->Decoder
!= "") {
857 o
.indent(Indentation
) << " " << I
->Decoder
<< "(MI, tmp);\n";
859 o
.indent(Indentation
)
860 << " MI.addOperand(MCOperand::CreateImm(tmp));\n";
863 o
.indent(Indentation
) << " return true; // " << nameWithID(Opc
)
865 o
.indent(Indentation
) << "}\n";
870 // Emits code to decode the singleton, and then to decode the rest.
871 void FilterChooser::emitSingletonDecoder(raw_ostream
&o
, unsigned &Indentation
,
874 unsigned Opc
= Best
.getSingletonOpc();
876 emitSingletonDecoder(o
, Indentation
, Opc
);
878 // Emit code for the rest.
879 o
.indent(Indentation
) << "else\n";
882 Best
.getVariableFC().emit(o
, Indentation
);
886 // Assign a single filter and run with it. Top level API client can initialize
887 // with a single filter to start the filtering process.
888 void FilterChooser::runSingleFilter(FilterChooser
&owner
, unsigned startBit
,
889 unsigned numBit
, bool mixed
) {
891 Filter
F(*this, startBit
, numBit
, true);
892 Filters
.push_back(F
);
893 BestIndex
= 0; // Sole Filter instance to choose from.
894 bestFilter().recurse();
897 // reportRegion is a helper function for filterProcessor to mark a region as
898 // eligible for use as a filter region.
899 void FilterChooser::reportRegion(bitAttr_t RA
, unsigned StartBit
,
900 unsigned BitIndex
, bool AllowMixed
) {
901 if (RA
== ATTR_MIXED
&& AllowMixed
)
902 Filters
.push_back(Filter(*this, StartBit
, BitIndex
- StartBit
, true));
903 else if (RA
== ATTR_ALL_SET
&& !AllowMixed
)
904 Filters
.push_back(Filter(*this, StartBit
, BitIndex
- StartBit
, false));
907 // FilterProcessor scans the well-known encoding bits of the instructions and
908 // builds up a list of candidate filters. It chooses the best filter and
909 // recursively descends down the decoding tree.
910 bool FilterChooser::filterProcessor(bool AllowMixed
, bool Greedy
) {
913 unsigned numInstructions
= Opcodes
.size();
915 assert(numInstructions
&& "Filter created with no instructions");
917 // No further filtering is necessary.
918 if (numInstructions
== 1)
921 // Heuristics. See also doFilter()'s "Heuristics" comment when num of
922 // instructions is 3.
923 if (AllowMixed
&& !Greedy
) {
924 assert(numInstructions
== 3);
926 for (unsigned i
= 0; i
< Opcodes
.size(); ++i
) {
927 std::vector
<unsigned> StartBits
;
928 std::vector
<unsigned> EndBits
;
929 std::vector
<uint64_t> FieldVals
;
932 insnWithID(Insn
, Opcodes
[i
]);
934 // Look for islands of undecoded bits of any instruction.
935 if (getIslands(StartBits
, EndBits
, FieldVals
, Insn
) > 0) {
936 // Found an instruction with island(s). Now just assign a filter.
937 runSingleFilter(*this, StartBits
[0], EndBits
[0] - StartBits
[0] + 1,
944 unsigned BitIndex
, InsnIndex
;
946 // We maintain BIT_WIDTH copies of the bitAttrs automaton.
947 // The automaton consumes the corresponding bit from each
950 // Input symbols: 0, 1, and _ (unset).
951 // States: NONE, FILTERED, ALL_SET, ALL_UNSET, and MIXED.
952 // Initial state: NONE.
954 // (NONE) ------- [01] -> (ALL_SET)
955 // (NONE) ------- _ ----> (ALL_UNSET)
956 // (ALL_SET) ---- [01] -> (ALL_SET)
957 // (ALL_SET) ---- _ ----> (MIXED)
958 // (ALL_UNSET) -- [01] -> (MIXED)
959 // (ALL_UNSET) -- _ ----> (ALL_UNSET)
960 // (MIXED) ------ . ----> (MIXED)
961 // (FILTERED)---- . ----> (FILTERED)
963 bitAttr_t bitAttrs
[BIT_WIDTH
];
965 // FILTERED bit positions provide no entropy and are not worthy of pursuing.
966 // Filter::recurse() set either BIT_TRUE or BIT_FALSE for each position.
967 for (BitIndex
= 0; BitIndex
< BIT_WIDTH
; ++BitIndex
)
968 if (FilterBitValues
[BitIndex
] == BIT_TRUE
||
969 FilterBitValues
[BitIndex
] == BIT_FALSE
)
970 bitAttrs
[BitIndex
] = ATTR_FILTERED
;
972 bitAttrs
[BitIndex
] = ATTR_NONE
;
974 for (InsnIndex
= 0; InsnIndex
< numInstructions
; ++InsnIndex
) {
977 insnWithID(insn
, Opcodes
[InsnIndex
]);
979 for (BitIndex
= 0; BitIndex
< BIT_WIDTH
; ++BitIndex
) {
980 switch (bitAttrs
[BitIndex
]) {
982 if (insn
[BitIndex
] == BIT_UNSET
)
983 bitAttrs
[BitIndex
] = ATTR_ALL_UNSET
;
985 bitAttrs
[BitIndex
] = ATTR_ALL_SET
;
988 if (insn
[BitIndex
] == BIT_UNSET
)
989 bitAttrs
[BitIndex
] = ATTR_MIXED
;
992 if (insn
[BitIndex
] != BIT_UNSET
)
993 bitAttrs
[BitIndex
] = ATTR_MIXED
;
1002 // The regionAttr automaton consumes the bitAttrs automatons' state,
1003 // lowest-to-highest.
1005 // Input symbols: F(iltered), (all_)S(et), (all_)U(nset), M(ixed)
1006 // States: NONE, ALL_SET, MIXED
1007 // Initial state: NONE
1009 // (NONE) ----- F --> (NONE)
1010 // (NONE) ----- S --> (ALL_SET) ; and set region start
1011 // (NONE) ----- U --> (NONE)
1012 // (NONE) ----- M --> (MIXED) ; and set region start
1013 // (ALL_SET) -- F --> (NONE) ; and report an ALL_SET region
1014 // (ALL_SET) -- S --> (ALL_SET)
1015 // (ALL_SET) -- U --> (NONE) ; and report an ALL_SET region
1016 // (ALL_SET) -- M --> (MIXED) ; and report an ALL_SET region
1017 // (MIXED) ---- F --> (NONE) ; and report a MIXED region
1018 // (MIXED) ---- S --> (ALL_SET) ; and report a MIXED region
1019 // (MIXED) ---- U --> (NONE) ; and report a MIXED region
1020 // (MIXED) ---- M --> (MIXED)
1022 bitAttr_t RA
= ATTR_NONE
;
1023 unsigned StartBit
= 0;
1025 for (BitIndex
= 0; BitIndex
< BIT_WIDTH
; BitIndex
++) {
1026 bitAttr_t bitAttr
= bitAttrs
[BitIndex
];
1028 assert(bitAttr
!= ATTR_NONE
&& "Bit without attributes");
1036 StartBit
= BitIndex
;
1039 case ATTR_ALL_UNSET
:
1042 StartBit
= BitIndex
;
1046 assert(0 && "Unexpected bitAttr!");
1052 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1057 case ATTR_ALL_UNSET
:
1058 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1062 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1063 StartBit
= BitIndex
;
1067 assert(0 && "Unexpected bitAttr!");
1073 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1074 StartBit
= BitIndex
;
1078 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1079 StartBit
= BitIndex
;
1082 case ATTR_ALL_UNSET
:
1083 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1089 assert(0 && "Unexpected bitAttr!");
1092 case ATTR_ALL_UNSET
:
1093 assert(0 && "regionAttr state machine has no ATTR_UNSET state");
1095 assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
1099 // At the end, if we're still in ALL_SET or MIXED states, report a region
1106 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1108 case ATTR_ALL_UNSET
:
1111 reportRegion(RA
, StartBit
, BitIndex
, AllowMixed
);
1115 // We have finished with the filter processings. Now it's time to choose
1116 // the best performing filter.
1118 bool AllUseless
= true;
1119 unsigned BestScore
= 0;
1121 for (unsigned i
= 0, e
= Filters
.size(); i
!= e
; ++i
) {
1122 unsigned Usefulness
= Filters
[i
].usefulness();
1127 if (Usefulness
> BestScore
) {
1129 BestScore
= Usefulness
;
1134 bestFilter().recurse();
1137 } // end of FilterChooser::filterProcessor(bool)
1139 // Decides on the best configuration of filter(s) to use in order to decode
1140 // the instructions. A conflict of instructions may occur, in which case we
1141 // dump the conflict set to the standard error.
1142 void FilterChooser::doFilter() {
1143 unsigned Num
= Opcodes
.size();
1144 assert(Num
&& "FilterChooser created with no instructions");
1146 // Try regions of consecutive known bit values first.
1147 if (filterProcessor(false))
1150 // Then regions of mixed bits (both known and unitialized bit values allowed).
1151 if (filterProcessor(true))
1154 // Heuristics to cope with conflict set {t2CMPrs, t2SUBSrr, t2SUBSrs} where
1155 // no single instruction for the maximum ATTR_MIXED region Inst{14-4} has a
1156 // well-known encoding pattern. In such case, we backtrack and scan for the
1157 // the very first consecutive ATTR_ALL_SET region and assign a filter to it.
1158 if (Num
== 3 && filterProcessor(true, false))
1161 // If we come to here, the instruction decoding has failed.
1162 // Set the BestIndex to -1 to indicate so.
1166 // Emits code to decode our share of instructions. Returns true if the
1167 // emitted code causes a return, which occurs if we know how to decode
1168 // the instruction at this level or the instruction is not decodeable.
1169 bool FilterChooser::emit(raw_ostream
&o
, unsigned &Indentation
) {
1170 if (Opcodes
.size() == 1)
1171 // There is only one instruction in the set, which is great!
1172 // Call emitSingletonDecoder() to see whether there are any remaining
1174 return emitSingletonDecoder(o
, Indentation
, Opcodes
[0]);
1176 // Choose the best filter to do the decodings!
1177 if (BestIndex
!= -1) {
1178 Filter
&Best
= bestFilter();
1179 if (Best
.getNumFiltered() == 1)
1180 emitSingletonDecoder(o
, Indentation
, Best
);
1182 bestFilter().emit(o
, Indentation
);
1186 // We don't know how to decode these instructions! Return 0 and dump the
1188 o
.indent(Indentation
) << "return 0;" << " // Conflict set: ";
1189 for (int i
= 0, N
= Opcodes
.size(); i
< N
; ++i
) {
1190 o
<< nameWithID(Opcodes
[i
]);
1197 // Print out useful conflict information for postmortem analysis.
1198 errs() << "Decoding Conflict:\n";
1200 dumpStack(errs(), "\t\t");
1202 for (unsigned i
= 0; i
< Opcodes
.size(); i
++) {
1203 const std::string
&Name
= nameWithID(Opcodes
[i
]);
1205 errs() << '\t' << Name
<< " ";
1207 getBitsField(*AllInstructions
[Opcodes
[i
]]->TheDef
, "Inst"));
1214 bool FixedLenDecoderEmitter::populateInstruction(const CodeGenInstruction
&CGI
,
1216 const Record
&Def
= *CGI
.TheDef
;
1217 // If all the bit positions are not specified; do not decode this instruction.
1218 // We are bound to fail! For proper disassembly, the well-known encoding bits
1219 // of the instruction must be fully specified.
1221 // This also removes pseudo instructions from considerations of disassembly,
1222 // which is a better design and less fragile than the name matchings.
1223 BitsInit
&Bits
= getBitsField(Def
, "Inst");
1224 if (Bits
.allInComplete()) return false;
1226 // Ignore "asm parser only" instructions.
1227 if (Def
.getValueAsBit("isAsmParserOnly") ||
1228 Def
.getValueAsBit("isCodeGenOnly"))
1231 std::vector
<OperandInfo
> InsnOperands
;
1233 // If the instruction has specified a custom decoding hook, use that instead
1234 // of trying to auto-generate the decoder.
1235 std::string InstDecoder
= Def
.getValueAsString("DecoderMethod");
1236 if (InstDecoder
!= "") {
1237 InsnOperands
.push_back(OperandInfo(~0U, ~0U, InstDecoder
));
1238 Operands
[Opc
] = InsnOperands
;
1242 // Generate a description of the operand of the instruction that we know
1243 // how to decode automatically.
1244 // FIXME: We'll need to have a way to manually override this as needed.
1246 // Gather the outputs/inputs of the instruction, so we can find their
1247 // positions in the encoding. This assumes for now that they appear in the
1248 // MCInst in the order that they're listed.
1249 std::vector
<std::pair
<Init
*, std::string
> > InOutOperands
;
1250 DagInit
*Out
= Def
.getValueAsDag("OutOperandList");
1251 DagInit
*In
= Def
.getValueAsDag("InOperandList");
1252 for (unsigned i
= 0; i
< Out
->getNumArgs(); ++i
)
1253 InOutOperands
.push_back(std::make_pair(Out
->getArg(i
), Out
->getArgName(i
)));
1254 for (unsigned i
= 0; i
< In
->getNumArgs(); ++i
)
1255 InOutOperands
.push_back(std::make_pair(In
->getArg(i
), In
->getArgName(i
)));
1257 // For each operand, see if we can figure out where it is encoded.
1258 for (std::vector
<std::pair
<Init
*, std::string
> >::iterator
1259 NI
= InOutOperands
.begin(), NE
= InOutOperands
.end(); NI
!= NE
; ++NI
) {
1260 unsigned PrevBit
= ~0;
1262 unsigned PrevPos
= ~0;
1263 std::string Decoder
= "";
1265 for (unsigned bi
= 0; bi
< Bits
.getNumBits(); ++bi
) {
1266 VarBitInit
*BI
= dynamic_cast<VarBitInit
*>(Bits
.getBit(bi
));
1269 VarInit
*Var
= dynamic_cast<VarInit
*>(BI
->getVariable());
1271 unsigned CurrBit
= BI
->getBitNum();
1272 if (Var
->getName() != NI
->second
) continue;
1274 // Figure out the lowest bit of the value, and the width of the field.
1275 // Deliberately don't try to handle cases where the field is scattered,
1276 // or where not all bits of the the field are explicit.
1277 if (Base
== ~0U && PrevBit
== ~0U && PrevPos
== ~0U) {
1284 if ((PrevPos
!= ~0U && bi
-1 != PrevPos
) ||
1285 (CurrBit
!= ~0U && CurrBit
-1 != PrevBit
)) {
1294 // At this point, we can locate the field, but we need to know how to
1295 // interpret it. As a first step, require the target to provide callbacks
1296 // for decoding register classes.
1297 // FIXME: This need to be extended to handle instructions with custom
1298 // decoder methods, and operands with (simple) MIOperandInfo's.
1299 TypedInit
*TI
= dynamic_cast<TypedInit
*>(NI
->first
);
1300 RecordRecTy
*Type
= dynamic_cast<RecordRecTy
*>(TI
->getType());
1301 Record
*TypeRecord
= Type
->getRecord();
1303 if (TypeRecord
->isSubClassOf("RegisterClass")) {
1304 Decoder
= "Decode" + Type
->getRecord()->getName() + "RegisterClass";
1308 RecordVal
*DecoderString
= TypeRecord
->getValue("DecoderMethod");
1309 StringInit
*String
= DecoderString
?
1310 dynamic_cast<StringInit
*>(DecoderString
->getValue()) :
1312 if (!isReg
&& String
&& String
->getValue() != "")
1313 Decoder
= String
->getValue();
1317 InsnOperands
.push_back(OperandInfo(Base
, PrevBit
+1, Decoder
));
1318 DEBUG(errs() << "ENCODED OPERAND: $" << NI
->second
<< " @ ("
1319 << utostr(Base
+PrevBit
) << ", " << utostr(Base
) << ")\n");
1323 Operands
[Opc
] = InsnOperands
;
1328 // Dumps the instruction encoding bits.
1329 dumpBits(errs(), Bits
);
1333 // Dumps the list of operand info.
1334 for (unsigned i
= 0, e
= CGI
.Operands
.size(); i
!= e
; ++i
) {
1335 const CGIOperandList::OperandInfo
&Info
= CGI
.Operands
[i
];
1336 const std::string
&OperandName
= Info
.Name
;
1337 const Record
&OperandDef
= *Info
.Rec
;
1339 errs() << "\t" << OperandName
<< " (" << OperandDef
.getName() << ")\n";
1347 void FixedLenDecoderEmitter::populateInstructions() {
1348 for (unsigned i
= 0, e
= NumberedInstructions
.size(); i
< e
; ++i
) {
1349 Record
*R
= NumberedInstructions
[i
]->TheDef
;
1350 if (R
->getValueAsString("Namespace") == "TargetOpcode")
1353 if (populateInstruction(*NumberedInstructions
[i
], i
))
1354 Opcodes
.push_back(i
);
1358 // Emits disassembler code for instruction decoding.
1359 void FixedLenDecoderEmitter::run(raw_ostream
&o
)
1361 o
<< "#include \"llvm/MC/MCInst.h\"\n";
1362 o
<< "#include \"llvm/Support/DataTypes.h\"\n";
1363 o
<< "#include <assert.h>\n";
1365 o
<< "namespace llvm {\n\n";
1367 NumberedInstructions
= Target
.getInstructionsByEnumValue();
1368 populateInstructions();
1369 FilterChooser
FC(NumberedInstructions
, Opcodes
, Operands
);
1372 o
<< "\n} // End llvm namespace \n";