llvm.org GIT mirror llvm / a9cf196
remove anders-aa from mainline, it isn't maintained and is tantalyzing enough that people keep trying to use it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97483 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
6 changed file(s) with 2 addition(s) and 2974 deletion(s). Raw diff Collapse all Expand all
402402 href="#basic-aa">basicaa and no-aa
403403 passes) every alias analysis pass chains to another alias analysis
404404 implementation (for example, the user can specify "-basicaa -ds-aa
405 -anders-aa -licm" to get the maximum benefit from the three alias
405 -licm" to get the maximum benefit from both alias
406406 analyses). The alias analysis class automatically takes care of most of this
407407 for methods that you don't override. For methods that you do override, in code
408408 paths that return a conservative MayAlias or Mod/Ref result, simply return
704704
705705
706706
707 The -anders-aa pass
708
709
710
711
712

The -anders-aa pass implements the well-known "Andersen's algorithm"

713 for interprocedural alias analysis. This algorithm is a subset-based,
714 flow-insensitive, context-insensitive, and field-insensitive alias analysis that
715 is widely believed to be fairly precise. Unfortunately, this algorithm is also
716 O(N3). The LLVM implementation currently does not implement any of
717 the refinements (such as "online cycle elimination" or "offline variable
718 substitution") to improve its efficiency, so it can be quite slow in common
719 cases.
720

721
722
723
724
725
726707 The -steens-aa pass
727708
728709
854835
855836
856837

These passes are useful for evaluating the various alias analysis

857 implementations. You can use them with commands like 'opt -anders-aa -ds-aa
838 implementations. You can use them with commands like 'opt -ds-aa
858839 -aa-eval foo.bc -disable-output -stats'.

859840
860841
7474
ANALYSIS PASSES
7575
OptionName
7676
-aa-evalExhaustive Alias Analysis Precision Evaluator
77
-anders-aaAndersen's Interprocedural Alias Analysis
7877
-basicaaBasic Alias Analysis (default AA impl)
7978
-basiccgBasic CallGraph Construction
8079
-codegenprepareOptimize for code generation
203202
204203
205204
206 Andersen's Interprocedural Alias Analysis
207
208
209

210 This is an implementation of Andersen's interprocedural alias
211 analysis
212

213
214

215 In pointer analysis terms, this is a subset-based, flow-insensitive,
216 field-sensitive, and context-insensitive algorithm pointer algorithm.
217

218
219

220 This algorithm is implemented as three stages:
221

222
223
224
  • Object identification.
  • 225
  • Inclusion constraint identification.
  • 226
  • Offline constraint graph optimization.
  • 227
  • Inclusion constraint solving.
  • 228
    229
    230

    231 The object identification stage identifies all of the memory objects in the
    232 program, which includes globals, heap allocated objects, and stack allocated
    233 objects.
    234

    235
    236

    237 The inclusion constraint identification stage finds all inclusion constraints
    238 in the program by scanning the program, looking for pointer assignments and
    239 other statements that effect the points-to graph. For a statement like
    240 A = B, this statement is processed to
    241 indicate that A can point to anything that B can point
    242 to. Constraints can handle copies, loads, and stores, and address taking.
    243

    244
    245

    246 The offline constraint graph optimization portion includes offline variable
    247 substitution algorithms intended to computer pointer and location
    248 equivalences. Pointer equivalences are those pointers that will have the
    249 same points-to sets, and location equivalences are those variables that
    250 always appear together in points-to sets.
    251

    252
    253

    254 The inclusion constraint solving phase iteratively propagates the inclusion
    255 constraints until a fixed point is reached. This is an O(n³)
    256 algorithm.
    257

    258
    259

    260 Function constraints are handled as if they were structs with X
    261 fields. Thus, an access to argument X of function Y is
    262 an access to node index getNode(Y) + X.
    263 This representation allows handling of indirect calls without any issues. To
    264 wit, an indirect call Y(a,b) is
    265 equivalent to *(Y + 1) = a, *(Y + 2) =
    266 b. The return node for a function F is always
    267 located at getNode(F) + CallReturnPos. The arguments
    268 start at getNode(F) + CallArgPos.
    269

    270
    271

    272 Please keep in mind that the current andersen's pass has many known
    273 problems and bugs. It should be considered "research quality".
    274

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