Index: llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp =================================================================== --- llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp +++ llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp @@ -317,6 +317,8 @@ MF.getFunction(), "amdgpu-max-memory-clause", MaxClause); for (MachineBasicBlock &MBB : MF) { + GCNDownwardRPTracker RPT(*LIS); + bool InitializedInBlock = false; MachineBasicBlock::instr_iterator Next; for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; I = Next) { MachineInstr &MI = *I; @@ -327,12 +329,21 @@ if (!isValidClauseInst(MI, IsVMEM)) continue; - RegUse Defs, Uses; - GCNDownwardRPTracker RPT(*LIS); - RPT.reset(MI); + if (!InitializedInBlock) { + RPT.reset(MI); + InitializedInBlock = true; + } else {// Advance the state to the current MI. + RPT.advance(MachineBasicBlock::const_iterator(MI)); + RPT.advance(); + } - if (!processRegUses(MI, Defs, Uses, RPT)) + auto LiveRegs = RPT.getLiveRegs(); + RegUse Defs, Uses; + if (!processRegUses(MI, Defs, Uses, RPT)) { + // Restore the state before continue. + RPT.reset(MI, &LiveRegs); continue; + } unsigned Length = 1; for ( ; Next != E && Length < FuncMaxClause; ++Next) { @@ -347,6 +358,10 @@ ++Length; } + + // Restore the state after processing the bundle. + RPT.reset(MI, &LiveRegs); + if (Length < 2) continue;