blob: a99c59f927bf08390f645e2115595893260096db [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- DataStructure.cpp - Implement the core data structure analysis -----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00009//
Chris Lattnerc68c31b2002-07-10 22:38:08 +000010// This file implements the core data structure functionality.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerfccd06f2002-10-01 22:33:50 +000014#include "llvm/Analysis/DSGraph.h"
15#include "llvm/Function.h"
Chris Lattnercf14e712004-02-25 23:36:08 +000016#include "llvm/GlobalVariable.h"
Vikram S. Adve26b98262002-10-20 21:41:02 +000017#include "llvm/iOther.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000018#include "llvm/DerivedTypes.h"
Chris Lattner7b7200c2002-10-02 04:57:39 +000019#include "llvm/Target/TargetData.h"
Chris Lattner58f98d02003-07-02 04:38:49 +000020#include "llvm/Assembly/Writer.h"
Chris Lattner0b144872004-01-27 22:03:40 +000021#include "Support/CommandLine.h"
Chris Lattner6806f562003-08-01 22:15:03 +000022#include "Support/Debug.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000023#include "Support/STLExtras.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000024#include "Support/Statistic.h"
Chris Lattner18552922002-11-18 21:44:46 +000025#include "Support/Timer.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000026#include <algorithm>
Chris Lattner9a927292003-11-12 23:11:14 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner08db7192002-11-06 06:20:27 +000029namespace {
Chris Lattnere92e7642004-02-07 23:58:05 +000030 Statistic<> NumFolds ("dsa", "Number of nodes completely folded");
31 Statistic<> NumCallNodesMerged("dsa", "Number of call nodes merged");
32 Statistic<> NumNodeAllocated ("dsa", "Number of nodes allocated");
33 Statistic<> NumDNE ("dsa", "Number of nodes removed by reachability");
Chris Lattnerc3f5f772004-02-08 01:51:48 +000034 Statistic<> NumTrivialDNE ("dsa", "Number of nodes trivially removed");
35 Statistic<> NumTrivialGlobalDNE("dsa", "Number of globals trivially removed");
Chris Lattner08db7192002-11-06 06:20:27 +000036};
37
Chris Lattnera88a55c2004-01-28 02:41:32 +000038#if 1
Chris Lattner93ddd7e2004-01-22 16:36:28 +000039#define TIME_REGION(VARNAME, DESC) \
40 NamedRegionTimer VARNAME(DESC)
41#else
42#define TIME_REGION(VARNAME, DESC)
43#endif
44
Chris Lattnerb1060432002-11-07 05:20:53 +000045using namespace DS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000046
Chris Lattner731b2d72003-02-13 19:09:00 +000047DSNode *DSNodeHandle::HandleForwarding() const {
Chris Lattner4ff0b962004-02-08 01:27:18 +000048 assert(N->isForwarding() && "Can only be invoked if forwarding!");
Chris Lattner731b2d72003-02-13 19:09:00 +000049
50 // Handle node forwarding here!
51 DSNode *Next = N->ForwardNH.getNode(); // Cause recursive shrinkage
52 Offset += N->ForwardNH.getOffset();
53
54 if (--N->NumReferrers == 0) {
55 // Removing the last referrer to the node, sever the forwarding link
56 N->stopForwarding();
57 }
58
59 N = Next;
60 N->NumReferrers++;
61 if (N->Size <= Offset) {
62 assert(N->Size <= 1 && "Forwarded to shrunk but not collapsed node?");
63 Offset = 0;
64 }
65 return N;
66}
67
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000068//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +000069// DSNode Implementation
70//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000071
Chris Lattnerbd92b732003-06-19 21:15:11 +000072DSNode::DSNode(const Type *T, DSGraph *G)
Chris Lattner70793862003-07-02 23:57:05 +000073 : NumReferrers(0), Size(0), ParentGraph(G), Ty(Type::VoidTy), NodeType(0) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +000074 // Add the type entry if it is specified...
Chris Lattner08db7192002-11-06 06:20:27 +000075 if (T) mergeTypeInfo(T, 0);
Chris Lattner9857c1a2004-02-08 01:05:37 +000076 if (G) G->addNode(this);
Chris Lattner0b144872004-01-27 22:03:40 +000077 ++NumNodeAllocated;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000078}
79
Chris Lattner0d9bab82002-07-18 00:12:30 +000080// DSNode copy constructor... do not copy over the referrers list!
Chris Lattner0b144872004-01-27 22:03:40 +000081DSNode::DSNode(const DSNode &N, DSGraph *G, bool NullLinks)
Chris Lattner70793862003-07-02 23:57:05 +000082 : NumReferrers(0), Size(N.Size), ParentGraph(G),
Chris Lattner0b144872004-01-27 22:03:40 +000083 Ty(N.Ty), Globals(N.Globals), NodeType(N.NodeType) {
84 if (!NullLinks)
85 Links = N.Links;
86 else
87 Links.resize(N.Links.size()); // Create the appropriate number of null links
Chris Lattnere92e7642004-02-07 23:58:05 +000088 G->addNode(this);
Chris Lattner0b144872004-01-27 22:03:40 +000089 ++NumNodeAllocated;
Chris Lattner0d9bab82002-07-18 00:12:30 +000090}
91
Chris Lattner15869aa2003-11-02 22:27:28 +000092/// getTargetData - Get the target data object used to construct this node.
93///
94const TargetData &DSNode::getTargetData() const {
95 return ParentGraph->getTargetData();
96}
97
Chris Lattner72d29a42003-02-11 23:11:51 +000098void DSNode::assertOK() const {
99 assert((Ty != Type::VoidTy ||
100 Ty == Type::VoidTy && (Size == 0 ||
101 (NodeType & DSNode::Array))) &&
102 "Node not OK!");
Chris Lattner85cfe012003-07-03 02:03:53 +0000103
104 assert(ParentGraph && "Node has no parent?");
Chris Lattner62482e52004-01-28 09:15:42 +0000105 const DSScalarMap &SM = ParentGraph->getScalarMap();
Chris Lattner85cfe012003-07-03 02:03:53 +0000106 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
Chris Lattnera88a55c2004-01-28 02:41:32 +0000107 assert(SM.count(Globals[i]));
Chris Lattner85cfe012003-07-03 02:03:53 +0000108 assert(SM.find(Globals[i])->second.getNode() == this);
109 }
Chris Lattner72d29a42003-02-11 23:11:51 +0000110}
111
112/// forwardNode - Mark this node as being obsolete, and all references to it
113/// should be forwarded to the specified node and offset.
114///
115void DSNode::forwardNode(DSNode *To, unsigned Offset) {
116 assert(this != To && "Cannot forward a node to itself!");
117 assert(ForwardNH.isNull() && "Already forwarding from this node!");
118 if (To->Size <= 1) Offset = 0;
119 assert((Offset < To->Size || (Offset == To->Size && Offset == 0)) &&
120 "Forwarded offset is wrong!");
121 ForwardNH.setNode(To);
122 ForwardNH.setOffset(Offset);
123 NodeType = DEAD;
124 Size = 0;
125 Ty = Type::VoidTy;
Chris Lattner4ff0b962004-02-08 01:27:18 +0000126
127 // Remove this node from the parent graph's Nodes list.
128 ParentGraph->unlinkNode(this);
129 ParentGraph = 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000130}
131
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000132// addGlobal - Add an entry for a global value to the Globals list. This also
133// marks the node with the 'G' flag if it does not already have it.
134//
135void DSNode::addGlobal(GlobalValue *GV) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000136 // Keep the list sorted.
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000137 std::vector<GlobalValue*>::iterator I =
Chris Lattner0d9bab82002-07-18 00:12:30 +0000138 std::lower_bound(Globals.begin(), Globals.end(), GV);
139
140 if (I == Globals.end() || *I != GV) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000141 //assert(GV->getType()->getElementType() == Ty);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000142 Globals.insert(I, GV);
143 NodeType |= GlobalNode;
144 }
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000145}
146
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000147/// foldNodeCompletely - If we determine that this node has some funny
148/// behavior happening to it that we cannot represent, we fold it down to a
149/// single, completely pessimistic, node. This node is represented as a
150/// single byte with a single TypeEntry of "void".
151///
152void DSNode::foldNodeCompletely() {
Chris Lattner72d29a42003-02-11 23:11:51 +0000153 if (isNodeCompletelyFolded()) return; // If this node is already folded...
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000154
Chris Lattner08db7192002-11-06 06:20:27 +0000155 ++NumFolds;
156
Chris Lattner0b144872004-01-27 22:03:40 +0000157 // If this node has a size that is <= 1, we don't need to create a forwarding
158 // node.
159 if (getSize() <= 1) {
160 NodeType |= DSNode::Array;
161 Ty = Type::VoidTy;
162 Size = 1;
163 assert(Links.size() <= 1 && "Size is 1, but has more links?");
164 Links.resize(1);
Chris Lattner72d29a42003-02-11 23:11:51 +0000165 } else {
Chris Lattner0b144872004-01-27 22:03:40 +0000166 // Create the node we are going to forward to. This is required because
167 // some referrers may have an offset that is > 0. By forcing them to
168 // forward, the forwarder has the opportunity to correct the offset.
169 DSNode *DestNode = new DSNode(0, ParentGraph);
170 DestNode->NodeType = NodeType|DSNode::Array;
171 DestNode->Ty = Type::VoidTy;
172 DestNode->Size = 1;
173 DestNode->Globals.swap(Globals);
174
175 // Start forwarding to the destination node...
176 forwardNode(DestNode, 0);
177
178 if (!Links.empty()) {
179 DestNode->Links.reserve(1);
180
181 DSNodeHandle NH(DestNode);
182 DestNode->Links.push_back(Links[0]);
183
184 // If we have links, merge all of our outgoing links together...
185 for (unsigned i = Links.size()-1; i != 0; --i)
186 NH.getNode()->Links[0].mergeWith(Links[i]);
187 Links.clear();
188 } else {
189 DestNode->Links.resize(1);
190 }
Chris Lattner72d29a42003-02-11 23:11:51 +0000191 }
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000192}
Chris Lattner076c1f92002-11-07 06:31:54 +0000193
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000194/// isNodeCompletelyFolded - Return true if this node has been completely
195/// folded down to something that can never be expanded, effectively losing
196/// all of the field sensitivity that may be present in the node.
197///
198bool DSNode::isNodeCompletelyFolded() const {
Chris Lattner18552922002-11-18 21:44:46 +0000199 return getSize() == 1 && Ty == Type::VoidTy && isArray();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000200}
201
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000202namespace {
203 /// TypeElementWalker Class - Used for implementation of physical subtyping...
204 ///
205 class TypeElementWalker {
206 struct StackState {
207 const Type *Ty;
208 unsigned Offset;
209 unsigned Idx;
210 StackState(const Type *T, unsigned Off = 0)
211 : Ty(T), Offset(Off), Idx(0) {}
212 };
213
214 std::vector<StackState> Stack;
Chris Lattner15869aa2003-11-02 22:27:28 +0000215 const TargetData &TD;
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000216 public:
Chris Lattner15869aa2003-11-02 22:27:28 +0000217 TypeElementWalker(const Type *T, const TargetData &td) : TD(td) {
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000218 Stack.push_back(T);
219 StepToLeaf();
220 }
221
222 bool isDone() const { return Stack.empty(); }
223 const Type *getCurrentType() const { return Stack.back().Ty; }
224 unsigned getCurrentOffset() const { return Stack.back().Offset; }
225
226 void StepToNextType() {
227 PopStackAndAdvance();
228 StepToLeaf();
229 }
230
231 private:
232 /// PopStackAndAdvance - Pop the current element off of the stack and
233 /// advance the underlying element to the next contained member.
234 void PopStackAndAdvance() {
235 assert(!Stack.empty() && "Cannot pop an empty stack!");
236 Stack.pop_back();
237 while (!Stack.empty()) {
238 StackState &SS = Stack.back();
239 if (const StructType *ST = dyn_cast<StructType>(SS.Ty)) {
240 ++SS.Idx;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000241 if (SS.Idx != ST->getNumElements()) {
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000242 const StructLayout *SL = TD.getStructLayout(ST);
243 SS.Offset += SL->MemberOffsets[SS.Idx]-SL->MemberOffsets[SS.Idx-1];
244 return;
245 }
246 Stack.pop_back(); // At the end of the structure
247 } else {
248 const ArrayType *AT = cast<ArrayType>(SS.Ty);
249 ++SS.Idx;
250 if (SS.Idx != AT->getNumElements()) {
251 SS.Offset += TD.getTypeSize(AT->getElementType());
252 return;
253 }
254 Stack.pop_back(); // At the end of the array
255 }
256 }
257 }
258
259 /// StepToLeaf - Used by physical subtyping to move to the first leaf node
260 /// on the type stack.
261 void StepToLeaf() {
262 if (Stack.empty()) return;
263 while (!Stack.empty() && !Stack.back().Ty->isFirstClassType()) {
264 StackState &SS = Stack.back();
265 if (const StructType *ST = dyn_cast<StructType>(SS.Ty)) {
Chris Lattnerd21cd802004-02-09 04:37:31 +0000266 if (ST->getNumElements() == 0) {
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000267 assert(SS.Idx == 0);
268 PopStackAndAdvance();
269 } else {
270 // Step into the structure...
Chris Lattnerd21cd802004-02-09 04:37:31 +0000271 assert(SS.Idx < ST->getNumElements());
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000272 const StructLayout *SL = TD.getStructLayout(ST);
Chris Lattnerd21cd802004-02-09 04:37:31 +0000273 Stack.push_back(StackState(ST->getElementType(SS.Idx),
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000274 SS.Offset+SL->MemberOffsets[SS.Idx]));
275 }
276 } else {
277 const ArrayType *AT = cast<ArrayType>(SS.Ty);
278 if (AT->getNumElements() == 0) {
279 assert(SS.Idx == 0);
280 PopStackAndAdvance();
281 } else {
282 // Step into the array...
283 assert(SS.Idx < AT->getNumElements());
284 Stack.push_back(StackState(AT->getElementType(),
285 SS.Offset+SS.Idx*
286 TD.getTypeSize(AT->getElementType())));
287 }
288 }
289 }
290 }
291 };
Brian Gaeked0fde302003-11-11 22:41:34 +0000292} // end anonymous namespace
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000293
294/// ElementTypesAreCompatible - Check to see if the specified types are
295/// "physically" compatible. If so, return true, else return false. We only
Chris Lattnerdbfe36e2003-11-02 21:02:20 +0000296/// have to check the fields in T1: T2 may be larger than T1. If AllowLargerT1
297/// is true, then we also allow a larger T1.
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000298///
Chris Lattnerdbfe36e2003-11-02 21:02:20 +0000299static bool ElementTypesAreCompatible(const Type *T1, const Type *T2,
Chris Lattner15869aa2003-11-02 22:27:28 +0000300 bool AllowLargerT1, const TargetData &TD){
301 TypeElementWalker T1W(T1, TD), T2W(T2, TD);
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000302
303 while (!T1W.isDone() && !T2W.isDone()) {
304 if (T1W.getCurrentOffset() != T2W.getCurrentOffset())
305 return false;
306
307 const Type *T1 = T1W.getCurrentType();
308 const Type *T2 = T2W.getCurrentType();
309 if (T1 != T2 && !T1->isLosslesslyConvertibleTo(T2))
310 return false;
311
312 T1W.StepToNextType();
313 T2W.StepToNextType();
314 }
315
Chris Lattnerdbfe36e2003-11-02 21:02:20 +0000316 return AllowLargerT1 || T1W.isDone();
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000317}
318
319
Chris Lattner08db7192002-11-06 06:20:27 +0000320/// mergeTypeInfo - This method merges the specified type into the current node
321/// at the specified offset. This may update the current node's type record if
322/// this gives more information to the node, it may do nothing to the node if
323/// this information is already known, or it may merge the node completely (and
324/// return true) if the information is incompatible with what is already known.
Chris Lattner7b7200c2002-10-02 04:57:39 +0000325///
Chris Lattner08db7192002-11-06 06:20:27 +0000326/// This method returns true if the node is completely folded, otherwise false.
327///
Chris Lattner088b6392003-03-03 17:13:31 +0000328bool DSNode::mergeTypeInfo(const Type *NewTy, unsigned Offset,
329 bool FoldIfIncompatible) {
Chris Lattner15869aa2003-11-02 22:27:28 +0000330 const TargetData &TD = getTargetData();
Chris Lattner08db7192002-11-06 06:20:27 +0000331 // Check to make sure the Size member is up-to-date. Size can be one of the
332 // following:
333 // Size = 0, Ty = Void: Nothing is known about this node.
334 // Size = 0, Ty = FnTy: FunctionPtr doesn't have a size, so we use zero
335 // Size = 1, Ty = Void, Array = 1: The node is collapsed
336 // Otherwise, sizeof(Ty) = Size
337 //
Chris Lattner18552922002-11-18 21:44:46 +0000338 assert(((Size == 0 && Ty == Type::VoidTy && !isArray()) ||
339 (Size == 0 && !Ty->isSized() && !isArray()) ||
340 (Size == 1 && Ty == Type::VoidTy && isArray()) ||
341 (Size == 0 && !Ty->isSized() && !isArray()) ||
342 (TD.getTypeSize(Ty) == Size)) &&
Chris Lattner08db7192002-11-06 06:20:27 +0000343 "Size member of DSNode doesn't match the type structure!");
344 assert(NewTy != Type::VoidTy && "Cannot merge void type into DSNode!");
Chris Lattner7b7200c2002-10-02 04:57:39 +0000345
Chris Lattner18552922002-11-18 21:44:46 +0000346 if (Offset == 0 && NewTy == Ty)
Chris Lattner08db7192002-11-06 06:20:27 +0000347 return false; // This should be a common case, handle it efficiently
Chris Lattner7b7200c2002-10-02 04:57:39 +0000348
Chris Lattner08db7192002-11-06 06:20:27 +0000349 // Return true immediately if the node is completely folded.
350 if (isNodeCompletelyFolded()) return true;
351
Chris Lattner23f83dc2002-11-08 22:49:57 +0000352 // If this is an array type, eliminate the outside arrays because they won't
353 // be used anyway. This greatly reduces the size of large static arrays used
354 // as global variables, for example.
355 //
Chris Lattnerd8888932002-11-09 19:25:27 +0000356 bool WillBeArray = false;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000357 while (const ArrayType *AT = dyn_cast<ArrayType>(NewTy)) {
358 // FIXME: we might want to keep small arrays, but must be careful about
359 // things like: [2 x [10000 x int*]]
360 NewTy = AT->getElementType();
Chris Lattnerd8888932002-11-09 19:25:27 +0000361 WillBeArray = true;
Chris Lattner23f83dc2002-11-08 22:49:57 +0000362 }
363
Chris Lattner08db7192002-11-06 06:20:27 +0000364 // Figure out how big the new type we're merging in is...
365 unsigned NewTySize = NewTy->isSized() ? TD.getTypeSize(NewTy) : 0;
366
367 // Otherwise check to see if we can fold this type into the current node. If
368 // we can't, we fold the node completely, if we can, we potentially update our
369 // internal state.
370 //
Chris Lattner18552922002-11-18 21:44:46 +0000371 if (Ty == Type::VoidTy) {
Chris Lattner08db7192002-11-06 06:20:27 +0000372 // If this is the first type that this node has seen, just accept it without
373 // question....
Chris Lattnerdbfe36e2003-11-02 21:02:20 +0000374 assert(Offset == 0 && !isArray() &&
375 "Cannot have an offset into a void node!");
Chris Lattner18552922002-11-18 21:44:46 +0000376 Ty = NewTy;
377 NodeType &= ~Array;
378 if (WillBeArray) NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +0000379 Size = NewTySize;
380
381 // Calculate the number of outgoing links from this node.
382 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
383 return false;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000384 }
Chris Lattner08db7192002-11-06 06:20:27 +0000385
386 // Handle node expansion case here...
387 if (Offset+NewTySize > Size) {
388 // It is illegal to grow this node if we have treated it as an array of
389 // objects...
Chris Lattner18552922002-11-18 21:44:46 +0000390 if (isArray()) {
Chris Lattner088b6392003-03-03 17:13:31 +0000391 if (FoldIfIncompatible) foldNodeCompletely();
Chris Lattner08db7192002-11-06 06:20:27 +0000392 return true;
393 }
394
395 if (Offset) { // We could handle this case, but we don't for now...
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000396 std::cerr << "UNIMP: Trying to merge a growth type into "
397 << "offset != 0: Collapsing!\n";
Chris Lattner088b6392003-03-03 17:13:31 +0000398 if (FoldIfIncompatible) foldNodeCompletely();
Chris Lattner08db7192002-11-06 06:20:27 +0000399 return true;
400 }
401
402 // Okay, the situation is nice and simple, we are trying to merge a type in
403 // at offset 0 that is bigger than our current type. Implement this by
404 // switching to the new type and then merge in the smaller one, which should
405 // hit the other code path here. If the other code path decides it's not
406 // ok, it will collapse the node as appropriate.
407 //
Chris Lattner18552922002-11-18 21:44:46 +0000408 const Type *OldTy = Ty;
409 Ty = NewTy;
410 NodeType &= ~Array;
411 if (WillBeArray) NodeType |= Array;
Chris Lattner08db7192002-11-06 06:20:27 +0000412 Size = NewTySize;
413
414 // Must grow links to be the appropriate size...
415 Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift);
416
417 // Merge in the old type now... which is guaranteed to be smaller than the
418 // "current" type.
419 return mergeTypeInfo(OldTy, 0);
420 }
421
Chris Lattnerf17b39a2002-11-07 04:59:28 +0000422 assert(Offset <= Size &&
Chris Lattner08db7192002-11-06 06:20:27 +0000423 "Cannot merge something into a part of our type that doesn't exist!");
424
Chris Lattner18552922002-11-18 21:44:46 +0000425 // Find the section of Ty that NewTy overlaps with... first we find the
Chris Lattner08db7192002-11-06 06:20:27 +0000426 // type that starts at offset Offset.
427 //
428 unsigned O = 0;
Chris Lattner18552922002-11-18 21:44:46 +0000429 const Type *SubType = Ty;
Chris Lattner08db7192002-11-06 06:20:27 +0000430 while (O < Offset) {
431 assert(Offset-O < TD.getTypeSize(SubType) && "Offset out of range!");
432
433 switch (SubType->getPrimitiveID()) {
434 case Type::StructTyID: {
435 const StructType *STy = cast<StructType>(SubType);
436 const StructLayout &SL = *TD.getStructLayout(STy);
437
438 unsigned i = 0, e = SL.MemberOffsets.size();
439 for (; i+1 < e && SL.MemberOffsets[i+1] <= Offset-O; ++i)
440 /* empty */;
441
442 // The offset we are looking for must be in the i'th element...
Chris Lattnerd21cd802004-02-09 04:37:31 +0000443 SubType = STy->getElementType(i);
Chris Lattner08db7192002-11-06 06:20:27 +0000444 O += SL.MemberOffsets[i];
445 break;
446 }
447 case Type::ArrayTyID: {
448 SubType = cast<ArrayType>(SubType)->getElementType();
449 unsigned ElSize = TD.getTypeSize(SubType);
450 unsigned Remainder = (Offset-O) % ElSize;
451 O = Offset-Remainder;
452 break;
453 }
454 default:
Chris Lattner088b6392003-03-03 17:13:31 +0000455 if (FoldIfIncompatible) foldNodeCompletely();
Chris Lattner0ac7d5c2003-02-03 19:12:15 +0000456 return true;
Chris Lattner08db7192002-11-06 06:20:27 +0000457 }
458 }
459
460 assert(O == Offset && "Could not achieve the correct offset!");
461
462 // If we found our type exactly, early exit
463 if (SubType == NewTy) return false;
464
Chris Lattner0b144872004-01-27 22:03:40 +0000465 // Differing function types don't require us to merge. They are not values anyway.
466 if (isa<FunctionType>(SubType) &&
467 isa<FunctionType>(NewTy)) return false;
468
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000469 unsigned SubTypeSize = SubType->isSized() ? TD.getTypeSize(SubType) : 0;
470
471 // Ok, we are getting desperate now. Check for physical subtyping, where we
472 // just require each element in the node to be compatible.
Chris Lattner06e24c82003-06-29 22:36:31 +0000473 if (NewTySize <= SubTypeSize && NewTySize && NewTySize < 256 &&
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000474 SubTypeSize && SubTypeSize < 256 &&
Chris Lattner15869aa2003-11-02 22:27:28 +0000475 ElementTypesAreCompatible(NewTy, SubType, !isArray(), TD))
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000476 return false;
477
Chris Lattner08db7192002-11-06 06:20:27 +0000478 // Okay, so we found the leader type at the offset requested. Search the list
479 // of types that starts at this offset. If SubType is currently an array or
480 // structure, the type desired may actually be the first element of the
481 // composite type...
482 //
Chris Lattner18552922002-11-18 21:44:46 +0000483 unsigned PadSize = SubTypeSize; // Size, including pad memory which is ignored
Chris Lattner08db7192002-11-06 06:20:27 +0000484 while (SubType != NewTy) {
485 const Type *NextSubType = 0;
Chris Lattnerbf10f052002-11-09 00:49:05 +0000486 unsigned NextSubTypeSize = 0;
Chris Lattner18552922002-11-18 21:44:46 +0000487 unsigned NextPadSize = 0;
Chris Lattner08db7192002-11-06 06:20:27 +0000488 switch (SubType->getPrimitiveID()) {
Chris Lattner18552922002-11-18 21:44:46 +0000489 case Type::StructTyID: {
490 const StructType *STy = cast<StructType>(SubType);
491 const StructLayout &SL = *TD.getStructLayout(STy);
492 if (SL.MemberOffsets.size() > 1)
493 NextPadSize = SL.MemberOffsets[1];
494 else
495 NextPadSize = SubTypeSize;
Chris Lattnerd21cd802004-02-09 04:37:31 +0000496 NextSubType = STy->getElementType(0);
Chris Lattner18552922002-11-18 21:44:46 +0000497 NextSubTypeSize = TD.getTypeSize(NextSubType);
Chris Lattner08db7192002-11-06 06:20:27 +0000498 break;
Chris Lattner18552922002-11-18 21:44:46 +0000499 }
Chris Lattner08db7192002-11-06 06:20:27 +0000500 case Type::ArrayTyID:
501 NextSubType = cast<ArrayType>(SubType)->getElementType();
Chris Lattner18552922002-11-18 21:44:46 +0000502 NextSubTypeSize = TD.getTypeSize(NextSubType);
503 NextPadSize = NextSubTypeSize;
Chris Lattner08db7192002-11-06 06:20:27 +0000504 break;
505 default: ;
506 // fall out
507 }
508
509 if (NextSubType == 0)
510 break; // In the default case, break out of the loop
511
Chris Lattner18552922002-11-18 21:44:46 +0000512 if (NextPadSize < NewTySize)
Chris Lattner08db7192002-11-06 06:20:27 +0000513 break; // Don't allow shrinking to a smaller type than NewTySize
514 SubType = NextSubType;
515 SubTypeSize = NextSubTypeSize;
Chris Lattner18552922002-11-18 21:44:46 +0000516 PadSize = NextPadSize;
Chris Lattner08db7192002-11-06 06:20:27 +0000517 }
518
519 // If we found the type exactly, return it...
520 if (SubType == NewTy)
521 return false;
522
523 // Check to see if we have a compatible, but different type...
524 if (NewTySize == SubTypeSize) {
Misha Brukmanf117cc92003-05-20 18:45:36 +0000525 // Check to see if this type is obviously convertible... int -> uint f.e.
526 if (NewTy->isLosslesslyConvertibleTo(SubType))
Chris Lattner08db7192002-11-06 06:20:27 +0000527 return false;
528
529 // Check to see if we have a pointer & integer mismatch going on here,
530 // loading a pointer as a long, for example.
531 //
532 if (SubType->isInteger() && isa<PointerType>(NewTy) ||
533 NewTy->isInteger() && isa<PointerType>(SubType))
534 return false;
Chris Lattner18552922002-11-18 21:44:46 +0000535 } else if (NewTySize > SubTypeSize && NewTySize <= PadSize) {
536 // We are accessing the field, plus some structure padding. Ignore the
537 // structure padding.
538 return false;
Chris Lattner08db7192002-11-06 06:20:27 +0000539 }
540
Chris Lattner58f98d02003-07-02 04:38:49 +0000541 Module *M = 0;
Chris Lattner58f98d02003-07-02 04:38:49 +0000542 if (getParentGraph()->getReturnNodes().size())
543 M = getParentGraph()->getReturnNodes().begin()->first->getParent();
Chris Lattner58f98d02003-07-02 04:38:49 +0000544 DEBUG(std::cerr << "MergeTypeInfo Folding OrigTy: ";
545 WriteTypeSymbolic(std::cerr, Ty, M) << "\n due to:";
546 WriteTypeSymbolic(std::cerr, NewTy, M) << " @ " << Offset << "!\n"
547 << "SubType: ";
548 WriteTypeSymbolic(std::cerr, SubType, M) << "\n\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000549
Chris Lattner088b6392003-03-03 17:13:31 +0000550 if (FoldIfIncompatible) foldNodeCompletely();
Chris Lattner08db7192002-11-06 06:20:27 +0000551 return true;
Chris Lattner7b7200c2002-10-02 04:57:39 +0000552}
553
Chris Lattner08db7192002-11-06 06:20:27 +0000554
555
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000556// addEdgeTo - Add an edge from the current node to the specified node. This
557// can cause merging of nodes in the graph.
558//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000559void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) {
Chris Lattner0b144872004-01-27 22:03:40 +0000560 if (NH.isNull()) return; // Nothing to do
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000561
Chris Lattner08db7192002-11-06 06:20:27 +0000562 DSNodeHandle &ExistingEdge = getLink(Offset);
Chris Lattner0b144872004-01-27 22:03:40 +0000563 if (!ExistingEdge.isNull()) {
Chris Lattner7b7200c2002-10-02 04:57:39 +0000564 // Merge the two nodes...
Chris Lattner08db7192002-11-06 06:20:27 +0000565 ExistingEdge.mergeWith(NH);
Chris Lattner7b7200c2002-10-02 04:57:39 +0000566 } else { // No merging to perform...
567 setLink(Offset, NH); // Just force a link in there...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000568 }
Chris Lattner7b7200c2002-10-02 04:57:39 +0000569}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000570
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000571
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000572// MergeSortedVectors - Efficiently merge a vector into another vector where
573// duplicates are not allowed and both are sorted. This assumes that 'T's are
574// efficiently copyable and have sane comparison semantics.
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000575//
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000576static void MergeSortedVectors(std::vector<GlobalValue*> &Dest,
577 const std::vector<GlobalValue*> &Src) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000578 // By far, the most common cases will be the simple ones. In these cases,
579 // avoid having to allocate a temporary vector...
580 //
581 if (Src.empty()) { // Nothing to merge in...
582 return;
583 } else if (Dest.empty()) { // Just copy the result in...
584 Dest = Src;
585 } else if (Src.size() == 1) { // Insert a single element...
Chris Lattner18552922002-11-18 21:44:46 +0000586 const GlobalValue *V = Src[0];
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000587 std::vector<GlobalValue*>::iterator I =
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000588 std::lower_bound(Dest.begin(), Dest.end(), V);
589 if (I == Dest.end() || *I != Src[0]) // If not already contained...
590 Dest.insert(I, Src[0]);
591 } else if (Dest.size() == 1) {
Chris Lattner18552922002-11-18 21:44:46 +0000592 GlobalValue *Tmp = Dest[0]; // Save value in temporary...
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000593 Dest = Src; // Copy over list...
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000594 std::vector<GlobalValue*>::iterator I =
Chris Lattner5190ce82002-11-12 07:20:45 +0000595 std::lower_bound(Dest.begin(), Dest.end(), Tmp);
596 if (I == Dest.end() || *I != Tmp) // If not already contained...
597 Dest.insert(I, Tmp);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000598
599 } else {
600 // Make a copy to the side of Dest...
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000601 std::vector<GlobalValue*> Old(Dest);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000602
603 // Make space for all of the type entries now...
604 Dest.resize(Dest.size()+Src.size());
605
606 // Merge the two sorted ranges together... into Dest.
607 std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin());
608
609 // Now erase any duplicate entries that may have accumulated into the
610 // vectors (because they were in both of the input sets)
611 Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end());
612 }
613}
614
Chris Lattner0b144872004-01-27 22:03:40 +0000615void DSNode::mergeGlobals(const std::vector<GlobalValue*> &RHS) {
616 MergeSortedVectors(Globals, RHS);
617}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000618
Chris Lattner0b144872004-01-27 22:03:40 +0000619// MergeNodes - Helper function for DSNode::mergeWith().
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000620// This function does the hard work of merging two nodes, CurNodeH
621// and NH after filtering out trivial cases and making sure that
622// CurNodeH.offset >= NH.offset.
623//
624// ***WARNING***
625// Since merging may cause either node to go away, we must always
626// use the node-handles to refer to the nodes. These node handles are
627// automatically updated during merging, so will always provide access
628// to the correct node after a merge.
629//
630void DSNode::MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH) {
631 assert(CurNodeH.getOffset() >= NH.getOffset() &&
632 "This should have been enforced in the caller.");
633
634 // Now we know that Offset >= NH.Offset, so convert it so our "Offset" (with
635 // respect to NH.Offset) is now zero. NOffset is the distance from the base
636 // of our object that N starts from.
637 //
638 unsigned NOffset = CurNodeH.getOffset()-NH.getOffset();
639 unsigned NSize = NH.getNode()->getSize();
640
Chris Lattner5c5b10f2003-06-29 20:27:45 +0000641 // If the two nodes are of different size, and the smaller node has the array
642 // bit set, collapse!
643 if (NSize != CurNodeH.getNode()->getSize()) {
644 if (NSize < CurNodeH.getNode()->getSize()) {
645 if (NH.getNode()->isArray())
646 NH.getNode()->foldNodeCompletely();
647 } else if (CurNodeH.getNode()->isArray()) {
648 NH.getNode()->foldNodeCompletely();
649 }
650 }
651
652 // Merge the type entries of the two nodes together...
Chris Lattner72d29a42003-02-11 23:11:51 +0000653 if (NH.getNode()->Ty != Type::VoidTy)
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000654 CurNodeH.getNode()->mergeTypeInfo(NH.getNode()->Ty, NOffset);
Chris Lattnerbd92b732003-06-19 21:15:11 +0000655 assert(!CurNodeH.getNode()->isDeadNode());
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000656
657 // If we are merging a node with a completely folded node, then both nodes are
658 // now completely folded.
659 //
660 if (CurNodeH.getNode()->isNodeCompletelyFolded()) {
661 if (!NH.getNode()->isNodeCompletelyFolded()) {
662 NH.getNode()->foldNodeCompletely();
Chris Lattner72d29a42003-02-11 23:11:51 +0000663 assert(NH.getNode() && NH.getOffset() == 0 &&
664 "folding did not make offset 0?");
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000665 NOffset = NH.getOffset();
666 NSize = NH.getNode()->getSize();
667 assert(NOffset == 0 && NSize == 1);
668 }
669 } else if (NH.getNode()->isNodeCompletelyFolded()) {
670 CurNodeH.getNode()->foldNodeCompletely();
Chris Lattner72d29a42003-02-11 23:11:51 +0000671 assert(CurNodeH.getNode() && CurNodeH.getOffset() == 0 &&
672 "folding did not make offset 0?");
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000673 NOffset = NH.getOffset();
674 NSize = NH.getNode()->getSize();
675 assert(NOffset == 0 && NSize == 1);
676 }
677
Chris Lattner72d29a42003-02-11 23:11:51 +0000678 DSNode *N = NH.getNode();
679 if (CurNodeH.getNode() == N || N == 0) return;
Chris Lattnerbd92b732003-06-19 21:15:11 +0000680 assert(!CurNodeH.getNode()->isDeadNode());
681
Chris Lattner0b144872004-01-27 22:03:40 +0000682 // Merge the NodeType information.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000683 CurNodeH.getNode()->NodeType |= N->NodeType;
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000684
Chris Lattner72d29a42003-02-11 23:11:51 +0000685 // Start forwarding to the new node!
Chris Lattner72d29a42003-02-11 23:11:51 +0000686 N->forwardNode(CurNodeH.getNode(), NOffset);
Chris Lattnerbd92b732003-06-19 21:15:11 +0000687 assert(!CurNodeH.getNode()->isDeadNode());
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000688
Chris Lattner72d29a42003-02-11 23:11:51 +0000689 // Make all of the outgoing links of N now be outgoing links of CurNodeH.
690 //
691 for (unsigned i = 0; i < N->getNumLinks(); ++i) {
692 DSNodeHandle &Link = N->getLink(i << DS::PointerShift);
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000693 if (Link.getNode()) {
694 // Compute the offset into the current node at which to
695 // merge this link. In the common case, this is a linear
696 // relation to the offset in the original node (with
697 // wrapping), but if the current node gets collapsed due to
698 // recursive merging, we must make sure to merge in all remaining
699 // links at offset zero.
700 unsigned MergeOffset = 0;
Chris Lattner72d29a42003-02-11 23:11:51 +0000701 DSNode *CN = CurNodeH.getNode();
702 if (CN->Size != 1)
703 MergeOffset = ((i << DS::PointerShift)+NOffset) % CN->getSize();
704 CN->addEdgeTo(MergeOffset, Link);
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000705 }
706 }
707
708 // Now that there are no outgoing edges, all of the Links are dead.
Chris Lattner72d29a42003-02-11 23:11:51 +0000709 N->Links.clear();
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000710
711 // Merge the globals list...
Chris Lattner72d29a42003-02-11 23:11:51 +0000712 if (!N->Globals.empty()) {
Chris Lattner0b144872004-01-27 22:03:40 +0000713 CurNodeH.getNode()->mergeGlobals(N->Globals);
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000714
715 // Delete the globals from the old node...
Chris Lattner72d29a42003-02-11 23:11:51 +0000716 std::vector<GlobalValue*>().swap(N->Globals);
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000717 }
718}
719
720
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000721// mergeWith - Merge this node and the specified node, moving all links to and
722// from the argument node into the current node, deleting the node argument.
723// Offset indicates what offset the specified node is to be merged into the
724// current node.
725//
Chris Lattner5254a8d2004-01-22 16:31:08 +0000726// The specified node may be a null pointer (in which case, we update it to
727// point to this node).
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000728//
729void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) {
730 DSNode *N = NH.getNode();
Chris Lattner5254a8d2004-01-22 16:31:08 +0000731 if (N == this && NH.getOffset() == Offset)
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000732 return; // Noop
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000733
Chris Lattner5254a8d2004-01-22 16:31:08 +0000734 // If the RHS is a null node, make it point to this node!
735 if (N == 0) {
736 NH.mergeWith(DSNodeHandle(this, Offset));
737 return;
738 }
739
Chris Lattnerbd92b732003-06-19 21:15:11 +0000740 assert(!N->isDeadNode() && !isDeadNode());
Chris Lattner679e8e12002-11-08 21:27:12 +0000741 assert(!hasNoReferrers() && "Should not try to fold a useless node!");
742
Chris Lattner02606632002-11-04 06:48:26 +0000743 if (N == this) {
Chris Lattner08db7192002-11-06 06:20:27 +0000744 // We cannot merge two pieces of the same node together, collapse the node
745 // completely.
Chris Lattner3c87b292002-11-07 01:54:56 +0000746 DEBUG(std::cerr << "Attempting to merge two chunks of"
747 << " the same node together!\n");
Chris Lattner08db7192002-11-06 06:20:27 +0000748 foldNodeCompletely();
Chris Lattner02606632002-11-04 06:48:26 +0000749 return;
750 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000751
Chris Lattner5190ce82002-11-12 07:20:45 +0000752 // If both nodes are not at offset 0, make sure that we are merging the node
753 // at an later offset into the node with the zero offset.
754 //
755 if (Offset < NH.getOffset()) {
756 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
757 return;
758 } else if (Offset == NH.getOffset() && getSize() < N->getSize()) {
759 // If the offsets are the same, merge the smaller node into the bigger node
760 N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset());
761 return;
762 }
763
Vikram S. Adve2b7a92c2002-12-06 21:15:21 +0000764 // Ok, now we can merge the two nodes. Use a static helper that works with
765 // two node handles, since "this" may get merged away at intermediate steps.
766 DSNodeHandle CurNodeH(this, Offset);
767 DSNodeHandle NHCopy(NH);
768 DSNode::MergeNodes(CurNodeH, NHCopy);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000769}
770
Chris Lattner0b144872004-01-27 22:03:40 +0000771
772//===----------------------------------------------------------------------===//
773// ReachabilityCloner Implementation
774//===----------------------------------------------------------------------===//
775
776DSNodeHandle ReachabilityCloner::getClonedNH(const DSNodeHandle &SrcNH) {
777 if (SrcNH.isNull()) return DSNodeHandle();
778 const DSNode *SN = SrcNH.getNode();
779
780 DSNodeHandle &NH = NodeMap[SN];
781 if (!NH.isNull()) // Node already mapped?
782 return DSNodeHandle(NH.getNode(), NH.getOffset()+SrcNH.getOffset());
783
784 DSNode *DN = new DSNode(*SN, &Dest, true /* Null out all links */);
785 DN->maskNodeTypes(BitsToKeep);
Chris Lattner00948c02004-01-28 02:05:05 +0000786 NH = DN;
Chris Lattner0b144872004-01-27 22:03:40 +0000787
788 // Next, recursively clone all outgoing links as necessary. Note that
789 // adding these links can cause the node to collapse itself at any time, and
790 // the current node may be merged with arbitrary other nodes. For this
791 // reason, we must always go through NH.
792 DN = 0;
793 for (unsigned i = 0, e = SN->getNumLinks(); i != e; ++i) {
794 const DSNodeHandle &SrcEdge = SN->getLink(i << DS::PointerShift);
795 if (!SrcEdge.isNull()) {
796 const DSNodeHandle &DestEdge = getClonedNH(SrcEdge);
797 // Compute the offset into the current node at which to
798 // merge this link. In the common case, this is a linear
799 // relation to the offset in the original node (with
800 // wrapping), but if the current node gets collapsed due to
801 // recursive merging, we must make sure to merge in all remaining
802 // links at offset zero.
803 unsigned MergeOffset = 0;
804 DSNode *CN = NH.getNode();
805 if (CN->getSize() != 1)
806 MergeOffset = ((i << DS::PointerShift)+NH.getOffset()
807 - SrcNH.getOffset()) %CN->getSize();
808 CN->addEdgeTo(MergeOffset, DestEdge);
809 }
810 }
811
812 // If this node contains any globals, make sure they end up in the scalar
813 // map with the correct offset.
814 for (DSNode::global_iterator I = SN->global_begin(), E = SN->global_end();
815 I != E; ++I) {
816 GlobalValue *GV = *I;
817 const DSNodeHandle &SrcGNH = Src.getNodeForValue(GV);
818 DSNodeHandle &DestGNH = NodeMap[SrcGNH.getNode()];
819 assert(DestGNH.getNode() == NH.getNode() &&"Global mapping inconsistent");
820 Dest.getNodeForValue(GV).mergeWith(DSNodeHandle(DestGNH.getNode(),
Chris Lattner00948c02004-01-28 02:05:05 +0000821 DestGNH.getOffset()+SrcGNH.getOffset()));
Chris Lattner0b144872004-01-27 22:03:40 +0000822
823 if (CloneFlags & DSGraph::UpdateInlinedGlobals)
824 Dest.getInlinedGlobals().insert(GV);
825 }
826
827 return DSNodeHandle(NH.getNode(), NH.getOffset()+SrcNH.getOffset());
828}
829
830void ReachabilityCloner::merge(const DSNodeHandle &NH,
831 const DSNodeHandle &SrcNH) {
832 if (SrcNH.isNull()) return; // Noop
833 if (NH.isNull()) {
834 // If there is no destination node, just clone the source and assign the
835 // destination node to be it.
836 NH.mergeWith(getClonedNH(SrcNH));
837 return;
838 }
839
840 // Okay, at this point, we know that we have both a destination and a source
841 // node that need to be merged. Check to see if the source node has already
842 // been cloned.
843 const DSNode *SN = SrcNH.getNode();
844 DSNodeHandle &SCNH = NodeMap[SN]; // SourceClonedNodeHandle
Chris Lattner0ad91702004-02-22 00:53:54 +0000845 if (!SCNH.isNull()) { // Node already cloned?
Chris Lattner0b144872004-01-27 22:03:40 +0000846 NH.mergeWith(DSNodeHandle(SCNH.getNode(),
847 SCNH.getOffset()+SrcNH.getOffset()));
848
849 return; // Nothing to do!
850 }
851
852 // Okay, so the source node has not already been cloned. Instead of creating
853 // a new DSNode, only to merge it into the one we already have, try to perform
854 // the merge in-place. The only case we cannot handle here is when the offset
855 // into the existing node is less than the offset into the virtual node we are
856 // merging in. In this case, we have to extend the existing node, which
857 // requires an allocation anyway.
858 DSNode *DN = NH.getNode(); // Make sure the Offset is up-to-date
859 if (NH.getOffset() >= SrcNH.getOffset()) {
Chris Lattner0b144872004-01-27 22:03:40 +0000860 if (!DN->isNodeCompletelyFolded()) {
861 // Make sure the destination node is folded if the source node is folded.
862 if (SN->isNodeCompletelyFolded()) {
863 DN->foldNodeCompletely();
864 DN = NH.getNode();
865 } else if (SN->getSize() != DN->getSize()) {
866 // If the two nodes are of different size, and the smaller node has the
867 // array bit set, collapse!
868 if (SN->getSize() < DN->getSize()) {
869 if (SN->isArray()) {
870 DN->foldNodeCompletely();
871 DN = NH.getNode();
872 }
873 } else if (DN->isArray()) {
874 DN->foldNodeCompletely();
875 DN = NH.getNode();
876 }
877 }
878
879 // Merge the type entries of the two nodes together...
880 if (SN->getType() != Type::VoidTy && !DN->isNodeCompletelyFolded()) {
881 DN->mergeTypeInfo(SN->getType(), NH.getOffset()-SrcNH.getOffset());
882 DN = NH.getNode();
883 }
884 }
885
886 assert(!DN->isDeadNode());
887
888 // Merge the NodeType information.
889 DN->mergeNodeFlags(SN->getNodeFlags() & BitsToKeep);
890
891 // Before we start merging outgoing links and updating the scalar map, make
892 // sure it is known that this is the representative node for the src node.
893 SCNH = DSNodeHandle(DN, NH.getOffset()-SrcNH.getOffset());
894
895 // If the source node contains any globals, make sure they end up in the
896 // scalar map with the correct offset.
897 if (SN->global_begin() != SN->global_end()) {
898 // Update the globals in the destination node itself.
899 DN->mergeGlobals(SN->getGlobals());
900
901 // Update the scalar map for the graph we are merging the source node
902 // into.
903 for (DSNode::global_iterator I = SN->global_begin(), E = SN->global_end();
904 I != E; ++I) {
905 GlobalValue *GV = *I;
906 const DSNodeHandle &SrcGNH = Src.getNodeForValue(GV);
907 DSNodeHandle &DestGNH = NodeMap[SrcGNH.getNode()];
908 assert(DestGNH.getNode()==NH.getNode() &&"Global mapping inconsistent");
909 Dest.getNodeForValue(GV).mergeWith(DSNodeHandle(DestGNH.getNode(),
Chris Lattneread9eb72004-01-29 08:36:22 +0000910 DestGNH.getOffset()+SrcGNH.getOffset()));
Chris Lattner0b144872004-01-27 22:03:40 +0000911
912 if (CloneFlags & DSGraph::UpdateInlinedGlobals)
913 Dest.getInlinedGlobals().insert(GV);
914 }
915 }
916 } else {
917 // We cannot handle this case without allocating a temporary node. Fall
918 // back on being simple.
Chris Lattner0b144872004-01-27 22:03:40 +0000919 DSNode *NewDN = new DSNode(*SN, &Dest, true /* Null out all links */);
920 NewDN->maskNodeTypes(BitsToKeep);
921
922 unsigned NHOffset = NH.getOffset();
923 NH.mergeWith(DSNodeHandle(NewDN, SrcNH.getOffset()));
Chris Lattneread9eb72004-01-29 08:36:22 +0000924
Chris Lattner0b144872004-01-27 22:03:40 +0000925 assert(NH.getNode() &&
926 (NH.getOffset() > NHOffset ||
927 (NH.getOffset() == 0 && NH.getNode()->isNodeCompletelyFolded())) &&
928 "Merging did not adjust the offset!");
929
930 // Before we start merging outgoing links and updating the scalar map, make
931 // sure it is known that this is the representative node for the src node.
932 SCNH = DSNodeHandle(NH.getNode(), NH.getOffset()-SrcNH.getOffset());
Chris Lattneread9eb72004-01-29 08:36:22 +0000933
934 // If the source node contained any globals, make sure to create entries
935 // in the scalar map for them!
936 for (DSNode::global_iterator I = SN->global_begin(), E = SN->global_end();
937 I != E; ++I) {
938 GlobalValue *GV = *I;
939 const DSNodeHandle &SrcGNH = Src.getNodeForValue(GV);
940 DSNodeHandle &DestGNH = NodeMap[SrcGNH.getNode()];
941 assert(DestGNH.getNode()==NH.getNode() &&"Global mapping inconsistent");
942 assert(SrcGNH.getNode() == SN && "Global mapping inconsistent");
943 Dest.getNodeForValue(GV).mergeWith(DSNodeHandle(DestGNH.getNode(),
944 DestGNH.getOffset()+SrcGNH.getOffset()));
945
946 if (CloneFlags & DSGraph::UpdateInlinedGlobals)
947 Dest.getInlinedGlobals().insert(GV);
948 }
Chris Lattner0b144872004-01-27 22:03:40 +0000949 }
950
951
952 // Next, recursively merge all outgoing links as necessary. Note that
953 // adding these links can cause the destination node to collapse itself at
954 // any time, and the current node may be merged with arbitrary other nodes.
955 // For this reason, we must always go through NH.
956 DN = 0;
957 for (unsigned i = 0, e = SN->getNumLinks(); i != e; ++i) {
958 const DSNodeHandle &SrcEdge = SN->getLink(i << DS::PointerShift);
959 if (!SrcEdge.isNull()) {
960 // Compute the offset into the current node at which to
961 // merge this link. In the common case, this is a linear
962 // relation to the offset in the original node (with
963 // wrapping), but if the current node gets collapsed due to
964 // recursive merging, we must make sure to merge in all remaining
965 // links at offset zero.
966 unsigned MergeOffset = 0;
967 DSNode *CN = SCNH.getNode();
968 if (CN->getSize() != 1)
969 MergeOffset = ((i << DS::PointerShift)+SCNH.getOffset()) %CN->getSize();
970
Chris Lattner0ad91702004-02-22 00:53:54 +0000971 DSNodeHandle &Link = CN->getLink(MergeOffset);
972 if (!Link.isNull()) {
973 // Perform the recursive merging. Make sure to create a temporary NH,
974 // because the Link can disappear in the process of recursive merging.
975 DSNodeHandle Tmp = Link;
976 merge(Tmp, SrcEdge);
977 } else {
978 merge(Link, SrcEdge);
979 }
Chris Lattner0b144872004-01-27 22:03:40 +0000980 }
981 }
982}
983
984/// mergeCallSite - Merge the nodes reachable from the specified src call
985/// site into the nodes reachable from DestCS.
986void ReachabilityCloner::mergeCallSite(const DSCallSite &DestCS,
987 const DSCallSite &SrcCS) {
988 merge(DestCS.getRetVal(), SrcCS.getRetVal());
989 unsigned MinArgs = DestCS.getNumPtrArgs();
990 if (SrcCS.getNumPtrArgs() < MinArgs) MinArgs = SrcCS.getNumPtrArgs();
991
992 for (unsigned a = 0; a != MinArgs; ++a)
993 merge(DestCS.getPtrArg(a), SrcCS.getPtrArg(a));
994}
995
996
Chris Lattner9de906c2002-10-20 22:11:44 +0000997//===----------------------------------------------------------------------===//
998// DSCallSite Implementation
999//===----------------------------------------------------------------------===//
1000
Vikram S. Adve26b98262002-10-20 21:41:02 +00001001// Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h
Chris Lattner9de906c2002-10-20 22:11:44 +00001002Function &DSCallSite::getCaller() const {
Chris Lattner808a7ae2003-09-20 16:34:13 +00001003 return *Site.getInstruction()->getParent()->getParent();
Vikram S. Adve26b98262002-10-20 21:41:02 +00001004}
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001005
Chris Lattner0b144872004-01-27 22:03:40 +00001006void DSCallSite::InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
1007 ReachabilityCloner &RC) {
1008 NH = RC.getClonedNH(Src);
1009}
Vikram S. Adve42fd1692002-10-20 18:07:37 +00001010
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001011//===----------------------------------------------------------------------===//
1012// DSGraph Implementation
1013//===----------------------------------------------------------------------===//
1014
Chris Lattnera9d65662003-06-30 05:57:30 +00001015/// getFunctionNames - Return a space separated list of the name of the
1016/// functions in this graph (if any)
1017std::string DSGraph::getFunctionNames() const {
1018 switch (getReturnNodes().size()) {
1019 case 0: return "Globals graph";
1020 case 1: return getReturnNodes().begin()->first->getName();
1021 default:
1022 std::string Return;
1023 for (DSGraph::ReturnNodesTy::const_iterator I = getReturnNodes().begin();
1024 I != getReturnNodes().end(); ++I)
1025 Return += I->first->getName() + " ";
1026 Return.erase(Return.end()-1, Return.end()); // Remove last space character
1027 return Return;
1028 }
1029}
1030
1031
Chris Lattner15869aa2003-11-02 22:27:28 +00001032DSGraph::DSGraph(const DSGraph &G) : GlobalsGraph(0), TD(G.TD) {
Chris Lattneraa8146f2002-11-10 06:59:55 +00001033 PrintAuxCalls = false;
Chris Lattner5a540632003-06-30 03:15:25 +00001034 NodeMapTy NodeMap;
1035 cloneInto(G, ScalarMap, ReturnNodes, NodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +00001036}
1037
Chris Lattner5a540632003-06-30 03:15:25 +00001038DSGraph::DSGraph(const DSGraph &G, NodeMapTy &NodeMap)
Chris Lattner15869aa2003-11-02 22:27:28 +00001039 : GlobalsGraph(0), TD(G.TD) {
Chris Lattneraa8146f2002-11-10 06:59:55 +00001040 PrintAuxCalls = false;
Chris Lattner5a540632003-06-30 03:15:25 +00001041 cloneInto(G, ScalarMap, ReturnNodes, NodeMap);
Chris Lattnereff0da92002-10-21 15:32:34 +00001042}
1043
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001044DSGraph::~DSGraph() {
1045 FunctionCalls.clear();
Chris Lattner679e8e12002-11-08 21:27:12 +00001046 AuxFunctionCalls.clear();
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001047 InlinedGlobals.clear();
Chris Lattnerc875f022002-11-03 21:27:48 +00001048 ScalarMap.clear();
Chris Lattner5a540632003-06-30 03:15:25 +00001049 ReturnNodes.clear();
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001050
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001051 // Drop all intra-node references, so that assertions don't fail...
Chris Lattner28897e12004-02-08 00:53:26 +00001052 for (node_iterator NI = node_begin(), E = node_end(); NI != E; ++NI)
1053 (*NI)->dropAllReferences();
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001054
Chris Lattner28897e12004-02-08 00:53:26 +00001055 // Free all of the nodes.
1056 Nodes.clear();
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001057}
1058
Chris Lattner0d9bab82002-07-18 00:12:30 +00001059// dump - Allow inspection of graph in a debugger.
1060void DSGraph::dump() const { print(std::cerr); }
1061
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001062
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001063/// remapLinks - Change all of the Links in the current node according to the
1064/// specified mapping.
Chris Lattner8f0a16e2002-10-31 05:45:02 +00001065///
Chris Lattner8d327672003-06-30 03:36:09 +00001066void DSNode::remapLinks(DSGraph::NodeMapTy &OldNodeMap) {
Chris Lattner2f561382004-01-22 16:56:13 +00001067 for (unsigned i = 0, e = Links.size(); i != e; ++i)
1068 if (DSNode *N = Links[i].getNode()) {
Chris Lattner091f7762004-01-23 01:44:53 +00001069 DSGraph::NodeMapTy::const_iterator ONMI = OldNodeMap.find(N);
1070 if (ONMI != OldNodeMap.end()) {
1071 Links[i].setNode(ONMI->second.getNode());
1072 Links[i].setOffset(Links[i].getOffset()+ONMI->second.getOffset());
1073 }
Chris Lattner2f561382004-01-22 16:56:13 +00001074 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001075}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001076
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001077/// updateFromGlobalGraph - This function rematerializes global nodes and
1078/// nodes reachable from them from the globals graph into the current graph.
Chris Lattner0b144872004-01-27 22:03:40 +00001079/// It uses the vector InlinedGlobals to avoid cloning and merging globals that
1080/// are already up-to-date in the current graph. In practice, in the TD pass,
1081/// this is likely to be a large fraction of the live global nodes in each
1082/// function (since most live nodes are likely to have been brought up-to-date
1083/// in at _some_ caller or callee).
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001084///
1085void DSGraph::updateFromGlobalGraph() {
Chris Lattner0b144872004-01-27 22:03:40 +00001086 TIME_REGION(X, "updateFromGlobalGraph");
Chris Lattner0b144872004-01-27 22:03:40 +00001087 ReachabilityCloner RC(*this, *GlobalsGraph, 0);
1088
1089 // Clone the non-up-to-date global nodes into this graph.
Chris Lattnerbdce7b72004-01-28 03:03:06 +00001090 for (DSScalarMap::global_iterator I = getScalarMap().global_begin(),
1091 E = getScalarMap().global_end(); I != E; ++I)
1092 if (InlinedGlobals.count(*I) == 0) { // GNode is not up-to-date
Chris Lattner62482e52004-01-28 09:15:42 +00001093 DSScalarMap::iterator It = GlobalsGraph->ScalarMap.find(*I);
Chris Lattnerbdce7b72004-01-28 03:03:06 +00001094 if (It != GlobalsGraph->ScalarMap.end())
1095 RC.merge(getNodeForValue(*I), It->second);
1096 }
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001097}
1098
Chris Lattner5a540632003-06-30 03:15:25 +00001099/// cloneInto - Clone the specified DSGraph into the current graph. The
1100/// translated ScalarMap for the old function is filled into the OldValMap
1101/// member, and the translated ReturnNodes map is returned into ReturnNodes.
1102///
1103/// The CloneFlags member controls various aspects of the cloning process.
1104///
Chris Lattner62482e52004-01-28 09:15:42 +00001105void DSGraph::cloneInto(const DSGraph &G, DSScalarMap &OldValMap,
Chris Lattner5a540632003-06-30 03:15:25 +00001106 ReturnNodesTy &OldReturnNodes, NodeMapTy &OldNodeMap,
1107 unsigned CloneFlags) {
Chris Lattner93ddd7e2004-01-22 16:36:28 +00001108 TIME_REGION(X, "cloneInto");
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001109 assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!");
Chris Lattner33312f72002-11-08 01:21:07 +00001110 assert(&G != this && "Cannot clone graph into itself!");
Chris Lattner0d9bab82002-07-18 00:12:30 +00001111
Chris Lattner1e883692003-02-03 20:08:51 +00001112 // Remove alloca or mod/ref bits as specified...
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001113 unsigned BitsToClear = ((CloneFlags & StripAllocaBit)? DSNode::AllocaNode : 0)
1114 | ((CloneFlags & StripModRefBits)? (DSNode::Modified | DSNode::Read) : 0)
1115 | ((CloneFlags & StripIncompleteBit)? DSNode::Incomplete : 0);
Chris Lattnerbd92b732003-06-19 21:15:11 +00001116 BitsToClear |= DSNode::DEAD; // Clear dead flag...
Chris Lattner9fd37ba2004-02-08 00:23:16 +00001117
Chris Lattnerd85645f2004-02-21 22:28:26 +00001118 for (node_iterator I = G.node_begin(), E = G.node_end(); I != E; ++I) {
1119 assert(!(*I)->isForwarding() &&
1120 "Forward nodes shouldn't be in node list!");
1121 DSNode *New = new DSNode(**I, this);
1122 New->maskNodeTypes(~BitsToClear);
1123 OldNodeMap[*I] = New;
1124 }
1125
Chris Lattner18552922002-11-18 21:44:46 +00001126#ifndef NDEBUG
1127 Timer::addPeakMemoryMeasurement();
1128#endif
Chris Lattnerd85645f2004-02-21 22:28:26 +00001129
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001130 // Rewrite the links in the new nodes to point into the current graph now.
Chris Lattnerd85645f2004-02-21 22:28:26 +00001131 // Note that we don't loop over the node's list to do this. The problem is
1132 // that remaping links can cause recursive merging to happen, which means
1133 // that node_iterator's can get easily invalidated! Because of this, we
1134 // loop over the OldNodeMap, which contains all of the new nodes as the
1135 // .second element of the map elements. Also note that if we remap a node
1136 // more than once, we won't break anything.
1137 for (NodeMapTy::iterator I = OldNodeMap.begin(), E = OldNodeMap.end();
1138 I != E; ++I)
1139 I->second.getNode()->remapLinks(OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +00001140
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001141 // Copy the scalar map... merging all of the global nodes...
Chris Lattner62482e52004-01-28 09:15:42 +00001142 for (DSScalarMap::const_iterator I = G.ScalarMap.begin(),
Chris Lattnerc875f022002-11-03 21:27:48 +00001143 E = G.ScalarMap.end(); I != E; ++I) {
Chris Lattnerf8c6aab2002-11-08 05:01:14 +00001144 DSNodeHandle &MappedNode = OldNodeMap[I->second.getNode()];
Chris Lattner2cb9acd2003-06-30 05:09:29 +00001145 DSNodeHandle &H = OldValMap[I->first];
1146 H.mergeWith(DSNodeHandle(MappedNode.getNode(),
1147 I->second.getOffset()+MappedNode.getOffset()));
Chris Lattnercf15db32002-10-17 20:09:52 +00001148
Chris Lattner2cb9acd2003-06-30 05:09:29 +00001149 // If this is a global, add the global to this fn or merge if already exists
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001150 if (GlobalValue* GV = dyn_cast<GlobalValue>(I->first)) {
1151 ScalarMap[GV].mergeWith(H);
Chris Lattner091f7762004-01-23 01:44:53 +00001152 if (CloneFlags & DSGraph::UpdateInlinedGlobals)
1153 InlinedGlobals.insert(GV);
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001154 }
Chris Lattnercf15db32002-10-17 20:09:52 +00001155 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001156
Chris Lattner679e8e12002-11-08 21:27:12 +00001157 if (!(CloneFlags & DontCloneCallNodes)) {
1158 // Copy the function calls list...
1159 unsigned FC = FunctionCalls.size(); // FirstCall
1160 FunctionCalls.reserve(FC+G.FunctionCalls.size());
1161 for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i)
1162 FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap));
Chris Lattneracf491f2002-11-08 22:27:09 +00001163 }
Chris Lattner679e8e12002-11-08 21:27:12 +00001164
Chris Lattneracf491f2002-11-08 22:27:09 +00001165 if (!(CloneFlags & DontCloneAuxCallNodes)) {
Misha Brukman2f2d0652003-09-11 18:14:24 +00001166 // Copy the auxiliary function calls list...
Chris Lattneracf491f2002-11-08 22:27:09 +00001167 unsigned FC = AuxFunctionCalls.size(); // FirstCall
Chris Lattner679e8e12002-11-08 21:27:12 +00001168 AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size());
1169 for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i)
1170 AuxFunctionCalls.push_back(DSCallSite(G.AuxFunctionCalls[i], OldNodeMap));
1171 }
Chris Lattnercf15db32002-10-17 20:09:52 +00001172
Chris Lattner5a540632003-06-30 03:15:25 +00001173 // Map the return node pointers over...
1174 for (ReturnNodesTy::const_iterator I = G.getReturnNodes().begin(),
1175 E = G.getReturnNodes().end(); I != E; ++I) {
1176 const DSNodeHandle &Ret = I->second;
1177 DSNodeHandle &MappedRet = OldNodeMap[Ret.getNode()];
1178 OldReturnNodes.insert(std::make_pair(I->first,
1179 DSNodeHandle(MappedRet.getNode(),
1180 MappedRet.getOffset()+Ret.getOffset())));
1181 }
Chris Lattner0d9bab82002-07-18 00:12:30 +00001182}
1183
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001184
Chris Lattner076c1f92002-11-07 06:31:54 +00001185/// mergeInGraph - The method is used for merging graphs together. If the
1186/// argument graph is not *this, it makes a clone of the specified graph, then
1187/// merges the nodes specified in the call site with the formal arguments in the
1188/// graph.
1189///
Chris Lattner9f930552003-06-30 05:27:18 +00001190void DSGraph::mergeInGraph(const DSCallSite &CS, Function &F,
1191 const DSGraph &Graph, unsigned CloneFlags) {
Chris Lattner0b144872004-01-27 22:03:40 +00001192 TIME_REGION(X, "mergeInGraph");
1193
Chris Lattner2c7725a2004-03-03 20:55:27 +00001194 // Fastpath for a noop inline.
1195 if (CS.getNumPtrArgs() == 0 && CS.getRetVal().isNull())
1196 return;
1197
Chris Lattner076c1f92002-11-07 06:31:54 +00001198 // If this is not a recursive call, clone the graph into this graph...
1199 if (&Graph != this) {
Chris Lattner0b144872004-01-27 22:03:40 +00001200 // Clone the callee's graph into the current graph, keeping track of where
1201 // scalars in the old graph _used_ to point, and of the new nodes matching
1202 // nodes of the old graph.
1203 ReachabilityCloner RC(*this, Graph, CloneFlags);
1204
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001205 // Set up argument bindings
1206 Function::aiterator AI = F.abegin();
1207 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) {
1208 // Advance the argument iterator to the first pointer argument...
1209 while (AI != F.aend() && !isPointerType(AI->getType())) {
1210 ++AI;
Chris Lattner17a93e22004-01-29 03:32:15 +00001211#ifndef NDEBUG // FIXME: We should merge vararg arguments!
1212 if (AI == F.aend() && !F.getFunctionType()->isVarArg())
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001213 std::cerr << "Bad call to Function: " << F.getName() << "\n";
Chris Lattner076c1f92002-11-07 06:31:54 +00001214#endif
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001215 }
1216 if (AI == F.aend()) break;
1217
1218 // Add the link from the argument scalar to the provided value.
Chris Lattner0b144872004-01-27 22:03:40 +00001219 RC.merge(CS.getPtrArg(i), Graph.getNodeForValue(AI));
Chris Lattner076c1f92002-11-07 06:31:54 +00001220 }
1221
Chris Lattner0b144872004-01-27 22:03:40 +00001222 // Map the return node pointer over.
Chris Lattner0ad91702004-02-22 00:53:54 +00001223 if (!CS.getRetVal().isNull())
Chris Lattner0b144872004-01-27 22:03:40 +00001224 RC.merge(CS.getRetVal(), Graph.getReturnNodeFor(F));
1225
1226 // If requested, copy the calls or aux-calls lists.
1227 if (!(CloneFlags & DontCloneCallNodes)) {
1228 // Copy the function calls list...
1229 FunctionCalls.reserve(FunctionCalls.size()+Graph.FunctionCalls.size());
1230 for (unsigned i = 0, ei = Graph.FunctionCalls.size(); i != ei; ++i)
1231 FunctionCalls.push_back(DSCallSite(Graph.FunctionCalls[i], RC));
1232 }
1233
1234 if (!(CloneFlags & DontCloneAuxCallNodes)) {
1235 // Copy the auxiliary function calls list...
1236 AuxFunctionCalls.reserve(AuxFunctionCalls.size()+
1237 Graph.AuxFunctionCalls.size());
1238 for (unsigned i = 0, ei = Graph.AuxFunctionCalls.size(); i != ei; ++i)
1239 AuxFunctionCalls.push_back(DSCallSite(Graph.AuxFunctionCalls[i], RC));
1240 }
1241
Chris Lattner0321b682004-02-27 20:05:15 +00001242 // Clone over all globals that appear in the caller and callee graphs.
1243 for (DSScalarMap::global_iterator GI = Graph.getScalarMap().global_begin(),
1244 E = Graph.getScalarMap().global_end(); GI != E; ++GI)
1245 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*GI))
1246 if (ScalarMap.count(GV))
1247 RC.merge(ScalarMap[GV], Graph.getNodeForValue(GV));
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001248 } else {
1249 DSNodeHandle RetVal = getReturnNodeFor(F);
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001250
1251 // Merge the return value with the return value of the context...
1252 RetVal.mergeWith(CS.getRetVal());
1253
1254 // Resolve all of the function arguments...
1255 Function::aiterator AI = F.abegin();
1256
1257 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) {
1258 // Advance the argument iterator to the first pointer argument...
1259 while (AI != F.aend() && !isPointerType(AI->getType())) {
1260 ++AI;
Chris Lattner17a93e22004-01-29 03:32:15 +00001261#ifndef NDEBUG // FIXME: We should merge varargs arguments!!
1262 if (AI == F.aend() && !F.getFunctionType()->isVarArg())
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001263 std::cerr << "Bad call to Function: " << F.getName() << "\n";
1264#endif
1265 }
1266 if (AI == F.aend()) break;
1267
1268 // Add the link from the argument scalar to the provided value
Chris Lattner0b144872004-01-27 22:03:40 +00001269 DSNodeHandle &NH = getNodeForValue(AI);
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001270 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
1271 NH.mergeWith(CS.getPtrArg(i));
1272 }
Chris Lattner076c1f92002-11-07 06:31:54 +00001273 }
1274}
1275
Chris Lattner58f98d02003-07-02 04:38:49 +00001276/// getCallSiteForArguments - Get the arguments and return value bindings for
1277/// the specified function in the current graph.
1278///
1279DSCallSite DSGraph::getCallSiteForArguments(Function &F) const {
1280 std::vector<DSNodeHandle> Args;
1281
1282 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
1283 if (isPointerType(I->getType()))
Chris Lattner0b144872004-01-27 22:03:40 +00001284 Args.push_back(getNodeForValue(I));
Chris Lattner58f98d02003-07-02 04:38:49 +00001285
Chris Lattner808a7ae2003-09-20 16:34:13 +00001286 return DSCallSite(CallSite(), getReturnNodeFor(F), &F, Args);
Chris Lattner58f98d02003-07-02 04:38:49 +00001287}
1288
1289
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001290
Chris Lattner0d9bab82002-07-18 00:12:30 +00001291// markIncompleteNodes - Mark the specified node as having contents that are not
1292// known with the current analysis we have performed. Because a node makes all
Chris Lattnerbd92b732003-06-19 21:15:11 +00001293// of the nodes it can reach incomplete if the node itself is incomplete, we
Chris Lattner0d9bab82002-07-18 00:12:30 +00001294// must recursively traverse the data structure graph, marking all reachable
1295// nodes as incomplete.
1296//
1297static void markIncompleteNode(DSNode *N) {
1298 // Stop recursion if no node, or if node already marked...
Chris Lattner72d50a02003-06-28 21:58:28 +00001299 if (N == 0 || N->isIncomplete()) return;
Chris Lattner0d9bab82002-07-18 00:12:30 +00001300
1301 // Actually mark the node
Chris Lattnerbd92b732003-06-19 21:15:11 +00001302 N->setIncompleteMarker();
Chris Lattner0d9bab82002-07-18 00:12:30 +00001303
Misha Brukman2f2d0652003-09-11 18:14:24 +00001304 // Recursively process children...
Chris Lattner08db7192002-11-06 06:20:27 +00001305 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
1306 if (DSNode *DSN = N->getLink(i).getNode())
1307 markIncompleteNode(DSN);
Chris Lattner0d9bab82002-07-18 00:12:30 +00001308}
1309
Chris Lattnere71ffc22002-11-11 03:36:55 +00001310static void markIncomplete(DSCallSite &Call) {
1311 // Then the return value is certainly incomplete!
1312 markIncompleteNode(Call.getRetVal().getNode());
1313
1314 // All objects pointed to by function arguments are incomplete!
1315 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i)
1316 markIncompleteNode(Call.getPtrArg(i).getNode());
1317}
Chris Lattner0d9bab82002-07-18 00:12:30 +00001318
1319// markIncompleteNodes - Traverse the graph, identifying nodes that may be
1320// modified by other functions that have not been resolved yet. This marks
1321// nodes that are reachable through three sources of "unknownness":
1322//
1323// Global Variables, Function Calls, and Incoming Arguments
1324//
1325// For any node that may have unknown components (because something outside the
1326// scope of current analysis may have modified it), the 'Incomplete' flag is
1327// added to the NodeType.
1328//
Chris Lattner394471f2003-01-23 22:05:33 +00001329void DSGraph::markIncompleteNodes(unsigned Flags) {
Chris Lattner0d9bab82002-07-18 00:12:30 +00001330 // Mark any incoming arguments as incomplete...
Chris Lattner5a540632003-06-30 03:15:25 +00001331 if (Flags & DSGraph::MarkFormalArgs)
1332 for (ReturnNodesTy::iterator FI = ReturnNodes.begin(), E =ReturnNodes.end();
1333 FI != E; ++FI) {
1334 Function &F = *FI->first;
1335 if (F.getName() != "main")
1336 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
Chris Lattner0b144872004-01-27 22:03:40 +00001337 if (isPointerType(I->getType()))
1338 markIncompleteNode(getNodeForValue(I).getNode());
Chris Lattner5a540632003-06-30 03:15:25 +00001339 }
Chris Lattner0d9bab82002-07-18 00:12:30 +00001340
1341 // Mark stuff passed into functions calls as being incomplete...
Chris Lattnere71ffc22002-11-11 03:36:55 +00001342 if (!shouldPrintAuxCalls())
1343 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
1344 markIncomplete(FunctionCalls[i]);
1345 else
1346 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1347 markIncomplete(AuxFunctionCalls[i]);
1348
Chris Lattner0d9bab82002-07-18 00:12:30 +00001349
Chris Lattner93d7a212003-02-09 18:41:49 +00001350 // Mark all global nodes as incomplete...
1351 if ((Flags & DSGraph::IgnoreGlobals) == 0)
Chris Lattner51c06ab2004-02-25 23:08:00 +00001352 for (DSScalarMap::global_iterator I = ScalarMap.global_begin(),
1353 E = ScalarMap.global_end(); I != E; ++I)
Chris Lattnercf14e712004-02-25 23:36:08 +00001354 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
1355 if (!GV->isConstant())
1356 markIncompleteNode(ScalarMap[GV].getNode());
Chris Lattner0d9bab82002-07-18 00:12:30 +00001357}
1358
Chris Lattneraa8146f2002-11-10 06:59:55 +00001359static inline void killIfUselessEdge(DSNodeHandle &Edge) {
1360 if (DSNode *N = Edge.getNode()) // Is there an edge?
Chris Lattner72d29a42003-02-11 23:11:51 +00001361 if (N->getNumReferrers() == 1) // Does it point to a lonely node?
Chris Lattnerbd92b732003-06-19 21:15:11 +00001362 // No interesting info?
1363 if ((N->getNodeFlags() & ~DSNode::Incomplete) == 0 &&
Chris Lattner18552922002-11-18 21:44:46 +00001364 N->getType() == Type::VoidTy && !N->isNodeCompletelyFolded())
Chris Lattneraa8146f2002-11-10 06:59:55 +00001365 Edge.setNode(0); // Kill the edge!
1366}
Chris Lattner0d9bab82002-07-18 00:12:30 +00001367
Chris Lattneraa8146f2002-11-10 06:59:55 +00001368static inline bool nodeContainsExternalFunction(const DSNode *N) {
1369 const std::vector<GlobalValue*> &Globals = N->getGlobals();
1370 for (unsigned i = 0, e = Globals.size(); i != e; ++i)
1371 if (Globals[i]->isExternal())
1372 return true;
Chris Lattner0d9bab82002-07-18 00:12:30 +00001373 return false;
1374}
1375
Chris Lattner5a540632003-06-30 03:15:25 +00001376static void removeIdenticalCalls(std::vector<DSCallSite> &Calls) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001377 // Remove trivially identical function calls
1378 unsigned NumFns = Calls.size();
Chris Lattneraa8146f2002-11-10 06:59:55 +00001379 std::sort(Calls.begin(), Calls.end()); // Sort by callee as primary key!
1380
Chris Lattner0b144872004-01-27 22:03:40 +00001381#if 1
Chris Lattneraa8146f2002-11-10 06:59:55 +00001382 // Scan the call list cleaning it up as necessary...
Chris Lattner923fc052003-02-05 21:59:58 +00001383 DSNode *LastCalleeNode = 0;
1384 Function *LastCalleeFunc = 0;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001385 unsigned NumDuplicateCalls = 0;
1386 bool LastCalleeContainsExternalFunction = false;
Chris Lattnere4258442002-11-11 21:35:38 +00001387 for (unsigned i = 0; i != Calls.size(); ++i) {
Chris Lattneraa8146f2002-11-10 06:59:55 +00001388 DSCallSite &CS = Calls[i];
1389
Chris Lattnere4258442002-11-11 21:35:38 +00001390 // If the Callee is a useless edge, this must be an unreachable call site,
1391 // eliminate it.
Chris Lattner72d29a42003-02-11 23:11:51 +00001392 if (CS.isIndirectCall() && CS.getCalleeNode()->getNumReferrers() == 1 &&
Chris Lattnerabcdf802004-02-26 03:43:43 +00001393 CS.getCalleeNode()->isComplete() &&
Chris Lattneraf6926a2004-02-26 03:45:03 +00001394 CS.getCalleeNode()->getGlobals().empty()) { // No useful info?
Chris Lattner64507e32004-01-28 01:19:52 +00001395#ifndef NDEBUG
Chris Lattnerabcdf802004-02-26 03:43:43 +00001396 std::cerr << "WARNING: Useless call site found.\n";
Chris Lattner64507e32004-01-28 01:19:52 +00001397#endif
Chris Lattnere4258442002-11-11 21:35:38 +00001398 CS.swap(Calls.back());
1399 Calls.pop_back();
1400 --i;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001401 } else {
Chris Lattnere4258442002-11-11 21:35:38 +00001402 // If the return value or any arguments point to a void node with no
1403 // information at all in it, and the call node is the only node to point
1404 // to it, remove the edge to the node (killing the node).
1405 //
1406 killIfUselessEdge(CS.getRetVal());
1407 for (unsigned a = 0, e = CS.getNumPtrArgs(); a != e; ++a)
1408 killIfUselessEdge(CS.getPtrArg(a));
1409
1410 // If this call site calls the same function as the last call site, and if
1411 // the function pointer contains an external function, this node will
1412 // never be resolved. Merge the arguments of the call node because no
1413 // information will be lost.
1414 //
Chris Lattner923fc052003-02-05 21:59:58 +00001415 if ((CS.isDirectCall() && CS.getCalleeFunc() == LastCalleeFunc) ||
1416 (CS.isIndirectCall() && CS.getCalleeNode() == LastCalleeNode)) {
Chris Lattnere4258442002-11-11 21:35:38 +00001417 ++NumDuplicateCalls;
1418 if (NumDuplicateCalls == 1) {
Chris Lattner923fc052003-02-05 21:59:58 +00001419 if (LastCalleeNode)
1420 LastCalleeContainsExternalFunction =
1421 nodeContainsExternalFunction(LastCalleeNode);
1422 else
1423 LastCalleeContainsExternalFunction = LastCalleeFunc->isExternal();
Chris Lattnere4258442002-11-11 21:35:38 +00001424 }
Chris Lattner0b144872004-01-27 22:03:40 +00001425
1426 // It is not clear why, but enabling this code makes DSA really
1427 // sensitive to node forwarding. Basically, with this enabled, DSA
1428 // performs different number of inlinings based on which nodes are
1429 // forwarding or not. This is clearly a problem, so this code is
1430 // disabled until this can be resolved.
Chris Lattner58f98d02003-07-02 04:38:49 +00001431#if 1
Chris Lattner0b144872004-01-27 22:03:40 +00001432 if (LastCalleeContainsExternalFunction
1433#if 0
1434 ||
Chris Lattnere4258442002-11-11 21:35:38 +00001435 // This should be more than enough context sensitivity!
1436 // FIXME: Evaluate how many times this is tripped!
Chris Lattner0b144872004-01-27 22:03:40 +00001437 NumDuplicateCalls > 20
1438#endif
1439 ) {
Chris Lattnere4258442002-11-11 21:35:38 +00001440 DSCallSite &OCS = Calls[i-1];
1441 OCS.mergeWith(CS);
1442
1443 // The node will now be eliminated as a duplicate!
1444 if (CS.getNumPtrArgs() < OCS.getNumPtrArgs())
1445 CS = OCS;
1446 else if (CS.getNumPtrArgs() > OCS.getNumPtrArgs())
1447 OCS = CS;
1448 }
Chris Lattner18f07a12003-07-01 16:28:11 +00001449#endif
Chris Lattnere4258442002-11-11 21:35:38 +00001450 } else {
Chris Lattner923fc052003-02-05 21:59:58 +00001451 if (CS.isDirectCall()) {
1452 LastCalleeFunc = CS.getCalleeFunc();
1453 LastCalleeNode = 0;
1454 } else {
1455 LastCalleeNode = CS.getCalleeNode();
1456 LastCalleeFunc = 0;
1457 }
Chris Lattnere4258442002-11-11 21:35:38 +00001458 NumDuplicateCalls = 0;
1459 }
Chris Lattneraa8146f2002-11-10 06:59:55 +00001460 }
1461 }
Chris Lattner0b144872004-01-27 22:03:40 +00001462#endif
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001463 Calls.erase(std::unique(Calls.begin(), Calls.end()), Calls.end());
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001464
Chris Lattner33312f72002-11-08 01:21:07 +00001465 // Track the number of call nodes merged away...
1466 NumCallNodesMerged += NumFns-Calls.size();
1467
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001468 DEBUG(if (NumFns != Calls.size())
Chris Lattner5a540632003-06-30 03:15:25 +00001469 std::cerr << "Merged " << (NumFns-Calls.size()) << " call nodes.\n";);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001470}
Chris Lattner0d9bab82002-07-18 00:12:30 +00001471
Chris Lattneraa8146f2002-11-10 06:59:55 +00001472
Chris Lattnere2219762002-07-18 18:22:40 +00001473// removeTriviallyDeadNodes - After the graph has been constructed, this method
1474// removes all unreachable nodes that are created because they got merged with
1475// other nodes in the graph. These nodes will all be trivially unreachable, so
1476// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +00001477//
Chris Lattnerf40f0a32002-11-09 22:07:02 +00001478void DSGraph::removeTriviallyDeadNodes() {
Chris Lattner93ddd7e2004-01-22 16:36:28 +00001479 TIME_REGION(X, "removeTriviallyDeadNodes");
Chris Lattneraa8146f2002-11-10 06:59:55 +00001480
Chris Lattnerbab8c282003-09-20 21:34:07 +00001481 // Loop over all of the nodes in the graph, calling getNode on each field.
1482 // This will cause all nodes to update their forwarding edges, causing
1483 // forwarded nodes to be delete-able.
Chris Lattner9fd37ba2004-02-08 00:23:16 +00001484 for (node_iterator NI = node_begin(), E = node_end(); NI != E; ++NI) {
1485 DSNode *N = *NI;
Chris Lattnerbab8c282003-09-20 21:34:07 +00001486 for (unsigned l = 0, e = N->getNumLinks(); l != e; ++l)
1487 N->getLink(l*N->getPointerSize()).getNode();
1488 }
1489
Chris Lattner0b144872004-01-27 22:03:40 +00001490 // NOTE: This code is disabled. Though it should, in theory, allow us to
1491 // remove more nodes down below, the scan of the scalar map is incredibly
1492 // expensive for certain programs (with large SCCs). In the future, if we can
1493 // make the scalar map scan more efficient, then we can reenable this.
1494#if 0
1495 { TIME_REGION(X, "removeTriviallyDeadNodes:scalarmap");
1496
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001497 // Likewise, forward any edges from the scalar nodes. While we are at it,
1498 // clean house a bit.
Chris Lattner62482e52004-01-28 09:15:42 +00001499 for (DSScalarMap::iterator I = ScalarMap.begin(),E = ScalarMap.end();I != E;){
Chris Lattner0b144872004-01-27 22:03:40 +00001500 I->second.getNode();
1501 ++I;
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001502 }
Chris Lattner0b144872004-01-27 22:03:40 +00001503 }
1504#endif
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001505 bool isGlobalsGraph = !GlobalsGraph;
1506
Chris Lattner9fd37ba2004-02-08 00:23:16 +00001507 for (NodeListTy::iterator NI = Nodes.begin(), E = Nodes.end(); NI != E; ) {
Chris Lattner28897e12004-02-08 00:53:26 +00001508 DSNode &Node = *NI;
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001509
1510 // Do not remove *any* global nodes in the globals graph.
1511 // This is a special case because such nodes may not have I, M, R flags set.
Chris Lattner28897e12004-02-08 00:53:26 +00001512 if (Node.isGlobalNode() && isGlobalsGraph) {
Chris Lattner9fd37ba2004-02-08 00:23:16 +00001513 ++NI;
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001514 continue;
Chris Lattner9fd37ba2004-02-08 00:23:16 +00001515 }
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001516
Chris Lattner28897e12004-02-08 00:53:26 +00001517 if (Node.isComplete() && !Node.isModified() && !Node.isRead()) {
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001518 // This is a useless node if it has no mod/ref info (checked above),
1519 // outgoing edges (which it cannot, as it is not modified in this
1520 // context), and it has no incoming edges. If it is a global node it may
1521 // have all of these properties and still have incoming edges, due to the
1522 // scalar map, so we check those now.
1523 //
Chris Lattner28897e12004-02-08 00:53:26 +00001524 if (Node.getNumReferrers() == Node.getGlobals().size()) {
1525 const std::vector<GlobalValue*> &Globals = Node.getGlobals();
Chris Lattner72d29a42003-02-11 23:11:51 +00001526
Chris Lattner17a93e22004-01-29 03:32:15 +00001527 // Loop through and make sure all of the globals are referring directly
1528 // to the node...
1529 for (unsigned j = 0, e = Globals.size(); j != e; ++j) {
1530 DSNode *N = getNodeForValue(Globals[j]).getNode();
Chris Lattner28897e12004-02-08 00:53:26 +00001531 assert(N == &Node && "ScalarMap doesn't match globals list!");
Chris Lattner17a93e22004-01-29 03:32:15 +00001532 }
1533
Chris Lattnerbd92b732003-06-19 21:15:11 +00001534 // Make sure NumReferrers still agrees, if so, the node is truly dead.
Chris Lattner28897e12004-02-08 00:53:26 +00001535 if (Node.getNumReferrers() == Globals.size()) {
Chris Lattner72d29a42003-02-11 23:11:51 +00001536 for (unsigned j = 0, e = Globals.size(); j != e; ++j)
1537 ScalarMap.erase(Globals[j]);
Chris Lattner28897e12004-02-08 00:53:26 +00001538 Node.makeNodeDead();
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001539 ++NumTrivialGlobalDNE;
Chris Lattner72d29a42003-02-11 23:11:51 +00001540 }
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001541 }
1542 }
1543
Chris Lattner28897e12004-02-08 00:53:26 +00001544 if (Node.getNodeFlags() == 0 && Node.hasNoReferrers()) {
Chris Lattner2609c072003-02-10 18:18:18 +00001545 // This node is dead!
Chris Lattner28897e12004-02-08 00:53:26 +00001546 NI = Nodes.erase(NI); // Erase & remove from node list.
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001547 ++NumTrivialDNE;
Chris Lattner9fd37ba2004-02-08 00:23:16 +00001548 } else {
1549 ++NI;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001550 }
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001551 }
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001552
1553 removeIdenticalCalls(FunctionCalls);
1554 removeIdenticalCalls(AuxFunctionCalls);
Chris Lattner0d9bab82002-07-18 00:12:30 +00001555}
1556
1557
Chris Lattner5c7380e2003-01-29 21:10:20 +00001558/// markReachableNodes - This method recursively traverses the specified
1559/// DSNodes, marking any nodes which are reachable. All reachable nodes it adds
1560/// to the set, which allows it to only traverse visited nodes once.
1561///
Chris Lattner41c04f72003-02-01 04:52:08 +00001562void DSNode::markReachableNodes(hash_set<DSNode*> &ReachableNodes) {
Chris Lattner5c7380e2003-01-29 21:10:20 +00001563 if (this == 0) return;
Chris Lattner72d29a42003-02-11 23:11:51 +00001564 assert(getForwardNode() == 0 && "Cannot mark a forwarded node!");
Chris Lattner4c6cb7a2004-01-22 15:30:58 +00001565 if (ReachableNodes.insert(this).second) // Is newly reachable?
1566 for (unsigned i = 0, e = getSize(); i < e; i += DS::PointerSize)
1567 getLink(i).getNode()->markReachableNodes(ReachableNodes);
Chris Lattner5c7380e2003-01-29 21:10:20 +00001568}
1569
Chris Lattner41c04f72003-02-01 04:52:08 +00001570void DSCallSite::markReachableNodes(hash_set<DSNode*> &Nodes) {
Chris Lattner5c7380e2003-01-29 21:10:20 +00001571 getRetVal().getNode()->markReachableNodes(Nodes);
Chris Lattner923fc052003-02-05 21:59:58 +00001572 if (isIndirectCall()) getCalleeNode()->markReachableNodes(Nodes);
Chris Lattner5c7380e2003-01-29 21:10:20 +00001573
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001574 for (unsigned i = 0, e = getNumPtrArgs(); i != e; ++i)
1575 getPtrArg(i).getNode()->markReachableNodes(Nodes);
Chris Lattnere2219762002-07-18 18:22:40 +00001576}
1577
Chris Lattnera1220af2003-02-01 06:17:02 +00001578// CanReachAliveNodes - Simple graph walker that recursively traverses the graph
1579// looking for a node that is marked alive. If an alive node is found, return
1580// true, otherwise return false. If an alive node is reachable, this node is
1581// marked as alive...
Chris Lattneraa8146f2002-11-10 06:59:55 +00001582//
Chris Lattnera1220af2003-02-01 06:17:02 +00001583static bool CanReachAliveNodes(DSNode *N, hash_set<DSNode*> &Alive,
Chris Lattner85cfe012003-07-03 02:03:53 +00001584 hash_set<DSNode*> &Visited,
1585 bool IgnoreGlobals) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001586 if (N == 0) return false;
Chris Lattner72d29a42003-02-11 23:11:51 +00001587 assert(N->getForwardNode() == 0 && "Cannot mark a forwarded node!");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001588
Chris Lattner85cfe012003-07-03 02:03:53 +00001589 // If this is a global node, it will end up in the globals graph anyway, so we
1590 // don't need to worry about it.
1591 if (IgnoreGlobals && N->isGlobalNode()) return false;
1592
Chris Lattneraa8146f2002-11-10 06:59:55 +00001593 // If we know that this node is alive, return so!
1594 if (Alive.count(N)) return true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001595
Chris Lattneraa8146f2002-11-10 06:59:55 +00001596 // Otherwise, we don't think the node is alive yet, check for infinite
1597 // recursion.
Chris Lattner41c04f72003-02-01 04:52:08 +00001598 if (Visited.count(N)) return false; // Found a cycle
Chris Lattnera1220af2003-02-01 06:17:02 +00001599 Visited.insert(N); // No recursion, insert into Visited...
Chris Lattneraa8146f2002-11-10 06:59:55 +00001600
Chris Lattner08db7192002-11-06 06:20:27 +00001601 for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize)
Chris Lattner85cfe012003-07-03 02:03:53 +00001602 if (CanReachAliveNodes(N->getLink(i).getNode(), Alive, Visited,
1603 IgnoreGlobals)) {
Chris Lattnera1220af2003-02-01 06:17:02 +00001604 N->markReachableNodes(Alive);
1605 return true;
1606 }
1607 return false;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001608}
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001609
Chris Lattnera1220af2003-02-01 06:17:02 +00001610// CallSiteUsesAliveArgs - Return true if the specified call site can reach any
1611// alive nodes.
1612//
Chris Lattner41c04f72003-02-01 04:52:08 +00001613static bool CallSiteUsesAliveArgs(DSCallSite &CS, hash_set<DSNode*> &Alive,
Chris Lattner85cfe012003-07-03 02:03:53 +00001614 hash_set<DSNode*> &Visited,
1615 bool IgnoreGlobals) {
1616 if (CanReachAliveNodes(CS.getRetVal().getNode(), Alive, Visited,
1617 IgnoreGlobals))
Chris Lattner923fc052003-02-05 21:59:58 +00001618 return true;
1619 if (CS.isIndirectCall() &&
Chris Lattner85cfe012003-07-03 02:03:53 +00001620 CanReachAliveNodes(CS.getCalleeNode(), Alive, Visited, IgnoreGlobals))
Chris Lattneraa8146f2002-11-10 06:59:55 +00001621 return true;
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001622 for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i)
Chris Lattner85cfe012003-07-03 02:03:53 +00001623 if (CanReachAliveNodes(CS.getPtrArg(i).getNode(), Alive, Visited,
1624 IgnoreGlobals))
Chris Lattneraa8146f2002-11-10 06:59:55 +00001625 return true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001626 return false;
1627}
1628
Chris Lattnere2219762002-07-18 18:22:40 +00001629// removeDeadNodes - Use a more powerful reachability analysis to eliminate
1630// subgraphs that are unreachable. This often occurs because the data
1631// structure doesn't "escape" into it's caller, and thus should be eliminated
1632// from the caller's graph entirely. This is only appropriate to use when
1633// inlining graphs.
1634//
Chris Lattner394471f2003-01-23 22:05:33 +00001635void DSGraph::removeDeadNodes(unsigned Flags) {
Chris Lattner9dc41852003-11-12 04:57:58 +00001636 DEBUG(AssertGraphOK(); if (GlobalsGraph) GlobalsGraph->AssertGraphOK());
Chris Lattner85cfe012003-07-03 02:03:53 +00001637
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001638 // Reduce the amount of work we have to do... remove dummy nodes left over by
1639 // merging...
Chris Lattnera3fd88d2004-01-28 03:24:41 +00001640 removeTriviallyDeadNodes();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001641
Chris Lattner93ddd7e2004-01-22 16:36:28 +00001642 TIME_REGION(X, "removeDeadNodes");
1643
Misha Brukman2f2d0652003-09-11 18:14:24 +00001644 // FIXME: Merge non-trivially identical call nodes...
Chris Lattnere2219762002-07-18 18:22:40 +00001645
1646 // Alive - a set that holds all nodes found to be reachable/alive.
Chris Lattner41c04f72003-02-01 04:52:08 +00001647 hash_set<DSNode*> Alive;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001648 std::vector<std::pair<Value*, DSNode*> > GlobalNodes;
Chris Lattnere2219762002-07-18 18:22:40 +00001649
Chris Lattner0b144872004-01-27 22:03:40 +00001650 // Copy and merge all information about globals to the GlobalsGraph if this is
1651 // not a final pass (where unreachable globals are removed).
1652 //
1653 // Strip all alloca bits since the current function is only for the BU pass.
1654 // Strip all incomplete bits since they are short-lived properties and they
1655 // will be correctly computed when rematerializing nodes into the functions.
1656 //
1657 ReachabilityCloner GGCloner(*GlobalsGraph, *this, DSGraph::StripAllocaBit |
1658 DSGraph::StripIncompleteBit);
1659
Chris Lattneraa8146f2002-11-10 06:59:55 +00001660 // Mark all nodes reachable by (non-global) scalar nodes as alive...
Chris Lattner00948c02004-01-28 02:05:05 +00001661 { TIME_REGION(Y, "removeDeadNodes:scalarscan");
Chris Lattner62482e52004-01-28 09:15:42 +00001662 for (DSScalarMap::iterator I = ScalarMap.begin(), E = ScalarMap.end(); I !=E;)
Chris Lattner5f07a8b2003-02-14 06:28:00 +00001663 if (isa<GlobalValue>(I->first)) { // Keep track of global nodes
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001664 assert(I->second.getNode() && "Null global node?");
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001665 assert(I->second.getNode()->isGlobalNode() && "Should be a global node!");
Chris Lattner5f07a8b2003-02-14 06:28:00 +00001666 GlobalNodes.push_back(std::make_pair(I->first, I->second.getNode()));
Chris Lattner0b144872004-01-27 22:03:40 +00001667
1668 // Make sure that all globals are cloned over as roots.
Chris Lattner00948c02004-01-28 02:05:05 +00001669 if (!(Flags & DSGraph::RemoveUnreachableGlobals)) {
1670 DSGraph::ScalarMapTy::iterator SMI =
1671 GlobalsGraph->getScalarMap().find(I->first);
1672 if (SMI != GlobalsGraph->getScalarMap().end())
1673 GGCloner.merge(SMI->second, I->second);
1674 else
1675 GGCloner.getClonedNH(I->second);
1676 }
Chris Lattner0b144872004-01-27 22:03:40 +00001677 ++I;
Chris Lattner5f07a8b2003-02-14 06:28:00 +00001678 } else {
Chris Lattnera88a55c2004-01-28 02:41:32 +00001679 DSNode *N = I->second.getNode();
1680#if 0
Chris Lattner0b144872004-01-27 22:03:40 +00001681 // Check to see if this is a worthless node generated for non-pointer
1682 // values, such as integers. Consider an addition of long types: A+B.
1683 // Assuming we can track all uses of the value in this context, and it is
1684 // NOT used as a pointer, we can delete the node. We will be able to
1685 // detect this situation if the node pointed to ONLY has Unknown bit set
1686 // in the node. In this case, the node is not incomplete, does not point
1687 // to any other nodes (no mod/ref bits set), and is therefore
1688 // uninteresting for data structure analysis. If we run across one of
1689 // these, prune the scalar pointing to it.
1690 //
Chris Lattner0b144872004-01-27 22:03:40 +00001691 if (N->getNodeFlags() == DSNode::UnknownNode && !isa<Argument>(I->first))
1692 ScalarMap.erase(I++);
1693 else {
Chris Lattnera88a55c2004-01-28 02:41:32 +00001694#endif
Chris Lattner0b144872004-01-27 22:03:40 +00001695 N->markReachableNodes(Alive);
1696 ++I;
Chris Lattnera88a55c2004-01-28 02:41:32 +00001697 //}
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001698 }
Chris Lattner00948c02004-01-28 02:05:05 +00001699 }
Chris Lattnere2219762002-07-18 18:22:40 +00001700
Chris Lattner0b144872004-01-27 22:03:40 +00001701 // The return values are alive as well.
Chris Lattner5a540632003-06-30 03:15:25 +00001702 for (ReturnNodesTy::iterator I = ReturnNodes.begin(), E = ReturnNodes.end();
1703 I != E; ++I)
1704 I->second.getNode()->markReachableNodes(Alive);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +00001705
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001706 // Mark any nodes reachable by primary calls as alive...
1707 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
1708 FunctionCalls[i].markReachableNodes(Alive);
1709
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001710
1711 // Now find globals and aux call nodes that are already live or reach a live
1712 // value (which makes them live in turn), and continue till no more are found.
1713 //
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001714 bool Iterate;
Chris Lattner41c04f72003-02-01 04:52:08 +00001715 hash_set<DSNode*> Visited;
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001716 std::vector<unsigned char> AuxFCallsAlive(AuxFunctionCalls.size());
1717 do {
1718 Visited.clear();
Chris Lattner70793862003-07-02 23:57:05 +00001719 // If any global node points to a non-global that is "alive", the global is
Chris Lattner72d29a42003-02-11 23:11:51 +00001720 // "alive" as well... Remove it from the GlobalNodes list so we only have
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001721 // unreachable globals in the list.
1722 //
1723 Iterate = false;
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001724 if (!(Flags & DSGraph::RemoveUnreachableGlobals))
Chris Lattner0b144872004-01-27 22:03:40 +00001725 for (unsigned i = 0; i != GlobalNodes.size(); ++i)
1726 if (CanReachAliveNodes(GlobalNodes[i].second, Alive, Visited,
1727 Flags & DSGraph::RemoveUnreachableGlobals)) {
1728 std::swap(GlobalNodes[i--], GlobalNodes.back()); // Move to end to...
1729 GlobalNodes.pop_back(); // erase efficiently
1730 Iterate = true;
1731 }
Chris Lattneraa8146f2002-11-10 06:59:55 +00001732
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001733 // Mark only unresolvable call nodes for moving to the GlobalsGraph since
1734 // call nodes that get resolved will be difficult to remove from that graph.
1735 // The final unresolved call nodes must be handled specially at the end of
1736 // the BU pass (i.e., in main or other roots of the call graph).
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001737 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1738 if (!AuxFCallsAlive[i] &&
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001739 (AuxFunctionCalls[i].isIndirectCall()
1740 || CallSiteUsesAliveArgs(AuxFunctionCalls[i], Alive, Visited,
1741 Flags & DSGraph::RemoveUnreachableGlobals))) {
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001742 AuxFunctionCalls[i].markReachableNodes(Alive);
1743 AuxFCallsAlive[i] = true;
1744 Iterate = true;
1745 }
1746 } while (Iterate);
Chris Lattneraa8146f2002-11-10 06:59:55 +00001747
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001748 // Move dead aux function calls to the end of the list
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001749 unsigned CurIdx = 0;
Chris Lattneraa8146f2002-11-10 06:59:55 +00001750 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1751 if (AuxFCallsAlive[i])
1752 AuxFunctionCalls[CurIdx++].swap(AuxFunctionCalls[i]);
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001753
1754 // Copy and merge all global nodes and dead aux call nodes into the
1755 // GlobalsGraph, and all nodes reachable from those nodes
1756 //
1757 if (!(Flags & DSGraph::RemoveUnreachableGlobals)) {
Chris Lattner0b144872004-01-27 22:03:40 +00001758 // Copy the unreachable call nodes to the globals graph, updating their
1759 // target pointers using the GGCloner
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001760 for (unsigned i = CurIdx, e = AuxFunctionCalls.size(); i != e; ++i)
1761 GlobalsGraph->AuxFunctionCalls.push_back(DSCallSite(AuxFunctionCalls[i],
Chris Lattner0b144872004-01-27 22:03:40 +00001762 GGCloner));
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001763 }
1764 // Crop all the useless ones out...
Chris Lattneraa8146f2002-11-10 06:59:55 +00001765 AuxFunctionCalls.erase(AuxFunctionCalls.begin()+CurIdx,
1766 AuxFunctionCalls.end());
1767
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001768 // We are finally done with the GGCloner so we can destroy it.
1769 GGCloner.destroy();
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001770
Vikram S. Adve40c600e2003-07-22 12:08:58 +00001771 // At this point, any nodes which are visited, but not alive, are nodes
1772 // which can be removed. Loop over all nodes, eliminating completely
1773 // unreachable nodes.
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001774 //
Chris Lattner72d29a42003-02-11 23:11:51 +00001775 std::vector<DSNode*> DeadNodes;
1776 DeadNodes.reserve(Nodes.size());
Chris Lattner51c06ab2004-02-25 23:08:00 +00001777 for (NodeListTy::iterator NI = Nodes.begin(), E = Nodes.end(); NI != E;) {
1778 DSNode *N = NI++;
1779 assert(!N->isForwarding() && "Forwarded node in nodes list?");
1780
1781 if (!Alive.count(N)) {
1782 Nodes.remove(N);
1783 assert(!N->isForwarding() && "Cannot remove a forwarding node!");
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001784 DeadNodes.push_back(N);
1785 N->dropAllReferences();
Chris Lattner51c06ab2004-02-25 23:08:00 +00001786 ++NumDNE;
Chris Lattnere2219762002-07-18 18:22:40 +00001787 }
Chris Lattner51c06ab2004-02-25 23:08:00 +00001788 }
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001789
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001790 // Remove all unreachable globals from the ScalarMap.
1791 // If flag RemoveUnreachableGlobals is set, GlobalNodes has only dead nodes.
1792 // In either case, the dead nodes will not be in the set Alive.
Chris Lattner0b144872004-01-27 22:03:40 +00001793 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001794 if (!Alive.count(GlobalNodes[i].second))
1795 ScalarMap.erase(GlobalNodes[i].first);
Chris Lattner0b144872004-01-27 22:03:40 +00001796 else
1797 assert((Flags & DSGraph::RemoveUnreachableGlobals) && "non-dead global");
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001798
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001799 // Delete all dead nodes now since their referrer counts are zero.
Chris Lattner72d29a42003-02-11 23:11:51 +00001800 for (unsigned i = 0, e = DeadNodes.size(); i != e; ++i)
1801 delete DeadNodes[i];
1802
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001803 DEBUG(AssertGraphOK(); GlobalsGraph->AssertGraphOK());
Chris Lattnere2219762002-07-18 18:22:40 +00001804}
1805
Chris Lattner2c7725a2004-03-03 20:55:27 +00001806void DSGraph::AssertCallSiteInGraph(const DSCallSite &CS) const {
1807 if (CS.isIndirectCall()) {
1808 AssertNodeInGraph(CS.getCalleeNode());
1809#if 0
1810 if (CS.getNumPtrArgs() && CS.getCalleeNode() == CS.getPtrArg(0).getNode() &&
1811 CS.getCalleeNode() && CS.getCalleeNode()->getGlobals().empty())
1812 std::cerr << "WARNING: WIERD CALL SITE FOUND!\n";
1813#endif
1814 }
1815 AssertNodeInGraph(CS.getRetVal().getNode());
1816 for (unsigned j = 0, e = CS.getNumPtrArgs(); j != e; ++j)
1817 AssertNodeInGraph(CS.getPtrArg(j).getNode());
1818}
1819
1820void DSGraph::AssertCallNodesInGraph() const {
1821 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
1822 AssertCallSiteInGraph(FunctionCalls[i]);
1823}
1824void DSGraph::AssertAuxCallNodesInGraph() const {
1825 for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i)
1826 AssertCallSiteInGraph(AuxFunctionCalls[i]);
1827}
1828
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001829void DSGraph::AssertGraphOK() const {
Chris Lattner9fd37ba2004-02-08 00:23:16 +00001830 for (node_iterator NI = node_begin(), E = node_end(); NI != E; ++NI)
1831 (*NI)->assertOK();
Chris Lattner85cfe012003-07-03 02:03:53 +00001832
Chris Lattner8d327672003-06-30 03:36:09 +00001833 for (ScalarMapTy::const_iterator I = ScalarMap.begin(),
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001834 E = ScalarMap.end(); I != E; ++I) {
1835 assert(I->second.getNode() && "Null node in scalarmap!");
1836 AssertNodeInGraph(I->second.getNode());
1837 if (GlobalValue *GV = dyn_cast<GlobalValue>(I->first)) {
Chris Lattnerbd92b732003-06-19 21:15:11 +00001838 assert(I->second.getNode()->isGlobalNode() &&
Chris Lattner0ac7d5c2003-02-03 19:12:15 +00001839 "Global points to node, but node isn't global?");
1840 AssertNodeContainsGlobal(I->second.getNode(), GV);
1841 }
1842 }
1843 AssertCallNodesInGraph();
1844 AssertAuxCallNodesInGraph();
1845}
Vikram S. Adve78bbec72003-07-16 21:36:31 +00001846
Chris Lattner400433d2003-11-11 05:08:59 +00001847/// computeNodeMapping - Given roots in two different DSGraphs, traverse the
1848/// nodes reachable from the two graphs, computing the mapping of nodes from
1849/// the first to the second graph.
1850///
1851void DSGraph::computeNodeMapping(const DSNodeHandle &NH1,
Chris Lattnerafc1dba2003-11-12 17:58:22 +00001852 const DSNodeHandle &NH2, NodeMapTy &NodeMap,
1853 bool StrictChecking) {
Chris Lattner400433d2003-11-11 05:08:59 +00001854 DSNode *N1 = NH1.getNode(), *N2 = NH2.getNode();
1855 if (N1 == 0 || N2 == 0) return;
1856
1857 DSNodeHandle &Entry = NodeMap[N1];
1858 if (Entry.getNode()) {
1859 // Termination of recursion!
Chris Lattnerafc1dba2003-11-12 17:58:22 +00001860 assert(!StrictChecking ||
1861 (Entry.getNode() == N2 &&
1862 Entry.getOffset() == (NH2.getOffset()-NH1.getOffset())) &&
Chris Lattner400433d2003-11-11 05:08:59 +00001863 "Inconsistent mapping detected!");
1864 return;
1865 }
1866
1867 Entry.setNode(N2);
Chris Lattner413406c2003-11-11 20:12:32 +00001868 Entry.setOffset(NH2.getOffset()-NH1.getOffset());
Chris Lattner400433d2003-11-11 05:08:59 +00001869
1870 // Loop over all of the fields that N1 and N2 have in common, recursively
1871 // mapping the edges together now.
1872 int N2Idx = NH2.getOffset()-NH1.getOffset();
1873 unsigned N2Size = N2->getSize();
1874 for (unsigned i = 0, e = N1->getSize(); i < e; i += DS::PointerSize)
1875 if (unsigned(N2Idx)+i < N2Size)
1876 computeNodeMapping(N1->getLink(i), N2->getLink(N2Idx+i), NodeMap);
Chris Lattnerb1aaeee2004-03-03 05:34:31 +00001877 else
1878 computeNodeMapping(N1->getLink(i),
1879 N2->getLink(unsigned(N2Idx+i) % N2Size), NodeMap);
Chris Lattner400433d2003-11-11 05:08:59 +00001880}