1 //===- DataStructure.cpp - Analysis for data structure identification -------=//
3 // Implement the LLVM data structure analysis library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/DataStructure.h"
8 #include "llvm/Module.h"
9 #include "llvm/Function.h"
13 //===----------------------------------------------------------------------===//
14 // DataStructure Class Implementation
17 AnalysisID DataStructure::ID(AnalysisID::create<DataStructure>());
19 // releaseMemory - If the pass pipeline is done with this pass, we can release
20 // our memory... here...
21 void 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;
27 // Empty map so next time memory is released, data structures are not
34 #include "Support/CommandLine.h"
36 cl::Flag Time("t", "Print analysis time...");
39 // print - Print out the analysis results...
40 void DataStructure::print(std::ostream &O, Module *M) const {
43 gettimeofday(&TV1, 0);
44 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
45 if (!(*I)->isExternal()) {
49 gettimeofday(&TV2, 0);
50 cerr << "Analysis took "
51 << (TV2.tv_sec-TV1.tv_sec)*1000000+(TV2.tv_usec-TV1.tv_usec)
52 << " microseconds.\n";
55 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
56 if (!(*I)->isExternal()) {
58 string Filename = "ds." + (*I)->getName() + ".dot";
59 O << "Writing '" << Filename << "'...";
60 ofstream F(Filename.c_str());
62 F << "digraph DataStructures {\n"
63 << "\tnode [shape=Mrecord];\n"
64 << "\tedge [arrowtail=\"dot\"];\n"
65 << "\tsize=\"10,7.5\";\n"
66 << "\trotate=\"90\";\n";
68 getDSGraph(*I).printFunction(F, "Local");
69 getClosedDSGraph(*I).printFunction(F, "Closed");
73 O << " error opening file for writing!\n";
77 O << " [" << getDSGraph(*I).getGraphSize() << ", "
78 << getClosedDSGraph(*I).getGraphSize() << "]\n";
85 //===----------------------------------------------------------------------===//
86 // PointerVal Class Implementation
89 void PointerVal::print(std::ostream &O) const {
91 O << " Node: " << Node->getCaption() << "[" << Index << "]\n";
97 //===----------------------------------------------------------------------===//
98 // PointerValSet Class Implementation
101 void PointerValSet::addRefs() {
102 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
103 Vals[i].Node->addReferrer(this);
106 void PointerValSet::dropRefs() {
107 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
108 Vals[i].Node->removeReferrer(this);
111 const PointerValSet &PointerValSet::operator=(const PointerValSet &PVS) {
119 // operator< - Allow insertion into a map...
120 bool PointerValSet::operator<(const PointerValSet &PVS) const {
121 if (Vals.size() < PVS.Vals.size()) return true;
122 if (Vals.size() > PVS.Vals.size()) return false;
123 if (Vals.size() == 1) return Vals[0] < PVS.Vals[0]; // Most common case
125 vector<PointerVal> S1(Vals), S2(PVS.Vals);
126 sort(S1.begin(), S1.end());
127 sort(S2.begin(), S2.end());
131 bool PointerValSet::operator==(const PointerValSet &PVS) const {
132 if (Vals.size() != PVS.Vals.size()) return false;
133 if (Vals.size() == 1) return Vals[0] == PVS.Vals[0]; // Most common case...
135 vector<PointerVal> S1(Vals), S2(PVS.Vals);
136 sort(S1.begin(), S1.end());
137 sort(S2.begin(), S2.end());
142 bool PointerValSet::add(const PointerVal &PV, Value *Pointer) {
143 if (std::find(Vals.begin(), Vals.end(), PV) != Vals.end())
146 if (Pointer) PV.Node->addPointer(Pointer);
147 PV.Node->addReferrer(this);
151 // removePointerTo - Remove a single pointer val that points to the specified
153 void PointerValSet::removePointerTo(DSNode *Node) {
154 vector<PointerVal>::iterator I = std::find(Vals.begin(), Vals.end(), Node);
155 assert(I != Vals.end() && "Couldn't remove nonexistent edge!");
157 Node->removeReferrer(this);
161 void PointerValSet::print(std::ostream &O) const {
162 for (unsigned i = 0, e = Vals.size(); i != e; ++i)