4

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 
    }
}
javaDeveloper
  • 1,403
  • 3
  • 28
  • 42

2 Answers2

3

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)
Brian
  • 2,693
  • 5
  • 26
  • 27
3

Slightly less code if you'd prefer that:

#include "llvm/IR/CFG.h"
BasicBlock *BB = ...;

for (BasicBlock *Pred : predecessors(BB)) {
  // ...
}

The snippet is taken from LLVM's Programmers Handbook.

derpsteb
  • 31
  • 4