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
21namespace llvm {
22
23class GCNSubtarget;
24class MachineFunction;
25class TargetMachine;
26
27struct AMDGPUResourceUsageAnalysis : public ModulePass {
28 static char ID;
29
30public:
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() : ModulePass(ID) {}
54
55 bool doInitialization(Module &M) override {
56 CallGraphResourceInfo.clear();
57 return ModulePass::doInitialization(M);
58 }
59
60 bool runOnModule(Module &M) override;
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(Val: F);
69 assert(Info != CallGraphResourceInfo.end() &&
70 "Failed to find resource info for function");
71 return Info->getSecond();
72 }
73
74private:
75 SIFunctionResourceInfo
76 analyzeResourceUsage(const MachineFunction &MF, const TargetMachine &TM,
77 uint32_t AssumedStackSizeForDynamicSizeObjects,
78 uint32_t AssumedStackSizeForExternalCall) const;
79 void propagateIndirectCallRegisterUsage();
80
81 DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo;
82};
83} // namespace llvm
84#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPURESOURCEUSAGEANALYSIS_H
85