Index: include/llvm/CodeGen/MachineInstr.h =================================================================== --- include/llvm/CodeGen/MachineInstr.h +++ include/llvm/CodeGen/MachineInstr.h @@ -93,6 +93,7 @@ uint8_t NumMemRefs; // Information on memory references. mmo_iterator MemRefs; + bool NonVolatileLdSt; DebugLoc debugLoc; // Source line information. @@ -1179,9 +1180,10 @@ } /// Clear this MachineInstr's memory reference descriptor list. - void clearMemRefs() { + void clearMemRefs(bool IsNonVolatileLdSt) { MemRefs = nullptr; NumMemRefs = 0; + NonVolatileLdSt = IsNonVolatileLdSt; } /// Break any tie involving OpIdx. Index: lib/CodeGen/BranchFolding.cpp =================================================================== --- lib/CodeGen/BranchFolding.cpp +++ lib/CodeGen/BranchFolding.cpp @@ -793,7 +793,8 @@ if (MBBICommon->mayLoad() || MBBICommon->mayStore()) if (!hasIdenticalMMOs(&*MBBI, &*MBBICommon)) - MBBICommon->clearMemRefs(); + MBBICommon->clearMemRefs(!MBBICommon->hasOrderedMemoryRef() && + !MBBI->hasOrderedMemoryRef()); ++MBBI; ++MBBICommon; Index: lib/CodeGen/MachineInstr.cpp =================================================================== --- lib/CodeGen/MachineInstr.cpp +++ lib/CodeGen/MachineInstr.cpp @@ -645,7 +645,7 @@ DebugLoc dl, bool NoImp) : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0), AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr), - debugLoc(std::move(dl)) { + NonVolatileLdSt(false), debugLoc(std::move(dl)) { assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); // Reserve space for the expected number of operands. @@ -662,10 +662,9 @@ /// MachineInstr ctor - Copies MachineInstr arg exactly /// MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) - : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0), - Flags(0), AsmPrinterFlags(0), - NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs), - debugLoc(MI.getDebugLoc()) { + : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0), + Flags(0), AsmPrinterFlags(0), NumMemRefs(MI.NumMemRefs), + MemRefs(MI.MemRefs), NonVolatileLdSt(false), debugLoc(MI.getDebugLoc()) { assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor"); CapOperands = OperandCapacity::get(MI.getNumOperands()); @@ -1442,7 +1441,7 @@ // Otherwise, if the instruction has no memory reference information, // conservatively assume it wasn't preserved. - if (memoperands_empty()) + if (!NonVolatileLdSt && memoperands_empty()) return true; // Check the memory reference information for ordered references.