Reputation: 1439
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
}
}
Upvotes: 5
Views: 7767
Reputation: 41
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.
Upvotes: 4
Reputation: 2813
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)
Upvotes: 3