1 //===- Andersens.cpp - Andersen's Interprocedural Alias Analysis ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an implementation of Andersen's interprocedural alias
13 // In pointer analysis terms, this is a subset-based, flow-insensitive,
14 // field-sensitive, and context-insensitive algorithm pointer algorithm.
16 // This algorithm is implemented as three stages:
17 // 1. Object identification.
18 // 2. Inclusion constraint identification.
19 // 3. Offline constraint graph optimization
20 // 4. Inclusion constraint solving.
22 // The object identification stage identifies all of the memory objects in the
23 // program, which includes globals, heap allocated objects, and stack allocated
26 // The inclusion constraint identification stage finds all inclusion constraints
27 // in the program by scanning the program, looking for pointer assignments and
28 // other statements that effect the points-to graph. For a statement like "A =
29 // B", this statement is processed to indicate that A can point to anything that
30 // B can point to. Constraints can handle copies, loads, and stores, and
33 // The offline constraint graph optimization portion includes offline variable
34 // substitution algorithms intended to compute pointer and location
35 // equivalences. Pointer equivalences are those pointers that will have the
36 // same points-to sets, and location equivalences are those variables that
37 // always appear together in points-to sets. It also includes an offline
38 // cycle detection algorithm that allows cycles to be collapsed sooner
41 // The inclusion constraint solving phase iteratively propagates the inclusion
42 // constraints until a fixed point is reached. This is an O(N^3) algorithm.
44 // Function constraints are handled as if they were structs with X fields.
45 // Thus, an access to argument X of function Y is an access to node index
46 // getNode(Y) + X. This representation allows handling of indirect calls
47 // without any issues. To wit, an indirect call Y(a,b) is equivalent to
48 // *(Y + 1) = a, *(Y + 2) = b.
49 // The return node for a function is always located at getNode(F) +
50 // CallReturnPos. The arguments start at getNode(F) + CallArgPos.
52 // Future Improvements:
54 //===----------------------------------------------------------------------===//
56 #define DEBUG_TYPE "anders-aa"
57 #include "llvm/Constants.h"
58 #include "llvm/DerivedTypes.h"
59 #include "llvm/Instructions.h"
60 #include "llvm/Module.h"
61 #include "llvm/Pass.h"
62 #include "llvm/Support/Compiler.h"
63 #include "llvm/Support/ErrorHandling.h"
64 #include "llvm/Support/InstIterator.h"
65 #include "llvm/Support/InstVisitor.h"
66 #include "llvm/Analysis/AliasAnalysis.h"
67 #include "llvm/Analysis/Passes.h"
68 #include "llvm/Support/Debug.h"
69 #include "llvm/System/Atomic.h"
70 #include "llvm/ADT/Statistic.h"
71 #include "llvm/ADT/SparseBitVector.h"
72 #include "llvm/ADT/DenseSet.h"
81 // Determining the actual set of nodes the universal set can consist of is very
82 // expensive because it means propagating around very large sets. We rely on
83 // other analysis being able to determine which nodes can never be pointed to in
84 // order to disambiguate further than "points-to anything".
85 #define FULL_UNIVERSAL 0
89 STATISTIC(NumIters
, "Number of iterations to reach convergence");
91 STATISTIC(NumConstraints
, "Number of constraints");
92 STATISTIC(NumNodes
, "Number of nodes");
93 STATISTIC(NumUnified
, "Number of variables unified");
94 STATISTIC(NumErased
, "Number of redundant constraints erased");
96 static const unsigned SelfRep
= (unsigned)-1;
97 static const unsigned Unvisited
= (unsigned)-1;
98 // Position of the function return node relative to the function node.
99 static const unsigned CallReturnPos
= 1;
100 // Position of the function call node relative to the function node.
101 static const unsigned CallFirstArgPos
= 2;
104 struct BitmapKeyInfo
{
105 static inline SparseBitVector
<> *getEmptyKey() {
106 return reinterpret_cast<SparseBitVector
<> *>(-1);
108 static inline SparseBitVector
<> *getTombstoneKey() {
109 return reinterpret_cast<SparseBitVector
<> *>(-2);
111 static unsigned getHashValue(const SparseBitVector
<> *bitmap
) {
112 return bitmap
->getHashValue();
114 static bool isEqual(const SparseBitVector
<> *LHS
,
115 const SparseBitVector
<> *RHS
) {
118 else if (LHS
== getEmptyKey() || RHS
== getEmptyKey()
119 || LHS
== getTombstoneKey() || RHS
== getTombstoneKey())
125 static bool isPod() { return true; }
128 class VISIBILITY_HIDDEN Andersens
: public ModulePass
, public AliasAnalysis
,
129 private InstVisitor
<Andersens
> {
132 /// Constraint - Objects of this structure are used to represent the various
133 /// constraints identified by the algorithm. The constraints are 'copy',
134 /// for statements like "A = B", 'load' for statements like "A = *B",
135 /// 'store' for statements like "*A = B", and AddressOf for statements like
136 /// A = alloca; The Offset is applied as *(A + K) = B for stores,
137 /// A = *(B + K) for loads, and A = B + K for copies. It is
138 /// illegal on addressof constraints (because it is statically
139 /// resolvable to A = &C where C = B + K)
142 enum ConstraintType
{ Copy
, Load
, Store
, AddressOf
} Type
;
147 Constraint(ConstraintType Ty
, unsigned D
, unsigned S
, unsigned O
= 0)
148 : Type(Ty
), Dest(D
), Src(S
), Offset(O
) {
149 assert((Offset
== 0 || Ty
!= AddressOf
) &&
150 "Offset is illegal on addressof constraints");
153 bool operator==(const Constraint
&RHS
) const {
154 return RHS
.Type
== Type
157 && RHS
.Offset
== Offset
;
160 bool operator!=(const Constraint
&RHS
) const {
161 return !(*this == RHS
);
164 bool operator<(const Constraint
&RHS
) const {
165 if (RHS
.Type
!= Type
)
166 return RHS
.Type
< Type
;
167 else if (RHS
.Dest
!= Dest
)
168 return RHS
.Dest
< Dest
;
169 else if (RHS
.Src
!= Src
)
170 return RHS
.Src
< Src
;
171 return RHS
.Offset
< Offset
;
175 // Information DenseSet requires implemented in order to be able to do
178 static inline std::pair
<unsigned, unsigned> getEmptyKey() {
179 return std::make_pair(~0U, ~0U);
181 static inline std::pair
<unsigned, unsigned> getTombstoneKey() {
182 return std::make_pair(~0U - 1, ~0U - 1);
184 static unsigned getHashValue(const std::pair
<unsigned, unsigned> &P
) {
185 return P
.first
^ P
.second
;
187 static unsigned isEqual(const std::pair
<unsigned, unsigned> &LHS
,
188 const std::pair
<unsigned, unsigned> &RHS
) {
193 struct ConstraintKeyInfo
{
194 static inline Constraint
getEmptyKey() {
195 return Constraint(Constraint::Copy
, ~0U, ~0U, ~0U);
197 static inline Constraint
getTombstoneKey() {
198 return Constraint(Constraint::Copy
, ~0U - 1, ~0U - 1, ~0U - 1);
200 static unsigned getHashValue(const Constraint
&C
) {
201 return C
.Src
^ C
.Dest
^ C
.Type
^ C
.Offset
;
203 static bool isEqual(const Constraint
&LHS
,
204 const Constraint
&RHS
) {
205 return LHS
.Type
== RHS
.Type
&& LHS
.Dest
== RHS
.Dest
206 && LHS
.Src
== RHS
.Src
&& LHS
.Offset
== RHS
.Offset
;
210 // Node class - This class is used to represent a node in the constraint
211 // graph. Due to various optimizations, it is not always the case that
212 // there is a mapping from a Node to a Value. In particular, we add
213 // artificial Node's that represent the set of pointed-to variables shared
214 // for each location equivalent Node.
217 static volatile sys::cas_flag Counter
;
221 SparseBitVector
<> *Edges
;
222 SparseBitVector
<> *PointsTo
;
223 SparseBitVector
<> *OldPointsTo
;
224 std::list
<Constraint
> Constraints
;
226 // Pointer and location equivalence labels
227 unsigned PointerEquivLabel
;
228 unsigned LocationEquivLabel
;
229 // Predecessor edges, both real and implicit
230 SparseBitVector
<> *PredEdges
;
231 SparseBitVector
<> *ImplicitPredEdges
;
232 // Set of nodes that point to us, only use for location equivalence.
233 SparseBitVector
<> *PointedToBy
;
234 // Number of incoming edges, used during variable substitution to early
235 // free the points-to sets
237 // True if our points-to set is in the Set2PEClass map
239 // True if our node has no indirect constraints (complex or otherwise)
241 // True if the node is address taken, *or* it is part of a group of nodes
242 // that must be kept together. This is set to true for functions and
243 // their arg nodes, which must be kept at the same position relative to
244 // their base function node.
247 // Nodes in cycles (or in equivalence classes) are united together using a
248 // standard union-find representation with path compression. NodeRep
249 // gives the index into GraphNodes for the representative Node.
252 // Modification timestamp. Assigned from Counter.
253 // Used for work list prioritization.
256 explicit Node(bool direct
= true) :
257 Val(0), Edges(0), PointsTo(0), OldPointsTo(0),
258 PointerEquivLabel(0), LocationEquivLabel(0), PredEdges(0),
259 ImplicitPredEdges(0), PointedToBy(0), NumInEdges(0),
260 StoredInHash(false), Direct(direct
), AddressTaken(false),
261 NodeRep(SelfRep
), Timestamp(0) { }
263 Node
*setValue(Value
*V
) {
264 assert(Val
== 0 && "Value already set for this node!");
269 /// getValue - Return the LLVM value corresponding to this node.
271 Value
*getValue() const { return Val
; }
273 /// addPointerTo - Add a pointer to the list of pointees of this node,
274 /// returning true if this caused a new pointer to be added, or false if
275 /// we already knew about the points-to relation.
276 bool addPointerTo(unsigned Node
) {
277 return PointsTo
->test_and_set(Node
);
280 /// intersects - Return true if the points-to set of this node intersects
281 /// with the points-to set of the specified node.
282 bool intersects(Node
*N
) const;
284 /// intersectsIgnoring - Return true if the points-to set of this node
285 /// intersects with the points-to set of the specified node on any nodes
286 /// except for the specified node to ignore.
287 bool intersectsIgnoring(Node
*N
, unsigned) const;
289 // Timestamp a node (used for work list prioritization)
291 Timestamp
= sys::AtomicIncrement(&Counter
);
296 return( (int) NodeRep
< 0 );
300 struct WorkListElement
{
303 WorkListElement(Node
* n
, unsigned t
) : node(n
), Timestamp(t
) {}
305 // Note that we reverse the sense of the comparison because we
306 // actually want to give low timestamps the priority over high,
307 // whereas priority is typically interpreted as a greater value is
308 // given high priority.
309 bool operator<(const WorkListElement
& that
) const {
310 return( this->Timestamp
> that
.Timestamp
);
314 // Priority-queue based work list specialized for Nodes.
316 std::priority_queue
<WorkListElement
> Q
;
319 void insert(Node
* n
) {
320 Q
.push( WorkListElement(n
, n
->Timestamp
) );
323 // We automatically discard non-representative nodes and nodes
324 // that were in the work list twice (we keep a copy of the
325 // timestamp in the work list so we can detect this situation by
326 // comparing against the node's current timestamp).
328 while( !Q
.empty() ) {
329 WorkListElement x
= Q
.top(); Q
.pop();
330 Node
* INode
= x
.node
;
332 if( INode
->isRep() &&
333 INode
->Timestamp
== x
.Timestamp
) {
345 /// GraphNodes - This vector is populated as part of the object
346 /// identification stage of the analysis, which populates this vector with a
347 /// node for each memory object and fills in the ValueNodes map.
348 std::vector
<Node
> GraphNodes
;
350 /// ValueNodes - This map indicates the Node that a particular Value* is
351 /// represented by. This contains entries for all pointers.
352 DenseMap
<Value
*, unsigned> ValueNodes
;
354 /// ObjectNodes - This map contains entries for each memory object in the
355 /// program: globals, alloca's and mallocs.
356 DenseMap
<Value
*, unsigned> ObjectNodes
;
358 /// ReturnNodes - This map contains an entry for each function in the
359 /// program that returns a value.
360 DenseMap
<Function
*, unsigned> ReturnNodes
;
362 /// VarargNodes - This map contains the entry used to represent all pointers
363 /// passed through the varargs portion of a function call for a particular
364 /// function. An entry is not present in this map for functions that do not
365 /// take variable arguments.
366 DenseMap
<Function
*, unsigned> VarargNodes
;
369 /// Constraints - This vector contains a list of all of the constraints
370 /// identified by the program.
371 std::vector
<Constraint
> Constraints
;
373 // Map from graph node to maximum K value that is allowed (for functions,
374 // this is equivalent to the number of arguments + CallFirstArgPos)
375 std::map
<unsigned, unsigned> MaxK
;
377 /// This enum defines the GraphNodes indices that correspond to important
385 // Stack for Tarjan's
386 std::stack
<unsigned> SCCStack
;
387 // Map from Graph Node to DFS number
388 std::vector
<unsigned> Node2DFS
;
389 // Map from Graph Node to Deleted from graph.
390 std::vector
<bool> Node2Deleted
;
391 // Same as Node Maps, but implemented as std::map because it is faster to
393 std::map
<unsigned, unsigned> Tarjan2DFS
;
394 std::map
<unsigned, bool> Tarjan2Deleted
;
395 // Current DFS number
400 WorkList
*CurrWL
, *NextWL
; // "current" and "next" work lists
402 // Offline variable substitution related things
404 // Temporary rep storage, used because we can't collapse SCC's in the
405 // predecessor graph by uniting the variables permanently, we can only do so
406 // for the successor graph.
407 std::vector
<unsigned> VSSCCRep
;
408 // Mapping from node to whether we have visited it during SCC finding yet.
409 std::vector
<bool> Node2Visited
;
410 // During variable substitution, we create unknowns to represent the unknown
411 // value that is a dereference of a variable. These nodes are known as
412 // "ref" nodes (since they represent the value of dereferences).
413 unsigned FirstRefNode
;
414 // During HVN, we create represent address taken nodes as if they were
415 // unknown (since HVN, unlike HU, does not evaluate unions).
416 unsigned FirstAdrNode
;
417 // Current pointer equivalence class number
419 // Mapping from points-to sets to equivalence classes
420 typedef DenseMap
<SparseBitVector
<> *, unsigned, BitmapKeyInfo
> BitVectorMap
;
421 BitVectorMap Set2PEClass
;
422 // Mapping from pointer equivalences to the representative node. -1 if we
423 // have no representative node for this pointer equivalence class yet.
424 std::vector
<int> PEClass2Node
;
425 // Mapping from pointer equivalences to representative node. This includes
426 // pointer equivalent but not location equivalent variables. -1 if we have
427 // no representative node for this pointer equivalence class yet.
428 std::vector
<int> PENLEClass2Node
;
429 // Union/Find for HCD
430 std::vector
<unsigned> HCDSCCRep
;
431 // HCD's offline-detected cycles; "Statically DeTected"
432 // -1 if not part of such a cycle, otherwise a representative node.
433 std::vector
<int> SDT
;
434 // Whether to use SDT (UniteNodes can use it during solving, but not before)
439 Andersens() : ModulePass(&ID
) {}
441 bool runOnModule(Module
&M
) {
442 InitializeAliasAnalysis(this);
444 CollectConstraints(M
);
446 #define DEBUG_TYPE "anders-aa-constraints"
447 DEBUG(PrintConstraints());
449 #define DEBUG_TYPE "anders-aa"
451 DEBUG(PrintPointsToGraph());
453 // Free the constraints list, as we don't need it to respond to alias
455 std::vector
<Constraint
>().swap(Constraints
);
456 //These are needed for Print() (-analyze in opt)
457 //ObjectNodes.clear();
458 //ReturnNodes.clear();
459 //VarargNodes.clear();
463 void releaseMemory() {
464 // FIXME: Until we have transitively required passes working correctly,
465 // this cannot be enabled! Otherwise, using -count-aa with the pass
466 // causes memory to be freed too early. :(
468 // The memory objects and ValueNodes data structures at the only ones that
469 // are still live after construction.
470 std::vector
<Node
>().swap(GraphNodes
);
475 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
476 AliasAnalysis::getAnalysisUsage(AU
);
477 AU
.setPreservesAll(); // Does not transform code
480 //------------------------------------------------
481 // Implement the AliasAnalysis API
483 AliasResult
alias(const Value
*V1
, unsigned V1Size
,
484 const Value
*V2
, unsigned V2Size
);
485 virtual ModRefResult
getModRefInfo(CallSite CS
, Value
*P
, unsigned Size
);
486 virtual ModRefResult
getModRefInfo(CallSite CS1
, CallSite CS2
);
487 void getMustAliases(Value
*P
, std::vector
<Value
*> &RetVals
);
488 bool pointsToConstantMemory(const Value
*P
);
490 virtual void deleteValue(Value
*V
) {
492 getAnalysis
<AliasAnalysis
>().deleteValue(V
);
495 virtual void copyValue(Value
*From
, Value
*To
) {
496 ValueNodes
[To
] = ValueNodes
[From
];
497 getAnalysis
<AliasAnalysis
>().copyValue(From
, To
);
501 /// getNode - Return the node corresponding to the specified pointer scalar.
503 unsigned getNode(Value
*V
) {
504 if (Constant
*C
= dyn_cast
<Constant
>(V
))
505 if (!isa
<GlobalValue
>(C
))
506 return getNodeForConstantPointer(C
);
508 DenseMap
<Value
*, unsigned>::iterator I
= ValueNodes
.find(V
);
509 if (I
== ValueNodes
.end()) {
513 llvm_unreachable("Value does not have a node in the points-to graph!");
518 /// getObject - Return the node corresponding to the memory object for the
519 /// specified global or allocation instruction.
520 unsigned getObject(Value
*V
) const {
521 DenseMap
<Value
*, unsigned>::iterator I
= ObjectNodes
.find(V
);
522 assert(I
!= ObjectNodes
.end() &&
523 "Value does not have an object in the points-to graph!");
527 /// getReturnNode - Return the node representing the return value for the
528 /// specified function.
529 unsigned getReturnNode(Function
*F
) const {
530 DenseMap
<Function
*, unsigned>::iterator I
= ReturnNodes
.find(F
);
531 assert(I
!= ReturnNodes
.end() && "Function does not return a value!");
535 /// getVarargNode - Return the node representing the variable arguments
536 /// formal for the specified function.
537 unsigned getVarargNode(Function
*F
) const {
538 DenseMap
<Function
*, unsigned>::iterator I
= VarargNodes
.find(F
);
539 assert(I
!= VarargNodes
.end() && "Function does not take var args!");
543 /// getNodeValue - Get the node for the specified LLVM value and set the
544 /// value for it to be the specified value.
545 unsigned getNodeValue(Value
&V
) {
546 unsigned Index
= getNode(&V
);
547 GraphNodes
[Index
].setValue(&V
);
551 unsigned UniteNodes(unsigned First
, unsigned Second
,
552 bool UnionByRank
= true);
553 unsigned FindNode(unsigned Node
);
554 unsigned FindNode(unsigned Node
) const;
556 void IdentifyObjects(Module
&M
);
557 void CollectConstraints(Module
&M
);
558 bool AnalyzeUsesOfFunction(Value
*);
559 void CreateConstraintGraph();
560 void OptimizeConstraints();
561 unsigned FindEquivalentNode(unsigned, unsigned);
562 void ClumpAddressTaken();
563 void RewriteConstraints();
567 void Search(unsigned Node
);
568 void UnitePointerEquivalences();
569 void SolveConstraints();
570 bool QueryNode(unsigned Node
);
571 void Condense(unsigned Node
);
572 void HUValNum(unsigned Node
);
573 void HVNValNum(unsigned Node
);
574 unsigned getNodeForConstantPointer(Constant
*C
);
575 unsigned getNodeForConstantPointerTarget(Constant
*C
);
576 void AddGlobalInitializerConstraints(unsigned, Constant
*C
);
578 void AddConstraintsForNonInternalLinkage(Function
*F
);
579 void AddConstraintsForCall(CallSite CS
, Function
*F
);
580 bool AddConstraintsForExternalCall(CallSite CS
, Function
*F
);
583 void PrintNode(const Node
*N
) const;
584 void PrintConstraints() const ;
585 void PrintConstraint(const Constraint
&) const;
586 void PrintLabels() const;
587 void PrintPointsToGraph() const;
589 //===------------------------------------------------------------------===//
590 // Instruction visitation methods for adding constraints
592 friend class InstVisitor
<Andersens
>;
593 void visitReturnInst(ReturnInst
&RI
);
594 void visitInvokeInst(InvokeInst
&II
) { visitCallSite(CallSite(&II
)); }
595 void visitCallInst(CallInst
&CI
) { visitCallSite(CallSite(&CI
)); }
596 void visitCallSite(CallSite CS
);
597 void visitAllocationInst(AllocationInst
&AI
);
598 void visitLoadInst(LoadInst
&LI
);
599 void visitStoreInst(StoreInst
&SI
);
600 void visitGetElementPtrInst(GetElementPtrInst
&GEP
);
601 void visitPHINode(PHINode
&PN
);
602 void visitCastInst(CastInst
&CI
);
603 void visitICmpInst(ICmpInst
&ICI
) {} // NOOP!
604 void visitFCmpInst(FCmpInst
&ICI
) {} // NOOP!
605 void visitSelectInst(SelectInst
&SI
);
606 void visitVAArg(VAArgInst
&I
);
607 void visitInstruction(Instruction
&I
);
609 //===------------------------------------------------------------------===//
610 // Implement Analyize interface
612 void print(raw_ostream
&O
, const Module
*) const {
613 PrintPointsToGraph();
618 char Andersens::ID
= 0;
619 static RegisterPass
<Andersens
>
620 X("anders-aa", "Andersen's Interprocedural Alias Analysis (experimental)",
622 static RegisterAnalysisGroup
<AliasAnalysis
> Y(X
);
624 // Initialize Timestamp Counter (static).
625 volatile llvm::sys::cas_flag
Andersens::Node::Counter
= 0;
627 ModulePass
*llvm::createAndersensPass() { return new Andersens(); }
629 //===----------------------------------------------------------------------===//
630 // AliasAnalysis Interface Implementation
631 //===----------------------------------------------------------------------===//
633 AliasAnalysis::AliasResult
Andersens::alias(const Value
*V1
, unsigned V1Size
,
634 const Value
*V2
, unsigned V2Size
) {
635 Node
*N1
= &GraphNodes
[FindNode(getNode(const_cast<Value
*>(V1
)))];
636 Node
*N2
= &GraphNodes
[FindNode(getNode(const_cast<Value
*>(V2
)))];
638 // Check to see if the two pointers are known to not alias. They don't alias
639 // if their points-to sets do not intersect.
640 if (!N1
->intersectsIgnoring(N2
, NullObject
))
643 return AliasAnalysis::alias(V1
, V1Size
, V2
, V2Size
);
646 AliasAnalysis::ModRefResult
647 Andersens::getModRefInfo(CallSite CS
, Value
*P
, unsigned Size
) {
648 // The only thing useful that we can contribute for mod/ref information is
649 // when calling external function calls: if we know that memory never escapes
650 // from the program, it cannot be modified by an external call.
652 // NOTE: This is not really safe, at least not when the entire program is not
653 // available. The deal is that the external function could call back into the
654 // program and modify stuff. We ignore this technical niggle for now. This
655 // is, after all, a "research quality" implementation of Andersen's analysis.
656 if (Function
*F
= CS
.getCalledFunction())
657 if (F
->isDeclaration()) {
658 Node
*N1
= &GraphNodes
[FindNode(getNode(P
))];
660 if (N1
->PointsTo
->empty())
663 if (!UniversalSet
->PointsTo
->test(FindNode(getNode(P
))))
664 return NoModRef
; // Universal set does not contain P
666 if (!N1
->PointsTo
->test(UniversalSet
))
667 return NoModRef
; // P doesn't point to the universal set.
671 return AliasAnalysis::getModRefInfo(CS
, P
, Size
);
674 AliasAnalysis::ModRefResult
675 Andersens::getModRefInfo(CallSite CS1
, CallSite CS2
) {
676 return AliasAnalysis::getModRefInfo(CS1
,CS2
);
679 /// getMustAlias - We can provide must alias information if we know that a
680 /// pointer can only point to a specific function or the null pointer.
681 /// Unfortunately we cannot determine must-alias information for global
682 /// variables or any other memory memory objects because we do not track whether
683 /// a pointer points to the beginning of an object or a field of it.
684 void Andersens::getMustAliases(Value
*P
, std::vector
<Value
*> &RetVals
) {
685 Node
*N
= &GraphNodes
[FindNode(getNode(P
))];
686 if (N
->PointsTo
->count() == 1) {
687 Node
*Pointee
= &GraphNodes
[N
->PointsTo
->find_first()];
688 // If a function is the only object in the points-to set, then it must be
689 // the destination. Note that we can't handle global variables here,
690 // because we don't know if the pointer is actually pointing to a field of
691 // the global or to the beginning of it.
692 if (Value
*V
= Pointee
->getValue()) {
693 if (Function
*F
= dyn_cast
<Function
>(V
))
694 RetVals
.push_back(F
);
696 // If the object in the points-to set is the null object, then the null
697 // pointer is a must alias.
698 if (Pointee
== &GraphNodes
[NullObject
])
699 RetVals
.push_back(Constant::getNullValue(P
->getType()));
702 AliasAnalysis::getMustAliases(P
, RetVals
);
705 /// pointsToConstantMemory - If we can determine that this pointer only points
706 /// to constant memory, return true. In practice, this means that if the
707 /// pointer can only point to constant globals, functions, or the null pointer,
710 bool Andersens::pointsToConstantMemory(const Value
*P
) {
711 Node
*N
= &GraphNodes
[FindNode(getNode(const_cast<Value
*>(P
)))];
714 for (SparseBitVector
<>::iterator bi
= N
->PointsTo
->begin();
715 bi
!= N
->PointsTo
->end();
718 Node
*Pointee
= &GraphNodes
[i
];
719 if (Value
*V
= Pointee
->getValue()) {
720 if (!isa
<GlobalValue
>(V
) || (isa
<GlobalVariable
>(V
) &&
721 !cast
<GlobalVariable
>(V
)->isConstant()))
722 return AliasAnalysis::pointsToConstantMemory(P
);
725 return AliasAnalysis::pointsToConstantMemory(P
);
732 //===----------------------------------------------------------------------===//
733 // Object Identification Phase
734 //===----------------------------------------------------------------------===//
736 /// IdentifyObjects - This stage scans the program, adding an entry to the
737 /// GraphNodes list for each memory object in the program (global stack or
738 /// heap), and populates the ValueNodes and ObjectNodes maps for these objects.
740 void Andersens::IdentifyObjects(Module
&M
) {
741 unsigned NumObjects
= 0;
743 // Object #0 is always the universal set: the object that we don't know
745 assert(NumObjects
== UniversalSet
&& "Something changed!");
748 // Object #1 always represents the null pointer.
749 assert(NumObjects
== NullPtr
&& "Something changed!");
752 // Object #2 always represents the null object (the object pointed to by null)
753 assert(NumObjects
== NullObject
&& "Something changed!");
756 // Add all the globals first.
757 for (Module::global_iterator I
= M
.global_begin(), E
= M
.global_end();
759 ObjectNodes
[I
] = NumObjects
++;
760 ValueNodes
[I
] = NumObjects
++;
763 // Add nodes for all of the functions and the instructions inside of them.
764 for (Module::iterator F
= M
.begin(), E
= M
.end(); F
!= E
; ++F
) {
765 // The function itself is a memory object.
766 unsigned First
= NumObjects
;
767 ValueNodes
[F
] = NumObjects
++;
768 if (isa
<PointerType
>(F
->getFunctionType()->getReturnType()))
769 ReturnNodes
[F
] = NumObjects
++;
770 if (F
->getFunctionType()->isVarArg())
771 VarargNodes
[F
] = NumObjects
++;
774 // Add nodes for all of the incoming pointer arguments.
775 for (Function::arg_iterator I
= F
->arg_begin(), E
= F
->arg_end();
778 if (isa
<PointerType
>(I
->getType()))
779 ValueNodes
[I
] = NumObjects
++;
781 MaxK
[First
] = NumObjects
- First
;
783 // Scan the function body, creating a memory object for each heap/stack
784 // allocation in the body of the function and a node to represent all
785 // pointer values defined by instructions and used as operands.
786 for (inst_iterator II
= inst_begin(F
), E
= inst_end(F
); II
!= E
; ++II
) {
787 // If this is an heap or stack allocation, create a node for the memory
789 if (isa
<PointerType
>(II
->getType())) {
790 ValueNodes
[&*II
] = NumObjects
++;
791 if (AllocationInst
*AI
= dyn_cast
<AllocationInst
>(&*II
))
792 ObjectNodes
[AI
] = NumObjects
++;
795 // Calls to inline asm need to be added as well because the callee isn't
796 // referenced anywhere else.
797 if (CallInst
*CI
= dyn_cast
<CallInst
>(&*II
)) {
798 Value
*Callee
= CI
->getCalledValue();
799 if (isa
<InlineAsm
>(Callee
))
800 ValueNodes
[Callee
] = NumObjects
++;
805 // Now that we know how many objects to create, make them all now!
806 GraphNodes
.resize(NumObjects
);
807 NumNodes
+= NumObjects
;
810 //===----------------------------------------------------------------------===//
811 // Constraint Identification Phase
812 //===----------------------------------------------------------------------===//
814 /// getNodeForConstantPointer - Return the node corresponding to the constant
816 unsigned Andersens::getNodeForConstantPointer(Constant
*C
) {
817 assert(isa
<PointerType
>(C
->getType()) && "Not a constant pointer!");
819 if (isa
<ConstantPointerNull
>(C
) || isa
<UndefValue
>(C
))
821 else if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
823 else if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
)) {
824 switch (CE
->getOpcode()) {
825 case Instruction::GetElementPtr
:
826 return getNodeForConstantPointer(CE
->getOperand(0));
827 case Instruction::IntToPtr
:
829 case Instruction::BitCast
:
830 return getNodeForConstantPointer(CE
->getOperand(0));
832 errs() << "Constant Expr not yet handled: " << *CE
<< "\n";
836 llvm_unreachable("Unknown constant pointer!");
841 /// getNodeForConstantPointerTarget - Return the node POINTED TO by the
842 /// specified constant pointer.
843 unsigned Andersens::getNodeForConstantPointerTarget(Constant
*C
) {
844 assert(isa
<PointerType
>(C
->getType()) && "Not a constant pointer!");
846 if (isa
<ConstantPointerNull
>(C
))
848 else if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
849 return getObject(GV
);
850 else if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
)) {
851 switch (CE
->getOpcode()) {
852 case Instruction::GetElementPtr
:
853 return getNodeForConstantPointerTarget(CE
->getOperand(0));
854 case Instruction::IntToPtr
:
856 case Instruction::BitCast
:
857 return getNodeForConstantPointerTarget(CE
->getOperand(0));
859 errs() << "Constant Expr not yet handled: " << *CE
<< "\n";
863 llvm_unreachable("Unknown constant pointer!");
868 /// AddGlobalInitializerConstraints - Add inclusion constraints for the memory
869 /// object N, which contains values indicated by C.
870 void Andersens::AddGlobalInitializerConstraints(unsigned NodeIndex
,
872 if (C
->getType()->isSingleValueType()) {
873 if (isa
<PointerType
>(C
->getType()))
874 Constraints
.push_back(Constraint(Constraint::Copy
, NodeIndex
,
875 getNodeForConstantPointer(C
)));
876 } else if (C
->isNullValue()) {
877 Constraints
.push_back(Constraint(Constraint::Copy
, NodeIndex
,
880 } else if (!isa
<UndefValue
>(C
)) {
881 // If this is an array or struct, include constraints for each element.
882 assert(isa
<ConstantArray
>(C
) || isa
<ConstantStruct
>(C
));
883 for (unsigned i
= 0, e
= C
->getNumOperands(); i
!= e
; ++i
)
884 AddGlobalInitializerConstraints(NodeIndex
,
885 cast
<Constant
>(C
->getOperand(i
)));
889 /// AddConstraintsForNonInternalLinkage - If this function does not have
890 /// internal linkage, realize that we can't trust anything passed into or
891 /// returned by this function.
892 void Andersens::AddConstraintsForNonInternalLinkage(Function
*F
) {
893 for (Function::arg_iterator I
= F
->arg_begin(), E
= F
->arg_end(); I
!= E
; ++I
)
894 if (isa
<PointerType
>(I
->getType()))
895 // If this is an argument of an externally accessible function, the
896 // incoming pointer might point to anything.
897 Constraints
.push_back(Constraint(Constraint::Copy
, getNode(I
),
901 /// AddConstraintsForCall - If this is a call to a "known" function, add the
902 /// constraints and return true. If this is a call to an unknown function,
904 bool Andersens::AddConstraintsForExternalCall(CallSite CS
, Function
*F
) {
905 assert(F
->isDeclaration() && "Not an external function!");
907 // These functions don't induce any points-to constraints.
908 if (F
->getName() == "atoi" || F
->getName() == "atof" ||
909 F
->getName() == "atol" || F
->getName() == "atoll" ||
910 F
->getName() == "remove" || F
->getName() == "unlink" ||
911 F
->getName() == "rename" || F
->getName() == "memcmp" ||
912 F
->getName() == "llvm.memset" ||
913 F
->getName() == "strcmp" || F
->getName() == "strncmp" ||
914 F
->getName() == "execl" || F
->getName() == "execlp" ||
915 F
->getName() == "execle" || F
->getName() == "execv" ||
916 F
->getName() == "execvp" || F
->getName() == "chmod" ||
917 F
->getName() == "puts" || F
->getName() == "write" ||
918 F
->getName() == "open" || F
->getName() == "create" ||
919 F
->getName() == "truncate" || F
->getName() == "chdir" ||
920 F
->getName() == "mkdir" || F
->getName() == "rmdir" ||
921 F
->getName() == "read" || F
->getName() == "pipe" ||
922 F
->getName() == "wait" || F
->getName() == "time" ||
923 F
->getName() == "stat" || F
->getName() == "fstat" ||
924 F
->getName() == "lstat" || F
->getName() == "strtod" ||
925 F
->getName() == "strtof" || F
->getName() == "strtold" ||
926 F
->getName() == "fopen" || F
->getName() == "fdopen" ||
927 F
->getName() == "freopen" ||
928 F
->getName() == "fflush" || F
->getName() == "feof" ||
929 F
->getName() == "fileno" || F
->getName() == "clearerr" ||
930 F
->getName() == "rewind" || F
->getName() == "ftell" ||
931 F
->getName() == "ferror" || F
->getName() == "fgetc" ||
932 F
->getName() == "fgetc" || F
->getName() == "_IO_getc" ||
933 F
->getName() == "fwrite" || F
->getName() == "fread" ||
934 F
->getName() == "fgets" || F
->getName() == "ungetc" ||
935 F
->getName() == "fputc" ||
936 F
->getName() == "fputs" || F
->getName() == "putc" ||
937 F
->getName() == "ftell" || F
->getName() == "rewind" ||
938 F
->getName() == "_IO_putc" || F
->getName() == "fseek" ||
939 F
->getName() == "fgetpos" || F
->getName() == "fsetpos" ||
940 F
->getName() == "printf" || F
->getName() == "fprintf" ||
941 F
->getName() == "sprintf" || F
->getName() == "vprintf" ||
942 F
->getName() == "vfprintf" || F
->getName() == "vsprintf" ||
943 F
->getName() == "scanf" || F
->getName() == "fscanf" ||
944 F
->getName() == "sscanf" || F
->getName() == "__assert_fail" ||
945 F
->getName() == "modf")
949 // These functions do induce points-to edges.
950 if (F
->getName() == "llvm.memcpy" ||
951 F
->getName() == "llvm.memmove" ||
952 F
->getName() == "memmove") {
954 const FunctionType
*FTy
= F
->getFunctionType();
955 if (FTy
->getNumParams() > 1 &&
956 isa
<PointerType
>(FTy
->getParamType(0)) &&
957 isa
<PointerType
>(FTy
->getParamType(1))) {
959 // *Dest = *Src, which requires an artificial graph node to represent the
960 // constraint. It is broken up into *Dest = temp, temp = *Src
961 unsigned FirstArg
= getNode(CS
.getArgument(0));
962 unsigned SecondArg
= getNode(CS
.getArgument(1));
963 unsigned TempArg
= GraphNodes
.size();
964 GraphNodes
.push_back(Node());
965 Constraints
.push_back(Constraint(Constraint::Store
,
967 Constraints
.push_back(Constraint(Constraint::Load
,
968 TempArg
, SecondArg
));
969 // In addition, Dest = Src
970 Constraints
.push_back(Constraint(Constraint::Copy
,
971 FirstArg
, SecondArg
));
977 if (F
->getName() == "realloc" || F
->getName() == "strchr" ||
978 F
->getName() == "strrchr" || F
->getName() == "strstr" ||
979 F
->getName() == "strtok") {
980 const FunctionType
*FTy
= F
->getFunctionType();
981 if (FTy
->getNumParams() > 0 &&
982 isa
<PointerType
>(FTy
->getParamType(0))) {
983 Constraints
.push_back(Constraint(Constraint::Copy
,
984 getNode(CS
.getInstruction()),
985 getNode(CS
.getArgument(0))));
995 /// AnalyzeUsesOfFunction - Look at all of the users of the specified function.
996 /// If this is used by anything complex (i.e., the address escapes), return
998 bool Andersens::AnalyzeUsesOfFunction(Value
*V
) {
1000 if (!isa
<PointerType
>(V
->getType())) return true;
1002 for (Value::use_iterator UI
= V
->use_begin(), E
= V
->use_end(); UI
!= E
; ++UI
)
1003 if (isa
<LoadInst
>(*UI
)) {
1005 } else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(*UI
)) {
1006 if (V
== SI
->getOperand(1)) {
1008 } else if (SI
->getOperand(1)) {
1009 return true; // Storing the pointer
1011 } else if (GetElementPtrInst
*GEP
= dyn_cast
<GetElementPtrInst
>(*UI
)) {
1012 if (AnalyzeUsesOfFunction(GEP
)) return true;
1013 } else if (CallInst
*CI
= dyn_cast
<CallInst
>(*UI
)) {
1014 // Make sure that this is just the function being called, not that it is
1015 // passing into the function.
1016 for (unsigned i
= 1, e
= CI
->getNumOperands(); i
!= e
; ++i
)
1017 if (CI
->getOperand(i
) == V
) return true;
1018 } else if (InvokeInst
*II
= dyn_cast
<InvokeInst
>(*UI
)) {
1019 // Make sure that this is just the function being called, not that it is
1020 // passing into the function.
1021 for (unsigned i
= 3, e
= II
->getNumOperands(); i
!= e
; ++i
)
1022 if (II
->getOperand(i
) == V
) return true;
1023 } else if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(*UI
)) {
1024 if (CE
->getOpcode() == Instruction::GetElementPtr
||
1025 CE
->getOpcode() == Instruction::BitCast
) {
1026 if (AnalyzeUsesOfFunction(CE
))
1031 } else if (ICmpInst
*ICI
= dyn_cast
<ICmpInst
>(*UI
)) {
1032 if (!isa
<ConstantPointerNull
>(ICI
->getOperand(1)))
1033 return true; // Allow comparison against null.
1034 } else if (isa
<FreeInst
>(*UI
)) {
1042 /// CollectConstraints - This stage scans the program, adding a constraint to
1043 /// the Constraints list for each instruction in the program that induces a
1044 /// constraint, and setting up the initial points-to graph.
1046 void Andersens::CollectConstraints(Module
&M
) {
1047 // First, the universal set points to itself.
1048 Constraints
.push_back(Constraint(Constraint::AddressOf
, UniversalSet
,
1050 Constraints
.push_back(Constraint(Constraint::Store
, UniversalSet
,
1053 // Next, the null pointer points to the null object.
1054 Constraints
.push_back(Constraint(Constraint::AddressOf
, NullPtr
, NullObject
));
1056 // Next, add any constraints on global variables and their initializers.
1057 for (Module::global_iterator I
= M
.global_begin(), E
= M
.global_end();
1059 // Associate the address of the global object as pointing to the memory for
1060 // the global: &G = <G memory>
1061 unsigned ObjectIndex
= getObject(I
);
1062 Node
*Object
= &GraphNodes
[ObjectIndex
];
1063 Object
->setValue(I
);
1064 Constraints
.push_back(Constraint(Constraint::AddressOf
, getNodeValue(*I
),
1067 if (I
->hasDefinitiveInitializer()) {
1068 AddGlobalInitializerConstraints(ObjectIndex
, I
->getInitializer());
1070 // If it doesn't have an initializer (i.e. it's defined in another
1071 // translation unit), it points to the universal set.
1072 Constraints
.push_back(Constraint(Constraint::Copy
, ObjectIndex
,
1077 for (Module::iterator F
= M
.begin(), E
= M
.end(); F
!= E
; ++F
) {
1078 // Set up the return value node.
1079 if (isa
<PointerType
>(F
->getFunctionType()->getReturnType()))
1080 GraphNodes
[getReturnNode(F
)].setValue(F
);
1081 if (F
->getFunctionType()->isVarArg())
1082 GraphNodes
[getVarargNode(F
)].setValue(F
);
1084 // Set up incoming argument nodes.
1085 for (Function::arg_iterator I
= F
->arg_begin(), E
= F
->arg_end();
1087 if (isa
<PointerType
>(I
->getType()))
1090 // At some point we should just add constraints for the escaping functions
1091 // at solve time, but this slows down solving. For now, we simply mark
1092 // address taken functions as escaping and treat them as external.
1093 if (!F
->hasLocalLinkage() || AnalyzeUsesOfFunction(F
))
1094 AddConstraintsForNonInternalLinkage(F
);
1096 if (!F
->isDeclaration()) {
1097 // Scan the function body, creating a memory object for each heap/stack
1098 // allocation in the body of the function and a node to represent all
1099 // pointer values defined by instructions and used as operands.
1102 // External functions that return pointers return the universal set.
1103 if (isa
<PointerType
>(F
->getFunctionType()->getReturnType()))
1104 Constraints
.push_back(Constraint(Constraint::Copy
,
1108 // Any pointers that are passed into the function have the universal set
1109 // stored into them.
1110 for (Function::arg_iterator I
= F
->arg_begin(), E
= F
->arg_end();
1112 if (isa
<PointerType
>(I
->getType())) {
1113 // Pointers passed into external functions could have anything stored
1115 Constraints
.push_back(Constraint(Constraint::Store
, getNode(I
),
1117 // Memory objects passed into external function calls can have the
1118 // universal set point to them.
1120 Constraints
.push_back(Constraint(Constraint::Copy
,
1124 Constraints
.push_back(Constraint(Constraint::Copy
,
1130 // If this is an external varargs function, it can also store pointers
1131 // into any pointers passed through the varargs section.
1132 if (F
->getFunctionType()->isVarArg())
1133 Constraints
.push_back(Constraint(Constraint::Store
, getVarargNode(F
),
1137 NumConstraints
+= Constraints
.size();
1141 void Andersens::visitInstruction(Instruction
&I
) {
1143 return; // This function is just a big assert.
1145 if (isa
<BinaryOperator
>(I
))
1147 // Most instructions don't have any effect on pointer values.
1148 switch (I
.getOpcode()) {
1149 case Instruction::Br
:
1150 case Instruction::Switch
:
1151 case Instruction::Unwind
:
1152 case Instruction::Unreachable
:
1153 case Instruction::Free
:
1154 case Instruction::ICmp
:
1155 case Instruction::FCmp
:
1158 // Is this something we aren't handling yet?
1159 errs() << "Unknown instruction: " << I
;
1160 llvm_unreachable(0);
1164 void Andersens::visitAllocationInst(AllocationInst
&AI
) {
1165 unsigned ObjectIndex
= getObject(&AI
);
1166 GraphNodes
[ObjectIndex
].setValue(&AI
);
1167 Constraints
.push_back(Constraint(Constraint::AddressOf
, getNodeValue(AI
),
1171 void Andersens::visitReturnInst(ReturnInst
&RI
) {
1172 if (RI
.getNumOperands() && isa
<PointerType
>(RI
.getOperand(0)->getType()))
1173 // return V --> <Copy/retval{F}/v>
1174 Constraints
.push_back(Constraint(Constraint::Copy
,
1175 getReturnNode(RI
.getParent()->getParent()),
1176 getNode(RI
.getOperand(0))));
1179 void Andersens::visitLoadInst(LoadInst
&LI
) {
1180 if (isa
<PointerType
>(LI
.getType()))
1181 // P1 = load P2 --> <Load/P1/P2>
1182 Constraints
.push_back(Constraint(Constraint::Load
, getNodeValue(LI
),
1183 getNode(LI
.getOperand(0))));
1186 void Andersens::visitStoreInst(StoreInst
&SI
) {
1187 if (isa
<PointerType
>(SI
.getOperand(0)->getType()))
1188 // store P1, P2 --> <Store/P2/P1>
1189 Constraints
.push_back(Constraint(Constraint::Store
,
1190 getNode(SI
.getOperand(1)),
1191 getNode(SI
.getOperand(0))));
1194 void Andersens::visitGetElementPtrInst(GetElementPtrInst
&GEP
) {
1195 // P1 = getelementptr P2, ... --> <Copy/P1/P2>
1196 Constraints
.push_back(Constraint(Constraint::Copy
, getNodeValue(GEP
),
1197 getNode(GEP
.getOperand(0))));
1200 void Andersens::visitPHINode(PHINode
&PN
) {
1201 if (isa
<PointerType
>(PN
.getType())) {
1202 unsigned PNN
= getNodeValue(PN
);
1203 for (unsigned i
= 0, e
= PN
.getNumIncomingValues(); i
!= e
; ++i
)
1204 // P1 = phi P2, P3 --> <Copy/P1/P2>, <Copy/P1/P3>, ...
1205 Constraints
.push_back(Constraint(Constraint::Copy
, PNN
,
1206 getNode(PN
.getIncomingValue(i
))));
1210 void Andersens::visitCastInst(CastInst
&CI
) {
1211 Value
*Op
= CI
.getOperand(0);
1212 if (isa
<PointerType
>(CI
.getType())) {
1213 if (isa
<PointerType
>(Op
->getType())) {
1214 // P1 = cast P2 --> <Copy/P1/P2>
1215 Constraints
.push_back(Constraint(Constraint::Copy
, getNodeValue(CI
),
1216 getNode(CI
.getOperand(0))));
1218 // P1 = cast int --> <Copy/P1/Univ>
1220 Constraints
.push_back(Constraint(Constraint::Copy
, getNodeValue(CI
),
1226 } else if (isa
<PointerType
>(Op
->getType())) {
1227 // int = cast P1 --> <Copy/Univ/P1>
1229 Constraints
.push_back(Constraint(Constraint::Copy
,
1231 getNode(CI
.getOperand(0))));
1233 getNode(CI
.getOperand(0));
1238 void Andersens::visitSelectInst(SelectInst
&SI
) {
1239 if (isa
<PointerType
>(SI
.getType())) {
1240 unsigned SIN
= getNodeValue(SI
);
1241 // P1 = select C, P2, P3 ---> <Copy/P1/P2>, <Copy/P1/P3>
1242 Constraints
.push_back(Constraint(Constraint::Copy
, SIN
,
1243 getNode(SI
.getOperand(1))));
1244 Constraints
.push_back(Constraint(Constraint::Copy
, SIN
,
1245 getNode(SI
.getOperand(2))));
1249 void Andersens::visitVAArg(VAArgInst
&I
) {
1250 llvm_unreachable("vaarg not handled yet!");
1253 /// AddConstraintsForCall - Add constraints for a call with actual arguments
1254 /// specified by CS to the function specified by F. Note that the types of
1255 /// arguments might not match up in the case where this is an indirect call and
1256 /// the function pointer has been casted. If this is the case, do something
1258 void Andersens::AddConstraintsForCall(CallSite CS
, Function
*F
) {
1259 Value
*CallValue
= CS
.getCalledValue();
1260 bool IsDeref
= F
== NULL
;
1262 // If this is a call to an external function, try to handle it directly to get
1263 // some taste of context sensitivity.
1264 if (F
&& F
->isDeclaration() && AddConstraintsForExternalCall(CS
, F
))
1267 if (isa
<PointerType
>(CS
.getType())) {
1268 unsigned CSN
= getNode(CS
.getInstruction());
1269 if (!F
|| isa
<PointerType
>(F
->getFunctionType()->getReturnType())) {
1271 Constraints
.push_back(Constraint(Constraint::Load
, CSN
,
1272 getNode(CallValue
), CallReturnPos
));
1274 Constraints
.push_back(Constraint(Constraint::Copy
, CSN
,
1275 getNode(CallValue
) + CallReturnPos
));
1277 // If the function returns a non-pointer value, handle this just like we
1278 // treat a nonpointer cast to pointer.
1279 Constraints
.push_back(Constraint(Constraint::Copy
, CSN
,
1282 } else if (F
&& isa
<PointerType
>(F
->getFunctionType()->getReturnType())) {
1284 Constraints
.push_back(Constraint(Constraint::Copy
,
1286 getNode(CallValue
) + CallReturnPos
));
1288 Constraints
.push_back(Constraint(Constraint::Copy
,
1289 getNode(CallValue
) + CallReturnPos
,
1296 CallSite::arg_iterator ArgI
= CS
.arg_begin(), ArgE
= CS
.arg_end();
1297 bool external
= !F
|| F
->isDeclaration();
1300 Function::arg_iterator AI
= F
->arg_begin(), AE
= F
->arg_end();
1301 for (; AI
!= AE
&& ArgI
!= ArgE
; ++AI
, ++ArgI
)
1304 if (external
&& isa
<PointerType
>((*ArgI
)->getType()))
1306 // Add constraint that ArgI can now point to anything due to
1307 // escaping, as can everything it points to. The second portion of
1308 // this should be taken care of by universal = *universal
1309 Constraints
.push_back(Constraint(Constraint::Copy
,
1314 if (isa
<PointerType
>(AI
->getType())) {
1315 if (isa
<PointerType
>((*ArgI
)->getType())) {
1316 // Copy the actual argument into the formal argument.
1317 Constraints
.push_back(Constraint(Constraint::Copy
, getNode(AI
),
1320 Constraints
.push_back(Constraint(Constraint::Copy
, getNode(AI
),
1323 } else if (isa
<PointerType
>((*ArgI
)->getType())) {
1325 Constraints
.push_back(Constraint(Constraint::Copy
,
1329 Constraints
.push_back(Constraint(Constraint::Copy
,
1337 unsigned ArgPos
= CallFirstArgPos
;
1338 for (; ArgI
!= ArgE
; ++ArgI
) {
1339 if (isa
<PointerType
>((*ArgI
)->getType())) {
1340 // Copy the actual argument into the formal argument.
1341 Constraints
.push_back(Constraint(Constraint::Store
,
1343 getNode(*ArgI
), ArgPos
++));
1345 Constraints
.push_back(Constraint(Constraint::Store
,
1346 getNode (CallValue
),
1347 UniversalSet
, ArgPos
++));
1351 // Copy all pointers passed through the varargs section to the varargs node.
1352 if (F
&& F
->getFunctionType()->isVarArg())
1353 for (; ArgI
!= ArgE
; ++ArgI
)
1354 if (isa
<PointerType
>((*ArgI
)->getType()))
1355 Constraints
.push_back(Constraint(Constraint::Copy
, getVarargNode(F
),
1357 // If more arguments are passed in than we track, just drop them on the floor.
1360 void Andersens::visitCallSite(CallSite CS
) {
1361 if (isa
<PointerType
>(CS
.getType()))
1362 getNodeValue(*CS
.getInstruction());
1364 if (Function
*F
= CS
.getCalledFunction()) {
1365 AddConstraintsForCall(CS
, F
);
1367 AddConstraintsForCall(CS
, NULL
);
1371 //===----------------------------------------------------------------------===//
1372 // Constraint Solving Phase
1373 //===----------------------------------------------------------------------===//
1375 /// intersects - Return true if the points-to set of this node intersects
1376 /// with the points-to set of the specified node.
1377 bool Andersens::Node::intersects(Node
*N
) const {
1378 return PointsTo
->intersects(N
->PointsTo
);
1381 /// intersectsIgnoring - Return true if the points-to set of this node
1382 /// intersects with the points-to set of the specified node on any nodes
1383 /// except for the specified node to ignore.
1384 bool Andersens::Node::intersectsIgnoring(Node
*N
, unsigned Ignoring
) const {
1385 // TODO: If we are only going to call this with the same value for Ignoring,
1386 // we should move the special values out of the points-to bitmap.
1387 bool WeHadIt
= PointsTo
->test(Ignoring
);
1388 bool NHadIt
= N
->PointsTo
->test(Ignoring
);
1389 bool Result
= false;
1391 PointsTo
->reset(Ignoring
);
1393 N
->PointsTo
->reset(Ignoring
);
1394 Result
= PointsTo
->intersects(N
->PointsTo
);
1396 PointsTo
->set(Ignoring
);
1398 N
->PointsTo
->set(Ignoring
);
1403 /// Clump together address taken variables so that the points-to sets use up
1404 /// less space and can be operated on faster.
1406 void Andersens::ClumpAddressTaken() {
1408 #define DEBUG_TYPE "anders-aa-renumber"
1409 std::vector
<unsigned> Translate
;
1410 std::vector
<Node
> NewGraphNodes
;
1412 Translate
.resize(GraphNodes
.size());
1413 unsigned NewPos
= 0;
1415 for (unsigned i
= 0; i
< Constraints
.size(); ++i
) {
1416 Constraint
&C
= Constraints
[i
];
1417 if (C
.Type
== Constraint::AddressOf
) {
1418 GraphNodes
[C
.Src
].AddressTaken
= true;
1421 for (unsigned i
= 0; i
< NumberSpecialNodes
; ++i
) {
1422 unsigned Pos
= NewPos
++;
1424 NewGraphNodes
.push_back(GraphNodes
[i
]);
1425 DEBUG(errs() << "Renumbering node " << i
<< " to node " << Pos
<< "\n");
1428 // I believe this ends up being faster than making two vectors and splicing
1430 for (unsigned i
= NumberSpecialNodes
; i
< GraphNodes
.size(); ++i
) {
1431 if (GraphNodes
[i
].AddressTaken
) {
1432 unsigned Pos
= NewPos
++;
1434 NewGraphNodes
.push_back(GraphNodes
[i
]);
1435 DEBUG(errs() << "Renumbering node " << i
<< " to node " << Pos
<< "\n");
1439 for (unsigned i
= NumberSpecialNodes
; i
< GraphNodes
.size(); ++i
) {
1440 if (!GraphNodes
[i
].AddressTaken
) {
1441 unsigned Pos
= NewPos
++;
1443 NewGraphNodes
.push_back(GraphNodes
[i
]);
1444 DEBUG(errs() << "Renumbering node " << i
<< " to node " << Pos
<< "\n");
1448 for (DenseMap
<Value
*, unsigned>::iterator Iter
= ValueNodes
.begin();
1449 Iter
!= ValueNodes
.end();
1451 Iter
->second
= Translate
[Iter
->second
];
1453 for (DenseMap
<Value
*, unsigned>::iterator Iter
= ObjectNodes
.begin();
1454 Iter
!= ObjectNodes
.end();
1456 Iter
->second
= Translate
[Iter
->second
];
1458 for (DenseMap
<Function
*, unsigned>::iterator Iter
= ReturnNodes
.begin();
1459 Iter
!= ReturnNodes
.end();
1461 Iter
->second
= Translate
[Iter
->second
];
1463 for (DenseMap
<Function
*, unsigned>::iterator Iter
= VarargNodes
.begin();
1464 Iter
!= VarargNodes
.end();
1466 Iter
->second
= Translate
[Iter
->second
];
1468 for (unsigned i
= 0; i
< Constraints
.size(); ++i
) {
1469 Constraint
&C
= Constraints
[i
];
1470 C
.Src
= Translate
[C
.Src
];
1471 C
.Dest
= Translate
[C
.Dest
];
1474 GraphNodes
.swap(NewGraphNodes
);
1476 #define DEBUG_TYPE "anders-aa"
1479 /// The technique used here is described in "Exploiting Pointer and Location
1480 /// Equivalence to Optimize Pointer Analysis. In the 14th International Static
1481 /// Analysis Symposium (SAS), August 2007." It is known as the "HVN" algorithm,
1482 /// and is equivalent to value numbering the collapsed constraint graph without
1483 /// evaluating unions. This is used as a pre-pass to HU in order to resolve
1484 /// first order pointer dereferences and speed up/reduce memory usage of HU.
1485 /// Running both is equivalent to HRU without the iteration
1486 /// HVN in more detail:
1487 /// Imagine the set of constraints was simply straight line code with no loops
1488 /// (we eliminate cycles, so there are no loops), such as:
1494 /// Applying value numbering to this code tells us:
1497 /// For HVN, this is as far as it goes. We assign new value numbers to every
1498 /// "address node", and every "reference node".
1499 /// To get the optimal result for this, we use a DFS + SCC (since all nodes in a
1500 /// cycle must have the same value number since the = operation is really
1501 /// inclusion, not overwrite), and value number nodes we receive points-to sets
1502 /// before we value our own node.
1503 /// The advantage of HU over HVN is that HU considers the inclusion property, so
1504 /// that if you have
1511 /// HU will determine that G == F == E. HVN will not, because it cannot prove
1512 /// that the points to information ends up being the same because they all
1513 /// receive &D from E anyway.
1515 void Andersens::HVN() {
1516 DEBUG(errs() << "Beginning HVN\n");
1517 // Build a predecessor graph. This is like our constraint graph with the
1518 // edges going in the opposite direction, and there are edges for all the
1519 // constraints, instead of just copy constraints. We also build implicit
1520 // edges for constraints are implied but not explicit. I.E for the constraint
1521 // a = &b, we add implicit edges *a = b. This helps us capture more cycles
1522 for (unsigned i
= 0, e
= Constraints
.size(); i
!= e
; ++i
) {
1523 Constraint
&C
= Constraints
[i
];
1524 if (C
.Type
== Constraint::AddressOf
) {
1525 GraphNodes
[C
.Src
].AddressTaken
= true;
1526 GraphNodes
[C
.Src
].Direct
= false;
1529 unsigned AdrNode
= C
.Src
+ FirstAdrNode
;
1530 if (!GraphNodes
[C
.Dest
].PredEdges
)
1531 GraphNodes
[C
.Dest
].PredEdges
= new SparseBitVector
<>;
1532 GraphNodes
[C
.Dest
].PredEdges
->set(AdrNode
);
1535 unsigned RefNode
= C
.Dest
+ FirstRefNode
;
1536 if (!GraphNodes
[RefNode
].ImplicitPredEdges
)
1537 GraphNodes
[RefNode
].ImplicitPredEdges
= new SparseBitVector
<>;
1538 GraphNodes
[RefNode
].ImplicitPredEdges
->set(C
.Src
);
1539 } else if (C
.Type
== Constraint::Load
) {
1540 if (C
.Offset
== 0) {
1542 if (!GraphNodes
[C
.Dest
].PredEdges
)
1543 GraphNodes
[C
.Dest
].PredEdges
= new SparseBitVector
<>;
1544 GraphNodes
[C
.Dest
].PredEdges
->set(C
.Src
+ FirstRefNode
);
1546 GraphNodes
[C
.Dest
].Direct
= false;
1548 } else if (C
.Type
== Constraint::Store
) {
1549 if (C
.Offset
== 0) {
1551 unsigned RefNode
= C
.Dest
+ FirstRefNode
;
1552 if (!GraphNodes
[RefNode
].PredEdges
)
1553 GraphNodes
[RefNode
].PredEdges
= new SparseBitVector
<>;
1554 GraphNodes
[RefNode
].PredEdges
->set(C
.Src
);
1557 // Dest = Src edge and *Dest = *Src edge
1558 if (!GraphNodes
[C
.Dest
].PredEdges
)
1559 GraphNodes
[C
.Dest
].PredEdges
= new SparseBitVector
<>;
1560 GraphNodes
[C
.Dest
].PredEdges
->set(C
.Src
);
1561 unsigned RefNode
= C
.Dest
+ FirstRefNode
;
1562 if (!GraphNodes
[RefNode
].ImplicitPredEdges
)
1563 GraphNodes
[RefNode
].ImplicitPredEdges
= new SparseBitVector
<>;
1564 GraphNodes
[RefNode
].ImplicitPredEdges
->set(C
.Src
+ FirstRefNode
);
1568 // Do SCC finding first to condense our predecessor graph
1570 Node2DFS
.insert(Node2DFS
.begin(), GraphNodes
.size(), 0);
1571 Node2Deleted
.insert(Node2Deleted
.begin(), GraphNodes
.size(), false);
1572 Node2Visited
.insert(Node2Visited
.begin(), GraphNodes
.size(), false);
1574 for (unsigned i
= 0; i
< FirstRefNode
; ++i
) {
1575 unsigned Node
= VSSCCRep
[i
];
1576 if (!Node2Visited
[Node
])
1579 for (BitVectorMap::iterator Iter
= Set2PEClass
.begin();
1580 Iter
!= Set2PEClass
.end();
1583 Set2PEClass
.clear();
1585 Node2Deleted
.clear();
1586 Node2Visited
.clear();
1587 DEBUG(errs() << "Finished HVN\n");
1591 /// This is the workhorse of HVN value numbering. We combine SCC finding at the
1592 /// same time because it's easy.
1593 void Andersens::HVNValNum(unsigned NodeIndex
) {
1594 unsigned MyDFS
= DFSNumber
++;
1595 Node
*N
= &GraphNodes
[NodeIndex
];
1596 Node2Visited
[NodeIndex
] = true;
1597 Node2DFS
[NodeIndex
] = MyDFS
;
1599 // First process all our explicit edges
1601 for (SparseBitVector
<>::iterator Iter
= N
->PredEdges
->begin();
1602 Iter
!= N
->PredEdges
->end();
1604 unsigned j
= VSSCCRep
[*Iter
];
1605 if (!Node2Deleted
[j
]) {
1606 if (!Node2Visited
[j
])
1608 if (Node2DFS
[NodeIndex
] > Node2DFS
[j
])
1609 Node2DFS
[NodeIndex
] = Node2DFS
[j
];
1613 // Now process all the implicit edges
1614 if (N
->ImplicitPredEdges
)
1615 for (SparseBitVector
<>::iterator Iter
= N
->ImplicitPredEdges
->begin();
1616 Iter
!= N
->ImplicitPredEdges
->end();
1618 unsigned j
= VSSCCRep
[*Iter
];
1619 if (!Node2Deleted
[j
]) {
1620 if (!Node2Visited
[j
])
1622 if (Node2DFS
[NodeIndex
] > Node2DFS
[j
])
1623 Node2DFS
[NodeIndex
] = Node2DFS
[j
];
1627 // See if we found any cycles
1628 if (MyDFS
== Node2DFS
[NodeIndex
]) {
1629 while (!SCCStack
.empty() && Node2DFS
[SCCStack
.top()] >= MyDFS
) {
1630 unsigned CycleNodeIndex
= SCCStack
.top();
1631 Node
*CycleNode
= &GraphNodes
[CycleNodeIndex
];
1632 VSSCCRep
[CycleNodeIndex
] = NodeIndex
;
1634 N
->Direct
&= CycleNode
->Direct
;
1636 if (CycleNode
->PredEdges
) {
1638 N
->PredEdges
= new SparseBitVector
<>;
1639 *(N
->PredEdges
) |= CycleNode
->PredEdges
;
1640 delete CycleNode
->PredEdges
;
1641 CycleNode
->PredEdges
= NULL
;
1643 if (CycleNode
->ImplicitPredEdges
) {
1644 if (!N
->ImplicitPredEdges
)
1645 N
->ImplicitPredEdges
= new SparseBitVector
<>;
1646 *(N
->ImplicitPredEdges
) |= CycleNode
->ImplicitPredEdges
;
1647 delete CycleNode
->ImplicitPredEdges
;
1648 CycleNode
->ImplicitPredEdges
= NULL
;
1654 Node2Deleted
[NodeIndex
] = true;
1657 GraphNodes
[NodeIndex
].PointerEquivLabel
= PEClass
++;
1661 // Collect labels of successor nodes
1662 bool AllSame
= true;
1663 unsigned First
= ~0;
1664 SparseBitVector
<> *Labels
= new SparseBitVector
<>;
1668 for (SparseBitVector
<>::iterator Iter
= N
->PredEdges
->begin();
1669 Iter
!= N
->PredEdges
->end();
1671 unsigned j
= VSSCCRep
[*Iter
];
1672 unsigned Label
= GraphNodes
[j
].PointerEquivLabel
;
1673 // Ignore labels that are equal to us or non-pointers
1674 if (j
== NodeIndex
|| Label
== 0)
1676 if (First
== (unsigned)~0)
1678 else if (First
!= Label
)
1683 // We either have a non-pointer, a copy of an existing node, or a new node.
1684 // Assign the appropriate pointer equivalence label.
1685 if (Labels
->empty()) {
1686 GraphNodes
[NodeIndex
].PointerEquivLabel
= 0;
1687 } else if (AllSame
) {
1688 GraphNodes
[NodeIndex
].PointerEquivLabel
= First
;
1690 GraphNodes
[NodeIndex
].PointerEquivLabel
= Set2PEClass
[Labels
];
1691 if (GraphNodes
[NodeIndex
].PointerEquivLabel
== 0) {
1692 unsigned EquivClass
= PEClass
++;
1693 Set2PEClass
[Labels
] = EquivClass
;
1694 GraphNodes
[NodeIndex
].PointerEquivLabel
= EquivClass
;
1701 SCCStack
.push(NodeIndex
);
1705 /// The technique used here is described in "Exploiting Pointer and Location
1706 /// Equivalence to Optimize Pointer Analysis. In the 14th International Static
1707 /// Analysis Symposium (SAS), August 2007." It is known as the "HU" algorithm,
1708 /// and is equivalent to value numbering the collapsed constraint graph
1709 /// including evaluating unions.
1710 void Andersens::HU() {
1711 DEBUG(errs() << "Beginning HU\n");
1712 // Build a predecessor graph. This is like our constraint graph with the
1713 // edges going in the opposite direction, and there are edges for all the
1714 // constraints, instead of just copy constraints. We also build implicit
1715 // edges for constraints are implied but not explicit. I.E for the constraint
1716 // a = &b, we add implicit edges *a = b. This helps us capture more cycles
1717 for (unsigned i
= 0, e
= Constraints
.size(); i
!= e
; ++i
) {
1718 Constraint
&C
= Constraints
[i
];
1719 if (C
.Type
== Constraint::AddressOf
) {
1720 GraphNodes
[C
.Src
].AddressTaken
= true;
1721 GraphNodes
[C
.Src
].Direct
= false;
1723 GraphNodes
[C
.Dest
].PointsTo
->set(C
.Src
);
1725 unsigned RefNode
= C
.Dest
+ FirstRefNode
;
1726 if (!GraphNodes
[RefNode
].ImplicitPredEdges
)
1727 GraphNodes
[RefNode
].ImplicitPredEdges
= new SparseBitVector
<>;
1728 GraphNodes
[RefNode
].ImplicitPredEdges
->set(C
.Src
);
1729 GraphNodes
[C
.Src
].PointedToBy
->set(C
.Dest
);
1730 } else if (C
.Type
== Constraint::Load
) {
1731 if (C
.Offset
== 0) {
1733 if (!GraphNodes
[C
.Dest
].PredEdges
)
1734 GraphNodes
[C
.Dest
].PredEdges
= new SparseBitVector
<>;
1735 GraphNodes
[C
.Dest
].PredEdges
->set(C
.Src
+ FirstRefNode
);
1737 GraphNodes
[C
.Dest
].Direct
= false;
1739 } else if (C
.Type
== Constraint::Store
) {
1740 if (C
.Offset
== 0) {
1742 unsigned RefNode
= C
.Dest
+ FirstRefNode
;
1743 if (!GraphNodes
[RefNode
].PredEdges
)
1744 GraphNodes
[RefNode
].PredEdges
= new SparseBitVector
<>;
1745 GraphNodes
[RefNode
].PredEdges
->set(C
.Src
);
1748 // Dest = Src edge and *Dest = *Src edg
1749 if (!GraphNodes
[C
.Dest
].PredEdges
)
1750 GraphNodes
[C
.Dest
].PredEdges
= new SparseBitVector
<>;
1751 GraphNodes
[C
.Dest
].PredEdges
->set(C
.Src
);
1752 unsigned RefNode
= C
.Dest
+ FirstRefNode
;
1753 if (!GraphNodes
[RefNode
].ImplicitPredEdges
)
1754 GraphNodes
[RefNode
].ImplicitPredEdges
= new SparseBitVector
<>;
1755 GraphNodes
[RefNode
].ImplicitPredEdges
->set(C
.Src
+ FirstRefNode
);
1759 // Do SCC finding first to condense our predecessor graph
1761 Node2DFS
.insert(Node2DFS
.begin(), GraphNodes
.size(), 0);
1762 Node2Deleted
.insert(Node2Deleted
.begin(), GraphNodes
.size(), false);
1763 Node2Visited
.insert(Node2Visited
.begin(), GraphNodes
.size(), false);
1765 for (unsigned i
= 0; i
< FirstRefNode
; ++i
) {
1766 if (FindNode(i
) == i
) {
1767 unsigned Node
= VSSCCRep
[i
];
1768 if (!Node2Visited
[Node
])
1773 // Reset tables for actual labeling
1775 Node2Visited
.clear();
1776 Node2Deleted
.clear();
1777 // Pre-grow our densemap so that we don't get really bad behavior
1778 Set2PEClass
.resize(GraphNodes
.size());
1780 // Visit the condensed graph and generate pointer equivalence labels.
1781 Node2Visited
.insert(Node2Visited
.begin(), GraphNodes
.size(), false);
1782 for (unsigned i
= 0; i
< FirstRefNode
; ++i
) {
1783 if (FindNode(i
) == i
) {
1784 unsigned Node
= VSSCCRep
[i
];
1785 if (!Node2Visited
[Node
])
1789 // PEClass nodes will be deleted by the deleting of N->PointsTo in our caller.
1790 Set2PEClass
.clear();
1791 DEBUG(errs() << "Finished HU\n");
1795 /// Implementation of standard Tarjan SCC algorithm as modified by Nuutilla.
1796 void Andersens::Condense(unsigned NodeIndex
) {
1797 unsigned MyDFS
= DFSNumber
++;
1798 Node
*N
= &GraphNodes
[NodeIndex
];
1799 Node2Visited
[NodeIndex
] = true;
1800 Node2DFS
[NodeIndex
] = MyDFS
;
1802 // First process all our explicit edges
1804 for (SparseBitVector
<>::iterator Iter
= N
->PredEdges
->begin();
1805 Iter
!= N
->PredEdges
->end();
1807 unsigned j
= VSSCCRep
[*Iter
];
1808 if (!Node2Deleted
[j
]) {
1809 if (!Node2Visited
[j
])
1811 if (Node2DFS
[NodeIndex
] > Node2DFS
[j
])
1812 Node2DFS
[NodeIndex
] = Node2DFS
[j
];
1816 // Now process all the implicit edges
1817 if (N
->ImplicitPredEdges
)
1818 for (SparseBitVector
<>::iterator Iter
= N
->ImplicitPredEdges
->begin();
1819 Iter
!= N
->ImplicitPredEdges
->end();
1821 unsigned j
= VSSCCRep
[*Iter
];
1822 if (!Node2Deleted
[j
]) {
1823 if (!Node2Visited
[j
])
1825 if (Node2DFS
[NodeIndex
] > Node2DFS
[j
])
1826 Node2DFS
[NodeIndex
] = Node2DFS
[j
];
1830 // See if we found any cycles
1831 if (MyDFS
== Node2DFS
[NodeIndex
]) {
1832 while (!SCCStack
.empty() && Node2DFS
[SCCStack
.top()] >= MyDFS
) {
1833 unsigned CycleNodeIndex
= SCCStack
.top();
1834 Node
*CycleNode
= &GraphNodes
[CycleNodeIndex
];
1835 VSSCCRep
[CycleNodeIndex
] = NodeIndex
;
1837 N
->Direct
&= CycleNode
->Direct
;
1839 *(N
->PointsTo
) |= CycleNode
->PointsTo
;
1840 delete CycleNode
->PointsTo
;
1841 CycleNode
->PointsTo
= NULL
;
1842 if (CycleNode
->PredEdges
) {
1844 N
->PredEdges
= new SparseBitVector
<>;
1845 *(N
->PredEdges
) |= CycleNode
->PredEdges
;
1846 delete CycleNode
->PredEdges
;
1847 CycleNode
->PredEdges
= NULL
;
1849 if (CycleNode
->ImplicitPredEdges
) {
1850 if (!N
->ImplicitPredEdges
)
1851 N
->ImplicitPredEdges
= new SparseBitVector
<>;
1852 *(N
->ImplicitPredEdges
) |= CycleNode
->ImplicitPredEdges
;
1853 delete CycleNode
->ImplicitPredEdges
;
1854 CycleNode
->ImplicitPredEdges
= NULL
;
1859 Node2Deleted
[NodeIndex
] = true;
1861 // Set up number of incoming edges for other nodes
1863 for (SparseBitVector
<>::iterator Iter
= N
->PredEdges
->begin();
1864 Iter
!= N
->PredEdges
->end();
1866 ++GraphNodes
[VSSCCRep
[*Iter
]].NumInEdges
;
1868 SCCStack
.push(NodeIndex
);
1872 void Andersens::HUValNum(unsigned NodeIndex
) {
1873 Node
*N
= &GraphNodes
[NodeIndex
];
1874 Node2Visited
[NodeIndex
] = true;
1876 // Eliminate dereferences of non-pointers for those non-pointers we have
1877 // already identified. These are ref nodes whose non-ref node:
1878 // 1. Has already been visited determined to point to nothing (and thus, a
1879 // dereference of it must point to nothing)
1880 // 2. Any direct node with no predecessor edges in our graph and with no
1881 // points-to set (since it can't point to anything either, being that it
1882 // receives no points-to sets and has none).
1883 if (NodeIndex
>= FirstRefNode
) {
1884 unsigned j
= VSSCCRep
[FindNode(NodeIndex
- FirstRefNode
)];
1885 if ((Node2Visited
[j
] && !GraphNodes
[j
].PointerEquivLabel
)
1886 || (GraphNodes
[j
].Direct
&& !GraphNodes
[j
].PredEdges
1887 && GraphNodes
[j
].PointsTo
->empty())){
1891 // Process all our explicit edges
1893 for (SparseBitVector
<>::iterator Iter
= N
->PredEdges
->begin();
1894 Iter
!= N
->PredEdges
->end();
1896 unsigned j
= VSSCCRep
[*Iter
];
1897 if (!Node2Visited
[j
])
1900 // If this edge turned out to be the same as us, or got no pointer
1901 // equivalence label (and thus points to nothing) , just decrement our
1902 // incoming edges and continue.
1903 if (j
== NodeIndex
|| GraphNodes
[j
].PointerEquivLabel
== 0) {
1904 --GraphNodes
[j
].NumInEdges
;
1908 *(N
->PointsTo
) |= GraphNodes
[j
].PointsTo
;
1910 // If we didn't end up storing this in the hash, and we're done with all
1911 // the edges, we don't need the points-to set anymore.
1912 --GraphNodes
[j
].NumInEdges
;
1913 if (!GraphNodes
[j
].NumInEdges
&& !GraphNodes
[j
].StoredInHash
) {
1914 delete GraphNodes
[j
].PointsTo
;
1915 GraphNodes
[j
].PointsTo
= NULL
;
1918 // If this isn't a direct node, generate a fresh variable.
1920 N
->PointsTo
->set(FirstRefNode
+ NodeIndex
);
1923 // See If we have something equivalent to us, if not, generate a new
1924 // equivalence class.
1925 if (N
->PointsTo
->empty()) {
1930 N
->PointerEquivLabel
= Set2PEClass
[N
->PointsTo
];
1931 if (N
->PointerEquivLabel
== 0) {
1932 unsigned EquivClass
= PEClass
++;
1933 N
->StoredInHash
= true;
1934 Set2PEClass
[N
->PointsTo
] = EquivClass
;
1935 N
->PointerEquivLabel
= EquivClass
;
1938 N
->PointerEquivLabel
= PEClass
++;
1943 /// Rewrite our list of constraints so that pointer equivalent nodes are
1944 /// replaced by their the pointer equivalence class representative.
1945 void Andersens::RewriteConstraints() {
1946 std::vector
<Constraint
> NewConstraints
;
1947 DenseSet
<Constraint
, ConstraintKeyInfo
> Seen
;
1949 PEClass2Node
.clear();
1950 PENLEClass2Node
.clear();
1952 // We may have from 1 to Graphnodes + 1 equivalence classes.
1953 PEClass2Node
.insert(PEClass2Node
.begin(), GraphNodes
.size() + 1, -1);
1954 PENLEClass2Node
.insert(PENLEClass2Node
.begin(), GraphNodes
.size() + 1, -1);
1956 // Rewrite constraints, ignoring non-pointer constraints, uniting equivalent
1957 // nodes, and rewriting constraints to use the representative nodes.
1958 for (unsigned i
= 0, e
= Constraints
.size(); i
!= e
; ++i
) {
1959 Constraint
&C
= Constraints
[i
];
1960 unsigned RHSNode
= FindNode(C
.Src
);
1961 unsigned LHSNode
= FindNode(C
.Dest
);
1962 unsigned RHSLabel
= GraphNodes
[VSSCCRep
[RHSNode
]].PointerEquivLabel
;
1963 unsigned LHSLabel
= GraphNodes
[VSSCCRep
[LHSNode
]].PointerEquivLabel
;
1965 // First we try to eliminate constraints for things we can prove don't point
1967 if (LHSLabel
== 0) {
1968 DEBUG(PrintNode(&GraphNodes
[LHSNode
]));
1969 DEBUG(errs() << " is a non-pointer, ignoring constraint.\n");
1972 if (RHSLabel
== 0) {
1973 DEBUG(PrintNode(&GraphNodes
[RHSNode
]));
1974 DEBUG(errs() << " is a non-pointer, ignoring constraint.\n");
1977 // This constraint may be useless, and it may become useless as we translate
1979 if (C
.Src
== C
.Dest
&& C
.Type
== Constraint::Copy
)
1982 C
.Src
= FindEquivalentNode(RHSNode
, RHSLabel
);
1983 C
.Dest
= FindEquivalentNode(FindNode(LHSNode
), LHSLabel
);
1984 if ((C
.Src
== C
.Dest
&& C
.Type
== Constraint::Copy
)
1989 NewConstraints
.push_back(C
);
1991 Constraints
.swap(NewConstraints
);
1992 PEClass2Node
.clear();
1995 /// See if we have a node that is pointer equivalent to the one being asked
1996 /// about, and if so, unite them and return the equivalent node. Otherwise,
1997 /// return the original node.
1998 unsigned Andersens::FindEquivalentNode(unsigned NodeIndex
,
1999 unsigned NodeLabel
) {
2000 if (!GraphNodes
[NodeIndex
].AddressTaken
) {
2001 if (PEClass2Node
[NodeLabel
] != -1) {
2002 // We found an existing node with the same pointer label, so unify them.
2003 // We specifically request that Union-By-Rank not be used so that
2004 // PEClass2Node[NodeLabel] U= NodeIndex and not the other way around.
2005 return UniteNodes(PEClass2Node
[NodeLabel
], NodeIndex
, false);
2007 PEClass2Node
[NodeLabel
] = NodeIndex
;
2008 PENLEClass2Node
[NodeLabel
] = NodeIndex
;
2010 } else if (PENLEClass2Node
[NodeLabel
] == -1) {
2011 PENLEClass2Node
[NodeLabel
] = NodeIndex
;
2017 void Andersens::PrintLabels() const {
2018 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2019 if (i
< FirstRefNode
) {
2020 PrintNode(&GraphNodes
[i
]);
2021 } else if (i
< FirstAdrNode
) {
2022 DEBUG(errs() << "REF(");
2023 PrintNode(&GraphNodes
[i
-FirstRefNode
]);
2024 DEBUG(errs() <<")");
2026 DEBUG(errs() << "ADR(");
2027 PrintNode(&GraphNodes
[i
-FirstAdrNode
]);
2028 DEBUG(errs() <<")");
2031 DEBUG(errs() << " has pointer label " << GraphNodes
[i
].PointerEquivLabel
2032 << " and SCC rep " << VSSCCRep
[i
]
2033 << " and is " << (GraphNodes
[i
].Direct
? "Direct" : "Not direct")
2038 /// The technique used here is described in "The Ant and the
2039 /// Grasshopper: Fast and Accurate Pointer Analysis for Millions of
2040 /// Lines of Code. In Programming Language Design and Implementation
2041 /// (PLDI), June 2007." It is known as the "HCD" (Hybrid Cycle
2042 /// Detection) algorithm. It is called a hybrid because it performs an
2043 /// offline analysis and uses its results during the solving (online)
2044 /// phase. This is just the offline portion; the results of this
2045 /// operation are stored in SDT and are later used in SolveContraints()
2046 /// and UniteNodes().
2047 void Andersens::HCD() {
2048 DEBUG(errs() << "Starting HCD.\n");
2049 HCDSCCRep
.resize(GraphNodes
.size());
2051 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2052 GraphNodes
[i
].Edges
= new SparseBitVector
<>;
2056 for (unsigned i
= 0, e
= Constraints
.size(); i
!= e
; ++i
) {
2057 Constraint
&C
= Constraints
[i
];
2058 assert (C
.Src
< GraphNodes
.size() && C
.Dest
< GraphNodes
.size());
2059 if (C
.Type
== Constraint::AddressOf
) {
2061 } else if (C
.Type
== Constraint::Load
) {
2063 GraphNodes
[C
.Dest
].Edges
->set(C
.Src
+ FirstRefNode
);
2064 } else if (C
.Type
== Constraint::Store
) {
2066 GraphNodes
[C
.Dest
+ FirstRefNode
].Edges
->set(C
.Src
);
2068 GraphNodes
[C
.Dest
].Edges
->set(C
.Src
);
2072 Node2DFS
.insert(Node2DFS
.begin(), GraphNodes
.size(), 0);
2073 Node2Deleted
.insert(Node2Deleted
.begin(), GraphNodes
.size(), false);
2074 Node2Visited
.insert(Node2Visited
.begin(), GraphNodes
.size(), false);
2075 SDT
.insert(SDT
.begin(), GraphNodes
.size() / 2, -1);
2078 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2079 unsigned Node
= HCDSCCRep
[i
];
2080 if (!Node2Deleted
[Node
])
2084 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
)
2085 if (GraphNodes
[i
].Edges
!= NULL
) {
2086 delete GraphNodes
[i
].Edges
;
2087 GraphNodes
[i
].Edges
= NULL
;
2090 while( !SCCStack
.empty() )
2094 Node2Visited
.clear();
2095 Node2Deleted
.clear();
2097 DEBUG(errs() << "HCD complete.\n");
2100 // Component of HCD:
2101 // Use Nuutila's variant of Tarjan's algorithm to detect
2102 // Strongly-Connected Components (SCCs). For non-trivial SCCs
2103 // containing ref nodes, insert the appropriate information in SDT.
2104 void Andersens::Search(unsigned Node
) {
2105 unsigned MyDFS
= DFSNumber
++;
2107 Node2Visited
[Node
] = true;
2108 Node2DFS
[Node
] = MyDFS
;
2110 for (SparseBitVector
<>::iterator Iter
= GraphNodes
[Node
].Edges
->begin(),
2111 End
= GraphNodes
[Node
].Edges
->end();
2114 unsigned J
= HCDSCCRep
[*Iter
];
2115 assert(GraphNodes
[J
].isRep() && "Debug check; must be representative");
2116 if (!Node2Deleted
[J
]) {
2117 if (!Node2Visited
[J
])
2119 if (Node2DFS
[Node
] > Node2DFS
[J
])
2120 Node2DFS
[Node
] = Node2DFS
[J
];
2124 if( MyDFS
!= Node2DFS
[Node
] ) {
2125 SCCStack
.push(Node
);
2129 // This node is the root of a SCC, so process it.
2131 // If the SCC is "non-trivial" (not a singleton) and contains a reference
2132 // node, we place this SCC into SDT. We unite the nodes in any case.
2133 if (!SCCStack
.empty() && Node2DFS
[SCCStack
.top()] >= MyDFS
) {
2134 SparseBitVector
<> SCC
;
2138 bool Ref
= (Node
>= FirstRefNode
);
2140 Node2Deleted
[Node
] = true;
2143 unsigned P
= SCCStack
.top(); SCCStack
.pop();
2144 Ref
|= (P
>= FirstRefNode
);
2146 HCDSCCRep
[P
] = Node
;
2147 } while (!SCCStack
.empty() && Node2DFS
[SCCStack
.top()] >= MyDFS
);
2150 unsigned Rep
= SCC
.find_first();
2151 assert(Rep
< FirstRefNode
&& "The SCC didn't have a non-Ref node!");
2153 SparseBitVector
<>::iterator i
= SCC
.begin();
2155 // Skip over the non-ref nodes
2156 while( *i
< FirstRefNode
)
2159 while( i
!= SCC
.end() )
2160 SDT
[ (*i
++) - FirstRefNode
] = Rep
;
2166 /// Optimize the constraints by performing offline variable substitution and
2167 /// other optimizations.
2168 void Andersens::OptimizeConstraints() {
2169 DEBUG(errs() << "Beginning constraint optimization\n");
2173 // Function related nodes need to stay in the same relative position and can't
2174 // be location equivalent.
2175 for (std::map
<unsigned, unsigned>::iterator Iter
= MaxK
.begin();
2178 for (unsigned i
= Iter
->first
;
2179 i
!= Iter
->first
+ Iter
->second
;
2181 GraphNodes
[i
].AddressTaken
= true;
2182 GraphNodes
[i
].Direct
= false;
2186 ClumpAddressTaken();
2187 FirstRefNode
= GraphNodes
.size();
2188 FirstAdrNode
= FirstRefNode
+ GraphNodes
.size();
2189 GraphNodes
.insert(GraphNodes
.end(), 2 * GraphNodes
.size(),
2191 VSSCCRep
.resize(GraphNodes
.size());
2192 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2196 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2197 Node
*N
= &GraphNodes
[i
];
2198 delete N
->PredEdges
;
2199 N
->PredEdges
= NULL
;
2200 delete N
->ImplicitPredEdges
;
2201 N
->ImplicitPredEdges
= NULL
;
2204 #define DEBUG_TYPE "anders-aa-labels"
2205 DEBUG(PrintLabels());
2207 #define DEBUG_TYPE "anders-aa"
2208 RewriteConstraints();
2209 // Delete the adr nodes.
2210 GraphNodes
.resize(FirstRefNode
* 2);
2213 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2214 Node
*N
= &GraphNodes
[i
];
2215 if (FindNode(i
) == i
) {
2216 N
->PointsTo
= new SparseBitVector
<>;
2217 N
->PointedToBy
= new SparseBitVector
<>;
2221 N
->PointerEquivLabel
= 0;
2225 #define DEBUG_TYPE "anders-aa-labels"
2226 DEBUG(PrintLabels());
2228 #define DEBUG_TYPE "anders-aa"
2229 RewriteConstraints();
2230 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2231 if (FindNode(i
) == i
) {
2232 Node
*N
= &GraphNodes
[i
];
2235 delete N
->PredEdges
;
2236 N
->PredEdges
= NULL
;
2237 delete N
->ImplicitPredEdges
;
2238 N
->ImplicitPredEdges
= NULL
;
2239 delete N
->PointedToBy
;
2240 N
->PointedToBy
= NULL
;
2244 // perform Hybrid Cycle Detection (HCD)
2248 // No longer any need for the upper half of GraphNodes (for ref nodes).
2249 GraphNodes
.erase(GraphNodes
.begin() + FirstRefNode
, GraphNodes
.end());
2253 DEBUG(errs() << "Finished constraint optimization\n");
2258 /// Unite pointer but not location equivalent variables, now that the constraint
2260 void Andersens::UnitePointerEquivalences() {
2261 DEBUG(errs() << "Uniting remaining pointer equivalences\n");
2262 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2263 if (GraphNodes
[i
].AddressTaken
&& GraphNodes
[i
].isRep()) {
2264 unsigned Label
= GraphNodes
[i
].PointerEquivLabel
;
2266 if (Label
&& PENLEClass2Node
[Label
] != -1)
2267 UniteNodes(i
, PENLEClass2Node
[Label
]);
2270 DEBUG(errs() << "Finished remaining pointer equivalences\n");
2271 PENLEClass2Node
.clear();
2274 /// Create the constraint graph used for solving points-to analysis.
2276 void Andersens::CreateConstraintGraph() {
2277 for (unsigned i
= 0, e
= Constraints
.size(); i
!= e
; ++i
) {
2278 Constraint
&C
= Constraints
[i
];
2279 assert (C
.Src
< GraphNodes
.size() && C
.Dest
< GraphNodes
.size());
2280 if (C
.Type
== Constraint::AddressOf
)
2281 GraphNodes
[C
.Dest
].PointsTo
->set(C
.Src
);
2282 else if (C
.Type
== Constraint::Load
)
2283 GraphNodes
[C
.Src
].Constraints
.push_back(C
);
2284 else if (C
.Type
== Constraint::Store
)
2285 GraphNodes
[C
.Dest
].Constraints
.push_back(C
);
2286 else if (C
.Offset
!= 0)
2287 GraphNodes
[C
.Src
].Constraints
.push_back(C
);
2289 GraphNodes
[C
.Src
].Edges
->set(C
.Dest
);
2293 // Perform DFS and cycle detection.
2294 bool Andersens::QueryNode(unsigned Node
) {
2295 assert(GraphNodes
[Node
].isRep() && "Querying a non-rep node");
2296 unsigned OurDFS
= ++DFSNumber
;
2297 SparseBitVector
<> ToErase
;
2298 SparseBitVector
<> NewEdges
;
2299 Tarjan2DFS
[Node
] = OurDFS
;
2301 // Changed denotes a change from a recursive call that we will bubble up.
2302 // Merged is set if we actually merge a node ourselves.
2303 bool Changed
= false, Merged
= false;
2305 for (SparseBitVector
<>::iterator bi
= GraphNodes
[Node
].Edges
->begin();
2306 bi
!= GraphNodes
[Node
].Edges
->end();
2308 unsigned RepNode
= FindNode(*bi
);
2309 // If this edge points to a non-representative node but we are
2310 // already planning to add an edge to its representative, we have no
2311 // need for this edge anymore.
2312 if (RepNode
!= *bi
&& NewEdges
.test(RepNode
)){
2317 // Continue about our DFS.
2318 if (!Tarjan2Deleted
[RepNode
]){
2319 if (Tarjan2DFS
[RepNode
] == 0) {
2320 Changed
|= QueryNode(RepNode
);
2321 // May have been changed by QueryNode
2322 RepNode
= FindNode(RepNode
);
2324 if (Tarjan2DFS
[RepNode
] < Tarjan2DFS
[Node
])
2325 Tarjan2DFS
[Node
] = Tarjan2DFS
[RepNode
];
2328 // We may have just discovered that this node is part of a cycle, in
2329 // which case we can also erase it.
2330 if (RepNode
!= *bi
) {
2332 NewEdges
.set(RepNode
);
2336 GraphNodes
[Node
].Edges
->intersectWithComplement(ToErase
);
2337 GraphNodes
[Node
].Edges
|= NewEdges
;
2339 // If this node is a root of a non-trivial SCC, place it on our
2340 // worklist to be processed.
2341 if (OurDFS
== Tarjan2DFS
[Node
]) {
2342 while (!SCCStack
.empty() && Tarjan2DFS
[SCCStack
.top()] >= OurDFS
) {
2343 Node
= UniteNodes(Node
, SCCStack
.top());
2348 Tarjan2Deleted
[Node
] = true;
2351 NextWL
->insert(&GraphNodes
[Node
]);
2353 SCCStack
.push(Node
);
2356 return(Changed
| Merged
);
2359 /// SolveConstraints - This stage iteratively processes the constraints list
2360 /// propagating constraints (adding edges to the Nodes in the points-to graph)
2361 /// until a fixed point is reached.
2363 /// We use a variant of the technique called "Lazy Cycle Detection", which is
2364 /// described in "The Ant and the Grasshopper: Fast and Accurate Pointer
2365 /// Analysis for Millions of Lines of Code. In Programming Language Design and
2366 /// Implementation (PLDI), June 2007."
2367 /// The paper describes performing cycle detection one node at a time, which can
2368 /// be expensive if there are no cycles, but there are long chains of nodes that
2369 /// it heuristically believes are cycles (because it will DFS from each node
2370 /// without state from previous nodes).
2371 /// Instead, we use the heuristic to build a worklist of nodes to check, then
2372 /// cycle detect them all at the same time to do this more cheaply. This
2373 /// catches cycles slightly later than the original technique did, but does it
2374 /// make significantly cheaper.
2376 void Andersens::SolveConstraints() {
2380 OptimizeConstraints();
2382 #define DEBUG_TYPE "anders-aa-constraints"
2383 DEBUG(PrintConstraints());
2385 #define DEBUG_TYPE "anders-aa"
2387 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2388 Node
*N
= &GraphNodes
[i
];
2389 N
->PointsTo
= new SparseBitVector
<>;
2390 N
->OldPointsTo
= new SparseBitVector
<>;
2391 N
->Edges
= new SparseBitVector
<>;
2393 CreateConstraintGraph();
2394 UnitePointerEquivalences();
2395 assert(SCCStack
.empty() && "SCC Stack should be empty by now!");
2397 Node2Deleted
.clear();
2398 Node2DFS
.insert(Node2DFS
.begin(), GraphNodes
.size(), 0);
2399 Node2Deleted
.insert(Node2Deleted
.begin(), GraphNodes
.size(), false);
2401 DenseSet
<Constraint
, ConstraintKeyInfo
> Seen
;
2402 DenseSet
<std::pair
<unsigned,unsigned>, PairKeyInfo
> EdgesChecked
;
2404 // Order graph and add initial nodes to work list.
2405 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2406 Node
*INode
= &GraphNodes
[i
];
2408 // Add to work list if it's a representative and can contribute to the
2409 // calculation right now.
2410 if (INode
->isRep() && !INode
->PointsTo
->empty()
2411 && (!INode
->Edges
->empty() || !INode
->Constraints
.empty())) {
2413 CurrWL
->insert(INode
);
2416 std::queue
<unsigned int> TarjanWL
;
2418 // "Rep and special variables" - in order for HCD to maintain conservative
2419 // results when !FULL_UNIVERSAL, we need to treat the special variables in
2420 // the same way that the !FULL_UNIVERSAL tweak does throughout the rest of
2421 // the analysis - it's ok to add edges from the special nodes, but never
2422 // *to* the special nodes.
2423 std::vector
<unsigned int> RSV
;
2425 while( !CurrWL
->empty() ) {
2426 DEBUG(errs() << "Starting iteration #" << ++NumIters
<< "\n");
2429 unsigned CurrNodeIndex
;
2431 // Actual cycle checking code. We cycle check all of the lazy cycle
2432 // candidates from the last iteration in one go.
2433 if (!TarjanWL
.empty()) {
2437 Tarjan2Deleted
.clear();
2438 while (!TarjanWL
.empty()) {
2439 unsigned int ToTarjan
= TarjanWL
.front();
2441 if (!Tarjan2Deleted
[ToTarjan
]
2442 && GraphNodes
[ToTarjan
].isRep()
2443 && Tarjan2DFS
[ToTarjan
] == 0)
2444 QueryNode(ToTarjan
);
2448 // Add to work list if it's a representative and can contribute to the
2449 // calculation right now.
2450 while( (CurrNode
= CurrWL
->pop()) != NULL
) {
2451 CurrNodeIndex
= CurrNode
- &GraphNodes
[0];
2455 // Figure out the changed points to bits
2456 SparseBitVector
<> CurrPointsTo
;
2457 CurrPointsTo
.intersectWithComplement(CurrNode
->PointsTo
,
2458 CurrNode
->OldPointsTo
);
2459 if (CurrPointsTo
.empty())
2462 *(CurrNode
->OldPointsTo
) |= CurrPointsTo
;
2464 // Check the offline-computed equivalencies from HCD.
2468 if (SDT
[CurrNodeIndex
] >= 0) {
2470 Rep
= FindNode(SDT
[CurrNodeIndex
]);
2475 for (SparseBitVector
<>::iterator bi
= CurrPointsTo
.begin();
2476 bi
!= CurrPointsTo
.end(); ++bi
) {
2477 unsigned Node
= FindNode(*bi
);
2479 if (Node
< NumberSpecialNodes
) {
2480 RSV
.push_back(Node
);
2484 Rep
= UniteNodes(Rep
,Node
);
2490 NextWL
->insert(&GraphNodes
[Rep
]);
2492 if ( ! CurrNode
->isRep() )
2498 /* Now process the constraints for this node. */
2499 for (std::list
<Constraint
>::iterator li
= CurrNode
->Constraints
.begin();
2500 li
!= CurrNode
->Constraints
.end(); ) {
2501 li
->Src
= FindNode(li
->Src
);
2502 li
->Dest
= FindNode(li
->Dest
);
2504 // Delete redundant constraints
2505 if( Seen
.count(*li
) ) {
2506 std::list
<Constraint
>::iterator lk
= li
; li
++;
2508 CurrNode
->Constraints
.erase(lk
);
2514 // Src and Dest will be the vars we are going to process.
2515 // This may look a bit ugly, but what it does is allow us to process
2516 // both store and load constraints with the same code.
2517 // Load constraints say that every member of our RHS solution has K
2518 // added to it, and that variable gets an edge to LHS. We also union
2519 // RHS+K's solution into the LHS solution.
2520 // Store constraints say that every member of our LHS solution has K
2521 // added to it, and that variable gets an edge from RHS. We also union
2522 // RHS's solution into the LHS+K solution.
2525 unsigned K
= li
->Offset
;
2526 unsigned CurrMember
;
2527 if (li
->Type
== Constraint::Load
) {
2530 } else if (li
->Type
== Constraint::Store
) {
2534 // TODO Handle offseted copy constraint
2539 // See if we can use Hybrid Cycle Detection (that is, check
2540 // if it was a statically detected offline equivalence that
2541 // involves pointers; if so, remove the redundant constraints).
2542 if( SCC
&& K
== 0 ) {
2546 if (GraphNodes
[*Src
].Edges
->test_and_set(*Dest
))
2547 if (GraphNodes
[*Dest
].PointsTo
|= *(GraphNodes
[*Src
].PointsTo
))
2548 NextWL
->insert(&GraphNodes
[*Dest
]);
2550 for (unsigned i
=0; i
< RSV
.size(); ++i
) {
2551 CurrMember
= RSV
[i
];
2553 if (*Dest
< NumberSpecialNodes
)
2555 if (GraphNodes
[*Src
].Edges
->test_and_set(*Dest
))
2556 if (GraphNodes
[*Dest
].PointsTo
|= *(GraphNodes
[*Src
].PointsTo
))
2557 NextWL
->insert(&GraphNodes
[*Dest
]);
2560 // since all future elements of the points-to set will be
2561 // equivalent to the current ones, the complex constraints
2562 // become redundant.
2564 std::list
<Constraint
>::iterator lk
= li
; li
++;
2566 // In this case, we can still erase the constraints when the
2567 // elements of the points-to sets are referenced by *Dest,
2568 // but not when they are referenced by *Src (i.e. for a Load
2569 // constraint). This is because if another special variable is
2570 // put into the points-to set later, we still need to add the
2571 // new edge from that special variable.
2572 if( lk
->Type
!= Constraint::Load
)
2574 GraphNodes
[CurrNodeIndex
].Constraints
.erase(lk
);
2576 const SparseBitVector
<> &Solution
= CurrPointsTo
;
2578 for (SparseBitVector
<>::iterator bi
= Solution
.begin();
2579 bi
!= Solution
.end();
2583 // Need to increment the member by K since that is where we are
2584 // supposed to copy to/from. Note that in positive weight cycles,
2585 // which occur in address taking of fields, K can go past
2586 // MaxK[CurrMember] elements, even though that is all it could point
2588 if (K
> 0 && K
> MaxK
[CurrMember
])
2591 CurrMember
= FindNode(CurrMember
+ K
);
2593 // Add an edge to the graph, so we can just do regular
2594 // bitmap ior next time. It may also let us notice a cycle.
2596 if (*Dest
< NumberSpecialNodes
)
2599 if (GraphNodes
[*Src
].Edges
->test_and_set(*Dest
))
2600 if (GraphNodes
[*Dest
].PointsTo
|= *(GraphNodes
[*Src
].PointsTo
))
2601 NextWL
->insert(&GraphNodes
[*Dest
]);
2607 SparseBitVector
<> NewEdges
;
2608 SparseBitVector
<> ToErase
;
2610 // Now all we have left to do is propagate points-to info along the
2611 // edges, erasing the redundant edges.
2612 for (SparseBitVector
<>::iterator bi
= CurrNode
->Edges
->begin();
2613 bi
!= CurrNode
->Edges
->end();
2616 unsigned DestVar
= *bi
;
2617 unsigned Rep
= FindNode(DestVar
);
2619 // If we ended up with this node as our destination, or we've already
2620 // got an edge for the representative, delete the current edge.
2621 if (Rep
== CurrNodeIndex
||
2622 (Rep
!= DestVar
&& NewEdges
.test(Rep
))) {
2623 ToErase
.set(DestVar
);
2627 std::pair
<unsigned,unsigned> edge(CurrNodeIndex
,Rep
);
2629 // This is where we do lazy cycle detection.
2630 // If this is a cycle candidate (equal points-to sets and this
2631 // particular edge has not been cycle-checked previously), add to the
2632 // list to check for cycles on the next iteration.
2633 if (!EdgesChecked
.count(edge
) &&
2634 *(GraphNodes
[Rep
].PointsTo
) == *(CurrNode
->PointsTo
)) {
2635 EdgesChecked
.insert(edge
);
2638 // Union the points-to sets into the dest
2640 if (Rep
>= NumberSpecialNodes
)
2642 if (GraphNodes
[Rep
].PointsTo
|= CurrPointsTo
) {
2643 NextWL
->insert(&GraphNodes
[Rep
]);
2645 // If this edge's destination was collapsed, rewrite the edge.
2646 if (Rep
!= DestVar
) {
2647 ToErase
.set(DestVar
);
2651 CurrNode
->Edges
->intersectWithComplement(ToErase
);
2652 CurrNode
->Edges
|= NewEdges
;
2655 // Switch to other work list.
2656 WorkList
* t
= CurrWL
; CurrWL
= NextWL
; NextWL
= t
;
2661 Node2Deleted
.clear();
2662 for (unsigned i
= 0; i
< GraphNodes
.size(); ++i
) {
2663 Node
*N
= &GraphNodes
[i
];
2664 delete N
->OldPointsTo
;
2671 //===----------------------------------------------------------------------===//
2673 //===----------------------------------------------------------------------===//
2675 // Unite nodes First and Second, returning the one which is now the
2676 // representative node. First and Second are indexes into GraphNodes
2677 unsigned Andersens::UniteNodes(unsigned First
, unsigned Second
,
2679 assert (First
< GraphNodes
.size() && Second
< GraphNodes
.size() &&
2680 "Attempting to merge nodes that don't exist");
2682 Node
*FirstNode
= &GraphNodes
[First
];
2683 Node
*SecondNode
= &GraphNodes
[Second
];
2685 assert (SecondNode
->isRep() && FirstNode
->isRep() &&
2686 "Trying to unite two non-representative nodes!");
2687 if (First
== Second
)
2691 int RankFirst
= (int) FirstNode
->NodeRep
;
2692 int RankSecond
= (int) SecondNode
->NodeRep
;
2694 // Rank starts at -1 and gets decremented as it increases.
2695 // Translation: higher rank, lower NodeRep value, which is always negative.
2696 if (RankFirst
> RankSecond
) {
2697 unsigned t
= First
; First
= Second
; Second
= t
;
2698 Node
* tp
= FirstNode
; FirstNode
= SecondNode
; SecondNode
= tp
;
2699 } else if (RankFirst
== RankSecond
) {
2700 FirstNode
->NodeRep
= (unsigned) (RankFirst
- 1);
2704 SecondNode
->NodeRep
= First
;
2706 if (First
>= NumberSpecialNodes
)
2708 if (FirstNode
->PointsTo
&& SecondNode
->PointsTo
)
2709 FirstNode
->PointsTo
|= *(SecondNode
->PointsTo
);
2710 if (FirstNode
->Edges
&& SecondNode
->Edges
)
2711 FirstNode
->Edges
|= *(SecondNode
->Edges
);
2712 if (!SecondNode
->Constraints
.empty())
2713 FirstNode
->Constraints
.splice(FirstNode
->Constraints
.begin(),
2714 SecondNode
->Constraints
);
2715 if (FirstNode
->OldPointsTo
) {
2716 delete FirstNode
->OldPointsTo
;
2717 FirstNode
->OldPointsTo
= new SparseBitVector
<>;
2720 // Destroy interesting parts of the merged-from node.
2721 delete SecondNode
->OldPointsTo
;
2722 delete SecondNode
->Edges
;
2723 delete SecondNode
->PointsTo
;
2724 SecondNode
->Edges
= NULL
;
2725 SecondNode
->PointsTo
= NULL
;
2726 SecondNode
->OldPointsTo
= NULL
;
2729 DEBUG(errs() << "Unified Node ");
2730 DEBUG(PrintNode(FirstNode
));
2731 DEBUG(errs() << " and Node ");
2732 DEBUG(PrintNode(SecondNode
));
2733 DEBUG(errs() << "\n");
2736 if (SDT
[Second
] >= 0) {
2738 SDT
[First
] = SDT
[Second
];
2740 UniteNodes( FindNode(SDT
[First
]), FindNode(SDT
[Second
]) );
2741 First
= FindNode(First
);
2748 // Find the index into GraphNodes of the node representing Node, performing
2749 // path compression along the way
2750 unsigned Andersens::FindNode(unsigned NodeIndex
) {
2751 assert (NodeIndex
< GraphNodes
.size()
2752 && "Attempting to find a node that can't exist");
2753 Node
*N
= &GraphNodes
[NodeIndex
];
2757 return (N
->NodeRep
= FindNode(N
->NodeRep
));
2760 // Find the index into GraphNodes of the node representing Node,
2761 // don't perform path compression along the way (for Print)
2762 unsigned Andersens::FindNode(unsigned NodeIndex
) const {
2763 assert (NodeIndex
< GraphNodes
.size()
2764 && "Attempting to find a node that can't exist");
2765 const Node
*N
= &GraphNodes
[NodeIndex
];
2769 return FindNode(N
->NodeRep
);
2772 //===----------------------------------------------------------------------===//
2774 //===----------------------------------------------------------------------===//
2776 void Andersens::PrintNode(const Node
*N
) const {
2777 if (N
== &GraphNodes
[UniversalSet
]) {
2778 errs() << "<universal>";
2780 } else if (N
== &GraphNodes
[NullPtr
]) {
2781 errs() << "<nullptr>";
2783 } else if (N
== &GraphNodes
[NullObject
]) {
2787 if (!N
->getValue()) {
2788 errs() << "artificial" << (intptr_t) N
;
2792 assert(N
->getValue() != 0 && "Never set node label!");
2793 Value
*V
= N
->getValue();
2794 if (Function
*F
= dyn_cast
<Function
>(V
)) {
2795 if (isa
<PointerType
>(F
->getFunctionType()->getReturnType()) &&
2796 N
== &GraphNodes
[getReturnNode(F
)]) {
2797 errs() << F
->getName() << ":retval";
2799 } else if (F
->getFunctionType()->isVarArg() &&
2800 N
== &GraphNodes
[getVarargNode(F
)]) {
2801 errs() << F
->getName() << ":vararg";
2806 if (Instruction
*I
= dyn_cast
<Instruction
>(V
))
2807 errs() << I
->getParent()->getParent()->getName() << ":";
2808 else if (Argument
*Arg
= dyn_cast
<Argument
>(V
))
2809 errs() << Arg
->getParent()->getName() << ":";
2812 errs() << V
->getName();
2814 errs() << "(unnamed)";
2816 if (isa
<GlobalValue
>(V
) || isa
<AllocationInst
>(V
))
2817 if (N
== &GraphNodes
[getObject(V
)])
2820 void Andersens::PrintConstraint(const Constraint
&C
) const {
2821 if (C
.Type
== Constraint::Store
) {
2826 PrintNode(&GraphNodes
[C
.Dest
]);
2827 if (C
.Type
== Constraint::Store
&& C
.Offset
!= 0)
2828 errs() << " + " << C
.Offset
<< ")";
2830 if (C
.Type
== Constraint::Load
) {
2835 else if (C
.Type
== Constraint::AddressOf
)
2837 PrintNode(&GraphNodes
[C
.Src
]);
2838 if (C
.Offset
!= 0 && C
.Type
!= Constraint::Store
)
2839 errs() << " + " << C
.Offset
;
2840 if (C
.Type
== Constraint::Load
&& C
.Offset
!= 0)
2845 void Andersens::PrintConstraints() const {
2846 errs() << "Constraints:\n";
2848 for (unsigned i
= 0, e
= Constraints
.size(); i
!= e
; ++i
)
2849 PrintConstraint(Constraints
[i
]);
2852 void Andersens::PrintPointsToGraph() const {
2853 errs() << "Points-to graph:\n";
2854 for (unsigned i
= 0, e
= GraphNodes
.size(); i
!= e
; ++i
) {
2855 const Node
*N
= &GraphNodes
[i
];
2856 if (FindNode(i
) != i
) {
2858 errs() << "\t--> same as ";
2859 PrintNode(&GraphNodes
[FindNode(i
)]);
2862 errs() << "[" << (N
->PointsTo
->count()) << "] ";
2867 for (SparseBitVector
<>::iterator bi
= N
->PointsTo
->begin();
2868 bi
!= N
->PointsTo
->end();
2872 PrintNode(&GraphNodes
[*bi
]);