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

            Bug ID: 111378
           Summary: Missed optimization for comparing with exact_log2
                    constants
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lis8215 at gmail dot com
  Target Milestone: ---

The simple example below produces suboptimal code on many targets where
exact_log2 constant can't be represented as immediate operand.
(confirmed MIPS/PPC64/SPARC/RISC-V)

extern void do_something(char* p);
extern void do_something_other(char* p);

void test(char* p, uint32_t ch)
{
        if (ch < 0x10000)
        {
            do_something(p);
        }
        else /* ch >= 0x10000 */
        {
            do_something_other(p);
        }
}

However, instead of direct comparing with constant we can use shift & compare
to zero:
e.g. (ch < 0x10000) can be transformed into ((ch >> 16) == 0) which is usually
shorter & faster on many targets.

The condition appears in real world rarely AFAIK - 20-30 occurences per million
asm instructions. Fun fact: many of them related to unicode transformations.

Reply via email to