From 7bc1ba349b6909e037fab14460318abb74130df4 Mon Sep 17 00:00:00 2001 From: EgorBo Date: Wed, 10 Apr 2024 02:38:18 +0200 Subject: [PATCH 1/5] Add ArrOpLHS to CompareCheckedBoundArithInfo --- src/coreclr/jit/optcse.cpp | 8 +++++-- src/coreclr/jit/valuenum.h | 1 + .../JitBlue/Runtime_100809/Runtime_100809.cs | 23 +++++++++++++++++++ .../Runtime_100809/Runtime_100809.csproj | 5 ++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.cs create mode 100644 src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.csproj diff --git a/src/coreclr/jit/optcse.cpp b/src/coreclr/jit/optcse.cpp index 41b15792e24ae1..7fcaac35c43b45 100644 --- a/src/coreclr/jit/optcse.cpp +++ b/src/coreclr/jit/optcse.cpp @@ -4813,8 +4813,12 @@ void CSE_HeuristicCommon::PerformCSE(CSE_Candidate* successfulCandidate) assert(vnStore->IsVNCompareCheckedBoundArith(oldCmpVN)); vnStore->GetCompareCheckedBoundArithInfo(oldCmpVN, &info); - newCmpArgVN = vnStore->VNForFunc(vnStore->TypeOfVN(info.arrOp), (VNFunc)info.arrOper, - info.arrOp, theConservativeVN); + + ValueNum arrOp1 = info.arrOpLHS ? info.arrOp : theConservativeVN; + ValueNum arrOp2 = info.arrOpLHS ? theConservativeVN : info.arrOp; + + newCmpArgVN = + vnStore->VNForFunc(vnStore->TypeOfVN(info.arrOp), (VNFunc)info.arrOper, arrOp1, arrOp2); } ValueNum newCmpVN = vnStore->VNForFunc(vnStore->TypeOfVN(oldCmpVN), (VNFunc)info.cmpOper, info.cmpOp, newCmpArgVN); diff --git a/src/coreclr/jit/valuenum.h b/src/coreclr/jit/valuenum.h index 1f9171e13cef21..282ca39a85681c 100644 --- a/src/coreclr/jit/valuenum.h +++ b/src/coreclr/jit/valuenum.h @@ -932,6 +932,7 @@ class ValueNumStore ValueNum vnBound; unsigned arrOper; ValueNum arrOp; + bool arrOpLHS; // arrOp is on the left side of cmpOp expression unsigned cmpOper; ValueNum cmpOp; CompareCheckedBoundArithInfo() diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.cs b/src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.cs new file mode 100644 index 00000000000000..eba4e112918c30 --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.cs @@ -0,0 +1,23 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.CompilerServices; +using Xunit; + +public static class Runtime_100809 +{ + [Fact] + public static int TestEntryPoint() + { + return AlwaysFalse(96) ? -1 : 100; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool AlwaysFalse(int x) + { + var result = new byte[x]; + int count = result.Length - 2; + return (x < 0 || result.Length - count < 0); + } +} diff --git a/src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.csproj b/src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.csproj new file mode 100644 index 00000000000000..6c8c63b83414ad --- /dev/null +++ b/src/tests/JIT/Regression/JitBlue/Runtime_100809/Runtime_100809.csproj @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From f402038415ff06c265aef59c312f591169506614 Mon Sep 17 00:00:00 2001 From: EgorBo Date: Wed, 10 Apr 2024 02:43:16 +0200 Subject: [PATCH 2/5] fix GetCheckedBoundArithInfo --- src/coreclr/jit/valuenum.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/valuenum.cpp b/src/coreclr/jit/valuenum.cpp index 7a6136577ddba3..6b6b4ea33e6a44 100644 --- a/src/coreclr/jit/valuenum.cpp +++ b/src/coreclr/jit/valuenum.cpp @@ -6675,9 +6675,10 @@ void ValueNumStore::GetCheckedBoundArithInfo(ValueNum vn, CompareCheckedBoundAri } else { - info->arrOper = funcArith.m_func; - info->arrOp = funcArith.m_args[1]; - info->vnBound = funcArith.m_args[0]; + info->arrOper = funcArith.m_func; + info->arrOp = funcArith.m_args[1]; + info->vnBound = funcArith.m_args[0]; + info->arrOpLHS = false; } } From 288ddbb302082f4cfe25004ccd4f50276a4ab209 Mon Sep 17 00:00:00 2001 From: EgorBo Date: Wed, 10 Apr 2024 02:44:48 +0200 Subject: [PATCH 3/5] fix GetCheckedBoundArithInfo --- src/coreclr/jit/valuenum.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/valuenum.cpp b/src/coreclr/jit/valuenum.cpp index 6b6b4ea33e6a44..65239b356e461f 100644 --- a/src/coreclr/jit/valuenum.cpp +++ b/src/coreclr/jit/valuenum.cpp @@ -6669,9 +6669,10 @@ void ValueNumStore::GetCheckedBoundArithInfo(ValueNum vn, CompareCheckedBoundAri bool isOp1CheckedBound = IsVNCheckedBound(funcArith.m_args[1]); if (isOp1CheckedBound) { - info->arrOper = funcArith.m_func; - info->arrOp = funcArith.m_args[0]; - info->vnBound = funcArith.m_args[1]; + info->arrOper = funcArith.m_func; + info->arrOp = funcArith.m_args[0]; + info->vnBound = funcArith.m_args[1]; + info->arrOpLHS = true; } else { From 12a0fd0662533cfa36d9dedea3f46c1b090f4051 Mon Sep 17 00:00:00 2001 From: EgorBo Date: Wed, 10 Apr 2024 02:50:01 +0200 Subject: [PATCH 4/5] forgot to init it --- src/coreclr/jit/valuenum.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/coreclr/jit/valuenum.h b/src/coreclr/jit/valuenum.h index 282ca39a85681c..464d06b1a87dd7 100644 --- a/src/coreclr/jit/valuenum.h +++ b/src/coreclr/jit/valuenum.h @@ -941,6 +941,7 @@ class ValueNumStore , arrOp(NoVN) , cmpOper(GT_NONE) , cmpOp(NoVN) + , arrOpLHS(false) { } #ifdef DEBUG From cdf4fd788b676402d7e789b616cd94314afaaffa Mon Sep 17 00:00:00 2001 From: EgorBo Date: Wed, 10 Apr 2024 02:59:33 +0200 Subject: [PATCH 5/5] fix build --- src/coreclr/jit/valuenum.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/jit/valuenum.h b/src/coreclr/jit/valuenum.h index 464d06b1a87dd7..799d36472b00c9 100644 --- a/src/coreclr/jit/valuenum.h +++ b/src/coreclr/jit/valuenum.h @@ -939,9 +939,9 @@ class ValueNumStore : vnBound(NoVN) , arrOper(GT_NONE) , arrOp(NoVN) + , arrOpLHS(false) , cmpOper(GT_NONE) , cmpOp(NoVN) - , arrOpLHS(false) { } #ifdef DEBUG