Inserting MachineBasicBlock(s) before a MachineBasicBlock

I want to be able to do two things with LLVM (both just before code
emission):

1. Insert a MachineBasicBlock just before a MachineBasicBlock.
There is a function called AddPredecessor(). However, the comment says that
it does not update the actual CFG. I want to redirect all CFG edges that are
incoming to this MachineBasicBlock to the new one I create, and add just one
outgoing edge (no branch) to the newly formed MachineBasicBlock to jump to
the original MachineBasicBlock. How can I do this?

2. Insert a bunch of MachineInstructions at arbitrary points inside a
MachineBasicBlock.
Using the BuildMI function, I can add an arbitrary instruction just before
the position of a MachineBasicBlock::iterator. After the BuildMI function,
is the MachineBasicBlock::iterator still valid? What if I want to insert an
instruction before the newly added instruction? How should I do that?

Some general questions:

Why are there multiple MachineBasicBlocks per BasicBlock? What is the
motivation behind this representation?

Is there any example in the codebase of doing the above?

Thanks,

The BranchFolding code does CFG manipulation along the lines you want, although not exactly.
You can probably rephrase what you want to do so that SplitMBBAt works.

I want to be able to do two things with LLVM (both just before code
emission):

1. Insert a MachineBasicBlock just before a MachineBasicBlock.
There is a function called AddPredecessor(). However, the comment says that
it does not update the actual CFG. I want to redirect all CFG edges that are
incoming to this MachineBasicBlock to the new one I create, and add just one
outgoing edge (no branch) to the newly formed MachineBasicBlock to jump to
the original MachineBasicBlock. How can I do this?

Hi,

    I had an old pass to break critical edges, and I am sending you the code below. I Last tested it with LLVM 2.1. It inserts a basic block between src and dst.

Fernando