#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"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/SparseBitVector.h"
#include "llvm/ADT/DenseSet.h"
#include <algorithm>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#define FULL_UNIVERSAL 0
using namespace llvm;
STATISTIC(NumIters , "Number of iterations to reach convergence");
STATISTIC(NumConstraints, "Number of constraints");
STATISTIC(NumNodes , "Number of nodes");
STATISTIC(NumUnified , "Number of variables unified");
STATISTIC(NumErased , "Number of redundant constraints erased");
static const unsigned SelfRep = (unsigned)-1;
static const unsigned Unvisited = (unsigned)-1;
static const unsigned CallReturnPos = 1;
static const unsigned CallFirstArgPos = 2;
namespace {
struct BitmapKeyInfo {
static inline SparseBitVector<> *getEmptyKey() {
return reinterpret_cast<SparseBitVector<> *>(-1);
}
static inline SparseBitVector<> *getTombstoneKey() {
return reinterpret_cast<SparseBitVector<> *>(-2);
}
static unsigned getHashValue(const SparseBitVector<> *bitmap) {
return bitmap->getHashValue();
}
static bool isEqual(const SparseBitVector<> *LHS,
const SparseBitVector<> *RHS) {
if (LHS == RHS)
return true;
else if (LHS == getEmptyKey() || RHS == getEmptyKey()
|| LHS == getTombstoneKey() || RHS == getTombstoneKey())
return false;
return *LHS == *RHS;
}
static bool isPod() { return true; }
};
class VISIBILITY_HIDDEN Andersens : public ModulePass, public AliasAnalysis,
private InstVisitor<Andersens> {
struct Node;
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");
}
bool operator==(const Constraint &RHS) const {
return RHS.Type == Type
&& RHS.Dest == Dest
&& RHS.Src == Src
&& RHS.Offset == Offset;
}
bool operator!=(const Constraint &RHS) const {
return !(*this == RHS);
}
bool operator<(const Constraint &RHS) const {
if (RHS.Type != Type)
return RHS.Type < Type;
else if (RHS.Dest != Dest)
return RHS.Dest < Dest;
else if (RHS.Src != Src)
return RHS.Src < Src;
return RHS.Offset < Offset;
}
};
struct PairKeyInfo {
static inline std::pair<unsigned, unsigned> getEmptyKey() {
return std::make_pair(~0U, ~0U);
}
static inline std::pair<unsigned, unsigned> getTombstoneKey() {
return std::make_pair(~0U - 1, ~0U - 1);
}
static unsigned getHashValue(const std::pair<unsigned, unsigned> &P) {
return P.first ^ P.second;
}
static unsigned isEqual(const std::pair<unsigned, unsigned> &LHS,
const std::pair<unsigned, unsigned> &RHS) {
return LHS == RHS;
}
};
struct ConstraintKeyInfo {
static inline Constraint getEmptyKey() {
return Constraint(Constraint::Copy, ~0U, ~0U, ~0U);
}
static inline Constraint getTombstoneKey() {
return Constraint(Constraint::Copy, ~0U - 1, ~0U - 1, ~0U - 1);
}
static unsigned getHashValue(const Constraint &C) {
return C.Src ^ C.Dest ^ C.Type ^ C.Offset;
}
static bool isEqual(const Constraint &LHS,
const Constraint &RHS) {
return LHS.Type == RHS.Type && LHS.Dest == RHS.Dest
&& LHS.Src == RHS.Src && LHS.Offset == RHS.Offset;
}
};
struct Node {
private:
static unsigned Counter;
public:
Value *Val;
SparseBitVector<> *Edges;
SparseBitVector<> *PointsTo;
SparseBitVector<> *OldPointsTo;
std::list<Constraint> Constraints;
unsigned PointerEquivLabel;
unsigned LocationEquivLabel;
SparseBitVector<> *PredEdges;
SparseBitVector<> *ImplicitPredEdges;
SparseBitVector<> *PointedToBy;
unsigned NumInEdges;
bool StoredInHash;
bool Direct;
bool AddressTaken;
unsigned NodeRep;
unsigned Timestamp;
explicit Node(bool direct = true) :
Val(0), Edges(0), PointsTo(0), OldPointsTo(0),
PointerEquivLabel(0), LocationEquivLabel(0), PredEdges(0),
ImplicitPredEdges(0), PointedToBy(0), NumInEdges(0),
StoredInHash(false), Direct(direct), AddressTaken(false),
NodeRep(SelfRep), Timestamp(0) { }
Node *setValue(Value *V) {
assert(Val == 0 && "Value already set for this node!");
Val = V;
return this;
}
Value *getValue() const { return Val; }
bool addPointerTo(unsigned Node) {
return PointsTo->test_and_set(Node);
}
bool intersects(Node *N) const;
bool intersectsIgnoring(Node *N, unsigned) const;
void Stamp() {
Timestamp = Counter++;
}
bool isRep() const {
return( (int) NodeRep < 0 );
}
};
struct WorkListElement {
Node* node;
unsigned Timestamp;
WorkListElement(Node* n, unsigned t) : node(n), Timestamp(t) {}
bool operator<(const WorkListElement& that) const {
return( this->Timestamp > that.Timestamp );
}
};
class WorkList {
std::priority_queue<WorkListElement> Q;
public:
void insert(Node* n) {
Q.push( WorkListElement(n, n->Timestamp) );
}
Node* pop() {
while( !Q.empty() ) {
WorkListElement x = Q.top(); Q.pop();
Node* INode = x.node;
if( INode->isRep() &&
INode->Timestamp == x.Timestamp ) {
return(x.node);
}
}
return(0);
}
bool empty() {
return Q.empty();
}
};
std::vector<Node> GraphNodes;
DenseMap<Value*, unsigned> ValueNodes;
DenseMap<Value*, unsigned> ObjectNodes;
DenseMap<Function*, unsigned> ReturnNodes;
DenseMap<Function*, unsigned> VarargNodes;
std::vector<Constraint> Constraints;
std::map<unsigned, unsigned> MaxK;
enum {
UniversalSet = 0,
NullPtr = 1,
NullObject = 2,
NumberSpecialNodes
};
std::stack<unsigned> SCCStack;
std::vector<unsigned> Node2DFS;
std::vector<bool> Node2Deleted;
std::map<unsigned, unsigned> Tarjan2DFS;
std::map<unsigned, bool> Tarjan2Deleted;
unsigned DFSNumber;
WorkList w1, w2;
WorkList *CurrWL, *NextWL;
std::vector<unsigned> VSSCCRep;
std::vector<bool> Node2Visited;
unsigned FirstRefNode;
unsigned FirstAdrNode;
unsigned PEClass;
typedef DenseMap<SparseBitVector<> *, unsigned, BitmapKeyInfo> BitVectorMap;
BitVectorMap Set2PEClass;
std::vector<int> PEClass2Node;
std::vector<int> PENLEClass2Node;
std::vector<unsigned> HCDSCCRep;
std::vector<int> SDT;
bool SDTActive;
public:
static char ID;
Andersens() : ModulePass(&ID) {}
bool runOnModule(Module &M) {
InitializeAliasAnalysis(this);
IdentifyObjects(M);
CollectConstraints(M);
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa-constraints"
DEBUG(PrintConstraints());
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa"
SolveConstraints();
DEBUG(PrintPointsToGraph());
std::vector<Constraint>().swap(Constraints);
return false;
}
void releaseMemory() {
#if 0
std::vector<Node>().swap(GraphNodes);
ValueNodes.clear();
#endif
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AliasAnalysis::getAnalysisUsage(AU);
AU.setPreservesAll(); }
AliasResult alias(const Value *V1, unsigned V1Size,
const Value *V2, unsigned V2Size);
virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
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:
unsigned getNode(Value *V) {
if (Constant *C = dyn_cast<Constant>(V))
if (!isa<GlobalValue>(C))
return getNodeForConstantPointer(C);
DenseMap<Value*, unsigned>::iterator I = ValueNodes.find(V);
if (I == ValueNodes.end()) {
#ifndef NDEBUG
V->dump();
#endif
assert(0 && "Value does not have a node in the points-to graph!");
}
return I->second;
}
unsigned getObject(Value *V) const {
DenseMap<Value*, unsigned>::iterator I = ObjectNodes.find(V);
assert(I != ObjectNodes.end() &&
"Value does not have an object in the points-to graph!");
return I->second;
}
unsigned getReturnNode(Function *F) const {
DenseMap<Function*, unsigned>::iterator I = ReturnNodes.find(F);
assert(I != ReturnNodes.end() && "Function does not return a value!");
return I->second;
}
unsigned getVarargNode(Function *F) const {
DenseMap<Function*, unsigned>::iterator I = VarargNodes.find(F);
assert(I != VarargNodes.end() && "Function does not take var args!");
return I->second;
}
unsigned getNodeValue(Value &V) {
unsigned Index = getNode(&V);
GraphNodes[Index].setValue(&V);
return Index;
}
unsigned UniteNodes(unsigned First, unsigned Second,
bool UnionByRank = true);
unsigned FindNode(unsigned Node);
unsigned FindNode(unsigned Node) const;
void IdentifyObjects(Module &M);
void CollectConstraints(Module &M);
bool AnalyzeUsesOfFunction(Value *);
void CreateConstraintGraph();
void OptimizeConstraints();
unsigned FindEquivalentNode(unsigned, unsigned);
void ClumpAddressTaken();
void RewriteConstraints();
void HU();
void HVN();
void HCD();
void Search(unsigned Node);
void UnitePointerEquivalences();
void SolveConstraints();
bool QueryNode(unsigned Node);
void Condense(unsigned Node);
void HUValNum(unsigned Node);
void HVNValNum(unsigned Node);
unsigned getNodeForConstantPointer(Constant *C);
unsigned getNodeForConstantPointerTarget(Constant *C);
void AddGlobalInitializerConstraints(unsigned, Constant *C);
void AddConstraintsForNonInternalLinkage(Function *F);
void AddConstraintsForCall(CallSite CS, Function *F);
bool AddConstraintsForExternalCall(CallSite CS, Function *F);
void PrintNode(const Node *N) const;
void PrintConstraints() const ;
void PrintConstraint(const Constraint &) const;
void PrintLabels() const;
void PrintPointsToGraph() const;
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);
void visitICmpInst(ICmpInst &ICI) {} void visitFCmpInst(FCmpInst &ICI) {} void visitSelectInst(SelectInst &SI);
void visitVAArg(VAArgInst &I);
void visitInstruction(Instruction &I);
void print(std::ostream &O, const Module* M) const {
PrintPointsToGraph();
}
};
}
char Andersens::ID = 0;
static RegisterPass<Andersens>
X("anders-aa", "Andersen's Interprocedural Alias Analysis", false, true);
static RegisterAnalysisGroup<AliasAnalysis> Y(X);
unsigned Andersens::Node::Counter = 0;
ModulePass *llvm::createAndersensPass() { return new Andersens(); }
AliasAnalysis::AliasResult Andersens::alias(const Value *V1, unsigned V1Size,
const Value *V2, unsigned V2Size) {
Node *N1 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V1)))];
Node *N2 = &GraphNodes[FindNode(getNode(const_cast<Value*>(V2)))];
if (!N1->intersectsIgnoring(N2, NullObject))
return NoAlias;
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
}
AliasAnalysis::ModRefResult
Andersens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
if (Function *F = CS.getCalledFunction())
if (F->isDeclaration()) {
Node *N1 = &GraphNodes[FindNode(getNode(P))];
if (N1->PointsTo->empty())
return NoModRef;
#if FULL_UNIVERSAL
if (!UniversalSet->PointsTo->test(FindNode(getNode(P))))
return NoModRef; #else
if (!N1->PointsTo->test(UniversalSet))
return NoModRef; #endif
}
return AliasAnalysis::getModRefInfo(CS, P, Size);
}
AliasAnalysis::ModRefResult
Andersens::getModRefInfo(CallSite CS1, CallSite CS2) {
return AliasAnalysis::getModRefInfo(CS1,CS2);
}
void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Node *N = &GraphNodes[FindNode(getNode(P))];
if (N->PointsTo->count() == 1) {
Node *Pointee = &GraphNodes[N->PointsTo->find_first()];
if (Value *V = Pointee->getValue()) {
if (Function *F = dyn_cast<Function>(V))
RetVals.push_back(F);
} else {
if (Pointee == &GraphNodes[NullObject])
RetVals.push_back(Constant::getNullValue(P->getType()));
}
}
AliasAnalysis::getMustAliases(P, RetVals);
}
bool Andersens::pointsToConstantMemory(const Value *P) {
Node *N = &GraphNodes[FindNode(getNode(const_cast<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()) {
if (!isa<GlobalValue>(V) || (isa<GlobalVariable>(V) &&
!cast<GlobalVariable>(V)->isConstant()))
return AliasAnalysis::pointsToConstantMemory(P);
} else {
if (i != NullObject)
return AliasAnalysis::pointsToConstantMemory(P);
}
}
return true;
}
void Andersens::IdentifyObjects(Module &M) {
unsigned NumObjects = 0;
assert(NumObjects == UniversalSet && "Something changed!");
++NumObjects;
assert(NumObjects == NullPtr && "Something changed!");
++NumObjects;
assert(NumObjects == NullObject && "Something changed!");
++NumObjects;
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) {
ObjectNodes[I] = NumObjects++;
ValueNodes[I] = NumObjects++;
}
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
unsigned First = NumObjects;
ValueNodes[F] = NumObjects++;
if (isa<PointerType>(F->getFunctionType()->getReturnType()))
ReturnNodes[F] = NumObjects++;
if (F->getFunctionType()->isVarArg())
VarargNodes[F] = NumObjects++;
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
I != E; ++I)
{
if (isa<PointerType>(I->getType()))
ValueNodes[I] = NumObjects++;
}
MaxK[First] = NumObjects - First;
for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
if (isa<PointerType>(II->getType())) {
ValueNodes[&*II] = NumObjects++;
if (AllocationInst *AI = dyn_cast<AllocationInst>(&*II))
ObjectNodes[AI] = NumObjects++;
}
if (CallInst *CI = dyn_cast<CallInst>(&*II)) {
Value *Callee = CI->getCalledValue();
if (isa<InlineAsm>(Callee))
ValueNodes[Callee] = NumObjects++;
}
}
}
GraphNodes.resize(NumObjects);
NumNodes += NumObjects;
}
unsigned Andersens::getNodeForConstantPointer(Constant *C) {
assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
if (isa<ConstantPointerNull>(C) || isa<UndefValue>(C))
return NullPtr;
else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
return getNode(GV);
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
switch (CE->getOpcode()) {
case Instruction::GetElementPtr:
return getNodeForConstantPointer(CE->getOperand(0));
case Instruction::IntToPtr:
return UniversalSet;
case Instruction::BitCast:
return getNodeForConstantPointer(CE->getOperand(0));
default:
cerr << "Constant Expr not yet handled: " << *CE << "\n";
assert(0);
}
} else {
assert(0 && "Unknown constant pointer!");
}
return 0;
}
unsigned Andersens::getNodeForConstantPointerTarget(Constant *C) {
assert(isa<PointerType>(C->getType()) && "Not a constant pointer!");
if (isa<ConstantPointerNull>(C))
return NullObject;
else if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
return getObject(GV);
else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
switch (CE->getOpcode()) {
case Instruction::GetElementPtr:
return getNodeForConstantPointerTarget(CE->getOperand(0));
case Instruction::IntToPtr:
return UniversalSet;
case Instruction::BitCast:
return getNodeForConstantPointerTarget(CE->getOperand(0));
default:
cerr << "Constant Expr not yet handled: " << *CE << "\n";
assert(0);
}
} else {
assert(0 && "Unknown constant pointer!");
}
return 0;
}
void Andersens::AddGlobalInitializerConstraints(unsigned NodeIndex,
Constant *C) {
if (C->getType()->isSingleValueType()) {
if (isa<PointerType>(C->getType()))
Constraints.push_back(Constraint(Constraint::Copy, NodeIndex,
getNodeForConstantPointer(C)));
} else if (C->isNullValue()) {
Constraints.push_back(Constraint(Constraint::Copy, NodeIndex,
NullObject));
return;
} else if (!isa<UndefValue>(C)) {
assert(isa<ConstantArray>(C) || isa<ConstantStruct>(C));
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
AddGlobalInitializerConstraints(NodeIndex,
cast<Constant>(C->getOperand(i)));
}
}
void Andersens::AddConstraintsForNonInternalLinkage(Function *F) {
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
if (isa<PointerType>(I->getType()))
Constraints.push_back(Constraint(Constraint::Copy, getNode(I),
UniversalSet));
}
bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) {
assert(F->isDeclaration() && "Not an external function!");
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" ||
F->getName() == "llvm.memset" ||
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")
return true;
if (F->getName() == "llvm.memcpy" ||
F->getName() == "llvm.memmove" ||
F->getName() == "memmove") {
const FunctionType *FTy = F->getFunctionType();
if (FTy->getNumParams() > 1 &&
isa<PointerType>(FTy->getParamType(0)) &&
isa<PointerType>(FTy->getParamType(1))) {
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));
Constraints.push_back(Constraint(Constraint::Copy,
FirstArg, SecondArg));
return true;
}
}
if (F->getName() == "realloc" || F->getName() == "strchr" ||
F->getName() == "strrchr" || F->getName() == "strstr" ||
F->getName() == "strtok") {
const FunctionType *FTy = F->getFunctionType();
if (FTy->getNumParams() > 0 &&
isa<PointerType>(FTy->getParamType(0))) {
Constraints.push_back(Constraint(Constraint::Copy,
getNode(CS.getInstruction()),
getNode(CS.getArgument(0))));
return true;
}
}
return false;
}
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; }
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
if (AnalyzeUsesOfFunction(GEP)) return true;
} else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
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)) {
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; } else if (dyn_cast<FreeInst>(*UI)) {
return false;
} else {
return true;
}
return false;
}
void Andersens::CollectConstraints(Module &M) {
Constraints.push_back(Constraint(Constraint::AddressOf, UniversalSet,
UniversalSet));
Constraints.push_back(Constraint(Constraint::Store, UniversalSet,
UniversalSet));
Constraints.push_back(Constraint(Constraint::AddressOf, NullPtr, NullObject));
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) {
unsigned ObjectIndex = getObject(I);
Node *Object = &GraphNodes[ObjectIndex];
Object->setValue(I);
Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(*I),
ObjectIndex));
if (I->hasInitializer()) {
AddGlobalInitializerConstraints(ObjectIndex, I->getInitializer());
} else {
Constraints.push_back(Constraint(Constraint::Copy, ObjectIndex,
UniversalSet));
}
}
for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
if (isa<PointerType>(F->getFunctionType()->getReturnType()))
GraphNodes[getReturnNode(F)].setValue(F);
if (F->getFunctionType()->isVarArg())
GraphNodes[getVarargNode(F)].setValue(F);
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
I != E; ++I)
if (isa<PointerType>(I->getType()))
getNodeValue(*I);
if (!F->hasLocalLinkage() || AnalyzeUsesOfFunction(F))
AddConstraintsForNonInternalLinkage(F);
if (!F->isDeclaration()) {
visit(F);
} else {
if (isa<PointerType>(F->getFunctionType()->getReturnType()))
Constraints.push_back(Constraint(Constraint::Copy,
getReturnNode(F),
UniversalSet));
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
I != E; ++I)
if (isa<PointerType>(I->getType())) {
Constraints.push_back(Constraint(Constraint::Store, getNode(I),
UniversalSet));
#if FULL_UNIVERSAL
Constraints.push_back(Constraint(Constraint::Copy,
UniversalSet,
getNode(I)));
#else
Constraints.push_back(Constraint(Constraint::Copy,
getNode(I),
UniversalSet));
#endif
}
if (F->getFunctionType()->isVarArg())
Constraints.push_back(Constraint(Constraint::Store, getVarargNode(F),
UniversalSet));
}
}
NumConstraints += Constraints.size();
}
void Andersens::visitInstruction(Instruction &I) {
#ifdef NDEBUG
return; #endif
if (isa<BinaryOperator>(I))
return;
switch (I.getOpcode()) {
case Instruction::Br:
case Instruction::Switch:
case Instruction::Unwind:
case Instruction::Unreachable:
case Instruction::Free:
case Instruction::ICmp:
case Instruction::FCmp:
return;
default:
cerr << "Unknown instruction: " << I;
abort();
}
}
void Andersens::visitAllocationInst(AllocationInst &AI) {
unsigned ObjectIndex = getObject(&AI);
GraphNodes[ObjectIndex].setValue(&AI);
Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(AI),
ObjectIndex));
}
void Andersens::visitReturnInst(ReturnInst &RI) {
if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType()))
Constraints.push_back(Constraint(Constraint::Copy,
getReturnNode(RI.getParent()->getParent()),
getNode(RI.getOperand(0))));
}
void Andersens::visitLoadInst(LoadInst &LI) {
if (isa<PointerType>(LI.getType()))
Constraints.push_back(Constraint(Constraint::Load, getNodeValue(LI),
getNode(LI.getOperand(0))));
}
void Andersens::visitStoreInst(StoreInst &SI) {
if (isa<PointerType>(SI.getOperand(0)->getType()))
Constraints.push_back(Constraint(Constraint::Store,
getNode(SI.getOperand(1)),
getNode(SI.getOperand(0))));
}
void Andersens::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(GEP),
getNode(GEP.getOperand(0))));
}
void Andersens::visitPHINode(PHINode &PN) {
if (isa<PointerType>(PN.getType())) {
unsigned PNN = getNodeValue(PN);
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
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())) {
Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
getNode(CI.getOperand(0))));
} else {
#if 0
Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
UniversalSet));
#else
getNodeValue(CI);
#endif
}
} else if (isa<PointerType>(Op->getType())) {
#if 0
Constraints.push_back(Constraint(Constraint::Copy,
UniversalSet,
getNode(CI.getOperand(0))));
#else
getNode(CI.getOperand(0));
#endif
}
}
void Andersens::visitSelectInst(SelectInst &SI) {
if (isa<PointerType>(SI.getType())) {
unsigned SIN = getNodeValue(SI);
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!");
}
void Andersens::AddConstraintsForCall(CallSite CS, Function *F) {
Value *CallValue = CS.getCalledValue();
bool IsDeref = F == NULL;
if (F && F->isDeclaration() && AddConstraintsForExternalCall(CS, F))
return;
if (isa<PointerType>(CS.getType())) {
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));
} else {
Constraints.push_back(Constraint(Constraint::Copy, CSN,
UniversalSet));
}
} else if (F && isa<PointerType>(F->getFunctionType()->getReturnType())) {
#if FULL_UNIVERSAL
Constraints.push_back(Constraint(Constraint::Copy,
UniversalSet,
getNode(CallValue) + CallReturnPos));
#else
Constraints.push_back(Constraint(Constraint::Copy,
getNode(CallValue) + CallReturnPos,
UniversalSet));
#endif
}
CallSite::arg_iterator ArgI = CS.arg_begin(), ArgE = CS.arg_end();
bool external = !F || F->isDeclaration();
if (F) {
Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
for (; AI != AE && ArgI != ArgE; ++AI, ++ArgI)
{
#if !FULL_UNIVERSAL
if (external && isa<PointerType>((*ArgI)->getType()))
{
Constraints.push_back(Constraint(Constraint::Copy,
getNode(*ArgI),
UniversalSet));
}
#endif
if (isa<PointerType>(AI->getType())) {
if (isa<PointerType>((*ArgI)->getType())) {
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())) {
#if FULL_UNIVERSAL
Constraints.push_back(Constraint(Constraint::Copy,
UniversalSet,
getNode(*ArgI)));
#else
Constraints.push_back(Constraint(Constraint::Copy,
getNode(*ArgI),
UniversalSet));
#endif
}
}
} else {
unsigned ArgPos = CallFirstArgPos;
for (; ArgI != ArgE; ++ArgI) {
if (isa<PointerType>((*ArgI)->getType())) {
Constraints.push_back(Constraint(Constraint::Store,
getNode(CallValue),
getNode(*ArgI), ArgPos++));
} else {
Constraints.push_back(Constraint(Constraint::Store,
getNode (CallValue),
UniversalSet, ArgPos++));
}
}
}
if (F && F->getFunctionType()->isVarArg())
for (; ArgI != ArgE; ++ArgI)
if (isa<PointerType>((*ArgI)->getType()))
Constraints.push_back(Constraint(Constraint::Copy, getVarargNode(F),
getNode(*ArgI)));
}
void Andersens::visitCallSite(CallSite CS) {
if (isa<PointerType>(CS.getType()))
getNodeValue(*CS.getInstruction());
if (Function *F = CS.getCalledFunction()) {
AddConstraintsForCall(CS, F);
} else {
AddConstraintsForCall(CS, NULL);
}
}
bool Andersens::Node::intersects(Node *N) const {
return PointsTo->intersects(N->PointsTo);
}
bool Andersens::Node::intersectsIgnoring(Node *N, unsigned Ignoring) const {
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;
}
void dumpToDOUT(SparseBitVector<> *bitmap) {
#ifndef NDEBUG
dump(*bitmap, DOUT);
#endif
}
void Andersens::ClumpAddressTaken() {
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa-renumber"
std::vector<unsigned> Translate;
std::vector<Node> NewGraphNodes;
Translate.resize(GraphNodes.size());
unsigned NewPos = 0;
for (unsigned i = 0; i < Constraints.size(); ++i) {
Constraint &C = Constraints[i];
if (C.Type == Constraint::AddressOf) {
GraphNodes[C.Src].AddressTaken = true;
}
}
for (unsigned i = 0; i < NumberSpecialNodes; ++i) {
unsigned Pos = NewPos++;
Translate[i] = Pos;
NewGraphNodes.push_back(GraphNodes[i]);
DOUT << "Renumbering node " << i << " to node " << Pos << "\n";
}
for (unsigned i = NumberSpecialNodes; i < GraphNodes.size(); ++i) {
if (GraphNodes[i].AddressTaken) {
unsigned Pos = NewPos++;
Translate[i] = Pos;
NewGraphNodes.push_back(GraphNodes[i]);
DOUT << "Renumbering node " << i << " to node " << Pos << "\n";
}
}
for (unsigned i = NumberSpecialNodes; i < GraphNodes.size(); ++i) {
if (!GraphNodes[i].AddressTaken) {
unsigned Pos = NewPos++;
Translate[i] = Pos;
NewGraphNodes.push_back(GraphNodes[i]);
DOUT << "Renumbering node " << i << " to node " << Pos << "\n";
}
}
for (DenseMap<Value*, unsigned>::iterator Iter = ValueNodes.begin();
Iter != ValueNodes.end();
++Iter)
Iter->second = Translate[Iter->second];
for (DenseMap<Value*, unsigned>::iterator Iter = ObjectNodes.begin();
Iter != ObjectNodes.end();
++Iter)
Iter->second = Translate[Iter->second];
for (DenseMap<Function*, unsigned>::iterator Iter = ReturnNodes.begin();
Iter != ReturnNodes.end();
++Iter)
Iter->second = Translate[Iter->second];
for (DenseMap<Function*, unsigned>::iterator Iter = VarargNodes.begin();
Iter != VarargNodes.end();
++Iter)
Iter->second = Translate[Iter->second];
for (unsigned i = 0; i < Constraints.size(); ++i) {
Constraint &C = Constraints[i];
C.Src = Translate[C.Src];
C.Dest = Translate[C.Dest];
}
GraphNodes.swap(NewGraphNodes);
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa"
}
void Andersens::HVN() {
DOUT << "Beginning HVN\n";
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Constraint &C = Constraints[i];
if (C.Type == Constraint::AddressOf) {
GraphNodes[C.Src].AddressTaken = true;
GraphNodes[C.Src].Direct = false;
unsigned AdrNode = C.Src + FirstAdrNode;
if (!GraphNodes[C.Dest].PredEdges)
GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
GraphNodes[C.Dest].PredEdges->set(AdrNode);
unsigned RefNode = C.Dest + FirstRefNode;
if (!GraphNodes[RefNode].ImplicitPredEdges)
GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
GraphNodes[RefNode].ImplicitPredEdges->set(C.Src);
} else if (C.Type == Constraint::Load) {
if (C.Offset == 0) {
if (!GraphNodes[C.Dest].PredEdges)
GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
GraphNodes[C.Dest].PredEdges->set(C.Src + FirstRefNode);
} else {
GraphNodes[C.Dest].Direct = false;
}
} else if (C.Type == Constraint::Store) {
if (C.Offset == 0) {
unsigned RefNode = C.Dest + FirstRefNode;
if (!GraphNodes[RefNode].PredEdges)
GraphNodes[RefNode].PredEdges = new SparseBitVector<>;
GraphNodes[RefNode].PredEdges->set(C.Src);
}
} else {
if (!GraphNodes[C.Dest].PredEdges)
GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
GraphNodes[C.Dest].PredEdges->set(C.Src);
unsigned RefNode = C.Dest + FirstRefNode;
if (!GraphNodes[RefNode].ImplicitPredEdges)
GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
GraphNodes[RefNode].ImplicitPredEdges->set(C.Src + FirstRefNode);
}
}
PEClass = 1;
DFSNumber = 0;
Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
for (unsigned i = 0; i < FirstRefNode; ++i) {
unsigned Node = VSSCCRep[i];
if (!Node2Visited[Node])
HVNValNum(Node);
}
for (BitVectorMap::iterator Iter = Set2PEClass.begin();
Iter != Set2PEClass.end();
++Iter)
delete Iter->first;
Set2PEClass.clear();
Node2DFS.clear();
Node2Deleted.clear();
Node2Visited.clear();
DOUT << "Finished HVN\n";
}
void Andersens::HVNValNum(unsigned NodeIndex) {
unsigned MyDFS = DFSNumber++;
Node *N = &GraphNodes[NodeIndex];
Node2Visited[NodeIndex] = true;
Node2DFS[NodeIndex] = MyDFS;
if (N->PredEdges)
for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
Iter != N->PredEdges->end();
++Iter) {
unsigned j = VSSCCRep[*Iter];
if (!Node2Deleted[j]) {
if (!Node2Visited[j])
HVNValNum(j);
if (Node2DFS[NodeIndex] > Node2DFS[j])
Node2DFS[NodeIndex] = Node2DFS[j];
}
}
if (N->ImplicitPredEdges)
for (SparseBitVector<>::iterator Iter = N->ImplicitPredEdges->begin();
Iter != N->ImplicitPredEdges->end();
++Iter) {
unsigned j = VSSCCRep[*Iter];
if (!Node2Deleted[j]) {
if (!Node2Visited[j])
HVNValNum(j);
if (Node2DFS[NodeIndex] > Node2DFS[j])
Node2DFS[NodeIndex] = Node2DFS[j];
}
}
if (MyDFS == Node2DFS[NodeIndex]) {
while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) {
unsigned CycleNodeIndex = SCCStack.top();
Node *CycleNode = &GraphNodes[CycleNodeIndex];
VSSCCRep[CycleNodeIndex] = NodeIndex;
N->Direct &= CycleNode->Direct;
if (CycleNode->PredEdges) {
if (!N->PredEdges)
N->PredEdges = new SparseBitVector<>;
*(N->PredEdges) |= CycleNode->PredEdges;
delete CycleNode->PredEdges;
CycleNode->PredEdges = NULL;
}
if (CycleNode->ImplicitPredEdges) {
if (!N->ImplicitPredEdges)
N->ImplicitPredEdges = new SparseBitVector<>;
*(N->ImplicitPredEdges) |= CycleNode->ImplicitPredEdges;
delete CycleNode->ImplicitPredEdges;
CycleNode->ImplicitPredEdges = NULL;
}
SCCStack.pop();
}
Node2Deleted[NodeIndex] = true;
if (!N->Direct) {
GraphNodes[NodeIndex].PointerEquivLabel = PEClass++;
return;
}
bool AllSame = true;
unsigned First = ~0;
SparseBitVector<> *Labels = new SparseBitVector<>;
bool Used = false;
if (N->PredEdges)
for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
Iter != N->PredEdges->end();
++Iter) {
unsigned j = VSSCCRep[*Iter];
unsigned Label = GraphNodes[j].PointerEquivLabel;
if (j == NodeIndex || Label == 0)
continue;
if (First == (unsigned)~0)
First = Label;
else if (First != Label)
AllSame = false;
Labels->set(Label);
}
if (Labels->empty()) {
GraphNodes[NodeIndex].PointerEquivLabel = 0;
} else if (AllSame) {
GraphNodes[NodeIndex].PointerEquivLabel = First;
} else {
GraphNodes[NodeIndex].PointerEquivLabel = Set2PEClass[Labels];
if (GraphNodes[NodeIndex].PointerEquivLabel == 0) {
unsigned EquivClass = PEClass++;
Set2PEClass[Labels] = EquivClass;
GraphNodes[NodeIndex].PointerEquivLabel = EquivClass;
Used = true;
}
}
if (!Used)
delete Labels;
} else {
SCCStack.push(NodeIndex);
}
}
void Andersens::HU() {
DOUT << "Beginning HU\n";
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Constraint &C = Constraints[i];
if (C.Type == Constraint::AddressOf) {
GraphNodes[C.Src].AddressTaken = true;
GraphNodes[C.Src].Direct = false;
GraphNodes[C.Dest].PointsTo->set(C.Src);
unsigned RefNode = C.Dest + FirstRefNode;
if (!GraphNodes[RefNode].ImplicitPredEdges)
GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
GraphNodes[RefNode].ImplicitPredEdges->set(C.Src);
GraphNodes[C.Src].PointedToBy->set(C.Dest);
} else if (C.Type == Constraint::Load) {
if (C.Offset == 0) {
if (!GraphNodes[C.Dest].PredEdges)
GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
GraphNodes[C.Dest].PredEdges->set(C.Src + FirstRefNode);
} else {
GraphNodes[C.Dest].Direct = false;
}
} else if (C.Type == Constraint::Store) {
if (C.Offset == 0) {
unsigned RefNode = C.Dest + FirstRefNode;
if (!GraphNodes[RefNode].PredEdges)
GraphNodes[RefNode].PredEdges = new SparseBitVector<>;
GraphNodes[RefNode].PredEdges->set(C.Src);
}
} else {
if (!GraphNodes[C.Dest].PredEdges)
GraphNodes[C.Dest].PredEdges = new SparseBitVector<>;
GraphNodes[C.Dest].PredEdges->set(C.Src);
unsigned RefNode = C.Dest + FirstRefNode;
if (!GraphNodes[RefNode].ImplicitPredEdges)
GraphNodes[RefNode].ImplicitPredEdges = new SparseBitVector<>;
GraphNodes[RefNode].ImplicitPredEdges->set(C.Src + FirstRefNode);
}
}
PEClass = 1;
DFSNumber = 0;
Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
for (unsigned i = 0; i < FirstRefNode; ++i) {
if (FindNode(i) == i) {
unsigned Node = VSSCCRep[i];
if (!Node2Visited[Node])
Condense(Node);
}
}
Node2DFS.clear();
Node2Visited.clear();
Node2Deleted.clear();
Set2PEClass.resize(GraphNodes.size());
Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
for (unsigned i = 0; i < FirstRefNode; ++i) {
if (FindNode(i) == i) {
unsigned Node = VSSCCRep[i];
if (!Node2Visited[Node])
HUValNum(Node);
}
}
Set2PEClass.clear();
DOUT << "Finished HU\n";
}
void Andersens::Condense(unsigned NodeIndex) {
unsigned MyDFS = DFSNumber++;
Node *N = &GraphNodes[NodeIndex];
Node2Visited[NodeIndex] = true;
Node2DFS[NodeIndex] = MyDFS;
if (N->PredEdges)
for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
Iter != N->PredEdges->end();
++Iter) {
unsigned j = VSSCCRep[*Iter];
if (!Node2Deleted[j]) {
if (!Node2Visited[j])
Condense(j);
if (Node2DFS[NodeIndex] > Node2DFS[j])
Node2DFS[NodeIndex] = Node2DFS[j];
}
}
if (N->ImplicitPredEdges)
for (SparseBitVector<>::iterator Iter = N->ImplicitPredEdges->begin();
Iter != N->ImplicitPredEdges->end();
++Iter) {
unsigned j = VSSCCRep[*Iter];
if (!Node2Deleted[j]) {
if (!Node2Visited[j])
Condense(j);
if (Node2DFS[NodeIndex] > Node2DFS[j])
Node2DFS[NodeIndex] = Node2DFS[j];
}
}
if (MyDFS == Node2DFS[NodeIndex]) {
while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) {
unsigned CycleNodeIndex = SCCStack.top();
Node *CycleNode = &GraphNodes[CycleNodeIndex];
VSSCCRep[CycleNodeIndex] = NodeIndex;
N->Direct &= CycleNode->Direct;
*(N->PointsTo) |= CycleNode->PointsTo;
delete CycleNode->PointsTo;
CycleNode->PointsTo = NULL;
if (CycleNode->PredEdges) {
if (!N->PredEdges)
N->PredEdges = new SparseBitVector<>;
*(N->PredEdges) |= CycleNode->PredEdges;
delete CycleNode->PredEdges;
CycleNode->PredEdges = NULL;
}
if (CycleNode->ImplicitPredEdges) {
if (!N->ImplicitPredEdges)
N->ImplicitPredEdges = new SparseBitVector<>;
*(N->ImplicitPredEdges) |= CycleNode->ImplicitPredEdges;
delete CycleNode->ImplicitPredEdges;
CycleNode->ImplicitPredEdges = NULL;
}
SCCStack.pop();
}
Node2Deleted[NodeIndex] = true;
if (N->PredEdges)
for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
Iter != N->PredEdges->end();
++Iter)
++GraphNodes[VSSCCRep[*Iter]].NumInEdges;
} else {
SCCStack.push(NodeIndex);
}
}
void Andersens::HUValNum(unsigned NodeIndex) {
Node *N = &GraphNodes[NodeIndex];
Node2Visited[NodeIndex] = true;
if (NodeIndex >= FirstRefNode) {
unsigned j = VSSCCRep[FindNode(NodeIndex - FirstRefNode)];
if ((Node2Visited[j] && !GraphNodes[j].PointerEquivLabel)
|| (GraphNodes[j].Direct && !GraphNodes[j].PredEdges
&& GraphNodes[j].PointsTo->empty())){
return;
}
}
if (N->PredEdges)
for (SparseBitVector<>::iterator Iter = N->PredEdges->begin();
Iter != N->PredEdges->end();
++Iter) {
unsigned j = VSSCCRep[*Iter];
if (!Node2Visited[j])
HUValNum(j);
if (j == NodeIndex || GraphNodes[j].PointerEquivLabel == 0) {
--GraphNodes[j].NumInEdges;
continue;
}
*(N->PointsTo) |= GraphNodes[j].PointsTo;
--GraphNodes[j].NumInEdges;
if (!GraphNodes[j].NumInEdges && !GraphNodes[j].StoredInHash) {
delete GraphNodes[j].PointsTo;
GraphNodes[j].PointsTo = NULL;
}
}
if (!N->Direct) {
N->PointsTo->set(FirstRefNode + NodeIndex);
}
if (N->PointsTo->empty()) {
delete N->PointsTo;
N->PointsTo = NULL;
} else {
if (N->Direct) {
N->PointerEquivLabel = Set2PEClass[N->PointsTo];
if (N->PointerEquivLabel == 0) {
unsigned EquivClass = PEClass++;
N->StoredInHash = true;
Set2PEClass[N->PointsTo] = EquivClass;
N->PointerEquivLabel = EquivClass;
}
} else {
N->PointerEquivLabel = PEClass++;
}
}
}
void Andersens::RewriteConstraints() {
std::vector<Constraint> NewConstraints;
DenseSet<Constraint, ConstraintKeyInfo> Seen;
PEClass2Node.clear();
PENLEClass2Node.clear();
PEClass2Node.insert(PEClass2Node.begin(), GraphNodes.size() + 1, -1);
PENLEClass2Node.insert(PENLEClass2Node.begin(), GraphNodes.size() + 1, -1);
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Constraint &C = Constraints[i];
unsigned RHSNode = FindNode(C.Src);
unsigned LHSNode = FindNode(C.Dest);
unsigned RHSLabel = GraphNodes[VSSCCRep[RHSNode]].PointerEquivLabel;
unsigned LHSLabel = GraphNodes[VSSCCRep[LHSNode]].PointerEquivLabel;
if (LHSLabel == 0) {
DEBUG(PrintNode(&GraphNodes[LHSNode]));
DOUT << " is a non-pointer, ignoring constraint.\n";
continue;
}
if (RHSLabel == 0) {
DEBUG(PrintNode(&GraphNodes[RHSNode]));
DOUT << " is a non-pointer, ignoring constraint.\n";
continue;
}
if (C.Src == C.Dest && C.Type == Constraint::Copy)
continue;
C.Src = FindEquivalentNode(RHSNode, RHSLabel);
C.Dest = FindEquivalentNode(FindNode(LHSNode), LHSLabel);
if ((C.Src == C.Dest && C.Type == Constraint::Copy)
|| Seen.count(C))
continue;
Seen.insert(C);
NewConstraints.push_back(C);
}
Constraints.swap(NewConstraints);
PEClass2Node.clear();
}
unsigned Andersens::FindEquivalentNode(unsigned NodeIndex,
unsigned NodeLabel) {
if (!GraphNodes[NodeIndex].AddressTaken) {
if (PEClass2Node[NodeLabel] != -1) {
return UniteNodes(PEClass2Node[NodeLabel], NodeIndex, false);
} else {
PEClass2Node[NodeLabel] = NodeIndex;
PENLEClass2Node[NodeLabel] = NodeIndex;
}
} else if (PENLEClass2Node[NodeLabel] == -1) {
PENLEClass2Node[NodeLabel] = NodeIndex;
}
return NodeIndex;
}
void Andersens::PrintLabels() const {
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
if (i < FirstRefNode) {
PrintNode(&GraphNodes[i]);
} else if (i < FirstAdrNode) {
DOUT << "REF(";
PrintNode(&GraphNodes[i-FirstRefNode]);
DOUT <<")";
} else {
DOUT << "ADR(";
PrintNode(&GraphNodes[i-FirstAdrNode]);
DOUT <<")";
}
DOUT << " has pointer label " << GraphNodes[i].PointerEquivLabel
<< " and SCC rep " << VSSCCRep[i]
<< " and is " << (GraphNodes[i].Direct ? "Direct" : "Not direct")
<< "\n";
}
}
void Andersens::HCD() {
DOUT << "Starting HCD.\n";
HCDSCCRep.resize(GraphNodes.size());
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
GraphNodes[i].Edges = new SparseBitVector<>;
HCDSCCRep[i] = i;
}
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) {
continue;
} else if (C.Type == Constraint::Load) {
if( C.Offset == 0 )
GraphNodes[C.Dest].Edges->set(C.Src + FirstRefNode);
} else if (C.Type == Constraint::Store) {
if( C.Offset == 0 )
GraphNodes[C.Dest + FirstRefNode].Edges->set(C.Src);
} else {
GraphNodes[C.Dest].Edges->set(C.Src);
}
}
Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
Node2Visited.insert(Node2Visited.begin(), GraphNodes.size(), false);
SDT.insert(SDT.begin(), GraphNodes.size() / 2, -1);
DFSNumber = 0;
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
unsigned Node = HCDSCCRep[i];
if (!Node2Deleted[Node])
Search(Node);
}
for (unsigned i = 0; i < GraphNodes.size(); ++i)
if (GraphNodes[i].Edges != NULL) {
delete GraphNodes[i].Edges;
GraphNodes[i].Edges = NULL;
}
while( !SCCStack.empty() )
SCCStack.pop();
Node2DFS.clear();
Node2Visited.clear();
Node2Deleted.clear();
HCDSCCRep.clear();
DOUT << "HCD complete.\n";
}
void Andersens::Search(unsigned Node) {
unsigned MyDFS = DFSNumber++;
Node2Visited[Node] = true;
Node2DFS[Node] = MyDFS;
for (SparseBitVector<>::iterator Iter = GraphNodes[Node].Edges->begin(),
End = GraphNodes[Node].Edges->end();
Iter != End;
++Iter) {
unsigned J = HCDSCCRep[*Iter];
assert(GraphNodes[J].isRep() && "Debug check; must be representative");
if (!Node2Deleted[J]) {
if (!Node2Visited[J])
Search(J);
if (Node2DFS[Node] > Node2DFS[J])
Node2DFS[Node] = Node2DFS[J];
}
}
if( MyDFS != Node2DFS[Node] ) {
SCCStack.push(Node);
return;
}
if (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS) {
SparseBitVector<> SCC;
SCC.set(Node);
bool Ref = (Node >= FirstRefNode);
Node2Deleted[Node] = true;
do {
unsigned P = SCCStack.top(); SCCStack.pop();
Ref |= (P >= FirstRefNode);
SCC.set(P);
HCDSCCRep[P] = Node;
} while (!SCCStack.empty() && Node2DFS[SCCStack.top()] >= MyDFS);
if (Ref) {
unsigned Rep = SCC.find_first();
assert(Rep < FirstRefNode && "The SCC didn't have a non-Ref node!");
SparseBitVector<>::iterator i = SCC.begin();
while( *i < FirstRefNode )
++i;
while( i != SCC.end() )
SDT[ (*i++) - FirstRefNode ] = Rep;
}
}
}
void Andersens::OptimizeConstraints() {
DOUT << "Beginning constraint optimization\n";
SDTActive = false;
for (std::map<unsigned, unsigned>::iterator Iter = MaxK.begin();
Iter != MaxK.end();
++Iter) {
for (unsigned i = Iter->first;
i != Iter->first + Iter->second;
++i) {
GraphNodes[i].AddressTaken = true;
GraphNodes[i].Direct = false;
}
}
ClumpAddressTaken();
FirstRefNode = GraphNodes.size();
FirstAdrNode = FirstRefNode + GraphNodes.size();
GraphNodes.insert(GraphNodes.end(), 2 * GraphNodes.size(),
Node(false));
VSSCCRep.resize(GraphNodes.size());
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
VSSCCRep[i] = i;
}
HVN();
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
Node *N = &GraphNodes[i];
delete N->PredEdges;
N->PredEdges = NULL;
delete N->ImplicitPredEdges;
N->ImplicitPredEdges = NULL;
}
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa-labels"
DEBUG(PrintLabels());
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa"
RewriteConstraints();
GraphNodes.resize(FirstRefNode * 2);
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
Node *N = &GraphNodes[i];
if (FindNode(i) == i) {
N->PointsTo = new SparseBitVector<>;
N->PointedToBy = new SparseBitVector<>;
}
VSSCCRep[i] = i;
N->PointerEquivLabel = 0;
}
HU();
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa-labels"
DEBUG(PrintLabels());
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa"
RewriteConstraints();
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
if (FindNode(i) == i) {
Node *N = &GraphNodes[i];
delete N->PointsTo;
N->PointsTo = NULL;
delete N->PredEdges;
N->PredEdges = NULL;
delete N->ImplicitPredEdges;
N->ImplicitPredEdges = NULL;
delete N->PointedToBy;
N->PointedToBy = NULL;
}
}
HCD();
SDTActive = true;
GraphNodes.erase(GraphNodes.begin() + FirstRefNode, GraphNodes.end());
DOUT << "Finished constraint optimization\n";
FirstRefNode = 0;
FirstAdrNode = 0;
}
void Andersens::UnitePointerEquivalences() {
DOUT << "Uniting remaining pointer equivalences\n";
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
if (GraphNodes[i].AddressTaken && GraphNodes[i].isRep()) {
unsigned Label = GraphNodes[i].PointerEquivLabel;
if (Label && PENLEClass2Node[Label] != -1)
UniteNodes(i, PENLEClass2Node[Label]);
}
}
DOUT << "Finished remaining pointer equivalences\n";
PENLEClass2Node.clear();
}
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);
else
GraphNodes[C.Src].Edges->set(C.Dest);
}
}
bool Andersens::QueryNode(unsigned Node) {
assert(GraphNodes[Node].isRep() && "Querying a non-rep node");
unsigned OurDFS = ++DFSNumber;
SparseBitVector<> ToErase;
SparseBitVector<> NewEdges;
Tarjan2DFS[Node] = OurDFS;
bool Changed = false, Merged = false;
for (SparseBitVector<>::iterator bi = GraphNodes[Node].Edges->begin();
bi != GraphNodes[Node].Edges->end();
++bi) {
unsigned RepNode = FindNode(*bi);
if (RepNode != *bi && NewEdges.test(RepNode)){
ToErase.set(*bi);
continue;
}
if (!Tarjan2Deleted[RepNode]){
if (Tarjan2DFS[RepNode] == 0) {
Changed |= QueryNode(RepNode);
RepNode = FindNode(RepNode);
}
if (Tarjan2DFS[RepNode] < Tarjan2DFS[Node])
Tarjan2DFS[Node] = Tarjan2DFS[RepNode];
}
if (RepNode != *bi) {
ToErase.set(*bi);
NewEdges.set(RepNode);
}
}
GraphNodes[Node].Edges->intersectWithComplement(ToErase);
GraphNodes[Node].Edges |= NewEdges;
if (OurDFS == Tarjan2DFS[Node]) {
while (!SCCStack.empty() && Tarjan2DFS[SCCStack.top()] >= OurDFS) {
Node = UniteNodes(Node, SCCStack.top());
SCCStack.pop();
Merged = true;
}
Tarjan2Deleted[Node] = true;
if (Merged)
NextWL->insert(&GraphNodes[Node]);
} else {
SCCStack.push(Node);
}
return(Changed | Merged);
}
void Andersens::SolveConstraints() {
CurrWL = &w1;
NextWL = &w2;
OptimizeConstraints();
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa-constraints"
DEBUG(PrintConstraints());
#undef DEBUG_TYPE
#define DEBUG_TYPE "anders-aa"
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();
UnitePointerEquivalences();
assert(SCCStack.empty() && "SCC Stack should be empty by now!");
Node2DFS.clear();
Node2Deleted.clear();
Node2DFS.insert(Node2DFS.begin(), GraphNodes.size(), 0);
Node2Deleted.insert(Node2Deleted.begin(), GraphNodes.size(), false);
DFSNumber = 0;
DenseSet<Constraint, ConstraintKeyInfo> Seen;
DenseSet<std::pair<unsigned,unsigned>, PairKeyInfo> EdgesChecked;
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
Node *INode = &GraphNodes[i];
if (INode->isRep() && !INode->PointsTo->empty()
&& (!INode->Edges->empty() || !INode->Constraints.empty())) {
INode->Stamp();
CurrWL->insert(INode);
}
}
std::queue<unsigned int> TarjanWL;
#if !FULL_UNIVERSAL
std::vector<unsigned int> RSV;
#endif
while( !CurrWL->empty() ) {
DOUT << "Starting iteration #" << ++NumIters << "\n";
Node* CurrNode;
unsigned CurrNodeIndex;
if (!TarjanWL.empty()) {
DFSNumber = 0;
Tarjan2DFS.clear();
Tarjan2Deleted.clear();
while (!TarjanWL.empty()) {
unsigned int ToTarjan = TarjanWL.front();
TarjanWL.pop();
if (!Tarjan2Deleted[ToTarjan]
&& GraphNodes[ToTarjan].isRep()
&& Tarjan2DFS[ToTarjan] == 0)
QueryNode(ToTarjan);
}
}
while( (CurrNode = CurrWL->pop()) != NULL ) {
CurrNodeIndex = CurrNode - &GraphNodes[0];
CurrNode->Stamp();
SparseBitVector<> CurrPointsTo;
CurrPointsTo.intersectWithComplement(CurrNode->PointsTo,
CurrNode->OldPointsTo);
if (CurrPointsTo.empty())
continue;
*(CurrNode->OldPointsTo) |= CurrPointsTo;
bool SCC = false;
unsigned Rep;
if (SDT[CurrNodeIndex] >= 0) {
SCC = true;
Rep = FindNode(SDT[CurrNodeIndex]);
#if !FULL_UNIVERSAL
RSV.clear();
#endif
for (SparseBitVector<>::iterator bi = CurrPointsTo.begin();
bi != CurrPointsTo.end(); ++bi) {
unsigned Node = FindNode(*bi);
#if !FULL_UNIVERSAL
if (Node < NumberSpecialNodes) {
RSV.push_back(Node);
continue;
}
#endif
Rep = UniteNodes(Rep,Node);
}
#if !FULL_UNIVERSAL
RSV.push_back(Rep);
#endif
NextWL->insert(&GraphNodes[Rep]);
if ( ! CurrNode->isRep() )
continue;
}
Seen.clear();
for (std::list<Constraint>::iterator li = CurrNode->Constraints.begin();
li != CurrNode->Constraints.end(); ) {
li->Src = FindNode(li->Src);
li->Dest = FindNode(li->Dest);
if( Seen.count(*li) ) {
std::list<Constraint>::iterator lk = li; li++;
CurrNode->Constraints.erase(lk);
++NumErased;
continue;
}
Seen.insert(*li);
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 {
li++;
continue;
}
if( SCC && K == 0 ) {
#if FULL_UNIVERSAL
CurrMember = Rep;
if (GraphNodes[*Src].Edges->test_and_set(*Dest))
if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo))
NextWL->insert(&GraphNodes[*Dest]);
#else
for (unsigned i=0; i < RSV.size(); ++i) {
CurrMember = RSV[i];
if (*Dest < NumberSpecialNodes)
continue;
if (GraphNodes[*Src].Edges->test_and_set(*Dest))
if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo))
NextWL->insert(&GraphNodes[*Dest]);
}
#endif
std::list<Constraint>::iterator lk = li; li++;
#if !FULL_UNIVERSAL
if( lk->Type != Constraint::Load)
#endif
GraphNodes[CurrNodeIndex].Constraints.erase(lk);
} else {
const SparseBitVector<> &Solution = CurrPointsTo;
for (SparseBitVector<>::iterator bi = Solution.begin();
bi != Solution.end();
++bi) {
CurrMember = *bi;
if (K > 0 && K > MaxK[CurrMember])
continue;
else
CurrMember = FindNode(CurrMember + K);
#if !FULL_UNIVERSAL
if (*Dest < NumberSpecialNodes)
continue;
#endif
if (GraphNodes[*Src].Edges->test_and_set(*Dest))
if (GraphNodes[*Dest].PointsTo |= *(GraphNodes[*Src].PointsTo))
NextWL->insert(&GraphNodes[*Dest]);
}
li++;
}
}
SparseBitVector<> NewEdges;
SparseBitVector<> ToErase;
for (SparseBitVector<>::iterator bi = CurrNode->Edges->begin();
bi != CurrNode->Edges->end();
++bi) {
unsigned DestVar = *bi;
unsigned Rep = FindNode(DestVar);
if (Rep == CurrNodeIndex ||
(Rep != DestVar && NewEdges.test(Rep))) {
ToErase.set(DestVar);
continue;
}
std::pair<unsigned,unsigned> edge(CurrNodeIndex,Rep);
if (!EdgesChecked.count(edge) &&
*(GraphNodes[Rep].PointsTo) == *(CurrNode->PointsTo)) {
EdgesChecked.insert(edge);
TarjanWL.push(Rep);
}
#if !FULL_UNIVERSAL
if (Rep >= NumberSpecialNodes)
#endif
if (GraphNodes[Rep].PointsTo |= CurrPointsTo) {
NextWL->insert(&GraphNodes[Rep]);
}
if (Rep != DestVar) {
ToErase.set(DestVar);
NewEdges.set(Rep);
}
}
CurrNode->Edges->intersectWithComplement(ToErase);
CurrNode->Edges |= NewEdges;
}
WorkList* t = CurrWL; CurrWL = NextWL; NextWL = t;
}
Node2DFS.clear();
Node2Deleted.clear();
for (unsigned i = 0; i < GraphNodes.size(); ++i) {
Node *N = &GraphNodes[i];
delete N->OldPointsTo;
delete N->Edges;
}
SDTActive = false;
SDT.clear();
}
unsigned Andersens::UniteNodes(unsigned First, unsigned Second,
bool UnionByRank) {
assert (First < GraphNodes.size() && Second < GraphNodes.size() &&
"Attempting to merge nodes that don't exist");
Node *FirstNode = &GraphNodes[First];
Node *SecondNode = &GraphNodes[Second];
assert (SecondNode->isRep() && FirstNode->isRep() &&
"Trying to unite two non-representative nodes!");
if (First == Second)
return First;
if (UnionByRank) {
int RankFirst = (int) FirstNode ->NodeRep;
int RankSecond = (int) SecondNode->NodeRep;
if (RankFirst > RankSecond) {
unsigned t = First; First = Second; Second = t;
Node* tp = FirstNode; FirstNode = SecondNode; SecondNode = tp;
} else if (RankFirst == RankSecond) {
FirstNode->NodeRep = (unsigned) (RankFirst - 1);
}
}
SecondNode->NodeRep = First;
#if !FULL_UNIVERSAL
if (First >= NumberSpecialNodes)
#endif
if (FirstNode->PointsTo && SecondNode->PointsTo)
FirstNode->PointsTo |= *(SecondNode->PointsTo);
if (FirstNode->Edges && SecondNode->Edges)
FirstNode->Edges |= *(SecondNode->Edges);
if (!SecondNode->Constraints.empty())
FirstNode->Constraints.splice(FirstNode->Constraints.begin(),
SecondNode->Constraints);
if (FirstNode->OldPointsTo) {
delete FirstNode->OldPointsTo;
FirstNode->OldPointsTo = new SparseBitVector<>;
}
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";
if (SDTActive)
if (SDT[Second] >= 0) {
if (SDT[First] < 0)
SDT[First] = SDT[Second];
else {
UniteNodes( FindNode(SDT[First]), FindNode(SDT[Second]) );
First = FindNode(First);
}
}
return First;
}
unsigned Andersens::FindNode(unsigned NodeIndex) {
assert (NodeIndex < GraphNodes.size()
&& "Attempting to find a node that can't exist");
Node *N = &GraphNodes[NodeIndex];
if (N->isRep())
return NodeIndex;
else
return (N->NodeRep = FindNode(N->NodeRep));
}
unsigned Andersens::FindNode(unsigned NodeIndex) const {
assert (NodeIndex < GraphNodes.size()
&& "Attempting to find a node that can't exist");
const Node *N = &GraphNodes[NodeIndex];
if (N->isRep())
return NodeIndex;
else
return FindNode(N->NodeRep);
}
void Andersens::PrintNode(const Node *N) const {
if (N == &GraphNodes[UniversalSet]) {
cerr << "<universal>";
return;
} else if (N == &GraphNodes[NullPtr]) {
cerr << "<nullptr>";
return;
} else if (N == &GraphNodes[NullObject]) {
cerr << "<null>";
return;
}
if (!N->getValue()) {
cerr << "artificial" << (intptr_t) N;
return;
}
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()) &&
N == &GraphNodes[getReturnNode(F)]) {
cerr << F->getName() << ":retval";
return;
} else if (F->getFunctionType()->isVarArg() &&
N == &GraphNodes[getVarargNode(F)]) {
cerr << F->getName() << ":vararg";
return;
}
}
if (Instruction *I = dyn_cast<Instruction>(V))
cerr << I->getParent()->getParent()->getName() << ":";
else if (Argument *Arg = dyn_cast<Argument>(V))
cerr << Arg->getParent()->getName() << ":";
if (V->hasName())
cerr << V->getName();
else
cerr << "(unnamed)";
if (isa<GlobalValue>(V) || isa<AllocationInst>(V))
if (N == &GraphNodes[getObject(V)])
cerr << "<mem>";
}
void Andersens::PrintConstraint(const Constraint &C) const {
if (C.Type == Constraint::Store) {
cerr << "*";
if (C.Offset != 0)
cerr << "(";
}
PrintNode(&GraphNodes[C.Dest]);
if (C.Type == Constraint::Store && C.Offset != 0)
cerr << " + " << C.Offset << ")";
cerr << " = ";
if (C.Type == Constraint::Load) {
cerr << "*";
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 << ")";
cerr << "\n";
}
void Andersens::PrintConstraints() const {
cerr << "Constraints:\n";
for (unsigned i = 0, e = Constraints.size(); i != e; ++i)
PrintConstraint(Constraints[i]);
}
void Andersens::PrintPointsToGraph() const {
cerr << "Points-to graph:\n";
for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) {
const Node *N = &GraphNodes[i];
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";
}
}
}