-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.cpp
231 lines (203 loc) · 5.87 KB
/
utility.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
// STL dependencies
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <list>
#include <utility>
#include <string>
#include <regex>
#include <iterator>
#include <set>
// LLVM dependencies
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/AbstractCallSite.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Analysis/DDG.h"
#include "llvm/Analysis/DDGPrinter.h"
#include "llvm/IR/Constants.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/ADT/Statistic.h"
// JSON dependencies
#include <jsoncpp/json/json.h>
// GraphViz dependencies
// #include <graphviz/gvc.h>
// This dependency is not working at the moment. Lib file not found.
// [TODO: Fix GraphViz dependency issue. ]
// Boost dependencies
#include <boost/algorithm/string.hpp>
using namespace llvm;
using namespace std;
typedef list<string> PATH;
typedef pair<string, string> EDGE;
typedef vector<string> EDGE_LIST;
typedef map<string, EDGE_LIST> GRAPH;
namespace
{
static string getSimpleNodeLabel(const BasicBlock &Node)
{ // Copied from Stack Overflow
if (!Node.getName().empty())
return Node.getName().str();
string Str;
raw_string_ostream OS(Str);
Node.printAsOperand(OS, false);
return OS.str();
}
static string getSimpleNodeLabel(const BasicBlock *Node)
{ // Pointer Version
if (!Node->getName().empty())
{
return Node->getName().str();
}
string Str;
raw_string_ostream OS(Str);
Node->printAsOperand(OS, false);
return OS.str();
}
static string getStringRepresentationOfValue(Value *value)
{
string s;
raw_string_ostream OS(s);
value->printAsOperand(OS, false);
return OS.str();
}
static string getTypeFromAddress(Type *type)
{
string s;
raw_string_ostream OS(s);
type->print(OS, false);
return OS.str();
}
static string getInstructionString(Instruction *inst)
{
string s;
raw_string_ostream ss(s);
ss << *inst;
return ss.str();
}
static void parseInlineAssemblyString(string instructionString, CallInst *call)
{
// errs() << "\n\n";
regex pattern("\"([^\"]*)\"");
sregex_iterator start(instructionString.begin(), instructionString.end(), pattern);
sregex_iterator end;
for (sregex_iterator current = start; current != end; ++current)
{
smatch match = *current;
// errs() << match.str() << "\n";
}
// errs() << "Parsing complete.\n";
assert(call != NULL);
int numOperands = call->getNumOperands();
for (int i = 0; i < numOperands - 1; i++)
{
Value *operand = call->getArgOperand(i);
// operand->print(errs(), false);
// errs() << "\n";
}
}
static string getLeftHandSide(Instruction *inst)
{
// Untested Functionality
string Str;
raw_string_ostream OS(Str);
inst->print(OS, false);
string instString = OS.str();
size_t foundAt = instString.find('=');
string returnValue;
if (foundAt != string::npos)
{
returnValue = instString.substr(0, foundAt);
boost::trim(returnValue);
}
return returnValue;
}
static string getRightHandSide(Instruction *inst)
{
string Str;
raw_string_ostream OS(Str);
inst->print(OS, false);
string instString = OS.str();
size_t foundAt = instString.find('=');
string returnValue;
int len = instString.length();
if (foundAt != string::npos)
{
returnValue = instString.substr(foundAt + 1);
boost::trim(returnValue);
}
return returnValue;
}
static void printEdgeList(vector<pair<string, string>> eList)
{
for (pair<string, string> edge : eList)
{
errs() << edge.first << " -> " << edge.second << "\n";
}
}
static void printLoopingBlocks(vector<string> loopingBlocks)
{
errs() << "Looping blocks are: ";
for (auto elem : loopingBlocks)
{
errs() << elem << " ";
}
errs() << "\n";
}
static void printAdjacencyList(map<string, vector<string>> _adjacencyList)
{
errs() << "Adjacency List Is:\n";
for (auto key : _adjacencyList)
{
errs() << key.first << " -> ";
for (auto elem : key.second)
{
errs() << elem << ", ";
}
errs() << "\n";
}
}
static void printPath(PATH p)
{
errs() << "START -> ";
for (string s : p)
{
errs() << s << " ->";
}
errs() << " END\n";
}
static void printPaths(vector<PATH> _paths)
{
int pathNum = 0;
for (PATH path : _paths)
{
errs() << "Path Number: " << ++pathNum << "\n";
printPath(path);
}
}
static void printBackEdges(map<string, EDGE> backEdges)
{
for (auto &elem : backEdges)
{
errs() << elem.first << " : " << elem.second.first << " -> " << elem.second.second << "\n";
}
}
static void printLoopExecutionPaths(map<string, vector<PATH>> pathsInLoops)
{
for (auto &elem : pathsInLoops)
{
int numberOfPaths = elem.second.size();
errs() << "There are " << numberOfPaths << " in the loop anchored at: " << elem.first << "\n";
printPaths(elem.second);
}
}
}