-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashDCE.cpp
440 lines (369 loc) · 15.4 KB
/
HashDCE.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//===- HashDCE.cpp - Dead Code Elimination using Hashing -----------------===//
//
// The LLVM Compiler Infrastructure
//
//===----------------------------------------------------------------------===//
//
// This file implements the technique presented in "A New Technique for Copy
// Propagation And Dead Code Elimination Using Hash Based Value Numbering"
// by Dr. K.V.N.Sunitha and Dr V. Vijaya Kumar
//
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "hashdce"
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Instructions.h"
#include "llvm/Support/InstIterator.h"
#include <map>
#include <queue>
#include <stack>
using namespace llvm;
using namespace std;
// States for CP_Table restore on encountering brnaches
#define SAVE 0
#define RESTORE 1
// Toggle printing of messages on the terminal
#define PRINT 0
STATISTIC(HashDCECounter, "Counts number of functions greeted");
// The following class is used as an entry to the backup stack that
// restores the CP_Table for branches
class CP_Table_Stack_Entry
{
Value * key;
Value * entry;
int Depth; //enhancement for multilevel nesting
public:
CP_Table_Stack_Entry(Value *key_val, Value *entry_val)
{
key = key_val;
entry = entry_val;
}
Value * get_Key()
{
return key;
}
Value * get_Entry()
{
return entry;
}
};
namespace {
struct HashDCE : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
HashDCE() : FunctionPass(ID) {}
map<Value*, Value*> CP_Table; // Stores compiler temporaries for load elimination
stack<CP_Table_Stack_Entry> CP_Table_Stack; // Used as a store to restore CP_Table values after a branch
map<Value*,bool> Mark; // Mark[i] = True means statement i cannot be eliminated
queue<Value*> DeleteList;
queue<Value*> IncList;
map<Value*,int> HT; // Hash Table used for value numbering
map<Value*,int> StatementContext; // Determines the HT entry value for an operand in statement i
map<Value*,int> MarkContext; // Determines the HT value of the operand that must not be eliminated
unsigned int IfElseRestore;
virtual bool runOnFunction(Function &F)
{
++HashDCECounter;
for (Function::iterator I=F.begin(),E=F.end(); I!=E; I++)
{
BasicBlock* bb = I;
Instruction& be = bb->back();
bool CP_Table_status;
queue<Value*> UseList;
// The following lines check whether the block ends with a
// conditional branch or not
// If it does then we need to restore the CP Table
// And delete phi nodes from being eliminated
if (isa<BranchInst>(be))
{
if (PRINT)
errs() << bb->back()<<'\n';
BranchInst *bi = dyn_cast<BranchInst>(&be);
if (bi->isConditional())
{
CP_Table_status = SAVE;
IfElseRestore = 1;
}
else
CP_Table_status = RESTORE;
}
// Scan every basic block in the program to determine copies and save
// its value to the CP_Table
for (BasicBlock::iterator BI=bb->begin(),BE=bb->end(); BI!=BE; BI++)
{
Instruction * i = BI;
map<Value*,Value*>::iterator ii;
// If the statement is a load instruction then the destination is
// a compiler temporary and the operand is an alloc instance,
// store it as CP_Table[source] = destination.
// Uselist stores the instances that are modified in a basic block
// so that those entries can be eliminated if the block is within a
// branch.
if (isa<LoadInst>(i))
{
ii = CP_Table.find(i->getOperand(0));
if (ii != CP_Table.end())
{
CP_Table[i] = ii->second;
i->replaceAllUsesWith(ii->second);
UseList.push(i);
}
else
{
CP_Table[i->getOperand(0)] = i;
UseList.push(i->getOperand(0));
}
// Statement Context stores the HT value of an operand
// This helps determining the version that does should not be
// eliminated.
map<Value*,int>::iterator hi;
hi = HT.find(i->getOperand(0));
if (hi != HT.end())
{
StatementContext[i] = HT[i->getOperand(0)];
if (PRINT)
errs()<<"Context Save\n"<<HT[i->getOperand(0)]<<"\n";
}
}
// A store instruction contains 2 operands, the first is a temporary
// The second is an alloc instance
// We store the instance as CP_Table[second] = first
// Again Uselist provides information about all alloc instances touched
// in the block
else if (isa<StoreInst>(i))
{
ii = CP_Table.find(i->getOperand(1));
if (ii != CP_Table.end())
{
CP_Table[i->getOperand(1)] = ii->second;
UseList.push(i->getOperand(1));
}
else
{
CP_Table[i->getOperand(1)] = i->getOperand(0);
UseList.push(i->getOperand(1));
}
// In case of a store we know that the HT value needs to be incremented.
// Inclist stores entries that are incremented so that they can be restored
// in case of branches.
map<Value*,int>::iterator hi;
hi = HT.find(i->getOperand(1));
if (hi != HT.end())
{
HT[i->getOperand(1)]++;
if (PRINT)
errs()<<"HT plus plus\n"<< HT[i->getOperand(1)]<<"\n";
StatementContext[i] = HT[i->getOperand(1)];
IncList.push(i->getOperand(1));
}
}
// Do nothing in case of Call or Branch Instructions
else if (isa<CallInst>(i))
{
i->getNumOperands();
}
else if (isa<BranchInst>(i))
{
BranchInst* bi = dyn_cast<BranchInst>(i);
unsigned int count = 0;
if (bi->isConditional())
{
if (PRINT)
errs() << "Branch Inst " << i->getNumOperands()<<"\n";
count = 0;
while (count < i->getNumOperands())
{
if (PRINT)
errs()<<i->getOperand(count)<<"\n";
count++;
}
}
}
// In case the instruction is an alloc initialize the HT entry
// to zero
else if (isa<AllocaInst>(i))
{
if (PRINT)
errs()<<"Alloca "<< *(i)<<"\n";
HT[i] = 0;
}
if (PRINT)
errs()<<"\n"<<i<<"\n"<<*i<<"\n";
}
if (PRINT)
errs() << "Block End && IfElse Restore "<< IfElseRestore<<"\n";
if (CP_Table_status == RESTORE )
{
map<Value*,Value*>::iterator ii;
while (!UseList.empty())
{
Instruction *I = (Instruction *)UseList.front();
UseList.pop();
ii = CP_Table.find(I);
if (ii != CP_Table.end())
{
CP_Table.erase(I);
}
}
if( IfElseRestore != 0)
{
while(!IncList.empty())
{
map<Value*,int>::iterator hi;
Instruction *I = (Instruction *)IncList.front();
IncList.pop();
hi = HT.find(I);
if (hi != HT.end())
{
HT[I] --;
}
}
}
IfElseRestore--;
// Now we use the CP_Table_Store to restore the old values
// of variables in the CP_Table
// We also use a backup stack to restore the CP_Table_Stack
// Since we invoke restore on two instance of branch
stack<CP_Table_Stack_Entry> CP_Table_Stack_Bkp;
while (!CP_Table_Stack.empty())
{
CP_Table_Stack_Entry s = CP_Table_Stack.top();
CP_Table_Stack.pop();
map<Value*,Value*>::iterator ii;
ii = CP_Table.find(s.get_Key());
if (ii == CP_Table.end())
{
CP_Table[s.get_Key()] = s.get_Entry();
}
CP_Table_Stack_Bkp.push(s);
}
// Restore CP_Table_Stack using the backup
// stack
while (!CP_Table_Stack_Bkp.empty())
{
CP_Table_Stack_Entry s = CP_Table_Stack_Bkp.top();
CP_Table_Stack_Bkp.pop();
CP_Table_Stack.push(s);
}
}
// Save the CP_Table into the CP_Table_Stack
else if (CP_Table_status == SAVE )
{
map<Value*,Value*>::iterator it;
for(it = CP_Table.begin(); it!= CP_Table.end(); it++)
{
CP_Table_Stack_Entry s(it->first, it->second);
CP_Table_Stack.push(s);
}
}
}
if (PRINT)
errs().write_escaped(F.getName()) << '\n';
// The following lines begin a backward traversal of the
// Program to identify useful instructions and mark them
// Here we make use of StatementContext and MarkContext
// heavily
for (inst_iterator I=inst_end(F),E=inst_begin(F); true ; I--)
{
Instruction * i;
i =&*I;
if (i == NULL)
{
continue;
}
// If the statement is a Call/Return or Terminator Instruction we go ahead and
// mark it
if (isa<CallInst>(i) or Mark[i] or isa<ReturnInst>(i) or isa<TerminatorInst>(i))
{
Mark[i] = true;
unsigned int count = 0;
// If the instruction is a Call instruction we mark it and
// all its operands as useful
if(isa<CallInst>(i))
{
Mark[i->getOperand(1)] = true;
if (PRINT)
errs() << "Marked" << *(i->getOperand(1))<<'\n';
}
// If the instruction is a load we need to make sure that
// The correct HT value numbered operand is not eliminated
// Here we use MarkContext
else if (isa<LoadInst>(i))
{
MarkContext[i->getOperand(0)]=StatementContext[i];
}
// For any other types of instructions we go ahead and mark all
// its operands.
else
{
count = 0;
while (count < i->getNumOperands())
{
Mark[i->getOperand(count)] = true;
if (PRINT)
errs() << "Marked" << *(i->getOperand(count))<<'\n';
count++;
}
}
if (PRINT)
errs() << "Marked" << *i<<'\n';
}
// If an instruction is unmarked we can go ahead and erase it but only
// in case it is not a store an contains a HT valued operand that is
// in MarkContext
// DeleteList contains the instructions that need to be eliminated
// This list is maintained as a queue so that the instructions first to be
// eliminted are the uses and not the defs.
else
{
unsigned int count = 0;
if (isa<StoreInst>(i))
{
map<Value*,int>::iterator hi;
hi = MarkContext.find(i->getOperand(1));
// If operand is in MarkContext and a store we mark it and
// its operands.
if (hi != HT.end())
{
if (StatementContext[i] == MarkContext[i->getOperand(1)])
{
Mark[i] = true;
count = 0;
while (count < i->getNumOperands())
{
Mark[i->getOperand(count)] = true;
if (PRINT)
errs() << "Marked " << *(i->getOperand(count))<<'\n';
count++;
}
}
else
DeleteList.push(i);
}
else
DeleteList.push(i);
}
else
{
DeleteList.push(i);
}
}
if (I == E)
break;
}
// Finally the we simply go ahead an remove the instructions
// in the WorkList queue from the program.
while (!DeleteList.empty())
{
Instruction *I = (Instruction *)DeleteList.front();
DeleteList.pop();
I->eraseFromParent();
}
return false;
}
};
}
char HashDCE::ID = 0;
static RegisterPass<HashDCE> X("hashdce", "HashDCE Pass");