Bug 101541 - Missing ABSU detection at gimple
Summary: Missing ABSU detection at gimple
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.0
: P3 enhancement
Target Milestone: 14.0
Assignee: Andrew Pinski
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: missed-optimization, patch
Depends on:
Blocks:
 
Reported: 2021-07-20 22:58 UTC by Andrew Pinski
Modified: 2023-10-26 00:11 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-21 00:00:00


Attachments
Patch which I am testing (1.47 KB, patch)
2023-10-15 17:42 UTC, Andrew Pinski
Details | Diff
Patch which I am testing which allows for more (independent of the other) (1.31 KB, patch)
2023-10-15 19:24 UTC, Andrew Pinski
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2021-07-20 22:58:54 UTC
While looking at PR 44608 and finding it was already fixed, I noticed instead we should produce ABSU at the gimple level but don't currently.

Take:
unsigned abssat2 (int x)
{
    unsigned int y = x;

    if (x < 0)
        y = -y;

  return y;
}
Comment 1 Richard Biener 2021-07-21 07:01:03 UTC
Confirmed.
Comment 2 Andrew Pinski 2021-11-20 10:58:04 UTC
Mine, let me try to figure out the best way to fix this.
Comment 3 Andrew Pinski 2021-11-20 22:03:12 UTC
(In reply to Andrew Pinski from comment #2)
> Mine, let me try to figure out the best way to fix this.

Basically the patterns starting with:
/* abs/negative simplifications moved from fold_cond_expr_with_comparison,
   Need to handle (A - B) case as fold_cond_expr_with_comparison does.
   Need to handle UN* comparisons.

need to add nop_convertN? handling. There is 6 patterns.
Comment 4 Andrew Pinski 2023-05-04 00:01:33 UTC
Another one missed:
```
unsigned abssat2 (unsigned x)
{
    int y = x;

    if (y < 0)
        x = -x;

  return x;
}
```

I have a patch for the this case, I think. Working on the other case now.
Comment 5 Andrew Pinski 2023-10-15 16:59:31 UTC
Actually using bitwise_equal_p makes this easier and no need for the nop_convert ...

I have a patch where I just need to add testcases too.
Comment 6 Andrew Pinski 2023-10-15 17:42:14 UTC
Created attachment 56116 [details]
Patch which I am testing
Comment 7 Andrew Pinski 2023-10-15 19:24:10 UTC
Created attachment 56118 [details]
Patch which I am testing which allows for more (independent of the other)

This patch improves the case where we have a conversion at the end (after the negate expression). And with both of these patches together we are able to catch almost all.

There is one more phi-opt patch needed to catch the case where the conversion before the negate expression is conditional but that is for another day.
Comment 9 GCC Commits 2023-10-16 17:11:33 UTC
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:c7609acb8a8210188d21b2cd72ecc6d3b2de2ab8

commit r14-4662-gc7609acb8a8210188d21b2cd72ecc6d3b2de2ab8
Author: Andrew Pinski <pinskia@gmail.com>
Date:   Sun Oct 15 10:36:56 2023 -0700

    MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
    
    This improves the `A CMP 0 ? A : -A` set of match patterns to use
    bitwise_equal_p which allows an nop cast between signed and unsigned.
    This allows catching a few extra cases which were not being caught before.
    
    OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
    
    gcc/ChangeLog:
    
            PR tree-optimization/101541
            * match.pd (A CMP 0 ? A : -A): Improve
            using bitwise_equal_p.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/101541
            * gcc.dg/tree-ssa/phi-opt-36.c: New test.
            * gcc.dg/tree-ssa/phi-opt-37.c: New test.
Comment 10 Andrew Pinski 2023-10-16 17:15:04 UTC
Fixed.
Comment 11 GCC Commits 2023-10-24 11:17:45 UTC
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:0fc13e8c0e39c51e82deb93f324d9d86ad8d7460

commit r14-4889-g0fc13e8c0e39c51e82deb93f324d9d86ad8d7460
Author: Andrew Pinski <pinskia@gmail.com>
Date:   Sun Oct 15 19:15:38 2023 +0000

    Improve factor_out_conditional_operation for conversions and constants
    
    In the case of a NOP conversion (precisions of the 2 types are equal),
    factoring out the conversion can be done even if int_fits_type_p returns
    false and even when the conversion is defined by a statement inside the
    conditional. Since it is a NOP conversion there is no zero/sign extending
    happening which is why it is ok to be done here; we were trying to prevent
    an extra sign/zero extend from being moved away from definition which no-op
    conversions are not.
    
    Bootstrapped and tested on x86_64-linux-gnu with no regressions.
    
    gcc/ChangeLog:
    
            PR tree-optimization/104376
            PR tree-optimization/101541
            * tree-ssa-phiopt.cc (factor_out_conditional_operation):
            Allow nop conversions even if it is defined by a statement
            inside the conditional.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/101541
            * gcc.dg/tree-ssa/phi-opt-39.c: New test.