The order of basic block in MIR

As we know, in a function body, basic blocks are stored by Vec<BasicBlock>. Assume that the basic blocks are ordered from 0 to n.

Are all the predecessor of the i-th basic block smaller than i?

No they are not. In general that isn't even possible due to loops being allowed in MIR.

What if the program is loop-free? Or, we eliminate the back-edge firstly. Can we get the conclusion?

BBs are by default in no particular order, other than that the entry block is always bb0.

If you want them in a normalized order, enable the ReorderBasicBlocks pass -- like the mir-opt tests do -- and they'll be in reverse post-order.

1 Like

Thanks! Get it.