Skip to content

Commit a9e8a3a

Browse files
committed
[X86][CodeGen] Extend X86CompressEVEX for NF transform
1 parent 5532ab1 commit a9e8a3a

File tree

15 files changed

+2505
-18
lines changed

15 files changed

+2505
-18
lines changed

llvm/lib/Target/X86/X86CompressEVEX.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// b. Promoted instruction (EVEX) -> pre-promotion instruction (legacy/VEX)
1515
// c. NDD (EVEX) -> non-NDD (legacy)
1616
// d. NF_ND (EVEX) -> NF (EVEX)
17+
// e. NonNF (EVEX) -> NF (EVEX)
1718
//
1819
// Compression a, b and c can always reduce code size, with some exceptions
1920
// such as promoted 16-bit CRC32 which is as long as the legacy version.
@@ -30,6 +31,9 @@
3031
//
3132
// Compression d can help hardware decode (HW may skip reading the NDD
3233
// register) although the instruction length remains unchanged.
34+
//
35+
// Compression e can help hardware skip updating EFLAGS although the instruction
36+
// length remains unchanged.
3337
//===----------------------------------------------------------------------===//
3438

3539
#include "MCTargetDesc/X86BaseInfo.h"
@@ -219,25 +223,36 @@ static bool CompressEVEXImpl(MachineInstr &MI, const X86Subtarget &ST) {
219223
return false;
220224
// MOVBE*rr is special because it has semantic of NDD but not set EVEX_B.
221225
bool IsNDLike = IsND || Opc == X86::MOVBE32rr || Opc == X86::MOVBE64rr;
222-
if (IsNDLike && !isRedundantNewDataDest(MI, ST))
226+
bool IsRedundantNDD = IsNDLike ? isRedundantNewDataDest(MI, ST) : false;
227+
// NonNF -> NF only if it's not a compressible NDD instruction and eflags is
228+
// dead.
229+
unsigned NFOpc = (ST.hasNF() && !IsRedundantNDD &&
230+
MI.registerDefIsDead(X86::EFLAGS, /*TRI=*/nullptr))
231+
? X86::getNFVariant(Opc)
232+
: 0U;
233+
if (IsNDLike && !IsRedundantNDD && !NFOpc)
223234
return false;
224235

225-
ArrayRef<X86TableEntry> Table = ArrayRef(X86CompressEVEXTable);
226-
227-
Opc = MI.getOpcode();
228-
const auto *I = llvm::lower_bound(Table, Opc);
229-
if (I == Table.end() || I->OldOpc != Opc) {
230-
assert(!IsNDLike && "Missing entry for ND-like instruction");
231-
return false;
232-
}
236+
unsigned NewOpc = NFOpc;
237+
if (!NewOpc) {
238+
ArrayRef<X86TableEntry> Table = ArrayRef(X86CompressEVEXTable);
233239

234-
if (!IsNDLike) {
235-
if (usesExtendedRegister(MI) || !checkPredicate(I->NewOpc, &ST) ||
236-
!performCustomAdjustments(MI, I->NewOpc))
240+
Opc = MI.getOpcode();
241+
const auto I = llvm::lower_bound(Table, Opc);
242+
if (I == Table.end() || I->OldOpc != Opc) {
243+
assert(!IsNDLike && "Missing entry for ND-like instruction");
237244
return false;
245+
}
246+
247+
if (!IsNDLike) {
248+
if (usesExtendedRegister(MI) || !checkPredicate(I->NewOpc, &ST) ||
249+
!performCustomAdjustments(MI, I->NewOpc))
250+
return false;
251+
}
252+
NewOpc = I->NewOpc;
238253
}
239254

240-
const MCInstrDesc &NewDesc = ST.getInstrInfo()->get(I->NewOpc);
255+
const MCInstrDesc &NewDesc = ST.getInstrInfo()->get(NewOpc);
241256
MI.setDesc(NewDesc);
242257
unsigned AsmComment;
243258
switch (NewDesc.TSFlags & X86II::EncodingMask) {
@@ -256,7 +271,7 @@ static bool CompressEVEXImpl(MachineInstr &MI, const X86Subtarget &ST) {
256271
llvm_unreachable("Unknown EVEX compression");
257272
}
258273
MI.setAsmPrinterFlag(AsmComment);
259-
if (IsNDLike)
274+
if (IsRedundantNDD)
260275
MI.tieOperands(0, 1);
261276

262277
return true;

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,6 +3221,14 @@ int X86::getCCMPCondFlagsFromCondCode(X86::CondCode CC) {
32213221
}
32223222
}
32233223

3224+
#define GET_X86_NF_TRANSFORM_TABLE
3225+
#include "X86GenInstrMapping.inc"
3226+
unsigned X86::getNFVariant(unsigned Opc) {
3227+
ArrayRef<X86TableEntry> Table = ArrayRef(X86NFTransformTable);
3228+
const auto I = llvm::lower_bound(Table, Opc);
3229+
return (I == Table.end() || I->OldOpc != Opc) ? 0U : I->NewOpc;
3230+
}
3231+
32243232
/// Return the inverse of the specified condition,
32253233
/// e.g. turning COND_E to COND_NE.
32263234
X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {

llvm/lib/Target/X86/X86InstrInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ CondCode getCondFromCCMP(const MachineInstr &MI);
7777
// Turn condition code into condition flags for CCMP/CTEST.
7878
int getCCMPCondFlagsFromCondCode(CondCode CC);
7979

80+
// Get the opcode of corresponding NF variant.
81+
unsigned getNFVariant(unsigned Opc);
82+
8083
/// GetOppositeBranchCondition - Return the inverse of the specified cond,
8184
/// e.g. turning COND_E to COND_NE.
8285
CondCode GetOppositeBranchCondition(CondCode CC);

0 commit comments

Comments
 (0)