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

Commit 98adaa2

Browse files
Ulyanov Daniil Lab3 Var2 (#241)
Executed instructions counter inserting pass.
1 parent 311db70 commit 98adaa2

File tree

3 files changed

+1013
-0
lines changed

3 files changed

+1013
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set(PluginName X86UlyanovInstCounterPass)
2+
3+
file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.h)
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "X86.h"
2+
#include "X86InstrInfo.h"
3+
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
4+
5+
using namespace llvm;
6+
7+
namespace {
8+
9+
class X86UlyanovInstCounterPass : public MachineFunctionPass {
10+
public:
11+
static char ID;
12+
X86UlyanovInstCounterPass() : MachineFunctionPass(ID) {}
13+
bool runOnMachineFunction(llvm::MachineFunction &MF) override {
14+
auto *GV = MF.getFunction().getParent()->getNamedGlobal("ic");
15+
if (!GV) {
16+
Module *M = MF.getFunction().getParent();
17+
GV = new GlobalVariable(*M, IntegerType::get(M->getContext(), 64), false,
18+
GlobalValue::ExternalLinkage, nullptr, "ic");
19+
}
20+
21+
auto DL = MF.front().begin()->getDebugLoc();
22+
auto *TII = MF.getSubtarget().getInstrInfo();
23+
24+
for (auto &MBB : MF) {
25+
int Counter = std::distance(MBB.begin(), MBB.end());
26+
auto Place = MBB.getFirstTerminator();
27+
28+
if (Place != MBB.end() && Place != MBB.begin() &&
29+
Place->getOpcode() >= X86::JCC_1 &&
30+
Place->getOpcode() <= X86::JCC_4) {
31+
--Place;
32+
}
33+
BuildMI(MBB, Place, DL, TII->get(X86::ADD64mi32))
34+
.addReg(0)
35+
.addImm(1)
36+
.addReg(0)
37+
.addGlobalAddress(GV)
38+
.addReg(0)
39+
.addImm(Counter);
40+
}
41+
return true;
42+
}
43+
};
44+
} // namespace
45+
46+
char X86UlyanovInstCounterPass::ID = 0;
47+
static RegisterPass<X86UlyanovInstCounterPass>
48+
X("x86-ulyanov-inst-cnt", "Instruction counter pass", false, false);

0 commit comments

Comments
 (0)