在LLVM中,BasicBlock具有属性getSinglePredecessor()和getSingleSuccessor(),但是我需要获取基本块的后继者和前任者的完整列表。如何在llvm中实现?

我的代码是

        virtual bool runOnFunction(Function &F) {

        for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
        //Here I need to get the predecessor and successsor of the basic block b
    }
}

最佳答案

我同意BasicBlock没有直接属性。相反,您可以对基本块进行get the terminator instruction,然后遍历其successors

或者,基于将source code读取到BasicBlock类,可以从BasicBlock实例创建pred_iterator和succ_iterator。例如:

for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b)
{
    BasicBlock* bb = dyn_cast<BasicBlock>(&*b);
    for (pred_iterator pit = pred_begin(bb), pet = pred_end(bb); pit != pet; ++pit)

08-05 17:17