public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/90886] loop/while/for problem
       [not found] <bug-90886-4@http.gcc.gnu.org/bugzilla/>
@ 2023-05-21 19:32 ` gjl at gcc dot gnu.org
  2023-05-21 19:36 ` [Bug tree-optimization/90886] " gjl at gcc dot gnu.org
  2023-05-21 19:59 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 3+ messages in thread
From: gjl at gcc dot gnu.org @ 2023-05-21 19:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90886

--- Comment #11 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
Created attachment 55132
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55132&action=edit
C++ test case for v5.4 -Os

I can confirm this with avr-g++-5.4.0 and the attached test case:

> avr-gcc-5.4.0 -xc++ loop.c -Os -S -dp -da -fdump-tree-all

volatile unsigned char x; 

void callee (unsigned char);

void f128 (void)
{
    while (1)
        for (unsigned char i = 0; i < 128; i++)
            x = i;
}

void f128_call (void)
{
    while (1)
        for (unsigned char i = 0; i < 128; i++)
            callee (i);
}

The generated assembly is:

_Z4f128v:
        sts x,__zero_reg__       ;  5   movqi_insn/3    [length = 2]

_Z9f128_callv:
        ldi r24,0        ;  5   movqi_insn/1    [length = 1]
        rcall _Z6calleeh         ;  6   call_insn/2     [length = 1]

This is all of the code because in dump loop.c.073t.cunrolli there is:

void f128() ()
{
  unsigned char i;
  <bb 2>:
  <bb 3>:
  <bb 4>:
  x ={v} 0;
  i_7 = 1;
  __builtin_unreachable ();
}

and similar for the other function.

Reason appears to be that the C++ front-end expand the loop variable as signed
char, and later passes think that there is some signed overflow und Undefined
Behaviour.

With -fwrapv, correct code is generated.

Also, the C front end also uses signed char, but for some reason the C version
works.

A work-around is to use -fwrapv so that signed overflow wraps around.

This is not a target issue, it's some tree optimization where GCC falls in its
own pit as it tries to exploit signed overflow.

I could not reproduce this with later version of the compiler. The next version
I tried was v8.5 (I don't have other versions handy).

I leave this bug open for now.  Maybe the tree folks can pin down which PR this
actually is, and when it was fixed.

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

* [Bug tree-optimization/90886] loop/while/for problem
       [not found] <bug-90886-4@http.gcc.gnu.org/bugzilla/>
  2023-05-21 19:32 ` [Bug target/90886] loop/while/for problem gjl at gcc dot gnu.org
@ 2023-05-21 19:36 ` gjl at gcc dot gnu.org
  2023-05-21 19:59 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 3+ messages in thread
From: gjl at gcc dot gnu.org @ 2023-05-21 19:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90886

Georg-Johann Lay <gjl at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|target                      |tree-optimization

--- Comment #12 from Georg-Johann Lay <gjl at gcc dot gnu.org> ---
As explained in comment #11, this is a tree optimizaton issue:

(In reply to Georg-Johann Lay from comment #11)
> [...]
> Reason appears to be that the C++ front-end expand the loop variable as
> signed char, and later passes think that there is some signed overflow und
> Undefined Behaviour.
> 
> With -fwrapv, correct code is generated.
> 
> Also, the C front end also uses signed char, but for some reason the C
> version works.
> 
> A work-around is to use -fwrapv so that signed overflow wraps around.
> 
> This is not a target issue, it's some tree optimization where GCC falls in
> its own pit as it tries to exploit signed overflow.
> 
> I could not reproduce this with later version of the compiler. The next
> version I tried was v8.5 (I don't have other versions handy).
> 
> I leave this bug open for now.  Maybe the tree folks can pin down which PR
> this actually is, and when it was fixed.

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

* [Bug tree-optimization/90886] loop/while/for problem
       [not found] <bug-90886-4@http.gcc.gnu.org/bugzilla/>
  2023-05-21 19:32 ` [Bug target/90886] loop/while/for problem gjl at gcc dot gnu.org
  2023-05-21 19:36 ` [Bug tree-optimization/90886] " gjl at gcc dot gnu.org
@ 2023-05-21 19:59 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-21 19:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90886

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |needs-bisection

--- Comment #13 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is a really interesting case as it works for GCC 6.x just fine but it was
broken in GCC 5.x and GCC 7.x. From looking at the IR, it looks like it was
broken in two different ways before.

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

end of thread, other threads:[~2023-05-21 19:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-90886-4@http.gcc.gnu.org/bugzilla/>
2023-05-21 19:32 ` [Bug target/90886] loop/while/for problem gjl at gcc dot gnu.org
2023-05-21 19:36 ` [Bug tree-optimization/90886] " gjl at gcc dot gnu.org
2023-05-21 19:59 ` pinskia at gcc dot gnu.org

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).