删除每个BB的后继者

时间:2013-07-31 07:10:48

标签: llvm

我需要从每个基本块中移除后继者以插入新的

我尝试了这段代码,但它不起作用

void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
    assert(SuccNum < TI->getNumSuccessors() &&
            "Trying to remove a nonexistant successor!");

    // If our old successor block contains any PHI nodes, remove the entry in the
    // PHI nodes that comes from this branch...
    //
    BasicBlock *BB = TI->getParent();
    TI->getSuccessor(SuccNum)->removePredecessor(BB);

    TerminatorInst *NewTI = 0;
    switch (TI->getOpcode()) {
        case Instruction::Br:
            // If this is a conditional branch... convert to unconditional branch.
            if (TI->getNumSuccessors() == 2) {
                cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));
            } else {                    // Otherwise convert to a return instruction...
                Value *RetVal = 0;

                // Create a value to return... if the function doesn't return null...
                if (!(BB->getParent()->getReturnType())->isVoidTy())
                    RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
                // Create the return...
                NewTI = 0;
            }
            break;

        case Instruction::Invoke:    // Should convert to call
        case Instruction::Switch:    // Should remove entry
        default:
        case Instruction::Ret:       // Cannot happen, has no successors!
            assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
            abort();
    }

    if (NewTI)   // If it's a different instruction, replace.
        ReplaceInstWithInst(TI, NewTI);
}

结果示例:

if.then:                                          ; preds = %for.inc, %entry, %for.body

preds不应包含%for.body根据%for.body的继承者在插入新的后继者之前

1 个答案:

答案 0 :(得分:0)

除了我上面的评论要求澄清之外,这条线似乎很可疑:

cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));

据我所知,{em>年以前删除了setUnconditionalDest()方法,您使用的是哪个版本?无论如何,我建议您创建无条件BranchInst,然后使用ReplaceInstWithInst()替换有条件的。{/ p>