Iterating over basic blocks in reverse in LLVM Function

4.9k Views Asked by At

Is there any way to iterate in reverse on a LLVM Function. I've checked the docs but can't seem to find any member typedef for iterating Basic blocks ( in a function ) in reverse.

Any help will be appreciated.

Thanks, Malhar

4

There are 4 best solutions below

4
On

I think you can call Function::getBasicBlockList() and then use .rbegin() and rend() on that list.

0
On

Sharing the working code snippet based on @arrowd suggestions:

    auto bbList = &(func_ptr->getBasicBlockList()); //fetch the pointer of the list

    errs()<<"reverse  \n";

    for(auto bb = bbList->rbegin(); bb != bbList->rend(); bb++) {

        b = &(*bb);

        errs() << b->getName()<<", ";

    }

What is the sequence of printing the basic block names , in case there are multiple predecessors and successors ?

opposite of Reverse Post Order i.e Post - order (while iterating in reverse fashion)

0
On

You can use llvm::reverse() from StlExtras.h to iterate containers in reverse order.

0
On
for (BasicBlock::reverse_iterator i = BB->rbegin(), e = BB->rend(); i != e; ++i)
{ 
    // your code
}