[Bug middle-end/20517] New: bit shift/mask optimization potential

Thomas dot Koenig at online dot de gcc-bugzilla@gcc.gnu.org
Thu Mar 17 13:17:00 GMT 2005

$ cat shift.c
#include <stdio.h>

#define OFFSET 4
#define MASK 0xf0

int main()
    unsigned int f,g;
    if ((f & MASK) >> OFFSET == 1) {
    return 0;
$ gcc -S -O2 -fdump-tree-optimized shift.c
$ tail -20 shift.c.t66.optimized
  unsigned int g;
  unsigned int f;
  int D.1807;
  unsigned int D.1806;
  unsigned int D.1805;
  unsigned int f.10;

<bb 0>:
  scanf (&"%u"[0], &f);
  if ((f & 240) >> 4 == 1) goto <L0>; else goto <L1>;

  printf (&"success\n"[0]);

  return 0;


$ gcc -v
Using built-in specs.
Target: ia64-unknown-linux-gnu
Configured with: ../gcc-4.1-20050306/configure --prefix=/home/zfkts
--enable-languages=c,f95 --disable-optimization
Thread model: posix
gcc version 4.1.0 20050306 (experimental)

The expression (f & 0xf0) >> 4 == 1 could be optimized
to f & 0xf == 1 << 4.

Code like that occurs in the libgfortan library.

           Summary: bit shift/mask optimization potential
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: middle-end
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Thomas dot Koenig at online dot de
                CC: gcc-bugs at gcc dot gnu dot org


More information about the Gcc-bugs mailing list