https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77975
Bug ID: 77975 Summary: [6 / 7 Regression] Missed optimization for some small constants Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: quiath at go2 dot pl Target Milestone: --- When compiling with GCC 6.1, 6.2 or 7.0 the code generated with -O1 -O2 and -O3 does not fold depending on some small constants e.g. 3 used in the body of the function. GCC 5.4 was better. Example code: // missed optimization, compiled to a loop unsigned int f3() { unsigned int a = 3; while (a) { a >>= 1; } return a; } // expected optimization, compiled to a single instruction unsigned int f7() { unsigned int a = 7; while (a) { a >>= 1; } return a; } x86-64 assembly from gcc 7 (identical for 6) according to godbolt.org Note that f7 is folded while f3 is not. f3(): mov eax, 3 .L2: shr eax jne .L2 mov eax, 0 ret f7(): mov eax, 0 ret See also example here: https://godbolt.org/g/ircKQ0