Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gcc -O3 & gprof

292 views
Skip to first unread message

Alex Vinokur

unread,
Mar 17, 2003, 6:59:33 AM3/17/03
to
=====================
Windows 2000
DJGPP 2.03
GNU gcc version 3.2.1
GNU gprof 2.13
=====================


Here is a simple program that has been profiled with using gprof.
We can see that if the program is compiled with option -O3
then gprof's output doesn't contained called functions foo1() and foo2().

Any explanation ?


========= C code : BEGIN =========
/* File main.c */

int foo1 (int argc) { return argc; }
int foo2 (int argc) { return argc; }

int main(int argc)
{
return (foo1(argc) + foo2(argc));
}

========= C code : END ===========


========= Compiling & Running : BEGIN =========

% gcc -o a0.exe main.c -g -pg
% a0.exe
% gprof -b a0.exe

Flat profile:

Each sample counts as 0.0555556 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 foo1
0.00 0.00 0.00 1 0.00 0.00 foo2
0.00 0.00 0.00 1 0.00 0.00 main
[---omitted---]


-----------------------------------------------
% gcc -O1 -o a1.exe main.c -g -pg
% a1.exe
% gprof -b a1.exe

Flat profile:

Each sample counts as 0.0555556 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 foo1
0.00 0.00 0.00 1 0.00 0.00 foo2
0.00 0.00 0.00 1 0.00 0.00 main
[---omitted---]

-----------------------------------------------
% gcc -O2 -o a2.exe main.c -g -pg
% a2.exe
% gprof -b a2.exe

Flat profile:

Each sample counts as 0.0555556 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 foo1
0.00 0.00 0.00 1 0.00 0.00 foo2
0.00 0.00 0.00 1 0.00 0.00 main
[---omitted---]


-----------------------------------------------
% gcc -O3 -o a3.exe main.c -g -pg
% a3.exe
% gprof -b a3.exe

Flat profile:

Each sample counts as 0.0555556 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 main
[---omitted---]

========= Compiling & Running : END ===========

=================================
Alex Vinokur
mailto:ale...@connect.to
http://www.simtel.net/pub/oth/19088.html
=================================


Tauno Voipio

unread,
Mar 17, 2003, 7:31:42 AM3/17/03
to

"Alex Vinokur" <ale...@bigfoot.com> wrote in message
news:b54db9$252aq3$1...@ID-79865.news.dfncis.de...

> =====================
> Windows 2000
> DJGPP 2.03
> GNU gcc version 3.2.1
> GNU gprof 2.13
> =====================
>
>
> Here is a simple program that has been profiled with using gprof.
> We can see that if the program is compiled with option -O3
> then gprof's output doesn't contained called functions foo1() and foo2().
>
> Any explanation ?
>
>
> ========= C code : BEGIN =========
> /* File main.c */
>
> int foo1 (int argc) { return argc; }
> int foo2 (int argc) { return argc; }

The optimisation -O3 inlines simple functions (like yours here). There are
no calls to trace.

HTH

Tauno Voipio
tauno voipio @ iki fi


Alex Vinokur

unread,
Mar 17, 2003, 11:52:29 PM3/17/03
to

"Tauno Voipio" <tauno....@iki.fi.SPAMBAIT_REMOVE.invalid> wrote in message news:Oejda.183$Zf2...@read3.inet.fi...

>
> "Alex Vinokur" <ale...@bigfoot.com> wrote in message
[snip]

> >
> > ========= C code : BEGIN =========
> > /* File main.c */
> >
> > int foo1 (int argc) { return argc; }
> > int foo2 (int argc) { return argc; }
>
> The optimisation -O3 inlines simple functions (like yours here). There are
> no calls to trace.
[snip]

How can we see that ?
For instance, nm doesn't distinguish -O0, -O1, -O2, -O3.

====================
Windows 2000
DJGPP 2.03

GNU gcc/g++ version 3.2.1
GNU nm 2.13
====================

gcc -o a0.exe main.c -g -pg

nm a0.exe | grep foo
000016d0 T _foo1
000016e2 T _foo2

gcc -O1 -o a1.exe main.c -g -pg

nm a1.exe | grep foo
000016d0 T _foo1
000016e2 T _foo2

gcc -O2 -o a2.exe main.c -g -pg

nm a2.exe | grep foo
000016d0 T _foo1
000016f0 T _foo2

gcc -O3 -o a3.exe main.c -g -pg

nm a3.exe | grep foo
000016f0 T _foo1
00001710 T _foo2

André Pönitz

unread,
Mar 18, 2003, 2:55:56 AM3/18/03
to
In gnu.gcc.help Alex Vinokur <ale...@bigfoot.com> wrote:
>> The optimisation -O3 inlines simple functions (like yours here). There are
>> no calls to trace.
> [snip]
>
> How can we see that ?

By looking at the produced assembler...

> For instance, nm doesn't distinguish -O0, -O1, -O2, -O3.

The function definitions have to be there in case you want to access them
from another translation unit.

Try to make the functions "static". As the the functions are no longer
accessible from the outside I'd expect the symbols to be removed
completely. But that's just a wild guess.

Andre'

--
Those who desire to give up Freedom in order to gain Security,
will not have, nor do they deserve, either one. (T. Jefferson)

Tauno Voipio

unread,
Mar 18, 2003, 2:11:49 PM3/18/03
to

"André Pönitz" <poe...@gmx.net> wrote in message
news:b56jec$8n5$1...@narses.hrz.tu-chemnitz.de...

> In gnu.gcc.help Alex Vinokur <ale...@bigfoot.com> wrote:
> >> The optimisation -O3 inlines simple functions (like yours here). There
are
> >> no calls to trace.
> > [snip]
> >
> > How can we see that ?
>
> By looking at the produced assembler...
>
> > For instance, nm doesn't distinguish -O0, -O1, -O2, -O3.
>
> The function definitions have to be there in case you want to access them
> from another translation unit.
>
> Try to make the functions "static". As the the functions are no longer
> accessible from the outside I'd expect the symbols to be removed
> completely. But that's just a wild guess.
>

Right.

Also, if the functions are not 'static', there will be one copy of each
generated function for accesses from outside of the module. The internal
calls may still be inlined with nothing to trace.

Ben Peddell

unread,
Mar 18, 2003, 7:18:16 AM3/18/03
to
Alex Vinokur <ale...@bigfoot.com> wrote in message
news:b568o6$258tun$1...@ID-79865.news.dfncis.de...

Because you did not declare the functions static, they were still included
as discrete functions in the output (so that other modules can access them).
However, the compiler has also inlined copies of those functions in main()
(since the functions were so simple).

Your code:


========= C code : BEGIN =========
/* File main.c */

int foo1 (int argc) { return argc; }
int foo2 (int argc) { return argc; }

int main(int argc)
{
return (foo1(argc) + foo2(argc));
}

========= C code : END ===========

is optimized by -O3 so that it appears to be:


========= C code : BEGIN =========
/* File main.c */

int foo1 (int argc) { return argc; }
int foo2 (int argc) { return argc; }

int main(int argc)
{
return ((argc) + (argc));

0 new messages