blob: 683fe46b4c9a9ebcf905ced2cc5cd65670dbd8db [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- DataStructure.cpp - Implement the core data structure analysis -----===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00002//
Chris Lattnerc68c31b2002-07-10 22:38:08 +00003// This file implements the core data structure functionality.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattnerfccd06f2002-10-01 22:33:50 +00007#include "llvm/Analysis/DSGraph.h"
8#include "llvm/Function.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +00009#include "llvm/DerivedTypes.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000010#include "Support/STLExtras.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000011#include "Support/Statistic.h"
12#include "llvm/Target/TargetData.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000013#include <algorithm>
Chris Lattnerfccd06f2002-10-01 22:33:50 +000014#include <set>
Chris Lattnerc68c31b2002-07-10 22:38:08 +000015
Chris Lattnere2219762002-07-18 18:22:40 +000016using std::vector;
17
Chris Lattnerfccd06f2002-10-01 22:33:50 +000018// TODO: FIXME
19namespace DataStructureAnalysis {
20 // isPointerType - Return true if this first class type is big enough to hold
21 // a pointer.
22 //
23 bool isPointerType(const Type *Ty);
24 extern TargetData TD;
25}
26using namespace DataStructureAnalysis;
27
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000028//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029// DSNode Implementation
30//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000031
Chris Lattnerfccd06f2002-10-01 22:33:50 +000032DSNode::DSNode(enum NodeTy NT, const Type *T) : NodeType(NT) {
33 // If this node is big enough to have pointer fields, add space for them now.
34 if (T != Type::VoidTy && !isa<FunctionType>(T)) // Avoid TargetData assert's
35 LinkIndex.resize(TD.getTypeSize(T), -1);
36
37 TypeEntries.push_back(std::make_pair(T, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +000038}
39
Chris Lattner0d9bab82002-07-18 00:12:30 +000040// DSNode copy constructor... do not copy over the referrers list!
41DSNode::DSNode(const DSNode &N)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000042 : Links(N.Links), LinkIndex(N.LinkIndex),
43 TypeEntries(N.TypeEntries), Globals(N.Globals), NodeType(N.NodeType) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000044}
45
Chris Lattnerc68c31b2002-07-10 22:38:08 +000046void DSNode::removeReferrer(DSNodeHandle *H) {
47 // Search backwards, because we depopulate the list from the back for
48 // efficiency (because it's a vector).
Chris Lattnere2219762002-07-18 18:22:40 +000049 vector<DSNodeHandle*>::reverse_iterator I =
Chris Lattnerc68c31b2002-07-10 22:38:08 +000050 std::find(Referrers.rbegin(), Referrers.rend(), H);
51 assert(I != Referrers.rend() && "Referrer not pointing to node!");
52 Referrers.erase(I.base()-1);
53}
54
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000055// addGlobal - Add an entry for a global value to the Globals list. This also
56// marks the node with the 'G' flag if it does not already have it.
57//
58void DSNode::addGlobal(GlobalValue *GV) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000059 // Keep the list sorted.
Chris Lattnere2219762002-07-18 18:22:40 +000060 vector<GlobalValue*>::iterator I =
Chris Lattner0d9bab82002-07-18 00:12:30 +000061 std::lower_bound(Globals.begin(), Globals.end(), GV);
62
63 if (I == Globals.end() || *I != GV) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000064 //assert(GV->getType()->getElementType() == Ty);
Chris Lattner0d9bab82002-07-18 00:12:30 +000065 Globals.insert(I, GV);
66 NodeType |= GlobalNode;
67 }
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000068}
69
70
Chris Lattnerc68c31b2002-07-10 22:38:08 +000071// addEdgeTo - Add an edge from the current node to the specified node. This
72// can cause merging of nodes in the graph.
73//
Chris Lattnerfccd06f2002-10-01 22:33:50 +000074void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) {
75 assert(Offset < LinkIndex.size() && "Offset out of range!");
76 if (NH.getNode() == 0) return; // Nothing to do
77
78 if (LinkIndex[Offset] == -1) { // No merging to perform...
79 LinkIndex[Offset] = Links.size(); // Allocate a new link...
80 Links.push_back(NH);
81 return;
82 }
83
84 unsigned Idx = (unsigned)LinkIndex[Offset];
85 if (!Links[Idx].getNode()) { // No merging to perform
86 Links[Idx] = NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000087 return;
88 }
89
90 // Merge the two nodes...
Chris Lattnerfccd06f2002-10-01 22:33:50 +000091 Links[Idx].mergeWith(NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000092}
93
94
Chris Lattnerfccd06f2002-10-01 22:33:50 +000095// MergeSortedVectors - Efficiently merge a vector into another vector where
96// duplicates are not allowed and both are sorted. This assumes that 'T's are
97// efficiently copyable and have sane comparison semantics.
Chris Lattnerc68c31b2002-07-10 22:38:08 +000098//
Chris Lattnerfccd06f2002-10-01 22:33:50 +000099template<typename T>
100void MergeSortedVectors(vector<T> &Dest, const vector<T> &Src) {
101 // By far, the most common cases will be the simple ones. In these cases,
102 // avoid having to allocate a temporary vector...
103 //
104 if (Src.empty()) { // Nothing to merge in...
105 return;
106 } else if (Dest.empty()) { // Just copy the result in...
107 Dest = Src;
108 } else if (Src.size() == 1) { // Insert a single element...
109 const T &V = Src[0];
110 typename vector<T>::iterator I =
111 std::lower_bound(Dest.begin(), Dest.end(), V);
112 if (I == Dest.end() || *I != Src[0]) // If not already contained...
113 Dest.insert(I, Src[0]);
114 } else if (Dest.size() == 1) {
115 T Tmp = Dest[0]; // Save value in temporary...
116 Dest = Src; // Copy over list...
117 typename vector<T>::iterator I =
118 std::lower_bound(Dest.begin(), Dest.end(),Tmp);
119 if (I == Dest.end() || *I != Src[0]) // If not already contained...
120 Dest.insert(I, Src[0]);
121
122 } else {
123 // Make a copy to the side of Dest...
124 vector<T> Old(Dest);
125
126 // Make space for all of the type entries now...
127 Dest.resize(Dest.size()+Src.size());
128
129 // Merge the two sorted ranges together... into Dest.
130 std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin());
131
132 // Now erase any duplicate entries that may have accumulated into the
133 // vectors (because they were in both of the input sets)
134 Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end());
135 }
136}
137
138
139// mergeWith - Merge this node and the specified node, moving all links to and
140// from the argument node into the current node, deleting the node argument.
141// Offset indicates what offset the specified node is to be merged into the
142// current node.
143//
144// The specified node may be a null pointer (in which case, nothing happens).
145//
146void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
147 DSNode *N = NH.getNode();
148 if (N == 0 || (N == this && NH.getOffset() == Offset))
149 return; // Noop
150
151 assert(NH.getNode() != this &&
152 "Cannot merge two portions of the same node yet!");
153
154 // If both nodes are not at offset 0, make sure that we are merging the node
155 // at an later offset into the node with the zero offset.
156 //
157 if (Offset > NH.getOffset()) {
158 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
159 return;
160 }
161
162#if 0
163 std::cerr << "\n\nMerging:\n";
164 N->print(std::cerr, 0);
165 std::cerr << " and:\n";
166 print(std::cerr, 0);
167#endif
168
169 // Now we know that Offset <= NH.Offset, so convert it so our "Offset" (with
170 // respect to NH.Offset) is now zero.
171 //
172 unsigned NOffset = NH.getOffset()-Offset;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000173
174 // Remove all edges pointing at N, causing them to point to 'this' instead.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000175 // Make sure to adjust their offset, not just the node pointer.
176 //
177 while (!N->Referrers.empty()) {
178 DSNodeHandle &Ref = *N->Referrers.back();
179 Ref = DSNodeHandle(this, NOffset+Ref.getOffset());
180 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000181
182 // Make all of the outgoing links of N now be outgoing links of this. This
183 // can cause recursive merging!
184 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000185 for (unsigned i = 0, e = N->LinkIndex.size(); i != e; ++i)
186 if (N->LinkIndex[i] != -1) {
187 addEdgeTo(i+NOffset, N->Links[N->LinkIndex[i]]);
188 N->LinkIndex[i] = -1; // Reduce unneccesary edges in graph. N is dead
189 }
190
191 // Now that there are no outgoing edges, all of the Links are dead.
192 N->Links.clear();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000193
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000194 // Merge the node types
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000195 NodeType |= N->NodeType;
196 N->NodeType = 0; // N is now a dead node.
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000197
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000198 // If this merging into node has more than just void nodes in it, merge!
199 assert(!N->TypeEntries.empty() && "TypeEntries is empty for a node?");
200 if (N->TypeEntries.size() != 1 || N->TypeEntries[0].first != Type::VoidTy) {
201 // If the current node just has a Void entry in it, remove it.
202 if (TypeEntries.size() == 1 && TypeEntries[0].first == Type::VoidTy)
203 TypeEntries.clear();
204
205 // Adjust all of the type entries we are merging in by the offset... and add
206 // them to the TypeEntries list.
207 //
208 if (NOffset != 0) { // This case is common enough to optimize for
209 // Offset all of the TypeEntries in N with their new offset
210 for (unsigned i = 0, e = N->TypeEntries.size(); i != e; ++i)
211 N->TypeEntries[i].second += NOffset;
212 }
213
214 MergeSortedVectors(TypeEntries, N->TypeEntries);
215
216 N->TypeEntries.clear();
217 }
218
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000219 // Merge the globals list...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000220 if (!N->Globals.empty()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000221 MergeSortedVectors(Globals, N->Globals);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000222
223 // Delete the globals from the old node...
224 N->Globals.clear();
225 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000226}
227
228//===----------------------------------------------------------------------===//
229// DSGraph Implementation
230//===----------------------------------------------------------------------===//
231
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000232DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
233 std::map<const DSNode*, DSNode*> NodeMap;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000234 RetNode = cloneInto(G, ValueMap, NodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000235}
236
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000237DSGraph::~DSGraph() {
238 FunctionCalls.clear();
239 ValueMap.clear();
240 RetNode = 0;
241
242#ifndef NDEBUG
243 // Drop all intra-node references, so that assertions don't fail...
244 std::for_each(Nodes.begin(), Nodes.end(),
245 std::mem_fun(&DSNode::dropAllReferences));
246#endif
247
248 // Delete all of the nodes themselves...
249 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
250}
251
Chris Lattner0d9bab82002-07-18 00:12:30 +0000252// dump - Allow inspection of graph in a debugger.
253void DSGraph::dump() const { print(std::cerr); }
254
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000255// Helper function used to clone a function list.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000256//
257static void CopyFunctionCallsList(const vector<vector<DSNodeHandle> >&fromCalls,
258 vector<vector<DSNodeHandle> > &toCalls,
259 std::map<const DSNode*, DSNode*> &NodeMap) {
260
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000261 unsigned FC = toCalls.size(); // FirstCall
262 toCalls.reserve(FC+fromCalls.size());
263 for (unsigned i = 0, ei = fromCalls.size(); i != ei; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000264 toCalls.push_back(vector<DSNodeHandle>());
265
266 const vector<DSNodeHandle> &CurCall = fromCalls[i];
267 toCalls.back().reserve(CurCall.size());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000268 for (unsigned j = 0, ej = fromCalls[i].size(); j != ej; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000269 toCalls[FC+i].push_back(DSNodeHandle(NodeMap[CurCall[j].getNode()],
270 CurCall[j].getOffset()));
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000271 }
272}
273
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000274/// remapLinks - Change all of the Links in the current node according to the
275/// specified mapping.
276void DSNode::remapLinks(std::map<const DSNode*, DSNode*> &OldNodeMap) {
277 for (unsigned i = 0, e = Links.size(); i != e; ++i)
278 Links[i].setNode(OldNodeMap[Links[i].getNode()]);
279}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000280
Chris Lattner0d9bab82002-07-18 00:12:30 +0000281// cloneInto - Clone the specified DSGraph into the current graph, returning the
282// Return node of the graph. The translated ValueMap for the old function is
283// filled into the OldValMap member. If StripLocals is set to true, Scalar and
284// Alloca markers are removed from the graph, as the graph is being cloned into
285// a calling function's graph.
286//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000287DSNodeHandle DSGraph::cloneInto(const DSGraph &G,
288 std::map<Value*, DSNodeHandle> &OldValMap,
289 std::map<const DSNode*, DSNode*> &OldNodeMap,
290 bool StripScalars, bool StripAllocas,
291 bool CopyCallers, bool CopyOrigCalls) {
292 assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000293
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000294 unsigned FN = Nodes.size(); // First new node...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000295
296 // Duplicate all of the nodes, populating the node map...
297 Nodes.reserve(FN+G.Nodes.size());
298 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000299 DSNode *Old = G.Nodes[i];
300 DSNode *New = new DSNode(*Old);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000301 Nodes.push_back(New);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000302 OldNodeMap[Old] = New;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000303 }
304
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000305 // Rewrite the links in the new nodes to point into the current graph now.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000306 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000307 Nodes[i]->remapLinks(OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000308
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000309 // Remove local markers as specified
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000310 unsigned char StripBits = (StripScalars ? DSNode::ScalarNode : 0) |
311 (StripAllocas ? DSNode::AllocaNode : 0);
312 if (StripBits)
Chris Lattner0d9bab82002-07-18 00:12:30 +0000313 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000314 Nodes[i]->NodeType &= ~StripBits;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000315
316 // Copy the value map...
317 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ValueMap.begin(),
318 E = G.ValueMap.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000319 OldValMap[I->first] = DSNodeHandle(OldNodeMap[I->second.getNode()],
320 I->second.getOffset());
Chris Lattnere2219762002-07-18 18:22:40 +0000321 // Copy the function calls list...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000322 CopyFunctionCallsList(G.FunctionCalls, FunctionCalls, OldNodeMap);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000323
324#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000325 if (CopyOrigCalls)
326 CopyFunctionCallsList(G.OrigFunctionCalls, OrigFunctionCalls, OldNodeMap);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000327
328 // Copy the list of unresolved callers
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000329 if (CopyCallers)
330 PendingCallers.insert(G.PendingCallers.begin(), G.PendingCallers.end());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000331#endif
Chris Lattner0d9bab82002-07-18 00:12:30 +0000332
333 // Return the returned node pointer...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000334 return DSNodeHandle(OldNodeMap[G.RetNode.getNode()], G.RetNode.getOffset());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000335}
336
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000337#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000338// cloneGlobalInto - Clone the given global node and all its target links
339// (and all their llinks, recursively).
340//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000341DSNode *DSGraph::cloneGlobalInto(const DSNode *GNode) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000342 if (GNode == 0 || GNode->getGlobals().size() == 0) return 0;
343
344 // If a clone has already been created for GNode, return it.
345 DSNodeHandle& ValMapEntry = ValueMap[GNode->getGlobals()[0]];
346 if (ValMapEntry != 0)
347 return ValMapEntry;
348
349 // Clone the node and update the ValMap.
350 DSNode* NewNode = new DSNode(*GNode);
351 ValMapEntry = NewNode; // j=0 case of loop below!
352 Nodes.push_back(NewNode);
353 for (unsigned j = 1, N = NewNode->getGlobals().size(); j < N; ++j)
354 ValueMap[NewNode->getGlobals()[j]] = NewNode;
355
356 // Rewrite the links in the new node to point into the current graph.
357 for (unsigned j = 0, e = GNode->getNumLinks(); j != e; ++j)
358 NewNode->setLink(j, cloneGlobalInto(GNode->getLink(j)));
359
360 return NewNode;
361}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000362#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000363
364
Chris Lattner0d9bab82002-07-18 00:12:30 +0000365// markIncompleteNodes - Mark the specified node as having contents that are not
366// known with the current analysis we have performed. Because a node makes all
367// of the nodes it can reach imcomplete if the node itself is incomplete, we
368// must recursively traverse the data structure graph, marking all reachable
369// nodes as incomplete.
370//
371static void markIncompleteNode(DSNode *N) {
372 // Stop recursion if no node, or if node already marked...
373 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
374
375 // Actually mark the node
376 N->NodeType |= DSNode::Incomplete;
377
378 // Recusively process children...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000379 for (unsigned i = 0, e = N->getSize(); i != e; ++i)
380 if (DSNodeHandle *DSNH = N->getLink(i))
381 markIncompleteNode(DSNH->getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000382}
383
384
385// markIncompleteNodes - Traverse the graph, identifying nodes that may be
386// modified by other functions that have not been resolved yet. This marks
387// nodes that are reachable through three sources of "unknownness":
388//
389// Global Variables, Function Calls, and Incoming Arguments
390//
391// For any node that may have unknown components (because something outside the
392// scope of current analysis may have modified it), the 'Incomplete' flag is
393// added to the NodeType.
394//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000395void DSGraph::markIncompleteNodes(bool markFormalArgs) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000396 // Mark any incoming arguments as incomplete...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000397 if (markFormalArgs && Func)
398 for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I)
399 if (isPointerType(I->getType()) && ValueMap.find(I) != ValueMap.end()) {
400 DSNodeHandle &INH = ValueMap[I];
401 if (INH.getNode() && INH.hasLink(0))
402 markIncompleteNode(ValueMap[I].getLink(0)->getNode());
403 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000404
405 // Mark stuff passed into functions calls as being incomplete...
406 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Chris Lattnere2219762002-07-18 18:22:40 +0000407 vector<DSNodeHandle> &Args = FunctionCalls[i];
408 // Then the return value is certainly incomplete!
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000409 markIncompleteNode(Args[0].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000410
411 // The call does not make the function argument incomplete...
412
413 // All arguments to the function call are incomplete though!
414 for (unsigned i = 2, e = Args.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000415 markIncompleteNode(Args[i].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000416 }
417
Chris Lattner055dc2c2002-07-18 15:54:42 +0000418 // Mark all of the nodes pointed to by global or cast nodes as incomplete...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000419 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000420 if (Nodes[i]->NodeType & DSNode::GlobalNode) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000421 DSNode *N = Nodes[i];
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000422 for (unsigned i = 0, e = N->getSize(); i != e; ++i)
423 if (DSNodeHandle *DSNH = N->getLink(i))
424 markIncompleteNode(DSNH->getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000425 }
426}
427
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000428// removeRefsToGlobal - Helper function that removes globals from the
429// ValueMap so that the referrer count will go down to zero.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000430static void removeRefsToGlobal(DSNode* N,
431 std::map<Value*, DSNodeHandle> &ValueMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000432 while (!N->getGlobals().empty()) {
433 GlobalValue *GV = N->getGlobals().back();
434 N->getGlobals().pop_back();
435 ValueMap.erase(GV);
436 }
437}
438
439
Chris Lattner0d9bab82002-07-18 00:12:30 +0000440// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
441// checks to see if there are simple transformations that it can do to make it
442// dead.
443//
444bool DSGraph::isNodeDead(DSNode *N) {
445 // Is it a trivially dead shadow node...
446 if (N->getReferrers().empty() && N->NodeType == 0)
447 return true;
448
449 // Is it a function node or some other trivially unused global?
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000450 if (N->NodeType != 0 &&
451 (N->NodeType & ~DSNode::GlobalNode) == 0 &&
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000452 N->getSize() == 0 &&
Chris Lattner0d9bab82002-07-18 00:12:30 +0000453 N->getReferrers().size() == N->getGlobals().size()) {
454
455 // Remove the globals from the valuemap, so that the referrer count will go
456 // down to zero.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000457 removeRefsToGlobal(N, ValueMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000458 assert(N->getReferrers().empty() && "Referrers should all be gone now!");
459 return true;
460 }
461
462 return false;
463}
464
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000465static void removeIdenticalCalls(vector<vector<DSNodeHandle> > &Calls,
Chris Lattner7541b892002-07-31 19:32:12 +0000466 const std::string &where) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000467 // Remove trivially identical function calls
468 unsigned NumFns = Calls.size();
469 std::sort(Calls.begin(), Calls.end());
470 Calls.erase(std::unique(Calls.begin(), Calls.end()),
471 Calls.end());
472
473 DEBUG(if (NumFns != Calls.size())
Chris Lattner7541b892002-07-31 19:32:12 +0000474 std::cerr << "Merged " << (NumFns-Calls.size())
475 << " call nodes in " << where << "\n";);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000476}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000477
Chris Lattnere2219762002-07-18 18:22:40 +0000478// removeTriviallyDeadNodes - After the graph has been constructed, this method
479// removes all unreachable nodes that are created because they got merged with
480// other nodes in the graph. These nodes will all be trivially unreachable, so
481// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000482//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000483void DSGraph::removeTriviallyDeadNodes(bool KeepAllGlobals) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000484 for (unsigned i = 0; i != Nodes.size(); ++i)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000485 if (! KeepAllGlobals || ! (Nodes[i]->NodeType & DSNode::GlobalNode))
486 if (isNodeDead(Nodes[i])) { // This node is dead!
487 delete Nodes[i]; // Free memory...
488 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
489 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000490
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000491 removeIdenticalCalls(FunctionCalls, Func ? Func->getName() : "");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000492}
493
494
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000495// markAlive - Simple graph walker that recursively traverses the graph, marking
Chris Lattnere2219762002-07-18 18:22:40 +0000496// stuff to be alive.
497//
498static void markAlive(DSNode *N, std::set<DSNode*> &Alive) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000499 if (N == 0) return;
Chris Lattnere2219762002-07-18 18:22:40 +0000500
501 Alive.insert(N);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000502 for (unsigned i = 0, e = N->getSize(); i != e; ++i)
503 if (DSNodeHandle *DSNH = N->getLink(i))
504 if (!Alive.count(DSNH->getNode()))
505 markAlive(DSNH->getNode(), Alive);
Chris Lattnere2219762002-07-18 18:22:40 +0000506}
507
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000508static bool checkGlobalAlive(DSNode *N, std::set<DSNode*> &Alive,
509 std::set<DSNode*> &Visiting) {
510 if (N == 0) return false;
511
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000512 if (Visiting.count(N)) return false; // terminate recursion on a cycle
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000513 Visiting.insert(N);
514
515 // If any immediate successor is alive, N is alive
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000516 for (unsigned i = 0, e = N->getSize(); i != e; ++i)
517 if (DSNodeHandle *DSNH = N->getLink(i))
518 if (Alive.count(DSNH->getNode())) {
519 Visiting.erase(N);
520 return true;
521 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000522
523 // Else if any successor reaches a live node, N is alive
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000524 for (unsigned i = 0, e = N->getSize(); i != e; ++i)
525 if (DSNodeHandle *DSNH = N->getLink(i))
526 if (checkGlobalAlive(DSNH->getNode(), Alive, Visiting)) {
527 Visiting.erase(N); return true;
528 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000529
530 Visiting.erase(N);
531 return false;
532}
533
534
535// markGlobalsIteration - Recursive helper function for markGlobalsAlive().
536// This would be unnecessary if function calls were real nodes! In that case,
537// the simple iterative loop in the first few lines below suffice.
538//
539static void markGlobalsIteration(std::set<DSNode*>& GlobalNodes,
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000540 vector<vector<DSNodeHandle> > &Calls,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000541 std::set<DSNode*> &Alive,
542 bool FilterCalls) {
543
544 // Iterate, marking globals or cast nodes alive until no new live nodes
545 // are added to Alive
546 std::set<DSNode*> Visiting; // Used to identify cycles
547 std::set<DSNode*>::iterator I=GlobalNodes.begin(), E=GlobalNodes.end();
548 for (size_t liveCount = 0; liveCount < Alive.size(); ) {
549 liveCount = Alive.size();
550 for ( ; I != E; ++I)
551 if (Alive.count(*I) == 0) {
552 Visiting.clear();
553 if (checkGlobalAlive(*I, Alive, Visiting))
554 markAlive(*I, Alive);
555 }
556 }
557
558 // Find function calls with some dead and some live nodes.
559 // Since all call nodes must be live if any one is live, we have to mark
560 // all nodes of the call as live and continue the iteration (via recursion).
561 if (FilterCalls) {
562 bool recurse = false;
563 for (int i = 0, ei = Calls.size(); i < ei; ++i) {
564 bool CallIsDead = true, CallHasDeadArg = false;
565 for (unsigned j = 0, ej = Calls[i].size(); j != ej; ++j) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000566 bool argIsDead = Calls[i][j].getNode() == 0 ||
567 Alive.count(Calls[i][j].getNode()) == 0;
568 CallHasDeadArg |= (Calls[i][j].getNode() != 0 && argIsDead);
569 CallIsDead &= argIsDead;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000570 }
571 if (!CallIsDead && CallHasDeadArg) {
572 // Some node in this call is live and another is dead.
573 // Mark all nodes of call as live and iterate once more.
574 recurse = true;
575 for (unsigned j = 0, ej = Calls[i].size(); j != ej; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000576 markAlive(Calls[i][j].getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000577 }
578 }
579 if (recurse)
580 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
581 }
582}
583
584
585// markGlobalsAlive - Mark global nodes and cast nodes alive if they
586// can reach any other live node. Since this can produce new live nodes,
587// we use a simple iterative algorithm.
588//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000589static void markGlobalsAlive(DSGraph &G, std::set<DSNode*> &Alive,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000590 bool FilterCalls) {
591 // Add global and cast nodes to a set so we don't walk all nodes every time
592 std::set<DSNode*> GlobalNodes;
593 for (unsigned i = 0, e = G.getNodes().size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000594 if (G.getNodes()[i]->NodeType & DSNode::GlobalNode)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000595 GlobalNodes.insert(G.getNodes()[i]);
596
597 // Add all call nodes to the same set
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000598 vector<vector<DSNodeHandle> > &Calls = G.getFunctionCalls();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000599 if (FilterCalls) {
600 for (unsigned i = 0, e = Calls.size(); i != e; ++i)
601 for (unsigned j = 0, e = Calls[i].size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000602 if (Calls[i][j].getNode())
603 GlobalNodes.insert(Calls[i][j].getNode());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000604 }
605
606 // Iterate and recurse until no new live node are discovered.
607 // This would be a simple iterative loop if function calls were real nodes!
608 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
609
610 // Free up references to dead globals from the ValueMap
611 std::set<DSNode*>::iterator I=GlobalNodes.begin(), E=GlobalNodes.end();
612 for( ; I != E; ++I)
613 if (Alive.count(*I) == 0)
614 removeRefsToGlobal(*I, G.getValueMap());
615
616 // Delete dead function calls
617 if (FilterCalls)
618 for (int ei = Calls.size(), i = ei-1; i >= 0; --i) {
619 bool CallIsDead = true;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000620 for (unsigned j = 0, ej = Calls[i].size(); CallIsDead && j != ej; ++j)
621 CallIsDead = Alive.count(Calls[i][j].getNode()) == 0;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000622 if (CallIsDead)
623 Calls.erase(Calls.begin() + i); // remove the call entirely
624 }
625}
Chris Lattnere2219762002-07-18 18:22:40 +0000626
627// removeDeadNodes - Use a more powerful reachability analysis to eliminate
628// subgraphs that are unreachable. This often occurs because the data
629// structure doesn't "escape" into it's caller, and thus should be eliminated
630// from the caller's graph entirely. This is only appropriate to use when
631// inlining graphs.
632//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000633void DSGraph::removeDeadNodes(bool KeepAllGlobals, bool KeepCalls) {
634 assert((!KeepAllGlobals || KeepCalls) &&
635 "KeepAllGlobals without KeepCalls is meaningless");
636
Chris Lattnere2219762002-07-18 18:22:40 +0000637 // Reduce the amount of work we have to do...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000638 removeTriviallyDeadNodes(KeepAllGlobals);
639
Chris Lattnere2219762002-07-18 18:22:40 +0000640 // FIXME: Merge nontrivially identical call nodes...
641
642 // Alive - a set that holds all nodes found to be reachable/alive.
643 std::set<DSNode*> Alive;
644
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000645 // If KeepCalls, mark all nodes reachable by call nodes as alive...
646 if (KeepCalls)
647 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
648 for (unsigned j = 0, e = FunctionCalls[i].size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000649 markAlive(FunctionCalls[i][j].getNode(), Alive);
Chris Lattnere2219762002-07-18 18:22:40 +0000650
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000651#if 0
Chris Lattnere2219762002-07-18 18:22:40 +0000652 for (unsigned i = 0, e = OrigFunctionCalls.size(); i != e; ++i)
653 for (unsigned j = 0, e = OrigFunctionCalls[i].size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000654 markAlive(OrigFunctionCalls[i][j].getNode(), Alive);
655#endif
Chris Lattnere2219762002-07-18 18:22:40 +0000656
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000657 // Mark all nodes reachable by scalar nodes (and global nodes, if
658 // keeping them was specified) as alive...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000659 unsigned char keepBits = DSNode::ScalarNode |
660 (KeepAllGlobals ? DSNode::GlobalNode : 0);
Chris Lattnere2219762002-07-18 18:22:40 +0000661 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000662 if (Nodes[i]->NodeType & keepBits)
Chris Lattnere2219762002-07-18 18:22:40 +0000663 markAlive(Nodes[i], Alive);
664
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000665 // The return value is alive as well...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000666 markAlive(RetNode.getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000667
668 // Mark all globals or cast nodes that can reach a live node as alive.
669 // This also marks all nodes reachable from such nodes as alive.
670 // Of course, if KeepAllGlobals is specified, they would be live already.
671 if (! KeepAllGlobals)
672 markGlobalsAlive(*this, Alive, ! KeepCalls);
673
Chris Lattnere2219762002-07-18 18:22:40 +0000674 // Loop over all unreachable nodes, dropping their references...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000675 vector<DSNode*> DeadNodes;
Chris Lattnere2219762002-07-18 18:22:40 +0000676 DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed.
677 for (unsigned i = 0; i != Nodes.size(); ++i)
678 if (!Alive.count(Nodes[i])) {
679 DSNode *N = Nodes[i];
680 Nodes.erase(Nodes.begin()+i--); // Erase node from alive list.
681 DeadNodes.push_back(N); // Add node to our list of dead nodes
682 N->dropAllReferences(); // Drop all outgoing edges
683 }
684
Chris Lattnere2219762002-07-18 18:22:40 +0000685 // Delete all dead nodes...
686 std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
687}
688
689
690
Chris Lattner0d9bab82002-07-18 00:12:30 +0000691// maskNodeTypes - Apply a mask to all of the node types in the graph. This
692// is useful for clearing out markers like Scalar or Incomplete.
693//
694void DSGraph::maskNodeTypes(unsigned char Mask) {
695 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
696 Nodes[i]->NodeType &= Mask;
697}
698
699
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000700#if 0
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000701//===----------------------------------------------------------------------===//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000702// GlobalDSGraph Implementation
703//===----------------------------------------------------------------------===//
704
705GlobalDSGraph::GlobalDSGraph() : DSGraph(*(Function*)0, this) {
706}
707
708GlobalDSGraph::~GlobalDSGraph() {
709 assert(Referrers.size() == 0 &&
710 "Deleting global graph while references from other graphs exist");
711}
712
713void GlobalDSGraph::addReference(const DSGraph* referrer) {
714 if (referrer != this)
715 Referrers.insert(referrer);
716}
717
718void GlobalDSGraph::removeReference(const DSGraph* referrer) {
719 if (referrer != this) {
720 assert(Referrers.find(referrer) != Referrers.end() && "This is very bad!");
721 Referrers.erase(referrer);
722 if (Referrers.size() == 0)
723 delete this;
724 }
725}
726
727// Bits used in the next function
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000728static const char ExternalTypeBits = DSNode::GlobalNode | DSNode::NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000729
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000730#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000731// GlobalDSGraph::cloneNodeInto - Clone a global node and all its externally
732// visible target links (and recursively their such links) into this graph.
733// NodeCache maps the node being cloned to its clone in the Globals graph,
734// in order to track cycles.
735// GlobalsAreFinal is a flag that says whether it is safe to assume that
736// an existing global node is complete. This is important to avoid
737// reinserting all globals when inserting Calls to functions.
738// This is a helper function for cloneGlobals and cloneCalls.
739//
740DSNode* GlobalDSGraph::cloneNodeInto(DSNode *OldNode,
741 std::map<const DSNode*, DSNode*> &NodeCache,
742 bool GlobalsAreFinal) {
743 if (OldNode == 0) return 0;
744
745 // The caller should check this is an external node. Just more efficient...
746 assert((OldNode->NodeType & ExternalTypeBits) && "Non-external node");
747
748 // If a clone has already been created for OldNode, return it.
749 DSNode*& CacheEntry = NodeCache[OldNode];
750 if (CacheEntry != 0)
751 return CacheEntry;
752
753 // The result value...
754 DSNode* NewNode = 0;
755
756 // If nodes already exist for any of the globals of OldNode,
757 // merge all such nodes together since they are merged in OldNode.
758 // If ValueCacheIsFinal==true, look for an existing node that has
759 // an identical list of globals and return it if it exists.
760 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000761 for (unsigned j = 0, N = OldNode->getGlobals().size(); j != N; ++j)
762 if (DSNode *PrevNode = ValueMap[OldNode->getGlobals()[j]].getNode()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000763 if (NewNode == 0) {
764 NewNode = PrevNode; // first existing node found
765 if (GlobalsAreFinal && j == 0)
766 if (OldNode->getGlobals() == PrevNode->getGlobals()) {
767 CacheEntry = NewNode;
768 return NewNode;
769 }
770 }
771 else if (NewNode != PrevNode) { // found another, different from prev
772 // update ValMap *before* merging PrevNode into NewNode
773 for (unsigned k = 0, NK = PrevNode->getGlobals().size(); k < NK; ++k)
774 ValueMap[PrevNode->getGlobals()[k]] = NewNode;
775 NewNode->mergeWith(PrevNode);
776 }
777 } else if (NewNode != 0) {
778 ValueMap[OldNode->getGlobals()[j]] = NewNode; // add the merged node
779 }
780
781 // If no existing node was found, clone the node and update the ValMap.
782 if (NewNode == 0) {
783 NewNode = new DSNode(*OldNode);
784 Nodes.push_back(NewNode);
785 for (unsigned j = 0, e = NewNode->getNumLinks(); j != e; ++j)
786 NewNode->setLink(j, 0);
787 for (unsigned j = 0, N = NewNode->getGlobals().size(); j < N; ++j)
788 ValueMap[NewNode->getGlobals()[j]] = NewNode;
789 }
790 else
791 NewNode->NodeType |= OldNode->NodeType; // Markers may be different!
792
793 // Add the entry to NodeCache
794 CacheEntry = NewNode;
795
796 // Rewrite the links in the new node to point into the current graph,
797 // but only for links to external nodes. Set other links to NULL.
798 for (unsigned j = 0, e = OldNode->getNumLinks(); j != e; ++j) {
799 DSNode* OldTarget = OldNode->getLink(j);
800 if (OldTarget && (OldTarget->NodeType & ExternalTypeBits)) {
801 DSNode* NewLink = this->cloneNodeInto(OldTarget, NodeCache);
802 if (NewNode->getLink(j))
803 NewNode->getLink(j)->mergeWith(NewLink);
804 else
805 NewNode->setLink(j, NewLink);
806 }
807 }
808
809 // Remove all local markers
810 NewNode->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode);
811
812 return NewNode;
813}
814
815
816// GlobalDSGraph::cloneGlobals - Clone global nodes and all their externally
817// visible target links (and recursively their such links) into this graph.
818//
819void GlobalDSGraph::cloneGlobals(DSGraph& Graph, bool CloneCalls) {
820 std::map<const DSNode*, DSNode*> NodeCache;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000821#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000822 for (unsigned i = 0, N = Graph.Nodes.size(); i < N; ++i)
823 if (Graph.Nodes[i]->NodeType & DSNode::GlobalNode)
824 GlobalsGraph->cloneNodeInto(Graph.Nodes[i], NodeCache, false);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000825 if (CloneCalls)
826 GlobalsGraph->cloneCalls(Graph);
827
828 GlobalsGraph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000829#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000830}
831
832
833// GlobalDSGraph::cloneCalls - Clone function calls and their visible target
834// links (and recursively their such links) into this graph.
835//
836void GlobalDSGraph::cloneCalls(DSGraph& Graph) {
837 std::map<const DSNode*, DSNode*> NodeCache;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000838 vector<vector<DSNodeHandle> >& FromCalls =Graph.FunctionCalls;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000839
840 FunctionCalls.reserve(FunctionCalls.size() + FromCalls.size());
841
842 for (int i = 0, ei = FromCalls.size(); i < ei; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000843 FunctionCalls.push_back(vector<DSNodeHandle>());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000844 FunctionCalls.back().reserve(FromCalls[i].size());
845 for (unsigned j = 0, ej = FromCalls[i].size(); j != ej; ++j)
846 FunctionCalls.back().push_back
847 ((FromCalls[i][j] && (FromCalls[i][j]->NodeType & ExternalTypeBits))
848 ? cloneNodeInto(FromCalls[i][j], NodeCache, true)
849 : 0);
850 }
851
852 // remove trivially identical function calls
Chris Lattner7541b892002-07-31 19:32:12 +0000853 removeIdenticalCalls(FunctionCalls, "Globals Graph");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000854}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000855#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000856
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000857#endif