title: how to remove an unconditonal branch in LLVM?
body: <p>I want to remove a redundant unconditional branch from a Function. In the follwing example I want to remove <code>br label %26</code> and merge them to a single basic block.</p>
  
  <pre><code>; &lt;label&gt;: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
  
  ; &lt;label&gt;:15:                                     ; preds = %9
    br label %26
  </code></pre>
  
  <p>I tried to do this by,</p>
  
  <pre><code>for(auto it1 = F.begin(); it1 != F.end(); it1++){
      BasicBlock&amp; bb = *it1;
      auto BI = dyn_cast&lt;BranchInst&gt;(bb.getTerminator());
      if(BI &amp;&amp; BI-&gt;isUnconditional() &amp;&amp; bb.size() == 1){
  
          for (BasicBlock *pred : predecessors(&amp;bb)) {
              auto predBI = dyn_cast&lt;BranchInst&gt;(pred-&gt;getTerminator());
              if(predBI &amp;&amp; predBI-&gt;isUnconditional()){
                      predBI-&gt;setSuccessor(0, bb.getSingleSuccessor());
                      BI-&gt;dropAllReferences();
                      BI-&gt;removeFromParent();
              }
          }
      } 
  
  }
  </code></pre>
  
  <p>But this gives me an error. I am using LLVM 6.0.0</p>
  
  <pre><code>#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&amp;) /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)
  </code></pre>