10b57cec5SDimitry Andric //===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// This pass is required to take advantage of the interprocedural register 100b57cec5SDimitry Andric /// allocation infrastructure. 110b57cec5SDimitry Andric /// 120b57cec5SDimitry Andric /// This pass is simple MachineFunction pass which collects register usage 130b57cec5SDimitry Andric /// details by iterating through each physical registers and checking 140b57cec5SDimitry Andric /// MRI::isPhysRegUsed() then creates a RegMask based on this details. 150b57cec5SDimitry Andric /// The pass then stores this RegMask in PhysicalRegisterUsageInfo.cpp 160b57cec5SDimitry Andric /// 170b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterUsageInfo.h" 270b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric using namespace llvm; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric #define DEBUG_TYPE "ip-regalloc" 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric STATISTIC(NumCSROpt, 360b57cec5SDimitry Andric "Number of functions optimized for callee saved registers"); 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric namespace { 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric class RegUsageInfoCollector : public MachineFunctionPass { 410b57cec5SDimitry Andric public: 420b57cec5SDimitry Andric RegUsageInfoCollector() : MachineFunctionPass(ID) { 430b57cec5SDimitry Andric PassRegistry &Registry = *PassRegistry::getPassRegistry(); 440b57cec5SDimitry Andric initializeRegUsageInfoCollectorPass(Registry); 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric StringRef getPassName() const override { 480b57cec5SDimitry Andric return "Register Usage Information Collector Pass"; 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 520b57cec5SDimitry Andric AU.addRequired<PhysicalRegisterUsageInfo>(); 530b57cec5SDimitry Andric AU.setPreservesAll(); 540b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric // Call determineCalleeSaves and then also set the bits for subregs and 600b57cec5SDimitry Andric // fully saved superregs. 610b57cec5SDimitry Andric static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric static char ID; 640b57cec5SDimitry Andric }; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric } // end of anonymous namespace 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric char RegUsageInfoCollector::ID = 0; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector", 710b57cec5SDimitry Andric "Register Usage Information Collector", false, false) 720b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo) 730b57cec5SDimitry Andric INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector", 740b57cec5SDimitry Andric "Register Usage Information Collector", false, false) 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric FunctionPass *llvm::createRegUsageInfoCollector() { 770b57cec5SDimitry Andric return new RegUsageInfoCollector(); 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // TODO: Move to hook somwehere? 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // Return true if it is useful to track the used registers for IPRA / no CSR 830b57cec5SDimitry Andric // optimizations. This is not useful for entry points, and computing the 840b57cec5SDimitry Andric // register usage information is expensive. 850b57cec5SDimitry Andric static bool isCallableFunction(const MachineFunction &MF) { 860b57cec5SDimitry Andric switch (MF.getFunction().getCallingConv()) { 870b57cec5SDimitry Andric case CallingConv::AMDGPU_VS: 880b57cec5SDimitry Andric case CallingConv::AMDGPU_GS: 890b57cec5SDimitry Andric case CallingConv::AMDGPU_PS: 900b57cec5SDimitry Andric case CallingConv::AMDGPU_CS: 910b57cec5SDimitry Andric case CallingConv::AMDGPU_HS: 920b57cec5SDimitry Andric case CallingConv::AMDGPU_ES: 930b57cec5SDimitry Andric case CallingConv::AMDGPU_LS: 940b57cec5SDimitry Andric case CallingConv::AMDGPU_KERNEL: 950b57cec5SDimitry Andric return false; 960b57cec5SDimitry Andric default: 970b57cec5SDimitry Andric return true; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) { 1020b57cec5SDimitry Andric MachineRegisterInfo *MRI = &MF.getRegInfo(); 1030b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 1040b57cec5SDimitry Andric const LLVMTargetMachine &TM = MF.getTarget(); 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -------------------- " << getPassName() 1070b57cec5SDimitry Andric << " -------------------- \nFunction Name : " 1080b57cec5SDimitry Andric << MF.getName() << '\n'); 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric // Analyzing the register usage may be expensive on some targets. 1110b57cec5SDimitry Andric if (!isCallableFunction(MF)) { 1120b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Not analyzing non-callable function\n"); 1130b57cec5SDimitry Andric return false; 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric // If there are no callers, there's no point in computing more precise 1170b57cec5SDimitry Andric // register usage here. 1180b57cec5SDimitry Andric if (MF.getFunction().use_empty()) { 1190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Not analyzing function with no callers\n"); 1200b57cec5SDimitry Andric return false; 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric std::vector<uint32_t> RegMask; 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric // Compute the size of the bit vector to represent all the registers. 1260b57cec5SDimitry Andric // The bit vector is broken into 32-bit chunks, thus takes the ceil of 1270b57cec5SDimitry Andric // the number of registers divided by 32 for the size. 1280b57cec5SDimitry Andric unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs()); 1290b57cec5SDimitry Andric RegMask.resize(RegMaskSize, ~((uint32_t)0)); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric const Function &F = MF.getFunction(); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>(); 1340b57cec5SDimitry Andric PRUI.setTargetMachine(TM); 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Clobbered Registers: "); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric BitVector SavedRegs; 1390b57cec5SDimitry Andric computeCalleeSavedRegs(SavedRegs, MF); 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask(); 1420b57cec5SDimitry Andric auto SetRegAsDefined = [&RegMask] (unsigned Reg) { 1430b57cec5SDimitry Andric RegMask[Reg / 32] &= ~(1u << Reg % 32); 1440b57cec5SDimitry Andric }; 145*8bcb0991SDimitry Andric 146*8bcb0991SDimitry Andric // Some targets can clobber registers "inside" a call, typically in 147*8bcb0991SDimitry Andric // linker-generated code. 148*8bcb0991SDimitry Andric for (const MCPhysReg Reg : TRI->getIntraCallClobberedRegs(&MF)) 149*8bcb0991SDimitry Andric for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 150*8bcb0991SDimitry Andric SetRegAsDefined(*AI); 151*8bcb0991SDimitry Andric 1520b57cec5SDimitry Andric // Scan all the physical registers. When a register is defined in the current 1530b57cec5SDimitry Andric // function set it and all the aliasing registers as defined in the regmask. 1540b57cec5SDimitry Andric // FIXME: Rewrite to use regunits. 1550b57cec5SDimitry Andric for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) { 1560b57cec5SDimitry Andric // Don't count registers that are saved and restored. 1570b57cec5SDimitry Andric if (SavedRegs.test(PReg)) 1580b57cec5SDimitry Andric continue; 1590b57cec5SDimitry Andric // If a register is defined by an instruction mark it as defined together 1600b57cec5SDimitry Andric // with all it's unsaved aliases. 1610b57cec5SDimitry Andric if (!MRI->def_empty(PReg)) { 1620b57cec5SDimitry Andric for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI) 1630b57cec5SDimitry Andric if (!SavedRegs.test(*AI)) 1640b57cec5SDimitry Andric SetRegAsDefined(*AI); 1650b57cec5SDimitry Andric continue; 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric // If a register is in the UsedPhysRegsMask set then mark it as defined. 1680b57cec5SDimitry Andric // All clobbered aliases will also be in the set, so we can skip setting 1690b57cec5SDimitry Andric // as defined all the aliases here. 1700b57cec5SDimitry Andric if (UsedPhysRegsMask.test(PReg)) 1710b57cec5SDimitry Andric SetRegAsDefined(PReg); 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 174*8bcb0991SDimitry Andric if (TargetFrameLowering::isSafeForNoCSROpt(F) && 175*8bcb0991SDimitry Andric MF.getSubtarget().getFrameLowering()->isProfitableForNoCSROpt(F)) { 1760b57cec5SDimitry Andric ++NumCSROpt; 1770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << MF.getName() 1780b57cec5SDimitry Andric << " function optimized for not having CSR.\n"); 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric LLVM_DEBUG( 1820b57cec5SDimitry Andric for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) { 1830b57cec5SDimitry Andric if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg)) 1840b57cec5SDimitry Andric dbgs() << printReg(PReg, TRI) << " "; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric dbgs() << " \n----------------------------------------\n"; 1880b57cec5SDimitry Andric ); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric PRUI.storeUpdateRegUsageInfo(F, RegMask); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric return false; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric void RegUsageInfoCollector:: 1960b57cec5SDimitry Andric computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) { 1970b57cec5SDimitry Andric const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 1980b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // Target will return the set of registers that it saves/restores as needed. 2010b57cec5SDimitry Andric SavedRegs.clear(); 2020b57cec5SDimitry Andric TFI.determineCalleeSaves(MF, SavedRegs); 2030b57cec5SDimitry Andric if (SavedRegs.none()) 2040b57cec5SDimitry Andric return; 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric // Insert subregs. 2070b57cec5SDimitry Andric const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF); 2080b57cec5SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 2090b57cec5SDimitry Andric MCPhysReg Reg = CSRegs[i]; 2100b57cec5SDimitry Andric if (SavedRegs.test(Reg)) { 2110b57cec5SDimitry Andric // Save subregisters 2120b57cec5SDimitry Andric for (MCSubRegIterator SR(Reg, &TRI); SR.isValid(); ++SR) 2130b57cec5SDimitry Andric SavedRegs.set(*SR); 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric } 217