llvm-6502/include/llvm/Analysis/DSGraphTraits.h
Chris Lattner 08db719c4b Dramatically simplify internal DSNode representation, get implementation
*FULLY OPERATIONAL* and safe.  We are now capable of completely analyzing
at LEAST the Olden benchmarks + 181.mcf


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4562 91177308-0d34-0410-b5e6-96231b3b80d8
2002-11-06 06:20:27 +00:00

119 lines
4.0 KiB
C++

//===- DSGraphTraits.h - Provide generic graph interface --------*- C++ -*-===//
//
// This file provides GraphTraits specializations for the DataStructure graph
// nodes, allowing datastructure graphs to be processed by generic graph
// algorithms.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ANALYSIS_DSGRAPHTRAITS_H
#define LLVM_ANALYSIS_DSGRAPHTRAITS_H
#include "llvm/Analysis/DSGraph.h"
#include "Support/GraphTraits.h"
#include "Support/iterator"
#include "Support/STLExtras.h"
class DSNodeIterator : public forward_iterator<const DSNode, ptrdiff_t> {
friend class DSNode;
const DSNode * const Node;
unsigned Offset;
typedef DSNodeIterator _Self;
DSNodeIterator(const DSNode *N) : Node(N), Offset(0) {} // begin iterator
DSNodeIterator(const DSNode *N, bool) // Create end iterator
: Node(N) {
Offset = (N->getSize()+((1 << DS::PointerShift)-1)) &
~((1 << DS::PointerShift)-1);
}
public:
DSNodeIterator(const DSNodeHandle &NH)
: Node(NH.getNode()), Offset(NH.getOffset()) {}
bool operator==(const _Self& x) const {
return Offset == x.Offset;
}
bool operator!=(const _Self& x) const { return !operator==(x); }
const _Self &operator=(const _Self &I) {
assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
Offset = I.Offset;
return *this;
}
pointer operator*() const {
return Node->getLink(Offset).getNode();
}
pointer operator->() const { return operator*(); }
_Self& operator++() { // Preincrement
Offset += (1 << DS::PointerShift);
return *this;
}
_Self operator++(int) { // Postincrement
_Self tmp = *this; ++*this; return tmp;
}
unsigned getOffset() const { return Offset; }
const DSNode *getNode() const { return Node; }
};
// Provide iterators for DSNode...
inline DSNode::iterator DSNode::begin() const { return DSNodeIterator(this); }
inline DSNode::iterator DSNode::end() const { return DSNodeIterator(this, false); }
template <> struct GraphTraits<DSNode*> {
typedef DSNode NodeType;
typedef DSNode::iterator ChildIteratorType;
static NodeType *getEntryNode(NodeType *N) { return N; }
static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
static ChildIteratorType child_end(NodeType *N) { return N->end(); }
};
static DSNode &dereference ( DSNode *N) { return *N; }
static const DSNode &dereferenceC(const DSNode *N) { return *N; }
template <> struct GraphTraits<DSGraph*> {
typedef DSNode NodeType;
typedef DSNode::iterator ChildIteratorType;
typedef std::pointer_to_unary_function<DSNode *, DSNode&> DerefFun;
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
typedef mapped_iterator<std::vector<DSNode*>::iterator,
DerefFun> nodes_iterator;
static nodes_iterator nodes_begin(DSGraph *G) {
return map_iterator(G->getNodes().begin(), DerefFun(dereference));
}
static nodes_iterator nodes_end(DSGraph *G) {
return map_iterator(G->getNodes().end(), DerefFun(dereference));
}
static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
static ChildIteratorType child_end(NodeType *N) { return N->end(); }
};
template <> struct GraphTraits<const DSGraph*> {
typedef const DSNode NodeType;
typedef DSNode::iterator ChildIteratorType;
typedef std::pointer_to_unary_function<const DSNode *,const DSNode&> DerefFun;
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
typedef mapped_iterator<std::vector<DSNode*>::const_iterator,
DerefFun> nodes_iterator;
static nodes_iterator nodes_begin(const DSGraph *G) {
return map_iterator(G->getNodes().begin(), DerefFun(dereferenceC));
}
static nodes_iterator nodes_end(const DSGraph *G) {
return map_iterator(G->getNodes().end(), DerefFun(dereferenceC));
}
static ChildIteratorType child_begin(const NodeType *N) { return N->begin(); }
static ChildIteratorType child_end(const NodeType *N) { return N->end(); }
};
#endif