FixedLenDecoderEmitter.cpp [plain text]
#define DEBUG_TYPE "decoder-emitter"
#include "FixedLenDecoderEmitter.h"
#include "CodeGenTarget.h"
#include "Record.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>
#include <map>
#include <string>
using namespace llvm;
typedef enum {
BIT_TRUE, BIT_FALSE, BIT_UNSET, BIT_UNFILTERED } bit_value_t;
static bool ValueSet(bit_value_t V) {
return (V == BIT_TRUE || V == BIT_FALSE);
}
static bool ValueNotSet(bit_value_t V) {
return (V == BIT_UNSET);
}
static int Value(bit_value_t V) {
return ValueNotSet(V) ? -1 : (V == BIT_FALSE ? 0 : 1);
}
static bit_value_t bitFromBits(BitsInit &bits, unsigned index) {
if (BitInit *bit = dynamic_cast<BitInit*>(bits.getBit(index)))
return bit->getValue() ? BIT_TRUE : BIT_FALSE;
return BIT_UNSET;
}
static void dumpBits(raw_ostream &o, BitsInit &bits) {
unsigned index;
for (index = bits.getNumBits(); index > 0; index--) {
switch (bitFromBits(bits, index - 1)) {
case BIT_TRUE:
o << "1";
break;
case BIT_FALSE:
o << "0";
break;
case BIT_UNSET:
o << "_";
break;
default:
assert(0 && "unexpected return value from bitFromBits");
}
}
}
static BitsInit &getBitsField(const Record &def, const char *str) {
BitsInit *bits = def.getValueAsBitsInit(str);
return *bits;
}
class FilterChooser;
#define BIT_WIDTH 32
typedef bit_value_t insn_t[BIT_WIDTH];
class Filter {
protected:
FilterChooser *Owner; unsigned StartBit; unsigned NumBits; bool Mixed;
std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
std::vector<unsigned> VariableInstructions;
std::map<unsigned, FilterChooser*> FilterChooserMap;
unsigned NumFiltered;
unsigned LastOpcFiltered;
unsigned NumVariable;
public:
unsigned getNumFiltered() { return NumFiltered; }
unsigned getNumVariable() { return NumVariable; }
unsigned getSingletonOpc() {
assert(NumFiltered == 1);
return LastOpcFiltered;
}
FilterChooser &getVariableFC() {
assert(NumFiltered == 1);
assert(FilterChooserMap.size() == 1);
return *(FilterChooserMap.find((unsigned)-1)->second);
}
Filter(const Filter &f);
Filter(FilterChooser &owner, unsigned startBit, unsigned numBits, bool mixed);
~Filter();
void recurse();
void emit(raw_ostream &o, unsigned &Indentation);
unsigned usefulness() const;
};
typedef enum {
ATTR_NONE,
ATTR_FILTERED,
ATTR_ALL_SET,
ATTR_ALL_UNSET,
ATTR_MIXED
} bitAttr_t;
class FilterChooser {
protected:
friend class Filter;
const std::vector<const CodeGenInstruction*> &AllInstructions;
const std::vector<unsigned> Opcodes;
std::map<unsigned, std::vector<OperandInfo> > &Operands;
std::vector<Filter> Filters;
bit_value_t FilterBitValues[BIT_WIDTH];
FilterChooser *Parent;
int BestIndex;
public:
FilterChooser(const FilterChooser &FC) :
AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
Operands(FC.Operands), Filters(FC.Filters), Parent(FC.Parent),
BestIndex(FC.BestIndex) {
memcpy(FilterBitValues, FC.FilterBitValues, sizeof(FilterBitValues));
}
FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
const std::vector<unsigned> &IDs,
std::map<unsigned, std::vector<OperandInfo> > &Ops) :
AllInstructions(Insts), Opcodes(IDs), Operands(Ops), Filters(),
Parent(NULL), BestIndex(-1) {
for (unsigned i = 0; i < BIT_WIDTH; ++i)
FilterBitValues[i] = BIT_UNFILTERED;
doFilter();
}
FilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
const std::vector<unsigned> &IDs,
std::map<unsigned, std::vector<OperandInfo> > &Ops,
bit_value_t (&ParentFilterBitValues)[BIT_WIDTH],
FilterChooser &parent) :
AllInstructions(Insts), Opcodes(IDs), Operands(Ops),
Filters(), Parent(&parent), BestIndex(-1) {
for (unsigned i = 0; i < BIT_WIDTH; ++i)
FilterBitValues[i] = ParentFilterBitValues[i];
doFilter();
}
bool isTopLevel() { return Parent == NULL; }
void emitTop(raw_ostream &o, unsigned Indentation);
protected:
void insnWithID(insn_t &Insn, unsigned Opcode) const {
BitsInit &Bits = getBitsField(*AllInstructions[Opcode]->TheDef, "Inst");
for (unsigned i = 0; i < BIT_WIDTH; ++i)
Insn[i] = bitFromBits(Bits, i);
}
const std::string &nameWithID(unsigned Opcode) const {
return AllInstructions[Opcode]->TheDef->getName();
}
bool fieldFromInsn(uint64_t &Field, insn_t &Insn, unsigned StartBit,
unsigned NumBits) const;
void dumpFilterArray(raw_ostream &o, bit_value_t (&filter)[BIT_WIDTH]);
void dumpStack(raw_ostream &o, const char *prefix);
Filter &bestFilter() {
assert(BestIndex != -1 && "BestIndex not set");
return Filters[BestIndex];
}
void SingletonExists(unsigned Opc);
bool PositionFiltered(unsigned i) {
return ValueSet(FilterBitValues[i]);
}
unsigned getIslands(std::vector<unsigned> &StartBits,
std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
insn_t &Insn);
bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,Filter &Best);
void runSingleFilter(FilterChooser &owner, unsigned startBit, unsigned numBit,
bool mixed);
void reportRegion(bitAttr_t RA, unsigned StartBit, unsigned BitIndex,
bool AllowMixed);
bool filterProcessor(bool AllowMixed, bool Greedy = true);
void doFilter();
bool emit(raw_ostream &o, unsigned &Indentation);
};
Filter::Filter(const Filter &f) :
Owner(f.Owner), StartBit(f.StartBit), NumBits(f.NumBits), Mixed(f.Mixed),
FilteredInstructions(f.FilteredInstructions),
VariableInstructions(f.VariableInstructions),
FilterChooserMap(f.FilterChooserMap), NumFiltered(f.NumFiltered),
LastOpcFiltered(f.LastOpcFiltered), NumVariable(f.NumVariable) {
}
Filter::Filter(FilterChooser &owner, unsigned startBit, unsigned numBits,
bool mixed) : Owner(&owner), StartBit(startBit), NumBits(numBits),
Mixed(mixed) {
assert(StartBit + NumBits - 1 < BIT_WIDTH);
NumFiltered = 0;
LastOpcFiltered = 0;
NumVariable = 0;
for (unsigned i = 0, e = Owner->Opcodes.size(); i != e; ++i) {
insn_t Insn;
Owner->insnWithID(Insn, Owner->Opcodes[i]);
uint64_t Field;
bool ok = Owner->fieldFromInsn(Field, Insn, StartBit, NumBits);
if (ok) {
LastOpcFiltered = Owner->Opcodes[i];
FilteredInstructions[Field].push_back(LastOpcFiltered);
++NumFiltered;
} else {
VariableInstructions.push_back(Owner->Opcodes[i]);
++NumVariable;
}
}
assert((FilteredInstructions.size() + VariableInstructions.size() > 0)
&& "Filter returns no instruction categories");
}
Filter::~Filter() {
std::map<unsigned, FilterChooser*>::iterator filterIterator;
for (filterIterator = FilterChooserMap.begin();
filterIterator != FilterChooserMap.end();
filterIterator++) {
delete filterIterator->second;
}
}
void Filter::recurse() {
std::map<uint64_t, std::vector<unsigned> >::const_iterator mapIterator;
bit_value_t BitValueArray[BIT_WIDTH];
memcpy(BitValueArray, Owner->FilterBitValues, sizeof(BitValueArray));
unsigned bitIndex;
if (VariableInstructions.size()) {
for (bitIndex = 0; bitIndex < NumBits; bitIndex++)
BitValueArray[StartBit + bitIndex] = BIT_UNSET;
FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
(unsigned)-1,
new FilterChooser(Owner->AllInstructions,
VariableInstructions,
Owner->Operands,
BitValueArray,
*Owner)
));
}
if (getNumFiltered() == 1) {
assert(FilterChooserMap.size() == 1);
return;
}
for (mapIterator = FilteredInstructions.begin();
mapIterator != FilteredInstructions.end();
mapIterator++) {
for (bitIndex = 0; bitIndex < NumBits; bitIndex++) {
if (mapIterator->first & (1ULL << bitIndex))
BitValueArray[StartBit + bitIndex] = BIT_TRUE;
else
BitValueArray[StartBit + bitIndex] = BIT_FALSE;
}
FilterChooserMap.insert(std::pair<unsigned, FilterChooser*>(
mapIterator->first,
new FilterChooser(Owner->AllInstructions,
mapIterator->second,
Owner->Operands,
BitValueArray,
*Owner)
));
}
}
void Filter::emit(raw_ostream &o, unsigned &Indentation) {
o.indent(Indentation) << "// Check Inst{";
if (NumBits > 1)
o << (StartBit + NumBits - 1) << '-';
o << StartBit << "} ...\n";
o.indent(Indentation) << "switch (fieldFromInstruction(insn, "
<< StartBit << ", " << NumBits << ")) {\n";
std::map<unsigned, FilterChooser*>::iterator filterIterator;
bool DefaultCase = false;
for (filterIterator = FilterChooserMap.begin();
filterIterator != FilterChooserMap.end();
filterIterator++) {
if (filterIterator->first == (unsigned)-1) {
DefaultCase = true;
o.indent(Indentation) << "default:\n";
o.indent(Indentation) << " break; // fallthrough\n";
o.indent(Indentation) << "}\n";
} else
o.indent(Indentation) << "case " << filterIterator->first << ":\n";
if (!DefaultCase) { ++Indentation; ++Indentation; }
bool finished = filterIterator->second->emit(o, Indentation);
if (Owner->isTopLevel() && DefaultCase)
break;
if (!finished)
o.indent(Indentation) << "break;\n";
if (!DefaultCase) { --Indentation; --Indentation; }
}
if (!DefaultCase) {
o.indent(Indentation) << "}\n";
}
}
unsigned Filter::usefulness() const {
if (VariableInstructions.size())
return FilteredInstructions.size();
else
return FilteredInstructions.size() + 1;
}
void FilterChooser::emitTop(raw_ostream &o, unsigned Indentation) {
switch (BIT_WIDTH) {
case 8:
o.indent(Indentation) << "typedef uint8_t field_t;\n";
break;
case 16:
o.indent(Indentation) << "typedef uint16_t field_t;\n";
break;
case 32:
o.indent(Indentation) << "typedef uint32_t field_t;\n";
break;
case 64:
o.indent(Indentation) << "typedef uint64_t field_t;\n";
break;
default:
assert(0 && "Unexpected instruction size!");
}
o << '\n';
o.indent(Indentation) << "static field_t " <<
"fieldFromInstruction(field_t insn, unsigned startBit, unsigned numBits)\n";
o.indent(Indentation) << "{\n";
++Indentation; ++Indentation;
o.indent(Indentation) << "assert(startBit + numBits <= " << BIT_WIDTH
<< " && \"Instruction field out of bounds!\");\n";
o << '\n';
o.indent(Indentation) << "field_t fieldMask;\n";
o << '\n';
o.indent(Indentation) << "if (numBits == " << BIT_WIDTH << ")\n";
++Indentation; ++Indentation;
o.indent(Indentation) << "fieldMask = (field_t)-1;\n";
--Indentation; --Indentation;
o.indent(Indentation) << "else\n";
++Indentation; ++Indentation;
o.indent(Indentation) << "fieldMask = ((1 << numBits) - 1) << startBit;\n";
--Indentation; --Indentation;
o << '\n';
o.indent(Indentation) << "return (insn & fieldMask) >> startBit;\n";
--Indentation; --Indentation;
o.indent(Indentation) << "}\n";
o << '\n';
o.indent(Indentation) <<
"static bool decodeInstruction(MCInst &MI, field_t insn, "
"uint64_t Address, const void *Decoder) {\n";
o.indent(Indentation) << " unsigned tmp = 0;\n";
++Indentation; ++Indentation;
emit(o, Indentation);
o << '\n';
o.indent(Indentation) << "return false;\n";
--Indentation; --Indentation;
o.indent(Indentation) << "}\n";
o << '\n';
}
bool FilterChooser::fieldFromInsn(uint64_t &Field, insn_t &Insn,
unsigned StartBit, unsigned NumBits) const {
Field = 0;
for (unsigned i = 0; i < NumBits; ++i) {
if (Insn[StartBit + i] == BIT_UNSET)
return false;
if (Insn[StartBit + i] == BIT_TRUE)
Field = Field | (1ULL << i);
}
return true;
}
void FilterChooser::dumpFilterArray(raw_ostream &o,
bit_value_t (&filter)[BIT_WIDTH]) {
unsigned bitIndex;
for (bitIndex = BIT_WIDTH; bitIndex > 0; bitIndex--) {
switch (filter[bitIndex - 1]) {
case BIT_UNFILTERED:
o << ".";
break;
case BIT_UNSET:
o << "_";
break;
case BIT_TRUE:
o << "1";
break;
case BIT_FALSE:
o << "0";
break;
}
}
}
void FilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
FilterChooser *current = this;
while (current) {
o << prefix;
dumpFilterArray(o, current->FilterBitValues);
o << '\n';
current = current->Parent;
}
}
void FilterChooser::SingletonExists(unsigned Opc) {
insn_t Insn0;
insnWithID(Insn0, Opc);
errs() << "Singleton exists: " << nameWithID(Opc)
<< " with its decoding dominating ";
for (unsigned i = 0; i < Opcodes.size(); ++i) {
if (Opcodes[i] == Opc) continue;
errs() << nameWithID(Opcodes[i]) << ' ';
}
errs() << '\n';
dumpStack(errs(), "\t\t");
for (unsigned i = 0; i < Opcodes.size(); i++) {
const std::string &Name = nameWithID(Opcodes[i]);
errs() << '\t' << Name << " ";
dumpBits(errs(),
getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
errs() << '\n';
}
}
unsigned FilterChooser::getIslands(std::vector<unsigned> &StartBits,
std::vector<unsigned> &EndBits, std::vector<uint64_t> &FieldVals,
insn_t &Insn) {
unsigned Num, BitNo;
Num = BitNo = 0;
uint64_t FieldVal = 0;
int State = 0;
int Val = -1;
for (unsigned i = 0; i < BIT_WIDTH; ++i) {
Val = Value(Insn[i]);
bool Filtered = PositionFiltered(i);
switch (State) {
default:
assert(0 && "Unreachable code!");
break;
case 0:
case 1:
if (Filtered || Val == -1)
State = 1; else {
State = 2; BitNo = 0;
StartBits.push_back(i);
FieldVal = Val;
}
break;
case 2:
if (Filtered || Val == -1) {
State = 1; EndBits.push_back(i - 1);
FieldVals.push_back(FieldVal);
++Num;
} else {
State = 2; ++BitNo;
FieldVal = FieldVal | Val << BitNo;
}
break;
}
}
if (State == 2) {
EndBits.push_back(BIT_WIDTH - 1);
FieldVals.push_back(FieldVal);
++Num;
}
assert(StartBits.size() == Num && EndBits.size() == Num &&
FieldVals.size() == Num);
return Num;
}
bool FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
unsigned Opc) {
std::vector<unsigned> StartBits;
std::vector<unsigned> EndBits;
std::vector<uint64_t> FieldVals;
insn_t Insn;
insnWithID(Insn, Opc);
getIslands(StartBits, EndBits, FieldVals, Insn);
unsigned Size = StartBits.size();
unsigned I, NumBits;
if (Size == 0) {
o.indent(Indentation) << "{\n";
o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n";
std::vector<OperandInfo>& InsnOperands = Operands[Opc];
for (std::vector<OperandInfo>::iterator
I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
if (I->FieldBase == ~0U && I->FieldLength == ~0U) {
o.indent(Indentation) << " " << I->Decoder
<< "(MI, insn, Address, Decoder);\n";
break;
}
o.indent(Indentation)
<< " tmp = fieldFromInstruction(insn, " << I->FieldBase
<< ", " << I->FieldLength << ");\n";
if (I->Decoder != "") {
o.indent(Indentation) << " " << I->Decoder
<< "(MI, tmp, Address, Decoder);\n";
} else {
o.indent(Indentation)
<< " MI.addOperand(MCOperand::CreateImm(tmp));\n";
}
}
o.indent(Indentation) << " return true; // " << nameWithID(Opc)
<< '\n';
o.indent(Indentation) << "}\n";
return true;
}
o.indent(Indentation) << "// Check ";
for (I = Size; I != 0; --I) {
o << "Inst{" << EndBits[I-1] << '-' << StartBits[I-1] << "} ";
if (I > 1)
o << "&& ";
else
o << "for singleton decoding...\n";
}
o.indent(Indentation) << "if (";
for (I = Size; I != 0; --I) {
NumBits = EndBits[I-1] - StartBits[I-1] + 1;
o << "fieldFromInstruction(insn, " << StartBits[I-1] << ", " << NumBits
<< ") == " << FieldVals[I-1];
if (I > 1)
o << " && ";
else
o << ") {\n";
}
o.indent(Indentation) << " MI.setOpcode(" << Opc << ");\n";
std::vector<OperandInfo>& InsnOperands = Operands[Opc];
for (std::vector<OperandInfo>::iterator
I = InsnOperands.begin(), E = InsnOperands.end(); I != E; ++I) {
if (I->FieldBase == ~0U && I->FieldLength == ~0U) {
o.indent(Indentation) << " " << I->Decoder
<< "(MI, insn, Address, Decoder);\n";
break;
}
o.indent(Indentation)
<< " tmp = fieldFromInstruction(insn, " << I->FieldBase
<< ", " << I->FieldLength << ");\n";
if (I->Decoder != "") {
o.indent(Indentation) << " " << I->Decoder
<< "(MI, tmp, Address, Decoder);\n";
} else {
o.indent(Indentation)
<< " MI.addOperand(MCOperand::CreateImm(tmp));\n";
}
}
o.indent(Indentation) << " return true; // " << nameWithID(Opc)
<< '\n';
o.indent(Indentation) << "}\n";
return false;
}
void FilterChooser::emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
Filter &Best) {
unsigned Opc = Best.getSingletonOpc();
emitSingletonDecoder(o, Indentation, Opc);
o.indent(Indentation) << "else\n";
Indentation += 2;
Best.getVariableFC().emit(o, Indentation);
Indentation -= 2;
}
void FilterChooser::runSingleFilter(FilterChooser &owner, unsigned startBit,
unsigned numBit, bool mixed) {
Filters.clear();
Filter F(*this, startBit, numBit, true);
Filters.push_back(F);
BestIndex = 0; bestFilter().recurse();
}
void FilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
unsigned BitIndex, bool AllowMixed) {
if (RA == ATTR_MIXED && AllowMixed)
Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, true));
else if (RA == ATTR_ALL_SET && !AllowMixed)
Filters.push_back(Filter(*this, StartBit, BitIndex - StartBit, false));
}
bool FilterChooser::filterProcessor(bool AllowMixed, bool Greedy) {
Filters.clear();
BestIndex = -1;
unsigned numInstructions = Opcodes.size();
assert(numInstructions && "Filter created with no instructions");
if (numInstructions == 1)
return true;
if (AllowMixed && !Greedy) {
assert(numInstructions == 3);
for (unsigned i = 0; i < Opcodes.size(); ++i) {
std::vector<unsigned> StartBits;
std::vector<unsigned> EndBits;
std::vector<uint64_t> FieldVals;
insn_t Insn;
insnWithID(Insn, Opcodes[i]);
if (getIslands(StartBits, EndBits, FieldVals, Insn) > 0) {
runSingleFilter(*this, StartBits[0], EndBits[0] - StartBits[0] + 1,
true);
return true;
}
}
}
unsigned BitIndex, InsnIndex;
bitAttr_t bitAttrs[BIT_WIDTH];
for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex)
if (FilterBitValues[BitIndex] == BIT_TRUE ||
FilterBitValues[BitIndex] == BIT_FALSE)
bitAttrs[BitIndex] = ATTR_FILTERED;
else
bitAttrs[BitIndex] = ATTR_NONE;
for (InsnIndex = 0; InsnIndex < numInstructions; ++InsnIndex) {
insn_t insn;
insnWithID(insn, Opcodes[InsnIndex]);
for (BitIndex = 0; BitIndex < BIT_WIDTH; ++BitIndex) {
switch (bitAttrs[BitIndex]) {
case ATTR_NONE:
if (insn[BitIndex] == BIT_UNSET)
bitAttrs[BitIndex] = ATTR_ALL_UNSET;
else
bitAttrs[BitIndex] = ATTR_ALL_SET;
break;
case ATTR_ALL_SET:
if (insn[BitIndex] == BIT_UNSET)
bitAttrs[BitIndex] = ATTR_MIXED;
break;
case ATTR_ALL_UNSET:
if (insn[BitIndex] != BIT_UNSET)
bitAttrs[BitIndex] = ATTR_MIXED;
break;
case ATTR_MIXED:
case ATTR_FILTERED:
break;
}
}
}
bitAttr_t RA = ATTR_NONE;
unsigned StartBit = 0;
for (BitIndex = 0; BitIndex < BIT_WIDTH; BitIndex++) {
bitAttr_t bitAttr = bitAttrs[BitIndex];
assert(bitAttr != ATTR_NONE && "Bit without attributes");
switch (RA) {
case ATTR_NONE:
switch (bitAttr) {
case ATTR_FILTERED:
break;
case ATTR_ALL_SET:
StartBit = BitIndex;
RA = ATTR_ALL_SET;
break;
case ATTR_ALL_UNSET:
break;
case ATTR_MIXED:
StartBit = BitIndex;
RA = ATTR_MIXED;
break;
default:
assert(0 && "Unexpected bitAttr!");
}
break;
case ATTR_ALL_SET:
switch (bitAttr) {
case ATTR_FILTERED:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
RA = ATTR_NONE;
break;
case ATTR_ALL_SET:
break;
case ATTR_ALL_UNSET:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
RA = ATTR_NONE;
break;
case ATTR_MIXED:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
StartBit = BitIndex;
RA = ATTR_MIXED;
break;
default:
assert(0 && "Unexpected bitAttr!");
}
break;
case ATTR_MIXED:
switch (bitAttr) {
case ATTR_FILTERED:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
StartBit = BitIndex;
RA = ATTR_NONE;
break;
case ATTR_ALL_SET:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
StartBit = BitIndex;
RA = ATTR_ALL_SET;
break;
case ATTR_ALL_UNSET:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
RA = ATTR_NONE;
break;
case ATTR_MIXED:
break;
default:
assert(0 && "Unexpected bitAttr!");
}
break;
case ATTR_ALL_UNSET:
assert(0 && "regionAttr state machine has no ATTR_UNSET state");
case ATTR_FILTERED:
assert(0 && "regionAttr state machine has no ATTR_FILTERED state");
}
}
switch (RA) {
case ATTR_NONE:
break;
case ATTR_FILTERED:
break;
case ATTR_ALL_SET:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
break;
case ATTR_ALL_UNSET:
break;
case ATTR_MIXED:
reportRegion(RA, StartBit, BitIndex, AllowMixed);
break;
}
BestIndex = 0;
bool AllUseless = true;
unsigned BestScore = 0;
for (unsigned i = 0, e = Filters.size(); i != e; ++i) {
unsigned Usefulness = Filters[i].usefulness();
if (Usefulness)
AllUseless = false;
if (Usefulness > BestScore) {
BestIndex = i;
BestScore = Usefulness;
}
}
if (!AllUseless)
bestFilter().recurse();
return !AllUseless;
}
void FilterChooser::doFilter() {
unsigned Num = Opcodes.size();
assert(Num && "FilterChooser created with no instructions");
if (filterProcessor(false))
return;
if (filterProcessor(true))
return;
if (Num == 3 && filterProcessor(true, false))
return;
BestIndex = -1;
}
bool FilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
if (Opcodes.size() == 1)
return emitSingletonDecoder(o, Indentation, Opcodes[0]);
if (BestIndex != -1) {
Filter &Best = bestFilter();
if (Best.getNumFiltered() == 1)
emitSingletonDecoder(o, Indentation, Best);
else
bestFilter().emit(o, Indentation);
return false;
}
o.indent(Indentation) << "return 0;" << " // Conflict set: ";
for (int i = 0, N = Opcodes.size(); i < N; ++i) {
o << nameWithID(Opcodes[i]);
if (i < (N - 1))
o << ", ";
else
o << '\n';
}
errs() << "Decoding Conflict:\n";
dumpStack(errs(), "\t\t");
for (unsigned i = 0; i < Opcodes.size(); i++) {
const std::string &Name = nameWithID(Opcodes[i]);
errs() << '\t' << Name << " ";
dumpBits(errs(),
getBitsField(*AllInstructions[Opcodes[i]]->TheDef, "Inst"));
errs() << '\n';
}
return true;
}
bool FixedLenDecoderEmitter::populateInstruction(const CodeGenInstruction &CGI,
unsigned Opc){
const Record &Def = *CGI.TheDef;
BitsInit &Bits = getBitsField(Def, "Inst");
if (Bits.allInComplete()) return false;
if (Def.getValueAsBit("isAsmParserOnly") ||
Def.getValueAsBit("isCodeGenOnly"))
return false;
std::vector<OperandInfo> InsnOperands;
std::string InstDecoder = Def.getValueAsString("DecoderMethod");
if (InstDecoder != "") {
InsnOperands.push_back(OperandInfo(~0U, ~0U, InstDecoder));
Operands[Opc] = InsnOperands;
return true;
}
std::vector<std::pair<Init*, std::string> > InOutOperands;
DagInit *Out = Def.getValueAsDag("OutOperandList");
DagInit *In = Def.getValueAsDag("InOperandList");
for (unsigned i = 0; i < Out->getNumArgs(); ++i)
InOutOperands.push_back(std::make_pair(Out->getArg(i), Out->getArgName(i)));
for (unsigned i = 0; i < In->getNumArgs(); ++i)
InOutOperands.push_back(std::make_pair(In->getArg(i), In->getArgName(i)));
for (std::vector<std::pair<Init*, std::string> >::iterator
NI = InOutOperands.begin(), NE = InOutOperands.end(); NI != NE; ++NI) {
unsigned PrevBit = ~0;
unsigned Base = ~0;
unsigned PrevPos = ~0;
std::string Decoder = "";
for (unsigned bi = 0; bi < Bits.getNumBits(); ++bi) {
VarBitInit *BI = dynamic_cast<VarBitInit*>(Bits.getBit(bi));
if (!BI) continue;
VarInit *Var = dynamic_cast<VarInit*>(BI->getVariable());
assert(Var);
unsigned CurrBit = BI->getBitNum();
if (Var->getName() != NI->second) continue;
if (Base == ~0U && PrevBit == ~0U && PrevPos == ~0U) {
if (CurrBit == 0)
Base = bi;
else
continue;
}
if ((PrevPos != ~0U && bi-1 != PrevPos) ||
(CurrBit != ~0U && CurrBit-1 != PrevBit)) {
PrevBit = ~0;
Base = ~0;
PrevPos = ~0;
}
PrevPos = bi;
PrevBit = CurrBit;
TypedInit *TI = dynamic_cast<TypedInit*>(NI->first);
RecordRecTy *Type = dynamic_cast<RecordRecTy*>(TI->getType());
Record *TypeRecord = Type->getRecord();
bool isReg = false;
if (TypeRecord->isSubClassOf("RegisterClass")) {
Decoder = "Decode" + Type->getRecord()->getName() + "RegisterClass";
isReg = true;
}
RecordVal *DecoderString = TypeRecord->getValue("DecoderMethod");
StringInit *String = DecoderString ?
dynamic_cast<StringInit*>(DecoderString->getValue()) :
0;
if (!isReg && String && String->getValue() != "")
Decoder = String->getValue();
}
if (Base != ~0U) {
InsnOperands.push_back(OperandInfo(Base, PrevBit+1, Decoder));
DEBUG(errs() << "ENCODED OPERAND: $" << NI->second << " @ ("
<< utostr(Base+PrevBit) << ", " << utostr(Base) << ")\n");
}
}
Operands[Opc] = InsnOperands;
#if 0
DEBUG({
dumpBits(errs(), Bits);
errs() << '\n';
for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
const CGIOperandList::OperandInfo &Info = CGI.Operands[i];
const std::string &OperandName = Info.Name;
const Record &OperandDef = *Info.Rec;
errs() << "\t" << OperandName << " (" << OperandDef.getName() << ")\n";
}
});
#endif
return true;
}
void FixedLenDecoderEmitter::populateInstructions() {
for (unsigned i = 0, e = NumberedInstructions.size(); i < e; ++i) {
Record *R = NumberedInstructions[i]->TheDef;
if (R->getValueAsString("Namespace") == "TargetOpcode")
continue;
if (populateInstruction(*NumberedInstructions[i], i))
Opcodes.push_back(i);
}
}
void FixedLenDecoderEmitter::run(raw_ostream &o)
{
o << "#include \"llvm/MC/MCInst.h\"\n";
o << "#include \"llvm/Support/DataTypes.h\"\n";
o << "#include <assert.h>\n";
o << '\n';
o << "namespace llvm {\n\n";
NumberedInstructions = Target.getInstructionsByEnumValue();
populateInstructions();
FilterChooser FC(NumberedInstructions, Opcodes, Operands);
FC.emitTop(o, 0);
o << "\n} // End llvm namespace \n";
}