public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2
@ 2020-12-05 11:50 Dennis Luehring
  2020-12-05 12:04 ` Jan Hubicka
  2020-12-05 13:25 ` Eric Botcazou
  0 siblings, 2 replies; 5+ messages in thread
From: Dennis Luehring @ 2020-12-05 11:50 UTC (permalink / raw)
  To: Gcc Mailing List

gcc does not reduce to call result if called function is not static in
-O2 (will do with -O2)
clang and msvc does it also in -O2 regardless of the function beeing
static or not

can someone explain to me why the -O2 optimizer is not able(allowed) to
reduce this small sample the same way as clang/msvc?

x86-64 gcc 10.2 and trunk -O2: https://godbolt.org/z/r3GM57
x86-64 clang trunk and 11.0.0 -O2: https://godbolt.org/z/8hqbz5
x64 msvc v19.27 -O2: https://godbolt.org/z/nv3rWq

code to reproduce

---------------------------
#include <cstdint>

// part of run length encoding...

static void v32(uint32_t v_, uint8_t* b_, int& s_)
{
   if (v_ <= 0x0000007FU)
   {
     s_ = 1;
     b_[0] = (uint8_t)v_;
     return;
   }
   if (v_ <= 0x00003FFFU)
   {
     s_ = 2;
     b_[1] = (uint8_t)(v_ & 0x7F);
     b_[0] = (uint8_t)((v_ >> 7) | 0x80);
     return;
   }

   s_ = 3;
   b_[2] = (uint8_t)(v_ & 0x7F);
   b_[1] = (uint8_t)((v_ >> 7) | 0x80);
   b_[0] = (uint8_t)((v_ >> 14) | 0x80);
   return;
}

int test(uint32_t v_)
{
   uint8_t b[3]{};
   int s=0;
   v32(v_, b, s);
   return s+b[0]+b[1]+b[2];
}

int main(int argc, char** argv)
{
   return test(1337); // results in 197

   // clang reduces the call down to 197    regardless of test beeing
static or not
   //main:                                   # @main
   //  mov eax, 197
   //  ret

   // gcc reduces the call only if test is static
   //main:
   //  mov edi, 1337
   //  jmp test(unsigned int)
}
------------------------------------


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2
  2020-12-05 11:50 gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2 Dennis Luehring
@ 2020-12-05 12:04 ` Jan Hubicka
  2020-12-06  7:32   ` Dennis Luehring
  2020-12-05 13:25 ` Eric Botcazou
  1 sibling, 1 reply; 5+ messages in thread
From: Jan Hubicka @ 2020-12-05 12:04 UTC (permalink / raw)
  To: Dennis Luehring, mjambor; +Cc: Gcc Mailing List

> gcc does not reduce to call result if called function is not static in
> -O2 (will do with -O2)
> clang and msvc does it also in -O2 regardless of the function beeing
> static or not
> 
> can someone explain to me why the -O2 optimizer is not able(allowed) to
> reduce this small sample the same way as clang/msvc?

GCC is optimizing for size here, so it is more careful with inlning.
This is because it knows that function main is called once.  If you
rename main to something else or add a loop around the call, then it
will inline for speed and do same work as clang.  Clang (and probaby
msvc) does not implement the heuristics that certain functions
(static constructors, destructors, main and noreturns) are called once
so they probably both optimize for speed.

Even when optimizing for size it would be good idea to inline.  However
the inliner heruistics predicts that it is not.  This is because at the
inlining time compiler does not see that calee will optimize to constant.
The reason is that you store the temporary vlues to array and those are
not tracked. If you used scalar variables it would be able to constant
fold everything early.

Handling this would require either recovering early ipa-sra or adding
return functions for values passed by reference.

Honza

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2
  2020-12-05 11:50 gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2 Dennis Luehring
  2020-12-05 12:04 ` Jan Hubicka
@ 2020-12-05 13:25 ` Eric Botcazou
  2020-12-06  7:32   ` Dennis Luehring
  1 sibling, 1 reply; 5+ messages in thread
From: Eric Botcazou @ 2020-12-05 13:25 UTC (permalink / raw)
  To: Dennis Luehring; +Cc: gcc

> can someone explain to me why the -O2 optimizer is not able(allowed) to
> reduce this small sample the same way as clang/msvc?

Change the name of the function to something else than "main".

-- 
Eric Botcazou



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2
  2020-12-05 12:04 ` Jan Hubicka
@ 2020-12-06  7:32   ` Dennis Luehring
  0 siblings, 0 replies; 5+ messages in thread
From: Dennis Luehring @ 2020-12-06  7:32 UTC (permalink / raw)
  To: Jan Hubicka, mjambor; +Cc: Gcc Mailing List

Am 05.12.2020 um 13:04 schrieb Jan Hubicka:
> > gcc does not reduce to call result if called function is not static in
> > -O2 (will do with -O2)
> > clang and msvc does it also in -O2 regardless of the function beeing
> > static or not
> >
> > can someone explain to me why the -O2 optimizer is not able(allowed) to
> > reduce this small sample the same way as clang/msvc?
>
> GCC is optimizing for size here, so it is more careful with inlning.
> This is because it knows that function main is called once.  If you
> rename main to something else or add a loop around the call, then it
> will inline for speed and do same work as clang.  Clang (and probaby
> msvc) does not implement the heuristics that certain functions
> (static constructors, destructors, main and noreturns) are called once
> so they probably both optimize for speed.
>
> Even when optimizing for size it would be good idea to inline.  However
> the inliner heruistics predicts that it is not.  This is because at the
> inlining time compiler does not see that calee will optimize to constant.
> The reason is that you store the temporary vlues to array and those are
> not tracked. If you used scalar variables it would be able to constant
> fold everything early.
>
> Handling this would require either recovering early ipa-sra or adding
> return functions for values passed by reference.
>
> Honza


>If you rename main...

main -> test2 -> test

test(unsigned int):
moveax, edi
cmpedi, 127
jbe.L3
moveax, edi
         ...

test2(unsigned int):
moveax, 197
ret
main:
movedi, 1337
jmptest(unsignedint)


gives the expected result and that is enough for my benchmarking tests -
thanks



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2
  2020-12-05 13:25 ` Eric Botcazou
@ 2020-12-06  7:32   ` Dennis Luehring
  0 siblings, 0 replies; 5+ messages in thread
From: Dennis Luehring @ 2020-12-06  7:32 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: gcc

Am 05.12.2020 um 14:25 schrieb Eric Botcazou:
> > can someone explain to me why the -O2 optimizer is not able(allowed) to
> > reduce this small sample the same way as clang/msvc?
>
> Change the name of the function to something else than "main".
>
that works, thanks!


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-12-06  7:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-05 11:50 gcc does not reduce the function call to the result if called function is not static when using -O2, only with -O3, clang and msvc do the optimization also with -O2 Dennis Luehring
2020-12-05 12:04 ` Jan Hubicka
2020-12-06  7:32   ` Dennis Luehring
2020-12-05 13:25 ` Eric Botcazou
2020-12-06  7:32   ` Dennis Luehring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).