[llvm-dev] Printing MachineBasicBlock in an order that follows the CFG of the MachineFunction

Matthias Braun via llvm-dev llvm-dev at lists.llvm.org
Wed May 17 17:25:49 PDT 2017


> On May 17, 2017, at 5:11 PM, Alex Susu via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
>  Hello.
>    Is there a possibility to generate the ASM program with MachineBasicBlocks in an order that strictly respects the predecessors of each BasicBlock (BB)?
>    I ask because currently there are some cases where actually the BBs are printed in the .s file in a somewhat wrong order - for example, 1 BB does not follow immediately after its first predecessor although it should.
Not sure I fully understand you there, but with loops involved you simply cannot always print all predecessors before the block itself.

>    I would like to mention I am using LLVM checked out from SVN in Jul 2016, but I guess this problem persists in the latest revision.
> 
>    I guess I can modify the current implementation to change the order of the BBs as they are created in the MachineFunction class, s.t. when we later iterate over the BBs to get the right order.
>    Would this be a safe change? If not I can generate the right order when LLVM prints the ASM file, for example in method EmitFunctionBody().
As long as you are in IR you could modify the blocks to be in an arbitrary order (RPO probably gets you closest to what you want).

CodeGen however reorders block for performance anyway (see BranchFolding). This however only works if your target implements TargetInstrInfo::analyzeBranch()  (and whatever else BranchFolding uses).

- Matthias


More information about the llvm-dev mailing list