[Alignment][NFC] Convert StoreInst to MaybeAlign
[llvm-complete.git] / lib / CodeGen / RegUsageInfoCollector.cpp
blob757ff0e44953dcaf0366c8a65e57c6c2385aab09
1 //===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// This pass is required to take advantage of the interprocedural register
10 /// allocation infrastructure.
11 ///
12 /// This pass is simple MachineFunction pass which collects register usage
13 /// details by iterating through each physical registers and checking
14 /// MRI::isPhysRegUsed() then creates a RegMask based on this details.
15 /// The pass then stores this RegMask in PhysicalRegisterUsageInfo.cpp
16 ///
17 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/CodeGen/RegisterUsageInfo.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/CodeGen/TargetFrameLowering.h"
31 using namespace llvm;
33 #define DEBUG_TYPE "ip-regalloc"
35 STATISTIC(NumCSROpt,
36 "Number of functions optimized for callee saved registers");
38 namespace {
40 class RegUsageInfoCollector : public MachineFunctionPass {
41 public:
42 RegUsageInfoCollector() : MachineFunctionPass(ID) {
43 PassRegistry &Registry = *PassRegistry::getPassRegistry();
44 initializeRegUsageInfoCollectorPass(Registry);
47 StringRef getPassName() const override {
48 return "Register Usage Information Collector Pass";
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.addRequired<PhysicalRegisterUsageInfo>();
53 AU.setPreservesAll();
54 MachineFunctionPass::getAnalysisUsage(AU);
57 bool runOnMachineFunction(MachineFunction &MF) override;
59 // Call determineCalleeSaves and then also set the bits for subregs and
60 // fully saved superregs.
61 static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF);
63 static char ID;
66 } // end of anonymous namespace
68 char RegUsageInfoCollector::ID = 0;
70 INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
71 "Register Usage Information Collector", false, false)
72 INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
73 INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
74 "Register Usage Information Collector", false, false)
76 FunctionPass *llvm::createRegUsageInfoCollector() {
77 return new RegUsageInfoCollector();
80 // TODO: Move to hook somwehere?
82 // Return true if it is useful to track the used registers for IPRA / no CSR
83 // optimizations. This is not useful for entry points, and computing the
84 // register usage information is expensive.
85 static bool isCallableFunction(const MachineFunction &MF) {
86 switch (MF.getFunction().getCallingConv()) {
87 case CallingConv::AMDGPU_VS:
88 case CallingConv::AMDGPU_GS:
89 case CallingConv::AMDGPU_PS:
90 case CallingConv::AMDGPU_CS:
91 case CallingConv::AMDGPU_HS:
92 case CallingConv::AMDGPU_ES:
93 case CallingConv::AMDGPU_LS:
94 case CallingConv::AMDGPU_KERNEL:
95 return false;
96 default:
97 return true;
101 bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
102 MachineRegisterInfo *MRI = &MF.getRegInfo();
103 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
104 const LLVMTargetMachine &TM = MF.getTarget();
106 LLVM_DEBUG(dbgs() << " -------------------- " << getPassName()
107 << " -------------------- \nFunction Name : "
108 << MF.getName() << '\n');
110 // Analyzing the register usage may be expensive on some targets.
111 if (!isCallableFunction(MF)) {
112 LLVM_DEBUG(dbgs() << "Not analyzing non-callable function\n");
113 return false;
116 // If there are no callers, there's no point in computing more precise
117 // register usage here.
118 if (MF.getFunction().use_empty()) {
119 LLVM_DEBUG(dbgs() << "Not analyzing function with no callers\n");
120 return false;
123 std::vector<uint32_t> RegMask;
125 // Compute the size of the bit vector to represent all the registers.
126 // The bit vector is broken into 32-bit chunks, thus takes the ceil of
127 // the number of registers divided by 32 for the size.
128 unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
129 RegMask.resize(RegMaskSize, ~((uint32_t)0));
131 const Function &F = MF.getFunction();
133 PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>();
134 PRUI.setTargetMachine(TM);
136 LLVM_DEBUG(dbgs() << "Clobbered Registers: ");
138 BitVector SavedRegs;
139 computeCalleeSavedRegs(SavedRegs, MF);
141 const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
142 auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
143 RegMask[Reg / 32] &= ~(1u << Reg % 32);
146 // Some targets can clobber registers "inside" a call, typically in
147 // linker-generated code.
148 for (const MCPhysReg Reg : TRI->getIntraCallClobberedRegs(&MF))
149 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
150 SetRegAsDefined(*AI);
152 // Scan all the physical registers. When a register is defined in the current
153 // function set it and all the aliasing registers as defined in the regmask.
154 // FIXME: Rewrite to use regunits.
155 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
156 // Don't count registers that are saved and restored.
157 if (SavedRegs.test(PReg))
158 continue;
159 // If a register is defined by an instruction mark it as defined together
160 // with all it's unsaved aliases.
161 if (!MRI->def_empty(PReg)) {
162 for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
163 if (!SavedRegs.test(*AI))
164 SetRegAsDefined(*AI);
165 continue;
167 // If a register is in the UsedPhysRegsMask set then mark it as defined.
168 // All clobbered aliases will also be in the set, so we can skip setting
169 // as defined all the aliases here.
170 if (UsedPhysRegsMask.test(PReg))
171 SetRegAsDefined(PReg);
174 if (TargetFrameLowering::isSafeForNoCSROpt(F) &&
175 MF.getSubtarget().getFrameLowering()->isProfitableForNoCSROpt(F)) {
176 ++NumCSROpt;
177 LLVM_DEBUG(dbgs() << MF.getName()
178 << " function optimized for not having CSR.\n");
181 LLVM_DEBUG(
182 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
183 if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
184 dbgs() << printReg(PReg, TRI) << " ";
187 dbgs() << " \n----------------------------------------\n";
190 PRUI.storeUpdateRegUsageInfo(F, RegMask);
192 return false;
195 void RegUsageInfoCollector::
196 computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) {
197 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
198 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
200 // Target will return the set of registers that it saves/restores as needed.
201 SavedRegs.clear();
202 TFI.determineCalleeSaves(MF, SavedRegs);
203 if (SavedRegs.none())
204 return;
206 // Insert subregs.
207 const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
208 for (unsigned i = 0; CSRegs[i]; ++i) {
209 MCPhysReg Reg = CSRegs[i];
210 if (SavedRegs.test(Reg)) {
211 // Save subregisters
212 for (MCSubRegIterator SR(Reg, &TRI); SR.isValid(); ++SR)
213 SavedRegs.set(*SR);