Skip to content

Commit 0abf3a9

Browse files
committed
[X86][NFC] Use single table for EVEX compression
This patch is to address my review comments in #77065 to simplify the implemention of EVEX2Legacy compression.
1 parent 1687555 commit 0abf3a9

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

llvm/lib/Target/X86/X86CompressEVEX.cpp

+5-21
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,7 @@ static bool performCustomAdjustments(MachineInstr &MI, unsigned NewOpc) {
224224
return true;
225225
}
226226

227-
// For EVEX instructions that can be encoded using VEX encoding
228-
// replace them by the VEX encoding in order to reduce size.
229-
static bool CompressEvexToVexImpl(MachineInstr &MI, const X86Subtarget &ST) {
230-
// VEX format.
231-
// # of bytes: 0,2,3 1 1 0,1 0,1,2,4 0,1
232-
// [Prefixes] [VEX] OPCODE ModR/M [SIB] [DISP] [IMM]
233-
//
234-
// EVEX format.
235-
// # of bytes: 4 1 1 1 4 / 1 1
236-
// [Prefixes] EVEX Opcode ModR/M [SIB] [Disp32] / [Disp8*N] [Immediate]
227+
static bool CompressEVEXImpl(MachineInstr &MI, const X86Subtarget &ST) {
237228
const MCInstrDesc &Desc = MI.getDesc();
238229

239230
// Check for EVEX instructions only.
@@ -251,10 +242,7 @@ static bool CompressEvexToVexImpl(MachineInstr &MI, const X86Subtarget &ST) {
251242
if (Desc.TSFlags & X86II::EVEX_L2)
252243
return false;
253244

254-
// Use the VEX.L bit to select the 128 or 256-bit table.
255-
ArrayRef<X86CompressEVEXTableEntry> Table =
256-
(Desc.TSFlags & X86II::VEX_L) ? ArrayRef(X86EvexToVex256CompressTable)
257-
: ArrayRef(X86EvexToVex128CompressTable);
245+
ArrayRef<X86CompressEVEXTableEntry> Table = ArrayRef(X86CompressEVEXTable);
258246

259247
unsigned Opc = MI.getOpcode();
260248
const auto *I = llvm::lower_bound(Table, Opc);
@@ -278,10 +266,8 @@ bool CompressEVEXPass::runOnMachineFunction(MachineFunction &MF) {
278266
// Make sure the tables are sorted.
279267
static std::atomic<bool> TableChecked(false);
280268
if (!TableChecked.load(std::memory_order_relaxed)) {
281-
assert(llvm::is_sorted(X86EvexToVex128CompressTable) &&
282-
"X86EvexToVex128CompressTable is not sorted!");
283-
assert(llvm::is_sorted(X86EvexToVex256CompressTable) &&
284-
"X86EvexToVex256CompressTable is not sorted!");
269+
assert(llvm::is_sorted(X86CompressEVEXTable) &&
270+
"X86CompressEVEXTable is not sorted!");
285271
TableChecked.store(true, std::memory_order_relaxed);
286272
}
287273
#endif
@@ -291,12 +277,10 @@ bool CompressEVEXPass::runOnMachineFunction(MachineFunction &MF) {
291277

292278
bool Changed = false;
293279

294-
/// Go over all basic blocks in function and replace
295-
/// EVEX encoded instrs by VEX encoding when possible.
296280
for (MachineBasicBlock &MBB : MF) {
297281
// Traverse the basic block.
298282
for (MachineInstr &MI : MBB)
299-
Changed |= CompressEvexToVexImpl(MI, ST);
283+
Changed |= CompressEVEXImpl(MI, ST);
300284
}
301285

302286
return Changed;

llvm/utils/TableGen/X86CompressEVEXTablesEmitter.cpp

+10-25
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ class X86CompressEVEXTablesEmitter {
4747
typedef std::pair<const CodeGenInstruction *, const CodeGenInstruction *>
4848
Entry;
4949

50-
// Represent both compress tables
51-
std::vector<Entry> EVEX2VEX128;
52-
std::vector<Entry> EVEX2VEX256;
50+
std::vector<Entry> Table;
5351

5452
public:
5553
X86CompressEVEXTablesEmitter(RecordKeeper &R) : Records(R), Target(R) {}
@@ -64,20 +62,13 @@ class X86CompressEVEXTablesEmitter {
6462

6563
void X86CompressEVEXTablesEmitter::printTable(const std::vector<Entry> &Table,
6664
raw_ostream &OS) {
67-
StringRef Size = (Table == EVEX2VEX128) ? "128" : "256";
6865

69-
OS << "// X86 EVEX encoded instructions that have a VEX " << Size
70-
<< " encoding\n"
71-
<< "// (table format: <EVEX opcode, VEX-" << Size << " opcode>).\n"
72-
<< "static const X86CompressEVEXTableEntry X86EvexToVex" << Size
73-
<< "CompressTable[] = {\n"
74-
<< " // EVEX scalar with corresponding VEX.\n";
66+
OS << "static const X86CompressEVEXTableEntry X86CompressEVEXTable[] = { \n";
7567

7668
// Print all entries added to the table
77-
for (const auto &Pair : Table) {
69+
for (const auto &Pair : Table)
7870
OS << " { X86::" << Pair.first->TheDef->getName()
7971
<< ", X86::" << Pair.second->TheDef->getName() << " },\n";
80-
}
8172

8273
OS << "};\n\n";
8374
}
@@ -175,33 +166,27 @@ void X86CompressEVEXTablesEmitter::run(raw_ostream &OS) {
175166
const Record *Rec = Inst->TheDef;
176167
uint64_t Opcode =
177168
getValueFromBitsInit(Inst->TheDef->getValueAsBitsInit("Opcode"));
178-
const CodeGenInstruction *VEXInst = nullptr;
169+
const CodeGenInstruction *NewInst = nullptr;
179170
if (ManualMap.find(Rec->getName()) != ManualMap.end()) {
180171
Record *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
181172
assert(NewRec && "Instruction not found!");
182-
VEXInst = &Target.getInstruction(NewRec);
173+
NewInst = &Target.getInstruction(NewRec);
183174
} else {
184-
// For each EVEX instruction look for a VEX match in the appropriate
175+
// For each pre-compression instruction look for a match in the appropriate
185176
// vector (instructions with the same opcode) using function object
186177
// IsMatch.
187178
auto Match = llvm::find_if(CompressedInsts[Opcode], IsMatch(Inst));
188179
if (Match != CompressedInsts[Opcode].end())
189-
VEXInst = *Match;
180+
NewInst = *Match;
190181
}
191182

192-
if (!VEXInst)
183+
if (!NewInst)
193184
continue;
194185

195-
// In case a match is found add new entry to the appropriate table
196-
if (Rec->getValueAsBit("hasVEX_L"))
197-
EVEX2VEX256.push_back(std::make_pair(Inst, VEXInst)); // {0,1}
198-
else
199-
EVEX2VEX128.push_back(std::make_pair(Inst, VEXInst)); // {0,0}
186+
Table.push_back(std::make_pair(Inst, NewInst));
200187
}
201188

202-
// Print both tables
203-
printTable(EVEX2VEX128, OS);
204-
printTable(EVEX2VEX256, OS);
189+
printTable(Table, OS);
205190
}
206191
} // namespace
207192

0 commit comments

Comments
 (0)