public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/101610] New: CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2
@ 2021-07-24  6:20 pinskia at gcc dot gnu.org
  2021-07-24  6:20 ` [Bug tree-optimization/101610] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-24  6:20 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101610
           Summary: CST - (x ^ (CST-1)) can be optimized to x + 1 if x <
                    CST and CST is a power of 2
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
unsigned long f(unsigned long x)
{
    if (x >= 64)__builtin_unreachable();
    x = x ^ 63;
unsigned long y = (unsigned long )x;
unsigned long z = 64 - y;
return z;
}
This should just be:
unsigned long long f1(unsigned long x)
{
  return x + 1;
}

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

* [Bug tree-optimization/101610] CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2
  2021-07-24  6:20 [Bug tree-optimization/101610] New: CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2 pinskia at gcc dot gnu.org
@ 2021-07-24  6:20 ` pinskia at gcc dot gnu.org
  2021-07-28 21:43 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-24  6:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I noticed this while looking into PR 78103.

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

* [Bug tree-optimization/101610] CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2
  2021-07-24  6:20 [Bug tree-optimization/101610] New: CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2 pinskia at gcc dot gnu.org
  2021-07-24  6:20 ` [Bug tree-optimization/101610] " pinskia at gcc dot gnu.org
@ 2021-07-28 21:43 ` pinskia at gcc dot gnu.org
  2021-07-30 21:53 ` pinskia at gcc dot gnu.org
  2021-08-03 23:15 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-28 21:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=96921
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2021-07-28
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So looking at this one (and one which I just assigned myself):
unsigned long f(unsigned long x)
{
    if (x >= 64)__builtin_unreachable();
    x = x ^ 63;
unsigned long y = x; ;; Range is still [0,63]
unsigned long z = 64 - y; ;; is similar to (63 - y) +1 -> (y ^ 63) + 1 -> x + 1
return z;
}

So mine:

Something like:
(simplify
 (minus INTEGER_CST@0 SSA_NAME@1)
 (if (exact_power2(@0) && get_nonzero_bits(@1) == (@0 - 1)
  (add (bit_xor! @1 @0) {build_one_cst (type); }))

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

* [Bug tree-optimization/101610] CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2
  2021-07-24  6:20 [Bug tree-optimization/101610] New: CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2 pinskia at gcc dot gnu.org
  2021-07-24  6:20 ` [Bug tree-optimization/101610] " pinskia at gcc dot gnu.org
  2021-07-28 21:43 ` pinskia at gcc dot gnu.org
@ 2021-07-30 21:53 ` pinskia at gcc dot gnu.org
  2021-08-03 23:15 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-30 21:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> So looking at this one (and one which I just assigned myself):
> unsigned long f(unsigned long x)
> {
>     if (x >= 64)__builtin_unreachable();
>     x = x ^ 63;
> unsigned long y = x; ;; Range is still [0,63]
> unsigned long z = 64 - y; ;; is similar to (63 - y) +1 -> (y ^ 63) + 1 -> x
> + 1
> return z;
> }
> 
> So mine:
> 
> Something like:
> (simplify
>  (minus INTEGER_CST@0 SSA_NAME@1)
>  (if (exact_power2(@0) && get_nonzero_bits(@1) == (@0 - 1)
>   (add (bit_xor! @1 @0) {build_one_cst (type); }))

But I don't think this should be done at the gimple level unless we have a
place were we decide it should be considered "lowered gimple".
I will be doing a simplify-rtx.c patch for this case which should get some code
generation improvement but not with the original case as we need to export the
non-zero bits down from gimple to RTL still (though I hear someone is working
on keeping around the non-zero bits around through out the whole RTL phase).

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

* [Bug tree-optimization/101610] CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2
  2021-07-24  6:20 [Bug tree-optimization/101610] New: CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2 pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-07-30 21:53 ` pinskia at gcc dot gnu.org
@ 2021-08-03 23:15 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-03 23:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is basically PR 91213.

*** This bug has been marked as a duplicate of bug 91213 ***

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

end of thread, other threads:[~2021-08-03 23:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-24  6:20 [Bug tree-optimization/101610] New: CST - (x ^ (CST-1)) can be optimized to x + 1 if x < CST and CST is a power of 2 pinskia at gcc dot gnu.org
2021-07-24  6:20 ` [Bug tree-optimization/101610] " pinskia at gcc dot gnu.org
2021-07-28 21:43 ` pinskia at gcc dot gnu.org
2021-07-30 21:53 ` pinskia at gcc dot gnu.org
2021-08-03 23:15 ` 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).