@@ -8479,6 +8479,19 @@ MachineInstr *X86InstrInfo::foldMemoryOperandCustom(
8479
8479
return nullptr;
8480
8480
}
8481
8481
8482
+ static bool shouldPreventUndefRegUpdateMemFold(MachineFunction &MF, MachineInstr &MI) {
8483
+ if (MF.getFunction().optForSize() || !hasUndefRegUpdate(MI.getOpcode()) ||
8484
+ !MI.getOperand(1).isReg())
8485
+ return false;
8486
+
8487
+ MachineRegisterInfo &RegInfo = MF.getRegInfo();
8488
+ MachineInstr *VRegDef = RegInfo.getUniqueVRegDef(MI.getOperand(1).getReg());
8489
+ if (VRegDef == nullptr)
8490
+ return false;
8491
+ return VRegDef->isImplicitDef();
8492
+ }
8493
+
8494
+
8482
8495
MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
8483
8496
MachineFunction &MF, MachineInstr &MI, unsigned OpNum,
8484
8497
ArrayRef<MachineOperand> MOs, MachineBasicBlock::iterator InsertPt,
@@ -8497,10 +8510,10 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
8497
8510
MI.getOpcode() == X86::PUSH64r))
8498
8511
return nullptr;
8499
8512
8500
- // Avoid partial register update stalls unless optimizing for size.
8501
- // TODO: we should block undef reg update as well.
8513
+ // Avoid partial and undef register update stalls unless optimizing for size.
8502
8514
if (!MF.getFunction().optForSize() &&
8503
- hasPartialRegUpdate(MI.getOpcode(), Subtarget))
8515
+ (hasPartialRegUpdate(MI.getOpcode(), Subtarget) ||
8516
+ shouldPreventUndefRegUpdateMemFold(MF, MI)))
8504
8517
return nullptr;
8505
8518
8506
8519
unsigned NumOps = MI.getDesc().getNumOperands();
@@ -8674,11 +8687,10 @@ X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
8674
8687
if (NoFusing)
8675
8688
return nullptr;
8676
8689
8677
- // Unless optimizing for size, don't fold to avoid partial
8678
- // register update stalls
8679
- // TODO: we should block undef reg update as well.
8690
+ // Avoid partial and undef register update stalls unless optimizing for size.
8680
8691
if (!MF.getFunction().optForSize() &&
8681
- hasPartialRegUpdate(MI.getOpcode(), Subtarget))
8692
+ (hasPartialRegUpdate(MI.getOpcode(), Subtarget) ||
8693
+ shouldPreventUndefRegUpdateMemFold(MF, MI)))
8682
8694
return nullptr;
8683
8695
8684
8696
// Don't fold subreg spills, or reloads that use a high subreg.
@@ -8875,10 +8887,10 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
8875
8887
// Check switch flag
8876
8888
if (NoFusing) return nullptr;
8877
8889
8878
- // Avoid partial register update stalls unless optimizing for size.
8879
- // TODO: we should block undef reg update as well.
8890
+ // Avoid partial and undef register update stalls unless optimizing for size.
8880
8891
if (!MF.getFunction().optForSize() &&
8881
- hasPartialRegUpdate(MI.getOpcode(), Subtarget))
8892
+ (hasPartialRegUpdate(MI.getOpcode(), Subtarget) ||
8893
+ shouldPreventUndefRegUpdateMemFold(MF, MI)))
8882
8894
return nullptr;
8883
8895
8884
8896
// Determine the alignment of the load.
0 commit comments