blob: b5129a7d3ba127704f74be5ee5e5cdd450a10bb1 [file] [log] [blame]
Chris Lattnerd28b0d72004-06-25 04:24:22 +00001//===- Andersens.cpp - Andersen's Interprocedural Alias Analysis ----------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattnere995a2a2004-05-23 21:00:47 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattnere995a2a2004-05-23 21:00:47 +00008//===----------------------------------------------------------------------===//
9//
Daniel Berlinaad15882007-09-16 21:45:02 +000010// This file defines an implementation of Andersen's interprocedural alias
11// analysis
Chris Lattnere995a2a2004-05-23 21:00:47 +000012//
13// In pointer analysis terms, this is a subset-based, flow-insensitive,
Daniel Berlinaad15882007-09-16 21:45:02 +000014// field-sensitive, and context-insensitive algorithm pointer algorithm.
Chris Lattnere995a2a2004-05-23 21:00:47 +000015//
16// This algorithm is implemented as three stages:
17// 1. Object identification.
18// 2. Inclusion constraint identification.
Daniel Berlind81ccc22007-09-24 19:45:49 +000019// 3. Offline constraint graph optimization
20// 4. Inclusion constraint solving.
Chris Lattnere995a2a2004-05-23 21:00:47 +000021//
22// The object identification stage identifies all of the memory objects in the
23// program, which includes globals, heap allocated objects, and stack allocated
24// objects.
25//
26// The inclusion constraint identification stage finds all inclusion constraints
27// in the program by scanning the program, looking for pointer assignments and
28// other statements that effect the points-to graph. For a statement like "A =
29// B", this statement is processed to indicate that A can point to anything that
Daniel Berlinaad15882007-09-16 21:45:02 +000030// B can point to. Constraints can handle copies, loads, and stores, and
31// address taking.
Chris Lattnere995a2a2004-05-23 21:00:47 +000032//
Daniel Berline6f04792007-09-24 22:20:45 +000033// The offline constraint graph optimization portion includes offline variable
Daniel Berlinc864edb2008-03-05 19:31:47 +000034// substitution algorithms intended to compute pointer and location
Daniel Berline6f04792007-09-24 22:20:45 +000035// equivalences. Pointer equivalences are those pointers that will have the
36// same points-to sets, and location equivalences are those variables that
Daniel Berlinc864edb2008-03-05 19:31:47 +000037// always appear together in points-to sets. It also includes an offline
38// cycle detection algorithm that allows cycles to be collapsed sooner
39// during solving.
Daniel Berlind81ccc22007-09-24 19:45:49 +000040//
Chris Lattnere995a2a2004-05-23 21:00:47 +000041// The inclusion constraint solving phase iteratively propagates the inclusion
42// constraints until a fixed point is reached. This is an O(N^3) algorithm.
43//
Daniel Berlinaad15882007-09-16 21:45:02 +000044// Function constraints are handled as if they were structs with X fields.
45// Thus, an access to argument X of function Y is an access to node index
46// getNode(Y) + X. This representation allows handling of indirect calls
Daniel Berlind81ccc22007-09-24 19:45:49 +000047// without any issues. To wit, an indirect call Y(a,b) is equivalent to
Daniel Berlinaad15882007-09-16 21:45:02 +000048// *(Y + 1) = a, *(Y + 2) = b.
49// The return node for a function is always located at getNode(F) +
50// CallReturnPos. The arguments start at getNode(F) + CallArgPos.
Chris Lattnere995a2a2004-05-23 21:00:47 +000051//
Chris Lattnerc7ca32b2004-06-05 20:12:36 +000052// Future Improvements:
Daniel Berlinc864edb2008-03-05 19:31:47 +000053// Use of BDD's.
Chris Lattnere995a2a2004-05-23 21:00:47 +000054//===----------------------------------------------------------------------===//
55
56#define DEBUG_TYPE "anders-aa"
57#include "llvm/Constants.h"
58#include "llvm/DerivedTypes.h"
59#include "llvm/Instructions.h"
60#include "llvm/Module.h"
61#include "llvm/Pass.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000062#include "llvm/Support/Compiler.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000063#include "llvm/Support/ErrorHandling.h"
Chris Lattnere995a2a2004-05-23 21:00:47 +000064#include "llvm/Support/InstIterator.h"
65#include "llvm/Support/InstVisitor.h"
66#include "llvm/Analysis/AliasAnalysis.h"
Victor Hernandez46e83122009-09-18 21:34:51 +000067#include "llvm/Analysis/MallocHelper.h"
Jeff Cohen534927d2005-01-08 22:01:16 +000068#include "llvm/Analysis/Passes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000069#include "llvm/Support/Debug.h"
Owen Anderson2e693102009-06-24 22:16:52 +000070#include "llvm/System/Atomic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000071#include "llvm/ADT/Statistic.h"
Daniel Berlinaad15882007-09-16 21:45:02 +000072#include "llvm/ADT/SparseBitVector.h"
Chris Lattnerbe207732007-09-30 00:47:20 +000073#include "llvm/ADT/DenseSet.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000074#include <algorithm>
Chris Lattnere995a2a2004-05-23 21:00:47 +000075#include <set>
Daniel Berlinaad15882007-09-16 21:45:02 +000076#include <list>
Dan Gohmanc9235d22008-03-21 23:51:57 +000077#include <map>
Daniel Berlinaad15882007-09-16 21:45:02 +000078#include <stack>
79#include <vector>
Daniel Berlin3a3f1632007-12-12 00:37:04 +000080#include <queue>
81
82// Determining the actual set of nodes the universal set can consist of is very
83// expensive because it means propagating around very large sets. We rely on
84// other analysis being able to determine which nodes can never be pointed to in
85// order to disambiguate further than "points-to anything".
86#define FULL_UNIVERSAL 0
Chris Lattnere995a2a2004-05-23 21:00:47 +000087
Daniel Berlinaad15882007-09-16 21:45:02 +000088using namespace llvm;
Daniel Dunbare317bcc2009-08-23 10:29:55 +000089#ifndef NDEBUG
Daniel Berlind81ccc22007-09-24 19:45:49 +000090STATISTIC(NumIters , "Number of iterations to reach convergence");
Daniel Dunbare317bcc2009-08-23 10:29:55 +000091#endif
Daniel Berlind81ccc22007-09-24 19:45:49 +000092STATISTIC(NumConstraints, "Number of constraints");
93STATISTIC(NumNodes , "Number of nodes");
94STATISTIC(NumUnified , "Number of variables unified");
Daniel Berlin3a3f1632007-12-12 00:37:04 +000095STATISTIC(NumErased , "Number of redundant constraints erased");
Chris Lattnere995a2a2004-05-23 21:00:47 +000096
Dan Gohman844731a2008-05-13 00:00:25 +000097static const unsigned SelfRep = (unsigned)-1;
98static const unsigned Unvisited = (unsigned)-1;
99// Position of the function return node relative to the function node.
100static const unsigned CallReturnPos = 1;
101// Position of the function call node relative to the function node.
102static const unsigned CallFirstArgPos = 2;
Daniel Berlind81ccc22007-09-24 19:45:49 +0000103
Dan Gohman844731a2008-05-13 00:00:25 +0000104namespace {
Daniel Berlind81ccc22007-09-24 19:45:49 +0000105 struct BitmapKeyInfo {
106 static inline SparseBitVector<> *getEmptyKey() {
107 return reinterpret_cast<SparseBitVector<> *>(-1);
108 }
109 static inline SparseBitVector<> *getTombstoneKey() {
110 return reinterpret_cast<SparseBitVector<> *>(-2);
111 }
112 static unsigned getHashValue(const SparseBitVector<> *bitmap) {
113 return bitmap->getHashValue();
114 }
115 static bool isEqual(const SparseBitVector<> *LHS,
116 const SparseBitVector<> *RHS) {
117 if (LHS == RHS)
118 return true;
119 else if (LHS == getEmptyKey() || RHS == getEmptyKey()
120 || LHS == getTombstoneKey() || RHS == getTombstoneKey())
121 return false;
122
123 return *LHS == *RHS;
124 }
125
126 static bool isPod() { return true; }
127 };
Daniel Berlinaad15882007-09-16 21:45:02 +0000128
Reid Spencerd7d83db2007-02-05 23:42:17 +0000129 class VISIBILITY_HIDDEN Andersens : public ModulePass, public AliasAnalysis,
130 private InstVisitor<Andersens> {
Hartmut Kaiser081fdf22007-10-25 23:49:14 +0000131 struct Node;
Daniel Berlinaad15882007-09-16 21:45:02 +0000132
133 /// Constraint - Objects of this structure are used to represent the various
134 /// constraints identified by the algorithm. The constraints are 'copy',
135 /// for statements like "A = B", 'load' for statements like "A = *B",
136 /// 'store' for statements like "*A = B", and AddressOf for statements like
137 /// A = alloca; The Offset is applied as *(A + K) = B for stores,
138 /// A = *(B + K) for loads, and A = B + K for copies. It is
Daniel Berlind81ccc22007-09-24 19:45:49 +0000139 /// illegal on addressof constraints (because it is statically
Daniel Berlinaad15882007-09-16 21:45:02 +0000140 /// resolvable to A = &C where C = B + K)
141
142 struct Constraint {
143 enum ConstraintType { Copy, Load, Store, AddressOf } Type;
144 unsigned Dest;
145 unsigned Src;
146 unsigned Offset;
147
148 Constraint(ConstraintType Ty, unsigned D, unsigned S, unsigned O = 0)
149 : Type(Ty), Dest(D), Src(S), Offset(O) {
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +0000150 assert((Offset == 0 || Ty != AddressOf) &&
Daniel Berlinaad15882007-09-16 21:45:02 +0000151 "Offset is illegal on addressof constraints");
152 }
Daniel Berlin336c6c02007-09-29 00:50:40 +0000153
Daniel Berlinc7a12ae2007-09-27 15:42:23 +0000154 bool operator==(const Constraint &RHS) const {
155 return RHS.Type == Type
156 && RHS.Dest == Dest
157 && RHS.Src == Src
158 && RHS.Offset == Offset;
159 }
Daniel Berlin336c6c02007-09-29 00:50:40 +0000160
161 bool operator!=(const Constraint &RHS) const {
162 return !(*this == RHS);
163 }
164
Daniel Berlinc7a12ae2007-09-27 15:42:23 +0000165 bool operator<(const Constraint &RHS) const {
166 if (RHS.Type != Type)
167 return RHS.Type < Type;
168 else if (RHS.Dest != Dest)
169 return RHS.Dest < Dest;
170 else if (RHS.Src != Src)
171 return RHS.Src < Src;
172 return RHS.Offset < Offset;
173 }
Daniel Berlinaad15882007-09-16 21:45:02 +0000174 };
175
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000176 // Information DenseSet requires implemented in order to be able to do
177 // it's thing
178 struct PairKeyInfo {
179 static inline std::pair<unsigned, unsigned> getEmptyKey() {
Scott Michelacddf9d2008-03-18 16:55:06 +0000180 return std::make_pair(~0U, ~0U);
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000181 }
182 static inline std::pair<unsigned, unsigned> getTombstoneKey() {
Scott Michelacddf9d2008-03-18 16:55:06 +0000183 return std::make_pair(~0U - 1, ~0U - 1);
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000184 }
185 static unsigned getHashValue(const std::pair<unsigned, unsigned> &P) {
186 return P.first ^ P.second;
187 }
188 static unsigned isEqual(const std::pair<unsigned, unsigned> &LHS,
189 const std::pair<unsigned, unsigned> &RHS) {
190 return LHS == RHS;
191 }
192 };
193
Daniel Berlin336c6c02007-09-29 00:50:40 +0000194 struct ConstraintKeyInfo {
195 static inline Constraint getEmptyKey() {
Scott Michelacddf9d2008-03-18 16:55:06 +0000196 return Constraint(Constraint::Copy, ~0U, ~0U, ~0U);
Daniel Berlin336c6c02007-09-29 00:50:40 +0000197 }
198 static inline Constraint getTombstoneKey() {
Scott Michelacddf9d2008-03-18 16:55:06 +0000199 return Constraint(Constraint::Copy, ~0U - 1, ~0U - 1, ~0U - 1);
Daniel Berlin336c6c02007-09-29 00:50:40 +0000200 }
201 static unsigned getHashValue(const Constraint &C) {
202 return C.Src ^ C.Dest ^ C.Type ^ C.Offset;
203 }
204 static bool isEqual(const Constraint &LHS,
205 const Constraint &RHS) {
206 return LHS.Type == RHS.Type && LHS.Dest == RHS.Dest
207 && LHS.Src == RHS.Src && LHS.Offset == RHS.Offset;
208 }
209 };
210
Daniel Berlind81ccc22007-09-24 19:45:49 +0000211 // Node class - This class is used to represent a node in the constraint
Daniel Berline6f04792007-09-24 22:20:45 +0000212 // graph. Due to various optimizations, it is not always the case that
213 // there is a mapping from a Node to a Value. In particular, we add
214 // artificial Node's that represent the set of pointed-to variables shared
215 // for each location equivalent Node.
Daniel Berlinaad15882007-09-16 21:45:02 +0000216 struct Node {
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000217 private:
Owen Anderson5ec56cc2009-06-30 05:33:46 +0000218 static volatile sys::cas_flag Counter;
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000219
220 public:
Daniel Berlind81ccc22007-09-24 19:45:49 +0000221 Value *Val;
Daniel Berlinaad15882007-09-16 21:45:02 +0000222 SparseBitVector<> *Edges;
223 SparseBitVector<> *PointsTo;
224 SparseBitVector<> *OldPointsTo;
Daniel Berlinaad15882007-09-16 21:45:02 +0000225 std::list<Constraint> Constraints;
226
Daniel Berlind81ccc22007-09-24 19:45:49 +0000227 // Pointer and location equivalence labels
228 unsigned PointerEquivLabel;
229 unsigned LocationEquivLabel;
230 // Predecessor edges, both real and implicit
231 SparseBitVector<> *PredEdges;
232 SparseBitVector<> *ImplicitPredEdges;
233 // Set of nodes that point to us, only use for location equivalence.
234 SparseBitVector<> *PointedToBy;
235 // Number of incoming edges, used during variable substitution to early
236 // free the points-to sets
237 unsigned NumInEdges;
Daniel Berline6f04792007-09-24 22:20:45 +0000238 // True if our points-to set is in the Set2PEClass map
Daniel Berlind81ccc22007-09-24 19:45:49 +0000239 bool StoredInHash;
Daniel Berline6f04792007-09-24 22:20:45 +0000240 // True if our node has no indirect constraints (complex or otherwise)
Daniel Berlind81ccc22007-09-24 19:45:49 +0000241 bool Direct;
242 // True if the node is address taken, *or* it is part of a group of nodes
243 // that must be kept together. This is set to true for functions and
244 // their arg nodes, which must be kept at the same position relative to
245 // their base function node.
Daniel Berlind81ccc22007-09-24 19:45:49 +0000246 bool AddressTaken;
Daniel Berlinaad15882007-09-16 21:45:02 +0000247
Daniel Berlind81ccc22007-09-24 19:45:49 +0000248 // Nodes in cycles (or in equivalence classes) are united together using a
249 // standard union-find representation with path compression. NodeRep
250 // gives the index into GraphNodes for the representative Node.
251 unsigned NodeRep;
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000252
253 // Modification timestamp. Assigned from Counter.
254 // Used for work list prioritization.
255 unsigned Timestamp;
Daniel Berlind81ccc22007-09-24 19:45:49 +0000256
Dan Gohmanded2b0d2007-12-14 15:41:34 +0000257 explicit Node(bool direct = true) :
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000258 Val(0), Edges(0), PointsTo(0), OldPointsTo(0),
Daniel Berlind81ccc22007-09-24 19:45:49 +0000259 PointerEquivLabel(0), LocationEquivLabel(0), PredEdges(0),
260 ImplicitPredEdges(0), PointedToBy(0), NumInEdges(0),
261 StoredInHash(false), Direct(direct), AddressTaken(false),
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000262 NodeRep(SelfRep), Timestamp(0) { }
Daniel Berlinaad15882007-09-16 21:45:02 +0000263
Chris Lattnere995a2a2004-05-23 21:00:47 +0000264 Node *setValue(Value *V) {
265 assert(Val == 0 && "Value already set for this node!");
266 Val = V;
267 return this;
268 }
269
270 /// getValue - Return the LLVM value corresponding to this node.
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000271 ///
Chris Lattnere995a2a2004-05-23 21:00:47 +0000272 Value *getValue() const { return Val; }
273
Chris Lattnere995a2a2004-05-23 21:00:47 +0000274 /// addPointerTo - Add a pointer to the list of pointees of this node,
275 /// returning true if this caused a new pointer to be added, or false if
276 /// we already knew about the points-to relation.
Daniel Berlinaad15882007-09-16 21:45:02 +0000277 bool addPointerTo(unsigned Node) {
278 return PointsTo->test_and_set(Node);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000279 }
280
281 /// intersects - Return true if the points-to set of this node intersects
282 /// with the points-to set of the specified node.
283 bool intersects(Node *N) const;
284
285 /// intersectsIgnoring - Return true if the points-to set of this node
286 /// intersects with the points-to set of the specified node on any nodes
287 /// except for the specified node to ignore.
Daniel Berlinaad15882007-09-16 21:45:02 +0000288 bool intersectsIgnoring(Node *N, unsigned) const;
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000289
290 // Timestamp a node (used for work list prioritization)
291 void Stamp() {
Owen Anderson2d7f78e2009-06-25 16:32:45 +0000292 Timestamp = sys::AtomicIncrement(&Counter);
293 --Timestamp;
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000294 }
295
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000296 bool isRep() const {
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000297 return( (int) NodeRep < 0 );
298 }
299 };
300
301 struct WorkListElement {
302 Node* node;
303 unsigned Timestamp;
304 WorkListElement(Node* n, unsigned t) : node(n), Timestamp(t) {}
305
306 // Note that we reverse the sense of the comparison because we
307 // actually want to give low timestamps the priority over high,
308 // whereas priority is typically interpreted as a greater value is
309 // given high priority.
310 bool operator<(const WorkListElement& that) const {
311 return( this->Timestamp > that.Timestamp );
312 }
313 };
314
315 // Priority-queue based work list specialized for Nodes.
316 class WorkList {
317 std::priority_queue<WorkListElement> Q;
318
319 public:
320 void insert(Node* n) {
321 Q.push( WorkListElement(n, n->Timestamp) );
322 }
323
324 // We automatically discard non-representative nodes and nodes
325 // that were in the work list twice (we keep a copy of the
326 // timestamp in the work list so we can detect this situation by
327 // comparing against the node's current timestamp).
328 Node* pop() {
329 while( !Q.empty() ) {
330 WorkListElement x = Q.top(); Q.pop();
331 Node* INode = x.node;
332
333 if( INode->isRep() &&
334 INode->Timestamp == x.Timestamp ) {
335 return(x.node);
336 }
337 }
338 return(0);
339 }
340
341 bool empty() {
342 return Q.empty();
343 }
Chris Lattnere995a2a2004-05-23 21:00:47 +0000344 };
345
346 /// GraphNodes - This vector is populated as part of the object
347 /// identification stage of the analysis, which populates this vector with a
348 /// node for each memory object and fills in the ValueNodes map.
349 std::vector<Node> GraphNodes;
350
351 /// ValueNodes - This map indicates the Node that a particular Value* is
352 /// represented by. This contains entries for all pointers.
Daniel Berlind81ccc22007-09-24 19:45:49 +0000353 DenseMap<Value*, unsigned> ValueNodes;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000354
355 /// ObjectNodes - This map contains entries for each memory object in the
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000356 /// program: globals, alloca's and mallocs.
Daniel Berlind81ccc22007-09-24 19:45:49 +0000357 DenseMap<Value*, unsigned> ObjectNodes;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000358
359 /// ReturnNodes - This map contains an entry for each function in the
360 /// program that returns a value.
Daniel Berlind81ccc22007-09-24 19:45:49 +0000361 DenseMap<Function*, unsigned> ReturnNodes;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000362
363 /// VarargNodes - This map contains the entry used to represent all pointers
364 /// passed through the varargs portion of a function call for a particular
365 /// function. An entry is not present in this map for functions that do not
366 /// take variable arguments.
Daniel Berlind81ccc22007-09-24 19:45:49 +0000367 DenseMap<Function*, unsigned> VarargNodes;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000368
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000369
Chris Lattnere995a2a2004-05-23 21:00:47 +0000370 /// Constraints - This vector contains a list of all of the constraints
371 /// identified by the program.
372 std::vector<Constraint> Constraints;
373
Daniel Berlind81ccc22007-09-24 19:45:49 +0000374 // Map from graph node to maximum K value that is allowed (for functions,
Daniel Berlinaad15882007-09-16 21:45:02 +0000375 // this is equivalent to the number of arguments + CallFirstArgPos)
376 std::map<unsigned, unsigned> MaxK;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000377
378 /// This enum defines the GraphNodes indices that correspond to important
379 /// fixed sets.
380 enum {
381 UniversalSet = 0,
382 NullPtr = 1,
Daniel Berlind81ccc22007-09-24 19:45:49 +0000383 NullObject = 2,
384 NumberSpecialNodes
Chris Lattnere995a2a2004-05-23 21:00:47 +0000385 };
Daniel Berlind81ccc22007-09-24 19:45:49 +0000386 // Stack for Tarjan's
Daniel Berlinaad15882007-09-16 21:45:02 +0000387 std::stack<unsigned> SCCStack;
Daniel Berlinaad15882007-09-16 21:45:02 +0000388 // Map from Graph Node to DFS number
389 std::vector<unsigned> Node2DFS;
390 // Map from Graph Node to Deleted from graph.
391 std::vector<bool> Node2Deleted;
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000392 // Same as Node Maps, but implemented as std::map because it is faster to
393 // clear
394 std::map<unsigned, unsigned> Tarjan2DFS;
395 std::map<unsigned, bool> Tarjan2Deleted;
396 // Current DFS number
Daniel Berlinaad15882007-09-16 21:45:02 +0000397 unsigned DFSNumber;
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000398
399 // Work lists.
400 WorkList w1, w2;
401 WorkList *CurrWL, *NextWL; // "current" and "next" work lists
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000402
Daniel Berlind81ccc22007-09-24 19:45:49 +0000403 // Offline variable substitution related things
404
405 // Temporary rep storage, used because we can't collapse SCC's in the
406 // predecessor graph by uniting the variables permanently, we can only do so
407 // for the successor graph.
408 std::vector<unsigned> VSSCCRep;
409 // Mapping from node to whether we have visited it during SCC finding yet.
410 std::vector<bool> Node2Visited;
411 // During variable substitution, we create unknowns to represent the unknown
412 // value that is a dereference of a variable. These nodes are known as
413 // "ref" nodes (since they represent the value of dereferences).
414 unsigned FirstRefNode;
415 // During HVN, we create represent address taken nodes as if they were
416 // unknown (since HVN, unlike HU, does not evaluate unions).
417 unsigned FirstAdrNode;
418 // Current pointer equivalence class number
419 unsigned PEClass;
420 // Mapping from points-to sets to equivalence classes
421 typedef DenseMap<SparseBitVector<> *, unsigned, BitmapKeyInfo> BitVectorMap;
422 BitVectorMap Set2PEClass;
423 // Mapping from pointer equivalences to the representative node. -1 if we
424 // have no representative node for this pointer equivalence class yet.
425 std::vector<int> PEClass2Node;
426 // Mapping from pointer equivalences to representative node. This includes
427 // pointer equivalent but not location equivalent variables. -1 if we have
428 // no representative node for this pointer equivalence class yet.
429 std::vector<int> PENLEClass2Node;
Daniel Berlinc864edb2008-03-05 19:31:47 +0000430 // Union/Find for HCD
431 std::vector<unsigned> HCDSCCRep;
432 // HCD's offline-detected cycles; "Statically DeTected"
433 // -1 if not part of such a cycle, otherwise a representative node.
434 std::vector<int> SDT;
435 // Whether to use SDT (UniteNodes can use it during solving, but not before)
436 bool SDTActive;
Daniel Berlind81ccc22007-09-24 19:45:49 +0000437
Chris Lattnere995a2a2004-05-23 21:00:47 +0000438 public:
Daniel Berlinaad15882007-09-16 21:45:02 +0000439 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +0000440 Andersens() : ModulePass(&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +0000441
Chris Lattnerb12914b2004-09-20 04:48:05 +0000442 bool runOnModule(Module &M) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000443 InitializeAliasAnalysis(this);
444 IdentifyObjects(M);
445 CollectConstraints(M);
Daniel Berlind81ccc22007-09-24 19:45:49 +0000446#undef DEBUG_TYPE
447#define DEBUG_TYPE "anders-aa-constraints"
Chris Lattnere995a2a2004-05-23 21:00:47 +0000448 DEBUG(PrintConstraints());
Daniel Berlind81ccc22007-09-24 19:45:49 +0000449#undef DEBUG_TYPE
450#define DEBUG_TYPE "anders-aa"
Chris Lattnere995a2a2004-05-23 21:00:47 +0000451 SolveConstraints();
452 DEBUG(PrintPointsToGraph());
453
454 // Free the constraints list, as we don't need it to respond to alias
455 // requests.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000456 std::vector<Constraint>().swap(Constraints);
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000457 //These are needed for Print() (-analyze in opt)
458 //ObjectNodes.clear();
459 //ReturnNodes.clear();
460 //VarargNodes.clear();
Chris Lattnere995a2a2004-05-23 21:00:47 +0000461 return false;
462 }
463
464 void releaseMemory() {
465 // FIXME: Until we have transitively required passes working correctly,
466 // this cannot be enabled! Otherwise, using -count-aa with the pass
467 // causes memory to be freed too early. :(
468#if 0
469 // The memory objects and ValueNodes data structures at the only ones that
470 // are still live after construction.
471 std::vector<Node>().swap(GraphNodes);
472 ValueNodes.clear();
473#endif
474 }
475
476 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
477 AliasAnalysis::getAnalysisUsage(AU);
478 AU.setPreservesAll(); // Does not transform code
479 }
480
481 //------------------------------------------------
482 // Implement the AliasAnalysis API
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000483 //
Chris Lattnere995a2a2004-05-23 21:00:47 +0000484 AliasResult alias(const Value *V1, unsigned V1Size,
485 const Value *V2, unsigned V2Size);
Reid Spencer3a9ec242006-08-28 01:02:49 +0000486 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
487 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000488 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
489 bool pointsToConstantMemory(const Value *P);
490
491 virtual void deleteValue(Value *V) {
492 ValueNodes.erase(V);
493 getAnalysis<AliasAnalysis>().deleteValue(V);
494 }
495
496 virtual void copyValue(Value *From, Value *To) {
497 ValueNodes[To] = ValueNodes[From];
498 getAnalysis<AliasAnalysis>().copyValue(From, To);
499 }
500
501 private:
502 /// getNode - Return the node corresponding to the specified pointer scalar.
503 ///
Daniel Berlinaad15882007-09-16 21:45:02 +0000504 unsigned getNode(Value *V) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000505 if (Constant *C = dyn_cast<Constant>(V))
Chris Lattnerdf9b7bc2004-08-16 05:38:02 +0000506 if (!isa<GlobalValue>(C))
507 return getNodeForConstantPointer(C);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000508
Daniel Berlind81ccc22007-09-24 19:45:49 +0000509 DenseMap<Value*, unsigned>::iterator I = ValueNodes.find(V);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000510 if (I == ValueNodes.end()) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000511#ifndef NDEBUG
512 V->dump();
513#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000514 llvm_unreachable("Value does not have a node in the points-to graph!");
Chris Lattnere995a2a2004-05-23 21:00:47 +0000515 }
Daniel Berlinaad15882007-09-16 21:45:02 +0000516 return I->second;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000517 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000518
Chris Lattnere995a2a2004-05-23 21:00:47 +0000519 /// getObject - Return the node corresponding to the memory object for the
520 /// specified global or allocation instruction.
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000521 unsigned getObject(Value *V) const {
Daniel Berlind81ccc22007-09-24 19:45:49 +0000522 DenseMap<Value*, unsigned>::iterator I = ObjectNodes.find(V);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000523 assert(I != ObjectNodes.end() &&
524 "Value does not have an object in the points-to graph!");
Daniel Berlinaad15882007-09-16 21:45:02 +0000525 return I->second;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000526 }
527
528 /// getReturnNode - Return the node representing the return value for the
529 /// specified function.
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000530 unsigned getReturnNode(Function *F) const {
Daniel Berlind81ccc22007-09-24 19:45:49 +0000531 DenseMap<Function*, unsigned>::iterator I = ReturnNodes.find(F);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000532 assert(I != ReturnNodes.end() && "Function does not return a value!");
Daniel Berlinaad15882007-09-16 21:45:02 +0000533 return I->second;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000534 }
535
536 /// getVarargNode - Return the node representing the variable arguments
537 /// formal for the specified function.
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000538 unsigned getVarargNode(Function *F) const {
Daniel Berlind81ccc22007-09-24 19:45:49 +0000539 DenseMap<Function*, unsigned>::iterator I = VarargNodes.find(F);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000540 assert(I != VarargNodes.end() && "Function does not take var args!");
Daniel Berlinaad15882007-09-16 21:45:02 +0000541 return I->second;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000542 }
543
544 /// getNodeValue - Get the node for the specified LLVM value and set the
545 /// value for it to be the specified value.
Daniel Berlinaad15882007-09-16 21:45:02 +0000546 unsigned getNodeValue(Value &V) {
547 unsigned Index = getNode(&V);
548 GraphNodes[Index].setValue(&V);
549 return Index;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000550 }
551
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000552 unsigned UniteNodes(unsigned First, unsigned Second,
553 bool UnionByRank = true);
Daniel Berlinaad15882007-09-16 21:45:02 +0000554 unsigned FindNode(unsigned Node);
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000555 unsigned FindNode(unsigned Node) const;
Daniel Berlinaad15882007-09-16 21:45:02 +0000556
Chris Lattnere995a2a2004-05-23 21:00:47 +0000557 void IdentifyObjects(Module &M);
558 void CollectConstraints(Module &M);
Daniel Berlinaad15882007-09-16 21:45:02 +0000559 bool AnalyzeUsesOfFunction(Value *);
560 void CreateConstraintGraph();
Daniel Berlind81ccc22007-09-24 19:45:49 +0000561 void OptimizeConstraints();
562 unsigned FindEquivalentNode(unsigned, unsigned);
563 void ClumpAddressTaken();
564 void RewriteConstraints();
565 void HU();
566 void HVN();
Daniel Berlinc864edb2008-03-05 19:31:47 +0000567 void HCD();
568 void Search(unsigned Node);
Daniel Berlind81ccc22007-09-24 19:45:49 +0000569 void UnitePointerEquivalences();
Chris Lattnere995a2a2004-05-23 21:00:47 +0000570 void SolveConstraints();
Daniel Berlin3a3f1632007-12-12 00:37:04 +0000571 bool QueryNode(unsigned Node);
Daniel Berlind81ccc22007-09-24 19:45:49 +0000572 void Condense(unsigned Node);
573 void HUValNum(unsigned Node);
574 void HVNValNum(unsigned Node);
Daniel Berlinaad15882007-09-16 21:45:02 +0000575 unsigned getNodeForConstantPointer(Constant *C);
576 unsigned getNodeForConstantPointerTarget(Constant *C);
577 void AddGlobalInitializerConstraints(unsigned, Constant *C);
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000578
Chris Lattnere995a2a2004-05-23 21:00:47 +0000579 void AddConstraintsForNonInternalLinkage(Function *F);
580 void AddConstraintsForCall(CallSite CS, Function *F);
Chris Lattner8a446432005-03-29 06:09:07 +0000581 bool AddConstraintsForExternalCall(CallSite CS, Function *F);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000582
583
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000584 void PrintNode(const Node *N) const;
585 void PrintConstraints() const ;
586 void PrintConstraint(const Constraint &) const;
587 void PrintLabels() const;
588 void PrintPointsToGraph() const;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000589
590 //===------------------------------------------------------------------===//
591 // Instruction visitation methods for adding constraints
592 //
593 friend class InstVisitor<Andersens>;
594 void visitReturnInst(ReturnInst &RI);
595 void visitInvokeInst(InvokeInst &II) { visitCallSite(CallSite(&II)); }
Victor Hernandez46e83122009-09-18 21:34:51 +0000596 void visitCallInst(CallInst &CI) {
Victor Hernandez7b929da2009-10-23 21:09:37 +0000597 if (isMalloc(&CI)) visitAlloc(CI);
Victor Hernandez46e83122009-09-18 21:34:51 +0000598 else visitCallSite(CallSite(&CI));
599 }
Chris Lattnere995a2a2004-05-23 21:00:47 +0000600 void visitCallSite(CallSite CS);
Victor Hernandez7b929da2009-10-23 21:09:37 +0000601 void visitAllocaInst(AllocaInst &I);
602 void visitAlloc(Instruction &I);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000603 void visitLoadInst(LoadInst &LI);
604 void visitStoreInst(StoreInst &SI);
605 void visitGetElementPtrInst(GetElementPtrInst &GEP);
606 void visitPHINode(PHINode &PN);
607 void visitCastInst(CastInst &CI);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000608 void visitICmpInst(ICmpInst &ICI) {} // NOOP!
609 void visitFCmpInst(FCmpInst &ICI) {} // NOOP!
Chris Lattnere995a2a2004-05-23 21:00:47 +0000610 void visitSelectInst(SelectInst &SI);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000611 void visitVAArg(VAArgInst &I);
612 void visitInstruction(Instruction &I);
Daniel Berlinaad15882007-09-16 21:45:02 +0000613
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000614 //===------------------------------------------------------------------===//
615 // Implement Analyize interface
616 //
Chris Lattner45cfe542009-08-23 06:03:38 +0000617 void print(raw_ostream &O, const Module*) const {
Andrew Lenharth52d34d92008-03-20 15:36:44 +0000618 PrintPointsToGraph();
619 }
Chris Lattnere995a2a2004-05-23 21:00:47 +0000620 };
Chris Lattnere995a2a2004-05-23 21:00:47 +0000621}
622
Dan Gohman844731a2008-05-13 00:00:25 +0000623char Andersens::ID = 0;
624static RegisterPass<Andersens>
Chris Lattnerb80e1ab2009-08-28 00:45:47 +0000625X("anders-aa", "Andersen's Interprocedural Alias Analysis (experimental)",
626 false, true);
Dan Gohman844731a2008-05-13 00:00:25 +0000627static RegisterAnalysisGroup<AliasAnalysis> Y(X);
628
629// Initialize Timestamp Counter (static).
Owen Anderson5ec56cc2009-06-30 05:33:46 +0000630volatile llvm::sys::cas_flag Andersens::Node::Counter = 0;
Dan Gohman844731a2008-05-13 00:00:25 +0000631
Jeff Cohen534927d2005-01-08 22:01:16 +0000632ModulePass *llvm::createAndersensPass() { return new Andersens(); }
633
Chris Lattnere995a2a2004-05-23 21:00:47 +0000634//===----------------------------------------------------------------------===//
635// AliasAnalysis Interface Implementation
636//===----------------------------------------------------------------------===//
637
638AliasAnalysis::AliasResult Andersens::alias(const Value *V1, unsigned V1Size,
639 const Value *V2, unsigned V2Size) {
Daniel Berlinaad15882007-09-16 21:45:02 +0000640 Node *N1 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V1)))];
641 Node *N2 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V2)))];
Chris Lattnere995a2a2004-05-23 21:00:47 +0000642
643 // Check to see if the two pointers are known to not alias. They don't alias
644 // if their points-to sets do not intersect.
Daniel Berlinaad15882007-09-16 21:45:02 +0000645 if (!N1->intersectsIgnoring(N2, NullObject))
Chris Lattnere995a2a2004-05-23 21:00:47 +0000646 return NoAlias;
647
648 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
649}
650
Chris Lattnerf392c642005-03-28 06:21:17 +0000651AliasAnalysis::ModRefResult
652Andersens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
653 // The only thing useful that we can contribute for mod/ref information is
654 // when calling external function calls: if we know that memory never escapes
655 // from the program, it cannot be modified by an external call.
656 //
657 // NOTE: This is not really safe, at least not when the entire program is not
658 // available. The deal is that the external function could call back into the
659 // program and modify stuff. We ignore this technical niggle for now. This
660 // is, after all, a "research quality" implementation of Andersen's analysis.
661 if (Function *F = CS.getCalledFunction())
Reid Spencer5cbf9852007-01-30 20:08:39 +0000662 if (F->isDeclaration()) {
Daniel Berlinaad15882007-09-16 21:45:02 +0000663 Node *N1 = &GraphNodes[FindNode(getNode(P))];
Chris Lattnerf392c642005-03-28 06:21:17 +0000664
Daniel Berlinaad15882007-09-16 21:45:02 +0000665 if (N1->PointsTo->empty())
666 return NoModRef;
Daniel Berlind3bf1ae2008-03-18 22:22:53 +0000667#if FULL_UNIVERSAL
668 if (!UniversalSet->PointsTo->test(FindNode(getNode(P))))
669 return NoModRef; // Universal set does not contain P
670#else
Daniel Berlinaad15882007-09-16 21:45:02 +0000671 if (!N1->PointsTo->test(UniversalSet))
Chris Lattnerf392c642005-03-28 06:21:17 +0000672 return NoModRef; // P doesn't point to the universal set.
Daniel Berlind3bf1ae2008-03-18 22:22:53 +0000673#endif
Chris Lattnerf392c642005-03-28 06:21:17 +0000674 }
675
676 return AliasAnalysis::getModRefInfo(CS, P, Size);
677}
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000678
Reid Spencer3a9ec242006-08-28 01:02:49 +0000679AliasAnalysis::ModRefResult
680Andersens::getModRefInfo(CallSite CS1, CallSite CS2) {
681 return AliasAnalysis::getModRefInfo(CS1,CS2);
682}
683
Chris Lattnere995a2a2004-05-23 21:00:47 +0000684/// getMustAlias - We can provide must alias information if we know that a
685/// pointer can only point to a specific function or the null pointer.
686/// Unfortunately we cannot determine must-alias information for global
687/// variables or any other memory memory objects because we do not track whether
688/// a pointer points to the beginning of an object or a field of it.
689void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Daniel Berlinaad15882007-09-16 21:45:02 +0000690 Node *N = &GraphNodes[FindNode(getNode(P))];
691 if (N->PointsTo->count() == 1) {
692 Node *Pointee = &GraphNodes[N->PointsTo->find_first()];
693 // If a function is the only object in the points-to set, then it must be
694 // the destination. Note that we can't handle global variables here,
695 // because we don't know if the pointer is actually pointing to a field of
696 // the global or to the beginning of it.
697 if (Value *V = Pointee->getValue()) {
698 if (Function *F = dyn_cast<Function>(V))
699 RetVals.push_back(F);
700 } else {
701 // If the object in the points-to set is the null object, then the null
702 // pointer is a must alias.
703 if (Pointee == &GraphNodes[NullObject])
Owen Andersona7235ea2009-07-31 20:28:14 +0000704 RetVals.push_back(Constant::getNullValue(P->getType()));
Chris Lattnere995a2a2004-05-23 21:00:47 +0000705 }
706 }
Chris Lattnere995a2a2004-05-23 21:00:47 +0000707 AliasAnalysis::getMustAliases(P, RetVals);
708}
709
710/// pointsToConstantMemory - If we can determine that this pointer only points
711/// to constant memory, return true. In practice, this means that if the
712/// pointer can only point to constant globals, functions, or the null pointer,
713/// return true.
714///
715bool Andersens::pointsToConstantMemory(const Value *P) {
Dan Gohman6a551e72008-02-21 17:33:24 +0000716 Node *N = &GraphNodes[FindNode(getNode(const_cast<Value*>(P)))];
Daniel Berlinaad15882007-09-16 21:45:02 +0000717 unsigned i;
718
719 for (SparseBitVector<>::iterator bi = N->PointsTo->begin();
720 bi != N->PointsTo->end();
721 ++bi) {
722 i = *bi;
723 Node *Pointee = &GraphNodes[i];
724 if (Value *V = Pointee->getValue()) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000725 if (!isa<GlobalValue>(V) || (isa<GlobalVariable>(V) &&
726 !cast<GlobalVariable>(V)->isConstant()))
727 return AliasAnalysis::pointsToConstantMemory(P);
728 } else {
Daniel Berlinaad15882007-09-16 21:45:02 +0000729 if (i != NullObject)
Chris Lattnere995a2a2004-05-23 21:00:47 +0000730 return AliasAnalysis::pointsToConstantMemory(P);
731 }
732 }
733
734 return true;
735}
736
737//===----------------------------------------------------------------------===//
738// Object Identification Phase
739//===----------------------------------------------------------------------===//
740
741/// IdentifyObjects - This stage scans the program, adding an entry to the
742/// GraphNodes list for each memory object in the program (global stack or
743/// heap), and populates the ValueNodes and ObjectNodes maps for these objects.
744///
745void Andersens::IdentifyObjects(Module &M) {
746 unsigned NumObjects = 0;
747
748 // Object #0 is always the universal set: the object that we don't know
749 // anything about.
750 assert(NumObjects == UniversalSet && "Something changed!");
751 ++NumObjects;
752
753 // Object #1 always represents the null pointer.
754 assert(NumObjects == NullPtr && "Something changed!");
755 ++NumObjects;
756
757 // Object #2 always represents the null object (the object pointed to by null)
758 assert(NumObjects == NullObject && "Something changed!");
759 ++NumObjects;
760
761 // Add all the globals first.
Chris Lattner493f6362005-03-27 22:03:46 +0000762 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
763 I != E; ++I) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000764 ObjectNodes[I] = NumObjects++;
765 ValueNodes[I] = NumObjects++;
766 }
767
768 // Add nodes for all of the functions and the instructions inside of them.
769 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
770 // The function itself is a memory object.
Daniel Berlinaad15882007-09-16 21:45:02 +0000771 unsigned First = NumObjects;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000772 ValueNodes[F] = NumObjects++;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000773 if (isa<PointerType>(F->getFunctionType()->getReturnType()))
774 ReturnNodes[F] = NumObjects++;
775 if (F->getFunctionType()->isVarArg())
776 VarargNodes[F] = NumObjects++;
777
Daniel Berlinaad15882007-09-16 21:45:02 +0000778
Chris Lattnere995a2a2004-05-23 21:00:47 +0000779 // Add nodes for all of the incoming pointer arguments.
Chris Lattner493f6362005-03-27 22:03:46 +0000780 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
781 I != E; ++I)
Daniel Berlind81ccc22007-09-24 19:45:49 +0000782 {
783 if (isa<PointerType>(I->getType()))
784 ValueNodes[I] = NumObjects++;
785 }
Daniel Berlinaad15882007-09-16 21:45:02 +0000786 MaxK[First] = NumObjects - First;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000787
788 // Scan the function body, creating a memory object for each heap/stack
789 // allocation in the body of the function and a node to represent all
790 // pointer values defined by instructions and used as operands.
791 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
792 // If this is an heap or stack allocation, create a node for the memory
793 // object.
794 if (isa<PointerType>(II->getType())) {
795 ValueNodes[&*II] = NumObjects++;
Victor Hernandez7b929da2009-10-23 21:09:37 +0000796 if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
Chris Lattnere995a2a2004-05-23 21:00:47 +0000797 ObjectNodes[AI] = NumObjects++;
Victor Hernandez46e83122009-09-18 21:34:51 +0000798 else if (isMalloc(&*II))
799 ObjectNodes[&*II] = NumObjects++;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000800 }
Nick Lewycky4ac0e8d2007-11-22 03:07:37 +0000801
802 // Calls to inline asm need to be added as well because the callee isn't
803 // referenced anywhere else.
804 if (CallInst *CI = dyn_cast<CallInst>(&*II)) {
805 Value *Callee = CI->getCalledValue();
806 if (isa<InlineAsm>(Callee))
807 ValueNodes[Callee] = NumObjects++;
808 }
Chris Lattnere995a2a2004-05-23 21:00:47 +0000809 }
810 }
811
812 // Now that we know how many objects to create, make them all now!
813 GraphNodes.resize(NumObjects);
814 NumNodes += NumObjects;
815}
816
817//===----------------------------------------------------------------------===//
818// Constraint Identification Phase
819//===----------------------------------------------------------------------===//
820
821/// getNodeForConstantPointer - Return the node corresponding to the constant
822/// pointer itself.
Daniel Berlinaad15882007-09-16 21:45:02 +0000823unsigned Andersens::getNodeForConstantPointer(Constant *C) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000824 assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
825
Chris Lattner267a1b02005-03-27 18:58:23 +0000826 if (isa<ConstantPointerNull>(C) || isa<UndefValue>(C))
Daniel Berlinaad15882007-09-16 21:45:02 +0000827 return NullPtr;
Reid Spencere8404342004-07-18 00:18:30 +0000828 else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
829 return getNode(GV);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000830 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
831 switch (CE->getOpcode()) {
832 case Instruction::GetElementPtr:
833 return getNodeForConstantPointer(CE->getOperand(0));
Reid Spencer3da59db2006-11-27 01:05:10 +0000834 case Instruction::IntToPtr:
Daniel Berlinaad15882007-09-16 21:45:02 +0000835 return UniversalSet;
Reid Spencer3da59db2006-11-27 01:05:10 +0000836 case Instruction::BitCast:
837 return getNodeForConstantPointer(CE->getOperand(0));
Chris Lattnere995a2a2004-05-23 21:00:47 +0000838 default:
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000839 errs() << "Constant Expr not yet handled: " << *CE << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000840 llvm_unreachable(0);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000841 }
842 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000843 llvm_unreachable("Unknown constant pointer!");
Chris Lattnere995a2a2004-05-23 21:00:47 +0000844 }
Chris Lattner1fc37392004-05-27 20:57:01 +0000845 return 0;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000846}
847
848/// getNodeForConstantPointerTarget - Return the node POINTED TO by the
849/// specified constant pointer.
Daniel Berlinaad15882007-09-16 21:45:02 +0000850unsigned Andersens::getNodeForConstantPointerTarget(Constant *C) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000851 assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
852
853 if (isa<ConstantPointerNull>(C))
Daniel Berlinaad15882007-09-16 21:45:02 +0000854 return NullObject;
Reid Spencere8404342004-07-18 00:18:30 +0000855 else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
856 return getObject(GV);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000857 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
858 switch (CE->getOpcode()) {
859 case Instruction::GetElementPtr:
860 return getNodeForConstantPointerTarget(CE->getOperand(0));
Reid Spencer3da59db2006-11-27 01:05:10 +0000861 case Instruction::IntToPtr:
Daniel Berlinaad15882007-09-16 21:45:02 +0000862 return UniversalSet;
Reid Spencer3da59db2006-11-27 01:05:10 +0000863 case Instruction::BitCast:
864 return getNodeForConstantPointerTarget(CE->getOperand(0));
Chris Lattnere995a2a2004-05-23 21:00:47 +0000865 default:
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000866 errs() << "Constant Expr not yet handled: " << *CE << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000867 llvm_unreachable(0);
Chris Lattnere995a2a2004-05-23 21:00:47 +0000868 }
869 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000870 llvm_unreachable("Unknown constant pointer!");
Chris Lattnere995a2a2004-05-23 21:00:47 +0000871 }
Chris Lattner1fc37392004-05-27 20:57:01 +0000872 return 0;
Chris Lattnere995a2a2004-05-23 21:00:47 +0000873}
874
875/// AddGlobalInitializerConstraints - Add inclusion constraints for the memory
876/// object N, which contains values indicated by C.
Daniel Berlinaad15882007-09-16 21:45:02 +0000877void Andersens::AddGlobalInitializerConstraints(unsigned NodeIndex,
878 Constant *C) {
Dan Gohmanb64aa112008-05-22 23:43:22 +0000879 if (C->getType()->isSingleValueType()) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000880 if (isa<PointerType>(C->getType()))
Daniel Berlinaad15882007-09-16 21:45:02 +0000881 Constraints.push_back(Constraint(Constraint::Copy, NodeIndex,
882 getNodeForConstantPointer(C)));
Chris Lattnere995a2a2004-05-23 21:00:47 +0000883 } else if (C->isNullValue()) {
Daniel Berlinaad15882007-09-16 21:45:02 +0000884 Constraints.push_back(Constraint(Constraint::Copy, NodeIndex,
885 NullObject));
Chris Lattnere995a2a2004-05-23 21:00:47 +0000886 return;
Chris Lattner8a446432005-03-29 06:09:07 +0000887 } else if (!isa<UndefValue>(C)) {
Chris Lattnere995a2a2004-05-23 21:00:47 +0000888 // If this is an array or struct, include constraints for each element.
889 assert(isa<ConstantArray>(C) || isa<ConstantStruct>(C));
890 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
Daniel Berlinaad15882007-09-16 21:45:02 +0000891 AddGlobalInitializerConstraints(NodeIndex,
892 cast<Constant>(C->getOperand(i)));
Chris Lattnere995a2a2004-05-23 21:00:47 +0000893 }
894}
895
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000896/// AddConstraintsForNonInternalLinkage - If this function does not have
897/// internal linkage, realize that we can't trust anything passed into or
898/// returned by this function.
Chris Lattnere995a2a2004-05-23 21:00:47 +0000899void Andersens::AddConstraintsForNonInternalLinkage(Function *F) {
Chris Lattnere4d5c442005-03-15 04:54:21 +0000900 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattnere995a2a2004-05-23 21:00:47 +0000901 if (isa<PointerType>(I->getType()))
902 // If this is an argument of an externally accessible function, the
903 // incoming pointer might point to anything.
904 Constraints.push_back(Constraint(Constraint::Copy, getNode(I),
Daniel Berlinaad15882007-09-16 21:45:02 +0000905 UniversalSet));
Chris Lattnere995a2a2004-05-23 21:00:47 +0000906}
907
Chris Lattner8a446432005-03-29 06:09:07 +0000908/// AddConstraintsForCall - If this is a call to a "known" function, add the
909/// constraints and return true. If this is a call to an unknown function,
910/// return false.
911bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000912 assert(F->isDeclaration() && "Not an external function!");
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000913
914 // These functions don't induce any points-to constraints.
Chris Lattner175b9632005-03-29 20:36:05 +0000915 if (F->getName() == "atoi" || F->getName() == "atof" ||
916 F->getName() == "atol" || F->getName() == "atoll" ||
917 F->getName() == "remove" || F->getName() == "unlink" ||
918 F->getName() == "rename" || F->getName() == "memcmp" ||
Chris Lattner824b9582008-11-21 16:42:48 +0000919 F->getName() == "llvm.memset" ||
Chris Lattner175b9632005-03-29 20:36:05 +0000920 F->getName() == "strcmp" || F->getName() == "strncmp" ||
921 F->getName() == "execl" || F->getName() == "execlp" ||
922 F->getName() == "execle" || F->getName() == "execv" ||
923 F->getName() == "execvp" || F->getName() == "chmod" ||
924 F->getName() == "puts" || F->getName() == "write" ||
925 F->getName() == "open" || F->getName() == "create" ||
926 F->getName() == "truncate" || F->getName() == "chdir" ||
927 F->getName() == "mkdir" || F->getName() == "rmdir" ||
928 F->getName() == "read" || F->getName() == "pipe" ||
929 F->getName() == "wait" || F->getName() == "time" ||
930 F->getName() == "stat" || F->getName() == "fstat" ||
931 F->getName() == "lstat" || F->getName() == "strtod" ||
932 F->getName() == "strtof" || F->getName() == "strtold" ||
933 F->getName() == "fopen" || F->getName() == "fdopen" ||
934 F->getName() == "freopen" ||
935 F->getName() == "fflush" || F->getName() == "feof" ||
936 F->getName() == "fileno" || F->getName() == "clearerr" ||
937 F->getName() == "rewind" || F->getName() == "ftell" ||
938 F->getName() == "ferror" || F->getName() == "fgetc" ||
939 F->getName() == "fgetc" || F->getName() == "_IO_getc" ||
940 F->getName() == "fwrite" || F->getName() == "fread" ||
941 F->getName() == "fgets" || F->getName() == "ungetc" ||
942 F->getName() == "fputc" ||
943 F->getName() == "fputs" || F->getName() == "putc" ||
944 F->getName() == "ftell" || F->getName() == "rewind" ||
945 F->getName() == "_IO_putc" || F->getName() == "fseek" ||
946 F->getName() == "fgetpos" || F->getName() == "fsetpos" ||
947 F->getName() == "printf" || F->getName() == "fprintf" ||
948 F->getName() == "sprintf" || F->getName() == "vprintf" ||
949 F->getName() == "vfprintf" || F->getName() == "vsprintf" ||
950 F->getName() == "scanf" || F->getName() == "fscanf" ||
951 F->getName() == "sscanf" || F->getName() == "__assert_fail" ||
952 F->getName() == "modf")
Chris Lattner8a446432005-03-29 06:09:07 +0000953 return true;
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000954
Chris Lattner175b9632005-03-29 20:36:05 +0000955
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000956 // These functions do induce points-to edges.
Chris Lattner824b9582008-11-21 16:42:48 +0000957 if (F->getName() == "llvm.memcpy" ||
958 F->getName() == "llvm.memmove" ||
Chris Lattner4de57fd2005-03-29 06:52:20 +0000959 F->getName() == "memmove") {
Daniel Berlinaad15882007-09-16 21:45:02 +0000960
Nick Lewycky3037eda2008-12-27 16:20:53 +0000961 const FunctionType *FTy = F->getFunctionType();
962 if (FTy->getNumParams() > 1 &&
963 isa<PointerType>(FTy->getParamType(0)) &&
964 isa<PointerType>(FTy->getParamType(1))) {
965
966 // *Dest = *Src, which requires an artificial graph node to represent the
967 // constraint. It is broken up into *Dest = temp, temp = *Src
968 unsigned FirstArg = getNode(CS.getArgument(0));
969 unsigned SecondArg = getNode(CS.getArgument(1));
970 unsigned TempArg = GraphNodes.size();
971 GraphNodes.push_back(Node());
972 Constraints.push_back(Constraint(Constraint::Store,
973 FirstArg, TempArg));
974 Constraints.push_back(Constraint(Constraint::Load,
975 TempArg, SecondArg));
976 // In addition, Dest = Src
977 Constraints.push_back(Constraint(Constraint::Copy,
978 FirstArg, SecondArg));
979 return true;
980 }
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000981 }
982
Chris Lattner77b50562005-03-29 20:04:24 +0000983 // Result = Arg0
984 if (F->getName() == "realloc" || F->getName() == "strchr" ||
985 F->getName() == "strrchr" || F->getName() == "strstr" ||
986 F->getName() == "strtok") {
Nick Lewycky3037eda2008-12-27 16:20:53 +0000987 const FunctionType *FTy = F->getFunctionType();
988 if (FTy->getNumParams() > 0 &&
989 isa<PointerType>(FTy->getParamType(0))) {
990 Constraints.push_back(Constraint(Constraint::Copy,
991 getNode(CS.getInstruction()),
992 getNode(CS.getArgument(0))));
993 return true;
994 }
Chris Lattner8a446432005-03-29 06:09:07 +0000995 }
996
997 return false;
Chris Lattnerc3c9fd02005-03-28 04:03:52 +0000998}
999
1000
Chris Lattnere995a2a2004-05-23 21:00:47 +00001001
Daniel Berlinaad15882007-09-16 21:45:02 +00001002/// AnalyzeUsesOfFunction - Look at all of the users of the specified function.
1003/// If this is used by anything complex (i.e., the address escapes), return
1004/// true.
1005bool Andersens::AnalyzeUsesOfFunction(Value *V) {
1006
1007 if (!isa<PointerType>(V->getType())) return true;
1008
1009 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
Dan Gohman104eac12009-08-11 17:20:16 +00001010 if (isa<LoadInst>(*UI)) {
Daniel Berlinaad15882007-09-16 21:45:02 +00001011 return false;
1012 } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
1013 if (V == SI->getOperand(1)) {
1014 return false;
1015 } else if (SI->getOperand(1)) {
1016 return true; // Storing the pointer
1017 }
1018 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
1019 if (AnalyzeUsesOfFunction(GEP)) return true;
1020 } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
1021 // Make sure that this is just the function being called, not that it is
1022 // passing into the function.
1023 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
1024 if (CI->getOperand(i) == V) return true;
1025 } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
1026 // Make sure that this is just the function being called, not that it is
1027 // passing into the function.
Gabor Greif03a5f132009-09-03 02:02:59 +00001028 for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
Daniel Berlinaad15882007-09-16 21:45:02 +00001029 if (II->getOperand(i) == V) return true;
1030 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
1031 if (CE->getOpcode() == Instruction::GetElementPtr ||
1032 CE->getOpcode() == Instruction::BitCast) {
1033 if (AnalyzeUsesOfFunction(CE))
1034 return true;
1035 } else {
1036 return true;
1037 }
1038 } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
1039 if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
1040 return true; // Allow comparison against null.
Dan Gohman104eac12009-08-11 17:20:16 +00001041 } else if (isa<FreeInst>(*UI)) {
Daniel Berlinaad15882007-09-16 21:45:02 +00001042 return false;
1043 } else {
1044 return true;
1045 }
1046 return false;
1047}
1048
Chris Lattnere995a2a2004-05-23 21:00:47 +00001049/// CollectConstraints - This stage scans the program, adding a constraint to
1050/// the Constraints list for each instruction in the program that induces a
1051/// constraint, and setting up the initial points-to graph.
1052///
1053void Andersens::CollectConstraints(Module &M) {
1054 // First, the universal set points to itself.
Daniel Berlinaad15882007-09-16 21:45:02 +00001055 Constraints.push_back(Constraint(Constraint::AddressOf, UniversalSet,
1056 UniversalSet));
1057 Constraints.push_back(Constraint(Constraint::Store, UniversalSet,
1058 UniversalSet));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001059
1060 // Next, the null pointer points to the null object.
Daniel Berlinaad15882007-09-16 21:45:02 +00001061 Constraints.push_back(Constraint(Constraint::AddressOf, NullPtr, NullObject));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001062
1063 // Next, add any constraints on global variables and their initializers.
Chris Lattner493f6362005-03-27 22:03:46 +00001064 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1065 I != E; ++I) {
Chris Lattnere995a2a2004-05-23 21:00:47 +00001066 // Associate the address of the global object as pointing to the memory for
1067 // the global: &G = <G memory>
Daniel Berlinaad15882007-09-16 21:45:02 +00001068 unsigned ObjectIndex = getObject(I);
1069 Node *Object = &GraphNodes[ObjectIndex];
Chris Lattnere995a2a2004-05-23 21:00:47 +00001070 Object->setValue(I);
Daniel Berlinaad15882007-09-16 21:45:02 +00001071 Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(*I),
1072 ObjectIndex));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001073
Dan Gohman82555732009-08-19 18:20:44 +00001074 if (I->hasDefinitiveInitializer()) {
Daniel Berlinaad15882007-09-16 21:45:02 +00001075 AddGlobalInitializerConstraints(ObjectIndex, I->getInitializer());
Chris Lattnere995a2a2004-05-23 21:00:47 +00001076 } else {
1077 // If it doesn't have an initializer (i.e. it's defined in another
1078 // translation unit), it points to the universal set.
Daniel Berlinaad15882007-09-16 21:45:02 +00001079 Constraints.push_back(Constraint(Constraint::Copy, ObjectIndex,
1080 UniversalSet));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001081 }
1082 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001083
Chris Lattnere995a2a2004-05-23 21:00:47 +00001084 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Chris Lattnere995a2a2004-05-23 21:00:47 +00001085 // Set up the return value node.
1086 if (isa<PointerType>(F->getFunctionType()->getReturnType()))
Daniel Berlinaad15882007-09-16 21:45:02 +00001087 GraphNodes[getReturnNode(F)].setValue(F);
Chris Lattnere995a2a2004-05-23 21:00:47 +00001088 if (F->getFunctionType()->isVarArg())
Daniel Berlinaad15882007-09-16 21:45:02 +00001089 GraphNodes[getVarargNode(F)].setValue(F);
Chris Lattnere995a2a2004-05-23 21:00:47 +00001090
1091 // Set up incoming argument nodes.
Chris Lattner493f6362005-03-27 22:03:46 +00001092 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
1093 I != E; ++I)
Chris Lattnere995a2a2004-05-23 21:00:47 +00001094 if (isa<PointerType>(I->getType()))
1095 getNodeValue(*I);
1096
Daniel Berlinaad15882007-09-16 21:45:02 +00001097 // At some point we should just add constraints for the escaping functions
1098 // at solve time, but this slows down solving. For now, we simply mark
1099 // address taken functions as escaping and treat them as external.
Rafael Espindolabb46f522009-01-15 20:18:42 +00001100 if (!F->hasLocalLinkage() || AnalyzeUsesOfFunction(F))
Chris Lattnere995a2a2004-05-23 21:00:47 +00001101 AddConstraintsForNonInternalLinkage(F);
1102
Reid Spencer5cbf9852007-01-30 20:08:39 +00001103 if (!F->isDeclaration()) {
Chris Lattnere995a2a2004-05-23 21:00:47 +00001104 // Scan the function body, creating a memory object for each heap/stack
1105 // allocation in the body of the function and a node to represent all
1106 // pointer values defined by instructions and used as operands.
1107 visit(F);
Chris Lattner8a446432005-03-29 06:09:07 +00001108 } else {
Chris Lattnere995a2a2004-05-23 21:00:47 +00001109 // External functions that return pointers return the universal set.
1110 if (isa<PointerType>(F->getFunctionType()->getReturnType()))
1111 Constraints.push_back(Constraint(Constraint::Copy,
1112 getReturnNode(F),
Daniel Berlinaad15882007-09-16 21:45:02 +00001113 UniversalSet));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001114
1115 // Any pointers that are passed into the function have the universal set
1116 // stored into them.
Chris Lattner493f6362005-03-27 22:03:46 +00001117 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
1118 I != E; ++I)
Chris Lattnere995a2a2004-05-23 21:00:47 +00001119 if (isa<PointerType>(I->getType())) {
1120 // Pointers passed into external functions could have anything stored
1121 // through them.
1122 Constraints.push_back(Constraint(Constraint::Store, getNode(I),
Daniel Berlinaad15882007-09-16 21:45:02 +00001123 UniversalSet));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001124 // Memory objects passed into external function calls can have the
1125 // universal set point to them.
Daniel Berlin3a3f1632007-12-12 00:37:04 +00001126#if FULL_UNIVERSAL
Chris Lattnere995a2a2004-05-23 21:00:47 +00001127 Constraints.push_back(Constraint(Constraint::Copy,
Daniel Berlinaad15882007-09-16 21:45:02 +00001128 UniversalSet,
Chris Lattnere995a2a2004-05-23 21:00:47 +00001129 getNode(I)));
Daniel Berlin3a3f1632007-12-12 00:37:04 +00001130#else
1131 Constraints.push_back(Constraint(Constraint::Copy,
1132 getNode(I),
1133 UniversalSet));
1134#endif
Chris Lattnere995a2a2004-05-23 21:00:47 +00001135 }
1136
1137 // If this is an external varargs function, it can also store pointers
1138 // into any pointers passed through the varargs section.
1139 if (F->getFunctionType()->isVarArg())
1140 Constraints.push_back(Constraint(Constraint::Store, getVarargNode(F),
Daniel Berlinaad15882007-09-16 21:45:02 +00001141 UniversalSet));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001142 }
1143 }
1144 NumConstraints += Constraints.size();
1145}
1146
1147
1148void Andersens::visitInstruction(Instruction &I) {
1149#ifdef NDEBUG
1150 return; // This function is just a big assert.
1151#endif
1152 if (isa<BinaryOperator>(I))
1153 return;
1154 // Most instructions don't have any effect on pointer values.
1155 switch (I.getOpcode()) {
1156 case Instruction::Br:
1157 case Instruction::Switch:
1158 case Instruction::Unwind:
Chris Lattnerc17edbd2004-10-16 18:16:19 +00001159 case Instruction::Unreachable:
Chris Lattnere995a2a2004-05-23 21:00:47 +00001160 case Instruction::Free:
Reid Spencere4d87aa2006-12-23 06:05:41 +00001161 case Instruction::ICmp:
1162 case Instruction::FCmp:
Chris Lattnere995a2a2004-05-23 21:00:47 +00001163 return;
1164 default:
1165 // Is this something we aren't handling yet?
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00001166 errs() << "Unknown instruction: " << I;
Torok Edwinc23197a2009-07-14 16:55:14 +00001167 llvm_unreachable(0);
Chris Lattnere995a2a2004-05-23 21:00:47 +00001168 }
1169}
1170
Victor Hernandez7b929da2009-10-23 21:09:37 +00001171void Andersens::visitAllocaInst(AllocaInst &I) {
1172 visitAlloc(I);
1173}
1174
1175void Andersens::visitAlloc(Instruction &I) {
Victor Hernandez46e83122009-09-18 21:34:51 +00001176 unsigned ObjectIndex = getObject(&I);
1177 GraphNodes[ObjectIndex].setValue(&I);
1178 Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(I),
Daniel Berlinaad15882007-09-16 21:45:02 +00001179 ObjectIndex));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001180}
1181
1182void Andersens::visitReturnInst(ReturnInst &RI) {
1183 if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType()))
1184 // return V --> <Copy/retval{F}/v>
1185 Constraints.push_back(Constraint(Constraint::Copy,
1186 getReturnNode(RI.getParent()->getParent()),
1187 getNode(RI.getOperand(0))));
1188}
1189
1190void Andersens::visitLoadInst(LoadInst &LI) {
1191 if (isa<PointerType>(LI.getType()))
1192 // P1 = load P2 --> <Load/P1/P2>
1193 Constraints.push_back(Constraint(Constraint::Load, getNodeValue(LI),
1194 getNode(LI.getOperand(0))));
1195}
1196
1197void Andersens::visitStoreInst(StoreInst &SI) {
1198 if (isa<PointerType>(SI.getOperand(0)->getType()))
1199 // store P1, P2 --> <Store/P2/P1>
1200 Constraints.push_back(Constraint(Constraint::Store,
1201 getNode(SI.getOperand(1)),
1202 getNode(SI.getOperand(0))));
1203}
1204
1205void Andersens::visitGetElementPtrInst(GetElementPtrInst &GEP) {
1206 // P1 = getelementptr P2, ... --> <Copy/P1/P2>
1207 Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(GEP),
1208 getNode(GEP.getOperand(0))));
1209}
1210
1211void Andersens::visitPHINode(PHINode &PN) {
1212 if (isa<PointerType>(PN.getType())) {
Daniel Berlinaad15882007-09-16 21:45:02 +00001213 unsigned PNN = getNodeValue(PN);
Chris Lattnere995a2a2004-05-23 21:00:47 +00001214 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1215 // P1 = phi P2, P3 --> <Copy/P1/P2>, <Copy/P1/P3>, ...
1216 Constraints.push_back(Constraint(Constraint::Copy, PNN,
1217 getNode(PN.getIncomingValue(i))));
1218 }
1219}
1220
1221void Andersens::visitCastInst(CastInst &CI) {
1222 Value *Op = CI.getOperand(0);
1223 if (isa<PointerType>(CI.getType())) {
1224 if (isa<PointerType>(Op->getType())) {
1225 // P1 = cast P2 --> <Copy/P1/P2>
1226 Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
1227 getNode(CI.getOperand(0))));
1228 } else {
1229 // P1 = cast int --> <Copy/P1/Univ>
Chris Lattner175b9632005-03-29 20:36:05 +00001230#if 0
Chris Lattnere995a2a2004-05-23 21:00:47 +00001231 Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
Daniel Berlinaad15882007-09-16 21:45:02 +00001232 UniversalSet));
Chris Lattnerbd135c72005-04-05 01:12:03 +00001233#else
1234 getNodeValue(CI);
Chris Lattner175b9632005-03-29 20:36:05 +00001235#endif
Chris Lattnere995a2a2004-05-23 21:00:47 +00001236 }
1237 } else if (isa<PointerType>(Op->getType())) {
1238 // int = cast P1 --> <Copy/Univ/P1>
Chris Lattner175b9632005-03-29 20:36:05 +00001239#if 0
Chris Lattnere995a2a2004-05-23 21:00:47 +00001240 Constraints.push_back(Constraint(Constraint::Copy,
Daniel Berlinaad15882007-09-16 21:45:02 +00001241 UniversalSet,
Chris Lattnere995a2a2004-05-23 21:00:47 +00001242 getNode(CI.getOperand(0))));
Chris Lattnerbd135c72005-04-05 01:12:03 +00001243#else
1244 getNode(CI.getOperand(0));
Chris Lattner175b9632005-03-29 20:36:05 +00001245#endif
Chris Lattnere995a2a2004-05-23 21:00:47 +00001246 }
1247}
1248
1249void Andersens::visitSelectInst(SelectInst &SI) {
1250 if (isa<PointerType>(SI.getType())) {
Daniel Berlinaad15882007-09-16 21:45:02 +00001251 unsigned SIN = getNodeValue(SI);
Chris Lattnere995a2a2004-05-23 21:00:47 +00001252 // P1 = select C, P2, P3 ---> <Copy/P1/P2>, <Copy/P1/P3>
1253 Constraints.push_back(Constraint(Constraint::Copy, SIN,
1254 getNode(SI.getOperand(1))));
1255 Constraints.push_back(Constraint(Constraint::Copy, SIN,
1256 getNode(SI.getOperand(2))));
1257 }
1258}
1259
Chris Lattnere995a2a2004-05-23 21:00:47 +00001260void Andersens::visitVAArg(VAArgInst &I) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001261 llvm_unreachable("vaarg not handled yet!");
Chris Lattnere995a2a2004-05-23 21:00:47 +00001262}
1263
1264/// AddConstraintsForCall - Add constraints for a call with actual arguments
1265/// specified by CS to the function specified by F. Note that the types of
1266/// arguments might not match up in the case where this is an indirect call and
1267/// the function pointer has been casted. If this is the case, do something
1268/// reasonable.
1269void Andersens::AddConstraintsForCall(CallSite CS, Function *F) {
Daniel Berlinaad15882007-09-16 21:45:02 +00001270 Value *CallValue = CS.getCalledValue();
1271 bool IsDeref = F == NULL;
1272
1273 // If this is a call to an external function, try to handle it directly to get
1274 // some taste of context sensitivity.
1275 if (F && F->isDeclaration() && AddConstraintsForExternalCall(CS, F))
Chris Lattner8a446432005-03-29 06:09:07 +00001276 return;
1277
Chris Lattnere995a2a2004-05-23 21:00:47 +00001278 if (isa<PointerType>(CS.getType())) {
Daniel Berlinaad15882007-09-16 21:45:02 +00001279 unsigned CSN = getNode(CS.getInstruction());
1280 if (!F || isa<PointerType>(F->getFunctionType()->getReturnType())) {
1281 if (IsDeref)
1282 Constraints.push_back(Constraint(Constraint::Load, CSN,
1283 getNode(CallValue), CallReturnPos));
1284 else
1285 Constraints.push_back(Constraint(Constraint::Copy, CSN,
1286 getNode(CallValue) + CallReturnPos));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001287 } else {
1288 // If the function returns a non-pointer value, handle this just like we
1289 // treat a nonpointer cast to pointer.
1290 Constraints.push_back(Constraint(Constraint::Copy, CSN,
Daniel Berlinaad15882007-09-16 21:45:02 +00001291 UniversalSet));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001292 }
Daniel Berlinaad15882007-09-16 21:45:02 +00001293 } else if (F && isa<PointerType>(F->getFunctionType()->getReturnType())) {
Daniel Berlin3a3f1632007-12-12 00:37:04 +00001294#if FULL_UNIVERSAL
Chris Lattnere995a2a2004-05-23 21:00:47 +00001295 Constraints.push_back(Constraint(Constraint::Copy,
Daniel Berlinaad15882007-09-16 21:45:02 +00001296 UniversalSet,
1297 getNode(CallValue) + CallReturnPos));
Daniel Berlin3a3f1632007-12-12 00:37:04 +00001298#else
1299 Constraints.push_back(Constraint(Constraint::Copy,
1300 getNode(CallValue) + CallReturnPos,
1301 UniversalSet));
1302#endif
1303
1304
Chris Lattnere995a2a2004-05-23 21:00:47 +00001305 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001306
Chris Lattnere995a2a2004-05-23 21:00:47 +00001307 CallSite::arg_iterator ArgI = CS.arg_begin(), ArgE = CS.arg_end();
Daniel Berlind3bf1ae2008-03-18 22:22:53 +00001308 bool external = !F || F->isDeclaration();
Daniel Berlinaad15882007-09-16 21:45:02 +00001309 if (F) {
1310 // Direct Call
1311 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
Daniel Berlind3bf1ae2008-03-18 22:22:53 +00001312 for (; AI != AE && ArgI != ArgE; ++AI, ++ArgI)
1313 {
1314#if !FULL_UNIVERSAL
1315 if (external && isa<PointerType>((*ArgI)->getType()))
1316 {
1317 // Add constraint that ArgI can now point to anything due to
1318 // escaping, as can everything it points to. The second portion of
1319 // this should be taken care of by universal = *universal
1320 Constraints.push_back(Constraint(Constraint::Copy,
1321 getNode(*ArgI),
1322 UniversalSet));
1323 }
Daniel Berlin3a3f1632007-12-12 00:37:04 +00001324#endif
Daniel Berlind3bf1ae2008-03-18 22:22:53 +00001325 if (isa<PointerType>(AI->getType())) {
1326 if (isa<PointerType>((*ArgI)->getType())) {
1327 // Copy the actual argument into the formal argument.
1328 Constraints.push_back(Constraint(Constraint::Copy, getNode(AI),
1329 getNode(*ArgI)));
1330 } else {
1331 Constraints.push_back(Constraint(Constraint::Copy, getNode(AI),
1332 UniversalSet));
1333 }
1334 } else if (isa<PointerType>((*ArgI)->getType())) {
1335#if FULL_UNIVERSAL
1336 Constraints.push_back(Constraint(Constraint::Copy,
1337 UniversalSet,
1338 getNode(*ArgI)));
1339#else
1340 Constraints.push_back(Constraint(Constraint::Copy,
1341 getNode(*ArgI),
1342 UniversalSet));
1343#endif
1344 }
Daniel Berlinaad15882007-09-16 21:45:02 +00001345 }
1346 } else {
1347 //Indirect Call
1348 unsigned ArgPos = CallFirstArgPos;
1349 for (; ArgI != ArgE; ++ArgI) {
Chris Lattnere995a2a2004-05-23 21:00:47 +00001350 if (isa<PointerType>((*ArgI)->getType())) {
1351 // Copy the actual argument into the formal argument.
Daniel Berlinaad15882007-09-16 21:45:02 +00001352 Constraints.push_back(Constraint(Constraint::Store,
1353 getNode(CallValue),
1354 getNode(*ArgI), ArgPos++));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001355 } else {
Daniel Berlinaad15882007-09-16 21:45:02 +00001356 Constraints.push_back(Constraint(Constraint::Store,
1357 getNode (CallValue),
1358 UniversalSet, ArgPos++));
Chris Lattnere995a2a2004-05-23 21:00:47 +00001359 }
Chris Lattnere995a2a2004-05-23 21:00:47 +00001360 }
Daniel Berlinaad15882007-09-16 21:45:02 +00001361 }
Chris Lattnere995a2a2004-05-23 21:00:47 +00001362 // Copy all pointers passed through the varargs section to the varargs node.
Daniel Berlinaad15882007-09-16 21:45:02 +00001363 if (F && F->getFunctionType()->isVarArg())
Chris Lattnere995a2a2004-05-23 21:00:47 +00001364 for (; ArgI != ArgE; ++ArgI)
1365 if (isa<PointerType>((*ArgI)->getType()))
1366 Constraints.push_back(Constraint(Constraint::Copy, getVarargNode(F),
1367 getNode(*ArgI)));
1368 // If more arguments are passed in than we track, just drop them on the floor.
1369}
1370
1371void Andersens::visitCallSite(CallSite CS) {
1372 if (isa<PointerType>(CS.getType()))
1373 getNodeValue(*CS.getInstruction());
1374
1375 if (Function *F = CS.getCalledFunction()) {
1376 AddConstraintsForCall(CS, F);
1377 } else {
Daniel Berlinaad15882007-09-16 21:45:02 +00001378 AddConstraintsForCall(CS, NULL);
Chris Lattnere995a2a2004-05-23 21:00:47 +00001379 }
1380}
1381
1382//===----------------------------------------------------------------------===//
1383// Constraint Solving Phase
1384//===----------------------------------------------------------------------===//
1385
1386/// intersects - Return true if the points-to set of this node intersects
1387/// with the points-to set of the specified node.
1388bool Andersens::Node::intersects(Node *N) const {
Daniel Berlinaad15882007-09-16 21:45:02 +00001389 return PointsTo->intersects(N->PointsTo);
Chris Lattnere995a2a2004-05-23 21:00:47 +00001390}
1391
1392/// intersectsIgnoring - Return true if the points-to set of this node
1393/// intersects with the points-to set of the specified node on any nodes
1394/// except for the specified node to ignore.
Daniel Berlinaad15882007-09-16 21:45:02 +00001395bool Andersens::Node::intersectsIgnoring(Node *N, unsigned Ignoring) const {
1396 // TODO: If we are only going to call this with the same value for Ignoring,
1397 // we should move the special values out of the points-to bitmap.
1398 bool WeHadIt = PointsTo->test(Ignoring);
1399 bool NHadIt = N->PointsTo->test(Ignoring);
1400 bool Result = false;
1401 if (WeHadIt)
1402 PointsTo->reset(Ignoring);
1403 if (NHadIt)
1404 N->PointsTo->reset(Ignoring);
1405 Result = PointsTo->intersects(N->PointsTo);
1406 if (WeHadIt)
1407 PointsTo->set(Ignoring);
1408 if (NHadIt)
1409 N->PointsTo->set(Ignoring);
1410 return Result;
Chris Lattnere995a2a2004-05-23 21:00:47 +00001411}
1412
Daniel Berlind81ccc22007-09-24 19:45:49 +00001413
1414/// Clump together address taken variables so that the points-to sets use up
1415/// less space and can be operated on faster.
1416
1417void Andersens::ClumpAddressTaken() {
1418#undef DEBUG_TYPE
1419#define DEBUG_TYPE "anders-aa-renumber"
1420 std::vector<unsigned> Translate;
1421 std::vector<Node> NewGraphNodes;
1422
1423 Translate.resize(GraphNodes.size());
1424 unsigned NewPos = 0;
1425
1426 for (unsigned i = 0; i < Constraints.size(); ++i) {
1427 Constraint &C = Constraints[i];
1428 if (C.Type == Constraint::AddressOf) {
1429 GraphNodes[C.Src].AddressTaken = true;
1430 }
1431 }
1432 for (unsigned i = 0; i < NumberSpecialNodes; ++i) {
1433 unsigned Pos = NewPos++;
1434 Translate[i] = Pos;
1435 NewGraphNodes.push_back(GraphNodes[i]);
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001436 DEBUG(errs() << "Renumbering node " << i << " to node " << Pos << "\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001437 }
1438
1439 // I believe this ends up being faster than making two vectors and splicing
1440 // them.
1441 for (unsigned i = NumberSpecialNodes; i < GraphNodes.size(); ++i) {
1442 if (GraphNodes[i].AddressTaken) {
1443 unsigned Pos = NewPos++;
1444 Translate[i] = Pos;
1445 NewGraphNodes.push_back(GraphNodes[i]);
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001446 DEBUG(errs() << "Renumbering node " << i << " to node " << Pos << "\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001447 }
1448 }
1449
1450 for (unsigned i = NumberSpecialNodes; i < GraphNodes.size(); ++i) {
1451 if (!GraphNodes[i].AddressTaken) {
1452 unsigned Pos = NewPos++;
1453 Translate[i] = Pos;
1454 NewGraphNodes.push_back(GraphNodes[i]);
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001455 DEBUG(errs() << "Renumbering node " << i << " to node " << Pos << "\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001456 }
1457 }
1458
1459 for (DenseMap<Value*, unsigned>::iterator Iter = ValueNodes.begin();
1460 Iter != ValueNodes.end();
1461 ++Iter)
1462 Iter->second = Translate[Iter->second];
1463
1464 for (DenseMap<Value*, unsigned>::iterator Iter = ObjectNodes.begin();
1465 Iter != ObjectNodes.end();
1466 ++Iter)
1467 Iter->second = Translate[Iter->second];
1468
1469 for (DenseMap<Function*, unsigned>::iterator Iter = ReturnNodes.begin();
1470 Iter != ReturnNodes.end();
1471 ++Iter)
1472 Iter->second = Translate[Iter->second];
1473
1474 for (DenseMap<Function*, unsigned>::iterator Iter = VarargNodes.begin();
1475 Iter != VarargNodes.end();
1476 ++Iter)
1477 Iter->second = Translate[Iter->second];
1478
1479 for (unsigned i = 0; i < Constraints.size(); ++i) {
1480 Constraint &C = Constraints[i];
1481 C.Src = Translate[C.Src];
1482 C.Dest = Translate[C.Dest];
1483 }
1484
1485 GraphNodes.swap(NewGraphNodes);
1486#undef DEBUG_TYPE
1487#define DEBUG_TYPE "anders-aa"
1488}
1489
1490/// The technique used here is described in "Exploiting Pointer and Location
1491/// Equivalence to Optimize Pointer Analysis. In the 14th International Static
1492/// Analysis Symposium (SAS), August 2007." It is known as the "HVN" algorithm,
1493/// and is equivalent to value numbering the collapsed constraint graph without
1494/// evaluating unions. This is used as a pre-pass to HU in order to resolve
1495/// first order pointer dereferences and speed up/reduce memory usage of HU.
1496/// Running both is equivalent to HRU without the iteration
1497/// HVN in more detail:
1498/// Imagine the set of constraints was simply straight line code with no loops
1499/// (we eliminate cycles, so there are no loops), such as:
1500/// E = &D
1501/// E = &C
1502/// E = F
1503/// F = G
1504/// G = F
1505/// Applying value numbering to this code tells us:
1506/// G == F == E
1507///
1508/// For HVN, this is as far as it goes. We assign new value numbers to every
1509/// "address node", and every "reference node".
1510/// To get the optimal result for this, we use a DFS + SCC (since all nodes in a
1511/// cycle must have the same value number since the = operation is really
1512/// inclusion, not overwrite), and value number nodes we receive points-to sets
1513/// before we value our own node.
1514/// The advantage of HU over HVN is that HU considers the inclusion property, so
1515/// that if you have
1516/// E = &D
1517/// E = &C
1518/// E = F
1519/// F = G
1520/// F = &D
1521/// G = F
1522/// HU will determine that G == F == E. HVN will not, because it cannot prove
1523/// that the points to information ends up being the same because they all
1524/// receive &D from E anyway.
1525
1526void Andersens::HVN() {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001527 DEBUG(errs() << "Beginning HVN\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001528 // Build a predecessor graph. This is like our constraint graph with the
1529 // edges going in the opposite direction, and there are edges for all the
1530 // constraints, instead of just copy constraints. We also build implicit
1531 // edges for constraints are implied but not explicit. I.E for the constraint
1532 // a = &b, we add implicit edges *a = b. This helps us capture more cycles
1533 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
1534 Constraint &C = Constraints[i];
1535 if (C.Type == Constraint::AddressOf) {
1536 GraphNodes[C.Src].AddressTaken = true;
1537 GraphNodes[C.Src].Direct = false;
1538
1539 // Dest = &src edge
1540 unsigned AdrNode = C.Src + FirstAdrNode;
1541 if (!GraphNodes[C.Dest].PredEdges)
1542 GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
1543 GraphNodes[C.Dest].PredEdges->set(AdrNode);
1544
1545 // *Dest = src edge
1546 unsigned RefNode = C.Dest + FirstRefNode;
1547 if (!GraphNodes[RefNode].ImplicitPredEdges)
1548 GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
1549 GraphNodes[RefNode].ImplicitPredEdges->set(C.Src);
1550 } else if (C.Type == Constraint::Load) {
1551 if (C.Offset == 0) {
1552 // dest = *src edge
1553 if (!GraphNodes[C.Dest].PredEdges)
1554 GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
1555 GraphNodes[C.Dest].PredEdges->set(C.Src + FirstRefNode);
1556 } else {
1557 GraphNodes[C.Dest].Direct = false;
1558 }
1559 } else if (C.Type == Constraint::Store) {
1560 if (C.Offset == 0) {
1561 // *dest = src edge
1562 unsigned RefNode = C.Dest + FirstRefNode;
1563 if (!GraphNodes[RefNode].PredEdges)
1564 GraphNodes[RefNode].PredEdges = new SparseBitVector<>;
1565 GraphNodes[RefNode].PredEdges->set(C.Src);
1566 }
1567 } else {
1568 // Dest = Src edge and *Dest = *Src edge
1569 if (!GraphNodes[C.Dest].PredEdges)
1570 GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
1571 GraphNodes[C.Dest].PredEdges->set(C.Src);
1572 unsigned RefNode = C.Dest + FirstRefNode;
1573 if (!GraphNodes[RefNode].ImplicitPredEdges)
1574 GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
1575 GraphNodes[RefNode].ImplicitPredEdges->set(C.Src + FirstRefNode);
1576 }
1577 }
1578 PEClass = 1;
1579 // Do SCC finding first to condense our predecessor graph
1580 DFSNumber = 0;
1581 Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
1582 Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
1583 Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
1584
1585 for (unsigned i = 0; i < FirstRefNode; ++i) {
1586 unsigned Node = VSSCCRep[i];
1587 if (!Node2Visited[Node])
1588 HVNValNum(Node);
1589 }
1590 for (BitVectorMap::iterator Iter = Set2PEClass.begin();
1591 Iter != Set2PEClass.end();
1592 ++Iter)
1593 delete Iter->first;
1594 Set2PEClass.clear();
1595 Node2DFS.clear();
1596 Node2Deleted.clear();
1597 Node2Visited.clear();
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001598 DEBUG(errs() << "Finished HVN\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001599
1600}
1601
1602/// This is the workhorse of HVN value numbering. We combine SCC finding at the
1603/// same time because it's easy.
1604void Andersens::HVNValNum(unsigned NodeIndex) {
1605 unsigned MyDFS = DFSNumber++;
1606 Node *N = &GraphNodes[NodeIndex];
1607 Node2Visited[NodeIndex] = true;
1608 Node2DFS[NodeIndex] = MyDFS;
1609
1610 // First process all our explicit edges
1611 if (N->PredEdges)
1612 for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
1613 Iter != N->PredEdges->end();
1614 ++Iter) {
1615 unsigned j = VSSCCRep[*Iter];
1616 if (!Node2Deleted[j]) {
1617 if (!Node2Visited[j])
1618 HVNValNum(j);
1619 if (Node2DFS[NodeIndex] > Node2DFS[j])
1620 Node2DFS[NodeIndex] = Node2DFS[j];
1621 }
1622 }
1623
1624 // Now process all the implicit edges
1625 if (N->ImplicitPredEdges)
1626 for (SparseBitVector<>::iterator Iter = N->ImplicitPredEdges->begin();
1627 Iter != N->ImplicitPredEdges->end();
1628 ++Iter) {
1629 unsigned j = VSSCCRep[*Iter];
1630 if (!Node2Deleted[j]) {
1631 if (!Node2Visited[j])
1632 HVNValNum(j);
1633 if (Node2DFS[NodeIndex] > Node2DFS[j])
1634 Node2DFS[NodeIndex] = Node2DFS[j];
1635 }
1636 }
1637
1638 // See if we found any cycles
1639 if (MyDFS == Node2DFS[NodeIndex]) {
1640 while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) {
1641 unsigned CycleNodeIndex = SCCStack.top();
1642 Node *CycleNode = &GraphNodes[CycleNodeIndex];
1643 VSSCCRep[CycleNodeIndex] = NodeIndex;
1644 // Unify the nodes
1645 N->Direct &= CycleNode->Direct;
1646
1647 if (CycleNode->PredEdges) {
1648 if (!N->PredEdges)
1649 N->PredEdges = new SparseBitVector<>;
1650 *(N->PredEdges) |= CycleNode->PredEdges;
1651 delete CycleNode->PredEdges;
1652 CycleNode->PredEdges = NULL;
1653 }
1654 if (CycleNode->ImplicitPredEdges) {
1655 if (!N->ImplicitPredEdges)
1656 N->ImplicitPredEdges = new SparseBitVector<>;
1657 *(N->ImplicitPredEdges) |= CycleNode->ImplicitPredEdges;
1658 delete CycleNode->ImplicitPredEdges;
1659 CycleNode->ImplicitPredEdges = NULL;
1660 }
1661
1662 SCCStack.pop();
1663 }
1664
1665 Node2Deleted[NodeIndex] = true;
1666
1667 if (!N->Direct) {
1668 GraphNodes[NodeIndex].PointerEquivLabel = PEClass++;
1669 return;
1670 }
1671
1672 // Collect labels of successor nodes
1673 bool AllSame = true;
1674 unsigned First = ~0;
1675 SparseBitVector<> *Labels = new SparseBitVector<>;
1676 bool Used = false;
1677
1678 if (N->PredEdges)
1679 for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
1680 Iter != N->PredEdges->end();
1681 ++Iter) {
1682 unsigned j = VSSCCRep[*Iter];
1683 unsigned Label = GraphNodes[j].PointerEquivLabel;
1684 // Ignore labels that are equal to us or non-pointers
1685 if (j == NodeIndex || Label == 0)
1686 continue;
1687 if (First == (unsigned)~0)
1688 First = Label;
1689 else if (First != Label)
1690 AllSame = false;
1691 Labels->set(Label);
1692 }
1693
1694 // We either have a non-pointer, a copy of an existing node, or a new node.
1695 // Assign the appropriate pointer equivalence label.
1696 if (Labels->empty()) {
1697 GraphNodes[NodeIndex].PointerEquivLabel = 0;
1698 } else if (AllSame) {
1699 GraphNodes[NodeIndex].PointerEquivLabel = First;
1700 } else {
1701 GraphNodes[NodeIndex].PointerEquivLabel = Set2PEClass[Labels];
1702 if (GraphNodes[NodeIndex].PointerEquivLabel == 0) {
1703 unsigned EquivClass = PEClass++;
1704 Set2PEClass[Labels] = EquivClass;
1705 GraphNodes[NodeIndex].PointerEquivLabel = EquivClass;
1706 Used = true;
1707 }
1708 }
1709 if (!Used)
1710 delete Labels;
1711 } else {
1712 SCCStack.push(NodeIndex);
1713 }
1714}
1715
1716/// The technique used here is described in "Exploiting Pointer and Location
1717/// Equivalence to Optimize Pointer Analysis. In the 14th International Static
1718/// Analysis Symposium (SAS), August 2007." It is known as the "HU" algorithm,
1719/// and is equivalent to value numbering the collapsed constraint graph
1720/// including evaluating unions.
1721void Andersens::HU() {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001722 DEBUG(errs() << "Beginning HU\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001723 // Build a predecessor graph. This is like our constraint graph with the
1724 // edges going in the opposite direction, and there are edges for all the
1725 // constraints, instead of just copy constraints. We also build implicit
1726 // edges for constraints are implied but not explicit. I.E for the constraint
1727 // a = &b, we add implicit edges *a = b. This helps us capture more cycles
1728 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
1729 Constraint &C = Constraints[i];
1730 if (C.Type == Constraint::AddressOf) {
1731 GraphNodes[C.Src].AddressTaken = true;
1732 GraphNodes[C.Src].Direct = false;
1733
1734 GraphNodes[C.Dest].PointsTo->set(C.Src);
1735 // *Dest = src edge
1736 unsigned RefNode = C.Dest + FirstRefNode;
1737 if (!GraphNodes[RefNode].ImplicitPredEdges)
1738 GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
1739 GraphNodes[RefNode].ImplicitPredEdges->set(C.Src);
1740 GraphNodes[C.Src].PointedToBy->set(C.Dest);
1741 } else if (C.Type == Constraint::Load) {
1742 if (C.Offset == 0) {
1743 // dest = *src edge
1744 if (!GraphNodes[C.Dest].PredEdges)
1745 GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
1746 GraphNodes[C.Dest].PredEdges->set(C.Src + FirstRefNode);
1747 } else {
1748 GraphNodes[C.Dest].Direct = false;
1749 }
1750 } else if (C.Type == Constraint::Store) {
1751 if (C.Offset == 0) {
1752 // *dest = src edge
1753 unsigned RefNode = C.Dest + FirstRefNode;
1754 if (!GraphNodes[RefNode].PredEdges)
1755 GraphNodes[RefNode].PredEdges = new SparseBitVector<>;
1756 GraphNodes[RefNode].PredEdges->set(C.Src);
1757 }
1758 } else {
1759 // Dest = Src edge and *Dest = *Src edg
1760 if (!GraphNodes[C.Dest].PredEdges)
1761 GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
1762 GraphNodes[C.Dest].PredEdges->set(C.Src);
1763 unsigned RefNode = C.Dest + FirstRefNode;
1764 if (!GraphNodes[RefNode].ImplicitPredEdges)
1765 GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
1766 GraphNodes[RefNode].ImplicitPredEdges->set(C.Src + FirstRefNode);
1767 }
1768 }
1769 PEClass = 1;
1770 // Do SCC finding first to condense our predecessor graph
1771 DFSNumber = 0;
1772 Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
1773 Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
1774 Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
1775
1776 for (unsigned i = 0; i < FirstRefNode; ++i) {
1777 if (FindNode(i) == i) {
1778 unsigned Node = VSSCCRep[i];
1779 if (!Node2Visited[Node])
1780 Condense(Node);
1781 }
1782 }
1783
1784 // Reset tables for actual labeling
1785 Node2DFS.clear();
1786 Node2Visited.clear();
1787 Node2Deleted.clear();
1788 // Pre-grow our densemap so that we don't get really bad behavior
1789 Set2PEClass.resize(GraphNodes.size());
1790
1791 // Visit the condensed graph and generate pointer equivalence labels.
1792 Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
1793 for (unsigned i = 0; i < FirstRefNode; ++i) {
1794 if (FindNode(i) == i) {
1795 unsigned Node = VSSCCRep[i];
1796 if (!Node2Visited[Node])
1797 HUValNum(Node);
1798 }
1799 }
1800 // PEClass nodes will be deleted by the deleting of N->PointsTo in our caller.
1801 Set2PEClass.clear();
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001802 DEBUG(errs() << "Finished HU\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001803}
1804
1805
1806/// Implementation of standard Tarjan SCC algorithm as modified by Nuutilla.
1807void Andersens::Condense(unsigned NodeIndex) {
1808 unsigned MyDFS = DFSNumber++;
1809 Node *N = &GraphNodes[NodeIndex];
1810 Node2Visited[NodeIndex] = true;
1811 Node2DFS[NodeIndex] = MyDFS;
1812
1813 // First process all our explicit edges
1814 if (N->PredEdges)
1815 for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
1816 Iter != N->PredEdges->end();
1817 ++Iter) {
1818 unsigned j = VSSCCRep[*Iter];
1819 if (!Node2Deleted[j]) {
1820 if (!Node2Visited[j])
1821 Condense(j);
1822 if (Node2DFS[NodeIndex] > Node2DFS[j])
1823 Node2DFS[NodeIndex] = Node2DFS[j];
1824 }
1825 }
1826
1827 // Now process all the implicit edges
1828 if (N->ImplicitPredEdges)
1829 for (SparseBitVector<>::iterator Iter = N->ImplicitPredEdges->begin();
1830 Iter != N->ImplicitPredEdges->end();
1831 ++Iter) {
1832 unsigned j = VSSCCRep[*Iter];
1833 if (!Node2Deleted[j]) {
1834 if (!Node2Visited[j])
1835 Condense(j);
1836 if (Node2DFS[NodeIndex] > Node2DFS[j])
1837 Node2DFS[NodeIndex] = Node2DFS[j];
1838 }
1839 }
1840
1841 // See if we found any cycles
1842 if (MyDFS == Node2DFS[NodeIndex]) {
1843 while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) {
1844 unsigned CycleNodeIndex = SCCStack.top();
1845 Node *CycleNode = &GraphNodes[CycleNodeIndex];
1846 VSSCCRep[CycleNodeIndex] = NodeIndex;
1847 // Unify the nodes
1848 N->Direct &= CycleNode->Direct;
1849
1850 *(N->PointsTo) |= CycleNode->PointsTo;
1851 delete CycleNode->PointsTo;
1852 CycleNode->PointsTo = NULL;
1853 if (CycleNode->PredEdges) {
1854 if (!N->PredEdges)
1855 N->PredEdges = new SparseBitVector<>;
1856 *(N->PredEdges) |= CycleNode->PredEdges;
1857 delete CycleNode->PredEdges;
1858 CycleNode->PredEdges = NULL;
1859 }
1860 if (CycleNode->ImplicitPredEdges) {
1861 if (!N->ImplicitPredEdges)
1862 N->ImplicitPredEdges = new SparseBitVector<>;
1863 *(N->ImplicitPredEdges) |= CycleNode->ImplicitPredEdges;
1864 delete CycleNode->ImplicitPredEdges;
1865 CycleNode->ImplicitPredEdges = NULL;
1866 }
1867 SCCStack.pop();
1868 }
1869
1870 Node2Deleted[NodeIndex] = true;
1871
1872 // Set up number of incoming edges for other nodes
1873 if (N->PredEdges)
1874 for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
1875 Iter != N->PredEdges->end();
1876 ++Iter)
1877 ++GraphNodes[VSSCCRep[*Iter]].NumInEdges;
1878 } else {
1879 SCCStack.push(NodeIndex);
1880 }
1881}
1882
1883void Andersens::HUValNum(unsigned NodeIndex) {
1884 Node *N = &GraphNodes[NodeIndex];
1885 Node2Visited[NodeIndex] = true;
1886
1887 // Eliminate dereferences of non-pointers for those non-pointers we have
1888 // already identified. These are ref nodes whose non-ref node:
1889 // 1. Has already been visited determined to point to nothing (and thus, a
1890 // dereference of it must point to nothing)
1891 // 2. Any direct node with no predecessor edges in our graph and with no
1892 // points-to set (since it can't point to anything either, being that it
1893 // receives no points-to sets and has none).
1894 if (NodeIndex >= FirstRefNode) {
1895 unsigned j = VSSCCRep[FindNode(NodeIndex - FirstRefNode)];
1896 if ((Node2Visited[j] && !GraphNodes[j].PointerEquivLabel)
1897 || (GraphNodes[j].Direct && !GraphNodes[j].PredEdges
1898 && GraphNodes[j].PointsTo->empty())){
1899 return;
1900 }
1901 }
1902 // Process all our explicit edges
1903 if (N->PredEdges)
1904 for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
1905 Iter != N->PredEdges->end();
1906 ++Iter) {
1907 unsigned j = VSSCCRep[*Iter];
1908 if (!Node2Visited[j])
1909 HUValNum(j);
1910
1911 // If this edge turned out to be the same as us, or got no pointer
1912 // equivalence label (and thus points to nothing) , just decrement our
1913 // incoming edges and continue.
1914 if (j == NodeIndex || GraphNodes[j].PointerEquivLabel == 0) {
1915 --GraphNodes[j].NumInEdges;
1916 continue;
1917 }
1918
1919 *(N->PointsTo) |= GraphNodes[j].PointsTo;
1920
1921 // If we didn't end up storing this in the hash, and we're done with all
1922 // the edges, we don't need the points-to set anymore.
1923 --GraphNodes[j].NumInEdges;
1924 if (!GraphNodes[j].NumInEdges && !GraphNodes[j].StoredInHash) {
1925 delete GraphNodes[j].PointsTo;
1926 GraphNodes[j].PointsTo = NULL;
1927 }
1928 }
1929 // If this isn't a direct node, generate a fresh variable.
1930 if (!N->Direct) {
1931 N->PointsTo->set(FirstRefNode + NodeIndex);
1932 }
1933
1934 // See If we have something equivalent to us, if not, generate a new
1935 // equivalence class.
1936 if (N->PointsTo->empty()) {
1937 delete N->PointsTo;
1938 N->PointsTo = NULL;
1939 } else {
1940 if (N->Direct) {
1941 N->PointerEquivLabel = Set2PEClass[N->PointsTo];
1942 if (N->PointerEquivLabel == 0) {
1943 unsigned EquivClass = PEClass++;
1944 N->StoredInHash = true;
1945 Set2PEClass[N->PointsTo] = EquivClass;
1946 N->PointerEquivLabel = EquivClass;
1947 }
1948 } else {
1949 N->PointerEquivLabel = PEClass++;
1950 }
1951 }
1952}
1953
1954/// Rewrite our list of constraints so that pointer equivalent nodes are
1955/// replaced by their the pointer equivalence class representative.
1956void Andersens::RewriteConstraints() {
1957 std::vector<Constraint> NewConstraints;
Chris Lattnerbe207732007-09-30 00:47:20 +00001958 DenseSet<Constraint, ConstraintKeyInfo> Seen;
Daniel Berlind81ccc22007-09-24 19:45:49 +00001959
1960 PEClass2Node.clear();
1961 PENLEClass2Node.clear();
1962
1963 // We may have from 1 to Graphnodes + 1 equivalence classes.
1964 PEClass2Node.insert(PEClass2Node.begin(), GraphNodes.size() + 1, -1);
1965 PENLEClass2Node.insert(PENLEClass2Node.begin(), GraphNodes.size() + 1, -1);
1966
1967 // Rewrite constraints, ignoring non-pointer constraints, uniting equivalent
1968 // nodes, and rewriting constraints to use the representative nodes.
1969 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
1970 Constraint &C = Constraints[i];
1971 unsigned RHSNode = FindNode(C.Src);
1972 unsigned LHSNode = FindNode(C.Dest);
1973 unsigned RHSLabel = GraphNodes[VSSCCRep[RHSNode]].PointerEquivLabel;
1974 unsigned LHSLabel = GraphNodes[VSSCCRep[LHSNode]].PointerEquivLabel;
1975
1976 // First we try to eliminate constraints for things we can prove don't point
1977 // to anything.
1978 if (LHSLabel == 0) {
1979 DEBUG(PrintNode(&GraphNodes[LHSNode]));
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001980 DEBUG(errs() << " is a non-pointer, ignoring constraint.\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001981 continue;
1982 }
1983 if (RHSLabel == 0) {
1984 DEBUG(PrintNode(&GraphNodes[RHSNode]));
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001985 DEBUG(errs() << " is a non-pointer, ignoring constraint.\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00001986 continue;
1987 }
1988 // This constraint may be useless, and it may become useless as we translate
1989 // it.
1990 if (C.Src == C.Dest && C.Type == Constraint::Copy)
1991 continue;
Daniel Berlinc7a12ae2007-09-27 15:42:23 +00001992
Daniel Berlind81ccc22007-09-24 19:45:49 +00001993 C.Src = FindEquivalentNode(RHSNode, RHSLabel);
1994 C.Dest = FindEquivalentNode(FindNode(LHSNode), LHSLabel);
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00001995 if ((C.Src == C.Dest && C.Type == Constraint::Copy)
Chris Lattnerbe207732007-09-30 00:47:20 +00001996 || Seen.count(C))
Daniel Berlind81ccc22007-09-24 19:45:49 +00001997 continue;
1998
Chris Lattnerbe207732007-09-30 00:47:20 +00001999 Seen.insert(C);
Daniel Berlind81ccc22007-09-24 19:45:49 +00002000 NewConstraints.push_back(C);
2001 }
2002 Constraints.swap(NewConstraints);
2003 PEClass2Node.clear();
2004}
2005
2006/// See if we have a node that is pointer equivalent to the one being asked
2007/// about, and if so, unite them and return the equivalent node. Otherwise,
2008/// return the original node.
2009unsigned Andersens::FindEquivalentNode(unsigned NodeIndex,
2010 unsigned NodeLabel) {
2011 if (!GraphNodes[NodeIndex].AddressTaken) {
2012 if (PEClass2Node[NodeLabel] != -1) {
2013 // We found an existing node with the same pointer label, so unify them.
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002014 // We specifically request that Union-By-Rank not be used so that
2015 // PEClass2Node[NodeLabel] U= NodeIndex and not the other way around.
2016 return UniteNodes(PEClass2Node[NodeLabel], NodeIndex, false);
Daniel Berlind81ccc22007-09-24 19:45:49 +00002017 } else {
2018 PEClass2Node[NodeLabel] = NodeIndex;
2019 PENLEClass2Node[NodeLabel] = NodeIndex;
2020 }
2021 } else if (PENLEClass2Node[NodeLabel] == -1) {
2022 PENLEClass2Node[NodeLabel] = NodeIndex;
2023 }
2024
2025 return NodeIndex;
2026}
2027
Andrew Lenharth52d34d92008-03-20 15:36:44 +00002028void Andersens::PrintLabels() const {
Daniel Berlind81ccc22007-09-24 19:45:49 +00002029 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2030 if (i < FirstRefNode) {
2031 PrintNode(&GraphNodes[i]);
2032 } else if (i < FirstAdrNode) {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002033 DEBUG(errs() << "REF(");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002034 PrintNode(&GraphNodes[i-FirstRefNode]);
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002035 DEBUG(errs() <<")");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002036 } else {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002037 DEBUG(errs() << "ADR(");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002038 PrintNode(&GraphNodes[i-FirstAdrNode]);
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002039 DEBUG(errs() <<")");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002040 }
2041
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002042 DEBUG(errs() << " has pointer label " << GraphNodes[i].PointerEquivLabel
Daniel Berlind81ccc22007-09-24 19:45:49 +00002043 << " and SCC rep " << VSSCCRep[i]
2044 << " and is " << (GraphNodes[i].Direct ? "Direct" : "Not direct")
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002045 << "\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002046 }
2047}
2048
Daniel Berlinc864edb2008-03-05 19:31:47 +00002049/// The technique used here is described in "The Ant and the
2050/// Grasshopper: Fast and Accurate Pointer Analysis for Millions of
2051/// Lines of Code. In Programming Language Design and Implementation
2052/// (PLDI), June 2007." It is known as the "HCD" (Hybrid Cycle
2053/// Detection) algorithm. It is called a hybrid because it performs an
2054/// offline analysis and uses its results during the solving (online)
2055/// phase. This is just the offline portion; the results of this
2056/// operation are stored in SDT and are later used in SolveContraints()
2057/// and UniteNodes().
2058void Andersens::HCD() {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002059 DEBUG(errs() << "Starting HCD.\n");
Daniel Berlinc864edb2008-03-05 19:31:47 +00002060 HCDSCCRep.resize(GraphNodes.size());
2061
2062 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2063 GraphNodes[i].Edges = new SparseBitVector<>;
2064 HCDSCCRep[i] = i;
2065 }
2066
2067 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
2068 Constraint &C = Constraints[i];
2069 assert (C.Src < GraphNodes.size() && C.Dest < GraphNodes.size());
2070 if (C.Type == Constraint::AddressOf) {
2071 continue;
2072 } else if (C.Type == Constraint::Load) {
2073 if( C.Offset == 0 )
2074 GraphNodes[C.Dest].Edges->set(C.Src + FirstRefNode);
2075 } else if (C.Type == Constraint::Store) {
2076 if( C.Offset == 0 )
2077 GraphNodes[C.Dest + FirstRefNode].Edges->set(C.Src);
2078 } else {
2079 GraphNodes[C.Dest].Edges->set(C.Src);
2080 }
2081 }
2082
2083 Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
2084 Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
2085 Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
2086 SDT.insert(SDT.begin(), GraphNodes.size() / 2, -1);
2087
2088 DFSNumber = 0;
2089 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2090 unsigned Node = HCDSCCRep[i];
2091 if (!Node2Deleted[Node])
2092 Search(Node);
2093 }
2094
2095 for (unsigned i = 0; i < GraphNodes.size(); ++i)
2096 if (GraphNodes[i].Edges != NULL) {
2097 delete GraphNodes[i].Edges;
2098 GraphNodes[i].Edges = NULL;
2099 }
2100
2101 while( !SCCStack.empty() )
2102 SCCStack.pop();
2103
2104 Node2DFS.clear();
2105 Node2Visited.clear();
2106 Node2Deleted.clear();
2107 HCDSCCRep.clear();
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002108 DEBUG(errs() << "HCD complete.\n");
Daniel Berlinc864edb2008-03-05 19:31:47 +00002109}
2110
2111// Component of HCD:
2112// Use Nuutila's variant of Tarjan's algorithm to detect
2113// Strongly-Connected Components (SCCs). For non-trivial SCCs
2114// containing ref nodes, insert the appropriate information in SDT.
2115void Andersens::Search(unsigned Node) {
2116 unsigned MyDFS = DFSNumber++;
2117
2118 Node2Visited[Node] = true;
2119 Node2DFS[Node] = MyDFS;
2120
2121 for (SparseBitVector<>::iterator Iter = GraphNodes[Node].Edges->begin(),
2122 End = GraphNodes[Node].Edges->end();
2123 Iter != End;
2124 ++Iter) {
2125 unsigned J = HCDSCCRep[*Iter];
2126 assert(GraphNodes[J].isRep() && "Debug check; must be representative");
2127 if (!Node2Deleted[J]) {
2128 if (!Node2Visited[J])
2129 Search(J);
2130 if (Node2DFS[Node] > Node2DFS[J])
2131 Node2DFS[Node] = Node2DFS[J];
2132 }
2133 }
2134
2135 if( MyDFS != Node2DFS[Node] ) {
2136 SCCStack.push(Node);
2137 return;
2138 }
2139
2140 // This node is the root of a SCC, so process it.
2141 //
2142 // If the SCC is "non-trivial" (not a singleton) and contains a reference
2143 // node, we place this SCC into SDT. We unite the nodes in any case.
2144 if (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) {
2145 SparseBitVector<> SCC;
2146
2147 SCC.set(Node);
2148
2149 bool Ref = (Node >= FirstRefNode);
2150
2151 Node2Deleted[Node] = true;
2152
2153 do {
2154 unsigned P = SCCStack.top(); SCCStack.pop();
2155 Ref |= (P >= FirstRefNode);
2156 SCC.set(P);
2157 HCDSCCRep[P] = Node;
2158 } while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS);
2159
2160 if (Ref) {
2161 unsigned Rep = SCC.find_first();
2162 assert(Rep < FirstRefNode && "The SCC didn't have a non-Ref node!");
2163
2164 SparseBitVector<>::iterator i = SCC.begin();
2165
2166 // Skip over the non-ref nodes
2167 while( *i < FirstRefNode )
2168 ++i;
2169
2170 while( i != SCC.end() )
2171 SDT[ (*i++) - FirstRefNode ] = Rep;
2172 }
2173 }
2174}
2175
2176
Daniel Berlind81ccc22007-09-24 19:45:49 +00002177/// Optimize the constraints by performing offline variable substitution and
2178/// other optimizations.
2179void Andersens::OptimizeConstraints() {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002180 DEBUG(errs() << "Beginning constraint optimization\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002181
Daniel Berlinc864edb2008-03-05 19:31:47 +00002182 SDTActive = false;
2183
Daniel Berlind81ccc22007-09-24 19:45:49 +00002184 // Function related nodes need to stay in the same relative position and can't
2185 // be location equivalent.
2186 for (std::map<unsigned, unsigned>::iterator Iter = MaxK.begin();
2187 Iter != MaxK.end();
2188 ++Iter) {
2189 for (unsigned i = Iter->first;
2190 i != Iter->first + Iter->second;
2191 ++i) {
2192 GraphNodes[i].AddressTaken = true;
2193 GraphNodes[i].Direct = false;
2194 }
2195 }
2196
2197 ClumpAddressTaken();
2198 FirstRefNode = GraphNodes.size();
2199 FirstAdrNode = FirstRefNode + GraphNodes.size();
2200 GraphNodes.insert(GraphNodes.end(), 2 * GraphNodes.size(),
2201 Node(false));
2202 VSSCCRep.resize(GraphNodes.size());
2203 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2204 VSSCCRep[i] = i;
2205 }
2206 HVN();
2207 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2208 Node *N = &GraphNodes[i];
2209 delete N->PredEdges;
2210 N->PredEdges = NULL;
2211 delete N->ImplicitPredEdges;
2212 N->ImplicitPredEdges = NULL;
2213 }
2214#undef DEBUG_TYPE
2215#define DEBUG_TYPE "anders-aa-labels"
2216 DEBUG(PrintLabels());
2217#undef DEBUG_TYPE
2218#define DEBUG_TYPE "anders-aa"
2219 RewriteConstraints();
2220 // Delete the adr nodes.
2221 GraphNodes.resize(FirstRefNode * 2);
2222
2223 // Now perform HU
2224 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2225 Node *N = &GraphNodes[i];
2226 if (FindNode(i) == i) {
2227 N->PointsTo = new SparseBitVector<>;
2228 N->PointedToBy = new SparseBitVector<>;
2229 // Reset our labels
2230 }
2231 VSSCCRep[i] = i;
2232 N->PointerEquivLabel = 0;
2233 }
2234 HU();
2235#undef DEBUG_TYPE
2236#define DEBUG_TYPE "anders-aa-labels"
2237 DEBUG(PrintLabels());
2238#undef DEBUG_TYPE
2239#define DEBUG_TYPE "anders-aa"
2240 RewriteConstraints();
2241 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2242 if (FindNode(i) == i) {
2243 Node *N = &GraphNodes[i];
2244 delete N->PointsTo;
Daniel Berlinc864edb2008-03-05 19:31:47 +00002245 N->PointsTo = NULL;
Daniel Berlind81ccc22007-09-24 19:45:49 +00002246 delete N->PredEdges;
Daniel Berlinc864edb2008-03-05 19:31:47 +00002247 N->PredEdges = NULL;
Daniel Berlind81ccc22007-09-24 19:45:49 +00002248 delete N->ImplicitPredEdges;
Daniel Berlinc864edb2008-03-05 19:31:47 +00002249 N->ImplicitPredEdges = NULL;
Daniel Berlind81ccc22007-09-24 19:45:49 +00002250 delete N->PointedToBy;
Daniel Berlinc864edb2008-03-05 19:31:47 +00002251 N->PointedToBy = NULL;
Daniel Berlind81ccc22007-09-24 19:45:49 +00002252 }
2253 }
Daniel Berlinc864edb2008-03-05 19:31:47 +00002254
2255 // perform Hybrid Cycle Detection (HCD)
2256 HCD();
2257 SDTActive = true;
2258
2259 // No longer any need for the upper half of GraphNodes (for ref nodes).
Daniel Berlind81ccc22007-09-24 19:45:49 +00002260 GraphNodes.erase(GraphNodes.begin() + FirstRefNode, GraphNodes.end());
Daniel Berlinc864edb2008-03-05 19:31:47 +00002261
2262 // HCD complete.
2263
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002264 DEBUG(errs() << "Finished constraint optimization\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002265 FirstRefNode = 0;
2266 FirstAdrNode = 0;
2267}
2268
2269/// Unite pointer but not location equivalent variables, now that the constraint
2270/// graph is built.
2271void Andersens::UnitePointerEquivalences() {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002272 DEBUG(errs() << "Uniting remaining pointer equivalences\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002273 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002274 if (GraphNodes[i].AddressTaken && GraphNodes[i].isRep()) {
Daniel Berlind81ccc22007-09-24 19:45:49 +00002275 unsigned Label = GraphNodes[i].PointerEquivLabel;
2276
2277 if (Label && PENLEClass2Node[Label] != -1)
2278 UniteNodes(i, PENLEClass2Node[Label]);
2279 }
2280 }
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002281 DEBUG(errs() << "Finished remaining pointer equivalences\n");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002282 PENLEClass2Node.clear();
2283}
2284
2285/// Create the constraint graph used for solving points-to analysis.
2286///
Daniel Berlinaad15882007-09-16 21:45:02 +00002287void Andersens::CreateConstraintGraph() {
2288 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
2289 Constraint &C = Constraints[i];
2290 assert (C.Src < GraphNodes.size() && C.Dest < GraphNodes.size());
2291 if (C.Type == Constraint::AddressOf)
2292 GraphNodes[C.Dest].PointsTo->set(C.Src);
2293 else if (C.Type == Constraint::Load)
2294 GraphNodes[C.Src].Constraints.push_back(C);
2295 else if (C.Type == Constraint::Store)
2296 GraphNodes[C.Dest].Constraints.push_back(C);
2297 else if (C.Offset != 0)
2298 GraphNodes[C.Src].Constraints.push_back(C);
2299 else
2300 GraphNodes[C.Src].Edges->set(C.Dest);
2301 }
2302}
2303
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002304// Perform DFS and cycle detection.
2305bool Andersens::QueryNode(unsigned Node) {
2306 assert(GraphNodes[Node].isRep() && "Querying a non-rep node");
Daniel Berlinaad15882007-09-16 21:45:02 +00002307 unsigned OurDFS = ++DFSNumber;
2308 SparseBitVector<> ToErase;
2309 SparseBitVector<> NewEdges;
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002310 Tarjan2DFS[Node] = OurDFS;
2311
2312 // Changed denotes a change from a recursive call that we will bubble up.
2313 // Merged is set if we actually merge a node ourselves.
2314 bool Changed = false, Merged = false;
Daniel Berlinaad15882007-09-16 21:45:02 +00002315
2316 for (SparseBitVector<>::iterator bi = GraphNodes[Node].Edges->begin();
2317 bi != GraphNodes[Node].Edges->end();
2318 ++bi) {
2319 unsigned RepNode = FindNode(*bi);
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002320 // If this edge points to a non-representative node but we are
2321 // already planning to add an edge to its representative, we have no
2322 // need for this edge anymore.
Daniel Berlinaad15882007-09-16 21:45:02 +00002323 if (RepNode != *bi && NewEdges.test(RepNode)){
2324 ToErase.set(*bi);
2325 continue;
2326 }
2327
2328 // Continue about our DFS.
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002329 if (!Tarjan2Deleted[RepNode]){
2330 if (Tarjan2DFS[RepNode] == 0) {
2331 Changed |= QueryNode(RepNode);
2332 // May have been changed by QueryNode
Daniel Berlinaad15882007-09-16 21:45:02 +00002333 RepNode = FindNode(RepNode);
2334 }
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002335 if (Tarjan2DFS[RepNode] < Tarjan2DFS[Node])
2336 Tarjan2DFS[Node] = Tarjan2DFS[RepNode];
Daniel Berlinaad15882007-09-16 21:45:02 +00002337 }
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002338
2339 // We may have just discovered that this node is part of a cycle, in
2340 // which case we can also erase it.
Daniel Berlinaad15882007-09-16 21:45:02 +00002341 if (RepNode != *bi) {
2342 ToErase.set(*bi);
2343 NewEdges.set(RepNode);
Chris Lattnere995a2a2004-05-23 21:00:47 +00002344 }
2345 }
2346
Daniel Berlinaad15882007-09-16 21:45:02 +00002347 GraphNodes[Node].Edges->intersectWithComplement(ToErase);
2348 GraphNodes[Node].Edges |= NewEdges;
2349
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002350 // If this node is a root of a non-trivial SCC, place it on our
2351 // worklist to be processed.
2352 if (OurDFS == Tarjan2DFS[Node]) {
2353 while (!SCCStack.empty() && Tarjan2DFS[SCCStack.top()] >= OurDFS) {
2354 Node = UniteNodes(Node, SCCStack.top());
Daniel Berlinaad15882007-09-16 21:45:02 +00002355
2356 SCCStack.pop();
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002357 Merged = true;
Daniel Berlinaad15882007-09-16 21:45:02 +00002358 }
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002359 Tarjan2Deleted[Node] = true;
Daniel Berlinaad15882007-09-16 21:45:02 +00002360
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002361 if (Merged)
2362 NextWL->insert(&GraphNodes[Node]);
Daniel Berlinaad15882007-09-16 21:45:02 +00002363 } else {
2364 SCCStack.push(Node);
Chris Lattnere995a2a2004-05-23 21:00:47 +00002365 }
Chris Lattnere995a2a2004-05-23 21:00:47 +00002366
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002367 return(Changed | Merged);
2368}
Chris Lattnere995a2a2004-05-23 21:00:47 +00002369
2370/// SolveConstraints - This stage iteratively processes the constraints list
2371/// propagating constraints (adding edges to the Nodes in the points-to graph)
2372/// until a fixed point is reached.
2373///
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002374/// We use a variant of the technique called "Lazy Cycle Detection", which is
2375/// described in "The Ant and the Grasshopper: Fast and Accurate Pointer
2376/// Analysis for Millions of Lines of Code. In Programming Language Design and
2377/// Implementation (PLDI), June 2007."
2378/// The paper describes performing cycle detection one node at a time, which can
2379/// be expensive if there are no cycles, but there are long chains of nodes that
2380/// it heuristically believes are cycles (because it will DFS from each node
2381/// without state from previous nodes).
2382/// Instead, we use the heuristic to build a worklist of nodes to check, then
2383/// cycle detect them all at the same time to do this more cheaply. This
2384/// catches cycles slightly later than the original technique did, but does it
2385/// make significantly cheaper.
2386
Chris Lattnere995a2a2004-05-23 21:00:47 +00002387void Andersens::SolveConstraints() {
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002388 CurrWL = &w1;
2389 NextWL = &w2;
Daniel Berlinaad15882007-09-16 21:45:02 +00002390
Daniel Berlind81ccc22007-09-24 19:45:49 +00002391 OptimizeConstraints();
2392#undef DEBUG_TYPE
2393#define DEBUG_TYPE "anders-aa-constraints"
2394 DEBUG(PrintConstraints());
2395#undef DEBUG_TYPE
2396#define DEBUG_TYPE "anders-aa"
2397
Daniel Berlinaad15882007-09-16 21:45:02 +00002398 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2399 Node *N = &GraphNodes[i];
2400 N->PointsTo = new SparseBitVector<>;
2401 N->OldPointsTo = new SparseBitVector<>;
2402 N->Edges = new SparseBitVector<>;
2403 }
2404 CreateConstraintGraph();
Daniel Berlind81ccc22007-09-24 19:45:49 +00002405 UnitePointerEquivalences();
2406 assert(SCCStack.empty() && "SCC Stack should be empty by now!");
Daniel Berlind81ccc22007-09-24 19:45:49 +00002407 Node2DFS.clear();
2408 Node2Deleted.clear();
Daniel Berlinaad15882007-09-16 21:45:02 +00002409 Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
2410 Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
2411 DFSNumber = 0;
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002412 DenseSet<Constraint, ConstraintKeyInfo> Seen;
2413 DenseSet<std::pair<unsigned,unsigned>, PairKeyInfo> EdgesChecked;
2414
2415 // Order graph and add initial nodes to work list.
Daniel Berlinaad15882007-09-16 21:45:02 +00002416 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
Daniel Berlinaad15882007-09-16 21:45:02 +00002417 Node *INode = &GraphNodes[i];
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002418
2419 // Add to work list if it's a representative and can contribute to the
2420 // calculation right now.
2421 if (INode->isRep() && !INode->PointsTo->empty()
2422 && (!INode->Edges->empty() || !INode->Constraints.empty())) {
2423 INode->Stamp();
2424 CurrWL->insert(INode);
Daniel Berlinaad15882007-09-16 21:45:02 +00002425 }
2426 }
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002427 std::queue<unsigned int> TarjanWL;
Daniel Berlinc864edb2008-03-05 19:31:47 +00002428#if !FULL_UNIVERSAL
2429 // "Rep and special variables" - in order for HCD to maintain conservative
2430 // results when !FULL_UNIVERSAL, we need to treat the special variables in
2431 // the same way that the !FULL_UNIVERSAL tweak does throughout the rest of
2432 // the analysis - it's ok to add edges from the special nodes, but never
2433 // *to* the special nodes.
2434 std::vector<unsigned int> RSV;
2435#endif
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002436 while( !CurrWL->empty() ) {
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002437 DEBUG(errs() << "Starting iteration #" << ++NumIters << "\n");
Daniel Berlinaad15882007-09-16 21:45:02 +00002438
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002439 Node* CurrNode;
2440 unsigned CurrNodeIndex;
Chris Lattnere995a2a2004-05-23 21:00:47 +00002441
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002442 // Actual cycle checking code. We cycle check all of the lazy cycle
2443 // candidates from the last iteration in one go.
2444 if (!TarjanWL.empty()) {
2445 DFSNumber = 0;
2446
2447 Tarjan2DFS.clear();
2448 Tarjan2Deleted.clear();
2449 while (!TarjanWL.empty()) {
2450 unsigned int ToTarjan = TarjanWL.front();
2451 TarjanWL.pop();
2452 if (!Tarjan2Deleted[ToTarjan]
2453 && GraphNodes[ToTarjan].isRep()
2454 && Tarjan2DFS[ToTarjan] == 0)
2455 QueryNode(ToTarjan);
2456 }
2457 }
2458
2459 // Add to work list if it's a representative and can contribute to the
2460 // calculation right now.
2461 while( (CurrNode = CurrWL->pop()) != NULL ) {
2462 CurrNodeIndex = CurrNode - &GraphNodes[0];
2463 CurrNode->Stamp();
2464
2465
Daniel Berlinaad15882007-09-16 21:45:02 +00002466 // Figure out the changed points to bits
2467 SparseBitVector<> CurrPointsTo;
2468 CurrPointsTo.intersectWithComplement(CurrNode->PointsTo,
2469 CurrNode->OldPointsTo);
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002470 if (CurrPointsTo.empty())
Daniel Berlinaad15882007-09-16 21:45:02 +00002471 continue;
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002472
Daniel Berlinaad15882007-09-16 21:45:02 +00002473 *(CurrNode->OldPointsTo) |= CurrPointsTo;
Daniel Berlinc864edb2008-03-05 19:31:47 +00002474
2475 // Check the offline-computed equivalencies from HCD.
2476 bool SCC = false;
2477 unsigned Rep;
2478
2479 if (SDT[CurrNodeIndex] >= 0) {
2480 SCC = true;
2481 Rep = FindNode(SDT[CurrNodeIndex]);
2482
2483#if !FULL_UNIVERSAL
2484 RSV.clear();
2485#endif
2486 for (SparseBitVector<>::iterator bi = CurrPointsTo.begin();
2487 bi != CurrPointsTo.end(); ++bi) {
2488 unsigned Node = FindNode(*bi);
2489#if !FULL_UNIVERSAL
2490 if (Node < NumberSpecialNodes) {
2491 RSV.push_back(Node);
2492 continue;
2493 }
2494#endif
2495 Rep = UniteNodes(Rep,Node);
2496 }
2497#if !FULL_UNIVERSAL
2498 RSV.push_back(Rep);
2499#endif
2500
2501 NextWL->insert(&GraphNodes[Rep]);
2502
2503 if ( ! CurrNode->isRep() )
2504 continue;
2505 }
2506
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002507 Seen.clear();
Chris Lattnere995a2a2004-05-23 21:00:47 +00002508
Daniel Berlinaad15882007-09-16 21:45:02 +00002509 /* Now process the constraints for this node. */
2510 for (std::list<Constraint>::iterator li = CurrNode->Constraints.begin();
2511 li != CurrNode->Constraints.end(); ) {
2512 li->Src = FindNode(li->Src);
2513 li->Dest = FindNode(li->Dest);
Chris Lattnere995a2a2004-05-23 21:00:47 +00002514
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002515 // Delete redundant constraints
2516 if( Seen.count(*li) ) {
2517 std::list<Constraint>::iterator lk = li; li++;
2518
2519 CurrNode->Constraints.erase(lk);
2520 ++NumErased;
2521 continue;
2522 }
2523 Seen.insert(*li);
2524
Daniel Berlinaad15882007-09-16 21:45:02 +00002525 // Src and Dest will be the vars we are going to process.
2526 // This may look a bit ugly, but what it does is allow us to process
Daniel Berlind81ccc22007-09-24 19:45:49 +00002527 // both store and load constraints with the same code.
Daniel Berlinaad15882007-09-16 21:45:02 +00002528 // Load constraints say that every member of our RHS solution has K
2529 // added to it, and that variable gets an edge to LHS. We also union
2530 // RHS+K's solution into the LHS solution.
2531 // Store constraints say that every member of our LHS solution has K
2532 // added to it, and that variable gets an edge from RHS. We also union
2533 // RHS's solution into the LHS+K solution.
2534 unsigned *Src;
2535 unsigned *Dest;
2536 unsigned K = li->Offset;
2537 unsigned CurrMember;
2538 if (li->Type == Constraint::Load) {
2539 Src = &CurrMember;
2540 Dest = &li->Dest;
2541 } else if (li->Type == Constraint::Store) {
2542 Src = &li->Src;
2543 Dest = &CurrMember;
2544 } else {
2545 // TODO Handle offseted copy constraint
2546 li++;
2547 continue;
2548 }
Daniel Berlinc864edb2008-03-05 19:31:47 +00002549
2550 // See if we can use Hybrid Cycle Detection (that is, check
Daniel Berlinaad15882007-09-16 21:45:02 +00002551 // if it was a statically detected offline equivalence that
Daniel Berlinc864edb2008-03-05 19:31:47 +00002552 // involves pointers; if so, remove the redundant constraints).
2553 if( SCC && K == 0 ) {
2554#if FULL_UNIVERSAL
2555 CurrMember = Rep;
Chris Lattnere995a2a2004-05-23 21:00:47 +00002556
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002557 if (GraphNodes[*Src].Edges->test_and_set(*Dest))
2558 if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo))
2559 NextWL->insert(&GraphNodes[*Dest]);
Daniel Berlinc864edb2008-03-05 19:31:47 +00002560#else
2561 for (unsigned i=0; i < RSV.size(); ++i) {
2562 CurrMember = RSV[i];
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002563
Daniel Berlinc864edb2008-03-05 19:31:47 +00002564 if (*Dest < NumberSpecialNodes)
2565 continue;
2566 if (GraphNodes[*Src].Edges->test_and_set(*Dest))
2567 if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo))
2568 NextWL->insert(&GraphNodes[*Dest]);
2569 }
2570#endif
2571 // since all future elements of the points-to set will be
2572 // equivalent to the current ones, the complex constraints
2573 // become redundant.
2574 //
2575 std::list<Constraint>::iterator lk = li; li++;
2576#if !FULL_UNIVERSAL
2577 // In this case, we can still erase the constraints when the
2578 // elements of the points-to sets are referenced by *Dest,
2579 // but not when they are referenced by *Src (i.e. for a Load
2580 // constraint). This is because if another special variable is
2581 // put into the points-to set later, we still need to add the
2582 // new edge from that special variable.
2583 if( lk->Type != Constraint::Load)
2584#endif
2585 GraphNodes[CurrNodeIndex].Constraints.erase(lk);
2586 } else {
2587 const SparseBitVector<> &Solution = CurrPointsTo;
2588
2589 for (SparseBitVector<>::iterator bi = Solution.begin();
2590 bi != Solution.end();
2591 ++bi) {
2592 CurrMember = *bi;
2593
2594 // Need to increment the member by K since that is where we are
2595 // supposed to copy to/from. Note that in positive weight cycles,
2596 // which occur in address taking of fields, K can go past
2597 // MaxK[CurrMember] elements, even though that is all it could point
2598 // to.
2599 if (K > 0 && K > MaxK[CurrMember])
2600 continue;
2601 else
2602 CurrMember = FindNode(CurrMember + K);
2603
2604 // Add an edge to the graph, so we can just do regular
2605 // bitmap ior next time. It may also let us notice a cycle.
2606#if !FULL_UNIVERSAL
2607 if (*Dest < NumberSpecialNodes)
2608 continue;
2609#endif
2610 if (GraphNodes[*Src].Edges->test_and_set(*Dest))
2611 if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo))
2612 NextWL->insert(&GraphNodes[*Dest]);
2613
2614 }
2615 li++;
Daniel Berlinaad15882007-09-16 21:45:02 +00002616 }
Daniel Berlinaad15882007-09-16 21:45:02 +00002617 }
2618 SparseBitVector<> NewEdges;
2619 SparseBitVector<> ToErase;
2620
2621 // Now all we have left to do is propagate points-to info along the
2622 // edges, erasing the redundant edges.
Daniel Berlinaad15882007-09-16 21:45:02 +00002623 for (SparseBitVector<>::iterator bi = CurrNode->Edges->begin();
2624 bi != CurrNode->Edges->end();
2625 ++bi) {
2626
2627 unsigned DestVar = *bi;
2628 unsigned Rep = FindNode(DestVar);
2629
Bill Wendlingf059deb2008-02-26 10:51:52 +00002630 // If we ended up with this node as our destination, or we've already
2631 // got an edge for the representative, delete the current edge.
2632 if (Rep == CurrNodeIndex ||
2633 (Rep != DestVar && NewEdges.test(Rep))) {
Daniel Berlinc864edb2008-03-05 19:31:47 +00002634 ToErase.set(DestVar);
2635 continue;
Bill Wendlingf059deb2008-02-26 10:51:52 +00002636 }
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002637
Bill Wendlingf059deb2008-02-26 10:51:52 +00002638 std::pair<unsigned,unsigned> edge(CurrNodeIndex,Rep);
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002639
2640 // This is where we do lazy cycle detection.
2641 // If this is a cycle candidate (equal points-to sets and this
2642 // particular edge has not been cycle-checked previously), add to the
2643 // list to check for cycles on the next iteration.
2644 if (!EdgesChecked.count(edge) &&
2645 *(GraphNodes[Rep].PointsTo) == *(CurrNode->PointsTo)) {
2646 EdgesChecked.insert(edge);
2647 TarjanWL.push(Rep);
Daniel Berlinaad15882007-09-16 21:45:02 +00002648 }
2649 // Union the points-to sets into the dest
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002650#if !FULL_UNIVERSAL
2651 if (Rep >= NumberSpecialNodes)
2652#endif
Daniel Berlinaad15882007-09-16 21:45:02 +00002653 if (GraphNodes[Rep].PointsTo |= CurrPointsTo) {
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002654 NextWL->insert(&GraphNodes[Rep]);
Daniel Berlinaad15882007-09-16 21:45:02 +00002655 }
2656 // If this edge's destination was collapsed, rewrite the edge.
2657 if (Rep != DestVar) {
2658 ToErase.set(DestVar);
2659 NewEdges.set(Rep);
2660 }
2661 }
2662 CurrNode->Edges->intersectWithComplement(ToErase);
2663 CurrNode->Edges |= NewEdges;
2664 }
Daniel Berlinaad15882007-09-16 21:45:02 +00002665
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002666 // Switch to other work list.
2667 WorkList* t = CurrWL; CurrWL = NextWL; NextWL = t;
2668 }
Daniel Berlinaad15882007-09-16 21:45:02 +00002669
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002670
Daniel Berlinaad15882007-09-16 21:45:02 +00002671 Node2DFS.clear();
2672 Node2Deleted.clear();
2673 for (unsigned i = 0; i < GraphNodes.size(); ++i) {
2674 Node *N = &GraphNodes[i];
2675 delete N->OldPointsTo;
2676 delete N->Edges;
Chris Lattnere995a2a2004-05-23 21:00:47 +00002677 }
Daniel Berlinc864edb2008-03-05 19:31:47 +00002678 SDTActive = false;
2679 SDT.clear();
Chris Lattnere995a2a2004-05-23 21:00:47 +00002680}
2681
Daniel Berlinaad15882007-09-16 21:45:02 +00002682//===----------------------------------------------------------------------===//
2683// Union-Find
2684//===----------------------------------------------------------------------===//
Chris Lattnere995a2a2004-05-23 21:00:47 +00002685
Daniel Berlinaad15882007-09-16 21:45:02 +00002686// Unite nodes First and Second, returning the one which is now the
2687// representative node. First and Second are indexes into GraphNodes
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002688unsigned Andersens::UniteNodes(unsigned First, unsigned Second,
2689 bool UnionByRank) {
Daniel Berlinaad15882007-09-16 21:45:02 +00002690 assert (First < GraphNodes.size() && Second < GraphNodes.size() &&
2691 "Attempting to merge nodes that don't exist");
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002692
Daniel Berlinaad15882007-09-16 21:45:02 +00002693 Node *FirstNode = &GraphNodes[First];
2694 Node *SecondNode = &GraphNodes[Second];
2695
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002696 assert (SecondNode->isRep() && FirstNode->isRep() &&
Daniel Berlinaad15882007-09-16 21:45:02 +00002697 "Trying to unite two non-representative nodes!");
2698 if (First == Second)
2699 return First;
2700
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002701 if (UnionByRank) {
2702 int RankFirst = (int) FirstNode ->NodeRep;
2703 int RankSecond = (int) SecondNode->NodeRep;
2704
2705 // Rank starts at -1 and gets decremented as it increases.
2706 // Translation: higher rank, lower NodeRep value, which is always negative.
2707 if (RankFirst > RankSecond) {
2708 unsigned t = First; First = Second; Second = t;
2709 Node* tp = FirstNode; FirstNode = SecondNode; SecondNode = tp;
2710 } else if (RankFirst == RankSecond) {
2711 FirstNode->NodeRep = (unsigned) (RankFirst - 1);
2712 }
2713 }
2714
Daniel Berlinaad15882007-09-16 21:45:02 +00002715 SecondNode->NodeRep = First;
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002716#if !FULL_UNIVERSAL
2717 if (First >= NumberSpecialNodes)
2718#endif
Daniel Berlind81ccc22007-09-24 19:45:49 +00002719 if (FirstNode->PointsTo && SecondNode->PointsTo)
2720 FirstNode->PointsTo |= *(SecondNode->PointsTo);
2721 if (FirstNode->Edges && SecondNode->Edges)
2722 FirstNode->Edges |= *(SecondNode->Edges);
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002723 if (!SecondNode->Constraints.empty())
Daniel Berlind81ccc22007-09-24 19:45:49 +00002724 FirstNode->Constraints.splice(FirstNode->Constraints.begin(),
2725 SecondNode->Constraints);
2726 if (FirstNode->OldPointsTo) {
2727 delete FirstNode->OldPointsTo;
2728 FirstNode->OldPointsTo = new SparseBitVector<>;
2729 }
Daniel Berlinaad15882007-09-16 21:45:02 +00002730
2731 // Destroy interesting parts of the merged-from node.
2732 delete SecondNode->OldPointsTo;
2733 delete SecondNode->Edges;
2734 delete SecondNode->PointsTo;
2735 SecondNode->Edges = NULL;
2736 SecondNode->PointsTo = NULL;
2737 SecondNode->OldPointsTo = NULL;
2738
2739 NumUnified++;
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002740 DEBUG(errs() << "Unified Node ");
Daniel Berlinaad15882007-09-16 21:45:02 +00002741 DEBUG(PrintNode(FirstNode));
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002742 DEBUG(errs() << " and Node ");
Daniel Berlinaad15882007-09-16 21:45:02 +00002743 DEBUG(PrintNode(SecondNode));
Chris Lattnerbbbfa992009-08-23 06:35:02 +00002744 DEBUG(errs() << "\n");
Daniel Berlinaad15882007-09-16 21:45:02 +00002745
Daniel Berlinc864edb2008-03-05 19:31:47 +00002746 if (SDTActive)
Duncan Sands43e2a032008-05-27 11:50:51 +00002747 if (SDT[Second] >= 0) {
Daniel Berlinc864edb2008-03-05 19:31:47 +00002748 if (SDT[First] < 0)
2749 SDT[First] = SDT[Second];
2750 else {
2751 UniteNodes( FindNode(SDT[First]), FindNode(SDT[Second]) );
2752 First = FindNode(First);
2753 }
Duncan Sands43e2a032008-05-27 11:50:51 +00002754 }
Daniel Berlinc864edb2008-03-05 19:31:47 +00002755
Daniel Berlinaad15882007-09-16 21:45:02 +00002756 return First;
2757}
2758
2759// Find the index into GraphNodes of the node representing Node, performing
2760// path compression along the way
2761unsigned Andersens::FindNode(unsigned NodeIndex) {
2762 assert (NodeIndex < GraphNodes.size()
2763 && "Attempting to find a node that can't exist");
2764 Node *N = &GraphNodes[NodeIndex];
Daniel Berlin3a3f1632007-12-12 00:37:04 +00002765 if (N->isRep())
Daniel Berlinaad15882007-09-16 21:45:02 +00002766 return NodeIndex;
2767 else
2768 return (N->NodeRep = FindNode(N->NodeRep));
2769}
Chris Lattnere995a2a2004-05-23 21:00:47 +00002770
Andrew Lenharth52d34d92008-03-20 15:36:44 +00002771// Find the index into GraphNodes of the node representing Node,
2772// don't perform path compression along the way (for Print)
2773unsigned Andersens::FindNode(unsigned NodeIndex) const {
2774 assert (NodeIndex < GraphNodes.size()
2775 && "Attempting to find a node that can't exist");
2776 const Node *N = &GraphNodes[NodeIndex];
2777 if (N->isRep())
2778 return NodeIndex;
2779 else
2780 return FindNode(N->NodeRep);
2781}
2782
Chris Lattnere995a2a2004-05-23 21:00:47 +00002783//===----------------------------------------------------------------------===//
2784// Debugging Output
2785//===----------------------------------------------------------------------===//
2786
Andrew Lenharth52d34d92008-03-20 15:36:44 +00002787void Andersens::PrintNode(const Node *N) const {
Chris Lattnere995a2a2004-05-23 21:00:47 +00002788 if (N == &GraphNodes[UniversalSet]) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002789 errs() << "<universal>";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002790 return;
2791 } else if (N == &GraphNodes[NullPtr]) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002792 errs() << "<nullptr>";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002793 return;
2794 } else if (N == &GraphNodes[NullObject]) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002795 errs() << "<null>";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002796 return;
2797 }
Daniel Berlinaad15882007-09-16 21:45:02 +00002798 if (!N->getValue()) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002799 errs() << "artificial" << (intptr_t) N;
Daniel Berlinaad15882007-09-16 21:45:02 +00002800 return;
2801 }
Chris Lattnere995a2a2004-05-23 21:00:47 +00002802
2803 assert(N->getValue() != 0 && "Never set node label!");
2804 Value *V = N->getValue();
2805 if (Function *F = dyn_cast<Function>(V)) {
2806 if (isa<PointerType>(F->getFunctionType()->getReturnType()) &&
Daniel Berlinaad15882007-09-16 21:45:02 +00002807 N == &GraphNodes[getReturnNode(F)]) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002808 errs() << F->getName() << ":retval";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002809 return;
Daniel Berlinaad15882007-09-16 21:45:02 +00002810 } else if (F->getFunctionType()->isVarArg() &&
2811 N == &GraphNodes[getVarargNode(F)]) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002812 errs() << F->getName() << ":vararg";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002813 return;
2814 }
2815 }
2816
2817 if (Instruction *I = dyn_cast<Instruction>(V))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002818 errs() << I->getParent()->getParent()->getName() << ":";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002819 else if (Argument *Arg = dyn_cast<Argument>(V))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002820 errs() << Arg->getParent()->getName() << ":";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002821
2822 if (V->hasName())
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002823 errs() << V->getName();
Chris Lattnere995a2a2004-05-23 21:00:47 +00002824 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002825 errs() << "(unnamed)";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002826
Victor Hernandez7b929da2009-10-23 21:09:37 +00002827 if (isa<GlobalValue>(V) || isa<AllocaInst>(V) || isMalloc(V))
Daniel Berlinaad15882007-09-16 21:45:02 +00002828 if (N == &GraphNodes[getObject(V)])
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002829 errs() << "<mem>";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002830}
Andrew Lenharth52d34d92008-03-20 15:36:44 +00002831void Andersens::PrintConstraint(const Constraint &C) const {
Daniel Berlind81ccc22007-09-24 19:45:49 +00002832 if (C.Type == Constraint::Store) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002833 errs() << "*";
Daniel Berlind81ccc22007-09-24 19:45:49 +00002834 if (C.Offset != 0)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002835 errs() << "(";
Daniel Berlind81ccc22007-09-24 19:45:49 +00002836 }
2837 PrintNode(&GraphNodes[C.Dest]);
2838 if (C.Type == Constraint::Store && C.Offset != 0)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002839 errs() << " + " << C.Offset << ")";
2840 errs() << " = ";
Daniel Berlind81ccc22007-09-24 19:45:49 +00002841 if (C.Type == Constraint::Load) {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002842 errs() << "*";
Daniel Berlind81ccc22007-09-24 19:45:49 +00002843 if (C.Offset != 0)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002844 errs() << "(";
Daniel Berlind81ccc22007-09-24 19:45:49 +00002845 }
2846 else if (C.Type == Constraint::AddressOf)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002847 errs() << "&";
Daniel Berlind81ccc22007-09-24 19:45:49 +00002848 PrintNode(&GraphNodes[C.Src]);
2849 if (C.Offset != 0 && C.Type != Constraint::Store)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002850 errs() << " + " << C.Offset;
Daniel Berlind81ccc22007-09-24 19:45:49 +00002851 if (C.Type == Constraint::Load && C.Offset != 0)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002852 errs() << ")";
2853 errs() << "\n";
Daniel Berlind81ccc22007-09-24 19:45:49 +00002854}
Chris Lattnere995a2a2004-05-23 21:00:47 +00002855
Andrew Lenharth52d34d92008-03-20 15:36:44 +00002856void Andersens::PrintConstraints() const {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002857 errs() << "Constraints:\n";
Daniel Berlinaad15882007-09-16 21:45:02 +00002858
Daniel Berlind81ccc22007-09-24 19:45:49 +00002859 for (unsigned i = 0, e = Constraints.size(); i != e; ++i)
2860 PrintConstraint(Constraints[i]);
Chris Lattnere995a2a2004-05-23 21:00:47 +00002861}
2862
Andrew Lenharth52d34d92008-03-20 15:36:44 +00002863void Andersens::PrintPointsToGraph() const {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002864 errs() << "Points-to graph:\n";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002865 for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) {
Andrew Lenharth52d34d92008-03-20 15:36:44 +00002866 const Node *N = &GraphNodes[i];
2867 if (FindNode(i) != i) {
Daniel Berlinaad15882007-09-16 21:45:02 +00002868 PrintNode(N);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002869 errs() << "\t--> same as ";
Daniel Berlinaad15882007-09-16 21:45:02 +00002870 PrintNode(&GraphNodes[FindNode(i)]);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002871 errs() << "\n";
Daniel Berlinaad15882007-09-16 21:45:02 +00002872 } else {
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002873 errs() << "[" << (N->PointsTo->count()) << "] ";
Daniel Berlinaad15882007-09-16 21:45:02 +00002874 PrintNode(N);
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002875 errs() << "\t--> ";
Daniel Berlinaad15882007-09-16 21:45:02 +00002876
2877 bool first = true;
2878 for (SparseBitVector<>::iterator bi = N->PointsTo->begin();
2879 bi != N->PointsTo->end();
2880 ++bi) {
2881 if (!first)
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002882 errs() << ", ";
Daniel Berlinaad15882007-09-16 21:45:02 +00002883 PrintNode(&GraphNodes[*bi]);
2884 first = false;
2885 }
Daniel Dunbar3f0e8302009-07-24 09:53:24 +00002886 errs() << "\n";
Chris Lattnere995a2a2004-05-23 21:00:47 +00002887 }
Chris Lattnere995a2a2004-05-23 21:00:47 +00002888 }
2889}