Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

llvm dumping control flow graph to file inside a pass

I want to build a control flow graph diagram in llvm in one of my passes. I currently use the following to show the CFG

block->getParent()->viewCFG(); //block is a basic block

The problem is that it pops up a windows. I just want to dump the cfg at that particular program point, as a dot file (or jpg if possible), not to show up in a window. How can I do the same? I am using llvm 3.1.

NOTE: I am modifying the cfg in my pass, before that program point. Hence I cannot use the opt -view-cfg.

Update:

Thanks to Mishr, I was able to draw to graph with this

WriteGraph(File, (const llvm::Function*) &fun, true, "test"); //I have also tired with false

The CFG is shown. But the nodes are blank. How can I show the contents of the node

like image 469
simpleuser Avatar asked May 08 '13 15:05

simpleuser


2 Answers

Take a look at this, read the comment before the viewCFG() function.

http://llvm.org/docs/doxygen/html/CFGPrinter_8cpp_source.html

The viewCFG() function is intended for printing the CFG in a new window. To dump the CFG in a file you have to use CFGPrinter pass which can be invoked by the handle dot-cfg.

like image 123
shrm Avatar answered Oct 14 '22 15:10

shrm


Let me add something to ssubbotin's answer. The question is about

DOTGraphTraits<const Function*>

provided by CFGPrinter.

In my case I had to use the call like that:

WriteGraph<const llvm::Function*>(...)

to make it work.

The function template definition is like the following:

template<typename GraphType>
raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
                        bool ShortNames = false,
                        const Twine &Title = "")

so GraphType gets non-const with implicit template call.

like image 31
wvoquine Avatar answered Oct 14 '22 17:10

wvoquine