Index: include/llvm/CodeGen/MachineInstr.h =================================================================== --- include/llvm/CodeGen/MachineInstr.h +++ include/llvm/CodeGen/MachineInstr.h @@ -1170,6 +1170,12 @@ assert(NumMemRefs == NewMemRefsEnd - NewMemRefs && "Too many memrefs"); } + /// clearMemRefs - Clear this MachineInstr's memory reference descriptor list. + void clearMemRefs() { + MemRefs = nullptr; + NumMemRefs = 0; + } + private: /// getRegInfo - If this instruction is embedded into a MachineFunction, /// return the MachineRegisterInfo object for the current function, otherwise Index: lib/CodeGen/BranchFolding.cpp =================================================================== --- lib/CodeGen/BranchFolding.cpp +++ lib/CodeGen/BranchFolding.cpp @@ -727,6 +727,14 @@ return true; } +static void removeMMOsFromMemoryOperations(MachineBasicBlock &MBB) { + // Remove all MMOs from memory operations. This ensures later passes + // conservatively compute dependencies. + for (auto &MII : MBB) + if (MII.mayLoad() || MII.mayStore()) + MII.clearMemRefs(); +} + // See if any of the blocks in MergePotentials (which all have a common single // successor, or all have no successor) can be tail-merged. If there is a // successor, any blocks in MergePotentials that are not tail-merged and @@ -828,6 +836,9 @@ MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock(); + // Remove all MMOs from memory operations. + removeMMOsFromMemoryOperations(*MBB); + // Recompute commont tail MBB's edge weights and block frequency. setCommonTailEdgeWeights(*MBB);