Nick Johnson
2009-May-08 23:02 UTC
[LLVMdev] 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 -- Nick Johnson
John McCall
2009-May-08 23:10 UTC
[LLVMdev] Splitting a basic block, replacing it's terminator
On May 8, 2009, at 4:02 PM, Nick Johnson wrote:> 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()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.
Nick Johnson
2009-May-08 23:19 UTC
[LLVMdev] Splitting a basic block, replacing it's terminator
Thanks John. The problem was that I used bb->getTerminator()->removeFromParent(), but I should have used bb->getTerminator()->eraseFromParent(). Nick On Fri, May 8, 2009 at 7:10 PM, John McCall <rjmccall at apple.com> wrote:> On May 8, 2009, at 4:02 PM, Nick Johnson wrote: >> 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() > > 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. > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-- Nick Johnson
Possibly Parallel Threads
- [LLVMdev] Splitting a basic block, replacing it's terminator
- [LLVMdev] Syntax of 'br', 'switch' and 'indirectbr'
- with(data.frame,ifelse(___,___,___))
- [LLVMdev] all LLVM Instructions that may write to memory -- other than StoreInst?
- [LLVMdev] CodeExtractor.cpp potential bug?