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