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
33 // print - Print out the analysis results...
34 void 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()) {
38 string Filename = "ds." + (*I)->getName() + ".dot";
39 O << "Writing '" << Filename << "'...\n";
40 ofstream F(Filename.c_str());
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";
48 getDSGraph(*I).printFunction(F, "Local");
49 getClosedDSGraph(*I).printFunction(F, "Closed");
53 O << " error opening file for writing!\n";
59 //===----------------------------------------------------------------------===//
60 // PointerVal Class Implementation
63 void PointerVal::print(std::ostream &O) const {
65 O << " Node: " << Node->getCaption() << "[" << Index << "]\n";
71 //===----------------------------------------------------------------------===//
72 // PointerValSet Class Implementation
75 void PointerValSet::addRefs() {
76 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
77 Vals[i].Node->addReferrer(this);
80 void PointerValSet::dropRefs() {
81 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
82 Vals[i].Node->removeReferrer(this);
85 const PointerValSet &PointerValSet::operator=(const PointerValSet &PVS) {
93 // operator< - Allow insertion into a map...
94 bool PointerValSet::operator<(const PointerValSet &PVS) const {
95 if (Vals.size() < PVS.Vals.size()) return true;
96 if (Vals.size() > PVS.Vals.size()) return false;
97 if (Vals.size() == 1) return Vals[0] < PVS.Vals[0]; // Most common case
99 vector<PointerVal> S1(Vals), S2(PVS.Vals);
100 sort(S1.begin(), S1.end());
101 sort(S2.begin(), S2.end());
105 bool PointerValSet::operator==(const PointerValSet &PVS) const {
106 if (Vals.size() != PVS.Vals.size()) return false;
107 if (Vals.size() == 1) return Vals[0] == PVS.Vals[0]; // Most common case...
109 vector<PointerVal> S1(Vals), S2(PVS.Vals);
110 sort(S1.begin(), S1.end());
111 sort(S2.begin(), S2.end());
116 bool PointerValSet::add(const PointerVal &PV, Value *Pointer) {
117 if (std::find(Vals.begin(), Vals.end(), PV) != Vals.end())
120 if (Pointer) PV.Node->addPointer(Pointer);
121 PV.Node->addReferrer(this);
125 // removePointerTo - Remove a single pointer val that points to the specified
127 void PointerValSet::removePointerTo(DSNode *Node) {
128 vector<PointerVal>::iterator I = std::find(Vals.begin(), Vals.end(), Node);
129 assert(I != Vals.end() && "Couldn't remove nonexistent edge!");
131 Node->removeReferrer(this);
135 void PointerValSet::print(std::ostream &O) const {
136 for (unsigned i = 0, e = Vals.size(); i != e; ++i)