Skip to content

Commit cb16b33

Browse files
committed
Revert "[X86] Remove patterns for ADC/SBB with immediate 8 and optimize during MC lowering, NFCI"
This caused compiler assertions, see comment on https://reviews.llvm.org/D150107. This also reverts the dependent follow-up change: > [X86] Remove patterns for ADD/AND/OR/SUB/XOR/CMP with immediate 8 and optimize during MC lowering, NFCI > > This is follow-up of D150107. > > In addition, the function `X86::optimizeToFixedRegisterOrShortImmediateForm` can be > shared with project bolt and eliminates the code in X86InstrRelaxTables.cpp. > > Differential Revision: https://reviews.llvm.org/D150949 This reverts commit 2ef8ae1 and 5586bc5.
1 parent 218841a commit cb16b33

File tree

63 files changed

+735
-467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+735
-467
lines changed

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

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ bool X86::optimizeMOV(MCInst &MI, bool In64BitMode) {
370370

371371
/// Simplify FOO $imm, %{al,ax,eax,rax} to FOO $imm, for instruction with
372372
/// a short fixed-register form.
373-
static bool optimizeToFixedRegisterForm(MCInst &MI) {
373+
bool X86::optimizeToFixedRegisterForm(MCInst &MI) {
374374
unsigned NewOpc;
375375
switch (MI.getOpcode()) {
376376
default:
@@ -424,77 +424,3 @@ static bool optimizeToFixedRegisterForm(MCInst &MI) {
424424
MI.addOperand(Saved);
425425
return true;
426426
}
427-
428-
static bool optimizeToShortImmediateForm(MCInst &MI) {
429-
unsigned NewOpc;
430-
switch (MI.getOpcode()) {
431-
default:
432-
return false;
433-
FROM_TO(ADC16mi, ADC16mi8)
434-
FROM_TO(ADC16ri, ADC16ri8)
435-
FROM_TO(ADC32mi, ADC32mi8)
436-
FROM_TO(ADC32ri, ADC32ri8)
437-
FROM_TO(ADC64mi32, ADC64mi8)
438-
FROM_TO(ADC64ri32, ADC64ri8)
439-
FROM_TO(SBB16mi, SBB16mi8)
440-
FROM_TO(SBB16ri, SBB16ri8)
441-
FROM_TO(SBB32mi, SBB32mi8)
442-
FROM_TO(SBB32ri, SBB32ri8)
443-
FROM_TO(SBB64mi32, SBB64mi8)
444-
FROM_TO(SBB64ri32, SBB64ri8)
445-
FROM_TO(ADD16mi, ADD16mi8)
446-
FROM_TO(ADD16ri, ADD16ri8)
447-
FROM_TO(ADD32mi, ADD32mi8)
448-
FROM_TO(ADD32ri, ADD32ri8)
449-
FROM_TO(ADD64mi32, ADD64mi8)
450-
FROM_TO(ADD64ri32, ADD64ri8)
451-
FROM_TO(AND16mi, AND16mi8)
452-
FROM_TO(AND16ri, AND16ri8)
453-
FROM_TO(AND32mi, AND32mi8)
454-
FROM_TO(AND32ri, AND32ri8)
455-
FROM_TO(AND64mi32, AND64mi8)
456-
FROM_TO(AND64ri32, AND64ri8)
457-
FROM_TO(OR16mi, OR16mi8)
458-
FROM_TO(OR16ri, OR16ri8)
459-
FROM_TO(OR32mi, OR32mi8)
460-
FROM_TO(OR32ri, OR32ri8)
461-
FROM_TO(OR64mi32, OR64mi8)
462-
FROM_TO(OR64ri32, OR64ri8)
463-
FROM_TO(SUB16mi, SUB16mi8)
464-
FROM_TO(SUB16ri, SUB16ri8)
465-
FROM_TO(SUB32mi, SUB32mi8)
466-
FROM_TO(SUB32ri, SUB32ri8)
467-
FROM_TO(SUB64mi32, SUB64mi8)
468-
FROM_TO(SUB64ri32, SUB64ri8)
469-
FROM_TO(XOR16mi, XOR16mi8)
470-
FROM_TO(XOR16ri, XOR16ri8)
471-
FROM_TO(XOR32mi, XOR32mi8)
472-
FROM_TO(XOR32ri, XOR32ri8)
473-
FROM_TO(XOR64mi32, XOR64mi8)
474-
FROM_TO(XOR64ri32, XOR64ri8)
475-
FROM_TO(CMP16mi, CMP16mi8)
476-
FROM_TO(CMP16ri, CMP16ri8)
477-
FROM_TO(CMP32mi, CMP32mi8)
478-
FROM_TO(CMP32ri, CMP32ri8)
479-
FROM_TO(CMP64mi32, CMP64mi8)
480-
FROM_TO(CMP64ri32, CMP64ri8)
481-
}
482-
MCOperand &LastOp = MI.getOperand(MI.getNumOperands() - 1);
483-
if (LastOp.isExpr()) {
484-
const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(LastOp.getExpr());
485-
if (!SRE || SRE->getKind() != MCSymbolRefExpr::VK_X86_ABS8)
486-
return false;
487-
} else if (LastOp.isImm()) {
488-
if (!isInt<8>(LastOp.getImm()))
489-
return false;
490-
}
491-
MI.setOpcode(NewOpc);
492-
return true;
493-
}
494-
495-
bool X86::optimizeToFixedRegisterOrShortImmediateForm(MCInst &MI) {
496-
// We may optimize twice here.
497-
bool ShortImm = optimizeToShortImmediateForm(MI);
498-
bool FixedReg = optimizeToFixedRegisterForm(MI);
499-
return ShortImm || FixedReg;
500-
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bool optimizeVPCMPWithImmediateOneOrSix(MCInst &MI);
2222
bool optimizeMOVSX(MCInst &MI);
2323
bool optimizeINCDEC(MCInst &MI, bool In64BitMode);
2424
bool optimizeMOV(MCInst &MI, bool In64BitMode);
25-
bool optimizeToFixedRegisterOrShortImmediateForm(MCInst &MI);
25+
bool optimizeToFixedRegisterForm(MCInst &MI);
2626
} // namespace X86
2727
} // namespace llvm
2828
#endif

llvm/lib/Target/X86/X86CallFrameOptimization.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,15 @@ X86CallFrameOptimization::classifyInstruction(
285285
// The instructions we actually care about are movs onto the stack or special
286286
// cases of constant-stores to stack
287287
switch (MI->getOpcode()) {
288-
case X86::AND16mi:
289-
case X86::AND32mi:
290-
case X86::AND64mi32: {
288+
case X86::AND16mi8:
289+
case X86::AND32mi8:
290+
case X86::AND64mi8: {
291291
const MachineOperand &ImmOp = MI->getOperand(X86::AddrNumOperands);
292292
return ImmOp.getImm() == 0 ? Convert : Exit;
293293
}
294-
case X86::OR16mi:
295-
case X86::OR32mi:
296-
case X86::OR64mi32: {
294+
case X86::OR16mi8:
295+
case X86::OR32mi8:
296+
case X86::OR64mi8: {
297297
const MachineOperand &ImmOp = MI->getOperand(X86::AddrNumOperands);
298298
return ImmOp.getImm() == -1 ? Convert : Exit;
299299
}
@@ -512,12 +512,12 @@ void X86CallFrameOptimization::adjustCallSequence(MachineFunction &MF,
512512
switch (Store->getOpcode()) {
513513
default:
514514
llvm_unreachable("Unexpected Opcode!");
515-
case X86::AND16mi:
516-
case X86::AND32mi:
517-
case X86::AND64mi32:
518-
case X86::OR16mi:
519-
case X86::OR32mi:
520-
case X86::OR64mi32:
515+
case X86::AND16mi8:
516+
case X86::AND32mi8:
517+
case X86::AND64mi8:
518+
case X86::OR16mi8:
519+
case X86::OR32mi8:
520+
case X86::OR64mi8:
521521
case X86::MOV32mi:
522522
case X86::MOV64mi32:
523523
PushOpcode = Is64Bit ? X86::PUSH64i32 : X86::PUSHi32;

llvm/lib/Target/X86/X86DynAllocaExpander.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ void X86DynAllocaExpander::computeLowerings(MachineFunction &MF,
189189
}
190190
}
191191

192-
static unsigned getSubOpcode(bool Is64Bit) {
192+
static unsigned getSubOpcode(bool Is64Bit, int64_t Amount) {
193193
if (Is64Bit)
194-
return X86::SUB64ri32;
195-
return X86::SUB32ri;
194+
return isInt<8>(Amount) ? X86::SUB64ri8 : X86::SUB64ri32;
195+
return isInt<8>(Amount) ? X86::SUB32ri8 : X86::SUB32ri;
196196
}
197197

198198
void X86DynAllocaExpander::lower(MachineInstr *MI, Lowering L) {
@@ -242,7 +242,8 @@ void X86DynAllocaExpander::lower(MachineInstr *MI, Lowering L) {
242242
.addReg(RegA, RegState::Undef);
243243
} else {
244244
// Sub.
245-
BuildMI(*MBB, I, DL, TII->get(getSubOpcode(Is64BitAlloca)), StackPtr)
245+
BuildMI(*MBB, I, DL,
246+
TII->get(getSubOpcode(Is64BitAlloca, Amount)), StackPtr)
246247
.addReg(StackPtr)
247248
.addImm(Amount);
248249
}

llvm/lib/Target/X86/X86FastISel.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,20 +1376,29 @@ static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
13761376
/// If we have a comparison with RHS as the RHS of the comparison, return an
13771377
/// opcode that works for the compare (e.g. CMP32ri) otherwise return 0.
13781378
static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
1379+
int64_t Val = RHSC->getSExtValue();
13791380
switch (VT.getSimpleVT().SimpleTy) {
13801381
// Otherwise, we can't fold the immediate into this comparison.
13811382
default:
13821383
return 0;
13831384
case MVT::i8:
13841385
return X86::CMP8ri;
13851386
case MVT::i16:
1387+
if (isInt<8>(Val))
1388+
return X86::CMP16ri8;
13861389
return X86::CMP16ri;
13871390
case MVT::i32:
1391+
if (isInt<8>(Val))
1392+
return X86::CMP32ri8;
13881393
return X86::CMP32ri;
13891394
case MVT::i64:
1395+
if (isInt<8>(Val))
1396+
return X86::CMP64ri8;
13901397
// 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
13911398
// field.
1392-
return isInt<32>(RHSC->getSExtValue()) ? X86::CMP64ri32 : 0;
1399+
if (isInt<32>(Val))
1400+
return X86::CMP64ri32;
1401+
return 0;
13931402
}
13941403
}
13951404

llvm/lib/Target/X86/X86FixupLEAs.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@ FixupLEAPass::postRAConvertToLEA(MachineBasicBlock &MBB,
186186
// Only convert instructions that we've verified are safe.
187187
return nullptr;
188188
case X86::ADD64ri32:
189+
case X86::ADD64ri8:
189190
case X86::ADD64ri32_DB:
191+
case X86::ADD64ri8_DB:
190192
case X86::ADD32ri:
193+
case X86::ADD32ri8:
191194
case X86::ADD32ri_DB:
195+
case X86::ADD32ri8_DB:
192196
if (!MI.getOperand(2).isImm()) {
193197
// convertToThreeAddress will call getImm()
194198
// which requires isImm() to be true
@@ -370,14 +374,15 @@ static inline unsigned getSUBrrFromLEA(unsigned LEAOpcode) {
370374

371375
static inline unsigned getADDriFromLEA(unsigned LEAOpcode,
372376
const MachineOperand &Offset) {
377+
bool IsInt8 = Offset.isImm() && isInt<8>(Offset.getImm());
373378
switch (LEAOpcode) {
374379
default:
375380
llvm_unreachable("Unexpected LEA instruction");
376381
case X86::LEA32r:
377382
case X86::LEA64_32r:
378-
return X86::ADD32ri;
383+
return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri;
379384
case X86::LEA64r:
380-
return X86::ADD64ri32;
385+
return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32;
381386
}
382387
}
383388

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,28 @@ bool X86FrameLowering::hasFP(const MachineFunction &MF) const {
105105
(isWin64Prologue(MF) && MFI.hasCopyImplyingStackAdjustment()));
106106
}
107107

108-
static unsigned getSUBriOpcode(bool IsLP64) {
109-
return IsLP64 ? X86::SUB64ri32 : X86::SUB32ri;
108+
static unsigned getSUBriOpcode(bool IsLP64, int64_t Imm) {
109+
if (IsLP64) {
110+
if (isInt<8>(Imm))
111+
return X86::SUB64ri8;
112+
return X86::SUB64ri32;
113+
} else {
114+
if (isInt<8>(Imm))
115+
return X86::SUB32ri8;
116+
return X86::SUB32ri;
117+
}
110118
}
111119

112-
static unsigned getADDriOpcode(bool IsLP64) {
113-
return IsLP64 ? X86::ADD64ri32 : X86::ADD32ri;
120+
static unsigned getADDriOpcode(bool IsLP64, int64_t Imm) {
121+
if (IsLP64) {
122+
if (isInt<8>(Imm))
123+
return X86::ADD64ri8;
124+
return X86::ADD64ri32;
125+
} else {
126+
if (isInt<8>(Imm))
127+
return X86::ADD32ri8;
128+
return X86::ADD32ri;
129+
}
114130
}
115131

116132
static unsigned getSUBrrOpcode(bool IsLP64) {
@@ -122,7 +138,14 @@ static unsigned getADDrrOpcode(bool IsLP64) {
122138
}
123139

124140
static unsigned getANDriOpcode(bool IsLP64, int64_t Imm) {
125-
return IsLP64 ? X86::AND64ri32 : X86::AND32ri;
141+
if (IsLP64) {
142+
if (isInt<8>(Imm))
143+
return X86::AND64ri8;
144+
return X86::AND64ri32;
145+
}
146+
if (isInt<8>(Imm))
147+
return X86::AND32ri8;
148+
return X86::AND32ri;
126149
}
127150

128151
static unsigned getLEArOpcode(bool IsLP64) {
@@ -340,8 +363,8 @@ MachineInstrBuilder X86FrameLowering::BuildStackAdjustment(
340363
} else {
341364
bool IsSub = Offset < 0;
342365
uint64_t AbsOffset = IsSub ? -Offset : Offset;
343-
const unsigned Opc = IsSub ? getSUBriOpcode(Uses64BitFramePtr)
344-
: getADDriOpcode(Uses64BitFramePtr);
366+
const unsigned Opc = IsSub ? getSUBriOpcode(Uses64BitFramePtr, AbsOffset)
367+
: getADDriOpcode(Uses64BitFramePtr, AbsOffset);
345368
MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
346369
.addReg(StackPtr)
347370
.addImm(AbsOffset);
@@ -377,8 +400,9 @@ int X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB,
377400
unsigned Opc = PI->getOpcode();
378401
int Offset = 0;
379402

380-
if ((Opc == X86::ADD64ri32 || Opc == X86::ADD32ri) &&
381-
PI->getOperand(0).getReg() == StackPtr) {
403+
if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
404+
Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
405+
PI->getOperand(0).getReg() == StackPtr){
382406
assert(PI->getOperand(1).getReg() == StackPtr);
383407
Offset = PI->getOperand(2).getImm();
384408
} else if ((Opc == X86::LEA32r || Opc == X86::LEA64_32r) &&
@@ -389,7 +413,8 @@ int X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB,
389413
PI->getOperand(5).getReg() == X86::NoRegister) {
390414
// For LEAs we have: def = lea SP, FI, noreg, Offset, noreg.
391415
Offset = PI->getOperand(4).getImm();
392-
} else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB32ri) &&
416+
} else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
417+
Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
393418
PI->getOperand(0).getReg() == StackPtr) {
394419
assert(PI->getOperand(1).getReg() == StackPtr);
395420
Offset = -PI->getOperand(2).getImm();
@@ -808,7 +833,7 @@ void X86FrameLowering::emitStackProbeInlineGenericLoop(
808833
// save loop bound
809834
{
810835
const unsigned BoundOffset = alignDown(Offset, StackProbeSize);
811-
const unsigned SUBOpc = getSUBriOpcode(Uses64BitFramePtr);
836+
const unsigned SUBOpc = getSUBriOpcode(Uses64BitFramePtr, BoundOffset);
812837
BuildMI(MBB, MBBI, DL, TII.get(SUBOpc), FinalStackProbed)
813838
.addReg(FinalStackProbed)
814839
.addImm(BoundOffset)
@@ -1311,7 +1336,7 @@ void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB,
13111336

13121337
{
13131338
const unsigned SUBOpc =
1314-
getSUBriOpcode(Uses64BitFramePtr);
1339+
getSUBriOpcode(Uses64BitFramePtr, StackProbeSize);
13151340
BuildMI(headMBB, DL, TII.get(SUBOpc), StackPtr)
13161341
.addReg(StackPtr)
13171342
.addImm(StackProbeSize)
@@ -1342,7 +1367,7 @@ void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB,
13421367
.setMIFlag(MachineInstr::FrameSetup);
13431368

13441369
const unsigned SUBOpc =
1345-
getSUBriOpcode(Uses64BitFramePtr);
1370+
getSUBriOpcode(Uses64BitFramePtr, StackProbeSize);
13461371
BuildMI(bodyMBB, DL, TII.get(SUBOpc), StackPtr)
13471372
.addReg(StackPtr)
13481373
.addImm(StackProbeSize)
@@ -1775,7 +1800,7 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF,
17751800
.addImm(8)
17761801
.addUse(X86::NoRegister)
17771802
.setMIFlag(MachineInstr::FrameSetup);
1778-
BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64ri32), X86::RSP)
1803+
BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64ri8), X86::RSP)
17791804
.addUse(X86::RSP)
17801805
.addImm(8)
17811806
.setMIFlag(MachineInstr::FrameSetup);
@@ -2394,7 +2419,7 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF,
23942419
if ((Opc != X86::POP32r || !PI->getFlag(MachineInstr::FrameDestroy)) &&
23952420
(Opc != X86::POP64r || !PI->getFlag(MachineInstr::FrameDestroy)) &&
23962421
(Opc != X86::BTR64ri8 || !PI->getFlag(MachineInstr::FrameDestroy)) &&
2397-
(Opc != X86::ADD64ri32 || !PI->getFlag(MachineInstr::FrameDestroy)))
2422+
(Opc != X86::ADD64ri8 || !PI->getFlag(MachineInstr::FrameDestroy)))
23982423
break;
23992424
FirstCSPop = PI;
24002425
}
@@ -3768,7 +3793,7 @@ MachineBasicBlock::iterator X86FrameLowering::restoreWin32EHStackPointers(
37683793

37693794
if (UsedReg == FramePtr) {
37703795
// ADD $offset, %ebp
3771-
unsigned ADDri = getADDriOpcode(false);
3796+
unsigned ADDri = getADDriOpcode(false, EndOffset);
37723797
BuildMI(MBB, MBBI, DL, TII.get(ADDri), FramePtr)
37733798
.addReg(FramePtr)
37743799
.addImm(EndOffset)

0 commit comments

Comments
 (0)