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