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