Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit e3ecab5

Browse files
authored
Lebedeva Ksenia Lab3 Var2 (#228)
1 parent d9bc26b commit e3ecab5

File tree

3 files changed

+616
-0
lines changed

3 files changed

+616
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set(PluginName X86LebedevaInstrCounterPass)
2+
3+
file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp)
4+
add_llvm_pass_plugin(${PluginName}
5+
${ALL_SOURCE_FILES}
6+
DEPENDS
7+
intrinsics_gen
8+
X86
9+
BUILDTREE_ONLY)
10+
11+
target_include_directories(${PluginName} PUBLIC ${PATH_TO_X86})
12+
13+
set(LLVM_TEST_DEPENDS ${PluginName} ${LLVM_TEST_DEPENDS} PARENT_SCOPE)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "X86.h"
2+
#include "X86InstrInfo.h"
3+
#include "llvm/CodeGen/MachineFunctionPass.h"
4+
#include "llvm/Passes/PassBuilder.h"
5+
6+
using namespace llvm;
7+
8+
class X86LebedevaInstrCounterPass : public MachineFunctionPass {
9+
public:
10+
static inline char ID = 0;
11+
X86LebedevaInstrCounterPass() : MachineFunctionPass(ID) {}
12+
13+
bool runOnMachineFunction(MachineFunction &machFunc) override {
14+
DebugLoc debugLoc = machFunc.front().begin()->getDebugLoc();
15+
const TargetInstrInfo *instrInfo = machFunc.getSubtarget().getInstrInfo();
16+
17+
Module &module = *machFunc.getFunction().getParent();
18+
GlobalVariable *globalVar = module.getGlobalVariable("ic");
19+
if (!globalVar) {
20+
globalVar = new GlobalVariable(
21+
module, IntegerType::get(module.getContext(), 64), false,
22+
GlobalValue::ExternalLinkage, nullptr, "ic");
23+
}
24+
25+
for (auto &basicBlock : machFunc) {
26+
size_t count = basicBlock.size();
27+
BuildMI(basicBlock, basicBlock.begin(), debugLoc,
28+
instrInfo->get(X86::ADD64mi32))
29+
.addGlobalAddress(globalVar, 0, 0)
30+
.addImm(count);
31+
}
32+
33+
return true;
34+
}
35+
};
36+
37+
static RegisterPass<X86LebedevaInstrCounterPass>
38+
X("x86-lebedeva-instr-counter",
39+
"A pass counting the number of X86 machine instructions", false, false);

0 commit comments

Comments
 (0)