[llvm] 2137d5f - [X86][APX] Return CopyMI when added in foldMemoryOperandImpl (#191368)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 12 06:02:55 PDT 2026


Author: Phoebe Wang
Date: 2026-04-12T21:02:50+08:00
New Revision: 2137d5ffbd454c85c31f8533721cd68dcf24015b

URL: https://github.com/llvm/llvm-project/commit/2137d5ffbd454c85c31f8533721cd68dcf24015b
DIFF: https://github.com/llvm/llvm-project/commit/2137d5ffbd454c85c31f8533721cd68dcf24015b.diff

LOG: [X86][APX] Return CopyMI when added in foldMemoryOperandImpl (#191368)

Fixes: #190962 #191165 #191239

Added: 
    llvm/test/CodeGen/X86/apx/pr191368.ll

Modified: 
    llvm/include/llvm/CodeGen/TargetInstrInfo.h
    llvm/lib/CodeGen/InlineSpiller.cpp
    llvm/lib/CodeGen/LiveRangeEdit.cpp
    llvm/lib/CodeGen/PeepholeOptimizer.cpp
    llvm/lib/CodeGen/TargetInstrInfo.cpp
    llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
    llvm/lib/Target/AArch64/AArch64InstrInfo.h
    llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
    llvm/lib/Target/AMDGPU/SIInstrInfo.h
    llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
    llvm/lib/Target/RISCV/RISCVInstrInfo.h
    llvm/lib/Target/SystemZ/SystemZInstrInfo.cpp
    llvm/lib/Target/SystemZ/SystemZInstrInfo.h
    llvm/lib/Target/X86/X86FastISel.cpp
    llvm/lib/Target/X86/X86InstrInfo.cpp
    llvm/lib/Target/X86/X86InstrInfo.h
    llvm/test/CodeGen/X86/apx/or.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index f721af808149e..49562f16bf371 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -1254,17 +1254,18 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   /// operand folded, otherwise NULL is returned.
   /// The new instruction is inserted before MI, and the client is responsible
   /// for removing the old instruction.
+  /// If a copy instruction being created during fold, return it by CopyMI.
   /// If VRM is passed, the assigned physregs can be inspected by target to
   /// decide on using an opcode (note that those assignments can still change).
   MachineInstr *foldMemoryOperand(MachineInstr &MI, ArrayRef<unsigned> Ops,
-                                  int FI,
+                                  int FI, MachineInstr *&CopyMI,
                                   LiveIntervals *LIS = nullptr,
                                   VirtRegMap *VRM = nullptr) const;
 
   /// Same as the previous version except it allows folding of any load and
   /// store from / to any address, not just from a specific stack slot.
   MachineInstr *foldMemoryOperand(MachineInstr &MI, ArrayRef<unsigned> Ops,
-                                  MachineInstr &LoadMI,
+                                  MachineInstr &LoadMI, MachineInstr *&CopyMI,
                                   LiveIntervals *LIS = nullptr) const;
 
   /// This function defines the logic to lower COPY instruction to
@@ -1446,7 +1447,7 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
                         ArrayRef<unsigned> Ops,
                         MachineBasicBlock::iterator InsertPt, int FrameIndex,
-                        LiveIntervals *LIS = nullptr,
+                        MachineInstr *&CopyMI, LiveIntervals *LIS = nullptr,
                         VirtRegMap *VRM = nullptr) const {
     return nullptr;
   }
@@ -1459,7 +1460,7 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   virtual MachineInstr *foldMemoryOperandImpl(
       MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
       MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
-      LiveIntervals *LIS = nullptr) const {
+      MachineInstr *&CopyMI, LiveIntervals *LIS = nullptr) const {
     return nullptr;
   }
 
@@ -1841,11 +1842,13 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
   /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
   /// defined by the load we are trying to fold. DefMI returns the machine
   /// instruction that defines FoldAsLoadDefReg, and the function returns
-  /// the machine instruction generated due to folding.
+  /// the machine instruction generated due to folding. CopyMI returns the
+  /// copy instruction possibly generated due to folding.
   virtual MachineInstr *optimizeLoadInstr(MachineInstr &MI,
                                           const MachineRegisterInfo *MRI,
                                           Register &FoldAsLoadDefReg,
-                                          MachineInstr *&DefMI) const;
+                                          MachineInstr *&DefMI,
+                                          MachineInstr *&CopyMI) const;
 
   /// 'Reg' is known to be defined by a move immediate instruction,
   /// try to fold the immediate into the use instruction.

diff  --git a/llvm/lib/CodeGen/InlineSpiller.cpp b/llvm/lib/CodeGen/InlineSpiller.cpp
index 668c7c0a78098..352e1961332ad 100644
--- a/llvm/lib/CodeGen/InlineSpiller.cpp
+++ b/llvm/lib/CodeGen/InlineSpiller.cpp
@@ -1016,9 +1016,11 @@ foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
       MI->untieRegOperand(Idx);
     }
 
+  MachineInstr *CopyMI = nullptr;
   MachineInstr *FoldMI =
-      LoadMI ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, &LIS)
-             : TII.foldMemoryOperand(*MI, FoldOps, StackSlot, &LIS, &VRM);
+      LoadMI
+          ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, CopyMI, &LIS)
+          : TII.foldMemoryOperand(*MI, FoldOps, StackSlot, CopyMI, &LIS, &VRM);
   if (!FoldMI) {
     // Re-tie operands.
     for (auto Tied : TiedOps)
@@ -1050,7 +1052,15 @@ foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
   if (TII.isStoreToStackSlot(*MI, FI) &&
       HSpiller.rmFromMergeableSpills(*MI, FI))
     --NumSpills;
-  LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI);
+  SlotIndex FoldIdx = LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI);
+  if (CopyMI) {
+    SlotIndex CopyIdx = LIS.InsertMachineInstrInMaps(*CopyMI).getRegSlot();
+    if (!MRI.isSSA()) {
+      LiveInterval &LI = LIS.getInterval(CopyMI->getOperand(0).getReg());
+      VNInfo *VNI = LI.getNextValue(CopyIdx, LIS.getVNInfoAllocator());
+      LI.addSegment(LiveRange::Segment(CopyIdx, FoldIdx.getRegSlot(), VNI));
+    }
+  }
   // Update the call info.
   if (MI->isCandidateForAdditionalCallInfo())
     MI->getMF()->moveAdditionalCallInfo(MI, FoldMI);
@@ -1092,9 +1102,14 @@ foldMemoryOperand(ArrayRef<std::pair<MachineInstr *, unsigned>> Ops,
   // Insert any new instructions other than FoldMI into the LIS maps.
   assert(!MIS.empty() && "Unexpected empty span of instructions!");
   for (MachineInstr &MI : MIS)
-    if (&MI != FoldMI)
+    if (&MI != FoldMI && &MI != CopyMI)
       LIS.InsertMachineInstrInMaps(MI);
 
+  if (CopyMI) {
+    LiveInterval &LI = LIS.getInterval(CopyMI->getOperand(1).getReg());
+    LIS.shrinkToUses(&LI);
+  }
+
   // TII.foldMemoryOperand may have left some implicit operands on the
   // instruction.  Strip them.
   if (ImpReg)

diff  --git a/llvm/lib/CodeGen/LiveRangeEdit.cpp b/llvm/lib/CodeGen/LiveRangeEdit.cpp
index 4e1b0c0c66e69..0141d940d498f 100644
--- a/llvm/lib/CodeGen/LiveRangeEdit.cpp
+++ b/llvm/lib/CodeGen/LiveRangeEdit.cpp
@@ -151,11 +151,13 @@ bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
   if (UseMI->readsWritesVirtualRegister(LI->reg(), &Ops).second)
     return false;
 
-  MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
+  MachineInstr *CopyMI = nullptr;
+  MachineInstr *FoldMI =
+      TII.foldMemoryOperand(*UseMI, Ops, *DefMI, CopyMI, &LIS);
   if (!FoldMI)
     return false;
   LLVM_DEBUG(dbgs() << "                folded: " << *FoldMI);
-  LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
+  SlotIndex FoldIdx = LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
   // Update the call info.
   if (UseMI->shouldUpdateAdditionalCallInfo())
     UseMI->getMF()->moveAdditionalCallInfo(UseMI, FoldMI);
@@ -163,6 +165,14 @@ bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
   DefMI->addRegisterDead(LI->reg(), nullptr);
   Dead.push_back(DefMI);
   ++NumDCEFoldedLoads;
+  if (CopyMI) {
+    SlotIndex CopyIdx = LIS.InsertMachineInstrInMaps(*CopyMI).getRegSlot();
+    LiveInterval &LI = LIS.getInterval(CopyMI->getOperand(0).getReg());
+    VNInfo *VNI = LI.getNextValue(CopyIdx, LIS.getVNInfoAllocator());
+    LI.addSegment(LiveRange::Segment(CopyIdx, FoldIdx.getRegSlot(), VNI));
+    LiveInterval &SrcLI = LIS.getInterval(CopyMI->getOperand(1).getReg());
+    LIS.shrinkToUses(&SrcLI);
+  }
   return true;
 }
 

diff  --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index 73aecda4e522c..9365ea883eec9 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -1865,8 +1865,9 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
             // we need it for markUsesInDebugValueAsUndef().
             Register FoldedReg = FoldAsLoadDefReg;
             MachineInstr *DefMI = nullptr;
-            if (MachineInstr *FoldMI =
-                    TII->optimizeLoadInstr(*MI, MRI, FoldAsLoadDefReg, DefMI)) {
+            MachineInstr *CopyMI = nullptr;
+            if (MachineInstr *FoldMI = TII->optimizeLoadInstr(
+                    *MI, MRI, FoldAsLoadDefReg, DefMI, CopyMI)) {
               // Update LocalMIs since we replaced MI with FoldMI and deleted
               // DefMI.
               LLVM_DEBUG(dbgs() << "Replacing: " << *MI);
@@ -1874,6 +1875,8 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
               LocalMIs.erase(MI);
               LocalMIs.erase(DefMI);
               LocalMIs.insert(FoldMI);
+              if (CopyMI)
+                LocalMIs.insert(CopyMI);
               // Update the call info.
               if (MI->shouldUpdateAdditionalCallInfo())
                 MI->getMF()->moveAdditionalCallInfo(MI, FoldMI);

diff  --git a/llvm/lib/CodeGen/TargetInstrInfo.cpp b/llvm/lib/CodeGen/TargetInstrInfo.cpp
index 3e3b935135350..ba836df02048c 100644
--- a/llvm/lib/CodeGen/TargetInstrInfo.cpp
+++ b/llvm/lib/CodeGen/TargetInstrInfo.cpp
@@ -518,7 +518,8 @@ MCInst TargetInstrInfo::getNop() const { llvm_unreachable("Not implemented"); }
 MachineInstr *TargetInstrInfo::optimizeLoadInstr(MachineInstr &MI,
                                                  const MachineRegisterInfo *MRI,
                                                  Register &FoldAsLoadDefReg,
-                                                 MachineInstr *&DefMI) const {
+                                                 MachineInstr *&DefMI,
+                                                 MachineInstr *&CopyMI) const {
   // Check whether we can move DefMI here.
   DefMI = MRI->getVRegDef(FoldAsLoadDefReg);
   assert(DefMI);
@@ -544,7 +545,8 @@ MachineInstr *TargetInstrInfo::optimizeLoadInstr(MachineInstr &MI,
     return nullptr;
 
   // Check whether we can fold the def into SrcOperandId.
-  if (MachineInstr *FoldMI = foldMemoryOperand(MI, SrcOperandIds, *DefMI)) {
+  if (MachineInstr *FoldMI =
+          foldMemoryOperand(MI, SrcOperandIds, *DefMI, CopyMI)) {
     FoldAsLoadDefReg = 0;
     return FoldMI;
   }
@@ -703,6 +705,7 @@ static MachineInstr *foldInlineAsmMemOperand(MachineInstr &MI,
 
 MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
                                                  ArrayRef<unsigned> Ops, int FI,
+                                                 MachineInstr *&CopyMI,
                                                  LiveIntervals *LIS,
                                                  VirtRegMap *VRM) const {
   auto Flags = MachineMemOperand::MONone;
@@ -751,7 +754,7 @@ MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
     return foldInlineAsmMemOperand(MI, Ops, FI, *this);
   } else {
     // Ask the target to do the actual folding.
-    NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS, VRM);
+    NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, CopyMI, LIS, VRM);
   }
 
   if (NewMI) {
@@ -804,6 +807,7 @@ MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
 MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
                                                  ArrayRef<unsigned> Ops,
                                                  MachineInstr &LoadMI,
+                                                 MachineInstr *&CopyMI,
                                                  LiveIntervals *LIS) const {
   assert(LoadMI.canFoldAsLoad() && "LoadMI isn't foldable!");
 #ifndef NDEBUG
@@ -830,7 +834,7 @@ MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
     return foldInlineAsmMemOperand(MI, Ops, FrameIndex, *this);
   } else {
     // Ask the target to do the actual folding.
-    NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS);
+    NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, CopyMI, LIS);
   }
 
   if (!NewMI)

diff  --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 4094526574d7a..988fe7014ddcf 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -6807,7 +6807,7 @@ void llvm::emitFrameOffset(MachineBasicBlock &MBB,
 
 MachineInstr *AArch64InstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
-    MachineBasicBlock::iterator InsertPt, int FrameIndex,
+    MachineBasicBlock::iterator InsertPt, int FrameIndex, MachineInstr *&CopyMI,
     LiveIntervals *LIS, VirtRegMap *VRM) const {
   // This is a bit of a hack. Consider this instruction:
   //

diff  --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.h b/llvm/lib/Target/AArch64/AArch64InstrInfo.h
index a1ec4cbffdf02..fa63c56739d62 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.h
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.h
@@ -382,12 +382,12 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
   bool isSubregFoldable() const override { return true; }
 
   using TargetInstrInfo::foldMemoryOperandImpl;
-  MachineInstr *
-  foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
-                        ArrayRef<unsigned> Ops,
-                        MachineBasicBlock::iterator InsertPt, int FrameIndex,
-                        LiveIntervals *LIS = nullptr,
-                        VirtRegMap *VRM = nullptr) const override;
+  MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
+                                      ArrayRef<unsigned> Ops,
+                                      MachineBasicBlock::iterator InsertPt,
+                                      int FrameIndex, MachineInstr *&CopyMI,
+                                      LiveIntervals *LIS = nullptr,
+                                      VirtRegMap *VRM = nullptr) const override;
 
   /// \returns true if a branch from an instruction with opcode \p BranchOpc
   ///  bytes is capable of jumping to a position \p BrOffset bytes away.

diff  --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
index 3179c65340d18..bc3052b139d18 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
@@ -10656,8 +10656,8 @@ bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); }
 
 MachineInstr *SIInstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
-    MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
-    VirtRegMap *VRM) const {
+    MachineBasicBlock::iterator InsertPt, int FrameIndex, MachineInstr *&CopyMI,
+    LiveIntervals *LIS, VirtRegMap *VRM) const {
   // This is a bit of a hack (copied from AArch64). Consider this instruction:
   //
   //   %0:sreg_32 = COPY $m0

diff  --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.h b/llvm/lib/Target/AMDGPU/SIInstrInfo.h
index a58e281de17a1..3c1232ac098a0 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.h
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.h
@@ -1682,7 +1682,7 @@ class SIInstrInfo final : public AMDGPUGenInstrInfo {
   MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
                                       ArrayRef<unsigned> Ops,
                                       MachineBasicBlock::iterator InsertPt,
-                                      int FrameIndex,
+                                      int FrameIndex, MachineInstr *&CopyMI,
                                       LiveIntervals *LIS = nullptr,
                                       VirtRegMap *VRM = nullptr) const override;
 

diff  --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
index db559f4949904..ee10d71727c39 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
@@ -905,8 +905,8 @@ std::optional<unsigned> getFoldedOpcode(MachineFunction &MF, MachineInstr &MI,
 // This is the version used during InlineSpiller::spillAroundUses
 MachineInstr *RISCVInstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
-    MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
-    VirtRegMap *VRM) const {
+    MachineBasicBlock::iterator InsertPt, int FrameIndex, MachineInstr *&CopyMI,
+    LiveIntervals *LIS, VirtRegMap *VRM) const {
 
   std::optional<unsigned> LoadOpc = getFoldedOpcode(MF, MI, Ops, STI);
   if (!LoadOpc)
@@ -952,7 +952,7 @@ static unsigned getLoadPredicatedOpcode(unsigned Opcode) {
 MachineInstr *RISCVInstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
     MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
-    LiveIntervals *LIS) const {
+    MachineInstr *&CopyMI, LiveIntervals *LIS) const {
   // For now, only handle RISCV::PseudoCCMOVGPR.
   if (MI.getOpcode() != RISCV::PseudoCCMOVGPR)
     return nullptr;

diff  --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.h b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
index 5c36ff7525200..119b2a7eae6bf 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.h
@@ -130,14 +130,14 @@ class RISCVInstrInfo : public RISCVGenInstrInfo {
   MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
                                       ArrayRef<unsigned> Ops,
                                       MachineBasicBlock::iterator InsertPt,
-                                      int FrameIndex,
+                                      int FrameIndex, MachineInstr *&CopyMI,
                                       LiveIntervals *LIS = nullptr,
                                       VirtRegMap *VRM = nullptr) const override;
 
   MachineInstr *foldMemoryOperandImpl(
       MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
       MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
-      LiveIntervals *LIS = nullptr) const override;
+      MachineInstr *&CopyMI, LiveIntervals *LIS = nullptr) const override;
 
   // Materializes the given integer Val into DstReg.
   void movImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,

diff  --git a/llvm/lib/Target/SystemZ/SystemZInstrInfo.cpp b/llvm/lib/Target/SystemZ/SystemZInstrInfo.cpp
index a76424eff1e49..5d85a64844592 100644
--- a/llvm/lib/Target/SystemZ/SystemZInstrInfo.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZInstrInfo.cpp
@@ -1225,7 +1225,7 @@ SystemZInstrInfo::getInverseOpcode(unsigned Opcode) const {
 
 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
-    MachineBasicBlock::iterator InsertPt, int FrameIndex,
+    MachineBasicBlock::iterator InsertPt, int FrameIndex, MachineInstr *&CopyMI,
     LiveIntervals *LIS, VirtRegMap *VRM) const {
   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
   MachineRegisterInfo &MRI = MF.getRegInfo();
@@ -1558,7 +1558,7 @@ MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
 MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
     MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
-    LiveIntervals *LIS) const {
+    MachineInstr *&CopyMI, LiveIntervals *LIS) const {
   MachineRegisterInfo *MRI = &MF.getRegInfo();
   MachineBasicBlock *MBB = MI.getParent();
 

diff  --git a/llvm/lib/Target/SystemZ/SystemZInstrInfo.h b/llvm/lib/Target/SystemZ/SystemZInstrInfo.h
index 029fe93d5b15c..9fbd8e9a28d1d 100644
--- a/llvm/lib/Target/SystemZ/SystemZInstrInfo.h
+++ b/llvm/lib/Target/SystemZ/SystemZInstrInfo.h
@@ -292,16 +292,16 @@ class SystemZInstrInfo : public SystemZGenInstrInfo {
                                    bool Invert) const override;
   std::optional<unsigned> getInverseOpcode(unsigned Opcode) const override;
 
-  MachineInstr *
-  foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
-                        ArrayRef<unsigned> Ops,
-                        MachineBasicBlock::iterator InsertPt, int FrameIndex,
-                        LiveIntervals *LIS = nullptr,
-                        VirtRegMap *VRM = nullptr) const override;
+  MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
+                                      ArrayRef<unsigned> Ops,
+                                      MachineBasicBlock::iterator InsertPt,
+                                      int FrameIndex, MachineInstr *&CopyMI,
+                                      LiveIntervals *LIS = nullptr,
+                                      VirtRegMap *VRM = nullptr) const override;
   MachineInstr *foldMemoryOperandImpl(
       MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
       MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
-      LiveIntervals *LIS = nullptr) const override;
+      MachineInstr *&CopyMI, LiveIntervals *LIS = nullptr) const override;
   bool expandPostRAPseudo(MachineInstr &MBBI) const override;
   bool reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
     override;

diff  --git a/llvm/lib/Target/X86/X86FastISel.cpp b/llvm/lib/Target/X86/X86FastISel.cpp
index 3dbe6d14c610e..2017897166f3c 100644
--- a/llvm/lib/Target/X86/X86FastISel.cpp
+++ b/llvm/lib/Target/X86/X86FastISel.cpp
@@ -3992,9 +3992,10 @@ bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
   SmallVector<MachineOperand, 8> AddrOps;
   AM.getFullAddress(AddrOps);
 
+  MachineInstr *CopyMI = nullptr;
   MachineInstr *Result = XII.foldMemoryOperandImpl(
       *FuncInfo.MF, *MI, OpNo, AddrOps, FuncInfo.InsertPt, Size, LI->getAlign(),
-      /*AllowCommute=*/true);
+      /*AllowCommute=*/true, CopyMI);
   if (!Result)
     return false;
 

diff  --git a/llvm/lib/Target/X86/X86InstrInfo.cpp b/llvm/lib/Target/X86/X86InstrInfo.cpp
index 11a8db9d39541..f4c3da1cd63d8 100644
--- a/llvm/lib/Target/X86/X86InstrInfo.cpp
+++ b/llvm/lib/Target/X86/X86InstrInfo.cpp
@@ -7485,7 +7485,8 @@ static void printFailMsgforFold(const MachineInstr &MI, unsigned Idx) {
 MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, unsigned OpNum,
     ArrayRef<MachineOperand> MOs, MachineBasicBlock::iterator InsertPt,
-    unsigned Size, Align Alignment, bool AllowCommute) const {
+    unsigned Size, Align Alignment, bool AllowCommute, MachineInstr *&CopyMI,
+    VirtRegMap *VRM) const {
   bool isSlowTwoMemOps = Subtarget.slowTwoMemOps();
   unsigned Opc = MI.getOpcode();
 
@@ -7542,6 +7543,26 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
   // Utilize the mapping NonNDD if NDD memory variant is not preferred.
   bool NoNDDM = NonNDOpc && !Subtarget.hasNDDM();
 
+  MachineRegisterInfo &MRI = MF.getRegInfo();
+  if (NoNDDM && !IsTwoAddr && !MRI.isSSA()) {
+    // Bail out if dst has subreg. It happens during register-coalescer from
+    // 704B  %19:gr32 = SUB32rr_ND killed %0:gr32, killed %7:gr32, ...
+    // 752B  undef %23.sub_32bit:gr64 = COPY killed %19:gr32
+    // 768B  %25:gr32 = LEA64_32r killed %23:gr64, 1, killed %21:gr64_nosp, ...
+    // to
+    // 704B  undef %23.sub_32bit:gr64_with_sub_8bit = SUB32rr_ND %0:gr32, ...
+    // 768B  %25:gr32 = LEA64_32r %23:gr64_with_sub_8bit, 1, %21:gr64_nosp, ...
+    // Machine verifier fails if we try to tie %23 to the source.
+    if (MI.getOperand(0).getSubReg())
+      return nullptr;
+
+    // Bail out if dst has been assigned a physical register. Otherwise, we
+    // cannot update LiveRegMatrix properly.
+    if (VRM && VRM->getPhys(MI.getOperand(0).getReg()) &&
+        MI.getOperand(0).getReg() != MI.getOperand(1).getReg())
+      return nullptr;
+  }
+
   const X86FoldTableEntry *I =
       IsTwoAddr ? lookupTwoAddrFoldTable(NonNDOpc ? NonNDOpc : Opc)
                 : lookupFoldTable(NoNDDM ? NonNDOpc : Opc, OpNum);
@@ -7594,15 +7615,19 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
 
     if (NoNDDM && !IsTwoAddr) {
       Register SrcReg = MI.getOperand(1).getReg();
-      if (MI.killsRegister(SrcReg, /*TRI=*/nullptr))
+      unsigned SrcSub = MI.getOperand(1).getSubReg();
+      if (MI.killsRegister(SrcReg, /*TRI=*/nullptr) ||
+          MI.getOperand(0).getReg() == SrcReg)
         return NewMI;
 
       const TargetRegisterClass &RC = *MF.getRegInfo().getRegClass(SrcReg);
-      Register NewSrc = MF.getRegInfo().createVirtualRegister(&RC);
-      BuildMI(*NewMI->getParent(), *NewMI, MI.getDebugLoc(),
-              get(TargetOpcode::COPY))
-          .addReg(NewSrc, RegState::Define)
-          .addReg(SrcReg);
+      Register NewSrc = MRI.isSSA() ? MRI.createVirtualRegister(&RC)
+                                    : MI.getOperand(0).getReg();
+
+      CopyMI = BuildMI(*NewMI->getParent(), *NewMI, MI.getDebugLoc(),
+                       get(TargetOpcode::COPY))
+                   .addDef(NewSrc)
+                   .addReg(SrcReg, {}, SrcSub);
       NewMI->getOperand(1).setReg(NewSrc);
     }
     return NewMI;
@@ -7618,7 +7643,7 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
     }
     // Attempt to fold with the commuted version of the instruction.
     NewMI = foldMemoryOperandImpl(MF, MI, CommuteOpIdx2, MOs, InsertPt, Size,
-                                  Alignment, /*AllowCommute=*/false);
+                                  Alignment, /*AllowCommute=*/false, CopyMI);
     if (NewMI)
       return NewMI;
     // Folding failed again - undo the commute before returning.
@@ -7631,8 +7656,8 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
 
 MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
-    MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
-    VirtRegMap *VRM) const {
+    MachineBasicBlock::iterator InsertPt, int FrameIndex, MachineInstr *&CopyMI,
+    LiveIntervals *LIS, VirtRegMap *VRM) const {
   // Check switch flag
   if (NoFusing)
     return nullptr;
@@ -7665,9 +7690,9 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
         std::min(Alignment, Subtarget.getFrameLowering()->getStackAlign());
 
   auto Impl = [&]() {
-    return foldMemoryOperandImpl(MF, MI, Ops[0],
-                                 MachineOperand::CreateFI(FrameIndex), InsertPt,
-                                 Size, Alignment, /*AllowCommute=*/true);
+    return foldMemoryOperandImpl(
+        MF, MI, Ops[0], MachineOperand::CreateFI(FrameIndex), InsertPt, Size,
+        Alignment, /*AllowCommute=*/true, CopyMI, VRM);
   };
   if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
     unsigned NewOpc = 0;
@@ -8147,7 +8172,7 @@ static bool isNonFoldablePartialRegisterLoad(const MachineInstr &LoadMI,
 MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
     MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
-    LiveIntervals *LIS) const {
+    MachineInstr *&CopyMI, LiveIntervals *LIS) const {
 
   // If LoadMI is a masked load, check MI having the same mask.
   const MCInstrDesc &MCID = get(LoadMI.getOpcode());
@@ -8199,7 +8224,8 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
   if (isLoadFromStackSlot(LoadMI, FrameIndex)) {
     if (isNonFoldablePartialRegisterLoad(LoadMI, MI, MF))
       return nullptr;
-    return foldMemoryOperandImpl(MF, MI, Ops, InsertPt, FrameIndex, LIS);
+    return foldMemoryOperandImpl(MF, MI, Ops, InsertPt, FrameIndex, CopyMI,
+                                 LIS);
   }
 
   // Check switch flag
@@ -8444,7 +8470,8 @@ MachineInstr *X86InstrInfo::foldMemoryOperandImpl(
   }
   }
   return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, InsertPt,
-                               /*Size=*/0, Alignment, /*AllowCommute=*/true);
+                               /*Size=*/0, Alignment, /*AllowCommute=*/true,
+                               CopyMI);
 }
 
 MachineInstr *

diff  --git a/llvm/lib/Target/X86/X86InstrInfo.h b/llvm/lib/Target/X86/X86InstrInfo.h
index 1e2a94a700c3e..1c37fcd7e73b9 100644
--- a/llvm/lib/Target/X86/X86InstrInfo.h
+++ b/llvm/lib/Target/X86/X86InstrInfo.h
@@ -492,19 +492,19 @@ class X86InstrInfo final : public X86GenInstrInfo {
   /// is likely that the referenced instruction has been changed.
   ///
   /// \returns true on success.
-  MachineInstr *
-  foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
-                        ArrayRef<unsigned> Ops,
-                        MachineBasicBlock::iterator InsertPt, int FrameIndex,
-                        LiveIntervals *LIS = nullptr,
-                        VirtRegMap *VRM = nullptr) const override;
+  MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
+                                      ArrayRef<unsigned> Ops,
+                                      MachineBasicBlock::iterator InsertPt,
+                                      int FrameIndex, MachineInstr *&CopyMI,
+                                      LiveIntervals *LIS = nullptr,
+                                      VirtRegMap *VRM = nullptr) const override;
 
   /// Same as the previous version except it allows folding of any load and
   /// store from / to any address, not just from a specific stack slot.
   MachineInstr *foldMemoryOperandImpl(
       MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
       MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
-      LiveIntervals *LIS = nullptr) const override;
+      MachineInstr *&CopyMI, LiveIntervals *LIS = nullptr) const override;
 
   bool
   unfoldMemoryOperand(MachineFunction &MF, MachineInstr &MI, Register Reg,
@@ -582,7 +582,8 @@ class X86InstrInfo final : public X86GenInstrInfo {
                                       ArrayRef<MachineOperand> MOs,
                                       MachineBasicBlock::iterator InsertPt,
                                       unsigned Size, Align Alignment,
-                                      bool AllowCommute) const;
+                                      bool AllowCommute, MachineInstr *&CopyMI,
+                                      VirtRegMap *VRM = nullptr) const;
 
   bool isHighLatencyDef(int opc) const override;
 

diff  --git a/llvm/test/CodeGen/X86/apx/or.ll b/llvm/test/CodeGen/X86/apx/or.ll
index 594ed7b29216b..0a6e98e89f3fc 100644
--- a/llvm/test/CodeGen/X86/apx/or.ll
+++ b/llvm/test/CodeGen/X86/apx/or.ll
@@ -1240,3 +1240,41 @@ entry:
   store i64 %or, ptr %a
   ret void
 }
+
+define i64 @pr191165(i32 %a, ptr %b) {
+; NDD-LABEL: pr191165:
+; NDD:       # %bb.0:
+; NDD-NEXT:    movl (%rsi), %eax # encoding: [0x8b,0x06]
+; NDD-NEXT:    orl %edi, %eax # encoding: [0x09,0xf8]
+; NDD-NEXT:    movl %eax, 0 # encoding: [0x89,0x04,0x25,0x00,0x00,0x00,0x00]
+; NDD-NEXT:    movslq %edi, %rax # encoding: [0x48,0x63,0xc7]
+; NDD-NEXT:    retq # encoding: [0xc3]
+;
+; IMMONLY-LABEL: pr191165:
+; IMMONLY:       # %bb.0:
+; IMMONLY-NEXT:    movl (%rsi), %eax # encoding: [0x8b,0x06]
+; IMMONLY-NEXT:    orl %edi, %eax # encoding: [0x09,0xf8]
+; IMMONLY-NEXT:    movl %eax, 0 # encoding: [0x89,0x04,0x25,0x00,0x00,0x00,0x00]
+; IMMONLY-NEXT:    movslq %edi, %rax # encoding: [0x48,0x63,0xc7]
+; IMMONLY-NEXT:    retq # encoding: [0xc3]
+;
+; MEM-LABEL: pr191165:
+; MEM:       # %bb.0:
+; MEM-NEXT:    orl (%rsi), %edi, %eax # encoding: [0x62,0xf4,0x7c,0x18,0x0b,0x3e]
+; MEM-NEXT:    movl %eax, 0 # encoding: [0x89,0x04,0x25,0x00,0x00,0x00,0x00]
+; MEM-NEXT:    movslq %edi, %rax # encoding: [0x48,0x63,0xc7]
+; MEM-NEXT:    retq # encoding: [0xc3]
+;
+; NF-LABEL: pr191165:
+; NF:       # %bb.0:
+; NF-NEXT:    movl (%rsi), %eax # encoding: [0x8b,0x06]
+; NF-NEXT:    orl %edi, %eax # encoding: [0x09,0xf8]
+; NF-NEXT:    movl %eax, 0 # encoding: [0x89,0x04,0x25,0x00,0x00,0x00,0x00]
+; NF-NEXT:    movslq %edi, %rax # encoding: [0x48,0x63,0xc7]
+; NF-NEXT:    retq # encoding: [0xc3]
+  %x = load i32, ptr %b, align 4
+  %y = or i32 %x, %a
+  store volatile i32 %y, ptr null, align 4
+  %z = sext i32 %a to i64
+  ret i64 %z
+}

diff  --git a/llvm/test/CodeGen/X86/apx/pr191368.ll b/llvm/test/CodeGen/X86/apx/pr191368.ll
new file mode 100644
index 0000000000000..917f582d45124
--- /dev/null
+++ b/llvm/test/CodeGen/X86/apx/pr191368.ll
@@ -0,0 +1,385 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-pc-windows-gnu -mattr=ndd -verify-machineinstrs | FileCheck %s
+
+define ptr @foo(ptr %a, i32 %b, ptr %c, ptr %d, i32 %e, i32 %f, i32 %g) nounwind {
+; CHECK-LABEL: foo:
+; CHECK:       # %bb.0: # %l0
+; CHECK-NEXT:    pushq %r15
+; CHECK-NEXT:    pushq %r14
+; CHECK-NEXT:    pushq %r13
+; CHECK-NEXT:    pushq %r12
+; CHECK-NEXT:    pushq %rsi
+; CHECK-NEXT:    pushq %rdi
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    subq $40, %rsp
+; CHECK-NEXT:    movq %r9, %r14
+; CHECK-NEXT:    movq %r8, %rbx
+; CHECK-NEXT:    movl %edx, %esi
+; CHECK-NEXT:    movq %rcx, %rdi
+; CHECK-NEXT:    movl {{[0-9]+}}(%rsp), %r15d
+; CHECK-NEXT:    movl {{[0-9]+}}(%rsp), %r13d
+; CHECK-NEXT:    xorl %ebp, %ebp
+; CHECK-NEXT:    xorl %ecx, %ecx
+; CHECK-NEXT:    callq bitstob
+; CHECK-NEXT:    cmpl $-1, %r15d
+; CHECK-NEXT:    movl $-1, %r12d
+; CHECK-NEXT:    cmovll %r15d, %r12d
+; CHECK-NEXT:    cmpl $5, %r13d
+; CHECK-NEXT:    ja .LBB0_5
+; CHECK-NEXT:  # %bb.1: # %l0
+; CHECK-NEXT:    movl $3, %eax
+; CHECK-NEXT:    btl %r13d, %eax
+; CHECK-NEXT:    jb .LBB0_4
+; CHECK-NEXT:  # %bb.2: # %l0
+; CHECK-NEXT:    movl $20, %eax
+; CHECK-NEXT:    btl %r13d, %eax
+; CHECK-NEXT:    jb .LBB0_6
+; CHECK-NEXT:  # %bb.3: # %l1
+; CHECK-NEXT:    movl {{[0-9]+}}(%rsp), %eax
+; CHECK-NEXT:    movl %eax, %ebp
+; CHECK-NEXT:  .LBB0_4: # %.sink.split
+; CHECK-NEXT:    movl %ebp, (%r14)
+; CHECK-NEXT:  .LBB0_5: # %l2
+; CHECK-NEXT:    callq __rv_alloc_D2A
+; CHECK-NEXT:    movl $0, (%rbx)
+; CHECK-NEXT:    movl {{[0-9]+}}(%rsp), %eax
+; CHECK-NEXT:    subl %eax, %r12d, %eax
+; CHECK-NEXT:    movq 0, %rcx
+; CHECK-NEXT:    leal 1(%rax,%r15), %eax
+; CHECK-NEXT:    movl %eax, (%rdi)
+; CHECK-NEXT:    callq __Bfree_D2A
+; CHECK-NEXT:    movl %esi, (%rdi)
+; CHECK-NEXT:  .LBB0_6: # %common.ret1
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    addq $40, %rsp
+; CHECK-NEXT:    popq %rbx
+; CHECK-NEXT:    popq %rbp
+; CHECK-NEXT:    popq %rdi
+; CHECK-NEXT:    popq %rsi
+; CHECK-NEXT:    popq %r12
+; CHECK-NEXT:    popq %r13
+; CHECK-NEXT:    popq %r14
+; CHECK-NEXT:    popq %r15
+; CHECK-NEXT:    retq
+l0:
+  %x = tail call ptr @bitstob(ptr null)
+  %y = tail call i32 @llvm.smin.i32(i32 %g, i32 -1)
+  switch i32 %e, label %l2 [
+    i32 0, label %.sink.split
+    i32 1, label %.sink.split
+    i32 2, label %common.ret1
+    i32 4, label %common.ret1
+    i32 3, label %l1
+    i32 5, label %l1
+  ]
+
+common.ret1:                                      ; preds = %l2, %l0, %l0
+  ret ptr null
+
+l1:                                               ; preds = %l0, %l0
+  br label %.sink.split
+
+.sink.split:                                      ; preds = %l0, %l0, %l1
+  %.sink = phi i32 [ %f, %l1 ], [ 0, %l0 ], [ 0, %l0 ]
+  store i32 %.sink, ptr %d, align 4
+  br label %l2
+
+l2:                                               ; preds = %.sink.split, %l0
+  %b2 = tail call ptr @__rv_alloc_D2A()
+  store i32 0, ptr %c, align 4
+  %reass.sub = sub i32 %y, %f
+  %b3 = add i32 %reass.sub, 1
+  %b4 = load volatile ptr, ptr null, align 4294967296
+  %b5 = add i32 %b3, %g
+  store i32 %b5, ptr %a, align 4
+  tail call void @__Bfree_D2A()
+  store i32 %b, ptr %a, align 4
+  br label %common.ret1
+}
+
+declare ptr @bitstob(ptr)
+declare void @__Bfree_D2A()
+declare ptr @__rv_alloc_D2A()
+declare i32 @llvm.smin.i32(i32, i32)
+
+define i64 @bar(ptr %a, i64 %b, ptr %f, ptr %c, ptr %d, i8 %e, i64 %g, ptr %h, ptr %i, i8 %j, i8 %b0, i8 %b1, i8 %b2) nounwind {
+; CHECK-LABEL: bar:
+; CHECK:       # %bb.0: # %b3
+; CHECK-NEXT:    pushq %r15
+; CHECK-NEXT:    pushq %r14
+; CHECK-NEXT:    pushq %r13
+; CHECK-NEXT:    pushq %r12
+; CHECK-NEXT:    pushq %rsi
+; CHECK-NEXT:    pushq %rdi
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    pushq %rax
+; CHECK-NEXT:    movq %rdx, (%rsp) # 8-byte Spill
+; CHECK-NEXT:    movzbl {{[0-9]+}}(%rsp), %edx
+; CHECK-NEXT:    movzbl {{[0-9]+}}(%rsp), %r10d
+; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %rbx
+; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %rdi
+; CHECK-NEXT:    movzbl {{[0-9]+}}(%rsp), %r15d
+; CHECK-NEXT:    xorl %r12d, %r12d
+; CHECK-NEXT:    .p2align 4
+; CHECK-NEXT:  .LBB1_1: # %b4
+; CHECK-NEXT:    # =>This Loop Header: Depth=1
+; CHECK-NEXT:    # Child Loop BB1_2 Depth 2
+; CHECK-NEXT:    movq (%rsp), %rax # 8-byte Reload
+; CHECK-NEXT:    imulq %rax, %r12
+; CHECK-NEXT:    xorl %r13d, %r13d
+; CHECK-NEXT:    movzbl {{[0-9]+}}(%rsp), %r11d
+; CHECK-NEXT:    movzbl {{[0-9]+}}(%rsp), %esi
+; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %r14
+; CHECK-NEXT:    .p2align 4
+; CHECK-NEXT:  .LBB1_2: # %b6
+; CHECK-NEXT:    # Parent Loop BB1_1 Depth=1
+; CHECK-NEXT:    # => This Inner Loop Header: Depth=2
+; CHECK-NEXT:    movzbl (%rbx), %eax
+; CHECK-NEXT:    movq %rdi, %rbp
+; CHECK-NEXT:    movq %rbx, %rdi
+; CHECK-NEXT:    movl %r10d, %ebx
+; CHECK-NEXT:    movl %edx, %r10d
+; CHECK-NEXT:    movb $1, %dl
+; CHECK-NEXT:    subb %al, %dl
+; CHECK-NEXT:    movb %r11b, (%r14)
+; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %rax
+; CHECK-NEXT:    movb %sil, (%rax)
+; CHECK-NEXT:    movb %dl, (%r8)
+; CHECK-NEXT:    movl %r10d, %edx
+; CHECK-NEXT:    movl %ebx, %r10d
+; CHECK-NEXT:    movq %rdi, %rbx
+; CHECK-NEXT:    movq %rbp, %rdi
+; CHECK-NEXT:    movb %r10b, 0
+; CHECK-NEXT:    movb %dl, (%rcx,%r12)
+; CHECK-NEXT:    movb %r15b, (%r9)
+; CHECK-NEXT:    testq %r13, %r13
+; CHECK-NEXT:    movq %rbp, %r13
+; CHECK-NEXT:    jne .LBB1_2
+; CHECK-NEXT:  # %bb.3: # in Loop: Header=BB1_1 Depth=1
+; CHECK-NEXT:    movl $1, %r12d
+; CHECK-NEXT:    jmp .LBB1_1
+b3:
+  br label %b4
+
+b4:                                               ; preds = %b6, %b3
+  %b5 = phi i64 [ 0, %b3 ], [ 1, %b6 ]
+  br label %b6
+
+b6:                                               ; preds = %b6, %b4
+  %b7 = phi i64 [ 0, %b4 ], [ %g, %b6 ]
+  %b8 = load i8, ptr %i, align 1
+  %b9 = sub i8 1, %b8
+  store i8 %b1, ptr %h, align 1
+  store i8 %j, ptr %d, align 1
+  store i8 %b9, ptr %f, align 1
+  store i8 %b0, ptr null, align 1
+  %f0 = mul i64 %b5, %b
+  %f1 = getelementptr i8, ptr %a, i64 %f0
+  store i8 %b2, ptr %f1, align 1
+  store i8 %e, ptr %c, align 1
+  %f2 = icmp eq i64 %b7, 0
+  br i1 %f2, label %b4, label %b6
+}
+
+define i32 @baz(ptr %a, ptr %b, ptr %c, ptr %d, i64 %e, i32 %f, i32 %g, i1 %h) nounwind {
+; CHECK-LABEL: baz:
+; CHECK:       # %bb.0: # %.split
+; CHECK-NEXT:    pushq %r15
+; CHECK-NEXT:    pushq %r14
+; CHECK-NEXT:    pushq %r13
+; CHECK-NEXT:    pushq %r12
+; CHECK-NEXT:    pushq %rsi
+; CHECK-NEXT:    pushq %rdi
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    subq $72, %rsp
+; CHECK-NEXT:    movq %r9, %rdi
+; CHECK-NEXT:    movq %r8, %rbx
+; CHECK-NEXT:    movq %rdx, %r14
+; CHECK-NEXT:    movq %rcx, %r15
+; CHECK-NEXT:    xorl %esi, %esi
+; CHECK-NEXT:    movzbl {{[0-9]+}}(%rsp), %ebp
+; CHECK-NEXT:    movl {{[0-9]+}}(%rsp), %r12d
+; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %r13
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    .p2align 4
+; CHECK-NEXT:  .LBB2_1: # %.preheader14
+; CHECK-NEXT:    # =>This Loop Header: Depth=1
+; CHECK-NEXT:    # Child Loop BB2_3 Depth 2
+; CHECK-NEXT:    testb $1, %bpl
+; CHECK-NEXT:    jne .LBB2_6
+; CHECK-NEXT:  # %bb.2: # %j.preheader
+; CHECK-NEXT:    # in Loop: Header=BB2_1 Depth=1
+; CHECK-NEXT:    movl %eax, {{[-0-9]+}}(%r{{[sb]}}p) # 4-byte Spill
+; CHECK-NEXT:    .p2align 4
+; CHECK-NEXT:  .LBB2_3: # %j
+; CHECK-NEXT:    # Parent Loop BB2_1 Depth=1
+; CHECK-NEXT:    # => This Inner Loop Header: Depth=2
+; CHECK-NEXT:    movl %eax, 0
+; CHECK-NEXT:    movq %r13, {{[0-9]+}}(%rsp)
+; CHECK-NEXT:    movq %rdi, {{[0-9]+}}(%rsp)
+; CHECK-NEXT:    movq $0, {{[0-9]+}}(%rsp)
+; CHECK-NEXT:    movq %r15, %rcx
+; CHECK-NEXT:    movq %r14, %rdx
+; CHECK-NEXT:    movq %rbx, %r8
+; CHECK-NEXT:    xorl %r9d, %r9d
+; CHECK-NEXT:    callq *%rsi
+; CHECK-NEXT:    testb $1, %bpl
+; CHECK-NEXT:    movl %r12d, %eax
+; CHECK-NEXT:    je .LBB2_3
+; CHECK-NEXT:  # %bb.4: # %b2
+; CHECK-NEXT:    # in Loop: Header=BB2_1 Depth=1
+; CHECK-NEXT:    movl {{[-0-9]+}}(%r{{[sb]}}p), %eax # 4-byte Reload
+; CHECK-NEXT:    orl {{[0-9]+}}(%rsp), %eax
+; CHECK-NEXT:    testb $1, %bpl
+; CHECK-NEXT:    je .LBB2_1
+; CHECK-NEXT:  # %bb.5:
+; CHECK-NEXT:    movl $1, %esi
+; CHECK-NEXT:  .LBB2_6: # %.loopexit
+; CHECK-NEXT:    movl %esi, %eax
+; CHECK-NEXT:    addq $72, %rsp
+; CHECK-NEXT:    popq %rbx
+; CHECK-NEXT:    popq %rbp
+; CHECK-NEXT:    popq %rdi
+; CHECK-NEXT:    popq %rsi
+; CHECK-NEXT:    popq %r12
+; CHECK-NEXT:    popq %r13
+; CHECK-NEXT:    popq %r14
+; CHECK-NEXT:    popq %r15
+; CHECK-NEXT:    retq
+.split:
+  br label %.preheader14
+
+.preheader14:                                     ; preds = %b2, %.split
+  %i = phi i32 [ %b3, %b2 ], [ 0, %.split ]
+  br i1 %h, label %.loopexit, label %j
+
+j:                                                ; preds = %j, %.preheader14
+  %b0 = phi i32 [ %g, %j ], [ %i, %.preheader14 ]
+  store i32 %b0, ptr null, align 4
+  %b1 = call i32 null(ptr %a, ptr %b, ptr %c, ptr null, ptr null, ptr %d, i64 %e)
+  br i1 %h, label %b2, label %j
+
+b2:                                               ; preds = %j
+  %b3 = or i32 %i, %f
+  br i1 %h, label %.loopexit, label %.preheader14
+
+.loopexit:                                        ; preds = %b2, %.preheader14
+  %b4 = phi i32 [ 0, %.preheader14 ], [ 1, %b2 ]
+  ret i32 %b4
+}
+
+define i32 @pr190962(ptr %a, ptr %b, ptr %c, i64 %d, i64 %e, i64 %f) nounwind {
+; CHECK-LABEL: pr190962:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    pushq %r15
+; CHECK-NEXT:    pushq %r14
+; CHECK-NEXT:    pushq %r13
+; CHECK-NEXT:    pushq %r12
+; CHECK-NEXT:    pushq %rsi
+; CHECK-NEXT:    pushq %rdi
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    subq $56, %rsp
+; CHECK-NEXT:    movq %r9, %rdi
+; CHECK-NEXT:    movq %rdx, %rbx
+; CHECK-NEXT:    movq %rcx, %rsi
+; CHECK-NEXT:    callq f2
+; CHECK-NEXT:    movq %rdi, %rax
+; CHECK-NEXT:    imulq {{[0-9]+}}(%rsp), %rax
+; CHECK-NEXT:    testq %rax, %rax
+; CHECK-NEXT:    je .LBB3_3
+; CHECK-NEXT:  # %bb.1: # %l1
+; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %r13
+; CHECK-NEXT:    movq 0, %r15
+; CHECK-NEXT:    callq f2
+; CHECK-NEXT:    movq 0, %r12
+; CHECK-NEXT:    xorl %ebp, %ebp
+; CHECK-NEXT:    orq %r13, %r15
+; CHECK-NEXT:    jne .LBB3_2
+; CHECK-NEXT:  # %bb.4: # %l2
+; CHECK-NEXT:    movq %rax, %r14
+; CHECK-NEXT:    movq %rbx, %rcx
+; CHECK-NEXT:    callq f4
+; CHECK-NEXT:    movl $1, %ebp
+; CHECK-NEXT:    orq %r12, %r14
+; CHECK-NEXT:    je .LBB3_5
+; CHECK-NEXT:  .LBB3_2: # %common.ret1.sink.split
+; CHECK-NEXT:    callq f3
+; CHECK-NEXT:    xorl %ecx, %ecx
+; CHECK-NEXT:    xorl %edx, %edx
+; CHECK-NEXT:    xorl %r8d, %r8d
+; CHECK-NEXT:    movl %ebp, %r9d
+; CHECK-NEXT:    callq f1
+; CHECK-NEXT:    callq f3
+; CHECK-NEXT:  .LBB3_3: # %common.ret1
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    addq $56, %rsp
+; CHECK-NEXT:    popq %rbx
+; CHECK-NEXT:    popq %rbp
+; CHECK-NEXT:    popq %rdi
+; CHECK-NEXT:    popq %rsi
+; CHECK-NEXT:    popq %r12
+; CHECK-NEXT:    popq %r13
+; CHECK-NEXT:    popq %r14
+; CHECK-NEXT:    popq %r15
+; CHECK-NEXT:    retq
+; CHECK-NEXT:  .LBB3_5: # %l3
+; CHECK-NEXT:    testq %rdi, %rdi
+; CHECK-NEXT:    movq {{[0-9]+}}(%rsp), %rax
+; CHECK-NEXT:    je .LBB3_3
+; CHECK-NEXT:  # %bb.6: # %l4
+; CHECK-NEXT:    movl $0, (%rsi)
+; CHECK-NEXT:    movq %rax, {{[0-9]+}}(%rsp)
+; CHECK-NEXT:    movq $0, {{[0-9]+}}(%rsp)
+; CHECK-NEXT:    movq $0, {{[0-9]+}}(%rsp)
+; CHECK-NEXT:    xorl %ecx, %ecx
+; CHECK-NEXT:    xorl %edx, %edx
+; CHECK-NEXT:    xorl %r8d, %r8d
+; CHECK-NEXT:    xorl %r9d, %r9d
+; CHECK-NEXT:    callq f1
+  %g = tail call i64 @f2()
+  %h = mul i64 %e, %d
+  %i = icmp eq i64 %h, 0
+  br i1 %i, label %common.ret1, label %l1
+
+common.ret1.sink.split:                           ; preds = %l2, %l1
+  %.sink = phi i32 [ 0, %l1 ], [ 1, %l2 ]
+  tail call void @f3()
+  %b0 = tail call i32 (i32, ptr, ...) @f1(i32 0, ptr null, ptr null, i32 %.sink)
+  tail call void @f3()
+  br label %common.ret1
+
+common.ret1:                                      ; preds = %common.ret1.sink.split, %l3, %6
+  ret i32 0
+
+l1:                                               ; preds = %6
+  %b2 = load volatile i64, ptr null, align 4294967296
+  %b3 = tail call i64 @f2()
+  %b4 = load volatile i64, ptr null, align 4294967296
+  %b5 = or i64 %b2, %f
+  %.not = icmp eq i64 %b5, 0
+  br i1 %.not, label %l2, label %common.ret1.sink.split
+
+l2:                                               ; preds = %l1
+  %b7 = tail call i64 @f4(ptr %b)
+  %b8 = or i64 %b4, %b3
+  %.not1 = icmp eq i64 %b8, 0
+  br i1 %.not1, label %l3, label %common.ret1.sink.split
+
+l3:                                               ; preds = %l2
+  %c0 = icmp eq i64 %d, 0
+  br i1 %c0, label %common.ret1, label %l4
+
+l4:                                               ; preds = %l3
+  store i32 0, ptr %a, align 4
+  %c2 = tail call i32 (i32, ptr, ...) @f1(i32 0, ptr null, ptr null, i32 0, i64 0, i64 0, i64 %e)
+  unreachable
+}
+
+declare i32 @f1(i32, ptr, ...)
+declare i64 @f2()
+declare void @f3()
+declare i64 @f4(ptr)


        


More information about the llvm-commits mailing list