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 getFunctionPropertiesInfo(const Function & F,const LoopInfo & LI)20FunctionPropertiesInfo::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 FPI.TopLevelLoopCount += llvm::size(LI); 57 return FPI; 58 } 59 print(raw_ostream & OS) const60void FunctionPropertiesInfo::print(raw_ostream &OS) const { 61 OS << "BasicBlockCount: " << BasicBlockCount << "\n" 62 << "BlocksReachedFromConditionalInstruction: " 63 << BlocksReachedFromConditionalInstruction << "\n" 64 << "Uses: " << Uses << "\n" 65 << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions 66 << "\n" 67 << "LoadInstCount: " << LoadInstCount << "\n" 68 << "StoreInstCount: " << StoreInstCount << "\n" 69 << "MaxLoopDepth: " << MaxLoopDepth << "\n" 70 << "TopLevelLoopCount: " << TopLevelLoopCount << "\n\n"; 71 } 72 73 AnalysisKey FunctionPropertiesAnalysis::Key; 74 75 FunctionPropertiesInfo run(Function & F,FunctionAnalysisManager & FAM)76FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { 77 return FunctionPropertiesInfo::getFunctionPropertiesInfo( 78 F, FAM.getResult<LoopAnalysis>(F)); 79 } 80 81 PreservedAnalyses run(Function & F,FunctionAnalysisManager & AM)82FunctionPropertiesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { 83 OS << "Printing analysis results of CFA for function " 84 << "'" << F.getName() << "':" 85 << "\n"; 86 AM.getResult<FunctionPropertiesAnalysis>(F).print(OS); 87 return PreservedAnalyses::all(); 88 }