Make anders-aa much more precise by not being completely pessimistic about
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a very simple implementation of Andersen's interprocedural
11 // alias analysis.  This implementation does not include any of the fancy
12 // features that make Andersen's reasonably efficient (like cycle elimination or
13 // variable substitution), but it should be useful for getting precision
14 // numbers and can be extended in the future.
15 //
16 // In pointer analysis terms, this is a subset-based, flow-insensitive,
17 // field-insensitive, and context-insensitive algorithm pointer algorithm.
18 //
19 // This algorithm is implemented as three stages:
20 //   1. Object identification.
21 //   2. Inclusion constraint identification.
22 //   3. Inclusion constraint solving.
23 //
24 // The object identification stage identifies all of the memory objects in the
25 // program, which includes globals, heap allocated objects, and stack allocated
26 // objects.
27 //
28 // The inclusion constraint identification stage finds all inclusion constraints
29 // in the program by scanning the program, looking for pointer assignments and
30 // other statements that effect the points-to graph.  For a statement like "A =
31 // B", this statement is processed to indicate that A can point to anything that
32 // B can point to.  Constraints can handle copies, loads, and stores.
33 //
34 // The inclusion constraint solving phase iteratively propagates the inclusion
35 // constraints until a fixed point is reached.  This is an O(N^3) algorithm.
36 //
37 // In the initial pass, all indirect function calls are completely ignored.  As
38 // the analysis discovers new targets of function pointers, it iteratively
39 // resolves a precise (and conservative) call graph.  Also related, this
40 // analysis initially assumes that all internal functions have known incoming
41 // pointers.  If we find that an internal function's address escapes outside of
42 // the program, we update this assumption.
43 //
44 // Future Improvements:
45 //   This implementation of Andersen's algorithm is extremely slow.  To make it
46 //   scale reasonably well, the inclusion constraints could be sorted (easy), 
47 //   offline variable substitution would be a huge win (straight-forward), and 
48 //   online cycle elimination (trickier) might help as well.
49 //
50 //===----------------------------------------------------------------------===//
51
52 #define DEBUG_TYPE "anders-aa"
53 #include "llvm/Constants.h"
54 #include "llvm/DerivedTypes.h"
55 #include "llvm/Instructions.h"
56 #include "llvm/Module.h"
57 #include "llvm/Pass.h"
58 #include "llvm/Support/InstIterator.h"
59 #include "llvm/Support/InstVisitor.h"
60 #include "llvm/Analysis/AliasAnalysis.h"
61 #include "llvm/Analysis/Passes.h"
62 #include "llvm/Support/Debug.h"
63 #include "llvm/ADT/Statistic.h"
64 #include <set>
65 using namespace llvm;
66
67 namespace {
68   Statistic<>
69   NumIters("anders-aa", "Number of iterations to reach convergence");
70   Statistic<>
71   NumConstraints("anders-aa", "Number of constraints");
72   Statistic<>
73   NumNodes("anders-aa", "Number of nodes");
74   Statistic<>
75   NumEscapingFunctions("anders-aa", "Number of internal functions that escape");
76   Statistic<>
77   NumIndirectCallees("anders-aa", "Number of indirect callees found");
78
79   class Andersens : public ModulePass, public AliasAnalysis,
80                     private InstVisitor<Andersens> {
81     /// Node class - This class is used to represent a memory object in the
82     /// program, and is the primitive used to build the points-to graph.
83     class Node {
84       std::vector<Node*> Pointees;
85       Value *Val;
86     public:
87       Node() : Val(0) {}
88       Node *setValue(Value *V) {
89         assert(Val == 0 && "Value already set for this node!");
90         Val = V;
91         return this;
92       }
93
94       /// getValue - Return the LLVM value corresponding to this node.
95       ///
96       Value *getValue() const { return Val; }
97
98       typedef std::vector<Node*>::const_iterator iterator;
99       iterator begin() const { return Pointees.begin(); }
100       iterator end() const { return Pointees.end(); }
101
102       /// addPointerTo - Add a pointer to the list of pointees of this node,
103       /// returning true if this caused a new pointer to be added, or false if
104       /// we already knew about the points-to relation.
105       bool addPointerTo(Node *N) {
106         std::vector<Node*>::iterator I = std::lower_bound(Pointees.begin(),
107                                                           Pointees.end(),
108                                                           N);
109         if (I != Pointees.end() && *I == N)
110           return false;
111         Pointees.insert(I, N);
112         return true;
113       }
114
115       /// intersects - Return true if the points-to set of this node intersects
116       /// with the points-to set of the specified node.
117       bool intersects(Node *N) const;
118
119       /// intersectsIgnoring - Return true if the points-to set of this node
120       /// intersects with the points-to set of the specified node on any nodes
121       /// except for the specified node to ignore.
122       bool intersectsIgnoring(Node *N, Node *Ignoring) const;
123
124       // Constraint application methods.
125       bool copyFrom(Node *N);
126       bool loadFrom(Node *N);
127       bool storeThrough(Node *N);
128     };
129
130     /// GraphNodes - This vector is populated as part of the object
131     /// identification stage of the analysis, which populates this vector with a
132     /// node for each memory object and fills in the ValueNodes map.
133     std::vector<Node> GraphNodes;
134
135     /// ValueNodes - This map indicates the Node that a particular Value* is
136     /// represented by.  This contains entries for all pointers.
137     std::map<Value*, unsigned> ValueNodes;
138
139     /// ObjectNodes - This map contains entries for each memory object in the
140     /// program: globals, alloca's and mallocs.  
141     std::map<Value*, unsigned> ObjectNodes;
142
143     /// ReturnNodes - This map contains an entry for each function in the
144     /// program that returns a value.
145     std::map<Function*, unsigned> ReturnNodes;
146
147     /// VarargNodes - This map contains the entry used to represent all pointers
148     /// passed through the varargs portion of a function call for a particular
149     /// function.  An entry is not present in this map for functions that do not
150     /// take variable arguments.
151     std::map<Function*, unsigned> VarargNodes;
152
153     /// Constraint - Objects of this structure are used to represent the various
154     /// constraints identified by the algorithm.  The constraints are 'copy',
155     /// for statements like "A = B", 'load' for statements like "A = *B", and
156     /// 'store' for statements like "*A = B".
157     struct Constraint {
158       enum ConstraintType { Copy, Load, Store } Type;
159       Node *Dest, *Src;
160
161       Constraint(ConstraintType Ty, Node *D, Node *S)
162         : Type(Ty), Dest(D), Src(S) {}
163     };
164     
165     /// Constraints - This vector contains a list of all of the constraints
166     /// identified by the program.
167     std::vector<Constraint> Constraints;
168
169     /// EscapingInternalFunctions - This set contains all of the internal
170     /// functions that are found to escape from the program.  If the address of
171     /// an internal function is passed to an external function or otherwise
172     /// escapes from the analyzed portion of the program, we must assume that
173     /// any pointer arguments can alias the universal node.  This set keeps
174     /// track of those functions we are assuming to escape so far.
175     std::set<Function*> EscapingInternalFunctions;
176
177     /// IndirectCalls - This contains a list of all of the indirect call sites
178     /// in the program.  Since the call graph is iteratively discovered, we may
179     /// need to add constraints to our graph as we find new targets of function
180     /// pointers.
181     std::vector<CallSite> IndirectCalls;
182
183     /// IndirectCallees - For each call site in the indirect calls list, keep
184     /// track of the callees that we have discovered so far.  As the analysis
185     /// proceeds, more callees are discovered, until the call graph finally
186     /// stabilizes.
187     std::map<CallSite, std::vector<Function*> > IndirectCallees;
188
189     /// This enum defines the GraphNodes indices that correspond to important
190     /// fixed sets.
191     enum {
192       UniversalSet = 0,
193       NullPtr      = 1,
194       NullObject   = 2,
195     };
196     
197   public:
198     bool runOnModule(Module &M) {
199       InitializeAliasAnalysis(this);
200       IdentifyObjects(M);
201       CollectConstraints(M);
202       DEBUG(PrintConstraints());
203       SolveConstraints();
204       DEBUG(PrintPointsToGraph());
205
206       // Free the constraints list, as we don't need it to respond to alias
207       // requests.
208       ObjectNodes.clear();
209       ReturnNodes.clear();
210       VarargNodes.clear();
211       EscapingInternalFunctions.clear();
212       std::vector<Constraint>().swap(Constraints);      
213       return false;
214     }
215
216     void releaseMemory() {
217       // FIXME: Until we have transitively required passes working correctly,
218       // this cannot be enabled!  Otherwise, using -count-aa with the pass
219       // causes memory to be freed too early. :(
220 #if 0
221       // The memory objects and ValueNodes data structures at the only ones that
222       // are still live after construction.
223       std::vector<Node>().swap(GraphNodes);
224       ValueNodes.clear();
225 #endif
226     }
227
228     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
229       AliasAnalysis::getAnalysisUsage(AU);
230       AU.setPreservesAll();                         // Does not transform code
231     }
232
233     //------------------------------------------------
234     // Implement the AliasAnalysis API
235     //  
236     AliasResult alias(const Value *V1, unsigned V1Size,
237                       const Value *V2, unsigned V2Size);
238     void getMustAliases(Value *P, std::vector<Value*> &RetVals);
239     bool pointsToConstantMemory(const Value *P);
240
241     virtual void deleteValue(Value *V) {
242       ValueNodes.erase(V);
243       getAnalysis<AliasAnalysis>().deleteValue(V);
244     }
245
246     virtual void copyValue(Value *From, Value *To) {
247       ValueNodes[To] = ValueNodes[From];
248       getAnalysis<AliasAnalysis>().copyValue(From, To);
249     }
250
251   private:
252     /// getNode - Return the node corresponding to the specified pointer scalar.
253     ///
254     Node *getNode(Value *V) {
255       if (Constant *C = dyn_cast<Constant>(V))
256         if (!isa<GlobalValue>(C))
257           return getNodeForConstantPointer(C);
258
259       std::map<Value*, unsigned>::iterator I = ValueNodes.find(V);
260       if (I == ValueNodes.end()) {
261         V->dump();
262         assert(I != ValueNodes.end() &&
263                "Value does not have a node in the points-to graph!");
264       }
265       return &GraphNodes[I->second];
266     }
267     
268     /// getObject - Return the node corresponding to the memory object for the
269     /// specified global or allocation instruction.
270     Node *getObject(Value *V) {
271       std::map<Value*, unsigned>::iterator I = ObjectNodes.find(V);
272       assert(I != ObjectNodes.end() &&
273              "Value does not have an object in the points-to graph!");
274       return &GraphNodes[I->second];
275     }
276
277     /// getReturnNode - Return the node representing the return value for the
278     /// specified function.
279     Node *getReturnNode(Function *F) {
280       std::map<Function*, unsigned>::iterator I = ReturnNodes.find(F);
281       assert(I != ReturnNodes.end() && "Function does not return a value!");
282       return &GraphNodes[I->second];
283     }
284
285     /// getVarargNode - Return the node representing the variable arguments
286     /// formal for the specified function.
287     Node *getVarargNode(Function *F) {
288       std::map<Function*, unsigned>::iterator I = VarargNodes.find(F);
289       assert(I != VarargNodes.end() && "Function does not take var args!");
290       return &GraphNodes[I->second];
291     }
292
293     /// getNodeValue - Get the node for the specified LLVM value and set the
294     /// value for it to be the specified value.
295     Node *getNodeValue(Value &V) {
296       return getNode(&V)->setValue(&V);
297     }
298
299     void IdentifyObjects(Module &M);
300     void CollectConstraints(Module &M);
301     void SolveConstraints();
302
303     Node *getNodeForConstantPointer(Constant *C);
304     Node *getNodeForConstantPointerTarget(Constant *C);
305     void AddGlobalInitializerConstraints(Node *N, Constant *C);
306
307     void AddConstraintsForNonInternalLinkage(Function *F);
308     bool AddConstraintsForExternalFunction(Function *F);
309     void AddConstraintsForCall(CallSite CS, Function *F);
310
311
312     void PrintNode(Node *N);
313     void PrintConstraints();
314     void PrintPointsToGraph();
315
316     //===------------------------------------------------------------------===//
317     // Instruction visitation methods for adding constraints
318     //
319     friend class InstVisitor<Andersens>;
320     void visitReturnInst(ReturnInst &RI);
321     void visitInvokeInst(InvokeInst &II) { visitCallSite(CallSite(&II)); }
322     void visitCallInst(CallInst &CI) { visitCallSite(CallSite(&CI)); }
323     void visitCallSite(CallSite CS);
324     void visitAllocationInst(AllocationInst &AI);
325     void visitLoadInst(LoadInst &LI);
326     void visitStoreInst(StoreInst &SI);
327     void visitGetElementPtrInst(GetElementPtrInst &GEP);
328     void visitPHINode(PHINode &PN);
329     void visitCastInst(CastInst &CI);
330     void visitSelectInst(SelectInst &SI);
331     void visitVANext(VANextInst &I);
332     void visitVAArg(VAArgInst &I);
333     void visitInstruction(Instruction &I);
334   };
335
336   RegisterOpt<Andersens> X("anders-aa",
337                            "Andersen's Interprocedural Alias Analysis");
338   RegisterAnalysisGroup<AliasAnalysis, Andersens> Y;
339 }
340
341 ModulePass *llvm::createAndersensPass() { return new Andersens(); }
342
343 //===----------------------------------------------------------------------===//
344 //                  AliasAnalysis Interface Implementation
345 //===----------------------------------------------------------------------===//
346
347 AliasAnalysis::AliasResult Andersens::alias(const Value *V1, unsigned V1Size,
348                                             const Value *V2, unsigned V2Size) {
349   Node *N1 = getNode((Value*)V1);
350   Node *N2 = getNode((Value*)V2);
351
352   // Check to see if the two pointers are known to not alias.  They don't alias
353   // if their points-to sets do not intersect.
354   if (!N1->intersectsIgnoring(N2, &GraphNodes[NullObject]))
355     return NoAlias;
356
357   return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
358 }
359
360
361 /// getMustAlias - We can provide must alias information if we know that a
362 /// pointer can only point to a specific function or the null pointer.
363 /// Unfortunately we cannot determine must-alias information for global
364 /// variables or any other memory memory objects because we do not track whether
365 /// a pointer points to the beginning of an object or a field of it.
366 void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
367   Node *N = getNode(P);
368   Node::iterator I = N->begin();
369   if (I != N->end()) {
370     // If there is exactly one element in the points-to set for the object...
371     ++I;
372     if (I == N->end()) {
373       Node *Pointee = *N->begin();
374
375       // If a function is the only object in the points-to set, then it must be
376       // the destination.  Note that we can't handle global variables here,
377       // because we don't know if the pointer is actually pointing to a field of
378       // the global or to the beginning of it.
379       if (Value *V = Pointee->getValue()) {
380         if (Function *F = dyn_cast<Function>(V))
381           RetVals.push_back(F);
382       } else {
383         // If the object in the points-to set is the null object, then the null
384         // pointer is a must alias.
385         if (Pointee == &GraphNodes[NullObject])
386           RetVals.push_back(Constant::getNullValue(P->getType()));
387       }
388     }
389   }
390   
391   AliasAnalysis::getMustAliases(P, RetVals);
392 }
393
394 /// pointsToConstantMemory - If we can determine that this pointer only points
395 /// to constant memory, return true.  In practice, this means that if the
396 /// pointer can only point to constant globals, functions, or the null pointer,
397 /// return true.
398 ///
399 bool Andersens::pointsToConstantMemory(const Value *P) {
400   Node *N = getNode((Value*)P);
401   for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I) {
402     if (Value *V = (*I)->getValue()) {
403       if (!isa<GlobalValue>(V) || (isa<GlobalVariable>(V) &&
404                                    !cast<GlobalVariable>(V)->isConstant()))
405         return AliasAnalysis::pointsToConstantMemory(P);
406     } else {
407       if (*I != &GraphNodes[NullObject])
408         return AliasAnalysis::pointsToConstantMemory(P);
409     }
410   }
411
412   return true;
413 }
414
415 //===----------------------------------------------------------------------===//
416 //                       Object Identification Phase
417 //===----------------------------------------------------------------------===//
418
419 /// IdentifyObjects - This stage scans the program, adding an entry to the
420 /// GraphNodes list for each memory object in the program (global stack or
421 /// heap), and populates the ValueNodes and ObjectNodes maps for these objects.
422 ///
423 void Andersens::IdentifyObjects(Module &M) {
424   unsigned NumObjects = 0;
425
426   // Object #0 is always the universal set: the object that we don't know
427   // anything about.
428   assert(NumObjects == UniversalSet && "Something changed!");
429   ++NumObjects;
430
431   // Object #1 always represents the null pointer.
432   assert(NumObjects == NullPtr && "Something changed!");
433   ++NumObjects;
434
435   // Object #2 always represents the null object (the object pointed to by null)
436   assert(NumObjects == NullObject && "Something changed!");
437   ++NumObjects;
438
439   // Add all the globals first.
440   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
441        I != E; ++I) {
442     ObjectNodes[I] = NumObjects++;
443     ValueNodes[I] = NumObjects++;
444   }
445
446   // Add nodes for all of the functions and the instructions inside of them.
447   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
448     // The function itself is a memory object.
449     ValueNodes[F] = NumObjects++;
450     ObjectNodes[F] = NumObjects++;
451     if (isa<PointerType>(F->getFunctionType()->getReturnType()))
452       ReturnNodes[F] = NumObjects++;
453     if (F->getFunctionType()->isVarArg())
454       VarargNodes[F] = NumObjects++;
455
456     // Add nodes for all of the incoming pointer arguments.
457     for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
458          I != E; ++I)
459       if (isa<PointerType>(I->getType()))
460         ValueNodes[I] = NumObjects++;
461
462     // Scan the function body, creating a memory object for each heap/stack
463     // allocation in the body of the function and a node to represent all
464     // pointer values defined by instructions and used as operands.
465     for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
466       // If this is an heap or stack allocation, create a node for the memory
467       // object.
468       if (isa<PointerType>(II->getType())) {
469         ValueNodes[&*II] = NumObjects++;
470         if (AllocationInst *AI = dyn_cast<AllocationInst>(&*II))
471           ObjectNodes[AI] = NumObjects++;
472       }
473     }
474   }
475
476   // Now that we know how many objects to create, make them all now!
477   GraphNodes.resize(NumObjects);
478   NumNodes += NumObjects;
479 }
480
481 //===----------------------------------------------------------------------===//
482 //                     Constraint Identification Phase
483 //===----------------------------------------------------------------------===//
484
485 /// getNodeForConstantPointer - Return the node corresponding to the constant
486 /// pointer itself.
487 Andersens::Node *Andersens::getNodeForConstantPointer(Constant *C) {
488   assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
489
490   if (isa<ConstantPointerNull>(C) || isa<UndefValue>(C))
491     return &GraphNodes[NullPtr];
492   else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
493     return getNode(GV);
494   else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
495     switch (CE->getOpcode()) {
496     case Instruction::GetElementPtr:
497       return getNodeForConstantPointer(CE->getOperand(0));
498     case Instruction::Cast:
499       if (isa<PointerType>(CE->getOperand(0)->getType()))
500         return getNodeForConstantPointer(CE->getOperand(0));
501       else
502         return &GraphNodes[UniversalSet];
503     default:
504       std::cerr << "Constant Expr not yet handled: " << *CE << "\n";
505       assert(0);
506     }
507   } else {
508     assert(0 && "Unknown constant pointer!");
509   }
510   return 0;
511 }
512
513 /// getNodeForConstantPointerTarget - Return the node POINTED TO by the
514 /// specified constant pointer.
515 Andersens::Node *Andersens::getNodeForConstantPointerTarget(Constant *C) {
516   assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
517
518   if (isa<ConstantPointerNull>(C))
519     return &GraphNodes[NullObject];
520   else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
521     return getObject(GV);
522   else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
523     switch (CE->getOpcode()) {
524     case Instruction::GetElementPtr:
525       return getNodeForConstantPointerTarget(CE->getOperand(0));
526     case Instruction::Cast:
527       if (isa<PointerType>(CE->getOperand(0)->getType()))
528         return getNodeForConstantPointerTarget(CE->getOperand(0));
529       else
530         return &GraphNodes[UniversalSet];
531     default:
532       std::cerr << "Constant Expr not yet handled: " << *CE << "\n";
533       assert(0);
534     }
535   } else {
536     assert(0 && "Unknown constant pointer!");
537   }
538   return 0;
539 }
540
541 /// AddGlobalInitializerConstraints - Add inclusion constraints for the memory
542 /// object N, which contains values indicated by C.
543 void Andersens::AddGlobalInitializerConstraints(Node *N, Constant *C) {
544   if (C->getType()->isFirstClassType()) {
545     if (isa<PointerType>(C->getType()))
546       N->addPointerTo(getNodeForConstantPointer(C));
547   } else if (C->isNullValue()) {
548     N->addPointerTo(&GraphNodes[NullObject]);
549     return;
550   } else {
551     // If this is an array or struct, include constraints for each element.
552     assert(isa<ConstantArray>(C) || isa<ConstantStruct>(C));
553     for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
554       AddGlobalInitializerConstraints(N, cast<Constant>(C->getOperand(i)));
555   }
556 }
557
558 /// AddConstraintsForNonInternalLinkage - If this function does not have
559 /// internal linkage, realize that we can't trust anything passed into or
560 /// returned by this function.
561 void Andersens::AddConstraintsForNonInternalLinkage(Function *F) {
562   for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
563     if (isa<PointerType>(I->getType()))
564       // If this is an argument of an externally accessible function, the
565       // incoming pointer might point to anything.
566       Constraints.push_back(Constraint(Constraint::Copy, getNode(I),
567                                        &GraphNodes[UniversalSet]));
568 }
569
570 /// AddConstraintsForExternalFunction - If this is a call to a "known" function,
571 /// add the constraints an return false.  If this is a call to an unknown
572 /// function, return true.
573 bool Andersens::AddConstraintsForExternalFunction(Function *F) {
574   assert(F->isExternal() && "Not an external function!");
575
576   // These functions don't induce any points-to constraints.
577   if (F->getName() == "printf" || F->getName() == "fprintf" ||
578       F->getName() == "open" || F->getName() == "fopen" ||
579       F->getName() == "atoi" ||
580       F->getName() == "llvm.memset" || F->getName() == "memcmp" ||
581       F->getName() == "read" || F->getName() == "write")
582     return false;
583
584   // These functions do induce points-to edges.
585   if (F->getName() == "llvm.memcpy" || F->getName() == "llvm.memmove") {
586     Function::arg_iterator Dst = F->arg_begin(), Src = Dst;
587     // Note: this is a poor approximation, this says Dest = Src, instead of
588     // *Dest = *Src.
589     ++Src;
590     Constraints.push_back(Constraint(Constraint::Copy, getNode(Dst),
591                                      getNode(Src)));
592     return false;
593   }
594
595   return true;
596 }
597
598
599
600 /// CollectConstraints - This stage scans the program, adding a constraint to
601 /// the Constraints list for each instruction in the program that induces a
602 /// constraint, and setting up the initial points-to graph.
603 ///
604 void Andersens::CollectConstraints(Module &M) {
605   // First, the universal set points to itself.
606   GraphNodes[UniversalSet].addPointerTo(&GraphNodes[UniversalSet]);
607
608   // Next, the null pointer points to the null object.
609   GraphNodes[NullPtr].addPointerTo(&GraphNodes[NullObject]);
610
611   // Next, add any constraints on global variables and their initializers.
612   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
613        I != E; ++I) {
614     // Associate the address of the global object as pointing to the memory for
615     // the global: &G = <G memory>
616     Node *Object = getObject(I);
617     Object->setValue(I);
618     getNodeValue(*I)->addPointerTo(Object);
619
620     if (I->hasInitializer()) {
621       AddGlobalInitializerConstraints(Object, I->getInitializer());
622     } else {
623       // If it doesn't have an initializer (i.e. it's defined in another
624       // translation unit), it points to the universal set.
625       Constraints.push_back(Constraint(Constraint::Copy, Object,
626                                        &GraphNodes[UniversalSet]));
627     }
628   }
629   
630   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
631     // Make the function address point to the function object.
632     getNodeValue(*F)->addPointerTo(getObject(F)->setValue(F));
633
634     // Set up the return value node.
635     if (isa<PointerType>(F->getFunctionType()->getReturnType()))
636       getReturnNode(F)->setValue(F);
637     if (F->getFunctionType()->isVarArg())
638       getVarargNode(F)->setValue(F);
639
640     // Set up incoming argument nodes.
641     for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
642          I != E; ++I)
643       if (isa<PointerType>(I->getType()))
644         getNodeValue(*I);
645
646     if (!F->hasInternalLinkage())
647       AddConstraintsForNonInternalLinkage(F);
648
649     if (!F->isExternal()) {
650       // Scan the function body, creating a memory object for each heap/stack
651       // allocation in the body of the function and a node to represent all
652       // pointer values defined by instructions and used as operands.
653       visit(F);
654     } else if (AddConstraintsForExternalFunction(F)) {
655       // If we don't "know" about this function, assume the worst.
656
657       // External functions that return pointers return the universal set.
658       if (isa<PointerType>(F->getFunctionType()->getReturnType()))
659         Constraints.push_back(Constraint(Constraint::Copy,
660                                          getReturnNode(F),
661                                          &GraphNodes[UniversalSet]));
662
663       // Any pointers that are passed into the function have the universal set
664       // stored into them.
665       for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
666            I != E; ++I)
667         if (isa<PointerType>(I->getType())) {
668           // Pointers passed into external functions could have anything stored
669           // through them.
670           Constraints.push_back(Constraint(Constraint::Store, getNode(I),
671                                            &GraphNodes[UniversalSet]));
672           // Memory objects passed into external function calls can have the
673           // universal set point to them.
674           Constraints.push_back(Constraint(Constraint::Copy,
675                                            &GraphNodes[UniversalSet],
676                                            getNode(I)));
677         }
678
679       // If this is an external varargs function, it can also store pointers
680       // into any pointers passed through the varargs section.
681       if (F->getFunctionType()->isVarArg())
682         Constraints.push_back(Constraint(Constraint::Store, getVarargNode(F),
683                                          &GraphNodes[UniversalSet]));
684     }
685   }
686   NumConstraints += Constraints.size();
687 }
688
689
690 void Andersens::visitInstruction(Instruction &I) {
691 #ifdef NDEBUG
692   return;          // This function is just a big assert.
693 #endif
694   if (isa<BinaryOperator>(I))
695     return;
696   // Most instructions don't have any effect on pointer values.
697   switch (I.getOpcode()) {
698   case Instruction::Br:
699   case Instruction::Switch:
700   case Instruction::Unwind:
701   case Instruction::Unreachable:
702   case Instruction::Free:
703   case Instruction::Shl:
704   case Instruction::Shr:
705     return;
706   default:
707     // Is this something we aren't handling yet?
708     std::cerr << "Unknown instruction: " << I;
709     abort();
710   }
711 }
712
713 void Andersens::visitAllocationInst(AllocationInst &AI) {
714   getNodeValue(AI)->addPointerTo(getObject(&AI)->setValue(&AI));
715 }
716
717 void Andersens::visitReturnInst(ReturnInst &RI) {
718   if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType()))
719     // return V   -->   <Copy/retval{F}/v>
720     Constraints.push_back(Constraint(Constraint::Copy,
721                                      getReturnNode(RI.getParent()->getParent()),
722                                      getNode(RI.getOperand(0))));
723 }
724
725 void Andersens::visitLoadInst(LoadInst &LI) {
726   if (isa<PointerType>(LI.getType()))
727     // P1 = load P2  -->  <Load/P1/P2>
728     Constraints.push_back(Constraint(Constraint::Load, getNodeValue(LI),
729                                      getNode(LI.getOperand(0))));
730 }
731
732 void Andersens::visitStoreInst(StoreInst &SI) {
733   if (isa<PointerType>(SI.getOperand(0)->getType()))
734     // store P1, P2  -->  <Store/P2/P1>
735     Constraints.push_back(Constraint(Constraint::Store,
736                                      getNode(SI.getOperand(1)),
737                                      getNode(SI.getOperand(0))));
738 }
739
740 void Andersens::visitGetElementPtrInst(GetElementPtrInst &GEP) {
741   // P1 = getelementptr P2, ... --> <Copy/P1/P2>
742   Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(GEP),
743                                    getNode(GEP.getOperand(0))));
744 }
745
746 void Andersens::visitPHINode(PHINode &PN) {
747   if (isa<PointerType>(PN.getType())) {
748     Node *PNN = getNodeValue(PN);
749     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
750       // P1 = phi P2, P3  -->  <Copy/P1/P2>, <Copy/P1/P3>, ...
751       Constraints.push_back(Constraint(Constraint::Copy, PNN,
752                                        getNode(PN.getIncomingValue(i))));
753   }
754 }
755
756 void Andersens::visitCastInst(CastInst &CI) {
757   Value *Op = CI.getOperand(0);
758   if (isa<PointerType>(CI.getType())) {
759     if (isa<PointerType>(Op->getType())) {
760       // P1 = cast P2  --> <Copy/P1/P2>
761       Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
762                                        getNode(CI.getOperand(0))));
763     } else {
764       // P1 = cast int --> <Copy/P1/Univ>
765       Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
766                                        &GraphNodes[UniversalSet]));
767     }
768   } else if (isa<PointerType>(Op->getType())) {
769     // int = cast P1 --> <Copy/Univ/P1>
770     Constraints.push_back(Constraint(Constraint::Copy,
771                                      &GraphNodes[UniversalSet],
772                                      getNode(CI.getOperand(0))));
773   }
774 }
775
776 void Andersens::visitSelectInst(SelectInst &SI) {
777   if (isa<PointerType>(SI.getType())) {
778     Node *SIN = getNodeValue(SI);
779     // P1 = select C, P2, P3   ---> <Copy/P1/P2>, <Copy/P1/P3>
780     Constraints.push_back(Constraint(Constraint::Copy, SIN,
781                                      getNode(SI.getOperand(1))));
782     Constraints.push_back(Constraint(Constraint::Copy, SIN,
783                                      getNode(SI.getOperand(2))));
784   }
785 }
786
787 void Andersens::visitVANext(VANextInst &I) {
788   // FIXME: Implement
789   assert(0 && "vanext not handled yet!");
790 }
791 void Andersens::visitVAArg(VAArgInst &I) {
792   assert(0 && "vaarg not handled yet!");
793 }
794
795 /// AddConstraintsForCall - Add constraints for a call with actual arguments
796 /// specified by CS to the function specified by F.  Note that the types of
797 /// arguments might not match up in the case where this is an indirect call and
798 /// the function pointer has been casted.  If this is the case, do something
799 /// reasonable.
800 void Andersens::AddConstraintsForCall(CallSite CS, Function *F) {
801   if (isa<PointerType>(CS.getType())) {
802     Node *CSN = getNode(CS.getInstruction());
803     if (isa<PointerType>(F->getFunctionType()->getReturnType())) {
804       Constraints.push_back(Constraint(Constraint::Copy, CSN,
805                                        getReturnNode(F)));
806     } else {
807       // If the function returns a non-pointer value, handle this just like we
808       // treat a nonpointer cast to pointer.
809       Constraints.push_back(Constraint(Constraint::Copy, CSN,
810                                        &GraphNodes[UniversalSet]));
811     }
812   } else if (isa<PointerType>(F->getFunctionType()->getReturnType())) {
813     Constraints.push_back(Constraint(Constraint::Copy,
814                                      &GraphNodes[UniversalSet],
815                                      getReturnNode(F)));
816   }
817   
818   Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
819   CallSite::arg_iterator ArgI = CS.arg_begin(), ArgE = CS.arg_end();
820   for (; AI != AE && ArgI != ArgE; ++AI, ++ArgI)
821     if (isa<PointerType>(AI->getType())) {
822       if (isa<PointerType>((*ArgI)->getType())) {
823         // Copy the actual argument into the formal argument.
824         Constraints.push_back(Constraint(Constraint::Copy, getNode(AI),
825                                          getNode(*ArgI)));
826       } else {
827         Constraints.push_back(Constraint(Constraint::Copy, getNode(AI),
828                                          &GraphNodes[UniversalSet]));
829       }
830     } else if (isa<PointerType>((*ArgI)->getType())) {
831       Constraints.push_back(Constraint(Constraint::Copy,
832                                        &GraphNodes[UniversalSet],
833                                        getNode(*ArgI)));
834     }
835   
836   // Copy all pointers passed through the varargs section to the varargs node.
837   if (F->getFunctionType()->isVarArg())
838     for (; ArgI != ArgE; ++ArgI)
839       if (isa<PointerType>((*ArgI)->getType()))
840         Constraints.push_back(Constraint(Constraint::Copy, getVarargNode(F),
841                                          getNode(*ArgI)));
842   // If more arguments are passed in than we track, just drop them on the floor.
843 }
844
845 void Andersens::visitCallSite(CallSite CS) {
846   if (isa<PointerType>(CS.getType()))
847     getNodeValue(*CS.getInstruction());
848
849   if (Function *F = CS.getCalledFunction()) {
850     AddConstraintsForCall(CS, F);
851   } else {
852     // We don't handle indirect call sites yet.  Keep track of them for when we
853     // discover the call graph incrementally.
854     IndirectCalls.push_back(CS);
855   }
856 }
857
858 //===----------------------------------------------------------------------===//
859 //                         Constraint Solving Phase
860 //===----------------------------------------------------------------------===//
861
862 /// intersects - Return true if the points-to set of this node intersects
863 /// with the points-to set of the specified node.
864 bool Andersens::Node::intersects(Node *N) const {
865   iterator I1 = begin(), I2 = N->begin(), E1 = end(), E2 = N->end();
866   while (I1 != E1 && I2 != E2) {
867     if (*I1 == *I2) return true;
868     if (*I1 < *I2)
869       ++I1;
870     else
871       ++I2;
872   }
873   return false;
874 }
875
876 /// intersectsIgnoring - Return true if the points-to set of this node
877 /// intersects with the points-to set of the specified node on any nodes
878 /// except for the specified node to ignore.
879 bool Andersens::Node::intersectsIgnoring(Node *N, Node *Ignoring) const {
880   iterator I1 = begin(), I2 = N->begin(), E1 = end(), E2 = N->end();
881   while (I1 != E1 && I2 != E2) {
882     if (*I1 == *I2) {
883       if (*I1 != Ignoring) return true;
884       ++I1; ++I2;
885     } else if (*I1 < *I2)
886       ++I1;
887     else
888       ++I2;
889   }
890   return false;
891 }
892
893 // Copy constraint: all edges out of the source node get copied to the
894 // destination node.  This returns true if a change is made.
895 bool Andersens::Node::copyFrom(Node *N) {
896   // Use a mostly linear-time merge since both of the lists are sorted.
897   bool Changed = false;
898   iterator I = N->begin(), E = N->end();
899   unsigned i = 0;
900   while (I != E && i != Pointees.size()) {
901     if (Pointees[i] < *I) {
902       ++i;
903     } else if (Pointees[i] == *I) {
904       ++i; ++I;
905     } else {
906       // We found a new element to copy over.
907       Changed = true;
908       Pointees.insert(Pointees.begin()+i, *I);
909        ++i; ++I;
910     }
911   }
912
913   if (I != E) {
914     Pointees.insert(Pointees.end(), I, E);
915     Changed = true;
916   }
917
918   return Changed;
919 }
920
921 bool Andersens::Node::loadFrom(Node *N) {
922   bool Changed = false;
923   for (iterator I = N->begin(), E = N->end(); I != E; ++I)
924     Changed |= copyFrom(*I);
925   return Changed;
926 }
927
928 bool Andersens::Node::storeThrough(Node *N) {
929   bool Changed = false;
930   for (iterator I = begin(), E = end(); I != E; ++I)
931     Changed |= (*I)->copyFrom(N);
932   return Changed;
933 }
934
935
936 /// SolveConstraints - This stage iteratively processes the constraints list
937 /// propagating constraints (adding edges to the Nodes in the points-to graph)
938 /// until a fixed point is reached.
939 ///
940 void Andersens::SolveConstraints() {
941   bool Changed = true;
942   unsigned Iteration = 0;
943   while (Changed) {
944     Changed = false;
945     ++NumIters;
946     DEBUG(std::cerr << "Starting iteration #" << Iteration++ << "!\n");
947
948     // Loop over all of the constraints, applying them in turn.
949     for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
950       Constraint &C = Constraints[i];
951       switch (C.Type) {
952       case Constraint::Copy:
953         Changed |= C.Dest->copyFrom(C.Src);
954         break;
955       case Constraint::Load:
956         Changed |= C.Dest->loadFrom(C.Src);
957         break;
958       case Constraint::Store:
959         Changed |= C.Dest->storeThrough(C.Src);
960         break;
961       default:
962         assert(0 && "Unknown constraint!");
963       }
964     }
965
966     if (Changed) {
967       // Check to see if any internal function's addresses have been passed to
968       // external functions.  If so, we have to assume that their incoming
969       // arguments could be anything.  If there are any internal functions in
970       // the universal node that we don't know about, we must iterate.
971       for (Node::iterator I = GraphNodes[UniversalSet].begin(),
972              E = GraphNodes[UniversalSet].end(); I != E; ++I)
973         if (Function *F = dyn_cast_or_null<Function>((*I)->getValue()))
974           if (F->hasInternalLinkage() &&
975               EscapingInternalFunctions.insert(F).second) {
976             // We found a function that is just now escaping.  Mark it as if it
977             // didn't have internal linkage.
978             AddConstraintsForNonInternalLinkage(F);
979             DEBUG(std::cerr << "Found escaping internal function: "
980                             << F->getName() << "\n");
981             ++NumEscapingFunctions;
982           }
983
984       // Check to see if we have discovered any new callees of the indirect call
985       // sites.  If so, add constraints to the analysis.
986       for (unsigned i = 0, e = IndirectCalls.size(); i != e; ++i) {
987         CallSite CS = IndirectCalls[i];
988         std::vector<Function*> &KnownCallees = IndirectCallees[CS];
989         Node *CN = getNode(CS.getCalledValue());
990
991         for (Node::iterator NI = CN->begin(), E = CN->end(); NI != E; ++NI)
992           if (Function *F = dyn_cast_or_null<Function>((*NI)->getValue())) {
993             std::vector<Function*>::iterator IP =
994               std::lower_bound(KnownCallees.begin(), KnownCallees.end(), F);
995             if (IP == KnownCallees.end() || *IP != F) {
996               // Add the constraints for the call now.
997               AddConstraintsForCall(CS, F);
998               DEBUG(std::cerr << "Found actual callee '"
999                               << F->getName() << "' for call: "
1000                               << *CS.getInstruction() << "\n");
1001               ++NumIndirectCallees;
1002               KnownCallees.insert(IP, F);
1003             }
1004           }
1005       }
1006     }
1007   }
1008 }
1009
1010
1011
1012 //===----------------------------------------------------------------------===//
1013 //                               Debugging Output
1014 //===----------------------------------------------------------------------===//
1015
1016 void Andersens::PrintNode(Node *N) {
1017   if (N == &GraphNodes[UniversalSet]) {
1018     std::cerr << "<universal>";
1019     return;
1020   } else if (N == &GraphNodes[NullPtr]) {
1021     std::cerr << "<nullptr>";
1022     return;
1023   } else if (N == &GraphNodes[NullObject]) {
1024     std::cerr << "<null>";
1025     return;
1026   }
1027
1028   assert(N->getValue() != 0 && "Never set node label!");
1029   Value *V = N->getValue();
1030   if (Function *F = dyn_cast<Function>(V)) {
1031     if (isa<PointerType>(F->getFunctionType()->getReturnType()) &&
1032         N == getReturnNode(F)) {
1033       std::cerr << F->getName() << ":retval";
1034       return;
1035     } else if (F->getFunctionType()->isVarArg() && N == getVarargNode(F)) {
1036       std::cerr << F->getName() << ":vararg";
1037       return;
1038     }
1039   }
1040
1041   if (Instruction *I = dyn_cast<Instruction>(V))
1042     std::cerr << I->getParent()->getParent()->getName() << ":";
1043   else if (Argument *Arg = dyn_cast<Argument>(V))
1044     std::cerr << Arg->getParent()->getName() << ":";
1045
1046   if (V->hasName())
1047     std::cerr << V->getName();
1048   else
1049     std::cerr << "(unnamed)";
1050
1051   if (isa<GlobalValue>(V) || isa<AllocationInst>(V))
1052     if (N == getObject(V))
1053       std::cerr << "<mem>";
1054 }
1055
1056 void Andersens::PrintConstraints() {
1057   std::cerr << "Constraints:\n";
1058   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
1059     std::cerr << "  #" << i << ":  ";
1060     Constraint &C = Constraints[i];
1061     if (C.Type == Constraint::Store)
1062       std::cerr << "*";
1063     PrintNode(C.Dest);
1064     std::cerr << " = ";
1065     if (C.Type == Constraint::Load)
1066       std::cerr << "*";
1067     PrintNode(C.Src);
1068     std::cerr << "\n";
1069   }
1070 }
1071
1072 void Andersens::PrintPointsToGraph() {
1073   std::cerr << "Points-to graph:\n";
1074   for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) {
1075     Node *N = &GraphNodes[i];
1076     std::cerr << "[" << (N->end() - N->begin()) << "] ";
1077     PrintNode(N);
1078     std::cerr << "\t--> ";
1079     for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I) {
1080       if (I != N->begin()) std::cerr << ", ";
1081       PrintNode(*I);
1082     }
1083     std::cerr << "\n";
1084   }
1085 }