Skip to content

Commit d9610b4

Browse files
committed
[X86][MC] Move the code about MOVSX encoding optimization to X86EncodingOptimization.cpp, NFCI
1 parent 4dc9a2c commit d9610b4

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

llvm/lib/Target/X86/MCTargetDesc/X86EncodingOptimization.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ bool X86::optimizeVPCMPWithImmediateOneOrSix(MCInst &MI) {
230230
FROM_TO(VPCMPWZrmik, VPCMPEQWZrmk, VPCMPGTWZrmk)
231231
FROM_TO(VPCMPWZrri, VPCMPEQWZrr, VPCMPGTWZrr)
232232
FROM_TO(VPCMPWZrrik, VPCMPEQWZrrk, VPCMPGTWZrrk)
233+
#undef FROM_TO
233234
}
234235
MCOperand &LastOp = MI.getOperand(MI.getNumOperands() - 1);
235236
int64_t Imm = LastOp.getImm();
@@ -244,3 +245,24 @@ bool X86::optimizeVPCMPWithImmediateOneOrSix(MCInst &MI) {
244245
MI.erase(&LastOp);
245246
return true;
246247
}
248+
249+
bool X86::optimizeMOVSX(MCInst &MI) {
250+
unsigned NewOpc;
251+
#define FROM_TO(FROM, TO, R0, R1) \
252+
case X86::FROM: \
253+
if (MI.getOperand(0).getReg() != X86::R0 || \
254+
MI.getOperand(1).getReg() != X86::R1) \
255+
return false; \
256+
NewOpc = X86::TO; \
257+
break;
258+
switch (MI.getOpcode()) {
259+
default:
260+
return false;
261+
FROM_TO(MOVSX16rr8, CBW, AX, AL) // movsbw %al, %ax --> cbtw
262+
FROM_TO(MOVSX32rr16, CWDE, EAX, AX) // movswl %ax, %eax --> cwtl
263+
FROM_TO(MOVSX64rr32, CDQE, RAX, EAX) // movslq %eax, %rax --> cltq
264+
}
265+
MI.clear();
266+
MI.setOpcode(NewOpc);
267+
return true;
268+
}

llvm/lib/Target/X86/MCTargetDesc/X86EncodingOptimization.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace X86 {
1919
bool optimizeInstFromVEX3ToVEX2(MCInst &MI, const MCInstrDesc &Desc);
2020
bool optimizeShiftRotateWithImmediateOne(MCInst &MI);
2121
bool optimizeVPCMPWithImmediateOneOrSix(MCInst &MI);
22+
bool optimizeMOVSX(MCInst &MI);
2223
} // namespace X86
2324
} // namespace llvm
2425
#endif

llvm/lib/Target/X86/X86MCInstLower.cpp

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -343,34 +343,6 @@ static void SimplifyShortImmForm(MCInst &Inst, unsigned Opcode) {
343343
Inst.addOperand(Saved);
344344
}
345345

346-
/// If a movsx instruction has a shorter encoding for the used register
347-
/// simplify the instruction to use it instead.
348-
static void SimplifyMOVSX(MCInst &Inst) {
349-
unsigned NewOpcode = 0;
350-
unsigned Op0 = Inst.getOperand(0).getReg(), Op1 = Inst.getOperand(1).getReg();
351-
switch (Inst.getOpcode()) {
352-
default:
353-
llvm_unreachable("Unexpected instruction!");
354-
case X86::MOVSX16rr8: // movsbw %al, %ax --> cbtw
355-
if (Op0 == X86::AX && Op1 == X86::AL)
356-
NewOpcode = X86::CBW;
357-
break;
358-
case X86::MOVSX32rr16: // movswl %ax, %eax --> cwtl
359-
if (Op0 == X86::EAX && Op1 == X86::AX)
360-
NewOpcode = X86::CWDE;
361-
break;
362-
case X86::MOVSX64rr32: // movslq %eax, %rax --> cltq
363-
if (Op0 == X86::RAX && Op1 == X86::EAX)
364-
NewOpcode = X86::CDQE;
365-
break;
366-
}
367-
368-
if (NewOpcode != 0) {
369-
Inst = MCInst();
370-
Inst.setOpcode(NewOpcode);
371-
}
372-
}
373-
374346
/// Simplify things like MOV32rm to MOV32o32a.
375347
static void SimplifyShortMoveForm(X86AsmPrinter &Printer, MCInst &Inst,
376348
unsigned Opcode) {
@@ -511,6 +483,9 @@ void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
511483
if (X86::optimizeVPCMPWithImmediateOneOrSix(OutMI))
512484
return;
513485

486+
if (X86::optimizeMOVSX(OutMI))
487+
return;
488+
514489
// Handle a few special cases to eliminate operand modifiers.
515490
switch (OutMI.getOpcode()) {
516491
case X86::LEA64_32r:
@@ -703,14 +678,6 @@ void X86MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
703678
SimplifyShortImmForm(OutMI, NewOpc);
704679
break;
705680
}
706-
707-
// Try to shrink some forms of movsx.
708-
case X86::MOVSX16rr8:
709-
case X86::MOVSX32rr16:
710-
case X86::MOVSX64rr32:
711-
SimplifyMOVSX(OutMI);
712-
break;
713-
714681
case X86::VCMPPDrri:
715682
case X86::VCMPPDYrri:
716683
case X86::VCMPPSrri:

0 commit comments

Comments
 (0)