Skip to content

Clang trunk does not optimize trivial case #53861

@socketpair

Description

@socketpair

https://godbolt.org/z/65Tza7cdf

#include <stdint.h>

extern uint16_t qwe, asd, zxc, zzz;

int firewall(const uint8_t *restrict data) {
    const uint8_t ip_proto = *data;
    const uint16_t dst_port = *((const uint16_t *)data + 32);

    if (ip_proto == 17 && dst_port == qwe) return 1;

    if (ip_proto == 17 && dst_port == asd) return 1;

    if (ip_proto == 17 && dst_port == zxc) return 1;

    if (ip_proto == 17 && dst_port == zzz) return 1;

    return 0;
}
firewall:                               # @firewall
        mov     dl, byte ptr [rdi]
        movzx   ecx, word ptr [rdi + 64]
        mov     eax, 1
        cmp     dl, 17
        jne     .LBB0_2
        cmp     cx, word ptr [rip + qwe]
        jne     .LBB0_2
.LBB0_7:
        ret
.LBB0_2:
        cmp     dl, 17
        jne     .LBB0_4
        cmp     cx, word ptr [rip + asd]
        je      .LBB0_7
.LBB0_4:
        cmp     dl, 17
        jne     .LBB0_6
        cmp     cx, word ptr [rip + zxc]
        je      .LBB0_7
.LBB0_6:
        cmp     dl, 17
        sete    al
        cmp     cx, word ptr [rip + zzz]
        sete    cl
        and     cl, al
        movzx   eax, cl
        ret

Compiled with -O3 to x86_64.
cmp dl, 17 is repeated for some reason. This is a bug as I think.

Metadata

Metadata

Assignees

Labels

llvm:instcombineCovers the InstCombine, InstSimplify and AggressiveInstCombine passesregression

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions