Seeing Source-level Optimisations
Daniel Berlin
dberlin@dberlin.org
Sat Apr 15 15:46:00 GMT 2006
On Apr 15, 2006, at 8:29 AM, Andrew Talbot wrote:
> Dear gcc-help,
>
> A newbie question: does gcc have a facility to emit its source-
> level optimised
> source code?
Well, you can get some idea of it with -fdump-tree-optimized
That will give you the dump of what the IR looks like (which is a
simplified C-like IR) after optimization.
For C++ and large C functions, it may or may not be helpful :)
You can also get the dumps after each optimization pass using -fdump-
tree-<something> (the list of passes and what to use there is in the
documentation).
>
> Taking the example in the "changes" for version 4.1:
>
> int foo (int *, int *);
> int
> bar (int d)
> {
> int a, b, c;
> b = d + 1;
> c = d + 2;
> a = b + c;
> if (d)
> {
> foo (&b, &c);
> a = b + c;
> }
> printf ("%d\n", a);
> }
>
> "The a = b + c can be sunk to right before the printf."
>
> If I were notified of that duplication, I would be inclined to
> alter the
> source code, even though the compiler would optimise the
> duplication out for
> me if I left it alone. One reason is that its pleasing to remove the
> superfluity, another is that if it turned out that the expression
> should be
> a = b + 2 * c, I might alter one instance of it but miss the other.
For example, the after-sink dump looks like:
;; Function bar (bar)
Sinking a_11 = D.1519_6 + D.1518_3 from bb 2 to bb 5
bar (d)
{
int c;
int b;
int a;
int c.1;
int b.0;
int D.1519;
int D.1518;
<bb 2>:
D.1518_3 = d_2 + 1;
b = D.1518_3;
D.1519_6 = d_2 + 2;
c = D.1519_6;
if (d_2 != 0) goto <L0>; else goto <L2>;
<L2>:;
a_11 = D.1519_6 + D.1518_3;
goto <bb 4> (<L1>);
<L0>:;
foo (&b, &c);
b.0_12 = b;
c.1_13 = c;
a_14 = c.1_13 + b.0_12;
# a_1 = PHI <a_11(5), a_14(3)>;
<L1>:;
printf (&"%d\n"[0], a_1);
return;
}
(the optimized dump won't have PHI's).
However, be aware that a lot of this stuff *is* best left to the
compiler, particularly because not every optimization is always
profitable, etc.
More information about the Gcc-help
mailing list