Skip to content

JIT: Use KnownBits to fold casts, overflow checks, masks and switch defaults - #138311

Draft
EgorBo wants to merge 3 commits into
dotnet:mainfrom
EgorBo:knownbits-casts-ovf
Draft

EgorBo wants to merge 3 commits into
dotnet:mainfrom
EgorBo:knownbits-casts-ovf

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Closes #110315
Closes #10634
Closes #10830
Closes #134380
Closes #9426 (partially fixed by the previous KnownBits PR)
Closes #134444 (partially fixed by the previous KnownBits PR)

Follow up to #137936. KnownBitsAnalysis was only used to fold relops; now assertion prop also uses it for:

  • Casts (optAssertionProp_Cast): remove overflow checks and redundant extensions.
  • Overflow checks (optAssertionProp_AddMulSub): clear GTF_OVERFLOW when the result provably fits.
  • AND/OR: drop redundant masks, fold fully known results to constants.
  • Switch: remove the default case when the value is known to be in range (LowerSwitch now supports switches without a default case).
  • Bounds checks (optAssertionProp_BndsChk): drop the check when index u< length is proven by known bits/bounds.

The analysis itself also got smarter:

  • x != cns assertions refine bounds when cns is one of them.
  • Signed bounds for sign-extended values (small signed types, int -> long casts).
  • Bounds flow through value-preserving casts.
  • 32-bit PHIs are followed (previously only 64-bit).
  • otherVN relop vn assertions are used in addition to vn relop otherVN.
  • ADD/SUB propagate signed/unsigned bounds when the extremes don't overflow (and the bounds are converted to known bits).
  • Array lengths are known to be in [0..Array.MaxLength].

Plus a small morph fix for #134380: narrow (long & C1) == C2 to 32-bit when the constants fit into 32 bits unsigned.

Examples

// #9426: overflow check removed
Span<long> Cast(Span<char> s) => MemoryMarshal.Cast<char, long>(s);

// #10634: the second range check (default case) is gone
int Switch(ulong v)
{
    if (v > 4) return 0;
    switch (v) { case 0: return 11; case 1: return 22; case 2: return 37; case 3: return 41; case 4: return 55; }
    return -1;
}

// #10830: no default case in the jump table
string Switch(uint x)
{
    switch (x & 7) { case 0: return "0"; case 1: return "1"; /* ... */ case 7: return "7"; default: return "?"; }
}

// #110315: "n >= 1" is true given "n != 0 && n >= 0"
int Test(int n)
{
    if (n != 0 && n >= 0)
        return n >= 1 ? 1 : 2;
    return 3;
}

// #134444: unsigned division (ticks are known to be non-negative across the PHI)
long Millis(bool c, DateTime a, DateTime b) => (c ? a.Ticks : b.Ticks) / TimeSpan.TicksPerMillisecond;

// #134380: 32-bit and/cmp
bool HasTag(long value) => (value & 0xD0000000L) == 0x90000000L;

// Overflow checks removed
nuint Mul(uint a) => checked((nuint)a * 8);
long BigMul(int a, int b) => checked((long)a * b);

// Wide multiply can't overflow: TimeSpan range checks removed (System.Runtime.Caching)
TimeSpan PollingInterval => TimeSpan.FromMilliseconds(_configPollingInterval); // int field

// Span ctor's "length >= 0" check removed after checked(len * 2) (System.Reflection.Metadata)
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(str.AsSpan());

// Redundant/constant masks
long Disjoint(bool c, long x, long y)
{
    long p = c ? x << 4 : y << 4;
    return p & 0xF; // 0
}

…efaults

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6bf496d3-1e66-480a-9422-ffbb190b207e
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 25, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@EgorBo

EgorBo commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

@MihuBot -nuget

EgorBo and others added 2 commits November 25, 2026 23:00
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6bf496d3-1e66-480a-9422-ffbb190b207e
…hecks

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6bf496d3-1e66-480a-9422-ffbb190b207e
@EgorBo

EgorBo commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

@MihuBot -nuget

EgorBo added a commit that referenced this pull request Nov 30, 2026
Follow up to #137936 (split out of #138311). `optAssertionProp_Cast` now
falls back to KnownBitsAnalysis when RangeCheck can't prove that the
cast operand is in range. That lets it drop redundant casts and clear
overflow checks.

The analysis also got more precise:
* Signed bounds for sign-extended values (small signed types, `int ->
long` casts).
* Bounds flow through value-preserving casts.
* `x != cns` assertions refine bounds when `cns` is one of them.
* `otherVN relop vn` assertions are used in addition to `vn relop
otherVN`.
* 32-bit PHIs are followed (previously only 64-bit).

## Examples

```cs
// int * 10000 always fits into long: TimeSpan range checks removed (System.Runtime.Caching)
TimeSpan PollingInterval => TimeSpan.FromMilliseconds(_configPollingInterval); // int field

// Range of r is [0..510], checked((byte)r) doesn't need an overflow check under r <= 255 (Microsoft.VisualBasic)
object AddByte(byte x, byte y)
{
    short r = (short)(x + y);
    return r > byte.MaxValue ? r : checked((byte)r);
}

// redundant zero-extension removed (System.Formats.Asn1)
Asn1Tag AsPrimitive() => new Asn1Tag((byte)(_controlFlags & ~ConstructedMask), TagValue); // byte field
```

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6bf496d3-1e66-480a-9422-ffbb190b207e

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

1 participant