public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/85234] missed optimisation opportunity for (~x >> n) ? a : b with n, a, b constants
       [not found] <bug-85234-4@http.gcc.gnu.org/bugzilla/>
@ 2021-07-20 23:33 ` pinskia at gcc dot gnu.org
  2021-07-20 23:44 ` [Bug tree-optimization/85234] missed optimisation opportunity for (~x >> CST)!=0 is not optimized to (((unsigned)x) < ~((1<<CST) - 1)) pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-20 23:33 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

* [Bug tree-optimization/85234] missed optimisation opportunity for (~x >> CST)!=0 is not optimized to   (((unsigned)x) < ~((1<<CST) - 1))
       [not found] <bug-85234-4@http.gcc.gnu.org/bugzilla/>
  2021-07-20 23:33 ` [Bug middle-end/85234] missed optimisation opportunity for (~x >> n) ? a : b with n, a, b constants pinskia at gcc dot gnu.org
@ 2021-07-20 23:44 ` pinskia at gcc dot gnu.org
  2023-05-28  2:26 ` [Bug tree-optimization/85234] missed optimisation opportunity for (x >> CST)!=0 is not optimized to (((unsigned)x) >= (1<<CST) pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-20 23:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|missed optimisation         |missed optimisation
                   |opportunity for (~x >> n) ? |opportunity for (~x >>
                   |a : b with n, a, b          |CST)!=0 is not optimized to
                   |constants                   |  (((unsigned)x) <
                   |                            |~((1<<CST) - 1))
          Component|middle-end                  |tree-optimization

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
int f(int x)
{
    return (~x >> 3) !=0;
}
int f2(int x)
{
    return (((unsigned)x) < ~((1<<3) - 1));
}

The above is what LLVM does really

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

* [Bug tree-optimization/85234] missed optimisation opportunity for (x >> CST)!=0 is not optimized to   (((unsigned)x) >=  (1<<CST)
       [not found] <bug-85234-4@http.gcc.gnu.org/bugzilla/>
  2021-07-20 23:33 ` [Bug middle-end/85234] missed optimisation opportunity for (~x >> n) ? a : b with n, a, b constants pinskia at gcc dot gnu.org
  2021-07-20 23:44 ` [Bug tree-optimization/85234] missed optimisation opportunity for (~x >> CST)!=0 is not optimized to (((unsigned)x) < ~((1<<CST) - 1)) pinskia at gcc dot gnu.org
@ 2023-05-28  2:26 ` pinskia at gcc dot gnu.org
  2023-05-28 22:10 ` pinskia at gcc dot gnu.org
  2023-05-28 23:05 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-28  2:26 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|missed optimisation         |missed optimisation
                   |opportunity for (~x >>      |opportunity for (x >>
                   |CST)!=0 is not optimized to |CST)!=0 is not optimized to
                   |  (((unsigned)x) <          |  (((unsigned)x) >=
                   |~((1<<CST) - 1))            |(1<<CST)

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The general thing is:
  _2 = a >> 3;
  if (_2 != 0)

Should be optimized to just:
((unsigned)a) >= (1<<3)
Which is the same as:
((unsigned)a) > ((1<<3) - 1)

And then
~x > ((1<<3) - 1)

gets (already) optimized to:
x.0_1 <= -9

PR 110010 is related because instead of a 0, we have a similar shift.

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

* [Bug tree-optimization/85234] missed optimisation opportunity for (x >> CST)!=0 is not optimized to   (((unsigned)x) >=  (1<<CST)
       [not found] <bug-85234-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2023-05-28  2:26 ` [Bug tree-optimization/85234] missed optimisation opportunity for (x >> CST)!=0 is not optimized to (((unsigned)x) >= (1<<CST) pinskia at gcc dot gnu.org
@ 2023-05-28 22:10 ` pinskia at gcc dot gnu.org
  2023-05-28 23:05 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-28 22:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Here are some testcases dealing with this and showing what still needs to be
done:
```
#define N 3
#define unsigned int
#define cmp !=
_Bool rshift(unsigned x, int t)
{
        return (x << N) cmp 0;
}
_Bool rshift1(unsigned x, int t)
{
        return (x & (-1u>>N)) cmp 0;
}
_Bool lshift(unsigned x, int t)
{
        return (x >> N) cmp 0;
}
_Bool lshift1(unsigned x, int t)
{
        return (x & (-1u<<N)) cmp 0;
}
_Bool lshift2(unsigned x, int t)
{
        return (x >= (1u<<N)) cmp 0;
}
```

GCC handles lshift1 to lshift2 just fine.
But not lshift to lshift1 and rshift to rshift1.

Note GCC does the correct thing rshift/rshift1 even for riscv32 where forming
an constant for the and is hard.

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

* [Bug tree-optimization/85234] missed optimisation opportunity for (x >> CST)!=0 is not optimized to   (((unsigned)x) >=  (1<<CST)
       [not found] <bug-85234-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2023-05-28 22:10 ` pinskia at gcc dot gnu.org
@ 2023-05-28 23:05 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-28 23:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Here is the testcase for the constants besides 0:L
```
#define N 3
#define unsigned int
#define cmp ==
#define M 0xf0000000u
_Bool f(unsigned x, int t)
{
        return (x << N) cmp (M << N);
}
_Bool f1(unsigned x, int t)
{
        return ((x^M) & (-1u>>N)) cmp 0;
}

_Bool f2(unsigned x, int t)
{
        return (x & (-1u>>N)) cmp (M & (-1u>>N));
}

_Bool g(unsigned x, int t)
{
        return (x >> N) cmp M;
}
_Bool g2(unsigned x, int t)
{
        _Bool tttt = 0;
        if (tttt)
        return 0;
        return (x & (-1u<<N)) cmp (M << N);
}
#if M==0
_Bool g1(unsigned x, int t)
{
        return (x >= (1u<<N)) cmp M;
}
#endif
```
The tttt is defined somehow but I can't figure it out right now. 

Note for f, The bottom N bits need not to be set, that is basically what (M <<
N) specifies.

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

end of thread, other threads:[~2023-05-28 23:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-85234-4@http.gcc.gnu.org/bugzilla/>
2021-07-20 23:33 ` [Bug middle-end/85234] missed optimisation opportunity for (~x >> n) ? a : b with n, a, b constants pinskia at gcc dot gnu.org
2021-07-20 23:44 ` [Bug tree-optimization/85234] missed optimisation opportunity for (~x >> CST)!=0 is not optimized to (((unsigned)x) < ~((1<<CST) - 1)) pinskia at gcc dot gnu.org
2023-05-28  2:26 ` [Bug tree-optimization/85234] missed optimisation opportunity for (x >> CST)!=0 is not optimized to (((unsigned)x) >= (1<<CST) pinskia at gcc dot gnu.org
2023-05-28 22:10 ` pinskia at gcc dot gnu.org
2023-05-28 23:05 ` 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).