November 23, 1998.
Ulrich Drepper has contributed support for automatic generation of graphical flow graph representation.
GCC
now contains some code which allows to emit
information to allow a graphical representation of the flow
graphs.
Traditionally gcc
has the capability to dump
information about all the instructions, basic blocks and the
connections between them in textual form. This serves the purpose
quite well but following the logic of the program is quite
complicated and often requires drawing the flow graph on paper.
This is annoying especially since the compiler already has all the
information.
Programs to draw graphs from a textual input exist quite a few and at one of them are freely available (one even comes with all the source code). Currently known are daVinci and VCG. So far only VCG is supported but we certainly accept patches.
First you should get an impression on what the program and the gcc changes do. Take the following small program.
int gcd (int v1, int v2) { int l = v1 < v2 ? v1 : v2; int h = v1 + v2 - l; while (h > l) { h -= l; if (h < l) { int tmp = l; l = h; h = tmp; } } return h; } int main (int argc, char *argv[]) { int v1 = atol (argv[1]); int v2 = atol (argv[2]); printf ("gcd(%d, %d) = %d\n", v1, v2, gcd (v1, v2)); return 0; } |
If you want to understand how GCC translates this
programs you use the -d
option to select what GCC
should dump. E.g., giving GCC the option -da
dumps
the files:
# ../cc1 -O2 -o test.o test.c -da gcd main time in parse: 0.010000 [... some lines removed ...] # ls test.c.* test.c.addressof test.c.cse2 test.c.jump test.c.regmove test.c.stack test.c.bp test.c.flow test.c.jump2 test.c.rtl test.c.combine test.c.gcse test.c.loop test.c.sched test.c.cse test.c.greg test.c.lreg test.c.sched2 |
These files are kind of hard to read if you are not used to
RTL. Example? This is a part of test.c.lreg
:
[... some lines removed ...] Basic block 5: first insn 25, last 27. Registers live at start: 6 7 24 25 Basic block 6: first insn 59, last 63. Registers live at start: 6 7 25 (note 2 0 4 "" NOTE_INSN_DELETED) ;; Start of basic block 0, registers live: 6 [bp] 7 [sp] 16 [] (insn 4 2 6 (set (reg/v:SI 22) (mem:SI (reg:SI 16 %argp) 1)) 54 {movsi+2} (nil) (expr_list:REG_EQUIV (mem:SI (reg:SI 16 %argp) 1) (nil))) (insn 6 4 7 (set (reg/v:SI 24) (mem:SI (plus:SI (reg:SI 16 %argp) (const_int 4)) 1)) 54 {movsi+2} (nil) (expr_list:REG_EQUAL (mem:SI (plus:SI (reg:SI 16 %argp) (const_int 4)) 1) (nil))) (note 7 6 8 "" NOTE_INSN_FUNCTION_BEG) (note 8 7 10 "" NOTE_INSN_DELETED) (note 10 8 12 0 NOTE_INSN_BLOCK_BEG) (insn 12 10 13 (set (reg/v:SI 23) (reg/v:SI 24)) 54 {movsi+2} (insn_list 6 (nil)) (nil)) [... more lines removed ...] |
All the information about the basic blocks and the instructions and so on is available but not the the most human friendly form.
This is where the new GCC
features come into play.
If you add the option -dv
(`v' for VCG) to your
commandline you get a handful of extra files:
# ../cc1 -O2 -o test.o test.c -da -dv gcd main time in parse: 0.010000 [... some lines removed ...] # ls test.c.*.vcg test.c.addressof.vcg test.c.cse2.vcg test.c.jump.vcg test.c.regmove.vcg test.c.bp.vcg test.c.flow.vcg test.c.jump2.vcg test.c.sched.vcg test.c.combine.vcg test.c.gcse.vcg test.c.loop.vcg test.c.sched2.vcg test.c.cse.vcg test.c.greg.vcg test.c.lreg.vcg test.c.stack.vcg |
These files can be fed into the VCG program (actually
called xvcg
). If you do this you'll see a window like
this:
These are nodes representing all the functions in the file. If you read the VCG manual and find out how to expand the nodes you can get a picture like this. If you need more details you can unfold the basic block nodes as well. There are some more tricks the program can do, it's worth reading the manual.
If you have a GCC
later than 2.92.22 you have the
necessary changes to the compiler. There is no up-to-date patch
for older version of the compiler so you have to update to
GCC
in any case.
You also need the VCG program. The original version is available here. The problem with this version is that it has a few little bugs. A corrected version is available.
Oh, did we mention that you have to run a Unix with X Windows? Go away, Windows Weenies!
What we don't want to hear about is installation with vcg. We haven't written this program, it works for us. That's all.
What I would like to hear about is what should be displayed beside what currently is. We already have some extensions in mind but there might be more. We probably need a way for selecting what information should be dumped since the more you dump, the less readable the graphs are. I have not yet sorted this out.