diff --git a/llvm/lib/Target/X86/X86OptimizeLEAs.cpp b/llvm/lib/Target/X86/X86OptimizeLEAs.cpp --- a/llvm/lib/Target/X86/X86OptimizeLEAs.cpp +++ b/llvm/lib/Target/X86/X86OptimizeLEAs.cpp @@ -653,8 +653,12 @@ // isReplaceable function. Register FirstVReg = First.getOperand(0).getReg(); Register LastVReg = Last.getOperand(0).getReg(); - for (MachineOperand &MO : - llvm::make_early_inc_range(MRI->use_operands(LastVReg))) { + // We use use_empty here instead of the combination of + // make_early_inc_range and use_operands because we could replace two or + // more uses in a debug instruction in one iteration and that would + // deeply confuse make_early_inc_range. + while (!MRI->use_empty(LastVReg)) { + MachineOperand &MO = *MRI->use_begin(LastVReg); MachineInstr &MI = *MO.getParent(); if (MI.isDebugValue()) {