public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics
@ 2023-01-13 16:52 jakub at gcc dot gnu.org
  2023-01-13 19:01 ` [Bug tree-optimization/108397] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-01-13 16:52 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108397
           Summary: Missed optimization with [0, 0][-1U,-1U] range
                    arithmetics
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jakub at gcc dot gnu.org
                CC: hjl.tools at gmail dot com, jakub at gcc dot gnu.org,
                    marxin at gcc dot gnu.org, rguenth at gcc dot gnu.org,
                    unassigned at gcc dot gnu.org, uros at gcc dot gnu.org,
                    yinyuefengyi at gmail dot com, zsojka at seznam dot cz
        Depends on: 107131
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu

+++ This bug was initially created as a clone of Bug #107131 +++

__attribute__((noipa)) unsigned long long                                       
foo (unsigned char o)                                                           
{                                                                               
  unsigned long long t1 = -(long long) (o == 0);                                
  unsigned long long t2 = -(long long) (t1 > 10439075533421201520ULL);          
  unsigned long long t3 = -(long long) (t1 <= t2);                              
  return t3;                                                                    
}                                                                               

from comment 8 of that PR could be optimized into return -1ULL.
t1 has range [0,0][-1ULL,-1ULL], t2 is set to 0 if t1 is 0 and to -1ULL if t1
is -1ULL
and thus t2 could be optimized to t2 = t1.  And t1 <= t2 can then trivially
fold to
t1 <= t1 aka true.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107131
[Bug 107131] [11/12 Regression] wrong code with -Os -fno-ipa-vrp
-fno-tree-bit-ccp

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

* [Bug tree-optimization/108397] Missed optimization with [0, 0][-1U,-1U] range arithmetics
  2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
@ 2023-01-13 19:01 ` pinskia at gcc dot gnu.org
  2023-08-08 21:39 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-13 19:01 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-01-13
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Severity|normal                      |enhancement

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

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

* [Bug tree-optimization/108397] Missed optimization with [0, 0][-1U,-1U] range arithmetics
  2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
  2023-01-13 19:01 ` [Bug tree-optimization/108397] " pinskia at gcc dot gnu.org
@ 2023-08-08 21:39 ` pinskia at gcc dot gnu.org
  2023-08-08 21:40 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-08 21:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think the simple missed optimization here is:
```
int f2(int a)
{
 if(a != -1 && a != 0)
   __builtin_unreachable();
  unsigned c = a;
  if(c > 121212)
    return 1;
  return 0;
}
```
This should be optimized to just:
```
int f2_(int a)
{
  return a != 0;
}
```


That is if we have:
```
  # RANGE [irange] unsigned int [0, 0][+INF, +INF]
  a.0_1 = (unsigned int) a_4(D);
  if (a.0_1 > 121212)
```
Since we have two values for a.0_1 we can just compare to one or the other in
the above case.

Then that will optimize the original testcase as we can optimize away the
nop_convert and then negative and then we get:
t1 <= t1
and that is folded trivially to true.

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

* [Bug tree-optimization/108397] Missed optimization with [0, 0][-1U,-1U] range arithmetics
  2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
  2023-01-13 19:01 ` [Bug tree-optimization/108397] " pinskia at gcc dot gnu.org
  2023-08-08 21:39 ` pinskia at gcc dot gnu.org
@ 2023-08-08 21:40 ` pinskia at gcc dot gnu.org
  2023-08-08 21:45 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-08 21:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I have seen other bug reports having a similar issue too.

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

* [Bug tree-optimization/108397] Missed optimization with [0, 0][-1U,-1U] range arithmetics
  2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-08-08 21:40 ` pinskia at gcc dot gnu.org
@ 2023-08-08 21:45 ` pinskia at gcc dot gnu.org
  2023-08-08 22:38 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-08 21:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also:
```
int f(int a, int b)
{
        int c = a == b;
        c = -c;
        return c <= -1;
}
```
At `-O2 -fwrapv` is not optimized at the gimple level but is at the RTL level
even.

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

* [Bug tree-optimization/108397] Missed optimization with [0, 0][-1U,-1U] range arithmetics
  2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-08-08 21:45 ` pinskia at gcc dot gnu.org
@ 2023-08-08 22:38 ` pinskia at gcc dot gnu.org
  2023-08-09  1:09 ` pinskia at gcc dot gnu.org
  2023-08-31 19:53 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-08 22:38 UTC (permalink / raw)
  To: gcc-bugs

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

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 #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I am going to change/fix va-values.cc:test_for_singularity to implement this.

The comment before this function even says:
```
/* We are comparing trees OP0 and OP1 using COND_CODE.  OP0 has
   a known value range VR.

   If there is one and only one value which will satisfy the
   conditional, then return that value.  Else return NULL.
```
Which does make it sound like it should have done that too.

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

* [Bug tree-optimization/108397] Missed optimization with [0, 0][-1U,-1U] range arithmetics
  2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-08-08 22:38 ` pinskia at gcc dot gnu.org
@ 2023-08-09  1:09 ` pinskia at gcc dot gnu.org
  2023-08-31 19:53 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-09  1:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I have a patch to fix the testcase in comment #2.

After that patch we have:
```
  _1 = o_10(D) == 0;
  _2 = (long long int) _1;
  _3 = -_2;
  t1_11 = (long long unsigned int) _3;
  _4 = t1_11 == 18446744073709551615;
  _5 = (long long int) _4;
  _6 = -_5;
  t2_12 = (long long unsigned int) _6;
  _7 = t1_11 <= t2_12;
  _8 = (long long int) _7;
  _9 = -_8;
  t3_13 = (long long unsigned int) _9;
```

Forwprop will not change _4 to _1 though. because t1_11 is used twice.

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

* [Bug tree-optimization/108397] Missed optimization with [0, 0][-1U,-1U] range arithmetics
  2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-08-09  1:09 ` pinskia at gcc dot gnu.org
@ 2023-08-31 19:53 ` pinskia at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-31 19:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #6)
> Forwprop will not change _4 to _1 though. because t1_11 is used twice.

But combined with PR 107137, we are able to optimize it just fine.

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

end of thread, other threads:[~2023-08-31 19:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-13 16:52 [Bug tree-optimization/108397] New: Missed optimization with [0, 0][-1U,-1U] range arithmetics jakub at gcc dot gnu.org
2023-01-13 19:01 ` [Bug tree-optimization/108397] " pinskia at gcc dot gnu.org
2023-08-08 21:39 ` pinskia at gcc dot gnu.org
2023-08-08 21:40 ` pinskia at gcc dot gnu.org
2023-08-08 21:45 ` pinskia at gcc dot gnu.org
2023-08-08 22:38 ` pinskia at gcc dot gnu.org
2023-08-09  1:09 ` pinskia at gcc dot gnu.org
2023-08-31 19:53 ` 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).