2024-01-06 11:33:36 +08:00
|
|
|
//==- utils/TableGen/X86CompressEVEXTablesEmitter.cpp - X86 backend-*- C++ -*-//
|
2017-03-07 08:11:19 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-03-07 08:11:19 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
2024-01-06 11:33:36 +08:00
|
|
|
/// This tablegen backend is responsible for emitting the X86 backend EVEX
|
2017-03-07 08:11:19 +00:00
|
|
|
/// compression tables.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-03-09 22:28:44 +01:00
|
|
|
#include "CodeGenInstruction.h"
|
2017-03-07 08:11:19 +00:00
|
|
|
#include "CodeGenTarget.h"
|
2022-03-26 13:00:53 +08:00
|
|
|
#include "X86RecognizableInstr.h"
|
2017-03-07 08:11:19 +00:00
|
|
|
#include "llvm/TableGen/Error.h"
|
2023-02-17 00:28:07 +09:00
|
|
|
#include "llvm/TableGen/Record.h"
|
2017-03-07 08:11:19 +00:00
|
|
|
#include "llvm/TableGen/TableGenBackend.h"
|
2024-01-06 15:29:25 +08:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2017-03-07 08:11:19 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2022-03-27 08:41:17 +08:00
|
|
|
using namespace X86Disassembler;
|
2017-03-07 08:11:19 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2024-01-06 15:29:25 +08:00
|
|
|
const std::map<StringRef, StringRef> ManualMap = {
|
|
|
|
#define ENTRY(OLD, NEW) {#OLD, #NEW},
|
|
|
|
#include "X86ManualCompressEVEXTables.def"
|
|
|
|
};
|
|
|
|
const std::set<StringRef> NoCompressSet = {
|
|
|
|
#define NOCOMP(INSN) #INSN,
|
|
|
|
#include "X86ManualCompressEVEXTables.def"
|
|
|
|
};
|
|
|
|
|
2024-01-06 11:33:36 +08:00
|
|
|
class X86CompressEVEXTablesEmitter {
|
2018-06-19 04:24:44 +00:00
|
|
|
RecordKeeper &Records;
|
2017-03-07 08:11:19 +00:00
|
|
|
CodeGenTarget Target;
|
|
|
|
|
2024-01-06 11:33:36 +08:00
|
|
|
// Hold all pontentially compressible EVEX instructions
|
|
|
|
std::vector<const CodeGenInstruction *> PreCompressionInsts;
|
|
|
|
// Hold all compressed instructions. Divided into groups with same opcodes
|
2017-03-07 08:11:19 +00:00
|
|
|
// to make the search more efficient
|
2024-01-06 11:33:36 +08:00
|
|
|
std::map<uint64_t, std::vector<const CodeGenInstruction *>> CompressedInsts;
|
2017-03-07 08:11:19 +00:00
|
|
|
|
2023-11-28 11:33:05 +08:00
|
|
|
typedef std::pair<const CodeGenInstruction *, const CodeGenInstruction *>
|
|
|
|
Entry;
|
2017-03-07 08:11:19 +00:00
|
|
|
|
2024-01-06 18:30:37 +08:00
|
|
|
std::vector<Entry> Table;
|
2017-03-07 08:11:19 +00:00
|
|
|
|
|
|
|
public:
|
2024-01-06 11:33:36 +08:00
|
|
|
X86CompressEVEXTablesEmitter(RecordKeeper &R) : Records(R), Target(R) {}
|
2017-03-07 08:11:19 +00:00
|
|
|
|
2024-01-06 11:33:36 +08:00
|
|
|
// run - Output X86 EVEX compression tables.
|
2017-03-07 08:11:19 +00:00
|
|
|
void run(raw_ostream &OS);
|
|
|
|
|
|
|
|
private:
|
2024-01-06 11:33:36 +08:00
|
|
|
// Prints the given table as a C++ array of type X86CompressEVEXTableEntry
|
2017-03-07 08:11:19 +00:00
|
|
|
void printTable(const std::vector<Entry> &Table, raw_ostream &OS);
|
|
|
|
};
|
|
|
|
|
2024-01-06 11:33:36 +08:00
|
|
|
void X86CompressEVEXTablesEmitter::printTable(const std::vector<Entry> &Table,
|
|
|
|
raw_ostream &OS) {
|
2017-03-07 08:11:19 +00:00
|
|
|
|
2024-01-06 18:30:37 +08:00
|
|
|
OS << "static const X86CompressEVEXTableEntry X86CompressEVEXTable[] = { \n";
|
2017-03-07 08:11:19 +00:00
|
|
|
|
|
|
|
// Print all entries added to the table
|
2024-01-06 18:30:37 +08:00
|
|
|
for (const auto &Pair : Table)
|
2017-03-13 00:36:46 +00:00
|
|
|
OS << " { X86::" << Pair.first->TheDef->getName()
|
2017-03-07 08:11:19 +00:00
|
|
|
<< ", X86::" << Pair.second->TheDef->getName() << " },\n";
|
|
|
|
|
|
|
|
OS << "};\n\n";
|
|
|
|
}
|
|
|
|
|
2024-01-06 21:38:12 +08:00
|
|
|
static uint8_t byteFromBitsInit(const BitsInit *B) {
|
|
|
|
unsigned N = B->getNumBits();
|
|
|
|
assert(N <= 8 && "Field is too large for uint8_t!");
|
|
|
|
|
|
|
|
uint8_t Value = 0;
|
|
|
|
for (unsigned I = 0; I != N; ++I) {
|
|
|
|
BitInit *Bit = cast<BitInit>(B->getBit(I));
|
|
|
|
Value |= Bit->getValue() << I;
|
2017-03-07 08:11:19 +00:00
|
|
|
}
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
class IsMatch {
|
2024-01-06 17:07:39 +08:00
|
|
|
const CodeGenInstruction *OldInst;
|
2017-03-07 08:11:19 +00:00
|
|
|
|
|
|
|
public:
|
2024-01-06 17:07:39 +08:00
|
|
|
IsMatch(const CodeGenInstruction *OldInst) : OldInst(OldInst) {}
|
|
|
|
|
|
|
|
bool operator()(const CodeGenInstruction *NewInst) {
|
|
|
|
RecognizableInstrBase NewRI(*NewInst);
|
|
|
|
RecognizableInstrBase OldRI(*OldInst);
|
|
|
|
|
|
|
|
// Return false if any of the following fields of does not match.
|
|
|
|
if (std::make_tuple(OldRI.IsCodeGenOnly, OldRI.OpMap, NewRI.OpPrefix,
|
|
|
|
OldRI.HasVEX_4V, OldRI.HasVEX_L, OldRI.HasREX_W,
|
|
|
|
OldRI.Form) !=
|
|
|
|
std::make_tuple(NewRI.IsCodeGenOnly, NewRI.OpMap, OldRI.OpPrefix,
|
|
|
|
NewRI.HasVEX_4V, NewRI.HasVEX_L, NewRI.HasREX_W,
|
|
|
|
NewRI.Form))
|
2017-03-07 08:11:19 +00:00
|
|
|
return false;
|
|
|
|
|
2024-01-06 21:38:12 +08:00
|
|
|
for (unsigned I = 0, E = OldInst->Operands.size(); I < E; ++I) {
|
|
|
|
Record *OldOpRec = OldInst->Operands[I].Rec;
|
|
|
|
Record *NewOpRec = NewInst->Operands[I].Rec;
|
|
|
|
|
|
|
|
if (OldOpRec == NewOpRec)
|
2017-03-07 08:11:19 +00:00
|
|
|
continue;
|
|
|
|
|
2024-01-06 21:38:12 +08:00
|
|
|
if (isRegisterOperand(OldOpRec) && isRegisterOperand(NewOpRec)) {
|
|
|
|
if (getRegOperandSize(OldOpRec) != getRegOperandSize(NewOpRec))
|
|
|
|
return false;
|
|
|
|
} else if (isMemoryOperand(OldOpRec) && isMemoryOperand(NewOpRec)) {
|
|
|
|
if (getMemOperandSize(OldOpRec) != getMemOperandSize(NewOpRec))
|
2017-03-07 08:11:19 +00:00
|
|
|
return false;
|
2024-01-06 21:38:12 +08:00
|
|
|
} else if (isImmediateOperand(OldOpRec) && isImmediateOperand(NewOpRec)) {
|
|
|
|
if (OldOpRec->getValueAsDef("Type") != NewOpRec->getValueAsDef("Type"))
|
2017-03-07 08:11:19 +00:00
|
|
|
return false;
|
2024-01-06 21:38:12 +08:00
|
|
|
}
|
2017-03-07 08:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-06 11:33:36 +08:00
|
|
|
void X86CompressEVEXTablesEmitter::run(raw_ostream &OS) {
|
|
|
|
emitSourceFileHeader("X86 EVEX compression tables", OS);
|
2017-03-07 08:11:19 +00:00
|
|
|
|
|
|
|
ArrayRef<const CodeGenInstruction *> NumberedInstructions =
|
|
|
|
Target.getInstructionsByEnumValue();
|
|
|
|
|
|
|
|
for (const CodeGenInstruction *Inst : NumberedInstructions) {
|
2024-01-06 15:29:25 +08:00
|
|
|
const Record *Rec = Inst->TheDef;
|
2023-09-11 09:54:05 +08:00
|
|
|
// _REV instruction should not appear before encoding optimization
|
2024-01-06 23:30:22 +08:00
|
|
|
if (!Rec->isSubClassOf("X86Inst") ||
|
|
|
|
Rec->getValueAsBit("isAsmParserOnly") ||
|
|
|
|
Rec->getName().ends_with("_REV"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Promoted legacy instruction is in EVEX space, and has REX2-encoding
|
|
|
|
// alternative. It's added due to HW design and never emitted by compiler.
|
|
|
|
if (byteFromBitsInit(Rec->getValueAsBitsInit("OpMapBits")) ==
|
|
|
|
X86Local::T_MAP4 &&
|
|
|
|
byteFromBitsInit(Rec->getValueAsBitsInit("explicitOpPrefixBits")) ==
|
|
|
|
X86Local::ExplicitEVEX)
|
2023-09-11 09:54:05 +08:00
|
|
|
continue;
|
2024-01-06 15:29:25 +08:00
|
|
|
|
|
|
|
if (NoCompressSet.find(Rec->getName()) != NoCompressSet.end())
|
|
|
|
continue;
|
|
|
|
|
2022-03-27 08:41:17 +08:00
|
|
|
RecognizableInstrBase RI(*Inst);
|
2017-03-07 08:11:19 +00:00
|
|
|
|
2024-01-06 11:33:36 +08:00
|
|
|
// Add VEX encoded instructions to one of CompressedInsts vectors according
|
|
|
|
// to it's opcode.
|
2022-03-26 13:00:53 +08:00
|
|
|
if (RI.Encoding == X86Local::VEX)
|
2024-01-06 11:33:36 +08:00
|
|
|
CompressedInsts[RI.Opcode].push_back(Inst);
|
|
|
|
// Add relevant EVEX encoded instructions to PreCompressionInsts
|
2022-03-26 13:00:53 +08:00
|
|
|
else if (RI.Encoding == X86Local::EVEX && !RI.HasEVEX_K && !RI.HasEVEX_B &&
|
2024-01-06 15:29:25 +08:00
|
|
|
!RI.HasEVEX_L2)
|
2024-01-06 11:33:36 +08:00
|
|
|
PreCompressionInsts.push_back(Inst);
|
2017-03-07 08:11:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 15:29:25 +08:00
|
|
|
for (const CodeGenInstruction *Inst : PreCompressionInsts) {
|
|
|
|
const Record *Rec = Inst->TheDef;
|
2024-01-08 16:50:23 +08:00
|
|
|
uint8_t Opcode = byteFromBitsInit(Rec->getValueAsBitsInit("Opcode"));
|
|
|
|
StringRef Name = Rec->getName();
|
2024-01-06 18:30:37 +08:00
|
|
|
const CodeGenInstruction *NewInst = nullptr;
|
2024-01-08 16:50:23 +08:00
|
|
|
if (ManualMap.find(Name) != ManualMap.end()) {
|
2024-01-06 15:29:25 +08:00
|
|
|
Record *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
|
|
|
|
assert(NewRec && "Instruction not found!");
|
2024-01-06 18:30:37 +08:00
|
|
|
NewInst = &Target.getInstruction(NewRec);
|
2024-01-08 16:50:23 +08:00
|
|
|
} else if (Name.ends_with("_EVEX")) {
|
|
|
|
if (auto *NewRec = Records.getDef(Name.drop_back(5)))
|
|
|
|
NewInst = &Target.getInstruction(NewRec);
|
2018-06-19 04:24:44 +00:00
|
|
|
} else {
|
2024-01-06 18:30:37 +08:00
|
|
|
// For each pre-compression instruction look for a match in the appropriate
|
2024-01-06 15:29:25 +08:00
|
|
|
// vector (instructions with the same opcode) using function object
|
|
|
|
// IsMatch.
|
|
|
|
auto Match = llvm::find_if(CompressedInsts[Opcode], IsMatch(Inst));
|
2024-01-06 11:33:36 +08:00
|
|
|
if (Match != CompressedInsts[Opcode].end())
|
2024-01-06 18:30:37 +08:00
|
|
|
NewInst = *Match;
|
2018-06-19 04:24:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 18:30:37 +08:00
|
|
|
if (!NewInst)
|
2018-06-19 04:24:44 +00:00
|
|
|
continue;
|
|
|
|
|
2024-01-06 18:30:37 +08:00
|
|
|
Table.push_back(std::make_pair(Inst, NewInst));
|
2017-03-07 08:11:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 18:30:37 +08:00
|
|
|
printTable(Table, OS);
|
2017-03-07 08:11:19 +00:00
|
|
|
}
|
2023-02-18 23:15:23 +09:00
|
|
|
} // namespace
|
2017-03-07 08:11:19 +00:00
|
|
|
|
2024-01-06 11:33:36 +08:00
|
|
|
static TableGen::Emitter::OptClass<X86CompressEVEXTablesEmitter>
|
|
|
|
X("gen-x86-compress-evex-tables", "Generate X86 EVEX compression tables");
|