1 //===- FunctionPropertiesAnalysis.cpp - Function properties extraction ----===// 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 implements an analysis extracting function features, which may be 10 // used by ML-driven policies, for example. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/FunctionPropertiesAnalysis.h" 15 #include "llvm/IR/Instructions.h" 16 17 using namespace llvm; 18 19 AnalysisKey FunctionPropertiesAnalysis::Key; 20 21 FunctionPropertiesAnalysis::Result 22 FunctionPropertiesAnalysis::run(const Function &F, 23 FunctionAnalysisManager &FAM) { 24 Result Ret; 25 Ret.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses(); 26 for (const auto &BB : F) { 27 ++Ret.BasicBlockCount; 28 if (const auto *BI = dyn_cast<BranchInst>(BB.getTerminator())) { 29 if (BI->isConditional()) 30 Ret.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors(); 31 } else if (const auto *SI = dyn_cast<SwitchInst>(BB.getTerminator())) 32 Ret.BlocksReachedFromConditionalInstruction += 33 (SI->getNumCases() + (nullptr != SI->getDefaultDest())); 34 for (const auto &I : BB) 35 if (auto *CS = dyn_cast<CallBase>(&I)) { 36 const auto *Callee = CS->getCalledFunction(); 37 if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration()) 38 ++Ret.DirectCallsToDefinedFunctions; 39 } 40 } 41 return Ret; 42 }