Bin Zeng
2010-Oct-20 20:21 UTC
[LLVMdev] MachineBasicBlock insertion and use/def list update
Hi all, I am still stumped on the same bug. Did anyone try to insert MachineBasicBlock into a MachineFunction? Any advice will be appreciated. Thanks a lot in advance. ~Bin ---------------------------------------------------------------------------------------------------------------------------- Thanks a lot Jeff. I changed the setNumber function call to MBB->setNumber(mf->addToMBBNumbering(MBB)); But it still enters the infinite loop. It is strange that if I insert all these instructions into an existing MachineBasicBlock, the error disappears. It is due to the way I create or insert MachineBasicBlocks in a wrong way. When each operand is added to an instruction, the use/def list for the function will be updated. It should not happen that an MachineOperand.Contents.Reg.Next points to itself. Anyway, thanks a lot again for your reply. ~Bin On 10/20/2010 3:53 PM, Jeff Kunkel wrote:> 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 >>
Anton Korobeynikov
2010-Oct-20 21:00 UTC
[LLVMdev] MachineBasicBlock insertion and use/def list update
Hello> I am still stumped on the same bug. Did anyone try to insert > MachineBasicBlock into a MachineFunction?There are bunch of working examples, when new MBB is created. Look into current backend sources (e.g. custom instruction inserters) -- With best regards, Anton Korobeynikov Faculty of Mathematics and Mechanics, Saint Petersburg State University