nv50/ir: Deal with graph iterators using RAII.

This commit is contained in:
Francisco Jerez 2012-04-06 18:43:29 +02:00 committed by Christoph Bumiller
parent 898b0981b6
commit 78de8c8ab5
6 changed files with 43 additions and 70 deletions

View File

@ -402,16 +402,14 @@ Function::setExit(BasicBlock *bb)
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();
}
@ -466,7 +464,7 @@ Pass::run(Function *func, bool ordered, bool skipPhi)
bool
Pass::doRun(Function *func, bool ordered, bool skipPhi)
{
Iterator *bbIter;
IteratorRef bbIter;
BasicBlock *bb;
Instruction *insn, *next;
@ -487,7 +485,7 @@ Pass::doRun(Function *func, bool ordered, bool skipPhi)
break;
}
}
func->cfg.putIterator(bbIter);
return !err;
}
@ -503,10 +501,9 @@ Function::printCFGraph(const char *filePath)
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();
@ -532,7 +529,6 @@ Function::printCFGraph(const char *filePath)
}
}
}
cfg.putIterator(iter);
fprintf(out, "}\n");
fclose(out);

View File

@ -33,12 +33,8 @@ Graph::Graph()
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)
@ -192,7 +188,7 @@ Graph::Node::reachableBy(Node *node, Node *term)
return pos == this;
}
class DFSIterator : public Graph::GraphIterator
class DFSIterator : public Iterator
{
public:
DFSIterator(Graph *graph, const bool preorder)
@ -241,17 +237,17 @@ protected:
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)
@ -262,10 +258,8 @@ public:
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());
@ -327,27 +321,25 @@ private:
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));

View File

@ -36,12 +36,6 @@ class Graph
public:
class Node;
class GraphIterator : public Iterator
{
public:
virtual ~GraphIterator() { };
};
class Edge
{
public:
@ -162,14 +156,12 @@ 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();
@ -208,11 +200,6 @@ int Graph::Node::getSequence() const
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);

View File

@ -76,19 +76,17 @@ void DominatorTree::debugPrint()
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();
@ -190,33 +188,31 @@ void DominatorTree::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))

View File

@ -117,10 +117,8 @@ CodeEmitter::prepareEmission(Function *func)
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

View File

@ -27,6 +27,7 @@
#include <assert.h>
#include <stdio.h>
#include <map>
#include <memory>
#ifndef NDEBUG
# include <typeinfo>
@ -86,11 +87,14 @@ namespace nv50_ir {
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: