blob: a7653115b8ec617d907c917450004cbb65c9da1a [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"
Vikram S. Adve26b98262002-10-20 21:41:02 +00009#include "llvm/iOther.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000010#include "llvm/DerivedTypes.h"
Chris Lattner7b7200c2002-10-02 04:57:39 +000011#include "llvm/Target/TargetData.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000012#include "Support/STLExtras.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000013#include "Support/Statistic.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000014#include <algorithm>
Chris Lattnerfccd06f2002-10-01 22:33:50 +000015#include <set>
Chris Lattnerc68c31b2002-07-10 22:38:08 +000016
Chris Lattnere2219762002-07-18 18:22:40 +000017using std::vector;
18
Chris Lattner08db7192002-11-06 06:20:27 +000019namespace {
Chris Lattner33312f72002-11-08 01:21:07 +000020 Statistic<> NumFolds ("dsnode", "Number of nodes completely folded");
21 Statistic<> NumCallNodesMerged("dsnode", "Number of call nodes merged");
Chris Lattner08db7192002-11-06 06:20:27 +000022};
23
Chris Lattnerb1060432002-11-07 05:20:53 +000024namespace DS { // TODO: FIXME
Chris Lattnerfccd06f2002-10-01 22:33:50 +000025 extern TargetData TD;
26}
Chris Lattnerb1060432002-11-07 05:20:53 +000027using namespace DS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000028
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000029//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +000030// DSNode Implementation
31//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000032
Chris Lattner08db7192002-11-06 06:20:27 +000033DSNode::DSNode(enum NodeTy NT, const Type *T)
34 : Ty(Type::VoidTy), Size(0), NodeType(NT) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +000035 // Add the type entry if it is specified...
Chris Lattner08db7192002-11-06 06:20:27 +000036 if (T) mergeTypeInfo(T, 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000037}
38
Chris Lattner0d9bab82002-07-18 00:12:30 +000039// DSNode copy constructor... do not copy over the referrers list!
40DSNode::DSNode(const DSNode &N)
Chris Lattner08db7192002-11-06 06:20:27 +000041 : Links(N.Links), Globals(N.Globals), Ty(N.Ty), Size(N.Size),
42 NodeType(N.NodeType) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000043}
44
Chris Lattnerc68c31b2002-07-10 22:38:08 +000045void DSNode::removeReferrer(DSNodeHandle *H) {
46 // Search backwards, because we depopulate the list from the back for
47 // efficiency (because it's a vector).
Chris Lattnere2219762002-07-18 18:22:40 +000048 vector<DSNodeHandle*>::reverse_iterator I =
Chris Lattnerc68c31b2002-07-10 22:38:08 +000049 std::find(Referrers.rbegin(), Referrers.rend(), H);
50 assert(I != Referrers.rend() && "Referrer not pointing to node!");
51 Referrers.erase(I.base()-1);
52}
53
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000054// addGlobal - Add an entry for a global value to the Globals list. This also
55// marks the node with the 'G' flag if it does not already have it.
56//
57void DSNode::addGlobal(GlobalValue *GV) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000058 // Keep the list sorted.
Chris Lattnere2219762002-07-18 18:22:40 +000059 vector<GlobalValue*>::iterator I =
Chris Lattner0d9bab82002-07-18 00:12:30 +000060 std::lower_bound(Globals.begin(), Globals.end(), GV);
61
62 if (I == Globals.end() || *I != GV) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000063 //assert(GV->getType()->getElementType() == Ty);
Chris Lattner0d9bab82002-07-18 00:12:30 +000064 Globals.insert(I, GV);
65 NodeType |= GlobalNode;
66 }
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000067}
68
Chris Lattner8f0a16e2002-10-31 05:45:02 +000069/// foldNodeCompletely - If we determine that this node has some funny
70/// behavior happening to it that we cannot represent, we fold it down to a
71/// single, completely pessimistic, node. This node is represented as a
72/// single byte with a single TypeEntry of "void".
73///
74void DSNode::foldNodeCompletely() {
Chris Lattner08db7192002-11-06 06:20:27 +000075 if (isNodeCompletelyFolded()) return;
Chris Lattner8f0a16e2002-10-31 05:45:02 +000076
Chris Lattner08db7192002-11-06 06:20:27 +000077 ++NumFolds;
78
79 // We are no longer typed at all...
80 Ty = DSTypeRec(Type::VoidTy, true);
81 Size = 1;
82
83 // Loop over all of our referrers, making them point to our zero bytes of
84 // space.
Chris Lattner8f0a16e2002-10-31 05:45:02 +000085 for (vector<DSNodeHandle*>::iterator I = Referrers.begin(), E=Referrers.end();
86 I != E; ++I)
87 (*I)->setOffset(0);
88
Chris Lattner8f0a16e2002-10-31 05:45:02 +000089 // If we have links, merge all of our outgoing links together...
Chris Lattner08db7192002-11-06 06:20:27 +000090 for (unsigned i = 1, e = Links.size(); i < e; ++i)
91 Links[0].mergeWith(Links[i]);
92 Links.resize(1);
Chris Lattner8f0a16e2002-10-31 05:45:02 +000093}
Chris Lattner076c1f92002-11-07 06:31:54 +000094
Chris Lattner8f0a16e2002-10-31 05:45:02 +000095/// isNodeCompletelyFolded - Return true if this node has been completely
96/// folded down to something that can never be expanded, effectively losing
97/// all of the field sensitivity that may be present in the node.
98///
99bool DSNode::isNodeCompletelyFolded() const {
Chris Lattner08db7192002-11-06 06:20:27 +0000100 return getSize() == 1 && Ty.Ty == Type::VoidTy && Ty.isArray;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000101}
102
103
Chris Lattner08db7192002-11-06 06:20:27 +0000104/// mergeTypeInfo - This method merges the specified type into the current node
105/// at the specified offset. This may update the current node's type record if
106/// this gives more information to the node, it may do nothing to the node if
107/// this information is already known, or it may merge the node completely (and
108/// return true) if the information is incompatible with what is already known.
Chris Lattner7b7200c2002-10-02 04:57:39 +0000109///
Chris Lattner08db7192002-11-06 06:20:27 +0000110/// This method returns true if the node is completely folded, otherwise false.
111///
112bool DSNode::mergeTypeInfo(const Type *NewTy, unsigned Offset) {
113 // Check to make sure the Size member is up-to-date. Size can be one of the
114 // following:
115 // Size = 0, Ty = Void: Nothing is known about this node.
116 // Size = 0, Ty = FnTy: FunctionPtr doesn't have a size, so we use zero
117 // Size = 1, Ty = Void, Array = 1: The node is collapsed
118 // Otherwise, sizeof(Ty) = Size
119 //
120 assert(((Size == 0 && Ty.Ty == Type::VoidTy && !Ty.isArray) ||
121 (Size == 0 && !Ty.Ty->isSized() && !Ty.isArray) ||
122 (Size == 1 && Ty.Ty == Type::VoidTy && Ty.isArray) ||
123 (Size == 0 && !Ty.Ty->isSized() && !Ty.isArray) ||
124 (TD.getTypeSize(Ty.Ty) == Size)) &&
125 "Size member of DSNode doesn't match the type structure!");
126 assert(NewTy != Type::VoidTy && "Cannot merge void type into DSNode!");
Chris Lattner7b7200c2002-10-02 04:57:39 +0000127
Chris Lattner08db7192002-11-06 06:20:27 +0000128 if (Offset == 0 && NewTy == Ty.Ty)
129 return false; // This should be a common case, handle it efficiently
Chris Lattner7b7200c2002-10-02 04:57:39 +0000130
Chris Lattner08db7192002-11-06 06:20:27 +0000131 // Return true immediately if the node is completely folded.
132 if (isNodeCompletelyFolded()) return true;
133
134 // Figure out how big the new type we're merging in is...
135 unsigned NewTySize = NewTy->isSized() ? TD.getTypeSize(NewTy) : 0;
136
137 // Otherwise check to see if we can fold this type into the current node. If
138 // we can't, we fold the node completely, if we can, we potentially update our
139 // internal state.
140 //
141 if (Ty.Ty == Type::VoidTy) {
142 // If this is the first type that this node has seen, just accept it without
143 // question....
144 assert(Offset == 0 && "Cannot have an offset into a void node!");
145 assert(Ty.isArray == false && "This shouldn't happen!");
146 Ty.Ty = NewTy;
147 Size = NewTySize;
148
149 // Calculate the number of outgoing links from this node.
150 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
151 return false;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000152 }
Chris Lattner08db7192002-11-06 06:20:27 +0000153
154 // Handle node expansion case here...
155 if (Offset+NewTySize > Size) {
156 // It is illegal to grow this node if we have treated it as an array of
157 // objects...
158 if (Ty.isArray) {
159 foldNodeCompletely();
160 return true;
161 }
162
163 if (Offset) { // We could handle this case, but we don't for now...
Chris Lattner3c87b292002-11-07 01:54:56 +0000164 DEBUG(std::cerr << "UNIMP: Trying to merge a growth type into "
165 << "offset != 0: Collapsing!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000166 foldNodeCompletely();
167 return true;
168 }
169
170 // Okay, the situation is nice and simple, we are trying to merge a type in
171 // at offset 0 that is bigger than our current type. Implement this by
172 // switching to the new type and then merge in the smaller one, which should
173 // hit the other code path here. If the other code path decides it's not
174 // ok, it will collapse the node as appropriate.
175 //
176 const Type *OldTy = Ty.Ty;
177 Ty.Ty = NewTy;
178 Size = NewTySize;
179
180 // Must grow links to be the appropriate size...
181 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
182
183 // Merge in the old type now... which is guaranteed to be smaller than the
184 // "current" type.
185 return mergeTypeInfo(OldTy, 0);
186 }
187
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000188 assert(Offset <= Size &&
Chris Lattner08db7192002-11-06 06:20:27 +0000189 "Cannot merge something into a part of our type that doesn't exist!");
190
191 // Find the section of Ty.Ty that NewTy overlaps with... first we find the
192 // type that starts at offset Offset.
193 //
194 unsigned O = 0;
195 const Type *SubType = Ty.Ty;
196 while (O < Offset) {
197 assert(Offset-O < TD.getTypeSize(SubType) && "Offset out of range!");
198
199 switch (SubType->getPrimitiveID()) {
200 case Type::StructTyID: {
201 const StructType *STy = cast<StructType>(SubType);
202 const StructLayout &SL = *TD.getStructLayout(STy);
203
204 unsigned i = 0, e = SL.MemberOffsets.size();
205 for (; i+1 < e && SL.MemberOffsets[i+1] <= Offset-O; ++i)
206 /* empty */;
207
208 // The offset we are looking for must be in the i'th element...
209 SubType = STy->getElementTypes()[i];
210 O += SL.MemberOffsets[i];
211 break;
212 }
213 case Type::ArrayTyID: {
214 SubType = cast<ArrayType>(SubType)->getElementType();
215 unsigned ElSize = TD.getTypeSize(SubType);
216 unsigned Remainder = (Offset-O) % ElSize;
217 O = Offset-Remainder;
218 break;
219 }
220 default:
221 assert(0 && "Unknown type!");
222 }
223 }
224
225 assert(O == Offset && "Could not achieve the correct offset!");
226
227 // If we found our type exactly, early exit
228 if (SubType == NewTy) return false;
229
230 // Okay, so we found the leader type at the offset requested. Search the list
231 // of types that starts at this offset. If SubType is currently an array or
232 // structure, the type desired may actually be the first element of the
233 // composite type...
234 //
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000235 unsigned SubTypeSize = SubType->isSized() ? TD.getTypeSize(SubType) : 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000236 while (SubType != NewTy) {
237 const Type *NextSubType = 0;
238 unsigned NextSubTypeSize;
239 switch (SubType->getPrimitiveID()) {
240 case Type::StructTyID:
241 NextSubType = cast<StructType>(SubType)->getElementTypes()[0];
242 NextSubTypeSize = TD.getTypeSize(SubType);
243 break;
244 case Type::ArrayTyID:
245 NextSubType = cast<ArrayType>(SubType)->getElementType();
246 NextSubTypeSize = TD.getTypeSize(SubType);
247 break;
248 default: ;
249 // fall out
250 }
251
252 if (NextSubType == 0)
253 break; // In the default case, break out of the loop
254
255 if (NextSubTypeSize < NewTySize)
256 break; // Don't allow shrinking to a smaller type than NewTySize
257 SubType = NextSubType;
258 SubTypeSize = NextSubTypeSize;
259 }
260
261 // If we found the type exactly, return it...
262 if (SubType == NewTy)
263 return false;
264
265 // Check to see if we have a compatible, but different type...
266 if (NewTySize == SubTypeSize) {
267 // Check to see if this type is obviously convertable... int -> uint f.e.
268 if (NewTy->isLosslesslyConvertableTo(SubType))
269 return false;
270
271 // Check to see if we have a pointer & integer mismatch going on here,
272 // loading a pointer as a long, for example.
273 //
274 if (SubType->isInteger() && isa<PointerType>(NewTy) ||
275 NewTy->isInteger() && isa<PointerType>(SubType))
276 return false;
277
278 }
279
280
Chris Lattner3c87b292002-11-07 01:54:56 +0000281 DEBUG(std::cerr << "MergeTypeInfo Folding OrigTy: " << Ty.Ty
282 << "\n due to:" << NewTy << " @ " << Offset << "!\n"
283 << "SubType: " << SubType << "\n\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000284
285 foldNodeCompletely();
286 return true;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000287}
288
Chris Lattner08db7192002-11-06 06:20:27 +0000289
290
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000291// addEdgeTo - Add an edge from the current node to the specified node. This
292// can cause merging of nodes in the graph.
293//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000294void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000295 if (NH.getNode() == 0) return; // Nothing to do
296
Chris Lattner08db7192002-11-06 06:20:27 +0000297 DSNodeHandle &ExistingEdge = getLink(Offset);
298 if (ExistingEdge.getNode()) {
Chris Lattner7b7200c2002-10-02 04:57:39 +0000299 // Merge the two nodes...
Chris Lattner08db7192002-11-06 06:20:27 +0000300 ExistingEdge.mergeWith(NH);
Chris Lattner7b7200c2002-10-02 04:57:39 +0000301 } else { // No merging to perform...
302 setLink(Offset, NH); // Just force a link in there...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000303 }
Chris Lattner7b7200c2002-10-02 04:57:39 +0000304}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000305
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000306
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000307// MergeSortedVectors - Efficiently merge a vector into another vector where
308// duplicates are not allowed and both are sorted. This assumes that 'T's are
309// efficiently copyable and have sane comparison semantics.
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000310//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000311template<typename T>
312void MergeSortedVectors(vector<T> &Dest, const vector<T> &Src) {
313 // By far, the most common cases will be the simple ones. In these cases,
314 // avoid having to allocate a temporary vector...
315 //
316 if (Src.empty()) { // Nothing to merge in...
317 return;
318 } else if (Dest.empty()) { // Just copy the result in...
319 Dest = Src;
320 } else if (Src.size() == 1) { // Insert a single element...
321 const T &V = Src[0];
322 typename vector<T>::iterator I =
323 std::lower_bound(Dest.begin(), Dest.end(), V);
324 if (I == Dest.end() || *I != Src[0]) // If not already contained...
325 Dest.insert(I, Src[0]);
326 } else if (Dest.size() == 1) {
327 T Tmp = Dest[0]; // Save value in temporary...
328 Dest = Src; // Copy over list...
329 typename vector<T>::iterator I =
330 std::lower_bound(Dest.begin(), Dest.end(),Tmp);
331 if (I == Dest.end() || *I != Src[0]) // If not already contained...
332 Dest.insert(I, Src[0]);
333
334 } else {
335 // Make a copy to the side of Dest...
336 vector<T> Old(Dest);
337
338 // Make space for all of the type entries now...
339 Dest.resize(Dest.size()+Src.size());
340
341 // Merge the two sorted ranges together... into Dest.
342 std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin());
343
344 // Now erase any duplicate entries that may have accumulated into the
345 // vectors (because they were in both of the input sets)
346 Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end());
347 }
348}
349
350
351// mergeWith - Merge this node and the specified node, moving all links to and
352// from the argument node into the current node, deleting the node argument.
353// Offset indicates what offset the specified node is to be merged into the
354// current node.
355//
356// The specified node may be a null pointer (in which case, nothing happens).
357//
358void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
359 DSNode *N = NH.getNode();
360 if (N == 0 || (N == this && NH.getOffset() == Offset))
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000361 return; // Noop
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000362
Chris Lattner679e8e12002-11-08 21:27:12 +0000363 assert((N->NodeType & DSNode::DEAD) == 0);
364 assert((NodeType & DSNode::DEAD) == 0);
365 assert(!hasNoReferrers() && "Should not try to fold a useless node!");
366
Chris Lattner02606632002-11-04 06:48:26 +0000367 if (N == this) {
Chris Lattner08db7192002-11-06 06:20:27 +0000368 // We cannot merge two pieces of the same node together, collapse the node
369 // completely.
Chris Lattner3c87b292002-11-07 01:54:56 +0000370 DEBUG(std::cerr << "Attempting to merge two chunks of"
371 << " the same node together!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000372 foldNodeCompletely();
Chris Lattner02606632002-11-04 06:48:26 +0000373 return;
374 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000375
Chris Lattner08db7192002-11-06 06:20:27 +0000376 // Merge the type entries of the two nodes together...
Chris Lattner679e8e12002-11-08 21:27:12 +0000377 if (N->Ty.Ty != Type::VoidTy) {
Chris Lattner08db7192002-11-06 06:20:27 +0000378 mergeTypeInfo(N->Ty.Ty, Offset);
379
Chris Lattner679e8e12002-11-08 21:27:12 +0000380 // mergeTypeInfo can cause collapsing, which can cause this node to become
381 // dead.
382 if (hasNoReferrers()) return;
383 }
384 assert((NodeType & DSNode::DEAD) == 0);
385
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000386 // If we are merging a node with a completely folded node, then both nodes are
387 // now completely folded.
388 //
389 if (isNodeCompletelyFolded()) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000390 if (!N->isNodeCompletelyFolded()) {
Chris Lattner02606632002-11-04 06:48:26 +0000391 N->foldNodeCompletely();
Chris Lattner679e8e12002-11-08 21:27:12 +0000392 if (hasNoReferrers()) return;
393 }
Chris Lattner02606632002-11-04 06:48:26 +0000394 } else if (N->isNodeCompletelyFolded()) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000395 foldNodeCompletely();
396 Offset = 0;
Chris Lattner679e8e12002-11-08 21:27:12 +0000397 if (hasNoReferrers()) return;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000398 }
Chris Lattner02606632002-11-04 06:48:26 +0000399 N = NH.getNode();
Chris Lattner679e8e12002-11-08 21:27:12 +0000400 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner02606632002-11-04 06:48:26 +0000401
Chris Lattner2c0bd012002-11-06 18:01:39 +0000402 if (this == N || N == 0) return;
Chris Lattner679e8e12002-11-08 21:27:12 +0000403 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000404
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000405 // If both nodes are not at offset 0, make sure that we are merging the node
406 // at an later offset into the node with the zero offset.
407 //
408 if (Offset > NH.getOffset()) {
409 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
410 return;
Chris Lattner9b87c5c2002-10-31 22:41:15 +0000411 } else if (Offset == NH.getOffset() && getSize() < N->getSize()) {
412 // If the offsets are the same, merge the smaller node into the bigger node
413 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
414 return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000415 }
416
417#if 0
418 std::cerr << "\n\nMerging:\n";
419 N->print(std::cerr, 0);
420 std::cerr << " and:\n";
421 print(std::cerr, 0);
422#endif
423
424 // Now we know that Offset <= NH.Offset, so convert it so our "Offset" (with
425 // respect to NH.Offset) is now zero.
426 //
427 unsigned NOffset = NH.getOffset()-Offset;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000428 unsigned NSize = N->getSize();
Chris Lattner7b7200c2002-10-02 04:57:39 +0000429
Chris Lattner679e8e12002-11-08 21:27:12 +0000430 assert((NodeType & DSNode::DEAD) == 0);
431
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000432 // Remove all edges pointing at N, causing them to point to 'this' instead.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000433 // Make sure to adjust their offset, not just the node pointer.
434 //
435 while (!N->Referrers.empty()) {
436 DSNodeHandle &Ref = *N->Referrers.back();
437 Ref = DSNodeHandle(this, NOffset+Ref.getOffset());
438 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000439 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner59535132002-11-05 00:01:58 +0000440
441 // Make all of the outgoing links of N now be outgoing links of this. This
442 // can cause recursive merging!
443 //
Chris Lattner08db7192002-11-06 06:20:27 +0000444 for (unsigned i = 0; i < NSize; i += DS::PointerSize) {
445 DSNodeHandle &Link = N->getLink(i);
446 if (Link.getNode()) {
447 addEdgeTo((i+NOffset) % getSize(), Link);
Chris Lattner59535132002-11-05 00:01:58 +0000448
Chris Lattner08db7192002-11-06 06:20:27 +0000449 // It's possible that after adding the new edge that some recursive
450 // merging just occured, causing THIS node to get merged into oblivion.
451 // If that happens, we must not try to merge any more edges into it!
Chris Lattner7b7200c2002-10-02 04:57:39 +0000452 //
Chris Lattner08db7192002-11-06 06:20:27 +0000453 if (Size == 0) return;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000454 }
455 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000456
457 // Now that there are no outgoing edges, all of the Links are dead.
458 N->Links.clear();
Chris Lattner08db7192002-11-06 06:20:27 +0000459 N->Size = 0;
460 N->Ty.Ty = Type::VoidTy;
461 N->Ty.isArray = false;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000462
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000463 // Merge the node types
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000464 NodeType |= N->NodeType;
Chris Lattner33312f72002-11-08 01:21:07 +0000465 N->NodeType = DEAD; // N is now a dead node.
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000466
467 // Merge the globals list...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000468 if (!N->Globals.empty()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000469 MergeSortedVectors(Globals, N->Globals);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000470
471 // Delete the globals from the old node...
472 N->Globals.clear();
473 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000474}
475
Chris Lattner9de906c2002-10-20 22:11:44 +0000476//===----------------------------------------------------------------------===//
477// DSCallSite Implementation
478//===----------------------------------------------------------------------===//
479
Vikram S. Adve26b98262002-10-20 21:41:02 +0000480// Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h
Chris Lattner9de906c2002-10-20 22:11:44 +0000481Function &DSCallSite::getCaller() const {
Chris Lattner0969c502002-10-21 02:08:03 +0000482 return *Inst->getParent()->getParent();
Vikram S. Adve26b98262002-10-20 21:41:02 +0000483}
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000484
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000485
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000486//===----------------------------------------------------------------------===//
487// DSGraph Implementation
488//===----------------------------------------------------------------------===//
489
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000490DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000491 std::map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattnerc875f022002-11-03 21:27:48 +0000492 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000493}
494
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000495DSGraph::DSGraph(const DSGraph &G,
496 std::map<const DSNode*, DSNodeHandle> &NodeMap)
Chris Lattnereff0da92002-10-21 15:32:34 +0000497 : Func(G.Func) {
Chris Lattnerc875f022002-11-03 21:27:48 +0000498 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattnereff0da92002-10-21 15:32:34 +0000499}
500
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000501DSGraph::~DSGraph() {
502 FunctionCalls.clear();
Chris Lattner679e8e12002-11-08 21:27:12 +0000503 AuxFunctionCalls.clear();
Chris Lattnerc875f022002-11-03 21:27:48 +0000504 ScalarMap.clear();
Chris Lattner13ec72a2002-10-21 13:31:48 +0000505 RetNode.setNode(0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000506
507#ifndef NDEBUG
508 // Drop all intra-node references, so that assertions don't fail...
509 std::for_each(Nodes.begin(), Nodes.end(),
510 std::mem_fun(&DSNode::dropAllReferences));
511#endif
512
513 // Delete all of the nodes themselves...
514 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
515}
516
Chris Lattner0d9bab82002-07-18 00:12:30 +0000517// dump - Allow inspection of graph in a debugger.
518void DSGraph::dump() const { print(std::cerr); }
519
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000520
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000521/// remapLinks - Change all of the Links in the current node according to the
522/// specified mapping.
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000523///
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000524void DSNode::remapLinks(std::map<const DSNode*, DSNodeHandle> &OldNodeMap) {
525 for (unsigned i = 0, e = Links.size(); i != e; ++i) {
526 DSNodeHandle &H = OldNodeMap[Links[i].getNode()];
527 Links[i].setNode(H.getNode());
528 Links[i].setOffset(Links[i].getOffset()+H.getOffset());
529 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000530}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000531
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000532
Chris Lattner0d9bab82002-07-18 00:12:30 +0000533// cloneInto - Clone the specified DSGraph into the current graph, returning the
Chris Lattnerc875f022002-11-03 21:27:48 +0000534// Return node of the graph. The translated ScalarMap for the old function is
Chris Lattner92673292002-11-02 00:13:20 +0000535// filled into the OldValMap member. If StripAllocas is set to true, Alloca
536// markers are removed from the graph, as the graph is being cloned into a
537// calling function's graph.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000538//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000539DSNodeHandle DSGraph::cloneInto(const DSGraph &G,
540 std::map<Value*, DSNodeHandle> &OldValMap,
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000541 std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
Chris Lattner679e8e12002-11-08 21:27:12 +0000542 unsigned CloneFlags) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000543 assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
Chris Lattner33312f72002-11-08 01:21:07 +0000544 assert(&G != this && "Cannot clone graph into itself!");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000545
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000546 unsigned FN = Nodes.size(); // First new node...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000547
548 // Duplicate all of the nodes, populating the node map...
549 Nodes.reserve(FN+G.Nodes.size());
550 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000551 DSNode *Old = G.Nodes[i];
552 DSNode *New = new DSNode(*Old);
Chris Lattner679e8e12002-11-08 21:27:12 +0000553 New->NodeType &= ~DSNode::DEAD; // Clear dead flag...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000554 Nodes.push_back(New);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000555 OldNodeMap[Old] = New;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000556 }
557
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000558 // Rewrite the links in the new nodes to point into the current graph now.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000559 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000560 Nodes[i]->remapLinks(OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000561
Chris Lattner460ea292002-11-07 07:06:20 +0000562 // Remove alloca markers as specified
Chris Lattner679e8e12002-11-08 21:27:12 +0000563 if (CloneFlags & StripAllocaBit)
Chris Lattner0d9bab82002-07-18 00:12:30 +0000564 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattner460ea292002-11-07 07:06:20 +0000565 Nodes[i]->NodeType &= ~DSNode::AllocaNode;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000566
Chris Lattnercf15db32002-10-17 20:09:52 +0000567 // Copy the value map... and merge all of the global nodes...
Chris Lattnerc875f022002-11-03 21:27:48 +0000568 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ScalarMap.begin(),
569 E = G.ScalarMap.end(); I != E; ++I) {
Chris Lattnercf15db32002-10-17 20:09:52 +0000570 DSNodeHandle &H = OldValMap[I->first];
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000571 DSNodeHandle &MappedNode = OldNodeMap[I->second.getNode()];
572 H.setNode(MappedNode.getNode());
573 H.setOffset(I->second.getOffset()+MappedNode.getOffset());
Chris Lattnercf15db32002-10-17 20:09:52 +0000574
575 if (isa<GlobalValue>(I->first)) { // Is this a global?
Chris Lattnerc875f022002-11-03 21:27:48 +0000576 std::map<Value*, DSNodeHandle>::iterator GVI = ScalarMap.find(I->first);
577 if (GVI != ScalarMap.end()) { // Is the global value in this fn already?
Chris Lattnercf15db32002-10-17 20:09:52 +0000578 GVI->second.mergeWith(H);
579 } else {
Chris Lattnerc875f022002-11-03 21:27:48 +0000580 ScalarMap[I->first] = H; // Add global pointer to this graph
Chris Lattnercf15db32002-10-17 20:09:52 +0000581 }
582 }
583 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000584
Chris Lattner679e8e12002-11-08 21:27:12 +0000585 if (!(CloneFlags & DontCloneCallNodes)) {
586 // Copy the function calls list...
587 unsigned FC = FunctionCalls.size(); // FirstCall
588 FunctionCalls.reserve(FC+G.FunctionCalls.size());
589 for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i)
590 FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap));
Chris Lattneracf491f2002-11-08 22:27:09 +0000591 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000592
Chris Lattneracf491f2002-11-08 22:27:09 +0000593 if (!(CloneFlags & DontCloneAuxCallNodes)) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000594 // Copy the auxillary function calls list...
Chris Lattneracf491f2002-11-08 22:27:09 +0000595 unsigned FC = AuxFunctionCalls.size(); // FirstCall
Chris Lattner679e8e12002-11-08 21:27:12 +0000596 AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size());
597 for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i)
598 AuxFunctionCalls.push_back(DSCallSite(G.AuxFunctionCalls[i], OldNodeMap));
599 }
Chris Lattnercf15db32002-10-17 20:09:52 +0000600
Chris Lattner0d9bab82002-07-18 00:12:30 +0000601 // Return the returned node pointer...
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000602 DSNodeHandle &MappedRet = OldNodeMap[G.RetNode.getNode()];
603 return DSNodeHandle(MappedRet.getNode(),
604 MappedRet.getOffset()+G.RetNode.getOffset());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000605}
606
Chris Lattner076c1f92002-11-07 06:31:54 +0000607/// mergeInGraph - The method is used for merging graphs together. If the
608/// argument graph is not *this, it makes a clone of the specified graph, then
609/// merges the nodes specified in the call site with the formal arguments in the
610/// graph.
611///
612void DSGraph::mergeInGraph(DSCallSite &CS, const DSGraph &Graph,
Chris Lattner679e8e12002-11-08 21:27:12 +0000613 unsigned CloneFlags) {
Chris Lattner076c1f92002-11-07 06:31:54 +0000614 std::map<Value*, DSNodeHandle> OldValMap;
615 DSNodeHandle RetVal;
616 std::map<Value*, DSNodeHandle> *ScalarMap = &OldValMap;
617
618 // If this is not a recursive call, clone the graph into this graph...
619 if (&Graph != this) {
620 // Clone the callee's graph into the current graph, keeping
621 // track of where scalars in the old graph _used_ to point,
622 // and of the new nodes matching nodes of the old graph.
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000623 std::map<const DSNode*, DSNodeHandle> OldNodeMap;
Chris Lattner076c1f92002-11-07 06:31:54 +0000624
625 // The clone call may invalidate any of the vectors in the data
626 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner679e8e12002-11-08 21:27:12 +0000627 RetVal = cloneInto(Graph, OldValMap, OldNodeMap, CloneFlags);
Chris Lattner076c1f92002-11-07 06:31:54 +0000628 ScalarMap = &OldValMap;
629 } else {
630 RetVal = getRetNode();
631 ScalarMap = &getScalarMap();
632 }
633
634 // Merge the return value with the return value of the context...
635 RetVal.mergeWith(CS.getRetVal());
636
637 // Resolve all of the function arguments...
638 Function &F = Graph.getFunction();
639 Function::aiterator AI = F.abegin();
640 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) {
641 // Advance the argument iterator to the first pointer argument...
642 while (!isPointerType(AI->getType())) {
643 ++AI;
644#ifndef NDEBUG
645 if (AI == F.aend())
646 std::cerr << "Bad call to Function: " << F.getName() << "\n";
647#endif
648 assert(AI != F.aend() && "# Args provided is not # Args required!");
649 }
650
651 // Add the link from the argument scalar to the provided value
652 DSNodeHandle &NH = (*ScalarMap)[AI];
653 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
654 NH.mergeWith(CS.getPtrArg(i));
655 }
656}
657
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000658#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000659// cloneGlobalInto - Clone the given global node and all its target links
660// (and all their llinks, recursively).
661//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000662DSNode *DSGraph::cloneGlobalInto(const DSNode *GNode) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000663 if (GNode == 0 || GNode->getGlobals().size() == 0) return 0;
664
665 // If a clone has already been created for GNode, return it.
Chris Lattnerc875f022002-11-03 21:27:48 +0000666 DSNodeHandle& ValMapEntry = ScalarMap[GNode->getGlobals()[0]];
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000667 if (ValMapEntry != 0)
668 return ValMapEntry;
669
670 // Clone the node and update the ValMap.
671 DSNode* NewNode = new DSNode(*GNode);
672 ValMapEntry = NewNode; // j=0 case of loop below!
673 Nodes.push_back(NewNode);
674 for (unsigned j = 1, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +0000675 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000676
677 // Rewrite the links in the new node to point into the current graph.
678 for (unsigned j = 0, e = GNode->getNumLinks(); j != e; ++j)
679 NewNode->setLink(j, cloneGlobalInto(GNode->getLink(j)));
680
681 return NewNode;
682}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000683#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000684
685
Chris Lattner0d9bab82002-07-18 00:12:30 +0000686// markIncompleteNodes - Mark the specified node as having contents that are not
687// known with the current analysis we have performed. Because a node makes all
688// of the nodes it can reach imcomplete if the node itself is incomplete, we
689// must recursively traverse the data structure graph, marking all reachable
690// nodes as incomplete.
691//
692static void markIncompleteNode(DSNode *N) {
693 // Stop recursion if no node, or if node already marked...
694 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
695
696 // Actually mark the node
697 N->NodeType |= DSNode::Incomplete;
698
699 // Recusively process children...
Chris Lattner08db7192002-11-06 06:20:27 +0000700 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
701 if (DSNode *DSN = N->getLink(i).getNode())
702 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000703}
704
705
706// markIncompleteNodes - Traverse the graph, identifying nodes that may be
707// modified by other functions that have not been resolved yet. This marks
708// nodes that are reachable through three sources of "unknownness":
709//
710// Global Variables, Function Calls, and Incoming Arguments
711//
712// For any node that may have unknown components (because something outside the
713// scope of current analysis may have modified it), the 'Incomplete' flag is
714// added to the NodeType.
715//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000716void DSGraph::markIncompleteNodes(bool markFormalArgs) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000717 // Mark any incoming arguments as incomplete...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000718 if (markFormalArgs && Func)
719 for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I)
Chris Lattnerc875f022002-11-03 21:27:48 +0000720 if (isPointerType(I->getType()) && ScalarMap.find(I) != ScalarMap.end())
721 markIncompleteNode(ScalarMap[I].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000722
723 // Mark stuff passed into functions calls as being incomplete...
724 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Vikram S. Adve26b98262002-10-20 21:41:02 +0000725 DSCallSite &Call = FunctionCalls[i];
Chris Lattnere2219762002-07-18 18:22:40 +0000726 // Then the return value is certainly incomplete!
Chris Lattner0969c502002-10-21 02:08:03 +0000727 markIncompleteNode(Call.getRetVal().getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000728
Chris Lattner92673292002-11-02 00:13:20 +0000729 // All objects pointed to by function arguments are incomplete though!
Vikram S. Adve26b98262002-10-20 21:41:02 +0000730 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i)
Chris Lattner0969c502002-10-21 02:08:03 +0000731 markIncompleteNode(Call.getPtrArg(i).getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000732 }
733
Chris Lattner92673292002-11-02 00:13:20 +0000734 // Mark all of the nodes pointed to by global nodes as incomplete...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000735 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000736 if (Nodes[i]->NodeType & DSNode::GlobalNode) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000737 DSNode *N = Nodes[i];
Chris Lattner92673292002-11-02 00:13:20 +0000738 // FIXME: Make more efficient by looking over Links directly
Chris Lattner08db7192002-11-06 06:20:27 +0000739 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
740 if (DSNode *DSN = N->getLink(i).getNode())
741 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000742 }
743}
744
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000745// removeRefsToGlobal - Helper function that removes globals from the
Chris Lattnerc875f022002-11-03 21:27:48 +0000746// ScalarMap so that the referrer count will go down to zero.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000747static void removeRefsToGlobal(DSNode* N,
Chris Lattnerc875f022002-11-03 21:27:48 +0000748 std::map<Value*, DSNodeHandle> &ScalarMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000749 while (!N->getGlobals().empty()) {
750 GlobalValue *GV = N->getGlobals().back();
751 N->getGlobals().pop_back();
Chris Lattnerc875f022002-11-03 21:27:48 +0000752 ScalarMap.erase(GV);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000753 }
754}
755
756
Chris Lattner0d9bab82002-07-18 00:12:30 +0000757// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
758// checks to see if there are simple transformations that it can do to make it
759// dead.
760//
761bool DSGraph::isNodeDead(DSNode *N) {
762 // Is it a trivially dead shadow node...
Chris Lattner33312f72002-11-08 01:21:07 +0000763 if (N->getReferrers().empty() &&
764 (N->NodeType == 0 || N->NodeType == DSNode::DEAD))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000765 return true;
766
767 // Is it a function node or some other trivially unused global?
Chris Lattner92673292002-11-02 00:13:20 +0000768 if ((N->NodeType & ~DSNode::GlobalNode) == 0 && N->getSize() == 0 &&
Chris Lattner0d9bab82002-07-18 00:12:30 +0000769 N->getReferrers().size() == N->getGlobals().size()) {
770
Chris Lattnerc875f022002-11-03 21:27:48 +0000771 // Remove the globals from the ScalarMap, so that the referrer count will go
Chris Lattner0d9bab82002-07-18 00:12:30 +0000772 // down to zero.
Chris Lattnerc875f022002-11-03 21:27:48 +0000773 removeRefsToGlobal(N, ScalarMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000774 assert(N->getReferrers().empty() && "Referrers should all be gone now!");
775 return true;
776 }
777
778 return false;
779}
780
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000781static void removeIdenticalCalls(vector<DSCallSite> &Calls,
Chris Lattner7541b892002-07-31 19:32:12 +0000782 const std::string &where) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000783 // Remove trivially identical function calls
784 unsigned NumFns = Calls.size();
785 std::sort(Calls.begin(), Calls.end());
786 Calls.erase(std::unique(Calls.begin(), Calls.end()),
787 Calls.end());
788
Chris Lattner33312f72002-11-08 01:21:07 +0000789 // Track the number of call nodes merged away...
790 NumCallNodesMerged += NumFns-Calls.size();
791
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000792 DEBUG(if (NumFns != Calls.size())
Chris Lattner7541b892002-07-31 19:32:12 +0000793 std::cerr << "Merged " << (NumFns-Calls.size())
794 << " call nodes in " << where << "\n";);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000795}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000796
Chris Lattnere2219762002-07-18 18:22:40 +0000797// removeTriviallyDeadNodes - After the graph has been constructed, this method
798// removes all unreachable nodes that are created because they got merged with
799// other nodes in the graph. These nodes will all be trivially unreachable, so
800// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000801//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000802void DSGraph::removeTriviallyDeadNodes(bool KeepAllGlobals) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000803 for (unsigned i = 0; i != Nodes.size(); ++i)
Chris Lattnera00397e2002-10-03 21:55:28 +0000804 if (!KeepAllGlobals || !(Nodes[i]->NodeType & DSNode::GlobalNode))
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000805 if (isNodeDead(Nodes[i])) { // This node is dead!
806 delete Nodes[i]; // Free memory...
807 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
808 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000809
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000810 removeIdenticalCalls(FunctionCalls, Func ? Func->getName() : "");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000811}
812
813
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000814// markAlive - Simple graph walker that recursively traverses the graph, marking
Chris Lattnere2219762002-07-18 18:22:40 +0000815// stuff to be alive.
816//
817static void markAlive(DSNode *N, std::set<DSNode*> &Alive) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000818 if (N == 0) return;
Chris Lattnere2219762002-07-18 18:22:40 +0000819
820 Alive.insert(N);
Chris Lattner08db7192002-11-06 06:20:27 +0000821 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
822 if (DSNode *DSN = N->getLink(i).getNode())
823 if (!Alive.count(DSN))
824 markAlive(DSN, Alive);
Chris Lattnere2219762002-07-18 18:22:40 +0000825}
826
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000827static bool checkGlobalAlive(DSNode *N, std::set<DSNode*> &Alive,
828 std::set<DSNode*> &Visiting) {
829 if (N == 0) return false;
830
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000831 if (Visiting.count(N)) return false; // terminate recursion on a cycle
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000832 Visiting.insert(N);
833
834 // If any immediate successor is alive, N is alive
Chris Lattner08db7192002-11-06 06:20:27 +0000835 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
836 if (DSNode *DSN = N->getLink(i).getNode())
837 if (Alive.count(DSN)) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000838 Visiting.erase(N);
839 return true;
840 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000841
842 // Else if any successor reaches a live node, N is alive
Chris Lattner08db7192002-11-06 06:20:27 +0000843 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
844 if (DSNode *DSN = N->getLink(i).getNode())
845 if (checkGlobalAlive(DSN, Alive, Visiting)) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000846 Visiting.erase(N); return true;
847 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000848
849 Visiting.erase(N);
850 return false;
851}
852
853
854// markGlobalsIteration - Recursive helper function for markGlobalsAlive().
855// This would be unnecessary if function calls were real nodes! In that case,
856// the simple iterative loop in the first few lines below suffice.
857//
858static void markGlobalsIteration(std::set<DSNode*>& GlobalNodes,
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000859 vector<DSCallSite> &Calls,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000860 std::set<DSNode*> &Alive,
861 bool FilterCalls) {
862
863 // Iterate, marking globals or cast nodes alive until no new live nodes
864 // are added to Alive
865 std::set<DSNode*> Visiting; // Used to identify cycles
Chris Lattner0969c502002-10-21 02:08:03 +0000866 std::set<DSNode*>::iterator I = GlobalNodes.begin(), E = GlobalNodes.end();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000867 for (size_t liveCount = 0; liveCount < Alive.size(); ) {
868 liveCount = Alive.size();
869 for ( ; I != E; ++I)
870 if (Alive.count(*I) == 0) {
871 Visiting.clear();
872 if (checkGlobalAlive(*I, Alive, Visiting))
873 markAlive(*I, Alive);
874 }
875 }
876
877 // Find function calls with some dead and some live nodes.
878 // Since all call nodes must be live if any one is live, we have to mark
879 // all nodes of the call as live and continue the iteration (via recursion).
880 if (FilterCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000881 bool Recurse = false;
882 for (unsigned i = 0, ei = Calls.size(); i < ei; ++i) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000883 bool CallIsDead = true, CallHasDeadArg = false;
Chris Lattner0969c502002-10-21 02:08:03 +0000884 DSCallSite &CS = Calls[i];
885 for (unsigned j = 0, ej = CS.getNumPtrArgs(); j != ej; ++j)
886 if (DSNode *N = CS.getPtrArg(j).getNode()) {
887 bool ArgIsDead = !Alive.count(N);
888 CallHasDeadArg |= ArgIsDead;
889 CallIsDead &= ArgIsDead;
890 }
891
892 if (DSNode *N = CS.getRetVal().getNode()) {
893 bool RetIsDead = !Alive.count(N);
894 CallHasDeadArg |= RetIsDead;
895 CallIsDead &= RetIsDead;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000896 }
Chris Lattner0969c502002-10-21 02:08:03 +0000897
898 DSNode *N = CS.getCallee().getNode();
899 bool FnIsDead = !Alive.count(N);
900 CallHasDeadArg |= FnIsDead;
901 CallIsDead &= FnIsDead;
902
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000903 if (!CallIsDead && CallHasDeadArg) {
904 // Some node in this call is live and another is dead.
905 // Mark all nodes of call as live and iterate once more.
Chris Lattner0969c502002-10-21 02:08:03 +0000906 Recurse = true;
907 for (unsigned j = 0, ej = CS.getNumPtrArgs(); j != ej; ++j)
908 markAlive(CS.getPtrArg(j).getNode(), Alive);
909 markAlive(CS.getRetVal().getNode(), Alive);
910 markAlive(CS.getCallee().getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000911 }
912 }
Chris Lattner0969c502002-10-21 02:08:03 +0000913 if (Recurse)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000914 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
915 }
916}
917
918
919// markGlobalsAlive - Mark global nodes and cast nodes alive if they
920// can reach any other live node. Since this can produce new live nodes,
921// we use a simple iterative algorithm.
922//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000923static void markGlobalsAlive(DSGraph &G, std::set<DSNode*> &Alive,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000924 bool FilterCalls) {
925 // Add global and cast nodes to a set so we don't walk all nodes every time
926 std::set<DSNode*> GlobalNodes;
927 for (unsigned i = 0, e = G.getNodes().size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000928 if (G.getNodes()[i]->NodeType & DSNode::GlobalNode)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000929 GlobalNodes.insert(G.getNodes()[i]);
930
931 // Add all call nodes to the same set
Chris Lattner679e8e12002-11-08 21:27:12 +0000932 vector<DSCallSite> &Calls = G.getAuxFunctionCalls();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000933 if (FilterCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000934 for (unsigned i = 0, e = Calls.size(); i != e; ++i) {
935 for (unsigned j = 0, e = Calls[i].getNumPtrArgs(); j != e; ++j)
936 if (DSNode *N = Calls[i].getPtrArg(j).getNode())
937 GlobalNodes.insert(N);
938 if (DSNode *N = Calls[i].getRetVal().getNode())
939 GlobalNodes.insert(N);
940 if (DSNode *N = Calls[i].getCallee().getNode())
941 GlobalNodes.insert(N);
942 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000943 }
944
945 // Iterate and recurse until no new live node are discovered.
946 // This would be a simple iterative loop if function calls were real nodes!
947 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
948
Chris Lattnerc875f022002-11-03 21:27:48 +0000949 // Free up references to dead globals from the ScalarMap
Chris Lattner92673292002-11-02 00:13:20 +0000950 std::set<DSNode*>::iterator I = GlobalNodes.begin(), E = GlobalNodes.end();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000951 for( ; I != E; ++I)
952 if (Alive.count(*I) == 0)
Chris Lattnerc875f022002-11-03 21:27:48 +0000953 removeRefsToGlobal(*I, G.getScalarMap());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000954
955 // Delete dead function calls
956 if (FilterCalls)
957 for (int ei = Calls.size(), i = ei-1; i >= 0; --i) {
958 bool CallIsDead = true;
Chris Lattner0969c502002-10-21 02:08:03 +0000959 for (unsigned j = 0, ej = Calls[i].getNumPtrArgs();
960 CallIsDead && j != ej; ++j)
961 CallIsDead = Alive.count(Calls[i].getPtrArg(j).getNode()) == 0;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000962 if (CallIsDead)
963 Calls.erase(Calls.begin() + i); // remove the call entirely
964 }
965}
Chris Lattnere2219762002-07-18 18:22:40 +0000966
967// removeDeadNodes - Use a more powerful reachability analysis to eliminate
968// subgraphs that are unreachable. This often occurs because the data
969// structure doesn't "escape" into it's caller, and thus should be eliminated
970// from the caller's graph entirely. This is only appropriate to use when
971// inlining graphs.
972//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000973void DSGraph::removeDeadNodes(bool KeepAllGlobals, bool KeepCalls) {
Chris Lattner33312f72002-11-08 01:21:07 +0000974 assert((!KeepAllGlobals || KeepCalls) && // FIXME: This should be an enum!
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000975 "KeepAllGlobals without KeepCalls is meaningless");
976
Chris Lattnere2219762002-07-18 18:22:40 +0000977 // Reduce the amount of work we have to do...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000978 removeTriviallyDeadNodes(KeepAllGlobals);
979
Chris Lattnere2219762002-07-18 18:22:40 +0000980 // FIXME: Merge nontrivially identical call nodes...
981
982 // Alive - a set that holds all nodes found to be reachable/alive.
983 std::set<DSNode*> Alive;
984
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000985 // If KeepCalls, mark all nodes reachable by call nodes as alive...
Chris Lattneracf491f2002-11-08 22:27:09 +0000986 if (KeepCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000987 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
988 for (unsigned j = 0, e = FunctionCalls[i].getNumPtrArgs(); j != e; ++j)
989 markAlive(FunctionCalls[i].getPtrArg(j).getNode(), Alive);
990 markAlive(FunctionCalls[i].getRetVal().getNode(), Alive);
991 markAlive(FunctionCalls[i].getCallee().getNode(), Alive);
992 }
Chris Lattneracf491f2002-11-08 22:27:09 +0000993 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i) {
994 for (unsigned j = 0, e = AuxFunctionCalls[i].getNumPtrArgs(); j != e; ++j)
995 markAlive(AuxFunctionCalls[i].getPtrArg(j).getNode(), Alive);
996 markAlive(AuxFunctionCalls[i].getRetVal().getNode(), Alive);
997 markAlive(AuxFunctionCalls[i].getCallee().getNode(), Alive);
998 }
999 }
Chris Lattnere2219762002-07-18 18:22:40 +00001000
Chris Lattner92673292002-11-02 00:13:20 +00001001 // Mark all nodes reachable by scalar nodes as alive...
Chris Lattnerc875f022002-11-03 21:27:48 +00001002 for (std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.begin(),
1003 E = ScalarMap.end(); I != E; ++I)
Chris Lattner92673292002-11-02 00:13:20 +00001004 markAlive(I->second.getNode(), Alive);
Chris Lattnere2219762002-07-18 18:22:40 +00001005
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001006 // The return value is alive as well...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001007 markAlive(RetNode.getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001008
1009 // Mark all globals or cast nodes that can reach a live node as alive.
1010 // This also marks all nodes reachable from such nodes as alive.
1011 // Of course, if KeepAllGlobals is specified, they would be live already.
Chris Lattner33312f72002-11-08 01:21:07 +00001012
Chris Lattner0969c502002-10-21 02:08:03 +00001013 if (!KeepAllGlobals)
Chris Lattner92673292002-11-02 00:13:20 +00001014 markGlobalsAlive(*this, Alive, !KeepCalls);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001015
Chris Lattnere2219762002-07-18 18:22:40 +00001016 // Loop over all unreachable nodes, dropping their references...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001017 vector<DSNode*> DeadNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001018 DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed.
1019 for (unsigned i = 0; i != Nodes.size(); ++i)
1020 if (!Alive.count(Nodes[i])) {
1021 DSNode *N = Nodes[i];
1022 Nodes.erase(Nodes.begin()+i--); // Erase node from alive list.
1023 DeadNodes.push_back(N); // Add node to our list of dead nodes
1024 N->dropAllReferences(); // Drop all outgoing edges
1025 }
1026
Chris Lattnere2219762002-07-18 18:22:40 +00001027 // Delete all dead nodes...
1028 std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
1029}
1030
1031
1032
Chris Lattner0d9bab82002-07-18 00:12:30 +00001033// maskNodeTypes - Apply a mask to all of the node types in the graph. This
1034// is useful for clearing out markers like Scalar or Incomplete.
1035//
1036void DSGraph::maskNodeTypes(unsigned char Mask) {
1037 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
1038 Nodes[i]->NodeType &= Mask;
1039}
1040
1041
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001042#if 0
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001043//===----------------------------------------------------------------------===//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001044// GlobalDSGraph Implementation
1045//===----------------------------------------------------------------------===//
1046
1047GlobalDSGraph::GlobalDSGraph() : DSGraph(*(Function*)0, this) {
1048}
1049
1050GlobalDSGraph::~GlobalDSGraph() {
1051 assert(Referrers.size() == 0 &&
1052 "Deleting global graph while references from other graphs exist");
1053}
1054
1055void GlobalDSGraph::addReference(const DSGraph* referrer) {
1056 if (referrer != this)
1057 Referrers.insert(referrer);
1058}
1059
1060void GlobalDSGraph::removeReference(const DSGraph* referrer) {
1061 if (referrer != this) {
1062 assert(Referrers.find(referrer) != Referrers.end() && "This is very bad!");
1063 Referrers.erase(referrer);
1064 if (Referrers.size() == 0)
1065 delete this;
1066 }
1067}
1068
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001069#if 0
Chris Lattnerd18f3422002-11-03 21:24:04 +00001070// Bits used in the next function
1071static const char ExternalTypeBits = DSNode::GlobalNode | DSNode::HeapNode;
1072
1073
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001074// GlobalDSGraph::cloneNodeInto - Clone a global node and all its externally
1075// visible target links (and recursively their such links) into this graph.
1076// NodeCache maps the node being cloned to its clone in the Globals graph,
1077// in order to track cycles.
1078// GlobalsAreFinal is a flag that says whether it is safe to assume that
1079// an existing global node is complete. This is important to avoid
1080// reinserting all globals when inserting Calls to functions.
1081// This is a helper function for cloneGlobals and cloneCalls.
1082//
1083DSNode* GlobalDSGraph::cloneNodeInto(DSNode *OldNode,
1084 std::map<const DSNode*, DSNode*> &NodeCache,
1085 bool GlobalsAreFinal) {
1086 if (OldNode == 0) return 0;
1087
1088 // The caller should check this is an external node. Just more efficient...
1089 assert((OldNode->NodeType & ExternalTypeBits) && "Non-external node");
1090
1091 // If a clone has already been created for OldNode, return it.
1092 DSNode*& CacheEntry = NodeCache[OldNode];
1093 if (CacheEntry != 0)
1094 return CacheEntry;
1095
1096 // The result value...
1097 DSNode* NewNode = 0;
1098
1099 // If nodes already exist for any of the globals of OldNode,
1100 // merge all such nodes together since they are merged in OldNode.
1101 // If ValueCacheIsFinal==true, look for an existing node that has
1102 // an identical list of globals and return it if it exists.
1103 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001104 for (unsigned j = 0, N = OldNode->getGlobals().size(); j != N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001105 if (DSNode *PrevNode = ScalarMap[OldNode->getGlobals()[j]].getNode()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001106 if (NewNode == 0) {
1107 NewNode = PrevNode; // first existing node found
1108 if (GlobalsAreFinal && j == 0)
1109 if (OldNode->getGlobals() == PrevNode->getGlobals()) {
1110 CacheEntry = NewNode;
1111 return NewNode;
1112 }
1113 }
1114 else if (NewNode != PrevNode) { // found another, different from prev
1115 // update ValMap *before* merging PrevNode into NewNode
1116 for (unsigned k = 0, NK = PrevNode->getGlobals().size(); k < NK; ++k)
Chris Lattnerc875f022002-11-03 21:27:48 +00001117 ScalarMap[PrevNode->getGlobals()[k]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001118 NewNode->mergeWith(PrevNode);
1119 }
1120 } else if (NewNode != 0) {
Chris Lattnerc875f022002-11-03 21:27:48 +00001121 ScalarMap[OldNode->getGlobals()[j]] = NewNode; // add the merged node
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001122 }
1123
1124 // If no existing node was found, clone the node and update the ValMap.
1125 if (NewNode == 0) {
1126 NewNode = new DSNode(*OldNode);
1127 Nodes.push_back(NewNode);
1128 for (unsigned j = 0, e = NewNode->getNumLinks(); j != e; ++j)
1129 NewNode->setLink(j, 0);
1130 for (unsigned j = 0, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001131 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001132 }
1133 else
1134 NewNode->NodeType |= OldNode->NodeType; // Markers may be different!
1135
1136 // Add the entry to NodeCache
1137 CacheEntry = NewNode;
1138
1139 // Rewrite the links in the new node to point into the current graph,
1140 // but only for links to external nodes. Set other links to NULL.
1141 for (unsigned j = 0, e = OldNode->getNumLinks(); j != e; ++j) {
1142 DSNode* OldTarget = OldNode->getLink(j);
1143 if (OldTarget && (OldTarget->NodeType & ExternalTypeBits)) {
1144 DSNode* NewLink = this->cloneNodeInto(OldTarget, NodeCache);
1145 if (NewNode->getLink(j))
1146 NewNode->getLink(j)->mergeWith(NewLink);
1147 else
1148 NewNode->setLink(j, NewLink);
1149 }
1150 }
1151
1152 // Remove all local markers
1153 NewNode->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode);
1154
1155 return NewNode;
1156}
1157
1158
1159// GlobalDSGraph::cloneGlobals - Clone global nodes and all their externally
1160// visible target links (and recursively their such links) into this graph.
1161//
1162void GlobalDSGraph::cloneGlobals(DSGraph& Graph, bool CloneCalls) {
1163 std::map<const DSNode*, DSNode*> NodeCache;
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001164#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001165 for (unsigned i = 0, N = Graph.Nodes.size(); i < N; ++i)
1166 if (Graph.Nodes[i]->NodeType & DSNode::GlobalNode)
1167 GlobalsGraph->cloneNodeInto(Graph.Nodes[i], NodeCache, false);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001168 if (CloneCalls)
1169 GlobalsGraph->cloneCalls(Graph);
1170
1171 GlobalsGraph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001172#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001173}
1174
1175
1176// GlobalDSGraph::cloneCalls - Clone function calls and their visible target
1177// links (and recursively their such links) into this graph.
1178//
1179void GlobalDSGraph::cloneCalls(DSGraph& Graph) {
1180 std::map<const DSNode*, DSNode*> NodeCache;
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001181 vector<DSCallSite >& FromCalls =Graph.FunctionCalls;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001182
1183 FunctionCalls.reserve(FunctionCalls.size() + FromCalls.size());
1184
1185 for (int i = 0, ei = FromCalls.size(); i < ei; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001186 DSCallSite& callCopy = FunctionCalls.back();
1187 callCopy.reserve(FromCalls[i].size());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001188 for (unsigned j = 0, ej = FromCalls[i].size(); j != ej; ++j)
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001189 callCopy.push_back
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001190 ((FromCalls[i][j] && (FromCalls[i][j]->NodeType & ExternalTypeBits))
1191 ? cloneNodeInto(FromCalls[i][j], NodeCache, true)
1192 : 0);
1193 }
1194
1195 // remove trivially identical function calls
Chris Lattner7541b892002-07-31 19:32:12 +00001196 removeIdenticalCalls(FunctionCalls, "Globals Graph");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001197}
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001198#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001199
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001200#endif