1 //===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===// 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 // This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis 10 // classes used to extract function properties. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/FunctionPropertiesAnalysis.h" 15 #include "llvm/IR/Instructions.h" 16 17 using namespace llvm; 18 19 FunctionPropertiesInfo 20 FunctionPropertiesInfo::getFunctionPropertiesInfo(const Function &F, 21 const LoopInfo &LI) { 22 23 FunctionPropertiesInfo FPI; 24 25 FPI.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses(); 26 27 for (const auto &BB : F) { 28 ++FPI.BasicBlockCount; 29 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())); 36 } 37 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; 43 } 44 if (I.getOpcode() == Instruction::Load) { 45 ++FPI.LoadInstCount; 46 } else if (I.getOpcode() == Instruction::Store) { 47 ++FPI.StoreInstCount; 48 } 49 } 50 // Loop Depth of the Basic Block 51 int64_t LoopDepth; 52 LoopDepth = LI.getLoopDepth(&BB); 53 if (FPI.MaxLoopDepth < LoopDepth) 54 FPI.MaxLoopDepth = LoopDepth; 55 } 56 for (Loop *L : LI) { 57 ++FPI.TopLevelLoopCount; 58 } 59 return FPI; 60 } 61 62 void FunctionPropertiesInfo::print(raw_ostream &OS) const { 63 OS << "BasicBlockCount: " << BasicBlockCount << "\n" 64 << "BlocksReachedFromConditionalInstruction: " 65 << BlocksReachedFromConditionalInstruction << "\n" 66 << "Uses: " << Uses << "\n" 67 << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions 68 << "\n" 69 << "LoadInstCount: " << LoadInstCount << "\n" 70 << "StoreInstCount: " << StoreInstCount << "\n" 71 << "MaxLoopDepth: " << MaxLoopDepth << "\n" 72 << "TopLevelLoopCount: " << TopLevelLoopCount << "\n\n"; 73 } 74 75 AnalysisKey FunctionPropertiesAnalysis::Key; 76 77 FunctionPropertiesInfo 78 FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { 79 return FunctionPropertiesInfo::getFunctionPropertiesInfo( 80 F, FAM.getResult<LoopAnalysis>(F)); 81 } 82 83 PreservedAnalyses 84 FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { 85 OS << "Printing analysis results of CFA for function " 86 << "'" << F.getName() << "':" 87 << "\n"; 88 AM.getResult<FunctionPropertiesAnalysis>(F).print(OS); 89 return PreservedAnalyses::all(); 90 }