Index: llvm/include/llvm/Analysis/ML/CodeFeaturesAnalysis.h =================================================================== --- /dev/null +++ llvm/include/llvm/Analysis/ML/CodeFeaturesAnalysis.h @@ -0,0 +1,59 @@ +#ifndef LLVM_CODEFEATURESANALYSIS_H_ +#define LLVM_CODEFEATURESANALYSIS_H_ + +#include "llvm/IR/PassManager.h" + +namespace llvm { +class Function; + +class CodeFeaturesInfo{ + 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; + + void calculate(const Function &F); + + void print(raw_ostream &OS) const; +}; + +//Analysis pass +class CodeFeaturesAnalysis + : public AnalysisInfoMixin { + + public: + static AnalysisKey Key; + + using Result = CodeFeaturesInfo; + + Result run(const Function &F, FunctionAnalysisManager &FAM); +}; + +/// Printer pass for the CodeFeaturesAnalysis results. +class CodeFeaturesPrinterPass + : public PassInfoMixin { + raw_ostream &OS; + +public: + explicit CodeFeaturesPrinterPass(raw_ostream &OS) : OS(OS) {} + + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; + +} // namespace llvm +#endif // LLVM_CODEFEATURESANALYSIS_H_ \ No newline at end of file Index: llvm/lib/Analysis/ML/CMakeLists.txt =================================================================== --- llvm/lib/Analysis/ML/CMakeLists.txt +++ llvm/lib/Analysis/ML/CMakeLists.txt @@ -1,4 +1,5 @@ add_llvm_component_library(LLVMMLPolicies + CodeFeaturesAnalysis.cpp InlineFeaturesAnalysis.cpp DEPENDS Index: llvm/lib/Analysis/ML/CodeFeaturesAnalysis.cpp =================================================================== --- /dev/null +++ llvm/lib/Analysis/ML/CodeFeaturesAnalysis.cpp @@ -0,0 +1,49 @@ +#include "llvm/Analysis/ML/CodeFeaturesAnalysis.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; + +void CodeFeaturesInfo::calculate(const Function &F) { + Uses = ((!F.hasLocalLinkage()) ? 1 : 0) + F.getNumUses(); + for (const auto &BB : F) { + ++BasicBlockCount; + if (const auto *BI = dyn_cast(BB.getTerminator())) { + if (BI->isConditional()) + BlocksReachedFromConditionalInstruction += BI->getNumSuccessors(); + } else if (const auto *SI = dyn_cast(BB.getTerminator())) + BlocksReachedFromConditionalInstruction += + (SI->getNumCases() + (nullptr != SI->getDefaultDest())); + for (const auto &I : BB) + if (auto *CS = dyn_cast(&I)) { + const auto *Callee = CS->getCalledFunction(); + if (Callee && !Callee->isIntrinsic() && !Callee->isDeclaration()) + ++DirectCallsToDefinedFunctions; + } + } +} + +void CodeFeaturesInfo::print(raw_ostream &OS) const { + OS << "BasicBlockCount: " << BasicBlockCount << "\n" + << "BlocksReachedFromConditionalInstruction: " << BlocksReachedFromConditionalInstruction << "\n" + << "Uses: " << Uses << "\n" + << "DirectCallsToDefinedFunctions: " << DirectCallsToDefinedFunctions << "\n\n"; +} + +AnalysisKey CodeFeaturesAnalysis::Key; + +CodeFeaturesInfo CodeFeaturesAnalysis::run(const Function &F, FunctionAnalysisManager &FAM) { + CodeFeaturesInfo CFI; + CFI.calculate(F); + return CFI; +} + +PreservedAnalyses CodeFeaturesPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { + OS << "Printing analysis results of CFA for function " + << "'" << F.getName() << "':" + << "\n"; + AM.getResult(F).print(OS); + return PreservedAnalyses::all(); +} Index: llvm/lib/Passes/PassBuilder.cpp =================================================================== --- llvm/lib/Passes/PassBuilder.cpp +++ llvm/lib/Passes/PassBuilder.cpp @@ -40,6 +40,7 @@ #include "llvm/Analysis/LoopCacheAnalysis.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopNestAnalysis.h" +#include "llvm/Analysis/ML/CodeFeaturesAnalysis.h" #include "llvm/Analysis/ML/InlineFeaturesAnalysis.h" #include "llvm/Analysis/MemoryDependenceAnalysis.h" #include "llvm/Analysis/MemorySSA.h" Index: llvm/lib/Passes/PassRegistry.def =================================================================== --- llvm/lib/Passes/PassRegistry.def +++ llvm/lib/Passes/PassRegistry.def @@ -126,6 +126,7 @@ FUNCTION_ANALYSIS("assumptions", AssumptionAnalysis()) FUNCTION_ANALYSIS("block-freq", BlockFrequencyAnalysis()) FUNCTION_ANALYSIS("branch-prob", BranchProbabilityAnalysis()) +FUNCTION_ANALYSIS("code-features", CodeFeaturesAnalysis()) FUNCTION_ANALYSIS("domtree", DominatorTreeAnalysis()) FUNCTION_ANALYSIS("postdomtree", PostDominatorTreeAnalysis()) FUNCTION_ANALYSIS("demanded-bits", DemandedBitsAnalysis()) @@ -228,6 +229,7 @@ FUNCTION_PASS("print", AssumptionPrinterPass(dbgs())) FUNCTION_PASS("print", BlockFrequencyPrinterPass(dbgs())) FUNCTION_PASS("print", BranchProbabilityPrinterPass(dbgs())) +FUNCTION_PASS("print", CodeFeaturesPrinterPass(dbgs())) FUNCTION_PASS("print", DependenceAnalysisPrinterPass(dbgs())) FUNCTION_PASS("print", DominatorTreePrinterPass(dbgs())) FUNCTION_PASS("print", PostDominatorTreePrinterPass(dbgs()))