ARMDecoderEmitter.cpp [plain text]
#define DEBUG_TYPE "arm-decoder-emitter"
#include "ARMDecoderEmitter.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;
#define ARM_FORMATS \
ENTRY(ARM_FORMAT_PSEUDO, 0) \
ENTRY(ARM_FORMAT_MULFRM, 1) \
ENTRY(ARM_FORMAT_BRFRM, 2) \
ENTRY(ARM_FORMAT_BRMISCFRM, 3) \
ENTRY(ARM_FORMAT_DPFRM, 4) \
ENTRY(ARM_FORMAT_DPSOREGFRM, 5) \
ENTRY(ARM_FORMAT_LDFRM, 6) \
ENTRY(ARM_FORMAT_STFRM, 7) \
ENTRY(ARM_FORMAT_LDMISCFRM, 8) \
ENTRY(ARM_FORMAT_STMISCFRM, 9) \
ENTRY(ARM_FORMAT_LDSTMULFRM, 10) \
ENTRY(ARM_FORMAT_LDSTEXFRM, 11) \
ENTRY(ARM_FORMAT_ARITHMISCFRM, 12) \
ENTRY(ARM_FORMAT_SATFRM, 13) \
ENTRY(ARM_FORMAT_EXTFRM, 14) \
ENTRY(ARM_FORMAT_VFPUNARYFRM, 15) \
ENTRY(ARM_FORMAT_VFPBINARYFRM, 16) \
ENTRY(ARM_FORMAT_VFPCONV1FRM, 17) \
ENTRY(ARM_FORMAT_VFPCONV2FRM, 18) \
ENTRY(ARM_FORMAT_VFPCONV3FRM, 19) \
ENTRY(ARM_FORMAT_VFPCONV4FRM, 20) \
ENTRY(ARM_FORMAT_VFPCONV5FRM, 21) \
ENTRY(ARM_FORMAT_VFPLDSTFRM, 22) \
ENTRY(ARM_FORMAT_VFPLDSTMULFRM, 23) \
ENTRY(ARM_FORMAT_VFPMISCFRM, 24) \
ENTRY(ARM_FORMAT_THUMBFRM, 25) \
ENTRY(ARM_FORMAT_MISCFRM, 26) \
ENTRY(ARM_FORMAT_NEONGETLNFRM, 27) \
ENTRY(ARM_FORMAT_NEONSETLNFRM, 28) \
ENTRY(ARM_FORMAT_NEONDUPFRM, 29) \
ENTRY(ARM_FORMAT_NLdSt, 30) \
ENTRY(ARM_FORMAT_N1RegModImm, 31) \
ENTRY(ARM_FORMAT_N2Reg, 32) \
ENTRY(ARM_FORMAT_NVCVT, 33) \
ENTRY(ARM_FORMAT_NVecDupLn, 34) \
ENTRY(ARM_FORMAT_N2RegVecShL, 35) \
ENTRY(ARM_FORMAT_N2RegVecShR, 36) \
ENTRY(ARM_FORMAT_N3Reg, 37) \
ENTRY(ARM_FORMAT_N3RegVecSh, 38) \
ENTRY(ARM_FORMAT_NVecExtract, 39) \
ENTRY(ARM_FORMAT_NVecMulScalar, 40) \
ENTRY(ARM_FORMAT_NVTBL, 41)
#define ENTRY(n, v) n = v,
typedef enum {
ARM_FORMATS
ARM_FORMAT_NA
} ARMFormat;
#undef ENTRY
static const char *stringForARMFormat(ARMFormat form) {
#define ENTRY(n, v) case n: return #n;
switch(form) {
ARM_FORMATS
case ARM_FORMAT_NA:
default:
return "";
}
#undef ENTRY
}
enum {
IndexModeNone = 0,
IndexModePre = 1,
IndexModePost = 2,
IndexModeUpd = 3
};
static uint8_t byteFromBitsInit(BitsInit &init) {
int width = init.getNumBits();
assert(width <= 8 && "Field is too large for uint8_t!");
int index;
uint8_t mask = 0x01;
uint8_t ret = 0;
for (index = 0; index < width; index++) {
if (static_cast<BitInit*>(init.getBit(index))->getValue())
ret |= mask;
mask <<= 1;
}
return ret;
}
static uint8_t getByteField(const Record &def, const char *str) {
BitsInit *bits = def.getValueAsBitsInit(str);
return byteFromBitsInit(*bits);
}
static BitsInit &getBitsField(const Record &def, const char *str) {
BitsInit *bits = def.getValueAsBitsInit(str);
return *bits;
}
static
bool sameStringExceptSuffix(const StringRef LHS, const StringRef RHS,
const StringRef Suffix) {
if (RHS.startswith(LHS) && RHS.endswith(Suffix))
return RHS.size() == LHS.size() + Suffix.size();
return false;
}
static bool thumbInstruction(uint8_t Form) {
return Form == ARM_FORMAT_THUMBFRM;
}
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");
}
}
}
typedef enum {
TARGET_ARM = 0,
TARGET_THUMB
} TARGET_NAME_t;
#define BIT_WIDTH 32
class ARMFilterChooser;
typedef bit_value_t insn_t[BIT_WIDTH];
class ARMFilter {
protected:
ARMFilterChooser *Owner; unsigned StartBit; unsigned NumBits; bool Mixed;
std::map<uint64_t, std::vector<unsigned> > FilteredInstructions;
std::vector<unsigned> VariableInstructions;
std::map<unsigned, ARMFilterChooser*> FilterChooserMap;
unsigned NumFiltered;
unsigned LastOpcFiltered;
unsigned NumVariable;
public:
unsigned getNumFiltered() { return NumFiltered; }
unsigned getNumVariable() { return NumVariable; }
unsigned getSingletonOpc() {
assert(NumFiltered == 1);
return LastOpcFiltered;
}
ARMFilterChooser &getVariableFC() {
assert(NumFiltered == 1);
assert(FilterChooserMap.size() == 1);
return *(FilterChooserMap.find((unsigned)-1)->second);
}
ARMFilter(const ARMFilter &f);
ARMFilter(ARMFilterChooser &owner, unsigned startBit, unsigned numBits,
bool mixed);
~ARMFilter();
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 ARMFilterChooser {
static TARGET_NAME_t TargetName;
protected:
friend class ARMFilter;
const std::vector<const CodeGenInstruction*> &AllInstructions;
const std::vector<unsigned> Opcodes;
std::vector<ARMFilter> Filters;
bit_value_t FilterBitValues[BIT_WIDTH];
ARMFilterChooser *Parent;
int BestIndex;
public:
static void setTargetName(TARGET_NAME_t tn) { TargetName = tn; }
ARMFilterChooser(const ARMFilterChooser &FC) :
AllInstructions(FC.AllInstructions), Opcodes(FC.Opcodes),
Filters(FC.Filters), Parent(FC.Parent), BestIndex(FC.BestIndex) {
memcpy(FilterBitValues, FC.FilterBitValues, sizeof(FilterBitValues));
}
ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
const std::vector<unsigned> &IDs) :
AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(NULL),
BestIndex(-1) {
for (unsigned i = 0; i < BIT_WIDTH; ++i)
FilterBitValues[i] = BIT_UNFILTERED;
doFilter();
}
ARMFilterChooser(const std::vector<const CodeGenInstruction*> &Insts,
const std::vector<unsigned> &IDs,
bit_value_t (&ParentFilterBitValues)[BIT_WIDTH],
ARMFilterChooser &parent) :
AllInstructions(Insts), Opcodes(IDs), Filters(), Parent(&parent),
BestIndex(-1) {
for (unsigned i = 0; i < BIT_WIDTH; ++i)
FilterBitValues[i] = ParentFilterBitValues[i];
doFilter();
}
bool isTopLevel() { return Parent == NULL; }
void emitTopHook(raw_ostream &o);
void emitTop(raw_ostream &o, unsigned &Indentation);
void emitBot(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);
Record *R = AllInstructions[Opcode]->TheDef;
if (R->getValue("IndexModeBits") &&
getByteField(*R, "IndexModeBits") == IndexModeUpd)
Insn[21] = BIT_TRUE;
}
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);
ARMFilter &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 LdStCopEncoding1(unsigned Opc) {
const std::string &Name = nameWithID(Opc);
if (Name == "LDC_OFFSET" || Name == "LDC_OPTION" ||
Name == "LDC_POST" || Name == "LDC_PRE" ||
Name == "LDCL_OFFSET" || Name == "LDCL_OPTION" ||
Name == "LDCL_POST" || Name == "LDCL_PRE" ||
Name == "STC_OFFSET" || Name == "STC_OPTION" ||
Name == "STC_POST" || Name == "STC_PRE" ||
Name == "STCL_OFFSET" || Name == "STCL_OPTION" ||
Name == "STCL_POST" || Name == "STCL_PRE")
return true;
else
return false;
}
bool emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,unsigned Opc);
void emitSingletonDecoder(raw_ostream &o, unsigned &Indentation,
ARMFilter &Best);
void runSingleFilter(ARMFilterChooser &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);
};
ARMFilter::ARMFilter(const ARMFilter &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) {
}
ARMFilter::ARMFilter(ARMFilterChooser &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");
}
ARMFilter::~ARMFilter() {
std::map<unsigned, ARMFilterChooser*>::iterator filterIterator;
for (filterIterator = FilterChooserMap.begin();
filterIterator != FilterChooserMap.end();
filterIterator++) {
delete filterIterator->second;
}
}
void ARMFilter::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, ARMFilterChooser*>(
(unsigned)-1,
new ARMFilterChooser(Owner->AllInstructions,
VariableInstructions,
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, ARMFilterChooser*>(
mapIterator->first,
new ARMFilterChooser(Owner->AllInstructions,
mapIterator->second,
BitValueArray,
*Owner)
));
}
}
void ARMFilter::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, ARMFilterChooser*>::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 ARMFilter::usefulness() const {
if (VariableInstructions.size())
return FilteredInstructions.size();
else
return FilteredInstructions.size() + 1;
}
TARGET_NAME_t ARMFilterChooser::TargetName;
void ARMFilterChooser::emitTopHook(raw_ostream &o) {
if (TargetName == TARGET_ARM) {
o << "static const ARMFormat ARMFormats[] = {\n";
for (unsigned i = 0, e = AllInstructions.size(); i != e; ++i) {
const Record &Def = *(AllInstructions[i]->TheDef);
const std::string &Name = Def.getName();
if (Def.isSubClassOf("InstARM") || Def.isSubClassOf("InstThumb"))
o.indent(2) <<
stringForARMFormat((ARMFormat)getByteField(Def, "Form"));
else
o << " ARM_FORMAT_NA";
o << ",\t// Inst #" << i << " = " << Name << '\n';
}
o << " ARM_FORMAT_NA\t// Unreachable.\n";
o << "};\n\n";
}
}
void ARMFilterChooser::emitTop(raw_ostream &o, unsigned &Indentation) {
emitTopHook(o);
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 uint16_t decodeInstruction(field_t insn) {\n";
++Indentation; ++Indentation;
emit(o, Indentation);
o << '\n';
o.indent(Indentation) << "return 0;\n";
--Indentation; --Indentation;
o.indent(Indentation) << "}\n";
o << '\n';
}
void ARMFilterChooser::emitBot(raw_ostream &o, unsigned &Indentation) {
if (TargetName != TARGET_THUMB) return;
o.indent(Indentation)
<< "static uint16_t decodeThumbInstruction(field_t insn) {\n";
++Indentation; ++Indentation;
emit(o, Indentation);
o << '\n';
o.indent(Indentation) << "return 0;\n";
--Indentation; --Indentation;
o.indent(Indentation) << "}\n";
}
bool ARMFilterChooser::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 ARMFilterChooser::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 ARMFilterChooser::dumpStack(raw_ostream &o, const char *prefix) {
ARMFilterChooser *current = this;
while (current) {
o << prefix;
dumpFilterArray(o, current->FilterBitValues);
o << '\n';
current = current->Parent;
}
}
void ARMFilterChooser::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 ARMFilterChooser::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 ARMFilterChooser::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);
if (TargetName == TARGET_ARM && LdStCopEncoding1(Opc)) {
o.indent(Indentation);
o << "if (fieldFromInstruction(insn, 9, 3) == 5) break; // fallthrough\n";
}
getIslands(StartBits, EndBits, FieldVals, Insn);
unsigned Size = StartBits.size();
unsigned I, NumBits;
if (Size == 0) {
o.indent(Indentation) << "return " << Opc << "; // " << nameWithID(Opc)
<< '\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) << " return " << Opc << "; // " << nameWithID(Opc)
<< '\n';
return false;
}
void ARMFilterChooser::emitSingletonDecoder(raw_ostream &o,
unsigned &Indentation,
ARMFilter &Best) {
unsigned Opc = Best.getSingletonOpc();
emitSingletonDecoder(o, Indentation, Opc);
o.indent(Indentation) << "else\n";
Indentation += 2;
Best.getVariableFC().emit(o, Indentation);
Indentation -= 2;
}
void ARMFilterChooser::runSingleFilter(ARMFilterChooser &owner,
unsigned startBit,
unsigned numBit, bool mixed) {
Filters.clear();
ARMFilter F(*this, startBit, numBit, true);
Filters.push_back(F);
BestIndex = 0; bestFilter().recurse();
}
void ARMFilterChooser::reportRegion(bitAttr_t RA, unsigned StartBit,
unsigned BitIndex, bool AllowMixed) {
if (RA == ATTR_MIXED && AllowMixed)
Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, true));
else if (RA == ATTR_ALL_SET && !AllowMixed)
Filters.push_back(ARMFilter(*this, StartBit, BitIndex - StartBit, false));
}
bool ARMFilterChooser::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 ARMFilterChooser::doFilter() {
unsigned Num = Opcodes.size();
assert(Num && "FilterChooser created with no instructions");
if (TargetName == TARGET_ARM && Parent == NULL) {
runSingleFilter(*this, 28, 4, false);
return;
}
if (filterProcessor(false))
return;
if (filterProcessor(true))
return;
if (Num == 3 && filterProcessor(true, false))
return;
BestIndex = -1;
}
bool ARMFilterChooser::emit(raw_ostream &o, unsigned &Indentation) {
if (Opcodes.size() == 1)
return emitSingletonDecoder(o, Indentation, Opcodes[0]);
if (BestIndex != -1) {
ARMFilter &Best = bestFilter();
if (Best.getNumFiltered() == 1)
emitSingletonDecoder(o, Indentation, Best);
else
bestFilter().emit(o, Indentation);
return false;
}
if ((TargetName == TARGET_ARM || TargetName == TARGET_THUMB) &&
Opcodes.size() == 2) {
const std::string &name1 = nameWithID(Opcodes[0]);
const std::string &name2 = nameWithID(Opcodes[1]);
if ((name1 == "VMOVDneon" && name2 == "VORRd") ||
(name1 == "VMOVQ" && name2 == "VORRq")) {
--Indentation; --Indentation;
o.indent(Indentation) << "{\n";
++Indentation; ++Indentation;
o.indent(Indentation)
<< "field_t N = fieldFromInstruction(insn, 7, 1), "
<< "M = fieldFromInstruction(insn, 5, 1);\n";
o.indent(Indentation)
<< "field_t Vn = fieldFromInstruction(insn, 16, 4), "
<< "Vm = fieldFromInstruction(insn, 0, 4);\n";
o.indent(Indentation)
<< "return (N == M && Vn == Vm) ? "
<< Opcodes[0] << " /* " << name1 << " */ : "
<< Opcodes[1] << " /* " << name2 << " */ ;\n";
--Indentation; --Indentation;
o.indent(Indentation) << "}\n";
++Indentation; ++Indentation;
return true;
}
if (name1 == "LDR" && name2 == "LDRcp") {
o.indent(Indentation)
<< "return " << Opcodes[0]
<< "; // Returning LDR for {LDR, LDRcp}\n";
return true;
}
if (name1 == "tLDMIA" && name2 == "tLDMIA_UPD") {
--Indentation; --Indentation;
o.indent(Indentation) << "{\n";
++Indentation; ++Indentation;
o.indent(Indentation)
<< "unsigned Rn = fieldFromInstruction(insn, 8, 3), "
<< "list = fieldFromInstruction(insn, 0, 8);\n";
o.indent(Indentation)
<< "return ((list >> Rn) & 1) == 0 ? "
<< Opcodes[1] << " /* " << name2 << " */ : "
<< Opcodes[0] << " /* " << name1 << " */ ;\n";
--Indentation; --Indentation;
o.indent(Indentation) << "}\n";
++Indentation; ++Indentation;
return true;
}
if (sameStringExceptSuffix(name1, name2, "_UPD")) {
o.indent(Indentation)
<< "return fieldFromInstruction(insn, 0, 4) == 15 ? " << Opcodes[0]
<< " /* " << name1 << " */ : " << Opcodes[1] << "/* " << name2
<< " */ ; // Advanced SIMD addressing mode\n";
return true;
}
}
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;
}
class ARMDecoderEmitter::ARMDEBackend {
public:
ARMDEBackend(ARMDecoderEmitter &frontend, RecordKeeper &Records) :
NumberedInstructions(),
Opcodes(),
Frontend(frontend),
Target(Records),
FC(NULL)
{
if (Target.getName() == "ARM")
TargetName = TARGET_ARM;
else {
errs() << "Target name " << Target.getName() << " not recognized\n";
assert(0 && "Unknown target");
}
populateInstructions();
}
~ARMDEBackend() {
if (FC) {
delete FC;
FC = NULL;
}
}
void getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
&NumberedInstructions) {
std::string Namespace = Target.getInstNamespace();
assert(!Namespace.empty() && "No instructions defined.");
NumberedInstructions = Target.getInstructionsByEnumValue();
}
bool populateInstruction(const CodeGenInstruction &CGI, TARGET_NAME_t TN);
void populateInstructions();
void emit(raw_ostream &o);
protected:
std::vector<const CodeGenInstruction*> NumberedInstructions;
std::vector<unsigned> Opcodes;
std::vector<unsigned> Opcodes2;
ARMDecoderEmitter &Frontend;
CodeGenTarget Target;
ARMFilterChooser *FC;
TARGET_NAME_t TargetName;
};
bool ARMDecoderEmitter::
ARMDEBackend::populateInstruction(const CodeGenInstruction &CGI,
TARGET_NAME_t TN) {
const Record &Def = *CGI.TheDef;
const StringRef Name = Def.getName();
uint8_t Form = getByteField(Def, "Form");
BitsInit &Bits = getBitsField(Def, "Inst");
if (Bits.allInComplete()) return false;
if (Def.getValueAsBit("isAsmParserOnly"))
return false;
if (TN == TARGET_ARM) {
if (Form == ARM_FORMAT_PSEUDO)
return false;
if (thumbInstruction(Form))
return false;
if (Name == "TCRETURNdi" || Name == "TCRETURNdiND" ||
Name == "TCRETURNri" || Name == "TCRETURNriND" ||
Name == "TAILJMPd" || Name == "TAILJMPdt" ||
Name == "TAILJMPdND" || Name == "TAILJMPdNDt" ||
Name == "TAILJMPr" || Name == "TAILJMPrND" ||
Name == "MOVr_TC")
return false;
if (Name == "ADR")
return false;
if (Name == "RSCSri" || Name == "RSCSrs") return false;
if (Name == "VEXTd16" || Name == "VEXTd32" || Name == "VEXTdf" ||
Name == "VEXTq16" || Name == "VEXTq32" || Name == "VEXTqf")
return false;
} else if (TN == TARGET_THUMB) {
if (!thumbInstruction(Form))
return false;
if (Name == "tSTMIA")
return false;
if (Name == "tBL" || Name == "tBLXi" || Name == "tBLXr")
return false;
if (Name == "tBX_RET" || Name == "tBX_RET_vararg")
return false;
if (Name == "tTPsoft" || Name == "t2TPsoft")
return false;
if (Name == "tADR")
return false;
if (Name == "t2ADR")
return false;
if (Name == "tADDrSP" || Name == "tADDspr" || Name == "tPICADD" ||
Name == "t2SUBrSPs" || Name == "t2ADDrSPs" ||
Name == "t2ADDrSPi" || Name == "t2SUBrSPi" ||
Name == "t2ADDrSPi12" || Name == "t2SUBrSPi12")
return false;
if (Name == "tLDRpci")
return false;
if (Name == "t2LDRDpci")
return false;
if (Name == "tBfar" ||
Name == "tPOP_RET" || Name == "t2LDMIA_RET" ||
Name == "tMOVCCi" || Name == "tMOVCCr" ||
Name == "tSpill" || Name == "tLDRcp" || Name == "tRestore" ||
Name == "t2MOVCCi16")
return false;
}
DEBUG({
switch (TargetName) {
case TARGET_ARM:
case TARGET_THUMB:
errs() << Name << " " << stringForARMFormat((ARMFormat)Form);
break;
}
errs() << " ";
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";
}
});
return true;
}
void ARMDecoderEmitter::ARMDEBackend::populateInstructions() {
getInstructionsByEnumValue(NumberedInstructions);
unsigned numUIDs = NumberedInstructions.size();
if (TargetName == TARGET_ARM) {
for (unsigned uid = 0; uid < numUIDs; uid++) {
if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM"))
continue;
if (populateInstruction(*NumberedInstructions[uid], TargetName))
Opcodes.push_back(uid);
}
for (unsigned uid = 0; uid < numUIDs; uid++) {
if (!NumberedInstructions[uid]->TheDef->isSubClassOf("InstARM")
&& !NumberedInstructions[uid]->TheDef->isSubClassOf("InstThumb"))
continue;
if (populateInstruction(*NumberedInstructions[uid], TARGET_THUMB))
Opcodes2.push_back(uid);
}
return;
}
for (unsigned uid = 0; uid < numUIDs; uid++) {
Record *R = NumberedInstructions[uid]->TheDef;
if (R->getValueAsString("Namespace") == "TargetOpcode")
continue;
if (populateInstruction(*NumberedInstructions[uid], TargetName))
Opcodes.push_back(uid);
}
}
void ARMDecoderEmitter::ARMDEBackend::emit(raw_ostream &o) {
switch (TargetName) {
case TARGET_ARM:
Frontend.EmitSourceFileHeader("ARM/Thumb Decoders", o);
break;
default:
assert(0 && "Unreachable code!");
}
o << "#include \"llvm/Support/DataTypes.h\"\n";
o << "#include <assert.h>\n";
o << '\n';
o << "namespace llvm {\n\n";
ARMFilterChooser::setTargetName(TargetName);
switch (TargetName) {
case TARGET_ARM: {
FC = new ARMFilterChooser(NumberedInstructions, Opcodes);
unsigned Indentation = 0;
FC->emitTop(o, Indentation);
delete FC;
ARMFilterChooser::setTargetName(TARGET_THUMB);
FC = new ARMFilterChooser(NumberedInstructions, Opcodes2);
Indentation = 0;
FC->emitBot(o, Indentation);
break;
}
default:
assert(0 && "Unreachable code!");
}
o << "\n} // End llvm namespace \n";
}
void ARMDecoderEmitter::initBackend()
{
Backend = new ARMDEBackend(*this, Records);
}
void ARMDecoderEmitter::run(raw_ostream &o)
{
Backend->emit(o);
}
void ARMDecoderEmitter::shutdownBackend()
{
delete Backend;
Backend = NULL;
}