diff --git a/src/coreclr/jit/codegenxarch.cpp b/src/coreclr/jit/codegenxarch.cpp index 6f1ba9ab9bb8b6..daa655741c6e47 100644 --- a/src/coreclr/jit/codegenxarch.cpp +++ b/src/coreclr/jit/codegenxarch.cpp @@ -5955,29 +5955,28 @@ void CodeGen::genCompareInt(GenTree* treeNode) } else if (op1->isUsedFromReg() && op2->IsIntegralConst(0)) { - emitAttr targetSize = emitActualTypeSize(op1->TypeGet()); - emitAttr op1Size = emitActualTypeSize(op1->TypeGet()); - - // Optimize "x<0" and "x>=0" to "x>>31" if "x" is not a jump condition and in a reg. - // Morph/Lowering are responsible to rotate "00" so we won't handle it here. - if ((targetSize >= 4) && (op1Size >= 4) && (targetReg != REG_NA) && tree->OperIs(GT_LT, GT_GE)) + if (compiler->opts.OptimizationEnabled()) { - if (targetReg != op1->GetRegNum()) - { - inst_RV_RV(INS_mov, targetReg, op1->GetRegNum(), op1->TypeGet()); - } - if (tree->OperIs(GT_GE)) + emitAttr op1Size = emitActualTypeSize(op1->TypeGet()); + assert((int)op1Size >= 4); + + // Optimize "x<0" and "x>=0" to "x>>31" if "x" is not a jump condition and in a reg. + // Morph/Lowering are responsible to rotate "00" so we won't handle it here. + if ((targetReg != REG_NA) && tree->OperIs(GT_LT, GT_GE) && !tree->IsUnsigned()) { - // emit "not" for "x>=0" case - inst_RV(INS_not, targetReg, tree->TypeGet(), op1Size); + if (targetReg != op1->GetRegNum()) + { + inst_RV_RV(INS_mov, targetReg, op1->GetRegNum(), op1->TypeGet()); + } + if (tree->OperIs(GT_GE)) + { + // emit "not" for "x>=0" case + inst_RV(INS_not, targetReg, op1->TypeGet()); + } + inst_RV_IV(INS_shr_N, targetReg, (int)op1Size * 8 - 1, op1Size); + genProduceReg(tree); + return; } - inst_RV_IV(INS_shr_N, targetReg, (int)op1Size * 8 - 1, op1Size); - genProduceReg(tree); - return; - } - - if (compiler->opts.OptimizationEnabled()) - { canReuseFlags = true; } diff --git a/src/libraries/Microsoft.CSharp/tests/IntegerBinaryOperationTests.cs b/src/libraries/Microsoft.CSharp/tests/IntegerBinaryOperationTests.cs index fe21b494228a29..8888f6ce06723c 100644 --- a/src/libraries/Microsoft.CSharp/tests/IntegerBinaryOperationTests.cs +++ b/src/libraries/Microsoft.CSharp/tests/IntegerBinaryOperationTests.cs @@ -498,7 +498,6 @@ public void RuntimeExpression(object x, object y, ExpressionType type, object re [MemberData(nameof(UInt64TestNotEquals))] [MemberData(nameof(UInt64TestSubtractions))] [ActiveIssue("https://github.com/dotnet/runtime/issues/26798", TargetFrameworkMonikers.NetFramework)] - [SkipOnCoreClr("https://github.com/dotnet/runtime/issues/42719", RuntimeConfiguration.Checked)] public void ConstantExpressions(object x, object y, ExpressionType type, object result, bool shouldSucceedChecked) { var callsite = GetBinaryOperationCallSite(type, false, true, true); diff --git a/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719.il b/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719.il new file mode 100644 index 00000000000000..07f37c96b9b193 --- /dev/null +++ b/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719.il @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +.assembly extern mscorlib { } +.assembly GitHub_42719 {} +.module GitHub_42719.exe +.method public static int32 Main() cil managed +{ + .entrypoint + .maxstack 5 + // "0 u> -1" shouldn't be optimized into "-1 >> 31" + ldc.i4.s 100 + ldc.i4.0 + ldc.i4.m1 + cgt.un + add + ret +} diff --git a/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719.ilproj b/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719.ilproj new file mode 100644 index 00000000000000..ad6d87becfd14d --- /dev/null +++ b/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719.ilproj @@ -0,0 +1,9 @@ + + + Exe + False + + + + + diff --git a/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719_opt.ilproj b/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719_opt.ilproj new file mode 100644 index 00000000000000..f09b3428e3f15c --- /dev/null +++ b/src/tests/JIT/opt/JitMinOpts/Regression/GitHub_42719_opt.ilproj @@ -0,0 +1,9 @@ + + + Exe + True + + + + +