2002-10-31 06:34:18 +00:00
|
|
|
//===- DSSupport.h - Support for datastructure graphs -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Support for graph nodes, call sites, and types.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_DSSUPPORT_H
|
|
|
|
#define LLVM_ANALYSIS_DSSUPPORT_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
2003-02-01 04:52:08 +00:00
|
|
|
#include "Support/HashExtras.h"
|
|
|
|
#include "Support/hash_set"
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
class Function;
|
|
|
|
class CallInst;
|
|
|
|
class Value;
|
|
|
|
class GlobalValue;
|
|
|
|
class Type;
|
|
|
|
|
|
|
|
class DSNode; // Each node in the graph
|
|
|
|
class DSGraph; // A graph for a function
|
|
|
|
|
2002-11-06 18:02:26 +00:00
|
|
|
namespace DS { // FIXME: After the paper, this should get cleaned up
|
|
|
|
enum { PointerShift = 3, // 64bit ptrs = 3, 32 bit ptrs = 2
|
|
|
|
PointerSize = 1 << PointerShift
|
|
|
|
};
|
2002-11-07 05:21:15 +00:00
|
|
|
|
|
|
|
// isPointerType - Return true if this first class type is big enough to hold
|
|
|
|
// a pointer.
|
|
|
|
//
|
|
|
|
bool isPointerType(const Type *Ty);
|
2002-11-06 06:20:27 +00:00
|
|
|
};
|
|
|
|
|
2002-10-31 06:34:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// DSNodeHandle - Implement a "handle" to a data structure node that takes care
|
|
|
|
/// of all of the add/un'refing of the node to prevent the backpointers in the
|
|
|
|
/// graph from getting out of date. This class represents a "pointer" in the
|
|
|
|
/// graph, whose destination is an indexed offset into a node.
|
|
|
|
///
|
2002-11-02 21:18:53 +00:00
|
|
|
/// Note: some functions that are marked as inline in DSNodeHandle are actually
|
|
|
|
/// defined in DSNode.h because they need knowledge of DSNode operation. Putting
|
|
|
|
/// them in a CPP file wouldn't help making them inlined and keeping DSNode and
|
|
|
|
/// DSNodeHandle (and friends) in one file complicates things.
|
2002-11-06 06:20:27 +00:00
|
|
|
///
|
2002-10-31 06:34:18 +00:00
|
|
|
class DSNodeHandle {
|
|
|
|
DSNode *N;
|
|
|
|
unsigned Offset;
|
2003-02-03 19:11:04 +00:00
|
|
|
void operator==(const DSNode *N); // DISALLOW, use to promote N to nodehandle
|
2002-10-31 06:34:18 +00:00
|
|
|
public:
|
|
|
|
// Allow construction, destruction, and assignment...
|
|
|
|
DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
|
|
|
|
setNode(n);
|
|
|
|
}
|
|
|
|
DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
|
|
|
|
~DSNodeHandle() { setNode((DSNode*)0); }
|
|
|
|
DSNodeHandle &operator=(const DSNodeHandle &H) {
|
|
|
|
setNode(H.N); Offset = H.Offset;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const DSNodeHandle &H) const { // Allow sorting
|
|
|
|
return N < H.N || (N == H.N && Offset < H.Offset);
|
|
|
|
}
|
|
|
|
bool operator>(const DSNodeHandle &H) const { return H < *this; }
|
|
|
|
bool operator==(const DSNodeHandle &H) const { // Allow comparison
|
|
|
|
return N == H.N && Offset == H.Offset;
|
|
|
|
}
|
|
|
|
bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
|
|
|
|
|
2002-11-10 06:48:11 +00:00
|
|
|
inline void swap(DSNodeHandle &H);
|
|
|
|
|
2002-10-31 06:34:18 +00:00
|
|
|
// Allow explicit conversion to DSNode...
|
|
|
|
DSNode *getNode() const { return N; }
|
|
|
|
unsigned getOffset() const { return Offset; }
|
|
|
|
|
|
|
|
inline void setNode(DSNode *N); // Defined inline later...
|
|
|
|
void setOffset(unsigned O) { Offset = O; }
|
|
|
|
|
|
|
|
void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
|
|
|
|
void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
|
|
|
|
|
|
|
|
/// mergeWith - Merge the logical node pointed to by 'this' with the node
|
|
|
|
/// pointed to by 'N'.
|
|
|
|
///
|
|
|
|
void mergeWith(const DSNodeHandle &N);
|
|
|
|
|
|
|
|
// hasLink - Return true if there is a link at the specified offset...
|
|
|
|
inline bool hasLink(unsigned Num) const;
|
|
|
|
|
|
|
|
/// getLink - Treat this current node pointer as a pointer to a structure of
|
|
|
|
/// some sort. This method will return the pointer a mem[this+Num]
|
|
|
|
///
|
2002-11-06 06:20:27 +00:00
|
|
|
inline const DSNodeHandle &getLink(unsigned Num) const;
|
|
|
|
inline DSNodeHandle &getLink(unsigned Num);
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
inline void setLink(unsigned Num, const DSNodeHandle &NH);
|
|
|
|
};
|
|
|
|
|
2002-11-10 06:48:11 +00:00
|
|
|
inline void swap(DSNodeHandle &NH1, DSNodeHandle &NH2) { NH1.swap(NH2); }
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// DSCallSite - Representation of a call site via its call instruction,
|
|
|
|
/// the DSNode handle for the callee function (or function pointer), and
|
|
|
|
/// the DSNode handles for the function arguments.
|
|
|
|
///
|
|
|
|
class DSCallSite {
|
2003-02-05 21:59:58 +00:00
|
|
|
CallInst *Inst; // Actual call site
|
|
|
|
Function *CalleeF; // The function called (direct call)
|
|
|
|
DSNodeHandle CalleeN; // The function node called (indirect call)
|
|
|
|
DSNodeHandle RetVal; // Returned value
|
|
|
|
std::vector<DSNodeHandle> CallArgs;// The pointer arguments
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
|
2003-02-01 04:52:08 +00:00
|
|
|
const hash_map<const DSNode*, DSNode*> &NodeMap) {
|
2002-10-31 06:34:18 +00:00
|
|
|
if (DSNode *N = Src.getNode()) {
|
2003-02-01 04:52:08 +00:00
|
|
|
hash_map<const DSNode*, DSNode*>::const_iterator I = NodeMap.find(N);
|
2002-10-31 06:34:18 +00:00
|
|
|
assert(I != NodeMap.end() && "Not not in mapping!");
|
|
|
|
|
|
|
|
NH.setOffset(Src.getOffset());
|
|
|
|
NH.setNode(I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InitNH(DSNodeHandle &NH, const DSNodeHandle &Src,
|
2003-02-01 04:52:08 +00:00
|
|
|
const hash_map<const DSNode*, DSNodeHandle> &NodeMap) {
|
2002-10-31 06:34:18 +00:00
|
|
|
if (DSNode *N = Src.getNode()) {
|
2003-02-01 04:52:08 +00:00
|
|
|
hash_map<const DSNode*, DSNodeHandle>::const_iterator I = NodeMap.find(N);
|
2002-10-31 06:34:18 +00:00
|
|
|
assert(I != NodeMap.end() && "Not not in mapping!");
|
|
|
|
|
|
|
|
NH.setOffset(Src.getOffset()+I->second.getOffset());
|
|
|
|
NH.setNode(I->second.getNode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DSCallSite(); // DO NOT IMPLEMENT
|
|
|
|
public:
|
|
|
|
/// Constructor. Note - This ctor destroys the argument vector passed in. On
|
|
|
|
/// exit, the argument vector is empty.
|
|
|
|
///
|
2003-02-05 21:59:58 +00:00
|
|
|
DSCallSite(CallInst &inst, const DSNodeHandle &rv, DSNode *Callee,
|
2002-10-31 06:34:18 +00:00
|
|
|
std::vector<DSNodeHandle> &Args)
|
2003-02-05 21:59:58 +00:00
|
|
|
: Inst(&inst), CalleeF(0), CalleeN(Callee), RetVal(rv) {
|
|
|
|
assert(Callee && "Null callee node specified for call site!");
|
|
|
|
Args.swap(CallArgs);
|
|
|
|
}
|
|
|
|
DSCallSite(CallInst &inst, const DSNodeHandle &rv, Function *Callee,
|
|
|
|
std::vector<DSNodeHandle> &Args)
|
|
|
|
: Inst(&inst), CalleeF(Callee), RetVal(rv) {
|
|
|
|
assert(Callee && "Null callee function specified for call site!");
|
2002-10-31 06:34:18 +00:00
|
|
|
Args.swap(CallArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
DSCallSite(const DSCallSite &DSCS) // Simple copy ctor
|
2003-02-05 21:59:58 +00:00
|
|
|
: Inst(DSCS.Inst), CalleeF(DSCS.CalleeF), CalleeN(DSCS.CalleeN),
|
|
|
|
RetVal(DSCS.RetVal), CallArgs(DSCS.CallArgs) {}
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
/// Mapping copy constructor - This constructor takes a preexisting call site
|
|
|
|
/// to copy plus a map that specifies how the links should be transformed.
|
|
|
|
/// This is useful when moving a call site from one graph to another.
|
|
|
|
///
|
|
|
|
template<typename MapTy>
|
|
|
|
DSCallSite(const DSCallSite &FromCall, const MapTy &NodeMap) {
|
|
|
|
Inst = FromCall.Inst;
|
|
|
|
InitNH(RetVal, FromCall.RetVal, NodeMap);
|
2003-02-05 21:59:58 +00:00
|
|
|
InitNH(CalleeN, FromCall.CalleeN, NodeMap);
|
|
|
|
CalleeF = FromCall.CalleeF;
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
CallArgs.resize(FromCall.CallArgs.size());
|
|
|
|
for (unsigned i = 0, e = FromCall.CallArgs.size(); i != e; ++i)
|
|
|
|
InitNH(CallArgs[i], FromCall.CallArgs[i], NodeMap);
|
|
|
|
}
|
|
|
|
|
2003-02-05 21:59:58 +00:00
|
|
|
/// isDirectCall - Return true if this call site is a direct call of the
|
|
|
|
/// function specified by getCalleeFunc. If not, it is an indirect call to
|
|
|
|
/// the node specified by getCalleeNode.
|
|
|
|
///
|
|
|
|
bool isDirectCall() const { return CalleeF != 0; }
|
|
|
|
bool isIndirectCall() const { return !isDirectCall(); }
|
|
|
|
|
|
|
|
|
2002-10-31 06:34:18 +00:00
|
|
|
// Accessor functions...
|
|
|
|
Function &getCaller() const;
|
|
|
|
CallInst &getCallInst() const { return *Inst; }
|
|
|
|
DSNodeHandle &getRetVal() { return RetVal; }
|
|
|
|
const DSNodeHandle &getRetVal() const { return RetVal; }
|
2003-02-05 21:59:58 +00:00
|
|
|
|
|
|
|
DSNode *getCalleeNode() const {
|
|
|
|
assert(!CalleeF && CalleeN.getNode()); return CalleeN.getNode();
|
|
|
|
}
|
|
|
|
Function *getCalleeFunc() const {
|
|
|
|
assert(!CalleeN.getNode() && CalleeF); return CalleeF;
|
|
|
|
}
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
unsigned getNumPtrArgs() const { return CallArgs.size(); }
|
|
|
|
|
|
|
|
DSNodeHandle &getPtrArg(unsigned i) {
|
|
|
|
assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
|
|
|
|
return CallArgs[i];
|
|
|
|
}
|
|
|
|
const DSNodeHandle &getPtrArg(unsigned i) const {
|
|
|
|
assert(i < CallArgs.size() && "Argument to getPtrArgNode is out of range!");
|
|
|
|
return CallArgs[i];
|
|
|
|
}
|
|
|
|
|
2002-11-10 06:48:11 +00:00
|
|
|
void swap(DSCallSite &CS) {
|
|
|
|
if (this != &CS) {
|
|
|
|
std::swap(Inst, CS.Inst);
|
|
|
|
std::swap(RetVal, CS.RetVal);
|
2003-02-05 21:59:58 +00:00
|
|
|
std::swap(CalleeN, CS.CalleeN);
|
|
|
|
std::swap(CalleeF, CS.CalleeF);
|
2002-11-10 06:48:11 +00:00
|
|
|
std::swap(CallArgs, CS.CallArgs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-10 23:46:51 +00:00
|
|
|
// MergeWith - Merge the return value and parameters of the these two call
|
|
|
|
// sites.
|
|
|
|
void mergeWith(DSCallSite &CS) {
|
|
|
|
getRetVal().mergeWith(CS.getRetVal());
|
|
|
|
unsigned MinArgs = getNumPtrArgs();
|
|
|
|
if (CS.getNumPtrArgs() < MinArgs) MinArgs = CS.getNumPtrArgs();
|
|
|
|
|
|
|
|
for (unsigned a = 0; a != MinArgs; ++a)
|
|
|
|
getPtrArg(a).mergeWith(CS.getPtrArg(a));
|
|
|
|
}
|
|
|
|
|
2003-01-29 21:09:59 +00:00
|
|
|
/// markReachableNodes - This method recursively traverses the specified
|
|
|
|
/// DSNodes, marking any nodes which are reachable. All reachable nodes it
|
|
|
|
/// adds to the set, which allows it to only traverse visited nodes once.
|
|
|
|
///
|
2003-02-01 04:52:08 +00:00
|
|
|
void markReachableNodes(hash_set<DSNode*> &Nodes);
|
2003-01-29 21:09:59 +00:00
|
|
|
|
2002-10-31 06:34:18 +00:00
|
|
|
bool operator<(const DSCallSite &CS) const {
|
2003-02-05 21:59:58 +00:00
|
|
|
if (isDirectCall()) { // This must sort by callee first!
|
|
|
|
if (CS.isIndirectCall()) return true;
|
|
|
|
if (CalleeF < CS.CalleeF) return true;
|
|
|
|
if (CalleeF > CS.CalleeF) return false;
|
|
|
|
} else {
|
|
|
|
if (CS.isDirectCall()) return false;
|
|
|
|
if (CalleeN < CS.CalleeN) return true;
|
|
|
|
if (CalleeN > CS.CalleeN) return false;
|
|
|
|
}
|
2002-10-31 06:34:18 +00:00
|
|
|
if (RetVal < CS.RetVal) return true;
|
|
|
|
if (RetVal > CS.RetVal) return false;
|
|
|
|
return CallArgs < CS.CallArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const DSCallSite &CS) const {
|
2003-02-05 21:59:58 +00:00
|
|
|
return RetVal == CS.RetVal && CalleeN == CS.CalleeN &&
|
|
|
|
CalleeF == CS.CalleeF && CallArgs == CS.CallArgs;
|
2002-10-31 06:34:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-11-10 06:48:11 +00:00
|
|
|
inline void swap(DSCallSite &CS1, DSCallSite &CS2) { CS1.swap(CS2); }
|
2002-10-31 06:34:18 +00:00
|
|
|
|
|
|
|
#endif
|