xref: /openbsd-src/gnu/llvm/llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===-- SIFormMemoryClauses.cpp -------------------------------------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
973471bf0Spatrick /// \file This pass extends the live ranges of registers used as pointers in
1073471bf0Spatrick /// sequences of adjacent SMEM and VMEM instructions if XNACK is enabled. A
1173471bf0Spatrick /// load that would overwrite a pointer would require breaking the soft clause.
1273471bf0Spatrick /// Artificially extend the live ranges of the pointer operands by adding
1373471bf0Spatrick /// implicit-def early-clobber operands throughout the soft clause.
1409467b48Spatrick ///
1509467b48Spatrick //===----------------------------------------------------------------------===//
1609467b48Spatrick 
1709467b48Spatrick #include "AMDGPU.h"
1809467b48Spatrick #include "GCNRegPressure.h"
1909467b48Spatrick #include "SIMachineFunctionInfo.h"
2009467b48Spatrick #include "llvm/InitializePasses.h"
2109467b48Spatrick 
2209467b48Spatrick using namespace llvm;
2309467b48Spatrick 
2409467b48Spatrick #define DEBUG_TYPE "si-form-memory-clauses"
2509467b48Spatrick 
2609467b48Spatrick // Clauses longer then 15 instructions would overflow one of the counters
2709467b48Spatrick // and stall. They can stall even earlier if there are outstanding counters.
2809467b48Spatrick static cl::opt<unsigned>
2909467b48Spatrick MaxClause("amdgpu-max-memory-clause", cl::Hidden, cl::init(15),
3009467b48Spatrick           cl::desc("Maximum length of a memory clause, instructions"));
3109467b48Spatrick 
3209467b48Spatrick namespace {
3309467b48Spatrick 
3409467b48Spatrick class SIFormMemoryClauses : public MachineFunctionPass {
3509467b48Spatrick   typedef DenseMap<unsigned, std::pair<unsigned, LaneBitmask>> RegUse;
3609467b48Spatrick 
3709467b48Spatrick public:
3809467b48Spatrick   static char ID;
3909467b48Spatrick 
4009467b48Spatrick public:
SIFormMemoryClauses()4109467b48Spatrick   SIFormMemoryClauses() : MachineFunctionPass(ID) {
4209467b48Spatrick     initializeSIFormMemoryClausesPass(*PassRegistry::getPassRegistry());
4309467b48Spatrick   }
4409467b48Spatrick 
4509467b48Spatrick   bool runOnMachineFunction(MachineFunction &MF) override;
4609467b48Spatrick 
getPassName() const4709467b48Spatrick   StringRef getPassName() const override {
4809467b48Spatrick     return "SI Form memory clauses";
4909467b48Spatrick   }
5009467b48Spatrick 
getAnalysisUsage(AnalysisUsage & AU) const5109467b48Spatrick   void getAnalysisUsage(AnalysisUsage &AU) const override {
5209467b48Spatrick     AU.addRequired<LiveIntervals>();
5309467b48Spatrick     AU.setPreservesAll();
5409467b48Spatrick     MachineFunctionPass::getAnalysisUsage(AU);
5509467b48Spatrick   }
5609467b48Spatrick 
getClearedProperties() const5773471bf0Spatrick   MachineFunctionProperties getClearedProperties() const override {
5873471bf0Spatrick     return MachineFunctionProperties().set(
5973471bf0Spatrick         MachineFunctionProperties::Property::IsSSA);
6073471bf0Spatrick   }
6109467b48Spatrick 
6273471bf0Spatrick private:
6373471bf0Spatrick   bool canBundle(const MachineInstr &MI, const RegUse &Defs,
6473471bf0Spatrick                  const RegUse &Uses) const;
6509467b48Spatrick   bool checkPressure(const MachineInstr &MI, GCNDownwardRPTracker &RPT);
6609467b48Spatrick   void collectRegUses(const MachineInstr &MI, RegUse &Defs, RegUse &Uses) const;
6709467b48Spatrick   bool processRegUses(const MachineInstr &MI, RegUse &Defs, RegUse &Uses,
6809467b48Spatrick                       GCNDownwardRPTracker &RPT);
6909467b48Spatrick 
7009467b48Spatrick   const GCNSubtarget *ST;
7109467b48Spatrick   const SIRegisterInfo *TRI;
7209467b48Spatrick   const MachineRegisterInfo *MRI;
7309467b48Spatrick   SIMachineFunctionInfo *MFI;
7409467b48Spatrick 
7509467b48Spatrick   unsigned LastRecordedOccupancy;
7609467b48Spatrick   unsigned MaxVGPRs;
7709467b48Spatrick   unsigned MaxSGPRs;
7809467b48Spatrick };
7909467b48Spatrick 
8009467b48Spatrick } // End anonymous namespace.
8109467b48Spatrick 
8209467b48Spatrick INITIALIZE_PASS_BEGIN(SIFormMemoryClauses, DEBUG_TYPE,
8309467b48Spatrick                       "SI Form memory clauses", false, false)
8409467b48Spatrick INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
8509467b48Spatrick INITIALIZE_PASS_END(SIFormMemoryClauses, DEBUG_TYPE,
8609467b48Spatrick                     "SI Form memory clauses", false, false)
8709467b48Spatrick 
8809467b48Spatrick 
8909467b48Spatrick char SIFormMemoryClauses::ID = 0;
9009467b48Spatrick 
9109467b48Spatrick char &llvm::SIFormMemoryClausesID = SIFormMemoryClauses::ID;
9209467b48Spatrick 
createSIFormMemoryClausesPass()9309467b48Spatrick FunctionPass *llvm::createSIFormMemoryClausesPass() {
9409467b48Spatrick   return new SIFormMemoryClauses();
9509467b48Spatrick }
9609467b48Spatrick 
isVMEMClauseInst(const MachineInstr & MI)9709467b48Spatrick static bool isVMEMClauseInst(const MachineInstr &MI) {
9809467b48Spatrick   return SIInstrInfo::isFLAT(MI) || SIInstrInfo::isVMEM(MI);
9909467b48Spatrick }
10009467b48Spatrick 
isSMEMClauseInst(const MachineInstr & MI)10109467b48Spatrick static bool isSMEMClauseInst(const MachineInstr &MI) {
10209467b48Spatrick   return SIInstrInfo::isSMRD(MI);
10309467b48Spatrick }
10409467b48Spatrick 
10509467b48Spatrick // There no sense to create store clauses, they do not define anything,
10609467b48Spatrick // thus there is nothing to set early-clobber.
isValidClauseInst(const MachineInstr & MI,bool IsVMEMClause)10709467b48Spatrick static bool isValidClauseInst(const MachineInstr &MI, bool IsVMEMClause) {
10873471bf0Spatrick   assert(!MI.isDebugInstr() && "debug instructions should not reach here");
10973471bf0Spatrick   if (MI.isBundled())
11009467b48Spatrick     return false;
11109467b48Spatrick   if (!MI.mayLoad() || MI.mayStore())
11209467b48Spatrick     return false;
11373471bf0Spatrick   if (SIInstrInfo::isAtomic(MI))
11409467b48Spatrick     return false;
11509467b48Spatrick   if (IsVMEMClause && !isVMEMClauseInst(MI))
11609467b48Spatrick     return false;
11709467b48Spatrick   if (!IsVMEMClause && !isSMEMClauseInst(MI))
11809467b48Spatrick     return false;
11909467b48Spatrick   // If this is a load instruction where the result has been coalesced with an operand, then we cannot clause it.
12009467b48Spatrick   for (const MachineOperand &ResMO : MI.defs()) {
12109467b48Spatrick     Register ResReg = ResMO.getReg();
12209467b48Spatrick     for (const MachineOperand &MO : MI.uses()) {
12309467b48Spatrick       if (!MO.isReg() || MO.isDef())
12409467b48Spatrick         continue;
12509467b48Spatrick       if (MO.getReg() == ResReg)
12609467b48Spatrick         return false;
12709467b48Spatrick     }
12809467b48Spatrick     break; // Only check the first def.
12909467b48Spatrick   }
13009467b48Spatrick   return true;
13109467b48Spatrick }
13209467b48Spatrick 
getMopState(const MachineOperand & MO)13309467b48Spatrick static unsigned getMopState(const MachineOperand &MO) {
13409467b48Spatrick   unsigned S = 0;
13509467b48Spatrick   if (MO.isImplicit())
13609467b48Spatrick     S |= RegState::Implicit;
13709467b48Spatrick   if (MO.isDead())
13809467b48Spatrick     S |= RegState::Dead;
13909467b48Spatrick   if (MO.isUndef())
14009467b48Spatrick     S |= RegState::Undef;
14109467b48Spatrick   if (MO.isKill())
14209467b48Spatrick     S |= RegState::Kill;
14309467b48Spatrick   if (MO.isEarlyClobber())
14409467b48Spatrick     S |= RegState::EarlyClobber;
14573471bf0Spatrick   if (MO.getReg().isPhysical() && MO.isRenamable())
14609467b48Spatrick     S |= RegState::Renamable;
14709467b48Spatrick   return S;
14809467b48Spatrick }
14909467b48Spatrick 
15009467b48Spatrick // Returns false if there is a use of a def already in the map.
15109467b48Spatrick // In this case we must break the clause.
canBundle(const MachineInstr & MI,const RegUse & Defs,const RegUse & Uses) const15273471bf0Spatrick bool SIFormMemoryClauses::canBundle(const MachineInstr &MI, const RegUse &Defs,
15373471bf0Spatrick                                     const RegUse &Uses) const {
15409467b48Spatrick   // Check interference with defs.
15509467b48Spatrick   for (const MachineOperand &MO : MI.operands()) {
15609467b48Spatrick     // TODO: Prologue/Epilogue Insertion pass does not process bundled
15709467b48Spatrick     //       instructions.
15809467b48Spatrick     if (MO.isFI())
15909467b48Spatrick       return false;
16009467b48Spatrick 
16109467b48Spatrick     if (!MO.isReg())
16209467b48Spatrick       continue;
16309467b48Spatrick 
16409467b48Spatrick     Register Reg = MO.getReg();
16509467b48Spatrick 
16609467b48Spatrick     // If it is tied we will need to write same register as we read.
16709467b48Spatrick     if (MO.isTied())
16809467b48Spatrick       return false;
16909467b48Spatrick 
17073471bf0Spatrick     const RegUse &Map = MO.isDef() ? Uses : Defs;
17109467b48Spatrick     auto Conflict = Map.find(Reg);
17209467b48Spatrick     if (Conflict == Map.end())
17309467b48Spatrick       continue;
17409467b48Spatrick 
17573471bf0Spatrick     if (Reg.isPhysical())
17609467b48Spatrick       return false;
17709467b48Spatrick 
17809467b48Spatrick     LaneBitmask Mask = TRI->getSubRegIndexLaneMask(MO.getSubReg());
17909467b48Spatrick     if ((Conflict->second.second & Mask).any())
18009467b48Spatrick       return false;
18109467b48Spatrick   }
18209467b48Spatrick 
18309467b48Spatrick   return true;
18409467b48Spatrick }
18509467b48Spatrick 
18609467b48Spatrick // Since all defs in the clause are early clobber we can run out of registers.
18709467b48Spatrick // Function returns false if pressure would hit the limit if instruction is
18809467b48Spatrick // bundled into a memory clause.
checkPressure(const MachineInstr & MI,GCNDownwardRPTracker & RPT)18909467b48Spatrick bool SIFormMemoryClauses::checkPressure(const MachineInstr &MI,
19009467b48Spatrick                                         GCNDownwardRPTracker &RPT) {
19109467b48Spatrick   // NB: skip advanceBeforeNext() call. Since all defs will be marked
19209467b48Spatrick   // early-clobber they will all stay alive at least to the end of the
19309467b48Spatrick   // clause. Therefor we should not decrease pressure even if load
19409467b48Spatrick   // pointer becomes dead and could otherwise be reused for destination.
19509467b48Spatrick   RPT.advanceToNext();
19609467b48Spatrick   GCNRegPressure MaxPressure = RPT.moveMaxPressure();
19709467b48Spatrick   unsigned Occupancy = MaxPressure.getOccupancy(*ST);
19873471bf0Spatrick 
19973471bf0Spatrick   // Don't push over half the register budget. We don't want to introduce
20073471bf0Spatrick   // spilling just to form a soft clause.
20173471bf0Spatrick   //
20273471bf0Spatrick   // FIXME: This pressure check is fundamentally broken. First, this is checking
20373471bf0Spatrick   // the global pressure, not the pressure at this specific point in the
20473471bf0Spatrick   // program. Second, it's not accounting for the increased liveness of the use
20573471bf0Spatrick   // operands due to the early clobber we will introduce. Third, the pressure
20673471bf0Spatrick   // tracking does not account for the alignment requirements for SGPRs, or the
20773471bf0Spatrick   // fragmentation of registers the allocator will need to satisfy.
20809467b48Spatrick   if (Occupancy >= MFI->getMinAllowedOccupancy() &&
20973471bf0Spatrick       MaxPressure.getVGPRNum(ST->hasGFX90AInsts()) <= MaxVGPRs / 2 &&
21073471bf0Spatrick       MaxPressure.getSGPRNum() <= MaxSGPRs / 2) {
21109467b48Spatrick     LastRecordedOccupancy = Occupancy;
21209467b48Spatrick     return true;
21309467b48Spatrick   }
21409467b48Spatrick   return false;
21509467b48Spatrick }
21609467b48Spatrick 
21709467b48Spatrick // Collect register defs and uses along with their lane masks and states.
collectRegUses(const MachineInstr & MI,RegUse & Defs,RegUse & Uses) const21809467b48Spatrick void SIFormMemoryClauses::collectRegUses(const MachineInstr &MI,
21909467b48Spatrick                                          RegUse &Defs, RegUse &Uses) const {
22009467b48Spatrick   for (const MachineOperand &MO : MI.operands()) {
22109467b48Spatrick     if (!MO.isReg())
22209467b48Spatrick       continue;
22309467b48Spatrick     Register Reg = MO.getReg();
22409467b48Spatrick     if (!Reg)
22509467b48Spatrick       continue;
22609467b48Spatrick 
22773471bf0Spatrick     LaneBitmask Mask = Reg.isVirtual()
22809467b48Spatrick                            ? TRI->getSubRegIndexLaneMask(MO.getSubReg())
22909467b48Spatrick                            : LaneBitmask::getAll();
23009467b48Spatrick     RegUse &Map = MO.isDef() ? Defs : Uses;
23109467b48Spatrick 
23209467b48Spatrick     auto Loc = Map.find(Reg);
23309467b48Spatrick     unsigned State = getMopState(MO);
23409467b48Spatrick     if (Loc == Map.end()) {
235*d415bd75Srobert       Map[Reg] = std::pair(State, Mask);
23609467b48Spatrick     } else {
23709467b48Spatrick       Loc->second.first |= State;
23809467b48Spatrick       Loc->second.second |= Mask;
23909467b48Spatrick     }
24009467b48Spatrick   }
24109467b48Spatrick }
24209467b48Spatrick 
24309467b48Spatrick // Check register def/use conflicts, occupancy limits and collect def/use maps.
244*d415bd75Srobert // Return true if instruction can be bundled with previous. If it cannot
24509467b48Spatrick // def/use maps are not updated.
processRegUses(const MachineInstr & MI,RegUse & Defs,RegUse & Uses,GCNDownwardRPTracker & RPT)24609467b48Spatrick bool SIFormMemoryClauses::processRegUses(const MachineInstr &MI,
24709467b48Spatrick                                          RegUse &Defs, RegUse &Uses,
24809467b48Spatrick                                          GCNDownwardRPTracker &RPT) {
24909467b48Spatrick   if (!canBundle(MI, Defs, Uses))
25009467b48Spatrick     return false;
25109467b48Spatrick 
25209467b48Spatrick   if (!checkPressure(MI, RPT))
25309467b48Spatrick     return false;
25409467b48Spatrick 
25509467b48Spatrick   collectRegUses(MI, Defs, Uses);
25609467b48Spatrick   return true;
25709467b48Spatrick }
25809467b48Spatrick 
runOnMachineFunction(MachineFunction & MF)25909467b48Spatrick bool SIFormMemoryClauses::runOnMachineFunction(MachineFunction &MF) {
26009467b48Spatrick   if (skipFunction(MF.getFunction()))
26109467b48Spatrick     return false;
26209467b48Spatrick 
26309467b48Spatrick   ST = &MF.getSubtarget<GCNSubtarget>();
26409467b48Spatrick   if (!ST->isXNACKEnabled())
26509467b48Spatrick     return false;
26609467b48Spatrick 
26709467b48Spatrick   const SIInstrInfo *TII = ST->getInstrInfo();
26809467b48Spatrick   TRI = ST->getRegisterInfo();
26909467b48Spatrick   MRI = &MF.getRegInfo();
27009467b48Spatrick   MFI = MF.getInfo<SIMachineFunctionInfo>();
27109467b48Spatrick   LiveIntervals *LIS = &getAnalysis<LiveIntervals>();
27209467b48Spatrick   SlotIndexes *Ind = LIS->getSlotIndexes();
27309467b48Spatrick   bool Changed = false;
27409467b48Spatrick 
27509467b48Spatrick   MaxVGPRs = TRI->getAllocatableSet(MF, &AMDGPU::VGPR_32RegClass).count();
27609467b48Spatrick   MaxSGPRs = TRI->getAllocatableSet(MF, &AMDGPU::SGPR_32RegClass).count();
277*d415bd75Srobert   unsigned FuncMaxClause = MF.getFunction().getFnAttributeAsParsedInteger(
278*d415bd75Srobert       "amdgpu-max-memory-clause", MaxClause);
27909467b48Spatrick 
28009467b48Spatrick   for (MachineBasicBlock &MBB : MF) {
28173471bf0Spatrick     GCNDownwardRPTracker RPT(*LIS);
28209467b48Spatrick     MachineBasicBlock::instr_iterator Next;
28309467b48Spatrick     for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; I = Next) {
28409467b48Spatrick       MachineInstr &MI = *I;
28509467b48Spatrick       Next = std::next(I);
28609467b48Spatrick 
28773471bf0Spatrick       if (MI.isMetaInstruction())
28873471bf0Spatrick         continue;
28973471bf0Spatrick 
29009467b48Spatrick       bool IsVMEM = isVMEMClauseInst(MI);
29109467b48Spatrick 
29209467b48Spatrick       if (!isValidClauseInst(MI, IsVMEM))
29309467b48Spatrick         continue;
29409467b48Spatrick 
29573471bf0Spatrick       if (!RPT.getNext().isValid())
29609467b48Spatrick         RPT.reset(MI);
29773471bf0Spatrick       else { // Advance the state to the current MI.
29873471bf0Spatrick         RPT.advance(MachineBasicBlock::const_iterator(MI));
29973471bf0Spatrick         RPT.advanceBeforeNext();
30073471bf0Spatrick       }
30109467b48Spatrick 
30273471bf0Spatrick       const GCNRPTracker::LiveRegSet LiveRegsCopy(RPT.getLiveRegs());
30373471bf0Spatrick       RegUse Defs, Uses;
30473471bf0Spatrick       if (!processRegUses(MI, Defs, Uses, RPT)) {
30573471bf0Spatrick         RPT.reset(MI, &LiveRegsCopy);
30609467b48Spatrick         continue;
30773471bf0Spatrick       }
30809467b48Spatrick 
30973471bf0Spatrick       MachineBasicBlock::iterator LastClauseInst = Next;
31009467b48Spatrick       unsigned Length = 1;
31109467b48Spatrick       for ( ; Next != E && Length < FuncMaxClause; ++Next) {
31273471bf0Spatrick         // Debug instructions should not change the kill insertion.
31373471bf0Spatrick         if (Next->isMetaInstruction())
31473471bf0Spatrick           continue;
31573471bf0Spatrick 
31609467b48Spatrick         if (!isValidClauseInst(*Next, IsVMEM))
31709467b48Spatrick           break;
31809467b48Spatrick 
31909467b48Spatrick         // A load from pointer which was loaded inside the same bundle is an
32009467b48Spatrick         // impossible clause because we will need to write and read the same
32109467b48Spatrick         // register inside. In this case processRegUses will return false.
32209467b48Spatrick         if (!processRegUses(*Next, Defs, Uses, RPT))
32309467b48Spatrick           break;
32409467b48Spatrick 
32573471bf0Spatrick         LastClauseInst = Next;
32609467b48Spatrick         ++Length;
32709467b48Spatrick       }
32873471bf0Spatrick       if (Length < 2) {
32973471bf0Spatrick         RPT.reset(MI, &LiveRegsCopy);
33009467b48Spatrick         continue;
33173471bf0Spatrick       }
33209467b48Spatrick 
33309467b48Spatrick       Changed = true;
33409467b48Spatrick       MFI->limitOccupancy(LastRecordedOccupancy);
33509467b48Spatrick 
33673471bf0Spatrick       assert(!LastClauseInst->isMetaInstruction());
33709467b48Spatrick 
33873471bf0Spatrick       SlotIndex ClauseLiveInIdx = LIS->getInstructionIndex(MI);
33973471bf0Spatrick       SlotIndex ClauseLiveOutIdx =
34073471bf0Spatrick           LIS->getInstructionIndex(*LastClauseInst).getNextIndex();
34109467b48Spatrick 
34273471bf0Spatrick       // Track the last inserted kill.
34373471bf0Spatrick       MachineInstrBuilder Kill;
34409467b48Spatrick 
34573471bf0Spatrick       // Insert one kill per register, with operands covering all necessary
34673471bf0Spatrick       // subregisters.
34709467b48Spatrick       for (auto &&R : Uses) {
34873471bf0Spatrick         Register Reg = R.first;
34973471bf0Spatrick         if (Reg.isPhysical())
35073471bf0Spatrick           continue;
35173471bf0Spatrick 
35273471bf0Spatrick         // Collect the register operands we should extend the live ranges of.
35373471bf0Spatrick         SmallVector<std::tuple<unsigned, unsigned>> KillOps;
35473471bf0Spatrick         const LiveInterval &LI = LIS->getInterval(R.first);
35573471bf0Spatrick 
35673471bf0Spatrick         if (!LI.hasSubRanges()) {
35773471bf0Spatrick           if (!LI.liveAt(ClauseLiveOutIdx)) {
35873471bf0Spatrick             KillOps.emplace_back(R.second.first | RegState::Kill,
35973471bf0Spatrick                                  AMDGPU::NoSubRegister);
36073471bf0Spatrick           }
36173471bf0Spatrick         } else {
36273471bf0Spatrick           LaneBitmask KilledMask;
36373471bf0Spatrick           for (const LiveInterval::SubRange &SR : LI.subranges()) {
36473471bf0Spatrick             if (SR.liveAt(ClauseLiveInIdx) && !SR.liveAt(ClauseLiveOutIdx))
36573471bf0Spatrick               KilledMask |= SR.LaneMask;
36609467b48Spatrick           }
36709467b48Spatrick 
36873471bf0Spatrick           if (KilledMask.none())
36973471bf0Spatrick             continue;
37073471bf0Spatrick 
37173471bf0Spatrick           SmallVector<unsigned> KilledIndexes;
37273471bf0Spatrick           bool Success = TRI->getCoveringSubRegIndexes(
37373471bf0Spatrick               *MRI, MRI->getRegClass(Reg), KilledMask, KilledIndexes);
37473471bf0Spatrick           (void)Success;
37573471bf0Spatrick           assert(Success && "Failed to find subregister mask to cover lanes");
37673471bf0Spatrick           for (unsigned SubReg : KilledIndexes) {
37773471bf0Spatrick             KillOps.emplace_back(R.second.first | RegState::Kill, SubReg);
37873471bf0Spatrick           }
37973471bf0Spatrick         }
38073471bf0Spatrick 
38173471bf0Spatrick         if (KillOps.empty())
38273471bf0Spatrick           continue;
38373471bf0Spatrick 
38473471bf0Spatrick         // We only want to extend the live ranges of used registers. If they
38573471bf0Spatrick         // already have existing uses beyond the bundle, we don't need the kill.
38673471bf0Spatrick         //
38773471bf0Spatrick         // It's possible all of the use registers were already live past the
38873471bf0Spatrick         // bundle.
38973471bf0Spatrick         Kill = BuildMI(*MI.getParent(), std::next(LastClauseInst),
39073471bf0Spatrick                        DebugLoc(), TII->get(AMDGPU::KILL));
39173471bf0Spatrick         for (auto &Op : KillOps)
39273471bf0Spatrick           Kill.addUse(Reg, std::get<0>(Op), std::get<1>(Op));
39373471bf0Spatrick         Ind->insertMachineInstrInMaps(*Kill);
39473471bf0Spatrick       }
39573471bf0Spatrick 
39673471bf0Spatrick       // Restore the state after processing the end of the bundle.
397*d415bd75Srobert       RPT.reset(MI, &LiveRegsCopy);
398*d415bd75Srobert 
399*d415bd75Srobert       if (!Kill)
400*d415bd75Srobert         continue;
40173471bf0Spatrick 
40209467b48Spatrick       for (auto &&R : Defs) {
40373471bf0Spatrick         Register Reg = R.first;
40409467b48Spatrick         Uses.erase(Reg);
40573471bf0Spatrick         if (Reg.isPhysical())
40609467b48Spatrick           continue;
40709467b48Spatrick         LIS->removeInterval(Reg);
40809467b48Spatrick         LIS->createAndComputeVirtRegInterval(Reg);
40909467b48Spatrick       }
41009467b48Spatrick 
41109467b48Spatrick       for (auto &&R : Uses) {
41273471bf0Spatrick         Register Reg = R.first;
41373471bf0Spatrick         if (Reg.isPhysical())
41409467b48Spatrick           continue;
41509467b48Spatrick         LIS->removeInterval(Reg);
41609467b48Spatrick         LIS->createAndComputeVirtRegInterval(Reg);
41709467b48Spatrick       }
41809467b48Spatrick     }
41909467b48Spatrick   }
42009467b48Spatrick 
42109467b48Spatrick   return Changed;
42209467b48Spatrick }
423