1 //===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This pass is required to take advantage of the interprocedural register
11 /// allocation infrastructure.
13 /// This pass is simple MachineFunction pass which collects register usage
14 /// details by iterating through each physical registers and checking
15 /// MRI::isPhysRegUsed() then creates a RegMask based on this details.
16 /// The pass then stores this RegMask in PhysicalRegisterUsageInfo.cpp
18 //===----------------------------------------------------------------------===//
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/CodeGen/RegisterUsageInfo.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetFrameLowering.h"
34 #define DEBUG_TYPE "ip-regalloc"
37 "Number of functions optimized for callee saved registers");
40 void initializeRegUsageInfoCollectorPass(PassRegistry &);
44 class RegUsageInfoCollector : public MachineFunctionPass {
46 RegUsageInfoCollector() : MachineFunctionPass(ID) {
47 PassRegistry &Registry = *PassRegistry::getPassRegistry();
48 initializeRegUsageInfoCollectorPass(Registry);
51 StringRef getPassName() const override {
52 return "Register Usage Information Collector Pass";
55 void getAnalysisUsage(AnalysisUsage &AU) const override;
57 bool runOnMachineFunction(MachineFunction &MF) override;
61 } // end of anonymous namespace
63 char RegUsageInfoCollector::ID = 0;
65 INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
66 "Register Usage Information Collector", false, false)
67 INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
68 INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
69 "Register Usage Information Collector", false, false)
71 FunctionPass *llvm::createRegUsageInfoCollector() {
72 return new RegUsageInfoCollector();
75 void RegUsageInfoCollector::getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addRequired<PhysicalRegisterUsageInfo>();
78 MachineFunctionPass::getAnalysisUsage(AU);
81 bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
82 MachineRegisterInfo *MRI = &MF.getRegInfo();
83 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
84 const TargetMachine &TM = MF.getTarget();
86 DEBUG(dbgs() << " -------------------- " << getPassName()
87 << " -------------------- \n");
88 DEBUG(dbgs() << "Function Name : " << MF.getName() << "\n");
90 std::vector<uint32_t> RegMask;
92 // Compute the size of the bit vector to represent all the registers.
93 // The bit vector is broken into 32-bit chunks, thus takes the ceil of
94 // the number of registers divided by 32 for the size.
95 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
96 RegMask.resize(RegMaskSize, 0xFFFFFFFF);
98 const Function *F = MF.getFunction();
100 PhysicalRegisterUsageInfo *PRUI = &getAnalysis<PhysicalRegisterUsageInfo>();
102 PRUI->setTargetMachine(&TM);
104 DEBUG(dbgs() << "Clobbered Registers: ");
106 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
107 if (MRI->isPhysRegModified(PReg, true))
108 RegMask[PReg / 32] &= ~(1u << PReg % 32);
110 if (!TargetFrameLowering::isSafeForNoCSROpt(F)) {
111 const uint32_t *CallPreservedMask =
112 TRI->getCallPreservedMask(MF, F->getCallingConv());
113 // Set callee saved register as preserved.
114 for (unsigned i = 0; i < RegMaskSize; ++i)
115 RegMask[i] = RegMask[i] | CallPreservedMask[i];
118 DEBUG(dbgs() << MF.getName()
119 << " function optimized for not having CSR.\n");
122 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg)
123 if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
124 DEBUG(dbgs() << TRI->getName(PReg) << " ");
126 DEBUG(dbgs() << " \n----------------------------------------\n");
128 PRUI->storeUpdateRegUsageInfo(F, std::move(RegMask));