xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/RegUsageInfoCollector.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/MachineFunctionPass.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterUsageInfo.h"
2581ad6265SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
2681ad6265SDimitry Andric #include "llvm/IR/Function.h"
270b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #define DEBUG_TYPE "ip-regalloc"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric STATISTIC(NumCSROpt,
350b57cec5SDimitry Andric           "Number of functions optimized for callee saved registers");
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric namespace {
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric class RegUsageInfoCollector : public MachineFunctionPass {
400b57cec5SDimitry Andric public:
RegUsageInfoCollector()410b57cec5SDimitry Andric   RegUsageInfoCollector() : MachineFunctionPass(ID) {
420b57cec5SDimitry Andric     PassRegistry &Registry = *PassRegistry::getPassRegistry();
430b57cec5SDimitry Andric     initializeRegUsageInfoCollectorPass(Registry);
440b57cec5SDimitry Andric   }
450b57cec5SDimitry Andric 
getPassName() const460b57cec5SDimitry Andric   StringRef getPassName() const override {
470b57cec5SDimitry Andric     return "Register Usage Information Collector Pass";
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const500b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
510b57cec5SDimitry Andric     AU.addRequired<PhysicalRegisterUsageInfo>();
520b57cec5SDimitry Andric     AU.setPreservesAll();
530b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
570b57cec5SDimitry Andric 
58480093f4SDimitry Andric   // Call getCalleeSaves and then also set the bits for subregs and
590b57cec5SDimitry Andric   // fully saved superregs.
600b57cec5SDimitry Andric   static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF);
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   static char ID;
630b57cec5SDimitry Andric };
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric } // end of anonymous namespace
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric char RegUsageInfoCollector::ID = 0;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
700b57cec5SDimitry Andric                       "Register Usage Information Collector", false, false)
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)710b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
720b57cec5SDimitry Andric INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
730b57cec5SDimitry Andric                     "Register Usage Information Collector", false, false)
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric FunctionPass *llvm::createRegUsageInfoCollector() {
760b57cec5SDimitry Andric   return new RegUsageInfoCollector();
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric // TODO: Move to hook somwehere?
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric // Return true if it is useful to track the used registers for IPRA / no CSR
820b57cec5SDimitry Andric // optimizations. This is not useful for entry points, and computing the
830b57cec5SDimitry Andric // register usage information is expensive.
isCallableFunction(const MachineFunction & MF)840b57cec5SDimitry Andric static bool isCallableFunction(const MachineFunction &MF) {
850b57cec5SDimitry Andric   switch (MF.getFunction().getCallingConv()) {
860b57cec5SDimitry Andric   case CallingConv::AMDGPU_VS:
870b57cec5SDimitry Andric   case CallingConv::AMDGPU_GS:
880b57cec5SDimitry Andric   case CallingConv::AMDGPU_PS:
890b57cec5SDimitry Andric   case CallingConv::AMDGPU_CS:
900b57cec5SDimitry Andric   case CallingConv::AMDGPU_HS:
910b57cec5SDimitry Andric   case CallingConv::AMDGPU_ES:
920b57cec5SDimitry Andric   case CallingConv::AMDGPU_LS:
930b57cec5SDimitry Andric   case CallingConv::AMDGPU_KERNEL:
940b57cec5SDimitry Andric     return false;
950b57cec5SDimitry Andric   default:
960b57cec5SDimitry Andric     return true;
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)1000b57cec5SDimitry Andric bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
1010b57cec5SDimitry Andric   MachineRegisterInfo *MRI = &MF.getRegInfo();
1020b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1030b57cec5SDimitry Andric   const LLVMTargetMachine &TM = MF.getTarget();
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " -------------------- " << getPassName()
1060b57cec5SDimitry Andric                     << " -------------------- \nFunction Name : "
1070b57cec5SDimitry Andric                     << MF.getName() << '\n');
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   // Analyzing the register usage may be expensive on some targets.
1100b57cec5SDimitry Andric   if (!isCallableFunction(MF)) {
1110b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Not analyzing non-callable function\n");
1120b57cec5SDimitry Andric     return false;
1130b57cec5SDimitry Andric   }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   // If there are no callers, there's no point in computing more precise
1160b57cec5SDimitry Andric   // register usage here.
1170b57cec5SDimitry Andric   if (MF.getFunction().use_empty()) {
1180b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Not analyzing function with no callers\n");
1190b57cec5SDimitry Andric     return false;
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   std::vector<uint32_t> RegMask;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   // Compute the size of the bit vector to represent all the registers.
1250b57cec5SDimitry Andric   // The bit vector is broken into 32-bit chunks, thus takes the ceil of
1260b57cec5SDimitry Andric   // the number of registers divided by 32 for the size.
1270b57cec5SDimitry Andric   unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
1280b57cec5SDimitry Andric   RegMask.resize(RegMaskSize, ~((uint32_t)0));
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   const Function &F = MF.getFunction();
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>();
1330b57cec5SDimitry Andric   PRUI.setTargetMachine(TM);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Clobbered Registers: ");
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   BitVector SavedRegs;
1380b57cec5SDimitry Andric   computeCalleeSavedRegs(SavedRegs, MF);
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
1410b57cec5SDimitry Andric   auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
1420b57cec5SDimitry Andric     RegMask[Reg / 32] &= ~(1u << Reg % 32);
1430b57cec5SDimitry Andric   };
1448bcb0991SDimitry Andric 
145*0fca6ea1SDimitry Andric   // Don't include $noreg in any regmasks.
146*0fca6ea1SDimitry Andric   SetRegAsDefined(MCRegister::NoRegister);
147*0fca6ea1SDimitry Andric 
1488bcb0991SDimitry Andric   // Some targets can clobber registers "inside" a call, typically in
1498bcb0991SDimitry Andric   // linker-generated code.
1508bcb0991SDimitry Andric   for (const MCPhysReg Reg : TRI->getIntraCallClobberedRegs(&MF))
1518bcb0991SDimitry Andric     for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
1528bcb0991SDimitry Andric       SetRegAsDefined(*AI);
1538bcb0991SDimitry Andric 
1540b57cec5SDimitry Andric   // Scan all the physical registers. When a register is defined in the current
1550b57cec5SDimitry Andric   // function set it and all the aliasing registers as defined in the regmask.
1560b57cec5SDimitry Andric   // FIXME: Rewrite to use regunits.
1570b57cec5SDimitry Andric   for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
1580b57cec5SDimitry Andric     // Don't count registers that are saved and restored.
1590b57cec5SDimitry Andric     if (SavedRegs.test(PReg))
1600b57cec5SDimitry Andric       continue;
1610b57cec5SDimitry Andric     // If a register is defined by an instruction mark it as defined together
1620b57cec5SDimitry Andric     // with all it's unsaved aliases.
1630b57cec5SDimitry Andric     if (!MRI->def_empty(PReg)) {
1640b57cec5SDimitry Andric       for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
1650b57cec5SDimitry Andric         if (!SavedRegs.test(*AI))
1660b57cec5SDimitry Andric           SetRegAsDefined(*AI);
1670b57cec5SDimitry Andric       continue;
1680b57cec5SDimitry Andric     }
1690b57cec5SDimitry Andric     // If a register is in the UsedPhysRegsMask set then mark it as defined.
1700b57cec5SDimitry Andric     // All clobbered aliases will also be in the set, so we can skip setting
1710b57cec5SDimitry Andric     // as defined all the aliases here.
1720b57cec5SDimitry Andric     if (UsedPhysRegsMask.test(PReg))
1730b57cec5SDimitry Andric       SetRegAsDefined(PReg);
1740b57cec5SDimitry Andric   }
1750b57cec5SDimitry Andric 
1768bcb0991SDimitry Andric   if (TargetFrameLowering::isSafeForNoCSROpt(F) &&
1778bcb0991SDimitry Andric       MF.getSubtarget().getFrameLowering()->isProfitableForNoCSROpt(F)) {
1780b57cec5SDimitry Andric     ++NumCSROpt;
1790b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << MF.getName()
1800b57cec5SDimitry Andric                       << " function optimized for not having CSR.\n");
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   LLVM_DEBUG(
1840b57cec5SDimitry Andric     for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
1850b57cec5SDimitry Andric       if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
1860b57cec5SDimitry Andric         dbgs() << printReg(PReg, TRI) << " ";
1870b57cec5SDimitry Andric     }
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric     dbgs() << " \n----------------------------------------\n";
1900b57cec5SDimitry Andric   );
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   PRUI.storeUpdateRegUsageInfo(F, RegMask);
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   return false;
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric void RegUsageInfoCollector::
computeCalleeSavedRegs(BitVector & SavedRegs,MachineFunction & MF)1980b57cec5SDimitry Andric computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) {
1990b57cec5SDimitry Andric   const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
2000b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   // Target will return the set of registers that it saves/restores as needed.
2030b57cec5SDimitry Andric   SavedRegs.clear();
204480093f4SDimitry Andric   TFI.getCalleeSaves(MF, SavedRegs);
2050b57cec5SDimitry Andric   if (SavedRegs.none())
2060b57cec5SDimitry Andric     return;
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric   // Insert subregs.
2090b57cec5SDimitry Andric   const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
2100b57cec5SDimitry Andric   for (unsigned i = 0; CSRegs[i]; ++i) {
2110b57cec5SDimitry Andric     MCPhysReg Reg = CSRegs[i];
2120b57cec5SDimitry Andric     if (SavedRegs.test(Reg)) {
2130b57cec5SDimitry Andric       // Save subregisters
21406c3fb27SDimitry Andric       for (MCPhysReg SR : TRI.subregs(Reg))
21506c3fb27SDimitry Andric         SavedRegs.set(SR);
2160b57cec5SDimitry Andric     }
2170b57cec5SDimitry Andric   }
2180b57cec5SDimitry Andric }
219