Chris Lattner | d28b0d7 | 2004-06-25 04:24:22 +0000 | [diff] [blame] | 1 | //===- Andersens.cpp - Andersen's Interprocedural Alias Analysis ----------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 10 | // This file defines an implementation of Andersen's interprocedural alias |
| 11 | // analysis |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 12 | // |
| 13 | // In pointer analysis terms, this is a subset-based, flow-insensitive, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 14 | // field-sensitive, and context-insensitive algorithm pointer algorithm. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 15 | // |
| 16 | // This algorithm is implemented as three stages: |
| 17 | // 1. Object identification. |
| 18 | // 2. Inclusion constraint identification. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 19 | // 3. Offline constraint graph optimization |
| 20 | // 4. Inclusion constraint solving. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 21 | // |
| 22 | // The object identification stage identifies all of the memory objects in the |
| 23 | // program, which includes globals, heap allocated objects, and stack allocated |
| 24 | // objects. |
| 25 | // |
| 26 | // The inclusion constraint identification stage finds all inclusion constraints |
| 27 | // in the program by scanning the program, looking for pointer assignments and |
| 28 | // other statements that effect the points-to graph. For a statement like "A = |
| 29 | // B", this statement is processed to indicate that A can point to anything that |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 30 | // B can point to. Constraints can handle copies, loads, and stores, and |
| 31 | // address taking. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 32 | // |
Daniel Berlin | e6f0479 | 2007-09-24 22:20:45 +0000 | [diff] [blame] | 33 | // The offline constraint graph optimization portion includes offline variable |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 34 | // substitution algorithms intended to compute pointer and location |
Daniel Berlin | e6f0479 | 2007-09-24 22:20:45 +0000 | [diff] [blame] | 35 | // equivalences. Pointer equivalences are those pointers that will have the |
| 36 | // same points-to sets, and location equivalences are those variables that |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 37 | // always appear together in points-to sets. It also includes an offline |
| 38 | // cycle detection algorithm that allows cycles to be collapsed sooner |
| 39 | // during solving. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 40 | // |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 41 | // The inclusion constraint solving phase iteratively propagates the inclusion |
| 42 | // constraints until a fixed point is reached. This is an O(N^3) algorithm. |
| 43 | // |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 44 | // Function constraints are handled as if they were structs with X fields. |
| 45 | // Thus, an access to argument X of function Y is an access to node index |
| 46 | // getNode(Y) + X. This representation allows handling of indirect calls |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 47 | // without any issues. To wit, an indirect call Y(a,b) is equivalent to |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 48 | // *(Y + 1) = a, *(Y + 2) = b. |
| 49 | // The return node for a function is always located at getNode(F) + |
| 50 | // CallReturnPos. The arguments start at getNode(F) + CallArgPos. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 51 | // |
Chris Lattner | c7ca32b | 2004-06-05 20:12:36 +0000 | [diff] [blame] | 52 | // Future Improvements: |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 53 | // Use of BDD's. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | #define DEBUG_TYPE "anders-aa" |
| 57 | #include "llvm/Constants.h" |
| 58 | #include "llvm/DerivedTypes.h" |
| 59 | #include "llvm/Instructions.h" |
| 60 | #include "llvm/Module.h" |
| 61 | #include "llvm/Pass.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 62 | #include "llvm/Support/Compiler.h" |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 63 | #include "llvm/Support/InstIterator.h" |
| 64 | #include "llvm/Support/InstVisitor.h" |
| 65 | #include "llvm/Analysis/AliasAnalysis.h" |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 66 | #include "llvm/Analysis/Passes.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 67 | #include "llvm/Support/Debug.h" |
| 68 | #include "llvm/ADT/Statistic.h" |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 69 | #include "llvm/ADT/SparseBitVector.h" |
Chris Lattner | be20773 | 2007-09-30 00:47:20 +0000 | [diff] [blame] | 70 | #include "llvm/ADT/DenseSet.h" |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 71 | #include <algorithm> |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 72 | #include <set> |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 73 | #include <list> |
| 74 | #include <stack> |
| 75 | #include <vector> |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 76 | #include <queue> |
| 77 | |
| 78 | // Determining the actual set of nodes the universal set can consist of is very |
| 79 | // expensive because it means propagating around very large sets. We rely on |
| 80 | // other analysis being able to determine which nodes can never be pointed to in |
| 81 | // order to disambiguate further than "points-to anything". |
| 82 | #define FULL_UNIVERSAL 0 |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 83 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 84 | using namespace llvm; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 85 | STATISTIC(NumIters , "Number of iterations to reach convergence"); |
| 86 | STATISTIC(NumConstraints, "Number of constraints"); |
| 87 | STATISTIC(NumNodes , "Number of nodes"); |
| 88 | STATISTIC(NumUnified , "Number of variables unified"); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 89 | STATISTIC(NumErased , "Number of redundant constraints erased"); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 91 | namespace { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 92 | const unsigned SelfRep = (unsigned)-1; |
| 93 | const unsigned Unvisited = (unsigned)-1; |
| 94 | // Position of the function return node relative to the function node. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 95 | const unsigned CallReturnPos = 1; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 96 | // Position of the function call node relative to the function node. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 97 | const unsigned CallFirstArgPos = 2; |
| 98 | |
| 99 | struct BitmapKeyInfo { |
| 100 | static inline SparseBitVector<> *getEmptyKey() { |
| 101 | return reinterpret_cast<SparseBitVector<> *>(-1); |
| 102 | } |
| 103 | static inline SparseBitVector<> *getTombstoneKey() { |
| 104 | return reinterpret_cast<SparseBitVector<> *>(-2); |
| 105 | } |
| 106 | static unsigned getHashValue(const SparseBitVector<> *bitmap) { |
| 107 | return bitmap->getHashValue(); |
| 108 | } |
| 109 | static bool isEqual(const SparseBitVector<> *LHS, |
| 110 | const SparseBitVector<> *RHS) { |
| 111 | if (LHS == RHS) |
| 112 | return true; |
| 113 | else if (LHS == getEmptyKey() || RHS == getEmptyKey() |
| 114 | || LHS == getTombstoneKey() || RHS == getTombstoneKey()) |
| 115 | return false; |
| 116 | |
| 117 | return *LHS == *RHS; |
| 118 | } |
| 119 | |
| 120 | static bool isPod() { return true; } |
| 121 | }; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 122 | |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 123 | class VISIBILITY_HIDDEN Andersens : public ModulePass, public AliasAnalysis, |
| 124 | private InstVisitor<Andersens> { |
Hartmut Kaiser | 081fdf2 | 2007-10-25 23:49:14 +0000 | [diff] [blame] | 125 | struct Node; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 126 | |
| 127 | /// Constraint - Objects of this structure are used to represent the various |
| 128 | /// constraints identified by the algorithm. The constraints are 'copy', |
| 129 | /// for statements like "A = B", 'load' for statements like "A = *B", |
| 130 | /// 'store' for statements like "*A = B", and AddressOf for statements like |
| 131 | /// A = alloca; The Offset is applied as *(A + K) = B for stores, |
| 132 | /// A = *(B + K) for loads, and A = B + K for copies. It is |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 133 | /// illegal on addressof constraints (because it is statically |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 134 | /// resolvable to A = &C where C = B + K) |
| 135 | |
| 136 | struct Constraint { |
| 137 | enum ConstraintType { Copy, Load, Store, AddressOf } Type; |
| 138 | unsigned Dest; |
| 139 | unsigned Src; |
| 140 | unsigned Offset; |
| 141 | |
| 142 | Constraint(ConstraintType Ty, unsigned D, unsigned S, unsigned O = 0) |
| 143 | : Type(Ty), Dest(D), Src(S), Offset(O) { |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 144 | assert((Offset == 0 || Ty != AddressOf) && |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 145 | "Offset is illegal on addressof constraints"); |
| 146 | } |
Daniel Berlin | 336c6c0 | 2007-09-29 00:50:40 +0000 | [diff] [blame] | 147 | |
Daniel Berlin | c7a12ae | 2007-09-27 15:42:23 +0000 | [diff] [blame] | 148 | bool operator==(const Constraint &RHS) const { |
| 149 | return RHS.Type == Type |
| 150 | && RHS.Dest == Dest |
| 151 | && RHS.Src == Src |
| 152 | && RHS.Offset == Offset; |
| 153 | } |
Daniel Berlin | 336c6c0 | 2007-09-29 00:50:40 +0000 | [diff] [blame] | 154 | |
| 155 | bool operator!=(const Constraint &RHS) const { |
| 156 | return !(*this == RHS); |
| 157 | } |
| 158 | |
Daniel Berlin | c7a12ae | 2007-09-27 15:42:23 +0000 | [diff] [blame] | 159 | bool operator<(const Constraint &RHS) const { |
| 160 | if (RHS.Type != Type) |
| 161 | return RHS.Type < Type; |
| 162 | else if (RHS.Dest != Dest) |
| 163 | return RHS.Dest < Dest; |
| 164 | else if (RHS.Src != Src) |
| 165 | return RHS.Src < Src; |
| 166 | return RHS.Offset < Offset; |
| 167 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 170 | // Information DenseSet requires implemented in order to be able to do |
| 171 | // it's thing |
| 172 | struct PairKeyInfo { |
| 173 | static inline std::pair<unsigned, unsigned> getEmptyKey() { |
| 174 | return std::make_pair(~0UL, ~0UL); |
| 175 | } |
| 176 | static inline std::pair<unsigned, unsigned> getTombstoneKey() { |
| 177 | return std::make_pair(~0UL - 1, ~0UL - 1); |
| 178 | } |
| 179 | static unsigned getHashValue(const std::pair<unsigned, unsigned> &P) { |
| 180 | return P.first ^ P.second; |
| 181 | } |
| 182 | static unsigned isEqual(const std::pair<unsigned, unsigned> &LHS, |
| 183 | const std::pair<unsigned, unsigned> &RHS) { |
| 184 | return LHS == RHS; |
| 185 | } |
| 186 | }; |
| 187 | |
Daniel Berlin | 336c6c0 | 2007-09-29 00:50:40 +0000 | [diff] [blame] | 188 | struct ConstraintKeyInfo { |
| 189 | static inline Constraint getEmptyKey() { |
| 190 | return Constraint(Constraint::Copy, ~0UL, ~0UL, ~0UL); |
| 191 | } |
| 192 | static inline Constraint getTombstoneKey() { |
| 193 | return Constraint(Constraint::Copy, ~0UL - 1, ~0UL - 1, ~0UL - 1); |
| 194 | } |
| 195 | static unsigned getHashValue(const Constraint &C) { |
| 196 | return C.Src ^ C.Dest ^ C.Type ^ C.Offset; |
| 197 | } |
| 198 | static bool isEqual(const Constraint &LHS, |
| 199 | const Constraint &RHS) { |
| 200 | return LHS.Type == RHS.Type && LHS.Dest == RHS.Dest |
| 201 | && LHS.Src == RHS.Src && LHS.Offset == RHS.Offset; |
| 202 | } |
| 203 | }; |
| 204 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 205 | // Node class - This class is used to represent a node in the constraint |
Daniel Berlin | e6f0479 | 2007-09-24 22:20:45 +0000 | [diff] [blame] | 206 | // graph. Due to various optimizations, it is not always the case that |
| 207 | // there is a mapping from a Node to a Value. In particular, we add |
| 208 | // artificial Node's that represent the set of pointed-to variables shared |
| 209 | // for each location equivalent Node. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 210 | struct Node { |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 211 | private: |
| 212 | static unsigned Counter; |
| 213 | |
| 214 | public: |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 215 | Value *Val; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 216 | SparseBitVector<> *Edges; |
| 217 | SparseBitVector<> *PointsTo; |
| 218 | SparseBitVector<> *OldPointsTo; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 219 | std::list<Constraint> Constraints; |
| 220 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 221 | // Pointer and location equivalence labels |
| 222 | unsigned PointerEquivLabel; |
| 223 | unsigned LocationEquivLabel; |
| 224 | // Predecessor edges, both real and implicit |
| 225 | SparseBitVector<> *PredEdges; |
| 226 | SparseBitVector<> *ImplicitPredEdges; |
| 227 | // Set of nodes that point to us, only use for location equivalence. |
| 228 | SparseBitVector<> *PointedToBy; |
| 229 | // Number of incoming edges, used during variable substitution to early |
| 230 | // free the points-to sets |
| 231 | unsigned NumInEdges; |
Daniel Berlin | e6f0479 | 2007-09-24 22:20:45 +0000 | [diff] [blame] | 232 | // True if our points-to set is in the Set2PEClass map |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 233 | bool StoredInHash; |
Daniel Berlin | e6f0479 | 2007-09-24 22:20:45 +0000 | [diff] [blame] | 234 | // True if our node has no indirect constraints (complex or otherwise) |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 235 | bool Direct; |
| 236 | // True if the node is address taken, *or* it is part of a group of nodes |
| 237 | // that must be kept together. This is set to true for functions and |
| 238 | // their arg nodes, which must be kept at the same position relative to |
| 239 | // their base function node. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 240 | bool AddressTaken; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 241 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 242 | // Nodes in cycles (or in equivalence classes) are united together using a |
| 243 | // standard union-find representation with path compression. NodeRep |
| 244 | // gives the index into GraphNodes for the representative Node. |
| 245 | unsigned NodeRep; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 246 | |
| 247 | // Modification timestamp. Assigned from Counter. |
| 248 | // Used for work list prioritization. |
| 249 | unsigned Timestamp; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 250 | |
Dan Gohman | ded2b0d | 2007-12-14 15:41:34 +0000 | [diff] [blame] | 251 | explicit Node(bool direct = true) : |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 252 | Val(0), Edges(0), PointsTo(0), OldPointsTo(0), |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 253 | PointerEquivLabel(0), LocationEquivLabel(0), PredEdges(0), |
| 254 | ImplicitPredEdges(0), PointedToBy(0), NumInEdges(0), |
| 255 | StoredInHash(false), Direct(direct), AddressTaken(false), |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 256 | NodeRep(SelfRep), Timestamp(0) { } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 257 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 258 | Node *setValue(Value *V) { |
| 259 | assert(Val == 0 && "Value already set for this node!"); |
| 260 | Val = V; |
| 261 | return this; |
| 262 | } |
| 263 | |
| 264 | /// getValue - Return the LLVM value corresponding to this node. |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 265 | /// |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 266 | Value *getValue() const { return Val; } |
| 267 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 268 | /// addPointerTo - Add a pointer to the list of pointees of this node, |
| 269 | /// returning true if this caused a new pointer to be added, or false if |
| 270 | /// we already knew about the points-to relation. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 271 | bool addPointerTo(unsigned Node) { |
| 272 | return PointsTo->test_and_set(Node); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /// intersects - Return true if the points-to set of this node intersects |
| 276 | /// with the points-to set of the specified node. |
| 277 | bool intersects(Node *N) const; |
| 278 | |
| 279 | /// intersectsIgnoring - Return true if the points-to set of this node |
| 280 | /// intersects with the points-to set of the specified node on any nodes |
| 281 | /// except for the specified node to ignore. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 282 | bool intersectsIgnoring(Node *N, unsigned) const; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 283 | |
| 284 | // Timestamp a node (used for work list prioritization) |
| 285 | void Stamp() { |
| 286 | Timestamp = Counter++; |
| 287 | } |
| 288 | |
| 289 | bool isRep() { |
| 290 | return( (int) NodeRep < 0 ); |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | struct WorkListElement { |
| 295 | Node* node; |
| 296 | unsigned Timestamp; |
| 297 | WorkListElement(Node* n, unsigned t) : node(n), Timestamp(t) {} |
| 298 | |
| 299 | // Note that we reverse the sense of the comparison because we |
| 300 | // actually want to give low timestamps the priority over high, |
| 301 | // whereas priority is typically interpreted as a greater value is |
| 302 | // given high priority. |
| 303 | bool operator<(const WorkListElement& that) const { |
| 304 | return( this->Timestamp > that.Timestamp ); |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | // Priority-queue based work list specialized for Nodes. |
| 309 | class WorkList { |
| 310 | std::priority_queue<WorkListElement> Q; |
| 311 | |
| 312 | public: |
| 313 | void insert(Node* n) { |
| 314 | Q.push( WorkListElement(n, n->Timestamp) ); |
| 315 | } |
| 316 | |
| 317 | // We automatically discard non-representative nodes and nodes |
| 318 | // that were in the work list twice (we keep a copy of the |
| 319 | // timestamp in the work list so we can detect this situation by |
| 320 | // comparing against the node's current timestamp). |
| 321 | Node* pop() { |
| 322 | while( !Q.empty() ) { |
| 323 | WorkListElement x = Q.top(); Q.pop(); |
| 324 | Node* INode = x.node; |
| 325 | |
| 326 | if( INode->isRep() && |
| 327 | INode->Timestamp == x.Timestamp ) { |
| 328 | return(x.node); |
| 329 | } |
| 330 | } |
| 331 | return(0); |
| 332 | } |
| 333 | |
| 334 | bool empty() { |
| 335 | return Q.empty(); |
| 336 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /// GraphNodes - This vector is populated as part of the object |
| 340 | /// identification stage of the analysis, which populates this vector with a |
| 341 | /// node for each memory object and fills in the ValueNodes map. |
| 342 | std::vector<Node> GraphNodes; |
| 343 | |
| 344 | /// ValueNodes - This map indicates the Node that a particular Value* is |
| 345 | /// represented by. This contains entries for all pointers. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 346 | DenseMap<Value*, unsigned> ValueNodes; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 347 | |
| 348 | /// ObjectNodes - This map contains entries for each memory object in the |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 349 | /// program: globals, alloca's and mallocs. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 350 | DenseMap<Value*, unsigned> ObjectNodes; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 351 | |
| 352 | /// ReturnNodes - This map contains an entry for each function in the |
| 353 | /// program that returns a value. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 354 | DenseMap<Function*, unsigned> ReturnNodes; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 355 | |
| 356 | /// VarargNodes - This map contains the entry used to represent all pointers |
| 357 | /// passed through the varargs portion of a function call for a particular |
| 358 | /// function. An entry is not present in this map for functions that do not |
| 359 | /// take variable arguments. |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 360 | DenseMap<Function*, unsigned> VarargNodes; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 361 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 362 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 363 | /// Constraints - This vector contains a list of all of the constraints |
| 364 | /// identified by the program. |
| 365 | std::vector<Constraint> Constraints; |
| 366 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 367 | // Map from graph node to maximum K value that is allowed (for functions, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 368 | // this is equivalent to the number of arguments + CallFirstArgPos) |
| 369 | std::map<unsigned, unsigned> MaxK; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 370 | |
| 371 | /// This enum defines the GraphNodes indices that correspond to important |
| 372 | /// fixed sets. |
| 373 | enum { |
| 374 | UniversalSet = 0, |
| 375 | NullPtr = 1, |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 376 | NullObject = 2, |
| 377 | NumberSpecialNodes |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 378 | }; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 379 | // Stack for Tarjan's |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 380 | std::stack<unsigned> SCCStack; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 381 | // Map from Graph Node to DFS number |
| 382 | std::vector<unsigned> Node2DFS; |
| 383 | // Map from Graph Node to Deleted from graph. |
| 384 | std::vector<bool> Node2Deleted; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 385 | // Same as Node Maps, but implemented as std::map because it is faster to |
| 386 | // clear |
| 387 | std::map<unsigned, unsigned> Tarjan2DFS; |
| 388 | std::map<unsigned, bool> Tarjan2Deleted; |
| 389 | // Current DFS number |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 390 | unsigned DFSNumber; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 391 | |
| 392 | // Work lists. |
| 393 | WorkList w1, w2; |
| 394 | WorkList *CurrWL, *NextWL; // "current" and "next" work lists |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 395 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 396 | // Offline variable substitution related things |
| 397 | |
| 398 | // Temporary rep storage, used because we can't collapse SCC's in the |
| 399 | // predecessor graph by uniting the variables permanently, we can only do so |
| 400 | // for the successor graph. |
| 401 | std::vector<unsigned> VSSCCRep; |
| 402 | // Mapping from node to whether we have visited it during SCC finding yet. |
| 403 | std::vector<bool> Node2Visited; |
| 404 | // During variable substitution, we create unknowns to represent the unknown |
| 405 | // value that is a dereference of a variable. These nodes are known as |
| 406 | // "ref" nodes (since they represent the value of dereferences). |
| 407 | unsigned FirstRefNode; |
| 408 | // During HVN, we create represent address taken nodes as if they were |
| 409 | // unknown (since HVN, unlike HU, does not evaluate unions). |
| 410 | unsigned FirstAdrNode; |
| 411 | // Current pointer equivalence class number |
| 412 | unsigned PEClass; |
| 413 | // Mapping from points-to sets to equivalence classes |
| 414 | typedef DenseMap<SparseBitVector<> *, unsigned, BitmapKeyInfo> BitVectorMap; |
| 415 | BitVectorMap Set2PEClass; |
| 416 | // Mapping from pointer equivalences to the representative node. -1 if we |
| 417 | // have no representative node for this pointer equivalence class yet. |
| 418 | std::vector<int> PEClass2Node; |
| 419 | // Mapping from pointer equivalences to representative node. This includes |
| 420 | // pointer equivalent but not location equivalent variables. -1 if we have |
| 421 | // no representative node for this pointer equivalence class yet. |
| 422 | std::vector<int> PENLEClass2Node; |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 423 | // Union/Find for HCD |
| 424 | std::vector<unsigned> HCDSCCRep; |
| 425 | // HCD's offline-detected cycles; "Statically DeTected" |
| 426 | // -1 if not part of such a cycle, otherwise a representative node. |
| 427 | std::vector<int> SDT; |
| 428 | // Whether to use SDT (UniteNodes can use it during solving, but not before) |
| 429 | bool SDTActive; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 430 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 431 | public: |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 432 | static char ID; |
| 433 | Andersens() : ModulePass((intptr_t)&ID) {} |
| 434 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 435 | bool runOnModule(Module &M) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 436 | InitializeAliasAnalysis(this); |
| 437 | IdentifyObjects(M); |
| 438 | CollectConstraints(M); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 439 | #undef DEBUG_TYPE |
| 440 | #define DEBUG_TYPE "anders-aa-constraints" |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 441 | DEBUG(PrintConstraints()); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 442 | #undef DEBUG_TYPE |
| 443 | #define DEBUG_TYPE "anders-aa" |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 444 | SolveConstraints(); |
| 445 | DEBUG(PrintPointsToGraph()); |
| 446 | |
| 447 | // Free the constraints list, as we don't need it to respond to alias |
| 448 | // requests. |
| 449 | ObjectNodes.clear(); |
| 450 | ReturnNodes.clear(); |
| 451 | VarargNodes.clear(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 452 | std::vector<Constraint>().swap(Constraints); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 453 | return false; |
| 454 | } |
| 455 | |
| 456 | void releaseMemory() { |
| 457 | // FIXME: Until we have transitively required passes working correctly, |
| 458 | // this cannot be enabled! Otherwise, using -count-aa with the pass |
| 459 | // causes memory to be freed too early. :( |
| 460 | #if 0 |
| 461 | // The memory objects and ValueNodes data structures at the only ones that |
| 462 | // are still live after construction. |
| 463 | std::vector<Node>().swap(GraphNodes); |
| 464 | ValueNodes.clear(); |
| 465 | #endif |
| 466 | } |
| 467 | |
| 468 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 469 | AliasAnalysis::getAnalysisUsage(AU); |
| 470 | AU.setPreservesAll(); // Does not transform code |
| 471 | } |
| 472 | |
| 473 | //------------------------------------------------ |
| 474 | // Implement the AliasAnalysis API |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 475 | // |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 476 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 477 | const Value *V2, unsigned V2Size); |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 478 | virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); |
| 479 | virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 480 | void getMustAliases(Value *P, std::vector<Value*> &RetVals); |
| 481 | bool pointsToConstantMemory(const Value *P); |
| 482 | |
| 483 | virtual void deleteValue(Value *V) { |
| 484 | ValueNodes.erase(V); |
| 485 | getAnalysis<AliasAnalysis>().deleteValue(V); |
| 486 | } |
| 487 | |
| 488 | virtual void copyValue(Value *From, Value *To) { |
| 489 | ValueNodes[To] = ValueNodes[From]; |
| 490 | getAnalysis<AliasAnalysis>().copyValue(From, To); |
| 491 | } |
| 492 | |
| 493 | private: |
| 494 | /// getNode - Return the node corresponding to the specified pointer scalar. |
| 495 | /// |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 496 | unsigned getNode(Value *V) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 497 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | df9b7bc | 2004-08-16 05:38:02 +0000 | [diff] [blame] | 498 | if (!isa<GlobalValue>(C)) |
| 499 | return getNodeForConstantPointer(C); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 500 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 501 | DenseMap<Value*, unsigned>::iterator I = ValueNodes.find(V); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 502 | if (I == ValueNodes.end()) { |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 503 | #ifndef NDEBUG |
| 504 | V->dump(); |
| 505 | #endif |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 506 | assert(0 && "Value does not have a node in the points-to graph!"); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 507 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 508 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 509 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 510 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 511 | /// getObject - Return the node corresponding to the memory object for the |
| 512 | /// specified global or allocation instruction. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 513 | unsigned getObject(Value *V) { |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 514 | DenseMap<Value*, unsigned>::iterator I = ObjectNodes.find(V); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 515 | assert(I != ObjectNodes.end() && |
| 516 | "Value does not have an object in the points-to graph!"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 517 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /// getReturnNode - Return the node representing the return value for the |
| 521 | /// specified function. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 522 | unsigned getReturnNode(Function *F) { |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 523 | DenseMap<Function*, unsigned>::iterator I = ReturnNodes.find(F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 524 | assert(I != ReturnNodes.end() && "Function does not return a value!"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 525 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /// getVarargNode - Return the node representing the variable arguments |
| 529 | /// formal for the specified function. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 530 | unsigned getVarargNode(Function *F) { |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 531 | DenseMap<Function*, unsigned>::iterator I = VarargNodes.find(F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 532 | assert(I != VarargNodes.end() && "Function does not take var args!"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 533 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /// getNodeValue - Get the node for the specified LLVM value and set the |
| 537 | /// value for it to be the specified value. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 538 | unsigned getNodeValue(Value &V) { |
| 539 | unsigned Index = getNode(&V); |
| 540 | GraphNodes[Index].setValue(&V); |
| 541 | return Index; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 544 | unsigned UniteNodes(unsigned First, unsigned Second, |
| 545 | bool UnionByRank = true); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 546 | unsigned FindNode(unsigned Node); |
| 547 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 548 | void IdentifyObjects(Module &M); |
| 549 | void CollectConstraints(Module &M); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 550 | bool AnalyzeUsesOfFunction(Value *); |
| 551 | void CreateConstraintGraph(); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 552 | void OptimizeConstraints(); |
| 553 | unsigned FindEquivalentNode(unsigned, unsigned); |
| 554 | void ClumpAddressTaken(); |
| 555 | void RewriteConstraints(); |
| 556 | void HU(); |
| 557 | void HVN(); |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 558 | void HCD(); |
| 559 | void Search(unsigned Node); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 560 | void UnitePointerEquivalences(); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 561 | void SolveConstraints(); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 562 | bool QueryNode(unsigned Node); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 563 | void Condense(unsigned Node); |
| 564 | void HUValNum(unsigned Node); |
| 565 | void HVNValNum(unsigned Node); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 566 | unsigned getNodeForConstantPointer(Constant *C); |
| 567 | unsigned getNodeForConstantPointerTarget(Constant *C); |
| 568 | void AddGlobalInitializerConstraints(unsigned, Constant *C); |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 569 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 570 | void AddConstraintsForNonInternalLinkage(Function *F); |
| 571 | void AddConstraintsForCall(CallSite CS, Function *F); |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 572 | bool AddConstraintsForExternalCall(CallSite CS, Function *F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 573 | |
| 574 | |
| 575 | void PrintNode(Node *N); |
| 576 | void PrintConstraints(); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 577 | void PrintConstraint(const Constraint &); |
| 578 | void PrintLabels(); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 579 | void PrintPointsToGraph(); |
| 580 | |
| 581 | //===------------------------------------------------------------------===// |
| 582 | // Instruction visitation methods for adding constraints |
| 583 | // |
| 584 | friend class InstVisitor<Andersens>; |
| 585 | void visitReturnInst(ReturnInst &RI); |
| 586 | void visitInvokeInst(InvokeInst &II) { visitCallSite(CallSite(&II)); } |
| 587 | void visitCallInst(CallInst &CI) { visitCallSite(CallSite(&CI)); } |
| 588 | void visitCallSite(CallSite CS); |
| 589 | void visitAllocationInst(AllocationInst &AI); |
| 590 | void visitLoadInst(LoadInst &LI); |
| 591 | void visitStoreInst(StoreInst &SI); |
| 592 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 593 | void visitPHINode(PHINode &PN); |
| 594 | void visitCastInst(CastInst &CI); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 595 | void visitICmpInst(ICmpInst &ICI) {} // NOOP! |
| 596 | void visitFCmpInst(FCmpInst &ICI) {} // NOOP! |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 597 | void visitSelectInst(SelectInst &SI); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 598 | void visitVAArg(VAArgInst &I); |
| 599 | void visitInstruction(Instruction &I); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 600 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 601 | }; |
| 602 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 603 | char Andersens::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 604 | RegisterPass<Andersens> X("anders-aa", |
| 605 | "Andersen's Interprocedural Alias Analysis"); |
Chris Lattner | a537017 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 606 | RegisterAnalysisGroup<AliasAnalysis> Y(X); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 607 | |
| 608 | // Initialize Timestamp Counter (static). |
| 609 | unsigned Andersens::Node::Counter = 0; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 612 | ModulePass *llvm::createAndersensPass() { return new Andersens(); } |
| 613 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 614 | //===----------------------------------------------------------------------===// |
| 615 | // AliasAnalysis Interface Implementation |
| 616 | //===----------------------------------------------------------------------===// |
| 617 | |
| 618 | AliasAnalysis::AliasResult Andersens::alias(const Value *V1, unsigned V1Size, |
| 619 | const Value *V2, unsigned V2Size) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 620 | Node *N1 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V1)))]; |
| 621 | Node *N2 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V2)))]; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 622 | |
| 623 | // Check to see if the two pointers are known to not alias. They don't alias |
| 624 | // if their points-to sets do not intersect. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 625 | if (!N1->intersectsIgnoring(N2, NullObject)) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 626 | return NoAlias; |
| 627 | |
| 628 | return AliasAnalysis::alias(V1, V1Size, V2, V2Size); |
| 629 | } |
| 630 | |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 631 | AliasAnalysis::ModRefResult |
| 632 | Andersens::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 633 | // The only thing useful that we can contribute for mod/ref information is |
| 634 | // when calling external function calls: if we know that memory never escapes |
| 635 | // from the program, it cannot be modified by an external call. |
| 636 | // |
| 637 | // NOTE: This is not really safe, at least not when the entire program is not |
| 638 | // available. The deal is that the external function could call back into the |
| 639 | // program and modify stuff. We ignore this technical niggle for now. This |
| 640 | // is, after all, a "research quality" implementation of Andersen's analysis. |
| 641 | if (Function *F = CS.getCalledFunction()) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 642 | if (F->isDeclaration()) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 643 | Node *N1 = &GraphNodes[FindNode(getNode(P))]; |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 644 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 645 | if (N1->PointsTo->empty()) |
| 646 | return NoModRef; |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 647 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 648 | if (!N1->PointsTo->test(UniversalSet)) |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 649 | return NoModRef; // P doesn't point to the universal set. |
| 650 | } |
| 651 | |
| 652 | return AliasAnalysis::getModRefInfo(CS, P, Size); |
| 653 | } |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 654 | |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 655 | AliasAnalysis::ModRefResult |
| 656 | Andersens::getModRefInfo(CallSite CS1, CallSite CS2) { |
| 657 | return AliasAnalysis::getModRefInfo(CS1,CS2); |
| 658 | } |
| 659 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 660 | /// getMustAlias - We can provide must alias information if we know that a |
| 661 | /// pointer can only point to a specific function or the null pointer. |
| 662 | /// Unfortunately we cannot determine must-alias information for global |
| 663 | /// variables or any other memory memory objects because we do not track whether |
| 664 | /// a pointer points to the beginning of an object or a field of it. |
| 665 | void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 666 | Node *N = &GraphNodes[FindNode(getNode(P))]; |
| 667 | if (N->PointsTo->count() == 1) { |
| 668 | Node *Pointee = &GraphNodes[N->PointsTo->find_first()]; |
| 669 | // If a function is the only object in the points-to set, then it must be |
| 670 | // the destination. Note that we can't handle global variables here, |
| 671 | // because we don't know if the pointer is actually pointing to a field of |
| 672 | // the global or to the beginning of it. |
| 673 | if (Value *V = Pointee->getValue()) { |
| 674 | if (Function *F = dyn_cast<Function>(V)) |
| 675 | RetVals.push_back(F); |
| 676 | } else { |
| 677 | // If the object in the points-to set is the null object, then the null |
| 678 | // pointer is a must alias. |
| 679 | if (Pointee == &GraphNodes[NullObject]) |
| 680 | RetVals.push_back(Constant::getNullValue(P->getType())); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 683 | AliasAnalysis::getMustAliases(P, RetVals); |
| 684 | } |
| 685 | |
| 686 | /// pointsToConstantMemory - If we can determine that this pointer only points |
| 687 | /// to constant memory, return true. In practice, this means that if the |
| 688 | /// pointer can only point to constant globals, functions, or the null pointer, |
| 689 | /// return true. |
| 690 | /// |
| 691 | bool Andersens::pointsToConstantMemory(const Value *P) { |
Dan Gohman | 6a551e7 | 2008-02-21 17:33:24 +0000 | [diff] [blame] | 692 | Node *N = &GraphNodes[FindNode(getNode(const_cast<Value*>(P)))]; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 693 | unsigned i; |
| 694 | |
| 695 | for (SparseBitVector<>::iterator bi = N->PointsTo->begin(); |
| 696 | bi != N->PointsTo->end(); |
| 697 | ++bi) { |
| 698 | i = *bi; |
| 699 | Node *Pointee = &GraphNodes[i]; |
| 700 | if (Value *V = Pointee->getValue()) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 701 | if (!isa<GlobalValue>(V) || (isa<GlobalVariable>(V) && |
| 702 | !cast<GlobalVariable>(V)->isConstant())) |
| 703 | return AliasAnalysis::pointsToConstantMemory(P); |
| 704 | } else { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 705 | if (i != NullObject) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 706 | return AliasAnalysis::pointsToConstantMemory(P); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | //===----------------------------------------------------------------------===// |
| 714 | // Object Identification Phase |
| 715 | //===----------------------------------------------------------------------===// |
| 716 | |
| 717 | /// IdentifyObjects - This stage scans the program, adding an entry to the |
| 718 | /// GraphNodes list for each memory object in the program (global stack or |
| 719 | /// heap), and populates the ValueNodes and ObjectNodes maps for these objects. |
| 720 | /// |
| 721 | void Andersens::IdentifyObjects(Module &M) { |
| 722 | unsigned NumObjects = 0; |
| 723 | |
| 724 | // Object #0 is always the universal set: the object that we don't know |
| 725 | // anything about. |
| 726 | assert(NumObjects == UniversalSet && "Something changed!"); |
| 727 | ++NumObjects; |
| 728 | |
| 729 | // Object #1 always represents the null pointer. |
| 730 | assert(NumObjects == NullPtr && "Something changed!"); |
| 731 | ++NumObjects; |
| 732 | |
| 733 | // Object #2 always represents the null object (the object pointed to by null) |
| 734 | assert(NumObjects == NullObject && "Something changed!"); |
| 735 | ++NumObjects; |
| 736 | |
| 737 | // Add all the globals first. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 738 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 739 | I != E; ++I) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 740 | ObjectNodes[I] = NumObjects++; |
| 741 | ValueNodes[I] = NumObjects++; |
| 742 | } |
| 743 | |
| 744 | // Add nodes for all of the functions and the instructions inside of them. |
| 745 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 746 | // The function itself is a memory object. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 747 | unsigned First = NumObjects; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 748 | ValueNodes[F] = NumObjects++; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 749 | if (isa<PointerType>(F->getFunctionType()->getReturnType())) |
| 750 | ReturnNodes[F] = NumObjects++; |
| 751 | if (F->getFunctionType()->isVarArg()) |
| 752 | VarargNodes[F] = NumObjects++; |
| 753 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 754 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 755 | // Add nodes for all of the incoming pointer arguments. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 756 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 757 | I != E; ++I) |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 758 | { |
| 759 | if (isa<PointerType>(I->getType())) |
| 760 | ValueNodes[I] = NumObjects++; |
| 761 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 762 | MaxK[First] = NumObjects - First; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 763 | |
| 764 | // Scan the function body, creating a memory object for each heap/stack |
| 765 | // allocation in the body of the function and a node to represent all |
| 766 | // pointer values defined by instructions and used as operands. |
| 767 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 768 | // If this is an heap or stack allocation, create a node for the memory |
| 769 | // object. |
| 770 | if (isa<PointerType>(II->getType())) { |
| 771 | ValueNodes[&*II] = NumObjects++; |
| 772 | if (AllocationInst *AI = dyn_cast<AllocationInst>(&*II)) |
| 773 | ObjectNodes[AI] = NumObjects++; |
| 774 | } |
Nick Lewycky | 4ac0e8d | 2007-11-22 03:07:37 +0000 | [diff] [blame] | 775 | |
| 776 | // Calls to inline asm need to be added as well because the callee isn't |
| 777 | // referenced anywhere else. |
| 778 | if (CallInst *CI = dyn_cast<CallInst>(&*II)) { |
| 779 | Value *Callee = CI->getCalledValue(); |
| 780 | if (isa<InlineAsm>(Callee)) |
| 781 | ValueNodes[Callee] = NumObjects++; |
| 782 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
| 786 | // Now that we know how many objects to create, make them all now! |
| 787 | GraphNodes.resize(NumObjects); |
| 788 | NumNodes += NumObjects; |
| 789 | } |
| 790 | |
| 791 | //===----------------------------------------------------------------------===// |
| 792 | // Constraint Identification Phase |
| 793 | //===----------------------------------------------------------------------===// |
| 794 | |
| 795 | /// getNodeForConstantPointer - Return the node corresponding to the constant |
| 796 | /// pointer itself. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 797 | unsigned Andersens::getNodeForConstantPointer(Constant *C) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 798 | assert(isa<PointerType>(C->getType()) && "Not a constant pointer!"); |
| 799 | |
Chris Lattner | 267a1b0 | 2005-03-27 18:58:23 +0000 | [diff] [blame] | 800 | if (isa<ConstantPointerNull>(C) || isa<UndefValue>(C)) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 801 | return NullPtr; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 802 | else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 803 | return getNode(GV); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 804 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 805 | switch (CE->getOpcode()) { |
| 806 | case Instruction::GetElementPtr: |
| 807 | return getNodeForConstantPointer(CE->getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 808 | case Instruction::IntToPtr: |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 809 | return UniversalSet; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 810 | case Instruction::BitCast: |
| 811 | return getNodeForConstantPointer(CE->getOperand(0)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 812 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 813 | cerr << "Constant Expr not yet handled: " << *CE << "\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 814 | assert(0); |
| 815 | } |
| 816 | } else { |
| 817 | assert(0 && "Unknown constant pointer!"); |
| 818 | } |
Chris Lattner | 1fc3739 | 2004-05-27 20:57:01 +0000 | [diff] [blame] | 819 | return 0; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /// getNodeForConstantPointerTarget - Return the node POINTED TO by the |
| 823 | /// specified constant pointer. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 824 | unsigned Andersens::getNodeForConstantPointerTarget(Constant *C) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 825 | assert(isa<PointerType>(C->getType()) && "Not a constant pointer!"); |
| 826 | |
| 827 | if (isa<ConstantPointerNull>(C)) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 828 | return NullObject; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 829 | else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 830 | return getObject(GV); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 831 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 832 | switch (CE->getOpcode()) { |
| 833 | case Instruction::GetElementPtr: |
| 834 | return getNodeForConstantPointerTarget(CE->getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 835 | case Instruction::IntToPtr: |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 836 | return UniversalSet; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 837 | case Instruction::BitCast: |
| 838 | return getNodeForConstantPointerTarget(CE->getOperand(0)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 839 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 840 | cerr << "Constant Expr not yet handled: " << *CE << "\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 841 | assert(0); |
| 842 | } |
| 843 | } else { |
| 844 | assert(0 && "Unknown constant pointer!"); |
| 845 | } |
Chris Lattner | 1fc3739 | 2004-05-27 20:57:01 +0000 | [diff] [blame] | 846 | return 0; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /// AddGlobalInitializerConstraints - Add inclusion constraints for the memory |
| 850 | /// object N, which contains values indicated by C. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 851 | void Andersens::AddGlobalInitializerConstraints(unsigned NodeIndex, |
| 852 | Constant *C) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 853 | if (C->getType()->isFirstClassType()) { |
| 854 | if (isa<PointerType>(C->getType())) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 855 | Constraints.push_back(Constraint(Constraint::Copy, NodeIndex, |
| 856 | getNodeForConstantPointer(C))); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 857 | } else if (C->isNullValue()) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 858 | Constraints.push_back(Constraint(Constraint::Copy, NodeIndex, |
| 859 | NullObject)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 860 | return; |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 861 | } else if (!isa<UndefValue>(C)) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 862 | // If this is an array or struct, include constraints for each element. |
| 863 | assert(isa<ConstantArray>(C) || isa<ConstantStruct>(C)); |
| 864 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 865 | AddGlobalInitializerConstraints(NodeIndex, |
| 866 | cast<Constant>(C->getOperand(i))); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 870 | /// AddConstraintsForNonInternalLinkage - If this function does not have |
| 871 | /// internal linkage, realize that we can't trust anything passed into or |
| 872 | /// returned by this function. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 873 | void Andersens::AddConstraintsForNonInternalLinkage(Function *F) { |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 874 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 875 | if (isa<PointerType>(I->getType())) |
| 876 | // If this is an argument of an externally accessible function, the |
| 877 | // incoming pointer might point to anything. |
| 878 | Constraints.push_back(Constraint(Constraint::Copy, getNode(I), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 879 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 882 | /// AddConstraintsForCall - If this is a call to a "known" function, add the |
| 883 | /// constraints and return true. If this is a call to an unknown function, |
| 884 | /// return false. |
| 885 | bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 886 | assert(F->isDeclaration() && "Not an external function!"); |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 887 | |
| 888 | // These functions don't induce any points-to constraints. |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 889 | if (F->getName() == "atoi" || F->getName() == "atof" || |
| 890 | F->getName() == "atol" || F->getName() == "atoll" || |
| 891 | F->getName() == "remove" || F->getName() == "unlink" || |
| 892 | F->getName() == "rename" || F->getName() == "memcmp" || |
Chris Lattner | 01ac91e | 2006-03-03 01:21:36 +0000 | [diff] [blame] | 893 | F->getName() == "llvm.memset.i32" || |
| 894 | F->getName() == "llvm.memset.i64" || |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 895 | F->getName() == "strcmp" || F->getName() == "strncmp" || |
| 896 | F->getName() == "execl" || F->getName() == "execlp" || |
| 897 | F->getName() == "execle" || F->getName() == "execv" || |
| 898 | F->getName() == "execvp" || F->getName() == "chmod" || |
| 899 | F->getName() == "puts" || F->getName() == "write" || |
| 900 | F->getName() == "open" || F->getName() == "create" || |
| 901 | F->getName() == "truncate" || F->getName() == "chdir" || |
| 902 | F->getName() == "mkdir" || F->getName() == "rmdir" || |
| 903 | F->getName() == "read" || F->getName() == "pipe" || |
| 904 | F->getName() == "wait" || F->getName() == "time" || |
| 905 | F->getName() == "stat" || F->getName() == "fstat" || |
| 906 | F->getName() == "lstat" || F->getName() == "strtod" || |
| 907 | F->getName() == "strtof" || F->getName() == "strtold" || |
| 908 | F->getName() == "fopen" || F->getName() == "fdopen" || |
| 909 | F->getName() == "freopen" || |
| 910 | F->getName() == "fflush" || F->getName() == "feof" || |
| 911 | F->getName() == "fileno" || F->getName() == "clearerr" || |
| 912 | F->getName() == "rewind" || F->getName() == "ftell" || |
| 913 | F->getName() == "ferror" || F->getName() == "fgetc" || |
| 914 | F->getName() == "fgetc" || F->getName() == "_IO_getc" || |
| 915 | F->getName() == "fwrite" || F->getName() == "fread" || |
| 916 | F->getName() == "fgets" || F->getName() == "ungetc" || |
| 917 | F->getName() == "fputc" || |
| 918 | F->getName() == "fputs" || F->getName() == "putc" || |
| 919 | F->getName() == "ftell" || F->getName() == "rewind" || |
| 920 | F->getName() == "_IO_putc" || F->getName() == "fseek" || |
| 921 | F->getName() == "fgetpos" || F->getName() == "fsetpos" || |
| 922 | F->getName() == "printf" || F->getName() == "fprintf" || |
| 923 | F->getName() == "sprintf" || F->getName() == "vprintf" || |
| 924 | F->getName() == "vfprintf" || F->getName() == "vsprintf" || |
| 925 | F->getName() == "scanf" || F->getName() == "fscanf" || |
| 926 | F->getName() == "sscanf" || F->getName() == "__assert_fail" || |
| 927 | F->getName() == "modf") |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 928 | return true; |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 929 | |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 930 | |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 931 | // These functions do induce points-to edges. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 932 | if (F->getName() == "llvm.memcpy.i32" || F->getName() == "llvm.memcpy.i64" || |
Chris Lattner | 01ac91e | 2006-03-03 01:21:36 +0000 | [diff] [blame] | 933 | F->getName() == "llvm.memmove.i32" ||F->getName() == "llvm.memmove.i64" || |
Chris Lattner | 4de57fd | 2005-03-29 06:52:20 +0000 | [diff] [blame] | 934 | F->getName() == "memmove") { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 935 | |
| 936 | // *Dest = *Src, which requires an artificial graph node to represent the |
| 937 | // constraint. It is broken up into *Dest = temp, temp = *Src |
| 938 | unsigned FirstArg = getNode(CS.getArgument(0)); |
| 939 | unsigned SecondArg = getNode(CS.getArgument(1)); |
| 940 | unsigned TempArg = GraphNodes.size(); |
| 941 | GraphNodes.push_back(Node()); |
| 942 | Constraints.push_back(Constraint(Constraint::Store, |
| 943 | FirstArg, TempArg)); |
| 944 | Constraints.push_back(Constraint(Constraint::Load, |
| 945 | TempArg, SecondArg)); |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 946 | return true; |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Chris Lattner | 77b5056 | 2005-03-29 20:04:24 +0000 | [diff] [blame] | 949 | // Result = Arg0 |
| 950 | if (F->getName() == "realloc" || F->getName() == "strchr" || |
| 951 | F->getName() == "strrchr" || F->getName() == "strstr" || |
| 952 | F->getName() == "strtok") { |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 953 | Constraints.push_back(Constraint(Constraint::Copy, |
| 954 | getNode(CS.getInstruction()), |
| 955 | getNode(CS.getArgument(0)))); |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | return false; |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 963 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 964 | /// AnalyzeUsesOfFunction - Look at all of the users of the specified function. |
| 965 | /// If this is used by anything complex (i.e., the address escapes), return |
| 966 | /// true. |
| 967 | bool Andersens::AnalyzeUsesOfFunction(Value *V) { |
| 968 | |
| 969 | if (!isa<PointerType>(V->getType())) return true; |
| 970 | |
| 971 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 972 | if (dyn_cast<LoadInst>(*UI)) { |
| 973 | return false; |
| 974 | } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 975 | if (V == SI->getOperand(1)) { |
| 976 | return false; |
| 977 | } else if (SI->getOperand(1)) { |
| 978 | return true; // Storing the pointer |
| 979 | } |
| 980 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { |
| 981 | if (AnalyzeUsesOfFunction(GEP)) return true; |
| 982 | } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 983 | // Make sure that this is just the function being called, not that it is |
| 984 | // passing into the function. |
| 985 | for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) |
| 986 | if (CI->getOperand(i) == V) return true; |
| 987 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
| 988 | // Make sure that this is just the function being called, not that it is |
| 989 | // passing into the function. |
| 990 | for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i) |
| 991 | if (II->getOperand(i) == V) return true; |
| 992 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { |
| 993 | if (CE->getOpcode() == Instruction::GetElementPtr || |
| 994 | CE->getOpcode() == Instruction::BitCast) { |
| 995 | if (AnalyzeUsesOfFunction(CE)) |
| 996 | return true; |
| 997 | } else { |
| 998 | return true; |
| 999 | } |
| 1000 | } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) { |
| 1001 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
| 1002 | return true; // Allow comparison against null. |
| 1003 | } else if (dyn_cast<FreeInst>(*UI)) { |
| 1004 | return false; |
| 1005 | } else { |
| 1006 | return true; |
| 1007 | } |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1011 | /// CollectConstraints - This stage scans the program, adding a constraint to |
| 1012 | /// the Constraints list for each instruction in the program that induces a |
| 1013 | /// constraint, and setting up the initial points-to graph. |
| 1014 | /// |
| 1015 | void Andersens::CollectConstraints(Module &M) { |
| 1016 | // First, the universal set points to itself. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1017 | Constraints.push_back(Constraint(Constraint::AddressOf, UniversalSet, |
| 1018 | UniversalSet)); |
| 1019 | Constraints.push_back(Constraint(Constraint::Store, UniversalSet, |
| 1020 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1021 | |
| 1022 | // Next, the null pointer points to the null object. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1023 | Constraints.push_back(Constraint(Constraint::AddressOf, NullPtr, NullObject)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Next, add any constraints on global variables and their initializers. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 1026 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 1027 | I != E; ++I) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1028 | // Associate the address of the global object as pointing to the memory for |
| 1029 | // the global: &G = <G memory> |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1030 | unsigned ObjectIndex = getObject(I); |
| 1031 | Node *Object = &GraphNodes[ObjectIndex]; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1032 | Object->setValue(I); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1033 | Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(*I), |
| 1034 | ObjectIndex)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1035 | |
| 1036 | if (I->hasInitializer()) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1037 | AddGlobalInitializerConstraints(ObjectIndex, I->getInitializer()); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1038 | } else { |
| 1039 | // If it doesn't have an initializer (i.e. it's defined in another |
| 1040 | // translation unit), it points to the universal set. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1041 | Constraints.push_back(Constraint(Constraint::Copy, ObjectIndex, |
| 1042 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1043 | } |
| 1044 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1046 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1047 | // Set up the return value node. |
| 1048 | if (isa<PointerType>(F->getFunctionType()->getReturnType())) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1049 | GraphNodes[getReturnNode(F)].setValue(F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1050 | if (F->getFunctionType()->isVarArg()) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1051 | GraphNodes[getVarargNode(F)].setValue(F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1052 | |
| 1053 | // Set up incoming argument nodes. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 1054 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1055 | I != E; ++I) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1056 | if (isa<PointerType>(I->getType())) |
| 1057 | getNodeValue(*I); |
| 1058 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1059 | // At some point we should just add constraints for the escaping functions |
| 1060 | // at solve time, but this slows down solving. For now, we simply mark |
| 1061 | // address taken functions as escaping and treat them as external. |
| 1062 | if (!F->hasInternalLinkage() || AnalyzeUsesOfFunction(F)) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1063 | AddConstraintsForNonInternalLinkage(F); |
| 1064 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1065 | if (!F->isDeclaration()) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1066 | // Scan the function body, creating a memory object for each heap/stack |
| 1067 | // allocation in the body of the function and a node to represent all |
| 1068 | // pointer values defined by instructions and used as operands. |
| 1069 | visit(F); |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 1070 | } else { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1071 | // External functions that return pointers return the universal set. |
| 1072 | if (isa<PointerType>(F->getFunctionType()->getReturnType())) |
| 1073 | Constraints.push_back(Constraint(Constraint::Copy, |
| 1074 | getReturnNode(F), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1075 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Any pointers that are passed into the function have the universal set |
| 1078 | // stored into them. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 1079 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1080 | I != E; ++I) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1081 | if (isa<PointerType>(I->getType())) { |
| 1082 | // Pointers passed into external functions could have anything stored |
| 1083 | // through them. |
| 1084 | Constraints.push_back(Constraint(Constraint::Store, getNode(I), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1085 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1086 | // Memory objects passed into external function calls can have the |
| 1087 | // universal set point to them. |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 1088 | #if FULL_UNIVERSAL |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1089 | Constraints.push_back(Constraint(Constraint::Copy, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1090 | UniversalSet, |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1091 | getNode(I))); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 1092 | #else |
| 1093 | Constraints.push_back(Constraint(Constraint::Copy, |
| 1094 | getNode(I), |
| 1095 | UniversalSet)); |
| 1096 | #endif |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | // If this is an external varargs function, it can also store pointers |
| 1100 | // into any pointers passed through the varargs section. |
| 1101 | if (F->getFunctionType()->isVarArg()) |
| 1102 | Constraints.push_back(Constraint(Constraint::Store, getVarargNode(F), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1103 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | NumConstraints += Constraints.size(); |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | void Andersens::visitInstruction(Instruction &I) { |
| 1111 | #ifdef NDEBUG |
| 1112 | return; // This function is just a big assert. |
| 1113 | #endif |
| 1114 | if (isa<BinaryOperator>(I)) |
| 1115 | return; |
| 1116 | // Most instructions don't have any effect on pointer values. |
| 1117 | switch (I.getOpcode()) { |
| 1118 | case Instruction::Br: |
| 1119 | case Instruction::Switch: |
| 1120 | case Instruction::Unwind: |
Chris Lattner | c17edbd | 2004-10-16 18:16:19 +0000 | [diff] [blame] | 1121 | case Instruction::Unreachable: |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1122 | case Instruction::Free: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1123 | case Instruction::ICmp: |
| 1124 | case Instruction::FCmp: |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | default: |
| 1127 | // Is this something we aren't handling yet? |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1128 | cerr << "Unknown instruction: " << I; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1129 | abort(); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | void Andersens::visitAllocationInst(AllocationInst &AI) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1134 | unsigned ObjectIndex = getObject(&AI); |
| 1135 | GraphNodes[ObjectIndex].setValue(&AI); |
| 1136 | Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(AI), |
| 1137 | ObjectIndex)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | void Andersens::visitReturnInst(ReturnInst &RI) { |
| 1141 | if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType())) |
| 1142 | // return V --> <Copy/retval{F}/v> |
| 1143 | Constraints.push_back(Constraint(Constraint::Copy, |
| 1144 | getReturnNode(RI.getParent()->getParent()), |
| 1145 | getNode(RI.getOperand(0)))); |
| 1146 | } |
| 1147 | |
| 1148 | void Andersens::visitLoadInst(LoadInst &LI) { |
| 1149 | if (isa<PointerType>(LI.getType())) |
| 1150 | // P1 = load P2 --> <Load/P1/P2> |
| 1151 | Constraints.push_back(Constraint(Constraint::Load, getNodeValue(LI), |
| 1152 | getNode(LI.getOperand(0)))); |
| 1153 | } |
| 1154 | |
| 1155 | void Andersens::visitStoreInst(StoreInst &SI) { |
| 1156 | if (isa<PointerType>(SI.getOperand(0)->getType())) |
| 1157 | // store P1, P2 --> <Store/P2/P1> |
| 1158 | Constraints.push_back(Constraint(Constraint::Store, |
| 1159 | getNode(SI.getOperand(1)), |
| 1160 | getNode(SI.getOperand(0)))); |
| 1161 | } |
| 1162 | |
| 1163 | void Andersens::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
| 1164 | // P1 = getelementptr P2, ... --> <Copy/P1/P2> |
| 1165 | Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(GEP), |
| 1166 | getNode(GEP.getOperand(0)))); |
| 1167 | } |
| 1168 | |
| 1169 | void Andersens::visitPHINode(PHINode &PN) { |
| 1170 | if (isa<PointerType>(PN.getType())) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1171 | unsigned PNN = getNodeValue(PN); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1172 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 1173 | // P1 = phi P2, P3 --> <Copy/P1/P2>, <Copy/P1/P3>, ... |
| 1174 | Constraints.push_back(Constraint(Constraint::Copy, PNN, |
| 1175 | getNode(PN.getIncomingValue(i)))); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | void Andersens::visitCastInst(CastInst &CI) { |
| 1180 | Value *Op = CI.getOperand(0); |
| 1181 | if (isa<PointerType>(CI.getType())) { |
| 1182 | if (isa<PointerType>(Op->getType())) { |
| 1183 | // P1 = cast P2 --> <Copy/P1/P2> |
| 1184 | Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI), |
| 1185 | getNode(CI.getOperand(0)))); |
| 1186 | } else { |
| 1187 | // P1 = cast int --> <Copy/P1/Univ> |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 1188 | #if 0 |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1189 | Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1190 | UniversalSet)); |
Chris Lattner | bd135c7 | 2005-04-05 01:12:03 +0000 | [diff] [blame] | 1191 | #else |
| 1192 | getNodeValue(CI); |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 1193 | #endif |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } else if (isa<PointerType>(Op->getType())) { |
| 1196 | // int = cast P1 --> <Copy/Univ/P1> |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 1197 | #if 0 |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1198 | Constraints.push_back(Constraint(Constraint::Copy, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1199 | UniversalSet, |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1200 | getNode(CI.getOperand(0)))); |
Chris Lattner | bd135c7 | 2005-04-05 01:12:03 +0000 | [diff] [blame] | 1201 | #else |
| 1202 | getNode(CI.getOperand(0)); |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 1203 | #endif |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | void Andersens::visitSelectInst(SelectInst &SI) { |
| 1208 | if (isa<PointerType>(SI.getType())) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1209 | unsigned SIN = getNodeValue(SI); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1210 | // P1 = select C, P2, P3 ---> <Copy/P1/P2>, <Copy/P1/P3> |
| 1211 | Constraints.push_back(Constraint(Constraint::Copy, SIN, |
| 1212 | getNode(SI.getOperand(1)))); |
| 1213 | Constraints.push_back(Constraint(Constraint::Copy, SIN, |
| 1214 | getNode(SI.getOperand(2)))); |
| 1215 | } |
| 1216 | } |
| 1217 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1218 | void Andersens::visitVAArg(VAArgInst &I) { |
| 1219 | assert(0 && "vaarg not handled yet!"); |
| 1220 | } |
| 1221 | |
| 1222 | /// AddConstraintsForCall - Add constraints for a call with actual arguments |
| 1223 | /// specified by CS to the function specified by F. Note that the types of |
| 1224 | /// arguments might not match up in the case where this is an indirect call and |
| 1225 | /// the function pointer has been casted. If this is the case, do something |
| 1226 | /// reasonable. |
| 1227 | void Andersens::AddConstraintsForCall(CallSite CS, Function *F) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1228 | Value *CallValue = CS.getCalledValue(); |
| 1229 | bool IsDeref = F == NULL; |
| 1230 | |
| 1231 | // If this is a call to an external function, try to handle it directly to get |
| 1232 | // some taste of context sensitivity. |
| 1233 | if (F && F->isDeclaration() && AddConstraintsForExternalCall(CS, F)) |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 1234 | return; |
| 1235 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1236 | if (isa<PointerType>(CS.getType())) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1237 | unsigned CSN = getNode(CS.getInstruction()); |
| 1238 | if (!F || isa<PointerType>(F->getFunctionType()->getReturnType())) { |
| 1239 | if (IsDeref) |
| 1240 | Constraints.push_back(Constraint(Constraint::Load, CSN, |
| 1241 | getNode(CallValue), CallReturnPos)); |
| 1242 | else |
| 1243 | Constraints.push_back(Constraint(Constraint::Copy, CSN, |
| 1244 | getNode(CallValue) + CallReturnPos)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1245 | } else { |
| 1246 | // If the function returns a non-pointer value, handle this just like we |
| 1247 | // treat a nonpointer cast to pointer. |
| 1248 | Constraints.push_back(Constraint(Constraint::Copy, CSN, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1249 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1250 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1251 | } else if (F && isa<PointerType>(F->getFunctionType()->getReturnType())) { |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 1252 | #if FULL_UNIVERSAL |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1253 | Constraints.push_back(Constraint(Constraint::Copy, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1254 | UniversalSet, |
| 1255 | getNode(CallValue) + CallReturnPos)); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 1256 | #else |
| 1257 | Constraints.push_back(Constraint(Constraint::Copy, |
| 1258 | getNode(CallValue) + CallReturnPos, |
| 1259 | UniversalSet)); |
| 1260 | #endif |
| 1261 | |
| 1262 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1263 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1265 | CallSite::arg_iterator ArgI = CS.arg_begin(), ArgE = CS.arg_end(); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1266 | if (F) { |
| 1267 | // Direct Call |
| 1268 | Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 1269 | for (; AI != AE && ArgI != ArgE; ++AI, ++ArgI) |
| 1270 | if (isa<PointerType>(AI->getType())) { |
| 1271 | if (isa<PointerType>((*ArgI)->getType())) { |
| 1272 | // Copy the actual argument into the formal argument. |
| 1273 | Constraints.push_back(Constraint(Constraint::Copy, getNode(AI), |
| 1274 | getNode(*ArgI))); |
| 1275 | } else { |
| 1276 | Constraints.push_back(Constraint(Constraint::Copy, getNode(AI), |
| 1277 | UniversalSet)); |
| 1278 | } |
| 1279 | } else if (isa<PointerType>((*ArgI)->getType())) { |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 1280 | #if FULL_UNIVERSAL |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1281 | Constraints.push_back(Constraint(Constraint::Copy, |
| 1282 | UniversalSet, |
| 1283 | getNode(*ArgI))); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 1284 | #else |
| 1285 | Constraints.push_back(Constraint(Constraint::Copy, |
| 1286 | getNode(*ArgI), |
| 1287 | UniversalSet)); |
| 1288 | #endif |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1289 | } |
| 1290 | } else { |
| 1291 | //Indirect Call |
| 1292 | unsigned ArgPos = CallFirstArgPos; |
| 1293 | for (; ArgI != ArgE; ++ArgI) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1294 | if (isa<PointerType>((*ArgI)->getType())) { |
| 1295 | // Copy the actual argument into the formal argument. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1296 | Constraints.push_back(Constraint(Constraint::Store, |
| 1297 | getNode(CallValue), |
| 1298 | getNode(*ArgI), ArgPos++)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1299 | } else { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1300 | Constraints.push_back(Constraint(Constraint::Store, |
| 1301 | getNode (CallValue), |
| 1302 | UniversalSet, ArgPos++)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1303 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1304 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1305 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1306 | // Copy all pointers passed through the varargs section to the varargs node. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1307 | if (F && F->getFunctionType()->isVarArg()) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1308 | for (; ArgI != ArgE; ++ArgI) |
| 1309 | if (isa<PointerType>((*ArgI)->getType())) |
| 1310 | Constraints.push_back(Constraint(Constraint::Copy, getVarargNode(F), |
| 1311 | getNode(*ArgI))); |
| 1312 | // If more arguments are passed in than we track, just drop them on the floor. |
| 1313 | } |
| 1314 | |
| 1315 | void Andersens::visitCallSite(CallSite CS) { |
| 1316 | if (isa<PointerType>(CS.getType())) |
| 1317 | getNodeValue(*CS.getInstruction()); |
| 1318 | |
| 1319 | if (Function *F = CS.getCalledFunction()) { |
| 1320 | AddConstraintsForCall(CS, F); |
| 1321 | } else { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1322 | AddConstraintsForCall(CS, NULL); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | //===----------------------------------------------------------------------===// |
| 1327 | // Constraint Solving Phase |
| 1328 | //===----------------------------------------------------------------------===// |
| 1329 | |
| 1330 | /// intersects - Return true if the points-to set of this node intersects |
| 1331 | /// with the points-to set of the specified node. |
| 1332 | bool Andersens::Node::intersects(Node *N) const { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1333 | return PointsTo->intersects(N->PointsTo); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /// intersectsIgnoring - Return true if the points-to set of this node |
| 1337 | /// intersects with the points-to set of the specified node on any nodes |
| 1338 | /// except for the specified node to ignore. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1339 | bool Andersens::Node::intersectsIgnoring(Node *N, unsigned Ignoring) const { |
| 1340 | // TODO: If we are only going to call this with the same value for Ignoring, |
| 1341 | // we should move the special values out of the points-to bitmap. |
| 1342 | bool WeHadIt = PointsTo->test(Ignoring); |
| 1343 | bool NHadIt = N->PointsTo->test(Ignoring); |
| 1344 | bool Result = false; |
| 1345 | if (WeHadIt) |
| 1346 | PointsTo->reset(Ignoring); |
| 1347 | if (NHadIt) |
| 1348 | N->PointsTo->reset(Ignoring); |
| 1349 | Result = PointsTo->intersects(N->PointsTo); |
| 1350 | if (WeHadIt) |
| 1351 | PointsTo->set(Ignoring); |
| 1352 | if (NHadIt) |
| 1353 | N->PointsTo->set(Ignoring); |
| 1354 | return Result; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1357 | void dumpToDOUT(SparseBitVector<> *bitmap) { |
Bill Wendling | cab5f5d | 2007-09-24 22:43:48 +0000 | [diff] [blame] | 1358 | #ifndef NDEBUG |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1359 | dump(*bitmap, DOUT); |
Bill Wendling | cab5f5d | 2007-09-24 22:43:48 +0000 | [diff] [blame] | 1360 | #endif |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | |
| 1364 | /// Clump together address taken variables so that the points-to sets use up |
| 1365 | /// less space and can be operated on faster. |
| 1366 | |
| 1367 | void Andersens::ClumpAddressTaken() { |
| 1368 | #undef DEBUG_TYPE |
| 1369 | #define DEBUG_TYPE "anders-aa-renumber" |
| 1370 | std::vector<unsigned> Translate; |
| 1371 | std::vector<Node> NewGraphNodes; |
| 1372 | |
| 1373 | Translate.resize(GraphNodes.size()); |
| 1374 | unsigned NewPos = 0; |
| 1375 | |
| 1376 | for (unsigned i = 0; i < Constraints.size(); ++i) { |
| 1377 | Constraint &C = Constraints[i]; |
| 1378 | if (C.Type == Constraint::AddressOf) { |
| 1379 | GraphNodes[C.Src].AddressTaken = true; |
| 1380 | } |
| 1381 | } |
| 1382 | for (unsigned i = 0; i < NumberSpecialNodes; ++i) { |
| 1383 | unsigned Pos = NewPos++; |
| 1384 | Translate[i] = Pos; |
| 1385 | NewGraphNodes.push_back(GraphNodes[i]); |
| 1386 | DOUT << "Renumbering node " << i << " to node " << Pos << "\n"; |
| 1387 | } |
| 1388 | |
| 1389 | // I believe this ends up being faster than making two vectors and splicing |
| 1390 | // them. |
| 1391 | for (unsigned i = NumberSpecialNodes; i < GraphNodes.size(); ++i) { |
| 1392 | if (GraphNodes[i].AddressTaken) { |
| 1393 | unsigned Pos = NewPos++; |
| 1394 | Translate[i] = Pos; |
| 1395 | NewGraphNodes.push_back(GraphNodes[i]); |
| 1396 | DOUT << "Renumbering node " << i << " to node " << Pos << "\n"; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | for (unsigned i = NumberSpecialNodes; i < GraphNodes.size(); ++i) { |
| 1401 | if (!GraphNodes[i].AddressTaken) { |
| 1402 | unsigned Pos = NewPos++; |
| 1403 | Translate[i] = Pos; |
| 1404 | NewGraphNodes.push_back(GraphNodes[i]); |
| 1405 | DOUT << "Renumbering node " << i << " to node " << Pos << "\n"; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | for (DenseMap<Value*, unsigned>::iterator Iter = ValueNodes.begin(); |
| 1410 | Iter != ValueNodes.end(); |
| 1411 | ++Iter) |
| 1412 | Iter->second = Translate[Iter->second]; |
| 1413 | |
| 1414 | for (DenseMap<Value*, unsigned>::iterator Iter = ObjectNodes.begin(); |
| 1415 | Iter != ObjectNodes.end(); |
| 1416 | ++Iter) |
| 1417 | Iter->second = Translate[Iter->second]; |
| 1418 | |
| 1419 | for (DenseMap<Function*, unsigned>::iterator Iter = ReturnNodes.begin(); |
| 1420 | Iter != ReturnNodes.end(); |
| 1421 | ++Iter) |
| 1422 | Iter->second = Translate[Iter->second]; |
| 1423 | |
| 1424 | for (DenseMap<Function*, unsigned>::iterator Iter = VarargNodes.begin(); |
| 1425 | Iter != VarargNodes.end(); |
| 1426 | ++Iter) |
| 1427 | Iter->second = Translate[Iter->second]; |
| 1428 | |
| 1429 | for (unsigned i = 0; i < Constraints.size(); ++i) { |
| 1430 | Constraint &C = Constraints[i]; |
| 1431 | C.Src = Translate[C.Src]; |
| 1432 | C.Dest = Translate[C.Dest]; |
| 1433 | } |
| 1434 | |
| 1435 | GraphNodes.swap(NewGraphNodes); |
| 1436 | #undef DEBUG_TYPE |
| 1437 | #define DEBUG_TYPE "anders-aa" |
| 1438 | } |
| 1439 | |
| 1440 | /// The technique used here is described in "Exploiting Pointer and Location |
| 1441 | /// Equivalence to Optimize Pointer Analysis. In the 14th International Static |
| 1442 | /// Analysis Symposium (SAS), August 2007." It is known as the "HVN" algorithm, |
| 1443 | /// and is equivalent to value numbering the collapsed constraint graph without |
| 1444 | /// evaluating unions. This is used as a pre-pass to HU in order to resolve |
| 1445 | /// first order pointer dereferences and speed up/reduce memory usage of HU. |
| 1446 | /// Running both is equivalent to HRU without the iteration |
| 1447 | /// HVN in more detail: |
| 1448 | /// Imagine the set of constraints was simply straight line code with no loops |
| 1449 | /// (we eliminate cycles, so there are no loops), such as: |
| 1450 | /// E = &D |
| 1451 | /// E = &C |
| 1452 | /// E = F |
| 1453 | /// F = G |
| 1454 | /// G = F |
| 1455 | /// Applying value numbering to this code tells us: |
| 1456 | /// G == F == E |
| 1457 | /// |
| 1458 | /// For HVN, this is as far as it goes. We assign new value numbers to every |
| 1459 | /// "address node", and every "reference node". |
| 1460 | /// To get the optimal result for this, we use a DFS + SCC (since all nodes in a |
| 1461 | /// cycle must have the same value number since the = operation is really |
| 1462 | /// inclusion, not overwrite), and value number nodes we receive points-to sets |
| 1463 | /// before we value our own node. |
| 1464 | /// The advantage of HU over HVN is that HU considers the inclusion property, so |
| 1465 | /// that if you have |
| 1466 | /// E = &D |
| 1467 | /// E = &C |
| 1468 | /// E = F |
| 1469 | /// F = G |
| 1470 | /// F = &D |
| 1471 | /// G = F |
| 1472 | /// HU will determine that G == F == E. HVN will not, because it cannot prove |
| 1473 | /// that the points to information ends up being the same because they all |
| 1474 | /// receive &D from E anyway. |
| 1475 | |
| 1476 | void Andersens::HVN() { |
| 1477 | DOUT << "Beginning HVN\n"; |
| 1478 | // Build a predecessor graph. This is like our constraint graph with the |
| 1479 | // edges going in the opposite direction, and there are edges for all the |
| 1480 | // constraints, instead of just copy constraints. We also build implicit |
| 1481 | // edges for constraints are implied but not explicit. I.E for the constraint |
| 1482 | // a = &b, we add implicit edges *a = b. This helps us capture more cycles |
| 1483 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 1484 | Constraint &C = Constraints[i]; |
| 1485 | if (C.Type == Constraint::AddressOf) { |
| 1486 | GraphNodes[C.Src].AddressTaken = true; |
| 1487 | GraphNodes[C.Src].Direct = false; |
| 1488 | |
| 1489 | // Dest = &src edge |
| 1490 | unsigned AdrNode = C.Src + FirstAdrNode; |
| 1491 | if (!GraphNodes[C.Dest].PredEdges) |
| 1492 | GraphNodes[C.Dest].PredEdges = new SparseBitVector<>; |
| 1493 | GraphNodes[C.Dest].PredEdges->set(AdrNode); |
| 1494 | |
| 1495 | // *Dest = src edge |
| 1496 | unsigned RefNode = C.Dest + FirstRefNode; |
| 1497 | if (!GraphNodes[RefNode].ImplicitPredEdges) |
| 1498 | GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>; |
| 1499 | GraphNodes[RefNode].ImplicitPredEdges->set(C.Src); |
| 1500 | } else if (C.Type == Constraint::Load) { |
| 1501 | if (C.Offset == 0) { |
| 1502 | // dest = *src edge |
| 1503 | if (!GraphNodes[C.Dest].PredEdges) |
| 1504 | GraphNodes[C.Dest].PredEdges = new SparseBitVector<>; |
| 1505 | GraphNodes[C.Dest].PredEdges->set(C.Src + FirstRefNode); |
| 1506 | } else { |
| 1507 | GraphNodes[C.Dest].Direct = false; |
| 1508 | } |
| 1509 | } else if (C.Type == Constraint::Store) { |
| 1510 | if (C.Offset == 0) { |
| 1511 | // *dest = src edge |
| 1512 | unsigned RefNode = C.Dest + FirstRefNode; |
| 1513 | if (!GraphNodes[RefNode].PredEdges) |
| 1514 | GraphNodes[RefNode].PredEdges = new SparseBitVector<>; |
| 1515 | GraphNodes[RefNode].PredEdges->set(C.Src); |
| 1516 | } |
| 1517 | } else { |
| 1518 | // Dest = Src edge and *Dest = *Src edge |
| 1519 | if (!GraphNodes[C.Dest].PredEdges) |
| 1520 | GraphNodes[C.Dest].PredEdges = new SparseBitVector<>; |
| 1521 | GraphNodes[C.Dest].PredEdges->set(C.Src); |
| 1522 | unsigned RefNode = C.Dest + FirstRefNode; |
| 1523 | if (!GraphNodes[RefNode].ImplicitPredEdges) |
| 1524 | GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>; |
| 1525 | GraphNodes[RefNode].ImplicitPredEdges->set(C.Src + FirstRefNode); |
| 1526 | } |
| 1527 | } |
| 1528 | PEClass = 1; |
| 1529 | // Do SCC finding first to condense our predecessor graph |
| 1530 | DFSNumber = 0; |
| 1531 | Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0); |
| 1532 | Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false); |
| 1533 | Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false); |
| 1534 | |
| 1535 | for (unsigned i = 0; i < FirstRefNode; ++i) { |
| 1536 | unsigned Node = VSSCCRep[i]; |
| 1537 | if (!Node2Visited[Node]) |
| 1538 | HVNValNum(Node); |
| 1539 | } |
| 1540 | for (BitVectorMap::iterator Iter = Set2PEClass.begin(); |
| 1541 | Iter != Set2PEClass.end(); |
| 1542 | ++Iter) |
| 1543 | delete Iter->first; |
| 1544 | Set2PEClass.clear(); |
| 1545 | Node2DFS.clear(); |
| 1546 | Node2Deleted.clear(); |
| 1547 | Node2Visited.clear(); |
| 1548 | DOUT << "Finished HVN\n"; |
| 1549 | |
| 1550 | } |
| 1551 | |
| 1552 | /// This is the workhorse of HVN value numbering. We combine SCC finding at the |
| 1553 | /// same time because it's easy. |
| 1554 | void Andersens::HVNValNum(unsigned NodeIndex) { |
| 1555 | unsigned MyDFS = DFSNumber++; |
| 1556 | Node *N = &GraphNodes[NodeIndex]; |
| 1557 | Node2Visited[NodeIndex] = true; |
| 1558 | Node2DFS[NodeIndex] = MyDFS; |
| 1559 | |
| 1560 | // First process all our explicit edges |
| 1561 | if (N->PredEdges) |
| 1562 | for (SparseBitVector<>::iterator Iter = N->PredEdges->begin(); |
| 1563 | Iter != N->PredEdges->end(); |
| 1564 | ++Iter) { |
| 1565 | unsigned j = VSSCCRep[*Iter]; |
| 1566 | if (!Node2Deleted[j]) { |
| 1567 | if (!Node2Visited[j]) |
| 1568 | HVNValNum(j); |
| 1569 | if (Node2DFS[NodeIndex] > Node2DFS[j]) |
| 1570 | Node2DFS[NodeIndex] = Node2DFS[j]; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | // Now process all the implicit edges |
| 1575 | if (N->ImplicitPredEdges) |
| 1576 | for (SparseBitVector<>::iterator Iter = N->ImplicitPredEdges->begin(); |
| 1577 | Iter != N->ImplicitPredEdges->end(); |
| 1578 | ++Iter) { |
| 1579 | unsigned j = VSSCCRep[*Iter]; |
| 1580 | if (!Node2Deleted[j]) { |
| 1581 | if (!Node2Visited[j]) |
| 1582 | HVNValNum(j); |
| 1583 | if (Node2DFS[NodeIndex] > Node2DFS[j]) |
| 1584 | Node2DFS[NodeIndex] = Node2DFS[j]; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | // See if we found any cycles |
| 1589 | if (MyDFS == Node2DFS[NodeIndex]) { |
| 1590 | while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) { |
| 1591 | unsigned CycleNodeIndex = SCCStack.top(); |
| 1592 | Node *CycleNode = &GraphNodes[CycleNodeIndex]; |
| 1593 | VSSCCRep[CycleNodeIndex] = NodeIndex; |
| 1594 | // Unify the nodes |
| 1595 | N->Direct &= CycleNode->Direct; |
| 1596 | |
| 1597 | if (CycleNode->PredEdges) { |
| 1598 | if (!N->PredEdges) |
| 1599 | N->PredEdges = new SparseBitVector<>; |
| 1600 | *(N->PredEdges) |= CycleNode->PredEdges; |
| 1601 | delete CycleNode->PredEdges; |
| 1602 | CycleNode->PredEdges = NULL; |
| 1603 | } |
| 1604 | if (CycleNode->ImplicitPredEdges) { |
| 1605 | if (!N->ImplicitPredEdges) |
| 1606 | N->ImplicitPredEdges = new SparseBitVector<>; |
| 1607 | *(N->ImplicitPredEdges) |= CycleNode->ImplicitPredEdges; |
| 1608 | delete CycleNode->ImplicitPredEdges; |
| 1609 | CycleNode->ImplicitPredEdges = NULL; |
| 1610 | } |
| 1611 | |
| 1612 | SCCStack.pop(); |
| 1613 | } |
| 1614 | |
| 1615 | Node2Deleted[NodeIndex] = true; |
| 1616 | |
| 1617 | if (!N->Direct) { |
| 1618 | GraphNodes[NodeIndex].PointerEquivLabel = PEClass++; |
| 1619 | return; |
| 1620 | } |
| 1621 | |
| 1622 | // Collect labels of successor nodes |
| 1623 | bool AllSame = true; |
| 1624 | unsigned First = ~0; |
| 1625 | SparseBitVector<> *Labels = new SparseBitVector<>; |
| 1626 | bool Used = false; |
| 1627 | |
| 1628 | if (N->PredEdges) |
| 1629 | for (SparseBitVector<>::iterator Iter = N->PredEdges->begin(); |
| 1630 | Iter != N->PredEdges->end(); |
| 1631 | ++Iter) { |
| 1632 | unsigned j = VSSCCRep[*Iter]; |
| 1633 | unsigned Label = GraphNodes[j].PointerEquivLabel; |
| 1634 | // Ignore labels that are equal to us or non-pointers |
| 1635 | if (j == NodeIndex || Label == 0) |
| 1636 | continue; |
| 1637 | if (First == (unsigned)~0) |
| 1638 | First = Label; |
| 1639 | else if (First != Label) |
| 1640 | AllSame = false; |
| 1641 | Labels->set(Label); |
| 1642 | } |
| 1643 | |
| 1644 | // We either have a non-pointer, a copy of an existing node, or a new node. |
| 1645 | // Assign the appropriate pointer equivalence label. |
| 1646 | if (Labels->empty()) { |
| 1647 | GraphNodes[NodeIndex].PointerEquivLabel = 0; |
| 1648 | } else if (AllSame) { |
| 1649 | GraphNodes[NodeIndex].PointerEquivLabel = First; |
| 1650 | } else { |
| 1651 | GraphNodes[NodeIndex].PointerEquivLabel = Set2PEClass[Labels]; |
| 1652 | if (GraphNodes[NodeIndex].PointerEquivLabel == 0) { |
| 1653 | unsigned EquivClass = PEClass++; |
| 1654 | Set2PEClass[Labels] = EquivClass; |
| 1655 | GraphNodes[NodeIndex].PointerEquivLabel = EquivClass; |
| 1656 | Used = true; |
| 1657 | } |
| 1658 | } |
| 1659 | if (!Used) |
| 1660 | delete Labels; |
| 1661 | } else { |
| 1662 | SCCStack.push(NodeIndex); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | /// The technique used here is described in "Exploiting Pointer and Location |
| 1667 | /// Equivalence to Optimize Pointer Analysis. In the 14th International Static |
| 1668 | /// Analysis Symposium (SAS), August 2007." It is known as the "HU" algorithm, |
| 1669 | /// and is equivalent to value numbering the collapsed constraint graph |
| 1670 | /// including evaluating unions. |
| 1671 | void Andersens::HU() { |
| 1672 | DOUT << "Beginning HU\n"; |
| 1673 | // Build a predecessor graph. This is like our constraint graph with the |
| 1674 | // edges going in the opposite direction, and there are edges for all the |
| 1675 | // constraints, instead of just copy constraints. We also build implicit |
| 1676 | // edges for constraints are implied but not explicit. I.E for the constraint |
| 1677 | // a = &b, we add implicit edges *a = b. This helps us capture more cycles |
| 1678 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 1679 | Constraint &C = Constraints[i]; |
| 1680 | if (C.Type == Constraint::AddressOf) { |
| 1681 | GraphNodes[C.Src].AddressTaken = true; |
| 1682 | GraphNodes[C.Src].Direct = false; |
| 1683 | |
| 1684 | GraphNodes[C.Dest].PointsTo->set(C.Src); |
| 1685 | // *Dest = src edge |
| 1686 | unsigned RefNode = C.Dest + FirstRefNode; |
| 1687 | if (!GraphNodes[RefNode].ImplicitPredEdges) |
| 1688 | GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>; |
| 1689 | GraphNodes[RefNode].ImplicitPredEdges->set(C.Src); |
| 1690 | GraphNodes[C.Src].PointedToBy->set(C.Dest); |
| 1691 | } else if (C.Type == Constraint::Load) { |
| 1692 | if (C.Offset == 0) { |
| 1693 | // dest = *src edge |
| 1694 | if (!GraphNodes[C.Dest].PredEdges) |
| 1695 | GraphNodes[C.Dest].PredEdges = new SparseBitVector<>; |
| 1696 | GraphNodes[C.Dest].PredEdges->set(C.Src + FirstRefNode); |
| 1697 | } else { |
| 1698 | GraphNodes[C.Dest].Direct = false; |
| 1699 | } |
| 1700 | } else if (C.Type == Constraint::Store) { |
| 1701 | if (C.Offset == 0) { |
| 1702 | // *dest = src edge |
| 1703 | unsigned RefNode = C.Dest + FirstRefNode; |
| 1704 | if (!GraphNodes[RefNode].PredEdges) |
| 1705 | GraphNodes[RefNode].PredEdges = new SparseBitVector<>; |
| 1706 | GraphNodes[RefNode].PredEdges->set(C.Src); |
| 1707 | } |
| 1708 | } else { |
| 1709 | // Dest = Src edge and *Dest = *Src edg |
| 1710 | if (!GraphNodes[C.Dest].PredEdges) |
| 1711 | GraphNodes[C.Dest].PredEdges = new SparseBitVector<>; |
| 1712 | GraphNodes[C.Dest].PredEdges->set(C.Src); |
| 1713 | unsigned RefNode = C.Dest + FirstRefNode; |
| 1714 | if (!GraphNodes[RefNode].ImplicitPredEdges) |
| 1715 | GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>; |
| 1716 | GraphNodes[RefNode].ImplicitPredEdges->set(C.Src + FirstRefNode); |
| 1717 | } |
| 1718 | } |
| 1719 | PEClass = 1; |
| 1720 | // Do SCC finding first to condense our predecessor graph |
| 1721 | DFSNumber = 0; |
| 1722 | Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0); |
| 1723 | Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false); |
| 1724 | Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false); |
| 1725 | |
| 1726 | for (unsigned i = 0; i < FirstRefNode; ++i) { |
| 1727 | if (FindNode(i) == i) { |
| 1728 | unsigned Node = VSSCCRep[i]; |
| 1729 | if (!Node2Visited[Node]) |
| 1730 | Condense(Node); |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | // Reset tables for actual labeling |
| 1735 | Node2DFS.clear(); |
| 1736 | Node2Visited.clear(); |
| 1737 | Node2Deleted.clear(); |
| 1738 | // Pre-grow our densemap so that we don't get really bad behavior |
| 1739 | Set2PEClass.resize(GraphNodes.size()); |
| 1740 | |
| 1741 | // Visit the condensed graph and generate pointer equivalence labels. |
| 1742 | Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false); |
| 1743 | for (unsigned i = 0; i < FirstRefNode; ++i) { |
| 1744 | if (FindNode(i) == i) { |
| 1745 | unsigned Node = VSSCCRep[i]; |
| 1746 | if (!Node2Visited[Node]) |
| 1747 | HUValNum(Node); |
| 1748 | } |
| 1749 | } |
| 1750 | // PEClass nodes will be deleted by the deleting of N->PointsTo in our caller. |
| 1751 | Set2PEClass.clear(); |
| 1752 | DOUT << "Finished HU\n"; |
| 1753 | } |
| 1754 | |
| 1755 | |
| 1756 | /// Implementation of standard Tarjan SCC algorithm as modified by Nuutilla. |
| 1757 | void Andersens::Condense(unsigned NodeIndex) { |
| 1758 | unsigned MyDFS = DFSNumber++; |
| 1759 | Node *N = &GraphNodes[NodeIndex]; |
| 1760 | Node2Visited[NodeIndex] = true; |
| 1761 | Node2DFS[NodeIndex] = MyDFS; |
| 1762 | |
| 1763 | // First process all our explicit edges |
| 1764 | if (N->PredEdges) |
| 1765 | for (SparseBitVector<>::iterator Iter = N->PredEdges->begin(); |
| 1766 | Iter != N->PredEdges->end(); |
| 1767 | ++Iter) { |
| 1768 | unsigned j = VSSCCRep[*Iter]; |
| 1769 | if (!Node2Deleted[j]) { |
| 1770 | if (!Node2Visited[j]) |
| 1771 | Condense(j); |
| 1772 | if (Node2DFS[NodeIndex] > Node2DFS[j]) |
| 1773 | Node2DFS[NodeIndex] = Node2DFS[j]; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | // Now process all the implicit edges |
| 1778 | if (N->ImplicitPredEdges) |
| 1779 | for (SparseBitVector<>::iterator Iter = N->ImplicitPredEdges->begin(); |
| 1780 | Iter != N->ImplicitPredEdges->end(); |
| 1781 | ++Iter) { |
| 1782 | unsigned j = VSSCCRep[*Iter]; |
| 1783 | if (!Node2Deleted[j]) { |
| 1784 | if (!Node2Visited[j]) |
| 1785 | Condense(j); |
| 1786 | if (Node2DFS[NodeIndex] > Node2DFS[j]) |
| 1787 | Node2DFS[NodeIndex] = Node2DFS[j]; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | // See if we found any cycles |
| 1792 | if (MyDFS == Node2DFS[NodeIndex]) { |
| 1793 | while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) { |
| 1794 | unsigned CycleNodeIndex = SCCStack.top(); |
| 1795 | Node *CycleNode = &GraphNodes[CycleNodeIndex]; |
| 1796 | VSSCCRep[CycleNodeIndex] = NodeIndex; |
| 1797 | // Unify the nodes |
| 1798 | N->Direct &= CycleNode->Direct; |
| 1799 | |
| 1800 | *(N->PointsTo) |= CycleNode->PointsTo; |
| 1801 | delete CycleNode->PointsTo; |
| 1802 | CycleNode->PointsTo = NULL; |
| 1803 | if (CycleNode->PredEdges) { |
| 1804 | if (!N->PredEdges) |
| 1805 | N->PredEdges = new SparseBitVector<>; |
| 1806 | *(N->PredEdges) |= CycleNode->PredEdges; |
| 1807 | delete CycleNode->PredEdges; |
| 1808 | CycleNode->PredEdges = NULL; |
| 1809 | } |
| 1810 | if (CycleNode->ImplicitPredEdges) { |
| 1811 | if (!N->ImplicitPredEdges) |
| 1812 | N->ImplicitPredEdges = new SparseBitVector<>; |
| 1813 | *(N->ImplicitPredEdges) |= CycleNode->ImplicitPredEdges; |
| 1814 | delete CycleNode->ImplicitPredEdges; |
| 1815 | CycleNode->ImplicitPredEdges = NULL; |
| 1816 | } |
| 1817 | SCCStack.pop(); |
| 1818 | } |
| 1819 | |
| 1820 | Node2Deleted[NodeIndex] = true; |
| 1821 | |
| 1822 | // Set up number of incoming edges for other nodes |
| 1823 | if (N->PredEdges) |
| 1824 | for (SparseBitVector<>::iterator Iter = N->PredEdges->begin(); |
| 1825 | Iter != N->PredEdges->end(); |
| 1826 | ++Iter) |
| 1827 | ++GraphNodes[VSSCCRep[*Iter]].NumInEdges; |
| 1828 | } else { |
| 1829 | SCCStack.push(NodeIndex); |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | void Andersens::HUValNum(unsigned NodeIndex) { |
| 1834 | Node *N = &GraphNodes[NodeIndex]; |
| 1835 | Node2Visited[NodeIndex] = true; |
| 1836 | |
| 1837 | // Eliminate dereferences of non-pointers for those non-pointers we have |
| 1838 | // already identified. These are ref nodes whose non-ref node: |
| 1839 | // 1. Has already been visited determined to point to nothing (and thus, a |
| 1840 | // dereference of it must point to nothing) |
| 1841 | // 2. Any direct node with no predecessor edges in our graph and with no |
| 1842 | // points-to set (since it can't point to anything either, being that it |
| 1843 | // receives no points-to sets and has none). |
| 1844 | if (NodeIndex >= FirstRefNode) { |
| 1845 | unsigned j = VSSCCRep[FindNode(NodeIndex - FirstRefNode)]; |
| 1846 | if ((Node2Visited[j] && !GraphNodes[j].PointerEquivLabel) |
| 1847 | || (GraphNodes[j].Direct && !GraphNodes[j].PredEdges |
| 1848 | && GraphNodes[j].PointsTo->empty())){ |
| 1849 | return; |
| 1850 | } |
| 1851 | } |
| 1852 | // Process all our explicit edges |
| 1853 | if (N->PredEdges) |
| 1854 | for (SparseBitVector<>::iterator Iter = N->PredEdges->begin(); |
| 1855 | Iter != N->PredEdges->end(); |
| 1856 | ++Iter) { |
| 1857 | unsigned j = VSSCCRep[*Iter]; |
| 1858 | if (!Node2Visited[j]) |
| 1859 | HUValNum(j); |
| 1860 | |
| 1861 | // If this edge turned out to be the same as us, or got no pointer |
| 1862 | // equivalence label (and thus points to nothing) , just decrement our |
| 1863 | // incoming edges and continue. |
| 1864 | if (j == NodeIndex || GraphNodes[j].PointerEquivLabel == 0) { |
| 1865 | --GraphNodes[j].NumInEdges; |
| 1866 | continue; |
| 1867 | } |
| 1868 | |
| 1869 | *(N->PointsTo) |= GraphNodes[j].PointsTo; |
| 1870 | |
| 1871 | // If we didn't end up storing this in the hash, and we're done with all |
| 1872 | // the edges, we don't need the points-to set anymore. |
| 1873 | --GraphNodes[j].NumInEdges; |
| 1874 | if (!GraphNodes[j].NumInEdges && !GraphNodes[j].StoredInHash) { |
| 1875 | delete GraphNodes[j].PointsTo; |
| 1876 | GraphNodes[j].PointsTo = NULL; |
| 1877 | } |
| 1878 | } |
| 1879 | // If this isn't a direct node, generate a fresh variable. |
| 1880 | if (!N->Direct) { |
| 1881 | N->PointsTo->set(FirstRefNode + NodeIndex); |
| 1882 | } |
| 1883 | |
| 1884 | // See If we have something equivalent to us, if not, generate a new |
| 1885 | // equivalence class. |
| 1886 | if (N->PointsTo->empty()) { |
| 1887 | delete N->PointsTo; |
| 1888 | N->PointsTo = NULL; |
| 1889 | } else { |
| 1890 | if (N->Direct) { |
| 1891 | N->PointerEquivLabel = Set2PEClass[N->PointsTo]; |
| 1892 | if (N->PointerEquivLabel == 0) { |
| 1893 | unsigned EquivClass = PEClass++; |
| 1894 | N->StoredInHash = true; |
| 1895 | Set2PEClass[N->PointsTo] = EquivClass; |
| 1896 | N->PointerEquivLabel = EquivClass; |
| 1897 | } |
| 1898 | } else { |
| 1899 | N->PointerEquivLabel = PEClass++; |
| 1900 | } |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | /// Rewrite our list of constraints so that pointer equivalent nodes are |
| 1905 | /// replaced by their the pointer equivalence class representative. |
| 1906 | void Andersens::RewriteConstraints() { |
| 1907 | std::vector<Constraint> NewConstraints; |
Chris Lattner | be20773 | 2007-09-30 00:47:20 +0000 | [diff] [blame] | 1908 | DenseSet<Constraint, ConstraintKeyInfo> Seen; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1909 | |
| 1910 | PEClass2Node.clear(); |
| 1911 | PENLEClass2Node.clear(); |
| 1912 | |
| 1913 | // We may have from 1 to Graphnodes + 1 equivalence classes. |
| 1914 | PEClass2Node.insert(PEClass2Node.begin(), GraphNodes.size() + 1, -1); |
| 1915 | PENLEClass2Node.insert(PENLEClass2Node.begin(), GraphNodes.size() + 1, -1); |
| 1916 | |
| 1917 | // Rewrite constraints, ignoring non-pointer constraints, uniting equivalent |
| 1918 | // nodes, and rewriting constraints to use the representative nodes. |
| 1919 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 1920 | Constraint &C = Constraints[i]; |
| 1921 | unsigned RHSNode = FindNode(C.Src); |
| 1922 | unsigned LHSNode = FindNode(C.Dest); |
| 1923 | unsigned RHSLabel = GraphNodes[VSSCCRep[RHSNode]].PointerEquivLabel; |
| 1924 | unsigned LHSLabel = GraphNodes[VSSCCRep[LHSNode]].PointerEquivLabel; |
| 1925 | |
| 1926 | // First we try to eliminate constraints for things we can prove don't point |
| 1927 | // to anything. |
| 1928 | if (LHSLabel == 0) { |
| 1929 | DEBUG(PrintNode(&GraphNodes[LHSNode])); |
| 1930 | DOUT << " is a non-pointer, ignoring constraint.\n"; |
| 1931 | continue; |
| 1932 | } |
| 1933 | if (RHSLabel == 0) { |
| 1934 | DEBUG(PrintNode(&GraphNodes[RHSNode])); |
| 1935 | DOUT << " is a non-pointer, ignoring constraint.\n"; |
| 1936 | continue; |
| 1937 | } |
| 1938 | // This constraint may be useless, and it may become useless as we translate |
| 1939 | // it. |
| 1940 | if (C.Src == C.Dest && C.Type == Constraint::Copy) |
| 1941 | continue; |
Daniel Berlin | c7a12ae | 2007-09-27 15:42:23 +0000 | [diff] [blame] | 1942 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1943 | C.Src = FindEquivalentNode(RHSNode, RHSLabel); |
| 1944 | C.Dest = FindEquivalentNode(FindNode(LHSNode), LHSLabel); |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1945 | if ((C.Src == C.Dest && C.Type == Constraint::Copy) |
Chris Lattner | be20773 | 2007-09-30 00:47:20 +0000 | [diff] [blame] | 1946 | || Seen.count(C)) |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1947 | continue; |
| 1948 | |
Chris Lattner | be20773 | 2007-09-30 00:47:20 +0000 | [diff] [blame] | 1949 | Seen.insert(C); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1950 | NewConstraints.push_back(C); |
| 1951 | } |
| 1952 | Constraints.swap(NewConstraints); |
| 1953 | PEClass2Node.clear(); |
| 1954 | } |
| 1955 | |
| 1956 | /// See if we have a node that is pointer equivalent to the one being asked |
| 1957 | /// about, and if so, unite them and return the equivalent node. Otherwise, |
| 1958 | /// return the original node. |
| 1959 | unsigned Andersens::FindEquivalentNode(unsigned NodeIndex, |
| 1960 | unsigned NodeLabel) { |
| 1961 | if (!GraphNodes[NodeIndex].AddressTaken) { |
| 1962 | if (PEClass2Node[NodeLabel] != -1) { |
| 1963 | // We found an existing node with the same pointer label, so unify them. |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 1964 | // We specifically request that Union-By-Rank not be used so that |
| 1965 | // PEClass2Node[NodeLabel] U= NodeIndex and not the other way around. |
| 1966 | return UniteNodes(PEClass2Node[NodeLabel], NodeIndex, false); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 1967 | } else { |
| 1968 | PEClass2Node[NodeLabel] = NodeIndex; |
| 1969 | PENLEClass2Node[NodeLabel] = NodeIndex; |
| 1970 | } |
| 1971 | } else if (PENLEClass2Node[NodeLabel] == -1) { |
| 1972 | PENLEClass2Node[NodeLabel] = NodeIndex; |
| 1973 | } |
| 1974 | |
| 1975 | return NodeIndex; |
| 1976 | } |
| 1977 | |
| 1978 | void Andersens::PrintLabels() { |
| 1979 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 1980 | if (i < FirstRefNode) { |
| 1981 | PrintNode(&GraphNodes[i]); |
| 1982 | } else if (i < FirstAdrNode) { |
| 1983 | DOUT << "REF("; |
| 1984 | PrintNode(&GraphNodes[i-FirstRefNode]); |
| 1985 | DOUT <<")"; |
| 1986 | } else { |
| 1987 | DOUT << "ADR("; |
| 1988 | PrintNode(&GraphNodes[i-FirstAdrNode]); |
| 1989 | DOUT <<")"; |
| 1990 | } |
| 1991 | |
| 1992 | DOUT << " has pointer label " << GraphNodes[i].PointerEquivLabel |
| 1993 | << " and SCC rep " << VSSCCRep[i] |
| 1994 | << " and is " << (GraphNodes[i].Direct ? "Direct" : "Not direct") |
| 1995 | << "\n"; |
| 1996 | } |
| 1997 | } |
| 1998 | |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 1999 | /// The technique used here is described in "The Ant and the |
| 2000 | /// Grasshopper: Fast and Accurate Pointer Analysis for Millions of |
| 2001 | /// Lines of Code. In Programming Language Design and Implementation |
| 2002 | /// (PLDI), June 2007." It is known as the "HCD" (Hybrid Cycle |
| 2003 | /// Detection) algorithm. It is called a hybrid because it performs an |
| 2004 | /// offline analysis and uses its results during the solving (online) |
| 2005 | /// phase. This is just the offline portion; the results of this |
| 2006 | /// operation are stored in SDT and are later used in SolveContraints() |
| 2007 | /// and UniteNodes(). |
| 2008 | void Andersens::HCD() { |
| 2009 | DOUT << "Starting HCD.\n"; |
| 2010 | HCDSCCRep.resize(GraphNodes.size()); |
| 2011 | |
| 2012 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2013 | GraphNodes[i].Edges = new SparseBitVector<>; |
| 2014 | HCDSCCRep[i] = i; |
| 2015 | } |
| 2016 | |
| 2017 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 2018 | Constraint &C = Constraints[i]; |
| 2019 | assert (C.Src < GraphNodes.size() && C.Dest < GraphNodes.size()); |
| 2020 | if (C.Type == Constraint::AddressOf) { |
| 2021 | continue; |
| 2022 | } else if (C.Type == Constraint::Load) { |
| 2023 | if( C.Offset == 0 ) |
| 2024 | GraphNodes[C.Dest].Edges->set(C.Src + FirstRefNode); |
| 2025 | } else if (C.Type == Constraint::Store) { |
| 2026 | if( C.Offset == 0 ) |
| 2027 | GraphNodes[C.Dest + FirstRefNode].Edges->set(C.Src); |
| 2028 | } else { |
| 2029 | GraphNodes[C.Dest].Edges->set(C.Src); |
| 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0); |
| 2034 | Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false); |
| 2035 | Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false); |
| 2036 | SDT.insert(SDT.begin(), GraphNodes.size() / 2, -1); |
| 2037 | |
| 2038 | DFSNumber = 0; |
| 2039 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2040 | unsigned Node = HCDSCCRep[i]; |
| 2041 | if (!Node2Deleted[Node]) |
| 2042 | Search(Node); |
| 2043 | } |
| 2044 | |
| 2045 | for (unsigned i = 0; i < GraphNodes.size(); ++i) |
| 2046 | if (GraphNodes[i].Edges != NULL) { |
| 2047 | delete GraphNodes[i].Edges; |
| 2048 | GraphNodes[i].Edges = NULL; |
| 2049 | } |
| 2050 | |
| 2051 | while( !SCCStack.empty() ) |
| 2052 | SCCStack.pop(); |
| 2053 | |
| 2054 | Node2DFS.clear(); |
| 2055 | Node2Visited.clear(); |
| 2056 | Node2Deleted.clear(); |
| 2057 | HCDSCCRep.clear(); |
| 2058 | DOUT << "HCD complete.\n"; |
| 2059 | } |
| 2060 | |
| 2061 | // Component of HCD: |
| 2062 | // Use Nuutila's variant of Tarjan's algorithm to detect |
| 2063 | // Strongly-Connected Components (SCCs). For non-trivial SCCs |
| 2064 | // containing ref nodes, insert the appropriate information in SDT. |
| 2065 | void Andersens::Search(unsigned Node) { |
| 2066 | unsigned MyDFS = DFSNumber++; |
| 2067 | |
| 2068 | Node2Visited[Node] = true; |
| 2069 | Node2DFS[Node] = MyDFS; |
| 2070 | |
| 2071 | for (SparseBitVector<>::iterator Iter = GraphNodes[Node].Edges->begin(), |
| 2072 | End = GraphNodes[Node].Edges->end(); |
| 2073 | Iter != End; |
| 2074 | ++Iter) { |
| 2075 | unsigned J = HCDSCCRep[*Iter]; |
| 2076 | assert(GraphNodes[J].isRep() && "Debug check; must be representative"); |
| 2077 | if (!Node2Deleted[J]) { |
| 2078 | if (!Node2Visited[J]) |
| 2079 | Search(J); |
| 2080 | if (Node2DFS[Node] > Node2DFS[J]) |
| 2081 | Node2DFS[Node] = Node2DFS[J]; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | if( MyDFS != Node2DFS[Node] ) { |
| 2086 | SCCStack.push(Node); |
| 2087 | return; |
| 2088 | } |
| 2089 | |
| 2090 | // This node is the root of a SCC, so process it. |
| 2091 | // |
| 2092 | // If the SCC is "non-trivial" (not a singleton) and contains a reference |
| 2093 | // node, we place this SCC into SDT. We unite the nodes in any case. |
| 2094 | if (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) { |
| 2095 | SparseBitVector<> SCC; |
| 2096 | |
| 2097 | SCC.set(Node); |
| 2098 | |
| 2099 | bool Ref = (Node >= FirstRefNode); |
| 2100 | |
| 2101 | Node2Deleted[Node] = true; |
| 2102 | |
| 2103 | do { |
| 2104 | unsigned P = SCCStack.top(); SCCStack.pop(); |
| 2105 | Ref |= (P >= FirstRefNode); |
| 2106 | SCC.set(P); |
| 2107 | HCDSCCRep[P] = Node; |
| 2108 | } while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS); |
| 2109 | |
| 2110 | if (Ref) { |
| 2111 | unsigned Rep = SCC.find_first(); |
| 2112 | assert(Rep < FirstRefNode && "The SCC didn't have a non-Ref node!"); |
| 2113 | |
| 2114 | SparseBitVector<>::iterator i = SCC.begin(); |
| 2115 | |
| 2116 | // Skip over the non-ref nodes |
| 2117 | while( *i < FirstRefNode ) |
| 2118 | ++i; |
| 2119 | |
| 2120 | while( i != SCC.end() ) |
| 2121 | SDT[ (*i++) - FirstRefNode ] = Rep; |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2127 | /// Optimize the constraints by performing offline variable substitution and |
| 2128 | /// other optimizations. |
| 2129 | void Andersens::OptimizeConstraints() { |
| 2130 | DOUT << "Beginning constraint optimization\n"; |
| 2131 | |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2132 | SDTActive = false; |
| 2133 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2134 | // Function related nodes need to stay in the same relative position and can't |
| 2135 | // be location equivalent. |
| 2136 | for (std::map<unsigned, unsigned>::iterator Iter = MaxK.begin(); |
| 2137 | Iter != MaxK.end(); |
| 2138 | ++Iter) { |
| 2139 | for (unsigned i = Iter->first; |
| 2140 | i != Iter->first + Iter->second; |
| 2141 | ++i) { |
| 2142 | GraphNodes[i].AddressTaken = true; |
| 2143 | GraphNodes[i].Direct = false; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | ClumpAddressTaken(); |
| 2148 | FirstRefNode = GraphNodes.size(); |
| 2149 | FirstAdrNode = FirstRefNode + GraphNodes.size(); |
| 2150 | GraphNodes.insert(GraphNodes.end(), 2 * GraphNodes.size(), |
| 2151 | Node(false)); |
| 2152 | VSSCCRep.resize(GraphNodes.size()); |
| 2153 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2154 | VSSCCRep[i] = i; |
| 2155 | } |
| 2156 | HVN(); |
| 2157 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2158 | Node *N = &GraphNodes[i]; |
| 2159 | delete N->PredEdges; |
| 2160 | N->PredEdges = NULL; |
| 2161 | delete N->ImplicitPredEdges; |
| 2162 | N->ImplicitPredEdges = NULL; |
| 2163 | } |
| 2164 | #undef DEBUG_TYPE |
| 2165 | #define DEBUG_TYPE "anders-aa-labels" |
| 2166 | DEBUG(PrintLabels()); |
| 2167 | #undef DEBUG_TYPE |
| 2168 | #define DEBUG_TYPE "anders-aa" |
| 2169 | RewriteConstraints(); |
| 2170 | // Delete the adr nodes. |
| 2171 | GraphNodes.resize(FirstRefNode * 2); |
| 2172 | |
| 2173 | // Now perform HU |
| 2174 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2175 | Node *N = &GraphNodes[i]; |
| 2176 | if (FindNode(i) == i) { |
| 2177 | N->PointsTo = new SparseBitVector<>; |
| 2178 | N->PointedToBy = new SparseBitVector<>; |
| 2179 | // Reset our labels |
| 2180 | } |
| 2181 | VSSCCRep[i] = i; |
| 2182 | N->PointerEquivLabel = 0; |
| 2183 | } |
| 2184 | HU(); |
| 2185 | #undef DEBUG_TYPE |
| 2186 | #define DEBUG_TYPE "anders-aa-labels" |
| 2187 | DEBUG(PrintLabels()); |
| 2188 | #undef DEBUG_TYPE |
| 2189 | #define DEBUG_TYPE "anders-aa" |
| 2190 | RewriteConstraints(); |
| 2191 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2192 | if (FindNode(i) == i) { |
| 2193 | Node *N = &GraphNodes[i]; |
| 2194 | delete N->PointsTo; |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2195 | N->PointsTo = NULL; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2196 | delete N->PredEdges; |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2197 | N->PredEdges = NULL; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2198 | delete N->ImplicitPredEdges; |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2199 | N->ImplicitPredEdges = NULL; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2200 | delete N->PointedToBy; |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2201 | N->PointedToBy = NULL; |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2202 | } |
| 2203 | } |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2204 | |
| 2205 | // perform Hybrid Cycle Detection (HCD) |
| 2206 | HCD(); |
| 2207 | SDTActive = true; |
| 2208 | |
| 2209 | // No longer any need for the upper half of GraphNodes (for ref nodes). |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2210 | GraphNodes.erase(GraphNodes.begin() + FirstRefNode, GraphNodes.end()); |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2211 | |
| 2212 | // HCD complete. |
| 2213 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2214 | DOUT << "Finished constraint optimization\n"; |
| 2215 | FirstRefNode = 0; |
| 2216 | FirstAdrNode = 0; |
| 2217 | } |
| 2218 | |
| 2219 | /// Unite pointer but not location equivalent variables, now that the constraint |
| 2220 | /// graph is built. |
| 2221 | void Andersens::UnitePointerEquivalences() { |
| 2222 | DOUT << "Uniting remaining pointer equivalences\n"; |
| 2223 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2224 | if (GraphNodes[i].AddressTaken && GraphNodes[i].isRep()) { |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2225 | unsigned Label = GraphNodes[i].PointerEquivLabel; |
| 2226 | |
| 2227 | if (Label && PENLEClass2Node[Label] != -1) |
| 2228 | UniteNodes(i, PENLEClass2Node[Label]); |
| 2229 | } |
| 2230 | } |
| 2231 | DOUT << "Finished remaining pointer equivalences\n"; |
| 2232 | PENLEClass2Node.clear(); |
| 2233 | } |
| 2234 | |
| 2235 | /// Create the constraint graph used for solving points-to analysis. |
| 2236 | /// |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2237 | void Andersens::CreateConstraintGraph() { |
| 2238 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 2239 | Constraint &C = Constraints[i]; |
| 2240 | assert (C.Src < GraphNodes.size() && C.Dest < GraphNodes.size()); |
| 2241 | if (C.Type == Constraint::AddressOf) |
| 2242 | GraphNodes[C.Dest].PointsTo->set(C.Src); |
| 2243 | else if (C.Type == Constraint::Load) |
| 2244 | GraphNodes[C.Src].Constraints.push_back(C); |
| 2245 | else if (C.Type == Constraint::Store) |
| 2246 | GraphNodes[C.Dest].Constraints.push_back(C); |
| 2247 | else if (C.Offset != 0) |
| 2248 | GraphNodes[C.Src].Constraints.push_back(C); |
| 2249 | else |
| 2250 | GraphNodes[C.Src].Edges->set(C.Dest); |
| 2251 | } |
| 2252 | } |
| 2253 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2254 | // Perform DFS and cycle detection. |
| 2255 | bool Andersens::QueryNode(unsigned Node) { |
| 2256 | assert(GraphNodes[Node].isRep() && "Querying a non-rep node"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2257 | unsigned OurDFS = ++DFSNumber; |
| 2258 | SparseBitVector<> ToErase; |
| 2259 | SparseBitVector<> NewEdges; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2260 | Tarjan2DFS[Node] = OurDFS; |
| 2261 | |
| 2262 | // Changed denotes a change from a recursive call that we will bubble up. |
| 2263 | // Merged is set if we actually merge a node ourselves. |
| 2264 | bool Changed = false, Merged = false; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2265 | |
| 2266 | for (SparseBitVector<>::iterator bi = GraphNodes[Node].Edges->begin(); |
| 2267 | bi != GraphNodes[Node].Edges->end(); |
| 2268 | ++bi) { |
| 2269 | unsigned RepNode = FindNode(*bi); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2270 | // If this edge points to a non-representative node but we are |
| 2271 | // already planning to add an edge to its representative, we have no |
| 2272 | // need for this edge anymore. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2273 | if (RepNode != *bi && NewEdges.test(RepNode)){ |
| 2274 | ToErase.set(*bi); |
| 2275 | continue; |
| 2276 | } |
| 2277 | |
| 2278 | // Continue about our DFS. |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2279 | if (!Tarjan2Deleted[RepNode]){ |
| 2280 | if (Tarjan2DFS[RepNode] == 0) { |
| 2281 | Changed |= QueryNode(RepNode); |
| 2282 | // May have been changed by QueryNode |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2283 | RepNode = FindNode(RepNode); |
| 2284 | } |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2285 | if (Tarjan2DFS[RepNode] < Tarjan2DFS[Node]) |
| 2286 | Tarjan2DFS[Node] = Tarjan2DFS[RepNode]; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2287 | } |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2288 | |
| 2289 | // We may have just discovered that this node is part of a cycle, in |
| 2290 | // which case we can also erase it. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2291 | if (RepNode != *bi) { |
| 2292 | ToErase.set(*bi); |
| 2293 | NewEdges.set(RepNode); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2297 | GraphNodes[Node].Edges->intersectWithComplement(ToErase); |
| 2298 | GraphNodes[Node].Edges |= NewEdges; |
| 2299 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2300 | // If this node is a root of a non-trivial SCC, place it on our |
| 2301 | // worklist to be processed. |
| 2302 | if (OurDFS == Tarjan2DFS[Node]) { |
| 2303 | while (!SCCStack.empty() && Tarjan2DFS[SCCStack.top()] >= OurDFS) { |
| 2304 | Node = UniteNodes(Node, SCCStack.top()); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2305 | |
| 2306 | SCCStack.pop(); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2307 | Merged = true; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2308 | } |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2309 | Tarjan2Deleted[Node] = true; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2310 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2311 | if (Merged) |
| 2312 | NextWL->insert(&GraphNodes[Node]); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2313 | } else { |
| 2314 | SCCStack.push(Node); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2315 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2316 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2317 | return(Changed | Merged); |
| 2318 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2319 | |
| 2320 | /// SolveConstraints - This stage iteratively processes the constraints list |
| 2321 | /// propagating constraints (adding edges to the Nodes in the points-to graph) |
| 2322 | /// until a fixed point is reached. |
| 2323 | /// |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2324 | /// We use a variant of the technique called "Lazy Cycle Detection", which is |
| 2325 | /// described in "The Ant and the Grasshopper: Fast and Accurate Pointer |
| 2326 | /// Analysis for Millions of Lines of Code. In Programming Language Design and |
| 2327 | /// Implementation (PLDI), June 2007." |
| 2328 | /// The paper describes performing cycle detection one node at a time, which can |
| 2329 | /// be expensive if there are no cycles, but there are long chains of nodes that |
| 2330 | /// it heuristically believes are cycles (because it will DFS from each node |
| 2331 | /// without state from previous nodes). |
| 2332 | /// Instead, we use the heuristic to build a worklist of nodes to check, then |
| 2333 | /// cycle detect them all at the same time to do this more cheaply. This |
| 2334 | /// catches cycles slightly later than the original technique did, but does it |
| 2335 | /// make significantly cheaper. |
| 2336 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2337 | void Andersens::SolveConstraints() { |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2338 | CurrWL = &w1; |
| 2339 | NextWL = &w2; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2340 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2341 | OptimizeConstraints(); |
| 2342 | #undef DEBUG_TYPE |
| 2343 | #define DEBUG_TYPE "anders-aa-constraints" |
| 2344 | DEBUG(PrintConstraints()); |
| 2345 | #undef DEBUG_TYPE |
| 2346 | #define DEBUG_TYPE "anders-aa" |
| 2347 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2348 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2349 | Node *N = &GraphNodes[i]; |
| 2350 | N->PointsTo = new SparseBitVector<>; |
| 2351 | N->OldPointsTo = new SparseBitVector<>; |
| 2352 | N->Edges = new SparseBitVector<>; |
| 2353 | } |
| 2354 | CreateConstraintGraph(); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2355 | UnitePointerEquivalences(); |
| 2356 | assert(SCCStack.empty() && "SCC Stack should be empty by now!"); |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2357 | Node2DFS.clear(); |
| 2358 | Node2Deleted.clear(); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2359 | Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0); |
| 2360 | Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false); |
| 2361 | DFSNumber = 0; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2362 | DenseSet<Constraint, ConstraintKeyInfo> Seen; |
| 2363 | DenseSet<std::pair<unsigned,unsigned>, PairKeyInfo> EdgesChecked; |
| 2364 | |
| 2365 | // Order graph and add initial nodes to work list. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2366 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2367 | Node *INode = &GraphNodes[i]; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2368 | |
| 2369 | // Add to work list if it's a representative and can contribute to the |
| 2370 | // calculation right now. |
| 2371 | if (INode->isRep() && !INode->PointsTo->empty() |
| 2372 | && (!INode->Edges->empty() || !INode->Constraints.empty())) { |
| 2373 | INode->Stamp(); |
| 2374 | CurrWL->insert(INode); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2377 | std::queue<unsigned int> TarjanWL; |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2378 | #if !FULL_UNIVERSAL |
| 2379 | // "Rep and special variables" - in order for HCD to maintain conservative |
| 2380 | // results when !FULL_UNIVERSAL, we need to treat the special variables in |
| 2381 | // the same way that the !FULL_UNIVERSAL tweak does throughout the rest of |
| 2382 | // the analysis - it's ok to add edges from the special nodes, but never |
| 2383 | // *to* the special nodes. |
| 2384 | std::vector<unsigned int> RSV; |
| 2385 | #endif |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2386 | while( !CurrWL->empty() ) { |
| 2387 | DOUT << "Starting iteration #" << ++NumIters << "\n"; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2388 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2389 | Node* CurrNode; |
| 2390 | unsigned CurrNodeIndex; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2391 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2392 | // Actual cycle checking code. We cycle check all of the lazy cycle |
| 2393 | // candidates from the last iteration in one go. |
| 2394 | if (!TarjanWL.empty()) { |
| 2395 | DFSNumber = 0; |
| 2396 | |
| 2397 | Tarjan2DFS.clear(); |
| 2398 | Tarjan2Deleted.clear(); |
| 2399 | while (!TarjanWL.empty()) { |
| 2400 | unsigned int ToTarjan = TarjanWL.front(); |
| 2401 | TarjanWL.pop(); |
| 2402 | if (!Tarjan2Deleted[ToTarjan] |
| 2403 | && GraphNodes[ToTarjan].isRep() |
| 2404 | && Tarjan2DFS[ToTarjan] == 0) |
| 2405 | QueryNode(ToTarjan); |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | // Add to work list if it's a representative and can contribute to the |
| 2410 | // calculation right now. |
| 2411 | while( (CurrNode = CurrWL->pop()) != NULL ) { |
| 2412 | CurrNodeIndex = CurrNode - &GraphNodes[0]; |
| 2413 | CurrNode->Stamp(); |
| 2414 | |
| 2415 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2416 | // Figure out the changed points to bits |
| 2417 | SparseBitVector<> CurrPointsTo; |
| 2418 | CurrPointsTo.intersectWithComplement(CurrNode->PointsTo, |
| 2419 | CurrNode->OldPointsTo); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2420 | if (CurrPointsTo.empty()) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2421 | continue; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2422 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2423 | *(CurrNode->OldPointsTo) |= CurrPointsTo; |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2424 | |
| 2425 | // Check the offline-computed equivalencies from HCD. |
| 2426 | bool SCC = false; |
| 2427 | unsigned Rep; |
| 2428 | |
| 2429 | if (SDT[CurrNodeIndex] >= 0) { |
| 2430 | SCC = true; |
| 2431 | Rep = FindNode(SDT[CurrNodeIndex]); |
| 2432 | |
| 2433 | #if !FULL_UNIVERSAL |
| 2434 | RSV.clear(); |
| 2435 | #endif |
| 2436 | for (SparseBitVector<>::iterator bi = CurrPointsTo.begin(); |
| 2437 | bi != CurrPointsTo.end(); ++bi) { |
| 2438 | unsigned Node = FindNode(*bi); |
| 2439 | #if !FULL_UNIVERSAL |
| 2440 | if (Node < NumberSpecialNodes) { |
| 2441 | RSV.push_back(Node); |
| 2442 | continue; |
| 2443 | } |
| 2444 | #endif |
| 2445 | Rep = UniteNodes(Rep,Node); |
| 2446 | } |
| 2447 | #if !FULL_UNIVERSAL |
| 2448 | RSV.push_back(Rep); |
| 2449 | #endif |
| 2450 | |
| 2451 | NextWL->insert(&GraphNodes[Rep]); |
| 2452 | |
| 2453 | if ( ! CurrNode->isRep() ) |
| 2454 | continue; |
| 2455 | } |
| 2456 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2457 | Seen.clear(); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2458 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2459 | /* Now process the constraints for this node. */ |
| 2460 | for (std::list<Constraint>::iterator li = CurrNode->Constraints.begin(); |
| 2461 | li != CurrNode->Constraints.end(); ) { |
| 2462 | li->Src = FindNode(li->Src); |
| 2463 | li->Dest = FindNode(li->Dest); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2464 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2465 | // Delete redundant constraints |
| 2466 | if( Seen.count(*li) ) { |
| 2467 | std::list<Constraint>::iterator lk = li; li++; |
| 2468 | |
| 2469 | CurrNode->Constraints.erase(lk); |
| 2470 | ++NumErased; |
| 2471 | continue; |
| 2472 | } |
| 2473 | Seen.insert(*li); |
| 2474 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2475 | // Src and Dest will be the vars we are going to process. |
| 2476 | // This may look a bit ugly, but what it does is allow us to process |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2477 | // both store and load constraints with the same code. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2478 | // Load constraints say that every member of our RHS solution has K |
| 2479 | // added to it, and that variable gets an edge to LHS. We also union |
| 2480 | // RHS+K's solution into the LHS solution. |
| 2481 | // Store constraints say that every member of our LHS solution has K |
| 2482 | // added to it, and that variable gets an edge from RHS. We also union |
| 2483 | // RHS's solution into the LHS+K solution. |
| 2484 | unsigned *Src; |
| 2485 | unsigned *Dest; |
| 2486 | unsigned K = li->Offset; |
| 2487 | unsigned CurrMember; |
| 2488 | if (li->Type == Constraint::Load) { |
| 2489 | Src = &CurrMember; |
| 2490 | Dest = &li->Dest; |
| 2491 | } else if (li->Type == Constraint::Store) { |
| 2492 | Src = &li->Src; |
| 2493 | Dest = &CurrMember; |
| 2494 | } else { |
| 2495 | // TODO Handle offseted copy constraint |
| 2496 | li++; |
| 2497 | continue; |
| 2498 | } |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2499 | |
| 2500 | // See if we can use Hybrid Cycle Detection (that is, check |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2501 | // if it was a statically detected offline equivalence that |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2502 | // involves pointers; if so, remove the redundant constraints). |
| 2503 | if( SCC && K == 0 ) { |
| 2504 | #if FULL_UNIVERSAL |
| 2505 | CurrMember = Rep; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2506 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2507 | if (GraphNodes[*Src].Edges->test_and_set(*Dest)) |
| 2508 | if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo)) |
| 2509 | NextWL->insert(&GraphNodes[*Dest]); |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2510 | #else |
| 2511 | for (unsigned i=0; i < RSV.size(); ++i) { |
| 2512 | CurrMember = RSV[i]; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2513 | |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2514 | if (*Dest < NumberSpecialNodes) |
| 2515 | continue; |
| 2516 | if (GraphNodes[*Src].Edges->test_and_set(*Dest)) |
| 2517 | if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo)) |
| 2518 | NextWL->insert(&GraphNodes[*Dest]); |
| 2519 | } |
| 2520 | #endif |
| 2521 | // since all future elements of the points-to set will be |
| 2522 | // equivalent to the current ones, the complex constraints |
| 2523 | // become redundant. |
| 2524 | // |
| 2525 | std::list<Constraint>::iterator lk = li; li++; |
| 2526 | #if !FULL_UNIVERSAL |
| 2527 | // In this case, we can still erase the constraints when the |
| 2528 | // elements of the points-to sets are referenced by *Dest, |
| 2529 | // but not when they are referenced by *Src (i.e. for a Load |
| 2530 | // constraint). This is because if another special variable is |
| 2531 | // put into the points-to set later, we still need to add the |
| 2532 | // new edge from that special variable. |
| 2533 | if( lk->Type != Constraint::Load) |
| 2534 | #endif |
| 2535 | GraphNodes[CurrNodeIndex].Constraints.erase(lk); |
| 2536 | } else { |
| 2537 | const SparseBitVector<> &Solution = CurrPointsTo; |
| 2538 | |
| 2539 | for (SparseBitVector<>::iterator bi = Solution.begin(); |
| 2540 | bi != Solution.end(); |
| 2541 | ++bi) { |
| 2542 | CurrMember = *bi; |
| 2543 | |
| 2544 | // Need to increment the member by K since that is where we are |
| 2545 | // supposed to copy to/from. Note that in positive weight cycles, |
| 2546 | // which occur in address taking of fields, K can go past |
| 2547 | // MaxK[CurrMember] elements, even though that is all it could point |
| 2548 | // to. |
| 2549 | if (K > 0 && K > MaxK[CurrMember]) |
| 2550 | continue; |
| 2551 | else |
| 2552 | CurrMember = FindNode(CurrMember + K); |
| 2553 | |
| 2554 | // Add an edge to the graph, so we can just do regular |
| 2555 | // bitmap ior next time. It may also let us notice a cycle. |
| 2556 | #if !FULL_UNIVERSAL |
| 2557 | if (*Dest < NumberSpecialNodes) |
| 2558 | continue; |
| 2559 | #endif |
| 2560 | if (GraphNodes[*Src].Edges->test_and_set(*Dest)) |
| 2561 | if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo)) |
| 2562 | NextWL->insert(&GraphNodes[*Dest]); |
| 2563 | |
| 2564 | } |
| 2565 | li++; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2566 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2567 | } |
| 2568 | SparseBitVector<> NewEdges; |
| 2569 | SparseBitVector<> ToErase; |
| 2570 | |
| 2571 | // Now all we have left to do is propagate points-to info along the |
| 2572 | // edges, erasing the redundant edges. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2573 | for (SparseBitVector<>::iterator bi = CurrNode->Edges->begin(); |
| 2574 | bi != CurrNode->Edges->end(); |
| 2575 | ++bi) { |
| 2576 | |
| 2577 | unsigned DestVar = *bi; |
| 2578 | unsigned Rep = FindNode(DestVar); |
| 2579 | |
Bill Wendling | f059deb | 2008-02-26 10:51:52 +0000 | [diff] [blame] | 2580 | // If we ended up with this node as our destination, or we've already |
| 2581 | // got an edge for the representative, delete the current edge. |
| 2582 | if (Rep == CurrNodeIndex || |
| 2583 | (Rep != DestVar && NewEdges.test(Rep))) { |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2584 | ToErase.set(DestVar); |
| 2585 | continue; |
Bill Wendling | f059deb | 2008-02-26 10:51:52 +0000 | [diff] [blame] | 2586 | } |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2587 | |
Bill Wendling | f059deb | 2008-02-26 10:51:52 +0000 | [diff] [blame] | 2588 | std::pair<unsigned,unsigned> edge(CurrNodeIndex,Rep); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2589 | |
| 2590 | // This is where we do lazy cycle detection. |
| 2591 | // If this is a cycle candidate (equal points-to sets and this |
| 2592 | // particular edge has not been cycle-checked previously), add to the |
| 2593 | // list to check for cycles on the next iteration. |
| 2594 | if (!EdgesChecked.count(edge) && |
| 2595 | *(GraphNodes[Rep].PointsTo) == *(CurrNode->PointsTo)) { |
| 2596 | EdgesChecked.insert(edge); |
| 2597 | TarjanWL.push(Rep); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2598 | } |
| 2599 | // Union the points-to sets into the dest |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2600 | #if !FULL_UNIVERSAL |
| 2601 | if (Rep >= NumberSpecialNodes) |
| 2602 | #endif |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2603 | if (GraphNodes[Rep].PointsTo |= CurrPointsTo) { |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2604 | NextWL->insert(&GraphNodes[Rep]); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2605 | } |
| 2606 | // If this edge's destination was collapsed, rewrite the edge. |
| 2607 | if (Rep != DestVar) { |
| 2608 | ToErase.set(DestVar); |
| 2609 | NewEdges.set(Rep); |
| 2610 | } |
| 2611 | } |
| 2612 | CurrNode->Edges->intersectWithComplement(ToErase); |
| 2613 | CurrNode->Edges |= NewEdges; |
| 2614 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2615 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2616 | // Switch to other work list. |
| 2617 | WorkList* t = CurrWL; CurrWL = NextWL; NextWL = t; |
| 2618 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2619 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2620 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2621 | Node2DFS.clear(); |
| 2622 | Node2Deleted.clear(); |
| 2623 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 2624 | Node *N = &GraphNodes[i]; |
| 2625 | delete N->OldPointsTo; |
| 2626 | delete N->Edges; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2627 | } |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2628 | SDTActive = false; |
| 2629 | SDT.clear(); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2632 | //===----------------------------------------------------------------------===// |
| 2633 | // Union-Find |
| 2634 | //===----------------------------------------------------------------------===// |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2635 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2636 | // Unite nodes First and Second, returning the one which is now the |
| 2637 | // representative node. First and Second are indexes into GraphNodes |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2638 | unsigned Andersens::UniteNodes(unsigned First, unsigned Second, |
| 2639 | bool UnionByRank) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2640 | assert (First < GraphNodes.size() && Second < GraphNodes.size() && |
| 2641 | "Attempting to merge nodes that don't exist"); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2642 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2643 | Node *FirstNode = &GraphNodes[First]; |
| 2644 | Node *SecondNode = &GraphNodes[Second]; |
| 2645 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2646 | assert (SecondNode->isRep() && FirstNode->isRep() && |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2647 | "Trying to unite two non-representative nodes!"); |
| 2648 | if (First == Second) |
| 2649 | return First; |
| 2650 | |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2651 | if (UnionByRank) { |
| 2652 | int RankFirst = (int) FirstNode ->NodeRep; |
| 2653 | int RankSecond = (int) SecondNode->NodeRep; |
| 2654 | |
| 2655 | // Rank starts at -1 and gets decremented as it increases. |
| 2656 | // Translation: higher rank, lower NodeRep value, which is always negative. |
| 2657 | if (RankFirst > RankSecond) { |
| 2658 | unsigned t = First; First = Second; Second = t; |
| 2659 | Node* tp = FirstNode; FirstNode = SecondNode; SecondNode = tp; |
| 2660 | } else if (RankFirst == RankSecond) { |
| 2661 | FirstNode->NodeRep = (unsigned) (RankFirst - 1); |
| 2662 | } |
| 2663 | } |
| 2664 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2665 | SecondNode->NodeRep = First; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2666 | #if !FULL_UNIVERSAL |
| 2667 | if (First >= NumberSpecialNodes) |
| 2668 | #endif |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2669 | if (FirstNode->PointsTo && SecondNode->PointsTo) |
| 2670 | FirstNode->PointsTo |= *(SecondNode->PointsTo); |
| 2671 | if (FirstNode->Edges && SecondNode->Edges) |
| 2672 | FirstNode->Edges |= *(SecondNode->Edges); |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2673 | if (!SecondNode->Constraints.empty()) |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2674 | FirstNode->Constraints.splice(FirstNode->Constraints.begin(), |
| 2675 | SecondNode->Constraints); |
| 2676 | if (FirstNode->OldPointsTo) { |
| 2677 | delete FirstNode->OldPointsTo; |
| 2678 | FirstNode->OldPointsTo = new SparseBitVector<>; |
| 2679 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2680 | |
| 2681 | // Destroy interesting parts of the merged-from node. |
| 2682 | delete SecondNode->OldPointsTo; |
| 2683 | delete SecondNode->Edges; |
| 2684 | delete SecondNode->PointsTo; |
| 2685 | SecondNode->Edges = NULL; |
| 2686 | SecondNode->PointsTo = NULL; |
| 2687 | SecondNode->OldPointsTo = NULL; |
| 2688 | |
| 2689 | NumUnified++; |
| 2690 | DOUT << "Unified Node "; |
| 2691 | DEBUG(PrintNode(FirstNode)); |
| 2692 | DOUT << " and Node "; |
| 2693 | DEBUG(PrintNode(SecondNode)); |
| 2694 | DOUT << "\n"; |
| 2695 | |
Daniel Berlin | c864edb | 2008-03-05 19:31:47 +0000 | [diff] [blame^] | 2696 | if (SDTActive) |
| 2697 | if (SDT[Second] >= 0) |
| 2698 | if (SDT[First] < 0) |
| 2699 | SDT[First] = SDT[Second]; |
| 2700 | else { |
| 2701 | UniteNodes( FindNode(SDT[First]), FindNode(SDT[Second]) ); |
| 2702 | First = FindNode(First); |
| 2703 | } |
| 2704 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2705 | return First; |
| 2706 | } |
| 2707 | |
| 2708 | // Find the index into GraphNodes of the node representing Node, performing |
| 2709 | // path compression along the way |
| 2710 | unsigned Andersens::FindNode(unsigned NodeIndex) { |
| 2711 | assert (NodeIndex < GraphNodes.size() |
| 2712 | && "Attempting to find a node that can't exist"); |
| 2713 | Node *N = &GraphNodes[NodeIndex]; |
Daniel Berlin | 3a3f163 | 2007-12-12 00:37:04 +0000 | [diff] [blame] | 2714 | if (N->isRep()) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2715 | return NodeIndex; |
| 2716 | else |
| 2717 | return (N->NodeRep = FindNode(N->NodeRep)); |
| 2718 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2719 | |
| 2720 | //===----------------------------------------------------------------------===// |
| 2721 | // Debugging Output |
| 2722 | //===----------------------------------------------------------------------===// |
| 2723 | |
| 2724 | void Andersens::PrintNode(Node *N) { |
| 2725 | if (N == &GraphNodes[UniversalSet]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2726 | cerr << "<universal>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | } else if (N == &GraphNodes[NullPtr]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2729 | cerr << "<nullptr>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2730 | return; |
| 2731 | } else if (N == &GraphNodes[NullObject]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2732 | cerr << "<null>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2733 | return; |
| 2734 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2735 | if (!N->getValue()) { |
| 2736 | cerr << "artificial" << (intptr_t) N; |
| 2737 | return; |
| 2738 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2739 | |
| 2740 | assert(N->getValue() != 0 && "Never set node label!"); |
| 2741 | Value *V = N->getValue(); |
| 2742 | if (Function *F = dyn_cast<Function>(V)) { |
| 2743 | if (isa<PointerType>(F->getFunctionType()->getReturnType()) && |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2744 | N == &GraphNodes[getReturnNode(F)]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2745 | cerr << F->getName() << ":retval"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2746 | return; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2747 | } else if (F->getFunctionType()->isVarArg() && |
| 2748 | N == &GraphNodes[getVarargNode(F)]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2749 | cerr << F->getName() << ":vararg"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2750 | return; |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2755 | cerr << I->getParent()->getParent()->getName() << ":"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2756 | else if (Argument *Arg = dyn_cast<Argument>(V)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2757 | cerr << Arg->getParent()->getName() << ":"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2758 | |
| 2759 | if (V->hasName()) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2760 | cerr << V->getName(); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2761 | else |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2762 | cerr << "(unnamed)"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2763 | |
| 2764 | if (isa<GlobalValue>(V) || isa<AllocationInst>(V)) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2765 | if (N == &GraphNodes[getObject(V)]) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2766 | cerr << "<mem>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2767 | } |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2768 | void Andersens::PrintConstraint(const Constraint &C) { |
| 2769 | if (C.Type == Constraint::Store) { |
| 2770 | cerr << "*"; |
| 2771 | if (C.Offset != 0) |
| 2772 | cerr << "("; |
| 2773 | } |
| 2774 | PrintNode(&GraphNodes[C.Dest]); |
| 2775 | if (C.Type == Constraint::Store && C.Offset != 0) |
| 2776 | cerr << " + " << C.Offset << ")"; |
| 2777 | cerr << " = "; |
| 2778 | if (C.Type == Constraint::Load) { |
| 2779 | cerr << "*"; |
| 2780 | if (C.Offset != 0) |
| 2781 | cerr << "("; |
| 2782 | } |
| 2783 | else if (C.Type == Constraint::AddressOf) |
| 2784 | cerr << "&"; |
| 2785 | PrintNode(&GraphNodes[C.Src]); |
| 2786 | if (C.Offset != 0 && C.Type != Constraint::Store) |
| 2787 | cerr << " + " << C.Offset; |
| 2788 | if (C.Type == Constraint::Load && C.Offset != 0) |
| 2789 | cerr << ")"; |
| 2790 | cerr << "\n"; |
| 2791 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2792 | |
| 2793 | void Andersens::PrintConstraints() { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2794 | cerr << "Constraints:\n"; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2795 | |
Daniel Berlin | d81ccc2 | 2007-09-24 19:45:49 +0000 | [diff] [blame] | 2796 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) |
| 2797 | PrintConstraint(Constraints[i]); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | void Andersens::PrintPointsToGraph() { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 2801 | cerr << "Points-to graph:\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2802 | for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) { |
| 2803 | Node *N = &GraphNodes[i]; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 2804 | if (FindNode (i) != i) { |
| 2805 | PrintNode(N); |
| 2806 | cerr << "\t--> same as "; |
| 2807 | PrintNode(&GraphNodes[FindNode(i)]); |
| 2808 | cerr << "\n"; |
| 2809 | } else { |
| 2810 | cerr << "[" << (N->PointsTo->count()) << "] "; |
| 2811 | PrintNode(N); |
| 2812 | cerr << "\t--> "; |
| 2813 | |
| 2814 | bool first = true; |
| 2815 | for (SparseBitVector<>::iterator bi = N->PointsTo->begin(); |
| 2816 | bi != N->PointsTo->end(); |
| 2817 | ++bi) { |
| 2818 | if (!first) |
| 2819 | cerr << ", "; |
| 2820 | PrintNode(&GraphNodes[*bi]); |
| 2821 | first = false; |
| 2822 | } |
| 2823 | cerr << "\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2824 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 2825 | } |
| 2826 | } |