1 //===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis
10 // classes used to extract function properties.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
15 #include "llvm/IR/Instructions.h"
19 FunctionPropertiesInfo
20 FunctionPropertiesInfo::getFunctionPropertiesInfo(const Function &F,
23 FunctionPropertiesInfo FPI;
25 FPI.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses();
27 for (const auto &BB : F) {
28 ++FPI.BasicBlockCount;
30 if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
31 if (BI->isConditional())
32 FPI.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors();
33 } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
34 FPI.BlocksReachedFromConditionalInstruction +=
35 (SI->getNumCases() + (nullptr != SI->getDefaultDest()));
38 for (const auto &I : BB) {
39 if (auto *CS = dyn_cast<CallBase>(&I)) {
40 const auto *Callee = CS->getCalledFunction();
41 if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration())
42 ++FPI.DirectCallsToDefinedFunctions;
44 if (I.getOpcode() == Instruction::Load) {
46 } else if (I.getOpcode() == Instruction::Store) {
50 // Loop Depth of the Basic Block
52 LoopDepth = LI.getLoopDepth(&BB);
53 if (FPI.MaxLoopDepth < LoopDepth)
54 FPI.MaxLoopDepth = LoopDepth;
56 FPI.TopLevelLoopCount += llvm::size(LI);
60 void FunctionPropertiesInfo::print(raw_ostream &OS) const {
61 OS << "BasicBlockCount: " << BasicBlockCount << "\n"
62 << "BlocksReachedFromConditionalInstruction: "
63 << BlocksReachedFromConditionalInstruction << "\n"
64 << "Uses: " << Uses << "\n"
65 << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions
67 << "LoadInstCount: " << LoadInstCount << "\n"
68 << "StoreInstCount: " << StoreInstCount << "\n"
69 << "MaxLoopDepth: " << MaxLoopDepth << "\n"
70 << "TopLevelLoopCount: " << TopLevelLoopCount << "\n\n";
73 AnalysisKey FunctionPropertiesAnalysis::Key;
75 FunctionPropertiesInfo
76 FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
77 return FunctionPropertiesInfo::getFunctionPropertiesInfo(
78 F, FAM.getResult<LoopAnalysis>(F));
82 FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
83 OS << "Printing analysis results of CFA for function "
84 << "'" << F.getName() << "':"
86 AM.getResult<FunctionPropertiesAnalysis>(F).print(OS);
87 return PreservedAnalyses::all();