15ffd83dbSDimitry Andric //===-- FixupStatepointCallerSaved.cpp - Fixup caller saved registers ----===// 25ffd83dbSDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry 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/StackMaps.h" 285ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 295ffd83dbSDimitry Andric #include "llvm/IR/Statepoint.h" 305ffd83dbSDimitry Andric #include "llvm/InitializePasses.h" 315ffd83dbSDimitry Andric #include "llvm/Support/Debug.h" 325ffd83dbSDimitry Andric 335ffd83dbSDimitry Andric using namespace llvm; 345ffd83dbSDimitry Andric 355ffd83dbSDimitry Andric #define DEBUG_TYPE "fixup-statepoint-caller-saved" 365ffd83dbSDimitry Andric STATISTIC(NumSpilledRegisters, "Number of spilled register"); 375ffd83dbSDimitry Andric STATISTIC(NumSpillSlotsAllocated, "Number of spill slots allocated"); 385ffd83dbSDimitry Andric STATISTIC(NumSpillSlotsExtended, "Number of spill slots extended"); 395ffd83dbSDimitry Andric 405ffd83dbSDimitry Andric static cl::opt<bool> FixupSCSExtendSlotSize( 415ffd83dbSDimitry Andric "fixup-scs-extend-slot-size", cl::Hidden, cl::init(false), 425ffd83dbSDimitry Andric cl::desc("Allow spill in spill slot of greater size than register size"), 435ffd83dbSDimitry Andric cl::Hidden); 445ffd83dbSDimitry Andric 45e8d8bef9SDimitry Andric static cl::opt<bool> PassGCPtrInCSR( 46e8d8bef9SDimitry Andric "fixup-allow-gcptr-in-csr", cl::Hidden, cl::init(false), 47e8d8bef9SDimitry Andric cl::desc("Allow passing GC Pointer arguments in callee saved registers")); 48e8d8bef9SDimitry Andric 49e8d8bef9SDimitry Andric static cl::opt<bool> EnableCopyProp( 50e8d8bef9SDimitry Andric "fixup-scs-enable-copy-propagation", cl::Hidden, cl::init(true), 51e8d8bef9SDimitry Andric cl::desc("Enable simple copy propagation during register reloading")); 52e8d8bef9SDimitry Andric 53e8d8bef9SDimitry Andric // This is purely debugging option. 54e8d8bef9SDimitry Andric // It may be handy for investigating statepoint spilling issues. 55e8d8bef9SDimitry Andric static cl::opt<unsigned> MaxStatepointsWithRegs( 56e8d8bef9SDimitry Andric "fixup-max-csr-statepoints", cl::Hidden, 57e8d8bef9SDimitry Andric cl::desc("Max number of statepoints allowed to pass GC Ptrs in registers")); 58e8d8bef9SDimitry Andric 595ffd83dbSDimitry Andric namespace { 605ffd83dbSDimitry Andric 615ffd83dbSDimitry Andric class FixupStatepointCallerSaved : public MachineFunctionPass { 625ffd83dbSDimitry Andric public: 635ffd83dbSDimitry Andric static char ID; 645ffd83dbSDimitry Andric 655ffd83dbSDimitry Andric FixupStatepointCallerSaved() : MachineFunctionPass(ID) { 665ffd83dbSDimitry Andric initializeFixupStatepointCallerSavedPass(*PassRegistry::getPassRegistry()); 675ffd83dbSDimitry Andric } 685ffd83dbSDimitry Andric 695ffd83dbSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 705ffd83dbSDimitry Andric AU.setPreservesCFG(); 715ffd83dbSDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 725ffd83dbSDimitry Andric } 735ffd83dbSDimitry Andric 745ffd83dbSDimitry Andric StringRef getPassName() const override { 755ffd83dbSDimitry Andric return "Fixup Statepoint Caller Saved"; 765ffd83dbSDimitry Andric } 775ffd83dbSDimitry Andric 785ffd83dbSDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 795ffd83dbSDimitry Andric }; 80e8d8bef9SDimitry Andric 815ffd83dbSDimitry Andric } // End anonymous namespace. 825ffd83dbSDimitry Andric 835ffd83dbSDimitry Andric char FixupStatepointCallerSaved::ID = 0; 845ffd83dbSDimitry Andric char &llvm::FixupStatepointCallerSavedID = FixupStatepointCallerSaved::ID; 855ffd83dbSDimitry Andric 865ffd83dbSDimitry Andric INITIALIZE_PASS_BEGIN(FixupStatepointCallerSaved, DEBUG_TYPE, 875ffd83dbSDimitry Andric "Fixup Statepoint Caller Saved", false, false) 885ffd83dbSDimitry Andric INITIALIZE_PASS_END(FixupStatepointCallerSaved, DEBUG_TYPE, 895ffd83dbSDimitry Andric "Fixup Statepoint Caller Saved", false, false) 905ffd83dbSDimitry Andric 915ffd83dbSDimitry Andric // Utility function to get size of the register. 925ffd83dbSDimitry Andric static unsigned getRegisterSize(const TargetRegisterInfo &TRI, Register Reg) { 935ffd83dbSDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 945ffd83dbSDimitry Andric return TRI.getSpillSize(*RC); 955ffd83dbSDimitry Andric } 965ffd83dbSDimitry Andric 97e8d8bef9SDimitry Andric // Try to eliminate redundant copy to register which we're going to 98e8d8bef9SDimitry Andric // spill, i.e. try to change: 99e8d8bef9SDimitry Andric // X = COPY Y 100e8d8bef9SDimitry Andric // SPILL X 101e8d8bef9SDimitry Andric // to 102e8d8bef9SDimitry Andric // SPILL Y 103e8d8bef9SDimitry Andric // If there are no uses of X between copy and STATEPOINT, that COPY 104e8d8bef9SDimitry Andric // may be eliminated. 105e8d8bef9SDimitry Andric // Reg - register we're about to spill 106e8d8bef9SDimitry Andric // RI - On entry points to statepoint. 107e8d8bef9SDimitry Andric // On successful copy propagation set to new spill point. 108e8d8bef9SDimitry Andric // IsKill - set to true if COPY is Kill (there are no uses of Y) 109e8d8bef9SDimitry Andric // Returns either found source copy register or original one. 110e8d8bef9SDimitry Andric static Register performCopyPropagation(Register Reg, 111e8d8bef9SDimitry Andric MachineBasicBlock::iterator &RI, 112e8d8bef9SDimitry Andric bool &IsKill, const TargetInstrInfo &TII, 113e8d8bef9SDimitry Andric const TargetRegisterInfo &TRI) { 114e8d8bef9SDimitry Andric // First check if statepoint itself uses Reg in non-meta operands. 115*0fca6ea1SDimitry Andric int Idx = RI->findRegisterUseOperandIdx(Reg, &TRI, false); 116e8d8bef9SDimitry Andric if (Idx >= 0 && (unsigned)Idx < StatepointOpers(&*RI).getNumDeoptArgsIdx()) { 117e8d8bef9SDimitry Andric IsKill = false; 118e8d8bef9SDimitry Andric return Reg; 119e8d8bef9SDimitry Andric } 120e8d8bef9SDimitry Andric 121e8d8bef9SDimitry Andric if (!EnableCopyProp) 122e8d8bef9SDimitry Andric return Reg; 123e8d8bef9SDimitry Andric 124e8d8bef9SDimitry Andric MachineBasicBlock *MBB = RI->getParent(); 125e8d8bef9SDimitry Andric MachineBasicBlock::reverse_iterator E = MBB->rend(); 126e8d8bef9SDimitry Andric MachineInstr *Def = nullptr, *Use = nullptr; 127e8d8bef9SDimitry Andric for (auto It = ++(RI.getReverse()); It != E; ++It) { 128e8d8bef9SDimitry Andric if (It->readsRegister(Reg, &TRI) && !Use) 129e8d8bef9SDimitry Andric Use = &*It; 130e8d8bef9SDimitry Andric if (It->modifiesRegister(Reg, &TRI)) { 131e8d8bef9SDimitry Andric Def = &*It; 132e8d8bef9SDimitry Andric break; 133e8d8bef9SDimitry Andric } 134e8d8bef9SDimitry Andric } 135e8d8bef9SDimitry Andric 136e8d8bef9SDimitry Andric if (!Def) 137e8d8bef9SDimitry Andric return Reg; 138e8d8bef9SDimitry Andric 139e8d8bef9SDimitry Andric auto DestSrc = TII.isCopyInstr(*Def); 140e8d8bef9SDimitry Andric if (!DestSrc || DestSrc->Destination->getReg() != Reg) 141e8d8bef9SDimitry Andric return Reg; 142e8d8bef9SDimitry Andric 143e8d8bef9SDimitry Andric Register SrcReg = DestSrc->Source->getReg(); 144e8d8bef9SDimitry Andric 145e8d8bef9SDimitry Andric if (getRegisterSize(TRI, Reg) != getRegisterSize(TRI, SrcReg)) 146e8d8bef9SDimitry Andric return Reg; 147e8d8bef9SDimitry Andric 148e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "spillRegisters: perform copy propagation " 149e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " -> " << printReg(SrcReg, &TRI) 150e8d8bef9SDimitry Andric << "\n"); 151e8d8bef9SDimitry Andric 152e8d8bef9SDimitry Andric // Insert spill immediately after Def 153e8d8bef9SDimitry Andric RI = ++MachineBasicBlock::iterator(Def); 154e8d8bef9SDimitry Andric IsKill = DestSrc->Source->isKill(); 155e8d8bef9SDimitry Andric 15681ad6265SDimitry Andric if (!Use) { 157e8d8bef9SDimitry Andric // There are no uses of original register between COPY and STATEPOINT. 158e8d8bef9SDimitry Andric // There can't be any after STATEPOINT, so we can eliminate Def. 159e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "spillRegisters: removing dead copy " << *Def); 160e8d8bef9SDimitry Andric Def->eraseFromParent(); 16181ad6265SDimitry Andric } else if (IsKill) { 16281ad6265SDimitry Andric // COPY will remain in place, spill will be inserted *after* it, so it is 16381ad6265SDimitry Andric // not a kill of source anymore. 16481ad6265SDimitry Andric const_cast<MachineOperand *>(DestSrc->Source)->setIsKill(false); 165e8d8bef9SDimitry Andric } 16681ad6265SDimitry Andric 167e8d8bef9SDimitry Andric return SrcReg; 168e8d8bef9SDimitry Andric } 169e8d8bef9SDimitry Andric 1705ffd83dbSDimitry Andric namespace { 171e8d8bef9SDimitry Andric // Pair {Register, FrameIndex} 172e8d8bef9SDimitry Andric using RegSlotPair = std::pair<Register, int>; 173e8d8bef9SDimitry Andric 174e8d8bef9SDimitry Andric // Keeps track of what reloads were inserted in MBB. 175e8d8bef9SDimitry Andric class RegReloadCache { 176e8d8bef9SDimitry Andric using ReloadSet = SmallSet<RegSlotPair, 8>; 177e8d8bef9SDimitry Andric DenseMap<const MachineBasicBlock *, ReloadSet> Reloads; 178e8d8bef9SDimitry Andric 179e8d8bef9SDimitry Andric public: 180e8d8bef9SDimitry Andric RegReloadCache() = default; 181e8d8bef9SDimitry Andric 182e8d8bef9SDimitry Andric // Record reload of Reg from FI in block MBB 183e8d8bef9SDimitry Andric void recordReload(Register Reg, int FI, const MachineBasicBlock *MBB) { 184e8d8bef9SDimitry Andric RegSlotPair RSP(Reg, FI); 185e8d8bef9SDimitry Andric auto Res = Reloads[MBB].insert(RSP); 186e8d8bef9SDimitry Andric (void)Res; 187e8d8bef9SDimitry Andric assert(Res.second && "reload already exists"); 188e8d8bef9SDimitry Andric } 189e8d8bef9SDimitry Andric 190e8d8bef9SDimitry Andric // Does basic block MBB contains reload of Reg from FI? 191e8d8bef9SDimitry Andric bool hasReload(Register Reg, int FI, const MachineBasicBlock *MBB) { 192e8d8bef9SDimitry Andric RegSlotPair RSP(Reg, FI); 193e8d8bef9SDimitry Andric return Reloads.count(MBB) && Reloads[MBB].count(RSP); 194e8d8bef9SDimitry Andric } 195e8d8bef9SDimitry Andric }; 196e8d8bef9SDimitry Andric 1975ffd83dbSDimitry Andric // Cache used frame indexes during statepoint re-write to re-use them in 1985ffd83dbSDimitry Andric // processing next statepoint instruction. 1995ffd83dbSDimitry Andric // Two strategies. One is to preserve the size of spill slot while another one 2005ffd83dbSDimitry Andric // extends the size of spill slots to reduce the number of them, causing 2015ffd83dbSDimitry Andric // the less total frame size. But unspill will have "implicit" any extend. 2025ffd83dbSDimitry Andric class FrameIndexesCache { 2035ffd83dbSDimitry Andric private: 2045ffd83dbSDimitry Andric struct FrameIndexesPerSize { 2055ffd83dbSDimitry Andric // List of used frame indexes during processing previous statepoints. 2065ffd83dbSDimitry Andric SmallVector<int, 8> Slots; 2075ffd83dbSDimitry Andric // Current index of un-used yet frame index. 2085ffd83dbSDimitry Andric unsigned Index = 0; 2095ffd83dbSDimitry Andric }; 2105ffd83dbSDimitry Andric MachineFrameInfo &MFI; 2115ffd83dbSDimitry Andric const TargetRegisterInfo &TRI; 2125ffd83dbSDimitry Andric // Map size to list of frame indexes of this size. If the mode is 2135ffd83dbSDimitry Andric // FixupSCSExtendSlotSize then the key 0 is used to keep all frame indexes. 2145ffd83dbSDimitry Andric // If the size of required spill slot is greater than in a cache then the 2155ffd83dbSDimitry Andric // size will be increased. 2165ffd83dbSDimitry Andric DenseMap<unsigned, FrameIndexesPerSize> Cache; 2175ffd83dbSDimitry Andric 218e8d8bef9SDimitry Andric // Keeps track of slots reserved for the shared landing pad processing. 219e8d8bef9SDimitry Andric // Initialized from GlobalIndices for the current EHPad. 220e8d8bef9SDimitry Andric SmallSet<int, 8> ReservedSlots; 221e8d8bef9SDimitry Andric 222e8d8bef9SDimitry Andric // Landing pad can be destination of several statepoints. Every register 223e8d8bef9SDimitry Andric // defined by such statepoints must be spilled to the same stack slot. 224e8d8bef9SDimitry Andric // This map keeps that information. 225e8d8bef9SDimitry Andric DenseMap<const MachineBasicBlock *, SmallVector<RegSlotPair, 8>> 226e8d8bef9SDimitry Andric GlobalIndices; 227e8d8bef9SDimitry Andric 228e8d8bef9SDimitry Andric FrameIndexesPerSize &getCacheBucket(unsigned Size) { 229e8d8bef9SDimitry Andric // In FixupSCSExtendSlotSize mode the bucket with 0 index is used 230e8d8bef9SDimitry Andric // for all sizes. 231e8d8bef9SDimitry Andric return Cache[FixupSCSExtendSlotSize ? 0 : Size]; 232e8d8bef9SDimitry Andric } 233e8d8bef9SDimitry Andric 2345ffd83dbSDimitry Andric public: 2355ffd83dbSDimitry Andric FrameIndexesCache(MachineFrameInfo &MFI, const TargetRegisterInfo &TRI) 2365ffd83dbSDimitry Andric : MFI(MFI), TRI(TRI) {} 2375ffd83dbSDimitry Andric // Reset the current state of used frame indexes. After invocation of 238e8d8bef9SDimitry Andric // this function all frame indexes are available for allocation with 239e8d8bef9SDimitry Andric // the exception of slots reserved for landing pad processing (if any). 240e8d8bef9SDimitry Andric void reset(const MachineBasicBlock *EHPad) { 2415ffd83dbSDimitry Andric for (auto &It : Cache) 2425ffd83dbSDimitry Andric It.second.Index = 0; 243e8d8bef9SDimitry Andric 244e8d8bef9SDimitry Andric ReservedSlots.clear(); 245e8d8bef9SDimitry Andric if (EHPad && GlobalIndices.count(EHPad)) 246e8d8bef9SDimitry Andric for (auto &RSP : GlobalIndices[EHPad]) 247e8d8bef9SDimitry Andric ReservedSlots.insert(RSP.second); 2485ffd83dbSDimitry Andric } 249e8d8bef9SDimitry Andric 2505ffd83dbSDimitry Andric // Get frame index to spill the register. 251e8d8bef9SDimitry Andric int getFrameIndex(Register Reg, MachineBasicBlock *EHPad) { 252e8d8bef9SDimitry Andric // Check if slot for Reg is already reserved at EHPad. 253e8d8bef9SDimitry Andric auto It = GlobalIndices.find(EHPad); 254e8d8bef9SDimitry Andric if (It != GlobalIndices.end()) { 255e8d8bef9SDimitry Andric auto &Vec = It->second; 256e8d8bef9SDimitry Andric auto Idx = llvm::find_if( 257e8d8bef9SDimitry Andric Vec, [Reg](RegSlotPair &RSP) { return Reg == RSP.first; }); 258e8d8bef9SDimitry Andric if (Idx != Vec.end()) { 259e8d8bef9SDimitry Andric int FI = Idx->second; 260e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Found global FI " << FI << " for register " 261e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " at " 262e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 263e8d8bef9SDimitry Andric assert(ReservedSlots.count(FI) && "using unreserved slot"); 264e8d8bef9SDimitry Andric return FI; 265e8d8bef9SDimitry Andric } 266e8d8bef9SDimitry Andric } 267e8d8bef9SDimitry Andric 2685ffd83dbSDimitry Andric unsigned Size = getRegisterSize(TRI, Reg); 269e8d8bef9SDimitry Andric FrameIndexesPerSize &Line = getCacheBucket(Size); 270e8d8bef9SDimitry Andric while (Line.Index < Line.Slots.size()) { 2715ffd83dbSDimitry Andric int FI = Line.Slots[Line.Index++]; 272e8d8bef9SDimitry Andric if (ReservedSlots.count(FI)) 273e8d8bef9SDimitry Andric continue; 2745ffd83dbSDimitry Andric // If all sizes are kept together we probably need to extend the 2755ffd83dbSDimitry Andric // spill slot size. 2765ffd83dbSDimitry Andric if (MFI.getObjectSize(FI) < Size) { 2775ffd83dbSDimitry Andric MFI.setObjectSize(FI, Size); 2785ffd83dbSDimitry Andric MFI.setObjectAlignment(FI, Align(Size)); 2795ffd83dbSDimitry Andric NumSpillSlotsExtended++; 2805ffd83dbSDimitry Andric } 2815ffd83dbSDimitry Andric return FI; 2825ffd83dbSDimitry Andric } 2835ffd83dbSDimitry Andric int FI = MFI.CreateSpillStackObject(Size, Align(Size)); 2845ffd83dbSDimitry Andric NumSpillSlotsAllocated++; 2855ffd83dbSDimitry Andric Line.Slots.push_back(FI); 2865ffd83dbSDimitry Andric ++Line.Index; 287e8d8bef9SDimitry Andric 288e8d8bef9SDimitry Andric // Remember assignment {Reg, FI} for EHPad 289e8d8bef9SDimitry Andric if (EHPad) { 290e8d8bef9SDimitry Andric GlobalIndices[EHPad].push_back(std::make_pair(Reg, FI)); 291e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Reserved FI " << FI << " for spilling reg " 292e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " at landing pad " 293e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 294e8d8bef9SDimitry Andric } 295e8d8bef9SDimitry Andric 2965ffd83dbSDimitry Andric return FI; 2975ffd83dbSDimitry Andric } 298e8d8bef9SDimitry Andric 2995ffd83dbSDimitry Andric // Sort all registers to spill in descendent order. In the 3005ffd83dbSDimitry Andric // FixupSCSExtendSlotSize mode it will minimize the total frame size. 3015ffd83dbSDimitry Andric // In non FixupSCSExtendSlotSize mode we can skip this step. 3025ffd83dbSDimitry Andric void sortRegisters(SmallVectorImpl<Register> &Regs) { 3035ffd83dbSDimitry Andric if (!FixupSCSExtendSlotSize) 3045ffd83dbSDimitry Andric return; 305e8d8bef9SDimitry Andric llvm::sort(Regs, [&](Register &A, Register &B) { 3065ffd83dbSDimitry Andric return getRegisterSize(TRI, A) > getRegisterSize(TRI, B); 3075ffd83dbSDimitry Andric }); 3085ffd83dbSDimitry Andric } 3095ffd83dbSDimitry Andric }; 3105ffd83dbSDimitry Andric 3115ffd83dbSDimitry Andric // Describes the state of the current processing statepoint instruction. 3125ffd83dbSDimitry Andric class StatepointState { 3135ffd83dbSDimitry Andric private: 3145ffd83dbSDimitry Andric // statepoint instruction. 3155ffd83dbSDimitry Andric MachineInstr &MI; 3165ffd83dbSDimitry Andric MachineFunction &MF; 317e8d8bef9SDimitry Andric // If non-null then statepoint is invoke, and this points to the landing pad. 318e8d8bef9SDimitry Andric MachineBasicBlock *EHPad; 3195ffd83dbSDimitry Andric const TargetRegisterInfo &TRI; 3205ffd83dbSDimitry Andric const TargetInstrInfo &TII; 3215ffd83dbSDimitry Andric MachineFrameInfo &MFI; 3225ffd83dbSDimitry Andric // Mask with callee saved registers. 3235ffd83dbSDimitry Andric const uint32_t *Mask; 3245ffd83dbSDimitry Andric // Cache of frame indexes used on previous instruction processing. 3255ffd83dbSDimitry Andric FrameIndexesCache &CacheFI; 326e8d8bef9SDimitry Andric bool AllowGCPtrInCSR; 3275ffd83dbSDimitry Andric // Operands with physical registers requiring spilling. 3285ffd83dbSDimitry Andric SmallVector<unsigned, 8> OpsToSpill; 3295ffd83dbSDimitry Andric // Set of register to spill. 3305ffd83dbSDimitry Andric SmallVector<Register, 8> RegsToSpill; 331e8d8bef9SDimitry Andric // Set of registers to reload after statepoint. 332e8d8bef9SDimitry Andric SmallVector<Register, 8> RegsToReload; 3335ffd83dbSDimitry Andric // Map Register to Frame Slot index. 3345ffd83dbSDimitry Andric DenseMap<Register, int> RegToSlotIdx; 3355ffd83dbSDimitry Andric 3365ffd83dbSDimitry Andric public: 3375ffd83dbSDimitry Andric StatepointState(MachineInstr &MI, const uint32_t *Mask, 338e8d8bef9SDimitry Andric FrameIndexesCache &CacheFI, bool AllowGCPtrInCSR) 3395ffd83dbSDimitry Andric : MI(MI), MF(*MI.getMF()), TRI(*MF.getSubtarget().getRegisterInfo()), 3405ffd83dbSDimitry Andric TII(*MF.getSubtarget().getInstrInfo()), MFI(MF.getFrameInfo()), 341e8d8bef9SDimitry Andric Mask(Mask), CacheFI(CacheFI), AllowGCPtrInCSR(AllowGCPtrInCSR) { 342e8d8bef9SDimitry Andric 343e8d8bef9SDimitry Andric // Find statepoint's landing pad, if any. 344e8d8bef9SDimitry Andric EHPad = nullptr; 345e8d8bef9SDimitry Andric MachineBasicBlock *MBB = MI.getParent(); 346e8d8bef9SDimitry Andric // Invoke statepoint must be last one in block. 347e8d8bef9SDimitry Andric bool Last = std::none_of(++MI.getIterator(), MBB->end().getInstrIterator(), 348e8d8bef9SDimitry Andric [](MachineInstr &I) { 349e8d8bef9SDimitry Andric return I.getOpcode() == TargetOpcode::STATEPOINT; 350e8d8bef9SDimitry Andric }); 351e8d8bef9SDimitry Andric 352e8d8bef9SDimitry Andric if (!Last) 353e8d8bef9SDimitry Andric return; 354e8d8bef9SDimitry Andric 355e8d8bef9SDimitry Andric auto IsEHPad = [](MachineBasicBlock *B) { return B->isEHPad(); }; 356e8d8bef9SDimitry Andric 357e8d8bef9SDimitry Andric assert(llvm::count_if(MBB->successors(), IsEHPad) < 2 && "multiple EHPads"); 358e8d8bef9SDimitry Andric 359e8d8bef9SDimitry Andric auto It = llvm::find_if(MBB->successors(), IsEHPad); 360e8d8bef9SDimitry Andric if (It != MBB->succ_end()) 361e8d8bef9SDimitry Andric EHPad = *It; 362e8d8bef9SDimitry Andric } 363e8d8bef9SDimitry Andric 364e8d8bef9SDimitry Andric MachineBasicBlock *getEHPad() const { return EHPad; } 365e8d8bef9SDimitry Andric 3665ffd83dbSDimitry Andric // Return true if register is callee saved. 3675ffd83dbSDimitry Andric bool isCalleeSaved(Register Reg) { return (Mask[Reg / 32] >> Reg % 32) & 1; } 368e8d8bef9SDimitry Andric 3695ffd83dbSDimitry Andric // Iterates over statepoint meta args to find caller saver registers. 3705ffd83dbSDimitry Andric // Also cache the size of found registers. 3715ffd83dbSDimitry Andric // Returns true if caller save registers found. 3725ffd83dbSDimitry Andric bool findRegistersToSpill() { 373e8d8bef9SDimitry Andric SmallSet<Register, 8> GCRegs; 374e8d8bef9SDimitry Andric // All GC pointer operands assigned to registers produce new value. 375e8d8bef9SDimitry Andric // Since they're tied to their defs, it is enough to collect def registers. 376e8d8bef9SDimitry Andric for (const auto &Def : MI.defs()) 377e8d8bef9SDimitry Andric GCRegs.insert(Def.getReg()); 378e8d8bef9SDimitry Andric 3795ffd83dbSDimitry Andric SmallSet<Register, 8> VisitedRegs; 3805ffd83dbSDimitry Andric for (unsigned Idx = StatepointOpers(&MI).getVarIdx(), 3815ffd83dbSDimitry Andric EndIdx = MI.getNumOperands(); 3825ffd83dbSDimitry Andric Idx < EndIdx; ++Idx) { 3835ffd83dbSDimitry Andric MachineOperand &MO = MI.getOperand(Idx); 384e8d8bef9SDimitry Andric // Leave `undef` operands as is, StackMaps will rewrite them 385e8d8bef9SDimitry Andric // into a constant. 386e8d8bef9SDimitry Andric if (!MO.isReg() || MO.isImplicit() || MO.isUndef()) 3875ffd83dbSDimitry Andric continue; 3885ffd83dbSDimitry Andric Register Reg = MO.getReg(); 3895ffd83dbSDimitry Andric assert(Reg.isPhysical() && "Only physical regs are expected"); 390e8d8bef9SDimitry Andric 39106c3fb27SDimitry Andric if (isCalleeSaved(Reg) && (AllowGCPtrInCSR || !GCRegs.contains(Reg))) 3925ffd83dbSDimitry Andric continue; 393e8d8bef9SDimitry Andric 394e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Will spill " << printReg(Reg, &TRI) << " at index " 395e8d8bef9SDimitry Andric << Idx << "\n"); 396e8d8bef9SDimitry Andric 3975ffd83dbSDimitry Andric if (VisitedRegs.insert(Reg).second) 3985ffd83dbSDimitry Andric RegsToSpill.push_back(Reg); 3995ffd83dbSDimitry Andric OpsToSpill.push_back(Idx); 4005ffd83dbSDimitry Andric } 4015ffd83dbSDimitry Andric CacheFI.sortRegisters(RegsToSpill); 4025ffd83dbSDimitry Andric return !RegsToSpill.empty(); 4035ffd83dbSDimitry Andric } 404e8d8bef9SDimitry Andric 4055ffd83dbSDimitry Andric // Spill all caller saved registers right before statepoint instruction. 4065ffd83dbSDimitry Andric // Remember frame index where register is spilled. 4075ffd83dbSDimitry Andric void spillRegisters() { 4085ffd83dbSDimitry Andric for (Register Reg : RegsToSpill) { 409e8d8bef9SDimitry Andric int FI = CacheFI.getFrameIndex(Reg, EHPad); 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); 42106c3fb27SDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 422e8d8bef9SDimitry Andric 423e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Insert spill before " << *InsertBefore); 424e8d8bef9SDimitry Andric TII.storeRegToStackSlot(*MI.getParent(), InsertBefore, Reg, IsKill, FI, 425bdd1243dSDimitry Andric RC, &TRI, Register()); 4265ffd83dbSDimitry Andric } 4275ffd83dbSDimitry Andric } 428e8d8bef9SDimitry Andric 429e8d8bef9SDimitry Andric void insertReloadBefore(unsigned Reg, MachineBasicBlock::iterator It, 430e8d8bef9SDimitry Andric MachineBasicBlock *MBB) { 431e8d8bef9SDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 432e8d8bef9SDimitry Andric int FI = RegToSlotIdx[Reg]; 433e8d8bef9SDimitry Andric if (It != MBB->end()) { 434bdd1243dSDimitry Andric TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI, Register()); 435e8d8bef9SDimitry Andric return; 436e8d8bef9SDimitry Andric } 437e8d8bef9SDimitry Andric 438e8d8bef9SDimitry Andric // To insert reload at the end of MBB, insert it before last instruction 439e8d8bef9SDimitry Andric // and then swap them. 440e8d8bef9SDimitry Andric assert(!MBB->empty() && "Empty block"); 441e8d8bef9SDimitry Andric --It; 442bdd1243dSDimitry Andric TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI, Register()); 443e8d8bef9SDimitry Andric MachineInstr *Reload = It->getPrevNode(); 444e8d8bef9SDimitry Andric int Dummy = 0; 445e8d8bef9SDimitry Andric (void)Dummy; 446e8d8bef9SDimitry Andric assert(TII.isLoadFromStackSlot(*Reload, Dummy) == Reg); 447e8d8bef9SDimitry Andric assert(Dummy == FI); 448e8d8bef9SDimitry Andric MBB->remove(Reload); 449e8d8bef9SDimitry Andric MBB->insertAfter(It, Reload); 450e8d8bef9SDimitry Andric } 451e8d8bef9SDimitry Andric 452e8d8bef9SDimitry Andric // Insert reloads of (relocated) registers spilled in statepoint. 453e8d8bef9SDimitry Andric void insertReloads(MachineInstr *NewStatepoint, RegReloadCache &RC) { 454e8d8bef9SDimitry Andric MachineBasicBlock *MBB = NewStatepoint->getParent(); 455e8d8bef9SDimitry Andric auto InsertPoint = std::next(NewStatepoint->getIterator()); 456e8d8bef9SDimitry Andric 457e8d8bef9SDimitry Andric for (auto Reg : RegsToReload) { 458e8d8bef9SDimitry Andric insertReloadBefore(Reg, InsertPoint, MBB); 459e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Reloading " << printReg(Reg, &TRI) << " from FI " 460e8d8bef9SDimitry Andric << RegToSlotIdx[Reg] << " after statepoint\n"); 461e8d8bef9SDimitry Andric 462e8d8bef9SDimitry Andric if (EHPad && !RC.hasReload(Reg, RegToSlotIdx[Reg], EHPad)) { 463e8d8bef9SDimitry Andric RC.recordReload(Reg, RegToSlotIdx[Reg], EHPad); 4645f757f3fSDimitry Andric auto EHPadInsertPoint = 4655f757f3fSDimitry Andric EHPad->SkipPHIsLabelsAndDebug(EHPad->begin(), Reg); 466e8d8bef9SDimitry Andric insertReloadBefore(Reg, EHPadInsertPoint, EHPad); 467e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "...also reload at EHPad " 468e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 469e8d8bef9SDimitry Andric } 470e8d8bef9SDimitry Andric } 471e8d8bef9SDimitry Andric } 472e8d8bef9SDimitry Andric 4735ffd83dbSDimitry Andric // Re-write statepoint machine instruction to replace caller saved operands 4745ffd83dbSDimitry Andric // with indirect memory location (frame index). 475e8d8bef9SDimitry Andric MachineInstr *rewriteStatepoint() { 4765ffd83dbSDimitry Andric MachineInstr *NewMI = 4775ffd83dbSDimitry Andric MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true); 4785ffd83dbSDimitry Andric MachineInstrBuilder MIB(MF, NewMI); 4795ffd83dbSDimitry Andric 480e8d8bef9SDimitry Andric unsigned NumOps = MI.getNumOperands(); 481e8d8bef9SDimitry Andric 482e8d8bef9SDimitry Andric // New indices for the remaining defs. 483e8d8bef9SDimitry Andric SmallVector<unsigned, 8> NewIndices; 484e8d8bef9SDimitry Andric unsigned NumDefs = MI.getNumDefs(); 485e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumDefs; ++I) { 486e8d8bef9SDimitry Andric MachineOperand &DefMO = MI.getOperand(I); 487e8d8bef9SDimitry Andric assert(DefMO.isReg() && DefMO.isDef() && "Expected Reg Def operand"); 488e8d8bef9SDimitry Andric Register Reg = DefMO.getReg(); 489fe6060f1SDimitry Andric assert(DefMO.isTied() && "Def is expected to be tied"); 490fe6060f1SDimitry Andric // We skipped undef uses and did not spill them, so we should not 491fe6060f1SDimitry Andric // proceed with defs here. 492fe6060f1SDimitry Andric if (MI.getOperand(MI.findTiedOperandIdx(I)).isUndef()) { 493fe6060f1SDimitry Andric if (AllowGCPtrInCSR) { 494fe6060f1SDimitry Andric NewIndices.push_back(NewMI->getNumOperands()); 495fe6060f1SDimitry Andric MIB.addReg(Reg, RegState::Define); 496fe6060f1SDimitry Andric } 497fe6060f1SDimitry Andric continue; 498fe6060f1SDimitry Andric } 499e8d8bef9SDimitry Andric if (!AllowGCPtrInCSR) { 500e8d8bef9SDimitry Andric assert(is_contained(RegsToSpill, Reg)); 501e8d8bef9SDimitry Andric RegsToReload.push_back(Reg); 502e8d8bef9SDimitry Andric } else { 503e8d8bef9SDimitry Andric if (isCalleeSaved(Reg)) { 504e8d8bef9SDimitry Andric NewIndices.push_back(NewMI->getNumOperands()); 505e8d8bef9SDimitry Andric MIB.addReg(Reg, RegState::Define); 506e8d8bef9SDimitry Andric } else { 507e8d8bef9SDimitry Andric NewIndices.push_back(NumOps); 508e8d8bef9SDimitry Andric RegsToReload.push_back(Reg); 509e8d8bef9SDimitry Andric } 510e8d8bef9SDimitry Andric } 511e8d8bef9SDimitry Andric } 512e8d8bef9SDimitry Andric 5135ffd83dbSDimitry Andric // Add End marker. 5145ffd83dbSDimitry Andric OpsToSpill.push_back(MI.getNumOperands()); 5155ffd83dbSDimitry Andric unsigned CurOpIdx = 0; 5165ffd83dbSDimitry Andric 517e8d8bef9SDimitry Andric for (unsigned I = NumDefs; I < MI.getNumOperands(); ++I) { 5185ffd83dbSDimitry Andric MachineOperand &MO = MI.getOperand(I); 5195ffd83dbSDimitry Andric if (I == OpsToSpill[CurOpIdx]) { 5205ffd83dbSDimitry Andric int FI = RegToSlotIdx[MO.getReg()]; 5215ffd83dbSDimitry Andric MIB.addImm(StackMaps::IndirectMemRefOp); 5225ffd83dbSDimitry Andric MIB.addImm(getRegisterSize(TRI, MO.getReg())); 5235ffd83dbSDimitry Andric assert(MO.isReg() && "Should be register"); 5245ffd83dbSDimitry Andric assert(MO.getReg().isPhysical() && "Should be physical register"); 5255ffd83dbSDimitry Andric MIB.addFrameIndex(FI); 5265ffd83dbSDimitry Andric MIB.addImm(0); 5275ffd83dbSDimitry Andric ++CurOpIdx; 528e8d8bef9SDimitry Andric } else { 5295ffd83dbSDimitry Andric MIB.add(MO); 530e8d8bef9SDimitry Andric unsigned OldDef; 531e8d8bef9SDimitry Andric if (AllowGCPtrInCSR && MI.isRegTiedToDefOperand(I, &OldDef)) { 532e8d8bef9SDimitry Andric assert(OldDef < NumDefs); 533e8d8bef9SDimitry Andric assert(NewIndices[OldDef] < NumOps); 534e8d8bef9SDimitry Andric MIB->tieOperands(NewIndices[OldDef], MIB->getNumOperands() - 1); 535e8d8bef9SDimitry Andric } 536e8d8bef9SDimitry Andric } 5375ffd83dbSDimitry Andric } 5385ffd83dbSDimitry Andric assert(CurOpIdx == (OpsToSpill.size() - 1) && "Not all operands processed"); 5395ffd83dbSDimitry Andric // Add mem operands. 5405ffd83dbSDimitry Andric NewMI->setMemRefs(MF, MI.memoperands()); 5415ffd83dbSDimitry Andric for (auto It : RegToSlotIdx) { 542e8d8bef9SDimitry Andric Register R = It.first; 5435ffd83dbSDimitry Andric int FrameIndex = It.second; 5445ffd83dbSDimitry Andric auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex); 545e8d8bef9SDimitry Andric MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad; 546e8d8bef9SDimitry Andric if (is_contained(RegsToReload, R)) 547e8d8bef9SDimitry Andric Flags |= MachineMemOperand::MOStore; 548e8d8bef9SDimitry Andric auto *MMO = 549e8d8bef9SDimitry Andric MF.getMachineMemOperand(PtrInfo, Flags, getRegisterSize(TRI, R), 5505ffd83dbSDimitry Andric MFI.getObjectAlign(FrameIndex)); 5515ffd83dbSDimitry Andric NewMI->addMemOperand(MF, MMO); 5525ffd83dbSDimitry Andric } 553e8d8bef9SDimitry Andric 5545ffd83dbSDimitry Andric // Insert new statepoint and erase old one. 5555ffd83dbSDimitry Andric MI.getParent()->insert(MI, NewMI); 556e8d8bef9SDimitry Andric 557e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "rewritten statepoint to : " << *NewMI << "\n"); 5585ffd83dbSDimitry Andric MI.eraseFromParent(); 559e8d8bef9SDimitry Andric return NewMI; 5605ffd83dbSDimitry Andric } 5615ffd83dbSDimitry Andric }; 5625ffd83dbSDimitry Andric 5635ffd83dbSDimitry Andric class StatepointProcessor { 5645ffd83dbSDimitry Andric private: 5655ffd83dbSDimitry Andric MachineFunction &MF; 5665ffd83dbSDimitry Andric const TargetRegisterInfo &TRI; 5675ffd83dbSDimitry Andric FrameIndexesCache CacheFI; 568e8d8bef9SDimitry Andric RegReloadCache ReloadCache; 5695ffd83dbSDimitry Andric 5705ffd83dbSDimitry Andric public: 5715ffd83dbSDimitry Andric StatepointProcessor(MachineFunction &MF) 5725ffd83dbSDimitry Andric : MF(MF), TRI(*MF.getSubtarget().getRegisterInfo()), 5735ffd83dbSDimitry Andric CacheFI(MF.getFrameInfo(), TRI) {} 5745ffd83dbSDimitry Andric 575e8d8bef9SDimitry Andric bool process(MachineInstr &MI, bool AllowGCPtrInCSR) { 5765ffd83dbSDimitry Andric StatepointOpers SO(&MI); 5775ffd83dbSDimitry Andric uint64_t Flags = SO.getFlags(); 5785ffd83dbSDimitry Andric // Do nothing for LiveIn, it supports all registers. 5795ffd83dbSDimitry Andric if (Flags & (uint64_t)StatepointFlags::DeoptLiveIn) 5805ffd83dbSDimitry Andric return false; 581e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "\nMBB " << MI.getParent()->getNumber() << " " 582e8d8bef9SDimitry Andric << MI.getParent()->getName() << " : process statepoint " 583e8d8bef9SDimitry Andric << MI); 5845ffd83dbSDimitry Andric CallingConv::ID CC = SO.getCallingConv(); 5855ffd83dbSDimitry Andric const uint32_t *Mask = TRI.getCallPreservedMask(MF, CC); 586e8d8bef9SDimitry Andric StatepointState SS(MI, Mask, CacheFI, AllowGCPtrInCSR); 587e8d8bef9SDimitry Andric CacheFI.reset(SS.getEHPad()); 5885ffd83dbSDimitry Andric 5895ffd83dbSDimitry Andric if (!SS.findRegistersToSpill()) 5905ffd83dbSDimitry Andric return false; 5915ffd83dbSDimitry Andric 5925ffd83dbSDimitry Andric SS.spillRegisters(); 593e8d8bef9SDimitry Andric auto *NewStatepoint = SS.rewriteStatepoint(); 594e8d8bef9SDimitry Andric SS.insertReloads(NewStatepoint, ReloadCache); 5955ffd83dbSDimitry Andric return true; 5965ffd83dbSDimitry Andric } 5975ffd83dbSDimitry Andric }; 5985ffd83dbSDimitry Andric } // namespace 5995ffd83dbSDimitry Andric 6005ffd83dbSDimitry Andric bool FixupStatepointCallerSaved::runOnMachineFunction(MachineFunction &MF) { 6015ffd83dbSDimitry Andric if (skipFunction(MF.getFunction())) 6025ffd83dbSDimitry Andric return false; 6035ffd83dbSDimitry Andric 6045ffd83dbSDimitry Andric const Function &F = MF.getFunction(); 6055ffd83dbSDimitry Andric if (!F.hasGC()) 6065ffd83dbSDimitry Andric return false; 6075ffd83dbSDimitry Andric 6085ffd83dbSDimitry Andric SmallVector<MachineInstr *, 16> Statepoints; 6095ffd83dbSDimitry Andric for (MachineBasicBlock &BB : MF) 6105ffd83dbSDimitry Andric for (MachineInstr &I : BB) 6115ffd83dbSDimitry Andric if (I.getOpcode() == TargetOpcode::STATEPOINT) 6125ffd83dbSDimitry Andric Statepoints.push_back(&I); 6135ffd83dbSDimitry Andric 6145ffd83dbSDimitry Andric if (Statepoints.empty()) 6155ffd83dbSDimitry Andric return false; 6165ffd83dbSDimitry Andric 6175ffd83dbSDimitry Andric bool Changed = false; 6185ffd83dbSDimitry Andric StatepointProcessor SPP(MF); 619e8d8bef9SDimitry Andric unsigned NumStatepoints = 0; 620e8d8bef9SDimitry Andric bool AllowGCPtrInCSR = PassGCPtrInCSR; 621e8d8bef9SDimitry Andric for (MachineInstr *I : Statepoints) { 622e8d8bef9SDimitry Andric ++NumStatepoints; 623e8d8bef9SDimitry Andric if (MaxStatepointsWithRegs.getNumOccurrences() && 624e8d8bef9SDimitry Andric NumStatepoints >= MaxStatepointsWithRegs) 625e8d8bef9SDimitry Andric AllowGCPtrInCSR = false; 626e8d8bef9SDimitry Andric Changed |= SPP.process(*I, AllowGCPtrInCSR); 627e8d8bef9SDimitry Andric } 6285ffd83dbSDimitry Andric return Changed; 6295ffd83dbSDimitry Andric } 630