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,11 +19,8 @@ namespace llvm { class Function; -class FunctionPropertiesAnalysis - : public AnalysisInfoMixin { -public: - static AnalysisKey Key; - struct Result { +class FunctionPropertiesInfo{ + public: /// Number of basic blocks int64_t BasicBlockCount = 0; @@ -29,9 +39,21 @@ /// 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); + + void analyze(const Function &F); +}; + +//Analysis pass +class FunctionPropertiesAnalysis + : public AnalysisInfoMixin { + + public: + static AnalysisKey Key; + + using Result = FunctionPropertiesInfo; + + Result run(Function &F, FunctionAnalysisManager &FAM); }; } // namespace llvm -#endif // LLVM_FUNCTIONPROPERTIESANALYSIS_H_ \ No newline at end of file +#endif // LLVM_FUNCTIONPROPERTIESANALYSIS_H_ \ No newline at end of file Index: llvm/lib/Analysis/ML/FunctionPropertiesAnalysis.cpp =================================================================== --- llvm/lib/Analysis/ML/FunctionPropertiesAnalysis.cpp +++ llvm/lib/Analysis/ML/FunctionPropertiesAnalysis.cpp @@ -1,28 +1,53 @@ +//===- 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; +void FunctionPropertiesInfo::analyze(const Function &F) { + + 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; + ++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 += + BlocksReachedFromConditionalInstruction += BI->getNumSuccessors(); + } else if (const auto *SI = dyn_cast(BB.getTerminator())) { + 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; + ++DirectCallsToDefinedFunctions; } + } } - return Ret; -} \ No newline at end of file +} + +AnalysisKey FunctionPropertiesAnalysis::Key; + +FunctionPropertiesInfo FunctionPropertiesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) { + FunctionPropertiesInfo FPI; + FPI.analyze(F); + return FPI; +} 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.