-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathClamBCRemoveICMPSLE.cpp
115 lines (99 loc) · 3.37 KB
/
ClamBCRemoveICMPSLE.cpp
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
/*
* Compile LLVM bytecode to ClamAV bytecode.
*
* Copyright (C) 2020-2023 Sourcefire, Inc.
*
* Authors: Andy Ragusa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "clambc.h"
#include <llvm/IR/Module.h>
#include <llvm/IR/Instructions.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#include <llvm/Analysis/FunctionPropertiesAnalysis.h>
#include <vector>
using namespace llvm;
using namespace std;
/* Modeled after CallGraphAnalysis */
namespace
{
struct ClamBCRemoveICMPSLE : public PassInfoMixin<ClamBCRemoveICMPSLE> {
protected:
Module *pMod = nullptr;
bool bChanged = false;
virtual void gatherInstructions(Function *pFunc, std::vector<ICmpInst *> &insts)
{
for (auto i = pFunc->begin(), e = pFunc->end(); i != e; i++) {
BasicBlock *pBB = llvm::cast<BasicBlock>(i);
for (auto bbi = pBB->begin(), bbe = pBB->end(); bbi != bbe; bbi++) {
ICmpInst *inst = llvm::dyn_cast<ICmpInst>(bbi);
if (inst) {
if (CmpInst::ICMP_SLE == inst->getPredicate()) {
insts.push_back(inst);
}
}
}
}
}
virtual void processFunction(Function *pFunc)
{
std::vector<ICmpInst *> insts;
gatherInstructions(pFunc, insts);
for (size_t i = 0; i < insts.size(); i++) {
insts[i]->swapOperands();
}
}
public:
virtual ~ClamBCRemoveICMPSLE() {}
PreservedAnalyses run(Module &m, ModuleAnalysisManager &MAM)
{
pMod = &m;
for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) {
Function *pFunc = llvm::dyn_cast<Function>(i);
if (pFunc) {
if (pFunc->isDeclaration()) {
continue;
}
processFunction(pFunc);
}
}
if (bChanged) {
return PreservedAnalyses::none();
}
return PreservedAnalyses::all();
}
}; // end of struct ClamBCRemoveICMPSLE
} // end of anonymous namespace
// This part is the new way of registering your pass
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo()
{
return {
LLVM_PLUGIN_API_VERSION, "ClamBCRemoveICMPSLE", "v0.1",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if (Name == "clambc-remove-icmp-sle") {
FPM.addPass(ClamBCRemoveICMPSLE());
return true;
}
return false;
});
}};
}