forked from revng/revng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplifycomparisons.cpp
312 lines (260 loc) · 8.86 KB
/
simplifycomparisons.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/// \file simplifycomparison.cpp
/// \brief Implementation of the SimplifyComparisonsPass
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// Standard includes
#include <queue>
#include <tuple>
// LLVM includes
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Casting.h"
// Local include
#include "datastructures.h"
#include "debug.h"
#include "ir-helpers.h"
#include "simplifycomparisons.h"
using namespace llvm;
char SimplifyComparisonsPass::ID = 0;
static RegisterPass<SimplifyComparisonsPass> X("scp",
"Simplify Comparisons Pass",
true,
true);
using std::array;
using std::pair;
using std::queue;
using std::tie;
using std::tuple;
using std::unique_ptr;
using std::vector;
using Predicate = CmpInst::Predicate;
// TODO: expand this
static const array<tuple<unsigned, unsigned, Predicate>, 1> KnownTruthTables {
{ std::make_tuple(0b010110010U, 8U, CmpInst::ICMP_SGE) }
};
static const unsigned MaxDepth = 10;
/// \brief Base class for all the types of terms of a boolean expression
class Term {
public:
virtual bool evaluate(unsigned Assignments) const { assert(false); }
};
/// \brief A free-operand term (a variable)
/// It has to be associated to the index of the variable
class VariableTerm : public Term {
public:
VariableTerm(unsigned Index) : VariableIndex(Index) { }
VariableTerm() : VariableIndex(0) { }
virtual bool evaluate(unsigned Assignments) const override;
private:
unsigned VariableIndex;
};
class BinaryTerm;
/// \brief Simple data structure associating a Term to a BinaryTerm operand
class TermUse {
public:
TermUse(BinaryTerm *Op, unsigned OpIndex) : T(Op), OpIndex(OpIndex) { }
TermUse() : T(nullptr), OpIndex(0) { }
void set(Term *Operand);
private:
BinaryTerm *T;
unsigned OpIndex;
};
/// \brief Term representing a binary operation
class BinaryTerm : public Term {
public:
BinaryTerm() : Opcode(0), Operands({{ nullptr, nullptr }}) {
}
BinaryTerm(unsigned Opcode) : Opcode(Opcode),
Operands({{ nullptr, nullptr }}) {
}
void setOperand(unsigned Index, Term *T) {
Operands[Index] = T;
}
TermUse getOperandUse(unsigned OperandIndex) {
assert(OperandIndex < 2);
return TermUse(this, OperandIndex);
}
Term *getOperand(unsigned OperandIndex) {
assert(OperandIndex < 2);
return Operands[OperandIndex];
}
static bool isSupported(unsigned Opcode) {
switch (Opcode) {
case Instruction::Xor:
case Instruction::And:
case Instruction::Or:
return true;
default:
return false;
}
}
virtual bool evaluate(unsigned Assignments) const override;
private:
unsigned Opcode;
array<Term *, 2> Operands;
};
void TermUse::set(Term *Operand) {
T->setOperand(OpIndex, Operand);
}
bool VariableTerm::evaluate(unsigned Assignments) const {
return Assignments & (1 << VariableIndex);
}
bool BinaryTerm::evaluate(unsigned Assignments) const {
bool A = Operands[0]->evaluate(Assignments);
bool B = Operands[1]->evaluate(Assignments);
switch (Opcode) {
case Instruction::Xor:
return A ^ B;
case Instruction::And:
return A & B;
case Instruction::Or:
return A | B;
default:
assert(false);
}
}
/// \brief If \p V is a LoadInst, looks for the last time it was written
// TODO: use MemoryAccess?
Value *SimplifyComparisonsPass::findOldest(Value *V) {
llvm::SmallSet<Value *, 2> Seen;
while (auto *Load = dyn_cast<LoadInst>(V)) {
if (Seen.count(V) != 0)
break;
Seen.insert(V);
auto &ReachingDefinitions = RDP->getReachingDefinitions(Load);
if (ReachingDefinitions.size() != 1)
break;
V = ReachingDefinitions[0];
if (auto *Store = dyn_cast<StoreInst>(V))
V = Store->getValueOperand();
}
return V;
}
/// \brief Find the subtraction of the comparison
BinaryOperator *findSubtraction(SimplifyComparisonsPass *SCP, User *Cmp) {
queue<pair<unsigned, Value *>> WorkList;
WorkList.push({ 0, Cmp->getOperand(0) });
while (!WorkList.empty()) {
Value *V = nullptr;
unsigned Depth = 0;
tie(Depth, V) = WorkList.front();
WorkList.pop();
if (Depth > MaxDepth)
continue;
if (auto *BinOp = dyn_cast<BinaryOperator>(V)) {
auto Opcode = BinOp->getOpcode();
if (Opcode == Instruction::Sub) {
return BinOp;
} else if (BinaryTerm::isSupported(Opcode)) {
WorkList.push({ Depth + 1, BinOp->getOperand(0) });
WorkList.push({ Depth + 1, BinOp->getOperand(1) });
}
} else if (auto *Load = dyn_cast<LoadInst>(V)) {
// TODO: extend to unique predecessors
if (isa<GlobalVariable>(Load->getPointerOperand())) {
auto *Oldest = SCP->findOldest(Load);
if (Oldest != Load)
WorkList.push({ Depth, Oldest });
}
}
}
return nullptr;
}
/// \brief Overload the meaning of an existing predicate as a failure mark
static const auto NoEquivalentPredicate = CmpInst::FCMP_FALSE;
/// Obtain the predicate equivalent to the boolean expression associated to Cmp
/// and whose operands come from \p Subtraction
static Predicate getEquivalentPredicate(SimplifyComparisonsPass *SCP,
CmpInst *Cmp,
BinaryOperator *Subtraction) {
array<Value *, 3> Variables = {
{
SCP->findOldest(Subtraction->getOperand(0)),
SCP->findOldest(Subtraction->getOperand(1)),
Subtraction
}
};
const unsigned OpsCount = std::tuple_size<decltype(Variables)>::value;
array<VariableTerm, OpsCount> VariableTerms;
for (unsigned I = 0; I < OpsCount; I++)
VariableTerms[I] = VariableTerm(I);
vector<unique_ptr<BinaryTerm>> BinaryTerms;
BinaryTerm Start = BinaryTerm(Instruction::Or);
queue<pair<TermUse, Value *>> WorkList;
WorkList.push({ Start.getOperandUse(0), Cmp->getOperand(0) });
while (!WorkList.empty()) {
TermUse PlaceholderUse;
Value *Operand;
tie(PlaceholderUse, Operand) = WorkList.front();
WorkList.pop();
Operand = SCP->findOldest(Operand);
unsigned OpIndex = 0;
for (; OpIndex < OpsCount; OpIndex++)
if (Operand == Variables[OpIndex])
break;
if (OpIndex < OpsCount) {
// It matches one of the variables
PlaceholderUse.set(&VariableTerms[OpIndex]);
} else if (auto *BinOp = dyn_cast<BinaryOperator>(Operand)) {
// It's not a variable, is it a supported operation?
if (!BinaryTerm::isSupported(BinOp->getOpcode()))
return NoEquivalentPredicate;
BinaryTerms.emplace_back(new BinaryTerm(BinOp->getOpcode()));
BinaryTerm *NewOp = BinaryTerms.back().get();
PlaceholderUse.set(NewOp);
WorkList.push({ NewOp->getOperandUse(0), BinOp->getOperand(0) });
WorkList.push({ NewOp->getOperandUse(1), BinOp->getOperand(1) });
} else {
// It's something we don't handle
return NoEquivalentPredicate;
}
}
// Build the truth table
assert(OpsCount < 8 * sizeof(unsigned));
unsigned TruthTable = 0;
unsigned TruthTableSize = 1 << OpsCount;
for (unsigned Assignment = 0; Assignment < TruthTableSize; Assignment++)
if (Start.getOperand(0)->evaluate(Assignment))
TruthTable |= 1 << Assignment;
DBG("sc", dbg << "Found truth table "
<< "0b" << std::bitset<8 * sizeof(unsigned)>(TruthTable)
<< " at " << Cmp->getParent()->getName().data() << "\n");
// Compare with known truth tables
for (auto &P : KnownTruthTables)
if (std::get<0>(P) == TruthTable && std::get<1>(P) == TruthTableSize)
return std::get<2>(P);
return NoEquivalentPredicate;
}
bool SimplifyComparisonsPass::runOnFunction(Function &F) {
RDP = &getAnalysis<ReachingDefinitionsPass>();
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (auto *Cmp = isa_with_op<CmpInst, Value, ConstantInt>(&I)) {
uint64_t N = getLimitedValue(Cmp->getOperand(1));
CmpInst::Predicate OriginalPredicate = Cmp->getPredicate();
if (N == 0
&& (OriginalPredicate == CmpInst::ICMP_SGE
|| OriginalPredicate == CmpInst::ICMP_SLT)) {
if (BinaryOperator *Subtraction = findSubtraction(this, Cmp)) {
auto Predicate = getEquivalentPredicate(this, Cmp, Subtraction);
if (Predicate != NoEquivalentPredicate) {
Comparison Simplified;
Simplified.LHS = Subtraction->getOperand(0);
Simplified.RHS = Subtraction->getOperand(1);
if (OriginalPredicate == CmpInst::ICMP_SLT)
Predicate = CmpInst::getInversePredicate(Predicate);
Simplified.Predicate = Predicate;
SimplifiedComparisons[Cmp] = Simplified;
}
}
}
}
}
}
return false;
}