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 49*e8d8bef9SDimitry Andric static cl::opt<bool> PassGCPtrInCSR( 50*e8d8bef9SDimitry Andric "fixup-allow-gcptr-in-csr", cl::Hidden, cl::init(false), 51*e8d8bef9SDimitry Andric cl::desc("Allow passing GC Pointer arguments in callee saved registers")); 52*e8d8bef9SDimitry Andric 53*e8d8bef9SDimitry Andric static cl::opt<bool> EnableCopyProp( 54*e8d8bef9SDimitry Andric "fixup-scs-enable-copy-propagation", cl::Hidden, cl::init(true), 55*e8d8bef9SDimitry Andric cl::desc("Enable simple copy propagation during register reloading")); 56*e8d8bef9SDimitry Andric 57*e8d8bef9SDimitry Andric // This is purely debugging option. 58*e8d8bef9SDimitry Andric // It may be handy for investigating statepoint spilling issues. 59*e8d8bef9SDimitry Andric static cl::opt<unsigned> MaxStatepointsWithRegs( 60*e8d8bef9SDimitry Andric "fixup-max-csr-statepoints", cl::Hidden, 61*e8d8bef9SDimitry Andric cl::desc("Max number of statepoints allowed to pass GC Ptrs in registers")); 62*e8d8bef9SDimitry 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 }; 84*e8d8bef9SDimitry 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 101*e8d8bef9SDimitry Andric // Try to eliminate redundant copy to register which we're going to 102*e8d8bef9SDimitry Andric // spill, i.e. try to change: 103*e8d8bef9SDimitry Andric // X = COPY Y 104*e8d8bef9SDimitry Andric // SPILL X 105*e8d8bef9SDimitry Andric // to 106*e8d8bef9SDimitry Andric // SPILL Y 107*e8d8bef9SDimitry Andric // If there are no uses of X between copy and STATEPOINT, that COPY 108*e8d8bef9SDimitry Andric // may be eliminated. 109*e8d8bef9SDimitry Andric // Reg - register we're about to spill 110*e8d8bef9SDimitry Andric // RI - On entry points to statepoint. 111*e8d8bef9SDimitry Andric // On successful copy propagation set to new spill point. 112*e8d8bef9SDimitry Andric // IsKill - set to true if COPY is Kill (there are no uses of Y) 113*e8d8bef9SDimitry Andric // Returns either found source copy register or original one. 114*e8d8bef9SDimitry Andric static Register performCopyPropagation(Register Reg, 115*e8d8bef9SDimitry Andric MachineBasicBlock::iterator &RI, 116*e8d8bef9SDimitry Andric bool &IsKill, const TargetInstrInfo &TII, 117*e8d8bef9SDimitry Andric const TargetRegisterInfo &TRI) { 118*e8d8bef9SDimitry Andric // First check if statepoint itself uses Reg in non-meta operands. 119*e8d8bef9SDimitry Andric int Idx = RI->findRegisterUseOperandIdx(Reg, false, &TRI); 120*e8d8bef9SDimitry Andric if (Idx >= 0 && (unsigned)Idx < StatepointOpers(&*RI).getNumDeoptArgsIdx()) { 121*e8d8bef9SDimitry Andric IsKill = false; 122*e8d8bef9SDimitry Andric return Reg; 123*e8d8bef9SDimitry Andric } 124*e8d8bef9SDimitry Andric 125*e8d8bef9SDimitry Andric if (!EnableCopyProp) 126*e8d8bef9SDimitry Andric return Reg; 127*e8d8bef9SDimitry Andric 128*e8d8bef9SDimitry Andric MachineBasicBlock *MBB = RI->getParent(); 129*e8d8bef9SDimitry Andric MachineBasicBlock::reverse_iterator E = MBB->rend(); 130*e8d8bef9SDimitry Andric MachineInstr *Def = nullptr, *Use = nullptr; 131*e8d8bef9SDimitry Andric for (auto It = ++(RI.getReverse()); It != E; ++It) { 132*e8d8bef9SDimitry Andric if (It->readsRegister(Reg, &TRI) && !Use) 133*e8d8bef9SDimitry Andric Use = &*It; 134*e8d8bef9SDimitry Andric if (It->modifiesRegister(Reg, &TRI)) { 135*e8d8bef9SDimitry Andric Def = &*It; 136*e8d8bef9SDimitry Andric break; 137*e8d8bef9SDimitry Andric } 138*e8d8bef9SDimitry Andric } 139*e8d8bef9SDimitry Andric 140*e8d8bef9SDimitry Andric if (!Def) 141*e8d8bef9SDimitry Andric return Reg; 142*e8d8bef9SDimitry Andric 143*e8d8bef9SDimitry Andric auto DestSrc = TII.isCopyInstr(*Def); 144*e8d8bef9SDimitry Andric if (!DestSrc || DestSrc->Destination->getReg() != Reg) 145*e8d8bef9SDimitry Andric return Reg; 146*e8d8bef9SDimitry Andric 147*e8d8bef9SDimitry Andric Register SrcReg = DestSrc->Source->getReg(); 148*e8d8bef9SDimitry Andric 149*e8d8bef9SDimitry Andric if (getRegisterSize(TRI, Reg) != getRegisterSize(TRI, SrcReg)) 150*e8d8bef9SDimitry Andric return Reg; 151*e8d8bef9SDimitry Andric 152*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "spillRegisters: perform copy propagation " 153*e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " -> " << printReg(SrcReg, &TRI) 154*e8d8bef9SDimitry Andric << "\n"); 155*e8d8bef9SDimitry Andric 156*e8d8bef9SDimitry Andric // Insert spill immediately after Def 157*e8d8bef9SDimitry Andric RI = ++MachineBasicBlock::iterator(Def); 158*e8d8bef9SDimitry Andric IsKill = DestSrc->Source->isKill(); 159*e8d8bef9SDimitry Andric 160*e8d8bef9SDimitry Andric // There are no uses of original register between COPY and STATEPOINT. 161*e8d8bef9SDimitry Andric // There can't be any after STATEPOINT, so we can eliminate Def. 162*e8d8bef9SDimitry Andric if (!Use) { 163*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "spillRegisters: removing dead copy " << *Def); 164*e8d8bef9SDimitry Andric Def->eraseFromParent(); 165*e8d8bef9SDimitry Andric } 166*e8d8bef9SDimitry Andric return SrcReg; 167*e8d8bef9SDimitry Andric } 168*e8d8bef9SDimitry Andric 1695ffd83dbSDimitry Andric namespace { 170*e8d8bef9SDimitry Andric // Pair {Register, FrameIndex} 171*e8d8bef9SDimitry Andric using RegSlotPair = std::pair<Register, int>; 172*e8d8bef9SDimitry Andric 173*e8d8bef9SDimitry Andric // Keeps track of what reloads were inserted in MBB. 174*e8d8bef9SDimitry Andric class RegReloadCache { 175*e8d8bef9SDimitry Andric using ReloadSet = SmallSet<RegSlotPair, 8>; 176*e8d8bef9SDimitry Andric DenseMap<const MachineBasicBlock *, ReloadSet> Reloads; 177*e8d8bef9SDimitry Andric 178*e8d8bef9SDimitry Andric public: 179*e8d8bef9SDimitry Andric RegReloadCache() = default; 180*e8d8bef9SDimitry Andric 181*e8d8bef9SDimitry Andric // Record reload of Reg from FI in block MBB 182*e8d8bef9SDimitry Andric void recordReload(Register Reg, int FI, const MachineBasicBlock *MBB) { 183*e8d8bef9SDimitry Andric RegSlotPair RSP(Reg, FI); 184*e8d8bef9SDimitry Andric auto Res = Reloads[MBB].insert(RSP); 185*e8d8bef9SDimitry Andric (void)Res; 186*e8d8bef9SDimitry Andric assert(Res.second && "reload already exists"); 187*e8d8bef9SDimitry Andric } 188*e8d8bef9SDimitry Andric 189*e8d8bef9SDimitry Andric // Does basic block MBB contains reload of Reg from FI? 190*e8d8bef9SDimitry Andric bool hasReload(Register Reg, int FI, const MachineBasicBlock *MBB) { 191*e8d8bef9SDimitry Andric RegSlotPair RSP(Reg, FI); 192*e8d8bef9SDimitry Andric return Reloads.count(MBB) && Reloads[MBB].count(RSP); 193*e8d8bef9SDimitry Andric } 194*e8d8bef9SDimitry Andric }; 195*e8d8bef9SDimitry 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 217*e8d8bef9SDimitry Andric // Keeps track of slots reserved for the shared landing pad processing. 218*e8d8bef9SDimitry Andric // Initialized from GlobalIndices for the current EHPad. 219*e8d8bef9SDimitry Andric SmallSet<int, 8> ReservedSlots; 220*e8d8bef9SDimitry Andric 221*e8d8bef9SDimitry Andric // Landing pad can be destination of several statepoints. Every register 222*e8d8bef9SDimitry Andric // defined by such statepoints must be spilled to the same stack slot. 223*e8d8bef9SDimitry Andric // This map keeps that information. 224*e8d8bef9SDimitry Andric DenseMap<const MachineBasicBlock *, SmallVector<RegSlotPair, 8>> 225*e8d8bef9SDimitry Andric GlobalIndices; 226*e8d8bef9SDimitry Andric 227*e8d8bef9SDimitry Andric FrameIndexesPerSize &getCacheBucket(unsigned Size) { 228*e8d8bef9SDimitry Andric // In FixupSCSExtendSlotSize mode the bucket with 0 index is used 229*e8d8bef9SDimitry Andric // for all sizes. 230*e8d8bef9SDimitry Andric return Cache[FixupSCSExtendSlotSize ? 0 : Size]; 231*e8d8bef9SDimitry Andric } 232*e8d8bef9SDimitry 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 237*e8d8bef9SDimitry Andric // this function all frame indexes are available for allocation with 238*e8d8bef9SDimitry Andric // the exception of slots reserved for landing pad processing (if any). 239*e8d8bef9SDimitry Andric void reset(const MachineBasicBlock *EHPad) { 2405ffd83dbSDimitry Andric for (auto &It : Cache) 2415ffd83dbSDimitry Andric It.second.Index = 0; 242*e8d8bef9SDimitry Andric 243*e8d8bef9SDimitry Andric ReservedSlots.clear(); 244*e8d8bef9SDimitry Andric if (EHPad && GlobalIndices.count(EHPad)) 245*e8d8bef9SDimitry Andric for (auto &RSP : GlobalIndices[EHPad]) 246*e8d8bef9SDimitry Andric ReservedSlots.insert(RSP.second); 2475ffd83dbSDimitry Andric } 248*e8d8bef9SDimitry Andric 2495ffd83dbSDimitry Andric // Get frame index to spill the register. 250*e8d8bef9SDimitry Andric int getFrameIndex(Register Reg, MachineBasicBlock *EHPad) { 251*e8d8bef9SDimitry Andric // Check if slot for Reg is already reserved at EHPad. 252*e8d8bef9SDimitry Andric auto It = GlobalIndices.find(EHPad); 253*e8d8bef9SDimitry Andric if (It != GlobalIndices.end()) { 254*e8d8bef9SDimitry Andric auto &Vec = It->second; 255*e8d8bef9SDimitry Andric auto Idx = llvm::find_if( 256*e8d8bef9SDimitry Andric Vec, [Reg](RegSlotPair &RSP) { return Reg == RSP.first; }); 257*e8d8bef9SDimitry Andric if (Idx != Vec.end()) { 258*e8d8bef9SDimitry Andric int FI = Idx->second; 259*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Found global FI " << FI << " for register " 260*e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " at " 261*e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 262*e8d8bef9SDimitry Andric assert(ReservedSlots.count(FI) && "using unreserved slot"); 263*e8d8bef9SDimitry Andric return FI; 264*e8d8bef9SDimitry Andric } 265*e8d8bef9SDimitry Andric } 266*e8d8bef9SDimitry Andric 2675ffd83dbSDimitry Andric unsigned Size = getRegisterSize(TRI, Reg); 268*e8d8bef9SDimitry Andric FrameIndexesPerSize &Line = getCacheBucket(Size); 269*e8d8bef9SDimitry Andric while (Line.Index < Line.Slots.size()) { 2705ffd83dbSDimitry Andric int FI = Line.Slots[Line.Index++]; 271*e8d8bef9SDimitry Andric if (ReservedSlots.count(FI)) 272*e8d8bef9SDimitry 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; 286*e8d8bef9SDimitry Andric 287*e8d8bef9SDimitry Andric // Remember assignment {Reg, FI} for EHPad 288*e8d8bef9SDimitry Andric if (EHPad) { 289*e8d8bef9SDimitry Andric GlobalIndices[EHPad].push_back(std::make_pair(Reg, FI)); 290*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Reserved FI " << FI << " for spilling reg " 291*e8d8bef9SDimitry Andric << printReg(Reg, &TRI) << " at landing pad " 292*e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 293*e8d8bef9SDimitry Andric } 294*e8d8bef9SDimitry Andric 2955ffd83dbSDimitry Andric return FI; 2965ffd83dbSDimitry Andric } 297*e8d8bef9SDimitry 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; 304*e8d8bef9SDimitry 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; 316*e8d8bef9SDimitry Andric // If non-null then statepoint is invoke, and this points to the landing pad. 317*e8d8bef9SDimitry 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; 325*e8d8bef9SDimitry 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; 330*e8d8bef9SDimitry Andric // Set of registers to reload after statepoint. 331*e8d8bef9SDimitry 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, 337*e8d8bef9SDimitry 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()), 340*e8d8bef9SDimitry Andric Mask(Mask), CacheFI(CacheFI), AllowGCPtrInCSR(AllowGCPtrInCSR) { 341*e8d8bef9SDimitry Andric 342*e8d8bef9SDimitry Andric // Find statepoint's landing pad, if any. 343*e8d8bef9SDimitry Andric EHPad = nullptr; 344*e8d8bef9SDimitry Andric MachineBasicBlock *MBB = MI.getParent(); 345*e8d8bef9SDimitry Andric // Invoke statepoint must be last one in block. 346*e8d8bef9SDimitry Andric bool Last = std::none_of(++MI.getIterator(), MBB->end().getInstrIterator(), 347*e8d8bef9SDimitry Andric [](MachineInstr &I) { 348*e8d8bef9SDimitry Andric return I.getOpcode() == TargetOpcode::STATEPOINT; 349*e8d8bef9SDimitry Andric }); 350*e8d8bef9SDimitry Andric 351*e8d8bef9SDimitry Andric if (!Last) 352*e8d8bef9SDimitry Andric return; 353*e8d8bef9SDimitry Andric 354*e8d8bef9SDimitry Andric auto IsEHPad = [](MachineBasicBlock *B) { return B->isEHPad(); }; 355*e8d8bef9SDimitry Andric 356*e8d8bef9SDimitry Andric assert(llvm::count_if(MBB->successors(), IsEHPad) < 2 && "multiple EHPads"); 357*e8d8bef9SDimitry Andric 358*e8d8bef9SDimitry Andric auto It = llvm::find_if(MBB->successors(), IsEHPad); 359*e8d8bef9SDimitry Andric if (It != MBB->succ_end()) 360*e8d8bef9SDimitry Andric EHPad = *It; 361*e8d8bef9SDimitry Andric } 362*e8d8bef9SDimitry Andric 363*e8d8bef9SDimitry Andric MachineBasicBlock *getEHPad() const { return EHPad; } 364*e8d8bef9SDimitry Andric 3655ffd83dbSDimitry Andric // Return true if register is callee saved. 3665ffd83dbSDimitry Andric bool isCalleeSaved(Register Reg) { return (Mask[Reg / 32] >> Reg % 32) & 1; } 367*e8d8bef9SDimitry 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() { 372*e8d8bef9SDimitry Andric SmallSet<Register, 8> GCRegs; 373*e8d8bef9SDimitry Andric // All GC pointer operands assigned to registers produce new value. 374*e8d8bef9SDimitry Andric // Since they're tied to their defs, it is enough to collect def registers. 375*e8d8bef9SDimitry Andric for (const auto &Def : MI.defs()) 376*e8d8bef9SDimitry Andric GCRegs.insert(Def.getReg()); 377*e8d8bef9SDimitry 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); 383*e8d8bef9SDimitry Andric // Leave `undef` operands as is, StackMaps will rewrite them 384*e8d8bef9SDimitry Andric // into a constant. 385*e8d8bef9SDimitry 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"); 389*e8d8bef9SDimitry Andric 390*e8d8bef9SDimitry Andric if (isCalleeSaved(Reg) && (AllowGCPtrInCSR || !is_contained(GCRegs, Reg))) 3915ffd83dbSDimitry Andric continue; 392*e8d8bef9SDimitry Andric 393*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Will spill " << printReg(Reg, &TRI) << " at index " 394*e8d8bef9SDimitry Andric << Idx << "\n"); 395*e8d8bef9SDimitry 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 } 403*e8d8bef9SDimitry 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) { 408*e8d8bef9SDimitry Andric int FI = CacheFI.getFrameIndex(Reg, EHPad); 4095ffd83dbSDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 410*e8d8bef9SDimitry Andric 4115ffd83dbSDimitry Andric NumSpilledRegisters++; 4125ffd83dbSDimitry Andric RegToSlotIdx[Reg] = FI; 413*e8d8bef9SDimitry Andric 414*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, &TRI) << " to FI " << FI 415*e8d8bef9SDimitry Andric << "\n"); 416*e8d8bef9SDimitry Andric 417*e8d8bef9SDimitry Andric // Perform trivial copy propagation 418*e8d8bef9SDimitry Andric bool IsKill = true; 419*e8d8bef9SDimitry Andric MachineBasicBlock::iterator InsertBefore(MI); 420*e8d8bef9SDimitry Andric Reg = performCopyPropagation(Reg, InsertBefore, IsKill, TII, TRI); 421*e8d8bef9SDimitry Andric 422*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Insert spill before " << *InsertBefore); 423*e8d8bef9SDimitry Andric TII.storeRegToStackSlot(*MI.getParent(), InsertBefore, Reg, IsKill, FI, 424*e8d8bef9SDimitry Andric RC, &TRI); 4255ffd83dbSDimitry Andric } 4265ffd83dbSDimitry Andric } 427*e8d8bef9SDimitry Andric 428*e8d8bef9SDimitry Andric void insertReloadBefore(unsigned Reg, MachineBasicBlock::iterator It, 429*e8d8bef9SDimitry Andric MachineBasicBlock *MBB) { 430*e8d8bef9SDimitry Andric const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 431*e8d8bef9SDimitry Andric int FI = RegToSlotIdx[Reg]; 432*e8d8bef9SDimitry Andric if (It != MBB->end()) { 433*e8d8bef9SDimitry Andric TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI); 434*e8d8bef9SDimitry Andric return; 435*e8d8bef9SDimitry Andric } 436*e8d8bef9SDimitry Andric 437*e8d8bef9SDimitry Andric // To insert reload at the end of MBB, insert it before last instruction 438*e8d8bef9SDimitry Andric // and then swap them. 439*e8d8bef9SDimitry Andric assert(!MBB->empty() && "Empty block"); 440*e8d8bef9SDimitry Andric --It; 441*e8d8bef9SDimitry Andric TII.loadRegFromStackSlot(*MBB, It, Reg, FI, RC, &TRI); 442*e8d8bef9SDimitry Andric MachineInstr *Reload = It->getPrevNode(); 443*e8d8bef9SDimitry Andric int Dummy = 0; 444*e8d8bef9SDimitry Andric (void)Dummy; 445*e8d8bef9SDimitry Andric assert(TII.isLoadFromStackSlot(*Reload, Dummy) == Reg); 446*e8d8bef9SDimitry Andric assert(Dummy == FI); 447*e8d8bef9SDimitry Andric MBB->remove(Reload); 448*e8d8bef9SDimitry Andric MBB->insertAfter(It, Reload); 449*e8d8bef9SDimitry Andric } 450*e8d8bef9SDimitry Andric 451*e8d8bef9SDimitry Andric // Insert reloads of (relocated) registers spilled in statepoint. 452*e8d8bef9SDimitry Andric void insertReloads(MachineInstr *NewStatepoint, RegReloadCache &RC) { 453*e8d8bef9SDimitry Andric MachineBasicBlock *MBB = NewStatepoint->getParent(); 454*e8d8bef9SDimitry Andric auto InsertPoint = std::next(NewStatepoint->getIterator()); 455*e8d8bef9SDimitry Andric 456*e8d8bef9SDimitry Andric for (auto Reg : RegsToReload) { 457*e8d8bef9SDimitry Andric insertReloadBefore(Reg, InsertPoint, MBB); 458*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Reloading " << printReg(Reg, &TRI) << " from FI " 459*e8d8bef9SDimitry Andric << RegToSlotIdx[Reg] << " after statepoint\n"); 460*e8d8bef9SDimitry Andric 461*e8d8bef9SDimitry Andric if (EHPad && !RC.hasReload(Reg, RegToSlotIdx[Reg], EHPad)) { 462*e8d8bef9SDimitry Andric RC.recordReload(Reg, RegToSlotIdx[Reg], EHPad); 463*e8d8bef9SDimitry Andric auto EHPadInsertPoint = EHPad->SkipPHIsLabelsAndDebug(EHPad->begin()); 464*e8d8bef9SDimitry Andric insertReloadBefore(Reg, EHPadInsertPoint, EHPad); 465*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "...also reload at EHPad " 466*e8d8bef9SDimitry Andric << printMBBReference(*EHPad) << "\n"); 467*e8d8bef9SDimitry Andric } 468*e8d8bef9SDimitry Andric } 469*e8d8bef9SDimitry Andric } 470*e8d8bef9SDimitry Andric 4715ffd83dbSDimitry Andric // Re-write statepoint machine instruction to replace caller saved operands 4725ffd83dbSDimitry Andric // with indirect memory location (frame index). 473*e8d8bef9SDimitry 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 478*e8d8bef9SDimitry Andric unsigned NumOps = MI.getNumOperands(); 479*e8d8bef9SDimitry Andric 480*e8d8bef9SDimitry Andric // New indices for the remaining defs. 481*e8d8bef9SDimitry Andric SmallVector<unsigned, 8> NewIndices; 482*e8d8bef9SDimitry Andric unsigned NumDefs = MI.getNumDefs(); 483*e8d8bef9SDimitry Andric for (unsigned I = 0; I < NumDefs; ++I) { 484*e8d8bef9SDimitry Andric MachineOperand &DefMO = MI.getOperand(I); 485*e8d8bef9SDimitry Andric assert(DefMO.isReg() && DefMO.isDef() && "Expected Reg Def operand"); 486*e8d8bef9SDimitry Andric Register Reg = DefMO.getReg(); 487*e8d8bef9SDimitry Andric if (!AllowGCPtrInCSR) { 488*e8d8bef9SDimitry Andric assert(is_contained(RegsToSpill, Reg)); 489*e8d8bef9SDimitry Andric RegsToReload.push_back(Reg); 490*e8d8bef9SDimitry Andric } else { 491*e8d8bef9SDimitry Andric if (isCalleeSaved(Reg)) { 492*e8d8bef9SDimitry Andric NewIndices.push_back(NewMI->getNumOperands()); 493*e8d8bef9SDimitry Andric MIB.addReg(Reg, RegState::Define); 494*e8d8bef9SDimitry Andric } else { 495*e8d8bef9SDimitry Andric NewIndices.push_back(NumOps); 496*e8d8bef9SDimitry Andric RegsToReload.push_back(Reg); 497*e8d8bef9SDimitry Andric } 498*e8d8bef9SDimitry Andric } 499*e8d8bef9SDimitry Andric } 500*e8d8bef9SDimitry Andric 5015ffd83dbSDimitry Andric // Add End marker. 5025ffd83dbSDimitry Andric OpsToSpill.push_back(MI.getNumOperands()); 5035ffd83dbSDimitry Andric unsigned CurOpIdx = 0; 5045ffd83dbSDimitry Andric 505*e8d8bef9SDimitry Andric for (unsigned I = NumDefs; I < MI.getNumOperands(); ++I) { 5065ffd83dbSDimitry Andric MachineOperand &MO = MI.getOperand(I); 5075ffd83dbSDimitry Andric if (I == OpsToSpill[CurOpIdx]) { 5085ffd83dbSDimitry Andric int FI = RegToSlotIdx[MO.getReg()]; 5095ffd83dbSDimitry Andric MIB.addImm(StackMaps::IndirectMemRefOp); 5105ffd83dbSDimitry Andric MIB.addImm(getRegisterSize(TRI, MO.getReg())); 5115ffd83dbSDimitry Andric assert(MO.isReg() && "Should be register"); 5125ffd83dbSDimitry Andric assert(MO.getReg().isPhysical() && "Should be physical register"); 5135ffd83dbSDimitry Andric MIB.addFrameIndex(FI); 5145ffd83dbSDimitry Andric MIB.addImm(0); 5155ffd83dbSDimitry Andric ++CurOpIdx; 516*e8d8bef9SDimitry Andric } else { 5175ffd83dbSDimitry Andric MIB.add(MO); 518*e8d8bef9SDimitry Andric unsigned OldDef; 519*e8d8bef9SDimitry Andric if (AllowGCPtrInCSR && MI.isRegTiedToDefOperand(I, &OldDef)) { 520*e8d8bef9SDimitry Andric assert(OldDef < NumDefs); 521*e8d8bef9SDimitry Andric assert(NewIndices[OldDef] < NumOps); 522*e8d8bef9SDimitry Andric MIB->tieOperands(NewIndices[OldDef], MIB->getNumOperands() - 1); 523*e8d8bef9SDimitry Andric } 524*e8d8bef9SDimitry Andric } 5255ffd83dbSDimitry Andric } 5265ffd83dbSDimitry Andric assert(CurOpIdx == (OpsToSpill.size() - 1) && "Not all operands processed"); 5275ffd83dbSDimitry Andric // Add mem operands. 5285ffd83dbSDimitry Andric NewMI->setMemRefs(MF, MI.memoperands()); 5295ffd83dbSDimitry Andric for (auto It : RegToSlotIdx) { 530*e8d8bef9SDimitry Andric Register R = It.first; 5315ffd83dbSDimitry Andric int FrameIndex = It.second; 5325ffd83dbSDimitry Andric auto PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIndex); 533*e8d8bef9SDimitry Andric MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad; 534*e8d8bef9SDimitry Andric if (is_contained(RegsToReload, R)) 535*e8d8bef9SDimitry Andric Flags |= MachineMemOperand::MOStore; 536*e8d8bef9SDimitry Andric auto *MMO = 537*e8d8bef9SDimitry Andric MF.getMachineMemOperand(PtrInfo, Flags, getRegisterSize(TRI, R), 5385ffd83dbSDimitry Andric MFI.getObjectAlign(FrameIndex)); 5395ffd83dbSDimitry Andric NewMI->addMemOperand(MF, MMO); 5405ffd83dbSDimitry Andric } 541*e8d8bef9SDimitry Andric 5425ffd83dbSDimitry Andric // Insert new statepoint and erase old one. 5435ffd83dbSDimitry Andric MI.getParent()->insert(MI, NewMI); 544*e8d8bef9SDimitry Andric 545*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "rewritten statepoint to : " << *NewMI << "\n"); 5465ffd83dbSDimitry Andric MI.eraseFromParent(); 547*e8d8bef9SDimitry Andric return NewMI; 5485ffd83dbSDimitry Andric } 5495ffd83dbSDimitry Andric }; 5505ffd83dbSDimitry Andric 5515ffd83dbSDimitry Andric class StatepointProcessor { 5525ffd83dbSDimitry Andric private: 5535ffd83dbSDimitry Andric MachineFunction &MF; 5545ffd83dbSDimitry Andric const TargetRegisterInfo &TRI; 5555ffd83dbSDimitry Andric FrameIndexesCache CacheFI; 556*e8d8bef9SDimitry Andric RegReloadCache ReloadCache; 5575ffd83dbSDimitry Andric 5585ffd83dbSDimitry Andric public: 5595ffd83dbSDimitry Andric StatepointProcessor(MachineFunction &MF) 5605ffd83dbSDimitry Andric : MF(MF), TRI(*MF.getSubtarget().getRegisterInfo()), 5615ffd83dbSDimitry Andric CacheFI(MF.getFrameInfo(), TRI) {} 5625ffd83dbSDimitry Andric 563*e8d8bef9SDimitry Andric bool process(MachineInstr &MI, bool AllowGCPtrInCSR) { 5645ffd83dbSDimitry Andric StatepointOpers SO(&MI); 5655ffd83dbSDimitry Andric uint64_t Flags = SO.getFlags(); 5665ffd83dbSDimitry Andric // Do nothing for LiveIn, it supports all registers. 5675ffd83dbSDimitry Andric if (Flags & (uint64_t)StatepointFlags::DeoptLiveIn) 5685ffd83dbSDimitry Andric return false; 569*e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "\nMBB " << MI.getParent()->getNumber() << " " 570*e8d8bef9SDimitry Andric << MI.getParent()->getName() << " : process statepoint " 571*e8d8bef9SDimitry Andric << MI); 5725ffd83dbSDimitry Andric CallingConv::ID CC = SO.getCallingConv(); 5735ffd83dbSDimitry Andric const uint32_t *Mask = TRI.getCallPreservedMask(MF, CC); 574*e8d8bef9SDimitry Andric StatepointState SS(MI, Mask, CacheFI, AllowGCPtrInCSR); 575*e8d8bef9SDimitry Andric CacheFI.reset(SS.getEHPad()); 5765ffd83dbSDimitry Andric 5775ffd83dbSDimitry Andric if (!SS.findRegistersToSpill()) 5785ffd83dbSDimitry Andric return false; 5795ffd83dbSDimitry Andric 5805ffd83dbSDimitry Andric SS.spillRegisters(); 581*e8d8bef9SDimitry Andric auto *NewStatepoint = SS.rewriteStatepoint(); 582*e8d8bef9SDimitry Andric SS.insertReloads(NewStatepoint, ReloadCache); 5835ffd83dbSDimitry Andric return true; 5845ffd83dbSDimitry Andric } 5855ffd83dbSDimitry Andric }; 5865ffd83dbSDimitry Andric } // namespace 5875ffd83dbSDimitry Andric 5885ffd83dbSDimitry Andric bool FixupStatepointCallerSaved::runOnMachineFunction(MachineFunction &MF) { 5895ffd83dbSDimitry Andric if (skipFunction(MF.getFunction())) 5905ffd83dbSDimitry Andric return false; 5915ffd83dbSDimitry Andric 5925ffd83dbSDimitry Andric const Function &F = MF.getFunction(); 5935ffd83dbSDimitry Andric if (!F.hasGC()) 5945ffd83dbSDimitry Andric return false; 5955ffd83dbSDimitry Andric 5965ffd83dbSDimitry Andric SmallVector<MachineInstr *, 16> Statepoints; 5975ffd83dbSDimitry Andric for (MachineBasicBlock &BB : MF) 5985ffd83dbSDimitry Andric for (MachineInstr &I : BB) 5995ffd83dbSDimitry Andric if (I.getOpcode() == TargetOpcode::STATEPOINT) 6005ffd83dbSDimitry Andric Statepoints.push_back(&I); 6015ffd83dbSDimitry Andric 6025ffd83dbSDimitry Andric if (Statepoints.empty()) 6035ffd83dbSDimitry Andric return false; 6045ffd83dbSDimitry Andric 6055ffd83dbSDimitry Andric bool Changed = false; 6065ffd83dbSDimitry Andric StatepointProcessor SPP(MF); 607*e8d8bef9SDimitry Andric unsigned NumStatepoints = 0; 608*e8d8bef9SDimitry Andric bool AllowGCPtrInCSR = PassGCPtrInCSR; 609*e8d8bef9SDimitry Andric for (MachineInstr *I : Statepoints) { 610*e8d8bef9SDimitry Andric ++NumStatepoints; 611*e8d8bef9SDimitry Andric if (MaxStatepointsWithRegs.getNumOccurrences() && 612*e8d8bef9SDimitry Andric NumStatepoints >= MaxStatepointsWithRegs) 613*e8d8bef9SDimitry Andric AllowGCPtrInCSR = false; 614*e8d8bef9SDimitry Andric Changed |= SPP.process(*I, AllowGCPtrInCSR); 615*e8d8bef9SDimitry Andric } 6165ffd83dbSDimitry Andric return Changed; 6175ffd83dbSDimitry Andric } 618