public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/111324] New: More optimization about "(X * Y) / Y"
@ 2023-09-07  8:53 guojiufu at gcc dot gnu.org
  2023-09-07  9:01 ` [Bug middle-end/111324] " guojiufu at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: guojiufu at gcc dot gnu.org @ 2023-09-07  8:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111324
           Summary: More optimization about "(X * Y) / Y"
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: guojiufu at gcc dot gnu.org
  Target Milestone: ---

For case:
------ t.c
typedef unsigned int INT;

INT
foo (INT x, INT y)
{
  if (x > 100 || y > 100)
    return 0;
  return (x * y) / y;
}
---------
gcc -O2 t.c -S -fdump-tree-optimized

  <bb 4> [local count: 467721933]:
  _1 = x_3(D) * y_4(D);
  _5 = _1 / y_4(D);

  <bb 5> [local count: 1073741824]:
  # _2 = PHI <0(2), _5(4), 0(3)>
  return _2;

While for the below case, it can be optimized.

------
typedef unsigned int INT;

INT
foo (INT x, INT y)
{
  if (x > 100 || y > 100)
    return 0;
  INT x1 = x + 1;
  INT y1 = y + 1;
  return (x1 * y1) / y1;
}
-------

The "(x1 * y1) / y1" is optimized to "x1". 

  <bb 4> [local count: 467721933]:
  x1_4 = x_2(D) + 1;

  <bb 5> [local count: 1073741824]:
  # _1 = PHI <0(2), x1_4(4), 0(3)>
  return _1;

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

* [Bug middle-end/111324] More optimization about "(X * Y) / Y"
  2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
@ 2023-09-07  9:01 ` guojiufu at gcc dot gnu.org
  2023-09-07 19:15 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: guojiufu at gcc dot gnu.org @ 2023-09-07  9:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jiu Fu Guo <guojiufu at gcc dot gnu.org> ---
In match.pd, there is a pattern:

/* Simplify (t * 2) / 2) -> t.  */
(for div (trunc_div ceil_div floor_div round_div exact_div)
 (simplify
  (div (mult:c @0 @1) @1)
  (if (ANY_INTEGRAL_TYPE_P (type))
   (if (TYPE_OVERFLOW_UNDEFINED (type))
    @0
#if GIMPLE
    (with
     {
       bool overflowed = true;
       value_range vr0, vr1;
       if (INTEGRAL_TYPE_P (type)
           && get_global_range_query ()->range_of_expr (vr0, @0)
           && get_global_range_query ()->range_of_expr (vr1, @1)
           && !vr0.varying_p () && !vr0.undefined_p ()
           && !vr1.varying_p () && !vr1.undefined_p ())
         {

Here, "get_global_range_query" is able to get the value-range info for SSA.
But it does not handle the case t.c. "get_range_query" can handle it.

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

* [Bug middle-end/111324] More optimization about "(X * Y) / Y"
  2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
  2023-09-07  9:01 ` [Bug middle-end/111324] " guojiufu at gcc dot gnu.org
@ 2023-09-07 19:15 ` pinskia at gcc dot gnu.org
  2023-09-11  3:26 ` guojiufu at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-07 19:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2023-09-07
           Severity|normal                      |enhancement
                 CC|                            |pinskia at gcc dot gnu.org

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

So using the local range in this case is ok. There might be only a few times we
don't want to use it though in match.

Here is a testcase where we should just change it into "return x;" but don't
yet:
```
typedef unsigned int INT;

INT
foo (INT x, INT y)
{
  if (x > 100 || y > 100)
    return x;
  return (x * y) / y;
}
```

Note clang is able to change it though.

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

* [Bug middle-end/111324] More optimization about "(X * Y) / Y"
  2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
  2023-09-07  9:01 ` [Bug middle-end/111324] " guojiufu at gcc dot gnu.org
  2023-09-07 19:15 ` pinskia at gcc dot gnu.org
@ 2023-09-11  3:26 ` guojiufu at gcc dot gnu.org
  2023-09-13  8:43 ` guojiufu at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: guojiufu at gcc dot gnu.org @ 2023-09-11  3:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jiu Fu Guo <guojiufu at gcc dot gnu.org> ---
A patch is posted:
https://gcc.gnu.org/pipermail/gcc-patches/2023-September/629534.html

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

* [Bug middle-end/111324] More optimization about "(X * Y) / Y"
  2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-09-11  3:26 ` guojiufu at gcc dot gnu.org
@ 2023-09-13  8:43 ` guojiufu at gcc dot gnu.org
  2023-09-13  8:59 ` guojiufu at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: guojiufu at gcc dot gnu.org @ 2023-09-13  8:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jiu Fu Guo <guojiufu at gcc dot gnu.org> ---
(In reply to Jiu Fu Guo from comment #3)
> A patch is posted:
> https://gcc.gnu.org/pipermail/gcc-patches/2023-September/629534.html
It is not for this PR. Sorry for typo.

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

* [Bug middle-end/111324] More optimization about "(X * Y) / Y"
  2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-09-13  8:43 ` guojiufu at gcc dot gnu.org
@ 2023-09-13  8:59 ` guojiufu at gcc dot gnu.org
  2023-09-18  2:30 ` guojiufu at gcc dot gnu.org
  2023-09-18  2:36 ` guojiufu at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: guojiufu at gcc dot gnu.org @ 2023-09-13  8:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jiu Fu Guo <guojiufu at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> Confirmed. 
> 
> So using the local range in this case is ok. There might be only a few times
> we don't want to use it though in match.

Agree, "get_range_query" would be more useful for most cases.


Through a quick look at match.pd, there are another two patterns that use
"get_global_range_query".

Some concerns about those patterns, so those patterns may not need to be
updated.

* (T)(A)+cst -->(T)(A+cst): I'm wondering if this transformation is really in
favor of PPC.
e.g. "return (long) x1 + 40;" could save one "extend-insn" less than "return
(long)(x1 + 40);"

* For pattern "((x * cst) + cst1) * cst2": it seems this pattern does not
affect any cases. I mean this optimization is done by other parts (before
match.pd).

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

* [Bug middle-end/111324] More optimization about "(X * Y) / Y"
  2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-09-13  8:59 ` guojiufu at gcc dot gnu.org
@ 2023-09-18  2:30 ` guojiufu at gcc dot gnu.org
  2023-09-18  2:36 ` guojiufu at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: guojiufu at gcc dot gnu.org @ 2023-09-18  2:30 UTC (permalink / raw)
  To: gcc-bugs

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

Jiu Fu Guo <guojiufu at gcc dot gnu.org> changed:

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

--- Comment #6 from Jiu Fu Guo <guojiufu at gcc dot gnu.org> ---
This can be handled by r14-3913-g8d8bc560b6ab7f3153db23ffb37157528e5b2c9a.

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

* [Bug middle-end/111324] More optimization about "(X * Y) / Y"
  2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-09-18  2:30 ` guojiufu at gcc dot gnu.org
@ 2023-09-18  2:36 ` guojiufu at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: guojiufu at gcc dot gnu.org @ 2023-09-18  2:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jiu Fu Guo <guojiufu at gcc dot gnu.org> ---
A comment https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111303#c7 
should be attached here.

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

end of thread, other threads:[~2023-09-18  2:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-07  8:53 [Bug middle-end/111324] New: More optimization about "(X * Y) / Y" guojiufu at gcc dot gnu.org
2023-09-07  9:01 ` [Bug middle-end/111324] " guojiufu at gcc dot gnu.org
2023-09-07 19:15 ` pinskia at gcc dot gnu.org
2023-09-11  3:26 ` guojiufu at gcc dot gnu.org
2023-09-13  8:43 ` guojiufu at gcc dot gnu.org
2023-09-13  8:59 ` guojiufu at gcc dot gnu.org
2023-09-18  2:30 ` guojiufu at gcc dot gnu.org
2023-09-18  2:36 ` guojiufu 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).