15ffd83dbSDimitry Andric //===-- FixupStatepointCallerSaved.cpp - Fixup caller saved registers ----===// 25ffd83dbSDimitry Andric // 35ffd83dbSDimitry Andric // The LLVM Compiler Infrastructure 45ffd83dbSDimitry Andric // 55ffd83dbSDimitry Andric // This file is distributed under the University of Illinois Open Source 65ffd83dbSDimitry Andric // License. See LICENSE.TXT for details. 75ffd83dbSDimitry Andric // 85ffd83dbSDimitry Andric //===----------------------------------------------------------------------===// 95ffd83dbSDimitry Andric /// 105ffd83dbSDimitry Andric /// \file 115ffd83dbSDimitry Andric /// Statepoint instruction in deopt parameters contains values which are 125ffd83dbSDimitry Andric /// meaningful to the runtime and should be able to be read at the moment the 135ffd83dbSDimitry Andric /// call returns. So we can say that we need to encode the fact that these 145ffd83dbSDimitry Andric /// values are "late read" by runtime. If we could express this notion for 155ffd83dbSDimitry Andric /// register allocator it would produce the right form for us. 165ffd83dbSDimitry Andric /// The need to fixup (i.e this pass) is specifically handling the fact that 175ffd83dbSDimitry Andric /// we cannot describe such a late read for the register allocator. 185ffd83dbSDimitry Andric /// Register allocator may put the value on a register clobbered by the call. 195ffd83dbSDimitry Andric /// This pass forces the spill of such registers and replaces corresponding 205ffd83dbSDimitry Andric /// statepoint operands to added spill slots. 215ffd83dbSDimitry Andric /// 225ffd83dbSDimitry Andric //===----------------------------------------------------------------------===// 235ffd83dbSDimitry Andric 245ffd83dbSDimitry Andric #include "llvm/ADT/SmallSet.h" 255ffd83dbSDimitry Andric #include "llvm/ADT/Statistic.h" 265ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 275ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 285ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 295ffd83dbSDimitry Andric #include "llvm/CodeGen/Passes.h" 305ffd83dbSDimitry Andric #include "llvm/CodeGen/StackMaps.h" 315ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 325ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 335ffd83dbSDimitry Andric #include "llvm/IR/Statepoint.h" 345ffd83dbSDimitry Andric #include "llvm/InitializePasses.h" 355ffd83dbSDimitry Andric #include "llvm/Support/Debug.h" 365ffd83dbSDimitry Andric 375ffd83dbSDimitry Andric using namespace llvm; 385ffd83dbSDimitry Andric 395ffd83dbSDimitry Andric #define DEBUG_TYPE "fixup-statepoint-caller-saved" 405ffd83dbSDimitry Andric STATISTIC(NumSpilledRegisters, "Number of spilled register"); 415ffd83dbSDimitry Andric STATISTIC(NumSpillSlotsAllocated, "Number of spill slots allocated"); 425ffd83dbSDimitry Andric STATISTIC(NumSpillSlotsExtended, "Number of spill slots extended"); 435ffd83dbSDimitry Andric 445ffd83dbSDimitry Andric static cl::opt<bool> FixupSCSExtendSlotSize( 455ffd83dbSDimitry Andric "fixup-scs-extend-slot-size", cl::Hidden, cl::init(false), 465ffd83dbSDimitry Andric cl::desc("Allow spill in spill slot of greater size than register size"), 475ffd83dbSDimitry Andric cl::Hidden); 485ffd83dbSDimitry Andric 49e8d8bef9SDimitry Andric static cl::opt<bool> PassGCPtrInCSR( 50e8d8bef9SDimitry Andric "fixup-allow-gcptr-in-csr", cl::Hidden, cl::init(false), 51e8d8bef9SDimitry Andric cl::desc("Allow passing GC Pointer arguments in callee saved registers")); 52e8d8bef9SDimitry Andric 53e8d8bef9SDimitry Andric static cl::opt<bool> EnableCopyProp( 54e8d8bef9SDimitry Andric "fixup-scs-enable-copy-propagation", cl::Hidden, cl::init(true), 55e8d8bef9SDimitry Andric cl::desc("Enable simple copy propagation during register reloading")); 56e8d8bef9SDimitry Andric 57e8d8bef9SDimitry Andric // This is purely debugging option. 58e8d8bef9SDimitry Andric // It may be handy for investigating statepoint spilling issues. 59e8d8bef9SDimitry Andric static cl::opt<unsigned> MaxStatepointsWithRegs( 60e8d8bef9SDimitry Andric "fixup-max-csr-statepoints", cl::Hidden, 61e8d8bef9SDimitry Andric cl::desc("Max number of statepoints allowed to pass GC Ptrs in registers")); 62e8d8bef9SDimitry Andric 635ffd83dbSDimitry Andric namespace { 645ffd83dbSDimitry Andric 655ffd83dbSDimitry Andric class FixupStatepointCallerSaved : public MachineFunctionPass { 665ffd83dbSDimitry Andric public: 675ffd83dbSDimitry Andric static char ID; 685ffd83dbSDimitry Andric 695ffd83dbSDimitry Andric FixupStatepointCallerSaved() : MachineFunctionPass(ID) { 705ffd83dbSDimitry Andric initializeFixupStatepointCallerSavedPass(*PassRegistry::getPassRegistry()); 715ffd83dbSDimitry Andric } 725ffd83dbSDimitry Andric 735ffd83dbSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 745ffd83dbSDimitry Andric AU.setPreservesCFG(); 755ffd83dbSDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 765ffd83dbSDimitry Andric } 775ffd83dbSDimitry Andric 785ffd83dbSDimitry Andric StringRef getPassName() const override { 795ffd83dbSDimitry Andric return "Fixup Statepoint Caller Saved"; 805ffd83dbSDimitry Andric } 815ffd83dbSDimitry Andric 825ffd83dbSDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 835ffd83dbSDimitry Andric }; 84e8d8bef9SDimitry Andric 855ffd83dbSDimitry Andric } // End anonymous namespace. 865ffd83dbSDimitry Andric 875ffd83dbSDimitry Andric char FixupStatepointCallerSaved::ID = 0; 885ffd83dbSDimitry Andric char &llvm::FixupStatepointCallerSavedID = FixupStatepointCallerSaved::ID; 895ffd83dbSDimitry Andric 905ffd83dbSDimitry Andric INITIALIZE_PASS_BEGIN(FixupStatepointCallerSaved, DEBUG_TYPE, 915ffd83dbSDimitry Andric "Fixup Statepoint Caller Saved", false, false) 925ffd83dbSDimitry Andric INITIALIZE_PASS_END(FixupStatepointCallerSaved, DEBUG_TYPE, 935ffd83dbSDimitry Andric "Fixup Statepoint Caller Saved", false, false) 945ffd83dbSDimitry Andric 955ffd83dbSDimitry Andric // Utility function to get size of the register. 965ffd83dbSDimitry Andric static unsigned getRegisterSize(const TargetRegisterInfo &TRI, Register Reg) { 975ffd83dbSDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 985ffd83dbSDimitry Andric return TRI.getSpillSize(*RC); 995ffd83dbSDimitry Andric } 1005ffd83dbSDimitry Andric 101e8d8bef9SDimitry Andric // Try to eliminate redundant copy to register which we're going to 102e8d8bef9SDimitry Andric // spill, i.e. try to change: 103e8d8bef9SDimitry Andric // X = COPY Y 104e8d8bef9SDimitry Andric // SPILL X 105e8d8bef9SDimitry Andric // to 106e8d8bef9SDimitry Andric // SPILL Y 107e8d8bef9SDimitry Andric // If there are no uses of X between copy and STATEPOINT, that COPY 108e8d8bef9SDimitry Andric // may be eliminated. 109e8d8bef9SDimitry Andric // Reg - register we're about to spill 110e8d8bef9SDimitry Andric // RI - On entry points to statepoint. 111e8d8bef9SDimitry Andric // On successful copy propagation set to new spill point. 112e8d8bef9SDimitry Andric // IsKill - set to true if COPY is Kill (there are no uses of Y) 113e8d8bef9SDimitry Andric // Returns either found source copy register or original one. 114e8d8bef9SDimitry Andric static Register performCopyPropagation(Register Reg, 115e8d8bef9SDimitry Andric MachineBasicBlock::iterator &RI, 116e8d8bef9SDimitry Andric bool &IsKill, const TargetInstrInfo &TII, 117e8d8bef9SDimitry Andric const TargetRegisterInfo &TRI) { 118e8d8bef9SDimitry Andric // First check if statepoint itself uses Reg in non-meta operands. 119e8d8bef9SDimitry Andric int Idx = RI->findRegisterUseOperandIdx(Reg, false, &TRI); 120e8d8bef9SDimitry Andric if (Idx >= 0 && (unsigned)Idx < StatepointOpers(&*RI).getNumDeoptArgsIdx()) { 121e8d8bef9SDimitry Andric IsKill = false; 122e8d8bef9SDimitry Andric return Reg; 123e8d8bef9SDimitry Andric } 124e8d8bef9SDimitry Andric 125e8d8bef9SDimitry Andric if (!EnableCopyProp) 126e8d8bef9SDimitry Andric return Reg; 127e8d8bef9SDimitry Andric 128e8d8bef9SDimitry Andric MachineBasicBlock *MBB = RI->getParent(); 129e8d8bef9SDimitry Andric MachineBasicBlock::reverse_iterator E = MBB->rend(); 130e8d8bef9SDimitry Andric MachineInstr *Def = nullptr, *Use = nullptr; 131e8d8bef9SDimitry Andric for (auto It = ++(RI.getReverse()); It != E; ++It) { 132e8d8bef9SDimitry Andric if (It->readsRegister(Reg, &TRI) && !Use) 133e8d8bef9SDimitry Andric Use = &*It; 134e8d8bef9SDimitry Andric if (It->modifiesRegister(Reg, &TRI)) { 135e8d8bef9SDimitry Andric Def = &*It; 136e8d8bef9SDimitry Andric break; 137e8d8bef9SDimitry Andric } 138e8d8bef9SDimitry Andric } 139e8d8bef9SDimitry Andric 140e8d8bef9SDimitry Andric if (!Def) 141e8d8bef9SDimitry Andric return Reg; 142e8d8bef9SDimitry Andric 143e8d8bef9SDimitry Andric auto DestSrc = TII.isCopyInstr(*Def); 144e8d8bef9SDimitry Andric if (!DestSrc || DestSrc->Destination->getReg() != Reg) 145e8d8bef9SDimitry Andric return Reg; 146e8d8bef9SDimitry Andric 147e8d8bef9SDimitry Andric Register SrcReg = DestSrc->Source->getReg(); 148e8d8bef9SDimitry Andric 149e8d8bef9SDimitry Andric if (getRegisterSize(TRI, Reg) != getRegisterSize(TRI, SrcReg)) 150e8d8bef9SDimitry Andric return Reg; 151e8d8bef9SDimitry Andric 152e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "spillRegisters: perform copy propagation " 153e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " -> " << printReg(SrcReg, &TRI) 154e8d8bef9SDimitry Andric << "\n"); 155e8d8bef9SDimitry Andric 156e8d8bef9SDimitry Andric // Insert spill immediately after Def 157e8d8bef9SDimitry Andric RI = ++MachineBasicBlock::iterator(Def); 158e8d8bef9SDimitry Andric IsKill = DestSrc->Source->isKill(); 159e8d8bef9SDimitry Andric 160e8d8bef9SDimitry Andric // There are no uses of original register between COPY and STATEPOINT. 161e8d8bef9SDimitry Andric // There can't be any after STATEPOINT, so we can eliminate Def. 162e8d8bef9SDimitry Andric if (!Use) { 163e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "spillRegisters: removing dead copy " << *Def); 164e8d8bef9SDimitry Andric Def->eraseFromParent(); 165e8d8bef9SDimitry Andric } 166e8d8bef9SDimitry Andric return SrcReg; 167e8d8bef9SDimitry Andric } 168e8d8bef9SDimitry Andric 1695ffd83dbSDimitry Andric namespace { 170e8d8bef9SDimitry Andric // Pair {Register, FrameIndex} 171e8d8bef9SDimitry Andric using RegSlotPair = std::pair<Register, int>; 172e8d8bef9SDimitry Andric 173e8d8bef9SDimitry Andric // Keeps track of what reloads were inserted in MBB. 174e8d8bef9SDimitry Andric class RegReloadCache { 175e8d8bef9SDimitry Andric using ReloadSet = SmallSet<RegSlotPair, 8>; 176e8d8bef9SDimitry Andric DenseMap<const MachineBasicBlock *, ReloadSet> Reloads; 177e8d8bef9SDimitry Andric 178e8d8bef9SDimitry Andric public: 179e8d8bef9SDimitry Andric RegReloadCache() = default; 180e8d8bef9SDimitry Andric 181e8d8bef9SDimitry Andric // Record reload of Reg from FI in block MBB 182e8d8bef9SDimitry Andric void recordReload(Register Reg, int FI, const MachineBasicBlock *MBB) { 183e8d8bef9SDimitry Andric RegSlotPair RSP(Reg, FI); 184e8d8bef9SDimitry Andric auto Res = Reloads[MBB].insert(RSP); 185e8d8bef9SDimitry Andric (void)Res; 186e8d8bef9SDimitry Andric assert(Res.second && "reload already exists"); 187e8d8bef9SDimitry Andric } 188e8d8bef9SDimitry Andric 189e8d8bef9SDimitry Andric // Does basic block MBB contains reload of Reg from FI? 190e8d8bef9SDimitry Andric bool hasReload(Register Reg, int FI, const MachineBasicBlock *MBB) { 191e8d8bef9SDimitry Andric RegSlotPair RSP(Reg, FI); 192e8d8bef9SDimitry Andric return Reloads.count(MBB) && Reloads[MBB].count(RSP); 193e8d8bef9SDimitry Andric } 194e8d8bef9SDimitry Andric }; 195e8d8bef9SDimitry Andric 1965ffd83dbSDimitry Andric // Cache used frame indexes during statepoint re-write to re-use them in 1975ffd83dbSDimitry Andric // processing next statepoint instruction. 1985ffd83dbSDimitry Andric // Two strategies. One is to preserve the size of spill slot while another one 1995ffd83dbSDimitry Andric // extends the size of spill slots to reduce the number of them, causing 2005ffd83dbSDimitry Andric // the less total frame size. But unspill will have "implicit" any extend. 2015ffd83dbSDimitry Andric class FrameIndexesCache { 2025ffd83dbSDimitry Andric private: 2035ffd83dbSDimitry Andric struct FrameIndexesPerSize { 2045ffd83dbSDimitry Andric // List of used frame indexes during processing previous statepoints. 2055ffd83dbSDimitry Andric SmallVector<int, 8> Slots; 2065ffd83dbSDimitry Andric // Current index of un-used yet frame index. 2075ffd83dbSDimitry Andric unsigned Index = 0; 2085ffd83dbSDimitry Andric }; 2095ffd83dbSDimitry Andric MachineFrameInfo &MFI; 2105ffd83dbSDimitry Andric const TargetRegisterInfo &TRI; 2115ffd83dbSDimitry Andric // Map size to list of frame indexes of this size. If the mode is 2125ffd83dbSDimitry Andric // FixupSCSExtendSlotSize then the key 0 is used to keep all frame indexes. 2135ffd83dbSDimitry Andric // If the size of required spill slot is greater than in a cache then the 2145ffd83dbSDimitry Andric // size will be increased. 2155ffd83dbSDimitry Andric DenseMap<unsigned, FrameIndexesPerSize> Cache; 2165ffd83dbSDimitry Andric 217e8d8bef9SDimitry Andric // Keeps track of slots reserved for the shared landing pad processing. 218e8d8bef9SDimitry Andric // Initialized from GlobalIndices for the current EHPad. 219e8d8bef9SDimitry Andric SmallSet<int, 8> ReservedSlots; 220e8d8bef9SDimitry Andric 221e8d8bef9SDimitry Andric // Landing pad can be destination of several statepoints. Every register 222e8d8bef9SDimitry Andric // defined by such statepoints must be spilled to the same stack slot. 223e8d8bef9SDimitry Andric // This map keeps that information. 224e8d8bef9SDimitry Andric DenseMap<const MachineBasicBlock *, SmallVector<RegSlotPair, 8>> 225e8d8bef9SDimitry Andric GlobalIndices; 226e8d8bef9SDimitry Andric 227e8d8bef9SDimitry Andric FrameIndexesPerSize &getCacheBucket(unsigned Size) { 228e8d8bef9SDimitry Andric // In FixupSCSExtendSlotSize mode the bucket with 0 index is used 229e8d8bef9SDimitry Andric // for all sizes. 230e8d8bef9SDimitry Andric return Cache[FixupSCSExtendSlotSize ? 0 : Size]; 231e8d8bef9SDimitry Andric } 232e8d8bef9SDimitry Andric 2335ffd83dbSDimitry Andric public: 2345ffd83dbSDimitry Andric FrameIndexesCache(MachineFrameInfo &MFI, const TargetRegisterInfo &TRI) 2355ffd83dbSDimitry Andric : MFI(MFI), TRI(TRI) {} 2365ffd83dbSDimitry Andric // Reset the current state of used frame indexes. After invocation of 237e8d8bef9SDimitry Andric // this function all frame indexes are available for allocation with 238e8d8bef9SDimitry Andric // the exception of slots reserved for landing pad processing (if any). 239e8d8bef9SDimitry Andric void reset(const MachineBasicBlock *EHPad) { 2405ffd83dbSDimitry Andric for (auto &It : Cache) 2415ffd83dbSDimitry Andric It.second.Index = 0; 242e8d8bef9SDimitry Andric 243e8d8bef9SDimitry Andric ReservedSlots.clear(); 244e8d8bef9SDimitry Andric if (EHPad && GlobalIndices.count(EHPad)) 245e8d8bef9SDimitry Andric for (auto &RSP : GlobalIndices[EHPad]) 246e8d8bef9SDimitry Andric ReservedSlots.insert(RSP.second); 2475ffd83dbSDimitry Andric } 248e8d8bef9SDimitry Andric 2495ffd83dbSDimitry Andric // Get frame index to spill the register. 250e8d8bef9SDimitry Andric int getFrameIndex(Register Reg, MachineBasicBlock *EHPad) { 251e8d8bef9SDimitry Andric // Check if slot for Reg is already reserved at EHPad. 252e8d8bef9SDimitry Andric auto It = GlobalIndices.find(EHPad); 253e8d8bef9SDimitry Andric if (It != GlobalIndices.end()) { 254e8d8bef9SDimitry Andric auto &Vec = It->second; 255e8d8bef9SDimitry Andric auto Idx = llvm::find_if( 256e8d8bef9SDimitry Andric Vec, [Reg](RegSlotPair &RSP) { return Reg == RSP.first; }); 257e8d8bef9SDimitry Andric if (Idx != Vec.end()) { 258e8d8bef9SDimitry Andric int FI = Idx->second; 259e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Found global FI " << FI << " for register " 260e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " at " 261e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 262e8d8bef9SDimitry Andric assert(ReservedSlots.count(FI) && "using unreserved slot"); 263e8d8bef9SDimitry Andric return FI; 264e8d8bef9SDimitry Andric } 265e8d8bef9SDimitry Andric } 266e8d8bef9SDimitry Andric 2675ffd83dbSDimitry Andric unsigned Size = getRegisterSize(TRI, Reg); 268e8d8bef9SDimitry Andric FrameIndexesPerSize &Line = getCacheBucket(Size); 269e8d8bef9SDimitry Andric while (Line.Index < Line.Slots.size()) { 2705ffd83dbSDimitry Andric int FI = Line.Slots[Line.Index++]; 271e8d8bef9SDimitry Andric if (ReservedSlots.count(FI)) 272e8d8bef9SDimitry Andric continue; 2735ffd83dbSDimitry Andric // If all sizes are kept together we probably need to extend the 2745ffd83dbSDimitry Andric // spill slot size. 2755ffd83dbSDimitry Andric if (MFI.getObjectSize(FI) < Size) { 2765ffd83dbSDimitry Andric MFI.setObjectSize(FI, Size); 2775ffd83dbSDimitry Andric MFI.setObjectAlignment(FI, Align(Size)); 2785ffd83dbSDimitry Andric NumSpillSlotsExtended++; 2795ffd83dbSDimitry Andric } 2805ffd83dbSDimitry Andric return FI; 2815ffd83dbSDimitry Andric } 2825ffd83dbSDimitry Andric int FI = MFI.CreateSpillStackObject(Size, Align(Size)); 2835ffd83dbSDimitry Andric NumSpillSlotsAllocated++; 2845ffd83dbSDimitry Andric Line.Slots.push_back(FI); 2855ffd83dbSDimitry Andric ++Line.Index; 286e8d8bef9SDimitry Andric 287e8d8bef9SDimitry Andric // Remember assignment {Reg, FI} for EHPad 288e8d8bef9SDimitry Andric if (EHPad) { 289e8d8bef9SDimitry Andric GlobalIndices[EHPad].push_back(std::make_pair(Reg, FI)); 290e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Reserved FI " << FI << " for spilling reg " 291e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " at landing pad " 292e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 293e8d8bef9SDimitry Andric } 294e8d8bef9SDimitry Andric 2955ffd83dbSDimitry Andric return FI; 2965ffd83dbSDimitry Andric } 297e8d8bef9SDimitry Andric 2985ffd83dbSDimitry Andric // Sort all registers to spill in descendent order. In the 2995ffd83dbSDimitry Andric // FixupSCSExtendSlotSize mode it will minimize the total frame size. 3005ffd83dbSDimitry Andric // In non FixupSCSExtendSlotSize mode we can skip this step. 3015ffd83dbSDimitry Andric void sortRegisters(SmallVectorImpl<Register> &Regs) { 3025ffd83dbSDimitry Andric if (!FixupSCSExtendSlotSize) 3035ffd83dbSDimitry Andric return; 304e8d8bef9SDimitry Andric llvm::sort(Regs, [&](Register &A, Register &B) { 3055ffd83dbSDimitry Andric return getRegisterSize(TRI, A) > getRegisterSize(TRI, B); 3065ffd83dbSDimitry Andric }); 3075ffd83dbSDimitry Andric } 3085ffd83dbSDimitry Andric }; 3095ffd83dbSDimitry Andric 3105ffd83dbSDimitry Andric // Describes the state of the current processing statepoint instruction. 3115ffd83dbSDimitry Andric class StatepointState { 3125ffd83dbSDimitry Andric private: 3135ffd83dbSDimitry Andric // statepoint instruction. 3145ffd83dbSDimitry Andric MachineInstr &MI; 3155ffd83dbSDimitry Andric MachineFunction &MF; 316e8d8bef9SDimitry Andric // If non-null then statepoint is invoke, and this points to the landing pad. 317e8d8bef9SDimitry Andric MachineBasicBlock *EHPad; 3185ffd83dbSDimitry Andric const TargetRegisterInfo &TRI; 3195ffd83dbSDimitry Andric const TargetInstrInfo &TII; 3205ffd83dbSDimitry Andric MachineFrameInfo &MFI; 3215ffd83dbSDimitry Andric // Mask with callee saved registers. 3225ffd83dbSDimitry Andric const uint32_t *Mask; 3235ffd83dbSDimitry Andric // Cache of frame indexes used on previous instruction processing. 3245ffd83dbSDimitry Andric FrameIndexesCache &CacheFI; 325e8d8bef9SDimitry Andric bool AllowGCPtrInCSR; 3265ffd83dbSDimitry Andric // Operands with physical registers requiring spilling. 3275ffd83dbSDimitry Andric SmallVector<unsigned, 8> OpsToSpill; 3285ffd83dbSDimitry Andric // Set of register to spill. 3295ffd83dbSDimitry Andric SmallVector<Register, 8> RegsToSpill; 330e8d8bef9SDimitry Andric // Set of registers to reload after statepoint. 331e8d8bef9SDimitry Andric SmallVector<Register, 8> RegsToReload; 3325ffd83dbSDimitry Andric // Map Register to Frame Slot index. 3335ffd83dbSDimitry Andric DenseMap<Register, int> RegToSlotIdx; 3345ffd83dbSDimitry Andric 3355ffd83dbSDimitry Andric public: 3365ffd83dbSDimitry Andric StatepointState(MachineInstr &MI, const uint32_t *Mask, 337e8d8bef9SDimitry Andric FrameIndexesCache &CacheFI, bool AllowGCPtrInCSR) 3385ffd83dbSDimitry Andric : MI(MI), MF(*MI.getMF()), TRI(*MF.getSubtarget().getRegisterInfo()), 3395ffd83dbSDimitry Andric TII(*MF.getSubtarget().getInstrInfo()), MFI(MF.getFrameInfo()), 340e8d8bef9SDimitry Andric Mask(Mask), CacheFI(CacheFI), AllowGCPtrInCSR(AllowGCPtrInCSR) { 341e8d8bef9SDimitry Andric 342e8d8bef9SDimitry Andric // Find statepoint's landing pad, if any. 343e8d8bef9SDimitry Andric EHPad = nullptr; 344e8d8bef9SDimitry Andric MachineBasicBlock *MBB = MI.getParent(); 345e8d8bef9SDimitry Andric // Invoke statepoint must be last one in block. 346e8d8bef9SDimitry Andric bool Last = std::none_of(++MI.getIterator(), MBB->end().getInstrIterator(), 347e8d8bef9SDimitry Andric [](MachineInstr &I) { 348e8d8bef9SDimitry Andric return I.getOpcode() == TargetOpcode::STATEPOINT; 349e8d8bef9SDimitry Andric }); 350e8d8bef9SDimitry Andric 351e8d8bef9SDimitry Andric if (!Last) 352e8d8bef9SDimitry Andric return; 353e8d8bef9SDimitry Andric 354e8d8bef9SDimitry Andric auto IsEHPad = [](MachineBasicBlock *B) { return B->isEHPad(); }; 355e8d8bef9SDimitry Andric 356e8d8bef9SDimitry Andric assert(llvm::count_if(MBB->successors(), IsEHPad) < 2 && "multiple EHPads"); 357e8d8bef9SDimitry Andric 358e8d8bef9SDimitry Andric auto It = llvm::find_if(MBB->successors(), IsEHPad); 359e8d8bef9SDimitry Andric if (It != MBB->succ_end()) 360e8d8bef9SDimitry Andric EHPad = *It; 361e8d8bef9SDimitry Andric } 362e8d8bef9SDimitry Andric 363e8d8bef9SDimitry Andric MachineBasicBlock *getEHPad() const { return EHPad; } 364e8d8bef9SDimitry Andric 3655ffd83dbSDimitry Andric // Return true if register is callee saved. 3665ffd83dbSDimitry Andric bool isCalleeSaved(Register Reg) { return (Mask[Reg / 32] >> Reg % 32) & 1; } 367e8d8bef9SDimitry Andric 3685ffd83dbSDimitry Andric // Iterates over statepoint meta args to find caller saver registers. 3695ffd83dbSDimitry Andric // Also cache the size of found registers. 3705ffd83dbSDimitry Andric // Returns true if caller save registers found. 3715ffd83dbSDimitry Andric bool findRegistersToSpill() { 372e8d8bef9SDimitry Andric SmallSet<Register, 8> GCRegs; 373e8d8bef9SDimitry Andric // All GC pointer operands assigned to registers produce new value. 374e8d8bef9SDimitry Andric // Since they're tied to their defs, it is enough to collect def registers. 375e8d8bef9SDimitry Andric for (const auto &Def : MI.defs()) 376e8d8bef9SDimitry Andric GCRegs.insert(Def.getReg()); 377e8d8bef9SDimitry Andric 3785ffd83dbSDimitry Andric SmallSet<Register, 8> VisitedRegs; 3795ffd83dbSDimitry Andric for (unsigned Idx = StatepointOpers(&MI).getVarIdx(), 3805ffd83dbSDimitry Andric EndIdx = MI.getNumOperands(); 3815ffd83dbSDimitry Andric Idx < EndIdx; ++Idx) { 3825ffd83dbSDimitry Andric MachineOperand &MO = MI.getOperand(Idx); 383e8d8bef9SDimitry Andric // Leave `undef` operands as is, StackMaps will rewrite them 384e8d8bef9SDimitry Andric // into a constant. 385e8d8bef9SDimitry Andric if (!MO.isReg() || MO.isImplicit() || MO.isUndef()) 3865ffd83dbSDimitry Andric continue; 3875ffd83dbSDimitry Andric Register Reg = MO.getReg(); 3885ffd83dbSDimitry Andric assert(Reg.isPhysical() && "Only physical regs are expected"); 389e8d8bef9SDimitry Andric 390e8d8bef9SDimitry Andric if (isCalleeSaved(Reg) && (AllowGCPtrInCSR || !is_contained(GCRegs, Reg))) 3915ffd83dbSDimitry Andric continue; 392e8d8bef9SDimitry Andric 393e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Will spill " << printReg(Reg, &TRI) << " at index " 394e8d8bef9SDimitry Andric << Idx << "\n"); 395e8d8bef9SDimitry Andric 3965ffd83dbSDimitry Andric if (VisitedRegs.insert(Reg).second) 3975ffd83dbSDimitry Andric RegsToSpill.push_back(Reg); 3985ffd83dbSDimitry Andric OpsToSpill.push_back(Idx); 3995ffd83dbSDimitry Andric } 4005ffd83dbSDimitry Andric CacheFI.sortRegisters(RegsToSpill); 4015ffd83dbSDimitry Andric return !RegsToSpill.empty(); 4025ffd83dbSDimitry Andric } 403e8d8bef9SDimitry Andric 4045ffd83dbSDimitry Andric // Spill all caller saved registers right before statepoint instruction. 4055ffd83dbSDimitry Andric // Remember frame index where register is spilled. 4065ffd83dbSDimitry Andric void spillRegisters() { 4075ffd83dbSDimitry Andric for (Register Reg : RegsToSpill) { 408e8d8bef9SDimitry Andric int FI = CacheFI.getFrameIndex(Reg, EHPad); 4095ffd83dbSDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 410e8d8bef9SDimitry Andric 4115ffd83dbSDimitry Andric NumSpilledRegisters++; 4125ffd83dbSDimitry Andric RegToSlotIdx[Reg] = FI; 413e8d8bef9SDimitry Andric 414e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, &TRI) << " to FI " << FI 415e8d8bef9SDimitry Andric << "\n"); 416e8d8bef9SDimitry Andric 417e8d8bef9SDimitry Andric // Perform trivial copy propagation 418e8d8bef9SDimitry Andric bool IsKill = true; 419e8d8bef9SDimitry Andric MachineBasicBlock::iterator InsertBefore(MI); 420e8d8bef9SDimitry Andric Reg = performCopyPropagation(Reg, InsertBefore, IsKill, TII, TRI); 421e8d8bef9SDimitry Andric 422e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Insert spill before " << *InsertBefore); 423e8d8bef9SDimitry Andric TII.storeRegToStackSlot(*MI.getParent(), InsertBefore, Reg, IsKill, FI, 424e8d8bef9SDimitry Andric RC, &TRI); 4255ffd83dbSDimitry Andric } 4265ffd83dbSDimitry Andric } 427e8d8bef9SDimitry Andric 428e8d8bef9SDimitry Andric void insertReloadBefore(unsigned Reg, MachineBasicBlock::iterator It, 429e8d8bef9SDimitry Andric MachineBasicBlock *MBB) { 430e8d8bef9SDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 431e8d8bef9SDimitry Andric int FI = RegToSlotIdx[Reg]; 432e8d8bef9SDimitry Andric if (It != MBB->end()) { 433e8d8bef9SDimitry Andric TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI); 434e8d8bef9SDimitry Andric return; 435e8d8bef9SDimitry Andric } 436e8d8bef9SDimitry Andric 437e8d8bef9SDimitry Andric // To insert reload at the end of MBB, insert it before last instruction 438e8d8bef9SDimitry Andric // and then swap them. 439e8d8bef9SDimitry Andric assert(!MBB->empty() && "Empty block"); 440e8d8bef9SDimitry Andric --It; 441e8d8bef9SDimitry Andric TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI); 442e8d8bef9SDimitry Andric MachineInstr *Reload = It->getPrevNode(); 443e8d8bef9SDimitry Andric int Dummy = 0; 444e8d8bef9SDimitry Andric (void)Dummy; 445e8d8bef9SDimitry Andric assert(TII.isLoadFromStackSlot(*Reload, Dummy) == Reg); 446e8d8bef9SDimitry Andric assert(Dummy == FI); 447e8d8bef9SDimitry Andric MBB->remove(Reload); 448e8d8bef9SDimitry Andric MBB->insertAfter(It, Reload); 449e8d8bef9SDimitry Andric } 450e8d8bef9SDimitry Andric 451e8d8bef9SDimitry Andric // Insert reloads of (relocated) registers spilled in statepoint. 452e8d8bef9SDimitry Andric void insertReloads(MachineInstr *NewStatepoint, RegReloadCache &RC) { 453e8d8bef9SDimitry Andric MachineBasicBlock *MBB = NewStatepoint->getParent(); 454e8d8bef9SDimitry Andric auto InsertPoint = std::next(NewStatepoint->getIterator()); 455e8d8bef9SDimitry Andric 456e8d8bef9SDimitry Andric for (auto Reg : RegsToReload) { 457e8d8bef9SDimitry Andric insertReloadBefore(Reg, InsertPoint, MBB); 458e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Reloading " << printReg(Reg, &TRI) << " from FI " 459e8d8bef9SDimitry Andric << RegToSlotIdx[Reg] << " after statepoint\n"); 460e8d8bef9SDimitry Andric 461e8d8bef9SDimitry Andric if (EHPad && !RC.hasReload(Reg, RegToSlotIdx[Reg], EHPad)) { 462e8d8bef9SDimitry Andric RC.recordReload(Reg, RegToSlotIdx[Reg], EHPad); 463e8d8bef9SDimitry Andric auto EHPadInsertPoint = EHPad->SkipPHIsLabelsAndDebug(EHPad->begin()); 464e8d8bef9SDimitry Andric insertReloadBefore(Reg, EHPadInsertPoint, EHPad); 465e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "...also reload at EHPad " 466e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 467e8d8bef9SDimitry Andric } 468e8d8bef9SDimitry Andric } 469e8d8bef9SDimitry Andric } 470e8d8bef9SDimitry Andric 4715ffd83dbSDimitry Andric // Re-write statepoint machine instruction to replace caller saved operands 4725ffd83dbSDimitry Andric // with indirect memory location (frame index). 473e8d8bef9SDimitry Andric MachineInstr *rewriteStatepoint() { 4745ffd83dbSDimitry Andric MachineInstr *NewMI = 4755ffd83dbSDimitry Andric MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true); 4765ffd83dbSDimitry Andric MachineInstrBuilder MIB(MF, NewMI); 4775ffd83dbSDimitry Andric 478e8d8bef9SDimitry Andric unsigned NumOps = MI.getNumOperands(); 479e8d8bef9SDimitry Andric 480e8d8bef9SDimitry Andric // New indices for the remaining defs. 481e8d8bef9SDimitry Andric SmallVector<unsigned, 8> NewIndices; 482e8d8bef9SDimitry Andric unsigned NumDefs = MI.getNumDefs(); 483e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumDefs; ++I) { 484e8d8bef9SDimitry Andric MachineOperand &DefMO = MI.getOperand(I); 485e8d8bef9SDimitry Andric assert(DefMO.isReg() && DefMO.isDef() && "Expected Reg Def operand"); 486e8d8bef9SDimitry Andric Register Reg = DefMO.getReg(); 487*fe6060f1SDimitry Andric assert(DefMO.isTied() && "Def is expected to be tied"); 488*fe6060f1SDimitry Andric // We skipped undef uses and did not spill them, so we should not 489*fe6060f1SDimitry Andric // proceed with defs here. 490*fe6060f1SDimitry Andric if (MI.getOperand(MI.findTiedOperandIdx(I)).isUndef()) { 491*fe6060f1SDimitry Andric if (AllowGCPtrInCSR) { 492*fe6060f1SDimitry Andric NewIndices.push_back(NewMI->getNumOperands()); 493*fe6060f1SDimitry Andric MIB.addReg(Reg, RegState::Define); 494*fe6060f1SDimitry Andric } 495*fe6060f1SDimitry Andric continue; 496*fe6060f1SDimitry Andric } 497e8d8bef9SDimitry Andric if (!AllowGCPtrInCSR) { 498e8d8bef9SDimitry Andric assert(is_contained(RegsToSpill, Reg)); 499e8d8bef9SDimitry Andric RegsToReload.push_back(Reg); 500e8d8bef9SDimitry Andric } else { 501e8d8bef9SDimitry Andric if (isCalleeSaved(Reg)) { 502e8d8bef9SDimitry Andric NewIndices.push_back(NewMI->getNumOperands()); 503e8d8bef9SDimitry Andric MIB.addReg(Reg, RegState::Define); 504e8d8bef9SDimitry Andric } else { 505e8d8bef9SDimitry Andric NewIndices.push_back(NumOps); 506e8d8bef9SDimitry Andric RegsToReload.push_back(Reg); 507e8d8bef9SDimitry Andric } 508e8d8bef9SDimitry Andric } 509e8d8bef9SDimitry Andric } 510e8d8bef9SDimitry Andric 5115ffd83dbSDimitry Andric // Add End marker. 5125ffd83dbSDimitry Andric OpsToSpill.push_back(MI.getNumOperands()); 5135ffd83dbSDimitry Andric unsigned CurOpIdx = 0; 5145ffd83dbSDimitry Andric 515e8d8bef9SDimitry Andric for (unsigned I = NumDefs; I < MI.getNumOperands(); ++I) { 5165ffd83dbSDimitry Andric MachineOperand &MO = MI.getOperand(I); 5175ffd83dbSDimitry Andric if (I == OpsToSpill[CurOpIdx]) { 5185ffd83dbSDimitry Andric int FI = RegToSlotIdx[MO.getReg()]; 5195ffd83dbSDimitry Andric MIB.addImm(StackMaps::IndirectMemRefOp); 5205ffd83dbSDimitry Andric MIB.addImm(getRegisterSize(TRI, MO.getReg())); 5215ffd83dbSDimitry Andric assert(MO.isReg() && "Should be register"); 5225ffd83dbSDimitry Andric assert(MO.getReg().isPhysical() && "Should be physical register"); 5235ffd83dbSDimitry Andric MIB.addFrameIndex(FI); 5245ffd83dbSDimitry Andric MIB.addImm(0); 5255ffd83dbSDimitry Andric ++CurOpIdx; 526e8d8bef9SDimitry Andric } else { 5275ffd83dbSDimitry Andric MIB.add(MO); 528e8d8bef9SDimitry Andric unsigned OldDef; 529e8d8bef9SDimitry Andric if (AllowGCPtrInCSR && MI.isRegTiedToDefOperand(I, &OldDef)) { 530e8d8bef9SDimitry Andric assert(OldDef < NumDefs); 531e8d8bef9SDimitry Andric assert(NewIndices[OldDef] < NumOps); 532e8d8bef9SDimitry Andric MIB->tieOperands(NewIndices[OldDef], MIB->getNumOperands() - 1); 533e8d8bef9SDimitry Andric } 534e8d8bef9SDimitry Andric } 5355ffd83dbSDimitry Andric } 5365ffd83dbSDimitry Andric assert(CurOpIdx == (OpsToSpill.size() - 1) && "Not all operands processed"); 5375ffd83dbSDimitry Andric // Add mem operands. 5385ffd83dbSDimitry Andric NewMI->setMemRefs(MF, MI.memoperands()); 5395ffd83dbSDimitry Andric for (auto It : RegToSlotIdx) { 540e8d8bef9SDimitry Andric Register R = It.first; 5415ffd83dbSDimitry Andric int FrameIndex = It.second; 5425ffd83dbSDimitry Andric auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex); 543e8d8bef9SDimitry Andric MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad; 544e8d8bef9SDimitry Andric if (is_contained(RegsToReload, R)) 545e8d8bef9SDimitry Andric Flags |= MachineMemOperand::MOStore; 546e8d8bef9SDimitry Andric auto *MMO = 547e8d8bef9SDimitry Andric MF.getMachineMemOperand(PtrInfo, Flags, getRegisterSize(TRI, R), 5485ffd83dbSDimitry Andric MFI.getObjectAlign(FrameIndex)); 5495ffd83dbSDimitry Andric NewMI->addMemOperand(MF, MMO); 5505ffd83dbSDimitry Andric } 551e8d8bef9SDimitry Andric 5525ffd83dbSDimitry Andric // Insert new statepoint and erase old one. 5535ffd83dbSDimitry Andric MI.getParent()->insert(MI, NewMI); 554e8d8bef9SDimitry Andric 555e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "rewritten statepoint to : " << *NewMI << "\n"); 5565ffd83dbSDimitry Andric MI.eraseFromParent(); 557e8d8bef9SDimitry Andric return NewMI; 5585ffd83dbSDimitry Andric } 5595ffd83dbSDimitry Andric }; 5605ffd83dbSDimitry Andric 5615ffd83dbSDimitry Andric class StatepointProcessor { 5625ffd83dbSDimitry Andric private: 5635ffd83dbSDimitry Andric MachineFunction &MF; 5645ffd83dbSDimitry Andric const TargetRegisterInfo &TRI; 5655ffd83dbSDimitry Andric FrameIndexesCache CacheFI; 566e8d8bef9SDimitry Andric RegReloadCache ReloadCache; 5675ffd83dbSDimitry Andric 5685ffd83dbSDimitry Andric public: 5695ffd83dbSDimitry Andric StatepointProcessor(MachineFunction &MF) 5705ffd83dbSDimitry Andric : MF(MF), TRI(*MF.getSubtarget().getRegisterInfo()), 5715ffd83dbSDimitry Andric CacheFI(MF.getFrameInfo(), TRI) {} 5725ffd83dbSDimitry Andric 573e8d8bef9SDimitry Andric bool process(MachineInstr &MI, bool AllowGCPtrInCSR) { 5745ffd83dbSDimitry Andric StatepointOpers SO(&MI); 5755ffd83dbSDimitry Andric uint64_t Flags = SO.getFlags(); 5765ffd83dbSDimitry Andric // Do nothing for LiveIn, it supports all registers. 5775ffd83dbSDimitry Andric if (Flags & (uint64_t)StatepointFlags::DeoptLiveIn) 5785ffd83dbSDimitry Andric return false; 579e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "\nMBB " << MI.getParent()->getNumber() << " " 580e8d8bef9SDimitry Andric << MI.getParent()->getName() << " : process statepoint " 581e8d8bef9SDimitry Andric << MI); 5825ffd83dbSDimitry Andric CallingConv::ID CC = SO.getCallingConv(); 5835ffd83dbSDimitry Andric const uint32_t *Mask = TRI.getCallPreservedMask(MF, CC); 584e8d8bef9SDimitry Andric StatepointState SS(MI, Mask, CacheFI, AllowGCPtrInCSR); 585e8d8bef9SDimitry Andric CacheFI.reset(SS.getEHPad()); 5865ffd83dbSDimitry Andric 5875ffd83dbSDimitry Andric if (!SS.findRegistersToSpill()) 5885ffd83dbSDimitry Andric return false; 5895ffd83dbSDimitry Andric 5905ffd83dbSDimitry Andric SS.spillRegisters(); 591e8d8bef9SDimitry Andric auto *NewStatepoint = SS.rewriteStatepoint(); 592e8d8bef9SDimitry Andric SS.insertReloads(NewStatepoint, ReloadCache); 5935ffd83dbSDimitry Andric return true; 5945ffd83dbSDimitry Andric } 5955ffd83dbSDimitry Andric }; 5965ffd83dbSDimitry Andric } // namespace 5975ffd83dbSDimitry Andric 5985ffd83dbSDimitry Andric bool FixupStatepointCallerSaved::runOnMachineFunction(MachineFunction &MF) { 5995ffd83dbSDimitry Andric if (skipFunction(MF.getFunction())) 6005ffd83dbSDimitry Andric return false; 6015ffd83dbSDimitry Andric 6025ffd83dbSDimitry Andric const Function &F = MF.getFunction(); 6035ffd83dbSDimitry Andric if (!F.hasGC()) 6045ffd83dbSDimitry Andric return false; 6055ffd83dbSDimitry Andric 6065ffd83dbSDimitry Andric SmallVector<MachineInstr *, 16> Statepoints; 6075ffd83dbSDimitry Andric for (MachineBasicBlock &BB : MF) 6085ffd83dbSDimitry Andric for (MachineInstr &I : BB) 6095ffd83dbSDimitry Andric if (I.getOpcode() == TargetOpcode::STATEPOINT) 6105ffd83dbSDimitry Andric Statepoints.push_back(&I); 6115ffd83dbSDimitry Andric 6125ffd83dbSDimitry Andric if (Statepoints.empty()) 6135ffd83dbSDimitry Andric return false; 6145ffd83dbSDimitry Andric 6155ffd83dbSDimitry Andric bool Changed = false; 6165ffd83dbSDimitry Andric StatepointProcessor SPP(MF); 617e8d8bef9SDimitry Andric unsigned NumStatepoints = 0; 618e8d8bef9SDimitry Andric bool AllowGCPtrInCSR = PassGCPtrInCSR; 619e8d8bef9SDimitry Andric for (MachineInstr *I : Statepoints) { 620e8d8bef9SDimitry Andric ++NumStatepoints; 621e8d8bef9SDimitry Andric if (MaxStatepointsWithRegs.getNumOccurrences() && 622e8d8bef9SDimitry Andric NumStatepoints >= MaxStatepointsWithRegs) 623e8d8bef9SDimitry Andric AllowGCPtrInCSR = false; 624e8d8bef9SDimitry Andric Changed |= SPP.process(*I, AllowGCPtrInCSR); 625e8d8bef9SDimitry Andric } 6265ffd83dbSDimitry Andric return Changed; 6275ffd83dbSDimitry Andric } 628