/
idenRegion_dynamic.cpp
604 lines (540 loc) · 22.8 KB
/
idenRegion_dynamic.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//=============- idenRegion.cpp - Final Project for EECS 583 ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implement a static analysis on Idenpotent Region process
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "idenRegion"
#include <sstream>
#include <string>
#include <iomanip>
#include <set>
#include <map>
#include <vector>
#include "llvm/InstrTypes.h"
#include "llvm/Pass.h"
#include "llvm/Module.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/BasicBlock.h"
#include "llvm/Instructions.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/PredIteratorCache.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/LoopInfo.h"
#include "LAMP/LAMPLoadProfile.h"
using namespace llvm;
//===----------------------------------------------------------------------===//
// idenRegion
//===----------------------------------------------------------------------===//
namespace {
// statistic computation on operation counts
struct idenRegion : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
AliasAnalysis *AA; // Current AliasAnalysis information
LoopInfo *LI; // Current LoopInfo
DominatorTree *DT; // Dominator Tree for the current Loop.
LAMPLoadProfile *LLP; // LAMP profiling
PredIteratorCache PredCache_; // Cache fetch predecessor of a BB
typedef std::pair<Instruction *, Instruction *> AntiDepPairTy;
typedef SmallVector<Instruction *, 16> AntiDepPathTy;
// Intermediary data structure 1.
typedef SmallVector<AntiDepPairTy, 16> AntiDepPairs;
AntiDepPairs AntiDepPairs_;
// Intermediary data structure 2.
typedef SmallVector<AntiDepPathTy, 16> AntiDepPaths;
AntiDepPaths AntiDepPaths_;
// Hitting set of instructions
typedef SmallPtrSet<Instruction *, 16> SmallPtrSetTy;
SmallPtrSetTy HittingSet_;
// NEW
// Dynamic Load/Store Path
AntiDepPairs DynamicPairs_;
// pass constructor
idenRegion() : FunctionPass(ID) {}
// get the profile information
void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<DominatorTree>();
AU.addRequired<LoopInfo>();
AU.addRequired<AliasAnalysis>();
AU.addRequired<LAMPLoadProfile>();
}
// Find all necessary information about Function
virtual bool runOnFunction(Function &F);
//===----------------------------------------------------------------------===//
// Helpers
//===----------------------------------------------------------------------===//
////////////////
// New Begin
////////////////
bool isAntiDepPair(LoadInst *Load, StoreInst *Store); // for Dynamic analysis
bool IsStoreInDynPairs(StoreInst *Store); // check if a store inside a Dynamic Pairs
// if so, add to dependency pair directly
////////////////
// New End
////////////////
void findAntidependencePairs(StoreInst *Store);
bool scanForAliasingLoad(BasicBlock::iterator I,
BasicBlock::iterator E,
StoreInst *Store,
Value *StoreDst,
unsigned StoreDstSize);
void computeAntidependencePaths();
void computeHittingSet();
// return a set of BB that need cut
std::set<BasicBlock *> computeHittingSetinBB();
Instruction* findLargestCount(std::map<Instruction *, int> Map);
//===----------------------------------------------------------------------===//
// Printers
//===----------------------------------------------------------------------===//
// print instruction and its BB location
std::string getLocator(const Instruction &I) {
unsigned Offset = 1;
const BasicBlock *BB = I.getParent();
for (BasicBlock::const_iterator It = I; It != BB->begin(); --It)
++Offset;
std::stringstream SS;
SS << BB->getName().str() << ":" << Offset;
return SS.str();
}
// display anti-dependency pair
void printPair(const AntiDepPairTy &P) {
errs() << "( " << getLocator(*P.first) << ", "
<< getLocator(*P.second) << " )";
}
// display anti-dependency pairs
void printPairs(AntiDepPairs ADP) {
errs() << "[ ";
for (AntiDepPairs::iterator I = ADP.begin(), E = ADP.end(), First = I; I != E; I++) {
if (First != I)
errs() << ", ";
printPair(*I);
}
errs() << " ]";
}
// display anti-dependency path
void printPath(const AntiDepPathTy &P) {
errs() << "[ ";
for (AntiDepPathTy::const_iterator I = P.begin(), First = I,
E = P.end(); I != E; ++I) {
if (I != First)
errs() << ", ";
errs() << getLocator(**I);
}
errs() << " ]";
}
// display anti-dependency collection
void printCollection(const AntiDepPaths &PS) {
errs() << "{ ";
for (AntiDepPaths::const_iterator I = PS.begin(), First = I,
E = PS.end(); I != E; ++I) {
if (I != First)
errs() << ", ";
printPath(*I);
}
errs() << " }";
}
// print Inst -> index
void printMap(std::map<Instruction *, int> Map) {
for (std::map<Instruction *, int>::iterator I = Map.begin(), E = Map.end(); I != E; I++) {
errs() << " " << getLocator(*(I->first)) << " --> " << I->second << "\n";
}
}
// print index -> Inst
void printMap(std::map<int, Instruction *> Map) {
for (std::map<int, Instruction *>::iterator I = Map.begin(), E = Map.end(); I != E; I++) {
errs() << " " << I->first << "-->" << getLocator(*(I->second)) << "\n";
}
}
// print inst -> list of position
void printMap(std::map<Instruction *, std::set<int> > Map) {
for (std::map<Instruction *, std::set<int> >::iterator I = Map.begin(), E = Map.end(); I != E; I++) {
errs() << " " << getLocator(*(I->first)) << " --> ";
for (std::set<int>::iterator II = I->second.begin(), EE = I->second.end(); II != EE; II++) {
errs() << *II << " ";
}
errs() << "\n";
}
}
// print 2D array
void print2Darray(int *array, int len, std::map<int, Instruction *>idToinst) {
std::string del = " ";
errs() << del;
for (std::map<int, Instruction *>::iterator I = idToinst.begin(), E = idToinst.end(); I != E; I++) {
errs() << getLocator(*(I->second)) << del;
}
errs() << "\n";
for (int i = 0; i < len; i++) {
errs() << getLocator(*(idToinst[i])) << del;
for (int j = 0; j < len; j++) {
errs() << array[i * len + j] << del;
}
errs() << "\n";
}
}
// print Hitting set
void printHittingSet(const SmallPtrSetTy &SPS) {
errs() << "[ ";
int i = 0;
for (SmallPtrSetTy::iterator I = SPS.begin(), E = SPS.end(), First = I; I != E; I++, i++) {
if (First != I)
errs() << ", ";
errs() << getLocator(**I);
}
errs() << " ]\n";
errs() << "Hitting set Length is " << i << "\n";
}
// print Set
void printSet(std::set<BasicBlock *> BBSet) {
errs() << "[ ";
int i = 0;
for (std::set<BasicBlock *>::iterator I = BBSet.begin(), E = BBSet.end(), First = I; I != E; I++, i++) {
if (First != I)
errs() << ", ";
errs() << (*I)->getName().str();
}
errs() << " ]\n";
errs() << "Hitting BB Length is " << i << "\n";
}
};
}
char idenRegion::ID = 0;
static RegisterPass<idenRegion> X("idenRegion-dynamic", "EECS 583 project", false, false);
bool idenRegion::runOnFunction(Function &F) {
// Get our Loop and Alias Analysis information...
LI = &getAnalysis<LoopInfo>();
AA = &getAnalysis<AliasAnalysis>();
DT = &getAnalysis<DominatorTree>();
LLP = &getAnalysis<LAMPLoadProfile>();
typedef std::map<std::pair<Instruction*, Instruction*>*, unsigned int> dynamicDepMapTy;
typedef std::map<BasicBlock*, std::set<std::pair<Instruction*, Instruction*>* > > LoopToDepTy;
typedef std::set<std::pair<Instruction*, Instruction*>* > instSetTy;
dynamicDepMapTy dynamicDepMap = LLP->DepToTimesMap;
LoopToDepTy LoopToDep = LLP->LoopToDepSetMap;
//////////////
// NEW begin
//////////////
errs() << "*********************************************\n";
errs() << "************* LAMP information **************\n";
errs() << "*********************************************\n";
errs() << " Inst_1 --> Innt_2\tCount\n";
for (dynamicDepMapTy::iterator I = dynamicDepMap.begin(), E = dynamicDepMap.end(); I != E; I++) {
Instruction *firstInst = I->first->first;
Instruction *secondInst = I->first->second;
errs() << *(firstInst->getType()) << ";" << getLocator(*firstInst) << " --> " \
<< *(secondInst->getType()) << ";" << getLocator(*secondInst) << "\t" << I->second << "\n";
// push load/store into Dynamic pair if load/store is actually anti-dep
if (isa<LoadInst>(firstInst) && isa<StoreInst>(secondInst)) {
LoadInst *Load = dyn_cast<LoadInst>(firstInst);
StoreInst *Store = dyn_cast<StoreInst>(secondInst);
if (Load && Store){
errs() << "Working on the load/store pair ...\n";
if (isAntiDepPair(Load, Store)) {
AntiDepPairTy dynPair = AntiDepPairTy(Load, Store);
DynamicPairs_.push_back(dynPair);
}
}
}
}
errs() << "#############################\n";
errs() << "#### Right dynamic pairs: \n";
errs() << "#############################\n";
printPairs(DynamicPairs_);
errs() << "\n";
/////////////
// New end
/////////////
errs() << "---------------------------------------------\n";
errs() << "----------Find Anti-dependency region--------\n";
errs() << "---------------------------------------------\n";
errs() << "----------Compute Memory Antidependency Pairs---------\n";
for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
errs() << "##### BB #####" << "\n";
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
//////////////
// NEW begin
//////////////
// check if we already find the load/store pair in the LAMP profile info
if (!IsStoreInDynPairs(Store)) {
findAntidependencePairs(Store);
}
//////////////
// New End
//////////////
}
}
}
errs() << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
errs() << "^^^^^^ Anti-Dep Pair ^^^^^^^\n";
errs() << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
printPairs(AntiDepPairs_);
errs() << "\n";
if (AntiDepPairs_.empty())
return false;
errs() << "---------------------------------------------\n";
errs() << "----------Find anti-dependency Path----------\n";
errs() << "---------------------------------------------\n";
computeAntidependencePaths();
errs() << "---------------------------------------------\n";
errs() << "----------Compute the Hitting Set------------\n";
errs() << "---------------------------------------------\n";
computeHittingSet();
errs() << "!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
errs() << "!!!!! Hitting Set is !!!!!!\n";
errs() << "!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
printHittingSet(HittingSet_);
errs() << "!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
errs() << "!!!! Hitting Set BB is !!!!\n";
errs() << "!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
printSet(computeHittingSetinBB());
return false;
}
///////////////////
// NEW begin
///////////////////
// check dynamic pair satisfy anti-dependency
bool idenRegion::isAntiDepPair(LoadInst *Load, StoreInst *Store) {
// perform a DFS to check if store is after load
typedef std::pair<BasicBlock *, BasicBlock::iterator> WorkItem;
SmallVector<WorkItem, 8> Worklist;
SmallPtrSet<BasicBlock *, 32> Visited;
BasicBlock *LoadBB = Load->getParent();
Worklist.push_back(WorkItem(LoadBB, Load));
do {
BasicBlock *BB;
BasicBlock::iterator I, E;
tie(BB, I) = Worklist.pop_back_val();
errs() << "... On BB " << BB->getName() << "\n";
// If we revisited LoadBB, we scan to Load to complete cycle
// Otherwise we end at BB->end()
E = (BB == LoadBB && I == BB->begin()) ? Load : BB->end();
// errs() << "... Last instruction on current BB is " << getLocator(*E) << "\n";
// iterate throught BB to check if Load instruction exist in the BB
while (I != E) {
// errs() << "...... Inst: " << getLocator(*I) << "\n";
if (isa<StoreInst>(I) && dyn_cast<StoreInst>(I) == Store) {
return true;
}
++I;
}
// get current BB's succesor
TerminatorInst* ti = BB->getTerminator();
int numSuccesor = ti->getNumSuccessors();
for (int i = 0; i < numSuccesor; i++) {
BasicBlock* nextSuc = ti->getSuccessor(i);
// don't count backedge
if (Visited.insert(nextSuc) && !DT->dominates(nextSuc, BB)) {
Worklist.push_back(WorkItem(nextSuc, nextSuc->begin()));
}
}
} while(!Worklist.empty());
return false;
}
bool idenRegion::IsStoreInDynPairs(StoreInst *Store) {
// iterate dynamic analysis
for (AntiDepPairs::iterator I = DynamicPairs_.begin(), E = DynamicPairs_.end(); I != E; I++) {
if (dyn_cast<StoreInst>(I->second) == Store) {
if (LoadInst* Load = dyn_cast<LoadInst>(I->first)) {
AntiDepPairTy Pair = AntiDepPairTy(Load, Store);
// insert into dependent path
AntiDepPairs_.push_back(Pair);
return true;
}
}
}
return false;
}
////////////////
// New End
////////////////
void idenRegion::findAntidependencePairs(StoreInst *Store) {
// errs() << "** Analyzing Store: " << *Store << "\n";
// errs() << "** At location: " << getLocator(*Store) << "\n";
Value *StoreDst = Store->getOperand(1); // dst
// Value *First = Store->getOperand(0); // src
// errs() << "Store src type is " << *(First->getType()) << "\n";
unsigned StoreDstSize = AA->getTypeStoreSize(Store->getOperand(0)->getType());
// Perform a reverse depth-first search to find aliasing loads.
typedef std::pair<BasicBlock *, BasicBlock::iterator> WorkItem;
SmallVector<WorkItem, 8> Worklist;
SmallPtrSet<BasicBlock *, 32> Visited;
BasicBlock *StoreBB = Store->getParent();
Worklist.push_back(WorkItem(StoreBB, Store));
do {
BasicBlock *BB;
BasicBlock::iterator I, E;
tie(BB, I) = Worklist.pop_back_val();
// If we are revisiting StoreBB, we scan to Store to complete the cycle.
// Otherwise we end at BB->begin().
E = (BB == StoreBB && I == BB->end()) ? Store : BB->begin();
// Scan for an aliasing load. Terminate this path if we see one or a cut is
// already forced.
if (scanForAliasingLoad(I, E, Store, StoreDst, StoreDstSize))
continue;
// If the path didn't terminate, continue on to predecessors.
// errs() << "###### Predecessor Info #######" << "\n";
for (BasicBlock **P = PredCache_.GetPreds(StoreBB); *P; ++P) {
//errs() << "## Name is " << (*P)->getName() << "\n";
if (Visited.insert(*P))
Worklist.push_back(WorkItem((*P), (*P)->end()));
}
} while (!Worklist.empty());
}
bool idenRegion::scanForAliasingLoad (BasicBlock::iterator I,
BasicBlock::iterator E,
StoreInst *Store,
Value *StoreDst,
unsigned StoreDstSize) {
// I is the end of the instruction, E is the begining of the instruction
while (I != E) {
--I;
if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
// Load all the may alias case
if (AA->getModRefInfo(Load, StoreDst, StoreDstSize) & AliasAnalysis::Ref) {
errs() << "!!!!Detect AntiDep Pair!!!!\n";
AntiDepPairTy Pair = AntiDepPairTy(I, Store);
errs() << "~~~ First: " << *(Pair.first) << "\n";
errs() << "~~~ At location " << getLocator(*(Pair.first)) << "\n";
errs() << "~~~ Second: " << *(Pair.second) << "\n";
errs() << "~~~ At location " << getLocator(*(Pair.second)) << "\n";
AntiDepPairs_.push_back(Pair);
return true;
}
}
}
return false;
}
void idenRegion::computeAntidependencePaths() {
// Iterate through every pair of antidependency
// Record all the store along the path
for (AntiDepPairs::iterator I = AntiDepPairs_.begin(), E = AntiDepPairs_.end(); I != E; I++) {
BasicBlock::iterator Load, Store;
tie(Load, Store) = *I;
// create a new path
AntiDepPaths_.resize(AntiDepPaths_.size()+1);
AntiDepPathTy &newPath = AntiDepPaths_.back();
// Always record current store
newPath.push_back(Store);
// Load and store in the same basic block
BasicBlock::iterator curInst = Store;
BasicBlock *LoadBB = Load->getParent(), *StoreBB = Store->getParent();
if (LoadBB == StoreBB && DT->dominates(Load, Store)) {
while(--curInst != Load) {
if (isa<StoreInst>(curInst))
newPath.push_back(curInst);
}
errs() << "@@@ Local BB: \n@@@ ";
printPath(newPath);
errs() << "\n";
continue;
}
// Load and store in different basic block
BasicBlock *curBB = StoreBB;
DomTreeNode *curDTNode = DT->getNode(StoreBB), *LoadDTNode = DT->getNode(LoadBB);
// loop until load is not
while (DT->dominates(LoadDTNode, curDTNode)) {
errs() << "^^^^^ Current BB is " << curBB->getName() << "\n";
BasicBlock::iterator E;
// check if Load and current node in the same BB
if (curBB == LoadBB) {
E = Load;
} else {
E = curBB->begin();
}
// scan current BB
while(curInst != E) {
if (isa<StoreInst>(--curInst)) {
newPath.push_back(curInst);
}
}
// find current Node's iDOM
curDTNode = curDTNode->getIDom();
if (curDTNode == NULL)
break;
curBB = curDTNode->getBlock();
curInst = curBB->end();
}
errs() << "@@@ Inter BB: \n@@@ ";
printPath(newPath);
errs() << "\n";
}
errs() << "Path cap is " << AntiDepPaths_.capacity() << "\n";
errs() << "Path size is " << AntiDepPaths_.size() << "\n";
errs() << "#########################################################\n";
errs() << "#################### Paths Summary ######################\n";
errs() << "#########################################################\n";
printCollection(AntiDepPaths_);
errs() << "\n";
}
Instruction* idenRegion::findLargestCount(std::map<Instruction *, int> Map) {
// TODO: linear right now, optimize later
Instruction* temp_max;
int max = 0;
for (std::map<Instruction *, int>::iterator I = Map.begin(), E = Map.end(); I != E; I++) {
// exclude those in the hitting set
if (I->second > max) {
temp_max = I->first;
max = I->second;
}
}
return temp_max;
}
void idenRegion::computeHittingSet() {
std::vector<AntiDepPathTy> collectionPaths;
typedef std::map<Instruction *, int> instCountTy;
typedef std::map<Instruction *, std::set<int> > instPosTy;
instCountTy instCount;
instPosTy instPos;
int index = 0;
for (AntiDepPaths::iterator I = AntiDepPaths_.begin(), E = AntiDepPaths_.end(); I != E; I++, index++) {
collectionPaths.push_back(*I);
errs() << " " << index << ": ";
printPath(*I);
errs() << "\n";
// construct the map
for (AntiDepPathTy::iterator II = I->begin(), EE = I->end(); II != EE; II++) {
instCount[*II] += 1;
instPos[*II].insert(index);
}
}
errs() << "~~~~ Inst Count Map:\n";
printMap(instCount);
errs() << "~~~~ Inst Position Map:\n";
printMap(instPos);
// Generate the Hitting set based on Map information
int totalPaths = collectionPaths.size();
std::set<int> HittedSet;
while ((int)HittedSet.size() < totalPaths) {
bool increase = false;
int oldLen = HittedSet.size();
while (!increase) {
Instruction* maxCountInst = findLargestCount(instCount);
std::set<int> tempPos = instPos[maxCountInst];
for (std::set<int>::iterator I = tempPos.begin(), E = tempPos.end(); I != E; I++) {
HittedSet.insert(*I);
}
instCount.erase(maxCountInst);
if ((int)HittedSet.size() > oldLen) {
increase = true;
HittingSet_.insert(maxCountInst);
}
}
}
}
std::set<BasicBlock *> idenRegion::computeHittingSetinBB() {
std::set<BasicBlock *> HittingSetBB;
for (SmallPtrSetTy::iterator I = HittingSet_.begin(), E = HittingSet_.end(); I != E; I++) {
HittingSetBB.insert((*I)->getParent());
}
return HittingSetBB;
}