Charitha Saumya via llvm-dev
2020-Feb-07  04:17 UTC
[llvm-dev] Removing a redundant unconditional branch
Hi,
I want to remove a redundant unconditional branch from a Function. In the
following example I want to remove br label %26 and merge them to a single
basic block.
; <label>:9:                                      ; preds = %7
  %10 = fadd float %5, %8
  %11 = fmul float %5, %8
  %12 = fadd float %10, %11
  %13 = fdiv float %5, %8
  %14 = fadd float %13, %12
  br label %15
; <label>:15:                                     ; preds = %9
  br label %26
I tried to do this as follow but ended up getting runtime error. Can
someone help?
Thanks
Charitha
for(auto it1 = F.begin(); it1 != F.end(); it1++){
    BasicBlock& bb = *it1;
    auto BI = dyn_cast<BranchInst>(bb.getTerminator());
    if(BI && BI->isUnconditional() && bb.size() == 1){
        for (BasicBlock *pred : predecessors(&bb)) {
            auto predBI = dyn_cast<BranchInst>(pred->getTerminator());
            if(predBI && predBI->isUnconditional()){
                    predBI->setSuccessor(0, bb.getSingleSuccessor());
                    BI->dropAllReferences();
                    BI->removeFromParent();
            }
        }
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20200206/7da25ff0/attachment.html>
Hiroshi Yamauchi via llvm-dev
2020-Feb-07  16:59 UTC
[llvm-dev] Removing a redundant unconditional branch
Perhaps, you'd need to also check that the block %15 has only one predecessor and has no other users? Posting the error message might help. On Thu, Feb 6, 2020 at 8:17 PM Charitha Saumya via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > I want to remove a redundant unconditional branch from a Function. In the > following example I want to remove br label %26 and merge them to a > single basic block. > > ; <label>:9: ; preds = %7 > %10 = fadd float %5, %8 > %11 = fmul float %5, %8 > %12 = fadd float %10, %11 > %13 = fdiv float %5, %8 > %14 = fadd float %13, %12 > br label %15 > ; <label>:15: ; preds = %9 > br label %26 > > I tried to do this as follow but ended up getting runtime error. Can > someone help? > > Thanks > > Charitha > for(auto it1 = F.begin(); it1 != F.end(); it1++){ > BasicBlock& bb = *it1; > auto BI = dyn_cast<BranchInst>(bb.getTerminator()); > if(BI && BI->isUnconditional() && bb.size() == 1){ > > for (BasicBlock *pred : predecessors(&bb)) { > auto predBI = dyn_cast<BranchInst>(pred->getTerminator()); > if(predBI && predBI->isUnconditional()){ > predBI->setSuccessor(0, bb.getSingleSuccessor()); > BI->dropAllReferences(); > BI->removeFromParent(); > } > } > } > > } > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200207/828fc8e3/attachment.html>