1 //===- AMDGPUResourceUsageAnalysis.h ---- analysis of resources -*- C++ -*-===// 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 /// \file 10 /// \brief Analyzes how many registers and other resources are used by 11 /// functions. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPURESOURCEUSAGEANALYSIS_H 16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPURESOURCEUSAGEANALYSIS_H 17 18 #include "llvm/Analysis/CallGraphSCCPass.h" 19 #include "llvm/CodeGen/MachineModuleInfo.h" 20 21 namespace llvm { 22 23 class GCNSubtarget; 24 class MachineFunction; 25 class TargetMachine; 26 27 struct AMDGPUResourceUsageAnalysis : public CallGraphSCCPass { 28 static char ID; 29 30 public: 31 // Track resource usage for callee functions. 32 struct SIFunctionResourceInfo { 33 // Track the number of explicitly used VGPRs. Special registers reserved at 34 // the end are tracked separately. 35 int32_t NumVGPR = 0; 36 int32_t NumAGPR = 0; 37 int32_t NumExplicitSGPR = 0; 38 uint64_t PrivateSegmentSize = 0; 39 bool UsesVCC = false; 40 bool UsesFlatScratch = false; 41 bool HasDynamicallySizedStack = false; 42 bool HasRecursion = false; 43 bool HasIndirectCall = false; 44 45 int32_t getTotalNumSGPRs(const GCNSubtarget &ST) const; 46 int32_t getTotalNumVGPRs(const GCNSubtarget &ST) const; 47 }; 48 49 AMDGPUResourceUsageAnalysis() : CallGraphSCCPass(ID) {} 50 51 bool runOnSCC(CallGraphSCC &SCC) override; 52 53 bool doInitialization(CallGraph &CG) override { 54 CallGraphResourceInfo.clear(); 55 return CallGraphSCCPass::doInitialization(CG); 56 } 57 58 void getAnalysisUsage(AnalysisUsage &AU) const override { 59 AU.addRequired<MachineModuleInfoWrapperPass>(); 60 AU.setPreservesAll(); 61 } 62 63 const SIFunctionResourceInfo &getResourceInfo(const Function *F) const { 64 auto Info = CallGraphResourceInfo.find(F); 65 assert(Info != CallGraphResourceInfo.end() && 66 "Failed to find resource info for function"); 67 return Info->getSecond(); 68 } 69 70 private: 71 SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF, 72 const TargetMachine &TM) const; 73 void propagateIndirectCallRegisterUsage(); 74 75 DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo; 76 }; 77 } // namespace llvm 78 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPURESOURCEUSAGEANALYSIS_H 79