Splitting a basic block, replacing it's terminator

Hi,

I want to insert a conditional branch in the middle of a basic block.
To that end, I am doing these steps:

(1) Split the basic block:
bb->splitBasicBlock()

(2) Remove the old terminator:
succ->removePredecessor(bb)
bb->getTerminator()->getParent()

(3) Adding a new terminator:
BranchInst::Create(ifTrue, ifFalse, cnd, "", bb);

That seems to work, but later passes are dieing. When I dump the
function from my debugger, I notice that the new block (the second
half of the block I split) has a "<null operand>" as a predecessor.
What am I doing wrong?

Here's what bb->getParent()->dump shows. bb4.preheader is the
original basic block; bb4.preheader.noExit is the block created by the
splitBasicBlock() operation:

bb4.preheader: ; preds = %bb5, %preheader
      ; blah blah blah
        %6 = icmp slt i32 %timestamp, 0 ; <i1> [#uses=1]
        br i1 %6, label %__mtcg_sync_to_bb7.loopexit, label
%bb4.preheader.noExit

bb4.preheader.noExit: ; preds = %bb4.preheader, <null operand!>
     ; blah blah blah
        br i1 %10, label %bb5, label %bb1

Assuming that the new block will still be a successor of the old block
(just not the unique successor), all you really need to do is:

   BasicBlock* succ = bb->splitBasicBlock();
   bb->getTerminator()->eraseFromParent();
   BranchInst::Create(<ifTrue>, <ifFalse>, <cond>, bb);

John.

Thanks John.

The problem was that I used bb->getTerminator()->removeFromParent(),
but I should have used bb->getTerminator()->eraseFromParent().

Nick