gpt4 book ai didi

c++ - 如何在LLVM中删除无条件分支?

转载 作者:行者123 更新时间:2023-12-02 10:21:10 25 4
gpt4 key购买 nike

我想从功能中删除冗余的无条件分支。在下面的示例中,我想删除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)

最佳答案

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

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

关于c++ - 如何在LLVM中删除无条件分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60106734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com