diff --git a/llvm/lib/CodeGen/TargetInstrInfo.cpp b/llvm/lib/CodeGen/TargetInstrInfo.cpp --- a/llvm/lib/CodeGen/TargetInstrInfo.cpp +++ b/llvm/lib/CodeGen/TargetInstrInfo.cpp @@ -603,7 +603,7 @@ NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS, VRM); } - if (NewMI) { + if (NewMI && NewMI->mayLoadOrStore()) { NewMI->setMemRefs(MF, MI.memoperands()); // Add a memory operand, foldMemoryOperandImpl doesn't do that. assert((!(Flags & MachineMemOperand::MOStore) || @@ -676,6 +676,9 @@ if (!NewMI) return nullptr; + if (!NewMI->mayLoad()) + return NewMI; + // Copy the memoperands from the load to the folded instruction. if (MI.memoperands_empty()) { NewMI->setMemRefs(MF, LoadMI.memoperands());