2004-06-25 04:24:22 +00:00
|
|
|
//===- Andersens.cpp - Andersen's Interprocedural Alias Analysis ----------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-05-23 21:00:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-05-23 21:00:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-09-16 21:45:02 +00:00
|
|
|
// This file defines an implementation of Andersen's interprocedural alias
|
|
|
|
// analysis
|
2004-05-23 21:00:47 +00:00
|
|
|
//
|
|
|
|
// In pointer analysis terms, this is a subset-based, flow-insensitive,
|
2007-09-16 21:45:02 +00:00
|
|
|
// field-sensitive, and context-insensitive algorithm pointer algorithm.
|
2004-05-23 21:00:47 +00:00
|
|
|
//
|
|
|
|
// This algorithm is implemented as three stages:
|
|
|
|
// 1. Object identification.
|
|
|
|
// 2. Inclusion constraint identification.
|
|
|
|
// 3. Inclusion constraint solving.
|
|
|
|
//
|
|
|
|
// The object identification stage identifies all of the memory objects in the
|
|
|
|
// program, which includes globals, heap allocated objects, and stack allocated
|
|
|
|
// objects.
|
|
|
|
//
|
|
|
|
// The inclusion constraint identification stage finds all inclusion constraints
|
|
|
|
// in the program by scanning the program, looking for pointer assignments and
|
|
|
|
// other statements that effect the points-to graph. For a statement like "A =
|
|
|
|
// B", this statement is processed to indicate that A can point to anything that
|
2007-09-16 21:45:02 +00:00
|
|
|
// B can point to. Constraints can handle copies, loads, and stores, and
|
|
|
|
// address taking.
|
2004-05-23 21:00:47 +00:00
|
|
|
//
|
|
|
|
// The inclusion constraint solving phase iteratively propagates the inclusion
|
|
|
|
// constraints until a fixed point is reached. This is an O(N^3) algorithm.
|
|
|
|
//
|
2007-09-16 21:45:02 +00:00
|
|
|
// Function constraints are handled as if they were structs with X fields.
|
|
|
|
// Thus, an access to argument X of function Y is an access to node index
|
|
|
|
// getNode(Y) + X. This representation allows handling of indirect calls
|
|
|
|
// without any issues. To wit, an indirect call Y(a,b) is equivalence to
|
|
|
|
// *(Y + 1) = a, *(Y + 2) = b.
|
|
|
|
// The return node for a function is always located at getNode(F) +
|
|
|
|
// CallReturnPos. The arguments start at getNode(F) + CallArgPos.
|
2004-05-23 21:00:47 +00:00
|
|
|
//
|
2004-06-05 20:12:36 +00:00
|
|
|
// Future Improvements:
|
2007-09-16 21:45:02 +00:00
|
|
|
// Offline variable substitution, offline detection of online
|
|
|
|
// cycles. Use of BDD's.
|
2004-05-23 21:00:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "anders-aa"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2007-02-05 23:42:17 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-05-23 21:00:47 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
|
|
|
#include "llvm/Support/InstVisitor.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2005-01-08 22:01:16 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-09-16 21:45:02 +00:00
|
|
|
#include "llvm/ADT/SparseBitVector.h"
|
2007-03-05 00:00:42 +00:00
|
|
|
#include <algorithm>
|
2004-05-23 21:00:47 +00:00
|
|
|
#include <set>
|
2007-09-16 21:45:02 +00:00
|
|
|
#include <list>
|
|
|
|
#include <stack>
|
|
|
|
#include <vector>
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
using namespace llvm;
|
2006-12-19 22:30:33 +00:00
|
|
|
STATISTIC(NumIters , "Number of iterations to reach convergence");
|
|
|
|
STATISTIC(NumConstraints , "Number of constraints");
|
|
|
|
STATISTIC(NumNodes , "Number of nodes");
|
2007-09-16 21:45:02 +00:00
|
|
|
STATISTIC(NumUnified , "Number of variables unified");
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2006-12-19 22:30:33 +00:00
|
|
|
namespace {
|
2007-09-16 21:45:02 +00:00
|
|
|
const unsigned SelfRep = (unsigned)-1;
|
|
|
|
const unsigned Unvisited = (unsigned)-1;
|
|
|
|
// Position of the function return node relative to the function node.
|
|
|
|
const unsigned CallReturnPos = 2;
|
|
|
|
// Position of the function call node relative to the function node.
|
|
|
|
const unsigned CallFirstArgPos = 3;
|
|
|
|
|
2007-02-05 23:42:17 +00:00
|
|
|
class VISIBILITY_HIDDEN Andersens : public ModulePass, public AliasAnalysis,
|
|
|
|
private InstVisitor<Andersens> {
|
2007-09-16 21:45:02 +00:00
|
|
|
class Node;
|
|
|
|
|
|
|
|
/// Constraint - Objects of this structure are used to represent the various
|
|
|
|
/// constraints identified by the algorithm. The constraints are 'copy',
|
|
|
|
/// for statements like "A = B", 'load' for statements like "A = *B",
|
|
|
|
/// 'store' for statements like "*A = B", and AddressOf for statements like
|
|
|
|
/// A = alloca; The Offset is applied as *(A + K) = B for stores,
|
|
|
|
/// A = *(B + K) for loads, and A = B + K for copies. It is
|
|
|
|
/// illegal on addressof constraints (Because it is statically
|
|
|
|
/// resolvable to A = &C where C = B + K)
|
|
|
|
|
|
|
|
struct Constraint {
|
|
|
|
enum ConstraintType { Copy, Load, Store, AddressOf } Type;
|
|
|
|
unsigned Dest;
|
|
|
|
unsigned Src;
|
|
|
|
unsigned Offset;
|
|
|
|
|
|
|
|
Constraint(ConstraintType Ty, unsigned D, unsigned S, unsigned O = 0)
|
|
|
|
: Type(Ty), Dest(D), Src(S), Offset(O) {
|
|
|
|
assert(Offset == 0 || Ty != AddressOf &&
|
|
|
|
"Offset is illegal on addressof constraints");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Node class - This class is used to represent a node
|
|
|
|
// in the constraint graph. Due to various optimizations,
|
|
|
|
// not always the case that there is a mapping from a Node to a
|
|
|
|
// Value. In particular, we add artificial
|
|
|
|
// Node's that represent the set of pointed-to variables
|
|
|
|
// shared for each location equivalent Node.
|
|
|
|
struct Node {
|
|
|
|
Value *Val;
|
|
|
|
SparseBitVector<> *Edges;
|
|
|
|
SparseBitVector<> *PointsTo;
|
|
|
|
SparseBitVector<> *OldPointsTo;
|
|
|
|
bool Changed;
|
|
|
|
std::list<Constraint> Constraints;
|
|
|
|
|
|
|
|
// Nodes in cycles (or in equivalence classes) are united
|
|
|
|
// together using a standard union-find representation with path
|
|
|
|
// compression. NodeRep gives the index into GraphNodes
|
|
|
|
// representative for this one.
|
|
|
|
unsigned NodeRep; public:
|
|
|
|
|
|
|
|
Node() : Val(0), Edges(0), PointsTo(0), OldPointsTo(0), Changed(false),
|
|
|
|
NodeRep(SelfRep) {
|
|
|
|
}
|
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
Node *setValue(Value *V) {
|
|
|
|
assert(Val == 0 && "Value already set for this node!");
|
|
|
|
Val = V;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getValue - Return the LLVM value corresponding to this node.
|
2005-03-28 04:03:52 +00:00
|
|
|
///
|
2004-05-23 21:00:47 +00:00
|
|
|
Value *getValue() const { return Val; }
|
|
|
|
|
|
|
|
/// addPointerTo - Add a pointer to the list of pointees of this node,
|
|
|
|
/// returning true if this caused a new pointer to be added, or false if
|
|
|
|
/// we already knew about the points-to relation.
|
2007-09-16 21:45:02 +00:00
|
|
|
bool addPointerTo(unsigned Node) {
|
|
|
|
return PointsTo->test_and_set(Node);
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// intersects - Return true if the points-to set of this node intersects
|
|
|
|
/// with the points-to set of the specified node.
|
|
|
|
bool intersects(Node *N) const;
|
|
|
|
|
|
|
|
/// intersectsIgnoring - Return true if the points-to set of this node
|
|
|
|
/// intersects with the points-to set of the specified node on any nodes
|
|
|
|
/// except for the specified node to ignore.
|
2007-09-16 21:45:02 +00:00
|
|
|
bool intersectsIgnoring(Node *N, unsigned) const;
|
2004-05-23 21:00:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// GraphNodes - This vector is populated as part of the object
|
|
|
|
/// identification stage of the analysis, which populates this vector with a
|
|
|
|
/// node for each memory object and fills in the ValueNodes map.
|
|
|
|
std::vector<Node> GraphNodes;
|
|
|
|
|
|
|
|
/// ValueNodes - This map indicates the Node that a particular Value* is
|
|
|
|
/// represented by. This contains entries for all pointers.
|
|
|
|
std::map<Value*, unsigned> ValueNodes;
|
|
|
|
|
|
|
|
/// ObjectNodes - This map contains entries for each memory object in the
|
2005-04-21 21:13:18 +00:00
|
|
|
/// program: globals, alloca's and mallocs.
|
2004-05-23 21:00:47 +00:00
|
|
|
std::map<Value*, unsigned> ObjectNodes;
|
|
|
|
|
|
|
|
/// ReturnNodes - This map contains an entry for each function in the
|
|
|
|
/// program that returns a value.
|
|
|
|
std::map<Function*, unsigned> ReturnNodes;
|
|
|
|
|
|
|
|
/// VarargNodes - This map contains the entry used to represent all pointers
|
|
|
|
/// passed through the varargs portion of a function call for a particular
|
|
|
|
/// function. An entry is not present in this map for functions that do not
|
|
|
|
/// take variable arguments.
|
|
|
|
std::map<Function*, unsigned> VarargNodes;
|
|
|
|
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
/// Constraints - This vector contains a list of all of the constraints
|
|
|
|
/// identified by the program.
|
|
|
|
std::vector<Constraint> Constraints;
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
// Map from graph node to maximum K value that is allowed (For functions,
|
|
|
|
// this is equivalent to the number of arguments + CallFirstArgPos)
|
|
|
|
std::map<unsigned, unsigned> MaxK;
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
/// This enum defines the GraphNodes indices that correspond to important
|
|
|
|
/// fixed sets.
|
|
|
|
enum {
|
|
|
|
UniversalSet = 0,
|
|
|
|
NullPtr = 1,
|
2006-05-24 17:04:05 +00:00
|
|
|
NullObject = 2
|
2004-05-23 21:00:47 +00:00
|
|
|
};
|
2007-09-16 21:45:02 +00:00
|
|
|
// Stack for Tarjans
|
|
|
|
std::stack<unsigned> SCCStack;
|
|
|
|
// Topological Index -> Graph node
|
|
|
|
std::vector<unsigned> Topo2Node;
|
|
|
|
// Graph Node -> Topological Index;
|
|
|
|
std::vector<unsigned> Node2Topo;
|
|
|
|
// Map from Graph Node to DFS number
|
|
|
|
std::vector<unsigned> Node2DFS;
|
|
|
|
// Map from Graph Node to Deleted from graph.
|
|
|
|
std::vector<bool> Node2Deleted;
|
|
|
|
// Current DFS and RPO numbers
|
|
|
|
unsigned DFSNumber;
|
|
|
|
unsigned RPONumber;
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
public:
|
2007-09-16 21:45:02 +00:00
|
|
|
static char ID;
|
|
|
|
Andersens() : ModulePass((intptr_t)&ID) {}
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool runOnModule(Module &M) {
|
2004-05-23 21:00:47 +00:00
|
|
|
InitializeAliasAnalysis(this);
|
|
|
|
IdentifyObjects(M);
|
|
|
|
CollectConstraints(M);
|
|
|
|
DEBUG(PrintConstraints());
|
|
|
|
SolveConstraints();
|
|
|
|
DEBUG(PrintPointsToGraph());
|
|
|
|
|
|
|
|
// Free the constraints list, as we don't need it to respond to alias
|
|
|
|
// requests.
|
|
|
|
ObjectNodes.clear();
|
|
|
|
ReturnNodes.clear();
|
|
|
|
VarargNodes.clear();
|
2005-04-21 21:13:18 +00:00
|
|
|
std::vector<Constraint>().swap(Constraints);
|
2004-05-23 21:00:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void releaseMemory() {
|
|
|
|
// FIXME: Until we have transitively required passes working correctly,
|
|
|
|
// this cannot be enabled! Otherwise, using -count-aa with the pass
|
|
|
|
// causes memory to be freed too early. :(
|
|
|
|
#if 0
|
|
|
|
// The memory objects and ValueNodes data structures at the only ones that
|
|
|
|
// are still live after construction.
|
|
|
|
std::vector<Node>().swap(GraphNodes);
|
|
|
|
ValueNodes.clear();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AliasAnalysis::getAnalysisUsage(AU);
|
|
|
|
AU.setPreservesAll(); // Does not transform code
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the AliasAnalysis API
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-05-23 21:00:47 +00:00
|
|
|
AliasResult alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size);
|
2006-08-28 01:02:49 +00:00
|
|
|
virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
|
|
|
|
virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
|
2004-05-23 21:00:47 +00:00
|
|
|
void getMustAliases(Value *P, std::vector<Value*> &RetVals);
|
|
|
|
bool pointsToConstantMemory(const Value *P);
|
|
|
|
|
|
|
|
virtual void deleteValue(Value *V) {
|
|
|
|
ValueNodes.erase(V);
|
|
|
|
getAnalysis<AliasAnalysis>().deleteValue(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void copyValue(Value *From, Value *To) {
|
|
|
|
ValueNodes[To] = ValueNodes[From];
|
|
|
|
getAnalysis<AliasAnalysis>().copyValue(From, To);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// getNode - Return the node corresponding to the specified pointer scalar.
|
|
|
|
///
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned getNode(Value *V) {
|
2004-05-23 21:00:47 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(V))
|
2004-08-16 05:38:02 +00:00
|
|
|
if (!isa<GlobalValue>(C))
|
|
|
|
return getNodeForConstantPointer(C);
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
std::map<Value*, unsigned>::iterator I = ValueNodes.find(V);
|
|
|
|
if (I == ValueNodes.end()) {
|
2006-07-11 18:25:13 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
V->dump();
|
|
|
|
#endif
|
2006-07-11 17:58:07 +00:00
|
|
|
assert(0 && "Value does not have a node in the points-to graph!");
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
2007-09-16 21:45:02 +00:00
|
|
|
return I->second;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
/// getObject - Return the node corresponding to the memory object for the
|
|
|
|
/// specified global or allocation instruction.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned getObject(Value *V) {
|
2004-05-23 21:00:47 +00:00
|
|
|
std::map<Value*, unsigned>::iterator I = ObjectNodes.find(V);
|
|
|
|
assert(I != ObjectNodes.end() &&
|
|
|
|
"Value does not have an object in the points-to graph!");
|
2007-09-16 21:45:02 +00:00
|
|
|
return I->second;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getReturnNode - Return the node representing the return value for the
|
|
|
|
/// specified function.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned getReturnNode(Function *F) {
|
2004-05-23 21:00:47 +00:00
|
|
|
std::map<Function*, unsigned>::iterator I = ReturnNodes.find(F);
|
|
|
|
assert(I != ReturnNodes.end() && "Function does not return a value!");
|
2007-09-16 21:45:02 +00:00
|
|
|
return I->second;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getVarargNode - Return the node representing the variable arguments
|
|
|
|
/// formal for the specified function.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned getVarargNode(Function *F) {
|
2004-05-23 21:00:47 +00:00
|
|
|
std::map<Function*, unsigned>::iterator I = VarargNodes.find(F);
|
|
|
|
assert(I != VarargNodes.end() && "Function does not take var args!");
|
2007-09-16 21:45:02 +00:00
|
|
|
return I->second;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getNodeValue - Get the node for the specified LLVM value and set the
|
|
|
|
/// value for it to be the specified value.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned getNodeValue(Value &V) {
|
|
|
|
unsigned Index = getNode(&V);
|
|
|
|
GraphNodes[Index].setValue(&V);
|
|
|
|
return Index;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned UniteNodes(unsigned First, unsigned Second);
|
|
|
|
unsigned FindNode(unsigned Node);
|
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
void IdentifyObjects(Module &M);
|
|
|
|
void CollectConstraints(Module &M);
|
2007-09-16 21:45:02 +00:00
|
|
|
bool AnalyzeUsesOfFunction(Value *);
|
|
|
|
void CreateConstraintGraph();
|
2004-05-23 21:00:47 +00:00
|
|
|
void SolveConstraints();
|
2007-09-16 21:45:02 +00:00
|
|
|
void QueryNode(unsigned Node);
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned getNodeForConstantPointer(Constant *C);
|
|
|
|
unsigned getNodeForConstantPointerTarget(Constant *C);
|
|
|
|
void AddGlobalInitializerConstraints(unsigned, Constant *C);
|
2005-03-28 04:03:52 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
void AddConstraintsForNonInternalLinkage(Function *F);
|
|
|
|
void AddConstraintsForCall(CallSite CS, Function *F);
|
2005-03-29 06:09:07 +00:00
|
|
|
bool AddConstraintsForExternalCall(CallSite CS, Function *F);
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
void PrintNode(Node *N);
|
|
|
|
void PrintConstraints();
|
|
|
|
void PrintPointsToGraph();
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// Instruction visitation methods for adding constraints
|
|
|
|
//
|
|
|
|
friend class InstVisitor<Andersens>;
|
|
|
|
void visitReturnInst(ReturnInst &RI);
|
|
|
|
void visitInvokeInst(InvokeInst &II) { visitCallSite(CallSite(&II)); }
|
|
|
|
void visitCallInst(CallInst &CI) { visitCallSite(CallSite(&CI)); }
|
|
|
|
void visitCallSite(CallSite CS);
|
|
|
|
void visitAllocationInst(AllocationInst &AI);
|
|
|
|
void visitLoadInst(LoadInst &LI);
|
|
|
|
void visitStoreInst(StoreInst &SI);
|
|
|
|
void visitGetElementPtrInst(GetElementPtrInst &GEP);
|
|
|
|
void visitPHINode(PHINode &PN);
|
|
|
|
void visitCastInst(CastInst &CI);
|
2006-12-23 06:05:41 +00:00
|
|
|
void visitICmpInst(ICmpInst &ICI) {} // NOOP!
|
|
|
|
void visitFCmpInst(FCmpInst &ICI) {} // NOOP!
|
2004-05-23 21:00:47 +00:00
|
|
|
void visitSelectInst(SelectInst &SI);
|
|
|
|
void visitVAArg(VAArgInst &I);
|
|
|
|
void visitInstruction(Instruction &I);
|
2007-09-16 21:45:02 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char Andersens::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<Andersens> X("anders-aa",
|
|
|
|
"Andersen's Interprocedural Alias Analysis");
|
2006-08-28 00:42:29 +00:00
|
|
|
RegisterAnalysisGroup<AliasAnalysis> Y(X);
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
2005-01-08 22:01:16 +00:00
|
|
|
ModulePass *llvm::createAndersensPass() { return new Andersens(); }
|
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AliasAnalysis Interface Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
AliasAnalysis::AliasResult Andersens::alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size) {
|
2007-09-16 21:45:02 +00:00
|
|
|
Node *N1 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V1)))];
|
|
|
|
Node *N2 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V2)))];
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
// Check to see if the two pointers are known to not alias. They don't alias
|
|
|
|
// if their points-to sets do not intersect.
|
2007-09-16 21:45:02 +00:00
|
|
|
if (!N1->intersectsIgnoring(N2, NullObject))
|
2004-05-23 21:00:47 +00:00
|
|
|
return NoAlias;
|
|
|
|
|
|
|
|
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
|
|
|
|
}
|
|
|
|
|
2005-03-28 06:21:17 +00:00
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
Andersens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
|
|
|
// The only thing useful that we can contribute for mod/ref information is
|
|
|
|
// when calling external function calls: if we know that memory never escapes
|
|
|
|
// from the program, it cannot be modified by an external call.
|
|
|
|
//
|
|
|
|
// NOTE: This is not really safe, at least not when the entire program is not
|
|
|
|
// available. The deal is that the external function could call back into the
|
|
|
|
// program and modify stuff. We ignore this technical niggle for now. This
|
|
|
|
// is, after all, a "research quality" implementation of Andersen's analysis.
|
|
|
|
if (Function *F = CS.getCalledFunction())
|
2007-01-30 20:08:39 +00:00
|
|
|
if (F->isDeclaration()) {
|
2007-09-16 21:45:02 +00:00
|
|
|
Node *N1 = &GraphNodes[FindNode(getNode(P))];
|
2005-03-28 06:21:17 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
if (N1->PointsTo->empty())
|
|
|
|
return NoModRef;
|
2005-03-28 06:21:17 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
if (!N1->PointsTo->test(UniversalSet))
|
2005-03-28 06:21:17 +00:00
|
|
|
return NoModRef; // P doesn't point to the universal set.
|
|
|
|
}
|
|
|
|
|
|
|
|
return AliasAnalysis::getModRefInfo(CS, P, Size);
|
|
|
|
}
|
2005-03-28 04:03:52 +00:00
|
|
|
|
2006-08-28 01:02:49 +00:00
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
Andersens::getModRefInfo(CallSite CS1, CallSite CS2) {
|
|
|
|
return AliasAnalysis::getModRefInfo(CS1,CS2);
|
|
|
|
}
|
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
/// getMustAlias - We can provide must alias information if we know that a
|
|
|
|
/// pointer can only point to a specific function or the null pointer.
|
|
|
|
/// Unfortunately we cannot determine must-alias information for global
|
|
|
|
/// variables or any other memory memory objects because we do not track whether
|
|
|
|
/// a pointer points to the beginning of an object or a field of it.
|
|
|
|
void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
|
2007-09-16 21:45:02 +00:00
|
|
|
Node *N = &GraphNodes[FindNode(getNode(P))];
|
|
|
|
if (N->PointsTo->count() == 1) {
|
|
|
|
Node *Pointee = &GraphNodes[N->PointsTo->find_first()];
|
|
|
|
// If a function is the only object in the points-to set, then it must be
|
|
|
|
// the destination. Note that we can't handle global variables here,
|
|
|
|
// because we don't know if the pointer is actually pointing to a field of
|
|
|
|
// the global or to the beginning of it.
|
|
|
|
if (Value *V = Pointee->getValue()) {
|
|
|
|
if (Function *F = dyn_cast<Function>(V))
|
|
|
|
RetVals.push_back(F);
|
|
|
|
} else {
|
|
|
|
// If the object in the points-to set is the null object, then the null
|
|
|
|
// pointer is a must alias.
|
|
|
|
if (Pointee == &GraphNodes[NullObject])
|
|
|
|
RetVals.push_back(Constant::getNullValue(P->getType()));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AliasAnalysis::getMustAliases(P, RetVals);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// pointsToConstantMemory - If we can determine that this pointer only points
|
|
|
|
/// to constant memory, return true. In practice, this means that if the
|
|
|
|
/// pointer can only point to constant globals, functions, or the null pointer,
|
|
|
|
/// return true.
|
|
|
|
///
|
|
|
|
bool Andersens::pointsToConstantMemory(const Value *P) {
|
2007-09-16 21:45:02 +00:00
|
|
|
Node *N = &GraphNodes[FindNode(getNode((Value*)P))];
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (SparseBitVector<>::iterator bi = N->PointsTo->begin();
|
|
|
|
bi != N->PointsTo->end();
|
|
|
|
++bi) {
|
|
|
|
i = *bi;
|
|
|
|
Node *Pointee = &GraphNodes[i];
|
|
|
|
if (Value *V = Pointee->getValue()) {
|
2004-05-23 21:00:47 +00:00
|
|
|
if (!isa<GlobalValue>(V) || (isa<GlobalVariable>(V) &&
|
|
|
|
!cast<GlobalVariable>(V)->isConstant()))
|
|
|
|
return AliasAnalysis::pointsToConstantMemory(P);
|
|
|
|
} else {
|
2007-09-16 21:45:02 +00:00
|
|
|
if (i != NullObject)
|
2004-05-23 21:00:47 +00:00
|
|
|
return AliasAnalysis::pointsToConstantMemory(P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Object Identification Phase
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// IdentifyObjects - This stage scans the program, adding an entry to the
|
|
|
|
/// GraphNodes list for each memory object in the program (global stack or
|
|
|
|
/// heap), and populates the ValueNodes and ObjectNodes maps for these objects.
|
|
|
|
///
|
|
|
|
void Andersens::IdentifyObjects(Module &M) {
|
|
|
|
unsigned NumObjects = 0;
|
|
|
|
|
|
|
|
// Object #0 is always the universal set: the object that we don't know
|
|
|
|
// anything about.
|
|
|
|
assert(NumObjects == UniversalSet && "Something changed!");
|
|
|
|
++NumObjects;
|
|
|
|
|
|
|
|
// Object #1 always represents the null pointer.
|
|
|
|
assert(NumObjects == NullPtr && "Something changed!");
|
|
|
|
++NumObjects;
|
|
|
|
|
|
|
|
// Object #2 always represents the null object (the object pointed to by null)
|
|
|
|
assert(NumObjects == NullObject && "Something changed!");
|
|
|
|
++NumObjects;
|
|
|
|
|
|
|
|
// Add all the globals first.
|
2005-03-27 22:03:46 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2004-05-23 21:00:47 +00:00
|
|
|
ObjectNodes[I] = NumObjects++;
|
|
|
|
ValueNodes[I] = NumObjects++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add nodes for all of the functions and the instructions inside of them.
|
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
|
|
|
// The function itself is a memory object.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned First = NumObjects;
|
2004-05-23 21:00:47 +00:00
|
|
|
ValueNodes[F] = NumObjects++;
|
|
|
|
ObjectNodes[F] = NumObjects++;
|
|
|
|
if (isa<PointerType>(F->getFunctionType()->getReturnType()))
|
|
|
|
ReturnNodes[F] = NumObjects++;
|
|
|
|
if (F->getFunctionType()->isVarArg())
|
|
|
|
VarargNodes[F] = NumObjects++;
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
// Add nodes for all of the incoming pointer arguments.
|
2005-03-27 22:03:46 +00:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
I != E; ++I)
|
2004-05-23 21:00:47 +00:00
|
|
|
if (isa<PointerType>(I->getType()))
|
|
|
|
ValueNodes[I] = NumObjects++;
|
2007-09-16 21:45:02 +00:00
|
|
|
MaxK[First] = NumObjects - First;
|
|
|
|
MaxK[First + 1] = NumObjects - First - 1;
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
// Scan the function body, creating a memory object for each heap/stack
|
|
|
|
// allocation in the body of the function and a node to represent all
|
|
|
|
// pointer values defined by instructions and used as operands.
|
|
|
|
for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
|
|
|
|
// If this is an heap or stack allocation, create a node for the memory
|
|
|
|
// object.
|
|
|
|
if (isa<PointerType>(II->getType())) {
|
|
|
|
ValueNodes[&*II] = NumObjects++;
|
|
|
|
if (AllocationInst *AI = dyn_cast<AllocationInst>(&*II))
|
|
|
|
ObjectNodes[AI] = NumObjects++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know how many objects to create, make them all now!
|
|
|
|
GraphNodes.resize(NumObjects);
|
|
|
|
NumNodes += NumObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Constraint Identification Phase
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// getNodeForConstantPointer - Return the node corresponding to the constant
|
|
|
|
/// pointer itself.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned Andersens::getNodeForConstantPointer(Constant *C) {
|
2004-05-23 21:00:47 +00:00
|
|
|
assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
|
|
|
|
|
2005-03-27 18:58:23 +00:00
|
|
|
if (isa<ConstantPointerNull>(C) || isa<UndefValue>(C))
|
2007-09-16 21:45:02 +00:00
|
|
|
return NullPtr;
|
2004-07-18 00:18:30 +00:00
|
|
|
else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
|
|
|
|
return getNode(GV);
|
2004-05-23 21:00:47 +00:00
|
|
|
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
return getNodeForConstantPointer(CE->getOperand(0));
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::IntToPtr:
|
2007-09-16 21:45:02 +00:00
|
|
|
return UniversalSet;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::BitCast:
|
|
|
|
return getNodeForConstantPointer(CE->getOperand(0));
|
2004-05-23 21:00:47 +00:00
|
|
|
default:
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Constant Expr not yet handled: " << *CE << "\n";
|
2004-05-23 21:00:47 +00:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown constant pointer!");
|
|
|
|
}
|
2004-05-27 20:57:01 +00:00
|
|
|
return 0;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getNodeForConstantPointerTarget - Return the node POINTED TO by the
|
|
|
|
/// specified constant pointer.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned Andersens::getNodeForConstantPointerTarget(Constant *C) {
|
2004-05-23 21:00:47 +00:00
|
|
|
assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
|
|
|
|
|
|
|
|
if (isa<ConstantPointerNull>(C))
|
2007-09-16 21:45:02 +00:00
|
|
|
return NullObject;
|
2004-07-18 00:18:30 +00:00
|
|
|
else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
|
|
|
|
return getObject(GV);
|
2004-05-23 21:00:47 +00:00
|
|
|
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
return getNodeForConstantPointerTarget(CE->getOperand(0));
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::IntToPtr:
|
2007-09-16 21:45:02 +00:00
|
|
|
return UniversalSet;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::BitCast:
|
|
|
|
return getNodeForConstantPointerTarget(CE->getOperand(0));
|
2004-05-23 21:00:47 +00:00
|
|
|
default:
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Constant Expr not yet handled: " << *CE << "\n";
|
2004-05-23 21:00:47 +00:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown constant pointer!");
|
|
|
|
}
|
2004-05-27 20:57:01 +00:00
|
|
|
return 0;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AddGlobalInitializerConstraints - Add inclusion constraints for the memory
|
|
|
|
/// object N, which contains values indicated by C.
|
2007-09-16 21:45:02 +00:00
|
|
|
void Andersens::AddGlobalInitializerConstraints(unsigned NodeIndex,
|
|
|
|
Constant *C) {
|
2004-05-23 21:00:47 +00:00
|
|
|
if (C->getType()->isFirstClassType()) {
|
|
|
|
if (isa<PointerType>(C->getType()))
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, NodeIndex,
|
|
|
|
getNodeForConstantPointer(C)));
|
2004-05-23 21:00:47 +00:00
|
|
|
} else if (C->isNullValue()) {
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, NodeIndex,
|
|
|
|
NullObject));
|
2004-05-23 21:00:47 +00:00
|
|
|
return;
|
2005-03-29 06:09:07 +00:00
|
|
|
} else if (!isa<UndefValue>(C)) {
|
2004-05-23 21:00:47 +00:00
|
|
|
// If this is an array or struct, include constraints for each element.
|
|
|
|
assert(isa<ConstantArray>(C) || isa<ConstantStruct>(C));
|
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
2007-09-16 21:45:02 +00:00
|
|
|
AddGlobalInitializerConstraints(NodeIndex,
|
|
|
|
cast<Constant>(C->getOperand(i)));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-28 04:03:52 +00:00
|
|
|
/// AddConstraintsForNonInternalLinkage - If this function does not have
|
|
|
|
/// internal linkage, realize that we can't trust anything passed into or
|
|
|
|
/// returned by this function.
|
2004-05-23 21:00:47 +00:00
|
|
|
void Andersens::AddConstraintsForNonInternalLinkage(Function *F) {
|
2005-03-15 04:54:21 +00:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
|
2004-05-23 21:00:47 +00:00
|
|
|
if (isa<PointerType>(I->getType()))
|
|
|
|
// If this is an argument of an externally accessible function, the
|
|
|
|
// incoming pointer might point to anything.
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, getNode(I),
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
2005-03-29 06:09:07 +00:00
|
|
|
/// AddConstraintsForCall - If this is a call to a "known" function, add the
|
|
|
|
/// constraints and return true. If this is a call to an unknown function,
|
|
|
|
/// return false.
|
|
|
|
bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) {
|
2007-01-30 20:08:39 +00:00
|
|
|
assert(F->isDeclaration() && "Not an external function!");
|
2005-03-28 04:03:52 +00:00
|
|
|
|
|
|
|
// These functions don't induce any points-to constraints.
|
2005-03-29 20:36:05 +00:00
|
|
|
if (F->getName() == "atoi" || F->getName() == "atof" ||
|
|
|
|
F->getName() == "atol" || F->getName() == "atoll" ||
|
|
|
|
F->getName() == "remove" || F->getName() == "unlink" ||
|
|
|
|
F->getName() == "rename" || F->getName() == "memcmp" ||
|
2006-03-03 01:21:36 +00:00
|
|
|
F->getName() == "llvm.memset.i32" ||
|
|
|
|
F->getName() == "llvm.memset.i64" ||
|
2005-03-29 20:36:05 +00:00
|
|
|
F->getName() == "strcmp" || F->getName() == "strncmp" ||
|
|
|
|
F->getName() == "execl" || F->getName() == "execlp" ||
|
|
|
|
F->getName() == "execle" || F->getName() == "execv" ||
|
|
|
|
F->getName() == "execvp" || F->getName() == "chmod" ||
|
|
|
|
F->getName() == "puts" || F->getName() == "write" ||
|
|
|
|
F->getName() == "open" || F->getName() == "create" ||
|
|
|
|
F->getName() == "truncate" || F->getName() == "chdir" ||
|
|
|
|
F->getName() == "mkdir" || F->getName() == "rmdir" ||
|
|
|
|
F->getName() == "read" || F->getName() == "pipe" ||
|
|
|
|
F->getName() == "wait" || F->getName() == "time" ||
|
|
|
|
F->getName() == "stat" || F->getName() == "fstat" ||
|
|
|
|
F->getName() == "lstat" || F->getName() == "strtod" ||
|
|
|
|
F->getName() == "strtof" || F->getName() == "strtold" ||
|
|
|
|
F->getName() == "fopen" || F->getName() == "fdopen" ||
|
|
|
|
F->getName() == "freopen" ||
|
|
|
|
F->getName() == "fflush" || F->getName() == "feof" ||
|
|
|
|
F->getName() == "fileno" || F->getName() == "clearerr" ||
|
|
|
|
F->getName() == "rewind" || F->getName() == "ftell" ||
|
|
|
|
F->getName() == "ferror" || F->getName() == "fgetc" ||
|
|
|
|
F->getName() == "fgetc" || F->getName() == "_IO_getc" ||
|
|
|
|
F->getName() == "fwrite" || F->getName() == "fread" ||
|
|
|
|
F->getName() == "fgets" || F->getName() == "ungetc" ||
|
|
|
|
F->getName() == "fputc" ||
|
|
|
|
F->getName() == "fputs" || F->getName() == "putc" ||
|
|
|
|
F->getName() == "ftell" || F->getName() == "rewind" ||
|
|
|
|
F->getName() == "_IO_putc" || F->getName() == "fseek" ||
|
|
|
|
F->getName() == "fgetpos" || F->getName() == "fsetpos" ||
|
|
|
|
F->getName() == "printf" || F->getName() == "fprintf" ||
|
|
|
|
F->getName() == "sprintf" || F->getName() == "vprintf" ||
|
|
|
|
F->getName() == "vfprintf" || F->getName() == "vsprintf" ||
|
|
|
|
F->getName() == "scanf" || F->getName() == "fscanf" ||
|
|
|
|
F->getName() == "sscanf" || F->getName() == "__assert_fail" ||
|
|
|
|
F->getName() == "modf")
|
2005-03-29 06:09:07 +00:00
|
|
|
return true;
|
2005-03-28 04:03:52 +00:00
|
|
|
|
2005-03-29 20:36:05 +00:00
|
|
|
|
2005-03-28 04:03:52 +00:00
|
|
|
// These functions do induce points-to edges.
|
2007-09-16 21:45:02 +00:00
|
|
|
if (F->getName() == "llvm.memcpy.i32" || F->getName() == "llvm.memcpy.i64" ||
|
2006-03-03 01:21:36 +00:00
|
|
|
F->getName() == "llvm.memmove.i32" ||F->getName() == "llvm.memmove.i64" ||
|
2005-03-29 06:52:20 +00:00
|
|
|
F->getName() == "memmove") {
|
2007-09-16 21:45:02 +00:00
|
|
|
|
|
|
|
// *Dest = *Src, which requires an artificial graph node to represent the
|
|
|
|
// constraint. It is broken up into *Dest = temp, temp = *Src
|
|
|
|
unsigned FirstArg = getNode(CS.getArgument(0));
|
|
|
|
unsigned SecondArg = getNode(CS.getArgument(1));
|
|
|
|
unsigned TempArg = GraphNodes.size();
|
|
|
|
GraphNodes.push_back(Node());
|
|
|
|
Constraints.push_back(Constraint(Constraint::Store,
|
|
|
|
FirstArg, TempArg));
|
|
|
|
Constraints.push_back(Constraint(Constraint::Load,
|
|
|
|
TempArg, SecondArg));
|
2005-03-29 06:09:07 +00:00
|
|
|
return true;
|
2005-03-28 04:03:52 +00:00
|
|
|
}
|
|
|
|
|
2005-03-29 20:04:24 +00:00
|
|
|
// Result = Arg0
|
|
|
|
if (F->getName() == "realloc" || F->getName() == "strchr" ||
|
|
|
|
F->getName() == "strrchr" || F->getName() == "strstr" ||
|
|
|
|
F->getName() == "strtok") {
|
2005-03-29 06:09:07 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Copy,
|
|
|
|
getNode(CS.getInstruction()),
|
|
|
|
getNode(CS.getArgument(0))));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2005-03-28 04:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
/// AnalyzeUsesOfFunction - Look at all of the users of the specified function.
|
|
|
|
/// If this is used by anything complex (i.e., the address escapes), return
|
|
|
|
/// true.
|
|
|
|
bool Andersens::AnalyzeUsesOfFunction(Value *V) {
|
|
|
|
|
|
|
|
if (!isa<PointerType>(V->getType())) return true;
|
|
|
|
|
|
|
|
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
|
|
|
|
if (dyn_cast<LoadInst>(*UI)) {
|
|
|
|
return false;
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
|
|
|
|
if (V == SI->getOperand(1)) {
|
|
|
|
return false;
|
|
|
|
} else if (SI->getOperand(1)) {
|
|
|
|
return true; // Storing the pointer
|
|
|
|
}
|
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
|
|
|
|
if (AnalyzeUsesOfFunction(GEP)) return true;
|
|
|
|
} else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
|
|
|
|
// Make sure that this is just the function being called, not that it is
|
|
|
|
// passing into the function.
|
|
|
|
for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
|
|
|
|
if (CI->getOperand(i) == V) return true;
|
|
|
|
} else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
|
|
|
|
// Make sure that this is just the function being called, not that it is
|
|
|
|
// passing into the function.
|
|
|
|
for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i)
|
|
|
|
if (II->getOperand(i) == V) return true;
|
|
|
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr ||
|
|
|
|
CE->getOpcode() == Instruction::BitCast) {
|
|
|
|
if (AnalyzeUsesOfFunction(CE))
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) {
|
|
|
|
if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
|
|
|
|
return true; // Allow comparison against null.
|
|
|
|
} else if (dyn_cast<FreeInst>(*UI)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
/// CollectConstraints - This stage scans the program, adding a constraint to
|
|
|
|
/// the Constraints list for each instruction in the program that induces a
|
|
|
|
/// constraint, and setting up the initial points-to graph.
|
|
|
|
///
|
|
|
|
void Andersens::CollectConstraints(Module &M) {
|
|
|
|
// First, the universal set points to itself.
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::AddressOf, UniversalSet,
|
|
|
|
UniversalSet));
|
|
|
|
Constraints.push_back(Constraint(Constraint::Store, UniversalSet,
|
|
|
|
UniversalSet));
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
// Next, the null pointer points to the null object.
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::AddressOf, NullPtr, NullObject));
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
// Next, add any constraints on global variables and their initializers.
|
2005-03-27 22:03:46 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2004-05-23 21:00:47 +00:00
|
|
|
// Associate the address of the global object as pointing to the memory for
|
|
|
|
// the global: &G = <G memory>
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned ObjectIndex = getObject(I);
|
|
|
|
Node *Object = &GraphNodes[ObjectIndex];
|
2004-05-23 21:00:47 +00:00
|
|
|
Object->setValue(I);
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(*I),
|
|
|
|
ObjectIndex));
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
if (I->hasInitializer()) {
|
2007-09-16 21:45:02 +00:00
|
|
|
AddGlobalInitializerConstraints(ObjectIndex, I->getInitializer());
|
2004-05-23 21:00:47 +00:00
|
|
|
} else {
|
|
|
|
// If it doesn't have an initializer (i.e. it's defined in another
|
|
|
|
// translation unit), it points to the universal set.
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, ObjectIndex,
|
|
|
|
UniversalSet));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
|
|
|
|
// Make the function address point to the function object.
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned ObjectIndex = getObject(F);
|
|
|
|
GraphNodes[ObjectIndex].setValue(F);
|
|
|
|
Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(*F),
|
|
|
|
ObjectIndex));
|
2004-05-23 21:00:47 +00:00
|
|
|
// Set up the return value node.
|
|
|
|
if (isa<PointerType>(F->getFunctionType()->getReturnType()))
|
2007-09-16 21:45:02 +00:00
|
|
|
GraphNodes[getReturnNode(F)].setValue(F);
|
2004-05-23 21:00:47 +00:00
|
|
|
if (F->getFunctionType()->isVarArg())
|
2007-09-16 21:45:02 +00:00
|
|
|
GraphNodes[getVarargNode(F)].setValue(F);
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
// Set up incoming argument nodes.
|
2005-03-27 22:03:46 +00:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
I != E; ++I)
|
2004-05-23 21:00:47 +00:00
|
|
|
if (isa<PointerType>(I->getType()))
|
|
|
|
getNodeValue(*I);
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
// At some point we should just add constraints for the escaping functions
|
|
|
|
// at solve time, but this slows down solving. For now, we simply mark
|
|
|
|
// address taken functions as escaping and treat them as external.
|
|
|
|
if (!F->hasInternalLinkage() || AnalyzeUsesOfFunction(F))
|
2004-05-23 21:00:47 +00:00
|
|
|
AddConstraintsForNonInternalLinkage(F);
|
|
|
|
|
2007-01-30 20:08:39 +00:00
|
|
|
if (!F->isDeclaration()) {
|
2004-05-23 21:00:47 +00:00
|
|
|
// Scan the function body, creating a memory object for each heap/stack
|
|
|
|
// allocation in the body of the function and a node to represent all
|
|
|
|
// pointer values defined by instructions and used as operands.
|
|
|
|
visit(F);
|
2005-03-29 06:09:07 +00:00
|
|
|
} else {
|
2004-05-23 21:00:47 +00:00
|
|
|
// External functions that return pointers return the universal set.
|
|
|
|
if (isa<PointerType>(F->getFunctionType()->getReturnType()))
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy,
|
|
|
|
getReturnNode(F),
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet));
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
// Any pointers that are passed into the function have the universal set
|
|
|
|
// stored into them.
|
2005-03-27 22:03:46 +00:00
|
|
|
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
|
|
|
|
I != E; ++I)
|
2004-05-23 21:00:47 +00:00
|
|
|
if (isa<PointerType>(I->getType())) {
|
|
|
|
// Pointers passed into external functions could have anything stored
|
|
|
|
// through them.
|
|
|
|
Constraints.push_back(Constraint(Constraint::Store, getNode(I),
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet));
|
2004-05-23 21:00:47 +00:00
|
|
|
// Memory objects passed into external function calls can have the
|
|
|
|
// universal set point to them.
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy,
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet,
|
2004-05-23 21:00:47 +00:00
|
|
|
getNode(I)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is an external varargs function, it can also store pointers
|
|
|
|
// into any pointers passed through the varargs section.
|
|
|
|
if (F->getFunctionType()->isVarArg())
|
|
|
|
Constraints.push_back(Constraint(Constraint::Store, getVarargNode(F),
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NumConstraints += Constraints.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Andersens::visitInstruction(Instruction &I) {
|
|
|
|
#ifdef NDEBUG
|
|
|
|
return; // This function is just a big assert.
|
|
|
|
#endif
|
|
|
|
if (isa<BinaryOperator>(I))
|
|
|
|
return;
|
|
|
|
// Most instructions don't have any effect on pointer values.
|
|
|
|
switch (I.getOpcode()) {
|
|
|
|
case Instruction::Br:
|
|
|
|
case Instruction::Switch:
|
|
|
|
case Instruction::Unwind:
|
2004-10-16 18:16:19 +00:00
|
|
|
case Instruction::Unreachable:
|
2004-05-23 21:00:47 +00:00
|
|
|
case Instruction::Free:
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::FCmp:
|
2004-05-23 21:00:47 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
// Is this something we aren't handling yet?
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Unknown instruction: " << I;
|
2004-05-23 21:00:47 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitAllocationInst(AllocationInst &AI) {
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned ObjectIndex = getObject(&AI);
|
|
|
|
GraphNodes[ObjectIndex].setValue(&AI);
|
|
|
|
Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(AI),
|
|
|
|
ObjectIndex));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitReturnInst(ReturnInst &RI) {
|
|
|
|
if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType()))
|
|
|
|
// return V --> <Copy/retval{F}/v>
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy,
|
|
|
|
getReturnNode(RI.getParent()->getParent()),
|
|
|
|
getNode(RI.getOperand(0))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitLoadInst(LoadInst &LI) {
|
|
|
|
if (isa<PointerType>(LI.getType()))
|
|
|
|
// P1 = load P2 --> <Load/P1/P2>
|
|
|
|
Constraints.push_back(Constraint(Constraint::Load, getNodeValue(LI),
|
|
|
|
getNode(LI.getOperand(0))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitStoreInst(StoreInst &SI) {
|
|
|
|
if (isa<PointerType>(SI.getOperand(0)->getType()))
|
|
|
|
// store P1, P2 --> <Store/P2/P1>
|
|
|
|
Constraints.push_back(Constraint(Constraint::Store,
|
|
|
|
getNode(SI.getOperand(1)),
|
|
|
|
getNode(SI.getOperand(0))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitGetElementPtrInst(GetElementPtrInst &GEP) {
|
|
|
|
// P1 = getelementptr P2, ... --> <Copy/P1/P2>
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(GEP),
|
|
|
|
getNode(GEP.getOperand(0))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitPHINode(PHINode &PN) {
|
|
|
|
if (isa<PointerType>(PN.getType())) {
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned PNN = getNodeValue(PN);
|
2004-05-23 21:00:47 +00:00
|
|
|
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
|
|
|
|
// P1 = phi P2, P3 --> <Copy/P1/P2>, <Copy/P1/P3>, ...
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, PNN,
|
|
|
|
getNode(PN.getIncomingValue(i))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitCastInst(CastInst &CI) {
|
|
|
|
Value *Op = CI.getOperand(0);
|
|
|
|
if (isa<PointerType>(CI.getType())) {
|
|
|
|
if (isa<PointerType>(Op->getType())) {
|
|
|
|
// P1 = cast P2 --> <Copy/P1/P2>
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
|
|
|
|
getNode(CI.getOperand(0))));
|
|
|
|
} else {
|
|
|
|
// P1 = cast int --> <Copy/P1/Univ>
|
2005-03-29 20:36:05 +00:00
|
|
|
#if 0
|
2004-05-23 21:00:47 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet));
|
2005-04-05 01:12:03 +00:00
|
|
|
#else
|
|
|
|
getNodeValue(CI);
|
2005-03-29 20:36:05 +00:00
|
|
|
#endif
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
} else if (isa<PointerType>(Op->getType())) {
|
|
|
|
// int = cast P1 --> <Copy/Univ/P1>
|
2005-03-29 20:36:05 +00:00
|
|
|
#if 0
|
2004-05-23 21:00:47 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Copy,
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet,
|
2004-05-23 21:00:47 +00:00
|
|
|
getNode(CI.getOperand(0))));
|
2005-04-05 01:12:03 +00:00
|
|
|
#else
|
|
|
|
getNode(CI.getOperand(0));
|
2005-03-29 20:36:05 +00:00
|
|
|
#endif
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitSelectInst(SelectInst &SI) {
|
|
|
|
if (isa<PointerType>(SI.getType())) {
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned SIN = getNodeValue(SI);
|
2004-05-23 21:00:47 +00:00
|
|
|
// P1 = select C, P2, P3 ---> <Copy/P1/P2>, <Copy/P1/P3>
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, SIN,
|
|
|
|
getNode(SI.getOperand(1))));
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, SIN,
|
|
|
|
getNode(SI.getOperand(2))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitVAArg(VAArgInst &I) {
|
|
|
|
assert(0 && "vaarg not handled yet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddConstraintsForCall - Add constraints for a call with actual arguments
|
|
|
|
/// specified by CS to the function specified by F. Note that the types of
|
|
|
|
/// arguments might not match up in the case where this is an indirect call and
|
|
|
|
/// the function pointer has been casted. If this is the case, do something
|
|
|
|
/// reasonable.
|
|
|
|
void Andersens::AddConstraintsForCall(CallSite CS, Function *F) {
|
2007-09-16 21:45:02 +00:00
|
|
|
Value *CallValue = CS.getCalledValue();
|
|
|
|
bool IsDeref = F == NULL;
|
|
|
|
|
|
|
|
// If this is a call to an external function, try to handle it directly to get
|
|
|
|
// some taste of context sensitivity.
|
|
|
|
if (F && F->isDeclaration() && AddConstraintsForExternalCall(CS, F))
|
2005-03-29 06:09:07 +00:00
|
|
|
return;
|
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
if (isa<PointerType>(CS.getType())) {
|
2007-09-16 21:45:02 +00:00
|
|
|
unsigned CSN = getNode(CS.getInstruction());
|
|
|
|
if (!F || isa<PointerType>(F->getFunctionType()->getReturnType())) {
|
|
|
|
if (IsDeref)
|
|
|
|
Constraints.push_back(Constraint(Constraint::Load, CSN,
|
|
|
|
getNode(CallValue), CallReturnPos));
|
|
|
|
else
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, CSN,
|
|
|
|
getNode(CallValue) + CallReturnPos));
|
2004-05-23 21:00:47 +00:00
|
|
|
} else {
|
|
|
|
// If the function returns a non-pointer value, handle this just like we
|
|
|
|
// treat a nonpointer cast to pointer.
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, CSN,
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
2007-09-16 21:45:02 +00:00
|
|
|
} else if (F && isa<PointerType>(F->getFunctionType()->getReturnType())) {
|
2004-05-23 21:00:47 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Copy,
|
2007-09-16 21:45:02 +00:00
|
|
|
UniversalSet,
|
|
|
|
getNode(CallValue) + CallReturnPos));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
CallSite::arg_iterator ArgI = CS.arg_begin(), ArgE = CS.arg_end();
|
2007-09-16 21:45:02 +00:00
|
|
|
if (F) {
|
|
|
|
// Direct Call
|
|
|
|
Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
|
|
|
|
for (; AI != AE && ArgI != ArgE; ++AI, ++ArgI)
|
|
|
|
if (isa<PointerType>(AI->getType())) {
|
|
|
|
if (isa<PointerType>((*ArgI)->getType())) {
|
|
|
|
// Copy the actual argument into the formal argument.
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, getNode(AI),
|
|
|
|
getNode(*ArgI)));
|
|
|
|
} else {
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, getNode(AI),
|
|
|
|
UniversalSet));
|
|
|
|
}
|
|
|
|
} else if (isa<PointerType>((*ArgI)->getType())) {
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy,
|
|
|
|
UniversalSet,
|
|
|
|
getNode(*ArgI)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Indirect Call
|
|
|
|
unsigned ArgPos = CallFirstArgPos;
|
|
|
|
for (; ArgI != ArgE; ++ArgI) {
|
2004-05-23 21:00:47 +00:00
|
|
|
if (isa<PointerType>((*ArgI)->getType())) {
|
|
|
|
// Copy the actual argument into the formal argument.
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Store,
|
|
|
|
getNode(CallValue),
|
|
|
|
getNode(*ArgI), ArgPos++));
|
2004-05-23 21:00:47 +00:00
|
|
|
} else {
|
2007-09-16 21:45:02 +00:00
|
|
|
Constraints.push_back(Constraint(Constraint::Store,
|
|
|
|
getNode (CallValue),
|
|
|
|
UniversalSet, ArgPos++));
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-16 21:45:02 +00:00
|
|
|
}
|
2004-05-23 21:00:47 +00:00
|
|
|
// Copy all pointers passed through the varargs section to the varargs node.
|
2007-09-16 21:45:02 +00:00
|
|
|
if (F && F->getFunctionType()->isVarArg())
|
2004-05-23 21:00:47 +00:00
|
|
|
for (; ArgI != ArgE; ++ArgI)
|
|
|
|
if (isa<PointerType>((*ArgI)->getType()))
|
|
|
|
Constraints.push_back(Constraint(Constraint::Copy, getVarargNode(F),
|
|
|
|
getNode(*ArgI)));
|
|
|
|
// If more arguments are passed in than we track, just drop them on the floor.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::visitCallSite(CallSite CS) {
|
|
|
|
if (isa<PointerType>(CS.getType()))
|
|
|
|
getNodeValue(*CS.getInstruction());
|
|
|
|
|
|
|
|
if (Function *F = CS.getCalledFunction()) {
|
|
|
|
AddConstraintsForCall(CS, F);
|
|
|
|
} else {
|
2007-09-16 21:45:02 +00:00
|
|
|
AddConstraintsForCall(CS, NULL);
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Constraint Solving Phase
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// intersects - Return true if the points-to set of this node intersects
|
|
|
|
/// with the points-to set of the specified node.
|
|
|
|
bool Andersens::Node::intersects(Node *N) const {
|
2007-09-16 21:45:02 +00:00
|
|
|
return PointsTo->intersects(N->PointsTo);
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// intersectsIgnoring - Return true if the points-to set of this node
|
|
|
|
/// intersects with the points-to set of the specified node on any nodes
|
|
|
|
/// except for the specified node to ignore.
|
2007-09-16 21:45:02 +00:00
|
|
|
bool Andersens::Node::intersectsIgnoring(Node *N, unsigned Ignoring) const {
|
|
|
|
// TODO: If we are only going to call this with the same value for Ignoring,
|
|
|
|
// we should move the special values out of the points-to bitmap.
|
|
|
|
bool WeHadIt = PointsTo->test(Ignoring);
|
|
|
|
bool NHadIt = N->PointsTo->test(Ignoring);
|
|
|
|
bool Result = false;
|
|
|
|
if (WeHadIt)
|
|
|
|
PointsTo->reset(Ignoring);
|
|
|
|
if (NHadIt)
|
|
|
|
N->PointsTo->reset(Ignoring);
|
|
|
|
Result = PointsTo->intersects(N->PointsTo);
|
|
|
|
if (WeHadIt)
|
|
|
|
PointsTo->set(Ignoring);
|
|
|
|
if (NHadIt)
|
|
|
|
N->PointsTo->set(Ignoring);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the constraint graph used for solving points-to analysis.
|
|
|
|
//
|
|
|
|
void Andersens::CreateConstraintGraph() {
|
|
|
|
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
|
|
|
|
Constraint &C = Constraints[i];
|
|
|
|
assert (C.Src < GraphNodes.size() && C.Dest < GraphNodes.size());
|
|
|
|
if (C.Type == Constraint::AddressOf)
|
|
|
|
GraphNodes[C.Dest].PointsTo->set(C.Src);
|
|
|
|
else if (C.Type == Constraint::Load)
|
|
|
|
GraphNodes[C.Src].Constraints.push_back(C);
|
|
|
|
else if (C.Type == Constraint::Store)
|
|
|
|
GraphNodes[C.Dest].Constraints.push_back(C);
|
|
|
|
else if (C.Offset != 0)
|
|
|
|
GraphNodes[C.Src].Constraints.push_back(C);
|
2004-05-23 21:00:47 +00:00
|
|
|
else
|
2007-09-16 21:45:02 +00:00
|
|
|
GraphNodes[C.Src].Edges->set(C.Dest);
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
// Perform cycle detection, DFS, and RPO finding.
|
|
|
|
void Andersens::QueryNode(unsigned Node) {
|
|
|
|
assert(GraphNodes[Node].NodeRep == SelfRep && "Querying a non-rep node");
|
|
|
|
unsigned OurDFS = ++DFSNumber;
|
|
|
|
SparseBitVector<> ToErase;
|
|
|
|
SparseBitVector<> NewEdges;
|
|
|
|
Node2DFS[Node] = OurDFS;
|
|
|
|
|
|
|
|
for (SparseBitVector<>::iterator bi = GraphNodes[Node].Edges->begin();
|
|
|
|
bi != GraphNodes[Node].Edges->end();
|
|
|
|
++bi) {
|
|
|
|
unsigned RepNode = FindNode(*bi);
|
|
|
|
// If we are going to add an edge to repnode, we have no need for the edge
|
|
|
|
// to e anymore.
|
|
|
|
if (RepNode != *bi && NewEdges.test(RepNode)){
|
|
|
|
ToErase.set(*bi);
|
|
|
|
continue;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
// Continue about our DFS.
|
|
|
|
if (!Node2Deleted[RepNode]){
|
|
|
|
if (Node2DFS[RepNode] == 0) {
|
|
|
|
QueryNode(RepNode);
|
|
|
|
// May have been changed by query
|
|
|
|
RepNode = FindNode(RepNode);
|
|
|
|
}
|
|
|
|
if (Node2DFS[RepNode] < Node2DFS[Node])
|
|
|
|
Node2DFS[Node] = Node2DFS[RepNode];
|
|
|
|
}
|
|
|
|
// We may have just discovered that e belongs to a cycle, in which case we
|
|
|
|
// can also erase it.
|
|
|
|
if (RepNode != *bi) {
|
|
|
|
ToErase.set(*bi);
|
|
|
|
NewEdges.set(RepNode);
|
|
|
|
}
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
GraphNodes[Node].Edges->intersectWithComplement(ToErase);
|
|
|
|
GraphNodes[Node].Edges |= NewEdges;
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
// If this node is a root of a non-trivial SCC, place it on our worklist to be
|
|
|
|
// processed
|
|
|
|
if (OurDFS == Node2DFS[Node]) {
|
|
|
|
bool Changed = false;
|
|
|
|
while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= OurDFS) {
|
|
|
|
Node = UniteNodes(Node, FindNode(SCCStack.top()));
|
|
|
|
|
|
|
|
SCCStack.pop();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
Node2Deleted[Node] = true;
|
|
|
|
RPONumber++;
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
Topo2Node.at(GraphNodes.size() - RPONumber) = Node;
|
|
|
|
Node2Topo[Node] = GraphNodes.size() - RPONumber;
|
|
|
|
if (Changed)
|
|
|
|
GraphNodes[Node].Changed = true;
|
|
|
|
} else {
|
|
|
|
SCCStack.push(Node);
|
|
|
|
}
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// SolveConstraints - This stage iteratively processes the constraints list
|
|
|
|
/// propagating constraints (adding edges to the Nodes in the points-to graph)
|
|
|
|
/// until a fixed point is reached.
|
|
|
|
///
|
|
|
|
void Andersens::SolveConstraints() {
|
|
|
|
bool Changed = true;
|
|
|
|
unsigned Iteration = 0;
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
// We create the bitmaps here to avoid getting jerked around by the compiler
|
|
|
|
// creating objects behind our back and wasting lots of memory.
|
|
|
|
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
|
|
|
|
Node *N = &GraphNodes[i];
|
|
|
|
N->PointsTo = new SparseBitVector<>;
|
|
|
|
N->OldPointsTo = new SparseBitVector<>;
|
|
|
|
N->Edges = new SparseBitVector<>;
|
|
|
|
}
|
|
|
|
CreateConstraintGraph();
|
|
|
|
|
|
|
|
Topo2Node.insert(Topo2Node.begin(), GraphNodes.size(), Unvisited);
|
|
|
|
Node2Topo.insert(Node2Topo.begin(), GraphNodes.size(), Unvisited);
|
|
|
|
Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
|
|
|
|
Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
|
|
|
|
DFSNumber = 0;
|
|
|
|
RPONumber = 0;
|
|
|
|
// Order graph and mark starting nodes as changed.
|
|
|
|
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
|
|
|
|
unsigned N = FindNode(i);
|
|
|
|
Node *INode = &GraphNodes[i];
|
|
|
|
if (Node2DFS[N] == 0) {
|
|
|
|
QueryNode(N);
|
|
|
|
// Mark as changed if it's a representation and can contribute to the
|
|
|
|
// calculation right now.
|
|
|
|
if (INode->NodeRep == SelfRep && !INode->PointsTo->empty()
|
|
|
|
&& (!INode->Edges->empty() || !INode->Constraints.empty()))
|
|
|
|
INode->Changed = true;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
2007-09-16 21:45:02 +00:00
|
|
|
}
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
do {
|
2007-09-16 23:59:53 +00:00
|
|
|
Changed = false;
|
2007-09-16 21:45:02 +00:00
|
|
|
++NumIters;
|
2007-09-16 23:59:53 +00:00
|
|
|
DOUT << "Starting iteration #" << Iteration++;
|
2007-09-16 21:45:02 +00:00
|
|
|
// TODO: In the microoptimization category, we could just make Topo2Node
|
|
|
|
// a fast map and thus only contain the visited nodes.
|
|
|
|
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
|
|
|
|
unsigned CurrNodeIndex = Topo2Node[i];
|
|
|
|
Node *CurrNode;
|
|
|
|
|
|
|
|
// We may not revisit all nodes on every iteration
|
|
|
|
if (CurrNodeIndex == Unvisited)
|
|
|
|
continue;
|
|
|
|
CurrNode = &GraphNodes[CurrNodeIndex];
|
|
|
|
// See if this is a node we need to process on this iteration
|
|
|
|
if (!CurrNode->Changed || CurrNode->NodeRep != SelfRep)
|
|
|
|
continue;
|
|
|
|
CurrNode->Changed = false;
|
|
|
|
|
|
|
|
// Figure out the changed points to bits
|
|
|
|
SparseBitVector<> CurrPointsTo;
|
|
|
|
CurrPointsTo.intersectWithComplement(CurrNode->PointsTo,
|
|
|
|
CurrNode->OldPointsTo);
|
|
|
|
if (CurrPointsTo.empty()){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*(CurrNode->OldPointsTo) |= CurrPointsTo;
|
|
|
|
|
|
|
|
/* Now process the constraints for this node. */
|
|
|
|
for (std::list<Constraint>::iterator li = CurrNode->Constraints.begin();
|
|
|
|
li != CurrNode->Constraints.end(); ) {
|
|
|
|
li->Src = FindNode(li->Src);
|
|
|
|
li->Dest = FindNode(li->Dest);
|
|
|
|
|
|
|
|
// TODO: We could delete redundant constraints here.
|
|
|
|
// Src and Dest will be the vars we are going to process.
|
|
|
|
// This may look a bit ugly, but what it does is allow us to process
|
|
|
|
// both store and load constraints with the same function.
|
|
|
|
// Load constraints say that every member of our RHS solution has K
|
|
|
|
// added to it, and that variable gets an edge to LHS. We also union
|
|
|
|
// RHS+K's solution into the LHS solution.
|
|
|
|
// Store constraints say that every member of our LHS solution has K
|
|
|
|
// added to it, and that variable gets an edge from RHS. We also union
|
|
|
|
// RHS's solution into the LHS+K solution.
|
|
|
|
unsigned *Src;
|
|
|
|
unsigned *Dest;
|
|
|
|
unsigned K = li->Offset;
|
|
|
|
unsigned CurrMember;
|
|
|
|
if (li->Type == Constraint::Load) {
|
|
|
|
Src = &CurrMember;
|
|
|
|
Dest = &li->Dest;
|
|
|
|
} else if (li->Type == Constraint::Store) {
|
|
|
|
Src = &li->Src;
|
|
|
|
Dest = &CurrMember;
|
|
|
|
} else {
|
|
|
|
// TODO Handle offseted copy constraint
|
|
|
|
li++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: hybrid cycle detection would go here, we should check
|
|
|
|
// if it was a statically detected offline equivalence that
|
|
|
|
// involves pointers , and if so, remove the redundant constraints.
|
|
|
|
|
|
|
|
const SparseBitVector<> &Solution = CurrPointsTo;
|
|
|
|
|
|
|
|
for (SparseBitVector<>::iterator bi = Solution.begin();
|
|
|
|
bi != Solution.end();
|
|
|
|
++bi) {
|
|
|
|
CurrMember = *bi;
|
|
|
|
|
|
|
|
// Need to increment the member by K since that is where we are
|
|
|
|
// supposed to copy to/from
|
|
|
|
// Node that in positive weight cycles, which occur in address taking
|
|
|
|
// of fields, K can go past
|
|
|
|
// MaxK[CurrMember] elements, even though that is all it could
|
|
|
|
// point to.
|
|
|
|
if (K > 0 && K > MaxK[CurrMember])
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
CurrMember = FindNode(CurrMember + K);
|
|
|
|
|
|
|
|
// Add an edge to the graph, so we can just do regular bitmap ior next
|
|
|
|
// time. It may also let us notice a cycle.
|
2007-09-16 23:59:53 +00:00
|
|
|
if (GraphNodes[*Src].Edges->test_and_set(*Dest)) {
|
2007-09-16 21:45:02 +00:00
|
|
|
if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo)) {
|
|
|
|
GraphNodes[*Dest].Changed = true;
|
|
|
|
// If we changed a node we've already processed, we need another
|
|
|
|
// iteration.
|
|
|
|
if (Node2Topo[*Dest] <= i)
|
|
|
|
Changed = true;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-16 21:45:02 +00:00
|
|
|
}
|
|
|
|
li++;
|
|
|
|
}
|
|
|
|
SparseBitVector<> NewEdges;
|
|
|
|
SparseBitVector<> ToErase;
|
|
|
|
|
|
|
|
// Now all we have left to do is propagate points-to info along the
|
|
|
|
// edges, erasing the redundant edges.
|
|
|
|
|
|
|
|
|
|
|
|
for (SparseBitVector<>::iterator bi = CurrNode->Edges->begin();
|
|
|
|
bi != CurrNode->Edges->end();
|
|
|
|
++bi) {
|
|
|
|
|
|
|
|
unsigned DestVar = *bi;
|
|
|
|
unsigned Rep = FindNode(DestVar);
|
|
|
|
|
|
|
|
// If we ended up with this node as our destination, or we've already
|
|
|
|
// got an edge for the representative, delete the current edge.
|
|
|
|
if (Rep == CurrNodeIndex ||
|
|
|
|
(Rep != DestVar && NewEdges.test(Rep))) {
|
|
|
|
ToErase.set(DestVar);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Union the points-to sets into the dest
|
|
|
|
if (GraphNodes[Rep].PointsTo |= CurrPointsTo) {
|
|
|
|
GraphNodes[Rep].Changed = true;
|
|
|
|
if (Node2Topo[Rep] <= i)
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
// If this edge's destination was collapsed, rewrite the edge.
|
|
|
|
if (Rep != DestVar) {
|
|
|
|
ToErase.set(DestVar);
|
|
|
|
NewEdges.set(Rep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CurrNode->Edges->intersectWithComplement(ToErase);
|
|
|
|
CurrNode->Edges |= NewEdges;
|
|
|
|
}
|
|
|
|
if (Changed) {
|
|
|
|
DFSNumber = RPONumber = 0;
|
|
|
|
Node2Deleted.clear();
|
|
|
|
Topo2Node.clear();
|
|
|
|
Node2Topo.clear();
|
|
|
|
Node2DFS.clear();
|
|
|
|
Topo2Node.insert(Topo2Node.begin(), GraphNodes.size(), Unvisited);
|
|
|
|
Node2Topo.insert(Node2Topo.begin(), GraphNodes.size(), Unvisited);
|
|
|
|
Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
|
|
|
|
Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
|
|
|
|
// Rediscover the DFS/Topo ordering, and cycle detect.
|
|
|
|
for (unsigned j = 0; j < GraphNodes.size(); j++) {
|
|
|
|
unsigned JRep = FindNode(j);
|
|
|
|
if (Node2DFS[JRep] == 0)
|
|
|
|
QueryNode(JRep);
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-16 21:45:02 +00:00
|
|
|
|
|
|
|
} while (Changed);
|
|
|
|
|
|
|
|
Node2Topo.clear();
|
|
|
|
Topo2Node.clear();
|
|
|
|
Node2DFS.clear();
|
|
|
|
Node2Deleted.clear();
|
|
|
|
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
|
|
|
|
Node *N = &GraphNodes[i];
|
|
|
|
delete N->OldPointsTo;
|
|
|
|
delete N->Edges;
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Union-Find
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Unite nodes First and Second, returning the one which is now the
|
|
|
|
// representative node. First and Second are indexes into GraphNodes
|
|
|
|
unsigned Andersens::UniteNodes(unsigned First, unsigned Second) {
|
|
|
|
assert (First < GraphNodes.size() && Second < GraphNodes.size() &&
|
|
|
|
"Attempting to merge nodes that don't exist");
|
|
|
|
// TODO: implement union by rank
|
|
|
|
Node *FirstNode = &GraphNodes[First];
|
|
|
|
Node *SecondNode = &GraphNodes[Second];
|
|
|
|
|
|
|
|
assert (SecondNode->NodeRep == SelfRep && FirstNode->NodeRep == SelfRep &&
|
|
|
|
"Trying to unite two non-representative nodes!");
|
|
|
|
if (First == Second)
|
|
|
|
return First;
|
|
|
|
|
|
|
|
SecondNode->NodeRep = First;
|
|
|
|
FirstNode->Changed |= SecondNode->Changed;
|
|
|
|
FirstNode->PointsTo |= *(SecondNode->PointsTo);
|
|
|
|
FirstNode->Edges |= *(SecondNode->Edges);
|
|
|
|
FirstNode->Constraints.splice(FirstNode->Constraints.begin(),
|
|
|
|
SecondNode->Constraints);
|
|
|
|
delete FirstNode->OldPointsTo;
|
|
|
|
FirstNode->OldPointsTo = new SparseBitVector<>;
|
|
|
|
|
|
|
|
// Destroy interesting parts of the merged-from node.
|
|
|
|
delete SecondNode->OldPointsTo;
|
|
|
|
delete SecondNode->Edges;
|
|
|
|
delete SecondNode->PointsTo;
|
|
|
|
SecondNode->Edges = NULL;
|
|
|
|
SecondNode->PointsTo = NULL;
|
|
|
|
SecondNode->OldPointsTo = NULL;
|
|
|
|
|
|
|
|
NumUnified++;
|
|
|
|
DOUT << "Unified Node ";
|
|
|
|
DEBUG(PrintNode(FirstNode));
|
|
|
|
DOUT << " and Node ";
|
|
|
|
DEBUG(PrintNode(SecondNode));
|
|
|
|
DOUT << "\n";
|
|
|
|
|
|
|
|
// TODO: Handle SDT
|
|
|
|
return First;
|
|
|
|
}
|
2004-05-23 21:00:47 +00:00
|
|
|
|
2007-09-16 21:45:02 +00:00
|
|
|
// Find the index into GraphNodes of the node representing Node, performing
|
|
|
|
// path compression along the way
|
|
|
|
unsigned Andersens::FindNode(unsigned NodeIndex) {
|
|
|
|
assert (NodeIndex < GraphNodes.size()
|
|
|
|
&& "Attempting to find a node that can't exist");
|
|
|
|
Node *N = &GraphNodes[NodeIndex];
|
|
|
|
if (N->NodeRep == SelfRep)
|
|
|
|
return NodeIndex;
|
|
|
|
else
|
|
|
|
return (N->NodeRep = FindNode(N->NodeRep));
|
|
|
|
}
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Debugging Output
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void Andersens::PrintNode(Node *N) {
|
|
|
|
if (N == &GraphNodes[UniversalSet]) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "<universal>";
|
2004-05-23 21:00:47 +00:00
|
|
|
return;
|
|
|
|
} else if (N == &GraphNodes[NullPtr]) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "<nullptr>";
|
2004-05-23 21:00:47 +00:00
|
|
|
return;
|
|
|
|
} else if (N == &GraphNodes[NullObject]) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "<null>";
|
2004-05-23 21:00:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-09-16 21:45:02 +00:00
|
|
|
if (!N->getValue()) {
|
|
|
|
cerr << "artificial" << (intptr_t) N;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
assert(N->getValue() != 0 && "Never set node label!");
|
|
|
|
Value *V = N->getValue();
|
|
|
|
if (Function *F = dyn_cast<Function>(V)) {
|
|
|
|
if (isa<PointerType>(F->getFunctionType()->getReturnType()) &&
|
2007-09-16 21:45:02 +00:00
|
|
|
N == &GraphNodes[getReturnNode(F)]) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << F->getName() << ":retval";
|
2004-05-23 21:00:47 +00:00
|
|
|
return;
|
2007-09-16 21:45:02 +00:00
|
|
|
} else if (F->getFunctionType()->isVarArg() &&
|
|
|
|
N == &GraphNodes[getVarargNode(F)]) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << F->getName() << ":vararg";
|
2004-05-23 21:00:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << I->getParent()->getParent()->getName() << ":";
|
2004-05-23 21:00:47 +00:00
|
|
|
else if (Argument *Arg = dyn_cast<Argument>(V))
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << Arg->getParent()->getName() << ":";
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
if (V->hasName())
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << V->getName();
|
2004-05-23 21:00:47 +00:00
|
|
|
else
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "(unnamed)";
|
2004-05-23 21:00:47 +00:00
|
|
|
|
|
|
|
if (isa<GlobalValue>(V) || isa<AllocationInst>(V))
|
2007-09-16 21:45:02 +00:00
|
|
|
if (N == &GraphNodes[getObject(V)])
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "<mem>";
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::PrintConstraints() {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Constraints:\n";
|
2007-09-16 21:45:02 +00:00
|
|
|
|
2004-05-23 21:00:47 +00:00
|
|
|
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
|
2007-09-16 21:45:02 +00:00
|
|
|
const Constraint &C = Constraints[i];
|
|
|
|
if (C.Type == Constraint::Store) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "*";
|
2007-09-16 21:45:02 +00:00
|
|
|
if (C.Offset != 0)
|
|
|
|
cerr << "(";
|
|
|
|
}
|
|
|
|
PrintNode(&GraphNodes[C.Dest]);
|
|
|
|
if (C.Type == Constraint::Store && C.Offset != 0)
|
|
|
|
cerr << " + " << C.Offset << ")";
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << " = ";
|
2007-09-16 21:45:02 +00:00
|
|
|
if (C.Type == Constraint::Load) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "*";
|
2007-09-16 21:45:02 +00:00
|
|
|
if (C.Offset != 0)
|
|
|
|
cerr << "(";
|
|
|
|
}
|
|
|
|
else if (C.Type == Constraint::AddressOf)
|
|
|
|
cerr << "&";
|
|
|
|
PrintNode(&GraphNodes[C.Src]);
|
|
|
|
if (C.Offset != 0 && C.Type != Constraint::Store)
|
|
|
|
cerr << " + " << C.Offset;
|
|
|
|
if (C.Type == Constraint::Load && C.Offset != 0)
|
|
|
|
cerr << ")";
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "\n";
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Andersens::PrintPointsToGraph() {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Points-to graph:\n";
|
2004-05-23 21:00:47 +00:00
|
|
|
for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) {
|
|
|
|
Node *N = &GraphNodes[i];
|
2007-09-16 21:45:02 +00:00
|
|
|
if (FindNode (i) != i) {
|
|
|
|
PrintNode(N);
|
|
|
|
cerr << "\t--> same as ";
|
|
|
|
PrintNode(&GraphNodes[FindNode(i)]);
|
|
|
|
cerr << "\n";
|
|
|
|
} else {
|
|
|
|
cerr << "[" << (N->PointsTo->count()) << "] ";
|
|
|
|
PrintNode(N);
|
|
|
|
cerr << "\t--> ";
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
for (SparseBitVector<>::iterator bi = N->PointsTo->begin();
|
|
|
|
bi != N->PointsTo->end();
|
|
|
|
++bi) {
|
|
|
|
if (!first)
|
|
|
|
cerr << ", ";
|
|
|
|
PrintNode(&GraphNodes[*bi]);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
cerr << "\n";
|
2004-05-23 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|