Depth First Sort of Machine Basic Blocks just before emitting code

For nefarious reasons I won't go into (this is for a quick hack that will be thrown away in a week or two), I'd like my Asm printer to get MBBs in a depth first order.

Is there an existing pass or function that will let me do this easily?



I was not able to find a simple way to do this outside of writing my
own DFS traversal using the succ nodes.


Check out "llvm/ADT/DepthFirstIterator.h", it lets you iterate over arbitrary graphs (including CFGs) in depth first order. grep the source base for df_begin etc to see examples of use.


That looks like it does a preorder depth first traversal (I think). I'm looking for postorder. Is there a trivial transform between the two? (I don't know one.)

Am I wrong about the preorder/postorder? If not, anything lurking for postorder traversal?



po_iterator (ADT/PostOrderIterator.h) ?

Too obvious!



I have used df_iterator (also idf_iterator, which I had to patch up a bit), and po_iterator. They have saved much coding, so many thanks to Chris and everyone for these.