A way to traverse machine basic blocks in order?

I’m looking for a way to traverse machine basic blocks in a specific order.

Basically I want all blocks that are predecessors to the current block to be traversed before the current block. I’ve looked at MachineDominatorTree but this doesn’t traverse them in quite the way I want them to. Anyone know of a way to do this?



The reverse postorder iterator does what you want. It's defined in ADT/PostOrderIterator.h, and is used like this:

  ReversePostOrderTraversal<Function*> RPOT(&F);
  for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(),
       RE = RPOT.end(); RI != RE; ++RI)
    <work here>

Creating a ReversePostOrderTraversal is not cheap, because it first has to do a postorder walk of the entire graph.



Thanks for the tip. I understand that it is not cheap, but I must traverse the graph in this specific order, so I really don’t have a choice but I only need to create it once per function.

Thanks again, it does what I need and I don’t have to write my own. :smiley: