llvm / llvm/llvm-project

GlobalOptPass turns global that only takes two values into a bool even if the values can be represented by a byte on the target

Open
#199,496 2 comments 0 reactions 1 assignee Claimed by @AnkitDubeycs25 View on GitHub
clang:codegen missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The following program:

```c
static int v;

void maybe_called_from_another_tu() {
v = 123;
}

int main() {
return v;
}
```

gets compiled into:

```asm
maybe_called_from_another_tu:
mov byte ptr [rip + v], 1
ret

main:
xor ecx, ecx
cmp byte ptr [rip + v], 0
mov eax, 123
cmove eax, ecx
ret
```

even though it could've been:

```asm
maybe_called_from_another_tu:
mov byte ptr [rip + v], 123
ret

main:
movzx eax, byte ptr [rip + v]
ret
```

This optimisation would and does make more sense if the value written was greater than 255 but the i32 turning into an i1 doesn't make sense to me when it ends up being stored as an i8 that could've stored the actual value in this case.

godbolt: https://godbolt.org/z/EsoabcGWP

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.