Search code examples
compiler-constructionllvmcompiler-optimizationllvm-clang

Finding the list of successors of a basic block in LLVM


In LLVM the BasicBlock has the properties getSinglePredecessor() and getSingleSuccessor(), but I need to get the whole list of successors and predecessors of a basic block. How can I achieve this in llvm?

My code is

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

Solution

  • I agree that there is no direct property to a BasicBlock. Instead, you can get the terminator instruction of the basic block and then iterate through its successors.

    Or, based on reading the source code to the BasicBlock class, you can create a pred_iterator and succ_iterator from your BasicBlock instance. For example:

    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)