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 //===----------------------------------------------------------------------===// 18 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" 30 31 using namespace llvm; 32 33 #define DEBUG_TYPE "ip-regalloc" 34 35 STATISTIC(NumCSROpt, 36 "Number of functions optimized for callee saved registers"); 37 38 namespace { 39 40 class RegUsageInfoCollector : public MachineFunctionPass { 41 public: 42 RegUsageInfoCollector() : MachineFunctionPass(ID) { 43 PassRegistry &Registry = *PassRegistry::getPassRegistry(); 44 initializeRegUsageInfoCollectorPass(Registry); 45 } 46 47 StringRef getPassName() const override { 48 return "Register Usage Information Collector Pass"; 49 } 50 51 void getAnalysisUsage(AnalysisUsage &AU) const override { 52 AU.addRequired<PhysicalRegisterUsageInfo>(); 53 AU.setPreservesAll(); 54 MachineFunctionPass::getAnalysisUsage(AU); 55 } 56 57 bool runOnMachineFunction(MachineFunction &MF) override; 58 59 // Call determineCalleeSaves and then also set the bits for subregs and 60 // fully saved superregs. 61 static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF); 62 63 static char ID; 64 }; 65 66 } // end of anonymous namespace 67 68 char RegUsageInfoCollector::ID = 0; 69 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) 75 76 FunctionPass *llvm::createRegUsageInfoCollector() { 77 return new RegUsageInfoCollector(); 78 } 79 80 // 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. 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_KERNEL: 92 return false; 93 default: 94 return true; 95 } 96 } 97 98 bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) { 99 MachineRegisterInfo *MRI = &MF.getRegInfo(); 100 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 101 const LLVMTargetMachine &TM = MF.getTarget(); 102 103 LLVM_DEBUG(dbgs() << " -------------------- " << getPassName() 104 << " -------------------- \nFunction Name : " 105 << MF.getName() << '\n'); 106 107 // Analyzing the register usage may be expensive on some targets. 108 if (!isCallableFunction(MF)) { 109 LLVM_DEBUG(dbgs() << "Not analyzing non-callable function\n"); 110 return false; 111 } 112 113 // If there are no callers, there's no point in computing more precise 114 // register usage here. 115 if (MF.getFunction().use_empty()) { 116 LLVM_DEBUG(dbgs() << "Not analyzing function with no callers\n"); 117 return false; 118 } 119 120 std::vector<uint32_t> RegMask; 121 122 // Compute the size of the bit vector to represent all the registers. 123 // The bit vector is broken into 32-bit chunks, thus takes the ceil of 124 // the number of registers divided by 32 for the size. 125 unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs()); 126 RegMask.resize(RegMaskSize, ~((uint32_t)0)); 127 128 const Function &F = MF.getFunction(); 129 130 PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>(); 131 PRUI.setTargetMachine(TM); 132 133 LLVM_DEBUG(dbgs() << "Clobbered Registers: "); 134 135 BitVector SavedRegs; 136 computeCalleeSavedRegs(SavedRegs, MF); 137 138 const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask(); 139 auto SetRegAsDefined = [&RegMask] (unsigned Reg) { 140 RegMask[Reg / 32] &= ~(1u << Reg % 32); 141 }; 142 // Scan all the physical registers. When a register is defined in the current 143 // function set it and all the aliasing registers as defined in the regmask. 144 // FIXME: Rewrite to use regunits. 145 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) { 146 // Don't count registers that are saved and restored. 147 if (SavedRegs.test(PReg)) 148 continue; 149 // If a register is defined by an instruction mark it as defined together 150 // with all it's unsaved aliases. 151 if (!MRI->def_empty(PReg)) { 152 for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI) 153 if (!SavedRegs.test(*AI)) 154 SetRegAsDefined(*AI); 155 continue; 156 } 157 // If a register is in the UsedPhysRegsMask set then mark it as defined. 158 // All clobbered aliases will also be in the set, so we can skip setting 159 // as defined all the aliases here. 160 if (UsedPhysRegsMask.test(PReg)) 161 SetRegAsDefined(PReg); 162 } 163 164 if (TargetFrameLowering::isSafeForNoCSROpt(F)) { 165 ++NumCSROpt; 166 LLVM_DEBUG(dbgs() << MF.getName() 167 << " function optimized for not having CSR.\n"); 168 } 169 170 LLVM_DEBUG( 171 for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) { 172 if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg)) 173 dbgs() << printReg(PReg, TRI) << " "; 174 } 175 176 dbgs() << " \n----------------------------------------\n"; 177 ); 178 179 PRUI.storeUpdateRegUsageInfo(F, RegMask); 180 181 return false; 182 } 183 184 void RegUsageInfoCollector:: 185 computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) { 186 const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering(); 187 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 188 189 // Target will return the set of registers that it saves/restores as needed. 190 SavedRegs.clear(); 191 TFI.determineCalleeSaves(MF, SavedRegs); 192 193 // Insert subregs. 194 const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF); 195 for (unsigned i = 0; CSRegs[i]; ++i) { 196 unsigned Reg = CSRegs[i]; 197 if (SavedRegs.test(Reg)) 198 for (MCSubRegIterator SR(Reg, &TRI, false); SR.isValid(); ++SR) 199 SavedRegs.set(*SR); 200 } 201 202 // Insert any register fully saved via subregisters. 203 // FIXME: Rewrite to use regunits. 204 for (const TargetRegisterClass *RC : TRI.regclasses()) { 205 if (!RC->CoveredBySubRegs) 206 continue; 207 208 for (unsigned PReg = 1, PRegE = TRI.getNumRegs(); PReg < PRegE; ++PReg) { 209 if (SavedRegs.test(PReg)) 210 continue; 211 212 // Check if PReg is fully covered by its subregs. 213 if (!RC->contains(PReg)) 214 continue; 215 216 // Add PReg to SavedRegs if all subregs are saved. 217 bool AllSubRegsSaved = true; 218 bool HasAtLeastOneSubreg = false; 219 for (MCSubRegIterator SR(PReg, &TRI, false); SR.isValid(); ++SR) { 220 HasAtLeastOneSubreg = true; 221 if (!SavedRegs.test(*SR)) { 222 AllSubRegsSaved = false; 223 break; 224 } 225 } 226 if (AllSubRegsSaved && HasAtLeastOneSubreg) 227 SavedRegs.set(PReg); 228 } 229 } 230 } 231