Order of Basic Blocks

LLVMers,

Is there any way to guarantee iteration of the basic blocks from top down or path to path? Currently it looks sort of semi-random, sometimes visiting loop heads first and other times loop tails, is there a way I can visit the BBs top down or path to path?

Thank you.

Your question is a bit vague, but you probably want ReversePostOrderTraversal from include/llvm/ADT/PostOrderIterator.h.

Cameron

Sorry, forgot to add group to CC.

Is there a way to cast the rpo_iterator to a basic block pointer? I need to use the functions of the class Basic Block.

Is there a way to cast the rpo_iterator to a basic block pointer? I need to use the functions of the class Basic Block.

Dereference it.

- Ben

This worked, though the RPO_iterator apparently wasn’t what I was looking for anyways, it seems it doesn’t rreally go top->down.

I have a simple example code, where the block follow this path:

BB0->BB1 (T), BB8 (F)
BB1-> BB2 (T), BB3 (F)
BB2-> BB4
BB3-> BB5
BB4-> BB6
BB5-> BB8 (T), BB5 (F)
BB6-> BB7 (T), BB4 (F)
BB7-> BB8 (T), BB4 (F)
BB8 (end)

So, this is the basic block flow graph. When I iterate using the ReversePostOrderTraversal iterator, the basic blocks vistied are in this order:

BB0, BB1, BB3, BB2, BB7, BB6, BB4, BB5, BB8

Can someone explain why this is and how the BBs are stored so that I can either go top->down or more prefably, depth first search (from the given graph above)?

Thanks.

Actually, I apologize, this worked like a charm, I was looking at the wrong readout. Thanks a lot!