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