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 ?
you can simply use iterator over function like :
for (Function::iterator b = func->begin(), be = func->end(); b != be; ++b) {
BasicBlock* BB = b;
....
}
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 intoLLVMGetNextBasicBlock()
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With