From 82b049c004bb008ccf44adc630c3f525395f2023 Mon Sep 17 00:00:00 2001 From: Mikhail Bogomazov Date: Wed, 13 Oct 2021 15:38:26 +0000 Subject: [PATCH] working version of the first lab --- .../BogomazovCount/BogomazovCount.h | 14 +++++ llvm/lib/Passes/CMakeLists.txt | 1 + llvm/lib/Passes/PassBuilder.cpp | 1 + llvm/lib/Passes/PassRegistry.def | 2 + .../BogomazovCount/BogomazovCount.cpp | 62 +++++++++++++++++++ .../Transforms/BogomazovCount/CMakeLists.txt | 13 ++++ llvm/lib/Transforms/CMakeLists.txt | 1 + 7 files changed, 94 insertions(+) create mode 100644 llvm/include/llvm/Transforms/BogomazovCount/BogomazovCount.h create mode 100644 llvm/lib/Transforms/BogomazovCount/BogomazovCount.cpp create mode 100644 llvm/lib/Transforms/BogomazovCount/CMakeLists.txt diff --git a/llvm/include/llvm/Transforms/BogomazovCount/BogomazovCount.h b/llvm/include/llvm/Transforms/BogomazovCount/BogomazovCount.h new file mode 100644 index 0000000000000..3f03ba3fc7930 --- /dev/null +++ b/llvm/include/llvm/Transforms/BogomazovCount/BogomazovCount.h @@ -0,0 +1,14 @@ +#ifndef LLVM_TRANSFORMS_BOGOMAZOV_COUNT_H +#define LLVM_TRANSFORMS_BOGOMAZOV_COUNT_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { + + class BogomazovCountPass : public PassInfoMixin { + public: + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); + }; +} // namespace llvm + +#endif // LLVM_TRANSFORMS_Bogomazov_COUNT_H \ No newline at end of file diff --git a/llvm/lib/Passes/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt index 89e960f01e2eb..c235ccbcc13bf 100644 --- a/llvm/lib/Passes/CMakeLists.txt +++ b/llvm/lib/Passes/CMakeLists.txt @@ -25,4 +25,5 @@ add_llvm_component_library(LLVMPasses TransformUtils Vectorize Instrumentation + BogomazovCountPass ) diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index e118633c7ea61..da1caf2e71f3b 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -136,6 +136,7 @@ #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h" #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h" #include "llvm/Transforms/ObjCARC.h" +#include "llvm/Transforms/BogomazovCount/BogomazovCount.h" #include "llvm/Transforms/Scalar/ADCE.h" #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" #include "llvm/Transforms/Scalar/AnnotationRemarks.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index fef3aba69e25f..8453fdce3ceb0 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -209,6 +209,7 @@ FUNCTION_ALIAS_ANALYSIS("tbaa", TypeBasedAA()) #ifndef FUNCTION_PASS #define FUNCTION_PASS(NAME, CREATE_PASS) #endif +FUNCTION_PASS("BogomazovCount", BogomazovCountPass()) FUNCTION_PASS("aa-eval", AAEvaluator()) FUNCTION_PASS("adce", ADCEPass()) FUNCTION_PASS("add-discriminators", AddDiscriminatorsPass()) @@ -280,6 +281,7 @@ FUNCTION_PASS("objc-arc", ObjCARCOptPass()) FUNCTION_PASS("objc-arc-contract", ObjCARCContractPass()) FUNCTION_PASS("objc-arc-expand", ObjCARCExpandPass()) FUNCTION_PASS("pgo-memop-opt", PGOMemOPSizeOpt()) +FUNCTION_PASS("BogomazovCount", BogomazovCountPass()) FUNCTION_PASS("print", PrintFunctionPass(dbgs())) FUNCTION_PASS("print", AssumptionPrinterPass(dbgs())) FUNCTION_PASS("print", BlockFrequencyPrinterPass(dbgs())) diff --git a/llvm/lib/Transforms/BogomazovCount/BogomazovCount.cpp b/llvm/lib/Transforms/BogomazovCount/BogomazovCount.cpp new file mode 100644 index 0000000000000..e15de005328c4 --- /dev/null +++ b/llvm/lib/Transforms/BogomazovCount/BogomazovCount.cpp @@ -0,0 +1,62 @@ +#include "llvm/Transforms/BogomazovCount/BogomazovCount.h" + + +#include "llvm/ADT/Statistic.h" +#include "llvm/IR/InstIterator.h" +#include "llvm/Analysis/LoopInfo.h" + +#define DEBUG_TYPE "BogomazovCount" + + using namespace llvm; + +ALWAYS_ENABLED_STATISTIC(F_count, "Number of functions"); +ALWAYS_ENABLED_STATISTIC(BB_count, "BB amount"); +ALWAYS_ENABLED_STATISTIC(OPS_count, "OPS amount"); +ALWAYS_ENABLED_STATISTIC(loops_count, "Loops amount"); + + +PreservedAnalyses BogomazovCountPass::run(Function &F, FunctionAnalysisManager &AM) { + F_count ++; + + for (auto BB = F.begin(); BB != F.end(); ++BB) { + BB_count += 1; + for (auto I = BB->begin(); I != BB->end(); ++I) { + switch (I->getOpcode()) { + case Instruction::Add: + OPS_count += 1; + break; + case Instruction::FAdd: + OPS_count += 1; + break; + case Instruction::Mul: + OPS_count += 1; + break; + case Instruction::FDiv: + OPS_count += 1; + break; + case Instruction::SDiv: + OPS_count += 1; + break; + case Instruction::UDiv: + OPS_count += 1; + break; + case Instruction::Sub: + OPS_count += 1; + break; + case Instruction::FSub: + OPS_count += 1; + break; + } + } + } + + auto& LA = AM.getResult(F); + for (auto& L : LA) ++loops_count; + + errs() << "Functions: " << F_count << "\n" + << "BB: " << BB_count << "\n" + << "OPS: " << OPS_count << "\n" + << "Loops: " << loops_count << "\n"; + + return PreservedAnalyses::all(); +} \ No newline at end of file diff --git a/llvm/lib/Transforms/BogomazovCount/CMakeLists.txt b/llvm/lib/Transforms/BogomazovCount/CMakeLists.txt new file mode 100644 index 0000000000000..9e9680617b7ce --- /dev/null +++ b/llvm/lib/Transforms/BogomazovCount/CMakeLists.txt @@ -0,0 +1,13 @@ +add_llvm_component_library(LLVMBogomazovCountPass +BogomazovCount.cpp + +DEPENDS +intrinsics_gen + +COMPONENT_NAME +BogomazovCount + +LINK_COMPONENTS +Core +Support +) diff --git a/llvm/lib/Transforms/CMakeLists.txt b/llvm/lib/Transforms/CMakeLists.txt index dda5f6de11e32..8f3a7220e314a 100644 --- a/llvm/lib/Transforms/CMakeLists.txt +++ b/llvm/lib/Transforms/CMakeLists.txt @@ -9,3 +9,4 @@ add_subdirectory(Hello) add_subdirectory(ObjCARC) add_subdirectory(Coroutines) add_subdirectory(CFGuard) +add_subdirectory(BogomazovCount)