13ca95b02SDimitry Andric //===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
23ca95b02SDimitry Andric //
33ca95b02SDimitry Andric // The LLVM Compiler Infrastructure
43ca95b02SDimitry Andric //
53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source
63ca95b02SDimitry Andric // License. See LICENSE.TXT for details.
73ca95b02SDimitry Andric //
83ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
93ca95b02SDimitry Andric ///
103ca95b02SDimitry Andric /// This pass is required to take advantage of the interprocedural register
113ca95b02SDimitry Andric /// allocation infrastructure.
123ca95b02SDimitry Andric ///
133ca95b02SDimitry Andric /// This pass is simple MachineFunction pass which collects register usage
143ca95b02SDimitry Andric /// details by iterating through each physical registers and checking
153ca95b02SDimitry Andric /// MRI::isPhysRegUsed() then creates a RegMask based on this details.
163ca95b02SDimitry Andric /// The pass then stores this RegMask in PhysicalRegisterUsageInfo.cpp
173ca95b02SDimitry Andric ///
183ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
193ca95b02SDimitry Andric
203ca95b02SDimitry Andric #include "llvm/ADT/Statistic.h"
213ca95b02SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
223ca95b02SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
233ca95b02SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
243ca95b02SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
253ca95b02SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
263ca95b02SDimitry Andric #include "llvm/CodeGen/Passes.h"
273ca95b02SDimitry Andric #include "llvm/CodeGen/RegisterUsageInfo.h"
283ca95b02SDimitry Andric #include "llvm/Support/Debug.h"
293ca95b02SDimitry Andric #include "llvm/Support/raw_ostream.h"
302cab237bSDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
313ca95b02SDimitry Andric
323ca95b02SDimitry Andric using namespace llvm;
333ca95b02SDimitry Andric
343ca95b02SDimitry Andric #define DEBUG_TYPE "ip-regalloc"
353ca95b02SDimitry Andric
363ca95b02SDimitry Andric STATISTIC(NumCSROpt,
373ca95b02SDimitry Andric "Number of functions optimized for callee saved registers");
383ca95b02SDimitry Andric
393ca95b02SDimitry Andric namespace {
404ba319b5SDimitry Andric
413ca95b02SDimitry Andric class RegUsageInfoCollector : public MachineFunctionPass {
423ca95b02SDimitry Andric public:
RegUsageInfoCollector()433ca95b02SDimitry Andric RegUsageInfoCollector() : MachineFunctionPass(ID) {
443ca95b02SDimitry Andric PassRegistry &Registry = *PassRegistry::getPassRegistry();
453ca95b02SDimitry Andric initializeRegUsageInfoCollectorPass(Registry);
463ca95b02SDimitry Andric }
473ca95b02SDimitry Andric
getPassName() const48d88c1a5aSDimitry Andric StringRef getPassName() const override {
493ca95b02SDimitry Andric return "Register Usage Information Collector Pass";
503ca95b02SDimitry Andric }
513ca95b02SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const524ba319b5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
534ba319b5SDimitry Andric AU.addRequired<PhysicalRegisterUsageInfo>();
544ba319b5SDimitry Andric AU.setPreservesAll();
554ba319b5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
564ba319b5SDimitry Andric }
573ca95b02SDimitry Andric
583ca95b02SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
593ca95b02SDimitry Andric
604ba319b5SDimitry Andric // Call determineCalleeSaves and then also set the bits for subregs and
614ba319b5SDimitry Andric // fully saved superregs.
624ba319b5SDimitry Andric static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF);
634ba319b5SDimitry Andric
643ca95b02SDimitry Andric static char ID;
653ca95b02SDimitry Andric };
664ba319b5SDimitry Andric
673ca95b02SDimitry Andric } // end of anonymous namespace
683ca95b02SDimitry Andric
693ca95b02SDimitry Andric char RegUsageInfoCollector::ID = 0;
703ca95b02SDimitry Andric
713ca95b02SDimitry Andric INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
723ca95b02SDimitry Andric "Register Usage Information Collector", false, false)
INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)733ca95b02SDimitry Andric INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
743ca95b02SDimitry Andric INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
753ca95b02SDimitry Andric "Register Usage Information Collector", false, false)
763ca95b02SDimitry Andric
773ca95b02SDimitry Andric FunctionPass *llvm::createRegUsageInfoCollector() {
783ca95b02SDimitry Andric return new RegUsageInfoCollector();
793ca95b02SDimitry Andric }
803ca95b02SDimitry Andric
runOnMachineFunction(MachineFunction & MF)813ca95b02SDimitry Andric bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
823ca95b02SDimitry Andric MachineRegisterInfo *MRI = &MF.getRegInfo();
833ca95b02SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
84*b5893f02SDimitry Andric const LLVMTargetMachine &TM = MF.getTarget();
853ca95b02SDimitry Andric
864ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " -------------------- " << getPassName()
873ca95b02SDimitry Andric << " -------------------- \n");
884ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Function Name : " << MF.getName() << "\n");
893ca95b02SDimitry Andric
903ca95b02SDimitry Andric std::vector<uint32_t> RegMask;
913ca95b02SDimitry Andric
923ca95b02SDimitry Andric // Compute the size of the bit vector to represent all the registers.
933ca95b02SDimitry Andric // The bit vector is broken into 32-bit chunks, thus takes the ceil of
943ca95b02SDimitry Andric // the number of registers divided by 32 for the size.
954ba319b5SDimitry Andric unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
964ba319b5SDimitry Andric RegMask.resize(RegMaskSize, ~((uint32_t)0));
973ca95b02SDimitry Andric
982cab237bSDimitry Andric const Function &F = MF.getFunction();
993ca95b02SDimitry Andric
1004ba319b5SDimitry Andric PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>();
1014ba319b5SDimitry Andric PRUI.setTargetMachine(TM);
1023ca95b02SDimitry Andric
1034ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Clobbered Registers: ");
1043ca95b02SDimitry Andric
1054ba319b5SDimitry Andric BitVector SavedRegs;
1064ba319b5SDimitry Andric computeCalleeSavedRegs(SavedRegs, MF);
1073ca95b02SDimitry Andric
1087a7e6055SDimitry Andric const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
1097a7e6055SDimitry Andric auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
1107a7e6055SDimitry Andric RegMask[Reg / 32] &= ~(1u << Reg % 32);
1117a7e6055SDimitry Andric };
1127a7e6055SDimitry Andric // Scan all the physical registers. When a register is defined in the current
1137a7e6055SDimitry Andric // function set it and all the aliasing registers as defined in the regmask.
1147a7e6055SDimitry Andric for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
1154ba319b5SDimitry Andric // Don't count registers that are saved and restored.
1164ba319b5SDimitry Andric if (SavedRegs.test(PReg))
1177a7e6055SDimitry Andric continue;
1187a7e6055SDimitry Andric // If a register is defined by an instruction mark it as defined together
1194ba319b5SDimitry Andric // with all it's unsaved aliases.
1207a7e6055SDimitry Andric if (!MRI->def_empty(PReg)) {
1217a7e6055SDimitry Andric for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
1224ba319b5SDimitry Andric if (!SavedRegs.test(*AI))
1237a7e6055SDimitry Andric SetRegAsDefined(*AI);
1244ba319b5SDimitry Andric continue;
1257a7e6055SDimitry Andric }
1264ba319b5SDimitry Andric // If a register is in the UsedPhysRegsMask set then mark it as defined.
1274ba319b5SDimitry Andric // All clobbered aliases will also be in the set, so we can skip setting
1284ba319b5SDimitry Andric // as defined all the aliases here.
1294ba319b5SDimitry Andric if (UsedPhysRegsMask.test(PReg))
1304ba319b5SDimitry Andric SetRegAsDefined(PReg);
1317a7e6055SDimitry Andric }
1323ca95b02SDimitry Andric
1334ba319b5SDimitry Andric if (TargetFrameLowering::isSafeForNoCSROpt(F)) {
1343ca95b02SDimitry Andric ++NumCSROpt;
1354ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << MF.getName()
1363ca95b02SDimitry Andric << " function optimized for not having CSR.\n");
1373ca95b02SDimitry Andric }
1383ca95b02SDimitry Andric
1393ca95b02SDimitry Andric for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
1403ca95b02SDimitry Andric if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
1414ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << printReg(PReg, TRI) << " ");
1423ca95b02SDimitry Andric
1434ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " \n----------------------------------------\n");
1443ca95b02SDimitry Andric
1454ba319b5SDimitry Andric PRUI.storeUpdateRegUsageInfo(F, RegMask);
1463ca95b02SDimitry Andric
1473ca95b02SDimitry Andric return false;
1483ca95b02SDimitry Andric }
1494ba319b5SDimitry Andric
1504ba319b5SDimitry Andric void RegUsageInfoCollector::
computeCalleeSavedRegs(BitVector & SavedRegs,MachineFunction & MF)1514ba319b5SDimitry Andric computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) {
1524ba319b5SDimitry Andric const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
1534ba319b5SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1544ba319b5SDimitry Andric
1554ba319b5SDimitry Andric // Target will return the set of registers that it saves/restores as needed.
1564ba319b5SDimitry Andric SavedRegs.clear();
1574ba319b5SDimitry Andric TFI.determineCalleeSaves(MF, SavedRegs);
1584ba319b5SDimitry Andric
1594ba319b5SDimitry Andric // Insert subregs.
1604ba319b5SDimitry Andric const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
1614ba319b5SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) {
1624ba319b5SDimitry Andric unsigned Reg = CSRegs[i];
1634ba319b5SDimitry Andric if (SavedRegs.test(Reg))
1644ba319b5SDimitry Andric for (MCSubRegIterator SR(Reg, &TRI, false); SR.isValid(); ++SR)
1654ba319b5SDimitry Andric SavedRegs.set(*SR);
1664ba319b5SDimitry Andric }
1674ba319b5SDimitry Andric
1684ba319b5SDimitry Andric // Insert any register fully saved via subregisters.
169*b5893f02SDimitry Andric for (const TargetRegisterClass *RC : TRI.regclasses()) {
170*b5893f02SDimitry Andric if (!RC->CoveredBySubRegs)
171*b5893f02SDimitry Andric continue;
172*b5893f02SDimitry Andric
1734ba319b5SDimitry Andric for (unsigned PReg = 1, PRegE = TRI.getNumRegs(); PReg < PRegE; ++PReg) {
1744ba319b5SDimitry Andric if (SavedRegs.test(PReg))
1754ba319b5SDimitry Andric continue;
1764ba319b5SDimitry Andric
1774ba319b5SDimitry Andric // Check if PReg is fully covered by its subregs.
178*b5893f02SDimitry Andric if (!RC->contains(PReg))
1794ba319b5SDimitry Andric continue;
1804ba319b5SDimitry Andric
1814ba319b5SDimitry Andric // Add PReg to SavedRegs if all subregs are saved.
1824ba319b5SDimitry Andric bool AllSubRegsSaved = true;
1834ba319b5SDimitry Andric for (MCSubRegIterator SR(PReg, &TRI, false); SR.isValid(); ++SR)
1844ba319b5SDimitry Andric if (!SavedRegs.test(*SR)) {
1854ba319b5SDimitry Andric AllSubRegsSaved = false;
1864ba319b5SDimitry Andric break;
1874ba319b5SDimitry Andric }
1884ba319b5SDimitry Andric if (AllSubRegsSaved)
1894ba319b5SDimitry Andric SavedRegs.set(PReg);
1904ba319b5SDimitry Andric }
1914ba319b5SDimitry Andric }
192*b5893f02SDimitry Andric }
193