Skip to content

Unnecessarily large constant created from reordering add and shift #123239

@dzaima

Description

@dzaima

https://godbolt.org/z/xoKf6bnTb

The code:

#include<stdint.h>
#include<stdbool.h>
bool foo(uint64_t x) {
  uint16_t tag = x>>48;
  return tag>=0b1111111111110010 && tag<=0b1111111111110100;
}

with -O3 as of clang 19 (and still in trunk) compiles to:

foo:
        movabs  rax, 3940649673949184
        add     rax, rdi
        shr     rax, 48
        cmp     eax, 3
        setb    al
        ret

whereas 18.0 did this, which is strictly better (i.e. is the exact same set of instructions, just in a different order and without movabs):

foo:
        shr     rdi, 48
        add     edi, -65522
        cmp     edi, 3
        setb    al
        ret

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions