blob: 1b3fb76505ea7eeadf6f5adbdd2c2a2a53ec426a [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
Chris Lattnerb28bf052002-03-31 07:16:08 +00007#include "llvm/Analysis/DataStructureGraph.h"
Chris Lattnere6d4ec32002-04-08 21:58:53 +00008#include "llvm/Assembly/Writer.h"
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00009#include "llvm/DerivedTypes.h"
10#include "llvm/Function.h"
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000011#include "llvm/iMemory.h"
12#include "llvm/iOther.h"
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000013#include "Support/STLExtras.h"
14#include <algorithm>
15#include <sstream>
Anand Shuklaa9284032002-06-25 20:35:19 +000016using std::map;
17using std::string;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000018
Chris Lattnerb28bf052002-03-31 07:16:08 +000019bool AllocDSNode::isEquivalentTo(DSNode *Node) const {
20 if (AllocDSNode *N = dyn_cast<AllocDSNode>(Node))
21 return getType() == Node->getType();
Chris Lattner18961502002-06-25 16:12:52 +000022 //&& isAllocaNode() == N->isAllocaNode();
Chris Lattnerb28bf052002-03-31 07:16:08 +000023 return false;
24}
25
Chris Lattneree7eafa2002-04-27 02:28:41 +000026void AllocDSNode::mergeInto(DSNode *Node) const {
27 // Make sure the merged node is variable size if this node is var size
28 AllocDSNode *N = cast<AllocDSNode>(Node);
29 N->isVarSize |= isVarSize;
30}
31
Chris Lattnerb28bf052002-03-31 07:16:08 +000032bool GlobalDSNode::isEquivalentTo(DSNode *Node) const {
Anand Shuklaa9284032002-06-25 20:35:19 +000033 if (const GlobalDSNode *G = dyn_cast<GlobalDSNode>(Node)) {
Chris Lattner212be2e2002-04-16 03:44:03 +000034 if (G->Val != Val) return false;
35
36 // Check that the outgoing links are identical...
37 assert(getNumLinks() == G->getNumLinks() && "Not identical shape?");
38 for (unsigned i = 0, e = getNumLinks(); i != e; ++i)
39 if (getLink(i) != G->getLink(i)) // Check links
40 return false;
41 return true;
42 }
Chris Lattnerb28bf052002-03-31 07:16:08 +000043 return false;
44}
45
Chris Lattner212be2e2002-04-16 03:44:03 +000046// Call node equivalency - Two call nodes are identical if all of the outgoing
47// links are the same, AND if all of the incoming links are identical.
48//
Chris Lattnerb28bf052002-03-31 07:16:08 +000049bool CallDSNode::isEquivalentTo(DSNode *Node) const {
Chris Lattner212be2e2002-04-16 03:44:03 +000050 if (CallDSNode *C = dyn_cast<CallDSNode>(Node)) {
Chris Lattner7650b942002-04-16 20:39:59 +000051 if (getReferrers().size() != C->getReferrers().size() ||
52 C->getType() != getType())
Chris Lattner212be2e2002-04-16 03:44:03 +000053 return false; // Quick check...
Chris Lattnerb28bf052002-03-31 07:16:08 +000054
Chris Lattner212be2e2002-04-16 03:44:03 +000055 // Check that the outgoing links are identical...
56 assert(getNumLinks() == C->getNumLinks() && "Not identical shape?");
57 for (unsigned i = 0, e = getNumLinks(); i != e; ++i)
58 if (getLink(i) != C->getLink(i)) // Check links
59 return false;
60
61
62 std::vector<PointerValSet*> Refs1 = C->getReferrers();
63 std::vector<PointerValSet*> Refs2 = getReferrers();
64
65 sort(Refs1.begin(), Refs1.end());
66 sort(Refs2.begin(), Refs2.end());
67 if (Refs1 != Refs2) return false; // Incoming edges different?
68
69 // Check that all outgoing links are the same...
70 return C->ArgLinks == ArgLinks; // Check that the arguments are identical
71 }
Chris Lattnerb28bf052002-03-31 07:16:08 +000072 return false;
73}
74
75// NodesAreEquivalent - Check to see if the nodes are equivalent in all ways
76// except node type. Since we know N1 is a shadow node, N2 is allowed to be
77// any type.
78//
79bool ShadowDSNode::isEquivalentTo(DSNode *Node) const {
Chris Lattner0009dac2002-04-04 19:21:51 +000080 return getType() == Node->getType();
Chris Lattnerb28bf052002-03-31 07:16:08 +000081}
82
83
84
85
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000086//===----------------------------------------------------------------------===//
87// DSNode Class Implementation
88//
89
90static void MapPVS(PointerValSet &PVSOut, const PointerValSet &PVSIn,
Chris Lattner1120c8b2002-03-28 17:56:03 +000091 map<const DSNode*, DSNode*> &NodeMap, bool ReinitOk = false){
92 assert((ReinitOk || PVSOut.empty()) && "Value set already initialized!");
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000093
94 for (unsigned i = 0, e = PVSIn.size(); i != e; ++i)
95 PVSOut.add(PointerVal(NodeMap[PVSIn[i].Node], PVSIn[i].Index));
96}
97
98
99
100unsigned countPointerFields(const Type *Ty) {
101 switch (Ty->getPrimitiveID()) {
102 case Type::StructTyID: {
103 const StructType *ST = cast<StructType>(Ty);
104 unsigned Sum = 0;
105 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i)
106 Sum += countPointerFields(ST->getContainedType(i));
107
108 return Sum;
109 }
110
111 case Type::ArrayTyID:
112 // All array elements are folded together...
113 return countPointerFields(cast<ArrayType>(Ty)->getElementType());
114
115 case Type::PointerTyID:
116 return 1;
117
118 default: // Some other type, just treat it like a scalar
119 return 0;
120 }
121}
122
123DSNode::DSNode(enum NodeTy NT, const Type *T) : Ty(T), NodeType(NT) {
124 // Create field entries for all of the values in this type...
125 FieldLinks.resize(countPointerFields(getType()));
126}
127
128void DSNode::removeReferrer(PointerValSet *PVS) {
Anand Shuklaa9284032002-06-25 20:35:19 +0000129 std::vector<PointerValSet*>::iterator I = std::find(Referrers.begin(),
Chris Lattner9fdaeb42002-06-30 16:01:15 +0000130 Referrers.end(), PVS);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000131 assert(I != Referrers.end() && "PVS not pointing to node!");
132 Referrers.erase(I);
133}
134
135
Chris Lattner6088c4f2002-03-27 19:46:05 +0000136// removeAllIncomingEdges - Erase all edges in the graph that point to this node
137void DSNode::removeAllIncomingEdges() {
138 while (!Referrers.empty())
139 Referrers.back()->removePointerTo(this);
140}
141
142
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000143static void replaceIn(std::string &S, char From, const std::string &To) {
144 for (unsigned i = 0; i < S.size(); )
145 if (S[i] == From) {
146 S.replace(S.begin()+i, S.begin()+i+1,
147 To.begin(), To.end());
148 i += To.size();
149 } else {
150 ++i;
151 }
152}
153
154static void writeEdges(std::ostream &O, const void *SrcNode,
155 const char *SrcNodePortName, int SrcNodeIdx,
156 const PointerValSet &VS, const string &EdgeAttr = "") {
157 for (unsigned j = 0, je = VS.size(); j != je; ++j) {
158 O << "\t\tNode" << SrcNode << SrcNodePortName;
159 if (SrcNodeIdx != -1) O << SrcNodeIdx;
160
161 O << " -> Node" << VS[j].Node;
162 if (VS[j].Index)
163 O << ":g" << VS[j].Index;
164
165 if (!EdgeAttr.empty())
166 O << "[" << EdgeAttr << "]";
167 O << ";\n";
168 }
169}
170
171static string escapeLabel(const string &In) {
172 string Label(In);
173 replaceIn(Label, '\\', "\\\\\\\\"); // Escape caption...
174 replaceIn(Label, ' ', "\\ ");
175 replaceIn(Label, '{', "\\{");
176 replaceIn(Label, '}', "\\}");
177 return Label;
178}
179
Anand Shuklaa9284032002-06-25 20:35:19 +0000180void DSNode::dump() const { print(std::cerr); }
Chris Lattnerb28bf052002-03-31 07:16:08 +0000181
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000182void DSNode::print(std::ostream &O) const {
183 string Caption = escapeLabel(getCaption());
184
185 O << "\t\tNode" << (void*)this << " [ label =\"{" << Caption;
186
Anand Shuklaa9284032002-06-25 20:35:19 +0000187 const std::vector<PointerValSet> *Links = getAuxLinks();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000188 if (Links && !Links->empty()) {
189 O << "|{";
190 for (unsigned i = 0; i < Links->size(); ++i) {
191 if (i) O << "|";
192 O << "<f" << i << ">";
193 }
194 O << "}";
195 }
196
197 if (!FieldLinks.empty()) {
198 O << "|{";
199 for (unsigned i = 0; i < FieldLinks.size(); ++i) {
200 if (i) O << "|";
201 O << "<g" << i << ">";
202 }
203 O << "}";
204 }
205 O << "}\"];\n";
206
207 if (Links)
208 for (unsigned i = 0; i < Links->size(); ++i)
209 writeEdges(O, this, ":f", i, (*Links)[i]);
210 for (unsigned i = 0; i < FieldLinks.size(); ++i)
211 writeEdges(O, this, ":g", i, FieldLinks[i]);
212}
213
214void DSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap, const DSNode *Old) {
215 assert(FieldLinks.size() == Old->FieldLinks.size() &&
216 "Cloned nodes do not have the same number of links!");
217 for (unsigned j = 0, je = FieldLinks.size(); j != je; ++j)
218 MapPVS(FieldLinks[j], Old->FieldLinks[j], NodeMap);
Chris Lattnerfe145682002-04-17 03:24:59 +0000219
220 // Map our SynthNodes...
221 assert(SynthNodes.empty() && "Synthnodes already mapped?");
222 SynthNodes.reserve(Old->SynthNodes.size());
223 for (unsigned i = 0, e = Old->SynthNodes.size(); i != e; ++i)
224 SynthNodes.push_back(std::make_pair(Old->SynthNodes[i].first,
225 (ShadowDSNode*)NodeMap[Old->SynthNodes[i].second]));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000226}
227
Chris Lattneree7eafa2002-04-27 02:28:41 +0000228AllocDSNode::AllocDSNode(AllocationInst *V, bool isvarsize)
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000229 : DSNode(NewNode, V->getType()->getElementType()), Allocation(V) {
Chris Lattneree7eafa2002-04-27 02:28:41 +0000230
231 // Is variable size if incoming flag says so, or if allocation is var size
232 // already.
233 isVarSize = isvarsize || !isa<Constant>(V->getArraySize());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000234}
235
Chris Lattner1120c8b2002-03-28 17:56:03 +0000236bool AllocDSNode::isAllocaNode() const {
Chris Lattner6088c4f2002-03-27 19:46:05 +0000237 return isa<AllocaInst>(Allocation);
238}
239
240
Chris Lattner1120c8b2002-03-28 17:56:03 +0000241string AllocDSNode::getCaption() const {
Anand Shuklaa9284032002-06-25 20:35:19 +0000242 std::stringstream OS;
Chris Lattner6088c4f2002-03-27 19:46:05 +0000243 OS << (isMallocNode() ? "new " : "alloca ");
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000244
245 WriteTypeSymbolic(OS, getType(),
246 Allocation->getParent()->getParent()->getParent());
Chris Lattneree7eafa2002-04-27 02:28:41 +0000247 if (isVarSize)
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000248 OS << "[ ]";
249 return OS.str();
250}
251
252GlobalDSNode::GlobalDSNode(GlobalValue *V)
253 : DSNode(GlobalNode, V->getType()->getElementType()), Val(V) {
254}
255
256string GlobalDSNode::getCaption() const {
Anand Shuklaa9284032002-06-25 20:35:19 +0000257 std::stringstream OS;
Chris Lattner7650b942002-04-16 20:39:59 +0000258 if (isa<Function>(Val))
259 OS << "fn ";
260 else
261 OS << "global ";
262
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000263 WriteTypeSymbolic(OS, getType(), Val->getParent());
Chris Lattner7650b942002-04-16 20:39:59 +0000264 return OS.str() + " %" + Val->getName();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000265}
266
267
Chris Lattner7650b942002-04-16 20:39:59 +0000268ShadowDSNode::ShadowDSNode(const Type *Ty, Module *M) : DSNode(ShadowNode, Ty) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000269 Mod = M;
270 ShadowParent = 0;
271}
272
Chris Lattnerfe145682002-04-17 03:24:59 +0000273ShadowDSNode::ShadowDSNode(const Type *Ty, Module *M, DSNode *ShadParent)
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000274 : DSNode(ShadowNode, Ty) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000275 Mod = M;
276 ShadowParent = ShadParent;
277}
278
279std::string ShadowDSNode::getCaption() const {
Anand Shuklaa9284032002-06-25 20:35:19 +0000280 std::stringstream OS;
Chris Lattner6088c4f2002-03-27 19:46:05 +0000281 OS << "shadow ";
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000282 WriteTypeSymbolic(OS, getType(), Mod);
Chris Lattner6088c4f2002-03-27 19:46:05 +0000283 return OS.str();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000284}
285
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000286CallDSNode::CallDSNode(CallInst *ci) : DSNode(CallNode, ci->getType()), CI(ci) {
287 unsigned NumPtrs = 0;
Chris Lattner7650b942002-04-16 20:39:59 +0000288 for (unsigned i = 0, e = ci->getNumOperands(); i != e; ++i)
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000289 if (isa<PointerType>(ci->getOperand(i)->getType()))
290 NumPtrs++;
291 ArgLinks.resize(NumPtrs);
292}
293
294string CallDSNode::getCaption() const {
Anand Shuklaa9284032002-06-25 20:35:19 +0000295 std::stringstream OS;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000296 if (const Function *CM = CI->getCalledFunction())
297 OS << "call " << CM->getName();
298 else
299 OS << "call <indirect>";
Chris Lattneref35ff02002-04-17 03:42:51 +0000300 OS << ": ";
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000301 WriteTypeSymbolic(OS, getType(),
302 CI->getParent()->getParent()->getParent());
303 return OS.str();
304}
305
306void CallDSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap,
307 const DSNode *O) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000308 const CallDSNode *Old = cast<CallDSNode>(O);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000309 DSNode::mapNode(NodeMap, Old); // Map base portions first...
310
311 assert(ArgLinks.size() == Old->ArgLinks.size() && "# Arguments changed!?");
312 for (unsigned i = 0, e = Old->ArgLinks.size(); i != e; ++i)
313 MapPVS(ArgLinks[i], Old->ArgLinks[i], NodeMap);
314}
315
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000316void FunctionDSGraph::printFunction(std::ostream &O,
317 const char *Label) const {
318 O << "\tsubgraph cluster_" << Label << "_Function" << (void*)this << " {\n";
319 O << "\t\tlabel=\"" << Label << " Function\\ " << Func->getName() << "\";\n";
Chris Lattner1120c8b2002-03-28 17:56:03 +0000320 for (unsigned i = 0, e = AllocNodes.size(); i != e; ++i)
321 AllocNodes[i]->print(O);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000322 for (unsigned i = 0, e = ShadowNodes.size(); i != e; ++i)
323 ShadowNodes[i]->print(O);
Chris Lattner1120c8b2002-03-28 17:56:03 +0000324 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
325 GlobalNodes[i]->print(O);
326 for (unsigned i = 0, e = CallNodes.size(); i != e; ++i)
327 CallNodes[i]->print(O);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000328
329 if (RetNode.size()) {
330 O << "\t\tNode" << (void*)this << Label
331 << " [shape=\"ellipse\", label=\"Returns\"];\n";
332 writeEdges(O, this, Label, -1, RetNode);
333 }
334
335 O << "\n";
336 for (std::map<Value*, PointerValSet>::const_iterator I = ValueMap.begin(),
337 E = ValueMap.end(); I != E; ++I) {
Chris Lattner7650b942002-04-16 20:39:59 +0000338 if (I->second.size()) { // Only output nodes with edges...
Anand Shuklaa9284032002-06-25 20:35:19 +0000339 std::stringstream OS;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000340 WriteTypeSymbolic(OS, I->first->getType(), Func->getParent());
341
342 // Create node for I->first
Chris Lattner595d2f62002-04-18 18:14:19 +0000343 O << "\t\tNode" << (void*)I->first << Label << " [shape=\""
344 << (isa<Argument>(I->first) ? "ellipse" : "box") << "\", label=\""
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000345 << escapeLabel(OS.str()) << "\\n%" << escapeLabel(I->first->getName())
346 << "\",fontsize=\"12.0\",color=\"gray70\"];\n";
347
348 // add edges from I->first to all pointers in I->second
349 writeEdges(O, I->first, Label, -1, I->second,
350 "weight=\"0.9\",color=\"gray70\"");
351 }
352 }
353
354 O << "\t}\n";
355}
356
357// Copy constructor - Since we copy the nodes over, we have to be sure to go
358// through and fix pointers to point into the new graph instead of into the old
359// graph...
360//
361FunctionDSGraph::FunctionDSGraph(const FunctionDSGraph &DSG) : Func(DSG.Func) {
Anand Shuklaa9284032002-06-25 20:35:19 +0000362 std::vector<PointerValSet> Args;
Chris Lattner212be2e2002-04-16 03:44:03 +0000363 RetNode = cloneFunctionIntoSelf(DSG, true, Args);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000364}
365
366
367// cloneFunctionIntoSelf - Clone the specified method graph into the current
368// method graph, returning the Return's set of the graph. If ValueMap is set
369// to true, the ValueMap of the function is cloned into this function as well
Chris Lattner212be2e2002-04-16 03:44:03 +0000370// as the data structure graph itself. Regardless, the arguments value sets
371// of DSG are copied into Args.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000372//
373PointerValSet FunctionDSGraph::cloneFunctionIntoSelf(const FunctionDSGraph &DSG,
Chris Lattner212be2e2002-04-16 03:44:03 +0000374 bool CloneValueMap,
Anand Shuklaa9284032002-06-25 20:35:19 +0000375 std::vector<PointerValSet> &Args) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000376 map<const DSNode*, DSNode*> NodeMap; // Map from old graph to new graph...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000377 unsigned StartAllocSize = AllocNodes.size();
378 AllocNodes.reserve(StartAllocSize+DSG.AllocNodes.size());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000379 unsigned StartShadowSize = ShadowNodes.size();
380 ShadowNodes.reserve(StartShadowSize+DSG.ShadowNodes.size());
Chris Lattner1120c8b2002-03-28 17:56:03 +0000381 unsigned StartGlobalSize = GlobalNodes.size();
382 GlobalNodes.reserve(StartGlobalSize+DSG.GlobalNodes.size());
383 unsigned StartCallSize = CallNodes.size();
384 CallNodes.reserve(StartCallSize+DSG.CallNodes.size());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000385
Chris Lattner1120c8b2002-03-28 17:56:03 +0000386 // Clone all of the alloc nodes similarly...
387 for (unsigned i = 0, e = DSG.AllocNodes.size(); i != e; ++i) {
388 AllocDSNode *New = cast<AllocDSNode>(DSG.AllocNodes[i]->clone());
389 NodeMap[DSG.AllocNodes[i]] = New;
390 AllocNodes.push_back(New);
391 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000392
393 // Clone all of the shadow nodes similarly...
Chris Lattnerdd3fc182002-03-27 00:54:31 +0000394 for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i) {
395 ShadowDSNode *New = cast<ShadowDSNode>(DSG.ShadowNodes[i]->clone());
396 NodeMap[DSG.ShadowNodes[i]] = New;
397 ShadowNodes.push_back(New);
398 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000399
Chris Lattner1120c8b2002-03-28 17:56:03 +0000400 // Clone all of the global nodes...
401 for (unsigned i = 0, e = DSG.GlobalNodes.size(); i != e; ++i) {
402 GlobalDSNode *New = cast<GlobalDSNode>(DSG.GlobalNodes[i]->clone());
403 NodeMap[DSG.GlobalNodes[i]] = New;
404 GlobalNodes.push_back(New);
405 }
406
407 // Clone all of the call nodes...
408 for (unsigned i = 0, e = DSG.CallNodes.size(); i != e; ++i) {
409 CallDSNode *New = cast<CallDSNode>(DSG.CallNodes[i]->clone());
410 NodeMap[DSG.CallNodes[i]] = New;
411 CallNodes.push_back(New);
412 }
413
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000414 // Convert all of the links over in the nodes now that the map has been filled
415 // in all the way...
416 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000417 for (unsigned i = 0, e = DSG.AllocNodes.size(); i != e; ++i)
418 AllocNodes[i+StartAllocSize]->mapNode(NodeMap, DSG.AllocNodes[i]);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000419 for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i)
420 ShadowNodes[i+StartShadowSize]->mapNode(NodeMap, DSG.ShadowNodes[i]);
Chris Lattner1120c8b2002-03-28 17:56:03 +0000421 for (unsigned i = 0, e = DSG.GlobalNodes.size(); i != e; ++i)
422 GlobalNodes[i+StartGlobalSize]->mapNode(NodeMap, DSG.GlobalNodes[i]);
423 for (unsigned i = 0, e = DSG.CallNodes.size(); i != e; ++i)
424 CallNodes[i+StartCallSize]->mapNode(NodeMap, DSG.CallNodes[i]);
425
Chris Lattner212be2e2002-04-16 03:44:03 +0000426 // Convert over the arguments...
427 Function *OF = DSG.getFunction();
Chris Lattner18961502002-06-25 16:12:52 +0000428 for (Function::aiterator I = OF->abegin(), E = OF->aend(); I != E; ++I)
429 if (isa<PointerType>(I->getType())) {
Chris Lattner212be2e2002-04-16 03:44:03 +0000430 PointerValSet ArgPVS;
Chris Lattner18961502002-06-25 16:12:52 +0000431 assert(DSG.getValueMap().find(I) != DSG.getValueMap().end());
432 MapPVS(ArgPVS, DSG.getValueMap().find(I)->second, NodeMap);
Chris Lattner212be2e2002-04-16 03:44:03 +0000433 assert(!ArgPVS.empty() && "Argument has no links!");
434 Args.push_back(ArgPVS);
435 }
436
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000437
438 if (CloneValueMap) {
439 // Convert value map... the values themselves stay the same, just the nodes
440 // have to change...
441 //
442 for (std::map<Value*,PointerValSet>::const_iterator I =DSG.ValueMap.begin(),
443 E = DSG.ValueMap.end(); I != E; ++I)
Chris Lattner1120c8b2002-03-28 17:56:03 +0000444 MapPVS(ValueMap[I->first], I->second, NodeMap, true);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000445 }
446
447 // Convert over return node...
448 PointerValSet RetVals;
449 MapPVS(RetVals, DSG.RetNode, NodeMap);
450 return RetVals;
451}
452
453
454FunctionDSGraph::~FunctionDSGraph() {
455 RetNode.clear();
456 ValueMap.clear();
Chris Lattner1120c8b2002-03-28 17:56:03 +0000457 for_each(AllocNodes.begin(), AllocNodes.end(),
Anand Shuklaa9284032002-06-25 20:35:19 +0000458 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000459 for_each(ShadowNodes.begin(), ShadowNodes.end(),
Anand Shuklaa9284032002-06-25 20:35:19 +0000460 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattner1120c8b2002-03-28 17:56:03 +0000461 for_each(GlobalNodes.begin(), GlobalNodes.end(),
Anand Shuklaa9284032002-06-25 20:35:19 +0000462 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattner1120c8b2002-03-28 17:56:03 +0000463 for_each(CallNodes.begin(), CallNodes.end(),
Anand Shuklaa9284032002-06-25 20:35:19 +0000464 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattner1120c8b2002-03-28 17:56:03 +0000465 for_each(AllocNodes.begin(), AllocNodes.end(), deleter<DSNode>);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000466 for_each(ShadowNodes.begin(), ShadowNodes.end(), deleter<DSNode>);
Chris Lattner1120c8b2002-03-28 17:56:03 +0000467 for_each(GlobalNodes.begin(), GlobalNodes.end(), deleter<DSNode>);
468 for_each(CallNodes.begin(), CallNodes.end(), deleter<DSNode>);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000469}
470