blob: 757ff0e44953dcaf0366c8a65e57c6c2385aab09 [file] [log] [blame]
Chad Rosier4f0dad12016-07-11 18:45:49 +00001//===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
Mehdi Aminibbacddf2016-06-10 16:19:46 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Mehdi Aminibbacddf2016-06-10 16:19:46 +00006//
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//===----------------------------------------------------------------------===//
18
Mehdi Amini4beea662016-07-13 23:39:34 +000019#include "llvm/ADT/Statistic.h"
Mehdi Aminibbacddf2016-06-10 16:19:46 +000020#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"
David Blaikie1be62f02017-11-03 22:32:11 +000029#include "llvm/CodeGen/TargetFrameLowering.h"
Mehdi Aminibbacddf2016-06-10 16:19:46 +000030
31using namespace llvm;
32
33#define DEBUG_TYPE "ip-regalloc"
34
Mehdi Amini4beea662016-07-13 23:39:34 +000035STATISTIC(NumCSROpt,
36 "Number of functions optimized for callee saved registers");
37
Mehdi Aminibbacddf2016-06-10 16:19:46 +000038namespace {
Matthias Braun5c1e23b2018-07-26 00:27:51 +000039
Mehdi Aminibbacddf2016-06-10 16:19:46 +000040class RegUsageInfoCollector : public MachineFunctionPass {
41public:
42 RegUsageInfoCollector() : MachineFunctionPass(ID) {
43 PassRegistry &Registry = *PassRegistry::getPassRegistry();
44 initializeRegUsageInfoCollectorPass(Registry);
45 }
46
Mehdi Amini117296c2016-10-01 02:56:57 +000047 StringRef getPassName() const override {
Mehdi Aminibbacddf2016-06-10 16:19:46 +000048 return "Register Usage Information Collector Pass";
49 }
50
Matthias Braun5c1e23b2018-07-26 00:27:51 +000051 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.addRequired<PhysicalRegisterUsageInfo>();
53 AU.setPreservesAll();
54 MachineFunctionPass::getAnalysisUsage(AU);
55 }
Mehdi Aminibbacddf2016-06-10 16:19:46 +000056
57 bool runOnMachineFunction(MachineFunction &MF) override;
58
Jonas Paulsson7d484fa2018-05-25 08:42:02 +000059 // Call determineCalleeSaves and then also set the bits for subregs and
60 // fully saved superregs.
61 static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF);
62
Mehdi Aminibbacddf2016-06-10 16:19:46 +000063 static char ID;
Mehdi Aminibbacddf2016-06-10 16:19:46 +000064};
Matthias Braun5c1e23b2018-07-26 00:27:51 +000065
Mehdi Aminibbacddf2016-06-10 16:19:46 +000066} // end of anonymous namespace
67
68char RegUsageInfoCollector::ID = 0;
69
70INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
71 "Register Usage Information Collector", false, false)
72INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
73INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
74 "Register Usage Information Collector", false, false)
75
76FunctionPass *llvm::createRegUsageInfoCollector() {
77 return new RegUsageInfoCollector();
78}
79
Matt Arsenault705e46f2019-07-05 23:33:43 +000080// TODO: Move to hook somwehere?
81
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.
85static 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:
Matt Arsenault6eb8ae82019-07-11 14:41:40 +000091 case CallingConv::AMDGPU_HS:
92 case CallingConv::AMDGPU_ES:
93 case CallingConv::AMDGPU_LS:
Matt Arsenault705e46f2019-07-05 23:33:43 +000094 case CallingConv::AMDGPU_KERNEL:
95 return false;
96 default:
97 return true;
98 }
99}
100
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000101bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
102 MachineRegisterInfo *MRI = &MF.getRegInfo();
Benjamin Kramerbc2f4fb2016-06-12 13:32:23 +0000103 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Matthias Braun7a75a912018-11-05 23:49:14 +0000104 const LLVMTargetMachine &TM = MF.getTarget();
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000105
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000106 LLVM_DEBUG(dbgs() << " -------------------- " << getPassName()
Matt Arsenault705e46f2019-07-05 23:33:43 +0000107 << " -------------------- \nFunction Name : "
108 << MF.getName() << '\n');
109
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;
114 }
115
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;
121 }
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000122
123 std::vector<uint32_t> RegMask;
124
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.
Matthias Braun57dd5b32018-07-26 00:27:47 +0000128 unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
Matthias Braun5c1e23b2018-07-26 00:27:51 +0000129 RegMask.resize(RegMaskSize, ~((uint32_t)0));
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000130
Matthias Braunf1caa282017-12-15 22:22:58 +0000131 const Function &F = MF.getFunction();
Mehdi Amini4beea662016-07-13 23:39:34 +0000132
Matthias Braun5c1e23b2018-07-26 00:27:51 +0000133 PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>();
134 PRUI.setTargetMachine(TM);
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000135
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000136 LLVM_DEBUG(dbgs() << "Clobbered Registers: ");
Chad Rosier4f0dad12016-07-11 18:45:49 +0000137
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000138 BitVector SavedRegs;
139 computeCalleeSavedRegs(SavedRegs, MF);
140
Marcello Maggioni598d89a2017-03-13 21:42:53 +0000141 const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
142 auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
143 RegMask[Reg / 32] &= ~(1u << Reg % 32);
144 };
Oliver Stannard8ed83532019-08-05 09:04:10 +0000145
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);
151
Marcello Maggioni598d89a2017-03-13 21:42:53 +0000152 // 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.
Matt Arsenault705e46f2019-07-05 23:33:43 +0000154 // FIXME: Rewrite to use regunits.
Marcello Maggioni598d89a2017-03-13 21:42:53 +0000155 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000156 // Don't count registers that are saved and restored.
157 if (SavedRegs.test(PReg))
158 continue;
Marcello Maggioni598d89a2017-03-13 21:42:53 +0000159 // If a register is defined by an instruction mark it as defined together
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000160 // with all it's unsaved aliases.
Marcello Maggioni598d89a2017-03-13 21:42:53 +0000161 if (!MRI->def_empty(PReg)) {
162 for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000163 if (!SavedRegs.test(*AI))
164 SetRegAsDefined(*AI);
Jonas Paulsson72fe7602018-05-04 07:50:05 +0000165 continue;
Marcello Maggioni598d89a2017-03-13 21:42:53 +0000166 }
Jonas Paulsson72fe7602018-05-04 07:50:05 +0000167 // 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);
Marcello Maggioni598d89a2017-03-13 21:42:53 +0000172 }
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000173
Oliver Stannard4b7239e2019-08-02 10:23:17 +0000174 if (TargetFrameLowering::isSafeForNoCSROpt(F) &&
175 MF.getSubtarget().getFrameLowering()->isProfitableForNoCSROpt(F)) {
Mehdi Amini4beea662016-07-13 23:39:34 +0000176 ++NumCSROpt;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000177 LLVM_DEBUG(dbgs() << MF.getName()
178 << " function optimized for not having CSR.\n");
Mehdi Amini4beea662016-07-13 23:39:34 +0000179 }
Chad Rosier20e4d9e2016-06-15 21:14:02 +0000180
Matt Arsenault705e46f2019-07-05 23:33:43 +0000181 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) << " ";
185 }
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000186
Matt Arsenault705e46f2019-07-05 23:33:43 +0000187 dbgs() << " \n----------------------------------------\n";
188 );
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000189
Matthias Braun5c1e23b2018-07-26 00:27:51 +0000190 PRUI.storeUpdateRegUsageInfo(F, RegMask);
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000191
192 return false;
193}
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000194
195void RegUsageInfoCollector::
196computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) {
Matthias Braun5c1e23b2018-07-26 00:27:51 +0000197 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
198 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000199
200 // Target will return the set of registers that it saves/restores as needed.
201 SavedRegs.clear();
Matthias Braun5c1e23b2018-07-26 00:27:51 +0000202 TFI.determineCalleeSaves(MF, SavedRegs);
Matt Arsenault5630e3a2019-07-08 18:48:42 +0000203 if (SavedRegs.none())
204 return;
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000205
206 // Insert subregs.
Matthias Braun5c1e23b2018-07-26 00:27:51 +0000207 const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000208 for (unsigned i = 0; CSRegs[i]; ++i) {
Matt Arsenault5630e3a2019-07-08 18:48:42 +0000209 MCPhysReg Reg = CSRegs[i];
210 if (SavedRegs.test(Reg)) {
211 // Save subregisters
212 for (MCSubRegIterator SR(Reg, &TRI); SR.isValid(); ++SR)
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000213 SavedRegs.set(*SR);
Matthias Braunb7b58602018-08-29 23:12:42 +0000214 }
Jonas Paulsson7d484fa2018-05-25 08:42:02 +0000215 }
216}