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