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";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the 2 BitsInits are equal
|
|
|
|
// Calculates the integer value residing BitsInit object
|
|
|
|
static inline uint64_t getValueFromBitsInit(const BitsInit *B) {
|
|
|
|
uint64_t Value = 0;
|
|
|
|
for (unsigned i = 0, e = B->getNumBits(); i != e; ++i) {
|
|
|
|
if (BitInit *Bit = dyn_cast<BitInit>(B->getBit(i)))
|
|
|
|
Value |= uint64_t(Bit->getValue()) << i;
|
|
|
|
else
|
|
|
|
PrintFatalError("Invalid VectSize bit");
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
// This is needed for instructions with intrinsic version (_Int).
|
|
|
|
// Where the only difference is the size of the operands.
|
|
|
|
// For example: VUCOMISDZrm and Int_VUCOMISDrm
|
|
|
|
// Also for instructions that their EVEX version was upgraded to work with
|
|
|
|
// k-registers. For example VPCMPEQBrm (xmm output register) and
|
|
|
|
// VPCMPEQBZ128rm (k register output register).
|
2024-01-06 17:07:39 +08:00
|
|
|
for (unsigned i = 0, e = OldInst->Operands.size(); i < e; i++) {
|
|
|
|
Record *OpRec1 = OldInst->Operands[i].Rec;
|
|
|
|
Record *OpRec2 = NewInst->Operands[i].Rec;
|
2017-03-07 08:11:19 +00:00
|
|
|
|
|
|
|
if (OpRec1 == OpRec2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isRegisterOperand(OpRec1) && isRegisterOperand(OpRec2)) {
|
|
|
|
if (getRegOperandSize(OpRec1) != getRegOperandSize(OpRec2))
|
|
|
|
return false;
|
|
|
|
} else if (isMemoryOperand(OpRec1) && isMemoryOperand(OpRec2)) {
|
2017-03-13 05:34:03 +00:00
|
|
|
return false;
|
2017-03-07 08:11:19 +00:00
|
|
|
} else if (isImmediateOperand(OpRec1) && isImmediateOperand(OpRec2)) {
|
2019-04-09 07:40:14 +00:00
|
|
|
if (OpRec1->getValueAsDef("Type") != OpRec2->getValueAsDef("Type")) {
|
2017-03-07 08:11:19 +00:00
|
|
|
return false;
|
2019-04-09 07:40:14 +00:00
|
|
|
}
|
2017-03-07 08:11:19 +00:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:29:25 +08:00
|
|
|
if (!Rec->isSubClassOf("X86Inst") || Rec->getName().ends_with("_REV"))
|
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-06 11:33:36 +08:00
|
|
|
uint64_t Opcode =
|
2024-01-06 15:29:25 +08:00
|
|
|
getValueFromBitsInit(Inst->TheDef->getValueAsBitsInit("Opcode"));
|
2024-01-06 18:30:37 +08:00
|
|
|
const CodeGenInstruction *NewInst = nullptr;
|
2024-01-06 15:29:25 +08:00
|
|
|
if (ManualMap.find(Rec->getName()) != ManualMap.end()) {
|
|
|
|
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);
|
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");
|