llvm / llvm/llvm-project

Missed X86 optimization of or to use lea when x < CST and TYPE is u16

Open
#218,235 2 comments 0 reactions 0 assignees View on GitHub
backend:X86 missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/1K7q748PP

```c
#define TYPE unsigned short
#define N 4
#define CST ((TYPE)1 << N)

#ifdef __clang__
TYPE
src (TYPE x)
{
if (x >= CST)
__builtin_unreachable ();
return x | CST;
}
#else
TYPE
src (TYPE x)
{
if (x >= CST)
__builtin_unreachable ();
return x + CST;
}
#endif
```

Clang with -O2 should produce assembly:

```asm
"src":
lea eax, [rdi+16]
ret
```

Contributor guide

Open the contributing guide

Research direction

Start with the C reproducer in the issue and compile it with Clang at -O2, using the linked Godbolt example to inspect the x86 assembly. Trace the optimization path for the unsigned-short `x | CST` or `x + CST` case and compare it with the expected `lea eax, [rdi+16]`; done means Clang emits that form for the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.