public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/111718] New: Missed optimization of '(a+a)/a'
@ 2023-10-07 11:05 652023330028 at smail dot nju.edu.cn
  2023-10-07 15:46 ` [Bug tree-optimization/111718] " vanyacpp at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2023-10-07 11:05 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111718
           Summary: Missed optimization of '(a+a)/a'
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello, we found some optimizations (regarding Arithmetic optimization) that GCC
may have missed. We would greatly appreicate if you can take a look and let us
know what you think.

Given the following code: 
https://godbolt.org/z/5de17zvz9

unsigned n1,n2;
void func1(unsigned a){
    if(a>10&&a<20){
        n1=a+a;
        n2=(a+a)/a;
    }
}

We note that `(a+a)/a` should be optimized to `2`, but gcc-trunk -O3 does not:
func1(unsigned int):
        lea     eax, [rdi-11]
        cmp     eax, 8
        ja      .L1
        lea     eax, [rdi+rdi]
        xor     edx, edx
        mov     DWORD PTR n1[rip], eax
        div     edi
        mov     DWORD PTR n2[rip], eax
.L1:
        ret


Thank you very much for your time and effort! We look forward to hearing from
you.

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

* [Bug tree-optimization/111718] Missed optimization of '(a+a)/a'
  2023-10-07 11:05 [Bug tree-optimization/111718] New: Missed optimization of '(a+a)/a' 652023330028 at smail dot nju.edu.cn
@ 2023-10-07 15:46 ` vanyacpp at gmail dot com
  2023-10-08  3:03 ` 652023330028 at smail dot nju.edu.cn
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: vanyacpp at gmail dot com @ 2023-10-07 15:46 UTC (permalink / raw)
  To: gcc-bugs

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

Ivan Sorokin <vanyacpp at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vanyacpp at gmail dot com

--- Comment #1 from Ivan Sorokin <vanyacpp at gmail dot com> ---
GCC does the optimization if the return from the function is replaced with
__builtin_unreachable:

unsigned n1, n2;

void func1(unsigned a)
{
    if (a <= 10 || a >= 20)
        __builtin_unreachable();

    n1 = a + a;
    n2 = (a + a)/a;
}

func1(unsigned int):
        mov     DWORD PTR n2[rip], 2
        add     edi, edi
        mov     DWORD PTR n1[rip], edi
        ret

https://godbolt.org/z/Tjsz6neTs

Perhaps this issue has the same underlying cause as the PR80015.

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

* [Bug tree-optimization/111718] Missed optimization of '(a+a)/a'
  2023-10-07 11:05 [Bug tree-optimization/111718] New: Missed optimization of '(a+a)/a' 652023330028 at smail dot nju.edu.cn
  2023-10-07 15:46 ` [Bug tree-optimization/111718] " vanyacpp at gmail dot com
@ 2023-10-08  3:03 ` 652023330028 at smail dot nju.edu.cn
  2023-10-08  3:52 ` pinskia at gcc dot gnu.org
  2023-10-08  4:00 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2023-10-08  3:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Yi <652023330028 at smail dot nju.edu.cn> ---
We noticed one change between gcc-13.2 and the current gcc-trunk:

https://godbolt.org/z/j5Mnvno9n

In the following code, gcc-13.2 does not yet have the ability to optimize as
expected, but on gcc-trunk, it does.

unsigned n1,n2;
void func1(unsigned a){
    if(a<=10 || a>=20)
        return;
    n2=(a+a)/a;
}


Maybe this change will help solve this issue?

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

* [Bug tree-optimization/111718] Missed optimization of '(a+a)/a'
  2023-10-07 11:05 [Bug tree-optimization/111718] New: Missed optimization of '(a+a)/a' 652023330028 at smail dot nju.edu.cn
  2023-10-07 15:46 ` [Bug tree-optimization/111718] " vanyacpp at gmail dot com
  2023-10-08  3:03 ` 652023330028 at smail dot nju.edu.cn
@ 2023-10-08  3:52 ` pinskia at gcc dot gnu.org
  2023-10-08  4:00 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-08  3:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
For comment #2 from EVRP:
Folding statement: _3 = _2 / a_5(D);
Applying pattern match.pd:934, gimple-match-4.cc:2021
gimple_simplified to _3 = 2;

Which corresponds to the match 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 {value_range vr0, vr1;}
     (if (INTEGRAL_TYPE_P (type)
          && get_range_query (cfun)->range_of_expr (vr0, @0)
          && get_range_query (cfun)->range_of_expr (vr1, @1)
          && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
      @0))
#endif
   ))))

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

* [Bug tree-optimization/111718] Missed optimization of '(a+a)/a'
  2023-10-07 11:05 [Bug tree-optimization/111718] New: Missed optimization of '(a+a)/a' 652023330028 at smail dot nju.edu.cn
                   ` (2 preceding siblings ...)
  2023-10-08  3:52 ` pinskia at gcc dot gnu.org
@ 2023-10-08  4:00 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-08  4:00 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2023-10-08

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> For comment #2 from EVRP:
> Folding statement: _3 = _2 / a_5(D);
> Applying pattern match.pd:934, gimple-match-4.cc:2021
> gimple_simplified to _3 = 2;
> 
> Which corresponds to the match 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 {value_range vr0, vr1;}
>      (if (INTEGRAL_TYPE_P (type)
> 	  && get_range_query (cfun)->range_of_expr (vr0, @0)
> 	  && get_range_query (cfun)->range_of_expr (vr1, @1)
> 	  && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
>       @0))
> #endif
>    ))))

Which was improved on the trunk by r14-4082-g55b22a6f630e (and then by
r14-4191-gd946fc1c71bd). I don't know why the original testcase is not causing
the above pattern to match though, maybe because a*2 is used twice ...

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

end of thread, other threads:[~2023-10-08  4:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-07 11:05 [Bug tree-optimization/111718] New: Missed optimization of '(a+a)/a' 652023330028 at smail dot nju.edu.cn
2023-10-07 15:46 ` [Bug tree-optimization/111718] " vanyacpp at gmail dot com
2023-10-08  3:03 ` 652023330028 at smail dot nju.edu.cn
2023-10-08  3:52 ` pinskia at gcc dot gnu.org
2023-10-08  4:00 ` 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).