unsigned int
Function::orderInstructions(ArrayList &result)
{
- Iterator *iter;
- for (iter = cfg.iteratorCFG(); !iter->end(); iter->next()) {
+ for (IteratorRef it = cfg.iteratorCFG(); !it->end(); it->next()) {
BasicBlock *bb =
- BasicBlock::get(reinterpret_cast<Graph::Node *>(iter->get()));
+ BasicBlock::get(reinterpret_cast<Graph::Node *>(it->get()));
for (Instruction *insn = bb->getFirst(); insn; insn = insn->next)
result.insert(insn, insn->serial);
}
- cfg.putIterator(iter);
return result.getSize();
}
bool
Pass::doRun(Function *func, bool ordered, bool skipPhi)
{
- Iterator *bbIter;
+ IteratorRef bbIter;
BasicBlock *bb;
Instruction *insn, *next;
break;
}
}
- func->cfg.putIterator(bbIter);
+
return !err;
}
fprintf(out, "digraph G {\n");
- Iterator *iter;
- for (iter = cfg.iteratorDFS(); !iter->end(); iter->next()) {
+ for (IteratorRef it = cfg.iteratorDFS(); !it->end(); it->next()) {
BasicBlock *bb = BasicBlock::get(
- reinterpret_cast<Graph::Node *>(iter->get()));
+ reinterpret_cast<Graph::Node *>(it->get()));
int idA = bb->getId();
for (Graph::EdgeIterator ei = bb->cfg.outgoing(); !ei.end(); ei.next()) {
int idB = BasicBlock::get(ei.getNode())->getId();
}
}
}
- cfg.putIterator(iter);
fprintf(out, "}\n");
fclose(out);
Graph::~Graph()
{
- Iterator *iter = this->safeIteratorDFS();
-
- for (; !iter->end(); iter->next())
- reinterpret_cast<Node *>(iter->get())->cut();
-
- putIterator(iter);
+ for (IteratorRef it = safeIteratorDFS(); !it->end(); it->next())
+ reinterpret_cast<Node *>(it->get())->cut();
}
void Graph::insert(Node *node)
return pos == this;
}
-class DFSIterator : public Graph::GraphIterator
+class DFSIterator : public Iterator
{
public:
DFSIterator(Graph *graph, const bool preorder)
int pos;
};
-Graph::GraphIterator *Graph::iteratorDFS(bool preorder)
+IteratorRef Graph::iteratorDFS(bool preorder)
{
- return new DFSIterator(this, preorder);
+ return IteratorRef(new DFSIterator(this, preorder));
}
-Graph::GraphIterator *Graph::safeIteratorDFS(bool preorder)
+IteratorRef Graph::safeIteratorDFS(bool preorder)
{
return this->iteratorDFS(preorder);
}
-class CFGIterator : public Graph::GraphIterator
+class CFGIterator : public Iterator
{
public:
CFGIterator(Graph *graph)
nodes[graph->getSize()] = 0;
// TODO: argh, use graph->sequence instead of tag and just raise it by > 1
- Iterator *iter;
- for (iter = graph->iteratorDFS(); !iter->end(); iter->next())
- reinterpret_cast<Graph::Node *>(iter->get())->tag = 0;
- graph->putIterator(iter);
+ for (IteratorRef it = graph->iteratorDFS(); !it->end(); it->next())
+ reinterpret_cast<Graph::Node *>(it->get())->tag = 0;
if (graph->getRoot())
search(graph->getRoot(), graph->nextSequence());
int pos;
};
-Graph::GraphIterator *Graph::iteratorCFG()
+IteratorRef Graph::iteratorCFG()
{
- return new CFGIterator(this);
+ return IteratorRef(new CFGIterator(this));
}
-Graph::GraphIterator *Graph::safeIteratorCFG()
+IteratorRef Graph::safeIteratorCFG()
{
return this->iteratorCFG();
}
void Graph::classifyEdges()
{
- DFSIterator *iter;
int seq;
- for (iter = new DFSIterator(this, true); !iter->end(); iter->next()) {
- Node *node = reinterpret_cast<Node *>(iter->get());
+ for (IteratorRef it = iteratorDFS(true); !it->end(); it->next()) {
+ Node *node = reinterpret_cast<Node *>(it->get());
node->visit(0);
node->tag = 0;
}
- putIterator(iter);
classifyDFS(root, (seq = 0));
public:
class Node;
- class GraphIterator : public Iterator
- {
- public:
- virtual ~GraphIterator() { };
- };
-
class Edge
{
public:
void insert(Node *node); // attach to or set as root
- GraphIterator *iteratorDFS(bool preorder = true);
- GraphIterator *iteratorCFG();
+ IteratorRef iteratorDFS(bool preorder = true);
+ IteratorRef iteratorCFG();
// safe iterators are unaffected by changes to the *edges* of the graph
- GraphIterator *safeIteratorDFS(bool preorder = true);
- GraphIterator *safeIteratorCFG();
-
- inline void putIterator(Iterator *); // should be GraphIterator *
+ IteratorRef safeIteratorDFS(bool preorder = true);
+ IteratorRef safeIteratorCFG();
void classifyEdges();
return visited;
}
-void Graph::putIterator(Iterator *iter)
-{
- delete reinterpret_cast<GraphIterator *>(iter);
-}
-
Graph::EdgeIterator Graph::Node::outgoing(bool reverse) const
{
return EdgeIterator(out, 0, reverse);
DominatorTree::DominatorTree(Graph *cfgraph) : cfg(cfgraph),
count(cfg->getSize())
{
- Iterator *iter;
- int i;
+ int i = 0;
vert = new Node * [count];
data = new int[5 * count];
- for (i = 0, iter = cfg->iteratorDFS(true); !iter->end(); iter->next(), ++i) {
- vert[i] = reinterpret_cast<Node *>(iter->get());
+ for (IteratorRef it = cfg->iteratorDFS(true); !it->end(); it->next(), ++i) {
+ vert[i] = reinterpret_cast<Node *>(it->get());
vert[i]->tag = i;
LABEL(i) = i;
SEMI(i) = ANCESTOR(i) = -1;
}
- cfg->putIterator(iter);
build();
void DominatorTree::findDominanceFrontiers()
{
- Iterator *dtIter;
BasicBlock *bb;
- for (dtIter = this->iteratorDFS(false); !dtIter->end(); dtIter->next()) {
- EdgeIterator succIter, chldIter;
+ for (IteratorRef dtIt = iteratorDFS(false); !dtIt->end(); dtIt->next()) {
+ EdgeIterator succIt, chldIt;
- bb = BasicBlock::get(reinterpret_cast<Node *>(dtIter->get()));
+ bb = BasicBlock::get(reinterpret_cast<Node *>(dtIt->get()));
bb->getDF().clear();
- for (succIter = bb->cfg.outgoing(); !succIter.end(); succIter.next()) {
- BasicBlock *dfLocal = BasicBlock::get(succIter.getNode());
+ for (succIt = bb->cfg.outgoing(); !succIt.end(); succIt.next()) {
+ BasicBlock *dfLocal = BasicBlock::get(succIt.getNode());
if (dfLocal->idom() != bb)
bb->getDF().insert(dfLocal);
}
- for (chldIter = bb->dom.outgoing(); !chldIter.end(); chldIter.next()) {
- BasicBlock *cb = BasicBlock::get(chldIter.getNode());
+ for (chldIt = bb->dom.outgoing(); !chldIt.end(); chldIt.next()) {
+ BasicBlock *cb = BasicBlock::get(chldIt.getNode());
- DLList::Iterator dfIter = cb->getDF().iterator();
- for (; !dfIter.end(); dfIter.next()) {
- BasicBlock *dfUp = BasicBlock::get(dfIter);
+ DLList::Iterator dfIt = cb->getDF().iterator();
+ for (; !dfIt.end(); dfIt.next()) {
+ BasicBlock *dfUp = BasicBlock::get(dfIt);
if (dfUp->idom() != bb)
bb->getDF().insert(dfUp);
}
}
}
- this->putIterator(dtIter);
}
// liveIn(bb) = usedBeforeAssigned(bb) U (liveOut(bb) - assigned(bb))
BasicBlock::get(func->cfg.getRoot())->binPos = func->binPos;
- Graph::GraphIterator *iter;
- for (iter = func->cfg.iteratorCFG(); !iter->end(); iter->next())
- prepareEmission(BasicBlock::get(*iter));
- func->cfg.putIterator(iter);
+ for (IteratorRef it = func->cfg.iteratorCFG(); !it->end(); it->next())
+ prepareEmission(BasicBlock::get(*it));
}
void
#include <assert.h>
#include <stdio.h>
#include <map>
+#include <memory>
#ifndef NDEBUG
# include <typeinfo>
class Iterator
{
public:
+ virtual ~Iterator() { };
virtual void next() = 0;
virtual void *get() const = 0;
virtual bool end() const = 0; // if true, get will return 0
};
+typedef std::auto_ptr<Iterator> IteratorRef;
+
class ManipIterator : public Iterator
{
public: