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