blob: 796b569ce4cbec418c0dd2c3e804affdc378845e [file] [log] [blame]
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00001//===- NodeImpl.cpp - Implement the data structure analysis nodes ---------===//
2//
3// Implement the LLVM data structure analysis library.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/Analysis/DataStructure.h"
8#include "llvm/DerivedTypes.h"
9#include "llvm/Function.h"
10#include "llvm/BasicBlock.h"
11#include "llvm/iMemory.h"
12#include "llvm/iOther.h"
13#include "llvm/Assembly/Writer.h"
14#include "Support/STLExtras.h"
15#include <algorithm>
16#include <sstream>
17
18//===----------------------------------------------------------------------===//
19// DSNode Class Implementation
20//
21
22static void MapPVS(PointerValSet &PVSOut, const PointerValSet &PVSIn,
23 map<const DSNode*, DSNode*> &NodeMap) {
24 assert(PVSOut.empty() && "Value set already initialized!");
25
26 for (unsigned i = 0, e = PVSIn.size(); i != e; ++i)
27 PVSOut.add(PointerVal(NodeMap[PVSIn[i].Node], PVSIn[i].Index));
28}
29
30
31
32unsigned countPointerFields(const Type *Ty) {
33 switch (Ty->getPrimitiveID()) {
34 case Type::StructTyID: {
35 const StructType *ST = cast<StructType>(Ty);
36 unsigned Sum = 0;
37 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i)
38 Sum += countPointerFields(ST->getContainedType(i));
39
40 return Sum;
41 }
42
43 case Type::ArrayTyID:
44 // All array elements are folded together...
45 return countPointerFields(cast<ArrayType>(Ty)->getElementType());
46
47 case Type::PointerTyID:
48 return 1;
49
50 default: // Some other type, just treat it like a scalar
51 return 0;
52 }
53}
54
55DSNode::DSNode(enum NodeTy NT, const Type *T) : Ty(T), NodeType(NT) {
56 // Create field entries for all of the values in this type...
57 FieldLinks.resize(countPointerFields(getType()));
58}
59
60void DSNode::removeReferrer(PointerValSet *PVS) {
61 vector<PointerValSet*>::iterator I = std::find(Referrers.begin(),
62 Referrers.end(), PVS);
63 assert(I != Referrers.end() && "PVS not pointing to node!");
64 Referrers.erase(I);
65}
66
67
68static void replaceIn(std::string &S, char From, const std::string &To) {
69 for (unsigned i = 0; i < S.size(); )
70 if (S[i] == From) {
71 S.replace(S.begin()+i, S.begin()+i+1,
72 To.begin(), To.end());
73 i += To.size();
74 } else {
75 ++i;
76 }
77}
78
79static void writeEdges(std::ostream &O, const void *SrcNode,
80 const char *SrcNodePortName, int SrcNodeIdx,
81 const PointerValSet &VS, const string &EdgeAttr = "") {
82 for (unsigned j = 0, je = VS.size(); j != je; ++j) {
83 O << "\t\tNode" << SrcNode << SrcNodePortName;
84 if (SrcNodeIdx != -1) O << SrcNodeIdx;
85
86 O << " -> Node" << VS[j].Node;
87 if (VS[j].Index)
88 O << ":g" << VS[j].Index;
89
90 if (!EdgeAttr.empty())
91 O << "[" << EdgeAttr << "]";
92 O << ";\n";
93 }
94}
95
96static string escapeLabel(const string &In) {
97 string Label(In);
98 replaceIn(Label, '\\', "\\\\\\\\"); // Escape caption...
99 replaceIn(Label, ' ', "\\ ");
100 replaceIn(Label, '{', "\\{");
101 replaceIn(Label, '}', "\\}");
102 return Label;
103}
104
105void DSNode::print(std::ostream &O) const {
106 string Caption = escapeLabel(getCaption());
107
108 O << "\t\tNode" << (void*)this << " [ label =\"{" << Caption;
109
110 const vector<PointerValSet> *Links = getAuxLinks();
111 if (Links && !Links->empty()) {
112 O << "|{";
113 for (unsigned i = 0; i < Links->size(); ++i) {
114 if (i) O << "|";
115 O << "<f" << i << ">";
116 }
117 O << "}";
118 }
119
120 if (!FieldLinks.empty()) {
121 O << "|{";
122 for (unsigned i = 0; i < FieldLinks.size(); ++i) {
123 if (i) O << "|";
124 O << "<g" << i << ">";
125 }
126 O << "}";
127 }
128 O << "}\"];\n";
129
130 if (Links)
131 for (unsigned i = 0; i < Links->size(); ++i)
132 writeEdges(O, this, ":f", i, (*Links)[i]);
133 for (unsigned i = 0; i < FieldLinks.size(); ++i)
134 writeEdges(O, this, ":g", i, FieldLinks[i]);
135}
136
137void DSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap, const DSNode *Old) {
138 assert(FieldLinks.size() == Old->FieldLinks.size() &&
139 "Cloned nodes do not have the same number of links!");
140 for (unsigned j = 0, je = FieldLinks.size(); j != je; ++j)
141 MapPVS(FieldLinks[j], Old->FieldLinks[j], NodeMap);
142}
143
144NewDSNode::NewDSNode(AllocationInst *V)
145 : DSNode(NewNode, V->getType()->getElementType()), Allocation(V) {
146}
147
148string NewDSNode::getCaption() const {
149 stringstream OS;
150 if (isa<MallocInst>(Allocation))
151 OS << "new ";
152 else
153 OS << "alloca ";
154
155 WriteTypeSymbolic(OS, getType(),
156 Allocation->getParent()->getParent()->getParent());
157 if (Allocation->isArrayAllocation())
158 OS << "[ ]";
159 return OS.str();
160}
161
162GlobalDSNode::GlobalDSNode(GlobalValue *V)
163 : DSNode(GlobalNode, V->getType()->getElementType()), Val(V) {
164}
165
166string GlobalDSNode::getCaption() const {
167 stringstream OS;
168 WriteTypeSymbolic(OS, getType(), Val->getParent());
169 return "global " + OS.str();
170}
171
172
173ShadowDSNode::ShadowDSNode(DSNode *P, Module *M)
174 : DSNode(ShadowNode, cast<PointerType>(P->getType())->getElementType()) {
175 Parent = P;
176 Mod = M;
177 ShadowParent = 0;
178}
179
180ShadowDSNode::ShadowDSNode(const Type *Ty, Module *M, ShadowDSNode *ShadParent)
181 : DSNode(ShadowNode, Ty) {
182 Parent = 0;
183 Mod = M;
184 ShadowParent = ShadParent;
185}
186
187std::string ShadowDSNode::getCaption() const {
188 stringstream OS;
189 WriteTypeSymbolic(OS, getType(), Mod);
190 return "shadow " + OS.str();
191}
192
193void ShadowDSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap,
194 const DSNode *O) {
195 const ShadowDSNode *Old = (ShadowDSNode*)O;
196 DSNode::mapNode(NodeMap, Old); // Map base portions first...
197
198 // Map our SynthNodes...
199 assert(SynthNodes.empty() && "Synthnodes already mapped?");
200 SynthNodes.reserve(Old->SynthNodes.size());
201 for (unsigned i = 0, e = Old->SynthNodes.size(); i != e; ++i)
202 SynthNodes.push_back(std::make_pair(Old->SynthNodes[i].first,
203 (ShadowDSNode*)NodeMap[Old->SynthNodes[i].second]));
204}
205
206
207CallDSNode::CallDSNode(CallInst *ci) : DSNode(CallNode, ci->getType()), CI(ci) {
208 unsigned NumPtrs = 0;
209 if (!isa<Function>(ci->getOperand(0)))
210 NumPtrs++; // Include the method pointer...
211
212 for (unsigned i = 1, e = ci->getNumOperands(); i != e; ++i)
213 if (isa<PointerType>(ci->getOperand(i)->getType()))
214 NumPtrs++;
215 ArgLinks.resize(NumPtrs);
216}
217
218string CallDSNode::getCaption() const {
219 stringstream OS;
220 if (const Function *CM = CI->getCalledFunction())
221 OS << "call " << CM->getName();
222 else
223 OS << "call <indirect>";
224 OS << "|Ret: ";
225 WriteTypeSymbolic(OS, getType(),
226 CI->getParent()->getParent()->getParent());
227 return OS.str();
228}
229
230void CallDSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap,
231 const DSNode *O) {
232 const CallDSNode *Old = (CallDSNode*)O;
233 DSNode::mapNode(NodeMap, Old); // Map base portions first...
234
235 assert(ArgLinks.size() == Old->ArgLinks.size() && "# Arguments changed!?");
236 for (unsigned i = 0, e = Old->ArgLinks.size(); i != e; ++i)
237 MapPVS(ArgLinks[i], Old->ArgLinks[i], NodeMap);
238}
239
240ArgDSNode::ArgDSNode(FunctionArgument *FA)
241 : DSNode(ArgNode, FA->getType()), FuncArg(FA) {
242}
243
244string ArgDSNode::getCaption() const {
245 stringstream OS;
246 OS << "arg %" << FuncArg->getName() << "|Ty: ";
247 WriteTypeSymbolic(OS, getType(), FuncArg->getParent()->getParent());
248 return OS.str();
249}
250
251void FunctionDSGraph::printFunction(std::ostream &O,
252 const char *Label) const {
253 O << "\tsubgraph cluster_" << Label << "_Function" << (void*)this << " {\n";
254 O << "\t\tlabel=\"" << Label << " Function\\ " << Func->getName() << "\";\n";
255 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
256 Nodes[i]->print(O);
257 for (unsigned i = 0, e = ShadowNodes.size(); i != e; ++i)
258 ShadowNodes[i]->print(O);
259
260 if (RetNode.size()) {
261 O << "\t\tNode" << (void*)this << Label
262 << " [shape=\"ellipse\", label=\"Returns\"];\n";
263 writeEdges(O, this, Label, -1, RetNode);
264 }
265
266 O << "\n";
267 for (std::map<Value*, PointerValSet>::const_iterator I = ValueMap.begin(),
268 E = ValueMap.end(); I != E; ++I) {
269 if (I->second.size()) { // Only output nodes with edges...
270 stringstream OS;
271 WriteTypeSymbolic(OS, I->first->getType(), Func->getParent());
272
273 // Create node for I->first
274 O << "\t\tNode" << (void*)I->first << Label << " [shape=\"box\", label=\""
275 << escapeLabel(OS.str()) << "\\n%" << escapeLabel(I->first->getName())
276 << "\",fontsize=\"12.0\",color=\"gray70\"];\n";
277
278 // add edges from I->first to all pointers in I->second
279 writeEdges(O, I->first, Label, -1, I->second,
280 "weight=\"0.9\",color=\"gray70\"");
281 }
282 }
283
284 O << "\t}\n";
285}
286
287// Copy constructor - Since we copy the nodes over, we have to be sure to go
288// through and fix pointers to point into the new graph instead of into the old
289// graph...
290//
291FunctionDSGraph::FunctionDSGraph(const FunctionDSGraph &DSG) : Func(DSG.Func) {
292 RetNode = cloneFunctionIntoSelf(DSG, true);
293}
294
295
296// cloneFunctionIntoSelf - Clone the specified method graph into the current
297// method graph, returning the Return's set of the graph. If ValueMap is set
298// to true, the ValueMap of the function is cloned into this function as well
299// as the data structure graph itself.
300//
301PointerValSet FunctionDSGraph::cloneFunctionIntoSelf(const FunctionDSGraph &DSG,
302 bool CloneValueMap) {
303 map<const DSNode*, DSNode*> NodeMap; // Map from old graph to new graph...
304 unsigned StartSize = Nodes.size(); // We probably already have nodes...
305 Nodes.reserve(StartSize+DSG.Nodes.size());
306 unsigned StartShadowSize = ShadowNodes.size();
307 ShadowNodes.reserve(StartShadowSize+DSG.ShadowNodes.size());
308
309 // Clone all of the nodes, keeping track of the mapping...
310 for (unsigned i = 0, e = DSG.Nodes.size(); i != e; ++i)
311 Nodes.push_back(NodeMap[DSG.Nodes[i]] = DSG.Nodes[i]->clone());
312
313 // Clone all of the shadow nodes similarly...
314 for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i)
315 ShadowNodes.push_back(cast<ShadowDSNode>(NodeMap[DSG.ShadowNodes[i]] = DSG.ShadowNodes[i]->clone()));
316
317
318 // Convert all of the links over in the nodes now that the map has been filled
319 // in all the way...
320 //
321 for (unsigned i = 0, e = DSG.Nodes.size(); i != e; ++i)
322 Nodes[i+StartSize]->mapNode(NodeMap, DSG.Nodes[i]);
323
324 for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i)
325 ShadowNodes[i+StartShadowSize]->mapNode(NodeMap, DSG.ShadowNodes[i]);
326
327 if (CloneValueMap) {
328 // Convert value map... the values themselves stay the same, just the nodes
329 // have to change...
330 //
331 for (std::map<Value*,PointerValSet>::const_iterator I =DSG.ValueMap.begin(),
332 E = DSG.ValueMap.end(); I != E; ++I)
333 MapPVS(ValueMap[I->first], I->second, NodeMap);
334 }
335
336 // Convert over return node...
337 PointerValSet RetVals;
338 MapPVS(RetVals, DSG.RetNode, NodeMap);
339 return RetVals;
340}
341
342
343FunctionDSGraph::~FunctionDSGraph() {
344 RetNode.clear();
345 ValueMap.clear();
346 for_each(Nodes.begin(), Nodes.end(), mem_fun(&DSNode::dropAllReferences));
347 for_each(ShadowNodes.begin(), ShadowNodes.end(),
348 mem_fun(&DSNode::dropAllReferences));
349 for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
350 for_each(ShadowNodes.begin(), ShadowNodes.end(), deleter<DSNode>);
351}
352