xref: /freebsd-src/contrib/llvm-project/llvm/lib/Analysis/FunctionPropertiesAnalysis.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric //
9*e8d8bef9SDimitry Andric // This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
10*e8d8bef9SDimitry Andric // classes used to extract function properties.
11*e8d8bef9SDimitry Andric //
12*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13*e8d8bef9SDimitry Andric 
14*e8d8bef9SDimitry Andric #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
15*e8d8bef9SDimitry Andric #include "llvm/IR/Instructions.h"
16*e8d8bef9SDimitry Andric 
17*e8d8bef9SDimitry Andric using namespace llvm;
18*e8d8bef9SDimitry Andric 
19*e8d8bef9SDimitry Andric FunctionPropertiesInfo
20*e8d8bef9SDimitry Andric FunctionPropertiesInfo::getFunctionPropertiesInfo(const Function &F,
21*e8d8bef9SDimitry Andric                                                   const LoopInfo &LI) {
22*e8d8bef9SDimitry Andric 
23*e8d8bef9SDimitry Andric   FunctionPropertiesInfo FPI;
24*e8d8bef9SDimitry Andric 
25*e8d8bef9SDimitry Andric   FPI.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
26*e8d8bef9SDimitry Andric 
27*e8d8bef9SDimitry Andric   for (const auto &BB : F) {
28*e8d8bef9SDimitry Andric     ++FPI.BasicBlockCount;
29*e8d8bef9SDimitry Andric 
30*e8d8bef9SDimitry Andric     if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
31*e8d8bef9SDimitry Andric       if (BI->isConditional())
32*e8d8bef9SDimitry Andric         FPI.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors();
33*e8d8bef9SDimitry Andric     } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
34*e8d8bef9SDimitry Andric       FPI.BlocksReachedFromConditionalInstruction +=
35*e8d8bef9SDimitry Andric           (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
36*e8d8bef9SDimitry Andric     }
37*e8d8bef9SDimitry Andric 
38*e8d8bef9SDimitry Andric     for (const auto &I : BB) {
39*e8d8bef9SDimitry Andric       if (auto *CS = dyn_cast<CallBase>(&I)) {
40*e8d8bef9SDimitry Andric         const auto *Callee = CS->getCalledFunction();
41*e8d8bef9SDimitry Andric         if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
42*e8d8bef9SDimitry Andric           ++FPI.DirectCallsToDefinedFunctions;
43*e8d8bef9SDimitry Andric       }
44*e8d8bef9SDimitry Andric       if (I.getOpcode() == Instruction::Load) {
45*e8d8bef9SDimitry Andric         ++FPI.LoadInstCount;
46*e8d8bef9SDimitry Andric       } else if (I.getOpcode() == Instruction::Store) {
47*e8d8bef9SDimitry Andric         ++FPI.StoreInstCount;
48*e8d8bef9SDimitry Andric       }
49*e8d8bef9SDimitry Andric     }
50*e8d8bef9SDimitry Andric     // Loop Depth of the Basic Block
51*e8d8bef9SDimitry Andric     int64_t LoopDepth;
52*e8d8bef9SDimitry Andric     LoopDepth = LI.getLoopDepth(&BB);
53*e8d8bef9SDimitry Andric     if (FPI.MaxLoopDepth < LoopDepth)
54*e8d8bef9SDimitry Andric       FPI.MaxLoopDepth = LoopDepth;
55*e8d8bef9SDimitry Andric   }
56*e8d8bef9SDimitry Andric   FPI.TopLevelLoopCount += llvm::size(LI);
57*e8d8bef9SDimitry Andric   return FPI;
58*e8d8bef9SDimitry Andric }
59*e8d8bef9SDimitry Andric 
60*e8d8bef9SDimitry Andric void FunctionPropertiesInfo::print(raw_ostream &OS) const {
61*e8d8bef9SDimitry Andric   OS << "BasicBlockCount: " << BasicBlockCount << "\n"
62*e8d8bef9SDimitry Andric      << "BlocksReachedFromConditionalInstruction: "
63*e8d8bef9SDimitry Andric      << BlocksReachedFromConditionalInstruction << "\n"
64*e8d8bef9SDimitry Andric      << "Uses: " << Uses << "\n"
65*e8d8bef9SDimitry Andric      << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions
66*e8d8bef9SDimitry Andric      << "\n"
67*e8d8bef9SDimitry Andric      << "LoadInstCount: " << LoadInstCount << "\n"
68*e8d8bef9SDimitry Andric      << "StoreInstCount: " << StoreInstCount << "\n"
69*e8d8bef9SDimitry Andric      << "MaxLoopDepth: " << MaxLoopDepth << "\n"
70*e8d8bef9SDimitry Andric      << "TopLevelLoopCount: " << TopLevelLoopCount << "\n\n";
71*e8d8bef9SDimitry Andric }
72*e8d8bef9SDimitry Andric 
73*e8d8bef9SDimitry Andric AnalysisKey FunctionPropertiesAnalysis::Key;
74*e8d8bef9SDimitry Andric 
75*e8d8bef9SDimitry Andric FunctionPropertiesInfo
76*e8d8bef9SDimitry Andric FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
77*e8d8bef9SDimitry Andric   return FunctionPropertiesInfo::getFunctionPropertiesInfo(
78*e8d8bef9SDimitry Andric       F, FAM.getResult<LoopAnalysis>(F));
79*e8d8bef9SDimitry Andric }
80*e8d8bef9SDimitry Andric 
81*e8d8bef9SDimitry Andric PreservedAnalyses
82*e8d8bef9SDimitry Andric FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
83*e8d8bef9SDimitry Andric   OS << "Printing analysis results of CFA for function "
84*e8d8bef9SDimitry Andric      << "'" << F.getName() << "':"
85*e8d8bef9SDimitry Andric      << "\n";
86*e8d8bef9SDimitry Andric   AM.getResult<FunctionPropertiesAnalysis>(F).print(OS);
87*e8d8bef9SDimitry Andric   return PreservedAnalyses::all();
88*e8d8bef9SDimitry Andric }
89