Skip to content

Commit 82b049c

Browse files
committed
working version of the first lab
1 parent 4f3a92c commit 82b049c

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef LLVM_TRANSFORMS_BOGOMAZOV_COUNT_H
2+
#define LLVM_TRANSFORMS_BOGOMAZOV_COUNT_H
3+
4+
#include "llvm/IR/PassManager.h"
5+
6+
namespace llvm {
7+
8+
class BogomazovCountPass : public PassInfoMixin<BogomazovCountPass> {
9+
public:
10+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
11+
};
12+
} // namespace llvm
13+
14+
#endif // LLVM_TRANSFORMS_Bogomazov_COUNT_H

llvm/lib/Passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ add_llvm_component_library(LLVMPasses
2525
TransformUtils
2626
Vectorize
2727
Instrumentation
28+
BogomazovCountPass
2829
)

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
137137
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
138138
#include "llvm/Transforms/ObjCARC.h"
139+
#include "llvm/Transforms/BogomazovCount/BogomazovCount.h"
139140
#include "llvm/Transforms/Scalar/ADCE.h"
140141
#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
141142
#include "llvm/Transforms/Scalar/AnnotationRemarks.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ FUNCTION_ALIAS_ANALYSIS("tbaa", TypeBasedAA())
209209
#ifndef FUNCTION_PASS
210210
#define FUNCTION_PASS(NAME, CREATE_PASS)
211211
#endif
212+
FUNCTION_PASS("BogomazovCount", BogomazovCountPass())
212213
FUNCTION_PASS("aa-eval", AAEvaluator())
213214
FUNCTION_PASS("adce", ADCEPass())
214215
FUNCTION_PASS("add-discriminators", AddDiscriminatorsPass())
@@ -280,6 +281,7 @@ FUNCTION_PASS("objc-arc", ObjCARCOptPass())
280281
FUNCTION_PASS("objc-arc-contract", ObjCARCContractPass())
281282
FUNCTION_PASS("objc-arc-expand", ObjCARCExpandPass())
282283
FUNCTION_PASS("pgo-memop-opt", PGOMemOPSizeOpt())
284+
FUNCTION_PASS("BogomazovCount", BogomazovCountPass())
283285
FUNCTION_PASS("print", PrintFunctionPass(dbgs()))
284286
FUNCTION_PASS("print<assumptions>", AssumptionPrinterPass(dbgs()))
285287
FUNCTION_PASS("print<block-freq>", BlockFrequencyPrinterPass(dbgs()))
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "llvm/Transforms/BogomazovCount/BogomazovCount.h"
2+
3+
4+
#include "llvm/ADT/Statistic.h"
5+
#include "llvm/IR/InstIterator.h"
6+
#include "llvm/Analysis/LoopInfo.h"
7+
8+
#define DEBUG_TYPE "BogomazovCount"
9+
10+
using namespace llvm;
11+
12+
ALWAYS_ENABLED_STATISTIC(F_count, "Number of functions");
13+
ALWAYS_ENABLED_STATISTIC(BB_count, "BB amount");
14+
ALWAYS_ENABLED_STATISTIC(OPS_count, "OPS amount");
15+
ALWAYS_ENABLED_STATISTIC(loops_count, "Loops amount");
16+
17+
18+
PreservedAnalyses BogomazovCountPass::run(Function &F, FunctionAnalysisManager &AM) {
19+
F_count ++;
20+
21+
for (auto BB = F.begin(); BB != F.end(); ++BB) {
22+
BB_count += 1;
23+
for (auto I = BB->begin(); I != BB->end(); ++I) {
24+
switch (I->getOpcode()) {
25+
case Instruction::Add:
26+
OPS_count += 1;
27+
break;
28+
case Instruction::FAdd:
29+
OPS_count += 1;
30+
break;
31+
case Instruction::Mul:
32+
OPS_count += 1;
33+
break;
34+
case Instruction::FDiv:
35+
OPS_count += 1;
36+
break;
37+
case Instruction::SDiv:
38+
OPS_count += 1;
39+
break;
40+
case Instruction::UDiv:
41+
OPS_count += 1;
42+
break;
43+
case Instruction::Sub:
44+
OPS_count += 1;
45+
break;
46+
case Instruction::FSub:
47+
OPS_count += 1;
48+
break;
49+
}
50+
}
51+
}
52+
53+
auto& LA = AM.getResult<LoopAnalysis>(F);
54+
for (auto& L : LA) ++loops_count;
55+
56+
errs() << "Functions: " << F_count << "\n"
57+
<< "BB: " << BB_count << "\n"
58+
<< "OPS: " << OPS_count << "\n"
59+
<< "Loops: " << loops_count << "\n";
60+
61+
return PreservedAnalyses::all();
62+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_llvm_component_library(LLVMBogomazovCountPass
2+
BogomazovCount.cpp
3+
4+
DEPENDS
5+
intrinsics_gen
6+
7+
COMPONENT_NAME
8+
BogomazovCount
9+
10+
LINK_COMPONENTS
11+
Core
12+
Support
13+
)

llvm/lib/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ add_subdirectory(Hello)
99
add_subdirectory(ObjCARC)
1010
add_subdirectory(Coroutines)
1111
add_subdirectory(CFGuard)
12+
add_subdirectory(BogomazovCount)

0 commit comments

Comments
 (0)