public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/104444] New: Missing constant folding in shift expression.
@ 2022-02-08 19:35 cassio.neri at gmail dot com
  2022-02-08 21:53 ` [Bug tree-optimization/104444] " pinskia at gcc dot gnu.org
  2022-02-09  7:30 ` rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: cassio.neri at gmail dot com @ 2022-02-08 19:35 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104444
           Summary: Missing constant folding in shift expression.
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cassio.neri at gmail dot com
  Target Milestone: ---

#include <cstdint>

inline bool f(uint32_t m, int n) {
  return (m >> n) != 0;
}

bool g(int n) {
  return f(1 << 24, n);
}

g can be optimised to "return n <= 24". LLVM does that but gcc doesn't.

The example above drove me to another missing optimisation opportunity based on
undefined behaviour. (Perhaps a matter for other report?)

bool h(uint32_t m, int n) {
  return (n >= 0 && n < 32) || (m >> n) != 0;
}

If (n >= 0 && n < 32) is false, then (m >> n) is UB (in C++, probably also in
C). Therefore, h can be optimised to "return true" but gcc doesn't do that
(neither does LLVM).

See here: https://godbolt.org/z/hx9vGe6Kj

If confirmed, these bugs could be added to
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19987

Potentially related:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95817
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94789#c1

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

* [Bug tree-optimization/104444] Missing constant folding in shift expression.
  2022-02-08 19:35 [Bug tree-optimization/104444] New: Missing constant folding in shift expression cassio.neri at gmail dot com
@ 2022-02-08 21:53 ` pinskia at gcc dot gnu.org
  2022-02-09  7:30 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-08 21:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-02-08

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed:
int g(unsigned n) {
  return ((0xffffff) >> n) != 0;
}

So the general case is:
(simplify
 (neeq (lshift INTEGER_CST@0 @1) zero_p)
 (ltge (@1 {bitsizeoftype(@1) - wi::clz(@0);}))

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

* [Bug tree-optimization/104444] Missing constant folding in shift expression.
  2022-02-08 19:35 [Bug tree-optimization/104444] New: Missing constant folding in shift expression cassio.neri at gmail dot com
  2022-02-08 21:53 ` [Bug tree-optimization/104444] " pinskia at gcc dot gnu.org
@ 2022-02-09  7:30 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-02-09  7:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note we generally avoid "folding" undefined to anything specific, but what
could
trigger here is path isolation seeing

  if (n >= 0 && n < 32)
   ;
  else
     if (m >> n != 0)
       ...

making the else path unreachable (or trap for the sake of QOI).

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

end of thread, other threads:[~2022-02-09  7:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-08 19:35 [Bug tree-optimization/104444] New: Missing constant folding in shift expression cassio.neri at gmail dot com
2022-02-08 21:53 ` [Bug tree-optimization/104444] " pinskia at gcc dot gnu.org
2022-02-09  7:30 ` rguenth 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).