Search code examples
c++clangllvmllvm-irllvm-c++-api

how to remove an unconditonal branch in LLVM?


I want to remove a redundant unconditional branch from a Function. In the follwing 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 by,

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();
            }
        }
    } 

}

But this gives me an error. I am using LLVM 6.0.0

#0 0x000056166a0bcfba (opt+0x11fbfba)
#1 0x000056166a0bb01e (opt+0x11fa01e)
#2 0x000056166a0bb16c (opt+0x11fa16c)
#3 0x00007f2d009cb890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890)
#4 0x0000561669b8ce4c (opt+0xccbe4c)
#5 0x00007f2cff43bdb1 mergeBlocks /home/charitha/workspace/rocm/hcc/compiler/lib/Transforms/lcs/MergePass.cpp:103:0
#6 0x00007f2cff43bdb1 (anonymous namespace)::SkeletonPass::runOnFunction(llvm::Function&) /home/charitha/workspace/rocm/hcc/compiler/lib/Transforms/lcs/MergePass.cpp:51:0
#7 0x0000561669bb4f5a (opt+0xcf3f5a)
#8 0x0000561669bb5003 (opt+0xcf4003)
#9 0x0000561669bb4b14 (opt+0xcf3b14)
#10 0x000056166924c765 (opt+0x38b765)
#11 0x00007f2cff65fb97 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b97)
#12 0x000056166929f5fa (opt+0x3de5fa)

Solution

  • You might be interested in llvm::MergeBlockIntoPredecessor() utility function defined in llvm/Transform/Utils/BasicBlockUtils.h file.

    See https://llvm.org/doxygen/BasicBlockUtils_8h.html