-
Notifications
You must be signed in to change notification settings - Fork 3
/
PruneBlocks.cpp
421 lines (365 loc) · 11.2 KB
/
PruneBlocks.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* This is the naive block pruning code.
*
* For some depth, we attempt to aggressively remove all
* blocks not-executed (i.e., those not found in the
* trace log file).
*
* For every block in a function, if it was executed, it
* is safe from being pruned. If it was not executed, it
* is going to have it's moment of glory to possibly be
* pruned.
*
* Issues:
* - Many.
*
*/
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/SymbolTableListTraits.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
// Like i said.. ugly as fuhhhh
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <set>
using namespace llvm;
#include "../TraceUtils.h"
#include "../CommonUtils.h"
#include "PruneBlocks.h"
#include "PruneHelp.h"
#include "PruneStats.h"
static cl::opt<std::string> TraceLogFile("pskin-log-file",
cl::desc("Specify trace log file"), cl::init("default_trace.log"),
cl::Required);
static cl::opt<unsigned> PruneDepth("pskin-depth",
cl::desc("Specify the maximum depth to prune"), cl::init(3),
cl::Required);
static cl::opt<std::string> FunctionCheckRetList("pskin-fnlist",
cl::desc("File for call-checkret branch whitelisting"), cl::init(""));
static cl::opt<std::string> LogRemovedBlocks("pskin-log-block-removal",
cl::desc("Specify the file to output which blocks are deleted"),
cl::init("block_removal.log"));
static cl::opt<bool> InteractiveMode("pskin-interactive",
cl::desc("Optional interactive mode"), cl::init(false));
bool
PruneBlocks::runOnModule(Module &M)
{
mod = &M;
ModuleLevelStats m(PruneDepth);
m.setFunctionCount(std::distance(M.begin(), M.end()));
traceEntries = TraceUtils::ParseTraceFile(TraceLogFile);
if (traceEntries.size() == 0) {
errs() << "No trace entries found in '" << TraceLogFile << "'\n";
return false;
}
if (FunctionCheckRetList != "") {
std::ifstream fileHandle(FunctionCheckRetList);
std::string pcfName;
while (std::getline(fileHandle, pcfName)) {
// Not really needed.
probeCallRetCompareNames.push_back(pcfName);
Function *fp = M.getFunction(pcfName);
if (fp != NULL) {
std::cout << "<PB> Ret-check function: " << pcfName << "\n";
probeCallRetCompareFunctions.insert(fp);
}
}
}
std::vector<FocusFunctionStats> focusFunctionStats;
unsigned dep = PruneDepth;
unsigned total_removed = 0;
std::ofstream blkRmLog;
blkRmLog.open(LogRemovedBlocks);
std::set<Function *> calledFromRemovedBlocks;
do {
unsigned removed_count = 0;
/*
* The way this is working, we are looking at all functions
* of a certain call depth, then depth - 1, depth - 2, ..., 1.
*
* And looking at the removable blocks in that function.
*
*/
if (setFocusFunc(dep) == false) {
#ifdef _DEBUG
errs() << "<PB> Adjusting pruning call depth\n";
#endif
--dep;
continue;
}
FocusFunctionStats ffStats(currentFocusFunctionName, dep);
#ifdef _DEBUG
errs() << "<PB> Prune call depth: " << dep << "\n";
errs() << "<PB> Function being pruned: " << currentFocusFunctionName << "\n";
#endif
keepBlocks.clear();
removeBlocks.clear();
ffStats.setBlockCount(parseFocusFuncForBlocks());
ffStats.setWantKeepBlockCount(keepBlocks.size());
ffStats.setWantRemoveBlockCount(removeBlocks.size());
for (auto rbi = removeBlocks.begin(); rbi != removeBlocks.end();
++rbi) {
BBlockRef *bbRef = const_cast<BBlockRef *>(&*rbi);
BasicBlock *bb = bbRef->getBB();
bool unhandledTerminator = false;
for (auto pi = pred_begin(bb); pi != pred_end(bb); ++pi) {
BasicBlock *predBB = *pi;
TerminatorInst *ti = predBB->getTerminator();
/* Handle BranchInst */
if (BranchInst *bi = dyn_cast<BranchInst>(ti)) {
PruneHelp::tryCleanseBranchInst(bbRef, predBB, bi,
removeBlocks, probeCallRetCompareFunctions);
/* Handle IndirectBrInst */
} else if (IndirectBrInst *bi = dyn_cast<IndirectBrInst>(ti)) {
PruneHelp::tryCleanseIndirectBrInst(bbRef, bi,
removeBlocks);
/* Handle CatchReturnInst */
} else if (CatchReturnInst *cri = \
dyn_cast<CatchReturnInst>(ti)) {
PruneHelp::tryCleanseCatchReturnInst(bbRef, cri,
removeBlocks);
/* Handle CatchSwitchInst */
} else if (CatchSwitchInst *cri = \
dyn_cast<CatchSwitchInst>(ti)) {
PruneHelp::tryCleanseCatchSwitchInst(bbRef, cri,
removeBlocks);
/* Handle SwitchInst */
} else if (SwitchInst *si = dyn_cast<SwitchInst>(ti)) {
PruneHelp::tryCleanseSwitchInst(bbRef, si, removeBlocks);
/* Handle InvokeInst */
} else if (InvokeInst *ini = dyn_cast<InvokeInst>(ti)) {
PruneHelp::tryCleanseInvokeInst(bbRef, ini, removeBlocks);
/* Handle CleanupReturnInst */
} else if (CleanupReturnInst *cri = \
dyn_cast<CleanupReturnInst>(ti)) {
PruneHelp::tryCleanseCleanupReturnInst(bbRef, cri,
removeBlocks);
} else {
errs() << "Unhandled terminator: \n";
bb->dump();
unhandledTerminator = true;
break;
}
if (unhandledTerminator) {
break;
}
}
}
for (auto rbi = removeBlocks.begin(); rbi != removeBlocks.end();
++rbi) {
BBlockRef *bbRef = const_cast<BBlockRef *>(&*rbi);
BasicBlock *bb = bbRef->getBB();
if (!bbRef->isRemoved() && pred_begin(bb) == pred_end(bb)) {
if (bbRef->getFocusIdx() == 1) {
continue;
}
if (succ_begin(bb) != succ_end(bb)) {
continue;
}
for (Instruction &I : *bb) {
if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
CallSite cs(&I);
Function *f = cs.getCalledFunction();
if (f == NULL) {
#ifdef _DEBUG
errs() << "<PB> Called null Function...indirect call UNHANDLED\n";
#endif
continue;
}
calledFromRemovedBlocks.insert(f);
}
}
DeleteDeadBlock(bb);
bbRef->setRemoved(true);
}
}
/*
* With the removal of blocks, we should go through and fix PHINodes
* to have valid incoming Values and proper predecessors.
*/
bool repairedSome = false;
do {
repairedSome = PruneHelp::repairPHINodes(currentFocusFunction, removeBlocks);
} while (repairedSome == true);
// XXX Count remove blocks
for (auto rbi = removeBlocks.begin(); rbi != removeBlocks.end(); ++rbi) {
BBlockRef *bbRef = const_cast<BBlockRef *>(&*rbi);
BasicBlock *bb = bbRef->getBB();
if (bbRef->isRemoved()) {
blkRmLog << bbRef->getFocusName() << ":" << bbRef->getFocusIdx() << "\n";
removed_count++;
}
}
total_removed += removed_count;
ffStats.setRemovedBlockCount(removed_count);
focusFunctionStats.push_back(ffStats);
} while (dep > 0);
// End of focus function lookup and prune loop
blkRmLog.close();
m.setTotalRemovedBlockCount(total_removed);
// How is this diff from below call to removeUnusedFunctions?
unsigned rmFormers = 0;
for (auto fi = calledFromRemovedBlocks.begin(); fi != calledFromRemovedBlocks.end(); ++fi) {
Function *formerlyCalled = *fi;
std::string n = "<noname>";
if (formerlyCalled->hasName()) {
n = formerlyCalled->getName().str();
}
if (n != "main") {
continue;
}
#ifdef _DEBUG
Value *v = cast<Value>(formerlyCalled);
if (v->use_empty()) {
errs() << "Value->use_empty() ...so ";
}
#endif
if (formerlyCalled->user_empty()) {
#ifdef _DEBUG
errs() << "removing: " << n << "\n";
#endif
formerlyCalled->eraseFromParent();
++rmFormers;
}
}
#ifdef _DEBUG
errs() << "<PB> Removed " << rmFormers << " no longer called functions from Module.\n";
#endif
std::set<std::string> leaveFunctions = {
"main"
};
// This is fairly awful.
for (auto e = traceEntries.begin(); e != traceEntries.end(); ++e) {
const TraceEntry *t = &*e;
std::string s = t->getFunction();
leaveFunctions.insert(s);
}
rmFormers += PruneHelp::removeUnusedFunctions(&M, leaveFunctions);
m.setTotalRemovedFunctionCount(rmFormers);
// PruneHelp::removeUnusedBasicBlocks(&M, leaveFunctions);
m.print();
for (auto i = focusFunctionStats.begin(); i != focusFunctionStats.end(); ++i) {
FocusFunctionStats foc = *i;
foc.print();
}
return true;
}
/*
* For a given depth, find a function of that depth to set as
* currentFocusFunctionName. Put function into list so we can iterate
* through each.
*/
bool
PruneBlocks::setFocusFunc(unsigned dep)
{
std::string nameofFocusFunction = "";
Function *tmpFun = NULL;
for (auto e = traceEntries.begin(); e != traceEntries.end(); ++e) {
const TraceEntry *t = &*e;
if (t->getDepth() == dep) {
nameofFocusFunction = t->getFunction();
if (prunedFunctions.end() != prunedFunctions.find(nameofFocusFunction)) {
nameofFocusFunction = "";
continue;
}
tmpFun = mod->getFunction(nameofFocusFunction);
if (tmpFun == NULL) {
// Try for the next of same dep.
errs() << "setFocusFunc: not found: " \
<< nameofFocusFunction << "\n";
nameofFocusFunction = "";
continue;
}
if (tmpFun->isDeclaration() || tmpFun->isIntrinsic()) {
errs() << "setFocusFunc: skipping external: " \
<< nameofFocusFunction << "\n";
tmpFun = NULL;
nameofFocusFunction = "";
continue;
}
break;
}
}
if (nameofFocusFunction != "" && tmpFun != NULL) {
currentFocusFunctionName = nameofFocusFunction;
currentFocusFunction = tmpFun;
prunedFunctions.insert(currentFocusFunctionName);
return true;
}
return false;
}
unsigned
PruneBlocks::parseFocusFuncForBlocks()
{
std::set<unsigned> blkNums;
bool interactiveFn = false;
if (InteractiveMode) {
std::string qf = "\n<PB> Function: " + currentFocusFunctionName \
+ ", interactive? (y/n) ";
char yn = CommonUtils::askQuestion(qf.c_str(), "yn");
if (yn == 'y') {
interactiveFn = true;
}
}
/*
* Project entries related to focus function to block number.
*
* Note: This ignores call depth. It just grabs any/all blocks
* reached within that function so that they are considered
* blocks to keep.
*/
for (auto e = traceEntries.begin(); e != traceEntries.end(); ++e) {
const TraceEntry *t = &*e;
if (t->getFunction() == currentFocusFunctionName) {
blkNums.insert(t->getBlock());
continue;
}
}
/*
* Split blocks of function into those we want to keep and those
* we hope to remove, keepBlocks and removeBlocks. We store them
* in BBlockRef's for some reason. :-P
*
*/
unsigned blkCnt = 0;
for (BasicBlock &BB : *currentFocusFunction) {
BasicBlock *BBp = &BB;
++blkCnt;
if (blkNums.end() != blkNums.find(blkCnt)) {
keepBlocks.push_back(BBlockRef(BBp, blkCnt));
} else {
if (InteractiveMode && interactiveFn) {
errs() << "<PB> Function: " << currentFocusFunctionName << "\n";
BBp->dump();
if (pred_begin(BBp) != pred_end(BBp)) {
auto pr = pred_begin(BBp);
BasicBlock *prBB = *pr;
errs() << "<PB> First predecessor: \n";
prBB->dump();
}
char yn = CommonUtils::askQuestion("\n<PB> Keep block? (y/n) ",
"yn");
if (yn == 'y') {
keepBlocks.push_back(BBlockRef(BBp, blkCnt));
} else {
removeBlocks.push_back(BBlockRef(BBp, blkCnt));
}
// Not interactive:
} else {
removeBlocks.push_back(BBlockRef(BBp, blkCnt));
}
}
}
return blkCnt;
}
char PruneBlocks::ID = 0;
static RegisterPass<PruneBlocks> X("pskin-naive-prune",
"Most basic");