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     // Total number of VGPRs is actually a combination of AGPR and VGPR
47     // depending on architecture - and some alignment constraints
48     int32_t getTotalNumVGPRs(const GCNSubtarget &ST, int32_t NumAGPR,
49                              int32_t NumVGPR) const;
50     int32_t getTotalNumVGPRs(const GCNSubtarget &ST) const;
51   };
52 
53   AMDGPUResourceUsageAnalysis() : CallGraphSCCPass(ID) {}
54 
55   bool runOnSCC(CallGraphSCC &SCC) override;
56 
57   bool doInitialization(CallGraph &CG) override {
58     CallGraphResourceInfo.clear();
59     return CallGraphSCCPass::doInitialization(CG);
60   }
61 
62   void getAnalysisUsage(AnalysisUsage &AU) const override {
63     AU.addRequired<MachineModuleInfoWrapperPass>();
64     AU.setPreservesAll();
65   }
66 
67   const SIFunctionResourceInfo &getResourceInfo(const Function *F) const {
68     auto Info = CallGraphResourceInfo.find(F);
69     assert(Info != CallGraphResourceInfo.end() &&
70            "Failed to find resource info for function");
71     return Info->getSecond();
72   }
73 
74 private:
75   SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF,
76                                               const TargetMachine &TM) const;
77   void propagateIndirectCallRegisterUsage();
78 
79   DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;
80 };
81 } // namespace llvm
82 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPURESOURCEUSAGEANALYSIS_H
83