public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/112454] New: csinc (csel is though) is not being used when there is matches twice
@ 2023-11-09  6:54 pinskia at gcc dot gnu.org
  2023-11-09 17:57 ` [Bug target/112454] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-09  6:54 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112454
           Summary: csinc (csel is though) is not being used when there is
                    matches twice
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---
            Target: aarch64

Take:
```
int f(int a, int b, int c, int d)
{
  return (a == 2 ? 1 : b) + (c == 3 ? 1 : d);
}
```

GCC produces:
```
        cmp     w0, 2
        mov     w4, 1
        csel    w0, w1, w4, ne
        cmp     w2, 3
        csel    w3, w3, w4, ne
        add     w0, w0, w3
```


But that `mov w4, 1` is useless if we use csinc .

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

* [Bug target/112454] csinc (csel is though) is not being used when there is matches twice
  2023-11-09  6:54 [Bug target/112454] New: csinc (csel is though) is not being used when there is matches twice pinskia at gcc dot gnu.org
@ 2023-11-09 17:57 ` pinskia at gcc dot gnu.org
  2023-11-18  6:48 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-09 17:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
here is another testcase which shows the issue with pulling the constant one
out of the loop when it could have been merged with the csel to use csinc:
```
int f(int *a, int n, int *b, int d)
{
  for(int i = 0; i < n; i++)
    b[i] = a[i] == 100 ? 1 : d;
  return 0;
}
```

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

* [Bug target/112454] csinc (csel is though) is not being used when there is matches twice
  2023-11-09  6:54 [Bug target/112454] New: csinc (csel is though) is not being used when there is matches twice pinskia at gcc dot gnu.org
  2023-11-09 17:57 ` [Bug target/112454] " pinskia at gcc dot gnu.org
@ 2023-11-18  6:48 ` pinskia at gcc dot gnu.org
  2023-11-18  6:56 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-18  6:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2023-11-18
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Mine, looks like a cost issue not recording that 1 (and ~0) are free to create.

you can see the cost issue if we look at combine for the case of 1 csel:
Trying 37 -> 39:
   37: r98:SI=0x1
   39: r92:SI={(cc:CC!=0)?r100:SI:r98:SI}
      REG_DEAD r100:SI
      REG_DEAD cc:CC
      REG_DEAD r98:SI
Successfully matched this instruction:
(set (reg:SI 92 [ <retval> ])
    (if_then_else:SI (ne (reg:CC 66 cc)
            (const_int 0 [0]))
        (reg:SI 100)
        (const_int 1 [0x1])))
allowing combination of insns 37 and 39
original costs 4 + 4 = 8
replacement cost 8
deferring deletion of insn with uid = 37.
modifying insn i3    39: r92:SI={(cc:CC!=0)?r100:SI:0x1}
      REG_DEAD cc:CC
      REG_DEAD r100:SI
deferring rescan insn with uid = 39.

The replacement cost should be still 4.

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

* [Bug target/112454] csinc (csel is though) is not being used when there is matches twice
  2023-11-09  6:54 [Bug target/112454] New: csinc (csel is though) is not being used when there is matches twice pinskia at gcc dot gnu.org
  2023-11-09 17:57 ` [Bug target/112454] " pinskia at gcc dot gnu.org
  2023-11-18  6:48 ` pinskia at gcc dot gnu.org
@ 2023-11-18  6:56 ` pinskia at gcc dot gnu.org
  2023-11-26 23:52 ` pinskia at gcc dot gnu.org
  2023-11-27 23:09 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-18  6:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

-1/~0 has the same issue as mentioned:
```
int finv(int a, int b, int c, int d)
{
  return (a == 2 ? -1 : b) + (c == 3 ? -1 : d);
}
```

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

* [Bug target/112454] csinc (csel is though) is not being used when there is matches twice
  2023-11-09  6:54 [Bug target/112454] New: csinc (csel is though) is not being used when there is matches twice pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-11-18  6:56 ` pinskia at gcc dot gnu.org
@ 2023-11-26 23:52 ` pinskia at gcc dot gnu.org
  2023-11-27 23:09 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-26 23:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So it looks like this is not only a cost issue. I have to look into forwprop to
see if it can handle this. Note the cost issue does need to be fixed anyways
since it will be needed there; otherwise forwprop might reject it.

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

* [Bug target/112454] csinc (csel is though) is not being used when there is matches twice
  2023-11-09  6:54 [Bug target/112454] New: csinc (csel is though) is not being used when there is matches twice pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-11-26 23:52 ` pinskia at gcc dot gnu.org
@ 2023-11-27 23:09 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-11-27 23:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from GCC 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:d29d27bde5df89e5357e0a33a71bb49125bd1655

commit r14-5893-gd29d27bde5df89e5357e0a33a71bb49125bd1655
Author: Andrew Pinski <quic_apinski@quicinc.com>
Date:   Sun Nov 26 23:25:51 2023 +0000

    aarch64: Improve cost of `a ? {-,}1 : b`

    While looking into PR 112454, I found the cost for
    `(if_then_else (cmp) (const_int 1) (reg))` was being recorded as 8
    (or `COSTS_N_INSNS (2)`) but it should have been 4 (or `COSTS_N_INSNS
(1)`).
    This improves the cost by not adding the cost of `(const_int 1)` to
    the total cost.

    It does not does not fully fix PR 112454 as that requires other changes to
forwprop
    the `(const_int 1)` earlier than combine. Though we do fix the loop case
where the
    constant was only used once.

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

    gcc/ChangeLog:

            * config/aarch64/aarch64.cc (aarch64_if_then_else_costs):
            Handle csinv/csinc case of 1/-1.

    gcc/testsuite/ChangeLog:

            * gcc.target/aarch64/csinc-3.c: New test.

    Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>

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

end of thread, other threads:[~2023-11-27 23:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-09  6:54 [Bug target/112454] New: csinc (csel is though) is not being used when there is matches twice pinskia at gcc dot gnu.org
2023-11-09 17:57 ` [Bug target/112454] " pinskia at gcc dot gnu.org
2023-11-18  6:48 ` pinskia at gcc dot gnu.org
2023-11-18  6:56 ` pinskia at gcc dot gnu.org
2023-11-26 23:52 ` pinskia at gcc dot gnu.org
2023-11-27 23:09 ` cvs-commit 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).