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
|
//===- 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) {
// Simply calculate the graphs for each 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 (!DataStructureAnalysis::isPointerType(AI->getType())) ++AI;
// TD ...Merge the formal arg scalar with the actual arg node
DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
if (NodeForFormal.getNode())
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());
}
static DSNodeHandle copyHelper(const DSNodeHandle* fromNode,
std::map<const DSNode*, DSNode*> *NodeMap) {
return DSNodeHandle((*NodeMap)[fromNode->getNode()], fromNode->getOffset());
}
DSGraph &TDDataStructures::calculateGraph(Function &F) {
// Make sure this graph has not already been calculated, or that we don't get
// into an infinite loop with mutually recursive functions.
//
DSGraph *&Graph = DSInfo[&F];
if (Graph) return *Graph;
BUDataStructures &BU = getAnalysis<BUDataStructures>();
DSGraph &BUGraph = BU.getDSGraph(F);
Graph = new DSGraph(BUGraph);
const std::vector<DSCallSite> *CallSitesP = BU.getCallSites(F);
if (CallSitesP == 0) {
DEBUG(std::cerr << " [TD] No callers for: " << F.getName() << "\n");
return *Graph; // If no call sites, the graph is the same as the BU graph!
}
// Loop over all call sites of this function, merging each one into this
// graph.
//
DEBUG(std::cerr << " [TD] Inlining callers for: " << F.getName() << "\n");
const std::vector<DSCallSite> &CallSites = *CallSitesP;
for (unsigned c = 0, ce = CallSites.size(); c != ce; ++c) {
const DSCallSite &CallSite = CallSites[c]; // Copy
Function &Caller = CallSite.getCaller();
assert(!Caller.isExternal() && "Externals function cannot 'call'!");
DEBUG(std::cerr << "\t [TD] Inlining caller #" << c << " '"
<< Caller.getName() << "' into callee: " << F.getName() << "\n");
if (&Caller == &F) {
// Self-recursive call: this can happen after a cycle of calls is inlined.
ResolveCallSite(*Graph, CallSite);
} else {
// Recursively compute the graph for the Caller. That should
// be fully resolved except if there is mutual recursion...
//
DSGraph &CG = calculateGraph(Caller); // Graph to inline
DEBUG(std::cerr << "\t\t[TD] Got graph for " << Caller.getName()
<< " in: " << F.getName() << "\n");
// These two maps keep track of where scalars in the old graph _used_
// to point to, and of new nodes matching nodes of the old graph.
std::map<Value*, DSNodeHandle> OldValMap;
std::map<const DSNode*, DSNode*> OldNodeMap;
// Clone the Caller's graph into the current graph, keeping
// track of where scalars in the old graph _used_ to point...
// Do this here because it only needs to happens once for each Caller!
// Strip scalars but not allocas since they are alive in callee.
//
DSNodeHandle RetVal = Graph->cloneInto(CG, OldValMap, OldNodeMap,
/*StripScalars*/ true,
/*StripAllocas*/ false,
/*CopyCallers*/ true,
/*CopyOrigCalls*/false);
// Make a temporary copy of the call site, and transform the argument node
// pointers.
DSCallSite TmpCallSite(CallSite, std::bind2nd(std::ptr_fun(©Helper),
&OldNodeMap));
ResolveCallSite(*Graph, CallSite);
}
}
// Recompute the Incomplete markers and eliminate unreachable nodes.
Graph->maskIncompleteMarkers();
Graph->markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage()
/*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false);
DEBUG(std::cerr << " [TD] Done inlining callers for: " << F.getName() << " ["
<< Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
<< "]\n");
return *Graph;
}
|