MachineBasicBlock insertion

Hi all,

I am really stumped on a problem for long. I could not figure out why. That is why i am here. OK, here is the problem:
I tried to insert a MachineBasicBlock into a function. Here is the code snippet:

// insert a machine basic block with the error_label into MF and before I
// Pred is the predecessor of the block to be inserted
// the new basic block is inserted right before I
void X86CFIOptPass::insertBasicBlockBefore(MachineFunction &MF,
                        MachineBasicBlock *Pred,
                        MachineFunction::iterator I){
   const BasicBlock* LLVM_BB = Pred->getBasicBlock();
   MachineBasicBlock * MBB = MF.CreateMachineBasicBlock(LLVM_BB); // create a MBB
   MBB->setNumber(19880616); // set the number of MBB to be 19880616 which is used as an ID

   const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
   DebugLoc dl;
   // CALLpcrel32 abort
   // JNE_4 error_label
   // MOV32ri %eax, 0
   // CALL32r %eax
   // BuildMI(MBB,dl,TII->get(X86::CALL32r)).addReg(X86::EAX);

When I tried to dump the code after the insertion, the program enters an infinite loop
in the while loop inside MachineRegisterInfo.h:defusechain_iterator &operator++().
Basically, the CALLpcrel32 instruction has a register operand that points to itself, that is,
Contents.Reg.Next stores the address of itself. Does anyone knows how to insert a MachineBasicBlock
into a function? Any advice will be appreciated. Thanks in advance.


From what I gathered about MachineBasicBlocks are the blocks have to

be added in a certain way. One cannot just choose any number, because
the MachineFunction basically owns a basic block. So the call (1) will
add the number to the block and add the block to the function. The
final mf.insert(mbb) will place where the block will be inserted
relative to other blocks, but the numbering is not in ascending

(1) mbb->setNumber( mf->addToMBBNumbering( mbb ) );

My 2 cents,
Jeff Kunkel