public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds
@ 2023-10-22  2:09 141242068 at smail dot nju.edu.cn
  2023-10-22  2:41 ` [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: 141242068 at smail dot nju.edu.cn @ 2023-10-22  2:09 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111913
           Summary: GCC: 14: -O1 compile __builtin_popcount fails while
                    -O0 succeeds
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 141242068 at smail dot nju.edu.cn
  Target Milestone: ---

When compile this program with `-O1`, gcc crashes:
```
int f(unsigned int x, unsigned int y)
{
  return __builtin_popcount (x&y) + __builtin_popcount (y|x--);
}
```

If change the optimization level to `-O0`, then nothing happens.

Compiler Explorer: https://gcc.godbolt.org/z/naoebKMor

The stack dump:
```
<source>: In function 'f':
<source>:4:1: error: invalid conversion in gimple call
    4 | }
      | ^
unsigned int

int

_3 = __builtin_popcount (x_6(D));
<source>:4:1: error: invalid conversion in gimple call
unsigned int

int

_11 = __builtin_popcount (y_7(D));
<source>:4:1: error: type mismatch in binary expression
int

unsigned int

unsigned int

_9 = _3 + _11;
during GIMPLE pass: forwprop
<source>:4:1: internal compiler error: verify_gimple failed
0x231f49e internal_error(char const*, ...)
        ???:0
0x1164659 verify_gimple_in_cfg(function*, bool, bool)
        ???:0
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
```

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
@ 2023-10-22  2:41 ` pinskia at gcc dot gnu.org
  2023-10-23  9:16 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-22  2:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ice-on-valid-code,
                   |                            |needs-bisection
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |14.0
     Ever confirmed|0                           |1
            Summary|GCC: 14: -O1 compile        |[14 Regression] ICE with
                   |__builtin_popcount fails    |__builtin_popcount(X) +
                   |while -O0 succeeds          |__builtin_popcount(Y)
                   |                            |simplification
   Last reconfirmed|                            |2023-10-22

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

So the simplification was being done in GCC 13 but the type was correct there.

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
  2023-10-22  2:41 ` [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification pinskia at gcc dot gnu.org
@ 2023-10-23  9:16 ` rguenth at gcc dot gnu.org
  2023-10-23 18:58 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-10-23  9:16 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P1

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
  2023-10-22  2:41 ` [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification pinskia at gcc dot gnu.org
  2023-10-23  9:16 ` rguenth at gcc dot gnu.org
@ 2023-10-23 18:58 ` pinskia at gcc dot gnu.org
  2023-10-23 19:40 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-23 18:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Works at r14-673 but fails at r14-1170.

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
                   ` (2 preceding siblings ...)
  2023-10-23 18:58 ` pinskia at gcc dot gnu.org
@ 2023-10-23 19:40 ` pinskia at gcc dot gnu.org
  2023-10-23 19:41 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-23 19:40 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I have a fix.

Basically the pattern:
```
/* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
(simplify
  (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
  (plus (POPCOUNT @0) (POPCOUNT @1)))
```

Needs a small help with the type that popcount returns. So:
```
/* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
(simplify
  (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
  (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
```

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
                   ` (3 preceding siblings ...)
  2023-10-23 19:40 ` pinskia at gcc dot gnu.org
@ 2023-10-23 19:41 ` pinskia at gcc dot gnu.org
  2023-10-23 19:43 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-23 19:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So this was introduced with r14-674-g5fdcfe3c5776 .

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
                   ` (4 preceding siblings ...)
  2023-10-23 19:41 ` pinskia at gcc dot gnu.org
@ 2023-10-23 19:43 ` pinskia at gcc dot gnu.org
  2023-10-23 23:05 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-23 19:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Another testcase:
```
int f(unsigned int x, unsigned int y)
{
  int t = __builtin_popcount (x&y);
  int t1 = __builtin_popcount (x|y);
  return t + t1;
}
```

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
                   ` (5 preceding siblings ...)
  2023-10-23 19:43 ` pinskia at gcc dot gnu.org
@ 2023-10-23 23:05 ` pinskia at gcc dot gnu.org
  2023-10-24 11:16 ` cvs-commit at gcc dot gnu.org
  2023-10-24 11:17 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-23 23:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2023-October
                   |                            |/634018.html
           Keywords|                            |patch

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch submitted:
https://gcc.gnu.org/pipermail/gcc-patches/2023-October/634018.html

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
                   ` (6 preceding siblings ...)
  2023-10-23 23:05 ` pinskia at gcc dot gnu.org
@ 2023-10-24 11:16 ` cvs-commit at gcc dot gnu.org
  2023-10-24 11:17 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-10-24 11:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:452c4f32373feb6b2c1c1d91b5ec6fe7e7ce0000

commit r14-4888-g452c4f32373feb6b2c1c1d91b5ec6fe7e7ce0000
Author: Andrew Pinski <pinskia@gmail.com>
Date:   Mon Oct 23 12:46:34 2023 -0700

    match: Fix the `popcnt(a&b) + popcnt(a|b)` pattern for types [PR111913]

    So this pattern needs a little help on the gimple side of things to know
what
    the type popcount should be. For most builtins, the type is the same as the
input
    but popcount and others are not. And when using it with another outer
expression,
    genmatch needs some slight help to know that the return type was type
rather than
    the argument type.

    Bootstrapped and tested on x86_64-linux-gnu with no regressions.

            PR tree-optimization/111913

    gcc/ChangeLog:

            * match.pd (`popcount(X&Y) + popcount(X|Y)`): Add the resulting
            type for popcount.

    gcc/testsuite/ChangeLog:

            * gcc.c-torture/compile/fold-popcount-1.c: New test.
            * gcc.dg/fold-popcount-8a.c: New test.

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

* [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification
  2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
                   ` (7 preceding siblings ...)
  2023-10-24 11:16 ` cvs-commit at gcc dot gnu.org
@ 2023-10-24 11:17 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-24 11:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2023-10-24 11:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-22  2:09 [Bug c/111913] New: GCC: 14: -O1 compile __builtin_popcount fails while -O0 succeeds 141242068 at smail dot nju.edu.cn
2023-10-22  2:41 ` [Bug tree-optimization/111913] [14 Regression] ICE with __builtin_popcount(X) + __builtin_popcount(Y) simplification pinskia at gcc dot gnu.org
2023-10-23  9:16 ` rguenth at gcc dot gnu.org
2023-10-23 18:58 ` pinskia at gcc dot gnu.org
2023-10-23 19:40 ` pinskia at gcc dot gnu.org
2023-10-23 19:41 ` pinskia at gcc dot gnu.org
2023-10-23 19:43 ` pinskia at gcc dot gnu.org
2023-10-23 23:05 ` pinskia at gcc dot gnu.org
2023-10-24 11:16 ` cvs-commit at gcc dot gnu.org
2023-10-24 11:17 ` 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).