dotnet / dotnet/runtime

JIT: Redundant bounds check

Open
#123,312 1 comment 1 reaction 1 assignee Claimed by @EgorBo View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

```csharp
public static byte N0(Span a)
{
if (a.Length != 0)
return a[a.Length / 2];

return 0;
}

public static byte N1(Span a)
{
if (a.Length != 0)
return a[a.Length >>> 1];

return 0;
}
```

Current codegen: [godbolt.org](https://godbolt.org/z/Ex1WqbddK)
```asm
Program:N0(System.Span`1[byte]):byte (FullOpts):
push rbp
mov rbp, rsp
test esi, esi
jne SHORT G_M16481_IG05
xor eax, eax
pop rbp
ret
G_M16481_IG05: ;; offset=0x000C
mov eax, esi
shr eax, 1
cmp eax, esi
jae SHORT G_M16481_IG07
movzx rax, byte ptr [rdi+rax]
pop rbp
ret
G_M16481_IG07: ;; offset=0x001A
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.