Selection DAG output as bare DAG, code review

Hello all,

I recently foudn myself wanting to view the basic blocks in the
selection DAG as pure DAGs - so just as a list of edges, with no other
information. I added the below code to the start of the "
void SelectionDAGISel::CodeGenAndEmitDAG()" function. It creates a
separate txt file for each basic block and gives a list of edges
between nodes. The segment of code is below - I'm looking for any
feedback on how I might have done things differently (I'm a very basic
c++ programmer) or if is a much easier way of getting the same
information - or if there are any obvious omissions in the code that
might be affecting the results.

Thanking you all in advance.

Joe

CODE

void SelectionDAGISel::CodeGenAndEmitDAG() {
  std::string GroupName;
    std::cout<<"-Joe: we have entered a block"<<std::endl;
    static int numberOfBlocks=0;
    numberOfBlocks++;
    std::cout<<"-We are in Block:"<<numberOfBlocks<<std::endl;
    //FIRST OF ALL, LET US TRY NUMBERING THE NODES
    int in=0;
    for (SelectionDAG::allnodes_iterator I =
CurDAG->allnodes_begin(),E = CurDAG->allnodes_end(); I != E; ++I)
    {
        I->setNodeId(in++);
    }
    //END NUMBERING THE NODES.

    //LET'S TRY WRITING A FILE
    std::ofstream myfile;
    std::string filename="DDGBLOCK"+convertInt(numberOfBlocks)+".txt";
    myfile.open (filename.c_str());
    myfile << "{\n";
    for (SelectionDAG::allnodes_iterator I =
CurDAG->allnodes_begin(),E = CurDAG->allnodes_end(); I != E; ++I)
    {
        std::cout<<"-Operation "<<I->getOperationName(CurDAG)<<"has
number "<<I->getNodeId()<<std::endl;
        int operands =I->getNumOperands();
        std::cout<<"- has "<<operands<<" operands"<<std::endl;
        for(int i=0;i<operands;i++)
        {
            std::cout<<"-
"<<I->getOperand(i)->getOperationName(CurDAG)<<"number:
"<<I->getOperand(i)->getNodeId()<<std::endl;
            myfile<<"( "<<I->getOperand(i)->getNodeId()<<"
"<<I->getNodeId()<<" )"<<std::endl;

        }
    }
    myfile<<"}"<<std::endl;
    myfile.close();
    //END WRITING A FILE

//rest of the void SelectionDAGISel::CodeGenAndEmitDAG() method
continues as normal...

It's not going to be that helpful to just look at the edges, since the
edges "come out of" and "go into" different parts of the nodes, and
that is really important information. I've attached a simple example
of what it looks like. I made that with Mathematica, with:

g = Import["/tmp/llvm_Cnrx9F/dag.setit.dot", {"DOT", "EdgeRules"}];
Graph[Union[g], ImageSize -> {400, 400}]

Note that this won't scale. That BB was trivial (I believe it was one
assignment). Once you get into anything more complicated, it's just
going to become a ball of spaghetti without the node info.

If you realy want to, you take the dot files produced by
`-view-dag-combine1-dags` and friends
<http://llvm.org/docs/CodeGenerator.html#selectiondag-instruction-selection-process&gt;
and then do some postprocessing to get it into the form that you want.

--Sean Silva

dag-no-nodes.png