[Bug tree-optimization/108215] New: Does not optimize trivial case with bit operations

socketpair at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Dec 23 18:48:58 GMT 2022


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

            Bug ID: 108215
           Summary: Does not optimize trivial case with bit operations
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: socketpair at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/5e3eKqPqs

```C
#include <stdint.h>

int firewall3(const uint8_t *restrict data) {
    const uint32_t src = *((const uint32_t *)data);
    if ((src & 0xFFFF0000) == 0x11220000) return 1;
    if ((src & 0xFFFFFF00) == 0x11223300) return 1;
    return 0;
}

int firewall4(const uint8_t *restrict data) {
    const uint32_t src = *((const uint32_t *)data);
    if ((src & 0xFFFFFF00) == 0x11223300) return 1;
    if ((src & 0xFFFF0000) == 0x11220000) return 1;
    return 0;
}
```

```
firewall3:
        movl    (%rdi), %eax
        xorw    %ax, %ax
        cmpl    $287440896, %eax
        sete    %al
        movzbl  %al, %eax
        ret
firewall4:
        movl    (%rdi), %eax
        movl    $1, %edx
        movl    %eax, %ecx
        xorb    %cl, %cl
        cmpl    $287453952, %ecx
        je      .L3
        xorw    %ax, %ax
        xorl    %edx, %edx
        cmpl    $287440896, %eax
        sete    %dl
.L3:
        movl    %edx, %eax
        ret
```

firewall3(): Excellent!
firewall4(): FAIL!

It's obvious that order of comparisons in this example does not matter. So I
think misoptimisation of firewall4() is a bug.


More information about the Gcc-bugs mailing list