blob: 62788d19b8d38cf854f121bfd70c7d1363f024fe [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 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...
Chris Lattner18552922002-11-18 21:44:46 +000080 Ty = Type::VoidTy;
81 NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +000082 Size = 1;
83
84 // Loop over all of our referrers, making them point to our zero bytes of
85 // space.
Chris Lattner8f0a16e2002-10-31 05:45:02 +000086 for (vector<DSNodeHandle*>::iterator I = Referrers.begin(), E=Referrers.end();
87 I != E; ++I)
88 (*I)->setOffset(0);
89
Chris Lattner8f0a16e2002-10-31 05:45:02 +000090 // If we have links, merge all of our outgoing links together...
Chris Lattner08db7192002-11-06 06:20:27 +000091 for (unsigned i = 1, e = Links.size(); i < e; ++i)
92 Links[0].mergeWith(Links[i]);
93 Links.resize(1);
Chris Lattner8f0a16e2002-10-31 05:45:02 +000094}
Chris Lattner076c1f92002-11-07 06:31:54 +000095
Chris Lattner8f0a16e2002-10-31 05:45:02 +000096/// isNodeCompletelyFolded - Return true if this node has been completely
97/// folded down to something that can never be expanded, effectively losing
98/// all of the field sensitivity that may be present in the node.
99///
100bool DSNode::isNodeCompletelyFolded() const {
Chris Lattner18552922002-11-18 21:44:46 +0000101 return getSize() == 1 && Ty == Type::VoidTy && isArray();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000102}
103
104
Chris Lattner08db7192002-11-06 06:20:27 +0000105/// mergeTypeInfo - This method merges the specified type into the current node
106/// at the specified offset. This may update the current node's type record if
107/// this gives more information to the node, it may do nothing to the node if
108/// this information is already known, or it may merge the node completely (and
109/// return true) if the information is incompatible with what is already known.
Chris Lattner7b7200c2002-10-02 04:57:39 +0000110///
Chris Lattner08db7192002-11-06 06:20:27 +0000111/// This method returns true if the node is completely folded, otherwise false.
112///
113bool DSNode::mergeTypeInfo(const Type *NewTy, unsigned Offset) {
114 // Check to make sure the Size member is up-to-date. Size can be one of the
115 // following:
116 // Size = 0, Ty = Void: Nothing is known about this node.
117 // Size = 0, Ty = FnTy: FunctionPtr doesn't have a size, so we use zero
118 // Size = 1, Ty = Void, Array = 1: The node is collapsed
119 // Otherwise, sizeof(Ty) = Size
120 //
Chris Lattner18552922002-11-18 21:44:46 +0000121 assert(((Size == 0 && Ty == Type::VoidTy && !isArray()) ||
122 (Size == 0 && !Ty->isSized() && !isArray()) ||
123 (Size == 1 && Ty == Type::VoidTy && isArray()) ||
124 (Size == 0 && !Ty->isSized() && !isArray()) ||
125 (TD.getTypeSize(Ty) == Size)) &&
Chris Lattner08db7192002-11-06 06:20:27 +0000126 "Size member of DSNode doesn't match the type structure!");
127 assert(NewTy != Type::VoidTy && "Cannot merge void type into DSNode!");
Chris Lattner7b7200c2002-10-02 04:57:39 +0000128
Chris Lattner18552922002-11-18 21:44:46 +0000129 if (Offset == 0 && NewTy == Ty)
Chris Lattner08db7192002-11-06 06:20:27 +0000130 return false; // This should be a common case, handle it efficiently
Chris Lattner7b7200c2002-10-02 04:57:39 +0000131
Chris Lattner08db7192002-11-06 06:20:27 +0000132 // Return true immediately if the node is completely folded.
133 if (isNodeCompletelyFolded()) return true;
134
Chris Lattner23f83dc2002-11-08 22:49:57 +0000135 // If this is an array type, eliminate the outside arrays because they won't
136 // be used anyway. This greatly reduces the size of large static arrays used
137 // as global variables, for example.
138 //
Chris Lattnerd8888932002-11-09 19:25:27 +0000139 bool WillBeArray = false;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000140 while (const ArrayType *AT = dyn_cast<ArrayType>(NewTy)) {
141 // FIXME: we might want to keep small arrays, but must be careful about
142 // things like: [2 x [10000 x int*]]
143 NewTy = AT->getElementType();
Chris Lattnerd8888932002-11-09 19:25:27 +0000144 WillBeArray = true;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000145 }
146
Chris Lattner08db7192002-11-06 06:20:27 +0000147 // Figure out how big the new type we're merging in is...
148 unsigned NewTySize = NewTy->isSized() ? TD.getTypeSize(NewTy) : 0;
149
150 // Otherwise check to see if we can fold this type into the current node. If
151 // we can't, we fold the node completely, if we can, we potentially update our
152 // internal state.
153 //
Chris Lattner18552922002-11-18 21:44:46 +0000154 if (Ty == Type::VoidTy) {
Chris Lattner08db7192002-11-06 06:20:27 +0000155 // If this is the first type that this node has seen, just accept it without
156 // question....
157 assert(Offset == 0 && "Cannot have an offset into a void node!");
Chris Lattner18552922002-11-18 21:44:46 +0000158 assert(!isArray() && "This shouldn't happen!");
159 Ty = NewTy;
160 NodeType &= ~Array;
161 if (WillBeArray) NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +0000162 Size = NewTySize;
163
164 // Calculate the number of outgoing links from this node.
165 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
166 return false;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000167 }
Chris Lattner08db7192002-11-06 06:20:27 +0000168
169 // Handle node expansion case here...
170 if (Offset+NewTySize > Size) {
171 // It is illegal to grow this node if we have treated it as an array of
172 // objects...
Chris Lattner18552922002-11-18 21:44:46 +0000173 if (isArray()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000174 foldNodeCompletely();
175 return true;
176 }
177
178 if (Offset) { // We could handle this case, but we don't for now...
Chris Lattner3c87b292002-11-07 01:54:56 +0000179 DEBUG(std::cerr << "UNIMP: Trying to merge a growth type into "
180 << "offset != 0: Collapsing!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000181 foldNodeCompletely();
182 return true;
183 }
184
185 // Okay, the situation is nice and simple, we are trying to merge a type in
186 // at offset 0 that is bigger than our current type. Implement this by
187 // switching to the new type and then merge in the smaller one, which should
188 // hit the other code path here. If the other code path decides it's not
189 // ok, it will collapse the node as appropriate.
190 //
Chris Lattner18552922002-11-18 21:44:46 +0000191 const Type *OldTy = Ty;
192 Ty = NewTy;
193 NodeType &= ~Array;
194 if (WillBeArray) NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +0000195 Size = NewTySize;
196
197 // Must grow links to be the appropriate size...
198 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
199
200 // Merge in the old type now... which is guaranteed to be smaller than the
201 // "current" type.
202 return mergeTypeInfo(OldTy, 0);
203 }
204
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000205 assert(Offset <= Size &&
Chris Lattner08db7192002-11-06 06:20:27 +0000206 "Cannot merge something into a part of our type that doesn't exist!");
207
Chris Lattner18552922002-11-18 21:44:46 +0000208 // Find the section of Ty that NewTy overlaps with... first we find the
Chris Lattner08db7192002-11-06 06:20:27 +0000209 // type that starts at offset Offset.
210 //
211 unsigned O = 0;
Chris Lattner18552922002-11-18 21:44:46 +0000212 const Type *SubType = Ty;
Chris Lattner08db7192002-11-06 06:20:27 +0000213 while (O < Offset) {
214 assert(Offset-O < TD.getTypeSize(SubType) && "Offset out of range!");
215
216 switch (SubType->getPrimitiveID()) {
217 case Type::StructTyID: {
218 const StructType *STy = cast<StructType>(SubType);
219 const StructLayout &SL = *TD.getStructLayout(STy);
220
221 unsigned i = 0, e = SL.MemberOffsets.size();
222 for (; i+1 < e && SL.MemberOffsets[i+1] <= Offset-O; ++i)
223 /* empty */;
224
225 // The offset we are looking for must be in the i'th element...
226 SubType = STy->getElementTypes()[i];
227 O += SL.MemberOffsets[i];
228 break;
229 }
230 case Type::ArrayTyID: {
231 SubType = cast<ArrayType>(SubType)->getElementType();
232 unsigned ElSize = TD.getTypeSize(SubType);
233 unsigned Remainder = (Offset-O) % ElSize;
234 O = Offset-Remainder;
235 break;
236 }
237 default:
238 assert(0 && "Unknown type!");
239 }
240 }
241
242 assert(O == Offset && "Could not achieve the correct offset!");
243
244 // If we found our type exactly, early exit
245 if (SubType == NewTy) return false;
246
247 // Okay, so we found the leader type at the offset requested. Search the list
248 // of types that starts at this offset. If SubType is currently an array or
249 // structure, the type desired may actually be the first element of the
250 // composite type...
251 //
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000252 unsigned SubTypeSize = SubType->isSized() ? TD.getTypeSize(SubType) : 0;
Chris Lattner18552922002-11-18 21:44:46 +0000253 unsigned PadSize = SubTypeSize; // Size, including pad memory which is ignored
Chris Lattner08db7192002-11-06 06:20:27 +0000254 while (SubType != NewTy) {
255 const Type *NextSubType = 0;
Chris Lattnerbf10f052002-11-09 00:49:05 +0000256 unsigned NextSubTypeSize = 0;
Chris Lattner18552922002-11-18 21:44:46 +0000257 unsigned NextPadSize = 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000258 switch (SubType->getPrimitiveID()) {
Chris Lattner18552922002-11-18 21:44:46 +0000259 case Type::StructTyID: {
260 const StructType *STy = cast<StructType>(SubType);
261 const StructLayout &SL = *TD.getStructLayout(STy);
262 if (SL.MemberOffsets.size() > 1)
263 NextPadSize = SL.MemberOffsets[1];
264 else
265 NextPadSize = SubTypeSize;
266 NextSubType = STy->getElementTypes()[0];
267 NextSubTypeSize = TD.getTypeSize(NextSubType);
Chris Lattner08db7192002-11-06 06:20:27 +0000268 break;
Chris Lattner18552922002-11-18 21:44:46 +0000269 }
Chris Lattner08db7192002-11-06 06:20:27 +0000270 case Type::ArrayTyID:
271 NextSubType = cast<ArrayType>(SubType)->getElementType();
Chris Lattner18552922002-11-18 21:44:46 +0000272 NextSubTypeSize = TD.getTypeSize(NextSubType);
273 NextPadSize = NextSubTypeSize;
Chris Lattner08db7192002-11-06 06:20:27 +0000274 break;
275 default: ;
276 // fall out
277 }
278
279 if (NextSubType == 0)
280 break; // In the default case, break out of the loop
281
Chris Lattner18552922002-11-18 21:44:46 +0000282 if (NextPadSize < NewTySize)
Chris Lattner08db7192002-11-06 06:20:27 +0000283 break; // Don't allow shrinking to a smaller type than NewTySize
284 SubType = NextSubType;
285 SubTypeSize = NextSubTypeSize;
Chris Lattner18552922002-11-18 21:44:46 +0000286 PadSize = NextPadSize;
Chris Lattner08db7192002-11-06 06:20:27 +0000287 }
288
289 // If we found the type exactly, return it...
290 if (SubType == NewTy)
291 return false;
292
293 // Check to see if we have a compatible, but different type...
294 if (NewTySize == SubTypeSize) {
295 // Check to see if this type is obviously convertable... int -> uint f.e.
296 if (NewTy->isLosslesslyConvertableTo(SubType))
297 return false;
298
299 // Check to see if we have a pointer & integer mismatch going on here,
300 // loading a pointer as a long, for example.
301 //
302 if (SubType->isInteger() && isa<PointerType>(NewTy) ||
303 NewTy->isInteger() && isa<PointerType>(SubType))
304 return false;
Chris Lattner18552922002-11-18 21:44:46 +0000305 } else if (NewTySize > SubTypeSize && NewTySize <= PadSize) {
306 // We are accessing the field, plus some structure padding. Ignore the
307 // structure padding.
308 return false;
Chris Lattner08db7192002-11-06 06:20:27 +0000309 }
310
311
Chris Lattner18552922002-11-18 21:44:46 +0000312 DEBUG(std::cerr << "MergeTypeInfo Folding OrigTy: " << Ty
Chris Lattner3c87b292002-11-07 01:54:56 +0000313 << "\n due to:" << NewTy << " @ " << Offset << "!\n"
314 << "SubType: " << SubType << "\n\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000315
316 foldNodeCompletely();
317 return true;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000318}
319
Chris Lattner08db7192002-11-06 06:20:27 +0000320
321
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000322// addEdgeTo - Add an edge from the current node to the specified node. This
323// can cause merging of nodes in the graph.
324//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000325void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000326 if (NH.getNode() == 0) return; // Nothing to do
327
Chris Lattner08db7192002-11-06 06:20:27 +0000328 DSNodeHandle &ExistingEdge = getLink(Offset);
329 if (ExistingEdge.getNode()) {
Chris Lattner7b7200c2002-10-02 04:57:39 +0000330 // Merge the two nodes...
Chris Lattner08db7192002-11-06 06:20:27 +0000331 ExistingEdge.mergeWith(NH);
Chris Lattner7b7200c2002-10-02 04:57:39 +0000332 } else { // No merging to perform...
333 setLink(Offset, NH); // Just force a link in there...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000334 }
Chris Lattner7b7200c2002-10-02 04:57:39 +0000335}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000336
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000337
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000338// MergeSortedVectors - Efficiently merge a vector into another vector where
339// duplicates are not allowed and both are sorted. This assumes that 'T's are
340// efficiently copyable and have sane comparison semantics.
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000341//
Chris Lattner18552922002-11-18 21:44:46 +0000342static void MergeSortedVectors(vector<GlobalValue*> &Dest,
343 const vector<GlobalValue*> &Src) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000344 // By far, the most common cases will be the simple ones. In these cases,
345 // avoid having to allocate a temporary vector...
346 //
347 if (Src.empty()) { // Nothing to merge in...
348 return;
349 } else if (Dest.empty()) { // Just copy the result in...
350 Dest = Src;
351 } else if (Src.size() == 1) { // Insert a single element...
Chris Lattner18552922002-11-18 21:44:46 +0000352 const GlobalValue *V = Src[0];
353 vector<GlobalValue*>::iterator I =
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000354 std::lower_bound(Dest.begin(), Dest.end(), V);
355 if (I == Dest.end() || *I != Src[0]) // If not already contained...
356 Dest.insert(I, Src[0]);
357 } else if (Dest.size() == 1) {
Chris Lattner18552922002-11-18 21:44:46 +0000358 GlobalValue *Tmp = Dest[0]; // Save value in temporary...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000359 Dest = Src; // Copy over list...
Chris Lattner18552922002-11-18 21:44:46 +0000360 vector<GlobalValue*>::iterator I =
Chris Lattner5190ce82002-11-12 07:20:45 +0000361 std::lower_bound(Dest.begin(), Dest.end(), Tmp);
362 if (I == Dest.end() || *I != Tmp) // If not already contained...
363 Dest.insert(I, Tmp);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000364
365 } else {
366 // Make a copy to the side of Dest...
Chris Lattner18552922002-11-18 21:44:46 +0000367 vector<GlobalValue*> Old(Dest);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000368
369 // Make space for all of the type entries now...
370 Dest.resize(Dest.size()+Src.size());
371
372 // Merge the two sorted ranges together... into Dest.
373 std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin());
374
375 // Now erase any duplicate entries that may have accumulated into the
376 // vectors (because they were in both of the input sets)
377 Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end());
378 }
379}
380
381
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000382// MergeNodes() - Helper function for DSNode::mergeWith().
383// This function does the hard work of merging two nodes, CurNodeH
384// and NH after filtering out trivial cases and making sure that
385// CurNodeH.offset >= NH.offset.
386//
387// ***WARNING***
388// Since merging may cause either node to go away, we must always
389// use the node-handles to refer to the nodes. These node handles are
390// automatically updated during merging, so will always provide access
391// to the correct node after a merge.
392//
393void DSNode::MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH) {
394 assert(CurNodeH.getOffset() >= NH.getOffset() &&
395 "This should have been enforced in the caller.");
396
397 // Now we know that Offset >= NH.Offset, so convert it so our "Offset" (with
398 // respect to NH.Offset) is now zero. NOffset is the distance from the base
399 // of our object that N starts from.
400 //
401 unsigned NOffset = CurNodeH.getOffset()-NH.getOffset();
402 unsigned NSize = NH.getNode()->getSize();
403
404 // Merge the type entries of the two nodes together...
405 if (NH.getNode()->Ty != Type::VoidTy) {
406 CurNodeH.getNode()->mergeTypeInfo(NH.getNode()->Ty, NOffset);
407 }
408 assert((CurNodeH.getNode()->NodeType & DSNode::DEAD) == 0);
409
410 // If we are merging a node with a completely folded node, then both nodes are
411 // now completely folded.
412 //
413 if (CurNodeH.getNode()->isNodeCompletelyFolded()) {
414 if (!NH.getNode()->isNodeCompletelyFolded()) {
415 NH.getNode()->foldNodeCompletely();
416 assert(NH.getOffset()==0 && "folding did not make offset 0?");
417 NOffset = NH.getOffset();
418 NSize = NH.getNode()->getSize();
419 assert(NOffset == 0 && NSize == 1);
420 }
421 } else if (NH.getNode()->isNodeCompletelyFolded()) {
422 CurNodeH.getNode()->foldNodeCompletely();
423 assert(CurNodeH.getOffset()==0 && "folding did not make offset 0?");
424 NOffset = NH.getOffset();
425 NSize = NH.getNode()->getSize();
426 assert(NOffset == 0 && NSize == 1);
427 }
428
429 if (CurNodeH.getNode() == NH.getNode() || NH.getNode() == 0) return;
430 assert((CurNodeH.getNode()->NodeType & DSNode::DEAD) == 0);
431
432 // Remove all edges pointing at N, causing them to point to 'this' instead.
433 // Make sure to adjust their offset, not just the node pointer.
434 // Also, be careful to use the DSNode* rather than NH since NH is one of
435 // the referrers and once NH refers to CurNodeH.getNode() this will
436 // become an infinite loop.
437 DSNode* N = NH.getNode();
438 unsigned OldNHOffset = NH.getOffset();
439 while (!N->Referrers.empty()) {
440 DSNodeHandle &Ref = *N->Referrers.back();
441 Ref = DSNodeHandle(CurNodeH.getNode(), NOffset+Ref.getOffset());
442 }
443 NH = DSNodeHandle(N, OldNHOffset); // reset NH to point back to where it was
444
445 assert((CurNodeH.getNode()->NodeType & DSNode::DEAD) == 0);
446
447 // Make all of the outgoing links of *NH now be outgoing links of
448 // this. This can cause recursive merging!
449 //
Chris Lattner6f6fef82003-01-22 22:00:24 +0000450 for (unsigned i = 0; i < NH.getNode()->getSize(); i += DS::PointerSize) {
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000451 DSNodeHandle &Link = NH.getNode()->getLink(i);
452 if (Link.getNode()) {
453 // Compute the offset into the current node at which to
454 // merge this link. In the common case, this is a linear
455 // relation to the offset in the original node (with
456 // wrapping), but if the current node gets collapsed due to
457 // recursive merging, we must make sure to merge in all remaining
458 // links at offset zero.
459 unsigned MergeOffset = 0;
460 if (CurNodeH.getNode()->Size != 1)
461 MergeOffset = (i+NOffset) % CurNodeH.getNode()->getSize();
462 CurNodeH.getNode()->addEdgeTo(MergeOffset, Link);
463 }
464 }
465
466 // Now that there are no outgoing edges, all of the Links are dead.
467 NH.getNode()->Links.clear();
468 NH.getNode()->Size = 0;
469 NH.getNode()->Ty = Type::VoidTy;
470
471 // Merge the node types
472 CurNodeH.getNode()->NodeType |= NH.getNode()->NodeType;
473 NH.getNode()->NodeType = DEAD; // NH is now a dead node.
474
475 // Merge the globals list...
476 if (!NH.getNode()->Globals.empty()) {
477 MergeSortedVectors(CurNodeH.getNode()->Globals, NH.getNode()->Globals);
478
479 // Delete the globals from the old node...
480 NH.getNode()->Globals.clear();
481 }
482}
483
484
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000485// mergeWith - Merge this node and the specified node, moving all links to and
486// from the argument node into the current node, deleting the node argument.
487// Offset indicates what offset the specified node is to be merged into the
488// current node.
489//
490// The specified node may be a null pointer (in which case, nothing happens).
491//
492void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
493 DSNode *N = NH.getNode();
494 if (N == 0 || (N == this && NH.getOffset() == Offset))
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000495 return; // Noop
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000496
Chris Lattner679e8e12002-11-08 21:27:12 +0000497 assert((N->NodeType & DSNode::DEAD) == 0);
498 assert((NodeType & DSNode::DEAD) == 0);
499 assert(!hasNoReferrers() && "Should not try to fold a useless node!");
500
Chris Lattner02606632002-11-04 06:48:26 +0000501 if (N == this) {
Chris Lattner08db7192002-11-06 06:20:27 +0000502 // We cannot merge two pieces of the same node together, collapse the node
503 // completely.
Chris Lattner3c87b292002-11-07 01:54:56 +0000504 DEBUG(std::cerr << "Attempting to merge two chunks of"
505 << " the same node together!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000506 foldNodeCompletely();
Chris Lattner02606632002-11-04 06:48:26 +0000507 return;
508 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000509
Chris Lattner5190ce82002-11-12 07:20:45 +0000510 // If both nodes are not at offset 0, make sure that we are merging the node
511 // at an later offset into the node with the zero offset.
512 //
513 if (Offset < NH.getOffset()) {
514 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
515 return;
516 } else if (Offset == NH.getOffset() && getSize() < N->getSize()) {
517 // If the offsets are the same, merge the smaller node into the bigger node
518 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
519 return;
520 }
521
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000522 // Ok, now we can merge the two nodes. Use a static helper that works with
523 // two node handles, since "this" may get merged away at intermediate steps.
524 DSNodeHandle CurNodeH(this, Offset);
525 DSNodeHandle NHCopy(NH);
526 DSNode::MergeNodes(CurNodeH, NHCopy);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000527}
528
Chris Lattner9de906c2002-10-20 22:11:44 +0000529//===----------------------------------------------------------------------===//
530// DSCallSite Implementation
531//===----------------------------------------------------------------------===//
532
Vikram S. Adve26b98262002-10-20 21:41:02 +0000533// Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h
Chris Lattner9de906c2002-10-20 22:11:44 +0000534Function &DSCallSite::getCaller() const {
Chris Lattner0969c502002-10-21 02:08:03 +0000535 return *Inst->getParent()->getParent();
Vikram S. Adve26b98262002-10-20 21:41:02 +0000536}
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000537
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000538
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000539//===----------------------------------------------------------------------===//
540// DSGraph Implementation
541//===----------------------------------------------------------------------===//
542
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000543DSGraph::DSGraph(const DSGraph &G) : Func(G.Func), GlobalsGraph(0) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000544 PrintAuxCalls = false;
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000545 std::map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattnerc875f022002-11-03 21:27:48 +0000546 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000547}
548
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000549DSGraph::DSGraph(const DSGraph &G,
550 std::map<const DSNode*, DSNodeHandle> &NodeMap)
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000551 : Func(G.Func), GlobalsGraph(0) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000552 PrintAuxCalls = false;
Chris Lattnerc875f022002-11-03 21:27:48 +0000553 RetNode = cloneInto(G, ScalarMap, NodeMap);
Chris Lattnereff0da92002-10-21 15:32:34 +0000554}
555
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000556DSGraph::~DSGraph() {
557 FunctionCalls.clear();
Chris Lattner679e8e12002-11-08 21:27:12 +0000558 AuxFunctionCalls.clear();
Chris Lattnerc875f022002-11-03 21:27:48 +0000559 ScalarMap.clear();
Chris Lattner13ec72a2002-10-21 13:31:48 +0000560 RetNode.setNode(0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000561
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000562 // Drop all intra-node references, so that assertions don't fail...
563 std::for_each(Nodes.begin(), Nodes.end(),
564 std::mem_fun(&DSNode::dropAllReferences));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000565
566 // Delete all of the nodes themselves...
567 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
568}
569
Chris Lattner0d9bab82002-07-18 00:12:30 +0000570// dump - Allow inspection of graph in a debugger.
571void DSGraph::dump() const { print(std::cerr); }
572
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000573
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000574/// remapLinks - Change all of the Links in the current node according to the
575/// specified mapping.
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000576///
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000577void DSNode::remapLinks(std::map<const DSNode*, DSNodeHandle> &OldNodeMap) {
578 for (unsigned i = 0, e = Links.size(); i != e; ++i) {
579 DSNodeHandle &H = OldNodeMap[Links[i].getNode()];
580 Links[i].setNode(H.getNode());
581 Links[i].setOffset(Links[i].getOffset()+H.getOffset());
582 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000583}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000584
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000585
Chris Lattner0d9bab82002-07-18 00:12:30 +0000586// cloneInto - Clone the specified DSGraph into the current graph, returning the
Chris Lattnerc875f022002-11-03 21:27:48 +0000587// Return node of the graph. The translated ScalarMap for the old function is
Chris Lattner92673292002-11-02 00:13:20 +0000588// filled into the OldValMap member. If StripAllocas is set to true, Alloca
589// markers are removed from the graph, as the graph is being cloned into a
590// calling function's graph.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000591//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000592DSNodeHandle DSGraph::cloneInto(const DSGraph &G,
593 std::map<Value*, DSNodeHandle> &OldValMap,
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000594 std::map<const DSNode*, DSNodeHandle> &OldNodeMap,
Chris Lattner679e8e12002-11-08 21:27:12 +0000595 unsigned CloneFlags) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000596 assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
Chris Lattner33312f72002-11-08 01:21:07 +0000597 assert(&G != this && "Cannot clone graph into itself!");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000598
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000599 unsigned FN = Nodes.size(); // First new node...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000600
601 // Duplicate all of the nodes, populating the node map...
602 Nodes.reserve(FN+G.Nodes.size());
603 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000604 DSNode *Old = G.Nodes[i];
605 DSNode *New = new DSNode(*Old);
Chris Lattner679e8e12002-11-08 21:27:12 +0000606 New->NodeType &= ~DSNode::DEAD; // Clear dead flag...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000607 Nodes.push_back(New);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000608 OldNodeMap[Old] = New;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000609 }
610
Chris Lattner18552922002-11-18 21:44:46 +0000611#ifndef NDEBUG
612 Timer::addPeakMemoryMeasurement();
613#endif
614
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000615 // Rewrite the links in the new nodes to point into the current graph now.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000616 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000617 Nodes[i]->remapLinks(OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000618
Chris Lattner460ea292002-11-07 07:06:20 +0000619 // Remove alloca markers as specified
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000620 if (CloneFlags & (StripAllocaBit | StripModRefBits)) {
621 unsigned short clearBits = (CloneFlags & StripAllocaBit
622 ? DSNode::AllocaNode : 0)
623 | (CloneFlags & StripModRefBits
624 ? (DSNode::Modified | DSNode::Read) : 0);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000625 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000626 Nodes[i]->NodeType &= ~clearBits;
627 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000628
Chris Lattnercf15db32002-10-17 20:09:52 +0000629 // Copy the value map... and merge all of the global nodes...
Chris Lattnerc875f022002-11-03 21:27:48 +0000630 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ScalarMap.begin(),
631 E = G.ScalarMap.end(); I != E; ++I) {
Chris Lattnercf15db32002-10-17 20:09:52 +0000632 DSNodeHandle &H = OldValMap[I->first];
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000633 DSNodeHandle &MappedNode = OldNodeMap[I->second.getNode()];
634 H.setNode(MappedNode.getNode());
635 H.setOffset(I->second.getOffset()+MappedNode.getOffset());
Chris Lattnercf15db32002-10-17 20:09:52 +0000636
637 if (isa<GlobalValue>(I->first)) { // Is this a global?
Chris Lattnerc875f022002-11-03 21:27:48 +0000638 std::map<Value*, DSNodeHandle>::iterator GVI = ScalarMap.find(I->first);
639 if (GVI != ScalarMap.end()) { // Is the global value in this fn already?
Chris Lattnercf15db32002-10-17 20:09:52 +0000640 GVI->second.mergeWith(H);
641 } else {
Chris Lattnerc875f022002-11-03 21:27:48 +0000642 ScalarMap[I->first] = H; // Add global pointer to this graph
Chris Lattnercf15db32002-10-17 20:09:52 +0000643 }
644 }
645 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000646
Chris Lattner679e8e12002-11-08 21:27:12 +0000647 if (!(CloneFlags & DontCloneCallNodes)) {
648 // Copy the function calls list...
649 unsigned FC = FunctionCalls.size(); // FirstCall
650 FunctionCalls.reserve(FC+G.FunctionCalls.size());
651 for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i)
652 FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap));
Chris Lattneracf491f2002-11-08 22:27:09 +0000653 }
Chris Lattner679e8e12002-11-08 21:27:12 +0000654
Chris Lattneracf491f2002-11-08 22:27:09 +0000655 if (!(CloneFlags & DontCloneAuxCallNodes)) {
Chris Lattner679e8e12002-11-08 21:27:12 +0000656 // Copy the auxillary function calls list...
Chris Lattneracf491f2002-11-08 22:27:09 +0000657 unsigned FC = AuxFunctionCalls.size(); // FirstCall
Chris Lattner679e8e12002-11-08 21:27:12 +0000658 AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size());
659 for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i)
660 AuxFunctionCalls.push_back(DSCallSite(G.AuxFunctionCalls[i], OldNodeMap));
661 }
Chris Lattnercf15db32002-10-17 20:09:52 +0000662
Chris Lattner0d9bab82002-07-18 00:12:30 +0000663 // Return the returned node pointer...
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000664 DSNodeHandle &MappedRet = OldNodeMap[G.RetNode.getNode()];
665 return DSNodeHandle(MappedRet.getNode(),
666 MappedRet.getOffset()+G.RetNode.getOffset());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000667}
668
Chris Lattner076c1f92002-11-07 06:31:54 +0000669/// mergeInGraph - The method is used for merging graphs together. If the
670/// argument graph is not *this, it makes a clone of the specified graph, then
671/// merges the nodes specified in the call site with the formal arguments in the
672/// graph.
673///
674void DSGraph::mergeInGraph(DSCallSite &CS, const DSGraph &Graph,
Chris Lattner679e8e12002-11-08 21:27:12 +0000675 unsigned CloneFlags) {
Chris Lattner076c1f92002-11-07 06:31:54 +0000676 std::map<Value*, DSNodeHandle> OldValMap;
677 DSNodeHandle RetVal;
678 std::map<Value*, DSNodeHandle> *ScalarMap = &OldValMap;
679
680 // If this is not a recursive call, clone the graph into this graph...
681 if (&Graph != this) {
682 // Clone the callee's graph into the current graph, keeping
683 // track of where scalars in the old graph _used_ to point,
684 // and of the new nodes matching nodes of the old graph.
Chris Lattnerf8c6aab2002-11-08 05:01:14 +0000685 std::map<const DSNode*, DSNodeHandle> OldNodeMap;
Chris Lattner076c1f92002-11-07 06:31:54 +0000686
687 // The clone call may invalidate any of the vectors in the data
688 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner679e8e12002-11-08 21:27:12 +0000689 RetVal = cloneInto(Graph, OldValMap, OldNodeMap, CloneFlags);
Chris Lattner076c1f92002-11-07 06:31:54 +0000690 ScalarMap = &OldValMap;
691 } else {
692 RetVal = getRetNode();
693 ScalarMap = &getScalarMap();
694 }
695
696 // Merge the return value with the return value of the context...
697 RetVal.mergeWith(CS.getRetVal());
698
699 // Resolve all of the function arguments...
700 Function &F = Graph.getFunction();
701 Function::aiterator AI = F.abegin();
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000702
Chris Lattner076c1f92002-11-07 06:31:54 +0000703 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) {
704 // Advance the argument iterator to the first pointer argument...
705 while (!isPointerType(AI->getType())) {
706 ++AI;
707#ifndef NDEBUG
708 if (AI == F.aend())
709 std::cerr << "Bad call to Function: " << F.getName() << "\n";
710#endif
711 assert(AI != F.aend() && "# Args provided is not # Args required!");
712 }
713
714 // Add the link from the argument scalar to the provided value
715 DSNodeHandle &NH = (*ScalarMap)[AI];
716 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
717 NH.mergeWith(CS.getPtrArg(i));
718 }
719}
720
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000721#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000722// cloneGlobalInto - Clone the given global node and all its target links
723// (and all their llinks, recursively).
724//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000725DSNode *DSGraph::cloneGlobalInto(const DSNode *GNode) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000726 if (GNode == 0 || GNode->getGlobals().size() == 0) return 0;
727
728 // If a clone has already been created for GNode, return it.
Chris Lattnerc875f022002-11-03 21:27:48 +0000729 DSNodeHandle& ValMapEntry = ScalarMap[GNode->getGlobals()[0]];
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000730 if (ValMapEntry != 0)
731 return ValMapEntry;
732
733 // Clone the node and update the ValMap.
734 DSNode* NewNode = new DSNode(*GNode);
735 ValMapEntry = NewNode; // j=0 case of loop below!
736 Nodes.push_back(NewNode);
737 for (unsigned j = 1, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +0000738 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000739
740 // Rewrite the links in the new node to point into the current graph.
741 for (unsigned j = 0, e = GNode->getNumLinks(); j != e; ++j)
742 NewNode->setLink(j, cloneGlobalInto(GNode->getLink(j)));
743
744 return NewNode;
745}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000746#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000747
748
Chris Lattner0d9bab82002-07-18 00:12:30 +0000749// markIncompleteNodes - Mark the specified node as having contents that are not
750// known with the current analysis we have performed. Because a node makes all
751// of the nodes it can reach imcomplete if the node itself is incomplete, we
752// must recursively traverse the data structure graph, marking all reachable
753// nodes as incomplete.
754//
755static void markIncompleteNode(DSNode *N) {
756 // Stop recursion if no node, or if node already marked...
757 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
758
759 // Actually mark the node
760 N->NodeType |= DSNode::Incomplete;
761
762 // Recusively process children...
Chris Lattner08db7192002-11-06 06:20:27 +0000763 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
764 if (DSNode *DSN = N->getLink(i).getNode())
765 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000766}
767
Chris Lattnere71ffc22002-11-11 03:36:55 +0000768static void markIncomplete(DSCallSite &Call) {
769 // Then the return value is certainly incomplete!
770 markIncompleteNode(Call.getRetVal().getNode());
771
772 // All objects pointed to by function arguments are incomplete!
773 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i)
774 markIncompleteNode(Call.getPtrArg(i).getNode());
775}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000776
777// markIncompleteNodes - Traverse the graph, identifying nodes that may be
778// modified by other functions that have not been resolved yet. This marks
779// nodes that are reachable through three sources of "unknownness":
780//
781// Global Variables, Function Calls, and Incoming Arguments
782//
783// For any node that may have unknown components (because something outside the
784// scope of current analysis may have modified it), the 'Incomplete' flag is
785// added to the NodeType.
786//
Chris Lattner394471f2003-01-23 22:05:33 +0000787void DSGraph::markIncompleteNodes(unsigned Flags) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000788 // Mark any incoming arguments as incomplete...
Chris Lattner394471f2003-01-23 22:05:33 +0000789 if ((Flags & DSGraph::MarkFormalArgs) && Func)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000790 for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I)
Chris Lattnerc875f022002-11-03 21:27:48 +0000791 if (isPointerType(I->getType()) && ScalarMap.find(I) != ScalarMap.end())
792 markIncompleteNode(ScalarMap[I].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000793
794 // Mark stuff passed into functions calls as being incomplete...
Chris Lattnere71ffc22002-11-11 03:36:55 +0000795 if (!shouldPrintAuxCalls())
796 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
797 markIncomplete(FunctionCalls[i]);
798 else
799 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
800 markIncomplete(AuxFunctionCalls[i]);
801
Chris Lattner0d9bab82002-07-18 00:12:30 +0000802
Chris Lattner92673292002-11-02 00:13:20 +0000803 // Mark all of the nodes pointed to by global nodes as incomplete...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000804 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000805 if (Nodes[i]->NodeType & DSNode::GlobalNode) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000806 DSNode *N = Nodes[i];
Chris Lattner08db7192002-11-06 06:20:27 +0000807 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
808 if (DSNode *DSN = N->getLink(i).getNode())
809 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000810 }
811}
812
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000813// removeRefsToGlobal - Helper function that removes globals from the
Chris Lattnerc875f022002-11-03 21:27:48 +0000814// ScalarMap so that the referrer count will go down to zero.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000815static void removeRefsToGlobal(DSNode* N,
Chris Lattnerc875f022002-11-03 21:27:48 +0000816 std::map<Value*, DSNodeHandle> &ScalarMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000817 while (!N->getGlobals().empty()) {
818 GlobalValue *GV = N->getGlobals().back();
819 N->getGlobals().pop_back();
Chris Lattnerc875f022002-11-03 21:27:48 +0000820 ScalarMap.erase(GV);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000821 }
822}
823
824
Chris Lattner0d9bab82002-07-18 00:12:30 +0000825// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
826// checks to see if there are simple transformations that it can do to make it
827// dead.
828//
829bool DSGraph::isNodeDead(DSNode *N) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000830 // Is it a trivially dead shadow node?
Chris Lattnerd11e9542002-11-10 07:46:08 +0000831 return N->getReferrers().empty() && (N->NodeType & ~DSNode::DEAD) == 0;
Chris Lattneraa8146f2002-11-10 06:59:55 +0000832}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000833
Chris Lattneraa8146f2002-11-10 06:59:55 +0000834static inline void killIfUselessEdge(DSNodeHandle &Edge) {
835 if (DSNode *N = Edge.getNode()) // Is there an edge?
836 if (N->getReferrers().size() == 1) // Does it point to a lonely node?
837 if ((N->NodeType & ~DSNode::Incomplete) == 0 && // No interesting info?
Chris Lattner18552922002-11-18 21:44:46 +0000838 N->getType() == Type::VoidTy && !N->isNodeCompletelyFolded())
Chris Lattneraa8146f2002-11-10 06:59:55 +0000839 Edge.setNode(0); // Kill the edge!
840}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000841
Chris Lattneraa8146f2002-11-10 06:59:55 +0000842static inline bool nodeContainsExternalFunction(const DSNode *N) {
843 const std::vector<GlobalValue*> &Globals = N->getGlobals();
844 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
845 if (Globals[i]->isExternal())
846 return true;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000847 return false;
848}
849
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000850static void removeIdenticalCalls(vector<DSCallSite> &Calls,
Chris Lattner7541b892002-07-31 19:32:12 +0000851 const std::string &where) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000852 // Remove trivially identical function calls
853 unsigned NumFns = Calls.size();
Chris Lattneraa8146f2002-11-10 06:59:55 +0000854 std::sort(Calls.begin(), Calls.end()); // Sort by callee as primary key!
855
856 // Scan the call list cleaning it up as necessary...
857 DSNode *LastCalleeNode = 0;
858 unsigned NumDuplicateCalls = 0;
859 bool LastCalleeContainsExternalFunction = false;
Chris Lattnere4258442002-11-11 21:35:38 +0000860 for (unsigned i = 0; i != Calls.size(); ++i) {
Chris Lattneraa8146f2002-11-10 06:59:55 +0000861 DSCallSite &CS = Calls[i];
862
Chris Lattnere4258442002-11-11 21:35:38 +0000863 // If the Callee is a useless edge, this must be an unreachable call site,
864 // eliminate it.
865 killIfUselessEdge(CS.getCallee());
866 if (CS.getCallee().getNode() == 0) {
867 CS.swap(Calls.back());
868 Calls.pop_back();
869 --i;
Chris Lattneraa8146f2002-11-10 06:59:55 +0000870 } else {
Chris Lattnere4258442002-11-11 21:35:38 +0000871 // If the return value or any arguments point to a void node with no
872 // information at all in it, and the call node is the only node to point
873 // to it, remove the edge to the node (killing the node).
874 //
875 killIfUselessEdge(CS.getRetVal());
876 for (unsigned a = 0, e = CS.getNumPtrArgs(); a != e; ++a)
877 killIfUselessEdge(CS.getPtrArg(a));
878
879 // If this call site calls the same function as the last call site, and if
880 // the function pointer contains an external function, this node will
881 // never be resolved. Merge the arguments of the call node because no
882 // information will be lost.
883 //
884 if (CS.getCallee().getNode() == LastCalleeNode) {
885 ++NumDuplicateCalls;
886 if (NumDuplicateCalls == 1) {
887 LastCalleeContainsExternalFunction =
888 nodeContainsExternalFunction(LastCalleeNode);
889 }
890
891 if (LastCalleeContainsExternalFunction ||
892 // This should be more than enough context sensitivity!
893 // FIXME: Evaluate how many times this is tripped!
894 NumDuplicateCalls > 20) {
895 DSCallSite &OCS = Calls[i-1];
896 OCS.mergeWith(CS);
897
898 // The node will now be eliminated as a duplicate!
899 if (CS.getNumPtrArgs() < OCS.getNumPtrArgs())
900 CS = OCS;
901 else if (CS.getNumPtrArgs() > OCS.getNumPtrArgs())
902 OCS = CS;
903 }
904 } else {
905 LastCalleeNode = CS.getCallee().getNode();
906 NumDuplicateCalls = 0;
907 }
Chris Lattneraa8146f2002-11-10 06:59:55 +0000908 }
909 }
910
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000911 Calls.erase(std::unique(Calls.begin(), Calls.end()),
912 Calls.end());
913
Chris Lattner33312f72002-11-08 01:21:07 +0000914 // Track the number of call nodes merged away...
915 NumCallNodesMerged += NumFns-Calls.size();
916
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000917 DEBUG(if (NumFns != Calls.size())
Chris Lattner7541b892002-07-31 19:32:12 +0000918 std::cerr << "Merged " << (NumFns-Calls.size())
919 << " call nodes in " << where << "\n";);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000920}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000921
Chris Lattneraa8146f2002-11-10 06:59:55 +0000922
Chris Lattnere2219762002-07-18 18:22:40 +0000923// removeTriviallyDeadNodes - After the graph has been constructed, this method
924// removes all unreachable nodes that are created because they got merged with
925// other nodes in the graph. These nodes will all be trivially unreachable, so
926// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000927//
Chris Lattnerf40f0a32002-11-09 22:07:02 +0000928void DSGraph::removeTriviallyDeadNodes() {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000929 removeIdenticalCalls(FunctionCalls, Func ? Func->getName() : "");
Chris Lattneraa8146f2002-11-10 06:59:55 +0000930 removeIdenticalCalls(AuxFunctionCalls, Func ? Func->getName() : "");
931
932 for (unsigned i = 0; i != Nodes.size(); ++i)
933 if (isNodeDead(Nodes[i])) { // This node is dead!
934 delete Nodes[i]; // Free memory...
935 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
936 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000937}
938
939
Chris Lattner5c7380e2003-01-29 21:10:20 +0000940/// markReachableNodes - This method recursively traverses the specified
941/// DSNodes, marking any nodes which are reachable. All reachable nodes it adds
942/// to the set, which allows it to only traverse visited nodes once.
943///
944void DSNode::markReachableNodes(std::set<DSNode*> &ReachableNodes) {
945 if (this == 0) return;
946 std::set<DSNode*>::iterator I = ReachableNodes.lower_bound(this);
947 if (I != ReachableNodes.end() && *I == this)
948 return; // Already marked reachable
949 ReachableNodes.insert(I, this); // Is reachable now
Chris Lattnere2219762002-07-18 18:22:40 +0000950
Chris Lattner5c7380e2003-01-29 21:10:20 +0000951 for (unsigned i = 0, e = getSize(); i < e; i += DS::PointerSize)
952 getLink(i).getNode()->markReachableNodes(ReachableNodes);
953}
954
955void DSCallSite::markReachableNodes(std::set<DSNode*> &Nodes) {
956 getRetVal().getNode()->markReachableNodes(Nodes);
957 getCallee().getNode()->markReachableNodes(Nodes);
958
959 for (unsigned j = 0, e = getNumPtrArgs(); j != e; ++j)
960 getPtrArg(j).getNode()->markReachableNodes(Nodes);
Chris Lattnere2219762002-07-18 18:22:40 +0000961}
962
Chris Lattneraa8146f2002-11-10 06:59:55 +0000963// markAliveIfCanReachAlive - Simple graph walker that recursively traverses the
964// graph looking for a node that is marked alive. If the node is marked alive,
965// the recursive unwind marks node alive that can point to the alive node. This
966// is basically just a post-order traversal.
967//
968// This function returns true if the specified node is alive.
969//
970static bool markAliveIfCanReachAlive(DSNode *N, std::set<DSNode*> &Alive,
971 std::set<DSNode*> &Visited) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000972 if (N == 0) return false;
973
Chris Lattneraa8146f2002-11-10 06:59:55 +0000974 // If we know that this node is alive, return so!
975 if (Alive.count(N)) return true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000976
Chris Lattneraa8146f2002-11-10 06:59:55 +0000977 // Otherwise, we don't think the node is alive yet, check for infinite
978 // recursion.
979 std::set<DSNode*>::iterator VI = Visited.lower_bound(N);
980 if (VI != Visited.end() && *VI == N) return false; // Found a cycle
981 // No recursion, insert into Visited...
982 Visited.insert(VI, N);
983
984 if (N->NodeType & DSNode::GlobalNode)
985 return false; // Global nodes will be marked on their own
986
987 bool ChildrenAreAlive = false;
988
Chris Lattner08db7192002-11-06 06:20:27 +0000989 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
Chris Lattneraa8146f2002-11-10 06:59:55 +0000990 ChildrenAreAlive |= markAliveIfCanReachAlive(N->getLink(i).getNode(),
991 Alive, Visited);
992 if (ChildrenAreAlive)
Chris Lattner5c7380e2003-01-29 21:10:20 +0000993 N->markReachableNodes(Alive);
Chris Lattneraa8146f2002-11-10 06:59:55 +0000994 return ChildrenAreAlive;
995}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000996
Chris Lattneraa8146f2002-11-10 06:59:55 +0000997static bool CallSiteUsesAliveArgs(DSCallSite &CS, std::set<DSNode*> &Alive,
998 std::set<DSNode*> &Visited) {
999 if (markAliveIfCanReachAlive(CS.getRetVal().getNode(), Alive, Visited) ||
1000 markAliveIfCanReachAlive(CS.getCallee().getNode(), Alive, Visited))
1001 return true;
1002 for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
1003 if (markAliveIfCanReachAlive(CS.getPtrArg(j).getNode(), Alive, Visited))
1004 return true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001005 return false;
1006}
1007
Chris Lattnere2219762002-07-18 18:22:40 +00001008// removeDeadNodes - Use a more powerful reachability analysis to eliminate
1009// subgraphs that are unreachable. This often occurs because the data
1010// structure doesn't "escape" into it's caller, and thus should be eliminated
1011// from the caller's graph entirely. This is only appropriate to use when
1012// inlining graphs.
1013//
Chris Lattner394471f2003-01-23 22:05:33 +00001014void DSGraph::removeDeadNodes(unsigned Flags) {
Chris Lattnere2219762002-07-18 18:22:40 +00001015 // Reduce the amount of work we have to do...
Chris Lattnerf40f0a32002-11-09 22:07:02 +00001016 removeTriviallyDeadNodes();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001017
Chris Lattnere2219762002-07-18 18:22:40 +00001018 // FIXME: Merge nontrivially identical call nodes...
1019
1020 // Alive - a set that holds all nodes found to be reachable/alive.
1021 std::set<DSNode*> Alive;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001022 std::vector<std::pair<Value*, DSNode*> > GlobalNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001023
Chris Lattneraa8146f2002-11-10 06:59:55 +00001024 // Mark all nodes reachable by (non-global) scalar nodes as alive...
Chris Lattnerc875f022002-11-03 21:27:48 +00001025 for (std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.begin(),
1026 E = ScalarMap.end(); I != E; ++I)
Chris Lattner394471f2003-01-23 22:05:33 +00001027 if (!(Flags & DSGraph::RemoveUnreachableGlobals) ||
1028 !isa<GlobalValue>(I->first)) // Don't mark globals!
Chris Lattner5c7380e2003-01-29 21:10:20 +00001029 I->second.getNode()->markReachableNodes(Alive);
Chris Lattner394471f2003-01-23 22:05:33 +00001030 else // Keep track of global nodes
1031 GlobalNodes.push_back(std::make_pair(I->first, I->second.getNode()));
Chris Lattnere2219762002-07-18 18:22:40 +00001032
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001033 // The return value is alive as well...
Chris Lattner5c7380e2003-01-29 21:10:20 +00001034 RetNode.getNode()->markReachableNodes(Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001035
Chris Lattneraa8146f2002-11-10 06:59:55 +00001036 // If any global nodes points to a non-global that is "alive", the global is
1037 // "alive" as well...
1038 //
1039 std::set<DSNode*> Visited;
1040 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
1041 markAliveIfCanReachAlive(GlobalNodes[i].second, Alive, Visited);
1042
1043 std::vector<bool> FCallsAlive(FunctionCalls.size());
1044 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
1045 if (CallSiteUsesAliveArgs(FunctionCalls[i], Alive, Visited)) {
Chris Lattner5c7380e2003-01-29 21:10:20 +00001046 FunctionCalls[i].markReachableNodes(Alive);
Chris Lattneraa8146f2002-11-10 06:59:55 +00001047 FCallsAlive[i] = true;
1048 }
1049
1050 std::vector<bool> AuxFCallsAlive(AuxFunctionCalls.size());
1051 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1052 if (CallSiteUsesAliveArgs(AuxFunctionCalls[i], Alive, Visited)) {
Chris Lattner5c7380e2003-01-29 21:10:20 +00001053 AuxFunctionCalls[i].markReachableNodes(Alive);
Chris Lattneraa8146f2002-11-10 06:59:55 +00001054 AuxFCallsAlive[i] = true;
1055 }
1056
1057 // Remove all dead function calls...
1058 unsigned CurIdx = 0;
1059 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
1060 if (FCallsAlive[i])
1061 FunctionCalls[CurIdx++].swap(FunctionCalls[i]);
1062 // Crop all the bad ones out...
1063 FunctionCalls.erase(FunctionCalls.begin()+CurIdx, FunctionCalls.end());
1064
1065 // Remove all dead aux function calls...
1066 CurIdx = 0;
1067 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1068 if (AuxFCallsAlive[i])
1069 AuxFunctionCalls[CurIdx++].swap(AuxFunctionCalls[i]);
1070 // Crop all the bad ones out...
1071 AuxFunctionCalls.erase(AuxFunctionCalls.begin()+CurIdx,
1072 AuxFunctionCalls.end());
1073
1074
1075 // Remove all unreachable globals from the ScalarMap
1076 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
1077 if (!Alive.count(GlobalNodes[i].second))
1078 ScalarMap.erase(GlobalNodes[i].first);
1079
Chris Lattnere2219762002-07-18 18:22:40 +00001080 // Loop over all unreachable nodes, dropping their references...
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001081 vector<DSNode*> DeadNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001082 DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed.
1083 for (unsigned i = 0; i != Nodes.size(); ++i)
1084 if (!Alive.count(Nodes[i])) {
1085 DSNode *N = Nodes[i];
1086 Nodes.erase(Nodes.begin()+i--); // Erase node from alive list.
1087 DeadNodes.push_back(N); // Add node to our list of dead nodes
1088 N->dropAllReferences(); // Drop all outgoing edges
1089 }
1090
Chris Lattnere2219762002-07-18 18:22:40 +00001091 // Delete all dead nodes...
1092 std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
1093}
1094
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001095#if 0
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001096//===----------------------------------------------------------------------===//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001097// GlobalDSGraph Implementation
1098//===----------------------------------------------------------------------===//
1099
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001100#if 0
Chris Lattnerd18f3422002-11-03 21:24:04 +00001101// Bits used in the next function
1102static const char ExternalTypeBits = DSNode::GlobalNode | DSNode::HeapNode;
1103
1104
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001105// GlobalDSGraph::cloneNodeInto - Clone a global node and all its externally
1106// visible target links (and recursively their such links) into this graph.
1107// NodeCache maps the node being cloned to its clone in the Globals graph,
1108// in order to track cycles.
1109// GlobalsAreFinal is a flag that says whether it is safe to assume that
1110// an existing global node is complete. This is important to avoid
1111// reinserting all globals when inserting Calls to functions.
1112// This is a helper function for cloneGlobals and cloneCalls.
1113//
1114DSNode* GlobalDSGraph::cloneNodeInto(DSNode *OldNode,
1115 std::map<const DSNode*, DSNode*> &NodeCache,
1116 bool GlobalsAreFinal) {
1117 if (OldNode == 0) return 0;
1118
1119 // The caller should check this is an external node. Just more efficient...
1120 assert((OldNode->NodeType & ExternalTypeBits) && "Non-external node");
1121
1122 // If a clone has already been created for OldNode, return it.
1123 DSNode*& CacheEntry = NodeCache[OldNode];
1124 if (CacheEntry != 0)
1125 return CacheEntry;
1126
1127 // The result value...
1128 DSNode* NewNode = 0;
1129
1130 // If nodes already exist for any of the globals of OldNode,
1131 // merge all such nodes together since they are merged in OldNode.
1132 // If ValueCacheIsFinal==true, look for an existing node that has
1133 // an identical list of globals and return it if it exists.
1134 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001135 for (unsigned j = 0, N = OldNode->getGlobals().size(); j != N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001136 if (DSNode *PrevNode = ScalarMap[OldNode->getGlobals()[j]].getNode()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001137 if (NewNode == 0) {
1138 NewNode = PrevNode; // first existing node found
1139 if (GlobalsAreFinal && j == 0)
1140 if (OldNode->getGlobals() == PrevNode->getGlobals()) {
1141 CacheEntry = NewNode;
1142 return NewNode;
1143 }
1144 }
1145 else if (NewNode != PrevNode) { // found another, different from prev
1146 // update ValMap *before* merging PrevNode into NewNode
1147 for (unsigned k = 0, NK = PrevNode->getGlobals().size(); k < NK; ++k)
Chris Lattnerc875f022002-11-03 21:27:48 +00001148 ScalarMap[PrevNode->getGlobals()[k]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001149 NewNode->mergeWith(PrevNode);
1150 }
1151 } else if (NewNode != 0) {
Chris Lattnerc875f022002-11-03 21:27:48 +00001152 ScalarMap[OldNode->getGlobals()[j]] = NewNode; // add the merged node
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001153 }
1154
1155 // If no existing node was found, clone the node and update the ValMap.
1156 if (NewNode == 0) {
1157 NewNode = new DSNode(*OldNode);
1158 Nodes.push_back(NewNode);
1159 for (unsigned j = 0, e = NewNode->getNumLinks(); j != e; ++j)
1160 NewNode->setLink(j, 0);
1161 for (unsigned j = 0, N = NewNode->getGlobals().size(); j < N; ++j)
Chris Lattnerc875f022002-11-03 21:27:48 +00001162 ScalarMap[NewNode->getGlobals()[j]] = NewNode;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001163 }
1164 else
1165 NewNode->NodeType |= OldNode->NodeType; // Markers may be different!
1166
1167 // Add the entry to NodeCache
1168 CacheEntry = NewNode;
1169
1170 // Rewrite the links in the new node to point into the current graph,
1171 // but only for links to external nodes. Set other links to NULL.
1172 for (unsigned j = 0, e = OldNode->getNumLinks(); j != e; ++j) {
1173 DSNode* OldTarget = OldNode->getLink(j);
1174 if (OldTarget && (OldTarget->NodeType & ExternalTypeBits)) {
1175 DSNode* NewLink = this->cloneNodeInto(OldTarget, NodeCache);
1176 if (NewNode->getLink(j))
1177 NewNode->getLink(j)->mergeWith(NewLink);
1178 else
1179 NewNode->setLink(j, NewLink);
1180 }
1181 }
1182
1183 // Remove all local markers
1184 NewNode->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode);
1185
1186 return NewNode;
1187}
1188
1189
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001190// GlobalDSGraph::cloneCalls - Clone function calls and their visible target
1191// links (and recursively their such links) into this graph.
1192//
1193void GlobalDSGraph::cloneCalls(DSGraph& Graph) {
1194 std::map<const DSNode*, DSNode*> NodeCache;
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001195 vector<DSCallSite >& FromCalls =Graph.FunctionCalls;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001196
1197 FunctionCalls.reserve(FunctionCalls.size() + FromCalls.size());
1198
1199 for (int i = 0, ei = FromCalls.size(); i < ei; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001200 DSCallSite& callCopy = FunctionCalls.back();
1201 callCopy.reserve(FromCalls[i].size());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001202 for (unsigned j = 0, ej = FromCalls[i].size(); j != ej; ++j)
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001203 callCopy.push_back
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001204 ((FromCalls[i][j] && (FromCalls[i][j]->NodeType & ExternalTypeBits))
1205 ? cloneNodeInto(FromCalls[i][j], NodeCache, true)
1206 : 0);
1207 }
1208
1209 // remove trivially identical function calls
Chris Lattner7541b892002-07-31 19:32:12 +00001210 removeIdenticalCalls(FunctionCalls, "Globals Graph");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001211}
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001212#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001213
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001214#endif