aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Analysis/DataStructure/DataStructure.cpp
blob: 923bca5f34725d97dd34c4f9fda9e025d34c6e8f (plain)
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
//===- DataStructure.cpp - Analysis for data structure identification -------=//
//
// Implement the LLVM data structure analysis library.
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/DataStructure.h"
#include "llvm/Module.h"
#include <fstream>
#include <algorithm>
#include <iostream>

//===----------------------------------------------------------------------===//
// DataStructure Class Implementation
//

AnalysisID DataStructure::ID(AnalysisID::create<DataStructure>());

// releaseMemory - If the pass pipeline is done with this pass, we can release
// our memory... here...
void DataStructure::releaseMemory() {
  for (InfoMap::iterator I = DSInfo.begin(), E = DSInfo.end(); I != E; ++I) {
    delete I->second.first;
    delete I->second.second;
  }

  // Empty map so next time memory is released, data structures are not
  // re-deleted.
  DSInfo.clear();
}

// FIXME REMOVE
#include <sys/time.h>
#include "Support/CommandLine.h"

cl::Flag   Time("t", "Print analysis time...");


// print - Print out the analysis results...
void DataStructure::print(std::ostream &O, Module *M) const {
  if (Time) {
    timeval TV1, TV2;
    gettimeofday(&TV1, 0);
    for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
      if (!I->isExternal() && I->getName() == "main") {
        //getDSGraph(*I);
        getClosedDSGraph(I);
      }
    gettimeofday(&TV2, 0);
    std::cerr << "Analysis took "
         << (TV2.tv_sec-TV1.tv_sec)*1000000+(TV2.tv_usec-TV1.tv_usec)
         << " microseconds.\n";
  }

  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
    if (!I->isExternal()) {

      std::string Filename = "ds." + I->getName() + ".dot";
      O << "Writing '" << Filename << "'...";
      std::ofstream F(Filename.c_str());

      if (F.good()) {
        F << "digraph DataStructures {\n"
          << "\tnode [shape=Mrecord];\n"
          << "\tedge [arrowtail=\"dot\"];\n"
          << "\tsize=\"10,7.5\";\n"
          << "\trotate=\"90\";\n";

        getDSGraph(I).printFunction(F, "Local");
        getClosedDSGraph(I).printFunction(F, "Closed");

        F << "}\n";
      } else {
        O << "  error opening file for writing!\n";
      }

      if (Time) 
        O << " [" << getDSGraph(I).getGraphSize() << ", "
          << getClosedDSGraph(I).getGraphSize() << "]\n";
      else
        O << "\n";
    }
}


//===----------------------------------------------------------------------===//
// PointerVal Class Implementation
//

void PointerVal::print(std::ostream &O) const {
  if (Node) {
    O << "  Node: " << Node->getCaption() << "[" << Index << "]\n";
  } else {
    O << "  NULL NODE\n";
  }
}

//===----------------------------------------------------------------------===//
// PointerValSet Class Implementation
//

void PointerValSet::addRefs() {
  for (unsigned i = 0, e = Vals.size(); i != e; ++i)
    Vals[i].Node->addReferrer(this);
}

void PointerValSet::dropRefs() {
  for (unsigned i = 0, e = Vals.size(); i != e; ++i)
    Vals[i].Node->removeReferrer(this);
}

const PointerValSet &PointerValSet::operator=(const PointerValSet &PVS) {
  dropRefs();
  Vals.clear();
  Vals = PVS.Vals;
  addRefs();
  return *this;
}

// operator< - Allow insertion into a map...
bool PointerValSet::operator<(const PointerValSet &PVS) const {
  if (Vals.size() < PVS.Vals.size()) return true;
  if (Vals.size() > PVS.Vals.size()) return false;
  if (Vals.size() == 1) return Vals[0] < PVS.Vals[0];  // Most common case

  std::vector<PointerVal> S1(Vals), S2(PVS.Vals);
  sort(S1.begin(), S1.end());
  sort(S2.begin(), S2.end());
  return S1 < S2;
}

bool PointerValSet::operator==(const PointerValSet &PVS) const {
  if (Vals.size() != PVS.Vals.size()) return false;
  if (Vals.size() == 1) return Vals[0] == PVS.Vals[0];  // Most common case...

  std::vector<PointerVal> S1(Vals), S2(PVS.Vals);
  sort(S1.begin(), S1.end());
  sort(S2.begin(), S2.end());
  return S1 == S2;
}


bool PointerValSet::add(const PointerVal &PV, Value *Pointer) {
  if (std::find(Vals.begin(), Vals.end(), PV) != Vals.end())
    return false;
  Vals.push_back(PV);
  if (Pointer) PV.Node->addPointer(Pointer);
  PV.Node->addReferrer(this);
  return true;
}

// removePointerTo - Remove a single pointer val that points to the specified
// node...
void PointerValSet::removePointerTo(DSNode *Node) {
  std::vector<PointerVal>::iterator I = std::find(Vals.begin(), Vals.end(), Node);
  assert(I != Vals.end() && "Couldn't remove nonexistent edge!");
  Vals.erase(I);
  Node->removeReferrer(this);
}


void PointerValSet::print(std::ostream &O) const {
  for (unsigned i = 0, e = Vals.size(); i != e; ++i)
    Vals[i].print(O);
}