Get predecessors for BasicBlock in LLVM

What is the easiest way to get the predecessors of BasicBlock in an LLVM framework?

I took a look at DepthFirstIterator and idf_iterator<BasicBlock*> , but I really need to do a width search on the control flow graph.

It seems to me that this should be easy, but it is not obvious from the documentation or examples that I studied on the Internet.

+6
source share
2 answers

This is not clear from the documentation, but the base class of the block supports a preliminary iterator, which gives the predecessors the base block. In C ++ 11 style, one could number the block predecessors as follows:

 BasicBlock* B = ... for (auto it = pred_begin(B), et = pred_end(B); it != et; ++it) { BasicBlock* predecessor = *it; ... } 
+11
source

An easier way to iterate over predecessors or successors is shown using the for-each loop in the Programmer's Guide :

Iterating over the block's predecessors and followers is fairly simple with the routines defined in llvm/IR/CFG.h Just use this code to iterate over all the predecessors of BB:

 #include "llvm/IR/CFG.h" BasicBlock *BB = ...; for (BasicBlock *Pred : predecessors(BB)) { // ... } 

Similarly, to iterate over successors, use successors .

This is much cleaner than using explicit iteration with pred_begin and pred_end .

0
source

All Articles