Extracting llvm IR CFG from Clang

Dear All:

I am working on Clang/LLVM and interested to know if there is a programmatic way to extract the CFG of a specific
node in AST like a for loop?

I am aware of -emit-llvmoption, but it will apply on the whole TU.

For example,
we could use CFG::buildCFG to extract the source level CFG of a specific node.
Is there a counterpart API to extract the LLVM IR level CFG of a specific node?

Sincerely
xiaohui

Dear All:

I am working on Clang/LLVM and interested to know if there is a programmatic way to extract the CFG of a specific
node in AST like a for loop?

I am aware of -emit-llvmoption, but it will apply on the whole TU.

For example,
we could use CFG::buildCFG to extract the source level CFG of a specific node.
Is there a counterpart API to extract the LLVM IR level CFG of a specific node?

Sincerely

xiaohui

The LLVM IR expresses each function as a set of basic blocks with an explicit CFG. In other words, the LLVM IR encodes the CFG of each function explicitly. If you have the LLVM IR of a function, you already have the CFG. Regards, John Criswell

Dear John,

Thanks for your reply! I understand.

My question is more subtle. Could I obtain the LLVM IR for a specific stmt ?

For example,

void foo()
{
… // some stmts

for(…) {

}

… // some stmts
}

Once I built the Clang AST, there is a node to represent that for-loop, say nodeFor.

Then I could take nodeFor as parameter to call buildCFG() to generate the source code level CFG of that for-loop.

Could I use a similar method to get the LLVM IR for that for-loop? Again I do not want to generate LLVM IR for other stmts other than that for-loop.
Hope my question is clear.

Sincerely
xiaohui

Dear Xiaohui,

I'm not very familiar with the Clang AST, but I suspect the answer is no: you probably can't generate LLVM IR for just an arbitrary subset of the Clang AST.

That said, if you have the LLVM IR, you can use existing analysis passes to loops, control-dependences, and that sort of thing. However, if you want to find a CFG for a subset of a Clang AST, then I do not know if there is existing code to do that.

Regards,

John Criswell