#include "CodeGenTarget.h"
#include "X86RecognizableInstr.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/TableGenBackend.h"
using namespace llvm;
using namespace X86Disassembler;
namespace {
enum UnfoldStrategy {
UNFOLD, NO_UNFOLD, NO_STRATEGY };
struct ManualMapEntry {
const char *RegInstStr;
const char *MemInstStr;
UnfoldStrategy Strategy;
ManualMapEntry(const char *RegInstStr, const char *MemInstStr,
UnfoldStrategy Strategy = NO_STRATEGY)
: RegInstStr(RegInstStr), MemInstStr(MemInstStr), Strategy(Strategy) {}
};
const char *ExplicitAlign[] = {"MOVDQA", "MOVAPS", "MOVAPD", "MOVNTPS",
"MOVNTPD", "MOVNTDQ", "MOVNTDQA"};
const char *ExplicitUnalign[] = {"MOVDQU", "MOVUPS", "MOVUPD",
"PCMPESTRM", "PCMPESTRI",
"PCMPISTRM", "PCMPISTRI" };
const ManualMapEntry ManualMapSet[] = {
{ "ADD16ri_DB", "ADD16mi", NO_UNFOLD },
{ "ADD16ri8_DB", "ADD16mi8", NO_UNFOLD },
{ "ADD16rr_DB", "ADD16mr", NO_UNFOLD },
{ "ADD32ri_DB", "ADD32mi", NO_UNFOLD },
{ "ADD32ri8_DB", "ADD32mi8", NO_UNFOLD },
{ "ADD32rr_DB", "ADD32mr", NO_UNFOLD },
{ "ADD64ri32_DB", "ADD64mi32", NO_UNFOLD },
{ "ADD64ri8_DB", "ADD64mi8", NO_UNFOLD },
{ "ADD64rr_DB", "ADD64mr", NO_UNFOLD },
{ "ADD8ri_DB", "ADD8mi", NO_UNFOLD },
{ "ADD8rr_DB", "ADD8mr", NO_UNFOLD },
{ "ADD16rr_DB", "ADD16rm", NO_UNFOLD },
{ "ADD32rr_DB", "ADD32rm", NO_UNFOLD },
{ "ADD64rr_DB", "ADD64rm", NO_UNFOLD },
{ "ADD8rr_DB", "ADD8rm", NO_UNFOLD },
{ "MMX_MOVD64from64rr", "MMX_MOVQ64mr", UNFOLD },
{ "MMX_MOVD64grr", "MMX_MOVD64mr", UNFOLD },
{ "MOVLHPSrr", "MOVHPSrm", NO_UNFOLD },
{ "PUSH16r", "PUSH16rmm", UNFOLD },
{ "PUSH32r", "PUSH32rmm", UNFOLD },
{ "PUSH64r", "PUSH64rmm", UNFOLD },
{ "TAILJMPr", "TAILJMPm", UNFOLD },
{ "TAILJMPr64", "TAILJMPm64", UNFOLD },
{ "TAILJMPr64_REX", "TAILJMPm64_REX", UNFOLD },
{ "VMOVLHPSZrr", "VMOVHPSZ128rm", NO_UNFOLD },
{ "VMOVLHPSrr", "VMOVHPSrm", NO_UNFOLD },
};
static bool isExplicitAlign(const CodeGenInstruction *Inst) {
return any_of(ExplicitAlign, [Inst](const char *InstStr) {
return Inst->TheDef->getName().contains(InstStr);
});
}
static bool isExplicitUnalign(const CodeGenInstruction *Inst) {
return any_of(ExplicitUnalign, [Inst](const char *InstStr) {
return Inst->TheDef->getName().contains(InstStr);
});
}
class X86FoldTablesEmitter {
RecordKeeper &Records;
CodeGenTarget Target;
class X86FoldTableEntry {
const CodeGenInstruction *RegInst;
const CodeGenInstruction *MemInst;
public:
bool CannotUnfold = false;
bool IsLoad = false;
bool IsStore = false;
bool IsAligned = false;
unsigned int Alignment = 0;
X86FoldTableEntry(const CodeGenInstruction *RegInst,
const CodeGenInstruction *MemInst)
: RegInst(RegInst), MemInst(MemInst) {}
void print(formatted_raw_ostream &OS) const {
OS.indent(2);
OS << "{ X86::" << RegInst->TheDef->getName() << ",";
OS.PadToColumn(40);
OS << "X86::" << MemInst->TheDef->getName() << ",";
OS.PadToColumn(75);
std::string Attrs;
if (IsLoad)
Attrs += "TB_FOLDED_LOAD | ";
if (IsStore)
Attrs += "TB_FOLDED_STORE | ";
if (CannotUnfold)
Attrs += "TB_NO_REVERSE | ";
if (IsAligned)
Attrs += "TB_ALIGN_" + std::to_string(Alignment) + " | ";
StringRef SimplifiedAttrs = StringRef(Attrs).rtrim("| ");
if (SimplifiedAttrs.empty())
SimplifiedAttrs = "0";
OS << SimplifiedAttrs << " },\n";
}
bool operator<(const X86FoldTableEntry &RHS) const {
bool LHSpseudo = RegInst->TheDef->getValueAsBit("isPseudo");
bool RHSpseudo = RHS.RegInst->TheDef->getValueAsBit("isPseudo");
if (LHSpseudo != RHSpseudo)
return LHSpseudo;
return RegInst->TheDef->getName() < RHS.RegInst->TheDef->getName();
}
};
typedef std::vector<X86FoldTableEntry> FoldTable;
FoldTable Table2Addr;
FoldTable Table0;
FoldTable Table1;
FoldTable Table2;
FoldTable Table3;
FoldTable Table4;
public:
X86FoldTablesEmitter(RecordKeeper &R) : Records(R), Target(R) {}
void run(formatted_raw_ostream &OS);
private:
void updateTables(const CodeGenInstruction *RegInstr,
const CodeGenInstruction *MemInstr,
const UnfoldStrategy S = NO_STRATEGY);
void addEntryWithFlags(FoldTable &Table, const CodeGenInstruction *RegInstr,
const CodeGenInstruction *MemInstr,
const UnfoldStrategy S, const unsigned int FoldedInd);
void printTable(const FoldTable &Table, StringRef TableName,
formatted_raw_ostream &OS) {
OS << "static const X86MemoryFoldTableEntry MemoryFold" << TableName
<< "[] = {\n";
for (const X86FoldTableEntry &E : Table)
E.print(OS);
OS << "};\n\n";
}
};
static bool hasRSTRegClass(const CodeGenInstruction *Inst) {
return any_of(Inst->Operands, [](const CGIOperandList::OperandInfo &OpIn) {
return OpIn.Rec->getName() == "RST" || OpIn.Rec->getName() == "RSTi";
});
}
static bool hasPtrTailcallRegClass(const CodeGenInstruction *Inst) {
return any_of(Inst->Operands, [](const CGIOperandList::OperandInfo &OpIn) {
return OpIn.Rec->getName() == "ptr_rc_tailcall";
});
}
static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
assert(B->getNumBits() <= sizeof(uint64_t) * 8 && "BitInits' too long!");
uint64_t Value = 0;
for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
BitInit *Bit = cast<BitInit>(B->getBit(i));
Value |= uint64_t(Bit->getValue()) << i;
}
return Value;
}
static inline bool hasRegisterFormat(const Record *Inst) {
const BitsInit *FormBits = Inst->getValueAsBitsInit("FormBits");
uint64_t FormBitsNum = getValueFromBitsInit(FormBits);
return FormBitsNum >= X86Local::MRMDestReg && FormBitsNum <= X86Local::MRM7r;
}
static inline bool hasMemoryFormat(const Record *Inst) {
const BitsInit *FormBits = Inst->getValueAsBitsInit("FormBits");
uint64_t FormBitsNum = getValueFromBitsInit(FormBits);
return FormBitsNum >= X86Local::MRMDestMem && FormBitsNum <= X86Local::MRM7m;
}
static inline bool isNOREXRegClass(const Record *Op) {
return Op->getName().contains("_NOREX");
}
static inline const CodeGenInstruction *
getAltRegInst(const CodeGenInstruction *I, const RecordKeeper &Records,
const CodeGenTarget &Target) {
StringRef AltRegInstStr = I->TheDef->getValueAsString("FoldGenRegForm");
Record *AltRegInstRec = Records.getDef(AltRegInstStr);
assert(AltRegInstRec &&
"Alternative register form instruction def not found");
CodeGenInstruction &AltRegInst = Target.getInstruction(AltRegInstRec);
return &AltRegInst;
}
class IsMatch {
const CodeGenInstruction *MemInst;
unsigned Variant;
public:
IsMatch(const CodeGenInstruction *Inst, unsigned V)
: MemInst(Inst), Variant(V) {}
bool operator()(const CodeGenInstruction *RegInst) {
X86Disassembler::RecognizableInstrBase RegRI(*RegInst);
X86Disassembler::RecognizableInstrBase MemRI(*MemInst);
const Record *RegRec = RegInst->TheDef;
const Record *MemRec = MemInst->TheDef;
if (RegRI.HasEVEX_B != 0 || MemRI.HasEVEX_B != 0)
return false;
if (!areOppositeForms(RegRI.Form, MemRI.Form))
return false;
if (X86Disassembler::getMnemonic(MemInst, Variant) !=
X86Disassembler::getMnemonic(RegInst, Variant))
return false;
if (RegRI.Encoding != MemRI.Encoding || RegRI.Opcode != MemRI.Opcode ||
RegRI.OpPrefix != MemRI.OpPrefix || RegRI.OpMap != MemRI.OpMap ||
RegRI.OpSize != MemRI.OpSize || RegRI.AdSize != MemRI.AdSize ||
RegRI.HasREX_W != MemRI.HasREX_W ||
RegRI.HasVEX_4V != MemRI.HasVEX_4V ||
RegRI.HasVEX_L != MemRI.HasVEX_L ||
RegRI.HasVEX_W != MemRI.HasVEX_W ||
RegRI.IgnoresVEX_L != MemRI.IgnoresVEX_L ||
RegRI.IgnoresVEX_W != MemRI.IgnoresVEX_W ||
RegRI.HasEVEX_K != MemRI.HasEVEX_K ||
RegRI.HasEVEX_KZ != MemRI.HasEVEX_KZ ||
RegRI.HasEVEX_L2 != MemRI.HasEVEX_L2 ||
RegRec->getValueAsBit("hasEVEX_RC") !=
MemRec->getValueAsBit("hasEVEX_RC") ||
RegRec->getValueAsBit("hasLockPrefix") !=
MemRec->getValueAsBit("hasLockPrefix") ||
RegRec->getValueAsBit("hasNoTrackPrefix") !=
MemRec->getValueAsBit("hasNoTrackPrefix") ||
RegRec->getValueAsBit("EVEX_W1_VEX_W0") !=
MemRec->getValueAsBit("EVEX_W1_VEX_W0"))
return false;
bool ArgFolded = false;
unsigned MemOutSize = MemRec->getValueAsDag("OutOperandList")->getNumArgs();
unsigned RegOutSize = RegRec->getValueAsDag("OutOperandList")->getNumArgs();
unsigned MemInSize = MemRec->getValueAsDag("InOperandList")->getNumArgs();
unsigned RegInSize = RegRec->getValueAsDag("InOperandList")->getNumArgs();
unsigned RegStartIdx =
(MemOutSize + 1 == RegOutSize) && (MemInSize == RegInSize) ? 1 : 0;
for (unsigned i = 0, e = MemInst->Operands.size(); i < e; i++) {
Record *MemOpRec = MemInst->Operands[i].Rec;
Record *RegOpRec = RegInst->Operands[i + RegStartIdx].Rec;
if (MemOpRec == RegOpRec)
continue;
if (isRegisterOperand(MemOpRec) && isRegisterOperand(RegOpRec)) {
if (getRegOperandSize(MemOpRec) != getRegOperandSize(RegOpRec) ||
isNOREXRegClass(MemOpRec) != isNOREXRegClass(RegOpRec))
return false;
} else if (isMemoryOperand(MemOpRec) && isMemoryOperand(RegOpRec)) {
if (getMemOperandSize(MemOpRec) != getMemOperandSize(RegOpRec))
return false;
} else if (isImmediateOperand(MemOpRec) && isImmediateOperand(RegOpRec)) {
if (MemOpRec->getValueAsDef("Type") != RegOpRec->getValueAsDef("Type"))
return false;
} else {
if (ArgFolded)
return false;
assert(isRegisterOperand(RegOpRec) && isMemoryOperand(MemOpRec));
ArgFolded = true;
}
}
return true;
}
private:
bool areOppositeForms(unsigned RegForm, unsigned MemForm) {
if ((MemForm == X86Local::MRM0m && RegForm == X86Local::MRM0r) ||
(MemForm == X86Local::MRM1m && RegForm == X86Local::MRM1r) ||
(MemForm == X86Local::MRM2m && RegForm == X86Local::MRM2r) ||
(MemForm == X86Local::MRM3m && RegForm == X86Local::MRM3r) ||
(MemForm == X86Local::MRM4m && RegForm == X86Local::MRM4r) ||
(MemForm == X86Local::MRM5m && RegForm == X86Local::MRM5r) ||
(MemForm == X86Local::MRM6m && RegForm == X86Local::MRM6r) ||
(MemForm == X86Local::MRM7m && RegForm == X86Local::MRM7r) ||
(MemForm == X86Local::MRMXm && RegForm == X86Local::MRMXr) ||
(MemForm == X86Local::MRMXmCC && RegForm == X86Local::MRMXrCC) ||
(MemForm == X86Local::MRMDestMem && RegForm == X86Local::MRMDestReg) ||
(MemForm == X86Local::MRMSrcMem && RegForm == X86Local::MRMSrcReg) ||
(MemForm == X86Local::MRMSrcMem4VOp3 &&
RegForm == X86Local::MRMSrcReg4VOp3) ||
(MemForm == X86Local::MRMSrcMemOp4 &&
RegForm == X86Local::MRMSrcRegOp4) ||
(MemForm == X86Local::MRMSrcMemCC && RegForm == X86Local::MRMSrcRegCC))
return true;
return false;
}
};
}
void X86FoldTablesEmitter::addEntryWithFlags(FoldTable &Table,
const CodeGenInstruction *RegInstr,
const CodeGenInstruction *MemInstr,
const UnfoldStrategy S,
const unsigned int FoldedInd) {
X86FoldTableEntry Result = X86FoldTableEntry(RegInstr, MemInstr);
Record *RegRec = RegInstr->TheDef;
Record *MemRec = MemInstr->TheDef;
if (&Table == &Table0) {
unsigned MemInOpsNum = MemRec->getValueAsDag("InOperandList")->getNumArgs();
unsigned RegInOpsNum = RegRec->getValueAsDag("InOperandList")->getNumArgs();
if (MemInOpsNum == RegInOpsNum)
Result.IsLoad = true;
else
Result.IsStore = true;
}
Record *RegOpRec = RegInstr->Operands[FoldedInd].Rec;
Record *MemOpRec = MemInstr->Operands[FoldedInd].Rec;
if (S == UNFOLD)
Result.CannotUnfold = false;
else if (S == NO_UNFOLD)
Result.CannotUnfold = true;
else if (getRegOperandSize(RegOpRec) > getMemOperandSize(MemOpRec))
Result.CannotUnfold = true;
uint64_t Enc = getValueFromBitsInit(RegRec->getValueAsBitsInit("OpEncBits"));
if (isExplicitAlign(RegInstr)) {
BitsInit *VectSize = RegRec->getValueAsBitsInit("VectSize");
uint64_t Value = getValueFromBitsInit(VectSize);
Result.IsAligned = true;
Result.Alignment = Value;
} else if (Enc != X86Local::XOP && Enc != X86Local::VEX &&
Enc != X86Local::EVEX) {
if (!isExplicitUnalign(RegInstr) && getMemOperandSize(MemOpRec) > 64) {
Result.IsAligned = true;
Result.Alignment = 16;
}
}
Table.push_back(Result);
}
void X86FoldTablesEmitter::updateTables(const CodeGenInstruction *RegInstr,
const CodeGenInstruction *MemInstr,
const UnfoldStrategy S) {
Record *RegRec = RegInstr->TheDef;
Record *MemRec = MemInstr->TheDef;
unsigned MemOutSize = MemRec->getValueAsDag("OutOperandList")->getNumArgs();
unsigned RegOutSize = RegRec->getValueAsDag("OutOperandList")->getNumArgs();
unsigned MemInSize = MemRec->getValueAsDag("InOperandList")->getNumArgs();
unsigned RegInSize = RegRec->getValueAsDag("InOperandList")->getNumArgs();
if (MemOutSize != RegOutSize && MemInSize == RegInSize) {
addEntryWithFlags(Table2Addr, RegInstr, MemInstr, S, 0);
return;
}
if (MemInSize == RegInSize && MemOutSize == RegOutSize) {
for (unsigned i = RegOutSize, e = RegInstr->Operands.size(); i < e; i++) {
Record *RegOpRec = RegInstr->Operands[i].Rec;
Record *MemOpRec = MemInstr->Operands[i].Rec;
if ((isRegisterOperand(RegOpRec) ||
RegOpRec->isSubClassOf("PointerLikeRegClass")) &&
isMemoryOperand(MemOpRec)) {
switch (i) {
case 0:
addEntryWithFlags(Table0, RegInstr, MemInstr, S, 0);
return;
case 1:
addEntryWithFlags(Table1, RegInstr, MemInstr, S, 1);
return;
case 2:
addEntryWithFlags(Table2, RegInstr, MemInstr, S, 2);
return;
case 3:
addEntryWithFlags(Table3, RegInstr, MemInstr, S, 3);
return;
case 4:
addEntryWithFlags(Table4, RegInstr, MemInstr, S, 4);
return;
}
}
}
} else if (MemInSize == RegInSize + 1 && MemOutSize + 1 == RegOutSize) {
Record *RegOpRec = RegInstr->Operands[RegOutSize - 1].Rec;
Record *MemOpRec = MemInstr->Operands[RegOutSize - 1].Rec;
if (isRegisterOperand(RegOpRec) && isMemoryOperand(MemOpRec) &&
getRegOperandSize(RegOpRec) == getMemOperandSize(MemOpRec))
addEntryWithFlags(Table0, RegInstr, MemInstr, S, 0);
}
}
void X86FoldTablesEmitter::run(formatted_raw_ostream &OS) {
emitSourceFileHeader("X86 fold tables", OS);
std::vector<const CodeGenInstruction *> MemInsts;
std::map<uint8_t, std::vector<const CodeGenInstruction *>> RegInsts;
ArrayRef<const CodeGenInstruction *> NumberedInstructions =
Target.getInstructionsByEnumValue();
for (const CodeGenInstruction *Inst : NumberedInstructions) {
const Record *Rec = Inst->TheDef;
if (!Rec->isSubClassOf("X86Inst") || Rec->getValueAsBit("isAsmParserOnly"))
continue;
if (Rec->getValueAsBit("isMemoryFoldable") == false ||
hasRSTRegClass(Inst) || hasPtrTailcallRegClass(Inst))
continue;
if (hasMemoryFormat(Rec))
MemInsts.push_back(Inst);
else if (hasRegisterFormat(Rec)) {
uint8_t Opc = getValueFromBitsInit(Rec->getValueAsBitsInit("Opcode"));
RegInsts[Opc].push_back(Inst);
}
}
Record *AsmWriter = Target.getAsmWriter();
unsigned Variant = AsmWriter->getValueAsInt("Variant");
for (const CodeGenInstruction *MemInst : MemInsts) {
uint8_t Opc =
getValueFromBitsInit(MemInst->TheDef->getValueAsBitsInit("Opcode"));
auto RegInstsIt = RegInsts.find(Opc);
if (RegInstsIt == RegInsts.end())
continue;
std::vector<const CodeGenInstruction *> &OpcRegInsts = RegInstsIt->second;
auto Match = find_if(OpcRegInsts, IsMatch(MemInst, Variant));
if (Match != OpcRegInsts.end()) {
const CodeGenInstruction *RegInst = *Match;
if (RegInst->TheDef->isValueUnset("FoldGenRegForm")) {
updateTables(RegInst, MemInst);
} else {
const CodeGenInstruction *AltRegInst =
getAltRegInst(RegInst, Records, Target);
updateTables(AltRegInst, MemInst);
}
OpcRegInsts.erase(Match);
}
}
for (const ManualMapEntry &Entry : ManualMapSet) {
Record *RegInstIter = Records.getDef(Entry.RegInstStr);
Record *MemInstIter = Records.getDef(Entry.MemInstStr);
updateTables(&(Target.getInstruction(RegInstIter)),
&(Target.getInstruction(MemInstIter)), Entry.Strategy);
}
llvm::sort(Table2Addr);
llvm::sort(Table0);
llvm::sort(Table1);
llvm::sort(Table2);
llvm::sort(Table3);
llvm::sort(Table4);
printTable(Table2Addr, "Table2Addr", OS);
printTable(Table0, "Table0", OS);
printTable(Table1, "Table1", OS);
printTable(Table2, "Table2", OS);
printTable(Table3, "Table3", OS);
printTable(Table4, "Table4", OS);
}
namespace llvm {
void EmitX86FoldTables(RecordKeeper &RK, raw_ostream &o) {
formatted_raw_ostream OS(o);
X86FoldTablesEmitter(RK).run(OS);
}
}