https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91680
Bug ID: 91680 Summary: Integer promotion quirk prevents efficient power of 2 division Product: gcc Version: 9.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: kim.nilsson at effnet dot com Target Milestone: --- Created attachment 46841 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46841&action=edit Preprocessed source For the function, uint_fast32_t foo_a(uint_fast8_t x) { const uint_fast32_t q = 1 << x; return 256 / q; } GCC-{8.*,9.*} generates inefficient division instructions for both x86_64 and ARM. Since 'q' is a power of 2, the result should be a single right shift. Indeed, if the function is changed to, uint_fast32_t foo_b(uint_fast8_t x) { return 256 / (1 << x); // This is, of course, equivalent to (256 >> x) }, the expected instructions are generated. Counter-intuitively, if the original function is instead changed slightly to, uint_fast32_t foo_c(uint_fast8_t x) { const uint_fast32_t q = (uint_fast32_t)1 << x; return 256 / q; }, the expected instructions are once again generated. See https://godbolt.org/z/37K0Ig for examples.