blob: 8b43653b9190b6eeb8b9a2f2fa163733e6c7444d [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
Chris Lattner23f83dc2002-11-08 22:49:57 +0000134 // If this is an array type, eliminate the outside arrays because they won't
135 // be used anyway. This greatly reduces the size of large static arrays used
136 // as global variables, for example.
137 //
138 while (const ArrayType *AT = dyn_cast<ArrayType>(NewTy)) {
139 // FIXME: we might want to keep small arrays, but must be careful about
140 // things like: [2 x [10000 x int*]]
141 NewTy = AT->getElementType();
142 }
143
Chris Lattner08db7192002-11-06 06:20:27 +0000144 // Figure out how big the new type we're merging in is...
145 unsigned NewTySize = NewTy->isSized() ? TD.getTypeSize(NewTy) : 0;
146
147 // Otherwise check to see if we can fold this type into the current node. If
148 // we can't, we fold the node completely, if we can, we potentially update our
149 // internal state.
150 //
151 if (Ty.Ty == Type::VoidTy) {
152 // If this is the first type that this node has seen, just accept it without
153 // question....
154 assert(Offset == 0 && "Cannot have an offset into a void node!");
155 assert(Ty.isArray == false && "This shouldn't happen!");
156 Ty.Ty = NewTy;
157 Size = NewTySize;
158
159 // Calculate the number of outgoing links from this node.
160 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
161 return false;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000162 }
Chris Lattner08db7192002-11-06 06:20:27 +0000163
164 // Handle node expansion case here...
165 if (Offset+NewTySize > Size) {
166 // It is illegal to grow this node if we have treated it as an array of
167 // objects...
168 if (Ty.isArray) {
169 foldNodeCompletely();
170 return true;
171 }
172
173 if (Offset) { // We could handle this case, but we don't for now...
Chris Lattner3c87b292002-11-07 01:54:56 +0000174 DEBUG(std::cerr << "UNIMP: Trying to merge a growth type into "
175 << "offset != 0: Collapsing!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000176 foldNodeCompletely();
177 return true;
178 }
179
180 // Okay, the situation is nice and simple, we are trying to merge a type in
181 // at offset 0 that is bigger than our current type. Implement this by
182 // switching to the new type and then merge in the smaller one, which should
183 // hit the other code path here. If the other code path decides it's not
184 // ok, it will collapse the node as appropriate.
185 //
186 const Type *OldTy = Ty.Ty;
187 Ty.Ty = NewTy;
188 Size = NewTySize;
189
190 // Must grow links to be the appropriate size...
191 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
192
193 // Merge in the old type now... which is guaranteed to be smaller than the
194 // "current" type.
195 return mergeTypeInfo(OldTy, 0);
196 }
197
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000198 assert(Offset <= Size &&
Chris Lattner08db7192002-11-06 06:20:27 +0000199 "Cannot merge something into a part of our type that doesn't exist!");
200
201 // Find the section of Ty.Ty that NewTy overlaps with... first we find the
202 // type that starts at offset Offset.
203 //
204 unsigned O = 0;
205 const Type *SubType = Ty.Ty;
206 while (O < Offset) {
207 assert(Offset-O < TD.getTypeSize(SubType) && "Offset out of range!");
208
209 switch (SubType->getPrimitiveID()) {
210 case Type::StructTyID: {
211 const StructType *STy = cast<StructType>(SubType);
212 const StructLayout &SL = *TD.getStructLayout(STy);
213
214 unsigned i = 0, e = SL.MemberOffsets.size();
215 for (; i+1 < e && SL.MemberOffsets[i+1] <= Offset-O; ++i)
216 /* empty */;
217
218 // The offset we are looking for must be in the i'th element...
219 SubType = STy->getElementTypes()[i];
220 O += SL.MemberOffsets[i];
221 break;
222 }
223 case Type::ArrayTyID: {
224 SubType = cast<ArrayType>(SubType)->getElementType();
225 unsigned ElSize = TD.getTypeSize(SubType);
226 unsigned Remainder = (Offset-O) % ElSize;
227 O = Offset-Remainder;
228 break;
229 }
230 default:
231 assert(0 && "Unknown type!");
232 }
233 }
234
235 assert(O == Offset && "Could not achieve the correct offset!");
236
237 // If we found our type exactly, early exit
238 if (SubType == NewTy) return false;
239
240 // Okay, so we found the leader type at the offset requested. Search the list
241 // of types that starts at this offset. If SubType is currently an array or
242 // structure, the type desired may actually be the first element of the
243 // composite type...
244 //
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000245 unsigned SubTypeSize = SubType->isSized() ? TD.getTypeSize(SubType) : 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000246 while (SubType != NewTy) {
247 const Type *NextSubType = 0;
Chris Lattnerbf10f052002-11-09 00:49:05 +0000248 unsigned NextSubTypeSize = 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000249 switch (SubType->getPrimitiveID()) {
250 case Type::StructTyID:
251 NextSubType = cast<StructType>(SubType)->getElementTypes()[0];
252 NextSubTypeSize = TD.getTypeSize(SubType);
253 break;
254 case Type::ArrayTyID:
255 NextSubType = cast<ArrayType>(SubType)->getElementType();
256 NextSubTypeSize = TD.getTypeSize(SubType);
257 break;
258 default: ;
259 // fall out
260 }
261
262 if (NextSubType == 0)
263 break; // In the default case, break out of the loop
264
265 if (NextSubTypeSize < NewTySize)
266 break; // Don't allow shrinking to a smaller type than NewTySize
267 SubType = NextSubType;
268 SubTypeSize = NextSubTypeSize;
269 }
270
271 // If we found the type exactly, return it...
272 if (SubType == NewTy)
273 return false;
274
275 // Check to see if we have a compatible, but different type...
276 if (NewTySize == SubTypeSize) {
277 // Check to see if this type is obviously convertable... int -> uint f.e.
278 if (NewTy->isLosslesslyConvertableTo(SubType))
279 return false;
280
281 // Check to see if we have a pointer & integer mismatch going on here,
282 // loading a pointer as a long, for example.
283 //
284 if (SubType->isInteger() && isa<PointerType>(NewTy) ||
285 NewTy->isInteger() && isa<PointerType>(SubType))
286 return false;
287
288 }
289
290
Chris Lattner3c87b292002-11-07 01:54:56 +0000291 DEBUG(std::cerr << "MergeTypeInfo Folding OrigTy: " << Ty.Ty
292 << "\n due to:" << NewTy << " @ " << Offset << "!\n"
293 << "SubType: " << SubType << "\n\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000294
295 foldNodeCompletely();
296 return true;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000297}
298
Chris Lattner08db7192002-11-06 06:20:27 +0000299
300
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000301// addEdgeTo - Add an edge from the current node to the specified node. This
302// can cause merging of nodes in the graph.
303//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000304void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000305 if (NH.getNode() == 0) return; // Nothing to do
306
Chris Lattner08db7192002-11-06 06:20:27 +0000307 DSNodeHandle &ExistingEdge = getLink(Offset);
308 if (ExistingEdge.getNode()) {
Chris Lattner7b7200c2002-10-02 04:57:39 +0000309 // Merge the two nodes...
Chris Lattner08db7192002-11-06 06:20:27 +0000310 ExistingEdge.mergeWith(NH);
Chris Lattner7b7200c2002-10-02 04:57:39 +0000311 } else { // No merging to perform...
312 setLink(Offset, NH); // Just force a link in there...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000313 }
Chris Lattner7b7200c2002-10-02 04:57:39 +0000314}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000315
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000316
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000317// MergeSortedVectors - Efficiently merge a vector into another vector where
318// duplicates are not allowed and both are sorted. This assumes that 'T's are
319// efficiently copyable and have sane comparison semantics.
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000320//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000321template<typename T>
322void MergeSortedVectors(vector<T> &Dest, const vector<T> &Src) {
323 // By far, the most common cases will be the simple ones. In these cases,
324 // avoid having to allocate a temporary vector...
325 //
326 if (Src.empty()) { // Nothing to merge in...
327 return;
328 } else if (Dest.empty()) { // Just copy the result in...
329 Dest = Src;
330 } else if (Src.size() == 1) { // Insert a single element...
331 const T &V = Src[0];
332 typename vector<T>::iterator I =
333 std::lower_bound(Dest.begin(), Dest.end(), V);
334 if (I == Dest.end() || *I != Src[0]) // If not already contained...
335 Dest.insert(I, Src[0]);
336 } else if (Dest.size() == 1) {
337 T Tmp = Dest[0]; // Save value in temporary...
338 Dest = Src; // Copy over list...
339 typename vector<T>::iterator I =
340 std::lower_bound(Dest.begin(), Dest.end(),Tmp);
341 if (I == Dest.end() || *I != Src[0]) // If not already contained...
342 Dest.insert(I, Src[0]);
343
344 } else {
345 // Make a copy to the side of Dest...
346 vector<T> Old(Dest);
347
348 // Make space for all of the type entries now...
349 Dest.resize(Dest.size()+Src.size());
350
351 // Merge the two sorted ranges together... into Dest.
352 std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin());
353
354 // Now erase any duplicate entries that may have accumulated into the
355 // vectors (because they were in both of the input sets)
356 Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end());
357 }
358}
359
360
361// mergeWith - Merge this node and the specified node, moving all links to and
362// from the argument node into the current node, deleting the node argument.
363// Offset indicates what offset the specified node is to be merged into the
364// current node.
365//
366// The specified node may be a null pointer (in which case, nothing happens).
367//
368void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
369 DSNode *N = NH.getNode();
370 if (N == 0 || (N == this && NH.getOffset() == Offset))
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000371 return; // Noop
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000372
Chris Lattner679e8e12002-11-08 21:27:12 +0000373 assert((N->NodeType & DSNode::DEAD) == 0);
374 assert((NodeType & DSNode::DEAD) == 0);
375 assert(!hasNoReferrers() && "Should not try to fold a useless node!");
376
Chris Lattner02606632002-11-04 06:48:26 +0000377 if (N == this) {
Chris Lattner08db7192002-11-06 06:20:27 +0000378 // We cannot merge two pieces of the same node together, collapse the node
379 // completely.
Chris Lattner3c87b292002-11-07 01:54:56 +0000380 DEBUG(std::cerr << "Attempting to merge two chunks of"
381 << " the same node together!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000382 foldNodeCompletely();
Chris Lattner02606632002-11-04 06:48:26 +0000383 return;
384 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000385
Chris Lattner08db7192002-11-06 06:20:27 +0000386 // Merge the type entries of the two nodes together...
Chris Lattner679e8e12002-11-08 21:27:12 +0000387 if (N->Ty.Ty != Type::VoidTy) {
Chris Lattner08db7192002-11-06 06:20:27 +0000388 mergeTypeInfo(N->Ty.Ty, Offset);
389
Chris Lattner679e8e12002-11-08 21:27:12 +0000390 // mergeTypeInfo can cause collapsing, which can cause this node to become
391 // dead.
392 if (hasNoReferrers()) return;
393 }
394 assert((NodeType & DSNode::DEAD) == 0);
395
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000396 // If we are merging a node with a completely folded node, then both nodes are
397 // now completely folded.
398 //
399 if (isNodeCompletelyFolded()) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000400 if (!N->isNodeCompletelyFolded()) {
Chris Lattner02606632002-11-04 06:48:26 +0000401 N->foldNodeCompletely();
Chris Lattner679e8e12002-11-08 21:27:12 +0000402 if (hasNoReferrers()) return;
403 }
Chris Lattner02606632002-11-04 06:48:26 +0000404 } else if (N->isNodeCompletelyFolded()) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000405 foldNodeCompletely();
406 Offset = 0;
Chris Lattner679e8e12002-11-08 21:27:12 +0000407 if (hasNoReferrers()) return;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000408 }
Chris Lattner02606632002-11-04 06:48:26 +0000409 N = NH.getNode();
Chris Lattner679e8e12002-11-08 21:27:12 +0000410 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner02606632002-11-04 06:48:26 +0000411
Chris Lattner2c0bd012002-11-06 18:01:39 +0000412 if (this == N || N == 0) return;
Chris Lattner679e8e12002-11-08 21:27:12 +0000413 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000414
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000415 // If both nodes are not at offset 0, make sure that we are merging the node
416 // at an later offset into the node with the zero offset.
417 //
418 if (Offset > NH.getOffset()) {
419 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
420 return;
Chris Lattner9b87c5c2002-10-31 22:41:15 +0000421 } else if (Offset == NH.getOffset() && getSize() < N->getSize()) {
422 // If the offsets are the same, merge the smaller node into the bigger node
423 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
424 return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000425 }
426
427#if 0
428 std::cerr << "\n\nMerging:\n";
429 N->print(std::cerr, 0);
430 std::cerr << " and:\n";
431 print(std::cerr, 0);
432#endif
433
434 // Now we know that Offset <= NH.Offset, so convert it so our "Offset" (with
435 // respect to NH.Offset) is now zero.
436 //
437 unsigned NOffset = NH.getOffset()-Offset;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000438 unsigned NSize = N->getSize();
Chris Lattner7b7200c2002-10-02 04:57:39 +0000439
Chris Lattner679e8e12002-11-08 21:27:12 +0000440 assert((NodeType & DSNode::DEAD) == 0);
441
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000442 // Remove all edges pointing at N, causing them to point to 'this' instead.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000443 // Make sure to adjust their offset, not just the node pointer.
444 //
445 while (!N->Referrers.empty()) {
446 DSNodeHandle &Ref = *N->Referrers.back();
447 Ref = DSNodeHandle(this, NOffset+Ref.getOffset());
448 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000449 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner59535132002-11-05 00:01:58 +0000450
451 // Make all of the outgoing links of N now be outgoing links of this. This
452 // can cause recursive merging!
453 //
Chris Lattner08db7192002-11-06 06:20:27 +0000454 for (unsigned i = 0; i < NSize; i += DS::PointerSize) {
455 DSNodeHandle &Link = N->getLink(i);
456 if (Link.getNode()) {
457 addEdgeTo((i+NOffset) % getSize(), Link);
Chris Lattner59535132002-11-05 00:01:58 +0000458
Chris Lattner08db7192002-11-06 06:20:27 +0000459 // It's possible that after adding the new edge that some recursive
460 // merging just occured, causing THIS node to get merged into oblivion.
461 // If that happens, we must not try to merge any more edges into it!
Chris Lattner7b7200c2002-10-02 04:57:39 +0000462 //
Chris Lattner08db7192002-11-06 06:20:27 +0000463 if (Size == 0) return;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000464 }
465 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000466
467 // Now that there are no outgoing edges, all of the Links are dead.
468 N->Links.clear();
Chris Lattner08db7192002-11-06 06:20:27 +0000469 N->Size = 0;
470 N->Ty.Ty = Type::VoidTy;
471 N->Ty.isArray = false;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000472
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000473 // Merge the node types
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000474 NodeType |= N->NodeType;
Chris Lattner33312f72002-11-08 01:21:07 +0000475 N->NodeType = DEAD; // N is now a dead node.
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000476
477 // Merge the globals list...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000478 if (!N->Globals.empty()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000479 MergeSortedVectors(Globals, N->Globals);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000480
481 // Delete the globals from the old node...
482 N->Globals.clear();
483 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000484}
485
Chris Lattner9de906c2002-10-20 22:11:44 +0000486//===----------------------------------------------------------------------===//
487// DSCallSite Implementation
488//===----------------------------------------------------------------------===//
489
Vikram S. Adve26b98262002-10-20 21:41:02 +0000490// Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h
Chris Lattner9de906c2002-10-20 22:11:44 +0000491Function &DSCallSite::getCaller() const {
Chris Lattner0969c502002-10-21 02:08:03 +0000492 return *Inst->getParent()->getParent();
Vikram S. Adve26b98262002-10-20 21:41:02 +0000493}
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000494
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000495
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000496//===----------------------------------------------------------------------===//
497// DSGraph Implementation
498//===----------------------------------------------------------------------===//
499
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000500DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000501 std::map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattnerc875f022002-11-03 21:27:48 +0000502 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000503}
504
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000505DSGraph::DSGraph(const DSGraph &G,
506 std::map<const DSNode*, DSNodeHandle> &NodeMap)
Chris Lattnereff0da92002-10-21 15:32:34 +0000507 : Func(G.Func) {
Chris Lattnerc875f022002-11-03 21:27:48 +0000508 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattnereff0da92002-10-21 15:32:34 +0000509}
510
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000511DSGraph::~DSGraph() {
512 FunctionCalls.clear();
Chris Lattner679e8e12002-11-08 21:27:12 +0000513 AuxFunctionCalls.clear();
Chris Lattnerc875f022002-11-03 21:27:48 +0000514 ScalarMap.clear();
Chris Lattner13ec72a2002-10-21 13:31:48 +0000515 RetNode.setNode(0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000516
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000517 // Drop all intra-node references, so that assertions don't fail...
518 std::for_each(Nodes.begin(), Nodes.end(),
519 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000520
521 // Delete all of the nodes themselves...
522 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
523}
524
Chris Lattner0d9bab82002-07-18 00:12:30 +0000525// dump - Allow inspection of graph in a debugger.
526void DSGraph::dump() const { print(std::cerr); }
527
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000528
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000529/// remapLinks - Change all of the Links in the current node according to the
530/// specified mapping.
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000531///
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000532void DSNode::remapLinks(std::map<const DSNode*, DSNodeHandle> &OldNodeMap) {
533 for (unsigned i = 0, e = Links.size(); i != e; ++i) {
534 DSNodeHandle &H = OldNodeMap[Links[i].getNode()];
535 Links[i].setNode(H.getNode());
536 Links[i].setOffset(Links[i].getOffset()+H.getOffset());
537 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000538}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000539
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000540
Chris Lattner0d9bab82002-07-18 00:12:30 +0000541// cloneInto - Clone the specified DSGraph into the current graph, returning the
Chris Lattnerc875f022002-11-03 21:27:48 +0000542// Return node of the graph. The translated ScalarMap for the old function is
Chris Lattner92673292002-11-02 00:13:20 +0000543// filled into the OldValMap member. If StripAllocas is set to true, Alloca
544// markers are removed from the graph, as the graph is being cloned into a
545// calling function's graph.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000546//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000547DSNodeHandle DSGraph::cloneInto(const DSGraph &G,
548 std::map<Value*, DSNodeHandle> &OldValMap,
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000549 std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
Chris Lattner679e8e12002-11-08 21:27:12 +0000550 unsigned CloneFlags) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000551 assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
Chris Lattner33312f72002-11-08 01:21:07 +0000552 assert(&G != this && "Cannot clone graph into itself!");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000553
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000554 unsigned FN = Nodes.size(); // First new node...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000555
556 // Duplicate all of the nodes, populating the node map...
557 Nodes.reserve(FN+G.Nodes.size());
558 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000559 DSNode *Old = G.Nodes[i];
560 DSNode *New = new DSNode(*Old);
Chris Lattner679e8e12002-11-08 21:27:12 +0000561 New->NodeType &= ~DSNode::DEAD; // Clear dead flag...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000562 Nodes.push_back(New);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000563 OldNodeMap[Old] = New;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000564 }
565
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000566 // Rewrite the links in the new nodes to point into the current graph now.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000567 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000568 Nodes[i]->remapLinks(OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000569
Chris Lattner460ea292002-11-07 07:06:20 +0000570 // Remove alloca markers as specified
Chris Lattner679e8e12002-11-08 21:27:12 +0000571 if (CloneFlags & StripAllocaBit)
Chris Lattner0d9bab82002-07-18 00:12:30 +0000572 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattner460ea292002-11-07 07:06:20 +0000573 Nodes[i]->NodeType &= ~DSNode::AllocaNode;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000574
Chris Lattnercf15db32002-10-17 20:09:52 +0000575 // Copy the value map... and merge all of the global nodes...
Chris Lattnerc875f022002-11-03 21:27:48 +0000576 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ScalarMap.begin(),
577 E = G.ScalarMap.end(); I != E; ++I) {
Chris Lattnercf15db32002-10-17 20:09:52 +0000578 DSNodeHandle &H = OldValMap[I->first];
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000579 DSNodeHandle &MappedNode = OldNodeMap[I->second.getNode()];
580 H.setNode(MappedNode.getNode());
581 H.setOffset(I->second.getOffset()+MappedNode.getOffset());
Chris Lattnercf15db32002-10-17 20:09:52 +0000582
583 if (isa<GlobalValue>(I->first)) { // Is this a global?
Chris Lattnerc875f022002-11-03 21:27:48 +0000584 std::map<Value*, DSNodeHandle>::iterator GVI = ScalarMap.find(I->first);
585 if (GVI != ScalarMap.end()) { // Is the global value in this fn already?
Chris Lattnercf15db32002-10-17 20:09:52 +0000586 GVI->second.mergeWith(H);
587 } else {
Chris Lattnerc875f022002-11-03 21:27:48 +0000588 ScalarMap[I->first] = H; // Add global pointer to this graph
Chris Lattnercf15db32002-10-17 20:09:52 +0000589 }
590 }
591 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000592
Chris Lattner679e8e12002-11-08 21:27:12 +0000593 if (!(CloneFlags & DontCloneCallNodes)) {
594 // Copy the function calls list...
595 unsigned FC = FunctionCalls.size(); // FirstCall
596 FunctionCalls.reserve(FC+G.FunctionCalls.size());
597 for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i)
598 FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap));
Chris Lattneracf491f2002-11-08 22:27:09 +0000599 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000600
Chris Lattneracf491f2002-11-08 22:27:09 +0000601 if (!(CloneFlags & DontCloneAuxCallNodes)) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000602 // Copy the auxillary function calls list...
Chris Lattneracf491f2002-11-08 22:27:09 +0000603 unsigned FC = AuxFunctionCalls.size(); // FirstCall
Chris Lattner679e8e12002-11-08 21:27:12 +0000604 AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size());
605 for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i)
606 AuxFunctionCalls.push_back(DSCallSite(G.AuxFunctionCalls[i], OldNodeMap));
607 }
Chris Lattnercf15db32002-10-17 20:09:52 +0000608
Chris Lattner0d9bab82002-07-18 00:12:30 +0000609 // Return the returned node pointer...
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000610 DSNodeHandle &MappedRet = OldNodeMap[G.RetNode.getNode()];
611 return DSNodeHandle(MappedRet.getNode(),
612 MappedRet.getOffset()+G.RetNode.getOffset());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000613}
614
Chris Lattner076c1f92002-11-07 06:31:54 +0000615/// mergeInGraph - The method is used for merging graphs together. If the
616/// argument graph is not *this, it makes a clone of the specified graph, then
617/// merges the nodes specified in the call site with the formal arguments in the
618/// graph.
619///
620void DSGraph::mergeInGraph(DSCallSite &CS, const DSGraph &Graph,
Chris Lattner679e8e12002-11-08 21:27:12 +0000621 unsigned CloneFlags) {
Chris Lattner076c1f92002-11-07 06:31:54 +0000622 std::map<Value*, DSNodeHandle> OldValMap;
623 DSNodeHandle RetVal;
624 std::map<Value*, DSNodeHandle> *ScalarMap = &OldValMap;
625
626 // If this is not a recursive call, clone the graph into this graph...
627 if (&Graph != this) {
628 // Clone the callee's graph into the current graph, keeping
629 // track of where scalars in the old graph _used_ to point,
630 // and of the new nodes matching nodes of the old graph.
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000631 std::map<const DSNode*, DSNodeHandle> OldNodeMap;
Chris Lattner076c1f92002-11-07 06:31:54 +0000632
633 // The clone call may invalidate any of the vectors in the data
634 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner679e8e12002-11-08 21:27:12 +0000635 RetVal = cloneInto(Graph, OldValMap, OldNodeMap, CloneFlags);
Chris Lattner076c1f92002-11-07 06:31:54 +0000636 ScalarMap = &OldValMap;
637 } else {
638 RetVal = getRetNode();
639 ScalarMap = &getScalarMap();
640 }
641
642 // Merge the return value with the return value of the context...
643 RetVal.mergeWith(CS.getRetVal());
644
645 // Resolve all of the function arguments...
646 Function &F = Graph.getFunction();
647 Function::aiterator AI = F.abegin();
648 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) {
649 // Advance the argument iterator to the first pointer argument...
650 while (!isPointerType(AI->getType())) {
651 ++AI;
652#ifndef NDEBUG
653 if (AI == F.aend())
654 std::cerr << "Bad call to Function: " << F.getName() << "\n";
655#endif
656 assert(AI != F.aend() && "# Args provided is not # Args required!");
657 }
658
659 // Add the link from the argument scalar to the provided value
660 DSNodeHandle &NH = (*ScalarMap)[AI];
661 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
662 NH.mergeWith(CS.getPtrArg(i));
663 }
664}
665
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000666#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000667// cloneGlobalInto - Clone the given global node and all its target links
668// (and all their llinks, recursively).
669//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000670DSNode *DSGraph::cloneGlobalInto(const DSNode *GNode) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000671 if (GNode == 0 || GNode->getGlobals().size() == 0) return 0;
672
673 // If a clone has already been created for GNode, return it.
Chris Lattnerc875f022002-11-03 21:27:48 +0000674 DSNodeHandle& ValMapEntry = ScalarMap[GNode->getGlobals()[0]];
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000675 if (ValMapEntry != 0)
676 return ValMapEntry;
677
678 // Clone the node and update the ValMap.
679 DSNode* NewNode = new DSNode(*GNode);
680 ValMapEntry = NewNode; // j=0 case of loop below!
681 Nodes.push_back(NewNode);
682 for (unsigned j = 1, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +0000683 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000684
685 // Rewrite the links in the new node to point into the current graph.
686 for (unsigned j = 0, e = GNode->getNumLinks(); j != e; ++j)
687 NewNode->setLink(j, cloneGlobalInto(GNode->getLink(j)));
688
689 return NewNode;
690}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000691#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000692
693
Chris Lattner0d9bab82002-07-18 00:12:30 +0000694// markIncompleteNodes - Mark the specified node as having contents that are not
695// known with the current analysis we have performed. Because a node makes all
696// of the nodes it can reach imcomplete if the node itself is incomplete, we
697// must recursively traverse the data structure graph, marking all reachable
698// nodes as incomplete.
699//
700static void markIncompleteNode(DSNode *N) {
701 // Stop recursion if no node, or if node already marked...
702 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
703
704 // Actually mark the node
705 N->NodeType |= DSNode::Incomplete;
706
707 // Recusively process children...
Chris Lattner08db7192002-11-06 06:20:27 +0000708 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
709 if (DSNode *DSN = N->getLink(i).getNode())
710 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000711}
712
713
714// markIncompleteNodes - Traverse the graph, identifying nodes that may be
715// modified by other functions that have not been resolved yet. This marks
716// nodes that are reachable through three sources of "unknownness":
717//
718// Global Variables, Function Calls, and Incoming Arguments
719//
720// For any node that may have unknown components (because something outside the
721// scope of current analysis may have modified it), the 'Incomplete' flag is
722// added to the NodeType.
723//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000724void DSGraph::markIncompleteNodes(bool markFormalArgs) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000725 // Mark any incoming arguments as incomplete...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000726 if (markFormalArgs && Func)
727 for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I)
Chris Lattnerc875f022002-11-03 21:27:48 +0000728 if (isPointerType(I->getType()) && ScalarMap.find(I) != ScalarMap.end())
729 markIncompleteNode(ScalarMap[I].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000730
731 // Mark stuff passed into functions calls as being incomplete...
732 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Vikram S. Adve26b98262002-10-20 21:41:02 +0000733 DSCallSite &Call = FunctionCalls[i];
Chris Lattnere2219762002-07-18 18:22:40 +0000734 // Then the return value is certainly incomplete!
Chris Lattner0969c502002-10-21 02:08:03 +0000735 markIncompleteNode(Call.getRetVal().getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000736
Chris Lattner92673292002-11-02 00:13:20 +0000737 // All objects pointed to by function arguments are incomplete though!
Vikram S. Adve26b98262002-10-20 21:41:02 +0000738 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i)
Chris Lattner0969c502002-10-21 02:08:03 +0000739 markIncompleteNode(Call.getPtrArg(i).getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000740 }
741
Chris Lattner92673292002-11-02 00:13:20 +0000742 // Mark all of the nodes pointed to by global nodes as incomplete...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000743 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000744 if (Nodes[i]->NodeType & DSNode::GlobalNode) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000745 DSNode *N = Nodes[i];
Chris Lattner92673292002-11-02 00:13:20 +0000746 // FIXME: Make more efficient by looking over Links directly
Chris Lattner08db7192002-11-06 06:20:27 +0000747 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
748 if (DSNode *DSN = N->getLink(i).getNode())
749 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000750 }
751}
752
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000753// removeRefsToGlobal - Helper function that removes globals from the
Chris Lattnerc875f022002-11-03 21:27:48 +0000754// ScalarMap so that the referrer count will go down to zero.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000755static void removeRefsToGlobal(DSNode* N,
Chris Lattnerc875f022002-11-03 21:27:48 +0000756 std::map<Value*, DSNodeHandle> &ScalarMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000757 while (!N->getGlobals().empty()) {
758 GlobalValue *GV = N->getGlobals().back();
759 N->getGlobals().pop_back();
Chris Lattnerc875f022002-11-03 21:27:48 +0000760 ScalarMap.erase(GV);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000761 }
762}
763
764
Chris Lattner0d9bab82002-07-18 00:12:30 +0000765// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
766// checks to see if there are simple transformations that it can do to make it
767// dead.
768//
769bool DSGraph::isNodeDead(DSNode *N) {
770 // Is it a trivially dead shadow node...
Chris Lattner33312f72002-11-08 01:21:07 +0000771 if (N->getReferrers().empty() &&
772 (N->NodeType == 0 || N->NodeType == DSNode::DEAD))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000773 return true;
774
775 // Is it a function node or some other trivially unused global?
Chris Lattner92673292002-11-02 00:13:20 +0000776 if ((N->NodeType & ~DSNode::GlobalNode) == 0 && N->getSize() == 0 &&
Chris Lattner0d9bab82002-07-18 00:12:30 +0000777 N->getReferrers().size() == N->getGlobals().size()) {
778
Chris Lattnerc875f022002-11-03 21:27:48 +0000779 // Remove the globals from the ScalarMap, so that the referrer count will go
Chris Lattner0d9bab82002-07-18 00:12:30 +0000780 // down to zero.
Chris Lattnerc875f022002-11-03 21:27:48 +0000781 removeRefsToGlobal(N, ScalarMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000782 assert(N->getReferrers().empty() && "Referrers should all be gone now!");
783 return true;
784 }
785
786 return false;
787}
788
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000789static void removeIdenticalCalls(vector<DSCallSite> &Calls,
Chris Lattner7541b892002-07-31 19:32:12 +0000790 const std::string &where) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000791 // Remove trivially identical function calls
792 unsigned NumFns = Calls.size();
793 std::sort(Calls.begin(), Calls.end());
794 Calls.erase(std::unique(Calls.begin(), Calls.end()),
795 Calls.end());
796
Chris Lattner33312f72002-11-08 01:21:07 +0000797 // Track the number of call nodes merged away...
798 NumCallNodesMerged += NumFns-Calls.size();
799
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000800 DEBUG(if (NumFns != Calls.size())
Chris Lattner7541b892002-07-31 19:32:12 +0000801 std::cerr << "Merged " << (NumFns-Calls.size())
802 << " call nodes in " << where << "\n";);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000803}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000804
Chris Lattnere2219762002-07-18 18:22:40 +0000805// removeTriviallyDeadNodes - After the graph has been constructed, this method
806// removes all unreachable nodes that are created because they got merged with
807// other nodes in the graph. These nodes will all be trivially unreachable, so
808// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000809//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000810void DSGraph::removeTriviallyDeadNodes(bool KeepAllGlobals) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000811 for (unsigned i = 0; i != Nodes.size(); ++i)
Chris Lattnera00397e2002-10-03 21:55:28 +0000812 if (!KeepAllGlobals || !(Nodes[i]->NodeType & DSNode::GlobalNode))
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000813 if (isNodeDead(Nodes[i])) { // This node is dead!
814 delete Nodes[i]; // Free memory...
815 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
816 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000817
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000818 removeIdenticalCalls(FunctionCalls, Func ? Func->getName() : "");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000819}
820
821
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000822// markAlive - Simple graph walker that recursively traverses the graph, marking
Chris Lattnere2219762002-07-18 18:22:40 +0000823// stuff to be alive.
824//
825static void markAlive(DSNode *N, std::set<DSNode*> &Alive) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000826 if (N == 0) return;
Chris Lattnere2219762002-07-18 18:22:40 +0000827
828 Alive.insert(N);
Chris Lattner08db7192002-11-06 06:20:27 +0000829 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
830 if (DSNode *DSN = N->getLink(i).getNode())
831 if (!Alive.count(DSN))
832 markAlive(DSN, Alive);
Chris Lattnere2219762002-07-18 18:22:40 +0000833}
834
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000835static bool checkGlobalAlive(DSNode *N, std::set<DSNode*> &Alive,
836 std::set<DSNode*> &Visiting) {
837 if (N == 0) return false;
838
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000839 if (Visiting.count(N)) return false; // terminate recursion on a cycle
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000840 Visiting.insert(N);
841
842 // If any immediate successor is alive, 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 (Alive.count(DSN)) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000846 Visiting.erase(N);
847 return true;
848 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000849
850 // Else if any successor reaches a live node, N is alive
Chris Lattner08db7192002-11-06 06:20:27 +0000851 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
852 if (DSNode *DSN = N->getLink(i).getNode())
853 if (checkGlobalAlive(DSN, Alive, Visiting)) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000854 Visiting.erase(N); return true;
855 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000856
857 Visiting.erase(N);
858 return false;
859}
860
861
862// markGlobalsIteration - Recursive helper function for markGlobalsAlive().
863// This would be unnecessary if function calls were real nodes! In that case,
864// the simple iterative loop in the first few lines below suffice.
865//
866static void markGlobalsIteration(std::set<DSNode*>& GlobalNodes,
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000867 vector<DSCallSite> &Calls,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000868 std::set<DSNode*> &Alive,
869 bool FilterCalls) {
870
871 // Iterate, marking globals or cast nodes alive until no new live nodes
872 // are added to Alive
873 std::set<DSNode*> Visiting; // Used to identify cycles
Chris Lattner0969c502002-10-21 02:08:03 +0000874 std::set<DSNode*>::iterator I = GlobalNodes.begin(), E = GlobalNodes.end();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000875 for (size_t liveCount = 0; liveCount < Alive.size(); ) {
876 liveCount = Alive.size();
877 for ( ; I != E; ++I)
878 if (Alive.count(*I) == 0) {
879 Visiting.clear();
880 if (checkGlobalAlive(*I, Alive, Visiting))
881 markAlive(*I, Alive);
882 }
883 }
884
885 // Find function calls with some dead and some live nodes.
886 // Since all call nodes must be live if any one is live, we have to mark
887 // all nodes of the call as live and continue the iteration (via recursion).
888 if (FilterCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000889 bool Recurse = false;
890 for (unsigned i = 0, ei = Calls.size(); i < ei; ++i) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000891 bool CallIsDead = true, CallHasDeadArg = false;
Chris Lattner0969c502002-10-21 02:08:03 +0000892 DSCallSite &CS = Calls[i];
893 for (unsigned j = 0, ej = CS.getNumPtrArgs(); j != ej; ++j)
894 if (DSNode *N = CS.getPtrArg(j).getNode()) {
895 bool ArgIsDead = !Alive.count(N);
896 CallHasDeadArg |= ArgIsDead;
897 CallIsDead &= ArgIsDead;
898 }
899
900 if (DSNode *N = CS.getRetVal().getNode()) {
901 bool RetIsDead = !Alive.count(N);
902 CallHasDeadArg |= RetIsDead;
903 CallIsDead &= RetIsDead;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000904 }
Chris Lattner0969c502002-10-21 02:08:03 +0000905
906 DSNode *N = CS.getCallee().getNode();
907 bool FnIsDead = !Alive.count(N);
908 CallHasDeadArg |= FnIsDead;
909 CallIsDead &= FnIsDead;
910
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000911 if (!CallIsDead && CallHasDeadArg) {
912 // Some node in this call is live and another is dead.
913 // Mark all nodes of call as live and iterate once more.
Chris Lattner0969c502002-10-21 02:08:03 +0000914 Recurse = true;
915 for (unsigned j = 0, ej = CS.getNumPtrArgs(); j != ej; ++j)
916 markAlive(CS.getPtrArg(j).getNode(), Alive);
917 markAlive(CS.getRetVal().getNode(), Alive);
918 markAlive(CS.getCallee().getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000919 }
920 }
Chris Lattner0969c502002-10-21 02:08:03 +0000921 if (Recurse)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000922 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
923 }
924}
925
926
927// markGlobalsAlive - Mark global nodes and cast nodes alive if they
928// can reach any other live node. Since this can produce new live nodes,
929// we use a simple iterative algorithm.
930//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000931static void markGlobalsAlive(DSGraph &G, std::set<DSNode*> &Alive,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000932 bool FilterCalls) {
933 // Add global and cast nodes to a set so we don't walk all nodes every time
934 std::set<DSNode*> GlobalNodes;
935 for (unsigned i = 0, e = G.getNodes().size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000936 if (G.getNodes()[i]->NodeType & DSNode::GlobalNode)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000937 GlobalNodes.insert(G.getNodes()[i]);
938
939 // Add all call nodes to the same set
Chris Lattner679e8e12002-11-08 21:27:12 +0000940 vector<DSCallSite> &Calls = G.getAuxFunctionCalls();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000941 if (FilterCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000942 for (unsigned i = 0, e = Calls.size(); i != e; ++i) {
943 for (unsigned j = 0, e = Calls[i].getNumPtrArgs(); j != e; ++j)
944 if (DSNode *N = Calls[i].getPtrArg(j).getNode())
945 GlobalNodes.insert(N);
946 if (DSNode *N = Calls[i].getRetVal().getNode())
947 GlobalNodes.insert(N);
948 if (DSNode *N = Calls[i].getCallee().getNode())
949 GlobalNodes.insert(N);
950 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000951 }
952
953 // Iterate and recurse until no new live node are discovered.
954 // This would be a simple iterative loop if function calls were real nodes!
955 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
956
Chris Lattnerc875f022002-11-03 21:27:48 +0000957 // Free up references to dead globals from the ScalarMap
Chris Lattner92673292002-11-02 00:13:20 +0000958 std::set<DSNode*>::iterator I = GlobalNodes.begin(), E = GlobalNodes.end();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000959 for( ; I != E; ++I)
960 if (Alive.count(*I) == 0)
Chris Lattnerc875f022002-11-03 21:27:48 +0000961 removeRefsToGlobal(*I, G.getScalarMap());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000962
963 // Delete dead function calls
964 if (FilterCalls)
965 for (int ei = Calls.size(), i = ei-1; i >= 0; --i) {
966 bool CallIsDead = true;
Chris Lattner0969c502002-10-21 02:08:03 +0000967 for (unsigned j = 0, ej = Calls[i].getNumPtrArgs();
968 CallIsDead && j != ej; ++j)
969 CallIsDead = Alive.count(Calls[i].getPtrArg(j).getNode()) == 0;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000970 if (CallIsDead)
971 Calls.erase(Calls.begin() + i); // remove the call entirely
972 }
973}
Chris Lattnere2219762002-07-18 18:22:40 +0000974
975// removeDeadNodes - Use a more powerful reachability analysis to eliminate
976// subgraphs that are unreachable. This often occurs because the data
977// structure doesn't "escape" into it's caller, and thus should be eliminated
978// from the caller's graph entirely. This is only appropriate to use when
979// inlining graphs.
980//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000981void DSGraph::removeDeadNodes(bool KeepAllGlobals, bool KeepCalls) {
Chris Lattner33312f72002-11-08 01:21:07 +0000982 assert((!KeepAllGlobals || KeepCalls) && // FIXME: This should be an enum!
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000983 "KeepAllGlobals without KeepCalls is meaningless");
984
Chris Lattnere2219762002-07-18 18:22:40 +0000985 // Reduce the amount of work we have to do...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000986 removeTriviallyDeadNodes(KeepAllGlobals);
987
Chris Lattnere2219762002-07-18 18:22:40 +0000988 // FIXME: Merge nontrivially identical call nodes...
989
990 // Alive - a set that holds all nodes found to be reachable/alive.
991 std::set<DSNode*> Alive;
992
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000993 // If KeepCalls, mark all nodes reachable by call nodes as alive...
Chris Lattneracf491f2002-11-08 22:27:09 +0000994 if (KeepCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000995 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
996 for (unsigned j = 0, e = FunctionCalls[i].getNumPtrArgs(); j != e; ++j)
997 markAlive(FunctionCalls[i].getPtrArg(j).getNode(), Alive);
998 markAlive(FunctionCalls[i].getRetVal().getNode(), Alive);
999 markAlive(FunctionCalls[i].getCallee().getNode(), Alive);
1000 }
Chris Lattneracf491f2002-11-08 22:27:09 +00001001 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i) {
1002 for (unsigned j = 0, e = AuxFunctionCalls[i].getNumPtrArgs(); j != e; ++j)
1003 markAlive(AuxFunctionCalls[i].getPtrArg(j).getNode(), Alive);
1004 markAlive(AuxFunctionCalls[i].getRetVal().getNode(), Alive);
1005 markAlive(AuxFunctionCalls[i].getCallee().getNode(), Alive);
1006 }
1007 }
Chris Lattnere2219762002-07-18 18:22:40 +00001008
Chris Lattner92673292002-11-02 00:13:20 +00001009 // Mark all nodes reachable by scalar nodes as alive...
Chris Lattnerc875f022002-11-03 21:27:48 +00001010 for (std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.begin(),
1011 E = ScalarMap.end(); I != E; ++I)
Chris Lattner92673292002-11-02 00:13:20 +00001012 markAlive(I->second.getNode(), Alive);
Chris Lattnere2219762002-07-18 18:22:40 +00001013
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001014 // The return value is alive as well...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001015 markAlive(RetNode.getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001016
1017 // Mark all globals or cast nodes that can reach a live node as alive.
1018 // This also marks all nodes reachable from such nodes as alive.
1019 // Of course, if KeepAllGlobals is specified, they would be live already.
Chris Lattner33312f72002-11-08 01:21:07 +00001020
Chris Lattner0969c502002-10-21 02:08:03 +00001021 if (!KeepAllGlobals)
Chris Lattner92673292002-11-02 00:13:20 +00001022 markGlobalsAlive(*this, Alive, !KeepCalls);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001023
Chris Lattnere2219762002-07-18 18:22:40 +00001024 // Loop over all unreachable nodes, dropping their references...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001025 vector<DSNode*> DeadNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001026 DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed.
1027 for (unsigned i = 0; i != Nodes.size(); ++i)
1028 if (!Alive.count(Nodes[i])) {
1029 DSNode *N = Nodes[i];
1030 Nodes.erase(Nodes.begin()+i--); // Erase node from alive list.
1031 DeadNodes.push_back(N); // Add node to our list of dead nodes
1032 N->dropAllReferences(); // Drop all outgoing edges
1033 }
1034
Chris Lattnere2219762002-07-18 18:22:40 +00001035 // Delete all dead nodes...
1036 std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
1037}
1038
1039
1040
Chris Lattner0d9bab82002-07-18 00:12:30 +00001041// maskNodeTypes - Apply a mask to all of the node types in the graph. This
1042// is useful for clearing out markers like Scalar or Incomplete.
1043//
1044void DSGraph::maskNodeTypes(unsigned char Mask) {
1045 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
1046 Nodes[i]->NodeType &= Mask;
1047}
1048
1049
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001050#if 0
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001051//===----------------------------------------------------------------------===//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001052// GlobalDSGraph Implementation
1053//===----------------------------------------------------------------------===//
1054
1055GlobalDSGraph::GlobalDSGraph() : DSGraph(*(Function*)0, this) {
1056}
1057
1058GlobalDSGraph::~GlobalDSGraph() {
1059 assert(Referrers.size() == 0 &&
1060 "Deleting global graph while references from other graphs exist");
1061}
1062
1063void GlobalDSGraph::addReference(const DSGraph* referrer) {
1064 if (referrer != this)
1065 Referrers.insert(referrer);
1066}
1067
1068void GlobalDSGraph::removeReference(const DSGraph* referrer) {
1069 if (referrer != this) {
1070 assert(Referrers.find(referrer) != Referrers.end() && "This is very bad!");
1071 Referrers.erase(referrer);
1072 if (Referrers.size() == 0)
1073 delete this;
1074 }
1075}
1076
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001077#if 0
Chris Lattnerd18f3422002-11-03 21:24:04 +00001078// Bits used in the next function
1079static const char ExternalTypeBits = DSNode::GlobalNode | DSNode::HeapNode;
1080
1081
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001082// GlobalDSGraph::cloneNodeInto - Clone a global node and all its externally
1083// visible target links (and recursively their such links) into this graph.
1084// NodeCache maps the node being cloned to its clone in the Globals graph,
1085// in order to track cycles.
1086// GlobalsAreFinal is a flag that says whether it is safe to assume that
1087// an existing global node is complete. This is important to avoid
1088// reinserting all globals when inserting Calls to functions.
1089// This is a helper function for cloneGlobals and cloneCalls.
1090//
1091DSNode* GlobalDSGraph::cloneNodeInto(DSNode *OldNode,
1092 std::map<const DSNode*, DSNode*> &NodeCache,
1093 bool GlobalsAreFinal) {
1094 if (OldNode == 0) return 0;
1095
1096 // The caller should check this is an external node. Just more efficient...
1097 assert((OldNode->NodeType & ExternalTypeBits) && "Non-external node");
1098
1099 // If a clone has already been created for OldNode, return it.
1100 DSNode*& CacheEntry = NodeCache[OldNode];
1101 if (CacheEntry != 0)
1102 return CacheEntry;
1103
1104 // The result value...
1105 DSNode* NewNode = 0;
1106
1107 // If nodes already exist for any of the globals of OldNode,
1108 // merge all such nodes together since they are merged in OldNode.
1109 // If ValueCacheIsFinal==true, look for an existing node that has
1110 // an identical list of globals and return it if it exists.
1111 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001112 for (unsigned j = 0, N = OldNode->getGlobals().size(); j != N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001113 if (DSNode *PrevNode = ScalarMap[OldNode->getGlobals()[j]].getNode()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001114 if (NewNode == 0) {
1115 NewNode = PrevNode; // first existing node found
1116 if (GlobalsAreFinal && j == 0)
1117 if (OldNode->getGlobals() == PrevNode->getGlobals()) {
1118 CacheEntry = NewNode;
1119 return NewNode;
1120 }
1121 }
1122 else if (NewNode != PrevNode) { // found another, different from prev
1123 // update ValMap *before* merging PrevNode into NewNode
1124 for (unsigned k = 0, NK = PrevNode->getGlobals().size(); k < NK; ++k)
Chris Lattnerc875f022002-11-03 21:27:48 +00001125 ScalarMap[PrevNode->getGlobals()[k]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001126 NewNode->mergeWith(PrevNode);
1127 }
1128 } else if (NewNode != 0) {
Chris Lattnerc875f022002-11-03 21:27:48 +00001129 ScalarMap[OldNode->getGlobals()[j]] = NewNode; // add the merged node
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001130 }
1131
1132 // If no existing node was found, clone the node and update the ValMap.
1133 if (NewNode == 0) {
1134 NewNode = new DSNode(*OldNode);
1135 Nodes.push_back(NewNode);
1136 for (unsigned j = 0, e = NewNode->getNumLinks(); j != e; ++j)
1137 NewNode->setLink(j, 0);
1138 for (unsigned j = 0, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001139 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001140 }
1141 else
1142 NewNode->NodeType |= OldNode->NodeType; // Markers may be different!
1143
1144 // Add the entry to NodeCache
1145 CacheEntry = NewNode;
1146
1147 // Rewrite the links in the new node to point into the current graph,
1148 // but only for links to external nodes. Set other links to NULL.
1149 for (unsigned j = 0, e = OldNode->getNumLinks(); j != e; ++j) {
1150 DSNode* OldTarget = OldNode->getLink(j);
1151 if (OldTarget && (OldTarget->NodeType & ExternalTypeBits)) {
1152 DSNode* NewLink = this->cloneNodeInto(OldTarget, NodeCache);
1153 if (NewNode->getLink(j))
1154 NewNode->getLink(j)->mergeWith(NewLink);
1155 else
1156 NewNode->setLink(j, NewLink);
1157 }
1158 }
1159
1160 // Remove all local markers
1161 NewNode->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode);
1162
1163 return NewNode;
1164}
1165
1166
1167// GlobalDSGraph::cloneGlobals - Clone global nodes and all their externally
1168// visible target links (and recursively their such links) into this graph.
1169//
1170void GlobalDSGraph::cloneGlobals(DSGraph& Graph, bool CloneCalls) {
1171 std::map<const DSNode*, DSNode*> NodeCache;
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001172#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001173 for (unsigned i = 0, N = Graph.Nodes.size(); i < N; ++i)
1174 if (Graph.Nodes[i]->NodeType & DSNode::GlobalNode)
1175 GlobalsGraph->cloneNodeInto(Graph.Nodes[i], NodeCache, false);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001176 if (CloneCalls)
1177 GlobalsGraph->cloneCalls(Graph);
1178
1179 GlobalsGraph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001180#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001181}
1182
1183
1184// GlobalDSGraph::cloneCalls - Clone function calls and their visible target
1185// links (and recursively their such links) into this graph.
1186//
1187void GlobalDSGraph::cloneCalls(DSGraph& Graph) {
1188 std::map<const DSNode*, DSNode*> NodeCache;
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001189 vector<DSCallSite >& FromCalls =Graph.FunctionCalls;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001190
1191 FunctionCalls.reserve(FunctionCalls.size() + FromCalls.size());
1192
1193 for (int i = 0, ei = FromCalls.size(); i < ei; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001194 DSCallSite& callCopy = FunctionCalls.back();
1195 callCopy.reserve(FromCalls[i].size());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001196 for (unsigned j = 0, ej = FromCalls[i].size(); j != ej; ++j)
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001197 callCopy.push_back
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001198 ((FromCalls[i][j] && (FromCalls[i][j]->NodeType & ExternalTypeBits))
1199 ? cloneNodeInto(FromCalls[i][j], NodeCache, true)
1200 : 0);
1201 }
1202
1203 // remove trivially identical function calls
Chris Lattner7541b892002-07-31 19:32:12 +00001204 removeIdenticalCalls(FunctionCalls, "Globals Graph");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001205}
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001206#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001207
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001208#endif