aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Analysis/DataStructure/TopDownClosure.cpp
blob: b4b43f77c59cd0b20c75bd34c6bddec64d5edb06 (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
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
//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
//
// This file implements the TDDataStructures class, which represents the
// Top-down Interprocedural closure of the data structure graph over the
// program.  This is useful (but not strictly necessary?) for applications
// like pointer analysis.
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/DataStructure.h"
#include "llvm/Analysis/DSGraph.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
#include "Support/Statistic.h"

static RegisterAnalysis<TDDataStructures>
Y("tddatastructure", "Top-down Data Structure Analysis Closure");

// releaseMemory - If the pass pipeline is done with this pass, we can release
// our memory... here...
//
void TDDataStructures::releaseMemory() {
  for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
         E = DSInfo.end(); I != E; ++I)
    delete I->second;

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

// run - Calculate the top down data structure graphs for each function in the
// program.
//
bool TDDataStructures::run(Module &M) {
  BUDataStructures &BU = getAnalysis<BUDataStructures>();

  // Calculate top-down from main...
  if (Function *F = M.getMainFunction())
    calculateGraph(*F);

  // Next calculate the graphs for each function unreachable function...
  for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
    if (!I->isExternal())
      calculateGraph(*I);
  return false;
}

/// ResolveCallSite - This method is used to link the actual arguments together
/// with the formal arguments for a function call in the top-down closure.  This
/// method assumes that the call site arguments have been mapped into nodes
/// local to the specified graph.
///
void TDDataStructures::ResolveCallSite(DSGraph &Graph,
                                       const DSCallSite &CallSite) {
  // Resolve all of the function formal arguments...
  Function &F = Graph.getFunction();
  Function::aiterator AI = F.abegin();

  for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) {
    // Advance the argument iterator to the first pointer argument...
    while (!DS::isPointerType(AI->getType())) ++AI;
    
    // TD ...Merge the formal arg scalar with the actual arg node
    DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
    assert(NodeForFormal.getNode() && "Pointer argument has no dest node!");
    NodeForFormal.mergeWith(CallSite.getPtrArg(i));
  }
  
  // Merge returned node in the caller with the "return" node in callee
  if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode())
    Graph.getRetNode().mergeWith(CallSite.getRetVal());
}

DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
  DSGraph *&G = DSInfo[&F];
  if (G == 0) { // Not created yet?  Clone BU graph...
    G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
    G->getAuxFunctionCalls().clear();
  }
  return *G;
}

void TDDataStructures::calculateGraph(Function &F) {
  // Make sure this graph has not already been calculated, and that we don't get
  // into an infinite loop with mutually recursive functions.
  //
  if (GraphDone.count(&F)) return;
  GraphDone.insert(&F);

  // Get the current functions graph...
  DSGraph &Graph = getOrCreateDSGraph(F);

  const std::vector<DSCallSite> &CallSites = Graph.getFunctionCalls();
  if (CallSites.empty()) {
    DEBUG(std::cerr << "  [TD] No callees for: " << F.getName() << "\n");
    return;  // If no call sites, the graph is the same as the BU graph!
  }

  // Loop over all of the call sites, building a multi-map from Callees to
  // DSCallSite*'s.  With this map we can then loop over each callee, cloning
  // this graph once into it, then resolving arguments.
  //
  std::multimap<Function*, const DSCallSite*> CalleeSites;
  for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
    const DSCallSite &CS = CallSites[i];
    const std::vector<GlobalValue*> Callees =
      CS.getCallee().getNode()->getGlobals();

    // Loop over all of the functions that this call may invoke...
    for (unsigned c = 0, e = Callees.size(); c != e; ++c)
      if (Function *F = dyn_cast<Function>(Callees[c]))  // If this is a fn...
        if (!F->isExternal())                            // If it's not external
          CalleeSites.insert(std::make_pair(F, &CS));    // Keep track of it!
  }

  // Now that we have information about all of the callees, propogate the
  // current graph into the callees.
  //
  DEBUG(std::cerr << "  [TD] Inlining '" << F.getName() << "' into "
                  << CalleeSites.size() << " callees.\n");

  // Loop over all the callees...
  for (std::multimap<Function*, const DSCallSite*>::iterator
         I = CalleeSites.begin(), E = CalleeSites.end(); I != E; )
    if (I->first == &F) {  // Bottom-up pass takes care of self loops!
      ++I;
    } else {
      // For each callee...
      Function *Callee = I->first;
      DSGraph &CG = getOrCreateDSGraph(*Callee);  // Get the callee's graph...
      
      DEBUG(std::cerr << "\t [TD] Inlining into callee '" << Callee->getName()
            << "'\n");
      
      // Clone our current graph into the callee...
      std::map<Value*, DSNodeHandle> OldValMap;
      std::map<const DSNode*, DSNodeHandle> OldNodeMap;
      CG.cloneInto(Graph, OldValMap, OldNodeMap,
                   DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes);
      OldValMap.clear();  // We don't care about the ValMap
      
      // Loop over all of the invocation sites of the callee, resolving
      // arguments to our graph.  This loop may iterate multiple times if the
      // current function calls this callee multiple times with different
      // signatures.
      //
      for (; I != E && I->first == Callee; ++I) {
        // Map call site into callee graph
        DSCallSite NewCS(*I->second, OldNodeMap);
        
        // Resolve the return values...
        NewCS.getRetVal().mergeWith(CG.getRetNode());
        
        // Resolve all of the arguments...
        Function::aiterator AI = Callee->abegin();
        for (unsigned i = 0, e = NewCS.getNumPtrArgs(); i != e; ++i, ++AI) {
          // Advance the argument iterator to the first pointer argument...
          while (!DS::isPointerType(AI->getType())) {
            ++AI;
#ifndef NDEBUG
            if (AI == Callee->aend())
              std::cerr << "Bad call to Function: " << Callee->getName()<< "\n";
#endif
            assert(AI != Callee->aend() &&
                   "# Args provided is not # Args required!");
          }
          
          // Add the link from the argument scalar to the provided value
          DSNodeHandle &NH = CG.getNodeForValue(AI);
          assert(NH.getNode() && "Pointer argument without scalarmap entry?");
          NH.mergeWith(NewCS.getPtrArg(i));
        }
      }

      // Done with the nodemap...
      OldNodeMap.clear();

      // Recompute the Incomplete markers and eliminate unreachable nodes.
      CG.maskIncompleteMarkers();
      CG.markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage()
                             /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
      CG.removeDeadNodes(/*KeepAllGlobals*/ false);
    }

  DEBUG(std::cerr << "  [TD] Done inlining into callees for: " << F.getName()
        << " [" << Graph.getGraphSize() << "+"
        << Graph.getFunctionCalls().size() << "]\n");

  
  // Loop over all the callees... making sure they are all resolved now...
  Function *LastFunc = 0;
  for (std::multimap<Function*, const DSCallSite*>::iterator
         I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I)
    if (I->first != LastFunc) {  // Only visit each callee once...
      LastFunc = I->first;
      calculateGraph(*I->first);
    }
}