Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4599,7 +4599,30 @@ GenTree* Lowering::OptimizeConstCompare(GenTree* cmp)
BlockRange().Remove(op2);

GenCondition cmpCondition = GenCondition::FromRelop(cmp);
GenTreeCC* setcc = m_compiler->gtNewCC(GT_SETCC, cmp->TypeGet(), cmpCondition);

// For unsigned compares against zero that rely on flags-as-compare-to-zero,
// we cannot use UGT/UGE/ULT/ULE directly because op1 may set only NZ flags
// (e.g. ANDS on ARM64 clears C/V). UGT/ULT depend on carry, which would be wrong.
// Rewrite the condition to use Z/N where possible, or bail out.
if (cmp->IsUnsigned() && op2->IsIntegralConst(0) && cmp->OperIs(GT_GT, GT_GE, GT_LT, GT_LE))
{
if (cmp->OperIs(GT_GT))
{
// x > 0U <=> x != 0
cmpCondition = GenCondition::NE;
}
else if (cmp->OperIs(GT_LE))
{
// x <= 0U <=> x == 0
cmpCondition = GenCondition::EQ;
}
else
{
// x >= 0U is always true and x < 0U is always false; keep the compare for correctness.
return cmp;
}
}
GenTreeCC* setcc = m_compiler->gtNewCC(GT_SETCC, cmp->TypeGet(), cmpCondition);
BlockRange().InsertAfter(op1, setcc);

use.ReplaceWith(setcc);
Expand Down
51 changes: 51 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Generated by Fuzzlyn v3.3 on 2026-02-15 16:52:57
// Run on Arm64 Linux
// Seed: 14599529654677333173-vectort,vector64,vector128,armadvsimd,armadvsimdarm64,armaes,armarmbase,armarmbasearm64,armcrc32,armcrc32arm64,armdp,armrdm,armrdmarm64,armsha1,armsha256,armsve,armsve2
// Reduced from 73.3 KiB to 0.7 KiB in 00:00:52
// Debug: Outputs 0
// Release: Outputs 1
public class C0
{
public ulong F3;
public C0(ulong f3)
{
F3 = f3;
}
}

public class C1
{
public byte F8;
}

public class C2
{
public C0 F1;
public C1 F2;
public C2(C0 f1, C1 f2)
{
F1 = f1;
F2 = f2;
}
}

public struct S0
{
public byte F0;
}

public class Program

Check failure on line 37 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build coreclr Common Pri0 Test Build AnyOS AnyCPU checked)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L37

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(37,14): error CS0260: Missing partial modifier on declaration of type 'Program'; another partial declaration of this type exists [/__w/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 37 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L37

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(37,14): error CS0260: Missing partial modifier on declaration of type 'Program'; another partial declaration of this type exists [/__w/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 37 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Release AllSubsets_Mono_LLVMAot_RuntimeTests llvmaot)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L37

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(37,14): error CS0260: Missing partial modifier on declaration of type 'Program'; another partial declaration of this type exists [/__w/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 37 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L37

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(37,14): error CS0260: Missing partial modifier on declaration of type 'Program'; another partial declaration of this type exists [/Users/runner/work/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 37 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Interpreter_RuntimeTests monointerpreter)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L37

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(37,14): error CS0260: Missing partial modifier on declaration of type 'Program'; another partial declaration of this type exists [/Users/runner/work/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]
{
public static S0 s_1;
public static C2 s_2 = new C2(new C0(8013948595597981922UL), new C1());
public static void Main()

Check failure on line 41 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build coreclr Common Pri0 Test Build AnyOS AnyCPU checked)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L41

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(41,24): error XUW1001: Projects in merged tests group should not have entry points. Convert to Facts or Theories. [/__w/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 41 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L41

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(41,24): error XUW1001: Projects in merged tests group should not have entry points. Convert to Facts or Theories. [/__w/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 41 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Release AllSubsets_Mono_LLVMAot_RuntimeTests llvmaot)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L41

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(41,24): error XUW1001: Projects in merged tests group should not have entry points. Convert to Facts or Theories. [/__w/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 41 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L41

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(41,24): error XUW1001: Projects in merged tests group should not have entry points. Convert to Facts or Theories. [/Users/runner/work/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]

Check failure on line 41 in src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Interpreter_RuntimeTests monointerpreter)

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs#L41

src/tests/JIT/Regression/JitBlue/Runtime_124510/Runtime_124510.cs(41,24): error XUW1001: Projects in merged tests group should not have entry points. Convert to Facts or Theories. [/Users/runner/work/1/s/src/tests/JIT/Regression/Regression_ro_1.csproj]
{
var vr1 = s_2.F1.F3;
if (((uint)(vr1 & 3080599622U) <= (ushort)(s_2.F2.F8 % 1)))
{
byte vr3 = s_1.F0++;
}

System.Console.WriteLine(s_1.F0);
}
}
1 change: 1 addition & 0 deletions src/tests/JIT/Regression/Regression_ro_1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="JitBlue\Runtime_121711\Runtime_121711.cs" />
<Compile Include="JitBlue\Runtime_122138\Runtime_122138.cs" />
<Compile Include="JitBlue\Runtime_123399\Runtime_123399.cs" />
<Compile Include="JitBlue\Runtime_124510\Runtime_124510.cs" />
<Compile Include="JitBlue\Runtime_31615\Runtime_31615.cs" />
<Compile Include="JitBlue\Runtime_33884\Runtime_33884.cs" />
<Compile Include="JitBlue\Runtime_38920\Runtime_38920.cs" />
Expand Down
51 changes: 47 additions & 4 deletions src/tests/JIT/opt/InstructionCombining/And.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ public static int CheckAnd()
fail = true;
}

if (AndsUnsignedStaticBranchLe() != 0)
{
fail = true;
}

if (AndsUnsignedStaticBranchGt() == 0)
{
fail = true;
}

if (fail)
{
return 101;
Expand Down Expand Up @@ -288,29 +298,62 @@ static bool AndsBinOpSingleLine(uint a, uint b, uint c, uint d)
[MethodImpl(MethodImplOptions.NoInlining)]
static bool AndsGreaterThan(int a, int b)
{
//ARM64-FULL-LINE: ands w0, {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: ands {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
return (a & b) > 0;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool AndsGreaterThanEq(int a, int b)
{
//ARM64-FULL-LINE: ands w0, {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: ands {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
return (a & b) >= 0;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool AndsLessThan(int a, int b)
{
//ARM64-FULL-LINE: ands w0, {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: ands {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
return (a & b) < 0;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool AndsLessThanEq(int a, int b)
{
//ARM64-FULL-LINE: ands w0, {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: ands {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
return (a & b) <= 0;
}

static ulong s_a = 8013948595597981922UL;
static byte s_b = 0;

[MethodImpl(MethodImplOptions.NoInlining)]
static int AndsUnsignedStaticBranchLe()
{
//ARM64-FULL-LINE: ands {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: cset {{x[0-9]+}}, eq
uint left = (uint)(s_a & 0xB79E3846u);
uint right = (ushort)(s_b % 1);

if (left <= right)
{
return 1;
}
return 0;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static int AndsUnsignedStaticBranchGt()
{
//ARM64-FULL-LINE: tst {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: cset {{x[0-9]+}}, ne
uint left = (uint)(s_a & 0xB79E3846u);
uint right = (ushort)(s_b % 1);

if (left > right)
{
return 1;
}
return 0;
}
}
}
43 changes: 43 additions & 0 deletions src/tests/JIT/opt/InstructionCombining/Bic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ public static int CheckBic()
fail = true;
}

if (BicsUnsignedStaticBranchLe() != 0)
{
fail = true;
}

if (BicsUnsignedStaticBranchGt() == 0)
{
fail = true;
}

if (fail)
{
return 101;
Expand Down Expand Up @@ -316,5 +326,38 @@ static bool BicsLessThanEq(int a, int b)
//ARM64-FULL-LINE: bics {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
return (a & ~b) <= 0;
}

static uint s_a = 0xFFFFFFFFu;
static uint s_b = 0x12345678u;

[MethodImpl(MethodImplOptions.NoInlining)]
static int BicsUnsignedStaticBranchLe()
{
//ARM64-FULL-LINE: bics {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: cset {{x[0-9]+}}, eq
uint left = s_a & ~s_b;
uint right = (ushort)(s_b % 1);

if (left <= right)
{
return 1;
}
return 0;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static int BicsUnsignedStaticBranchGt()
{
//ARM64-FULL-LINE: bics {{w[0-9]+}}, {{w[0-9]+}}, {{w[0-9]+}}
//ARM64-FULL-LINE: cset {{x[0-9]+}}, ne
uint left = s_a & ~s_b;
uint right = (ushort)(s_b % 1);

if (left > right)
{
return 1;
}
return 0;
}
}
}
Loading