Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1 | //===- DataStructure.cpp - Implement the core data structure analysis -----===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 10 | // This file implements the core data structure functionality. |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/DSGraph.h" |
| 15 | #include "llvm/Function.h" |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 16 | #include "llvm/iOther.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 20 | #include "Support/CommandLine.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 21 | #include "Support/Debug.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 22 | #include "Support/STLExtras.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 23 | #include "Support/Statistic.h" |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 24 | #include "Support/Timer.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 28 | namespace { |
Chris Lattner | 33312f7 | 2002-11-08 01:21:07 +0000 | [diff] [blame] | 29 | Statistic<> NumFolds ("dsnode", "Number of nodes completely folded"); |
| 30 | Statistic<> NumCallNodesMerged("dsnode", "Number of call nodes merged"); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 31 | Statistic<> NumNodeAllocated ("dsnode", "Number of nodes allocated"); |
| 32 | |
| 33 | cl::opt<bool> |
| 34 | EnableDSNodeGlobalRootsHack("enable-dsa-globalrootshack", cl::Hidden, |
| 35 | cl::desc("Make DSA less aggressive when cloning graphs")); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Chris Lattner | a88a55c | 2004-01-28 02:41:32 +0000 | [diff] [blame] | 38 | #if 1 |
Chris Lattner | 93ddd7e | 2004-01-22 16:36:28 +0000 | [diff] [blame] | 39 | #define TIME_REGION(VARNAME, DESC) \ |
| 40 | NamedRegionTimer VARNAME(DESC) |
| 41 | #else |
| 42 | #define TIME_REGION(VARNAME, DESC) |
| 43 | #endif |
| 44 | |
Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 45 | using namespace DS; |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 731b2d7 | 2003-02-13 19:09:00 +0000 | [diff] [blame] | 47 | DSNode *DSNodeHandle::HandleForwarding() const { |
| 48 | assert(!N->ForwardNH.isNull() && "Can only be invoked if forwarding!"); |
| 49 | |
| 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 Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 68 | //===----------------------------------------------------------------------===// |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 69 | // DSNode Implementation |
| 70 | //===----------------------------------------------------------------------===// |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 71 | |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 72 | DSNode::DSNode(const Type *T, DSGraph *G) |
Chris Lattner | 7079386 | 2003-07-02 23:57:05 +0000 | [diff] [blame] | 73 | : NumReferrers(0), Size(0), ParentGraph(G), Ty(Type::VoidTy), NodeType(0) { |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 74 | // Add the type entry if it is specified... |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 75 | if (T) mergeTypeInfo(T, 0); |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 76 | G->getNodes().push_back(this); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 77 | ++NumNodeAllocated; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 80 | // DSNode copy constructor... do not copy over the referrers list! |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 81 | DSNode::DSNode(const DSNode &N, DSGraph *G, bool NullLinks) |
Chris Lattner | 7079386 | 2003-07-02 23:57:05 +0000 | [diff] [blame] | 82 | : NumReferrers(0), Size(N.Size), ParentGraph(G), |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 83 | 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 Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 88 | G->getNodes().push_back(this); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 89 | ++NumNodeAllocated; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 92 | /// getTargetData - Get the target data object used to construct this node. |
| 93 | /// |
| 94 | const TargetData &DSNode::getTargetData() const { |
| 95 | return ParentGraph->getTargetData(); |
| 96 | } |
| 97 | |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 98 | void DSNode::assertOK() const { |
| 99 | assert((Ty != Type::VoidTy || |
| 100 | Ty == Type::VoidTy && (Size == 0 || |
| 101 | (NodeType & DSNode::Array))) && |
| 102 | "Node not OK!"); |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 103 | |
| 104 | assert(ParentGraph && "Node has no parent?"); |
Chris Lattner | 62482e5 | 2004-01-28 09:15:42 +0000 | [diff] [blame] | 105 | const DSScalarMap &SM = ParentGraph->getScalarMap(); |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 106 | for (unsigned i = 0, e = Globals.size(); i != e; ++i) { |
Chris Lattner | a88a55c | 2004-01-28 02:41:32 +0000 | [diff] [blame] | 107 | assert(SM.count(Globals[i])); |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 108 | assert(SM.find(Globals[i])->second.getNode() == this); |
| 109 | } |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 110 | } |
| 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 | /// |
| 115 | void 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 Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 128 | // addGlobal - Add an entry for a global value to the Globals list. This also |
| 129 | // marks the node with the 'G' flag if it does not already have it. |
| 130 | // |
| 131 | void DSNode::addGlobal(GlobalValue *GV) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 132 | // Keep the list sorted. |
Chris Lattner | b3416bc | 2003-02-01 04:01:21 +0000 | [diff] [blame] | 133 | std::vector<GlobalValue*>::iterator I = |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 134 | std::lower_bound(Globals.begin(), Globals.end(), GV); |
| 135 | |
| 136 | if (I == Globals.end() || *I != GV) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 137 | //assert(GV->getType()->getElementType() == Ty); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 138 | Globals.insert(I, GV); |
| 139 | NodeType |= GlobalNode; |
| 140 | } |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 143 | /// foldNodeCompletely - If we determine that this node has some funny |
| 144 | /// behavior happening to it that we cannot represent, we fold it down to a |
| 145 | /// single, completely pessimistic, node. This node is represented as a |
| 146 | /// single byte with a single TypeEntry of "void". |
| 147 | /// |
| 148 | void DSNode::foldNodeCompletely() { |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 149 | if (isNodeCompletelyFolded()) return; // If this node is already folded... |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 151 | ++NumFolds; |
| 152 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 153 | // If this node has a size that is <= 1, we don't need to create a forwarding |
| 154 | // node. |
| 155 | if (getSize() <= 1) { |
| 156 | NodeType |= DSNode::Array; |
| 157 | Ty = Type::VoidTy; |
| 158 | Size = 1; |
| 159 | assert(Links.size() <= 1 && "Size is 1, but has more links?"); |
| 160 | Links.resize(1); |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 161 | } else { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 162 | // Create the node we are going to forward to. This is required because |
| 163 | // some referrers may have an offset that is > 0. By forcing them to |
| 164 | // forward, the forwarder has the opportunity to correct the offset. |
| 165 | DSNode *DestNode = new DSNode(0, ParentGraph); |
| 166 | DestNode->NodeType = NodeType|DSNode::Array; |
| 167 | DestNode->Ty = Type::VoidTy; |
| 168 | DestNode->Size = 1; |
| 169 | DestNode->Globals.swap(Globals); |
| 170 | |
| 171 | // Start forwarding to the destination node... |
| 172 | forwardNode(DestNode, 0); |
| 173 | |
| 174 | if (!Links.empty()) { |
| 175 | DestNode->Links.reserve(1); |
| 176 | |
| 177 | DSNodeHandle NH(DestNode); |
| 178 | DestNode->Links.push_back(Links[0]); |
| 179 | |
| 180 | // If we have links, merge all of our outgoing links together... |
| 181 | for (unsigned i = Links.size()-1; i != 0; --i) |
| 182 | NH.getNode()->Links[0].mergeWith(Links[i]); |
| 183 | Links.clear(); |
| 184 | } else { |
| 185 | DestNode->Links.resize(1); |
| 186 | } |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 190 | /// isNodeCompletelyFolded - Return true if this node has been completely |
| 191 | /// folded down to something that can never be expanded, effectively losing |
| 192 | /// all of the field sensitivity that may be present in the node. |
| 193 | /// |
| 194 | bool DSNode::isNodeCompletelyFolded() const { |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 195 | return getSize() == 1 && Ty == Type::VoidTy && isArray(); |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 198 | namespace { |
| 199 | /// TypeElementWalker Class - Used for implementation of physical subtyping... |
| 200 | /// |
| 201 | class TypeElementWalker { |
| 202 | struct StackState { |
| 203 | const Type *Ty; |
| 204 | unsigned Offset; |
| 205 | unsigned Idx; |
| 206 | StackState(const Type *T, unsigned Off = 0) |
| 207 | : Ty(T), Offset(Off), Idx(0) {} |
| 208 | }; |
| 209 | |
| 210 | std::vector<StackState> Stack; |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 211 | const TargetData &TD; |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 212 | public: |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 213 | TypeElementWalker(const Type *T, const TargetData &td) : TD(td) { |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 214 | Stack.push_back(T); |
| 215 | StepToLeaf(); |
| 216 | } |
| 217 | |
| 218 | bool isDone() const { return Stack.empty(); } |
| 219 | const Type *getCurrentType() const { return Stack.back().Ty; } |
| 220 | unsigned getCurrentOffset() const { return Stack.back().Offset; } |
| 221 | |
| 222 | void StepToNextType() { |
| 223 | PopStackAndAdvance(); |
| 224 | StepToLeaf(); |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | /// PopStackAndAdvance - Pop the current element off of the stack and |
| 229 | /// advance the underlying element to the next contained member. |
| 230 | void PopStackAndAdvance() { |
| 231 | assert(!Stack.empty() && "Cannot pop an empty stack!"); |
| 232 | Stack.pop_back(); |
| 233 | while (!Stack.empty()) { |
| 234 | StackState &SS = Stack.back(); |
| 235 | if (const StructType *ST = dyn_cast<StructType>(SS.Ty)) { |
| 236 | ++SS.Idx; |
| 237 | if (SS.Idx != ST->getElementTypes().size()) { |
| 238 | const StructLayout *SL = TD.getStructLayout(ST); |
| 239 | SS.Offset += SL->MemberOffsets[SS.Idx]-SL->MemberOffsets[SS.Idx-1]; |
| 240 | return; |
| 241 | } |
| 242 | Stack.pop_back(); // At the end of the structure |
| 243 | } else { |
| 244 | const ArrayType *AT = cast<ArrayType>(SS.Ty); |
| 245 | ++SS.Idx; |
| 246 | if (SS.Idx != AT->getNumElements()) { |
| 247 | SS.Offset += TD.getTypeSize(AT->getElementType()); |
| 248 | return; |
| 249 | } |
| 250 | Stack.pop_back(); // At the end of the array |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /// StepToLeaf - Used by physical subtyping to move to the first leaf node |
| 256 | /// on the type stack. |
| 257 | void StepToLeaf() { |
| 258 | if (Stack.empty()) return; |
| 259 | while (!Stack.empty() && !Stack.back().Ty->isFirstClassType()) { |
| 260 | StackState &SS = Stack.back(); |
| 261 | if (const StructType *ST = dyn_cast<StructType>(SS.Ty)) { |
| 262 | if (ST->getElementTypes().empty()) { |
| 263 | assert(SS.Idx == 0); |
| 264 | PopStackAndAdvance(); |
| 265 | } else { |
| 266 | // Step into the structure... |
| 267 | assert(SS.Idx < ST->getElementTypes().size()); |
| 268 | const StructLayout *SL = TD.getStructLayout(ST); |
| 269 | Stack.push_back(StackState(ST->getElementTypes()[SS.Idx], |
| 270 | SS.Offset+SL->MemberOffsets[SS.Idx])); |
| 271 | } |
| 272 | } else { |
| 273 | const ArrayType *AT = cast<ArrayType>(SS.Ty); |
| 274 | if (AT->getNumElements() == 0) { |
| 275 | assert(SS.Idx == 0); |
| 276 | PopStackAndAdvance(); |
| 277 | } else { |
| 278 | // Step into the array... |
| 279 | assert(SS.Idx < AT->getNumElements()); |
| 280 | Stack.push_back(StackState(AT->getElementType(), |
| 281 | SS.Offset+SS.Idx* |
| 282 | TD.getTypeSize(AT->getElementType()))); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | }; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 288 | } // end anonymous namespace |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 289 | |
| 290 | /// ElementTypesAreCompatible - Check to see if the specified types are |
| 291 | /// "physically" compatible. If so, return true, else return false. We only |
Chris Lattner | dbfe36e | 2003-11-02 21:02:20 +0000 | [diff] [blame] | 292 | /// have to check the fields in T1: T2 may be larger than T1. If AllowLargerT1 |
| 293 | /// is true, then we also allow a larger T1. |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 294 | /// |
Chris Lattner | dbfe36e | 2003-11-02 21:02:20 +0000 | [diff] [blame] | 295 | static bool ElementTypesAreCompatible(const Type *T1, const Type *T2, |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 296 | bool AllowLargerT1, const TargetData &TD){ |
| 297 | TypeElementWalker T1W(T1, TD), T2W(T2, TD); |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 298 | |
| 299 | while (!T1W.isDone() && !T2W.isDone()) { |
| 300 | if (T1W.getCurrentOffset() != T2W.getCurrentOffset()) |
| 301 | return false; |
| 302 | |
| 303 | const Type *T1 = T1W.getCurrentType(); |
| 304 | const Type *T2 = T2W.getCurrentType(); |
| 305 | if (T1 != T2 && !T1->isLosslesslyConvertibleTo(T2)) |
| 306 | return false; |
| 307 | |
| 308 | T1W.StepToNextType(); |
| 309 | T2W.StepToNextType(); |
| 310 | } |
| 311 | |
Chris Lattner | dbfe36e | 2003-11-02 21:02:20 +0000 | [diff] [blame] | 312 | return AllowLargerT1 || T1W.isDone(); |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 316 | /// mergeTypeInfo - This method merges the specified type into the current node |
| 317 | /// at the specified offset. This may update the current node's type record if |
| 318 | /// this gives more information to the node, it may do nothing to the node if |
| 319 | /// this information is already known, or it may merge the node completely (and |
| 320 | /// return true) if the information is incompatible with what is already known. |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 321 | /// |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 322 | /// This method returns true if the node is completely folded, otherwise false. |
| 323 | /// |
Chris Lattner | 088b639 | 2003-03-03 17:13:31 +0000 | [diff] [blame] | 324 | bool DSNode::mergeTypeInfo(const Type *NewTy, unsigned Offset, |
| 325 | bool FoldIfIncompatible) { |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 326 | const TargetData &TD = getTargetData(); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 327 | // Check to make sure the Size member is up-to-date. Size can be one of the |
| 328 | // following: |
| 329 | // Size = 0, Ty = Void: Nothing is known about this node. |
| 330 | // Size = 0, Ty = FnTy: FunctionPtr doesn't have a size, so we use zero |
| 331 | // Size = 1, Ty = Void, Array = 1: The node is collapsed |
| 332 | // Otherwise, sizeof(Ty) = Size |
| 333 | // |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 334 | assert(((Size == 0 && Ty == Type::VoidTy && !isArray()) || |
| 335 | (Size == 0 && !Ty->isSized() && !isArray()) || |
| 336 | (Size == 1 && Ty == Type::VoidTy && isArray()) || |
| 337 | (Size == 0 && !Ty->isSized() && !isArray()) || |
| 338 | (TD.getTypeSize(Ty) == Size)) && |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 339 | "Size member of DSNode doesn't match the type structure!"); |
| 340 | assert(NewTy != Type::VoidTy && "Cannot merge void type into DSNode!"); |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 342 | if (Offset == 0 && NewTy == Ty) |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 343 | return false; // This should be a common case, handle it efficiently |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 345 | // Return true immediately if the node is completely folded. |
| 346 | if (isNodeCompletelyFolded()) return true; |
| 347 | |
Chris Lattner | 23f83dc | 2002-11-08 22:49:57 +0000 | [diff] [blame] | 348 | // If this is an array type, eliminate the outside arrays because they won't |
| 349 | // be used anyway. This greatly reduces the size of large static arrays used |
| 350 | // as global variables, for example. |
| 351 | // |
Chris Lattner | d888893 | 2002-11-09 19:25:27 +0000 | [diff] [blame] | 352 | bool WillBeArray = false; |
Chris Lattner | 23f83dc | 2002-11-08 22:49:57 +0000 | [diff] [blame] | 353 | while (const ArrayType *AT = dyn_cast<ArrayType>(NewTy)) { |
| 354 | // FIXME: we might want to keep small arrays, but must be careful about |
| 355 | // things like: [2 x [10000 x int*]] |
| 356 | NewTy = AT->getElementType(); |
Chris Lattner | d888893 | 2002-11-09 19:25:27 +0000 | [diff] [blame] | 357 | WillBeArray = true; |
Chris Lattner | 23f83dc | 2002-11-08 22:49:57 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 360 | // Figure out how big the new type we're merging in is... |
| 361 | unsigned NewTySize = NewTy->isSized() ? TD.getTypeSize(NewTy) : 0; |
| 362 | |
| 363 | // Otherwise check to see if we can fold this type into the current node. If |
| 364 | // we can't, we fold the node completely, if we can, we potentially update our |
| 365 | // internal state. |
| 366 | // |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 367 | if (Ty == Type::VoidTy) { |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 368 | // If this is the first type that this node has seen, just accept it without |
| 369 | // question.... |
Chris Lattner | dbfe36e | 2003-11-02 21:02:20 +0000 | [diff] [blame] | 370 | assert(Offset == 0 && !isArray() && |
| 371 | "Cannot have an offset into a void node!"); |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 372 | Ty = NewTy; |
| 373 | NodeType &= ~Array; |
| 374 | if (WillBeArray) NodeType |= Array; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 375 | Size = NewTySize; |
| 376 | |
| 377 | // Calculate the number of outgoing links from this node. |
| 378 | Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift); |
| 379 | return false; |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 381 | |
| 382 | // Handle node expansion case here... |
| 383 | if (Offset+NewTySize > Size) { |
| 384 | // It is illegal to grow this node if we have treated it as an array of |
| 385 | // objects... |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 386 | if (isArray()) { |
Chris Lattner | 088b639 | 2003-03-03 17:13:31 +0000 | [diff] [blame] | 387 | if (FoldIfIncompatible) foldNodeCompletely(); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 388 | return true; |
| 389 | } |
| 390 | |
| 391 | if (Offset) { // We could handle this case, but we don't for now... |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 392 | std::cerr << "UNIMP: Trying to merge a growth type into " |
| 393 | << "offset != 0: Collapsing!\n"; |
Chris Lattner | 088b639 | 2003-03-03 17:13:31 +0000 | [diff] [blame] | 394 | if (FoldIfIncompatible) foldNodeCompletely(); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | |
| 398 | // Okay, the situation is nice and simple, we are trying to merge a type in |
| 399 | // at offset 0 that is bigger than our current type. Implement this by |
| 400 | // switching to the new type and then merge in the smaller one, which should |
| 401 | // hit the other code path here. If the other code path decides it's not |
| 402 | // ok, it will collapse the node as appropriate. |
| 403 | // |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 404 | const Type *OldTy = Ty; |
| 405 | Ty = NewTy; |
| 406 | NodeType &= ~Array; |
| 407 | if (WillBeArray) NodeType |= Array; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 408 | Size = NewTySize; |
| 409 | |
| 410 | // Must grow links to be the appropriate size... |
| 411 | Links.resize((Size+DS::PointerSize-1) >> DS::PointerShift); |
| 412 | |
| 413 | // Merge in the old type now... which is guaranteed to be smaller than the |
| 414 | // "current" type. |
| 415 | return mergeTypeInfo(OldTy, 0); |
| 416 | } |
| 417 | |
Chris Lattner | f17b39a | 2002-11-07 04:59:28 +0000 | [diff] [blame] | 418 | assert(Offset <= Size && |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 419 | "Cannot merge something into a part of our type that doesn't exist!"); |
| 420 | |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 421 | // Find the section of Ty that NewTy overlaps with... first we find the |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 422 | // type that starts at offset Offset. |
| 423 | // |
| 424 | unsigned O = 0; |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 425 | const Type *SubType = Ty; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 426 | while (O < Offset) { |
| 427 | assert(Offset-O < TD.getTypeSize(SubType) && "Offset out of range!"); |
| 428 | |
| 429 | switch (SubType->getPrimitiveID()) { |
| 430 | case Type::StructTyID: { |
| 431 | const StructType *STy = cast<StructType>(SubType); |
| 432 | const StructLayout &SL = *TD.getStructLayout(STy); |
| 433 | |
| 434 | unsigned i = 0, e = SL.MemberOffsets.size(); |
| 435 | for (; i+1 < e && SL.MemberOffsets[i+1] <= Offset-O; ++i) |
| 436 | /* empty */; |
| 437 | |
| 438 | // The offset we are looking for must be in the i'th element... |
| 439 | SubType = STy->getElementTypes()[i]; |
| 440 | O += SL.MemberOffsets[i]; |
| 441 | break; |
| 442 | } |
| 443 | case Type::ArrayTyID: { |
| 444 | SubType = cast<ArrayType>(SubType)->getElementType(); |
| 445 | unsigned ElSize = TD.getTypeSize(SubType); |
| 446 | unsigned Remainder = (Offset-O) % ElSize; |
| 447 | O = Offset-Remainder; |
| 448 | break; |
| 449 | } |
| 450 | default: |
Chris Lattner | 088b639 | 2003-03-03 17:13:31 +0000 | [diff] [blame] | 451 | if (FoldIfIncompatible) foldNodeCompletely(); |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 452 | return true; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | assert(O == Offset && "Could not achieve the correct offset!"); |
| 457 | |
| 458 | // If we found our type exactly, early exit |
| 459 | if (SubType == NewTy) return false; |
| 460 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 461 | // Differing function types don't require us to merge. They are not values anyway. |
| 462 | if (isa<FunctionType>(SubType) && |
| 463 | isa<FunctionType>(NewTy)) return false; |
| 464 | |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 465 | unsigned SubTypeSize = SubType->isSized() ? TD.getTypeSize(SubType) : 0; |
| 466 | |
| 467 | // Ok, we are getting desperate now. Check for physical subtyping, where we |
| 468 | // just require each element in the node to be compatible. |
Chris Lattner | 06e24c8 | 2003-06-29 22:36:31 +0000 | [diff] [blame] | 469 | if (NewTySize <= SubTypeSize && NewTySize && NewTySize < 256 && |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 470 | SubTypeSize && SubTypeSize < 256 && |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 471 | ElementTypesAreCompatible(NewTy, SubType, !isArray(), TD)) |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 472 | return false; |
| 473 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 474 | // Okay, so we found the leader type at the offset requested. Search the list |
| 475 | // of types that starts at this offset. If SubType is currently an array or |
| 476 | // structure, the type desired may actually be the first element of the |
| 477 | // composite type... |
| 478 | // |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 479 | unsigned PadSize = SubTypeSize; // Size, including pad memory which is ignored |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 480 | while (SubType != NewTy) { |
| 481 | const Type *NextSubType = 0; |
Chris Lattner | bf10f05 | 2002-11-09 00:49:05 +0000 | [diff] [blame] | 482 | unsigned NextSubTypeSize = 0; |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 483 | unsigned NextPadSize = 0; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 484 | switch (SubType->getPrimitiveID()) { |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 485 | case Type::StructTyID: { |
| 486 | const StructType *STy = cast<StructType>(SubType); |
| 487 | const StructLayout &SL = *TD.getStructLayout(STy); |
| 488 | if (SL.MemberOffsets.size() > 1) |
| 489 | NextPadSize = SL.MemberOffsets[1]; |
| 490 | else |
| 491 | NextPadSize = SubTypeSize; |
| 492 | NextSubType = STy->getElementTypes()[0]; |
| 493 | NextSubTypeSize = TD.getTypeSize(NextSubType); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 494 | break; |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 496 | case Type::ArrayTyID: |
| 497 | NextSubType = cast<ArrayType>(SubType)->getElementType(); |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 498 | NextSubTypeSize = TD.getTypeSize(NextSubType); |
| 499 | NextPadSize = NextSubTypeSize; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 500 | break; |
| 501 | default: ; |
| 502 | // fall out |
| 503 | } |
| 504 | |
| 505 | if (NextSubType == 0) |
| 506 | break; // In the default case, break out of the loop |
| 507 | |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 508 | if (NextPadSize < NewTySize) |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 509 | break; // Don't allow shrinking to a smaller type than NewTySize |
| 510 | SubType = NextSubType; |
| 511 | SubTypeSize = NextSubTypeSize; |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 512 | PadSize = NextPadSize; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // If we found the type exactly, return it... |
| 516 | if (SubType == NewTy) |
| 517 | return false; |
| 518 | |
| 519 | // Check to see if we have a compatible, but different type... |
| 520 | if (NewTySize == SubTypeSize) { |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 521 | // Check to see if this type is obviously convertible... int -> uint f.e. |
| 522 | if (NewTy->isLosslesslyConvertibleTo(SubType)) |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 523 | return false; |
| 524 | |
| 525 | // Check to see if we have a pointer & integer mismatch going on here, |
| 526 | // loading a pointer as a long, for example. |
| 527 | // |
| 528 | if (SubType->isInteger() && isa<PointerType>(NewTy) || |
| 529 | NewTy->isInteger() && isa<PointerType>(SubType)) |
| 530 | return false; |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 531 | } else if (NewTySize > SubTypeSize && NewTySize <= PadSize) { |
| 532 | // We are accessing the field, plus some structure padding. Ignore the |
| 533 | // structure padding. |
| 534 | return false; |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 537 | Module *M = 0; |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 538 | if (getParentGraph()->getReturnNodes().size()) |
| 539 | M = getParentGraph()->getReturnNodes().begin()->first->getParent(); |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 540 | DEBUG(std::cerr << "MergeTypeInfo Folding OrigTy: "; |
| 541 | WriteTypeSymbolic(std::cerr, Ty, M) << "\n due to:"; |
| 542 | WriteTypeSymbolic(std::cerr, NewTy, M) << " @ " << Offset << "!\n" |
| 543 | << "SubType: "; |
| 544 | WriteTypeSymbolic(std::cerr, SubType, M) << "\n\n"); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 088b639 | 2003-03-03 17:13:31 +0000 | [diff] [blame] | 546 | if (FoldIfIncompatible) foldNodeCompletely(); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 547 | return true; |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 550 | |
| 551 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 552 | // addEdgeTo - Add an edge from the current node to the specified node. This |
| 553 | // can cause merging of nodes in the graph. |
| 554 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 555 | void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 556 | if (NH.isNull()) return; // Nothing to do |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 558 | DSNodeHandle &ExistingEdge = getLink(Offset); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 559 | if (!ExistingEdge.isNull()) { |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 560 | // Merge the two nodes... |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 561 | ExistingEdge.mergeWith(NH); |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 562 | } else { // No merging to perform... |
| 563 | setLink(Offset, NH); // Just force a link in there... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 564 | } |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 565 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 566 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 567 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 568 | // MergeSortedVectors - Efficiently merge a vector into another vector where |
| 569 | // duplicates are not allowed and both are sorted. This assumes that 'T's are |
| 570 | // efficiently copyable and have sane comparison semantics. |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 571 | // |
Chris Lattner | b3416bc | 2003-02-01 04:01:21 +0000 | [diff] [blame] | 572 | static void MergeSortedVectors(std::vector<GlobalValue*> &Dest, |
| 573 | const std::vector<GlobalValue*> &Src) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 574 | // By far, the most common cases will be the simple ones. In these cases, |
| 575 | // avoid having to allocate a temporary vector... |
| 576 | // |
| 577 | if (Src.empty()) { // Nothing to merge in... |
| 578 | return; |
| 579 | } else if (Dest.empty()) { // Just copy the result in... |
| 580 | Dest = Src; |
| 581 | } else if (Src.size() == 1) { // Insert a single element... |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 582 | const GlobalValue *V = Src[0]; |
Chris Lattner | b3416bc | 2003-02-01 04:01:21 +0000 | [diff] [blame] | 583 | std::vector<GlobalValue*>::iterator I = |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 584 | std::lower_bound(Dest.begin(), Dest.end(), V); |
| 585 | if (I == Dest.end() || *I != Src[0]) // If not already contained... |
| 586 | Dest.insert(I, Src[0]); |
| 587 | } else if (Dest.size() == 1) { |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 588 | GlobalValue *Tmp = Dest[0]; // Save value in temporary... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 589 | Dest = Src; // Copy over list... |
Chris Lattner | b3416bc | 2003-02-01 04:01:21 +0000 | [diff] [blame] | 590 | std::vector<GlobalValue*>::iterator I = |
Chris Lattner | 5190ce8 | 2002-11-12 07:20:45 +0000 | [diff] [blame] | 591 | std::lower_bound(Dest.begin(), Dest.end(), Tmp); |
| 592 | if (I == Dest.end() || *I != Tmp) // If not already contained... |
| 593 | Dest.insert(I, Tmp); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 594 | |
| 595 | } else { |
| 596 | // Make a copy to the side of Dest... |
Chris Lattner | b3416bc | 2003-02-01 04:01:21 +0000 | [diff] [blame] | 597 | std::vector<GlobalValue*> Old(Dest); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 598 | |
| 599 | // Make space for all of the type entries now... |
| 600 | Dest.resize(Dest.size()+Src.size()); |
| 601 | |
| 602 | // Merge the two sorted ranges together... into Dest. |
| 603 | std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin()); |
| 604 | |
| 605 | // Now erase any duplicate entries that may have accumulated into the |
| 606 | // vectors (because they were in both of the input sets) |
| 607 | Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end()); |
| 608 | } |
| 609 | } |
| 610 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 611 | void DSNode::mergeGlobals(const std::vector<GlobalValue*> &RHS) { |
| 612 | MergeSortedVectors(Globals, RHS); |
| 613 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 614 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 615 | // MergeNodes - Helper function for DSNode::mergeWith(). |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 616 | // This function does the hard work of merging two nodes, CurNodeH |
| 617 | // and NH after filtering out trivial cases and making sure that |
| 618 | // CurNodeH.offset >= NH.offset. |
| 619 | // |
| 620 | // ***WARNING*** |
| 621 | // Since merging may cause either node to go away, we must always |
| 622 | // use the node-handles to refer to the nodes. These node handles are |
| 623 | // automatically updated during merging, so will always provide access |
| 624 | // to the correct node after a merge. |
| 625 | // |
| 626 | void DSNode::MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH) { |
| 627 | assert(CurNodeH.getOffset() >= NH.getOffset() && |
| 628 | "This should have been enforced in the caller."); |
| 629 | |
| 630 | // Now we know that Offset >= NH.Offset, so convert it so our "Offset" (with |
| 631 | // respect to NH.Offset) is now zero. NOffset is the distance from the base |
| 632 | // of our object that N starts from. |
| 633 | // |
| 634 | unsigned NOffset = CurNodeH.getOffset()-NH.getOffset(); |
| 635 | unsigned NSize = NH.getNode()->getSize(); |
| 636 | |
Chris Lattner | 5c5b10f | 2003-06-29 20:27:45 +0000 | [diff] [blame] | 637 | // If the two nodes are of different size, and the smaller node has the array |
| 638 | // bit set, collapse! |
| 639 | if (NSize != CurNodeH.getNode()->getSize()) { |
| 640 | if (NSize < CurNodeH.getNode()->getSize()) { |
| 641 | if (NH.getNode()->isArray()) |
| 642 | NH.getNode()->foldNodeCompletely(); |
| 643 | } else if (CurNodeH.getNode()->isArray()) { |
| 644 | NH.getNode()->foldNodeCompletely(); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | // Merge the type entries of the two nodes together... |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 649 | if (NH.getNode()->Ty != Type::VoidTy) |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 650 | CurNodeH.getNode()->mergeTypeInfo(NH.getNode()->Ty, NOffset); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 651 | assert(!CurNodeH.getNode()->isDeadNode()); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 652 | |
| 653 | // If we are merging a node with a completely folded node, then both nodes are |
| 654 | // now completely folded. |
| 655 | // |
| 656 | if (CurNodeH.getNode()->isNodeCompletelyFolded()) { |
| 657 | if (!NH.getNode()->isNodeCompletelyFolded()) { |
| 658 | NH.getNode()->foldNodeCompletely(); |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 659 | assert(NH.getNode() && NH.getOffset() == 0 && |
| 660 | "folding did not make offset 0?"); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 661 | NOffset = NH.getOffset(); |
| 662 | NSize = NH.getNode()->getSize(); |
| 663 | assert(NOffset == 0 && NSize == 1); |
| 664 | } |
| 665 | } else if (NH.getNode()->isNodeCompletelyFolded()) { |
| 666 | CurNodeH.getNode()->foldNodeCompletely(); |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 667 | assert(CurNodeH.getNode() && CurNodeH.getOffset() == 0 && |
| 668 | "folding did not make offset 0?"); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 669 | NOffset = NH.getOffset(); |
| 670 | NSize = NH.getNode()->getSize(); |
| 671 | assert(NOffset == 0 && NSize == 1); |
| 672 | } |
| 673 | |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 674 | DSNode *N = NH.getNode(); |
| 675 | if (CurNodeH.getNode() == N || N == 0) return; |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 676 | assert(!CurNodeH.getNode()->isDeadNode()); |
| 677 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 678 | // Merge the NodeType information. |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 679 | CurNodeH.getNode()->NodeType |= N->NodeType; |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 681 | // Start forwarding to the new node! |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 682 | N->forwardNode(CurNodeH.getNode(), NOffset); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 683 | assert(!CurNodeH.getNode()->isDeadNode()); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 685 | // Make all of the outgoing links of N now be outgoing links of CurNodeH. |
| 686 | // |
| 687 | for (unsigned i = 0; i < N->getNumLinks(); ++i) { |
| 688 | DSNodeHandle &Link = N->getLink(i << DS::PointerShift); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 689 | if (Link.getNode()) { |
| 690 | // Compute the offset into the current node at which to |
| 691 | // merge this link. In the common case, this is a linear |
| 692 | // relation to the offset in the original node (with |
| 693 | // wrapping), but if the current node gets collapsed due to |
| 694 | // recursive merging, we must make sure to merge in all remaining |
| 695 | // links at offset zero. |
| 696 | unsigned MergeOffset = 0; |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 697 | DSNode *CN = CurNodeH.getNode(); |
| 698 | if (CN->Size != 1) |
| 699 | MergeOffset = ((i << DS::PointerShift)+NOffset) % CN->getSize(); |
| 700 | CN->addEdgeTo(MergeOffset, Link); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | // Now that there are no outgoing edges, all of the Links are dead. |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 705 | N->Links.clear(); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 706 | |
| 707 | // Merge the globals list... |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 708 | if (!N->Globals.empty()) { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 709 | CurNodeH.getNode()->mergeGlobals(N->Globals); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 710 | |
| 711 | // Delete the globals from the old node... |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 712 | std::vector<GlobalValue*>().swap(N->Globals); |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 717 | // mergeWith - Merge this node and the specified node, moving all links to and |
| 718 | // from the argument node into the current node, deleting the node argument. |
| 719 | // Offset indicates what offset the specified node is to be merged into the |
| 720 | // current node. |
| 721 | // |
Chris Lattner | 5254a8d | 2004-01-22 16:31:08 +0000 | [diff] [blame] | 722 | // The specified node may be a null pointer (in which case, we update it to |
| 723 | // point to this node). |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 724 | // |
| 725 | void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) { |
| 726 | DSNode *N = NH.getNode(); |
Chris Lattner | 5254a8d | 2004-01-22 16:31:08 +0000 | [diff] [blame] | 727 | if (N == this && NH.getOffset() == Offset) |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 728 | return; // Noop |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 729 | |
Chris Lattner | 5254a8d | 2004-01-22 16:31:08 +0000 | [diff] [blame] | 730 | // If the RHS is a null node, make it point to this node! |
| 731 | if (N == 0) { |
| 732 | NH.mergeWith(DSNodeHandle(this, Offset)); |
| 733 | return; |
| 734 | } |
| 735 | |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 736 | assert(!N->isDeadNode() && !isDeadNode()); |
Chris Lattner | 679e8e1 | 2002-11-08 21:27:12 +0000 | [diff] [blame] | 737 | assert(!hasNoReferrers() && "Should not try to fold a useless node!"); |
| 738 | |
Chris Lattner | 0260663 | 2002-11-04 06:48:26 +0000 | [diff] [blame] | 739 | if (N == this) { |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 740 | // We cannot merge two pieces of the same node together, collapse the node |
| 741 | // completely. |
Chris Lattner | 3c87b29 | 2002-11-07 01:54:56 +0000 | [diff] [blame] | 742 | DEBUG(std::cerr << "Attempting to merge two chunks of" |
| 743 | << " the same node together!\n"); |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 744 | foldNodeCompletely(); |
Chris Lattner | 0260663 | 2002-11-04 06:48:26 +0000 | [diff] [blame] | 745 | return; |
| 746 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 747 | |
Chris Lattner | 5190ce8 | 2002-11-12 07:20:45 +0000 | [diff] [blame] | 748 | // If both nodes are not at offset 0, make sure that we are merging the node |
| 749 | // at an later offset into the node with the zero offset. |
| 750 | // |
| 751 | if (Offset < NH.getOffset()) { |
| 752 | N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset()); |
| 753 | return; |
| 754 | } else if (Offset == NH.getOffset() && getSize() < N->getSize()) { |
| 755 | // If the offsets are the same, merge the smaller node into the bigger node |
| 756 | N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset()); |
| 757 | return; |
| 758 | } |
| 759 | |
Vikram S. Adve | 2b7a92c | 2002-12-06 21:15:21 +0000 | [diff] [blame] | 760 | // Ok, now we can merge the two nodes. Use a static helper that works with |
| 761 | // two node handles, since "this" may get merged away at intermediate steps. |
| 762 | DSNodeHandle CurNodeH(this, Offset); |
| 763 | DSNodeHandle NHCopy(NH); |
| 764 | DSNode::MergeNodes(CurNodeH, NHCopy); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 767 | |
| 768 | //===----------------------------------------------------------------------===// |
| 769 | // ReachabilityCloner Implementation |
| 770 | //===----------------------------------------------------------------------===// |
| 771 | |
| 772 | DSNodeHandle ReachabilityCloner::getClonedNH(const DSNodeHandle &SrcNH) { |
| 773 | if (SrcNH.isNull()) return DSNodeHandle(); |
| 774 | const DSNode *SN = SrcNH.getNode(); |
| 775 | |
| 776 | DSNodeHandle &NH = NodeMap[SN]; |
| 777 | if (!NH.isNull()) // Node already mapped? |
| 778 | return DSNodeHandle(NH.getNode(), NH.getOffset()+SrcNH.getOffset()); |
| 779 | |
| 780 | DSNode *DN = new DSNode(*SN, &Dest, true /* Null out all links */); |
| 781 | DN->maskNodeTypes(BitsToKeep); |
Chris Lattner | 00948c0 | 2004-01-28 02:05:05 +0000 | [diff] [blame] | 782 | NH = DN; |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 783 | |
| 784 | // Next, recursively clone all outgoing links as necessary. Note that |
| 785 | // adding these links can cause the node to collapse itself at any time, and |
| 786 | // the current node may be merged with arbitrary other nodes. For this |
| 787 | // reason, we must always go through NH. |
| 788 | DN = 0; |
| 789 | for (unsigned i = 0, e = SN->getNumLinks(); i != e; ++i) { |
| 790 | const DSNodeHandle &SrcEdge = SN->getLink(i << DS::PointerShift); |
| 791 | if (!SrcEdge.isNull()) { |
| 792 | const DSNodeHandle &DestEdge = getClonedNH(SrcEdge); |
| 793 | // Compute the offset into the current node at which to |
| 794 | // merge this link. In the common case, this is a linear |
| 795 | // relation to the offset in the original node (with |
| 796 | // wrapping), but if the current node gets collapsed due to |
| 797 | // recursive merging, we must make sure to merge in all remaining |
| 798 | // links at offset zero. |
| 799 | unsigned MergeOffset = 0; |
| 800 | DSNode *CN = NH.getNode(); |
| 801 | if (CN->getSize() != 1) |
| 802 | MergeOffset = ((i << DS::PointerShift)+NH.getOffset() |
| 803 | - SrcNH.getOffset()) %CN->getSize(); |
| 804 | CN->addEdgeTo(MergeOffset, DestEdge); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | // If this node contains any globals, make sure they end up in the scalar |
| 809 | // map with the correct offset. |
| 810 | for (DSNode::global_iterator I = SN->global_begin(), E = SN->global_end(); |
| 811 | I != E; ++I) { |
| 812 | GlobalValue *GV = *I; |
| 813 | const DSNodeHandle &SrcGNH = Src.getNodeForValue(GV); |
| 814 | DSNodeHandle &DestGNH = NodeMap[SrcGNH.getNode()]; |
| 815 | assert(DestGNH.getNode() == NH.getNode() &&"Global mapping inconsistent"); |
| 816 | Dest.getNodeForValue(GV).mergeWith(DSNodeHandle(DestGNH.getNode(), |
Chris Lattner | 00948c0 | 2004-01-28 02:05:05 +0000 | [diff] [blame] | 817 | DestGNH.getOffset()+SrcGNH.getOffset())); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 818 | |
| 819 | if (CloneFlags & DSGraph::UpdateInlinedGlobals) |
| 820 | Dest.getInlinedGlobals().insert(GV); |
| 821 | } |
| 822 | |
| 823 | return DSNodeHandle(NH.getNode(), NH.getOffset()+SrcNH.getOffset()); |
| 824 | } |
| 825 | |
| 826 | void ReachabilityCloner::merge(const DSNodeHandle &NH, |
| 827 | const DSNodeHandle &SrcNH) { |
| 828 | if (SrcNH.isNull()) return; // Noop |
| 829 | if (NH.isNull()) { |
| 830 | // If there is no destination node, just clone the source and assign the |
| 831 | // destination node to be it. |
| 832 | NH.mergeWith(getClonedNH(SrcNH)); |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | // Okay, at this point, we know that we have both a destination and a source |
| 837 | // node that need to be merged. Check to see if the source node has already |
| 838 | // been cloned. |
| 839 | const DSNode *SN = SrcNH.getNode(); |
| 840 | DSNodeHandle &SCNH = NodeMap[SN]; // SourceClonedNodeHandle |
| 841 | if (SCNH.getNode()) { // Node already cloned? |
| 842 | NH.mergeWith(DSNodeHandle(SCNH.getNode(), |
| 843 | SCNH.getOffset()+SrcNH.getOffset())); |
| 844 | |
| 845 | return; // Nothing to do! |
| 846 | } |
| 847 | |
| 848 | // Okay, so the source node has not already been cloned. Instead of creating |
| 849 | // a new DSNode, only to merge it into the one we already have, try to perform |
| 850 | // the merge in-place. The only case we cannot handle here is when the offset |
| 851 | // into the existing node is less than the offset into the virtual node we are |
| 852 | // merging in. In this case, we have to extend the existing node, which |
| 853 | // requires an allocation anyway. |
| 854 | DSNode *DN = NH.getNode(); // Make sure the Offset is up-to-date |
| 855 | if (NH.getOffset() >= SrcNH.getOffset()) { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 856 | if (!DN->isNodeCompletelyFolded()) { |
| 857 | // Make sure the destination node is folded if the source node is folded. |
| 858 | if (SN->isNodeCompletelyFolded()) { |
| 859 | DN->foldNodeCompletely(); |
| 860 | DN = NH.getNode(); |
| 861 | } else if (SN->getSize() != DN->getSize()) { |
| 862 | // If the two nodes are of different size, and the smaller node has the |
| 863 | // array bit set, collapse! |
| 864 | if (SN->getSize() < DN->getSize()) { |
| 865 | if (SN->isArray()) { |
| 866 | DN->foldNodeCompletely(); |
| 867 | DN = NH.getNode(); |
| 868 | } |
| 869 | } else if (DN->isArray()) { |
| 870 | DN->foldNodeCompletely(); |
| 871 | DN = NH.getNode(); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // Merge the type entries of the two nodes together... |
| 876 | if (SN->getType() != Type::VoidTy && !DN->isNodeCompletelyFolded()) { |
| 877 | DN->mergeTypeInfo(SN->getType(), NH.getOffset()-SrcNH.getOffset()); |
| 878 | DN = NH.getNode(); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | assert(!DN->isDeadNode()); |
| 883 | |
| 884 | // Merge the NodeType information. |
| 885 | DN->mergeNodeFlags(SN->getNodeFlags() & BitsToKeep); |
| 886 | |
| 887 | // Before we start merging outgoing links and updating the scalar map, make |
| 888 | // sure it is known that this is the representative node for the src node. |
| 889 | SCNH = DSNodeHandle(DN, NH.getOffset()-SrcNH.getOffset()); |
| 890 | |
| 891 | // If the source node contains any globals, make sure they end up in the |
| 892 | // scalar map with the correct offset. |
| 893 | if (SN->global_begin() != SN->global_end()) { |
| 894 | // Update the globals in the destination node itself. |
| 895 | DN->mergeGlobals(SN->getGlobals()); |
| 896 | |
| 897 | // Update the scalar map for the graph we are merging the source node |
| 898 | // into. |
| 899 | for (DSNode::global_iterator I = SN->global_begin(), E = SN->global_end(); |
| 900 | I != E; ++I) { |
| 901 | GlobalValue *GV = *I; |
| 902 | const DSNodeHandle &SrcGNH = Src.getNodeForValue(GV); |
| 903 | DSNodeHandle &DestGNH = NodeMap[SrcGNH.getNode()]; |
| 904 | assert(DestGNH.getNode()==NH.getNode() &&"Global mapping inconsistent"); |
| 905 | Dest.getNodeForValue(GV).mergeWith(DSNodeHandle(DestGNH.getNode(), |
Chris Lattner | ead9eb7 | 2004-01-29 08:36:22 +0000 | [diff] [blame^] | 906 | DestGNH.getOffset()+SrcGNH.getOffset())); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 907 | |
| 908 | if (CloneFlags & DSGraph::UpdateInlinedGlobals) |
| 909 | Dest.getInlinedGlobals().insert(GV); |
| 910 | } |
| 911 | } |
| 912 | } else { |
| 913 | // We cannot handle this case without allocating a temporary node. Fall |
| 914 | // back on being simple. |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 915 | DSNode *NewDN = new DSNode(*SN, &Dest, true /* Null out all links */); |
| 916 | NewDN->maskNodeTypes(BitsToKeep); |
| 917 | |
| 918 | unsigned NHOffset = NH.getOffset(); |
| 919 | NH.mergeWith(DSNodeHandle(NewDN, SrcNH.getOffset())); |
Chris Lattner | ead9eb7 | 2004-01-29 08:36:22 +0000 | [diff] [blame^] | 920 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 921 | assert(NH.getNode() && |
| 922 | (NH.getOffset() > NHOffset || |
| 923 | (NH.getOffset() == 0 && NH.getNode()->isNodeCompletelyFolded())) && |
| 924 | "Merging did not adjust the offset!"); |
| 925 | |
| 926 | // Before we start merging outgoing links and updating the scalar map, make |
| 927 | // sure it is known that this is the representative node for the src node. |
| 928 | SCNH = DSNodeHandle(NH.getNode(), NH.getOffset()-SrcNH.getOffset()); |
Chris Lattner | ead9eb7 | 2004-01-29 08:36:22 +0000 | [diff] [blame^] | 929 | |
| 930 | // If the source node contained any globals, make sure to create entries |
| 931 | // in the scalar map for them! |
| 932 | for (DSNode::global_iterator I = SN->global_begin(), E = SN->global_end(); |
| 933 | I != E; ++I) { |
| 934 | GlobalValue *GV = *I; |
| 935 | const DSNodeHandle &SrcGNH = Src.getNodeForValue(GV); |
| 936 | DSNodeHandle &DestGNH = NodeMap[SrcGNH.getNode()]; |
| 937 | assert(DestGNH.getNode()==NH.getNode() &&"Global mapping inconsistent"); |
| 938 | assert(SrcGNH.getNode() == SN && "Global mapping inconsistent"); |
| 939 | Dest.getNodeForValue(GV).mergeWith(DSNodeHandle(DestGNH.getNode(), |
| 940 | DestGNH.getOffset()+SrcGNH.getOffset())); |
| 941 | |
| 942 | if (CloneFlags & DSGraph::UpdateInlinedGlobals) |
| 943 | Dest.getInlinedGlobals().insert(GV); |
| 944 | } |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
| 948 | // Next, recursively merge all outgoing links as necessary. Note that |
| 949 | // adding these links can cause the destination node to collapse itself at |
| 950 | // any time, and the current node may be merged with arbitrary other nodes. |
| 951 | // For this reason, we must always go through NH. |
| 952 | DN = 0; |
| 953 | for (unsigned i = 0, e = SN->getNumLinks(); i != e; ++i) { |
| 954 | const DSNodeHandle &SrcEdge = SN->getLink(i << DS::PointerShift); |
| 955 | if (!SrcEdge.isNull()) { |
| 956 | // Compute the offset into the current node at which to |
| 957 | // merge this link. In the common case, this is a linear |
| 958 | // relation to the offset in the original node (with |
| 959 | // wrapping), but if the current node gets collapsed due to |
| 960 | // recursive merging, we must make sure to merge in all remaining |
| 961 | // links at offset zero. |
| 962 | unsigned MergeOffset = 0; |
| 963 | DSNode *CN = SCNH.getNode(); |
| 964 | if (CN->getSize() != 1) |
| 965 | MergeOffset = ((i << DS::PointerShift)+SCNH.getOffset()) %CN->getSize(); |
| 966 | |
| 967 | // Perform the recursive merging. Make sure to create a temporary NH, |
| 968 | // because the Link can disappear in the process of recursive merging. |
| 969 | DSNodeHandle Tmp = CN->getLink(MergeOffset); |
| 970 | merge(Tmp, SrcEdge); |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | /// mergeCallSite - Merge the nodes reachable from the specified src call |
| 976 | /// site into the nodes reachable from DestCS. |
| 977 | void ReachabilityCloner::mergeCallSite(const DSCallSite &DestCS, |
| 978 | const DSCallSite &SrcCS) { |
| 979 | merge(DestCS.getRetVal(), SrcCS.getRetVal()); |
| 980 | unsigned MinArgs = DestCS.getNumPtrArgs(); |
| 981 | if (SrcCS.getNumPtrArgs() < MinArgs) MinArgs = SrcCS.getNumPtrArgs(); |
| 982 | |
| 983 | for (unsigned a = 0; a != MinArgs; ++a) |
| 984 | merge(DestCS.getPtrArg(a), SrcCS.getPtrArg(a)); |
| 985 | } |
| 986 | |
| 987 | |
Chris Lattner | 9de906c | 2002-10-20 22:11:44 +0000 | [diff] [blame] | 988 | //===----------------------------------------------------------------------===// |
| 989 | // DSCallSite Implementation |
| 990 | //===----------------------------------------------------------------------===// |
| 991 | |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 992 | // Define here to avoid including iOther.h and BasicBlock.h in DSGraph.h |
Chris Lattner | 9de906c | 2002-10-20 22:11:44 +0000 | [diff] [blame] | 993 | Function &DSCallSite::getCaller() const { |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 994 | return *Site.getInstruction()->getParent()->getParent(); |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 995 | } |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 996 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 997 | void DSCallSite::InitNH(DSNodeHandle &NH, const DSNodeHandle &Src, |
| 998 | ReachabilityCloner &RC) { |
| 999 | NH = RC.getClonedNH(Src); |
| 1000 | } |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1002 | //===----------------------------------------------------------------------===// |
| 1003 | // DSGraph Implementation |
| 1004 | //===----------------------------------------------------------------------===// |
| 1005 | |
Chris Lattner | a9d6566 | 2003-06-30 05:57:30 +0000 | [diff] [blame] | 1006 | /// getFunctionNames - Return a space separated list of the name of the |
| 1007 | /// functions in this graph (if any) |
| 1008 | std::string DSGraph::getFunctionNames() const { |
| 1009 | switch (getReturnNodes().size()) { |
| 1010 | case 0: return "Globals graph"; |
| 1011 | case 1: return getReturnNodes().begin()->first->getName(); |
| 1012 | default: |
| 1013 | std::string Return; |
| 1014 | for (DSGraph::ReturnNodesTy::const_iterator I = getReturnNodes().begin(); |
| 1015 | I != getReturnNodes().end(); ++I) |
| 1016 | Return += I->first->getName() + " "; |
| 1017 | Return.erase(Return.end()-1, Return.end()); // Remove last space character |
| 1018 | return Return; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 1023 | DSGraph::DSGraph(const DSGraph &G) : GlobalsGraph(0), TD(G.TD) { |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1024 | PrintAuxCalls = false; |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1025 | NodeMapTy NodeMap; |
| 1026 | cloneInto(G, ScalarMap, ReturnNodes, NodeMap); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1029 | DSGraph::DSGraph(const DSGraph &G, NodeMapTy &NodeMap) |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 1030 | : GlobalsGraph(0), TD(G.TD) { |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1031 | PrintAuxCalls = false; |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1032 | cloneInto(G, ScalarMap, ReturnNodes, NodeMap); |
Chris Lattner | eff0da9 | 2002-10-21 15:32:34 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1035 | DSGraph::~DSGraph() { |
| 1036 | FunctionCalls.clear(); |
Chris Lattner | 679e8e1 | 2002-11-08 21:27:12 +0000 | [diff] [blame] | 1037 | AuxFunctionCalls.clear(); |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1038 | InlinedGlobals.clear(); |
Chris Lattner | c875f02 | 2002-11-03 21:27:48 +0000 | [diff] [blame] | 1039 | ScalarMap.clear(); |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1040 | ReturnNodes.clear(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1041 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1042 | // Drop all intra-node references, so that assertions don't fail... |
| 1043 | std::for_each(Nodes.begin(), Nodes.end(), |
| 1044 | std::mem_fun(&DSNode::dropAllReferences)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Delete all of the nodes themselves... |
| 1047 | std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>); |
| 1048 | } |
| 1049 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1050 | // dump - Allow inspection of graph in a debugger. |
| 1051 | void DSGraph::dump() const { print(std::cerr); } |
| 1052 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1054 | /// remapLinks - Change all of the Links in the current node according to the |
| 1055 | /// specified mapping. |
Chris Lattner | 8f0a16e | 2002-10-31 05:45:02 +0000 | [diff] [blame] | 1056 | /// |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 1057 | void DSNode::remapLinks(DSGraph::NodeMapTy &OldNodeMap) { |
Chris Lattner | 2f56138 | 2004-01-22 16:56:13 +0000 | [diff] [blame] | 1058 | for (unsigned i = 0, e = Links.size(); i != e; ++i) |
| 1059 | if (DSNode *N = Links[i].getNode()) { |
Chris Lattner | 091f776 | 2004-01-23 01:44:53 +0000 | [diff] [blame] | 1060 | DSGraph::NodeMapTy::const_iterator ONMI = OldNodeMap.find(N); |
| 1061 | if (ONMI != OldNodeMap.end()) { |
| 1062 | Links[i].setNode(ONMI->second.getNode()); |
| 1063 | Links[i].setOffset(Links[i].getOffset()+ONMI->second.getOffset()); |
| 1064 | } |
Chris Lattner | 2f56138 | 2004-01-22 16:56:13 +0000 | [diff] [blame] | 1065 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1066 | } |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1067 | |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1068 | /// updateFromGlobalGraph - This function rematerializes global nodes and |
| 1069 | /// nodes reachable from them from the globals graph into the current graph. |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1070 | /// It uses the vector InlinedGlobals to avoid cloning and merging globals that |
| 1071 | /// are already up-to-date in the current graph. In practice, in the TD pass, |
| 1072 | /// this is likely to be a large fraction of the live global nodes in each |
| 1073 | /// function (since most live nodes are likely to have been brought up-to-date |
| 1074 | /// in at _some_ caller or callee). |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1075 | /// |
| 1076 | void DSGraph::updateFromGlobalGraph() { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1077 | TIME_REGION(X, "updateFromGlobalGraph"); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1078 | ReachabilityCloner RC(*this, *GlobalsGraph, 0); |
| 1079 | |
| 1080 | // Clone the non-up-to-date global nodes into this graph. |
Chris Lattner | bdce7b7 | 2004-01-28 03:03:06 +0000 | [diff] [blame] | 1081 | for (DSScalarMap::global_iterator I = getScalarMap().global_begin(), |
| 1082 | E = getScalarMap().global_end(); I != E; ++I) |
| 1083 | if (InlinedGlobals.count(*I) == 0) { // GNode is not up-to-date |
Chris Lattner | 62482e5 | 2004-01-28 09:15:42 +0000 | [diff] [blame] | 1084 | DSScalarMap::iterator It = GlobalsGraph->ScalarMap.find(*I); |
Chris Lattner | bdce7b7 | 2004-01-28 03:03:06 +0000 | [diff] [blame] | 1085 | if (It != GlobalsGraph->ScalarMap.end()) |
| 1086 | RC.merge(getNodeForValue(*I), It->second); |
| 1087 | } |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1090 | /// cloneInto - Clone the specified DSGraph into the current graph. The |
| 1091 | /// translated ScalarMap for the old function is filled into the OldValMap |
| 1092 | /// member, and the translated ReturnNodes map is returned into ReturnNodes. |
| 1093 | /// |
| 1094 | /// The CloneFlags member controls various aspects of the cloning process. |
| 1095 | /// |
Chris Lattner | 62482e5 | 2004-01-28 09:15:42 +0000 | [diff] [blame] | 1096 | void DSGraph::cloneInto(const DSGraph &G, DSScalarMap &OldValMap, |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1097 | ReturnNodesTy &OldReturnNodes, NodeMapTy &OldNodeMap, |
| 1098 | unsigned CloneFlags) { |
Chris Lattner | 93ddd7e | 2004-01-22 16:36:28 +0000 | [diff] [blame] | 1099 | TIME_REGION(X, "cloneInto"); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1100 | assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!"); |
Chris Lattner | 33312f7 | 2002-11-08 01:21:07 +0000 | [diff] [blame] | 1101 | assert(&G != this && "Cannot clone graph into itself!"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1102 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1103 | unsigned FN = Nodes.size(); // First new node... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Duplicate all of the nodes, populating the node map... |
| 1106 | Nodes.reserve(FN+G.Nodes.size()); |
Chris Lattner | 1e88369 | 2003-02-03 20:08:51 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Remove alloca or mod/ref bits as specified... |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1109 | unsigned BitsToClear = ((CloneFlags & StripAllocaBit)? DSNode::AllocaNode : 0) |
| 1110 | | ((CloneFlags & StripModRefBits)? (DSNode::Modified | DSNode::Read) : 0) |
| 1111 | | ((CloneFlags & StripIncompleteBit)? DSNode::Incomplete : 0); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1112 | BitsToClear |= DSNode::DEAD; // Clear dead flag... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1113 | for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1114 | DSNode *Old = G.Nodes[i]; |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1115 | DSNode *New = new DSNode(*Old, this); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1116 | New->maskNodeTypes(~BitsToClear); |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 1117 | OldNodeMap[Old] = New; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1118 | } |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 1119 | #ifndef NDEBUG |
| 1120 | Timer::addPeakMemoryMeasurement(); |
| 1121 | #endif |
| 1122 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1123 | // Rewrite the links in the new nodes to point into the current graph now. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1124 | for (unsigned i = FN, e = Nodes.size(); i != e; ++i) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1125 | Nodes[i]->remapLinks(OldNodeMap); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1127 | // Copy the scalar map... merging all of the global nodes... |
Chris Lattner | 62482e5 | 2004-01-28 09:15:42 +0000 | [diff] [blame] | 1128 | for (DSScalarMap::const_iterator I = G.ScalarMap.begin(), |
Chris Lattner | c875f02 | 2002-11-03 21:27:48 +0000 | [diff] [blame] | 1129 | E = G.ScalarMap.end(); I != E; ++I) { |
Chris Lattner | f8c6aab | 2002-11-08 05:01:14 +0000 | [diff] [blame] | 1130 | DSNodeHandle &MappedNode = OldNodeMap[I->second.getNode()]; |
Chris Lattner | 2cb9acd | 2003-06-30 05:09:29 +0000 | [diff] [blame] | 1131 | DSNodeHandle &H = OldValMap[I->first]; |
| 1132 | H.mergeWith(DSNodeHandle(MappedNode.getNode(), |
| 1133 | I->second.getOffset()+MappedNode.getOffset())); |
Chris Lattner | cf15db3 | 2002-10-17 20:09:52 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | 2cb9acd | 2003-06-30 05:09:29 +0000 | [diff] [blame] | 1135 | // If this is a global, add the global to this fn or merge if already exists |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1136 | if (GlobalValue* GV = dyn_cast<GlobalValue>(I->first)) { |
| 1137 | ScalarMap[GV].mergeWith(H); |
Chris Lattner | 091f776 | 2004-01-23 01:44:53 +0000 | [diff] [blame] | 1138 | if (CloneFlags & DSGraph::UpdateInlinedGlobals) |
| 1139 | InlinedGlobals.insert(GV); |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1140 | } |
Chris Lattner | cf15db3 | 2002-10-17 20:09:52 +0000 | [diff] [blame] | 1141 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | 679e8e1 | 2002-11-08 21:27:12 +0000 | [diff] [blame] | 1143 | if (!(CloneFlags & DontCloneCallNodes)) { |
| 1144 | // Copy the function calls list... |
| 1145 | unsigned FC = FunctionCalls.size(); // FirstCall |
| 1146 | FunctionCalls.reserve(FC+G.FunctionCalls.size()); |
| 1147 | for (unsigned i = 0, ei = G.FunctionCalls.size(); i != ei; ++i) |
| 1148 | FunctionCalls.push_back(DSCallSite(G.FunctionCalls[i], OldNodeMap)); |
Chris Lattner | acf491f | 2002-11-08 22:27:09 +0000 | [diff] [blame] | 1149 | } |
Chris Lattner | 679e8e1 | 2002-11-08 21:27:12 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | acf491f | 2002-11-08 22:27:09 +0000 | [diff] [blame] | 1151 | if (!(CloneFlags & DontCloneAuxCallNodes)) { |
Misha Brukman | 2f2d065 | 2003-09-11 18:14:24 +0000 | [diff] [blame] | 1152 | // Copy the auxiliary function calls list... |
Chris Lattner | acf491f | 2002-11-08 22:27:09 +0000 | [diff] [blame] | 1153 | unsigned FC = AuxFunctionCalls.size(); // FirstCall |
Chris Lattner | 679e8e1 | 2002-11-08 21:27:12 +0000 | [diff] [blame] | 1154 | AuxFunctionCalls.reserve(FC+G.AuxFunctionCalls.size()); |
| 1155 | for (unsigned i = 0, ei = G.AuxFunctionCalls.size(); i != ei; ++i) |
| 1156 | AuxFunctionCalls.push_back(DSCallSite(G.AuxFunctionCalls[i], OldNodeMap)); |
| 1157 | } |
Chris Lattner | cf15db3 | 2002-10-17 20:09:52 +0000 | [diff] [blame] | 1158 | |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1159 | // Map the return node pointers over... |
| 1160 | for (ReturnNodesTy::const_iterator I = G.getReturnNodes().begin(), |
| 1161 | E = G.getReturnNodes().end(); I != E; ++I) { |
| 1162 | const DSNodeHandle &Ret = I->second; |
| 1163 | DSNodeHandle &MappedRet = OldNodeMap[Ret.getNode()]; |
| 1164 | OldReturnNodes.insert(std::make_pair(I->first, |
| 1165 | DSNodeHandle(MappedRet.getNode(), |
| 1166 | MappedRet.getOffset()+Ret.getOffset()))); |
| 1167 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 1171 | /// mergeInGraph - The method is used for merging graphs together. If the |
| 1172 | /// argument graph is not *this, it makes a clone of the specified graph, then |
| 1173 | /// merges the nodes specified in the call site with the formal arguments in the |
| 1174 | /// graph. |
| 1175 | /// |
Chris Lattner | 9f93055 | 2003-06-30 05:27:18 +0000 | [diff] [blame] | 1176 | void DSGraph::mergeInGraph(const DSCallSite &CS, Function &F, |
| 1177 | const DSGraph &Graph, unsigned CloneFlags) { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1178 | TIME_REGION(X, "mergeInGraph"); |
| 1179 | |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 1180 | // If this is not a recursive call, clone the graph into this graph... |
| 1181 | if (&Graph != this) { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1182 | // Clone the callee's graph into the current graph, keeping track of where |
| 1183 | // scalars in the old graph _used_ to point, and of the new nodes matching |
| 1184 | // nodes of the old graph. |
| 1185 | ReachabilityCloner RC(*this, Graph, CloneFlags); |
| 1186 | |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1187 | // Set up argument bindings |
| 1188 | Function::aiterator AI = F.abegin(); |
| 1189 | for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) { |
| 1190 | // Advance the argument iterator to the first pointer argument... |
| 1191 | while (AI != F.aend() && !isPointerType(AI->getType())) { |
| 1192 | ++AI; |
Chris Lattner | 17a93e2 | 2004-01-29 03:32:15 +0000 | [diff] [blame] | 1193 | #ifndef NDEBUG // FIXME: We should merge vararg arguments! |
| 1194 | if (AI == F.aend() && !F.getFunctionType()->isVarArg()) |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1195 | std::cerr << "Bad call to Function: " << F.getName() << "\n"; |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 1196 | #endif |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1197 | } |
| 1198 | if (AI == F.aend()) break; |
| 1199 | |
| 1200 | // Add the link from the argument scalar to the provided value. |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1201 | RC.merge(CS.getPtrArg(i), Graph.getNodeForValue(AI)); |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1204 | // Map the return node pointer over. |
| 1205 | if (CS.getRetVal().getNode()) |
| 1206 | RC.merge(CS.getRetVal(), Graph.getReturnNodeFor(F)); |
| 1207 | |
| 1208 | // If requested, copy the calls or aux-calls lists. |
| 1209 | if (!(CloneFlags & DontCloneCallNodes)) { |
| 1210 | // Copy the function calls list... |
| 1211 | FunctionCalls.reserve(FunctionCalls.size()+Graph.FunctionCalls.size()); |
| 1212 | for (unsigned i = 0, ei = Graph.FunctionCalls.size(); i != ei; ++i) |
| 1213 | FunctionCalls.push_back(DSCallSite(Graph.FunctionCalls[i], RC)); |
| 1214 | } |
| 1215 | |
| 1216 | if (!(CloneFlags & DontCloneAuxCallNodes)) { |
| 1217 | // Copy the auxiliary function calls list... |
| 1218 | AuxFunctionCalls.reserve(AuxFunctionCalls.size()+ |
| 1219 | Graph.AuxFunctionCalls.size()); |
| 1220 | for (unsigned i = 0, ei = Graph.AuxFunctionCalls.size(); i != ei; ++i) |
| 1221 | AuxFunctionCalls.push_back(DSCallSite(Graph.AuxFunctionCalls[i], RC)); |
| 1222 | } |
| 1223 | |
| 1224 | // If the user requested it, add the nodes that we need to clone to the |
| 1225 | // RootNodes set. |
| 1226 | if (!EnableDSNodeGlobalRootsHack) |
| 1227 | for (unsigned i = 0, e = Graph.Nodes.size(); i != e; ++i) |
| 1228 | if (!Graph.Nodes[i]->getGlobals().empty()) |
| 1229 | RC.getClonedNH(Graph.Nodes[i]); |
| 1230 | |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1231 | } else { |
| 1232 | DSNodeHandle RetVal = getReturnNodeFor(F); |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Merge the return value with the return value of the context... |
| 1235 | RetVal.mergeWith(CS.getRetVal()); |
| 1236 | |
| 1237 | // Resolve all of the function arguments... |
| 1238 | Function::aiterator AI = F.abegin(); |
| 1239 | |
| 1240 | for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i, ++AI) { |
| 1241 | // Advance the argument iterator to the first pointer argument... |
| 1242 | while (AI != F.aend() && !isPointerType(AI->getType())) { |
| 1243 | ++AI; |
Chris Lattner | 17a93e2 | 2004-01-29 03:32:15 +0000 | [diff] [blame] | 1244 | #ifndef NDEBUG // FIXME: We should merge varargs arguments!! |
| 1245 | if (AI == F.aend() && !F.getFunctionType()->isVarArg()) |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1246 | std::cerr << "Bad call to Function: " << F.getName() << "\n"; |
| 1247 | #endif |
| 1248 | } |
| 1249 | if (AI == F.aend()) break; |
| 1250 | |
| 1251 | // Add the link from the argument scalar to the provided value |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1252 | DSNodeHandle &NH = getNodeForValue(AI); |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1253 | assert(NH.getNode() && "Pointer argument without scalarmap entry?"); |
| 1254 | NH.mergeWith(CS.getPtrArg(i)); |
| 1255 | } |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 1259 | /// getCallSiteForArguments - Get the arguments and return value bindings for |
| 1260 | /// the specified function in the current graph. |
| 1261 | /// |
| 1262 | DSCallSite DSGraph::getCallSiteForArguments(Function &F) const { |
| 1263 | std::vector<DSNodeHandle> Args; |
| 1264 | |
| 1265 | for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) |
| 1266 | if (isPointerType(I->getType())) |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1267 | Args.push_back(getNodeForValue(I)); |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 1269 | return DSCallSite(CallSite(), getReturnNodeFor(F), &F, Args); |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1274 | // markIncompleteNodes - Mark the specified node as having contents that are not |
| 1275 | // known with the current analysis we have performed. Because a node makes all |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1276 | // of the nodes it can reach incomplete if the node itself is incomplete, we |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1277 | // must recursively traverse the data structure graph, marking all reachable |
| 1278 | // nodes as incomplete. |
| 1279 | // |
| 1280 | static void markIncompleteNode(DSNode *N) { |
| 1281 | // Stop recursion if no node, or if node already marked... |
Chris Lattner | 72d50a0 | 2003-06-28 21:58:28 +0000 | [diff] [blame] | 1282 | if (N == 0 || N->isIncomplete()) return; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Actually mark the node |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1285 | N->setIncompleteMarker(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1286 | |
Misha Brukman | 2f2d065 | 2003-09-11 18:14:24 +0000 | [diff] [blame] | 1287 | // Recursively process children... |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 1288 | for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize) |
| 1289 | if (DSNode *DSN = N->getLink(i).getNode()) |
| 1290 | markIncompleteNode(DSN); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Chris Lattner | e71ffc2 | 2002-11-11 03:36:55 +0000 | [diff] [blame] | 1293 | static void markIncomplete(DSCallSite &Call) { |
| 1294 | // Then the return value is certainly incomplete! |
| 1295 | markIncompleteNode(Call.getRetVal().getNode()); |
| 1296 | |
| 1297 | // All objects pointed to by function arguments are incomplete! |
| 1298 | for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i) |
| 1299 | markIncompleteNode(Call.getPtrArg(i).getNode()); |
| 1300 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1301 | |
| 1302 | // markIncompleteNodes - Traverse the graph, identifying nodes that may be |
| 1303 | // modified by other functions that have not been resolved yet. This marks |
| 1304 | // nodes that are reachable through three sources of "unknownness": |
| 1305 | // |
| 1306 | // Global Variables, Function Calls, and Incoming Arguments |
| 1307 | // |
| 1308 | // For any node that may have unknown components (because something outside the |
| 1309 | // scope of current analysis may have modified it), the 'Incomplete' flag is |
| 1310 | // added to the NodeType. |
| 1311 | // |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 1312 | void DSGraph::markIncompleteNodes(unsigned Flags) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1313 | // Mark any incoming arguments as incomplete... |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1314 | if (Flags & DSGraph::MarkFormalArgs) |
| 1315 | for (ReturnNodesTy::iterator FI = ReturnNodes.begin(), E =ReturnNodes.end(); |
| 1316 | FI != E; ++FI) { |
| 1317 | Function &F = *FI->first; |
| 1318 | if (F.getName() != "main") |
| 1319 | for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1320 | if (isPointerType(I->getType())) |
| 1321 | markIncompleteNode(getNodeForValue(I).getNode()); |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1322 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Mark stuff passed into functions calls as being incomplete... |
Chris Lattner | e71ffc2 | 2002-11-11 03:36:55 +0000 | [diff] [blame] | 1325 | if (!shouldPrintAuxCalls()) |
| 1326 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) |
| 1327 | markIncomplete(FunctionCalls[i]); |
| 1328 | else |
| 1329 | for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i) |
| 1330 | markIncomplete(AuxFunctionCalls[i]); |
| 1331 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | 93d7a21 | 2003-02-09 18:41:49 +0000 | [diff] [blame] | 1333 | // Mark all global nodes as incomplete... |
| 1334 | if ((Flags & DSGraph::IgnoreGlobals) == 0) |
| 1335 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1336 | if (Nodes[i]->isGlobalNode() && Nodes[i]->getNumLinks()) |
Chris Lattner | 93d7a21 | 2003-02-09 18:41:49 +0000 | [diff] [blame] | 1337 | markIncompleteNode(Nodes[i]); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1340 | static inline void killIfUselessEdge(DSNodeHandle &Edge) { |
| 1341 | if (DSNode *N = Edge.getNode()) // Is there an edge? |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1342 | if (N->getNumReferrers() == 1) // Does it point to a lonely node? |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1343 | // No interesting info? |
| 1344 | if ((N->getNodeFlags() & ~DSNode::Incomplete) == 0 && |
Chris Lattner | 1855292 | 2002-11-18 21:44:46 +0000 | [diff] [blame] | 1345 | N->getType() == Type::VoidTy && !N->isNodeCompletelyFolded()) |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1346 | Edge.setNode(0); // Kill the edge! |
| 1347 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1348 | |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1349 | static inline bool nodeContainsExternalFunction(const DSNode *N) { |
| 1350 | const std::vector<GlobalValue*> &Globals = N->getGlobals(); |
| 1351 | for (unsigned i = 0, e = Globals.size(); i != e; ++i) |
| 1352 | if (Globals[i]->isExternal()) |
| 1353 | return true; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1354 | return false; |
| 1355 | } |
| 1356 | |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1357 | static void removeIdenticalCalls(std::vector<DSCallSite> &Calls) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1358 | // Remove trivially identical function calls |
| 1359 | unsigned NumFns = Calls.size(); |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1360 | std::sort(Calls.begin(), Calls.end()); // Sort by callee as primary key! |
| 1361 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1362 | #if 1 |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1363 | // Scan the call list cleaning it up as necessary... |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 1364 | DSNode *LastCalleeNode = 0; |
| 1365 | Function *LastCalleeFunc = 0; |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1366 | unsigned NumDuplicateCalls = 0; |
| 1367 | bool LastCalleeContainsExternalFunction = false; |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1368 | for (unsigned i = 0; i != Calls.size(); ++i) { |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1369 | DSCallSite &CS = Calls[i]; |
| 1370 | |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1371 | // If the Callee is a useless edge, this must be an unreachable call site, |
| 1372 | // eliminate it. |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1373 | if (CS.isIndirectCall() && CS.getCalleeNode()->getNumReferrers() == 1 && |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1374 | CS.getCalleeNode()->getNodeFlags() == 0) { // No useful info? |
Chris Lattner | 64507e3 | 2004-01-28 01:19:52 +0000 | [diff] [blame] | 1375 | #ifndef NDEBUG |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 1376 | std::cerr << "WARNING: Useless call site found??\n"; |
Chris Lattner | 64507e3 | 2004-01-28 01:19:52 +0000 | [diff] [blame] | 1377 | #endif |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1378 | CS.swap(Calls.back()); |
| 1379 | Calls.pop_back(); |
| 1380 | --i; |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1381 | } else { |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1382 | // If the return value or any arguments point to a void node with no |
| 1383 | // information at all in it, and the call node is the only node to point |
| 1384 | // to it, remove the edge to the node (killing the node). |
| 1385 | // |
| 1386 | killIfUselessEdge(CS.getRetVal()); |
| 1387 | for (unsigned a = 0, e = CS.getNumPtrArgs(); a != e; ++a) |
| 1388 | killIfUselessEdge(CS.getPtrArg(a)); |
| 1389 | |
| 1390 | // If this call site calls the same function as the last call site, and if |
| 1391 | // the function pointer contains an external function, this node will |
| 1392 | // never be resolved. Merge the arguments of the call node because no |
| 1393 | // information will be lost. |
| 1394 | // |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 1395 | if ((CS.isDirectCall() && CS.getCalleeFunc() == LastCalleeFunc) || |
| 1396 | (CS.isIndirectCall() && CS.getCalleeNode() == LastCalleeNode)) { |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1397 | ++NumDuplicateCalls; |
| 1398 | if (NumDuplicateCalls == 1) { |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 1399 | if (LastCalleeNode) |
| 1400 | LastCalleeContainsExternalFunction = |
| 1401 | nodeContainsExternalFunction(LastCalleeNode); |
| 1402 | else |
| 1403 | LastCalleeContainsExternalFunction = LastCalleeFunc->isExternal(); |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1404 | } |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1405 | |
| 1406 | // It is not clear why, but enabling this code makes DSA really |
| 1407 | // sensitive to node forwarding. Basically, with this enabled, DSA |
| 1408 | // performs different number of inlinings based on which nodes are |
| 1409 | // forwarding or not. This is clearly a problem, so this code is |
| 1410 | // disabled until this can be resolved. |
Chris Lattner | 58f98d0 | 2003-07-02 04:38:49 +0000 | [diff] [blame] | 1411 | #if 1 |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1412 | if (LastCalleeContainsExternalFunction |
| 1413 | #if 0 |
| 1414 | || |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1415 | // This should be more than enough context sensitivity! |
| 1416 | // FIXME: Evaluate how many times this is tripped! |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1417 | NumDuplicateCalls > 20 |
| 1418 | #endif |
| 1419 | ) { |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1420 | DSCallSite &OCS = Calls[i-1]; |
| 1421 | OCS.mergeWith(CS); |
| 1422 | |
| 1423 | // The node will now be eliminated as a duplicate! |
| 1424 | if (CS.getNumPtrArgs() < OCS.getNumPtrArgs()) |
| 1425 | CS = OCS; |
| 1426 | else if (CS.getNumPtrArgs() > OCS.getNumPtrArgs()) |
| 1427 | OCS = CS; |
| 1428 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 1429 | #endif |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1430 | } else { |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 1431 | if (CS.isDirectCall()) { |
| 1432 | LastCalleeFunc = CS.getCalleeFunc(); |
| 1433 | LastCalleeNode = 0; |
| 1434 | } else { |
| 1435 | LastCalleeNode = CS.getCalleeNode(); |
| 1436 | LastCalleeFunc = 0; |
| 1437 | } |
Chris Lattner | e425844 | 2002-11-11 21:35:38 +0000 | [diff] [blame] | 1438 | NumDuplicateCalls = 0; |
| 1439 | } |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1442 | #endif |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1443 | Calls.erase(std::unique(Calls.begin(), Calls.end()), Calls.end()); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1444 | |
Chris Lattner | 33312f7 | 2002-11-08 01:21:07 +0000 | [diff] [blame] | 1445 | // Track the number of call nodes merged away... |
| 1446 | NumCallNodesMerged += NumFns-Calls.size(); |
| 1447 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1448 | DEBUG(if (NumFns != Calls.size()) |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1449 | std::cerr << "Merged " << (NumFns-Calls.size()) << " call nodes.\n";); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1450 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1452 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1453 | // removeTriviallyDeadNodes - After the graph has been constructed, this method |
| 1454 | // removes all unreachable nodes that are created because they got merged with |
| 1455 | // other nodes in the graph. These nodes will all be trivially unreachable, so |
| 1456 | // we don't have to perform any non-trivial analysis here. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1457 | // |
Chris Lattner | f40f0a3 | 2002-11-09 22:07:02 +0000 | [diff] [blame] | 1458 | void DSGraph::removeTriviallyDeadNodes() { |
Chris Lattner | 93ddd7e | 2004-01-22 16:36:28 +0000 | [diff] [blame] | 1459 | TIME_REGION(X, "removeTriviallyDeadNodes"); |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1460 | removeIdenticalCalls(FunctionCalls); |
| 1461 | removeIdenticalCalls(AuxFunctionCalls); |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | bab8c28 | 2003-09-20 21:34:07 +0000 | [diff] [blame] | 1463 | // Loop over all of the nodes in the graph, calling getNode on each field. |
| 1464 | // This will cause all nodes to update their forwarding edges, causing |
| 1465 | // forwarded nodes to be delete-able. |
| 1466 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 1467 | DSNode *N = Nodes[i]; |
| 1468 | for (unsigned l = 0, e = N->getNumLinks(); l != e; ++l) |
| 1469 | N->getLink(l*N->getPointerSize()).getNode(); |
| 1470 | } |
| 1471 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1472 | // NOTE: This code is disabled. Though it should, in theory, allow us to |
| 1473 | // remove more nodes down below, the scan of the scalar map is incredibly |
| 1474 | // expensive for certain programs (with large SCCs). In the future, if we can |
| 1475 | // make the scalar map scan more efficient, then we can reenable this. |
| 1476 | #if 0 |
| 1477 | { TIME_REGION(X, "removeTriviallyDeadNodes:scalarmap"); |
| 1478 | |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1479 | // Likewise, forward any edges from the scalar nodes. While we are at it, |
| 1480 | // clean house a bit. |
Chris Lattner | 62482e5 | 2004-01-28 09:15:42 +0000 | [diff] [blame] | 1481 | for (DSScalarMap::iterator I = ScalarMap.begin(),E = ScalarMap.end();I != E;){ |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1482 | I->second.getNode(); |
| 1483 | ++I; |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1484 | } |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1485 | } |
| 1486 | #endif |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1487 | bool isGlobalsGraph = !GlobalsGraph; |
| 1488 | |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1489 | for (unsigned i = 0; i != Nodes.size(); ++i) { |
| 1490 | DSNode *Node = Nodes[i]; |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1491 | |
| 1492 | // Do not remove *any* global nodes in the globals graph. |
| 1493 | // This is a special case because such nodes may not have I, M, R flags set. |
| 1494 | if (Node->isGlobalNode() && isGlobalsGraph) |
| 1495 | continue; |
| 1496 | |
Chris Lattner | 72d50a0 | 2003-06-28 21:58:28 +0000 | [diff] [blame] | 1497 | if (Node->isComplete() && !Node->isModified() && !Node->isRead()) { |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1498 | // This is a useless node if it has no mod/ref info (checked above), |
| 1499 | // outgoing edges (which it cannot, as it is not modified in this |
| 1500 | // context), and it has no incoming edges. If it is a global node it may |
| 1501 | // have all of these properties and still have incoming edges, due to the |
| 1502 | // scalar map, so we check those now. |
| 1503 | // |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1504 | if (Node->getNumReferrers() == Node->getGlobals().size()) { |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1505 | const std::vector<GlobalValue*> &Globals = Node->getGlobals(); |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1506 | |
Chris Lattner | 17a93e2 | 2004-01-29 03:32:15 +0000 | [diff] [blame] | 1507 | // Loop through and make sure all of the globals are referring directly |
| 1508 | // to the node... |
| 1509 | for (unsigned j = 0, e = Globals.size(); j != e; ++j) { |
| 1510 | DSNode *N = getNodeForValue(Globals[j]).getNode(); |
| 1511 | assert(N == Node && "ScalarMap doesn't match globals list!"); |
| 1512 | } |
| 1513 | |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1514 | // Make sure NumReferrers still agrees, if so, the node is truly dead. |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1515 | if (Node->getNumReferrers() == Globals.size()) { |
| 1516 | for (unsigned j = 0, e = Globals.size(); j != e; ++j) |
| 1517 | ScalarMap.erase(Globals[j]); |
Chris Lattner | 17a93e2 | 2004-01-29 03:32:15 +0000 | [diff] [blame] | 1518 | Node->makeNodeDead(); |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1519 | } |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1523 | if (Node->getNodeFlags() == 0 && Node->hasNoReferrers()) { |
Chris Lattner | 2609c07 | 2003-02-10 18:18:18 +0000 | [diff] [blame] | 1524 | // This node is dead! |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1525 | delete Node; // Free memory... |
Chris Lattner | a954b5e | 2003-02-10 18:47:23 +0000 | [diff] [blame] | 1526 | Nodes[i--] = Nodes.back(); |
| 1527 | Nodes.pop_back(); // Remove from node list... |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1528 | } |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1529 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | |
Chris Lattner | 5c7380e | 2003-01-29 21:10:20 +0000 | [diff] [blame] | 1533 | /// markReachableNodes - This method recursively traverses the specified |
| 1534 | /// DSNodes, marking any nodes which are reachable. All reachable nodes it adds |
| 1535 | /// to the set, which allows it to only traverse visited nodes once. |
| 1536 | /// |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 1537 | void DSNode::markReachableNodes(hash_set<DSNode*> &ReachableNodes) { |
Chris Lattner | 5c7380e | 2003-01-29 21:10:20 +0000 | [diff] [blame] | 1538 | if (this == 0) return; |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1539 | assert(getForwardNode() == 0 && "Cannot mark a forwarded node!"); |
Chris Lattner | 4c6cb7a | 2004-01-22 15:30:58 +0000 | [diff] [blame] | 1540 | if (ReachableNodes.insert(this).second) // Is newly reachable? |
| 1541 | for (unsigned i = 0, e = getSize(); i < e; i += DS::PointerSize) |
| 1542 | getLink(i).getNode()->markReachableNodes(ReachableNodes); |
Chris Lattner | 5c7380e | 2003-01-29 21:10:20 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 1545 | void DSCallSite::markReachableNodes(hash_set<DSNode*> &Nodes) { |
Chris Lattner | 5c7380e | 2003-01-29 21:10:20 +0000 | [diff] [blame] | 1546 | getRetVal().getNode()->markReachableNodes(Nodes); |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 1547 | if (isIndirectCall()) getCalleeNode()->markReachableNodes(Nodes); |
Chris Lattner | 5c7380e | 2003-01-29 21:10:20 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1549 | for (unsigned i = 0, e = getNumPtrArgs(); i != e; ++i) |
| 1550 | getPtrArg(i).getNode()->markReachableNodes(Nodes); |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Chris Lattner | a1220af | 2003-02-01 06:17:02 +0000 | [diff] [blame] | 1553 | // CanReachAliveNodes - Simple graph walker that recursively traverses the graph |
| 1554 | // looking for a node that is marked alive. If an alive node is found, return |
| 1555 | // true, otherwise return false. If an alive node is reachable, this node is |
| 1556 | // marked as alive... |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1557 | // |
Chris Lattner | a1220af | 2003-02-01 06:17:02 +0000 | [diff] [blame] | 1558 | static bool CanReachAliveNodes(DSNode *N, hash_set<DSNode*> &Alive, |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1559 | hash_set<DSNode*> &Visited, |
| 1560 | bool IgnoreGlobals) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1561 | if (N == 0) return false; |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1562 | assert(N->getForwardNode() == 0 && "Cannot mark a forwarded node!"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1564 | // If this is a global node, it will end up in the globals graph anyway, so we |
| 1565 | // don't need to worry about it. |
| 1566 | if (IgnoreGlobals && N->isGlobalNode()) return false; |
| 1567 | |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1568 | // If we know that this node is alive, return so! |
| 1569 | if (Alive.count(N)) return true; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1570 | |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1571 | // Otherwise, we don't think the node is alive yet, check for infinite |
| 1572 | // recursion. |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 1573 | if (Visited.count(N)) return false; // Found a cycle |
Chris Lattner | a1220af | 2003-02-01 06:17:02 +0000 | [diff] [blame] | 1574 | Visited.insert(N); // No recursion, insert into Visited... |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1575 | |
Chris Lattner | 08db719 | 2002-11-06 06:20:27 +0000 | [diff] [blame] | 1576 | for (unsigned i = 0, e = N->getSize(); i < e; i += DS::PointerSize) |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1577 | if (CanReachAliveNodes(N->getLink(i).getNode(), Alive, Visited, |
| 1578 | IgnoreGlobals)) { |
Chris Lattner | a1220af | 2003-02-01 06:17:02 +0000 | [diff] [blame] | 1579 | N->markReachableNodes(Alive); |
| 1580 | return true; |
| 1581 | } |
| 1582 | return false; |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1583 | } |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | a1220af | 2003-02-01 06:17:02 +0000 | [diff] [blame] | 1585 | // CallSiteUsesAliveArgs - Return true if the specified call site can reach any |
| 1586 | // alive nodes. |
| 1587 | // |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 1588 | static bool CallSiteUsesAliveArgs(DSCallSite &CS, hash_set<DSNode*> &Alive, |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1589 | hash_set<DSNode*> &Visited, |
| 1590 | bool IgnoreGlobals) { |
| 1591 | if (CanReachAliveNodes(CS.getRetVal().getNode(), Alive, Visited, |
| 1592 | IgnoreGlobals)) |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 1593 | return true; |
| 1594 | if (CS.isIndirectCall() && |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1595 | CanReachAliveNodes(CS.getCalleeNode(), Alive, Visited, IgnoreGlobals)) |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1596 | return true; |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1597 | for (unsigned i = 0, e = CS.getNumPtrArgs(); i != e; ++i) |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1598 | if (CanReachAliveNodes(CS.getPtrArg(i).getNode(), Alive, Visited, |
| 1599 | IgnoreGlobals)) |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1600 | return true; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
| 1603 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1604 | // removeDeadNodes - Use a more powerful reachability analysis to eliminate |
| 1605 | // subgraphs that are unreachable. This often occurs because the data |
| 1606 | // structure doesn't "escape" into it's caller, and thus should be eliminated |
| 1607 | // from the caller's graph entirely. This is only appropriate to use when |
| 1608 | // inlining graphs. |
| 1609 | // |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 1610 | void DSGraph::removeDeadNodes(unsigned Flags) { |
Chris Lattner | 9dc4185 | 2003-11-12 04:57:58 +0000 | [diff] [blame] | 1611 | DEBUG(AssertGraphOK(); if (GlobalsGraph) GlobalsGraph->AssertGraphOK()); |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1613 | // Reduce the amount of work we have to do... remove dummy nodes left over by |
| 1614 | // merging... |
Chris Lattner | a3fd88d | 2004-01-28 03:24:41 +0000 | [diff] [blame] | 1615 | removeTriviallyDeadNodes(); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | 93ddd7e | 2004-01-22 16:36:28 +0000 | [diff] [blame] | 1617 | TIME_REGION(X, "removeDeadNodes"); |
| 1618 | |
Misha Brukman | 2f2d065 | 2003-09-11 18:14:24 +0000 | [diff] [blame] | 1619 | // FIXME: Merge non-trivially identical call nodes... |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1620 | |
| 1621 | // Alive - a set that holds all nodes found to be reachable/alive. |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 1622 | hash_set<DSNode*> Alive; |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1623 | std::vector<std::pair<Value*, DSNode*> > GlobalNodes; |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1624 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1625 | // Copy and merge all information about globals to the GlobalsGraph if this is |
| 1626 | // not a final pass (where unreachable globals are removed). |
| 1627 | // |
| 1628 | // Strip all alloca bits since the current function is only for the BU pass. |
| 1629 | // Strip all incomplete bits since they are short-lived properties and they |
| 1630 | // will be correctly computed when rematerializing nodes into the functions. |
| 1631 | // |
| 1632 | ReachabilityCloner GGCloner(*GlobalsGraph, *this, DSGraph::StripAllocaBit | |
| 1633 | DSGraph::StripIncompleteBit); |
| 1634 | |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1635 | // Mark all nodes reachable by (non-global) scalar nodes as alive... |
Chris Lattner | 00948c0 | 2004-01-28 02:05:05 +0000 | [diff] [blame] | 1636 | { TIME_REGION(Y, "removeDeadNodes:scalarscan"); |
Chris Lattner | 62482e5 | 2004-01-28 09:15:42 +0000 | [diff] [blame] | 1637 | for (DSScalarMap::iterator I = ScalarMap.begin(), E = ScalarMap.end(); I !=E;) |
Chris Lattner | 5f07a8b | 2003-02-14 06:28:00 +0000 | [diff] [blame] | 1638 | if (isa<GlobalValue>(I->first)) { // Keep track of global nodes |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1639 | assert(I->second.getNode() && "Null global node?"); |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1640 | assert(I->second.getNode()->isGlobalNode() && "Should be a global node!"); |
Chris Lattner | 5f07a8b | 2003-02-14 06:28:00 +0000 | [diff] [blame] | 1641 | GlobalNodes.push_back(std::make_pair(I->first, I->second.getNode())); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1642 | |
| 1643 | // Make sure that all globals are cloned over as roots. |
Chris Lattner | 00948c0 | 2004-01-28 02:05:05 +0000 | [diff] [blame] | 1644 | if (!(Flags & DSGraph::RemoveUnreachableGlobals)) { |
| 1645 | DSGraph::ScalarMapTy::iterator SMI = |
| 1646 | GlobalsGraph->getScalarMap().find(I->first); |
| 1647 | if (SMI != GlobalsGraph->getScalarMap().end()) |
| 1648 | GGCloner.merge(SMI->second, I->second); |
| 1649 | else |
| 1650 | GGCloner.getClonedNH(I->second); |
| 1651 | } |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1652 | ++I; |
Chris Lattner | 5f07a8b | 2003-02-14 06:28:00 +0000 | [diff] [blame] | 1653 | } else { |
Chris Lattner | a88a55c | 2004-01-28 02:41:32 +0000 | [diff] [blame] | 1654 | DSNode *N = I->second.getNode(); |
| 1655 | #if 0 |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1656 | // Check to see if this is a worthless node generated for non-pointer |
| 1657 | // values, such as integers. Consider an addition of long types: A+B. |
| 1658 | // Assuming we can track all uses of the value in this context, and it is |
| 1659 | // NOT used as a pointer, we can delete the node. We will be able to |
| 1660 | // detect this situation if the node pointed to ONLY has Unknown bit set |
| 1661 | // in the node. In this case, the node is not incomplete, does not point |
| 1662 | // to any other nodes (no mod/ref bits set), and is therefore |
| 1663 | // uninteresting for data structure analysis. If we run across one of |
| 1664 | // these, prune the scalar pointing to it. |
| 1665 | // |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1666 | if (N->getNodeFlags() == DSNode::UnknownNode && !isa<Argument>(I->first)) |
| 1667 | ScalarMap.erase(I++); |
| 1668 | else { |
Chris Lattner | a88a55c | 2004-01-28 02:41:32 +0000 | [diff] [blame] | 1669 | #endif |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1670 | N->markReachableNodes(Alive); |
| 1671 | ++I; |
Chris Lattner | a88a55c | 2004-01-28 02:41:32 +0000 | [diff] [blame] | 1672 | //} |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1673 | } |
Chris Lattner | 00948c0 | 2004-01-28 02:05:05 +0000 | [diff] [blame] | 1674 | } |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1676 | // The return values are alive as well. |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 1677 | for (ReturnNodesTy::iterator I = ReturnNodes.begin(), E = ReturnNodes.end(); |
| 1678 | I != E; ++I) |
| 1679 | I->second.getNode()->markReachableNodes(Alive); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1680 | |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1681 | // Mark any nodes reachable by primary calls as alive... |
| 1682 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) |
| 1683 | FunctionCalls[i].markReachableNodes(Alive); |
| 1684 | |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1685 | |
| 1686 | // Now find globals and aux call nodes that are already live or reach a live |
| 1687 | // value (which makes them live in turn), and continue till no more are found. |
| 1688 | // |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1689 | bool Iterate; |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 1690 | hash_set<DSNode*> Visited; |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1691 | std::vector<unsigned char> AuxFCallsAlive(AuxFunctionCalls.size()); |
| 1692 | do { |
| 1693 | Visited.clear(); |
Chris Lattner | 7079386 | 2003-07-02 23:57:05 +0000 | [diff] [blame] | 1694 | // If any global node points to a non-global that is "alive", the global is |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1695 | // "alive" as well... Remove it from the GlobalNodes list so we only have |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1696 | // unreachable globals in the list. |
| 1697 | // |
| 1698 | Iterate = false; |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1699 | if (!(Flags & DSGraph::RemoveUnreachableGlobals)) |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1700 | for (unsigned i = 0; i != GlobalNodes.size(); ++i) |
| 1701 | if (CanReachAliveNodes(GlobalNodes[i].second, Alive, Visited, |
| 1702 | Flags & DSGraph::RemoveUnreachableGlobals)) { |
| 1703 | std::swap(GlobalNodes[i--], GlobalNodes.back()); // Move to end to... |
| 1704 | GlobalNodes.pop_back(); // erase efficiently |
| 1705 | Iterate = true; |
| 1706 | } |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1707 | |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1708 | // Mark only unresolvable call nodes for moving to the GlobalsGraph since |
| 1709 | // call nodes that get resolved will be difficult to remove from that graph. |
| 1710 | // The final unresolved call nodes must be handled specially at the end of |
| 1711 | // the BU pass (i.e., in main or other roots of the call graph). |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1712 | for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i) |
| 1713 | if (!AuxFCallsAlive[i] && |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1714 | (AuxFunctionCalls[i].isIndirectCall() |
| 1715 | || CallSiteUsesAliveArgs(AuxFunctionCalls[i], Alive, Visited, |
| 1716 | Flags & DSGraph::RemoveUnreachableGlobals))) { |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1717 | AuxFunctionCalls[i].markReachableNodes(Alive); |
| 1718 | AuxFCallsAlive[i] = true; |
| 1719 | Iterate = true; |
| 1720 | } |
| 1721 | } while (Iterate); |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1722 | |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1723 | // Move dead aux function calls to the end of the list |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1724 | unsigned CurIdx = 0; |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1725 | for (unsigned i = 0, e = AuxFunctionCalls.size(); i != e; ++i) |
| 1726 | if (AuxFCallsAlive[i]) |
| 1727 | AuxFunctionCalls[CurIdx++].swap(AuxFunctionCalls[i]); |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1728 | |
| 1729 | // Copy and merge all global nodes and dead aux call nodes into the |
| 1730 | // GlobalsGraph, and all nodes reachable from those nodes |
| 1731 | // |
| 1732 | if (!(Flags & DSGraph::RemoveUnreachableGlobals)) { |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1733 | // Copy the unreachable call nodes to the globals graph, updating their |
| 1734 | // target pointers using the GGCloner |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1735 | for (unsigned i = CurIdx, e = AuxFunctionCalls.size(); i != e; ++i) |
| 1736 | GlobalsGraph->AuxFunctionCalls.push_back(DSCallSite(AuxFunctionCalls[i], |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1737 | GGCloner)); |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1738 | } |
| 1739 | // Crop all the useless ones out... |
Chris Lattner | aa8146f | 2002-11-10 06:59:55 +0000 | [diff] [blame] | 1740 | AuxFunctionCalls.erase(AuxFunctionCalls.begin()+CurIdx, |
| 1741 | AuxFunctionCalls.end()); |
| 1742 | |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1743 | // We are finally done with the GGCloner so we can clear it and then get rid |
| 1744 | // of unused nodes in the GlobalsGraph produced by merging. |
| 1745 | if (GGCloner.clonedNode()) { |
| 1746 | GGCloner.destroy(); |
| 1747 | GlobalsGraph->removeTriviallyDeadNodes(); |
| 1748 | } |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1749 | |
Vikram S. Adve | 40c600e | 2003-07-22 12:08:58 +0000 | [diff] [blame] | 1750 | // At this point, any nodes which are visited, but not alive, are nodes |
| 1751 | // which can be removed. Loop over all nodes, eliminating completely |
| 1752 | // unreachable nodes. |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1753 | // |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1754 | std::vector<DSNode*> DeadNodes; |
| 1755 | DeadNodes.reserve(Nodes.size()); |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1756 | for (unsigned i = 0; i != Nodes.size(); ++i) |
| 1757 | if (!Alive.count(Nodes[i])) { |
| 1758 | DSNode *N = Nodes[i]; |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1759 | Nodes[i--] = Nodes.back(); // move node to end of vector |
| 1760 | Nodes.pop_back(); // Erase node from alive list. |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1761 | DeadNodes.push_back(N); |
| 1762 | N->dropAllReferences(); |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1763 | } else { |
| 1764 | assert(Nodes[i]->getForwardNode() == 0 && "Alive forwarded node?"); |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1765 | } |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1766 | |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1767 | // Remove all unreachable globals from the ScalarMap. |
| 1768 | // If flag RemoveUnreachableGlobals is set, GlobalNodes has only dead nodes. |
| 1769 | // In either case, the dead nodes will not be in the set Alive. |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1770 | for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i) |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1771 | if (!Alive.count(GlobalNodes[i].second)) |
| 1772 | ScalarMap.erase(GlobalNodes[i].first); |
Chris Lattner | 0b14487 | 2004-01-27 22:03:40 +0000 | [diff] [blame] | 1773 | else |
| 1774 | assert((Flags & DSGraph::RemoveUnreachableGlobals) && "non-dead global"); |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1775 | |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1776 | // Delete all dead nodes now since their referrer counts are zero. |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1777 | for (unsigned i = 0, e = DeadNodes.size(); i != e; ++i) |
| 1778 | delete DeadNodes[i]; |
| 1779 | |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1780 | DEBUG(AssertGraphOK(); GlobalsGraph->AssertGraphOK()); |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1783 | void DSGraph::AssertGraphOK() const { |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 1784 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 1785 | Nodes[i]->assertOK(); |
Chris Lattner | 85cfe01 | 2003-07-03 02:03:53 +0000 | [diff] [blame] | 1786 | |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 1787 | for (ScalarMapTy::const_iterator I = ScalarMap.begin(), |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1788 | E = ScalarMap.end(); I != E; ++I) { |
| 1789 | assert(I->second.getNode() && "Null node in scalarmap!"); |
| 1790 | AssertNodeInGraph(I->second.getNode()); |
| 1791 | if (GlobalValue *GV = dyn_cast<GlobalValue>(I->first)) { |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 1792 | assert(I->second.getNode()->isGlobalNode() && |
Chris Lattner | 0ac7d5c | 2003-02-03 19:12:15 +0000 | [diff] [blame] | 1793 | "Global points to node, but node isn't global?"); |
| 1794 | AssertNodeContainsGlobal(I->second.getNode(), GV); |
| 1795 | } |
| 1796 | } |
| 1797 | AssertCallNodesInGraph(); |
| 1798 | AssertAuxCallNodesInGraph(); |
| 1799 | } |
Vikram S. Adve | 78bbec7 | 2003-07-16 21:36:31 +0000 | [diff] [blame] | 1800 | |
Chris Lattner | 400433d | 2003-11-11 05:08:59 +0000 | [diff] [blame] | 1801 | /// computeNodeMapping - Given roots in two different DSGraphs, traverse the |
| 1802 | /// nodes reachable from the two graphs, computing the mapping of nodes from |
| 1803 | /// the first to the second graph. |
| 1804 | /// |
| 1805 | void DSGraph::computeNodeMapping(const DSNodeHandle &NH1, |
Chris Lattner | afc1dba | 2003-11-12 17:58:22 +0000 | [diff] [blame] | 1806 | const DSNodeHandle &NH2, NodeMapTy &NodeMap, |
| 1807 | bool StrictChecking) { |
Chris Lattner | 400433d | 2003-11-11 05:08:59 +0000 | [diff] [blame] | 1808 | DSNode *N1 = NH1.getNode(), *N2 = NH2.getNode(); |
| 1809 | if (N1 == 0 || N2 == 0) return; |
| 1810 | |
| 1811 | DSNodeHandle &Entry = NodeMap[N1]; |
| 1812 | if (Entry.getNode()) { |
| 1813 | // Termination of recursion! |
Chris Lattner | afc1dba | 2003-11-12 17:58:22 +0000 | [diff] [blame] | 1814 | assert(!StrictChecking || |
| 1815 | (Entry.getNode() == N2 && |
| 1816 | Entry.getOffset() == (NH2.getOffset()-NH1.getOffset())) && |
Chris Lattner | 400433d | 2003-11-11 05:08:59 +0000 | [diff] [blame] | 1817 | "Inconsistent mapping detected!"); |
| 1818 | return; |
| 1819 | } |
| 1820 | |
| 1821 | Entry.setNode(N2); |
Chris Lattner | 413406c | 2003-11-11 20:12:32 +0000 | [diff] [blame] | 1822 | Entry.setOffset(NH2.getOffset()-NH1.getOffset()); |
Chris Lattner | 400433d | 2003-11-11 05:08:59 +0000 | [diff] [blame] | 1823 | |
| 1824 | // Loop over all of the fields that N1 and N2 have in common, recursively |
| 1825 | // mapping the edges together now. |
| 1826 | int N2Idx = NH2.getOffset()-NH1.getOffset(); |
| 1827 | unsigned N2Size = N2->getSize(); |
| 1828 | for (unsigned i = 0, e = N1->getSize(); i < e; i += DS::PointerSize) |
| 1829 | if (unsigned(N2Idx)+i < N2Size) |
| 1830 | computeNodeMapping(N1->getLink(i), N2->getLink(N2Idx+i), NodeMap); |
| 1831 | } |