From 78de8c8ab54c50c96bc3fae2fe0976054e0acd14 Mon Sep 17 00:00:00 2001 From: Francisco Jerez Date: Fri, 6 Apr 2012 18:43:29 +0200 Subject: [PATCH] nv50/ir: Deal with graph iterators using RAII. --- .../drivers/nv50/codegen/nv50_ir_bb.cpp | 16 ++++----- .../drivers/nv50/codegen/nv50_ir_graph.cpp | 36 ++++++++----------- .../drivers/nv50/codegen/nv50_ir_graph.h | 21 +++-------- .../drivers/nv50/codegen/nv50_ir_ssa.cpp | 30 +++++++--------- .../drivers/nv50/codegen/nv50_ir_target.cpp | 6 ++-- .../drivers/nv50/codegen/nv50_ir_util.h | 4 +++ 6 files changed, 43 insertions(+), 70 deletions(-) diff --git a/src/gallium/drivers/nv50/codegen/nv50_ir_bb.cpp b/src/gallium/drivers/nv50/codegen/nv50_ir_bb.cpp index 54d8ef82bb3..991341b3f86 100644 --- a/src/gallium/drivers/nv50/codegen/nv50_ir_bb.cpp +++ b/src/gallium/drivers/nv50/codegen/nv50_ir_bb.cpp @@ -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(iter->get())); + BasicBlock::get(reinterpret_cast(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(iter->get())); + reinterpret_cast(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); diff --git a/src/gallium/drivers/nv50/codegen/nv50_ir_graph.cpp b/src/gallium/drivers/nv50/codegen/nv50_ir_graph.cpp index 069dc485b6a..90147b6bfb8 100644 --- a/src/gallium/drivers/nv50/codegen/nv50_ir_graph.cpp +++ b/src/gallium/drivers/nv50/codegen/nv50_ir_graph.cpp @@ -33,12 +33,8 @@ Graph::Graph() Graph::~Graph() { - Iterator *iter = this->safeIteratorDFS(); - - for (; !iter->end(); iter->next()) - reinterpret_cast(iter->get())->cut(); - - putIterator(iter); + for (IteratorRef it = safeIteratorDFS(); !it->end(); it->next()) + reinterpret_cast(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(iter->get())->tag = 0; - graph->putIterator(iter); + for (IteratorRef it = graph->iteratorDFS(); !it->end(); it->next()) + reinterpret_cast(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(iter->get()); + for (IteratorRef it = iteratorDFS(true); !it->end(); it->next()) { + Node *node = reinterpret_cast(it->get()); node->visit(0); node->tag = 0; } - putIterator(iter); classifyDFS(root, (seq = 0)); diff --git a/src/gallium/drivers/nv50/codegen/nv50_ir_graph.h b/src/gallium/drivers/nv50/codegen/nv50_ir_graph.h index 7f5f03902e3..cfa73c3dd1a 100644 --- a/src/gallium/drivers/nv50/codegen/nv50_ir_graph.h +++ b/src/gallium/drivers/nv50/codegen/nv50_ir_graph.h @@ -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(iter); -} - Graph::EdgeIterator Graph::Node::outgoing(bool reverse) const { return EdgeIterator(out, 0, reverse); diff --git a/src/gallium/drivers/nv50/codegen/nv50_ir_ssa.cpp b/src/gallium/drivers/nv50/codegen/nv50_ir_ssa.cpp index f8ee4d53f53..88a9911816a 100644 --- a/src/gallium/drivers/nv50/codegen/nv50_ir_ssa.cpp +++ b/src/gallium/drivers/nv50/codegen/nv50_ir_ssa.cpp @@ -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(iter->get()); + for (IteratorRef it = cfg->iteratorDFS(true); !it->end(); it->next(), ++i) { + vert[i] = reinterpret_cast(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(dtIter->get())); + bb = BasicBlock::get(reinterpret_cast(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)) diff --git a/src/gallium/drivers/nv50/codegen/nv50_ir_target.cpp b/src/gallium/drivers/nv50/codegen/nv50_ir_target.cpp index 6cd916004bb..c8529a3d395 100644 --- a/src/gallium/drivers/nv50/codegen/nv50_ir_target.cpp +++ b/src/gallium/drivers/nv50/codegen/nv50_ir_target.cpp @@ -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 diff --git a/src/gallium/drivers/nv50/codegen/nv50_ir_util.h b/src/gallium/drivers/nv50/codegen/nv50_ir_util.h index 23759d2ded2..dc6be9f3af8 100644 --- a/src/gallium/drivers/nv50/codegen/nv50_ir_util.h +++ b/src/gallium/drivers/nv50/codegen/nv50_ir_util.h @@ -27,6 +27,7 @@ #include #include #include +#include #ifndef NDEBUG # include @@ -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 IteratorRef; + class ManipIterator : public Iterator { public: -- 2.30.2