Problem adding a MachineBasicBlock during X86 EmitPrologue

I'm attempting to add an error handler to functions with a custom
calling convention. This error is checked upon function entry, before
any code is run (specifically, I cannot allow any stack operations).
Because of this, I figured a good place to do this code insertion is
in EmitPrologue. I also, at this time, create the block that handles
the error case.

      // create a new block for the overflow handling code
      MF.overflowBlock = MF.CreateMachineBasicBlock();
      MachineFunction::iterator obinsert = MF.begin();
      MF.addToMBBNumbering(MF.overflowBlock);
      MF.push_back(MF.overflowBlock);
      // on overflow jump to a new block to handle overflow
      BuildMI(MBB, MBBI, DL, TII.get(X86::JAE_4)).addMBB(MF.overflowBlock);

      //add dummy instruction to overflow block
      BuildMI(MF.overflowBlock, DL, TII.get(X86::RET));

The problem is that the block doesn't appear in generate code if the
code starts with more than one basic block. I get x86 code that looks
something like this:

_main:
    ...
    jae "LBB1_-1"
    ...

There is no label created "LBB1_-1" and the block containing the ret
instruction is missing.

If I attempt to do an insertion operation, assuming it doesn't insert
at the end() of the machine function, the code in MF.overflowBlock
replaces the code in the block that the iterator points to.

How am I doing this wrong?

Thanks much,
Arlen

I think I can answer my own question here. Somebody correct me if I am wrong.

I shouldn't have added this where I did. It should have been added as
its own MachineFunctionPass. The reason the block was disappearing
was either due to a later pass clobbering it or due to the verifier.
It seems that all other return blocks have a different return
associated (such as RET %EAX<imp-use,kill>), where as this one is just
a plain RET.

By moving my pass later in the passes, I placed it after the verifier
is no longer run and now not many passes are run after it. Now the
block shows up numbered correctly.

Thanks for any further feedback.

-Arlen