14ba319b5SDimitry Andric //===-- SIFormMemoryClauses.cpp -------------------------------------------===//
24ba319b5SDimitry Andric //
34ba319b5SDimitry Andric //                     The LLVM Compiler Infrastructure
44ba319b5SDimitry Andric //
54ba319b5SDimitry Andric // This file is distributed under the University of Illinois Open Source
64ba319b5SDimitry Andric // License. See LICENSE.TXT for details.
74ba319b5SDimitry Andric //
84ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
94ba319b5SDimitry Andric //
104ba319b5SDimitry Andric /// \file
114ba319b5SDimitry Andric /// This pass creates bundles of SMEM and VMEM instructions forming memory
124ba319b5SDimitry Andric /// clauses if XNACK is enabled. Def operands of clauses are marked as early
134ba319b5SDimitry Andric /// clobber to make sure we will not override any source within a clause.
144ba319b5SDimitry Andric ///
154ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
164ba319b5SDimitry Andric 
174ba319b5SDimitry Andric #include "AMDGPU.h"
184ba319b5SDimitry Andric #include "AMDGPUSubtarget.h"
194ba319b5SDimitry Andric #include "GCNRegPressure.h"
204ba319b5SDimitry Andric #include "SIInstrInfo.h"
214ba319b5SDimitry Andric #include "SIMachineFunctionInfo.h"
224ba319b5SDimitry Andric #include "SIRegisterInfo.h"
234ba319b5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
244ba319b5SDimitry Andric #include "llvm/ADT/DenseMap.h"
254ba319b5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
264ba319b5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
274ba319b5SDimitry Andric 
284ba319b5SDimitry Andric using namespace llvm;
294ba319b5SDimitry Andric 
304ba319b5SDimitry Andric #define DEBUG_TYPE "si-form-memory-clauses"
314ba319b5SDimitry Andric 
324ba319b5SDimitry Andric // Clauses longer then 15 instructions would overflow one of the counters
334ba319b5SDimitry Andric // and stall. They can stall even earlier if there are outstanding counters.
344ba319b5SDimitry Andric static cl::opt<unsigned>
354ba319b5SDimitry Andric MaxClause("amdgpu-max-memory-clause", cl::Hidden, cl::init(15),
364ba319b5SDimitry Andric           cl::desc("Maximum length of a memory clause, instructions"));
374ba319b5SDimitry Andric 
384ba319b5SDimitry Andric namespace {
394ba319b5SDimitry Andric 
404ba319b5SDimitry Andric class SIFormMemoryClauses : public MachineFunctionPass {
414ba319b5SDimitry Andric   typedef DenseMap<unsigned, std::pair<unsigned, LaneBitmask>> RegUse;
424ba319b5SDimitry Andric 
434ba319b5SDimitry Andric public:
444ba319b5SDimitry Andric   static char ID;
454ba319b5SDimitry Andric 
464ba319b5SDimitry Andric public:
SIFormMemoryClauses()474ba319b5SDimitry Andric   SIFormMemoryClauses() : MachineFunctionPass(ID) {
484ba319b5SDimitry Andric     initializeSIFormMemoryClausesPass(*PassRegistry::getPassRegistry());
494ba319b5SDimitry Andric   }
504ba319b5SDimitry Andric 
514ba319b5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
524ba319b5SDimitry Andric 
getPassName() const534ba319b5SDimitry Andric   StringRef getPassName() const override {
544ba319b5SDimitry Andric     return "SI Form memory clauses";
554ba319b5SDimitry Andric   }
564ba319b5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const574ba319b5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
584ba319b5SDimitry Andric     AU.addRequired<LiveIntervals>();
594ba319b5SDimitry Andric     AU.setPreservesAll();
604ba319b5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
614ba319b5SDimitry Andric   }
624ba319b5SDimitry Andric 
634ba319b5SDimitry Andric private:
644ba319b5SDimitry Andric   template <typename Callable>
654ba319b5SDimitry Andric   void forAllLanes(unsigned Reg, LaneBitmask LaneMask, Callable Func) const;
664ba319b5SDimitry Andric 
674ba319b5SDimitry Andric   bool canBundle(const MachineInstr &MI, RegUse &Defs, RegUse &Uses) const;
684ba319b5SDimitry Andric   bool checkPressure(const MachineInstr &MI, GCNDownwardRPTracker &RPT);
694ba319b5SDimitry Andric   void collectRegUses(const MachineInstr &MI, RegUse &Defs, RegUse &Uses) const;
704ba319b5SDimitry Andric   bool processRegUses(const MachineInstr &MI, RegUse &Defs, RegUse &Uses,
714ba319b5SDimitry Andric                       GCNDownwardRPTracker &RPT);
724ba319b5SDimitry Andric 
734ba319b5SDimitry Andric   const GCNSubtarget *ST;
744ba319b5SDimitry Andric   const SIRegisterInfo *TRI;
754ba319b5SDimitry Andric   const MachineRegisterInfo *MRI;
764ba319b5SDimitry Andric   SIMachineFunctionInfo *MFI;
774ba319b5SDimitry Andric 
784ba319b5SDimitry Andric   unsigned LastRecordedOccupancy;
794ba319b5SDimitry Andric   unsigned MaxVGPRs;
804ba319b5SDimitry Andric   unsigned MaxSGPRs;
814ba319b5SDimitry Andric };
824ba319b5SDimitry Andric 
834ba319b5SDimitry Andric } // End anonymous namespace.
844ba319b5SDimitry Andric 
854ba319b5SDimitry Andric INITIALIZE_PASS_BEGIN(SIFormMemoryClauses, DEBUG_TYPE,
864ba319b5SDimitry Andric                       "SI Form memory clauses", false, false)
874ba319b5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
884ba319b5SDimitry Andric INITIALIZE_PASS_END(SIFormMemoryClauses, DEBUG_TYPE,
894ba319b5SDimitry Andric                     "SI Form memory clauses", false, false)
904ba319b5SDimitry Andric 
914ba319b5SDimitry Andric 
924ba319b5SDimitry Andric char SIFormMemoryClauses::ID = 0;
934ba319b5SDimitry Andric 
944ba319b5SDimitry Andric char &llvm::SIFormMemoryClausesID = SIFormMemoryClauses::ID;
954ba319b5SDimitry Andric 
createSIFormMemoryClausesPass()964ba319b5SDimitry Andric FunctionPass *llvm::createSIFormMemoryClausesPass() {
974ba319b5SDimitry Andric   return new SIFormMemoryClauses();
984ba319b5SDimitry Andric }
994ba319b5SDimitry Andric 
isVMEMClauseInst(const MachineInstr & MI)1004ba319b5SDimitry Andric static bool isVMEMClauseInst(const MachineInstr &MI) {
1014ba319b5SDimitry Andric   return SIInstrInfo::isFLAT(MI) || SIInstrInfo::isVMEM(MI);
1024ba319b5SDimitry Andric }
1034ba319b5SDimitry Andric 
isSMEMClauseInst(const MachineInstr & MI)1044ba319b5SDimitry Andric static bool isSMEMClauseInst(const MachineInstr &MI) {
1054ba319b5SDimitry Andric   return SIInstrInfo::isSMRD(MI);
1064ba319b5SDimitry Andric }
1074ba319b5SDimitry Andric 
1084ba319b5SDimitry Andric // There no sense to create store clauses, they do not define anything,
1094ba319b5SDimitry Andric // thus there is nothing to set early-clobber.
isValidClauseInst(const MachineInstr & MI,bool IsVMEMClause)1104ba319b5SDimitry Andric static bool isValidClauseInst(const MachineInstr &MI, bool IsVMEMClause) {
1114ba319b5SDimitry Andric   if (MI.isDebugValue() || MI.isBundled())
1124ba319b5SDimitry Andric     return false;
1134ba319b5SDimitry Andric   if (!MI.mayLoad() || MI.mayStore())
1144ba319b5SDimitry Andric     return false;
1154ba319b5SDimitry Andric   if (AMDGPU::getAtomicNoRetOp(MI.getOpcode()) != -1 ||
1164ba319b5SDimitry Andric       AMDGPU::getAtomicRetOp(MI.getOpcode()) != -1)
1174ba319b5SDimitry Andric     return false;
1184ba319b5SDimitry Andric   if (IsVMEMClause && !isVMEMClauseInst(MI))
1194ba319b5SDimitry Andric     return false;
1204ba319b5SDimitry Andric   if (!IsVMEMClause && !isSMEMClauseInst(MI))
1214ba319b5SDimitry Andric     return false;
1224ba319b5SDimitry Andric   return true;
1234ba319b5SDimitry Andric }
1244ba319b5SDimitry Andric 
getMopState(const MachineOperand & MO)1254ba319b5SDimitry Andric static unsigned getMopState(const MachineOperand &MO) {
1264ba319b5SDimitry Andric   unsigned S = 0;
1274ba319b5SDimitry Andric   if (MO.isImplicit())
1284ba319b5SDimitry Andric     S |= RegState::Implicit;
1294ba319b5SDimitry Andric   if (MO.isDead())
1304ba319b5SDimitry Andric     S |= RegState::Dead;
1314ba319b5SDimitry Andric   if (MO.isUndef())
1324ba319b5SDimitry Andric     S |= RegState::Undef;
1334ba319b5SDimitry Andric   if (MO.isKill())
1344ba319b5SDimitry Andric     S |= RegState::Kill;
1354ba319b5SDimitry Andric   if (MO.isEarlyClobber())
1364ba319b5SDimitry Andric     S |= RegState::EarlyClobber;
1374ba319b5SDimitry Andric   if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && MO.isRenamable())
1384ba319b5SDimitry Andric     S |= RegState::Renamable;
1394ba319b5SDimitry Andric   return S;
1404ba319b5SDimitry Andric }
1414ba319b5SDimitry Andric 
1424ba319b5SDimitry Andric template <typename Callable>
forAllLanes(unsigned Reg,LaneBitmask LaneMask,Callable Func) const1434ba319b5SDimitry Andric void SIFormMemoryClauses::forAllLanes(unsigned Reg, LaneBitmask LaneMask,
1444ba319b5SDimitry Andric                                       Callable Func) const {
1454ba319b5SDimitry Andric   if (LaneMask.all() || TargetRegisterInfo::isPhysicalRegister(Reg) ||
1464ba319b5SDimitry Andric       LaneMask == MRI->getMaxLaneMaskForVReg(Reg)) {
1474ba319b5SDimitry Andric     Func(0);
1484ba319b5SDimitry Andric     return;
1494ba319b5SDimitry Andric   }
1504ba319b5SDimitry Andric 
1514ba319b5SDimitry Andric   const TargetRegisterClass *RC = MRI->getRegClass(Reg);
1524ba319b5SDimitry Andric   unsigned E = TRI->getNumSubRegIndices();
1534ba319b5SDimitry Andric   SmallVector<unsigned, AMDGPU::NUM_TARGET_SUBREGS> CoveringSubregs;
1544ba319b5SDimitry Andric   for (unsigned Idx = 1; Idx < E; ++Idx) {
1554ba319b5SDimitry Andric     // Is this index even compatible with the given class?
1564ba319b5SDimitry Andric     if (TRI->getSubClassWithSubReg(RC, Idx) != RC)
1574ba319b5SDimitry Andric       continue;
1584ba319b5SDimitry Andric     LaneBitmask SubRegMask = TRI->getSubRegIndexLaneMask(Idx);
1594ba319b5SDimitry Andric     // Early exit if we found a perfect match.
1604ba319b5SDimitry Andric     if (SubRegMask == LaneMask) {
1614ba319b5SDimitry Andric       Func(Idx);
1624ba319b5SDimitry Andric       return;
1634ba319b5SDimitry Andric     }
1644ba319b5SDimitry Andric 
1654ba319b5SDimitry Andric     if ((SubRegMask & ~LaneMask).any() || (SubRegMask & LaneMask).none())
1664ba319b5SDimitry Andric       continue;
1674ba319b5SDimitry Andric 
1684ba319b5SDimitry Andric     CoveringSubregs.push_back(Idx);
1694ba319b5SDimitry Andric   }
1704ba319b5SDimitry Andric 
171*b5893f02SDimitry Andric   llvm::sort(CoveringSubregs, [this](unsigned A, unsigned B) {
1724ba319b5SDimitry Andric     LaneBitmask MaskA = TRI->getSubRegIndexLaneMask(A);
1734ba319b5SDimitry Andric     LaneBitmask MaskB = TRI->getSubRegIndexLaneMask(B);
1744ba319b5SDimitry Andric     unsigned NA = MaskA.getNumLanes();
1754ba319b5SDimitry Andric     unsigned NB = MaskB.getNumLanes();
1764ba319b5SDimitry Andric     if (NA != NB)
1774ba319b5SDimitry Andric       return NA > NB;
1784ba319b5SDimitry Andric     return MaskA.getHighestLane() > MaskB.getHighestLane();
1794ba319b5SDimitry Andric   });
1804ba319b5SDimitry Andric 
1814ba319b5SDimitry Andric   for (unsigned Idx : CoveringSubregs) {
1824ba319b5SDimitry Andric     LaneBitmask SubRegMask = TRI->getSubRegIndexLaneMask(Idx);
1834ba319b5SDimitry Andric     if ((SubRegMask & ~LaneMask).any() || (SubRegMask & LaneMask).none())
1844ba319b5SDimitry Andric       continue;
1854ba319b5SDimitry Andric 
1864ba319b5SDimitry Andric     Func(Idx);
1874ba319b5SDimitry Andric     LaneMask &= ~SubRegMask;
1884ba319b5SDimitry Andric     if (LaneMask.none())
1894ba319b5SDimitry Andric       return;
1904ba319b5SDimitry Andric   }
1914ba319b5SDimitry Andric 
1924ba319b5SDimitry Andric   llvm_unreachable("Failed to find all subregs to cover lane mask");
1934ba319b5SDimitry Andric }
1944ba319b5SDimitry Andric 
1954ba319b5SDimitry Andric // Returns false if there is a use of a def already in the map.
1964ba319b5SDimitry Andric // In this case we must break the clause.
canBundle(const MachineInstr & MI,RegUse & Defs,RegUse & Uses) const1974ba319b5SDimitry Andric bool SIFormMemoryClauses::canBundle(const MachineInstr &MI,
1984ba319b5SDimitry Andric                                     RegUse &Defs, RegUse &Uses) const {
1994ba319b5SDimitry Andric   // Check interference with defs.
2004ba319b5SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
2014ba319b5SDimitry Andric     // TODO: Prologue/Epilogue Insertion pass does not process bundled
2024ba319b5SDimitry Andric     //       instructions.
2034ba319b5SDimitry Andric     if (MO.isFI())
2044ba319b5SDimitry Andric       return false;
2054ba319b5SDimitry Andric 
2064ba319b5SDimitry Andric     if (!MO.isReg())
2074ba319b5SDimitry Andric       continue;
2084ba319b5SDimitry Andric 
2094ba319b5SDimitry Andric     unsigned Reg = MO.getReg();
2104ba319b5SDimitry Andric 
2114ba319b5SDimitry Andric     // If it is tied we will need to write same register as we read.
2124ba319b5SDimitry Andric     if (MO.isTied())
2134ba319b5SDimitry Andric       return false;
2144ba319b5SDimitry Andric 
2154ba319b5SDimitry Andric     RegUse &Map = MO.isDef() ? Uses : Defs;
2164ba319b5SDimitry Andric     auto Conflict = Map.find(Reg);
2174ba319b5SDimitry Andric     if (Conflict == Map.end())
2184ba319b5SDimitry Andric       continue;
2194ba319b5SDimitry Andric 
2204ba319b5SDimitry Andric     if (TargetRegisterInfo::isPhysicalRegister(Reg))
2214ba319b5SDimitry Andric       return false;
2224ba319b5SDimitry Andric 
2234ba319b5SDimitry Andric     LaneBitmask Mask = TRI->getSubRegIndexLaneMask(MO.getSubReg());
2244ba319b5SDimitry Andric     if ((Conflict->second.second & Mask).any())
2254ba319b5SDimitry Andric       return false;
2264ba319b5SDimitry Andric   }
2274ba319b5SDimitry Andric 
2284ba319b5SDimitry Andric   return true;
2294ba319b5SDimitry Andric }
2304ba319b5SDimitry Andric 
2314ba319b5SDimitry Andric // Since all defs in the clause are early clobber we can run out of registers.
2324ba319b5SDimitry Andric // Function returns false if pressure would hit the limit if instruction is
2334ba319b5SDimitry Andric // bundled into a memory clause.
checkPressure(const MachineInstr & MI,GCNDownwardRPTracker & RPT)2344ba319b5SDimitry Andric bool SIFormMemoryClauses::checkPressure(const MachineInstr &MI,
2354ba319b5SDimitry Andric                                         GCNDownwardRPTracker &RPT) {
2364ba319b5SDimitry Andric   // NB: skip advanceBeforeNext() call. Since all defs will be marked
2374ba319b5SDimitry Andric   // early-clobber they will all stay alive at least to the end of the
2384ba319b5SDimitry Andric   // clause. Therefor we should not decrease pressure even if load
2394ba319b5SDimitry Andric   // pointer becomes dead and could otherwise be reused for destination.
2404ba319b5SDimitry Andric   RPT.advanceToNext();
2414ba319b5SDimitry Andric   GCNRegPressure MaxPressure = RPT.moveMaxPressure();
2424ba319b5SDimitry Andric   unsigned Occupancy = MaxPressure.getOccupancy(*ST);
2434ba319b5SDimitry Andric   if (Occupancy >= MFI->getMinAllowedOccupancy() &&
2444ba319b5SDimitry Andric       MaxPressure.getVGPRNum() <= MaxVGPRs &&
2454ba319b5SDimitry Andric       MaxPressure.getSGPRNum() <= MaxSGPRs) {
2464ba319b5SDimitry Andric     LastRecordedOccupancy = Occupancy;
2474ba319b5SDimitry Andric     return true;
2484ba319b5SDimitry Andric   }
2494ba319b5SDimitry Andric   return false;
2504ba319b5SDimitry Andric }
2514ba319b5SDimitry Andric 
2524ba319b5SDimitry Andric // Collect register defs and uses along with their lane masks and states.
collectRegUses(const MachineInstr & MI,RegUse & Defs,RegUse & Uses) const2534ba319b5SDimitry Andric void SIFormMemoryClauses::collectRegUses(const MachineInstr &MI,
2544ba319b5SDimitry Andric                                          RegUse &Defs, RegUse &Uses) const {
2554ba319b5SDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
2564ba319b5SDimitry Andric     if (!MO.isReg())
2574ba319b5SDimitry Andric       continue;
2584ba319b5SDimitry Andric     unsigned Reg = MO.getReg();
2594ba319b5SDimitry Andric     if (!Reg)
2604ba319b5SDimitry Andric       continue;
2614ba319b5SDimitry Andric 
2624ba319b5SDimitry Andric     LaneBitmask Mask = TargetRegisterInfo::isVirtualRegister(Reg) ?
2634ba319b5SDimitry Andric                          TRI->getSubRegIndexLaneMask(MO.getSubReg()) :
2644ba319b5SDimitry Andric                          LaneBitmask::getAll();
2654ba319b5SDimitry Andric     RegUse &Map = MO.isDef() ? Defs : Uses;
2664ba319b5SDimitry Andric 
2674ba319b5SDimitry Andric     auto Loc = Map.find(Reg);
2684ba319b5SDimitry Andric     unsigned State = getMopState(MO);
2694ba319b5SDimitry Andric     if (Loc == Map.end()) {
2704ba319b5SDimitry Andric       Map[Reg] = std::make_pair(State, Mask);
2714ba319b5SDimitry Andric     } else {
2724ba319b5SDimitry Andric       Loc->second.first |= State;
2734ba319b5SDimitry Andric       Loc->second.second |= Mask;
2744ba319b5SDimitry Andric     }
2754ba319b5SDimitry Andric   }
2764ba319b5SDimitry Andric }
2774ba319b5SDimitry Andric 
2784ba319b5SDimitry Andric // Check register def/use conflicts, occupancy limits and collect def/use maps.
2794ba319b5SDimitry Andric // Return true if instruction can be bundled with previous. It it cannot
2804ba319b5SDimitry Andric // def/use maps are not updated.
processRegUses(const MachineInstr & MI,RegUse & Defs,RegUse & Uses,GCNDownwardRPTracker & RPT)2814ba319b5SDimitry Andric bool SIFormMemoryClauses::processRegUses(const MachineInstr &MI,
2824ba319b5SDimitry Andric                                          RegUse &Defs, RegUse &Uses,
2834ba319b5SDimitry Andric                                          GCNDownwardRPTracker &RPT) {
2844ba319b5SDimitry Andric   if (!canBundle(MI, Defs, Uses))
2854ba319b5SDimitry Andric     return false;
2864ba319b5SDimitry Andric 
2874ba319b5SDimitry Andric   if (!checkPressure(MI, RPT))
2884ba319b5SDimitry Andric     return false;
2894ba319b5SDimitry Andric 
2904ba319b5SDimitry Andric   collectRegUses(MI, Defs, Uses);
2914ba319b5SDimitry Andric   return true;
2924ba319b5SDimitry Andric }
2934ba319b5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)2944ba319b5SDimitry Andric bool SIFormMemoryClauses::runOnMachineFunction(MachineFunction &MF) {
2954ba319b5SDimitry Andric   if (skipFunction(MF.getFunction()))
2964ba319b5SDimitry Andric     return false;
2974ba319b5SDimitry Andric 
2984ba319b5SDimitry Andric   ST = &MF.getSubtarget<GCNSubtarget>();
2994ba319b5SDimitry Andric   if (!ST->isXNACKEnabled())
3004ba319b5SDimitry Andric     return false;
3014ba319b5SDimitry Andric 
3024ba319b5SDimitry Andric   const SIInstrInfo *TII = ST->getInstrInfo();
3034ba319b5SDimitry Andric   TRI = ST->getRegisterInfo();
3044ba319b5SDimitry Andric   MRI = &MF.getRegInfo();
3054ba319b5SDimitry Andric   MFI = MF.getInfo<SIMachineFunctionInfo>();
3064ba319b5SDimitry Andric   LiveIntervals *LIS = &getAnalysis<LiveIntervals>();
3074ba319b5SDimitry Andric   SlotIndexes *Ind = LIS->getSlotIndexes();
3084ba319b5SDimitry Andric   bool Changed = false;
3094ba319b5SDimitry Andric 
3104ba319b5SDimitry Andric   MaxVGPRs = TRI->getAllocatableSet(MF, &AMDGPU::VGPR_32RegClass).count();
3114ba319b5SDimitry Andric   MaxSGPRs = TRI->getAllocatableSet(MF, &AMDGPU::SGPR_32RegClass).count();
3124ba319b5SDimitry Andric 
3134ba319b5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
3144ba319b5SDimitry Andric     MachineBasicBlock::instr_iterator Next;
3154ba319b5SDimitry Andric     for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; I = Next) {
3164ba319b5SDimitry Andric       MachineInstr &MI = *I;
3174ba319b5SDimitry Andric       Next = std::next(I);
3184ba319b5SDimitry Andric 
3194ba319b5SDimitry Andric       bool IsVMEM = isVMEMClauseInst(MI);
3204ba319b5SDimitry Andric 
3214ba319b5SDimitry Andric       if (!isValidClauseInst(MI, IsVMEM))
3224ba319b5SDimitry Andric         continue;
3234ba319b5SDimitry Andric 
3244ba319b5SDimitry Andric       RegUse Defs, Uses;
3254ba319b5SDimitry Andric       GCNDownwardRPTracker RPT(*LIS);
3264ba319b5SDimitry Andric       RPT.reset(MI);
3274ba319b5SDimitry Andric 
3284ba319b5SDimitry Andric       if (!processRegUses(MI, Defs, Uses, RPT))
3294ba319b5SDimitry Andric         continue;
3304ba319b5SDimitry Andric 
3314ba319b5SDimitry Andric       unsigned Length = 1;
3324ba319b5SDimitry Andric       for ( ; Next != E && Length < MaxClause; ++Next) {
3334ba319b5SDimitry Andric         if (!isValidClauseInst(*Next, IsVMEM))
3344ba319b5SDimitry Andric           break;
3354ba319b5SDimitry Andric 
3364ba319b5SDimitry Andric         // A load from pointer which was loaded inside the same bundle is an
3374ba319b5SDimitry Andric         // impossible clause because we will need to write and read the same
3384ba319b5SDimitry Andric         // register inside. In this case processRegUses will return false.
3394ba319b5SDimitry Andric         if (!processRegUses(*Next, Defs, Uses, RPT))
3404ba319b5SDimitry Andric           break;
3414ba319b5SDimitry Andric 
3424ba319b5SDimitry Andric         ++Length;
3434ba319b5SDimitry Andric       }
3444ba319b5SDimitry Andric       if (Length < 2)
3454ba319b5SDimitry Andric         continue;
3464ba319b5SDimitry Andric 
3474ba319b5SDimitry Andric       Changed = true;
3484ba319b5SDimitry Andric       MFI->limitOccupancy(LastRecordedOccupancy);
3494ba319b5SDimitry Andric 
3504ba319b5SDimitry Andric       auto B = BuildMI(MBB, I, DebugLoc(), TII->get(TargetOpcode::BUNDLE));
3514ba319b5SDimitry Andric       Ind->insertMachineInstrInMaps(*B);
3524ba319b5SDimitry Andric 
3534ba319b5SDimitry Andric       for (auto BI = I; BI != Next; ++BI) {
3544ba319b5SDimitry Andric         BI->bundleWithPred();
3554ba319b5SDimitry Andric         Ind->removeSingleMachineInstrFromMaps(*BI);
3564ba319b5SDimitry Andric 
3574ba319b5SDimitry Andric         for (MachineOperand &MO : BI->defs())
3584ba319b5SDimitry Andric           if (MO.readsReg())
3594ba319b5SDimitry Andric             MO.setIsInternalRead(true);
3604ba319b5SDimitry Andric       }
3614ba319b5SDimitry Andric 
3624ba319b5SDimitry Andric       for (auto &&R : Defs) {
3634ba319b5SDimitry Andric         forAllLanes(R.first, R.second.second, [&R, &B](unsigned SubReg) {
3644ba319b5SDimitry Andric           unsigned S = R.second.first | RegState::EarlyClobber;
3654ba319b5SDimitry Andric           if (!SubReg)
3664ba319b5SDimitry Andric             S &= ~(RegState::Undef | RegState::Dead);
3674ba319b5SDimitry Andric           B.addDef(R.first, S, SubReg);
3684ba319b5SDimitry Andric         });
3694ba319b5SDimitry Andric       }
3704ba319b5SDimitry Andric 
3714ba319b5SDimitry Andric       for (auto &&R : Uses) {
3724ba319b5SDimitry Andric         forAllLanes(R.first, R.second.second, [&R, &B](unsigned SubReg) {
3734ba319b5SDimitry Andric           B.addUse(R.first, R.second.first & ~RegState::Kill, SubReg);
3744ba319b5SDimitry Andric         });
3754ba319b5SDimitry Andric       }
3764ba319b5SDimitry Andric 
3774ba319b5SDimitry Andric       for (auto &&R : Defs) {
3784ba319b5SDimitry Andric         unsigned Reg = R.first;
3794ba319b5SDimitry Andric         Uses.erase(Reg);
3804ba319b5SDimitry Andric         if (TargetRegisterInfo::isPhysicalRegister(Reg))
3814ba319b5SDimitry Andric           continue;
3824ba319b5SDimitry Andric         LIS->removeInterval(Reg);
3834ba319b5SDimitry Andric         LIS->createAndComputeVirtRegInterval(Reg);
3844ba319b5SDimitry Andric       }
3854ba319b5SDimitry Andric 
3864ba319b5SDimitry Andric       for (auto &&R : Uses) {
3874ba319b5SDimitry Andric         unsigned Reg = R.first;
3884ba319b5SDimitry Andric         if (TargetRegisterInfo::isPhysicalRegister(Reg))
3894ba319b5SDimitry Andric           continue;
3904ba319b5SDimitry Andric         LIS->removeInterval(Reg);
3914ba319b5SDimitry Andric         LIS->createAndComputeVirtRegInterval(Reg);
3924ba319b5SDimitry Andric       }
3934ba319b5SDimitry Andric     }
3944ba319b5SDimitry Andric   }
3954ba319b5SDimitry Andric 
3964ba319b5SDimitry Andric   return Changed;
3974ba319b5SDimitry Andric }
398