dotnet / dotnet/runtime

JIT: Move code into branch if it's side-effect free and not used elsewhere

Open
#126,073 2 comments 0 reactions 0 assignees View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

I expected the entire computation leading up to `mask` to be moved inside the branch. Currently it's doing all these computations unconditionally.
https://godbolt.org/z/hbadsraxd
```cs
void BitArrayIndex(int index, bool value, uint[] data)
{
const int BITS_PER_ELEMENT = sizeof(uint) * 8;

uint arrIndex = (uint)index / BITS_PER_ELEMENT;
uint bitIndex = (uint)index % BITS_PER_ELEMENT;
uint mask = 1u << (int)bitIndex;

if (value)
{
data[arrIndex] |= mask;
}
}
```
```asm
G_M000_IG01: ;; offset=0x0000
sub rsp, 40

G_M000_IG02: ;; offset=0x0004
mov eax, edx
shr eax, 5
mov ecx, 1
shlx ecx, ecx, edx
test r8b, r8b
je SHORT G_M000_IG04

G_M000_IG03: ;; offset=0x0018
cmp eax, dword ptr [r9+0x08]
jae SHORT G_M000_IG05
mov eax, eax
lea rax, bword ptr [r9+4*rax+0x10]
or dword ptr [rax], ecx

G_M000_IG04: ;; offset=0x0027
add rsp, 40
ret

G_M000_IG05: ;; offset=0x002C
call CORINFO_HELP_RNGCHKFAIL
int3
```

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.