-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwaves.cpp
More file actions
177 lines (155 loc) · 4.66 KB
/
waves.cpp
File metadata and controls
177 lines (155 loc) · 4.66 KB
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
/*
* Spatial Computation - Manifestation of Spatial computing.
* Copyright © 2014 Pranav Kant
*
* This code is available to you under Apache License Version 2.0, January
* 2014. You can grab a copy of license from the same repository from where you
* fetched this code.
*/
#include "llvm/ADT/DenseMap.h"
#include "llvm/Analysis/CFGPrinter.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Pass.h"
#include "llvm/PassManager.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/GraphWriter.h"
#include "waves.hpp"
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <iostream>
#include <map>
using namespace llvm;
void WaveScalar::runBFS(){
while(!Q.empty()){
BasicBlock *bb = const_cast<BasicBlock*>(Q.front());
Q.pop_front();
outs() << "Block " << IDMap[bb] << " is in Wave " << waveNo << "\n";
setIMap(bb, waveNo);
const TerminatorInst *TInst = bb->getTerminator();
for (unsigned i = 0, nsucc = TInst->getNumSuccessors(); i < nsucc; i++){
BasicBlock *succ = TInst->getSuccessor(i);
Color succColor = ColorMap[succ];
if (succColor == WaveScalar::WHITE){
ColorMap[succ] = WaveScalar::GREY;
Q.push_back(succ);
}
}
if (BEMap[bb] == 0){
outs() << "Wave advanced at " << IDMap[bb] << "\n";
waveNo++;
}
ColorMap[bb] = WaveScalar::BLACK;
}
}
void WaveScalar::annotateWaves(const Function &F,
SmallVectorImpl<std::pair<const BasicBlock*, const BasicBlock*> > *res){
waveNo = 0;
init(F);
setBackEdges(F, res);
for (Function::const_iterator I = F.begin(), IE = F.end(); I != IE; I++){
ColorMap[I] = WaveScalar::WHITE;
}
Q.push_back(&F.getEntryBlock());
runBFS();
}
int WaveScalar::isSameWave(Instruction* i1,
Instruction *i2){
std::string ppstr;
raw_string_ostream rso1(ppstr);
i1->print(rso1);
std::string str;
raw_string_ostream rso(str);
i2->print(rso);
return IMap[str] == IMap[ppstr];
}
int WaveScalar::isSameWave(std::string i1,
std::string i2){
return IMap[i1] == IMap[i2];
}
int WaveScalar::getWaveNo(std::string instruction){
return IMap[instruction];
}
void WaveScalar::init (const Function &F){
//Intialize the color map.
unsigned K = 0;
for (Function::const_iterator I = F.begin(), IE = F.end(); I != IE; I++, K++){
ColorMap[I] = WaveScalar::WHITE;
IDMap[I] = K;
const BasicBlock *pbb = &*I;
BasicBlock *pp = const_cast<BasicBlock*>(pbb);
setLabel(&pp, K);
BEMap[I] = 1;
}
}
void WaveScalar::setIMap(BasicBlock* bb, unsigned waveNo){
for (BasicBlock::iterator I = bb->begin(), IE = bb->end(); I!=IE; I++){
Instruction *instr = &*I;
std::string ppstr;
raw_string_ostream rso1(ppstr);
instr->print(rso1);
IMap[ppstr] = waveNo;
}
}
void WaveScalar::setBackEdges (const Function &F, SmallVectorImpl<std::pair<const BasicBlock*, const BasicBlock*> > *res){
FindFunctionBackedges(F, *res);
while (!res->empty()){
std::pair<const BasicBlock*, const BasicBlock*> tempPair = res->pop_back_val();
BEMap[tempPair.first] = 0;
BEMap[tempPair.second] = 0;
}
}
void WaveScalar::setLabel(BasicBlock **succ, unsigned k){
(*succ)->setName(Twine(k));
}
/*
Helper Functions
*/
std::string EscapeString(const std::string &Label) {
std::string Str(Label);
for (unsigned i = 0; i != Str.length(); ++i)
switch (Str[i]) {
case '\n':
Str.insert(Str.begin()+i, '\\'); // Escape character...
++i;
Str[i] = 'n';
break;
case '\t':
Str.insert(Str.begin()+i, ' '); // Convert to two spaces
++i;
Str[i] = ' ';
break;
case '\\':
if (i+1 != Str.length())
switch (Str[i+1]) {
case 'l': continue; // don't disturb \l
case '|': case '{': case '}':
Str.erase(Str.begin()+i); continue;
default: break;
}
case '{': case '}':
case '<': case '>':
case '|': case '"':
case '[': case ']':
case '%':
Str.insert(Str.begin()+i, '\\'); // Escape character...
++i; // don't infinite loop
break;
}
return Str;
}
std::string insertWA(){
return " -> WA -> ";
}