-
Notifications
You must be signed in to change notification settings - Fork 11
/
LLVMParser.h
216 lines (144 loc) · 5.75 KB
/
LLVMParser.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#pragma warning(disable : 4244)
#pragma warning(disable : 4267)
#pragma warning(disable : 4146)
#pragma warning(disable : 4624)
#pragma warning(disable : 4530)
#ifndef LLVMPARSER_H
#define LLVMPARSER_H
#include "Z3Prover.h"
#include <map>
#include <unordered_set>
#include <llvm/ADT/SmallPtrSet.h>
#include <llvm/IR/Instructions.h>
#include "splitmix64.h"
namespace llvm {
class DominatorTree;
class Function;
class Module;
class LLVMContext;
class Evaluator;
class TargetLibraryInfoImpl;
class TargetLibraryInfo;
class Type;
} // namespace llvm
namespace LSiMBA {
typedef struct BFSEntry {
int Depth;
llvm::Instruction *I;
BFSEntry(int Depth, llvm::Instruction *I) : Depth(Depth), I(I){};
} BFSEntry;
typedef struct MBACandidate {
llvm::Instruction *Candidate;
int ASTSize = 0;
llvm::SmallVector<BFSEntry, 16> AST;
llvm::SmallVector<llvm::Value *, 8> Variables;
std::string Replacement;
bool isValid = false;
} MBACandidate;
class LLVMParser {
public:
LLVMParser(const std::string &filename, const std::string &OutputFile,
bool Parallel = true, bool Verify = true,
bool OptimizeBefore = false, bool OptimizeAfter = true,
bool Debug = false, bool Prove = false);
LLVMParser(llvm::Module *M, bool Parallel = true, bool Verify = true,
bool OptimizeBefore = false, bool OptimizeAfter = true,
bool Debug = false, bool Prove = false);
LLVMParser(llvm::Function *F, bool Parallel = true, bool Verify = true,
bool OptimizeBefore = false, bool OptimizeAfter = true,
bool Debug = false, bool Prove = false);
~LLVMParser();
/*
Searches for MBAs inside the function and tries to simplify all MBAs
*/
int simplify();
/*
Interprets the function as one MBA and tries to simplify it
*/
int simplifyMBAFunctionsOnly();
static llvm::LLVMContext &getLLVMContext();
int getInstructionCountBefore();
int getInstructionCountAfter();
private:
std::string OutputFile = "";
bool Parallel;
bool Verify;
bool Prove;
bool OptimizeBefore;
bool OptimizeAfter;
bool Debug;
int MaxThreadCount;
int InstructionCountBefore = 0;
int InstructionCountAfter = 0;
static llvm::LLVMContext Context;
llvm::TargetLibraryInfoImpl *TLII;
std::unique_ptr<llvm::TargetLibraryInfo> TLI;
llvm::Module *M;
llvm::Function *F;
SplitMix64 SP64;
std::unique_ptr<llvm::Evaluator> Eval;
int extractAndSimplify();
int simplifyMBAModule();
void writeModule();
bool parse(const std::string &filename);
bool verify(llvm::Function *F0, llvm::Function *F1, llvm::APInt &Modulus);
bool verify(int ASTSize, llvm::SmallVectorImpl<BFSEntry> &AST, std::string &SimpExpr,
llvm::SmallVectorImpl<llvm::Value *> &Variables);
llvm::Instruction *getSingleTerminator(llvm::Function &F);
bool hasLoadStores(llvm::Function &F);
void initResultVector(llvm::Function &F,
std::vector<llvm::APInt> &ResultVector,
const llvm::APInt &Modulus, int VNumber,
llvm::Type *IntType);
bool runLLVMOptimizer(bool Initial = false);
bool rewriteIntrinsics();
bool isSupportedInstruction(llvm::Value *V);
void extractCandidates(llvm::Function &F,
std::vector<MBACandidate> &Candidates);
bool constainsReplacedInstructions(
llvm::SmallPtrSet<llvm::Instruction *, 16> &ReplacedInstructions,
MBACandidate &Cand);
bool findReplacements(llvm::DominatorTree *DT,
std::vector<MBACandidate> &Candidates);
void getAST(llvm::DominatorTree *DT, llvm::Instruction *I,
llvm::SmallVectorImpl<BFSEntry> &AST,
llvm::SmallVectorImpl<llvm::Value *> &Variables, bool KeepRoot);
int getASTSize(llvm::SmallVectorImpl<BFSEntry> &AST);
bool walkSubAST(llvm::DominatorTree *DT, llvm::SmallVectorImpl<BFSEntry> &AST,
std::vector<MBACandidate> &Candidates);
void printAST(llvm::SmallVectorImpl<BFSEntry> &AST);
std::string getASTAsString(llvm::SmallVectorImpl<BFSEntry> &AST,
llvm::SmallVectorImpl<llvm::Value *> &Variables);
void initResultVectorFromAST(llvm::SmallVectorImpl<BFSEntry> &AST,
std::vector<llvm::APInt> &ResultVector,
const llvm::APInt &Modulus,
llvm::SmallVectorImpl<llvm::Value *> &Variables);
llvm::APInt evaluateAST(llvm::SmallVectorImpl<BFSEntry> &AST,
llvm::SmallVectorImpl<llvm::Value *> &Variables,
llvm::SmallVectorImpl<llvm::APInt> &Par, bool &Error);
llvm::Constant *
getVal(llvm::Value *V,
llvm::DenseMap<llvm::Value *, llvm::Constant *> &ValueStack,
llvm::SmallVectorImpl<llvm::Value *> &Variables,
llvm::SmallVectorImpl<llvm::APInt> &Par);
/*
Thread safe
*/
llvm::Constant *getConstantInt(llvm::Type *Ty, uint64_t Value);
llvm::Constant *getConstantInt(llvm::Type *Ty, llvm::APInt Value);
bool doesDominateInst(llvm::DominatorTree *DT, const llvm::Instruction *InstA,
const llvm::Instruction *InstB);
z3::expr getZ3ExpressionFromAST(
z3::context &Z3Ctx, llvm::SmallVectorImpl<BFSEntry> &AST,
llvm::SmallVectorImpl<llvm::Value *> &Variables,
std::map<std::string, z3::expr *> &VarMap, int OverrideBitWidth = 0);
z3::expr boolToBV(z3::context &Z3Ctx, z3::expr &BoolExpr,
int BitWidth);
z3::expr *getZ3Val(z3::context &Z3Ctx, llvm::Value *V,
llvm::DenseMap<llvm::Value *, z3::expr *> &ValueMap,
int OverrideBitWidth = 0);
uint64_t getMASK(llvm::Type *Ty);
static int getInstructionCount(llvm::Module *M);
};
} // namespace LSiMBA
#endif // LLVMPARSER_H