llvm / llvm/llvm-project

[X86] Missing use of 8-/16-bit `cmp` for comparing truncated values

Open
#196,827 4 comments 0 reactions 1 assignee Claimed by @he-weiwen View on GitHub
backend:X86 missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Code like:

```c
#include
void ext(void);
void gt_u8(long a, long b) {
if ((uint8_t)a > (uint8_t)b) ext();
}
```

compiled with `-O3`, compiles to:

```asm
gt_u8:
movzx eax, dil
movzx ecx, sil
cmp eax, ecx
ja ext@PLT
ret
```

but could compile to:

```asm
gt_u8:
cmp dil, sil
ja ext@PLT
ret
```

This applies to 8-bit and 16-bit types, and both `>` and `>=`.

Also happens with one arg being an immediate (though for the 16-bit case that may be actually undesirable due to slow data16).

https://c.godbolt.org/z/9hKc6shfc

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.