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
|
//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the 'dot' graph printer.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/DataStructure/DataStructure.h"
#include "llvm/Analysis/DataStructure/DSGraph.h"
#include "llvm/Analysis/DataStructure/DSGraphTraits.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/GraphWriter.h"
#include "llvm/System/Path.h"
#include "llvm/System/Program.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Config/config.h"
#include <fstream>
#include <sstream>
using namespace llvm;
// OnlyPrintMain - The DataStructure printer exposes this option to allow
// printing of only the graph for "main".
//
namespace {
cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
cl::opt<bool> DontPrintAnything("dont-print-ds", cl::ReallyHidden);
Statistic<> MaxGraphSize ("dsa", "Maximum graph size");
Statistic<> NumFoldedNodes ("dsa", "Number of folded nodes (in final graph)");
}
void DSNode::dump() const { print(std::cerr, 0); }
static std::string getCaption(const DSNode *N, const DSGraph *G) {
std::stringstream OS;
Module *M = 0;
if (!G) G = N->getParentGraph();
// Get the module from ONE of the functions in the graph it is available.
if (G && G->retnodes_begin() != G->retnodes_end())
M = G->retnodes_begin()->first->getParent();
if (M == 0 && G) {
// If there is a global in the graph, we can use it to find the module.
const DSScalarMap &SM = G->getScalarMap();
if (SM.global_begin() != SM.global_end())
M = (*SM.global_begin())->getParent();
}
if (N->isNodeCompletelyFolded())
OS << "COLLAPSED";
else {
WriteTypeSymbolic(OS, N->getType(), M);
if (N->isArray())
OS << " array";
}
if (unsigned NodeType = N->getNodeFlags()) {
OS << ": ";
if (NodeType & DSNode::AllocaNode ) OS << "S";
if (NodeType & DSNode::HeapNode ) OS << "H";
if (NodeType & DSNode::GlobalNode ) OS << "G";
if (NodeType & DSNode::UnknownNode) OS << "U";
if (NodeType & DSNode::Incomplete ) OS << "I";
if (NodeType & DSNode::Modified ) OS << "M";
if (NodeType & DSNode::Read ) OS << "R";
#ifndef NDEBUG
if (NodeType & DSNode::DEAD ) OS << "<dead>";
#endif
OS << "\n";
}
EquivalenceClasses<GlobalValue*> *GlobalECs = 0;
if (G) GlobalECs = &G->getGlobalECs();
for (unsigned i = 0, e = N->getGlobalsList().size(); i != e; ++i) {
WriteAsOperand(OS, N->getGlobalsList()[i], false, true, M);
// Figure out how many globals are equivalent to this one.
if (GlobalECs) {
EquivalenceClasses<GlobalValue*>::iterator I =
GlobalECs->findValue(N->getGlobalsList()[i]);
if (I != GlobalECs->end()) {
unsigned NumMembers =
std::distance(GlobalECs->member_begin(I), GlobalECs->member_end());
if (NumMembers != 1) OS << " + " << (NumMembers-1) << " EC";
}
}
OS << "\n";
}
return OS.str();
}
namespace llvm {
template<>
struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
static std::string getGraphName(const DSGraph *G) {
switch (G->getReturnNodes().size()) {
case 0: return G->getFunctionNames();
case 1: return "Function " + G->getFunctionNames();
default: return "Functions: " + G->getFunctionNames();
}
}
static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
return getCaption(Node, Graph);
}
static std::string getNodeAttributes(const DSNode *N) {
return "shape=Mrecord";
}
static bool edgeTargetsEdgeSource(const void *Node,
DSNode::const_iterator I) {
unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
return (O >> DS::PointerShift) != 0;
}
static DSNode::const_iterator getEdgeTarget(const DSNode *Node,
DSNode::const_iterator I) {
unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
unsigned LinkNo = O >> DS::PointerShift;
const DSNode *N = *I;
DSNode::const_iterator R = N->begin();
for (; LinkNo; --LinkNo)
++R;
return R;
}
/// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
/// and the return node.
///
static void addCustomGraphFeatures(const DSGraph *G,
GraphWriter<const DSGraph*> &GW) {
Module *CurMod = 0;
if (G->retnodes_begin() != G->retnodes_end())
CurMod = G->retnodes_begin()->first->getParent();
else {
// If there is a global in the graph, we can use it to find the module.
const DSScalarMap &SM = G->getScalarMap();
if (SM.global_begin() != SM.global_end())
CurMod = (*SM.global_begin())->getParent();
}
// Add scalar nodes to the graph...
const DSGraph::ScalarMapTy &VM = G->getScalarMap();
for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
if (!isa<GlobalValue>(I->first)) {
std::stringstream OS;
WriteAsOperand(OS, I->first, false, true, CurMod);
GW.emitSimpleNode(I->first, "", OS.str());
// Add edge from return node to real destination
DSNode *DestNode = I->second.getNode();
int EdgeDest = I->second.getOffset() >> DS::PointerShift;
if (EdgeDest == 0) EdgeDest = -1;
GW.emitEdge(I->first, -1, DestNode,
EdgeDest, "arrowtail=tee,color=gray63");
}
// Output the returned value pointer...
for (DSGraph::retnodes_iterator I = G->retnodes_begin(),
E = G->retnodes_end(); I != E; ++I)
if (I->second.getNode()) {
std::string Label;
if (G->getReturnNodes().size() == 1)
Label = "returning";
else
Label = I->first->getName() + " ret node";
// Output the return node...
GW.emitSimpleNode((void*)I->first, "plaintext=circle", Label);
// Add edge from return node to real destination
DSNode *RetNode = I->second.getNode();
int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
if (RetEdgeDest == 0) RetEdgeDest = -1;
GW.emitEdge((void*)I->first, -1, RetNode,
RetEdgeDest, "arrowtail=tee,color=gray63");
}
// Output all of the call nodes...
const std::list<DSCallSite> &FCs =
G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
: G->getFunctionCalls();
for (std::list<DSCallSite>::const_iterator I = FCs.begin(), E = FCs.end();
I != E; ++I) {
const DSCallSite &Call = *I;
std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
EdgeSourceCaptions[0] = "r";
if (Call.isDirectCall())
EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
else
EdgeSourceCaptions[1] = "f";
GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
&EdgeSourceCaptions);
if (DSNode *N = Call.getRetVal().getNode()) {
int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
if (EdgeDest == 0) EdgeDest = -1;
GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
}
// Print out the callee...
if (Call.isIndirectCall()) {
DSNode *N = Call.getCalleeNode();
assert(N && "Null call site callee node!");
GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
}
for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
if (DSNode *N = Call.getPtrArg(j).getNode()) {
int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
if (EdgeDest == 0) EdgeDest = -1;
GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
}
}
}
};
} // end namespace llvm
void DSNode::print(std::ostream &O, const DSGraph *G) const {
GraphWriter<const DSGraph *> W(O, G);
W.writeNode(this);
}
void DSGraph::print(std::ostream &O) const {
WriteGraph(O, this, "DataStructures");
}
void DSGraph::writeGraphToFile(std::ostream &O,
const std::string &GraphName) const {
std::string Filename = GraphName + ".dot";
O << "Writing '" << Filename << "'...";
std::ofstream F(Filename.c_str());
if (F.good()) {
print(F);
unsigned NumCalls = shouldPrintAuxCalls() ?
getAuxFunctionCalls().size() : getFunctionCalls().size();
O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
} else {
O << " error opening file for writing!\n";
}
}
/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
/// then cleanup. For use from the debugger.
///
void DSGraph::viewGraph() const {
char pathsuff[9];
sprintf(pathsuff, "%06u", unsigned(rand()));
sys::Path TempDir = sys::Path::GetTemporaryDirectory();
sys::Path Filename = TempDir;
Filename.appendComponent("ds.tempgraph." + std::string(pathsuff) + ".dot");
std::cerr << "Writing '" << Filename << "'... ";
std::ofstream F(Filename.c_str());
if (!F.good()) {
std::cerr << " error opening file for writing!\n";
return;
}
print(F);
F.close();
std::cerr << "\n";
#if HAVE_GRAPHVIZ
sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
std::vector<const char*> args;
args.push_back(Graphviz.c_str());
args.push_back(Filename.c_str());
args.push_back(0);
std::cerr << "Running 'Graphviz' program... " << std::flush;
if (sys::Program::ExecuteAndWait(Graphviz, &args[0])) {
std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
} else {
Filename.eraseFromDisk();
return;
}
#elif (HAVE_GV && HAVE_DOT)
sys::Path PSFilename = TempDir;
PSFilename.appendComponent(std::string("ds.tempgraph") + "." + pathsuff + ".ps");
sys::Path dot(LLVM_PATH_DOT);
std::vector<const char*> args;
args.push_back(dot.c_str());
args.push_back("-Tps");
args.push_back("-Nfontname=Courier");
args.push_back("-Gsize=7.5,10");
args.push_back(Filename.c_str());
args.push_back("-o");
args.push_back(PSFilename.c_str());
args.push_back(0);
std::cerr << "Running 'dot' program... " << std::flush;
if (sys::Program::ExecuteAndWait(dot, &args[0])) {
std::cerr << "Error viewing graph: 'dot' not in path?\n";
} else {
std::cerr << "\n";
sys::Path gv(LLVM_PATH_GV);
args.clear();
args.push_back(gv.c_str());
args.push_back(PSFilename.c_str());
args.push_back(0);
sys::Program::ExecuteAndWait(gv, &args[0]);
}
Filename.eraseFromDisk();
PSFilename.eraseFromDisk();
return;
#elif HAVE_DOTTY
sys::Path dotty(LLVM_PATH_DOTTY);
std::vector<const char*> args;
args.push_back(Filename.c_str());
args.push_back(0);
std::cerr << "Running 'dotty' program... " << std::flush;
if (sys::Program::ExecuteAndWait(dotty, &args[0])) {
std::cerr << "Error viewing graph: 'dotty' not in path?\n";
} else {
#ifndef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
Filename.eraseFromDisk();
#endif
return;
}
#endif
Filename.eraseFromDisk();
TempDir.eraseFromDisk(true);
}
template <typename Collection>
static void printCollection(const Collection &C, std::ostream &O,
const Module *M, const std::string &Prefix) {
if (M == 0) {
O << "Null Module pointer, cannot continue!\n";
return;
}
unsigned TotalNumNodes = 0, TotalCallNodes = 0;
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
if (C.hasGraph(*I)) {
DSGraph &Gr = C.getDSGraph((Function&)*I);
unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
bool IsDuplicateGraph = false;
if (I->getName() == "main" || !OnlyPrintMain) {
Function *SCCFn = Gr.retnodes_begin()->first;
if (&*I == SCCFn) {
Gr.writeGraphToFile(O, Prefix+I->getName());
} else {
IsDuplicateGraph = true; // Don't double count node/call nodes.
O << "Didn't write '" << Prefix+I->getName()
<< ".dot' - Graph already emitted to '" << Prefix+SCCFn->getName()
<< "\n";
}
} else {
Function *SCCFn = Gr.retnodes_begin()->first;
if (&*I == SCCFn) {
O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
<< Gr.getGraphSize() << "+" << NumCalls << "]\n";
} else {
IsDuplicateGraph = true; // Don't double count node/call nodes.
}
}
if (!IsDuplicateGraph) {
unsigned GraphSize = Gr.getGraphSize();
if (MaxGraphSize < GraphSize) MaxGraphSize = GraphSize;
TotalNumNodes += Gr.getGraphSize();
TotalCallNodes += NumCalls;
for (DSGraph::node_iterator NI = Gr.node_begin(), E = Gr.node_end();
NI != E; ++NI)
if (NI->isNodeCompletelyFolded())
++NumFoldedNodes;
}
}
DSGraph &GG = C.getGlobalsGraph();
TotalNumNodes += GG.getGraphSize();
TotalCallNodes += GG.getFunctionCalls().size();
if (!OnlyPrintMain) {
GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
} else {
O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
<< GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
}
O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
<< "] nodes total" << std::endl;
}
// print - Print out the analysis results...
void LocalDataStructures::print(std::ostream &O, const Module *M) const {
if (DontPrintAnything) return;
printCollection(*this, O, M, "ds.");
}
void BUDataStructures::print(std::ostream &O, const Module *M) const {
if (DontPrintAnything) return;
printCollection(*this, O, M, "bu.");
}
void TDDataStructures::print(std::ostream &O, const Module *M) const {
if (DontPrintAnything) return;
printCollection(*this, O, M, "td.");
}
void CompleteBUDataStructures::print(std::ostream &O, const Module *M) const {
if (DontPrintAnything) return;
printCollection(*this, O, M, "cbu.");
}
void EquivClassGraphs::print(std::ostream &O, const Module *M) const {
if (DontPrintAnything) return;
printCollection(*this, O, M, "eq.");
}
|