Skip to content

JIT should optimize {array | span}.Length division by constant that is power of 2 #59922

@gfoidl

Description

@gfoidl

E.g.

Span<byte> span = ...
nint n = span.Length / 8;    // or any constant that is a power of two

produces integer division by consant
(Also tested with .NET 6 RC 1, and didn't find an issue or PR that addressed this.)

JIT should be able to recnogize that .Length isn't negative, so produce code that is equivalent to

nint n = (nint)(uint)span.Length / 8;

which produces (on x86)

shr rax, 3

This pattern is quite common, so it's tedious and a bit unreadable to have the "cast-hacks".

category:cq
theme:expression-opts
skill-level:intermediate
cost:small
impact:small

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions