>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
ordering.
(1) mbb->setNumber( mf->addToMBBNumbering( mbb ) );
My 2 cents,
Jeff Kunkel
On Wed, Oct 20, 2010 at 1:52 PM, Bin Zeng <ezengbin at gmail.com>
wrote:> 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
>
> Pred->addSuccessor(MBB);
> const TargetInstrInfo *TII = MF.getTarget().getInstrInfo();
> DebugLoc dl;
> // CALLpcrel32 abort
>
BuildMI(MBB,dl,TII->get(X86::CALLpcrel32)).addExternalSymbol("abort");
> // JNE_4 error_label
>
BuildMI(MBB,dl,TII->get(X86::JNE_4)).addExternalSymbol("error_label");
> // MOV32ri %eax, 0
> BuildMI(MBB,dl,TII->get(X86::MOV32ri),X86::EAX).addImm(0);
> // CALL32r %eax
> // BuildMI(MBB,dl,TII->get(X86::CALL32r)).addReg(X86::EAX);
> MF.insert(I,MBB);
> }
>
> 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.
>
> ~Bin
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>