Whole-program Analysis via Link-Time Optimization (LTO) — gcc-python-plugin 0.16 documentation (original) (raw)

gcc-python-plugin

You can enable GCC’s “link time optimization” feature by passing -flto.

When this is enabled, gcc adds extra sections to the compiled .o file containing the SSA-Gimple internal representation of every function, so that this SSA representation is available at link-time. This allows gcc to inline functions defined in one source file into functions defined in another source file at link time.

Although the feature is intended for optimization, we can also use it for code analysis, and it’s possible to run the Python plugin at link time.

This means we can do interprocedural analysis across multiple source files.

An invocation might look like this:

gcc
-flto
-flto-partition=none
-v
-fplugin=PATH/TO/python.so
-fplugin-arg-python-script=PATH/TO/YOUR/SCRIPT.py
INPUT-1.c
INPUT-2.c
... INPUT-n.c

Looking at the above options in turn:

For example,

$ ./gcc-with-python
examples/show-lto-supergraph.py
-flto
-flto-partition=none
tests/examples/lto/input-*.c

will render a bitmap of the supergraph like this:

image of a supergraph

gcc. is_lto()

Return type: bool

Determine whether or not we’re being invoked during link-time optimization (i.e. from within the lto1 program)

Warning

The underlying boolean is not set up until passes are being invoked: it is always False during the initial invocation of the Python script.