【问题标题】:how to remove an unconditonal branch in LLVM?如何删除 LLVM 中的非条件分支?
【发布时间】:2020-02-07 03:34:32
【问题描述】:

我想从函数中删除多余的无条件分支。在以下示例中,我想删除 br label %26 并将它们合并到一个基本块中。

; <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

我尝试这样做,

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 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)

【问题讨论】:

  • 在迭代时不要更改基本块的列表。
  • 感谢您的建议。这是我弄错的事情之一。我在下面发布了我的工作代码。

标签: c++ clang llvm llvm-ir llvm-c++-api


【解决方案1】:

您可能对llvm/Transform/Utils/BasicBlockUtils.h 文件中定义的llvm::MergeBlockIntoPredecessor() 实用函数感兴趣。

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

【讨论】:

  • 谢谢@arrowd。这行得通。在下面发布我的代码给有同样问题的人!
【解决方案2】:

我重用了llvm/Transform/Utils/BasicBlockUtils.h 中的代码,并且成功了。

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){
        // code taken from BasicBlockUtils
        auto PredBB = BB->getUniquePredecessor();
        if(PredBB){
            PredBB->getInstList().pop_back();
            BB->replaceAllUsesWith(PredBB);
            PredBB->getInstList().splice(PredBB->end(), BB->getInstList());

            if (!PredBB->hasName())
                PredBB->takeName(BB);

            it1 = BB->eraseFromParent();

        }
    } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2021-12-11
    • 2022-08-17
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2022-11-27
    相关资源
    最近更新 更多