Index: llvm/include/llvm/Analysis/ML/FunctionPropertiesAnalysis.h =================================================================== --- llvm/include/llvm/Analysis/ML/FunctionPropertiesAnalysis.h +++ llvm/include/llvm/Analysis/ML/FunctionPropertiesAnalysis.h @@ -1,3 +1,16 @@ +//==- FunctionPropertiesAnalysis.h - Function Properties Analysis -*-C++ -*-==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis +// classes used to extract function properties. +// +//===----------------------------------------------------------------------===// + #ifndef LLVM_FUNCTIONPROPERTIESANALYSIS_H_ #define LLVM_FUNCTIONPROPERTIESANALYSIS_H_ @@ -6,31 +19,40 @@ namespace llvm { class Function; +class FunctionPropertiesInfo { +public: + /// Number of basic blocks + int64_t BasicBlockCount = 0; + + /// Number of blocks reached from a conditional instruction, or that are + /// 'cases' of a SwitchInstr. + // FIXME: We may want to replace this with a more meaningful metric, like + // number of conditionally executed blocks: + // 'if (a) s();' would be counted here as 2 blocks, just like + // 'if (a) s(); else s2(); s3();' would. + int64_t BlocksReachedFromConditionalInstruction = 0; + + /// Number of uses of this function, plus 1 if the function is callable + /// outside the module. + int64_t Uses = 0; + + /// Number of direct calls made from this function to other functions + /// defined in this module. + int64_t DirectCallsToDefinedFunctions = 0; + + static FunctionPropertiesInfo getFunctionPropertiesInfo(const Function &F); +}; + +// Analysis pass class FunctionPropertiesAnalysis : public AnalysisInfoMixin { + public: static AnalysisKey Key; - struct Result { - /// Number of basic blocks - int64_t BasicBlockCount = 0; - - /// Number of blocks reached from a conditional instruction, or that are - /// 'cases' of a SwitchInstr. - // FIXME: We may want to replace this with a more meaningful metric, like - // number of conditionally executed blocks: - // 'if (a) s();' would be counted here as 2 blocks, just like - // 'if (a) s(); else s2(); s3();' would. - int64_t BlocksReachedFromConditionalInstruction = 0; - - /// Number of uses of this function, plus 1 if the function is callable - /// outside the module. - int64_t Uses = 0; - - /// Number of direct calls made from this function to other functions - /// defined in this module. - int64_t DirectCallsToDefinedFunctions = 0; - }; - Result run(const Function &F, FunctionAnalysisManager &FAM); + + using Result = FunctionPropertiesInfo; + + Result run(Function &F, FunctionAnalysisManager &FAM); }; } // namespace llvm Index: llvm/lib/Analysis/ML/FunctionPropertiesAnalysis.cpp =================================================================== --- llvm/lib/Analysis/ML/FunctionPropertiesAnalysis.cpp +++ llvm/lib/Analysis/ML/FunctionPropertiesAnalysis.cpp @@ -1,28 +1,56 @@ +//===- FunctionPropertiesAnalysis.cpp - Function Properties Analysis ------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines the FunctionPropertiesInfo and FunctionPropertiesAnalysis +// classes used to extract function properties. +// +//===----------------------------------------------------------------------===// + #include "llvm/Analysis/ML/FunctionPropertiesAnalysis.h" #include "llvm/IR/Instructions.h" +#include "llvm/Passes/PassBuilder.h" +#include "llvm/Passes/PassPlugin.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; -AnalysisKey FunctionPropertiesAnalysis::Key; +FunctionPropertiesInfo +FunctionPropertiesInfo::getFunctionPropertiesInfo(const Function &F) { + + FunctionPropertiesInfo FPI; + + FPI.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses(); -FunctionPropertiesAnalysis::Result -FunctionPropertiesAnalysis::run(const Function &F, FunctionAnalysisManager &FAM) { - Result Ret; - Ret.Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses(); for (const auto &BB : F) { - ++Ret.BasicBlockCount; + ++FPI.BasicBlockCount; + if (const auto *BI = dyn_cast(BB.getTerminator())) { if (BI->isConditional()) - Ret.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors(); - } else if (const auto *SI = dyn_cast(BB.getTerminator())) - Ret.BlocksReachedFromConditionalInstruction += + FPI.BlocksReachedFromConditionalInstruction += BI->getNumSuccessors(); + } else if (const auto *SI = dyn_cast(BB.getTerminator())) { + FPI.BlocksReachedFromConditionalInstruction += (SI->getNumCases() + (nullptr != SI->getDefaultDest())); - for (const auto &I : BB) + } + + for (const auto &I : BB) { if (auto *CS = dyn_cast(&I)) { const auto *Callee = CS->getCalledFunction(); if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration()) - ++Ret.DirectCallsToDefinedFunctions; + ++FPI.DirectCallsToDefinedFunctions; } + } } - return Ret; -} \ No newline at end of file + return FPI; +} + +AnalysisKey FunctionPropertiesAnalysis::Key; + +FunctionPropertiesInfo +FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { + return FunctionPropertiesInfo::getFunctionPropertiesInfo(F); +} Index: llvm/unittests/Analysis/ML/FunctionPropertiesAnalysisTest.cpp =================================================================== --- llvm/unittests/Analysis/ML/FunctionPropertiesAnalysisTest.cpp +++ llvm/unittests/Analysis/ML/FunctionPropertiesAnalysisTest.cpp @@ -1,4 +1,4 @@ -//===- FunctionPropertiesAnalysisTest.cpp - function properties unit tests --------===// +//==- FunctionPropertiesAnalysisTest.cpp - Function Properties Unit Tests -==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information.