Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over basic blocks?

Tags:

llvm

I want to add an instruction to each of my basic blocks, I can use LLVMAppendBasicBlock for that once a bloc is specified. But how to iterate over all basic blocks in a function ? Are there iterators for that in the LLVM API ?

like image 211
mdrlol Avatar asked May 13 '15 08:05

mdrlol


2 Answers

you can simply use iterator over function like :

 for (Function::iterator b = func->begin(), be = func->end(); b != be; ++b) {
BasicBlock* BB = b;
....
}
like image 193
hadi sadeghi Avatar answered Oct 14 '22 00:10

hadi sadeghi


There's

LLVMBasicBlockRef   LLVMGetFirstBasicBlock (LLVMValueRef Fn)

and

LLVMGetNextBasicBlock (LLVMBasicBlockRef BB)

The documentation for LLVMGetFirstBasicBlock says:

Obtain the first basic block in a function.

The returned basic block can be used as an iterator. You will likely eventually call into LLVMGetNextBasicBlock() with it.

So call LLVMGetFirstBasicBlock once per function, and then LLVMGetNextBasicBlock repeatedly until you've gone through all the basic blocks of that function (juding by the source you'll get a nullptr when that happens).

like image 20
Michael Avatar answered Oct 14 '22 00:10

Michael