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