blob: d90d84a06b5fd322bae7928e4436139342374302 [file] [log] [blame]
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00001//===- DataStructure.cpp - Analysis for data structure identification -------=//
2//
3// Implement the LLVM data structure analysis library.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/Analysis/DataStructure.h"
8#include "llvm/Module.h"
9#include "llvm/Function.h"
10#include <fstream>
11#include <algorithm>
12
13//===----------------------------------------------------------------------===//
14// DataStructure Class Implementation
15//
16
17AnalysisID DataStructure::ID(AnalysisID::create<DataStructure>());
18
19// releaseMemory - If the pass pipeline is done with this pass, we can release
20// our memory... here...
21void DataStructure::releaseMemory() {
22 for (InfoMap::iterator I = DSInfo.begin(), E = DSInfo.end(); I != E; ++I) {
23 delete I->second.first;
24 delete I->second.second;
25 }
26
27 // Empty map so next time memory is released, data structures are not
28 // re-deleted.
29 DSInfo.clear();
30}
31
32
33// print - Print out the analysis results...
34void DataStructure::print(std::ostream &O, Module *M) const {
35 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
36 if (!(*I)->isExternal()) {
37
38 string Filename = "ds." + (*I)->getName() + ".dot";
39 O << "Writing '" << Filename << "'...\n";
40 ofstream F(Filename.c_str());
41 if (F.good()) {
42 F << "digraph DataStructures {\n"
43 << "\tnode [shape=Mrecord];\n"
44 << "\tedge [arrowtail=\"dot\"];\n"
45 << "\tsize=\"10,7.5\";\n"
46 << "\trotate=\"90\";\n";
47
48 getDSGraph(*I).printFunction(F, "Local");
49 getClosedDSGraph(*I).printFunction(F, "Closed");
50
51 F << "}\n";
52 } else {
53 O << " error opening file for writing!\n";
54 }
55 }
56}
57
58
59//===----------------------------------------------------------------------===//
60// PointerVal Class Implementation
61//
62
63void PointerVal::print(std::ostream &O) const {
64 if (Node) {
65 O << " Node: " << Node->getCaption() << "[" << Index << "]\n";
66 } else {
67 O << " NULL NODE\n";
68 }
69}
70
71//===----------------------------------------------------------------------===//
72// PointerValSet Class Implementation
73//
74
75void PointerValSet::addRefs() {
76 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
77 Vals[i].Node->addReferrer(this);
78}
79
80void PointerValSet::dropRefs() {
81 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
82 Vals[i].Node->removeReferrer(this);
83}
84
85const PointerValSet &PointerValSet::operator=(const PointerValSet &PVS) {
86 dropRefs();
87 Vals.clear();
88 Vals = PVS.Vals;
89 addRefs();
90 return *this;
91}
92
93
94bool PointerValSet::add(const PointerVal &PV, Value *Pointer) {
95 if (std::find(Vals.begin(), Vals.end(), PV) != Vals.end())
96 return false;
97 Vals.push_back(PV);
98 if (Pointer) PV.Node->addPointer(Pointer);
99 PV.Node->addReferrer(this);
100 return true;
101}
102
103// removePointerTo - Remove a single pointer val that points to the specified
104// node...
105void PointerValSet::removePointerTo(DSNode *Node) {
106 vector<PointerVal>::iterator I = std::find(Vals.begin(), Vals.end(), Node);
107 assert(I != Vals.end() && "Couldn't remove nonexistent edge!");
108 Vals.erase(I);
109 Node->removeReferrer(this);
110}
111
112
113void PointerValSet::print(std::ostream &O) const {
114 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
115 Vals[i].print(O);
116}
117