Skip to content

Commit 7f524f7

Browse files
committed
[X86][CodeGen] Simplify the code in foldMemoryOperandImpl, NFCI
In preparation for the coming NDD -> RMW fold.
1 parent 73f4c25 commit 7f524f7

File tree

1 file changed

+26
-41
lines changed

1 file changed

+26
-41
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7132,7 +7132,7 @@ static void updateOperandRegConstraints(MachineFunction &MF,
71327132
}
71337133
}
71347134

7135-
static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
7135+
static MachineInstr *fuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
71367136
ArrayRef<MachineOperand> MOs,
71377137
MachineBasicBlock::iterator InsertPt,
71387138
MachineInstr &MI,
@@ -7161,7 +7161,7 @@ static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
71617161
return MIB;
71627162
}
71637163

7164-
static MachineInstr *FuseInst(MachineFunction &MF, unsigned Opcode,
7164+
static MachineInstr *fuseInst(MachineFunction &MF, unsigned Opcode,
71657165
unsigned OpNo, ArrayRef<MachineOperand> MOs,
71667166
MachineBasicBlock::iterator InsertPt,
71677167
MachineInstr &MI, const TargetInstrInfo &TII,
@@ -7231,7 +7231,7 @@ MachineInstr *X86InstrInfo::foldMemoryOperandCustom(
72317231
: (MI.getOpcode() == X86::VINSERTPSrr) ? X86::VINSERTPSrm
72327232
: X86::INSERTPSrm;
72337233
MachineInstr *NewMI =
7234-
FuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, PtrOffset);
7234+
fuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, PtrOffset);
72357235
NewMI->getOperand(NewMI->getNumOperands() - 1).setImm(NewImm);
72367236
return NewMI;
72377237
}
@@ -7253,7 +7253,7 @@ MachineInstr *X86InstrInfo::foldMemoryOperandCustom(
72537253
: (MI.getOpcode() == X86::VMOVHLPSrr) ? X86::VMOVLPSrm
72547254
: X86::MOVLPSrm;
72557255
MachineInstr *NewMI =
7256-
FuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, 8);
7256+
fuseInst(MF, NewOpCode, OpNum, MOs, InsertPt, MI, *this, 8);
72577257
return NewMI;
72587258
}
72597259
}
@@ -7268,7 +7268,7 @@ MachineInstr *X86InstrInfo::foldMemoryOperandCustom(
72687268
unsigned RCSize = TRI.getRegSizeInBits(*RC) / 8;
72697269
if ((Size == 0 || Size >= 16) && RCSize >= 16 && Alignment < Align(16)) {
72707270
MachineInstr *NewMI =
7271-
FuseInst(MF, X86::MOVHPDrm, OpNum, MOs, InsertPt, MI, *this);
7271+
fuseInst(MF, X86::MOVHPDrm, OpNum, MOs, InsertPt, MI, *this);
72727272
return NewMI;
72737273
}
72747274
}
@@ -7328,30 +7328,30 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
73287328
ArrayRef<MachineOperand> MOs, MachineBasicBlock::iterator InsertPt,
73297329
unsigned Size, Align Alignment, bool AllowCommute) const {
73307330
bool isSlowTwoMemOps = Subtarget.slowTwoMemOps();
7331-
bool isTwoAddrFold = false;
7331+
unsigned Opc = MI.getOpcode();
73327332

73337333
// For CPUs that favor the register form of a call or push,
73347334
// do not fold loads into calls or pushes, unless optimizing for size
73357335
// aggressively.
73367336
if (isSlowTwoMemOps && !MF.getFunction().hasMinSize() &&
7337-
(MI.getOpcode() == X86::CALL32r || MI.getOpcode() == X86::CALL64r ||
7338-
MI.getOpcode() == X86::PUSH16r || MI.getOpcode() == X86::PUSH32r ||
7339-
MI.getOpcode() == X86::PUSH64r))
7337+
(Opc == X86::CALL32r || Opc == X86::CALL64r || Opc == X86::PUSH16r ||
7338+
Opc == X86::PUSH32r || Opc == X86::PUSH64r))
73407339
return nullptr;
73417340

73427341
// Avoid partial and undef register update stalls unless optimizing for size.
73437342
if (!MF.getFunction().hasOptSize() &&
7344-
(hasPartialRegUpdate(MI.getOpcode(), Subtarget, /*ForLoadFold*/ true) ||
7343+
(hasPartialRegUpdate(Opc, Subtarget, /*ForLoadFold*/ true) ||
73457344
shouldPreventUndefRegUpdateMemFold(MF, MI)))
73467345
return nullptr;
73477346

73487347
unsigned NumOps = MI.getDesc().getNumOperands();
7349-
bool isTwoAddr =
7350-
NumOps > 1 && MI.getDesc().getOperandConstraint(1, MCOI::TIED_TO) != -1;
7348+
bool IsTwoAddr = NumOps > 1 && OpNum < 2 && MI.getOperand(0).isReg() &&
7349+
MI.getOperand(1).isReg() &&
7350+
MI.getOperand(0).getReg() == MI.getOperand(1).getReg();
73517351

73527352
// FIXME: AsmPrinter doesn't know how to handle
73537353
// X86II::MO_GOT_ABSOLUTE_ADDRESS after folding.
7354-
if (MI.getOpcode() == X86::ADD32ri &&
7354+
if (Opc == X86::ADD32ri &&
73557355
MI.getOperand(2).getTargetFlags() == X86II::MO_GOT_ABSOLUTE_ADDRESS)
73567356
return nullptr;
73577357

@@ -7360,44 +7360,31 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
73607360
// instructions.
73617361
if (MOs.size() == X86::AddrNumOperands &&
73627362
MOs[X86::AddrDisp].getTargetFlags() == X86II::MO_GOTTPOFF &&
7363-
MI.getOpcode() != X86::ADD64rr)
7363+
Opc != X86::ADD64rr)
73647364
return nullptr;
73657365

73667366
// Don't fold loads into indirect calls that need a KCFI check as we'll
73677367
// have to unfold these in X86TargetLowering::EmitKCFICheck anyway.
73687368
if (MI.isCall() && MI.getCFIType())
73697369
return nullptr;
73707370

7371-
MachineInstr *NewMI = nullptr;
7372-
73737371
// Attempt to fold any custom cases we have.
7374-
if (MachineInstr *CustomMI = foldMemoryOperandCustom(
7375-
MF, MI, OpNum, MOs, InsertPt, Size, Alignment))
7372+
if (auto *CustomMI = foldMemoryOperandCustom(MF, MI, OpNum, MOs, InsertPt,
7373+
Size, Alignment))
73767374
return CustomMI;
73777375

7378-
const X86FoldTableEntry *I = nullptr;
7376+
if (Opc == X86::MOV32r0)
7377+
if (auto *NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, InsertPt, MI))
7378+
return NewMI;
73797379

73807380
// Folding a memory location into the two-address part of a two-address
73817381
// instruction is different than folding it other places. It requires
73827382
// replacing the *two* registers with the memory location.
7383-
if (isTwoAddr && NumOps >= 2 && OpNum < 2 && MI.getOperand(0).isReg() &&
7384-
MI.getOperand(1).isReg() &&
7385-
MI.getOperand(0).getReg() == MI.getOperand(1).getReg()) {
7386-
I = lookupTwoAddrFoldTable(MI.getOpcode());
7387-
isTwoAddrFold = true;
7388-
} else {
7389-
if (OpNum == 0) {
7390-
if (MI.getOpcode() == X86::MOV32r0) {
7391-
NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, InsertPt, MI);
7392-
if (NewMI)
7393-
return NewMI;
7394-
}
7395-
}
7396-
7397-
I = lookupFoldTable(MI.getOpcode(), OpNum);
7398-
}
7383+
const X86FoldTableEntry *I =
7384+
IsTwoAddr ? lookupTwoAddrFoldTable(Opc) : lookupFoldTable(Opc, OpNum);
73997385

7400-
if (I != nullptr) {
7386+
MachineInstr *NewMI = nullptr;
7387+
if (I) {
74017388
unsigned Opcode = I->DstOp;
74027389
if (Alignment <
74037390
Align(1ULL << ((I->Flags & TB_ALIGN_MASK) >> TB_ALIGN_SHIFT)))
@@ -7428,10 +7415,8 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
74287415
return nullptr;
74297416
}
74307417

7431-
if (isTwoAddrFold)
7432-
NewMI = FuseTwoAddrInst(MF, Opcode, MOs, InsertPt, MI, *this);
7433-
else
7434-
NewMI = FuseInst(MF, Opcode, OpNum, MOs, InsertPt, MI, *this);
7418+
NewMI = IsTwoAddr ? fuseTwoAddrInst(MF, Opcode, MOs, InsertPt, MI, *this)
7419+
: fuseInst(MF, Opcode, OpNum, MOs, InsertPt, MI, *this);
74357420

74367421
if (NarrowToMOV32rm) {
74377422
// If this is the special case where we use a MOV32rm to load a 32-bit
@@ -8231,7 +8216,7 @@ X86InstrInfo::foldMemoryBroadcast(MachineFunction &MF, MachineInstr &MI,
82318216

82328217
if (auto *I = lookupBroadcastFoldTable(MI.getOpcode(), OpNum))
82338218
return matchBroadcastSize(*I, BitsSize)
8234-
? FuseInst(MF, I->DstOp, OpNum, MOs, InsertPt, MI, *this)
8219+
? fuseInst(MF, I->DstOp, OpNum, MOs, InsertPt, MI, *this)
82358220
: nullptr;
82368221

82378222
if (AllowCommute) {

0 commit comments

Comments
 (0)