10b57cec5SDimitry Andric //===-- SIFormMemoryClauses.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
9*5f7ddb14SDimitry Andric /// \file This pass extends the live ranges of registers used as pointers in
10*5f7ddb14SDimitry Andric /// sequences of adjacent SMEM and VMEM instructions if XNACK is enabled. A
11*5f7ddb14SDimitry Andric /// load that would overwrite a pointer would require breaking the soft clause.
12*5f7ddb14SDimitry Andric /// Artificially extend the live ranges of the pointer operands by adding
13*5f7ddb14SDimitry Andric /// implicit-def early-clobber operands throughout the soft clause.
140b57cec5SDimitry Andric ///
150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "AMDGPU.h"
180b57cec5SDimitry Andric #include "GCNRegPressure.h"
190b57cec5SDimitry Andric #include "SIMachineFunctionInfo.h"
20480093f4SDimitry Andric #include "llvm/InitializePasses.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric #define DEBUG_TYPE "si-form-memory-clauses"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric // Clauses longer then 15 instructions would overflow one of the counters
270b57cec5SDimitry Andric // and stall. They can stall even earlier if there are outstanding counters.
280b57cec5SDimitry Andric static cl::opt<unsigned>
290b57cec5SDimitry Andric MaxClause("amdgpu-max-memory-clause", cl::Hidden, cl::init(15),
300b57cec5SDimitry Andric           cl::desc("Maximum length of a memory clause, instructions"));
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric namespace {
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric class SIFormMemoryClauses : public MachineFunctionPass {
350b57cec5SDimitry Andric   typedef DenseMap<unsigned, std::pair<unsigned, LaneBitmask>> RegUse;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric public:
380b57cec5SDimitry Andric   static char ID;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric public:
SIFormMemoryClauses()410b57cec5SDimitry Andric   SIFormMemoryClauses() : MachineFunctionPass(ID) {
420b57cec5SDimitry Andric     initializeSIFormMemoryClausesPass(*PassRegistry::getPassRegistry());
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
460b57cec5SDimitry Andric 
getPassName() const470b57cec5SDimitry Andric   StringRef getPassName() const override {
480b57cec5SDimitry Andric     return "SI Form memory clauses";
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const510b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
520b57cec5SDimitry Andric     AU.addRequired<LiveIntervals>();
530b57cec5SDimitry Andric     AU.setPreservesAll();
540b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
550b57cec5SDimitry Andric   }
560b57cec5SDimitry Andric 
getClearedProperties() const57af732203SDimitry Andric   MachineFunctionProperties getClearedProperties() const override {
58af732203SDimitry Andric     return MachineFunctionProperties().set(
59af732203SDimitry Andric         MachineFunctionProperties::Property::IsSSA);
60af732203SDimitry Andric   }
61af732203SDimitry Andric 
620b57cec5SDimitry Andric private:
63*5f7ddb14SDimitry Andric   bool canBundle(const MachineInstr &MI, const RegUse &Defs,
64*5f7ddb14SDimitry Andric                  const RegUse &Uses) const;
650b57cec5SDimitry Andric   bool checkPressure(const MachineInstr &MI, GCNDownwardRPTracker &RPT);
660b57cec5SDimitry Andric   void collectRegUses(const MachineInstr &MI, RegUse &Defs, RegUse &Uses) const;
670b57cec5SDimitry Andric   bool processRegUses(const MachineInstr &MI, RegUse &Defs, RegUse &Uses,
680b57cec5SDimitry Andric                       GCNDownwardRPTracker &RPT);
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   const GCNSubtarget *ST;
710b57cec5SDimitry Andric   const SIRegisterInfo *TRI;
720b57cec5SDimitry Andric   const MachineRegisterInfo *MRI;
730b57cec5SDimitry Andric   SIMachineFunctionInfo *MFI;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   unsigned LastRecordedOccupancy;
760b57cec5SDimitry Andric   unsigned MaxVGPRs;
770b57cec5SDimitry Andric   unsigned MaxSGPRs;
780b57cec5SDimitry Andric };
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric } // End anonymous namespace.
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(SIFormMemoryClauses, DEBUG_TYPE,
830b57cec5SDimitry Andric                       "SI Form memory clauses", false, false)
840b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
850b57cec5SDimitry Andric INITIALIZE_PASS_END(SIFormMemoryClauses, DEBUG_TYPE,
860b57cec5SDimitry Andric                     "SI Form memory clauses", false, false)
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric char SIFormMemoryClauses::ID = 0;
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric char &llvm::SIFormMemoryClausesID = SIFormMemoryClauses::ID;
920b57cec5SDimitry Andric 
createSIFormMemoryClausesPass()930b57cec5SDimitry Andric FunctionPass *llvm::createSIFormMemoryClausesPass() {
940b57cec5SDimitry Andric   return new SIFormMemoryClauses();
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
isVMEMClauseInst(const MachineInstr & MI)970b57cec5SDimitry Andric static bool isVMEMClauseInst(const MachineInstr &MI) {
980b57cec5SDimitry Andric   return SIInstrInfo::isFLAT(MI) || SIInstrInfo::isVMEM(MI);
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
isSMEMClauseInst(const MachineInstr & MI)1010b57cec5SDimitry Andric static bool isSMEMClauseInst(const MachineInstr &MI) {
1020b57cec5SDimitry Andric   return SIInstrInfo::isSMRD(MI);
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric // There no sense to create store clauses, they do not define anything,
1060b57cec5SDimitry Andric // thus there is nothing to set early-clobber.
isValidClauseInst(const MachineInstr & MI,bool IsVMEMClause)1070b57cec5SDimitry Andric static bool isValidClauseInst(const MachineInstr &MI, bool IsVMEMClause) {
108*5f7ddb14SDimitry Andric   assert(!MI.isDebugInstr() && "debug instructions should not reach here");
109*5f7ddb14SDimitry Andric   if (MI.isBundled())
1100b57cec5SDimitry Andric     return false;
1110b57cec5SDimitry Andric   if (!MI.mayLoad() || MI.mayStore())
1120b57cec5SDimitry Andric     return false;
113*5f7ddb14SDimitry Andric   if (SIInstrInfo::isAtomic(MI))
1140b57cec5SDimitry Andric     return false;
1150b57cec5SDimitry Andric   if (IsVMEMClause && !isVMEMClauseInst(MI))
1160b57cec5SDimitry Andric     return false;
1170b57cec5SDimitry Andric   if (!IsVMEMClause && !isSMEMClauseInst(MI))
1180b57cec5SDimitry Andric     return false;
1190b57cec5SDimitry Andric   // If this is a load instruction where the result has been coalesced with an operand, then we cannot clause it.
1200b57cec5SDimitry Andric   for (const MachineOperand &ResMO : MI.defs()) {
1218bcb0991SDimitry Andric     Register ResReg = ResMO.getReg();
1220b57cec5SDimitry Andric     for (const MachineOperand &MO : MI.uses()) {
1230b57cec5SDimitry Andric       if (!MO.isReg() || MO.isDef())
1240b57cec5SDimitry Andric         continue;
1250b57cec5SDimitry Andric       if (MO.getReg() == ResReg)
1260b57cec5SDimitry Andric         return false;
1270b57cec5SDimitry Andric     }
1280b57cec5SDimitry Andric     break; // Only check the first def.
1290b57cec5SDimitry Andric   }
1300b57cec5SDimitry Andric   return true;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
getMopState(const MachineOperand & MO)1330b57cec5SDimitry Andric static unsigned getMopState(const MachineOperand &MO) {
1340b57cec5SDimitry Andric   unsigned S = 0;
1350b57cec5SDimitry Andric   if (MO.isImplicit())
1360b57cec5SDimitry Andric     S |= RegState::Implicit;
1370b57cec5SDimitry Andric   if (MO.isDead())
1380b57cec5SDimitry Andric     S |= RegState::Dead;
1390b57cec5SDimitry Andric   if (MO.isUndef())
1400b57cec5SDimitry Andric     S |= RegState::Undef;
1410b57cec5SDimitry Andric   if (MO.isKill())
1420b57cec5SDimitry Andric     S |= RegState::Kill;
1430b57cec5SDimitry Andric   if (MO.isEarlyClobber())
1440b57cec5SDimitry Andric     S |= RegState::EarlyClobber;
145af732203SDimitry Andric   if (MO.getReg().isPhysical() && MO.isRenamable())
1460b57cec5SDimitry Andric     S |= RegState::Renamable;
1470b57cec5SDimitry Andric   return S;
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric // Returns false if there is a use of a def already in the map.
1510b57cec5SDimitry Andric // In this case we must break the clause.
canBundle(const MachineInstr & MI,const RegUse & Defs,const RegUse & Uses) const152*5f7ddb14SDimitry Andric bool SIFormMemoryClauses::canBundle(const MachineInstr &MI, const RegUse &Defs,
153*5f7ddb14SDimitry Andric                                     const RegUse &Uses) const {
1540b57cec5SDimitry Andric   // Check interference with defs.
1550b57cec5SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
1560b57cec5SDimitry Andric     // TODO: Prologue/Epilogue Insertion pass does not process bundled
1570b57cec5SDimitry Andric     //       instructions.
1580b57cec5SDimitry Andric     if (MO.isFI())
1590b57cec5SDimitry Andric       return false;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric     if (!MO.isReg())
1620b57cec5SDimitry Andric       continue;
1630b57cec5SDimitry Andric 
1648bcb0991SDimitry Andric     Register Reg = MO.getReg();
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric     // If it is tied we will need to write same register as we read.
1670b57cec5SDimitry Andric     if (MO.isTied())
1680b57cec5SDimitry Andric       return false;
1690b57cec5SDimitry Andric 
170*5f7ddb14SDimitry Andric     const RegUse &Map = MO.isDef() ? Uses : Defs;
1710b57cec5SDimitry Andric     auto Conflict = Map.find(Reg);
1720b57cec5SDimitry Andric     if (Conflict == Map.end())
1730b57cec5SDimitry Andric       continue;
1740b57cec5SDimitry Andric 
175af732203SDimitry Andric     if (Reg.isPhysical())
1760b57cec5SDimitry Andric       return false;
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric     LaneBitmask Mask = TRI->getSubRegIndexLaneMask(MO.getSubReg());
1790b57cec5SDimitry Andric     if ((Conflict->second.second & Mask).any())
1800b57cec5SDimitry Andric       return false;
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   return true;
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric // Since all defs in the clause are early clobber we can run out of registers.
1870b57cec5SDimitry Andric // Function returns false if pressure would hit the limit if instruction is
1880b57cec5SDimitry Andric // bundled into a memory clause.
checkPressure(const MachineInstr & MI,GCNDownwardRPTracker & RPT)1890b57cec5SDimitry Andric bool SIFormMemoryClauses::checkPressure(const MachineInstr &MI,
1900b57cec5SDimitry Andric                                         GCNDownwardRPTracker &RPT) {
1910b57cec5SDimitry Andric   // NB: skip advanceBeforeNext() call. Since all defs will be marked
1920b57cec5SDimitry Andric   // early-clobber they will all stay alive at least to the end of the
1930b57cec5SDimitry Andric   // clause. Therefor we should not decrease pressure even if load
1940b57cec5SDimitry Andric   // pointer becomes dead and could otherwise be reused for destination.
1950b57cec5SDimitry Andric   RPT.advanceToNext();
1960b57cec5SDimitry Andric   GCNRegPressure MaxPressure = RPT.moveMaxPressure();
1970b57cec5SDimitry Andric   unsigned Occupancy = MaxPressure.getOccupancy(*ST);
198*5f7ddb14SDimitry Andric 
199*5f7ddb14SDimitry Andric   // Don't push over half the register budget. We don't want to introduce
200*5f7ddb14SDimitry Andric   // spilling just to form a soft clause.
201*5f7ddb14SDimitry Andric   //
202*5f7ddb14SDimitry Andric   // FIXME: This pressure check is fundamentally broken. First, this is checking
203*5f7ddb14SDimitry Andric   // the global pressure, not the pressure at this specific point in the
204*5f7ddb14SDimitry Andric   // program. Second, it's not accounting for the increased liveness of the use
205*5f7ddb14SDimitry Andric   // operands due to the early clobber we will introduce. Third, the pressure
206*5f7ddb14SDimitry Andric   // tracking does not account for the alignment requirements for SGPRs, or the
207*5f7ddb14SDimitry Andric   // fragmentation of registers the allocator will need to satisfy.
2080b57cec5SDimitry Andric   if (Occupancy >= MFI->getMinAllowedOccupancy() &&
209*5f7ddb14SDimitry Andric       MaxPressure.getVGPRNum(ST->hasGFX90AInsts()) <= MaxVGPRs / 2 &&
210*5f7ddb14SDimitry Andric       MaxPressure.getSGPRNum() <= MaxSGPRs / 2) {
2110b57cec5SDimitry Andric     LastRecordedOccupancy = Occupancy;
2120b57cec5SDimitry Andric     return true;
2130b57cec5SDimitry Andric   }
2140b57cec5SDimitry Andric   return false;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric // Collect register defs and uses along with their lane masks and states.
collectRegUses(const MachineInstr & MI,RegUse & Defs,RegUse & Uses) const2180b57cec5SDimitry Andric void SIFormMemoryClauses::collectRegUses(const MachineInstr &MI,
2190b57cec5SDimitry Andric                                          RegUse &Defs, RegUse &Uses) const {
2200b57cec5SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
2210b57cec5SDimitry Andric     if (!MO.isReg())
2220b57cec5SDimitry Andric       continue;
2238bcb0991SDimitry Andric     Register Reg = MO.getReg();
2240b57cec5SDimitry Andric     if (!Reg)
2250b57cec5SDimitry Andric       continue;
2260b57cec5SDimitry Andric 
227af732203SDimitry Andric     LaneBitmask Mask = Reg.isVirtual()
2288bcb0991SDimitry Andric                            ? TRI->getSubRegIndexLaneMask(MO.getSubReg())
2298bcb0991SDimitry Andric                            : LaneBitmask::getAll();
2300b57cec5SDimitry Andric     RegUse &Map = MO.isDef() ? Defs : Uses;
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric     auto Loc = Map.find(Reg);
2330b57cec5SDimitry Andric     unsigned State = getMopState(MO);
2340b57cec5SDimitry Andric     if (Loc == Map.end()) {
2350b57cec5SDimitry Andric       Map[Reg] = std::make_pair(State, Mask);
2360b57cec5SDimitry Andric     } else {
2370b57cec5SDimitry Andric       Loc->second.first |= State;
2380b57cec5SDimitry Andric       Loc->second.second |= Mask;
2390b57cec5SDimitry Andric     }
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric // Check register def/use conflicts, occupancy limits and collect def/use maps.
2440b57cec5SDimitry Andric // Return true if instruction can be bundled with previous. It it cannot
2450b57cec5SDimitry Andric // def/use maps are not updated.
processRegUses(const MachineInstr & MI,RegUse & Defs,RegUse & Uses,GCNDownwardRPTracker & RPT)2460b57cec5SDimitry Andric bool SIFormMemoryClauses::processRegUses(const MachineInstr &MI,
2470b57cec5SDimitry Andric                                          RegUse &Defs, RegUse &Uses,
2480b57cec5SDimitry Andric                                          GCNDownwardRPTracker &RPT) {
2490b57cec5SDimitry Andric   if (!canBundle(MI, Defs, Uses))
2500b57cec5SDimitry Andric     return false;
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   if (!checkPressure(MI, RPT))
2530b57cec5SDimitry Andric     return false;
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric   collectRegUses(MI, Defs, Uses);
2560b57cec5SDimitry Andric   return true;
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)2590b57cec5SDimitry Andric bool SIFormMemoryClauses::runOnMachineFunction(MachineFunction &MF) {
2600b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
2610b57cec5SDimitry Andric     return false;
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   ST = &MF.getSubtarget<GCNSubtarget>();
2640b57cec5SDimitry Andric   if (!ST->isXNACKEnabled())
2650b57cec5SDimitry Andric     return false;
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   const SIInstrInfo *TII = ST->getInstrInfo();
2680b57cec5SDimitry Andric   TRI = ST->getRegisterInfo();
2690b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
2700b57cec5SDimitry Andric   MFI = MF.getInfo<SIMachineFunctionInfo>();
2710b57cec5SDimitry Andric   LiveIntervals *LIS = &getAnalysis<LiveIntervals>();
2720b57cec5SDimitry Andric   SlotIndexes *Ind = LIS->getSlotIndexes();
2730b57cec5SDimitry Andric   bool Changed = false;
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   MaxVGPRs = TRI->getAllocatableSet(MF, &AMDGPU::VGPR_32RegClass).count();
2760b57cec5SDimitry Andric   MaxSGPRs = TRI->getAllocatableSet(MF, &AMDGPU::SGPR_32RegClass).count();
2770b57cec5SDimitry Andric   unsigned FuncMaxClause = AMDGPU::getIntegerAttribute(
2780b57cec5SDimitry Andric       MF.getFunction(), "amdgpu-max-memory-clause", MaxClause);
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
281af732203SDimitry Andric     GCNDownwardRPTracker RPT(*LIS);
2820b57cec5SDimitry Andric     MachineBasicBlock::instr_iterator Next;
2830b57cec5SDimitry Andric     for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; I = Next) {
2840b57cec5SDimitry Andric       MachineInstr &MI = *I;
2850b57cec5SDimitry Andric       Next = std::next(I);
2860b57cec5SDimitry Andric 
287*5f7ddb14SDimitry Andric       if (MI.isMetaInstruction())
288*5f7ddb14SDimitry Andric         continue;
289*5f7ddb14SDimitry Andric 
2900b57cec5SDimitry Andric       bool IsVMEM = isVMEMClauseInst(MI);
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric       if (!isValidClauseInst(MI, IsVMEM))
2930b57cec5SDimitry Andric         continue;
2940b57cec5SDimitry Andric 
295af732203SDimitry Andric       if (!RPT.getNext().isValid())
2960b57cec5SDimitry Andric         RPT.reset(MI);
297af732203SDimitry Andric       else { // Advance the state to the current MI.
298af732203SDimitry Andric         RPT.advance(MachineBasicBlock::const_iterator(MI));
299af732203SDimitry Andric         RPT.advanceBeforeNext();
300af732203SDimitry Andric       }
3010b57cec5SDimitry Andric 
302af732203SDimitry Andric       const GCNRPTracker::LiveRegSet LiveRegsCopy(RPT.getLiveRegs());
303af732203SDimitry Andric       RegUse Defs, Uses;
304af732203SDimitry Andric       if (!processRegUses(MI, Defs, Uses, RPT)) {
305af732203SDimitry Andric         RPT.reset(MI, &LiveRegsCopy);
3060b57cec5SDimitry Andric         continue;
307af732203SDimitry Andric       }
3080b57cec5SDimitry Andric 
309*5f7ddb14SDimitry Andric       MachineBasicBlock::iterator LastClauseInst = Next;
3100b57cec5SDimitry Andric       unsigned Length = 1;
3110b57cec5SDimitry Andric       for ( ; Next != E && Length < FuncMaxClause; ++Next) {
312*5f7ddb14SDimitry Andric         // Debug instructions should not change the kill insertion.
313*5f7ddb14SDimitry Andric         if (Next->isMetaInstruction())
314*5f7ddb14SDimitry Andric           continue;
315*5f7ddb14SDimitry Andric 
3160b57cec5SDimitry Andric         if (!isValidClauseInst(*Next, IsVMEM))
3170b57cec5SDimitry Andric           break;
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric         // A load from pointer which was loaded inside the same bundle is an
3200b57cec5SDimitry Andric         // impossible clause because we will need to write and read the same
3210b57cec5SDimitry Andric         // register inside. In this case processRegUses will return false.
3220b57cec5SDimitry Andric         if (!processRegUses(*Next, Defs, Uses, RPT))
3230b57cec5SDimitry Andric           break;
3240b57cec5SDimitry Andric 
325*5f7ddb14SDimitry Andric         LastClauseInst = Next;
3260b57cec5SDimitry Andric         ++Length;
3270b57cec5SDimitry Andric       }
328af732203SDimitry Andric       if (Length < 2) {
329af732203SDimitry Andric         RPT.reset(MI, &LiveRegsCopy);
3300b57cec5SDimitry Andric         continue;
331af732203SDimitry Andric       }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric       Changed = true;
3340b57cec5SDimitry Andric       MFI->limitOccupancy(LastRecordedOccupancy);
3350b57cec5SDimitry Andric 
336*5f7ddb14SDimitry Andric       assert(!LastClauseInst->isMetaInstruction());
3370b57cec5SDimitry Andric 
338*5f7ddb14SDimitry Andric       SlotIndex ClauseLiveInIdx = LIS->getInstructionIndex(MI);
339*5f7ddb14SDimitry Andric       SlotIndex ClauseLiveOutIdx =
340*5f7ddb14SDimitry Andric           LIS->getInstructionIndex(*LastClauseInst).getNextIndex();
341af732203SDimitry Andric 
342*5f7ddb14SDimitry Andric       // Track the last inserted kill.
343*5f7ddb14SDimitry Andric       MachineInstrBuilder Kill;
3440b57cec5SDimitry Andric 
345*5f7ddb14SDimitry Andric       // Insert one kill per register, with operands covering all necessary
346*5f7ddb14SDimitry Andric       // subregisters.
3470b57cec5SDimitry Andric       for (auto &&R : Uses) {
348*5f7ddb14SDimitry Andric         Register Reg = R.first;
349*5f7ddb14SDimitry Andric         if (Reg.isPhysical())
350*5f7ddb14SDimitry Andric           continue;
351*5f7ddb14SDimitry Andric 
352*5f7ddb14SDimitry Andric         // Collect the register operands we should extend the live ranges of.
353*5f7ddb14SDimitry Andric         SmallVector<std::tuple<unsigned, unsigned>> KillOps;
354*5f7ddb14SDimitry Andric         const LiveInterval &LI = LIS->getInterval(R.first);
355*5f7ddb14SDimitry Andric 
356*5f7ddb14SDimitry Andric         if (!LI.hasSubRanges()) {
357*5f7ddb14SDimitry Andric           if (!LI.liveAt(ClauseLiveOutIdx)) {
358*5f7ddb14SDimitry Andric             KillOps.emplace_back(R.second.first | RegState::Kill,
359*5f7ddb14SDimitry Andric                                  AMDGPU::NoSubRegister);
3600b57cec5SDimitry Andric           }
361*5f7ddb14SDimitry Andric         } else {
362*5f7ddb14SDimitry Andric           LaneBitmask KilledMask;
363*5f7ddb14SDimitry Andric           for (const LiveInterval::SubRange &SR : LI.subranges()) {
364*5f7ddb14SDimitry Andric             if (SR.liveAt(ClauseLiveInIdx) && !SR.liveAt(ClauseLiveOutIdx))
365*5f7ddb14SDimitry Andric               KilledMask |= SR.LaneMask;
366*5f7ddb14SDimitry Andric           }
367*5f7ddb14SDimitry Andric 
368*5f7ddb14SDimitry Andric           if (KilledMask.none())
369*5f7ddb14SDimitry Andric             continue;
370*5f7ddb14SDimitry Andric 
371*5f7ddb14SDimitry Andric           SmallVector<unsigned> KilledIndexes;
372*5f7ddb14SDimitry Andric           bool Success = TRI->getCoveringSubRegIndexes(
373*5f7ddb14SDimitry Andric               *MRI, MRI->getRegClass(Reg), KilledMask, KilledIndexes);
374*5f7ddb14SDimitry Andric           (void)Success;
375*5f7ddb14SDimitry Andric           assert(Success && "Failed to find subregister mask to cover lanes");
376*5f7ddb14SDimitry Andric           for (unsigned SubReg : KilledIndexes) {
377*5f7ddb14SDimitry Andric             KillOps.emplace_back(R.second.first | RegState::Kill, SubReg);
378*5f7ddb14SDimitry Andric           }
379*5f7ddb14SDimitry Andric         }
380*5f7ddb14SDimitry Andric 
381*5f7ddb14SDimitry Andric         if (KillOps.empty())
382*5f7ddb14SDimitry Andric           continue;
383*5f7ddb14SDimitry Andric 
384*5f7ddb14SDimitry Andric         // We only want to extend the live ranges of used registers. If they
385*5f7ddb14SDimitry Andric         // already have existing uses beyond the bundle, we don't need the kill.
386*5f7ddb14SDimitry Andric         //
387*5f7ddb14SDimitry Andric         // It's possible all of the use registers were already live past the
388*5f7ddb14SDimitry Andric         // bundle.
389*5f7ddb14SDimitry Andric         Kill = BuildMI(*MI.getParent(), std::next(LastClauseInst),
390*5f7ddb14SDimitry Andric                        DebugLoc(), TII->get(AMDGPU::KILL));
391*5f7ddb14SDimitry Andric         for (auto &Op : KillOps)
392*5f7ddb14SDimitry Andric           Kill.addUse(Reg, std::get<0>(Op), std::get<1>(Op));
393*5f7ddb14SDimitry Andric         Ind->insertMachineInstrInMaps(*Kill);
394*5f7ddb14SDimitry Andric       }
395*5f7ddb14SDimitry Andric 
396*5f7ddb14SDimitry Andric       if (!Kill) {
397*5f7ddb14SDimitry Andric         RPT.reset(MI, &LiveRegsCopy);
398*5f7ddb14SDimitry Andric         continue;
399*5f7ddb14SDimitry Andric       }
400*5f7ddb14SDimitry Andric 
401*5f7ddb14SDimitry Andric       // Restore the state after processing the end of the bundle.
402*5f7ddb14SDimitry Andric       RPT.reset(*Kill, &LiveRegsCopy);
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric       for (auto &&R : Defs) {
405af732203SDimitry Andric         Register Reg = R.first;
4060b57cec5SDimitry Andric         Uses.erase(Reg);
407af732203SDimitry Andric         if (Reg.isPhysical())
4080b57cec5SDimitry Andric           continue;
4090b57cec5SDimitry Andric         LIS->removeInterval(Reg);
4100b57cec5SDimitry Andric         LIS->createAndComputeVirtRegInterval(Reg);
4110b57cec5SDimitry Andric       }
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric       for (auto &&R : Uses) {
414af732203SDimitry Andric         Register Reg = R.first;
415af732203SDimitry Andric         if (Reg.isPhysical())
4160b57cec5SDimitry Andric           continue;
4170b57cec5SDimitry Andric         LIS->removeInterval(Reg);
4180b57cec5SDimitry Andric         LIS->createAndComputeVirtRegInterval(Reg);
4190b57cec5SDimitry Andric       }
4200b57cec5SDimitry Andric     }
4210b57cec5SDimitry Andric   }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   return Changed;
4240b57cec5SDimitry Andric }
425