blob: aab344bb99cecfd2628aee3a17c6b9cb80480f80 [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 //
Chris Lattnerd8888932002-11-09 19:25:27 +0000138 bool WillBeArray = false;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000139 while (const ArrayType *AT = dyn_cast<ArrayType>(NewTy)) {
140 // FIXME: we might want to keep small arrays, but must be careful about
141 // things like: [2 x [10000 x int*]]
142 NewTy = AT->getElementType();
Chris Lattnerd8888932002-11-09 19:25:27 +0000143 WillBeArray = true;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000144 }
145
Chris Lattner08db7192002-11-06 06:20:27 +0000146 // Figure out how big the new type we're merging in is...
147 unsigned NewTySize = NewTy->isSized() ? TD.getTypeSize(NewTy) : 0;
148
149 // Otherwise check to see if we can fold this type into the current node. If
150 // we can't, we fold the node completely, if we can, we potentially update our
151 // internal state.
152 //
153 if (Ty.Ty == Type::VoidTy) {
154 // If this is the first type that this node has seen, just accept it without
155 // question....
156 assert(Offset == 0 && "Cannot have an offset into a void node!");
Chris Lattnerd8888932002-11-09 19:25:27 +0000157 assert(!Ty.isArray && "This shouldn't happen!");
Chris Lattner08db7192002-11-06 06:20:27 +0000158 Ty.Ty = NewTy;
Chris Lattnerd8888932002-11-09 19:25:27 +0000159 Ty.isArray = WillBeArray;
Chris Lattner08db7192002-11-06 06:20:27 +0000160 Size = NewTySize;
161
162 // Calculate the number of outgoing links from this node.
163 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
164 return false;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000165 }
Chris Lattner08db7192002-11-06 06:20:27 +0000166
167 // Handle node expansion case here...
168 if (Offset+NewTySize > Size) {
169 // It is illegal to grow this node if we have treated it as an array of
170 // objects...
171 if (Ty.isArray) {
172 foldNodeCompletely();
173 return true;
174 }
175
176 if (Offset) { // We could handle this case, but we don't for now...
Chris Lattner3c87b292002-11-07 01:54:56 +0000177 DEBUG(std::cerr << "UNIMP: Trying to merge a growth type into "
178 << "offset != 0: Collapsing!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000179 foldNodeCompletely();
180 return true;
181 }
182
183 // Okay, the situation is nice and simple, we are trying to merge a type in
184 // at offset 0 that is bigger than our current type. Implement this by
185 // switching to the new type and then merge in the smaller one, which should
186 // hit the other code path here. If the other code path decides it's not
187 // ok, it will collapse the node as appropriate.
188 //
189 const Type *OldTy = Ty.Ty;
190 Ty.Ty = NewTy;
Chris Lattnerd8888932002-11-09 19:25:27 +0000191 Ty.isArray = WillBeArray;
Chris Lattner08db7192002-11-06 06:20:27 +0000192 Size = NewTySize;
193
194 // Must grow links to be the appropriate size...
195 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
196
197 // Merge in the old type now... which is guaranteed to be smaller than the
198 // "current" type.
199 return mergeTypeInfo(OldTy, 0);
200 }
201
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000202 assert(Offset <= Size &&
Chris Lattner08db7192002-11-06 06:20:27 +0000203 "Cannot merge something into a part of our type that doesn't exist!");
204
205 // Find the section of Ty.Ty that NewTy overlaps with... first we find the
206 // type that starts at offset Offset.
207 //
208 unsigned O = 0;
209 const Type *SubType = Ty.Ty;
210 while (O < Offset) {
211 assert(Offset-O < TD.getTypeSize(SubType) && "Offset out of range!");
212
213 switch (SubType->getPrimitiveID()) {
214 case Type::StructTyID: {
215 const StructType *STy = cast<StructType>(SubType);
216 const StructLayout &SL = *TD.getStructLayout(STy);
217
218 unsigned i = 0, e = SL.MemberOffsets.size();
219 for (; i+1 < e && SL.MemberOffsets[i+1] <= Offset-O; ++i)
220 /* empty */;
221
222 // The offset we are looking for must be in the i'th element...
223 SubType = STy->getElementTypes()[i];
224 O += SL.MemberOffsets[i];
225 break;
226 }
227 case Type::ArrayTyID: {
228 SubType = cast<ArrayType>(SubType)->getElementType();
229 unsigned ElSize = TD.getTypeSize(SubType);
230 unsigned Remainder = (Offset-O) % ElSize;
231 O = Offset-Remainder;
232 break;
233 }
234 default:
235 assert(0 && "Unknown type!");
236 }
237 }
238
239 assert(O == Offset && "Could not achieve the correct offset!");
240
241 // If we found our type exactly, early exit
242 if (SubType == NewTy) return false;
243
244 // Okay, so we found the leader type at the offset requested. Search the list
245 // of types that starts at this offset. If SubType is currently an array or
246 // structure, the type desired may actually be the first element of the
247 // composite type...
248 //
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000249 unsigned SubTypeSize = SubType->isSized() ? TD.getTypeSize(SubType) : 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000250 while (SubType != NewTy) {
251 const Type *NextSubType = 0;
Chris Lattnerbf10f052002-11-09 00:49:05 +0000252 unsigned NextSubTypeSize = 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000253 switch (SubType->getPrimitiveID()) {
254 case Type::StructTyID:
255 NextSubType = cast<StructType>(SubType)->getElementTypes()[0];
256 NextSubTypeSize = TD.getTypeSize(SubType);
257 break;
258 case Type::ArrayTyID:
259 NextSubType = cast<ArrayType>(SubType)->getElementType();
260 NextSubTypeSize = TD.getTypeSize(SubType);
261 break;
262 default: ;
263 // fall out
264 }
265
266 if (NextSubType == 0)
267 break; // In the default case, break out of the loop
268
269 if (NextSubTypeSize < NewTySize)
270 break; // Don't allow shrinking to a smaller type than NewTySize
271 SubType = NextSubType;
272 SubTypeSize = NextSubTypeSize;
273 }
274
275 // If we found the type exactly, return it...
276 if (SubType == NewTy)
277 return false;
278
279 // Check to see if we have a compatible, but different type...
280 if (NewTySize == SubTypeSize) {
281 // Check to see if this type is obviously convertable... int -> uint f.e.
282 if (NewTy->isLosslesslyConvertableTo(SubType))
283 return false;
284
285 // Check to see if we have a pointer & integer mismatch going on here,
286 // loading a pointer as a long, for example.
287 //
288 if (SubType->isInteger() && isa<PointerType>(NewTy) ||
289 NewTy->isInteger() && isa<PointerType>(SubType))
290 return false;
291
292 }
293
294
Chris Lattner3c87b292002-11-07 01:54:56 +0000295 DEBUG(std::cerr << "MergeTypeInfo Folding OrigTy: " << Ty.Ty
296 << "\n due to:" << NewTy << " @ " << Offset << "!\n"
297 << "SubType: " << SubType << "\n\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000298
299 foldNodeCompletely();
300 return true;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000301}
302
Chris Lattner08db7192002-11-06 06:20:27 +0000303
304
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000305// addEdgeTo - Add an edge from the current node to the specified node. This
306// can cause merging of nodes in the graph.
307//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000308void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000309 if (NH.getNode() == 0) return; // Nothing to do
310
Chris Lattner08db7192002-11-06 06:20:27 +0000311 DSNodeHandle &ExistingEdge = getLink(Offset);
312 if (ExistingEdge.getNode()) {
Chris Lattner7b7200c2002-10-02 04:57:39 +0000313 // Merge the two nodes...
Chris Lattner08db7192002-11-06 06:20:27 +0000314 ExistingEdge.mergeWith(NH);
Chris Lattner7b7200c2002-10-02 04:57:39 +0000315 } else { // No merging to perform...
316 setLink(Offset, NH); // Just force a link in there...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000317 }
Chris Lattner7b7200c2002-10-02 04:57:39 +0000318}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000319
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000320
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000321// MergeSortedVectors - Efficiently merge a vector into another vector where
322// duplicates are not allowed and both are sorted. This assumes that 'T's are
323// efficiently copyable and have sane comparison semantics.
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000324//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000325template<typename T>
326void MergeSortedVectors(vector<T> &Dest, const vector<T> &Src) {
327 // By far, the most common cases will be the simple ones. In these cases,
328 // avoid having to allocate a temporary vector...
329 //
330 if (Src.empty()) { // Nothing to merge in...
331 return;
332 } else if (Dest.empty()) { // Just copy the result in...
333 Dest = Src;
334 } else if (Src.size() == 1) { // Insert a single element...
335 const T &V = Src[0];
336 typename vector<T>::iterator I =
337 std::lower_bound(Dest.begin(), Dest.end(), V);
338 if (I == Dest.end() || *I != Src[0]) // If not already contained...
339 Dest.insert(I, Src[0]);
340 } else if (Dest.size() == 1) {
341 T Tmp = Dest[0]; // Save value in temporary...
342 Dest = Src; // Copy over list...
343 typename vector<T>::iterator I =
344 std::lower_bound(Dest.begin(), Dest.end(),Tmp);
345 if (I == Dest.end() || *I != Src[0]) // If not already contained...
346 Dest.insert(I, Src[0]);
347
348 } else {
349 // Make a copy to the side of Dest...
350 vector<T> Old(Dest);
351
352 // Make space for all of the type entries now...
353 Dest.resize(Dest.size()+Src.size());
354
355 // Merge the two sorted ranges together... into Dest.
356 std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin());
357
358 // Now erase any duplicate entries that may have accumulated into the
359 // vectors (because they were in both of the input sets)
360 Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end());
361 }
362}
363
364
365// mergeWith - Merge this node and the specified node, moving all links to and
366// from the argument node into the current node, deleting the node argument.
367// Offset indicates what offset the specified node is to be merged into the
368// current node.
369//
370// The specified node may be a null pointer (in which case, nothing happens).
371//
372void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
373 DSNode *N = NH.getNode();
374 if (N == 0 || (N == this && NH.getOffset() == Offset))
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000375 return; // Noop
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000376
Chris Lattner679e8e12002-11-08 21:27:12 +0000377 assert((N->NodeType & DSNode::DEAD) == 0);
378 assert((NodeType & DSNode::DEAD) == 0);
379 assert(!hasNoReferrers() && "Should not try to fold a useless node!");
380
Chris Lattner02606632002-11-04 06:48:26 +0000381 if (N == this) {
Chris Lattner08db7192002-11-06 06:20:27 +0000382 // We cannot merge two pieces of the same node together, collapse the node
383 // completely.
Chris Lattner3c87b292002-11-07 01:54:56 +0000384 DEBUG(std::cerr << "Attempting to merge two chunks of"
385 << " the same node together!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000386 foldNodeCompletely();
Chris Lattner02606632002-11-04 06:48:26 +0000387 return;
388 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000389
Chris Lattner08db7192002-11-06 06:20:27 +0000390 // Merge the type entries of the two nodes together...
Chris Lattner679e8e12002-11-08 21:27:12 +0000391 if (N->Ty.Ty != Type::VoidTy) {
Chris Lattner08db7192002-11-06 06:20:27 +0000392 mergeTypeInfo(N->Ty.Ty, Offset);
393
Chris Lattner679e8e12002-11-08 21:27:12 +0000394 // mergeTypeInfo can cause collapsing, which can cause this node to become
395 // dead.
396 if (hasNoReferrers()) return;
397 }
398 assert((NodeType & DSNode::DEAD) == 0);
399
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000400 // If we are merging a node with a completely folded node, then both nodes are
401 // now completely folded.
402 //
403 if (isNodeCompletelyFolded()) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000404 if (!N->isNodeCompletelyFolded()) {
Chris Lattner02606632002-11-04 06:48:26 +0000405 N->foldNodeCompletely();
Chris Lattner679e8e12002-11-08 21:27:12 +0000406 if (hasNoReferrers()) return;
407 }
Chris Lattner02606632002-11-04 06:48:26 +0000408 } else if (N->isNodeCompletelyFolded()) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000409 foldNodeCompletely();
410 Offset = 0;
Chris Lattner679e8e12002-11-08 21:27:12 +0000411 if (hasNoReferrers()) return;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000412 }
Chris Lattner02606632002-11-04 06:48:26 +0000413 N = NH.getNode();
Chris Lattner679e8e12002-11-08 21:27:12 +0000414 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner02606632002-11-04 06:48:26 +0000415
Chris Lattner2c0bd012002-11-06 18:01:39 +0000416 if (this == N || N == 0) return;
Chris Lattner679e8e12002-11-08 21:27:12 +0000417 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000418
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000419 // If both nodes are not at offset 0, make sure that we are merging the node
420 // at an later offset into the node with the zero offset.
421 //
422 if (Offset > NH.getOffset()) {
423 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
424 return;
Chris Lattner9b87c5c2002-10-31 22:41:15 +0000425 } else if (Offset == NH.getOffset() && getSize() < N->getSize()) {
426 // If the offsets are the same, merge the smaller node into the bigger node
427 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
428 return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000429 }
430
431#if 0
432 std::cerr << "\n\nMerging:\n";
433 N->print(std::cerr, 0);
434 std::cerr << " and:\n";
435 print(std::cerr, 0);
436#endif
437
438 // Now we know that Offset <= NH.Offset, so convert it so our "Offset" (with
439 // respect to NH.Offset) is now zero.
440 //
441 unsigned NOffset = NH.getOffset()-Offset;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000442 unsigned NSize = N->getSize();
Chris Lattner7b7200c2002-10-02 04:57:39 +0000443
Chris Lattner679e8e12002-11-08 21:27:12 +0000444 assert((NodeType & DSNode::DEAD) == 0);
445
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000446 // Remove all edges pointing at N, causing them to point to 'this' instead.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000447 // Make sure to adjust their offset, not just the node pointer.
448 //
449 while (!N->Referrers.empty()) {
450 DSNodeHandle &Ref = *N->Referrers.back();
451 Ref = DSNodeHandle(this, NOffset+Ref.getOffset());
452 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000453 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner59535132002-11-05 00:01:58 +0000454
455 // Make all of the outgoing links of N now be outgoing links of this. This
456 // can cause recursive merging!
457 //
Chris Lattner08db7192002-11-06 06:20:27 +0000458 for (unsigned i = 0; i < NSize; i += DS::PointerSize) {
459 DSNodeHandle &Link = N->getLink(i);
460 if (Link.getNode()) {
461 addEdgeTo((i+NOffset) % getSize(), Link);
Chris Lattner59535132002-11-05 00:01:58 +0000462
Chris Lattner08db7192002-11-06 06:20:27 +0000463 // It's possible that after adding the new edge that some recursive
464 // merging just occured, causing THIS node to get merged into oblivion.
465 // If that happens, we must not try to merge any more edges into it!
Chris Lattner7b7200c2002-10-02 04:57:39 +0000466 //
Chris Lattner08db7192002-11-06 06:20:27 +0000467 if (Size == 0) return;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000468 }
469 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000470
471 // Now that there are no outgoing edges, all of the Links are dead.
472 N->Links.clear();
Chris Lattner08db7192002-11-06 06:20:27 +0000473 N->Size = 0;
474 N->Ty.Ty = Type::VoidTy;
475 N->Ty.isArray = false;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000476
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000477 // Merge the node types
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000478 NodeType |= N->NodeType;
Chris Lattner33312f72002-11-08 01:21:07 +0000479 N->NodeType = DEAD; // N is now a dead node.
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000480
481 // Merge the globals list...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000482 if (!N->Globals.empty()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000483 MergeSortedVectors(Globals, N->Globals);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000484
485 // Delete the globals from the old node...
486 N->Globals.clear();
487 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000488}
489
Chris Lattner9de906c2002-10-20 22:11:44 +0000490//===----------------------------------------------------------------------===//
491// DSCallSite Implementation
492//===----------------------------------------------------------------------===//
493
Vikram S. Adve26b98262002-10-20 21:41:02 +0000494// Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h
Chris Lattner9de906c2002-10-20 22:11:44 +0000495Function &DSCallSite::getCaller() const {
Chris Lattner0969c502002-10-21 02:08:03 +0000496 return *Inst->getParent()->getParent();
Vikram S. Adve26b98262002-10-20 21:41:02 +0000497}
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000498
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000499
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000500//===----------------------------------------------------------------------===//
501// DSGraph Implementation
502//===----------------------------------------------------------------------===//
503
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000504DSGraph::DSGraph(const DSGraph &G) : Func(G.Func), GlobalsGraph(0) {
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000505 std::map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattnerc875f022002-11-03 21:27:48 +0000506 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000507}
508
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000509DSGraph::DSGraph(const DSGraph &G,
510 std::map<const DSNode*, DSNodeHandle> &NodeMap)
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000511 : Func(G.Func), GlobalsGraph(0) {
Chris Lattnerc875f022002-11-03 21:27:48 +0000512 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattnereff0da92002-10-21 15:32:34 +0000513}
514
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000515DSGraph::~DSGraph() {
516 FunctionCalls.clear();
Chris Lattner679e8e12002-11-08 21:27:12 +0000517 AuxFunctionCalls.clear();
Chris Lattnerc875f022002-11-03 21:27:48 +0000518 ScalarMap.clear();
Chris Lattner13ec72a2002-10-21 13:31:48 +0000519 RetNode.setNode(0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000520
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000521 // Drop all intra-node references, so that assertions don't fail...
522 std::for_each(Nodes.begin(), Nodes.end(),
523 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000524
525 // Delete all of the nodes themselves...
526 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
527}
528
Chris Lattner0d9bab82002-07-18 00:12:30 +0000529// dump - Allow inspection of graph in a debugger.
530void DSGraph::dump() const { print(std::cerr); }
531
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000532
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000533/// remapLinks - Change all of the Links in the current node according to the
534/// specified mapping.
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000535///
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000536void DSNode::remapLinks(std::map<const DSNode*, DSNodeHandle> &OldNodeMap) {
537 for (unsigned i = 0, e = Links.size(); i != e; ++i) {
538 DSNodeHandle &H = OldNodeMap[Links[i].getNode()];
539 Links[i].setNode(H.getNode());
540 Links[i].setOffset(Links[i].getOffset()+H.getOffset());
541 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000542}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000543
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000544
Chris Lattner0d9bab82002-07-18 00:12:30 +0000545// cloneInto - Clone the specified DSGraph into the current graph, returning the
Chris Lattnerc875f022002-11-03 21:27:48 +0000546// Return node of the graph. The translated ScalarMap for the old function is
Chris Lattner92673292002-11-02 00:13:20 +0000547// filled into the OldValMap member. If StripAllocas is set to true, Alloca
548// markers are removed from the graph, as the graph is being cloned into a
549// calling function's graph.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000550//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000551DSNodeHandle DSGraph::cloneInto(const DSGraph &G,
552 std::map<Value*, DSNodeHandle> &OldValMap,
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000553 std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
Chris Lattner679e8e12002-11-08 21:27:12 +0000554 unsigned CloneFlags) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000555 assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
Chris Lattner33312f72002-11-08 01:21:07 +0000556 assert(&G != this && "Cannot clone graph into itself!");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000557
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000558 unsigned FN = Nodes.size(); // First new node...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000559
560 // Duplicate all of the nodes, populating the node map...
561 Nodes.reserve(FN+G.Nodes.size());
562 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000563 DSNode *Old = G.Nodes[i];
564 DSNode *New = new DSNode(*Old);
Chris Lattner679e8e12002-11-08 21:27:12 +0000565 New->NodeType &= ~DSNode::DEAD; // Clear dead flag...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000566 Nodes.push_back(New);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000567 OldNodeMap[Old] = New;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000568 }
569
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000570 // Rewrite the links in the new nodes to point into the current graph now.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000571 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000572 Nodes[i]->remapLinks(OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000573
Chris Lattner460ea292002-11-07 07:06:20 +0000574 // Remove alloca markers as specified
Chris Lattner679e8e12002-11-08 21:27:12 +0000575 if (CloneFlags & StripAllocaBit)
Chris Lattner0d9bab82002-07-18 00:12:30 +0000576 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattner460ea292002-11-07 07:06:20 +0000577 Nodes[i]->NodeType &= ~DSNode::AllocaNode;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000578
Chris Lattnercf15db32002-10-17 20:09:52 +0000579 // Copy the value map... and merge all of the global nodes...
Chris Lattnerc875f022002-11-03 21:27:48 +0000580 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ScalarMap.begin(),
581 E = G.ScalarMap.end(); I != E; ++I) {
Chris Lattnercf15db32002-10-17 20:09:52 +0000582 DSNodeHandle &H = OldValMap[I->first];
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000583 DSNodeHandle &MappedNode = OldNodeMap[I->second.getNode()];
584 H.setNode(MappedNode.getNode());
585 H.setOffset(I->second.getOffset()+MappedNode.getOffset());
Chris Lattnercf15db32002-10-17 20:09:52 +0000586
587 if (isa<GlobalValue>(I->first)) { // Is this a global?
Chris Lattnerc875f022002-11-03 21:27:48 +0000588 std::map<Value*, DSNodeHandle>::iterator GVI = ScalarMap.find(I->first);
589 if (GVI != ScalarMap.end()) { // Is the global value in this fn already?
Chris Lattnercf15db32002-10-17 20:09:52 +0000590 GVI->second.mergeWith(H);
591 } else {
Chris Lattnerc875f022002-11-03 21:27:48 +0000592 ScalarMap[I->first] = H; // Add global pointer to this graph
Chris Lattnercf15db32002-10-17 20:09:52 +0000593 }
594 }
595 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000596
Chris Lattner679e8e12002-11-08 21:27:12 +0000597 if (!(CloneFlags & DontCloneCallNodes)) {
598 // Copy the function calls list...
599 unsigned FC = FunctionCalls.size(); // FirstCall
600 FunctionCalls.reserve(FC+G.FunctionCalls.size());
601 for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i)
602 FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap));
Chris Lattneracf491f2002-11-08 22:27:09 +0000603 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000604
Chris Lattneracf491f2002-11-08 22:27:09 +0000605 if (!(CloneFlags & DontCloneAuxCallNodes)) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000606 // Copy the auxillary function calls list...
Chris Lattneracf491f2002-11-08 22:27:09 +0000607 unsigned FC = AuxFunctionCalls.size(); // FirstCall
Chris Lattner679e8e12002-11-08 21:27:12 +0000608 AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size());
609 for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i)
610 AuxFunctionCalls.push_back(DSCallSite(G.AuxFunctionCalls[i], OldNodeMap));
611 }
Chris Lattnercf15db32002-10-17 20:09:52 +0000612
Chris Lattner0d9bab82002-07-18 00:12:30 +0000613 // Return the returned node pointer...
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000614 DSNodeHandle &MappedRet = OldNodeMap[G.RetNode.getNode()];
615 return DSNodeHandle(MappedRet.getNode(),
616 MappedRet.getOffset()+G.RetNode.getOffset());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000617}
618
Chris Lattner076c1f92002-11-07 06:31:54 +0000619/// mergeInGraph - The method is used for merging graphs together. If the
620/// argument graph is not *this, it makes a clone of the specified graph, then
621/// merges the nodes specified in the call site with the formal arguments in the
622/// graph.
623///
624void DSGraph::mergeInGraph(DSCallSite &CS, const DSGraph &Graph,
Chris Lattner679e8e12002-11-08 21:27:12 +0000625 unsigned CloneFlags) {
Chris Lattner076c1f92002-11-07 06:31:54 +0000626 std::map<Value*, DSNodeHandle> OldValMap;
627 DSNodeHandle RetVal;
628 std::map<Value*, DSNodeHandle> *ScalarMap = &OldValMap;
629
630 // If this is not a recursive call, clone the graph into this graph...
631 if (&Graph != this) {
632 // Clone the callee's graph into the current graph, keeping
633 // track of where scalars in the old graph _used_ to point,
634 // and of the new nodes matching nodes of the old graph.
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000635 std::map<const DSNode*, DSNodeHandle> OldNodeMap;
Chris Lattner076c1f92002-11-07 06:31:54 +0000636
637 // The clone call may invalidate any of the vectors in the data
638 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner679e8e12002-11-08 21:27:12 +0000639 RetVal = cloneInto(Graph, OldValMap, OldNodeMap, CloneFlags);
Chris Lattner076c1f92002-11-07 06:31:54 +0000640 ScalarMap = &OldValMap;
641 } else {
642 RetVal = getRetNode();
643 ScalarMap = &getScalarMap();
644 }
645
646 // Merge the return value with the return value of the context...
647 RetVal.mergeWith(CS.getRetVal());
648
649 // Resolve all of the function arguments...
650 Function &F = Graph.getFunction();
651 Function::aiterator AI = F.abegin();
652 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) {
653 // Advance the argument iterator to the first pointer argument...
654 while (!isPointerType(AI->getType())) {
655 ++AI;
656#ifndef NDEBUG
657 if (AI == F.aend())
658 std::cerr << "Bad call to Function: " << F.getName() << "\n";
659#endif
660 assert(AI != F.aend() && "# Args provided is not # Args required!");
661 }
662
663 // Add the link from the argument scalar to the provided value
664 DSNodeHandle &NH = (*ScalarMap)[AI];
665 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
666 NH.mergeWith(CS.getPtrArg(i));
667 }
668}
669
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000670#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000671// cloneGlobalInto - Clone the given global node and all its target links
672// (and all their llinks, recursively).
673//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000674DSNode *DSGraph::cloneGlobalInto(const DSNode *GNode) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000675 if (GNode == 0 || GNode->getGlobals().size() == 0) return 0;
676
677 // If a clone has already been created for GNode, return it.
Chris Lattnerc875f022002-11-03 21:27:48 +0000678 DSNodeHandle& ValMapEntry = ScalarMap[GNode->getGlobals()[0]];
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000679 if (ValMapEntry != 0)
680 return ValMapEntry;
681
682 // Clone the node and update the ValMap.
683 DSNode* NewNode = new DSNode(*GNode);
684 ValMapEntry = NewNode; // j=0 case of loop below!
685 Nodes.push_back(NewNode);
686 for (unsigned j = 1, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +0000687 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000688
689 // Rewrite the links in the new node to point into the current graph.
690 for (unsigned j = 0, e = GNode->getNumLinks(); j != e; ++j)
691 NewNode->setLink(j, cloneGlobalInto(GNode->getLink(j)));
692
693 return NewNode;
694}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000695#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000696
697
Chris Lattner0d9bab82002-07-18 00:12:30 +0000698// markIncompleteNodes - Mark the specified node as having contents that are not
699// known with the current analysis we have performed. Because a node makes all
700// of the nodes it can reach imcomplete if the node itself is incomplete, we
701// must recursively traverse the data structure graph, marking all reachable
702// nodes as incomplete.
703//
704static void markIncompleteNode(DSNode *N) {
705 // Stop recursion if no node, or if node already marked...
706 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
707
708 // Actually mark the node
709 N->NodeType |= DSNode::Incomplete;
710
711 // Recusively process children...
Chris Lattner08db7192002-11-06 06:20:27 +0000712 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
713 if (DSNode *DSN = N->getLink(i).getNode())
714 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000715}
716
717
718// markIncompleteNodes - Traverse the graph, identifying nodes that may be
719// modified by other functions that have not been resolved yet. This marks
720// nodes that are reachable through three sources of "unknownness":
721//
722// Global Variables, Function Calls, and Incoming Arguments
723//
724// For any node that may have unknown components (because something outside the
725// scope of current analysis may have modified it), the 'Incomplete' flag is
726// added to the NodeType.
727//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000728void DSGraph::markIncompleteNodes(bool markFormalArgs) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000729 // Mark any incoming arguments as incomplete...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000730 if (markFormalArgs && Func)
731 for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I)
Chris Lattnerc875f022002-11-03 21:27:48 +0000732 if (isPointerType(I->getType()) && ScalarMap.find(I) != ScalarMap.end())
733 markIncompleteNode(ScalarMap[I].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000734
735 // Mark stuff passed into functions calls as being incomplete...
736 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Vikram S. Adve26b98262002-10-20 21:41:02 +0000737 DSCallSite &Call = FunctionCalls[i];
Chris Lattnere2219762002-07-18 18:22:40 +0000738 // Then the return value is certainly incomplete!
Chris Lattner0969c502002-10-21 02:08:03 +0000739 markIncompleteNode(Call.getRetVal().getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000740
Chris Lattner92673292002-11-02 00:13:20 +0000741 // All objects pointed to by function arguments are incomplete though!
Vikram S. Adve26b98262002-10-20 21:41:02 +0000742 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i)
Chris Lattner0969c502002-10-21 02:08:03 +0000743 markIncompleteNode(Call.getPtrArg(i).getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000744 }
745
Chris Lattner92673292002-11-02 00:13:20 +0000746 // Mark all of the nodes pointed to by global nodes as incomplete...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000747 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000748 if (Nodes[i]->NodeType & DSNode::GlobalNode) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000749 DSNode *N = Nodes[i];
Chris Lattner92673292002-11-02 00:13:20 +0000750 // FIXME: Make more efficient by looking over Links directly
Chris Lattner08db7192002-11-06 06:20:27 +0000751 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
752 if (DSNode *DSN = N->getLink(i).getNode())
753 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000754 }
755}
756
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000757// removeRefsToGlobal - Helper function that removes globals from the
Chris Lattnerc875f022002-11-03 21:27:48 +0000758// ScalarMap so that the referrer count will go down to zero.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000759static void removeRefsToGlobal(DSNode* N,
Chris Lattnerc875f022002-11-03 21:27:48 +0000760 std::map<Value*, DSNodeHandle> &ScalarMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000761 while (!N->getGlobals().empty()) {
762 GlobalValue *GV = N->getGlobals().back();
763 N->getGlobals().pop_back();
Chris Lattnerc875f022002-11-03 21:27:48 +0000764 ScalarMap.erase(GV);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000765 }
766}
767
768
Chris Lattner0d9bab82002-07-18 00:12:30 +0000769// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
770// checks to see if there are simple transformations that it can do to make it
771// dead.
772//
773bool DSGraph::isNodeDead(DSNode *N) {
774 // Is it a trivially dead shadow node...
Chris Lattner96517252002-11-09 20:55:24 +0000775 if (N->getReferrers().empty() && (N->NodeType & ~DSNode::DEAD) == 0)
Chris Lattner0d9bab82002-07-18 00:12:30 +0000776 return true;
777
778 // Is it a function node or some other trivially unused global?
Chris Lattner92673292002-11-02 00:13:20 +0000779 if ((N->NodeType & ~DSNode::GlobalNode) == 0 && N->getSize() == 0 &&
Chris Lattner0d9bab82002-07-18 00:12:30 +0000780 N->getReferrers().size() == N->getGlobals().size()) {
781
Chris Lattnerc875f022002-11-03 21:27:48 +0000782 // Remove the globals from the ScalarMap, so that the referrer count will go
Chris Lattner0d9bab82002-07-18 00:12:30 +0000783 // down to zero.
Chris Lattnerc875f022002-11-03 21:27:48 +0000784 removeRefsToGlobal(N, ScalarMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000785 assert(N->getReferrers().empty() && "Referrers should all be gone now!");
786 return true;
787 }
788
789 return false;
790}
791
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000792static void removeIdenticalCalls(vector<DSCallSite> &Calls,
Chris Lattner7541b892002-07-31 19:32:12 +0000793 const std::string &where) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000794 // Remove trivially identical function calls
795 unsigned NumFns = Calls.size();
796 std::sort(Calls.begin(), Calls.end());
797 Calls.erase(std::unique(Calls.begin(), Calls.end()),
798 Calls.end());
799
Chris Lattner33312f72002-11-08 01:21:07 +0000800 // Track the number of call nodes merged away...
801 NumCallNodesMerged += NumFns-Calls.size();
802
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000803 DEBUG(if (NumFns != Calls.size())
Chris Lattner7541b892002-07-31 19:32:12 +0000804 std::cerr << "Merged " << (NumFns-Calls.size())
805 << " call nodes in " << where << "\n";);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000806}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000807
Chris Lattnere2219762002-07-18 18:22:40 +0000808// removeTriviallyDeadNodes - After the graph has been constructed, this method
809// removes all unreachable nodes that are created because they got merged with
810// other nodes in the graph. These nodes will all be trivially unreachable, so
811// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000812//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000813void DSGraph::removeTriviallyDeadNodes(bool KeepAllGlobals) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000814 for (unsigned i = 0; i != Nodes.size(); ++i)
Chris Lattnera00397e2002-10-03 21:55:28 +0000815 if (!KeepAllGlobals || !(Nodes[i]->NodeType & DSNode::GlobalNode))
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000816 if (isNodeDead(Nodes[i])) { // This node is dead!
817 delete Nodes[i]; // Free memory...
818 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
819 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000820
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000821 removeIdenticalCalls(FunctionCalls, Func ? Func->getName() : "");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000822}
823
824
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000825// markAlive - Simple graph walker that recursively traverses the graph, marking
Chris Lattnere2219762002-07-18 18:22:40 +0000826// stuff to be alive.
827//
828static void markAlive(DSNode *N, std::set<DSNode*> &Alive) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000829 if (N == 0) return;
Chris Lattnere2219762002-07-18 18:22:40 +0000830
831 Alive.insert(N);
Chris Lattner08db7192002-11-06 06:20:27 +0000832 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
833 if (DSNode *DSN = N->getLink(i).getNode())
834 if (!Alive.count(DSN))
835 markAlive(DSN, Alive);
Chris Lattnere2219762002-07-18 18:22:40 +0000836}
837
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000838static bool checkGlobalAlive(DSNode *N, std::set<DSNode*> &Alive,
839 std::set<DSNode*> &Visiting) {
840 if (N == 0) return false;
841
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000842 if (Visiting.count(N)) return false; // terminate recursion on a cycle
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000843 Visiting.insert(N);
844
845 // If any immediate successor is alive, N is alive
Chris Lattner08db7192002-11-06 06:20:27 +0000846 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
847 if (DSNode *DSN = N->getLink(i).getNode())
848 if (Alive.count(DSN)) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000849 Visiting.erase(N);
850 return true;
851 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000852
853 // Else if any successor reaches a live node, N is alive
Chris Lattner08db7192002-11-06 06:20:27 +0000854 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
855 if (DSNode *DSN = N->getLink(i).getNode())
856 if (checkGlobalAlive(DSN, Alive, Visiting)) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000857 Visiting.erase(N); return true;
858 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000859
860 Visiting.erase(N);
861 return false;
862}
863
864
865// markGlobalsIteration - Recursive helper function for markGlobalsAlive().
866// This would be unnecessary if function calls were real nodes! In that case,
867// the simple iterative loop in the first few lines below suffice.
868//
869static void markGlobalsIteration(std::set<DSNode*>& GlobalNodes,
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000870 vector<DSCallSite> &Calls,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000871 std::set<DSNode*> &Alive,
872 bool FilterCalls) {
873
874 // Iterate, marking globals or cast nodes alive until no new live nodes
875 // are added to Alive
876 std::set<DSNode*> Visiting; // Used to identify cycles
Chris Lattner0969c502002-10-21 02:08:03 +0000877 std::set<DSNode*>::iterator I = GlobalNodes.begin(), E = GlobalNodes.end();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000878 for (size_t liveCount = 0; liveCount < Alive.size(); ) {
879 liveCount = Alive.size();
880 for ( ; I != E; ++I)
881 if (Alive.count(*I) == 0) {
882 Visiting.clear();
883 if (checkGlobalAlive(*I, Alive, Visiting))
884 markAlive(*I, Alive);
885 }
886 }
887
888 // Find function calls with some dead and some live nodes.
889 // Since all call nodes must be live if any one is live, we have to mark
890 // all nodes of the call as live and continue the iteration (via recursion).
891 if (FilterCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000892 bool Recurse = false;
893 for (unsigned i = 0, ei = Calls.size(); i < ei; ++i) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000894 bool CallIsDead = true, CallHasDeadArg = false;
Chris Lattner0969c502002-10-21 02:08:03 +0000895 DSCallSite &CS = Calls[i];
896 for (unsigned j = 0, ej = CS.getNumPtrArgs(); j != ej; ++j)
897 if (DSNode *N = CS.getPtrArg(j).getNode()) {
898 bool ArgIsDead = !Alive.count(N);
899 CallHasDeadArg |= ArgIsDead;
900 CallIsDead &= ArgIsDead;
901 }
902
903 if (DSNode *N = CS.getRetVal().getNode()) {
904 bool RetIsDead = !Alive.count(N);
905 CallHasDeadArg |= RetIsDead;
906 CallIsDead &= RetIsDead;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000907 }
Chris Lattner0969c502002-10-21 02:08:03 +0000908
909 DSNode *N = CS.getCallee().getNode();
910 bool FnIsDead = !Alive.count(N);
911 CallHasDeadArg |= FnIsDead;
912 CallIsDead &= FnIsDead;
913
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000914 if (!CallIsDead && CallHasDeadArg) {
915 // Some node in this call is live and another is dead.
916 // Mark all nodes of call as live and iterate once more.
Chris Lattner0969c502002-10-21 02:08:03 +0000917 Recurse = true;
918 for (unsigned j = 0, ej = CS.getNumPtrArgs(); j != ej; ++j)
919 markAlive(CS.getPtrArg(j).getNode(), Alive);
920 markAlive(CS.getRetVal().getNode(), Alive);
921 markAlive(CS.getCallee().getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000922 }
923 }
Chris Lattner0969c502002-10-21 02:08:03 +0000924 if (Recurse)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000925 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
926 }
927}
928
929
930// markGlobalsAlive - Mark global nodes and cast nodes alive if they
931// can reach any other live node. Since this can produce new live nodes,
932// we use a simple iterative algorithm.
933//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000934static void markGlobalsAlive(DSGraph &G, std::set<DSNode*> &Alive,
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000935 bool FilterCalls) {
936 // Add global and cast nodes to a set so we don't walk all nodes every time
937 std::set<DSNode*> GlobalNodes;
938 for (unsigned i = 0, e = G.getNodes().size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000939 if (G.getNodes()[i]->NodeType & DSNode::GlobalNode)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000940 GlobalNodes.insert(G.getNodes()[i]);
941
942 // Add all call nodes to the same set
Chris Lattner679e8e12002-11-08 21:27:12 +0000943 vector<DSCallSite> &Calls = G.getAuxFunctionCalls();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000944 if (FilterCalls) {
Chris Lattner0969c502002-10-21 02:08:03 +0000945 for (unsigned i = 0, e = Calls.size(); i != e; ++i) {
946 for (unsigned j = 0, e = Calls[i].getNumPtrArgs(); j != e; ++j)
947 if (DSNode *N = Calls[i].getPtrArg(j).getNode())
948 GlobalNodes.insert(N);
949 if (DSNode *N = Calls[i].getRetVal().getNode())
950 GlobalNodes.insert(N);
951 if (DSNode *N = Calls[i].getCallee().getNode())
952 GlobalNodes.insert(N);
953 }
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000954 }
955
956 // Iterate and recurse until no new live node are discovered.
957 // This would be a simple iterative loop if function calls were real nodes!
958 markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls);
959
Chris Lattnerc875f022002-11-03 21:27:48 +0000960 // Free up references to dead globals from the ScalarMap
Chris Lattner92673292002-11-02 00:13:20 +0000961 std::set<DSNode*>::iterator I = GlobalNodes.begin(), E = GlobalNodes.end();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000962 for( ; I != E; ++I)
963 if (Alive.count(*I) == 0)
Chris Lattnerc875f022002-11-03 21:27:48 +0000964 removeRefsToGlobal(*I, G.getScalarMap());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000965
966 // Delete dead function calls
967 if (FilterCalls)
968 for (int ei = Calls.size(), i = ei-1; i >= 0; --i) {
969 bool CallIsDead = true;
Chris Lattner0969c502002-10-21 02:08:03 +0000970 for (unsigned j = 0, ej = Calls[i].getNumPtrArgs();
971 CallIsDead && j != ej; ++j)
972 CallIsDead = Alive.count(Calls[i].getPtrArg(j).getNode()) == 0;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000973 if (CallIsDead)
974 Calls.erase(Calls.begin() + i); // remove the call entirely
975 }
976}
Chris Lattnere2219762002-07-18 18:22:40 +0000977
978// removeDeadNodes - Use a more powerful reachability analysis to eliminate
979// subgraphs that are unreachable. This often occurs because the data
980// structure doesn't "escape" into it's caller, and thus should be eliminated
981// from the caller's graph entirely. This is only appropriate to use when
982// inlining graphs.
983//
Chris Lattner65f28972002-11-09 21:00:49 +0000984void DSGraph::removeDeadNodes(bool KeepAllGlobals) {
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 Lattner65f28972002-11-09 21:00:49 +0000994 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
995 for (unsigned j = 0, e = FunctionCalls[i].getNumPtrArgs(); j != e; ++j)
996 markAlive(FunctionCalls[i].getPtrArg(j).getNode(), Alive);
997 markAlive(FunctionCalls[i].getRetVal().getNode(), Alive);
998 markAlive(FunctionCalls[i].getCallee().getNode(), Alive);
999 }
1000 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i) {
1001 for (unsigned j = 0, e = AuxFunctionCalls[i].getNumPtrArgs(); j != e; ++j)
1002 markAlive(AuxFunctionCalls[i].getPtrArg(j).getNode(), Alive);
1003 markAlive(AuxFunctionCalls[i].getRetVal().getNode(), Alive);
1004 markAlive(AuxFunctionCalls[i].getCallee().getNode(), Alive);
Chris Lattneracf491f2002-11-08 22:27:09 +00001005 }
Chris Lattnere2219762002-07-18 18:22:40 +00001006
Chris Lattner92673292002-11-02 00:13:20 +00001007 // Mark all nodes reachable by scalar nodes as alive...
Chris Lattnerc875f022002-11-03 21:27:48 +00001008 for (std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.begin(),
1009 E = ScalarMap.end(); I != E; ++I)
Chris Lattner92673292002-11-02 00:13:20 +00001010 markAlive(I->second.getNode(), Alive);
Chris Lattnere2219762002-07-18 18:22:40 +00001011
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001012 // The return value is alive as well...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001013 markAlive(RetNode.getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001014
1015 // Mark all globals or cast nodes that can reach a live node as alive.
1016 // This also marks all nodes reachable from such nodes as alive.
1017 // Of course, if KeepAllGlobals is specified, they would be live already.
Chris Lattner33312f72002-11-08 01:21:07 +00001018
Chris Lattner0969c502002-10-21 02:08:03 +00001019 if (!KeepAllGlobals)
Chris Lattner65f28972002-11-09 21:00:49 +00001020 markGlobalsAlive(*this, Alive, false);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001021
Chris Lattnere2219762002-07-18 18:22:40 +00001022 // Loop over all unreachable nodes, dropping their references...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001023 vector<DSNode*> DeadNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001024 DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed.
1025 for (unsigned i = 0; i != Nodes.size(); ++i)
1026 if (!Alive.count(Nodes[i])) {
1027 DSNode *N = Nodes[i];
1028 Nodes.erase(Nodes.begin()+i--); // Erase node from alive list.
1029 DeadNodes.push_back(N); // Add node to our list of dead nodes
1030 N->dropAllReferences(); // Drop all outgoing edges
1031 }
1032
Chris Lattnere2219762002-07-18 18:22:40 +00001033 // Delete all dead nodes...
1034 std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
1035}
1036
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001037#if 0
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001038//===----------------------------------------------------------------------===//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001039// GlobalDSGraph Implementation
1040//===----------------------------------------------------------------------===//
1041
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001042#if 0
Chris Lattnerd18f3422002-11-03 21:24:04 +00001043// Bits used in the next function
1044static const char ExternalTypeBits = DSNode::GlobalNode | DSNode::HeapNode;
1045
1046
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001047// GlobalDSGraph::cloneNodeInto - Clone a global node and all its externally
1048// visible target links (and recursively their such links) into this graph.
1049// NodeCache maps the node being cloned to its clone in the Globals graph,
1050// in order to track cycles.
1051// GlobalsAreFinal is a flag that says whether it is safe to assume that
1052// an existing global node is complete. This is important to avoid
1053// reinserting all globals when inserting Calls to functions.
1054// This is a helper function for cloneGlobals and cloneCalls.
1055//
1056DSNode* GlobalDSGraph::cloneNodeInto(DSNode *OldNode,
1057 std::map<const DSNode*, DSNode*> &NodeCache,
1058 bool GlobalsAreFinal) {
1059 if (OldNode == 0) return 0;
1060
1061 // The caller should check this is an external node. Just more efficient...
1062 assert((OldNode->NodeType & ExternalTypeBits) && "Non-external node");
1063
1064 // If a clone has already been created for OldNode, return it.
1065 DSNode*& CacheEntry = NodeCache[OldNode];
1066 if (CacheEntry != 0)
1067 return CacheEntry;
1068
1069 // The result value...
1070 DSNode* NewNode = 0;
1071
1072 // If nodes already exist for any of the globals of OldNode,
1073 // merge all such nodes together since they are merged in OldNode.
1074 // If ValueCacheIsFinal==true, look for an existing node that has
1075 // an identical list of globals and return it if it exists.
1076 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001077 for (unsigned j = 0, N = OldNode->getGlobals().size(); j != N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001078 if (DSNode *PrevNode = ScalarMap[OldNode->getGlobals()[j]].getNode()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001079 if (NewNode == 0) {
1080 NewNode = PrevNode; // first existing node found
1081 if (GlobalsAreFinal && j == 0)
1082 if (OldNode->getGlobals() == PrevNode->getGlobals()) {
1083 CacheEntry = NewNode;
1084 return NewNode;
1085 }
1086 }
1087 else if (NewNode != PrevNode) { // found another, different from prev
1088 // update ValMap *before* merging PrevNode into NewNode
1089 for (unsigned k = 0, NK = PrevNode->getGlobals().size(); k < NK; ++k)
Chris Lattnerc875f022002-11-03 21:27:48 +00001090 ScalarMap[PrevNode->getGlobals()[k]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001091 NewNode->mergeWith(PrevNode);
1092 }
1093 } else if (NewNode != 0) {
Chris Lattnerc875f022002-11-03 21:27:48 +00001094 ScalarMap[OldNode->getGlobals()[j]] = NewNode; // add the merged node
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001095 }
1096
1097 // If no existing node was found, clone the node and update the ValMap.
1098 if (NewNode == 0) {
1099 NewNode = new DSNode(*OldNode);
1100 Nodes.push_back(NewNode);
1101 for (unsigned j = 0, e = NewNode->getNumLinks(); j != e; ++j)
1102 NewNode->setLink(j, 0);
1103 for (unsigned j = 0, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001104 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001105 }
1106 else
1107 NewNode->NodeType |= OldNode->NodeType; // Markers may be different!
1108
1109 // Add the entry to NodeCache
1110 CacheEntry = NewNode;
1111
1112 // Rewrite the links in the new node to point into the current graph,
1113 // but only for links to external nodes. Set other links to NULL.
1114 for (unsigned j = 0, e = OldNode->getNumLinks(); j != e; ++j) {
1115 DSNode* OldTarget = OldNode->getLink(j);
1116 if (OldTarget && (OldTarget->NodeType & ExternalTypeBits)) {
1117 DSNode* NewLink = this->cloneNodeInto(OldTarget, NodeCache);
1118 if (NewNode->getLink(j))
1119 NewNode->getLink(j)->mergeWith(NewLink);
1120 else
1121 NewNode->setLink(j, NewLink);
1122 }
1123 }
1124
1125 // Remove all local markers
1126 NewNode->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode);
1127
1128 return NewNode;
1129}
1130
1131
1132// GlobalDSGraph::cloneGlobals - Clone global nodes and all their externally
1133// visible target links (and recursively their such links) into this graph.
1134//
1135void GlobalDSGraph::cloneGlobals(DSGraph& Graph, bool CloneCalls) {
1136 std::map<const DSNode*, DSNode*> NodeCache;
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001137#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001138 for (unsigned i = 0, N = Graph.Nodes.size(); i < N; ++i)
1139 if (Graph.Nodes[i]->NodeType & DSNode::GlobalNode)
1140 GlobalsGraph->cloneNodeInto(Graph.Nodes[i], NodeCache, false);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001141 if (CloneCalls)
1142 GlobalsGraph->cloneCalls(Graph);
1143
Chris Lattner65f28972002-11-09 21:00:49 +00001144 GlobalsGraph->removeDeadNodes(/*KeepAllGlobals*/ true);
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001145#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001146}
1147
1148
1149// GlobalDSGraph::cloneCalls - Clone function calls and their visible target
1150// links (and recursively their such links) into this graph.
1151//
1152void GlobalDSGraph::cloneCalls(DSGraph& Graph) {
1153 std::map<const DSNode*, DSNode*> NodeCache;
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001154 vector<DSCallSite >& FromCalls =Graph.FunctionCalls;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001155
1156 FunctionCalls.reserve(FunctionCalls.size() + FromCalls.size());
1157
1158 for (int i = 0, ei = FromCalls.size(); i < ei; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001159 DSCallSite& callCopy = FunctionCalls.back();
1160 callCopy.reserve(FromCalls[i].size());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001161 for (unsigned j = 0, ej = FromCalls[i].size(); j != ej; ++j)
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001162 callCopy.push_back
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001163 ((FromCalls[i][j] && (FromCalls[i][j]->NodeType & ExternalTypeBits))
1164 ? cloneNodeInto(FromCalls[i][j], NodeCache, true)
1165 : 0);
1166 }
1167
1168 // remove trivially identical function calls
Chris Lattner7541b892002-07-31 19:32:12 +00001169 removeIdenticalCalls(FunctionCalls, "Globals Graph");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001170}
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001171#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001172
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001173#endif