-
Notifications
You must be signed in to change notification settings - Fork 31
/
hopper-pre-pass.cc
154 lines (128 loc) · 4.06 KB
/
hopper-pre-pass.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Make optimization fail for branches
e.g
if (x == 1 & y == 1) {}
=>
if (x==1) {
if (y == 1) {}
}
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "config.h"
#include "debug.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/ADT/SmallSet.h"
#if LLVM_VERSION_MAJOR >= 11
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#else
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#endif
using namespace llvm;
namespace {
bool pre_process(Module &M) {
SAYF("start hopper pre-process..\n");
LLVMContext &C = M.getContext();
IntegerType *Int8Ty = IntegerType::getInt8Ty(C);
IntegerType *Int32Ty = IntegerType::getInt32Ty(C);
Type *VoidTy = Type::getVoidTy(C);
srandom(1851655);
Type *FnArgs[1] = {Int32Ty};
FunctionType *FnTy = FunctionType::get(VoidTy, FnArgs, /*isVarArg=*/false);
FunctionCallee BranchStub = M.getOrInsertFunction("__hopper_branch_stub", VoidTy, Int32Ty);
for (auto &F : M) {
// if the function is declaration, ignore
if (F.isDeclaration()) return false;
#ifdef DISABLE_PRE_PROCESS
return false;
#endif
SmallSet<BasicBlock *, 20> VisitedBB;
LLVMContext &C = F.getContext();
for (auto &BB : F) {
Instruction *Inst = BB.getTerminator();
if (isa<BranchInst>(Inst)) {
BranchInst *BI = dyn_cast<BranchInst>(Inst);
if (BI->isUnconditional() || BI->getNumSuccessors() < 2) continue;
Value *Cond = BI->getCondition();
if (!Cond) continue;
for (unsigned int i = 0; i < BI->getNumSuccessors(); i++) {
BasicBlock *B0 = BI->getSuccessor(i);
if (B0 && VisitedBB.count(B0) == 0) {
VisitedBB.insert(B0);
BasicBlock::iterator IP = B0->getFirstInsertionPt();
IRBuilder<> IRB(&(*IP));
unsigned int cur_loc = random() % 1048576;
CallInst *Call = IRB.CreateCall(
BranchStub, {ConstantInt::get(Int32Ty, cur_loc)});
Call->setMetadata(C.getMDKindID("stub"), MDNode::get(C, None));
}
}
}
}
}
return true;
}
#if LLVM_VERSION_MAJOR >= 11
struct HopperPreProcess : public PassInfoMixin<HopperPreProcess> {
public:
HopperPreProcess() {}
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
pre_process(M);
return PreservedAnalyses::all();
}
static bool isRequired() { return true; }
};
#endif
struct LegacyHopperPreProcess : public ModulePass {
public:
static char ID;
LegacyHopperPreProcess() : ModulePass(ID) {}
bool runOnModule(Module &M) override {
pre_process(M);
return true;
}
};
} // namespace
char LegacyHopperPreProcess::ID = 0;
#if LLVM_VERSION_MAJOR < 11
static void registerPrePass(const PassManagerBuilder &,
legacy::PassManagerBase &PM) {
PM.add(new LegacyHopperPreProcess());
}
static RegisterStandardPasses RegisterPass(
PassManagerBuilder::EP_EarlyAsPossible, registerPrePass);
/*
static RegisterStandardPasses RegisterPass0(
PassManagerBuilder::EP_EnabledOnOptLevel0, registerPrePass);
*/
#else
llvm::PassPluginLibraryInfo getHopperPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "HopperPreProcess", LLVM_VERSION_STRING,
[](PassBuilder &PB) {
PB.registerPipelineStartEPCallback(
[](ModulePassManager &MPM, OptimizationLevel OL) {
MPM.addPass(HopperPreProcess());
});
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &MPM,
ArrayRef<PassBuilder::PipelineElement>) {
if (Name == "HopperPreProcess") {
MPM.addPass(HopperPreProcess());
return true;
}
return false;
});
}};
}
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return getHopperPluginInfo();
}
#endif