blob: d26213fc4824f6b2218c77a18e5c94d314f79aee [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 Lattner18552922002-11-18 21:44:46 +000014#include "Support/Timer.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000015#include <algorithm>
Chris Lattnerfccd06f2002-10-01 22:33:50 +000016#include <set>
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017
Chris Lattnere2219762002-07-18 18:22:40 +000018using std::vector;
19
Chris Lattner08db7192002-11-06 06:20:27 +000020namespace {
Chris Lattner33312f72002-11-08 01:21:07 +000021 Statistic<> NumFolds ("dsnode", "Number of nodes completely folded");
22 Statistic<> NumCallNodesMerged("dsnode", "Number of call nodes merged");
Chris Lattner08db7192002-11-06 06:20:27 +000023};
24
Chris Lattnerb1060432002-11-07 05:20:53 +000025namespace DS { // TODO: FIXME
Chris Lattnerfccd06f2002-10-01 22:33:50 +000026 extern TargetData TD;
27}
Chris Lattnerb1060432002-11-07 05:20:53 +000028using namespace DS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000029
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000030//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +000031// DSNode Implementation
32//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000033
Chris Lattner08db7192002-11-06 06:20:27 +000034DSNode::DSNode(enum NodeTy NT, const Type *T)
35 : Ty(Type::VoidTy), Size(0), NodeType(NT) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +000036 // Add the type entry if it is specified...
Chris Lattner08db7192002-11-06 06:20:27 +000037 if (T) mergeTypeInfo(T, 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000038}
39
Chris Lattner0d9bab82002-07-18 00:12:30 +000040// DSNode copy constructor... do not copy over the referrers list!
41DSNode::DSNode(const DSNode &N)
Chris Lattner08db7192002-11-06 06:20:27 +000042 : Links(N.Links), Globals(N.Globals), Ty(N.Ty), Size(N.Size),
43 NodeType(N.NodeType) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000044}
45
Chris Lattnerc68c31b2002-07-10 22:38:08 +000046void DSNode::removeReferrer(DSNodeHandle *H) {
47 // Search backwards, because we depopulate the list from the back for
48 // efficiency (because it's a vector).
Chris Lattnere2219762002-07-18 18:22:40 +000049 vector<DSNodeHandle*>::reverse_iterator I =
Chris Lattnerc68c31b2002-07-10 22:38:08 +000050 std::find(Referrers.rbegin(), Referrers.rend(), H);
51 assert(I != Referrers.rend() && "Referrer not pointing to node!");
52 Referrers.erase(I.base()-1);
53}
54
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000055// addGlobal - Add an entry for a global value to the Globals list. This also
56// marks the node with the 'G' flag if it does not already have it.
57//
58void DSNode::addGlobal(GlobalValue *GV) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000059 // Keep the list sorted.
Chris Lattnere2219762002-07-18 18:22:40 +000060 vector<GlobalValue*>::iterator I =
Chris Lattner0d9bab82002-07-18 00:12:30 +000061 std::lower_bound(Globals.begin(), Globals.end(), GV);
62
63 if (I == Globals.end() || *I != GV) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000064 //assert(GV->getType()->getElementType() == Ty);
Chris Lattner0d9bab82002-07-18 00:12:30 +000065 Globals.insert(I, GV);
66 NodeType |= GlobalNode;
67 }
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000068}
69
Chris Lattner8f0a16e2002-10-31 05:45:02 +000070/// foldNodeCompletely - If we determine that this node has some funny
71/// behavior happening to it that we cannot represent, we fold it down to a
72/// single, completely pessimistic, node. This node is represented as a
73/// single byte with a single TypeEntry of "void".
74///
75void DSNode::foldNodeCompletely() {
Chris Lattner08db7192002-11-06 06:20:27 +000076 if (isNodeCompletelyFolded()) return;
Chris Lattner8f0a16e2002-10-31 05:45:02 +000077
Chris Lattner08db7192002-11-06 06:20:27 +000078 ++NumFolds;
79
80 // We are no longer typed at all...
Chris Lattner18552922002-11-18 21:44:46 +000081 Ty = Type::VoidTy;
82 NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +000083 Size = 1;
84
85 // Loop over all of our referrers, making them point to our zero bytes of
86 // space.
Chris Lattner8f0a16e2002-10-31 05:45:02 +000087 for (vector<DSNodeHandle*>::iterator I = Referrers.begin(), E=Referrers.end();
88 I != E; ++I)
89 (*I)->setOffset(0);
90
Chris Lattner8f0a16e2002-10-31 05:45:02 +000091 // If we have links, merge all of our outgoing links together...
Chris Lattner08db7192002-11-06 06:20:27 +000092 for (unsigned i = 1, e = Links.size(); i < e; ++i)
93 Links[0].mergeWith(Links[i]);
94 Links.resize(1);
Chris Lattner8f0a16e2002-10-31 05:45:02 +000095}
Chris Lattner076c1f92002-11-07 06:31:54 +000096
Chris Lattner8f0a16e2002-10-31 05:45:02 +000097/// isNodeCompletelyFolded - Return true if this node has been completely
98/// folded down to something that can never be expanded, effectively losing
99/// all of the field sensitivity that may be present in the node.
100///
101bool DSNode::isNodeCompletelyFolded() const {
Chris Lattner18552922002-11-18 21:44:46 +0000102 return getSize() == 1 && Ty == Type::VoidTy && isArray();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000103}
104
105
Chris Lattner08db7192002-11-06 06:20:27 +0000106/// mergeTypeInfo - This method merges the specified type into the current node
107/// at the specified offset. This may update the current node's type record if
108/// this gives more information to the node, it may do nothing to the node if
109/// this information is already known, or it may merge the node completely (and
110/// return true) if the information is incompatible with what is already known.
Chris Lattner7b7200c2002-10-02 04:57:39 +0000111///
Chris Lattner08db7192002-11-06 06:20:27 +0000112/// This method returns true if the node is completely folded, otherwise false.
113///
114bool DSNode::mergeTypeInfo(const Type *NewTy, unsigned Offset) {
115 // Check to make sure the Size member is up-to-date. Size can be one of the
116 // following:
117 // Size = 0, Ty = Void: Nothing is known about this node.
118 // Size = 0, Ty = FnTy: FunctionPtr doesn't have a size, so we use zero
119 // Size = 1, Ty = Void, Array = 1: The node is collapsed
120 // Otherwise, sizeof(Ty) = Size
121 //
Chris Lattner18552922002-11-18 21:44:46 +0000122 assert(((Size == 0 && Ty == Type::VoidTy && !isArray()) ||
123 (Size == 0 && !Ty->isSized() && !isArray()) ||
124 (Size == 1 && Ty == Type::VoidTy && isArray()) ||
125 (Size == 0 && !Ty->isSized() && !isArray()) ||
126 (TD.getTypeSize(Ty) == Size)) &&
Chris Lattner08db7192002-11-06 06:20:27 +0000127 "Size member of DSNode doesn't match the type structure!");
128 assert(NewTy != Type::VoidTy && "Cannot merge void type into DSNode!");
Chris Lattner7b7200c2002-10-02 04:57:39 +0000129
Chris Lattner18552922002-11-18 21:44:46 +0000130 if (Offset == 0 && NewTy == Ty)
Chris Lattner08db7192002-11-06 06:20:27 +0000131 return false; // This should be a common case, handle it efficiently
Chris Lattner7b7200c2002-10-02 04:57:39 +0000132
Chris Lattner08db7192002-11-06 06:20:27 +0000133 // Return true immediately if the node is completely folded.
134 if (isNodeCompletelyFolded()) return true;
135
Chris Lattner23f83dc2002-11-08 22:49:57 +0000136 // If this is an array type, eliminate the outside arrays because they won't
137 // be used anyway. This greatly reduces the size of large static arrays used
138 // as global variables, for example.
139 //
Chris Lattnerd8888932002-11-09 19:25:27 +0000140 bool WillBeArray = false;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000141 while (const ArrayType *AT = dyn_cast<ArrayType>(NewTy)) {
142 // FIXME: we might want to keep small arrays, but must be careful about
143 // things like: [2 x [10000 x int*]]
144 NewTy = AT->getElementType();
Chris Lattnerd8888932002-11-09 19:25:27 +0000145 WillBeArray = true;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000146 }
147
Chris Lattner08db7192002-11-06 06:20:27 +0000148 // Figure out how big the new type we're merging in is...
149 unsigned NewTySize = NewTy->isSized() ? TD.getTypeSize(NewTy) : 0;
150
151 // Otherwise check to see if we can fold this type into the current node. If
152 // we can't, we fold the node completely, if we can, we potentially update our
153 // internal state.
154 //
Chris Lattner18552922002-11-18 21:44:46 +0000155 if (Ty == Type::VoidTy) {
Chris Lattner08db7192002-11-06 06:20:27 +0000156 // If this is the first type that this node has seen, just accept it without
157 // question....
158 assert(Offset == 0 && "Cannot have an offset into a void node!");
Chris Lattner18552922002-11-18 21:44:46 +0000159 assert(!isArray() && "This shouldn't happen!");
160 Ty = NewTy;
161 NodeType &= ~Array;
162 if (WillBeArray) NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +0000163 Size = NewTySize;
164
165 // Calculate the number of outgoing links from this node.
166 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
167 return false;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000168 }
Chris Lattner08db7192002-11-06 06:20:27 +0000169
170 // Handle node expansion case here...
171 if (Offset+NewTySize > Size) {
172 // It is illegal to grow this node if we have treated it as an array of
173 // objects...
Chris Lattner18552922002-11-18 21:44:46 +0000174 if (isArray()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000175 foldNodeCompletely();
176 return true;
177 }
178
179 if (Offset) { // We could handle this case, but we don't for now...
Chris Lattner3c87b292002-11-07 01:54:56 +0000180 DEBUG(std::cerr << "UNIMP: Trying to merge a growth type into "
181 << "offset != 0: Collapsing!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000182 foldNodeCompletely();
183 return true;
184 }
185
186 // Okay, the situation is nice and simple, we are trying to merge a type in
187 // at offset 0 that is bigger than our current type. Implement this by
188 // switching to the new type and then merge in the smaller one, which should
189 // hit the other code path here. If the other code path decides it's not
190 // ok, it will collapse the node as appropriate.
191 //
Chris Lattner18552922002-11-18 21:44:46 +0000192 const Type *OldTy = Ty;
193 Ty = NewTy;
194 NodeType &= ~Array;
195 if (WillBeArray) NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +0000196 Size = NewTySize;
197
198 // Must grow links to be the appropriate size...
199 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
200
201 // Merge in the old type now... which is guaranteed to be smaller than the
202 // "current" type.
203 return mergeTypeInfo(OldTy, 0);
204 }
205
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000206 assert(Offset <= Size &&
Chris Lattner08db7192002-11-06 06:20:27 +0000207 "Cannot merge something into a part of our type that doesn't exist!");
208
Chris Lattner18552922002-11-18 21:44:46 +0000209 // Find the section of Ty that NewTy overlaps with... first we find the
Chris Lattner08db7192002-11-06 06:20:27 +0000210 // type that starts at offset Offset.
211 //
212 unsigned O = 0;
Chris Lattner18552922002-11-18 21:44:46 +0000213 const Type *SubType = Ty;
Chris Lattner08db7192002-11-06 06:20:27 +0000214 while (O < Offset) {
215 assert(Offset-O < TD.getTypeSize(SubType) && "Offset out of range!");
216
217 switch (SubType->getPrimitiveID()) {
218 case Type::StructTyID: {
219 const StructType *STy = cast<StructType>(SubType);
220 const StructLayout &SL = *TD.getStructLayout(STy);
221
222 unsigned i = 0, e = SL.MemberOffsets.size();
223 for (; i+1 < e && SL.MemberOffsets[i+1] <= Offset-O; ++i)
224 /* empty */;
225
226 // The offset we are looking for must be in the i'th element...
227 SubType = STy->getElementTypes()[i];
228 O += SL.MemberOffsets[i];
229 break;
230 }
231 case Type::ArrayTyID: {
232 SubType = cast<ArrayType>(SubType)->getElementType();
233 unsigned ElSize = TD.getTypeSize(SubType);
234 unsigned Remainder = (Offset-O) % ElSize;
235 O = Offset-Remainder;
236 break;
237 }
238 default:
239 assert(0 && "Unknown type!");
240 }
241 }
242
243 assert(O == Offset && "Could not achieve the correct offset!");
244
245 // If we found our type exactly, early exit
246 if (SubType == NewTy) return false;
247
248 // Okay, so we found the leader type at the offset requested. Search the list
249 // of types that starts at this offset. If SubType is currently an array or
250 // structure, the type desired may actually be the first element of the
251 // composite type...
252 //
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000253 unsigned SubTypeSize = SubType->isSized() ? TD.getTypeSize(SubType) : 0;
Chris Lattner18552922002-11-18 21:44:46 +0000254 unsigned PadSize = SubTypeSize; // Size, including pad memory which is ignored
Chris Lattner08db7192002-11-06 06:20:27 +0000255 while (SubType != NewTy) {
256 const Type *NextSubType = 0;
Chris Lattnerbf10f052002-11-09 00:49:05 +0000257 unsigned NextSubTypeSize = 0;
Chris Lattner18552922002-11-18 21:44:46 +0000258 unsigned NextPadSize = 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000259 switch (SubType->getPrimitiveID()) {
Chris Lattner18552922002-11-18 21:44:46 +0000260 case Type::StructTyID: {
261 const StructType *STy = cast<StructType>(SubType);
262 const StructLayout &SL = *TD.getStructLayout(STy);
263 if (SL.MemberOffsets.size() > 1)
264 NextPadSize = SL.MemberOffsets[1];
265 else
266 NextPadSize = SubTypeSize;
267 NextSubType = STy->getElementTypes()[0];
268 NextSubTypeSize = TD.getTypeSize(NextSubType);
Chris Lattner08db7192002-11-06 06:20:27 +0000269 break;
Chris Lattner18552922002-11-18 21:44:46 +0000270 }
Chris Lattner08db7192002-11-06 06:20:27 +0000271 case Type::ArrayTyID:
272 NextSubType = cast<ArrayType>(SubType)->getElementType();
Chris Lattner18552922002-11-18 21:44:46 +0000273 NextSubTypeSize = TD.getTypeSize(NextSubType);
274 NextPadSize = NextSubTypeSize;
Chris Lattner08db7192002-11-06 06:20:27 +0000275 break;
276 default: ;
277 // fall out
278 }
279
280 if (NextSubType == 0)
281 break; // In the default case, break out of the loop
282
Chris Lattner18552922002-11-18 21:44:46 +0000283 if (NextPadSize < NewTySize)
Chris Lattner08db7192002-11-06 06:20:27 +0000284 break; // Don't allow shrinking to a smaller type than NewTySize
285 SubType = NextSubType;
286 SubTypeSize = NextSubTypeSize;
Chris Lattner18552922002-11-18 21:44:46 +0000287 PadSize = NextPadSize;
Chris Lattner08db7192002-11-06 06:20:27 +0000288 }
289
290 // If we found the type exactly, return it...
291 if (SubType == NewTy)
292 return false;
293
294 // Check to see if we have a compatible, but different type...
295 if (NewTySize == SubTypeSize) {
296 // Check to see if this type is obviously convertable... int -> uint f.e.
297 if (NewTy->isLosslesslyConvertableTo(SubType))
298 return false;
299
300 // Check to see if we have a pointer & integer mismatch going on here,
301 // loading a pointer as a long, for example.
302 //
303 if (SubType->isInteger() && isa<PointerType>(NewTy) ||
304 NewTy->isInteger() && isa<PointerType>(SubType))
305 return false;
Chris Lattner18552922002-11-18 21:44:46 +0000306 } else if (NewTySize > SubTypeSize && NewTySize <= PadSize) {
307 // We are accessing the field, plus some structure padding. Ignore the
308 // structure padding.
309 return false;
Chris Lattner08db7192002-11-06 06:20:27 +0000310 }
311
312
Chris Lattner18552922002-11-18 21:44:46 +0000313 DEBUG(std::cerr << "MergeTypeInfo Folding OrigTy: " << Ty
Chris Lattner3c87b292002-11-07 01:54:56 +0000314 << "\n due to:" << NewTy << " @ " << Offset << "!\n"
315 << "SubType: " << SubType << "\n\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000316
317 foldNodeCompletely();
318 return true;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000319}
320
Chris Lattner08db7192002-11-06 06:20:27 +0000321
322
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000323// addEdgeTo - Add an edge from the current node to the specified node. This
324// can cause merging of nodes in the graph.
325//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000326void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000327 if (NH.getNode() == 0) return; // Nothing to do
328
Chris Lattner08db7192002-11-06 06:20:27 +0000329 DSNodeHandle &ExistingEdge = getLink(Offset);
330 if (ExistingEdge.getNode()) {
Chris Lattner7b7200c2002-10-02 04:57:39 +0000331 // Merge the two nodes...
Chris Lattner08db7192002-11-06 06:20:27 +0000332 ExistingEdge.mergeWith(NH);
Chris Lattner7b7200c2002-10-02 04:57:39 +0000333 } else { // No merging to perform...
334 setLink(Offset, NH); // Just force a link in there...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000335 }
Chris Lattner7b7200c2002-10-02 04:57:39 +0000336}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000337
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000338
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000339// MergeSortedVectors - Efficiently merge a vector into another vector where
340// duplicates are not allowed and both are sorted. This assumes that 'T's are
341// efficiently copyable and have sane comparison semantics.
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000342//
Chris Lattner18552922002-11-18 21:44:46 +0000343static void MergeSortedVectors(vector<GlobalValue*> &Dest,
344 const vector<GlobalValue*> &Src) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000345 // By far, the most common cases will be the simple ones. In these cases,
346 // avoid having to allocate a temporary vector...
347 //
348 if (Src.empty()) { // Nothing to merge in...
349 return;
350 } else if (Dest.empty()) { // Just copy the result in...
351 Dest = Src;
352 } else if (Src.size() == 1) { // Insert a single element...
Chris Lattner18552922002-11-18 21:44:46 +0000353 const GlobalValue *V = Src[0];
354 vector<GlobalValue*>::iterator I =
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000355 std::lower_bound(Dest.begin(), Dest.end(), V);
356 if (I == Dest.end() || *I != Src[0]) // If not already contained...
357 Dest.insert(I, Src[0]);
358 } else if (Dest.size() == 1) {
Chris Lattner18552922002-11-18 21:44:46 +0000359 GlobalValue *Tmp = Dest[0]; // Save value in temporary...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000360 Dest = Src; // Copy over list...
Chris Lattner18552922002-11-18 21:44:46 +0000361 vector<GlobalValue*>::iterator I =
Chris Lattner5190ce82002-11-12 07:20:45 +0000362 std::lower_bound(Dest.begin(), Dest.end(), Tmp);
363 if (I == Dest.end() || *I != Tmp) // If not already contained...
364 Dest.insert(I, Tmp);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000365
366 } else {
367 // Make a copy to the side of Dest...
Chris Lattner18552922002-11-18 21:44:46 +0000368 vector<GlobalValue*> Old(Dest);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000369
370 // Make space for all of the type entries now...
371 Dest.resize(Dest.size()+Src.size());
372
373 // Merge the two sorted ranges together... into Dest.
374 std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin());
375
376 // Now erase any duplicate entries that may have accumulated into the
377 // vectors (because they were in both of the input sets)
378 Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end());
379 }
380}
381
382
383// mergeWith - Merge this node and the specified node, moving all links to and
384// from the argument node into the current node, deleting the node argument.
385// Offset indicates what offset the specified node is to be merged into the
386// current node.
387//
388// The specified node may be a null pointer (in which case, nothing happens).
389//
390void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
391 DSNode *N = NH.getNode();
392 if (N == 0 || (N == this && NH.getOffset() == Offset))
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000393 return; // Noop
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000394
Chris Lattner679e8e12002-11-08 21:27:12 +0000395 assert((N->NodeType & DSNode::DEAD) == 0);
396 assert((NodeType & DSNode::DEAD) == 0);
397 assert(!hasNoReferrers() && "Should not try to fold a useless node!");
398
Chris Lattner02606632002-11-04 06:48:26 +0000399 if (N == this) {
Chris Lattner08db7192002-11-06 06:20:27 +0000400 // We cannot merge two pieces of the same node together, collapse the node
401 // completely.
Chris Lattner3c87b292002-11-07 01:54:56 +0000402 DEBUG(std::cerr << "Attempting to merge two chunks of"
403 << " the same node together!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000404 foldNodeCompletely();
Chris Lattner02606632002-11-04 06:48:26 +0000405 return;
406 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000407
Chris Lattner5190ce82002-11-12 07:20:45 +0000408 // If both nodes are not at offset 0, make sure that we are merging the node
409 // at an later offset into the node with the zero offset.
410 //
411 if (Offset < NH.getOffset()) {
412 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
413 return;
414 } else if (Offset == NH.getOffset() && getSize() < N->getSize()) {
415 // If the offsets are the same, merge the smaller node into the bigger node
416 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
417 return;
418 }
419
420 // Now we know that Offset >= NH.Offset, so convert it so our "Offset" (with
421 // respect to NH.Offset) is now zero. NOffset is the distance from the base
422 // of our object that N starts from.
423 //
424 unsigned NOffset = Offset-NH.getOffset();
425 unsigned NSize = N->getSize();
426
Chris Lattner08db7192002-11-06 06:20:27 +0000427 // Merge the type entries of the two nodes together...
Chris Lattner18552922002-11-18 21:44:46 +0000428 if (N->Ty != Type::VoidTy) {
429 mergeTypeInfo(N->Ty, NOffset);
Chris Lattner08db7192002-11-06 06:20:27 +0000430
Chris Lattner679e8e12002-11-08 21:27:12 +0000431 // mergeTypeInfo can cause collapsing, which can cause this node to become
432 // dead.
433 if (hasNoReferrers()) return;
434 }
435 assert((NodeType & DSNode::DEAD) == 0);
436
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000437 // If we are merging a node with a completely folded node, then both nodes are
438 // now completely folded.
439 //
440 if (isNodeCompletelyFolded()) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000441 if (!N->isNodeCompletelyFolded()) {
Chris Lattner02606632002-11-04 06:48:26 +0000442 N->foldNodeCompletely();
Chris Lattner679e8e12002-11-08 21:27:12 +0000443 if (hasNoReferrers()) return;
Chris Lattner18552922002-11-18 21:44:46 +0000444 NSize = N->getSize();
Chris Lattner679e8e12002-11-08 21:27:12 +0000445 }
Chris Lattner02606632002-11-04 06:48:26 +0000446 } else if (N->isNodeCompletelyFolded()) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000447 foldNodeCompletely();
Chris Lattner679e8e12002-11-08 21:27:12 +0000448 if (hasNoReferrers()) return;
Chris Lattner5190ce82002-11-12 07:20:45 +0000449 Offset = 0;
450 NOffset = NH.getOffset();
Chris Lattner18552922002-11-18 21:44:46 +0000451 NSize = N->getSize();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000452 }
Chris Lattner02606632002-11-04 06:48:26 +0000453 N = NH.getNode();
Chris Lattner2c0bd012002-11-06 18:01:39 +0000454 if (this == N || N == 0) return;
Chris Lattner679e8e12002-11-08 21:27:12 +0000455 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000456
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000457#if 0
458 std::cerr << "\n\nMerging:\n";
459 N->print(std::cerr, 0);
460 std::cerr << " and:\n";
461 print(std::cerr, 0);
462#endif
463
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000464 // Remove all edges pointing at N, causing them to point to 'this' instead.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000465 // Make sure to adjust their offset, not just the node pointer.
466 //
467 while (!N->Referrers.empty()) {
468 DSNodeHandle &Ref = *N->Referrers.back();
469 Ref = DSNodeHandle(this, NOffset+Ref.getOffset());
470 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000471 assert((NodeType & DSNode::DEAD) == 0);
Chris Lattner59535132002-11-05 00:01:58 +0000472
473 // Make all of the outgoing links of N now be outgoing links of this. This
474 // can cause recursive merging!
475 //
Chris Lattner08db7192002-11-06 06:20:27 +0000476 for (unsigned i = 0; i < NSize; i += DS::PointerSize) {
477 DSNodeHandle &Link = N->getLink(i);
478 if (Link.getNode()) {
479 addEdgeTo((i+NOffset) % getSize(), Link);
Chris Lattner59535132002-11-05 00:01:58 +0000480
Chris Lattner08db7192002-11-06 06:20:27 +0000481 // It's possible that after adding the new edge that some recursive
482 // merging just occured, causing THIS node to get merged into oblivion.
483 // If that happens, we must not try to merge any more edges into it!
Chris Lattner7b7200c2002-10-02 04:57:39 +0000484 //
Chris Lattner18552922002-11-18 21:44:46 +0000485 if (Size == 0)
486 return; // Node is now dead
487 if (Size == 1)
488 break; // Node got collapsed
Chris Lattner7b7200c2002-10-02 04:57:39 +0000489 }
490 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000491
492 // Now that there are no outgoing edges, all of the Links are dead.
493 N->Links.clear();
Chris Lattner08db7192002-11-06 06:20:27 +0000494 N->Size = 0;
Chris Lattner18552922002-11-18 21:44:46 +0000495 N->Ty = Type::VoidTy;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000496
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000497 // Merge the node types
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000498 NodeType |= N->NodeType;
Chris Lattner33312f72002-11-08 01:21:07 +0000499 N->NodeType = DEAD; // N is now a dead node.
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000500
501 // Merge the globals list...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000502 if (!N->Globals.empty()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000503 MergeSortedVectors(Globals, N->Globals);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000504
505 // Delete the globals from the old node...
506 N->Globals.clear();
507 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000508}
509
Chris Lattner9de906c2002-10-20 22:11:44 +0000510//===----------------------------------------------------------------------===//
511// DSCallSite Implementation
512//===----------------------------------------------------------------------===//
513
Vikram S. Adve26b98262002-10-20 21:41:02 +0000514// Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h
Chris Lattner9de906c2002-10-20 22:11:44 +0000515Function &DSCallSite::getCaller() const {
Chris Lattner0969c502002-10-21 02:08:03 +0000516 return *Inst->getParent()->getParent();
Vikram S. Adve26b98262002-10-20 21:41:02 +0000517}
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000518
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000519
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000520//===----------------------------------------------------------------------===//
521// DSGraph Implementation
522//===----------------------------------------------------------------------===//
523
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000524DSGraph::DSGraph(const DSGraph &G) : Func(G.Func), GlobalsGraph(0) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000525 PrintAuxCalls = false;
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000526 std::map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattnerc875f022002-11-03 21:27:48 +0000527 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000528}
529
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000530DSGraph::DSGraph(const DSGraph &G,
531 std::map<const DSNode*, DSNodeHandle> &NodeMap)
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000532 : Func(G.Func), GlobalsGraph(0) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000533 PrintAuxCalls = false;
Chris Lattnerc875f022002-11-03 21:27:48 +0000534 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattnereff0da92002-10-21 15:32:34 +0000535}
536
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000537DSGraph::~DSGraph() {
538 FunctionCalls.clear();
Chris Lattner679e8e12002-11-08 21:27:12 +0000539 AuxFunctionCalls.clear();
Chris Lattnerc875f022002-11-03 21:27:48 +0000540 ScalarMap.clear();
Chris Lattner13ec72a2002-10-21 13:31:48 +0000541 RetNode.setNode(0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000542
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000543 // Drop all intra-node references, so that assertions don't fail...
544 std::for_each(Nodes.begin(), Nodes.end(),
545 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000546
547 // Delete all of the nodes themselves...
548 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
549}
550
Chris Lattner0d9bab82002-07-18 00:12:30 +0000551// dump - Allow inspection of graph in a debugger.
552void DSGraph::dump() const { print(std::cerr); }
553
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000554
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000555/// remapLinks - Change all of the Links in the current node according to the
556/// specified mapping.
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000557///
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000558void DSNode::remapLinks(std::map<const DSNode*, DSNodeHandle> &OldNodeMap) {
559 for (unsigned i = 0, e = Links.size(); i != e; ++i) {
560 DSNodeHandle &H = OldNodeMap[Links[i].getNode()];
561 Links[i].setNode(H.getNode());
562 Links[i].setOffset(Links[i].getOffset()+H.getOffset());
563 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000564}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000565
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000566
Chris Lattner0d9bab82002-07-18 00:12:30 +0000567// cloneInto - Clone the specified DSGraph into the current graph, returning the
Chris Lattnerc875f022002-11-03 21:27:48 +0000568// Return node of the graph. The translated ScalarMap for the old function is
Chris Lattner92673292002-11-02 00:13:20 +0000569// filled into the OldValMap member. If StripAllocas is set to true, Alloca
570// markers are removed from the graph, as the graph is being cloned into a
571// calling function's graph.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000572//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000573DSNodeHandle DSGraph::cloneInto(const DSGraph &G,
574 std::map<Value*, DSNodeHandle> &OldValMap,
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000575 std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
Chris Lattner679e8e12002-11-08 21:27:12 +0000576 unsigned CloneFlags) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000577 assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
Chris Lattner33312f72002-11-08 01:21:07 +0000578 assert(&G != this && "Cannot clone graph into itself!");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000579
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000580 unsigned FN = Nodes.size(); // First new node...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000581
582 // Duplicate all of the nodes, populating the node map...
583 Nodes.reserve(FN+G.Nodes.size());
584 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000585 DSNode *Old = G.Nodes[i];
586 DSNode *New = new DSNode(*Old);
Chris Lattner679e8e12002-11-08 21:27:12 +0000587 New->NodeType &= ~DSNode::DEAD; // Clear dead flag...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000588 Nodes.push_back(New);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000589 OldNodeMap[Old] = New;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000590 }
591
Chris Lattner18552922002-11-18 21:44:46 +0000592#ifndef NDEBUG
593 Timer::addPeakMemoryMeasurement();
594#endif
595
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000596 // Rewrite the links in the new nodes to point into the current graph now.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000597 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000598 Nodes[i]->remapLinks(OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000599
Chris Lattner460ea292002-11-07 07:06:20 +0000600 // Remove alloca markers as specified
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000601 if (CloneFlags & (StripAllocaBit | StripModRefBits)) {
602 unsigned short clearBits = (CloneFlags & StripAllocaBit
603 ? DSNode::AllocaNode : 0)
604 | (CloneFlags & StripModRefBits
605 ? (DSNode::Modified | DSNode::Read) : 0);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000606 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000607 Nodes[i]->NodeType &= ~clearBits;
608 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000609
Chris Lattnercf15db32002-10-17 20:09:52 +0000610 // Copy the value map... and merge all of the global nodes...
Chris Lattnerc875f022002-11-03 21:27:48 +0000611 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ScalarMap.begin(),
612 E = G.ScalarMap.end(); I != E; ++I) {
Chris Lattnercf15db32002-10-17 20:09:52 +0000613 DSNodeHandle &H = OldValMap[I->first];
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000614 DSNodeHandle &MappedNode = OldNodeMap[I->second.getNode()];
615 H.setNode(MappedNode.getNode());
616 H.setOffset(I->second.getOffset()+MappedNode.getOffset());
Chris Lattnercf15db32002-10-17 20:09:52 +0000617
618 if (isa<GlobalValue>(I->first)) { // Is this a global?
Chris Lattnerc875f022002-11-03 21:27:48 +0000619 std::map<Value*, DSNodeHandle>::iterator GVI = ScalarMap.find(I->first);
620 if (GVI != ScalarMap.end()) { // Is the global value in this fn already?
Chris Lattnercf15db32002-10-17 20:09:52 +0000621 GVI->second.mergeWith(H);
622 } else {
Chris Lattnerc875f022002-11-03 21:27:48 +0000623 ScalarMap[I->first] = H; // Add global pointer to this graph
Chris Lattnercf15db32002-10-17 20:09:52 +0000624 }
625 }
626 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000627
Chris Lattner679e8e12002-11-08 21:27:12 +0000628 if (!(CloneFlags & DontCloneCallNodes)) {
629 // Copy the function calls list...
630 unsigned FC = FunctionCalls.size(); // FirstCall
631 FunctionCalls.reserve(FC+G.FunctionCalls.size());
632 for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i)
633 FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap));
Chris Lattneracf491f2002-11-08 22:27:09 +0000634 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000635
Chris Lattneracf491f2002-11-08 22:27:09 +0000636 if (!(CloneFlags & DontCloneAuxCallNodes)) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000637 // Copy the auxillary function calls list...
Chris Lattneracf491f2002-11-08 22:27:09 +0000638 unsigned FC = AuxFunctionCalls.size(); // FirstCall
Chris Lattner679e8e12002-11-08 21:27:12 +0000639 AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size());
640 for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i)
641 AuxFunctionCalls.push_back(DSCallSite(G.AuxFunctionCalls[i], OldNodeMap));
642 }
Chris Lattnercf15db32002-10-17 20:09:52 +0000643
Chris Lattner0d9bab82002-07-18 00:12:30 +0000644 // Return the returned node pointer...
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000645 DSNodeHandle &MappedRet = OldNodeMap[G.RetNode.getNode()];
646 return DSNodeHandle(MappedRet.getNode(),
647 MappedRet.getOffset()+G.RetNode.getOffset());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000648}
649
Chris Lattner076c1f92002-11-07 06:31:54 +0000650/// mergeInGraph - The method is used for merging graphs together. If the
651/// argument graph is not *this, it makes a clone of the specified graph, then
652/// merges the nodes specified in the call site with the formal arguments in the
653/// graph.
654///
655void DSGraph::mergeInGraph(DSCallSite &CS, const DSGraph &Graph,
Chris Lattner679e8e12002-11-08 21:27:12 +0000656 unsigned CloneFlags) {
Chris Lattner076c1f92002-11-07 06:31:54 +0000657 std::map<Value*, DSNodeHandle> OldValMap;
658 DSNodeHandle RetVal;
659 std::map<Value*, DSNodeHandle> *ScalarMap = &OldValMap;
660
661 // If this is not a recursive call, clone the graph into this graph...
662 if (&Graph != this) {
663 // Clone the callee's graph into the current graph, keeping
664 // track of where scalars in the old graph _used_ to point,
665 // and of the new nodes matching nodes of the old graph.
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000666 std::map<const DSNode*, DSNodeHandle> OldNodeMap;
Chris Lattner076c1f92002-11-07 06:31:54 +0000667
668 // The clone call may invalidate any of the vectors in the data
669 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner679e8e12002-11-08 21:27:12 +0000670 RetVal = cloneInto(Graph, OldValMap, OldNodeMap, CloneFlags);
Chris Lattner076c1f92002-11-07 06:31:54 +0000671 ScalarMap = &OldValMap;
672 } else {
673 RetVal = getRetNode();
674 ScalarMap = &getScalarMap();
675 }
676
677 // Merge the return value with the return value of the context...
678 RetVal.mergeWith(CS.getRetVal());
679
680 // Resolve all of the function arguments...
681 Function &F = Graph.getFunction();
682 Function::aiterator AI = F.abegin();
683 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) {
684 // Advance the argument iterator to the first pointer argument...
685 while (!isPointerType(AI->getType())) {
686 ++AI;
687#ifndef NDEBUG
688 if (AI == F.aend())
689 std::cerr << "Bad call to Function: " << F.getName() << "\n";
690#endif
691 assert(AI != F.aend() && "# Args provided is not # Args required!");
692 }
693
694 // Add the link from the argument scalar to the provided value
695 DSNodeHandle &NH = (*ScalarMap)[AI];
696 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
697 NH.mergeWith(CS.getPtrArg(i));
698 }
699}
700
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000701#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000702// cloneGlobalInto - Clone the given global node and all its target links
703// (and all their llinks, recursively).
704//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000705DSNode *DSGraph::cloneGlobalInto(const DSNode *GNode) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000706 if (GNode == 0 || GNode->getGlobals().size() == 0) return 0;
707
708 // If a clone has already been created for GNode, return it.
Chris Lattnerc875f022002-11-03 21:27:48 +0000709 DSNodeHandle& ValMapEntry = ScalarMap[GNode->getGlobals()[0]];
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000710 if (ValMapEntry != 0)
711 return ValMapEntry;
712
713 // Clone the node and update the ValMap.
714 DSNode* NewNode = new DSNode(*GNode);
715 ValMapEntry = NewNode; // j=0 case of loop below!
716 Nodes.push_back(NewNode);
717 for (unsigned j = 1, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +0000718 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000719
720 // Rewrite the links in the new node to point into the current graph.
721 for (unsigned j = 0, e = GNode->getNumLinks(); j != e; ++j)
722 NewNode->setLink(j, cloneGlobalInto(GNode->getLink(j)));
723
724 return NewNode;
725}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000726#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000727
728
Chris Lattner0d9bab82002-07-18 00:12:30 +0000729// markIncompleteNodes - Mark the specified node as having contents that are not
730// known with the current analysis we have performed. Because a node makes all
731// of the nodes it can reach imcomplete if the node itself is incomplete, we
732// must recursively traverse the data structure graph, marking all reachable
733// nodes as incomplete.
734//
735static void markIncompleteNode(DSNode *N) {
736 // Stop recursion if no node, or if node already marked...
737 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
738
739 // Actually mark the node
740 N->NodeType |= DSNode::Incomplete;
741
742 // Recusively process children...
Chris Lattner08db7192002-11-06 06:20:27 +0000743 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
744 if (DSNode *DSN = N->getLink(i).getNode())
745 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000746}
747
Chris Lattnere71ffc22002-11-11 03:36:55 +0000748static void markIncomplete(DSCallSite &Call) {
749 // Then the return value is certainly incomplete!
750 markIncompleteNode(Call.getRetVal().getNode());
751
752 // All objects pointed to by function arguments are incomplete!
753 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i)
754 markIncompleteNode(Call.getPtrArg(i).getNode());
755}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000756
757// markIncompleteNodes - Traverse the graph, identifying nodes that may be
758// modified by other functions that have not been resolved yet. This marks
759// nodes that are reachable through three sources of "unknownness":
760//
761// Global Variables, Function Calls, and Incoming Arguments
762//
763// For any node that may have unknown components (because something outside the
764// scope of current analysis may have modified it), the 'Incomplete' flag is
765// added to the NodeType.
766//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000767void DSGraph::markIncompleteNodes(bool markFormalArgs) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000768 // Mark any incoming arguments as incomplete...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000769 if (markFormalArgs && Func)
770 for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I)
Chris Lattnerc875f022002-11-03 21:27:48 +0000771 if (isPointerType(I->getType()) && ScalarMap.find(I) != ScalarMap.end())
772 markIncompleteNode(ScalarMap[I].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000773
774 // Mark stuff passed into functions calls as being incomplete...
Chris Lattnere71ffc22002-11-11 03:36:55 +0000775 if (!shouldPrintAuxCalls())
776 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
777 markIncomplete(FunctionCalls[i]);
778 else
779 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
780 markIncomplete(AuxFunctionCalls[i]);
781
Chris Lattner0d9bab82002-07-18 00:12:30 +0000782
Chris Lattner92673292002-11-02 00:13:20 +0000783 // Mark all of the nodes pointed to by global nodes as incomplete...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000784 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000785 if (Nodes[i]->NodeType & DSNode::GlobalNode) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000786 DSNode *N = Nodes[i];
Chris Lattner08db7192002-11-06 06:20:27 +0000787 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
788 if (DSNode *DSN = N->getLink(i).getNode())
789 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000790 }
791}
792
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000793// removeRefsToGlobal - Helper function that removes globals from the
Chris Lattnerc875f022002-11-03 21:27:48 +0000794// ScalarMap so that the referrer count will go down to zero.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000795static void removeRefsToGlobal(DSNode* N,
Chris Lattnerc875f022002-11-03 21:27:48 +0000796 std::map<Value*, DSNodeHandle> &ScalarMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000797 while (!N->getGlobals().empty()) {
798 GlobalValue *GV = N->getGlobals().back();
799 N->getGlobals().pop_back();
Chris Lattnerc875f022002-11-03 21:27:48 +0000800 ScalarMap.erase(GV);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000801 }
802}
803
804
Chris Lattner0d9bab82002-07-18 00:12:30 +0000805// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
806// checks to see if there are simple transformations that it can do to make it
807// dead.
808//
809bool DSGraph::isNodeDead(DSNode *N) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000810 // Is it a trivially dead shadow node?
Chris Lattnerd11e9542002-11-10 07:46:08 +0000811 return N->getReferrers().empty() && (N->NodeType & ~DSNode::DEAD) == 0;
Chris Lattneraa8146f2002-11-10 06:59:55 +0000812}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000813
Chris Lattneraa8146f2002-11-10 06:59:55 +0000814static inline void killIfUselessEdge(DSNodeHandle &Edge) {
815 if (DSNode *N = Edge.getNode()) // Is there an edge?
816 if (N->getReferrers().size() == 1) // Does it point to a lonely node?
817 if ((N->NodeType & ~DSNode::Incomplete) == 0 && // No interesting info?
Chris Lattner18552922002-11-18 21:44:46 +0000818 N->getType() == Type::VoidTy && !N->isNodeCompletelyFolded())
Chris Lattneraa8146f2002-11-10 06:59:55 +0000819 Edge.setNode(0); // Kill the edge!
820}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000821
Chris Lattneraa8146f2002-11-10 06:59:55 +0000822static inline bool nodeContainsExternalFunction(const DSNode *N) {
823 const std::vector<GlobalValue*> &Globals = N->getGlobals();
824 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
825 if (Globals[i]->isExternal())
826 return true;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000827 return false;
828}
829
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000830static void removeIdenticalCalls(vector<DSCallSite> &Calls,
Chris Lattner7541b892002-07-31 19:32:12 +0000831 const std::string &where) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000832 // Remove trivially identical function calls
833 unsigned NumFns = Calls.size();
Chris Lattneraa8146f2002-11-10 06:59:55 +0000834 std::sort(Calls.begin(), Calls.end()); // Sort by callee as primary key!
835
836 // Scan the call list cleaning it up as necessary...
837 DSNode *LastCalleeNode = 0;
838 unsigned NumDuplicateCalls = 0;
839 bool LastCalleeContainsExternalFunction = false;
Chris Lattnere4258442002-11-11 21:35:38 +0000840 for (unsigned i = 0; i != Calls.size(); ++i) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000841 DSCallSite &CS = Calls[i];
842
Chris Lattnere4258442002-11-11 21:35:38 +0000843 // If the Callee is a useless edge, this must be an unreachable call site,
844 // eliminate it.
845 killIfUselessEdge(CS.getCallee());
846 if (CS.getCallee().getNode() == 0) {
847 CS.swap(Calls.back());
848 Calls.pop_back();
849 --i;
Chris Lattneraa8146f2002-11-10 06:59:55 +0000850 } else {
Chris Lattnere4258442002-11-11 21:35:38 +0000851 // If the return value or any arguments point to a void node with no
852 // information at all in it, and the call node is the only node to point
853 // to it, remove the edge to the node (killing the node).
854 //
855 killIfUselessEdge(CS.getRetVal());
856 for (unsigned a = 0, e = CS.getNumPtrArgs(); a != e; ++a)
857 killIfUselessEdge(CS.getPtrArg(a));
858
859 // If this call site calls the same function as the last call site, and if
860 // the function pointer contains an external function, this node will
861 // never be resolved. Merge the arguments of the call node because no
862 // information will be lost.
863 //
864 if (CS.getCallee().getNode() == LastCalleeNode) {
865 ++NumDuplicateCalls;
866 if (NumDuplicateCalls == 1) {
867 LastCalleeContainsExternalFunction =
868 nodeContainsExternalFunction(LastCalleeNode);
869 }
870
871 if (LastCalleeContainsExternalFunction ||
872 // This should be more than enough context sensitivity!
873 // FIXME: Evaluate how many times this is tripped!
874 NumDuplicateCalls > 20) {
875 DSCallSite &OCS = Calls[i-1];
876 OCS.mergeWith(CS);
877
878 // The node will now be eliminated as a duplicate!
879 if (CS.getNumPtrArgs() < OCS.getNumPtrArgs())
880 CS = OCS;
881 else if (CS.getNumPtrArgs() > OCS.getNumPtrArgs())
882 OCS = CS;
883 }
884 } else {
885 LastCalleeNode = CS.getCallee().getNode();
886 NumDuplicateCalls = 0;
887 }
Chris Lattneraa8146f2002-11-10 06:59:55 +0000888 }
889 }
890
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000891 Calls.erase(std::unique(Calls.begin(), Calls.end()),
892 Calls.end());
893
Chris Lattner33312f72002-11-08 01:21:07 +0000894 // Track the number of call nodes merged away...
895 NumCallNodesMerged += NumFns-Calls.size();
896
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000897 DEBUG(if (NumFns != Calls.size())
Chris Lattner7541b892002-07-31 19:32:12 +0000898 std::cerr << "Merged " << (NumFns-Calls.size())
899 << " call nodes in " << where << "\n";);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000900}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000901
Chris Lattneraa8146f2002-11-10 06:59:55 +0000902
Chris Lattnere2219762002-07-18 18:22:40 +0000903// removeTriviallyDeadNodes - After the graph has been constructed, this method
904// removes all unreachable nodes that are created because they got merged with
905// other nodes in the graph. These nodes will all be trivially unreachable, so
906// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000907//
Chris Lattnerf40f0a32002-11-09 22:07:02 +0000908void DSGraph::removeTriviallyDeadNodes() {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000909 removeIdenticalCalls(FunctionCalls, Func ? Func->getName() : "");
Chris Lattneraa8146f2002-11-10 06:59:55 +0000910 removeIdenticalCalls(AuxFunctionCalls, Func ? Func->getName() : "");
911
912 for (unsigned i = 0; i != Nodes.size(); ++i)
913 if (isNodeDead(Nodes[i])) { // This node is dead!
914 delete Nodes[i]; // Free memory...
915 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
916 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000917}
918
919
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000920// markAlive - Simple graph walker that recursively traverses the graph, marking
Chris Lattnere2219762002-07-18 18:22:40 +0000921// stuff to be alive.
922//
923static void markAlive(DSNode *N, std::set<DSNode*> &Alive) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000924 if (N == 0) return;
Chris Lattneraa8146f2002-11-10 06:59:55 +0000925 std::set<DSNode*>::iterator I = Alive.lower_bound(N);
926 if (I != Alive.end() && *I == N) return; // Already marked alive
927 Alive.insert(I, N); // Is alive now
Chris Lattnere2219762002-07-18 18:22:40 +0000928
Chris Lattner08db7192002-11-06 06:20:27 +0000929 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
Chris Lattneraa8146f2002-11-10 06:59:55 +0000930 markAlive(N->getLink(i).getNode(), Alive);
Chris Lattnere2219762002-07-18 18:22:40 +0000931}
932
Chris Lattneraa8146f2002-11-10 06:59:55 +0000933// markAliveIfCanReachAlive - Simple graph walker that recursively traverses the
934// graph looking for a node that is marked alive. If the node is marked alive,
935// the recursive unwind marks node alive that can point to the alive node. This
936// is basically just a post-order traversal.
937//
938// This function returns true if the specified node is alive.
939//
940static bool markAliveIfCanReachAlive(DSNode *N, std::set<DSNode*> &Alive,
941 std::set<DSNode*> &Visited) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000942 if (N == 0) return false;
943
Chris Lattneraa8146f2002-11-10 06:59:55 +0000944 // If we know that this node is alive, return so!
945 if (Alive.count(N)) return true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000946
Chris Lattneraa8146f2002-11-10 06:59:55 +0000947 // Otherwise, we don't think the node is alive yet, check for infinite
948 // recursion.
949 std::set<DSNode*>::iterator VI = Visited.lower_bound(N);
950 if (VI != Visited.end() && *VI == N) return false; // Found a cycle
951 // No recursion, insert into Visited...
952 Visited.insert(VI, N);
953
954 if (N->NodeType & DSNode::GlobalNode)
955 return false; // Global nodes will be marked on their own
956
957 bool ChildrenAreAlive = false;
958
Chris Lattner08db7192002-11-06 06:20:27 +0000959 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
Chris Lattneraa8146f2002-11-10 06:59:55 +0000960 ChildrenAreAlive |= markAliveIfCanReachAlive(N->getLink(i).getNode(),
961 Alive, Visited);
962 if (ChildrenAreAlive)
963 markAlive(N, Alive);
964 return ChildrenAreAlive;
965}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000966
Chris Lattneraa8146f2002-11-10 06:59:55 +0000967static bool CallSiteUsesAliveArgs(DSCallSite &CS, std::set<DSNode*> &Alive,
968 std::set<DSNode*> &Visited) {
969 if (markAliveIfCanReachAlive(CS.getRetVal().getNode(), Alive, Visited) ||
970 markAliveIfCanReachAlive(CS.getCallee().getNode(), Alive, Visited))
971 return true;
972 for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
973 if (markAliveIfCanReachAlive(CS.getPtrArg(j).getNode(), Alive, Visited))
974 return true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000975 return false;
976}
977
Chris Lattneraa8146f2002-11-10 06:59:55 +0000978static void markAlive(DSCallSite &CS, std::set<DSNode*> &Alive) {
979 markAlive(CS.getRetVal().getNode(), Alive);
980 markAlive(CS.getCallee().getNode(), Alive);
981
982 for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
983 markAlive(CS.getPtrArg(j).getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000984}
Chris Lattnere2219762002-07-18 18:22:40 +0000985
986// removeDeadNodes - Use a more powerful reachability analysis to eliminate
987// subgraphs that are unreachable. This often occurs because the data
988// structure doesn't "escape" into it's caller, and thus should be eliminated
989// from the caller's graph entirely. This is only appropriate to use when
990// inlining graphs.
991//
Chris Lattnerf40f0a32002-11-09 22:07:02 +0000992void DSGraph::removeDeadNodes() {
Chris Lattnere2219762002-07-18 18:22:40 +0000993 // Reduce the amount of work we have to do...
Chris Lattnerf40f0a32002-11-09 22:07:02 +0000994 removeTriviallyDeadNodes();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000995
Chris Lattnere2219762002-07-18 18:22:40 +0000996 // FIXME: Merge nontrivially identical call nodes...
997
998 // Alive - a set that holds all nodes found to be reachable/alive.
999 std::set<DSNode*> Alive;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001000 std::vector<std::pair<Value*, DSNode*> > GlobalNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001001
Chris Lattneraa8146f2002-11-10 06:59:55 +00001002 // Mark all nodes reachable by (non-global) scalar nodes as alive...
Chris Lattnerc875f022002-11-03 21:27:48 +00001003 for (std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.begin(),
1004 E = ScalarMap.end(); I != E; ++I)
Vikram S. Advee31267d2002-11-25 18:21:25 +00001005 // if (!isa<GlobalValue>(I->first)) // Don't mark globals!
Chris Lattneraa8146f2002-11-10 06:59:55 +00001006 markAlive(I->second.getNode(), Alive);
Vikram S. Advee31267d2002-11-25 18:21:25 +00001007 // else // Keep track of global nodes
1008 // GlobalNodes.push_back(std::make_pair(I->first, I->second.getNode()));
Chris Lattnere2219762002-07-18 18:22:40 +00001009
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001010 // The return value is alive as well...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001011 markAlive(RetNode.getNode(), Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001012
Chris Lattneraa8146f2002-11-10 06:59:55 +00001013 // If any global nodes points to a non-global that is "alive", the global is
1014 // "alive" as well...
1015 //
1016 std::set<DSNode*> Visited;
1017 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
1018 markAliveIfCanReachAlive(GlobalNodes[i].second, Alive, Visited);
1019
1020 std::vector<bool> FCallsAlive(FunctionCalls.size());
1021 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
1022 if (CallSiteUsesAliveArgs(FunctionCalls[i], Alive, Visited)) {
1023 markAlive(FunctionCalls[i], Alive);
1024 FCallsAlive[i] = true;
1025 }
1026
1027 std::vector<bool> AuxFCallsAlive(AuxFunctionCalls.size());
1028 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1029 if (CallSiteUsesAliveArgs(AuxFunctionCalls[i], Alive, Visited)) {
1030 markAlive(AuxFunctionCalls[i], Alive);
1031 AuxFCallsAlive[i] = true;
1032 }
1033
1034 // Remove all dead function calls...
1035 unsigned CurIdx = 0;
1036 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
1037 if (FCallsAlive[i])
1038 FunctionCalls[CurIdx++].swap(FunctionCalls[i]);
1039 // Crop all the bad ones out...
1040 FunctionCalls.erase(FunctionCalls.begin()+CurIdx, FunctionCalls.end());
1041
1042 // Remove all dead aux function calls...
1043 CurIdx = 0;
1044 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1045 if (AuxFCallsAlive[i])
1046 AuxFunctionCalls[CurIdx++].swap(AuxFunctionCalls[i]);
1047 // Crop all the bad ones out...
1048 AuxFunctionCalls.erase(AuxFunctionCalls.begin()+CurIdx,
1049 AuxFunctionCalls.end());
1050
1051
1052 // Remove all unreachable globals from the ScalarMap
1053 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
1054 if (!Alive.count(GlobalNodes[i].second))
1055 ScalarMap.erase(GlobalNodes[i].first);
1056
Chris Lattnere2219762002-07-18 18:22:40 +00001057 // Loop over all unreachable nodes, dropping their references...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001058 vector<DSNode*> DeadNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001059 DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed.
1060 for (unsigned i = 0; i != Nodes.size(); ++i)
1061 if (!Alive.count(Nodes[i])) {
1062 DSNode *N = Nodes[i];
1063 Nodes.erase(Nodes.begin()+i--); // Erase node from alive list.
1064 DeadNodes.push_back(N); // Add node to our list of dead nodes
1065 N->dropAllReferences(); // Drop all outgoing edges
1066 }
1067
Chris Lattnere2219762002-07-18 18:22:40 +00001068 // Delete all dead nodes...
1069 std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
1070}
1071
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001072#if 0
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001073//===----------------------------------------------------------------------===//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001074// GlobalDSGraph Implementation
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
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001167// GlobalDSGraph::cloneCalls - Clone function calls and their visible target
1168// links (and recursively their such links) into this graph.
1169//
1170void GlobalDSGraph::cloneCalls(DSGraph& Graph) {
1171 std::map<const DSNode*, DSNode*> NodeCache;
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001172 vector<DSCallSite >& FromCalls =Graph.FunctionCalls;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001173
1174 FunctionCalls.reserve(FunctionCalls.size() + FromCalls.size());
1175
1176 for (int i = 0, ei = FromCalls.size(); i < ei; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001177 DSCallSite& callCopy = FunctionCalls.back();
1178 callCopy.reserve(FromCalls[i].size());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001179 for (unsigned j = 0, ej = FromCalls[i].size(); j != ej; ++j)
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001180 callCopy.push_back
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001181 ((FromCalls[i][j] && (FromCalls[i][j]->NodeType & ExternalTypeBits))
1182 ? cloneNodeInto(FromCalls[i][j], NodeCache, true)
1183 : 0);
1184 }
1185
1186 // remove trivially identical function calls
Chris Lattner7541b892002-07-31 19:32:12 +00001187 removeIdenticalCalls(FunctionCalls, "Globals Graph");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001188}
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001189#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001190
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001191#endif