Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1 | //===- DataStructure.cpp - Implement the core data structure analysis -----===// |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 3 | // This file implements the core data structure functionality. |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 7 | #include "llvm/Analysis/DSGraph.h" |
| 8 | #include "llvm/Function.h" |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 9 | #include "llvm/iOther.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 10 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 11 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 12 | #include "Support/STLExtras.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 13 | #include "Support/Statistic.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 15 | #include <set> |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 16 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 17 | using std::vector; |
| 18 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 19 | // TODO: FIXME |
| 20 | namespace DataStructureAnalysis { |
| 21 | // isPointerType - Return true if this first class type is big enough to hold |
| 22 | // a pointer. |
| 23 | // |
| 24 | bool isPointerType(const Type *Ty); |
| 25 | extern TargetData TD; |
| 26 | } |
| 27 | using namespace DataStructureAnalysis; |
| 28 | |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 30 | // DSNode Implementation |
| 31 | //===----------------------------------------------------------------------===// |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 32 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 33 | DSNode::DSNode(enum NodeTy NT, const Type *T) : NodeType(NT) { |
| 34 | // If this node is big enough to have pointer fields, add space for them now. |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 35 | if (T != Type::VoidTy && !isa<FunctionType>(T)) { // Avoid TargetData assert's |
| 36 | MergeMap.resize(TD.getTypeSize(T)); |
| 37 | |
| 38 | // Assign unique values to all of the elements of MergeMap |
| 39 | if (MergeMap.size() < 128) { |
| 40 | // Handle the common case of reasonable size structures... |
| 41 | for (unsigned i = 0, e = MergeMap.size(); i != e; ++i) |
| 42 | MergeMap[i] = -1-i; // Assign -1, -2, -3, ... |
| 43 | } else { |
| 44 | // It's possible that we have something really big here. In this case, |
| 45 | // divide the object into chunks until it will fit into 128 elements. |
| 46 | unsigned Multiple = MergeMap.size()/128; |
| 47 | |
| 48 | // It's probably an array, and probably some power of two in size. |
| 49 | // Because of this, find the biggest power of two that is bigger than |
| 50 | // multiple to use as our real Multiple. |
| 51 | unsigned RealMultiple = 2; |
Chris Lattner | 62d928e | 2002-10-03 21:06:38 +0000 | [diff] [blame] | 52 | while (RealMultiple <= Multiple) RealMultiple <<= 1; |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 53 | |
| 54 | unsigned RealBound = MergeMap.size()/RealMultiple; |
| 55 | assert(RealBound <= 128 && "Math didn't work out right"); |
| 56 | |
| 57 | // Now go through and assign indexes that are between -1 and -128 |
| 58 | // inclusive |
| 59 | // |
| 60 | for (unsigned i = 0, e = MergeMap.size(); i != e; ++i) |
| 61 | MergeMap[i] = -1-(i % RealBound); // Assign -1, -2, -3... |
| 62 | } |
| 63 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 64 | |
Chris Lattner | a3f8586 | 2002-10-18 18:22:46 +0000 | [diff] [blame] | 65 | TypeEntries.push_back(TypeRec(T, 0)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 68 | // DSNode copy constructor... do not copy over the referrers list! |
| 69 | DSNode::DSNode(const DSNode &N) |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 70 | : Links(N.Links), MergeMap(N.MergeMap), |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 71 | TypeEntries(N.TypeEntries), Globals(N.Globals), NodeType(N.NodeType) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 74 | void DSNode::removeReferrer(DSNodeHandle *H) { |
| 75 | // Search backwards, because we depopulate the list from the back for |
| 76 | // efficiency (because it's a vector). |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 77 | vector<DSNodeHandle*>::reverse_iterator I = |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 78 | std::find(Referrers.rbegin(), Referrers.rend(), H); |
| 79 | assert(I != Referrers.rend() && "Referrer not pointing to node!"); |
| 80 | Referrers.erase(I.base()-1); |
| 81 | } |
| 82 | |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 83 | // addGlobal - Add an entry for a global value to the Globals list. This also |
| 84 | // marks the node with the 'G' flag if it does not already have it. |
| 85 | // |
| 86 | void DSNode::addGlobal(GlobalValue *GV) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 87 | // Keep the list sorted. |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 88 | vector<GlobalValue*>::iterator I = |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 89 | std::lower_bound(Globals.begin(), Globals.end(), GV); |
| 90 | |
| 91 | if (I == Globals.end() || *I != GV) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 92 | //assert(GV->getType()->getElementType() == Ty); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 93 | Globals.insert(I, GV); |
| 94 | NodeType |= GlobalNode; |
| 95 | } |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 99 | /// setLink - Set the link at the specified offset to the specified |
| 100 | /// NodeHandle, replacing what was there. It is uncommon to use this method, |
| 101 | /// instead one of the higher level methods should be used, below. |
| 102 | /// |
| 103 | void DSNode::setLink(unsigned i, const DSNodeHandle &NH) { |
| 104 | // Create a new entry in the Links vector to hold a new element for offset. |
| 105 | if (!hasLink(i)) { |
| 106 | signed char NewIdx = Links.size(); |
| 107 | // Check to see if we allocate more than 128 distinct links for this node. |
| 108 | // If so, just merge with the last one. This really shouldn't ever happen, |
| 109 | // but it should work regardless of whether it does or not. |
| 110 | // |
| 111 | if (NewIdx >= 0) { |
| 112 | Links.push_back(NH); // Allocate space: common case |
| 113 | } else { // Wrap around? Too many links? |
| 114 | NewIdx--; // Merge with whatever happened last |
| 115 | assert(NewIdx > 0 && "Should wrap back around"); |
| 116 | std::cerr << "\n*** DSNode found that requires more than 128 " |
| 117 | << "active links at once!\n\n"; |
| 118 | } |
| 119 | |
| 120 | signed char OldIdx = MergeMap[i]; |
| 121 | assert (OldIdx < 0 && "Shouldn't contain link!"); |
| 122 | |
| 123 | // Make sure that anything aliasing this field gets updated to point to the |
| 124 | // new link field. |
| 125 | rewriteMergeMap(OldIdx, NewIdx); |
| 126 | assert(MergeMap[i] == NewIdx && "Field not replaced!"); |
| 127 | } else { |
| 128 | Links[MergeMap[i]] = NH; |
| 129 | } |
| 130 | } |
| 131 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 132 | // addEdgeTo - Add an edge from the current node to the specified node. This |
| 133 | // can cause merging of nodes in the graph. |
| 134 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 135 | void DSNode::addEdgeTo(unsigned Offset, const DSNodeHandle &NH) { |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 136 | assert(Offset < getSize() && "Offset out of range!"); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 137 | if (NH.getNode() == 0) return; // Nothing to do |
| 138 | |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 139 | if (DSNodeHandle *ExistingNH = getLink(Offset)) { |
| 140 | // Merge the two nodes... |
| 141 | ExistingNH->mergeWith(NH); |
| 142 | } else { // No merging to perform... |
| 143 | setLink(Offset, NH); // Just force a link in there... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 147 | /// mergeMappedValues - This is the higher level form of rewriteMergeMap. It is |
| 148 | /// fully capable of merging links together if neccesary as well as simply |
| 149 | /// rewriting the map entries. |
| 150 | /// |
| 151 | void DSNode::mergeMappedValues(signed char V1, signed char V2) { |
| 152 | assert(V1 != V2 && "Cannot merge two identical mapped values!"); |
| 153 | |
| 154 | if (V1 < 0) { // If there is no outgoing link from V1, merge it with V2 |
| 155 | if (V2 < 0 && V1 > V2) |
| 156 | // If both are not linked, merge to the field closer to 0 |
| 157 | rewriteMergeMap(V2, V1); |
| 158 | else |
| 159 | rewriteMergeMap(V1, V2); |
| 160 | } else if (V2 < 0) { // Is V2 < 0 && V1 >= 0? |
| 161 | rewriteMergeMap(V2, V1); // Merge into the one with the link... |
| 162 | } else { // Otherwise, links exist at both locations |
| 163 | // Merge Links[V1] with Links[V2] so they point to the same place now... |
| 164 | Links[V1].mergeWith(Links[V2]); |
| 165 | |
| 166 | // Merge the V2 link into V1 so that we reduce the overall value of the |
| 167 | // links are reduced... |
| 168 | // |
| 169 | if (V2 < V1) std::swap(V1, V2); // Ensure V1 < V2 |
| 170 | rewriteMergeMap(V2, V1); // After this, V2 is "dead" |
| 171 | |
| 172 | // Change the user of the last link to use V2 instead |
| 173 | if ((unsigned)V2 != Links.size()-1) { |
| 174 | rewriteMergeMap(Links.size()-1, V2); // Point to V2 instead of last el... |
| 175 | // Make sure V2 points the right DSNode |
| 176 | Links[V2] = Links.back(); |
| 177 | } |
| 178 | |
| 179 | // Reduce the number of distinct outgoing links... |
| 180 | Links.pop_back(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 185 | // MergeSortedVectors - Efficiently merge a vector into another vector where |
| 186 | // duplicates are not allowed and both are sorted. This assumes that 'T's are |
| 187 | // efficiently copyable and have sane comparison semantics. |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 188 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 189 | template<typename T> |
| 190 | void MergeSortedVectors(vector<T> &Dest, const vector<T> &Src) { |
| 191 | // By far, the most common cases will be the simple ones. In these cases, |
| 192 | // avoid having to allocate a temporary vector... |
| 193 | // |
| 194 | if (Src.empty()) { // Nothing to merge in... |
| 195 | return; |
| 196 | } else if (Dest.empty()) { // Just copy the result in... |
| 197 | Dest = Src; |
| 198 | } else if (Src.size() == 1) { // Insert a single element... |
| 199 | const T &V = Src[0]; |
| 200 | typename vector<T>::iterator I = |
| 201 | std::lower_bound(Dest.begin(), Dest.end(), V); |
| 202 | if (I == Dest.end() || *I != Src[0]) // If not already contained... |
| 203 | Dest.insert(I, Src[0]); |
| 204 | } else if (Dest.size() == 1) { |
| 205 | T Tmp = Dest[0]; // Save value in temporary... |
| 206 | Dest = Src; // Copy over list... |
| 207 | typename vector<T>::iterator I = |
| 208 | std::lower_bound(Dest.begin(), Dest.end(),Tmp); |
| 209 | if (I == Dest.end() || *I != Src[0]) // If not already contained... |
| 210 | Dest.insert(I, Src[0]); |
| 211 | |
| 212 | } else { |
| 213 | // Make a copy to the side of Dest... |
| 214 | vector<T> Old(Dest); |
| 215 | |
| 216 | // Make space for all of the type entries now... |
| 217 | Dest.resize(Dest.size()+Src.size()); |
| 218 | |
| 219 | // Merge the two sorted ranges together... into Dest. |
| 220 | std::merge(Old.begin(), Old.end(), Src.begin(), Src.end(), Dest.begin()); |
| 221 | |
| 222 | // Now erase any duplicate entries that may have accumulated into the |
| 223 | // vectors (because they were in both of the input sets) |
| 224 | Dest.erase(std::unique(Dest.begin(), Dest.end()), Dest.end()); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | // mergeWith - Merge this node and the specified node, moving all links to and |
| 230 | // from the argument node into the current node, deleting the node argument. |
| 231 | // Offset indicates what offset the specified node is to be merged into the |
| 232 | // current node. |
| 233 | // |
| 234 | // The specified node may be a null pointer (in which case, nothing happens). |
| 235 | // |
| 236 | void DSNode::mergeWith(const DSNodeHandle &NH, unsigned Offset) { |
| 237 | DSNode *N = NH.getNode(); |
| 238 | if (N == 0 || (N == this && NH.getOffset() == Offset)) |
| 239 | return; // Noop |
| 240 | |
| 241 | assert(NH.getNode() != this && |
| 242 | "Cannot merge two portions of the same node yet!"); |
| 243 | |
| 244 | // If both nodes are not at offset 0, make sure that we are merging the node |
| 245 | // at an later offset into the node with the zero offset. |
| 246 | // |
| 247 | if (Offset > NH.getOffset()) { |
| 248 | N->mergeWith(DSNodeHandle(this, Offset), NH.getOffset()); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | #if 0 |
| 253 | std::cerr << "\n\nMerging:\n"; |
| 254 | N->print(std::cerr, 0); |
| 255 | std::cerr << " and:\n"; |
| 256 | print(std::cerr, 0); |
| 257 | #endif |
| 258 | |
| 259 | // Now we know that Offset <= NH.Offset, so convert it so our "Offset" (with |
| 260 | // respect to NH.Offset) is now zero. |
| 261 | // |
| 262 | unsigned NOffset = NH.getOffset()-Offset; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 264 | unsigned NSize = N->getSize(); |
| 265 | assert(NSize+NOffset <= getSize() && |
| 266 | "Don't know how to merge extend a merged nodes size yet!"); |
| 267 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 268 | // Remove all edges pointing at N, causing them to point to 'this' instead. |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 269 | // Make sure to adjust their offset, not just the node pointer. |
| 270 | // |
| 271 | while (!N->Referrers.empty()) { |
| 272 | DSNodeHandle &Ref = *N->Referrers.back(); |
| 273 | Ref = DSNodeHandle(this, NOffset+Ref.getOffset()); |
| 274 | } |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 276 | // We must merge fields in this node due to nodes merged in the source node. |
| 277 | // In order to handle this we build a map that converts from the source node's |
| 278 | // MergeMap values to our MergeMap values. This map is indexed by the |
| 279 | // expression: MergeMap[SMM+SourceNodeSize] so we need to allocate at least |
| 280 | // 2*SourceNodeSize elements of space for the mapping. We can do this because |
| 281 | // we know that there are at most SourceNodeSize outgoing links in the node |
| 282 | // (thus that many positive values) and at most SourceNodeSize distinct fields |
| 283 | // (thus that many negative values). |
| 284 | // |
| 285 | std::vector<signed char> MergeMapMap(NSize*2, 127); |
| 286 | |
| 287 | // Loop through the structures, merging them together... |
| 288 | for (unsigned i = 0, e = NSize; i != e; ++i) { |
| 289 | // Get what this byte of N maps to... |
| 290 | signed char NElement = N->MergeMap[i]; |
| 291 | |
| 292 | // Get what we map this byte to... |
| 293 | signed char Element = MergeMap[i+NOffset]; |
| 294 | // We use 127 as a sentinal and don't check for it's existence yet... |
| 295 | assert(Element != 127 && "MergeMapMap doesn't permit 127 values yet!"); |
| 296 | |
| 297 | signed char CurMappedVal = MergeMapMap[NElement+NSize]; |
| 298 | if (CurMappedVal == 127) { // Haven't seen this NElement yet? |
| 299 | MergeMapMap[NElement+NSize] = Element; // Map the two together... |
| 300 | } else if (CurMappedVal != Element) { |
| 301 | // If we are mapping two different fields together this means that we need |
| 302 | // to merge fields in the current node due to merging in the source node. |
| 303 | // |
| 304 | mergeMappedValues(CurMappedVal, Element); |
| 305 | MergeMapMap[NElement+NSize] = MergeMap[i+NOffset]; |
| 306 | } |
| 307 | } |
| 308 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 309 | // Make all of the outgoing links of N now be outgoing links of this. This |
| 310 | // can cause recursive merging! |
| 311 | // |
Chris Lattner | 7b7200c | 2002-10-02 04:57:39 +0000 | [diff] [blame] | 312 | for (unsigned i = 0, e = NSize; i != e; ++i) |
| 313 | if (DSNodeHandle *Link = N->getLink(i)) { |
| 314 | addEdgeTo(i+NOffset, *Link); |
| 315 | N->MergeMap[i] = -1; // Kill outgoing edge |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Now that there are no outgoing edges, all of the Links are dead. |
| 319 | N->Links.clear(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 320 | |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 321 | // Merge the node types |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 322 | NodeType |= N->NodeType; |
| 323 | N->NodeType = 0; // N is now a dead node. |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 324 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 325 | // If this merging into node has more than just void nodes in it, merge! |
| 326 | assert(!N->TypeEntries.empty() && "TypeEntries is empty for a node?"); |
Chris Lattner | a3f8586 | 2002-10-18 18:22:46 +0000 | [diff] [blame] | 327 | if (N->TypeEntries.size() != 1 || N->TypeEntries[0].Ty != Type::VoidTy) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 328 | // If the current node just has a Void entry in it, remove it. |
Chris Lattner | a3f8586 | 2002-10-18 18:22:46 +0000 | [diff] [blame] | 329 | if (TypeEntries.size() == 1 && TypeEntries[0].Ty == Type::VoidTy) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 330 | TypeEntries.clear(); |
| 331 | |
| 332 | // Adjust all of the type entries we are merging in by the offset... and add |
| 333 | // them to the TypeEntries list. |
| 334 | // |
| 335 | if (NOffset != 0) { // This case is common enough to optimize for |
| 336 | // Offset all of the TypeEntries in N with their new offset |
| 337 | for (unsigned i = 0, e = N->TypeEntries.size(); i != e; ++i) |
Chris Lattner | a3f8586 | 2002-10-18 18:22:46 +0000 | [diff] [blame] | 338 | N->TypeEntries[i].Offset += NOffset; |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | MergeSortedVectors(TypeEntries, N->TypeEntries); |
| 342 | |
| 343 | N->TypeEntries.clear(); |
| 344 | } |
| 345 | |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 346 | // Merge the globals list... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 347 | if (!N->Globals.empty()) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 348 | MergeSortedVectors(Globals, N->Globals); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 349 | |
| 350 | // Delete the globals from the old node... |
| 351 | N->Globals.clear(); |
| 352 | } |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Chris Lattner | 9de906c | 2002-10-20 22:11:44 +0000 | [diff] [blame] | 355 | //===----------------------------------------------------------------------===// |
| 356 | // DSCallSite Implementation |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 359 | // 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] | 360 | Function &DSCallSite::getCaller() const { |
| 361 | return *callInst->getParent()->getParent(); |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 362 | } |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 9de906c | 2002-10-20 22:11:44 +0000 | [diff] [blame] | 364 | template <typename CopyFunctor> |
| 365 | DSCallSite::DSCallSite(const DSCallSite &FromCall, CopyFunctor nodeCopier) |
| 366 | : callInst(&FromCall.getCallInst()) { |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 367 | |
| 368 | reserve(FromCall.size()); |
| 369 | for (unsigned j = 0, ej = FromCall.size(); j != ej; ++j) |
Chris Lattner | 9de906c | 2002-10-20 22:11:44 +0000 | [diff] [blame] | 370 | push_back(&nodeCopier == 0 ? DSNodeHandle(FromCall[j]) |
| 371 | : nodeCopier(&FromCall[j])); |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 375 | //===----------------------------------------------------------------------===// |
| 376 | // DSGraph Implementation |
| 377 | //===----------------------------------------------------------------------===// |
| 378 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 379 | DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) { |
| 380 | std::map<const DSNode*, DSNode*> NodeMap; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 381 | RetNode = cloneInto(G, ValueMap, NodeMap); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 384 | DSGraph::~DSGraph() { |
| 385 | FunctionCalls.clear(); |
| 386 | ValueMap.clear(); |
| 387 | RetNode = 0; |
| 388 | |
| 389 | #ifndef NDEBUG |
| 390 | // Drop all intra-node references, so that assertions don't fail... |
| 391 | std::for_each(Nodes.begin(), Nodes.end(), |
| 392 | std::mem_fun(&DSNode::dropAllReferences)); |
| 393 | #endif |
| 394 | |
| 395 | // Delete all of the nodes themselves... |
| 396 | std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>); |
| 397 | } |
| 398 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 399 | // dump - Allow inspection of graph in a debugger. |
| 400 | void DSGraph::dump() const { print(std::cerr); } |
| 401 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 402 | |
Chris Lattner | e80fe61 | 2002-10-20 20:39:31 +0000 | [diff] [blame] | 403 | static DSNodeHandle copyHelper(const DSNodeHandle* fromNode, |
| 404 | std::map<const DSNode*, DSNode*> *NodeMap) { |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 405 | return DSNodeHandle((*NodeMap)[fromNode->getNode()], fromNode->getOffset()); |
| 406 | } |
| 407 | |
| 408 | // Helper function used to clone a function list. |
| 409 | // |
| 410 | static void CopyFunctionCallsList(const vector<DSCallSite>& fromCalls, |
| 411 | vector<DSCallSite> &toCalls, |
| 412 | std::map<const DSNode*, DSNode*> &NodeMap) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 413 | unsigned FC = toCalls.size(); // FirstCall |
| 414 | toCalls.reserve(FC+fromCalls.size()); |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 415 | for (unsigned i = 0, ei = fromCalls.size(); i != ei; ++i) |
| 416 | toCalls.push_back(DSCallSite(fromCalls[i], |
| 417 | std::bind2nd(std::ptr_fun(©Helper), &NodeMap))); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 420 | /// remapLinks - Change all of the Links in the current node according to the |
| 421 | /// specified mapping. |
| 422 | void DSNode::remapLinks(std::map<const DSNode*, DSNode*> &OldNodeMap) { |
| 423 | for (unsigned i = 0, e = Links.size(); i != e; ++i) |
| 424 | Links[i].setNode(OldNodeMap[Links[i].getNode()]); |
| 425 | } |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 426 | |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 428 | // cloneInto - Clone the specified DSGraph into the current graph, returning the |
| 429 | // Return node of the graph. The translated ValueMap for the old function is |
| 430 | // filled into the OldValMap member. If StripLocals is set to true, Scalar and |
| 431 | // Alloca markers are removed from the graph, as the graph is being cloned into |
| 432 | // a calling function's graph. |
| 433 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 434 | DSNodeHandle DSGraph::cloneInto(const DSGraph &G, |
| 435 | std::map<Value*, DSNodeHandle> &OldValMap, |
| 436 | std::map<const DSNode*, DSNode*> &OldNodeMap, |
| 437 | bool StripScalars, bool StripAllocas, |
| 438 | bool CopyCallers, bool CopyOrigCalls) { |
| 439 | assert(OldNodeMap.empty() && "Returned OldNodeMap should be empty!"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 440 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 441 | unsigned FN = Nodes.size(); // First new node... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 442 | |
| 443 | // Duplicate all of the nodes, populating the node map... |
| 444 | Nodes.reserve(FN+G.Nodes.size()); |
| 445 | for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 446 | DSNode *Old = G.Nodes[i]; |
| 447 | DSNode *New = new DSNode(*Old); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 448 | Nodes.push_back(New); |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 449 | OldNodeMap[Old] = New; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 452 | // 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] | 453 | for (unsigned i = FN, e = Nodes.size(); i != e; ++i) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 454 | Nodes[i]->remapLinks(OldNodeMap); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 455 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 456 | // Remove local markers as specified |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 457 | unsigned char StripBits = (StripScalars ? DSNode::ScalarNode : 0) | |
| 458 | (StripAllocas ? DSNode::AllocaNode : 0); |
| 459 | if (StripBits) |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 460 | for (unsigned i = FN, e = Nodes.size(); i != e; ++i) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 461 | Nodes[i]->NodeType &= ~StripBits; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 462 | |
Chris Lattner | cf15db3 | 2002-10-17 20:09:52 +0000 | [diff] [blame] | 463 | // Copy the value map... and merge all of the global nodes... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 464 | for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ValueMap.begin(), |
Chris Lattner | cf15db3 | 2002-10-17 20:09:52 +0000 | [diff] [blame] | 465 | E = G.ValueMap.end(); I != E; ++I) { |
| 466 | DSNodeHandle &H = OldValMap[I->first]; |
| 467 | H = DSNodeHandle(OldNodeMap[I->second.getNode()], I->second.getOffset()); |
| 468 | |
| 469 | if (isa<GlobalValue>(I->first)) { // Is this a global? |
| 470 | std::map<Value*, DSNodeHandle>::iterator GVI = ValueMap.find(I->first); |
| 471 | if (GVI != ValueMap.end()) { // Is the global value in this fun already? |
| 472 | GVI->second.mergeWith(H); |
| 473 | } else { |
| 474 | ValueMap[I->first] = H; // Add global pointer to this graph |
| 475 | } |
| 476 | } |
| 477 | } |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 478 | // Copy the function calls list... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 479 | CopyFunctionCallsList(G.FunctionCalls, FunctionCalls, OldNodeMap); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 480 | |
Chris Lattner | cf15db3 | 2002-10-17 20:09:52 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 482 | // Return the returned node pointer... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 483 | return DSNodeHandle(OldNodeMap[G.RetNode.getNode()], G.RetNode.getOffset()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 486 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 487 | // cloneGlobalInto - Clone the given global node and all its target links |
| 488 | // (and all their llinks, recursively). |
| 489 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 490 | DSNode *DSGraph::cloneGlobalInto(const DSNode *GNode) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 491 | if (GNode == 0 || GNode->getGlobals().size() == 0) return 0; |
| 492 | |
| 493 | // If a clone has already been created for GNode, return it. |
| 494 | DSNodeHandle& ValMapEntry = ValueMap[GNode->getGlobals()[0]]; |
| 495 | if (ValMapEntry != 0) |
| 496 | return ValMapEntry; |
| 497 | |
| 498 | // Clone the node and update the ValMap. |
| 499 | DSNode* NewNode = new DSNode(*GNode); |
| 500 | ValMapEntry = NewNode; // j=0 case of loop below! |
| 501 | Nodes.push_back(NewNode); |
| 502 | for (unsigned j = 1, N = NewNode->getGlobals().size(); j < N; ++j) |
| 503 | ValueMap[NewNode->getGlobals()[j]] = NewNode; |
| 504 | |
| 505 | // Rewrite the links in the new node to point into the current graph. |
| 506 | for (unsigned j = 0, e = GNode->getNumLinks(); j != e; ++j) |
| 507 | NewNode->setLink(j, cloneGlobalInto(GNode->getLink(j))); |
| 508 | |
| 509 | return NewNode; |
| 510 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 511 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 512 | |
| 513 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 514 | // markIncompleteNodes - Mark the specified node as having contents that are not |
| 515 | // known with the current analysis we have performed. Because a node makes all |
| 516 | // of the nodes it can reach imcomplete if the node itself is incomplete, we |
| 517 | // must recursively traverse the data structure graph, marking all reachable |
| 518 | // nodes as incomplete. |
| 519 | // |
| 520 | static void markIncompleteNode(DSNode *N) { |
| 521 | // Stop recursion if no node, or if node already marked... |
| 522 | if (N == 0 || (N->NodeType & DSNode::Incomplete)) return; |
| 523 | |
| 524 | // Actually mark the node |
| 525 | N->NodeType |= DSNode::Incomplete; |
| 526 | |
| 527 | // Recusively process children... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 528 | for (unsigned i = 0, e = N->getSize(); i != e; ++i) |
| 529 | if (DSNodeHandle *DSNH = N->getLink(i)) |
| 530 | markIncompleteNode(DSNH->getNode()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
| 534 | // markIncompleteNodes - Traverse the graph, identifying nodes that may be |
| 535 | // modified by other functions that have not been resolved yet. This marks |
| 536 | // nodes that are reachable through three sources of "unknownness": |
| 537 | // |
| 538 | // Global Variables, Function Calls, and Incoming Arguments |
| 539 | // |
| 540 | // For any node that may have unknown components (because something outside the |
| 541 | // scope of current analysis may have modified it), the 'Incomplete' flag is |
| 542 | // added to the NodeType. |
| 543 | // |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 544 | void DSGraph::markIncompleteNodes(bool markFormalArgs) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 545 | // Mark any incoming arguments as incomplete... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 546 | if (markFormalArgs && Func) |
| 547 | for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I) |
| 548 | if (isPointerType(I->getType()) && ValueMap.find(I) != ValueMap.end()) { |
| 549 | DSNodeHandle &INH = ValueMap[I]; |
| 550 | if (INH.getNode() && INH.hasLink(0)) |
| 551 | markIncompleteNode(ValueMap[I].getLink(0)->getNode()); |
| 552 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 553 | |
| 554 | // Mark stuff passed into functions calls as being incomplete... |
| 555 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) { |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 556 | DSCallSite &Call = FunctionCalls[i]; |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 557 | // Then the return value is certainly incomplete! |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 558 | markIncompleteNode(Call.getReturnValueNode().getNode()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 559 | |
| 560 | // The call does not make the function argument incomplete... |
| 561 | |
| 562 | // All arguments to the function call are incomplete though! |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 563 | for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i) |
| 564 | markIncompleteNode(Call.getPtrArgNode(i).getNode()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Chris Lattner | 055dc2c | 2002-07-18 15:54:42 +0000 | [diff] [blame] | 567 | // Mark all of the nodes pointed to by global or cast nodes as incomplete... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 568 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 569 | if (Nodes[i]->NodeType & DSNode::GlobalNode) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 570 | DSNode *N = Nodes[i]; |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 571 | for (unsigned i = 0, e = N->getSize(); i != e; ++i) |
| 572 | if (DSNodeHandle *DSNH = N->getLink(i)) |
| 573 | markIncompleteNode(DSNH->getNode()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 577 | // removeRefsToGlobal - Helper function that removes globals from the |
| 578 | // ValueMap so that the referrer count will go down to zero. |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 579 | static void removeRefsToGlobal(DSNode* N, |
| 580 | std::map<Value*, DSNodeHandle> &ValueMap) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 581 | while (!N->getGlobals().empty()) { |
| 582 | GlobalValue *GV = N->getGlobals().back(); |
| 583 | N->getGlobals().pop_back(); |
| 584 | ValueMap.erase(GV); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 589 | // isNodeDead - This method checks to see if a node is dead, and if it isn't, it |
| 590 | // checks to see if there are simple transformations that it can do to make it |
| 591 | // dead. |
| 592 | // |
| 593 | bool DSGraph::isNodeDead(DSNode *N) { |
| 594 | // Is it a trivially dead shadow node... |
| 595 | if (N->getReferrers().empty() && N->NodeType == 0) |
| 596 | return true; |
| 597 | |
| 598 | // Is it a function node or some other trivially unused global? |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 599 | if (N->NodeType != 0 && |
| 600 | (N->NodeType & ~DSNode::GlobalNode) == 0 && |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 601 | N->getSize() == 0 && |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 602 | N->getReferrers().size() == N->getGlobals().size()) { |
| 603 | |
Chris Lattner | a00397e | 2002-10-03 21:55:28 +0000 | [diff] [blame] | 604 | // Remove the globals from the ValueMap, so that the referrer count will go |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 605 | // down to zero. |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 606 | removeRefsToGlobal(N, ValueMap); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 607 | assert(N->getReferrers().empty() && "Referrers should all be gone now!"); |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | return false; |
| 612 | } |
| 613 | |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 614 | static void removeIdenticalCalls(vector<DSCallSite> &Calls, |
Chris Lattner | 7541b89 | 2002-07-31 19:32:12 +0000 | [diff] [blame] | 615 | const std::string &where) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 616 | // Remove trivially identical function calls |
| 617 | unsigned NumFns = Calls.size(); |
| 618 | std::sort(Calls.begin(), Calls.end()); |
| 619 | Calls.erase(std::unique(Calls.begin(), Calls.end()), |
| 620 | Calls.end()); |
| 621 | |
| 622 | DEBUG(if (NumFns != Calls.size()) |
Chris Lattner | 7541b89 | 2002-07-31 19:32:12 +0000 | [diff] [blame] | 623 | std::cerr << "Merged " << (NumFns-Calls.size()) |
| 624 | << " call nodes in " << where << "\n";); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 625 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 626 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 627 | // removeTriviallyDeadNodes - After the graph has been constructed, this method |
| 628 | // removes all unreachable nodes that are created because they got merged with |
| 629 | // other nodes in the graph. These nodes will all be trivially unreachable, so |
| 630 | // we don't have to perform any non-trivial analysis here. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 631 | // |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 632 | void DSGraph::removeTriviallyDeadNodes(bool KeepAllGlobals) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 633 | for (unsigned i = 0; i != Nodes.size(); ++i) |
Chris Lattner | a00397e | 2002-10-03 21:55:28 +0000 | [diff] [blame] | 634 | if (!KeepAllGlobals || !(Nodes[i]->NodeType & DSNode::GlobalNode)) |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 635 | if (isNodeDead(Nodes[i])) { // This node is dead! |
| 636 | delete Nodes[i]; // Free memory... |
| 637 | Nodes.erase(Nodes.begin()+i--); // Remove from node list... |
| 638 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 639 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 640 | removeIdenticalCalls(FunctionCalls, Func ? Func->getName() : ""); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 644 | // markAlive - Simple graph walker that recursively traverses the graph, marking |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 645 | // stuff to be alive. |
| 646 | // |
| 647 | static void markAlive(DSNode *N, std::set<DSNode*> &Alive) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 648 | if (N == 0) return; |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 649 | |
| 650 | Alive.insert(N); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 651 | for (unsigned i = 0, e = N->getSize(); i != e; ++i) |
| 652 | if (DSNodeHandle *DSNH = N->getLink(i)) |
| 653 | if (!Alive.count(DSNH->getNode())) |
| 654 | markAlive(DSNH->getNode(), Alive); |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 657 | static bool checkGlobalAlive(DSNode *N, std::set<DSNode*> &Alive, |
| 658 | std::set<DSNode*> &Visiting) { |
| 659 | if (N == 0) return false; |
| 660 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 661 | if (Visiting.count(N)) return false; // terminate recursion on a cycle |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 662 | Visiting.insert(N); |
| 663 | |
| 664 | // If any immediate successor is alive, N is alive |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 665 | for (unsigned i = 0, e = N->getSize(); i != e; ++i) |
| 666 | if (DSNodeHandle *DSNH = N->getLink(i)) |
| 667 | if (Alive.count(DSNH->getNode())) { |
| 668 | Visiting.erase(N); |
| 669 | return true; |
| 670 | } |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 671 | |
| 672 | // Else if any successor reaches a live node, N is alive |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 673 | for (unsigned i = 0, e = N->getSize(); i != e; ++i) |
| 674 | if (DSNodeHandle *DSNH = N->getLink(i)) |
| 675 | if (checkGlobalAlive(DSNH->getNode(), Alive, Visiting)) { |
| 676 | Visiting.erase(N); return true; |
| 677 | } |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 678 | |
| 679 | Visiting.erase(N); |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | |
| 684 | // markGlobalsIteration - Recursive helper function for markGlobalsAlive(). |
| 685 | // This would be unnecessary if function calls were real nodes! In that case, |
| 686 | // the simple iterative loop in the first few lines below suffice. |
| 687 | // |
| 688 | static void markGlobalsIteration(std::set<DSNode*>& GlobalNodes, |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 689 | vector<DSCallSite> &Calls, |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 690 | std::set<DSNode*> &Alive, |
| 691 | bool FilterCalls) { |
| 692 | |
| 693 | // Iterate, marking globals or cast nodes alive until no new live nodes |
| 694 | // are added to Alive |
| 695 | std::set<DSNode*> Visiting; // Used to identify cycles |
| 696 | std::set<DSNode*>::iterator I=GlobalNodes.begin(), E=GlobalNodes.end(); |
| 697 | for (size_t liveCount = 0; liveCount < Alive.size(); ) { |
| 698 | liveCount = Alive.size(); |
| 699 | for ( ; I != E; ++I) |
| 700 | if (Alive.count(*I) == 0) { |
| 701 | Visiting.clear(); |
| 702 | if (checkGlobalAlive(*I, Alive, Visiting)) |
| 703 | markAlive(*I, Alive); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | // Find function calls with some dead and some live nodes. |
| 708 | // Since all call nodes must be live if any one is live, we have to mark |
| 709 | // all nodes of the call as live and continue the iteration (via recursion). |
| 710 | if (FilterCalls) { |
| 711 | bool recurse = false; |
| 712 | for (int i = 0, ei = Calls.size(); i < ei; ++i) { |
| 713 | bool CallIsDead = true, CallHasDeadArg = false; |
| 714 | for (unsigned j = 0, ej = Calls[i].size(); j != ej; ++j) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 715 | bool argIsDead = Calls[i][j].getNode() == 0 || |
| 716 | Alive.count(Calls[i][j].getNode()) == 0; |
| 717 | CallHasDeadArg |= (Calls[i][j].getNode() != 0 && argIsDead); |
| 718 | CallIsDead &= argIsDead; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 719 | } |
| 720 | if (!CallIsDead && CallHasDeadArg) { |
| 721 | // Some node in this call is live and another is dead. |
| 722 | // Mark all nodes of call as live and iterate once more. |
| 723 | recurse = true; |
| 724 | for (unsigned j = 0, ej = Calls[i].size(); j != ej; ++j) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 725 | markAlive(Calls[i][j].getNode(), Alive); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | if (recurse) |
| 729 | markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | |
| 734 | // markGlobalsAlive - Mark global nodes and cast nodes alive if they |
| 735 | // can reach any other live node. Since this can produce new live nodes, |
| 736 | // we use a simple iterative algorithm. |
| 737 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 738 | static void markGlobalsAlive(DSGraph &G, std::set<DSNode*> &Alive, |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 739 | bool FilterCalls) { |
| 740 | // Add global and cast nodes to a set so we don't walk all nodes every time |
| 741 | std::set<DSNode*> GlobalNodes; |
| 742 | for (unsigned i = 0, e = G.getNodes().size(); i != e; ++i) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 743 | if (G.getNodes()[i]->NodeType & DSNode::GlobalNode) |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 744 | GlobalNodes.insert(G.getNodes()[i]); |
| 745 | |
| 746 | // Add all call nodes to the same set |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 747 | vector<DSCallSite> &Calls = G.getFunctionCalls(); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 748 | if (FilterCalls) { |
| 749 | for (unsigned i = 0, e = Calls.size(); i != e; ++i) |
| 750 | for (unsigned j = 0, e = Calls[i].size(); j != e; ++j) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 751 | if (Calls[i][j].getNode()) |
| 752 | GlobalNodes.insert(Calls[i][j].getNode()); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // Iterate and recurse until no new live node are discovered. |
| 756 | // This would be a simple iterative loop if function calls were real nodes! |
| 757 | markGlobalsIteration(GlobalNodes, Calls, Alive, FilterCalls); |
| 758 | |
| 759 | // Free up references to dead globals from the ValueMap |
| 760 | std::set<DSNode*>::iterator I=GlobalNodes.begin(), E=GlobalNodes.end(); |
| 761 | for( ; I != E; ++I) |
| 762 | if (Alive.count(*I) == 0) |
| 763 | removeRefsToGlobal(*I, G.getValueMap()); |
| 764 | |
| 765 | // Delete dead function calls |
| 766 | if (FilterCalls) |
| 767 | for (int ei = Calls.size(), i = ei-1; i >= 0; --i) { |
| 768 | bool CallIsDead = true; |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 769 | for (unsigned j = 0, ej = Calls[i].size(); CallIsDead && j != ej; ++j) |
| 770 | CallIsDead = Alive.count(Calls[i][j].getNode()) == 0; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 771 | if (CallIsDead) |
| 772 | Calls.erase(Calls.begin() + i); // remove the call entirely |
| 773 | } |
| 774 | } |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 775 | |
| 776 | // removeDeadNodes - Use a more powerful reachability analysis to eliminate |
| 777 | // subgraphs that are unreachable. This often occurs because the data |
| 778 | // structure doesn't "escape" into it's caller, and thus should be eliminated |
| 779 | // from the caller's graph entirely. This is only appropriate to use when |
| 780 | // inlining graphs. |
| 781 | // |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 782 | void DSGraph::removeDeadNodes(bool KeepAllGlobals, bool KeepCalls) { |
| 783 | assert((!KeepAllGlobals || KeepCalls) && |
| 784 | "KeepAllGlobals without KeepCalls is meaningless"); |
| 785 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 786 | // Reduce the amount of work we have to do... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 787 | removeTriviallyDeadNodes(KeepAllGlobals); |
| 788 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 789 | // FIXME: Merge nontrivially identical call nodes... |
| 790 | |
| 791 | // Alive - a set that holds all nodes found to be reachable/alive. |
| 792 | std::set<DSNode*> Alive; |
| 793 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 794 | // If KeepCalls, mark all nodes reachable by call nodes as alive... |
| 795 | if (KeepCalls) |
| 796 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) |
| 797 | for (unsigned j = 0, e = FunctionCalls[i].size(); j != e; ++j) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 798 | markAlive(FunctionCalls[i][j].getNode(), Alive); |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 799 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 800 | #if 0 |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 801 | for (unsigned i = 0, e = OrigFunctionCalls.size(); i != e; ++i) |
| 802 | for (unsigned j = 0, e = OrigFunctionCalls[i].size(); j != e; ++j) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 803 | markAlive(OrigFunctionCalls[i][j].getNode(), Alive); |
| 804 | #endif |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 805 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 806 | // Mark all nodes reachable by scalar nodes (and global nodes, if |
| 807 | // keeping them was specified) as alive... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 808 | unsigned char keepBits = DSNode::ScalarNode | |
| 809 | (KeepAllGlobals ? DSNode::GlobalNode : 0); |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 810 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 811 | if (Nodes[i]->NodeType & keepBits) |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 812 | markAlive(Nodes[i], Alive); |
| 813 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 814 | // The return value is alive as well... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 815 | markAlive(RetNode.getNode(), Alive); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 816 | |
| 817 | // Mark all globals or cast nodes that can reach a live node as alive. |
| 818 | // This also marks all nodes reachable from such nodes as alive. |
| 819 | // Of course, if KeepAllGlobals is specified, they would be live already. |
| 820 | if (! KeepAllGlobals) |
| 821 | markGlobalsAlive(*this, Alive, ! KeepCalls); |
| 822 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 823 | // Loop over all unreachable nodes, dropping their references... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 824 | vector<DSNode*> DeadNodes; |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 825 | DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed. |
| 826 | for (unsigned i = 0; i != Nodes.size(); ++i) |
| 827 | if (!Alive.count(Nodes[i])) { |
| 828 | DSNode *N = Nodes[i]; |
| 829 | Nodes.erase(Nodes.begin()+i--); // Erase node from alive list. |
| 830 | DeadNodes.push_back(N); // Add node to our list of dead nodes |
| 831 | N->dropAllReferences(); // Drop all outgoing edges |
| 832 | } |
| 833 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 834 | // Delete all dead nodes... |
| 835 | std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>); |
| 836 | } |
| 837 | |
| 838 | |
| 839 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 840 | // maskNodeTypes - Apply a mask to all of the node types in the graph. This |
| 841 | // is useful for clearing out markers like Scalar or Incomplete. |
| 842 | // |
| 843 | void DSGraph::maskNodeTypes(unsigned char Mask) { |
| 844 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 845 | Nodes[i]->NodeType &= Mask; |
| 846 | } |
| 847 | |
| 848 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 849 | #if 0 |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 850 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 851 | // GlobalDSGraph Implementation |
| 852 | //===----------------------------------------------------------------------===// |
| 853 | |
| 854 | GlobalDSGraph::GlobalDSGraph() : DSGraph(*(Function*)0, this) { |
| 855 | } |
| 856 | |
| 857 | GlobalDSGraph::~GlobalDSGraph() { |
| 858 | assert(Referrers.size() == 0 && |
| 859 | "Deleting global graph while references from other graphs exist"); |
| 860 | } |
| 861 | |
| 862 | void GlobalDSGraph::addReference(const DSGraph* referrer) { |
| 863 | if (referrer != this) |
| 864 | Referrers.insert(referrer); |
| 865 | } |
| 866 | |
| 867 | void GlobalDSGraph::removeReference(const DSGraph* referrer) { |
| 868 | if (referrer != this) { |
| 869 | assert(Referrers.find(referrer) != Referrers.end() && "This is very bad!"); |
| 870 | Referrers.erase(referrer); |
| 871 | if (Referrers.size() == 0) |
| 872 | delete this; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | // Bits used in the next function |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 877 | static const char ExternalTypeBits = DSNode::GlobalNode | DSNode::NewNode; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 878 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 879 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 880 | // GlobalDSGraph::cloneNodeInto - Clone a global node and all its externally |
| 881 | // visible target links (and recursively their such links) into this graph. |
| 882 | // NodeCache maps the node being cloned to its clone in the Globals graph, |
| 883 | // in order to track cycles. |
| 884 | // GlobalsAreFinal is a flag that says whether it is safe to assume that |
| 885 | // an existing global node is complete. This is important to avoid |
| 886 | // reinserting all globals when inserting Calls to functions. |
| 887 | // This is a helper function for cloneGlobals and cloneCalls. |
| 888 | // |
| 889 | DSNode* GlobalDSGraph::cloneNodeInto(DSNode *OldNode, |
| 890 | std::map<const DSNode*, DSNode*> &NodeCache, |
| 891 | bool GlobalsAreFinal) { |
| 892 | if (OldNode == 0) return 0; |
| 893 | |
| 894 | // The caller should check this is an external node. Just more efficient... |
| 895 | assert((OldNode->NodeType & ExternalTypeBits) && "Non-external node"); |
| 896 | |
| 897 | // If a clone has already been created for OldNode, return it. |
| 898 | DSNode*& CacheEntry = NodeCache[OldNode]; |
| 899 | if (CacheEntry != 0) |
| 900 | return CacheEntry; |
| 901 | |
| 902 | // The result value... |
| 903 | DSNode* NewNode = 0; |
| 904 | |
| 905 | // If nodes already exist for any of the globals of OldNode, |
| 906 | // merge all such nodes together since they are merged in OldNode. |
| 907 | // If ValueCacheIsFinal==true, look for an existing node that has |
| 908 | // an identical list of globals and return it if it exists. |
| 909 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 910 | for (unsigned j = 0, N = OldNode->getGlobals().size(); j != N; ++j) |
| 911 | if (DSNode *PrevNode = ValueMap[OldNode->getGlobals()[j]].getNode()) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 912 | if (NewNode == 0) { |
| 913 | NewNode = PrevNode; // first existing node found |
| 914 | if (GlobalsAreFinal && j == 0) |
| 915 | if (OldNode->getGlobals() == PrevNode->getGlobals()) { |
| 916 | CacheEntry = NewNode; |
| 917 | return NewNode; |
| 918 | } |
| 919 | } |
| 920 | else if (NewNode != PrevNode) { // found another, different from prev |
| 921 | // update ValMap *before* merging PrevNode into NewNode |
| 922 | for (unsigned k = 0, NK = PrevNode->getGlobals().size(); k < NK; ++k) |
| 923 | ValueMap[PrevNode->getGlobals()[k]] = NewNode; |
| 924 | NewNode->mergeWith(PrevNode); |
| 925 | } |
| 926 | } else if (NewNode != 0) { |
| 927 | ValueMap[OldNode->getGlobals()[j]] = NewNode; // add the merged node |
| 928 | } |
| 929 | |
| 930 | // If no existing node was found, clone the node and update the ValMap. |
| 931 | if (NewNode == 0) { |
| 932 | NewNode = new DSNode(*OldNode); |
| 933 | Nodes.push_back(NewNode); |
| 934 | for (unsigned j = 0, e = NewNode->getNumLinks(); j != e; ++j) |
| 935 | NewNode->setLink(j, 0); |
| 936 | for (unsigned j = 0, N = NewNode->getGlobals().size(); j < N; ++j) |
| 937 | ValueMap[NewNode->getGlobals()[j]] = NewNode; |
| 938 | } |
| 939 | else |
| 940 | NewNode->NodeType |= OldNode->NodeType; // Markers may be different! |
| 941 | |
| 942 | // Add the entry to NodeCache |
| 943 | CacheEntry = NewNode; |
| 944 | |
| 945 | // Rewrite the links in the new node to point into the current graph, |
| 946 | // but only for links to external nodes. Set other links to NULL. |
| 947 | for (unsigned j = 0, e = OldNode->getNumLinks(); j != e; ++j) { |
| 948 | DSNode* OldTarget = OldNode->getLink(j); |
| 949 | if (OldTarget && (OldTarget->NodeType & ExternalTypeBits)) { |
| 950 | DSNode* NewLink = this->cloneNodeInto(OldTarget, NodeCache); |
| 951 | if (NewNode->getLink(j)) |
| 952 | NewNode->getLink(j)->mergeWith(NewLink); |
| 953 | else |
| 954 | NewNode->setLink(j, NewLink); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // Remove all local markers |
| 959 | NewNode->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode); |
| 960 | |
| 961 | return NewNode; |
| 962 | } |
| 963 | |
| 964 | |
| 965 | // GlobalDSGraph::cloneGlobals - Clone global nodes and all their externally |
| 966 | // visible target links (and recursively their such links) into this graph. |
| 967 | // |
| 968 | void GlobalDSGraph::cloneGlobals(DSGraph& Graph, bool CloneCalls) { |
| 969 | std::map<const DSNode*, DSNode*> NodeCache; |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 970 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 971 | for (unsigned i = 0, N = Graph.Nodes.size(); i < N; ++i) |
| 972 | if (Graph.Nodes[i]->NodeType & DSNode::GlobalNode) |
| 973 | GlobalsGraph->cloneNodeInto(Graph.Nodes[i], NodeCache, false); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 974 | if (CloneCalls) |
| 975 | GlobalsGraph->cloneCalls(Graph); |
| 976 | |
| 977 | GlobalsGraph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 978 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | |
| 982 | // GlobalDSGraph::cloneCalls - Clone function calls and their visible target |
| 983 | // links (and recursively their such links) into this graph. |
| 984 | // |
| 985 | void GlobalDSGraph::cloneCalls(DSGraph& Graph) { |
| 986 | std::map<const DSNode*, DSNode*> NodeCache; |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 987 | vector<DSCallSite >& FromCalls =Graph.FunctionCalls; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 988 | |
| 989 | FunctionCalls.reserve(FunctionCalls.size() + FromCalls.size()); |
| 990 | |
| 991 | for (int i = 0, ei = FromCalls.size(); i < ei; ++i) { |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 992 | DSCallSite& callCopy = FunctionCalls.back(); |
| 993 | callCopy.reserve(FromCalls[i].size()); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 994 | for (unsigned j = 0, ej = FromCalls[i].size(); j != ej; ++j) |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 995 | callCopy.push_back |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 996 | ((FromCalls[i][j] && (FromCalls[i][j]->NodeType & ExternalTypeBits)) |
| 997 | ? cloneNodeInto(FromCalls[i][j], NodeCache, true) |
| 998 | : 0); |
| 999 | } |
| 1000 | |
| 1001 | // remove trivially identical function calls |
Chris Lattner | 7541b89 | 2002-07-31 19:32:12 +0000 | [diff] [blame] | 1002 | removeIdenticalCalls(FunctionCalls, "Globals Graph"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1003 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1004 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1006 | #endif |