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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
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. |
| 19 | // 3. Inclusion constraint solving. |
| 20 | // |
| 21 | // The object identification stage identifies all of the memory objects in the |
| 22 | // program, which includes globals, heap allocated objects, and stack allocated |
| 23 | // objects. |
| 24 | // |
| 25 | // The inclusion constraint identification stage finds all inclusion constraints |
| 26 | // in the program by scanning the program, looking for pointer assignments and |
| 27 | // other statements that effect the points-to graph. For a statement like "A = |
| 28 | // 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] | 29 | // B can point to. Constraints can handle copies, loads, and stores, and |
| 30 | // address taking. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 31 | // |
| 32 | // The inclusion constraint solving phase iteratively propagates the inclusion |
| 33 | // constraints until a fixed point is reached. This is an O(N^3) algorithm. |
| 34 | // |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 35 | // Function constraints are handled as if they were structs with X fields. |
| 36 | // Thus, an access to argument X of function Y is an access to node index |
| 37 | // getNode(Y) + X. This representation allows handling of indirect calls |
| 38 | // without any issues. To wit, an indirect call Y(a,b) is equivalence to |
| 39 | // *(Y + 1) = a, *(Y + 2) = b. |
| 40 | // The return node for a function is always located at getNode(F) + |
| 41 | // CallReturnPos. The arguments start at getNode(F) + CallArgPos. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 42 | // |
Chris Lattner | c7ca32b | 2004-06-05 20:12:36 +0000 | [diff] [blame] | 43 | // Future Improvements: |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 44 | // Offline variable substitution, offline detection of online |
| 45 | // cycles. Use of BDD's. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | #define DEBUG_TYPE "anders-aa" |
| 49 | #include "llvm/Constants.h" |
| 50 | #include "llvm/DerivedTypes.h" |
| 51 | #include "llvm/Instructions.h" |
| 52 | #include "llvm/Module.h" |
| 53 | #include "llvm/Pass.h" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Compiler.h" |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 55 | #include "llvm/Support/InstIterator.h" |
| 56 | #include "llvm/Support/InstVisitor.h" |
| 57 | #include "llvm/Analysis/AliasAnalysis.h" |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 58 | #include "llvm/Analysis/Passes.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 59 | #include "llvm/Support/Debug.h" |
| 60 | #include "llvm/ADT/Statistic.h" |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/SparseBitVector.h" |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 62 | #include <algorithm> |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 63 | #include <set> |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 64 | #include <list> |
| 65 | #include <stack> |
| 66 | #include <vector> |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 67 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 68 | using namespace llvm; |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 69 | STATISTIC(NumIters , "Number of iterations to reach convergence"); |
| 70 | STATISTIC(NumConstraints , "Number of constraints"); |
| 71 | STATISTIC(NumNodes , "Number of nodes"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 72 | STATISTIC(NumUnified , "Number of variables unified"); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 74 | namespace { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 75 | const unsigned SelfRep = (unsigned)-1; |
| 76 | const unsigned Unvisited = (unsigned)-1; |
| 77 | // Position of the function return node relative to the function node. |
| 78 | const unsigned CallReturnPos = 2; |
| 79 | // Position of the function call node relative to the function node. |
| 80 | const unsigned CallFirstArgPos = 3; |
| 81 | |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 82 | class VISIBILITY_HIDDEN Andersens : public ModulePass, public AliasAnalysis, |
| 83 | private InstVisitor<Andersens> { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 84 | class Node; |
| 85 | |
| 86 | /// Constraint - Objects of this structure are used to represent the various |
| 87 | /// constraints identified by the algorithm. The constraints are 'copy', |
| 88 | /// for statements like "A = B", 'load' for statements like "A = *B", |
| 89 | /// 'store' for statements like "*A = B", and AddressOf for statements like |
| 90 | /// A = alloca; The Offset is applied as *(A + K) = B for stores, |
| 91 | /// A = *(B + K) for loads, and A = B + K for copies. It is |
| 92 | /// illegal on addressof constraints (Because it is statically |
| 93 | /// resolvable to A = &C where C = B + K) |
| 94 | |
| 95 | struct Constraint { |
| 96 | enum ConstraintType { Copy, Load, Store, AddressOf } Type; |
| 97 | unsigned Dest; |
| 98 | unsigned Src; |
| 99 | unsigned Offset; |
| 100 | |
| 101 | Constraint(ConstraintType Ty, unsigned D, unsigned S, unsigned O = 0) |
| 102 | : Type(Ty), Dest(D), Src(S), Offset(O) { |
| 103 | assert(Offset == 0 || Ty != AddressOf && |
| 104 | "Offset is illegal on addressof constraints"); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | // Node class - This class is used to represent a node |
| 109 | // in the constraint graph. Due to various optimizations, |
| 110 | // not always the case that there is a mapping from a Node to a |
| 111 | // Value. In particular, we add artificial |
| 112 | // Node's that represent the set of pointed-to variables |
| 113 | // shared for each location equivalent Node. |
| 114 | struct Node { |
| 115 | Value *Val; |
| 116 | SparseBitVector<> *Edges; |
| 117 | SparseBitVector<> *PointsTo; |
| 118 | SparseBitVector<> *OldPointsTo; |
| 119 | bool Changed; |
| 120 | std::list<Constraint> Constraints; |
| 121 | |
| 122 | // Nodes in cycles (or in equivalence classes) are united |
| 123 | // together using a standard union-find representation with path |
| 124 | // compression. NodeRep gives the index into GraphNodes |
| 125 | // representative for this one. |
| 126 | unsigned NodeRep; public: |
| 127 | |
| 128 | Node() : Val(0), Edges(0), PointsTo(0), OldPointsTo(0), Changed(false), |
| 129 | NodeRep(SelfRep) { |
| 130 | } |
| 131 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 132 | Node *setValue(Value *V) { |
| 133 | assert(Val == 0 && "Value already set for this node!"); |
| 134 | Val = V; |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | /// getValue - Return the LLVM value corresponding to this node. |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 139 | /// |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 140 | Value *getValue() const { return Val; } |
| 141 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 142 | /// addPointerTo - Add a pointer to the list of pointees of this node, |
| 143 | /// returning true if this caused a new pointer to be added, or false if |
| 144 | /// we already knew about the points-to relation. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 145 | bool addPointerTo(unsigned Node) { |
| 146 | return PointsTo->test_and_set(Node); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /// intersects - Return true if the points-to set of this node intersects |
| 150 | /// with the points-to set of the specified node. |
| 151 | bool intersects(Node *N) const; |
| 152 | |
| 153 | /// intersectsIgnoring - Return true if the points-to set of this node |
| 154 | /// intersects with the points-to set of the specified node on any nodes |
| 155 | /// except for the specified node to ignore. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 156 | bool intersectsIgnoring(Node *N, unsigned) const; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /// GraphNodes - This vector is populated as part of the object |
| 160 | /// identification stage of the analysis, which populates this vector with a |
| 161 | /// node for each memory object and fills in the ValueNodes map. |
| 162 | std::vector<Node> GraphNodes; |
| 163 | |
| 164 | /// ValueNodes - This map indicates the Node that a particular Value* is |
| 165 | /// represented by. This contains entries for all pointers. |
| 166 | std::map<Value*, unsigned> ValueNodes; |
| 167 | |
| 168 | /// ObjectNodes - This map contains entries for each memory object in the |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 169 | /// program: globals, alloca's and mallocs. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 170 | std::map<Value*, unsigned> ObjectNodes; |
| 171 | |
| 172 | /// ReturnNodes - This map contains an entry for each function in the |
| 173 | /// program that returns a value. |
| 174 | std::map<Function*, unsigned> ReturnNodes; |
| 175 | |
| 176 | /// VarargNodes - This map contains the entry used to represent all pointers |
| 177 | /// passed through the varargs portion of a function call for a particular |
| 178 | /// function. An entry is not present in this map for functions that do not |
| 179 | /// take variable arguments. |
| 180 | std::map<Function*, unsigned> VarargNodes; |
| 181 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 182 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 183 | /// Constraints - This vector contains a list of all of the constraints |
| 184 | /// identified by the program. |
| 185 | std::vector<Constraint> Constraints; |
| 186 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 187 | // Map from graph node to maximum K value that is allowed (For functions, |
| 188 | // this is equivalent to the number of arguments + CallFirstArgPos) |
| 189 | std::map<unsigned, unsigned> MaxK; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 190 | |
| 191 | /// This enum defines the GraphNodes indices that correspond to important |
| 192 | /// fixed sets. |
| 193 | enum { |
| 194 | UniversalSet = 0, |
| 195 | NullPtr = 1, |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 196 | NullObject = 2 |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 197 | }; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 198 | // Stack for Tarjans |
| 199 | std::stack<unsigned> SCCStack; |
| 200 | // Topological Index -> Graph node |
| 201 | std::vector<unsigned> Topo2Node; |
| 202 | // Graph Node -> Topological Index; |
| 203 | std::vector<unsigned> Node2Topo; |
| 204 | // Map from Graph Node to DFS number |
| 205 | std::vector<unsigned> Node2DFS; |
| 206 | // Map from Graph Node to Deleted from graph. |
| 207 | std::vector<bool> Node2Deleted; |
| 208 | // Current DFS and RPO numbers |
| 209 | unsigned DFSNumber; |
| 210 | unsigned RPONumber; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 211 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 212 | public: |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 213 | static char ID; |
| 214 | Andersens() : ModulePass((intptr_t)&ID) {} |
| 215 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 216 | bool runOnModule(Module &M) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 217 | InitializeAliasAnalysis(this); |
| 218 | IdentifyObjects(M); |
| 219 | CollectConstraints(M); |
| 220 | DEBUG(PrintConstraints()); |
| 221 | SolveConstraints(); |
| 222 | DEBUG(PrintPointsToGraph()); |
| 223 | |
| 224 | // Free the constraints list, as we don't need it to respond to alias |
| 225 | // requests. |
| 226 | ObjectNodes.clear(); |
| 227 | ReturnNodes.clear(); |
| 228 | VarargNodes.clear(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 229 | std::vector<Constraint>().swap(Constraints); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | |
| 233 | void releaseMemory() { |
| 234 | // FIXME: Until we have transitively required passes working correctly, |
| 235 | // this cannot be enabled! Otherwise, using -count-aa with the pass |
| 236 | // causes memory to be freed too early. :( |
| 237 | #if 0 |
| 238 | // The memory objects and ValueNodes data structures at the only ones that |
| 239 | // are still live after construction. |
| 240 | std::vector<Node>().swap(GraphNodes); |
| 241 | ValueNodes.clear(); |
| 242 | #endif |
| 243 | } |
| 244 | |
| 245 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 246 | AliasAnalysis::getAnalysisUsage(AU); |
| 247 | AU.setPreservesAll(); // Does not transform code |
| 248 | } |
| 249 | |
| 250 | //------------------------------------------------ |
| 251 | // Implement the AliasAnalysis API |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 252 | // |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 253 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 254 | const Value *V2, unsigned V2Size); |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 255 | virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); |
| 256 | virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 257 | void getMustAliases(Value *P, std::vector<Value*> &RetVals); |
| 258 | bool pointsToConstantMemory(const Value *P); |
| 259 | |
| 260 | virtual void deleteValue(Value *V) { |
| 261 | ValueNodes.erase(V); |
| 262 | getAnalysis<AliasAnalysis>().deleteValue(V); |
| 263 | } |
| 264 | |
| 265 | virtual void copyValue(Value *From, Value *To) { |
| 266 | ValueNodes[To] = ValueNodes[From]; |
| 267 | getAnalysis<AliasAnalysis>().copyValue(From, To); |
| 268 | } |
| 269 | |
| 270 | private: |
| 271 | /// getNode - Return the node corresponding to the specified pointer scalar. |
| 272 | /// |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 273 | unsigned getNode(Value *V) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 274 | if (Constant *C = dyn_cast<Constant>(V)) |
Chris Lattner | df9b7bc | 2004-08-16 05:38:02 +0000 | [diff] [blame] | 275 | if (!isa<GlobalValue>(C)) |
| 276 | return getNodeForConstantPointer(C); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 277 | |
| 278 | std::map<Value*, unsigned>::iterator I = ValueNodes.find(V); |
| 279 | if (I == ValueNodes.end()) { |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 280 | #ifndef NDEBUG |
| 281 | V->dump(); |
| 282 | #endif |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 283 | assert(0 && "Value does not have a node in the points-to graph!"); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 284 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 285 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 286 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 287 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 288 | /// getObject - Return the node corresponding to the memory object for the |
| 289 | /// specified global or allocation instruction. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 290 | unsigned getObject(Value *V) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 291 | std::map<Value*, unsigned>::iterator I = ObjectNodes.find(V); |
| 292 | assert(I != ObjectNodes.end() && |
| 293 | "Value does not have an object in the points-to graph!"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 294 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /// getReturnNode - Return the node representing the return value for the |
| 298 | /// specified function. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 299 | unsigned getReturnNode(Function *F) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 300 | std::map<Function*, unsigned>::iterator I = ReturnNodes.find(F); |
| 301 | assert(I != ReturnNodes.end() && "Function does not return a value!"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 302 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /// getVarargNode - Return the node representing the variable arguments |
| 306 | /// formal for the specified function. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 307 | unsigned getVarargNode(Function *F) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 308 | std::map<Function*, unsigned>::iterator I = VarargNodes.find(F); |
| 309 | assert(I != VarargNodes.end() && "Function does not take var args!"); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 310 | return I->second; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /// getNodeValue - Get the node for the specified LLVM value and set the |
| 314 | /// value for it to be the specified value. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 315 | unsigned getNodeValue(Value &V) { |
| 316 | unsigned Index = getNode(&V); |
| 317 | GraphNodes[Index].setValue(&V); |
| 318 | return Index; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 321 | unsigned UniteNodes(unsigned First, unsigned Second); |
| 322 | unsigned FindNode(unsigned Node); |
| 323 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 324 | void IdentifyObjects(Module &M); |
| 325 | void CollectConstraints(Module &M); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 326 | bool AnalyzeUsesOfFunction(Value *); |
| 327 | void CreateConstraintGraph(); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 328 | void SolveConstraints(); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 329 | void QueryNode(unsigned Node); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 330 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 331 | unsigned getNodeForConstantPointer(Constant *C); |
| 332 | unsigned getNodeForConstantPointerTarget(Constant *C); |
| 333 | void AddGlobalInitializerConstraints(unsigned, Constant *C); |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 334 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 335 | void AddConstraintsForNonInternalLinkage(Function *F); |
| 336 | void AddConstraintsForCall(CallSite CS, Function *F); |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 337 | bool AddConstraintsForExternalCall(CallSite CS, Function *F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 338 | |
| 339 | |
| 340 | void PrintNode(Node *N); |
| 341 | void PrintConstraints(); |
| 342 | void PrintPointsToGraph(); |
| 343 | |
| 344 | //===------------------------------------------------------------------===// |
| 345 | // Instruction visitation methods for adding constraints |
| 346 | // |
| 347 | friend class InstVisitor<Andersens>; |
| 348 | void visitReturnInst(ReturnInst &RI); |
| 349 | void visitInvokeInst(InvokeInst &II) { visitCallSite(CallSite(&II)); } |
| 350 | void visitCallInst(CallInst &CI) { visitCallSite(CallSite(&CI)); } |
| 351 | void visitCallSite(CallSite CS); |
| 352 | void visitAllocationInst(AllocationInst &AI); |
| 353 | void visitLoadInst(LoadInst &LI); |
| 354 | void visitStoreInst(StoreInst &SI); |
| 355 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 356 | void visitPHINode(PHINode &PN); |
| 357 | void visitCastInst(CastInst &CI); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 358 | void visitICmpInst(ICmpInst &ICI) {} // NOOP! |
| 359 | void visitFCmpInst(FCmpInst &ICI) {} // NOOP! |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 360 | void visitSelectInst(SelectInst &SI); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 361 | void visitVAArg(VAArgInst &I); |
| 362 | void visitInstruction(Instruction &I); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 363 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 366 | char Andersens::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 367 | RegisterPass<Andersens> X("anders-aa", |
| 368 | "Andersen's Interprocedural Alias Analysis"); |
Chris Lattner | a537017 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 369 | RegisterAnalysisGroup<AliasAnalysis> Y(X); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Jeff Cohen | 534927d | 2005-01-08 22:01:16 +0000 | [diff] [blame] | 372 | ModulePass *llvm::createAndersensPass() { return new Andersens(); } |
| 373 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 374 | //===----------------------------------------------------------------------===// |
| 375 | // AliasAnalysis Interface Implementation |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | AliasAnalysis::AliasResult Andersens::alias(const Value *V1, unsigned V1Size, |
| 379 | const Value *V2, unsigned V2Size) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 380 | Node *N1 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V1)))]; |
| 381 | Node *N2 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V2)))]; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 382 | |
| 383 | // Check to see if the two pointers are known to not alias. They don't alias |
| 384 | // if their points-to sets do not intersect. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 385 | if (!N1->intersectsIgnoring(N2, NullObject)) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 386 | return NoAlias; |
| 387 | |
| 388 | return AliasAnalysis::alias(V1, V1Size, V2, V2Size); |
| 389 | } |
| 390 | |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 391 | AliasAnalysis::ModRefResult |
| 392 | Andersens::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 393 | // The only thing useful that we can contribute for mod/ref information is |
| 394 | // when calling external function calls: if we know that memory never escapes |
| 395 | // from the program, it cannot be modified by an external call. |
| 396 | // |
| 397 | // NOTE: This is not really safe, at least not when the entire program is not |
| 398 | // available. The deal is that the external function could call back into the |
| 399 | // program and modify stuff. We ignore this technical niggle for now. This |
| 400 | // is, after all, a "research quality" implementation of Andersen's analysis. |
| 401 | if (Function *F = CS.getCalledFunction()) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 402 | if (F->isDeclaration()) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 403 | Node *N1 = &GraphNodes[FindNode(getNode(P))]; |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 404 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 405 | if (N1->PointsTo->empty()) |
| 406 | return NoModRef; |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 407 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 408 | if (!N1->PointsTo->test(UniversalSet)) |
Chris Lattner | f392c64 | 2005-03-28 06:21:17 +0000 | [diff] [blame] | 409 | return NoModRef; // P doesn't point to the universal set. |
| 410 | } |
| 411 | |
| 412 | return AliasAnalysis::getModRefInfo(CS, P, Size); |
| 413 | } |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 414 | |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 415 | AliasAnalysis::ModRefResult |
| 416 | Andersens::getModRefInfo(CallSite CS1, CallSite CS2) { |
| 417 | return AliasAnalysis::getModRefInfo(CS1,CS2); |
| 418 | } |
| 419 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 420 | /// getMustAlias - We can provide must alias information if we know that a |
| 421 | /// pointer can only point to a specific function or the null pointer. |
| 422 | /// Unfortunately we cannot determine must-alias information for global |
| 423 | /// variables or any other memory memory objects because we do not track whether |
| 424 | /// a pointer points to the beginning of an object or a field of it. |
| 425 | void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 426 | Node *N = &GraphNodes[FindNode(getNode(P))]; |
| 427 | if (N->PointsTo->count() == 1) { |
| 428 | Node *Pointee = &GraphNodes[N->PointsTo->find_first()]; |
| 429 | // If a function is the only object in the points-to set, then it must be |
| 430 | // the destination. Note that we can't handle global variables here, |
| 431 | // because we don't know if the pointer is actually pointing to a field of |
| 432 | // the global or to the beginning of it. |
| 433 | if (Value *V = Pointee->getValue()) { |
| 434 | if (Function *F = dyn_cast<Function>(V)) |
| 435 | RetVals.push_back(F); |
| 436 | } else { |
| 437 | // If the object in the points-to set is the null object, then the null |
| 438 | // pointer is a must alias. |
| 439 | if (Pointee == &GraphNodes[NullObject]) |
| 440 | RetVals.push_back(Constant::getNullValue(P->getType())); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 443 | AliasAnalysis::getMustAliases(P, RetVals); |
| 444 | } |
| 445 | |
| 446 | /// pointsToConstantMemory - If we can determine that this pointer only points |
| 447 | /// to constant memory, return true. In practice, this means that if the |
| 448 | /// pointer can only point to constant globals, functions, or the null pointer, |
| 449 | /// return true. |
| 450 | /// |
| 451 | bool Andersens::pointsToConstantMemory(const Value *P) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 452 | Node *N = &GraphNodes[FindNode(getNode((Value*)P))]; |
| 453 | unsigned i; |
| 454 | |
| 455 | for (SparseBitVector<>::iterator bi = N->PointsTo->begin(); |
| 456 | bi != N->PointsTo->end(); |
| 457 | ++bi) { |
| 458 | i = *bi; |
| 459 | Node *Pointee = &GraphNodes[i]; |
| 460 | if (Value *V = Pointee->getValue()) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 461 | if (!isa<GlobalValue>(V) || (isa<GlobalVariable>(V) && |
| 462 | !cast<GlobalVariable>(V)->isConstant())) |
| 463 | return AliasAnalysis::pointsToConstantMemory(P); |
| 464 | } else { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 465 | if (i != NullObject) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 466 | return AliasAnalysis::pointsToConstantMemory(P); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | //===----------------------------------------------------------------------===// |
| 474 | // Object Identification Phase |
| 475 | //===----------------------------------------------------------------------===// |
| 476 | |
| 477 | /// IdentifyObjects - This stage scans the program, adding an entry to the |
| 478 | /// GraphNodes list for each memory object in the program (global stack or |
| 479 | /// heap), and populates the ValueNodes and ObjectNodes maps for these objects. |
| 480 | /// |
| 481 | void Andersens::IdentifyObjects(Module &M) { |
| 482 | unsigned NumObjects = 0; |
| 483 | |
| 484 | // Object #0 is always the universal set: the object that we don't know |
| 485 | // anything about. |
| 486 | assert(NumObjects == UniversalSet && "Something changed!"); |
| 487 | ++NumObjects; |
| 488 | |
| 489 | // Object #1 always represents the null pointer. |
| 490 | assert(NumObjects == NullPtr && "Something changed!"); |
| 491 | ++NumObjects; |
| 492 | |
| 493 | // Object #2 always represents the null object (the object pointed to by null) |
| 494 | assert(NumObjects == NullObject && "Something changed!"); |
| 495 | ++NumObjects; |
| 496 | |
| 497 | // Add all the globals first. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 498 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 499 | I != E; ++I) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 500 | ObjectNodes[I] = NumObjects++; |
| 501 | ValueNodes[I] = NumObjects++; |
| 502 | } |
| 503 | |
| 504 | // Add nodes for all of the functions and the instructions inside of them. |
| 505 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 506 | // The function itself is a memory object. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 507 | unsigned First = NumObjects; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 508 | ValueNodes[F] = NumObjects++; |
| 509 | ObjectNodes[F] = NumObjects++; |
| 510 | if (isa<PointerType>(F->getFunctionType()->getReturnType())) |
| 511 | ReturnNodes[F] = NumObjects++; |
| 512 | if (F->getFunctionType()->isVarArg()) |
| 513 | VarargNodes[F] = NumObjects++; |
| 514 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 515 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 516 | // Add nodes for all of the incoming pointer arguments. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 517 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 518 | I != E; ++I) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 519 | if (isa<PointerType>(I->getType())) |
| 520 | ValueNodes[I] = NumObjects++; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 521 | MaxK[First] = NumObjects - First; |
| 522 | MaxK[First + 1] = NumObjects - First - 1; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 523 | |
| 524 | // Scan the function body, creating a memory object for each heap/stack |
| 525 | // allocation in the body of the function and a node to represent all |
| 526 | // pointer values defined by instructions and used as operands. |
| 527 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 528 | // If this is an heap or stack allocation, create a node for the memory |
| 529 | // object. |
| 530 | if (isa<PointerType>(II->getType())) { |
| 531 | ValueNodes[&*II] = NumObjects++; |
| 532 | if (AllocationInst *AI = dyn_cast<AllocationInst>(&*II)) |
| 533 | ObjectNodes[AI] = NumObjects++; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // Now that we know how many objects to create, make them all now! |
| 539 | GraphNodes.resize(NumObjects); |
| 540 | NumNodes += NumObjects; |
| 541 | } |
| 542 | |
| 543 | //===----------------------------------------------------------------------===// |
| 544 | // Constraint Identification Phase |
| 545 | //===----------------------------------------------------------------------===// |
| 546 | |
| 547 | /// getNodeForConstantPointer - Return the node corresponding to the constant |
| 548 | /// pointer itself. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 549 | unsigned Andersens::getNodeForConstantPointer(Constant *C) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 550 | assert(isa<PointerType>(C->getType()) && "Not a constant pointer!"); |
| 551 | |
Chris Lattner | 267a1b0 | 2005-03-27 18:58:23 +0000 | [diff] [blame] | 552 | if (isa<ConstantPointerNull>(C) || isa<UndefValue>(C)) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 553 | return NullPtr; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 554 | else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 555 | return getNode(GV); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 556 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 557 | switch (CE->getOpcode()) { |
| 558 | case Instruction::GetElementPtr: |
| 559 | return getNodeForConstantPointer(CE->getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 560 | case Instruction::IntToPtr: |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 561 | return UniversalSet; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 562 | case Instruction::BitCast: |
| 563 | return getNodeForConstantPointer(CE->getOperand(0)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 564 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 565 | cerr << "Constant Expr not yet handled: " << *CE << "\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 566 | assert(0); |
| 567 | } |
| 568 | } else { |
| 569 | assert(0 && "Unknown constant pointer!"); |
| 570 | } |
Chris Lattner | 1fc3739 | 2004-05-27 20:57:01 +0000 | [diff] [blame] | 571 | return 0; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /// getNodeForConstantPointerTarget - Return the node POINTED TO by the |
| 575 | /// specified constant pointer. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 576 | unsigned Andersens::getNodeForConstantPointerTarget(Constant *C) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 577 | assert(isa<PointerType>(C->getType()) && "Not a constant pointer!"); |
| 578 | |
| 579 | if (isa<ConstantPointerNull>(C)) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 580 | return NullObject; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 581 | else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 582 | return getObject(GV); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 583 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 584 | switch (CE->getOpcode()) { |
| 585 | case Instruction::GetElementPtr: |
| 586 | return getNodeForConstantPointerTarget(CE->getOperand(0)); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 587 | case Instruction::IntToPtr: |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 588 | return UniversalSet; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 589 | case Instruction::BitCast: |
| 590 | return getNodeForConstantPointerTarget(CE->getOperand(0)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 591 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 592 | cerr << "Constant Expr not yet handled: " << *CE << "\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 593 | assert(0); |
| 594 | } |
| 595 | } else { |
| 596 | assert(0 && "Unknown constant pointer!"); |
| 597 | } |
Chris Lattner | 1fc3739 | 2004-05-27 20:57:01 +0000 | [diff] [blame] | 598 | return 0; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /// AddGlobalInitializerConstraints - Add inclusion constraints for the memory |
| 602 | /// object N, which contains values indicated by C. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 603 | void Andersens::AddGlobalInitializerConstraints(unsigned NodeIndex, |
| 604 | Constant *C) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 605 | if (C->getType()->isFirstClassType()) { |
| 606 | if (isa<PointerType>(C->getType())) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 607 | Constraints.push_back(Constraint(Constraint::Copy, NodeIndex, |
| 608 | getNodeForConstantPointer(C))); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 609 | } else if (C->isNullValue()) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 610 | Constraints.push_back(Constraint(Constraint::Copy, NodeIndex, |
| 611 | NullObject)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 612 | return; |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 613 | } else if (!isa<UndefValue>(C)) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 614 | // If this is an array or struct, include constraints for each element. |
| 615 | assert(isa<ConstantArray>(C) || isa<ConstantStruct>(C)); |
| 616 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 617 | AddGlobalInitializerConstraints(NodeIndex, |
| 618 | cast<Constant>(C->getOperand(i))); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 622 | /// AddConstraintsForNonInternalLinkage - If this function does not have |
| 623 | /// internal linkage, realize that we can't trust anything passed into or |
| 624 | /// returned by this function. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 625 | void Andersens::AddConstraintsForNonInternalLinkage(Function *F) { |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 626 | 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] | 627 | if (isa<PointerType>(I->getType())) |
| 628 | // If this is an argument of an externally accessible function, the |
| 629 | // incoming pointer might point to anything. |
| 630 | Constraints.push_back(Constraint(Constraint::Copy, getNode(I), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 631 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 634 | /// AddConstraintsForCall - If this is a call to a "known" function, add the |
| 635 | /// constraints and return true. If this is a call to an unknown function, |
| 636 | /// return false. |
| 637 | bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 638 | assert(F->isDeclaration() && "Not an external function!"); |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 639 | |
| 640 | // These functions don't induce any points-to constraints. |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 641 | if (F->getName() == "atoi" || F->getName() == "atof" || |
| 642 | F->getName() == "atol" || F->getName() == "atoll" || |
| 643 | F->getName() == "remove" || F->getName() == "unlink" || |
| 644 | F->getName() == "rename" || F->getName() == "memcmp" || |
Chris Lattner | 01ac91e | 2006-03-03 01:21:36 +0000 | [diff] [blame] | 645 | F->getName() == "llvm.memset.i32" || |
| 646 | F->getName() == "llvm.memset.i64" || |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 647 | F->getName() == "strcmp" || F->getName() == "strncmp" || |
| 648 | F->getName() == "execl" || F->getName() == "execlp" || |
| 649 | F->getName() == "execle" || F->getName() == "execv" || |
| 650 | F->getName() == "execvp" || F->getName() == "chmod" || |
| 651 | F->getName() == "puts" || F->getName() == "write" || |
| 652 | F->getName() == "open" || F->getName() == "create" || |
| 653 | F->getName() == "truncate" || F->getName() == "chdir" || |
| 654 | F->getName() == "mkdir" || F->getName() == "rmdir" || |
| 655 | F->getName() == "read" || F->getName() == "pipe" || |
| 656 | F->getName() == "wait" || F->getName() == "time" || |
| 657 | F->getName() == "stat" || F->getName() == "fstat" || |
| 658 | F->getName() == "lstat" || F->getName() == "strtod" || |
| 659 | F->getName() == "strtof" || F->getName() == "strtold" || |
| 660 | F->getName() == "fopen" || F->getName() == "fdopen" || |
| 661 | F->getName() == "freopen" || |
| 662 | F->getName() == "fflush" || F->getName() == "feof" || |
| 663 | F->getName() == "fileno" || F->getName() == "clearerr" || |
| 664 | F->getName() == "rewind" || F->getName() == "ftell" || |
| 665 | F->getName() == "ferror" || F->getName() == "fgetc" || |
| 666 | F->getName() == "fgetc" || F->getName() == "_IO_getc" || |
| 667 | F->getName() == "fwrite" || F->getName() == "fread" || |
| 668 | F->getName() == "fgets" || F->getName() == "ungetc" || |
| 669 | F->getName() == "fputc" || |
| 670 | F->getName() == "fputs" || F->getName() == "putc" || |
| 671 | F->getName() == "ftell" || F->getName() == "rewind" || |
| 672 | F->getName() == "_IO_putc" || F->getName() == "fseek" || |
| 673 | F->getName() == "fgetpos" || F->getName() == "fsetpos" || |
| 674 | F->getName() == "printf" || F->getName() == "fprintf" || |
| 675 | F->getName() == "sprintf" || F->getName() == "vprintf" || |
| 676 | F->getName() == "vfprintf" || F->getName() == "vsprintf" || |
| 677 | F->getName() == "scanf" || F->getName() == "fscanf" || |
| 678 | F->getName() == "sscanf" || F->getName() == "__assert_fail" || |
| 679 | F->getName() == "modf") |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 680 | return true; |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 682 | |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 683 | // These functions do induce points-to edges. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 684 | if (F->getName() == "llvm.memcpy.i32" || F->getName() == "llvm.memcpy.i64" || |
Chris Lattner | 01ac91e | 2006-03-03 01:21:36 +0000 | [diff] [blame] | 685 | F->getName() == "llvm.memmove.i32" ||F->getName() == "llvm.memmove.i64" || |
Chris Lattner | 4de57fd | 2005-03-29 06:52:20 +0000 | [diff] [blame] | 686 | F->getName() == "memmove") { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 687 | |
| 688 | // *Dest = *Src, which requires an artificial graph node to represent the |
| 689 | // constraint. It is broken up into *Dest = temp, temp = *Src |
| 690 | unsigned FirstArg = getNode(CS.getArgument(0)); |
| 691 | unsigned SecondArg = getNode(CS.getArgument(1)); |
| 692 | unsigned TempArg = GraphNodes.size(); |
| 693 | GraphNodes.push_back(Node()); |
| 694 | Constraints.push_back(Constraint(Constraint::Store, |
| 695 | FirstArg, TempArg)); |
| 696 | Constraints.push_back(Constraint(Constraint::Load, |
| 697 | TempArg, SecondArg)); |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 698 | return true; |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Chris Lattner | 77b5056 | 2005-03-29 20:04:24 +0000 | [diff] [blame] | 701 | // Result = Arg0 |
| 702 | if (F->getName() == "realloc" || F->getName() == "strchr" || |
| 703 | F->getName() == "strrchr" || F->getName() == "strstr" || |
| 704 | F->getName() == "strtok") { |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 705 | Constraints.push_back(Constraint(Constraint::Copy, |
| 706 | getNode(CS.getInstruction()), |
| 707 | getNode(CS.getArgument(0)))); |
| 708 | return true; |
| 709 | } |
| 710 | |
| 711 | return false; |
Chris Lattner | c3c9fd0 | 2005-03-28 04:03:52 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 715 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 716 | /// AnalyzeUsesOfFunction - Look at all of the users of the specified function. |
| 717 | /// If this is used by anything complex (i.e., the address escapes), return |
| 718 | /// true. |
| 719 | bool Andersens::AnalyzeUsesOfFunction(Value *V) { |
| 720 | |
| 721 | if (!isa<PointerType>(V->getType())) return true; |
| 722 | |
| 723 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 724 | if (dyn_cast<LoadInst>(*UI)) { |
| 725 | return false; |
| 726 | } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 727 | if (V == SI->getOperand(1)) { |
| 728 | return false; |
| 729 | } else if (SI->getOperand(1)) { |
| 730 | return true; // Storing the pointer |
| 731 | } |
| 732 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { |
| 733 | if (AnalyzeUsesOfFunction(GEP)) return true; |
| 734 | } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 735 | // Make sure that this is just the function being called, not that it is |
| 736 | // passing into the function. |
| 737 | for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) |
| 738 | if (CI->getOperand(i) == V) return true; |
| 739 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
| 740 | // Make sure that this is just the function being called, not that it is |
| 741 | // passing into the function. |
| 742 | for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i) |
| 743 | if (II->getOperand(i) == V) return true; |
| 744 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { |
| 745 | if (CE->getOpcode() == Instruction::GetElementPtr || |
| 746 | CE->getOpcode() == Instruction::BitCast) { |
| 747 | if (AnalyzeUsesOfFunction(CE)) |
| 748 | return true; |
| 749 | } else { |
| 750 | return true; |
| 751 | } |
| 752 | } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) { |
| 753 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
| 754 | return true; // Allow comparison against null. |
| 755 | } else if (dyn_cast<FreeInst>(*UI)) { |
| 756 | return false; |
| 757 | } else { |
| 758 | return true; |
| 759 | } |
| 760 | return false; |
| 761 | } |
| 762 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 763 | /// CollectConstraints - This stage scans the program, adding a constraint to |
| 764 | /// the Constraints list for each instruction in the program that induces a |
| 765 | /// constraint, and setting up the initial points-to graph. |
| 766 | /// |
| 767 | void Andersens::CollectConstraints(Module &M) { |
| 768 | // First, the universal set points to itself. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 769 | Constraints.push_back(Constraint(Constraint::AddressOf, UniversalSet, |
| 770 | UniversalSet)); |
| 771 | Constraints.push_back(Constraint(Constraint::Store, UniversalSet, |
| 772 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 773 | |
| 774 | // Next, the null pointer points to the null object. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 775 | Constraints.push_back(Constraint(Constraint::AddressOf, NullPtr, NullObject)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 776 | |
| 777 | // Next, add any constraints on global variables and their initializers. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 778 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 779 | I != E; ++I) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 780 | // Associate the address of the global object as pointing to the memory for |
| 781 | // the global: &G = <G memory> |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 782 | unsigned ObjectIndex = getObject(I); |
| 783 | Node *Object = &GraphNodes[ObjectIndex]; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 784 | Object->setValue(I); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 785 | Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(*I), |
| 786 | ObjectIndex)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 787 | |
| 788 | if (I->hasInitializer()) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 789 | AddGlobalInitializerConstraints(ObjectIndex, I->getInitializer()); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 790 | } else { |
| 791 | // If it doesn't have an initializer (i.e. it's defined in another |
| 792 | // translation unit), it points to the universal set. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 793 | Constraints.push_back(Constraint(Constraint::Copy, ObjectIndex, |
| 794 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 797 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 798 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 799 | // Make the function address point to the function object. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 800 | unsigned ObjectIndex = getObject(F); |
| 801 | GraphNodes[ObjectIndex].setValue(F); |
| 802 | Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(*F), |
| 803 | ObjectIndex)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 804 | // Set up the return value node. |
| 805 | if (isa<PointerType>(F->getFunctionType()->getReturnType())) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 806 | GraphNodes[getReturnNode(F)].setValue(F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 807 | if (F->getFunctionType()->isVarArg()) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 808 | GraphNodes[getVarargNode(F)].setValue(F); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 809 | |
| 810 | // Set up incoming argument nodes. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 811 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 812 | I != E; ++I) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 813 | if (isa<PointerType>(I->getType())) |
| 814 | getNodeValue(*I); |
| 815 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 816 | // At some point we should just add constraints for the escaping functions |
| 817 | // at solve time, but this slows down solving. For now, we simply mark |
| 818 | // address taken functions as escaping and treat them as external. |
| 819 | if (!F->hasInternalLinkage() || AnalyzeUsesOfFunction(F)) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 820 | AddConstraintsForNonInternalLinkage(F); |
| 821 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 822 | if (!F->isDeclaration()) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 823 | // Scan the function body, creating a memory object for each heap/stack |
| 824 | // allocation in the body of the function and a node to represent all |
| 825 | // pointer values defined by instructions and used as operands. |
| 826 | visit(F); |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 827 | } else { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 828 | // External functions that return pointers return the universal set. |
| 829 | if (isa<PointerType>(F->getFunctionType()->getReturnType())) |
| 830 | Constraints.push_back(Constraint(Constraint::Copy, |
| 831 | getReturnNode(F), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 832 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 833 | |
| 834 | // Any pointers that are passed into the function have the universal set |
| 835 | // stored into them. |
Chris Lattner | 493f636 | 2005-03-27 22:03:46 +0000 | [diff] [blame] | 836 | for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 837 | I != E; ++I) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 838 | if (isa<PointerType>(I->getType())) { |
| 839 | // Pointers passed into external functions could have anything stored |
| 840 | // through them. |
| 841 | Constraints.push_back(Constraint(Constraint::Store, getNode(I), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 842 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 843 | // Memory objects passed into external function calls can have the |
| 844 | // universal set point to them. |
| 845 | Constraints.push_back(Constraint(Constraint::Copy, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 846 | UniversalSet, |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 847 | getNode(I))); |
| 848 | } |
| 849 | |
| 850 | // If this is an external varargs function, it can also store pointers |
| 851 | // into any pointers passed through the varargs section. |
| 852 | if (F->getFunctionType()->isVarArg()) |
| 853 | Constraints.push_back(Constraint(Constraint::Store, getVarargNode(F), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 854 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | NumConstraints += Constraints.size(); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | void Andersens::visitInstruction(Instruction &I) { |
| 862 | #ifdef NDEBUG |
| 863 | return; // This function is just a big assert. |
| 864 | #endif |
| 865 | if (isa<BinaryOperator>(I)) |
| 866 | return; |
| 867 | // Most instructions don't have any effect on pointer values. |
| 868 | switch (I.getOpcode()) { |
| 869 | case Instruction::Br: |
| 870 | case Instruction::Switch: |
| 871 | case Instruction::Unwind: |
Chris Lattner | c17edbd | 2004-10-16 18:16:19 +0000 | [diff] [blame] | 872 | case Instruction::Unreachable: |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 873 | case Instruction::Free: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 874 | case Instruction::ICmp: |
| 875 | case Instruction::FCmp: |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 876 | return; |
| 877 | default: |
| 878 | // Is this something we aren't handling yet? |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 879 | cerr << "Unknown instruction: " << I; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 880 | abort(); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | void Andersens::visitAllocationInst(AllocationInst &AI) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 885 | unsigned ObjectIndex = getObject(&AI); |
| 886 | GraphNodes[ObjectIndex].setValue(&AI); |
| 887 | Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(AI), |
| 888 | ObjectIndex)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | void Andersens::visitReturnInst(ReturnInst &RI) { |
| 892 | if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType())) |
| 893 | // return V --> <Copy/retval{F}/v> |
| 894 | Constraints.push_back(Constraint(Constraint::Copy, |
| 895 | getReturnNode(RI.getParent()->getParent()), |
| 896 | getNode(RI.getOperand(0)))); |
| 897 | } |
| 898 | |
| 899 | void Andersens::visitLoadInst(LoadInst &LI) { |
| 900 | if (isa<PointerType>(LI.getType())) |
| 901 | // P1 = load P2 --> <Load/P1/P2> |
| 902 | Constraints.push_back(Constraint(Constraint::Load, getNodeValue(LI), |
| 903 | getNode(LI.getOperand(0)))); |
| 904 | } |
| 905 | |
| 906 | void Andersens::visitStoreInst(StoreInst &SI) { |
| 907 | if (isa<PointerType>(SI.getOperand(0)->getType())) |
| 908 | // store P1, P2 --> <Store/P2/P1> |
| 909 | Constraints.push_back(Constraint(Constraint::Store, |
| 910 | getNode(SI.getOperand(1)), |
| 911 | getNode(SI.getOperand(0)))); |
| 912 | } |
| 913 | |
| 914 | void Andersens::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
| 915 | // P1 = getelementptr P2, ... --> <Copy/P1/P2> |
| 916 | Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(GEP), |
| 917 | getNode(GEP.getOperand(0)))); |
| 918 | } |
| 919 | |
| 920 | void Andersens::visitPHINode(PHINode &PN) { |
| 921 | if (isa<PointerType>(PN.getType())) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 922 | unsigned PNN = getNodeValue(PN); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 923 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 924 | // P1 = phi P2, P3 --> <Copy/P1/P2>, <Copy/P1/P3>, ... |
| 925 | Constraints.push_back(Constraint(Constraint::Copy, PNN, |
| 926 | getNode(PN.getIncomingValue(i)))); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | void Andersens::visitCastInst(CastInst &CI) { |
| 931 | Value *Op = CI.getOperand(0); |
| 932 | if (isa<PointerType>(CI.getType())) { |
| 933 | if (isa<PointerType>(Op->getType())) { |
| 934 | // P1 = cast P2 --> <Copy/P1/P2> |
| 935 | Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI), |
| 936 | getNode(CI.getOperand(0)))); |
| 937 | } else { |
| 938 | // P1 = cast int --> <Copy/P1/Univ> |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 939 | #if 0 |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 940 | Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI), |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 941 | UniversalSet)); |
Chris Lattner | bd135c7 | 2005-04-05 01:12:03 +0000 | [diff] [blame] | 942 | #else |
| 943 | getNodeValue(CI); |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 944 | #endif |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 945 | } |
| 946 | } else if (isa<PointerType>(Op->getType())) { |
| 947 | // int = cast P1 --> <Copy/Univ/P1> |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 948 | #if 0 |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 949 | Constraints.push_back(Constraint(Constraint::Copy, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 950 | UniversalSet, |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 951 | getNode(CI.getOperand(0)))); |
Chris Lattner | bd135c7 | 2005-04-05 01:12:03 +0000 | [diff] [blame] | 952 | #else |
| 953 | getNode(CI.getOperand(0)); |
Chris Lattner | 175b963 | 2005-03-29 20:36:05 +0000 | [diff] [blame] | 954 | #endif |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
| 958 | void Andersens::visitSelectInst(SelectInst &SI) { |
| 959 | if (isa<PointerType>(SI.getType())) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 960 | unsigned SIN = getNodeValue(SI); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 961 | // P1 = select C, P2, P3 ---> <Copy/P1/P2>, <Copy/P1/P3> |
| 962 | Constraints.push_back(Constraint(Constraint::Copy, SIN, |
| 963 | getNode(SI.getOperand(1)))); |
| 964 | Constraints.push_back(Constraint(Constraint::Copy, SIN, |
| 965 | getNode(SI.getOperand(2)))); |
| 966 | } |
| 967 | } |
| 968 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 969 | void Andersens::visitVAArg(VAArgInst &I) { |
| 970 | assert(0 && "vaarg not handled yet!"); |
| 971 | } |
| 972 | |
| 973 | /// AddConstraintsForCall - Add constraints for a call with actual arguments |
| 974 | /// specified by CS to the function specified by F. Note that the types of |
| 975 | /// arguments might not match up in the case where this is an indirect call and |
| 976 | /// the function pointer has been casted. If this is the case, do something |
| 977 | /// reasonable. |
| 978 | void Andersens::AddConstraintsForCall(CallSite CS, Function *F) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 979 | Value *CallValue = CS.getCalledValue(); |
| 980 | bool IsDeref = F == NULL; |
| 981 | |
| 982 | // If this is a call to an external function, try to handle it directly to get |
| 983 | // some taste of context sensitivity. |
| 984 | if (F && F->isDeclaration() && AddConstraintsForExternalCall(CS, F)) |
Chris Lattner | 8a44643 | 2005-03-29 06:09:07 +0000 | [diff] [blame] | 985 | return; |
| 986 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 987 | if (isa<PointerType>(CS.getType())) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 988 | unsigned CSN = getNode(CS.getInstruction()); |
| 989 | if (!F || isa<PointerType>(F->getFunctionType()->getReturnType())) { |
| 990 | if (IsDeref) |
| 991 | Constraints.push_back(Constraint(Constraint::Load, CSN, |
| 992 | getNode(CallValue), CallReturnPos)); |
| 993 | else |
| 994 | Constraints.push_back(Constraint(Constraint::Copy, CSN, |
| 995 | getNode(CallValue) + CallReturnPos)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 996 | } else { |
| 997 | // If the function returns a non-pointer value, handle this just like we |
| 998 | // treat a nonpointer cast to pointer. |
| 999 | Constraints.push_back(Constraint(Constraint::Copy, CSN, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1000 | UniversalSet)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1001 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1002 | } else if (F && isa<PointerType>(F->getFunctionType()->getReturnType())) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1003 | Constraints.push_back(Constraint(Constraint::Copy, |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1004 | UniversalSet, |
| 1005 | getNode(CallValue) + CallReturnPos)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1006 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1008 | CallSite::arg_iterator ArgI = CS.arg_begin(), ArgE = CS.arg_end(); |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1009 | if (F) { |
| 1010 | // Direct Call |
| 1011 | Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 1012 | for (; AI != AE && ArgI != ArgE; ++AI, ++ArgI) |
| 1013 | if (isa<PointerType>(AI->getType())) { |
| 1014 | if (isa<PointerType>((*ArgI)->getType())) { |
| 1015 | // Copy the actual argument into the formal argument. |
| 1016 | Constraints.push_back(Constraint(Constraint::Copy, getNode(AI), |
| 1017 | getNode(*ArgI))); |
| 1018 | } else { |
| 1019 | Constraints.push_back(Constraint(Constraint::Copy, getNode(AI), |
| 1020 | UniversalSet)); |
| 1021 | } |
| 1022 | } else if (isa<PointerType>((*ArgI)->getType())) { |
| 1023 | Constraints.push_back(Constraint(Constraint::Copy, |
| 1024 | UniversalSet, |
| 1025 | getNode(*ArgI))); |
| 1026 | } |
| 1027 | } else { |
| 1028 | //Indirect Call |
| 1029 | unsigned ArgPos = CallFirstArgPos; |
| 1030 | for (; ArgI != ArgE; ++ArgI) { |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1031 | if (isa<PointerType>((*ArgI)->getType())) { |
| 1032 | // Copy the actual argument into the formal argument. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1033 | Constraints.push_back(Constraint(Constraint::Store, |
| 1034 | getNode(CallValue), |
| 1035 | getNode(*ArgI), ArgPos++)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1036 | } else { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1037 | Constraints.push_back(Constraint(Constraint::Store, |
| 1038 | getNode (CallValue), |
| 1039 | UniversalSet, ArgPos++)); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1040 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1041 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1042 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1043 | // Copy all pointers passed through the varargs section to the varargs node. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1044 | if (F && F->getFunctionType()->isVarArg()) |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1045 | for (; ArgI != ArgE; ++ArgI) |
| 1046 | if (isa<PointerType>((*ArgI)->getType())) |
| 1047 | Constraints.push_back(Constraint(Constraint::Copy, getVarargNode(F), |
| 1048 | getNode(*ArgI))); |
| 1049 | // If more arguments are passed in than we track, just drop them on the floor. |
| 1050 | } |
| 1051 | |
| 1052 | void Andersens::visitCallSite(CallSite CS) { |
| 1053 | if (isa<PointerType>(CS.getType())) |
| 1054 | getNodeValue(*CS.getInstruction()); |
| 1055 | |
| 1056 | if (Function *F = CS.getCalledFunction()) { |
| 1057 | AddConstraintsForCall(CS, F); |
| 1058 | } else { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1059 | AddConstraintsForCall(CS, NULL); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | //===----------------------------------------------------------------------===// |
| 1064 | // Constraint Solving Phase |
| 1065 | //===----------------------------------------------------------------------===// |
| 1066 | |
| 1067 | /// intersects - Return true if the points-to set of this node intersects |
| 1068 | /// with the points-to set of the specified node. |
| 1069 | bool Andersens::Node::intersects(Node *N) const { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1070 | return PointsTo->intersects(N->PointsTo); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /// intersectsIgnoring - Return true if the points-to set of this node |
| 1074 | /// intersects with the points-to set of the specified node on any nodes |
| 1075 | /// except for the specified node to ignore. |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1076 | bool Andersens::Node::intersectsIgnoring(Node *N, unsigned Ignoring) const { |
| 1077 | // TODO: If we are only going to call this with the same value for Ignoring, |
| 1078 | // we should move the special values out of the points-to bitmap. |
| 1079 | bool WeHadIt = PointsTo->test(Ignoring); |
| 1080 | bool NHadIt = N->PointsTo->test(Ignoring); |
| 1081 | bool Result = false; |
| 1082 | if (WeHadIt) |
| 1083 | PointsTo->reset(Ignoring); |
| 1084 | if (NHadIt) |
| 1085 | N->PointsTo->reset(Ignoring); |
| 1086 | Result = PointsTo->intersects(N->PointsTo); |
| 1087 | if (WeHadIt) |
| 1088 | PointsTo->set(Ignoring); |
| 1089 | if (NHadIt) |
| 1090 | N->PointsTo->set(Ignoring); |
| 1091 | return Result; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1094 | // Create the constraint graph used for solving points-to analysis. |
| 1095 | // |
| 1096 | void Andersens::CreateConstraintGraph() { |
| 1097 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 1098 | Constraint &C = Constraints[i]; |
| 1099 | assert (C.Src < GraphNodes.size() && C.Dest < GraphNodes.size()); |
| 1100 | if (C.Type == Constraint::AddressOf) |
| 1101 | GraphNodes[C.Dest].PointsTo->set(C.Src); |
| 1102 | else if (C.Type == Constraint::Load) |
| 1103 | GraphNodes[C.Src].Constraints.push_back(C); |
| 1104 | else if (C.Type == Constraint::Store) |
| 1105 | GraphNodes[C.Dest].Constraints.push_back(C); |
| 1106 | else if (C.Offset != 0) |
| 1107 | GraphNodes[C.Src].Constraints.push_back(C); |
| 1108 | else |
| 1109 | GraphNodes[C.Src].Edges->set(C.Dest); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // Perform cycle detection, DFS, and RPO finding. |
| 1114 | void Andersens::QueryNode(unsigned Node) { |
| 1115 | assert(GraphNodes[Node].NodeRep == SelfRep && "Querying a non-rep node"); |
| 1116 | unsigned OurDFS = ++DFSNumber; |
| 1117 | SparseBitVector<> ToErase; |
| 1118 | SparseBitVector<> NewEdges; |
| 1119 | Node2DFS[Node] = OurDFS; |
| 1120 | |
| 1121 | for (SparseBitVector<>::iterator bi = GraphNodes[Node].Edges->begin(); |
| 1122 | bi != GraphNodes[Node].Edges->end(); |
| 1123 | ++bi) { |
| 1124 | unsigned RepNode = FindNode(*bi); |
| 1125 | // If we are going to add an edge to repnode, we have no need for the edge |
| 1126 | // to e anymore. |
| 1127 | if (RepNode != *bi && NewEdges.test(RepNode)){ |
| 1128 | ToErase.set(*bi); |
| 1129 | continue; |
| 1130 | } |
| 1131 | |
| 1132 | // Continue about our DFS. |
| 1133 | if (!Node2Deleted[RepNode]){ |
| 1134 | if (Node2DFS[RepNode] == 0) { |
| 1135 | QueryNode(RepNode); |
| 1136 | // May have been changed by query |
| 1137 | RepNode = FindNode(RepNode); |
| 1138 | } |
| 1139 | if (Node2DFS[RepNode] < Node2DFS[Node]) |
| 1140 | Node2DFS[Node] = Node2DFS[RepNode]; |
| 1141 | } |
| 1142 | // We may have just discovered that e belongs to a cycle, in which case we |
| 1143 | // can also erase it. |
| 1144 | if (RepNode != *bi) { |
| 1145 | ToErase.set(*bi); |
| 1146 | NewEdges.set(RepNode); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1150 | GraphNodes[Node].Edges->intersectWithComplement(ToErase); |
| 1151 | GraphNodes[Node].Edges |= NewEdges; |
| 1152 | |
| 1153 | // If this node is a root of a non-trivial SCC, place it on our worklist to be |
| 1154 | // processed |
| 1155 | if (OurDFS == Node2DFS[Node]) { |
| 1156 | bool Changed = false; |
| 1157 | while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= OurDFS) { |
| 1158 | Node = UniteNodes(Node, FindNode(SCCStack.top())); |
| 1159 | |
| 1160 | SCCStack.pop(); |
| 1161 | Changed = true; |
| 1162 | } |
| 1163 | Node2Deleted[Node] = true; |
| 1164 | RPONumber++; |
| 1165 | |
| 1166 | Topo2Node.at(GraphNodes.size() - RPONumber) = Node; |
| 1167 | Node2Topo[Node] = GraphNodes.size() - RPONumber; |
| 1168 | if (Changed) |
| 1169 | GraphNodes[Node].Changed = true; |
| 1170 | } else { |
| 1171 | SCCStack.push(Node); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1172 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | /// SolveConstraints - This stage iteratively processes the constraints list |
| 1177 | /// propagating constraints (adding edges to the Nodes in the points-to graph) |
| 1178 | /// until a fixed point is reached. |
| 1179 | /// |
| 1180 | void Andersens::SolveConstraints() { |
| 1181 | bool Changed = true; |
| 1182 | unsigned Iteration = 0; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1183 | |
| 1184 | // We create the bitmaps here to avoid getting jerked around by the compiler |
| 1185 | // creating objects behind our back and wasting lots of memory. |
| 1186 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 1187 | Node *N = &GraphNodes[i]; |
| 1188 | N->PointsTo = new SparseBitVector<>; |
| 1189 | N->OldPointsTo = new SparseBitVector<>; |
| 1190 | N->Edges = new SparseBitVector<>; |
| 1191 | } |
| 1192 | CreateConstraintGraph(); |
| 1193 | |
| 1194 | Topo2Node.insert(Topo2Node.begin(), GraphNodes.size(), Unvisited); |
| 1195 | Node2Topo.insert(Node2Topo.begin(), GraphNodes.size(), Unvisited); |
| 1196 | Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0); |
| 1197 | Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false); |
| 1198 | DFSNumber = 0; |
| 1199 | RPONumber = 0; |
| 1200 | // Order graph and mark starting nodes as changed. |
| 1201 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 1202 | unsigned N = FindNode(i); |
| 1203 | Node *INode = &GraphNodes[i]; |
| 1204 | if (Node2DFS[N] == 0) { |
| 1205 | QueryNode(N); |
| 1206 | // Mark as changed if it's a representation and can contribute to the |
| 1207 | // calculation right now. |
| 1208 | if (INode->NodeRep == SelfRep && !INode->PointsTo->empty() |
| 1209 | && (!INode->Edges->empty() || !INode->Constraints.empty())) |
| 1210 | INode->Changed = true; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | do { |
Daniel Berlin | c6d9398 | 2007-09-16 23:59:53 +0000 | [diff] [blame^] | 1215 | Changed = false; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1216 | ++NumIters; |
Daniel Berlin | c6d9398 | 2007-09-16 23:59:53 +0000 | [diff] [blame^] | 1217 | DOUT << "Starting iteration #" << Iteration++; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1218 | // TODO: In the microoptimization category, we could just make Topo2Node |
| 1219 | // a fast map and thus only contain the visited nodes. |
| 1220 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 1221 | unsigned CurrNodeIndex = Topo2Node[i]; |
| 1222 | Node *CurrNode; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1223 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1224 | // We may not revisit all nodes on every iteration |
| 1225 | if (CurrNodeIndex == Unvisited) |
| 1226 | continue; |
| 1227 | CurrNode = &GraphNodes[CurrNodeIndex]; |
| 1228 | // See if this is a node we need to process on this iteration |
| 1229 | if (!CurrNode->Changed || CurrNode->NodeRep != SelfRep) |
| 1230 | continue; |
| 1231 | CurrNode->Changed = false; |
| 1232 | |
| 1233 | // Figure out the changed points to bits |
| 1234 | SparseBitVector<> CurrPointsTo; |
| 1235 | CurrPointsTo.intersectWithComplement(CurrNode->PointsTo, |
| 1236 | CurrNode->OldPointsTo); |
| 1237 | if (CurrPointsTo.empty()){ |
| 1238 | continue; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1239 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1240 | *(CurrNode->OldPointsTo) |= CurrPointsTo; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1241 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1242 | /* Now process the constraints for this node. */ |
| 1243 | for (std::list<Constraint>::iterator li = CurrNode->Constraints.begin(); |
| 1244 | li != CurrNode->Constraints.end(); ) { |
| 1245 | li->Src = FindNode(li->Src); |
| 1246 | li->Dest = FindNode(li->Dest); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1247 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1248 | // TODO: We could delete redundant constraints here. |
| 1249 | // Src and Dest will be the vars we are going to process. |
| 1250 | // This may look a bit ugly, but what it does is allow us to process |
| 1251 | // both store and load constraints with the same function. |
| 1252 | // Load constraints say that every member of our RHS solution has K |
| 1253 | // added to it, and that variable gets an edge to LHS. We also union |
| 1254 | // RHS+K's solution into the LHS solution. |
| 1255 | // Store constraints say that every member of our LHS solution has K |
| 1256 | // added to it, and that variable gets an edge from RHS. We also union |
| 1257 | // RHS's solution into the LHS+K solution. |
| 1258 | unsigned *Src; |
| 1259 | unsigned *Dest; |
| 1260 | unsigned K = li->Offset; |
| 1261 | unsigned CurrMember; |
| 1262 | if (li->Type == Constraint::Load) { |
| 1263 | Src = &CurrMember; |
| 1264 | Dest = &li->Dest; |
| 1265 | } else if (li->Type == Constraint::Store) { |
| 1266 | Src = &li->Src; |
| 1267 | Dest = &CurrMember; |
| 1268 | } else { |
| 1269 | // TODO Handle offseted copy constraint |
| 1270 | li++; |
| 1271 | continue; |
| 1272 | } |
| 1273 | // TODO: hybrid cycle detection would go here, we should check |
| 1274 | // if it was a statically detected offline equivalence that |
| 1275 | // involves pointers , and if so, remove the redundant constraints. |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1276 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1277 | const SparseBitVector<> &Solution = CurrPointsTo; |
| 1278 | |
| 1279 | for (SparseBitVector<>::iterator bi = Solution.begin(); |
| 1280 | bi != Solution.end(); |
| 1281 | ++bi) { |
| 1282 | CurrMember = *bi; |
| 1283 | |
| 1284 | // Need to increment the member by K since that is where we are |
| 1285 | // supposed to copy to/from |
| 1286 | // Node that in positive weight cycles, which occur in address taking |
| 1287 | // of fields, K can go past |
| 1288 | // MaxK[CurrMember] elements, even though that is all it could |
| 1289 | // point to. |
| 1290 | if (K > 0 && K > MaxK[CurrMember]) |
| 1291 | continue; |
| 1292 | else |
| 1293 | CurrMember = FindNode(CurrMember + K); |
| 1294 | |
| 1295 | // Add an edge to the graph, so we can just do regular bitmap ior next |
| 1296 | // time. It may also let us notice a cycle. |
Daniel Berlin | c6d9398 | 2007-09-16 23:59:53 +0000 | [diff] [blame^] | 1297 | if (GraphNodes[*Src].Edges->test_and_set(*Dest)) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1298 | if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo)) { |
| 1299 | GraphNodes[*Dest].Changed = true; |
| 1300 | // If we changed a node we've already processed, we need another |
| 1301 | // iteration. |
| 1302 | if (Node2Topo[*Dest] <= i) |
| 1303 | Changed = true; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1306 | } |
| 1307 | li++; |
| 1308 | } |
| 1309 | SparseBitVector<> NewEdges; |
| 1310 | SparseBitVector<> ToErase; |
| 1311 | |
| 1312 | // Now all we have left to do is propagate points-to info along the |
| 1313 | // edges, erasing the redundant edges. |
| 1314 | |
| 1315 | |
| 1316 | for (SparseBitVector<>::iterator bi = CurrNode->Edges->begin(); |
| 1317 | bi != CurrNode->Edges->end(); |
| 1318 | ++bi) { |
| 1319 | |
| 1320 | unsigned DestVar = *bi; |
| 1321 | unsigned Rep = FindNode(DestVar); |
| 1322 | |
| 1323 | // If we ended up with this node as our destination, or we've already |
| 1324 | // got an edge for the representative, delete the current edge. |
| 1325 | if (Rep == CurrNodeIndex || |
| 1326 | (Rep != DestVar && NewEdges.test(Rep))) { |
| 1327 | ToErase.set(DestVar); |
| 1328 | continue; |
| 1329 | } |
| 1330 | // Union the points-to sets into the dest |
| 1331 | if (GraphNodes[Rep].PointsTo |= CurrPointsTo) { |
| 1332 | GraphNodes[Rep].Changed = true; |
| 1333 | if (Node2Topo[Rep] <= i) |
| 1334 | Changed = true; |
| 1335 | } |
| 1336 | // If this edge's destination was collapsed, rewrite the edge. |
| 1337 | if (Rep != DestVar) { |
| 1338 | ToErase.set(DestVar); |
| 1339 | NewEdges.set(Rep); |
| 1340 | } |
| 1341 | } |
| 1342 | CurrNode->Edges->intersectWithComplement(ToErase); |
| 1343 | CurrNode->Edges |= NewEdges; |
| 1344 | } |
| 1345 | if (Changed) { |
| 1346 | DFSNumber = RPONumber = 0; |
| 1347 | Node2Deleted.clear(); |
| 1348 | Topo2Node.clear(); |
| 1349 | Node2Topo.clear(); |
| 1350 | Node2DFS.clear(); |
| 1351 | Topo2Node.insert(Topo2Node.begin(), GraphNodes.size(), Unvisited); |
| 1352 | Node2Topo.insert(Node2Topo.begin(), GraphNodes.size(), Unvisited); |
| 1353 | Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0); |
| 1354 | Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false); |
| 1355 | // Rediscover the DFS/Topo ordering, and cycle detect. |
| 1356 | for (unsigned j = 0; j < GraphNodes.size(); j++) { |
| 1357 | unsigned JRep = FindNode(j); |
| 1358 | if (Node2DFS[JRep] == 0) |
| 1359 | QueryNode(JRep); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1360 | } |
| 1361 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1362 | |
| 1363 | } while (Changed); |
| 1364 | |
| 1365 | Node2Topo.clear(); |
| 1366 | Topo2Node.clear(); |
| 1367 | Node2DFS.clear(); |
| 1368 | Node2Deleted.clear(); |
| 1369 | for (unsigned i = 0; i < GraphNodes.size(); ++i) { |
| 1370 | Node *N = &GraphNodes[i]; |
| 1371 | delete N->OldPointsTo; |
| 1372 | delete N->Edges; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1376 | //===----------------------------------------------------------------------===// |
| 1377 | // Union-Find |
| 1378 | //===----------------------------------------------------------------------===// |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1379 | |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1380 | // Unite nodes First and Second, returning the one which is now the |
| 1381 | // representative node. First and Second are indexes into GraphNodes |
| 1382 | unsigned Andersens::UniteNodes(unsigned First, unsigned Second) { |
| 1383 | assert (First < GraphNodes.size() && Second < GraphNodes.size() && |
| 1384 | "Attempting to merge nodes that don't exist"); |
| 1385 | // TODO: implement union by rank |
| 1386 | Node *FirstNode = &GraphNodes[First]; |
| 1387 | Node *SecondNode = &GraphNodes[Second]; |
| 1388 | |
| 1389 | assert (SecondNode->NodeRep == SelfRep && FirstNode->NodeRep == SelfRep && |
| 1390 | "Trying to unite two non-representative nodes!"); |
| 1391 | if (First == Second) |
| 1392 | return First; |
| 1393 | |
| 1394 | SecondNode->NodeRep = First; |
| 1395 | FirstNode->Changed |= SecondNode->Changed; |
| 1396 | FirstNode->PointsTo |= *(SecondNode->PointsTo); |
| 1397 | FirstNode->Edges |= *(SecondNode->Edges); |
| 1398 | FirstNode->Constraints.splice(FirstNode->Constraints.begin(), |
| 1399 | SecondNode->Constraints); |
| 1400 | delete FirstNode->OldPointsTo; |
| 1401 | FirstNode->OldPointsTo = new SparseBitVector<>; |
| 1402 | |
| 1403 | // Destroy interesting parts of the merged-from node. |
| 1404 | delete SecondNode->OldPointsTo; |
| 1405 | delete SecondNode->Edges; |
| 1406 | delete SecondNode->PointsTo; |
| 1407 | SecondNode->Edges = NULL; |
| 1408 | SecondNode->PointsTo = NULL; |
| 1409 | SecondNode->OldPointsTo = NULL; |
| 1410 | |
| 1411 | NumUnified++; |
| 1412 | DOUT << "Unified Node "; |
| 1413 | DEBUG(PrintNode(FirstNode)); |
| 1414 | DOUT << " and Node "; |
| 1415 | DEBUG(PrintNode(SecondNode)); |
| 1416 | DOUT << "\n"; |
| 1417 | |
| 1418 | // TODO: Handle SDT |
| 1419 | return First; |
| 1420 | } |
| 1421 | |
| 1422 | // Find the index into GraphNodes of the node representing Node, performing |
| 1423 | // path compression along the way |
| 1424 | unsigned Andersens::FindNode(unsigned NodeIndex) { |
| 1425 | assert (NodeIndex < GraphNodes.size() |
| 1426 | && "Attempting to find a node that can't exist"); |
| 1427 | Node *N = &GraphNodes[NodeIndex]; |
| 1428 | if (N->NodeRep == SelfRep) |
| 1429 | return NodeIndex; |
| 1430 | else |
| 1431 | return (N->NodeRep = FindNode(N->NodeRep)); |
| 1432 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1433 | |
| 1434 | //===----------------------------------------------------------------------===// |
| 1435 | // Debugging Output |
| 1436 | //===----------------------------------------------------------------------===// |
| 1437 | |
| 1438 | void Andersens::PrintNode(Node *N) { |
| 1439 | if (N == &GraphNodes[UniversalSet]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1440 | cerr << "<universal>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1441 | return; |
| 1442 | } else if (N == &GraphNodes[NullPtr]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1443 | cerr << "<nullptr>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | } else if (N == &GraphNodes[NullObject]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1446 | cerr << "<null>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1447 | return; |
| 1448 | } |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1449 | if (!N->getValue()) { |
| 1450 | cerr << "artificial" << (intptr_t) N; |
| 1451 | return; |
| 1452 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1453 | |
| 1454 | assert(N->getValue() != 0 && "Never set node label!"); |
| 1455 | Value *V = N->getValue(); |
| 1456 | if (Function *F = dyn_cast<Function>(V)) { |
| 1457 | if (isa<PointerType>(F->getFunctionType()->getReturnType()) && |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1458 | N == &GraphNodes[getReturnNode(F)]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1459 | cerr << F->getName() << ":retval"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1460 | return; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1461 | } else if (F->getFunctionType()->isVarArg() && |
| 1462 | N == &GraphNodes[getVarargNode(F)]) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1463 | cerr << F->getName() << ":vararg"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1464 | return; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1469 | cerr << I->getParent()->getParent()->getName() << ":"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1470 | else if (Argument *Arg = dyn_cast<Argument>(V)) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1471 | cerr << Arg->getParent()->getName() << ":"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1472 | |
| 1473 | if (V->hasName()) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1474 | cerr << V->getName(); |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1475 | else |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1476 | cerr << "(unnamed)"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1477 | |
| 1478 | if (isa<GlobalValue>(V) || isa<AllocationInst>(V)) |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1479 | if (N == &GraphNodes[getObject(V)]) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1480 | cerr << "<mem>"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | void Andersens::PrintConstraints() { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1484 | cerr << "Constraints:\n"; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1486 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1487 | const Constraint &C = Constraints[i]; |
| 1488 | if (C.Type == Constraint::Store) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1489 | cerr << "*"; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1490 | if (C.Offset != 0) |
| 1491 | cerr << "("; |
| 1492 | } |
| 1493 | PrintNode(&GraphNodes[C.Dest]); |
| 1494 | if (C.Type == Constraint::Store && C.Offset != 0) |
| 1495 | cerr << " + " << C.Offset << ")"; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1496 | cerr << " = "; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1497 | if (C.Type == Constraint::Load) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1498 | cerr << "*"; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1499 | if (C.Offset != 0) |
| 1500 | cerr << "("; |
| 1501 | } |
| 1502 | else if (C.Type == Constraint::AddressOf) |
| 1503 | cerr << "&"; |
| 1504 | PrintNode(&GraphNodes[C.Src]); |
| 1505 | if (C.Offset != 0 && C.Type != Constraint::Store) |
| 1506 | cerr << " + " << C.Offset; |
| 1507 | if (C.Type == Constraint::Load && C.Offset != 0) |
| 1508 | cerr << ")"; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1509 | cerr << "\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | void Andersens::PrintPointsToGraph() { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1514 | cerr << "Points-to graph:\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1515 | for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) { |
| 1516 | Node *N = &GraphNodes[i]; |
Daniel Berlin | aad1588 | 2007-09-16 21:45:02 +0000 | [diff] [blame] | 1517 | if (FindNode (i) != i) { |
| 1518 | PrintNode(N); |
| 1519 | cerr << "\t--> same as "; |
| 1520 | PrintNode(&GraphNodes[FindNode(i)]); |
| 1521 | cerr << "\n"; |
| 1522 | } else { |
| 1523 | cerr << "[" << (N->PointsTo->count()) << "] "; |
| 1524 | PrintNode(N); |
| 1525 | cerr << "\t--> "; |
| 1526 | |
| 1527 | bool first = true; |
| 1528 | for (SparseBitVector<>::iterator bi = N->PointsTo->begin(); |
| 1529 | bi != N->PointsTo->end(); |
| 1530 | ++bi) { |
| 1531 | if (!first) |
| 1532 | cerr << ", "; |
| 1533 | PrintNode(&GraphNodes[*bi]); |
| 1534 | first = false; |
| 1535 | } |
| 1536 | cerr << "\n"; |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1537 | } |
Chris Lattner | e995a2a | 2004-05-23 21:00:47 +0000 | [diff] [blame] | 1538 | } |
| 1539 | } |