From 38e76ba030cc11e0a3a68e39c19c0ec6de624a09 Mon Sep 17 00:00:00 2001 From: EgorBo Date: Fri, 10 May 2024 19:28:45 +0200 Subject: [PATCH] Use BoundsCheckNoThrow assertion in optAssertionProp_RangeProperties --- src/coreclr/jit/assertionprop.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/coreclr/jit/assertionprop.cpp b/src/coreclr/jit/assertionprop.cpp index 6e30d4874e447..7dd36e9b04e52 100644 --- a/src/coreclr/jit/assertionprop.cpp +++ b/src/coreclr/jit/assertionprop.cpp @@ -3930,6 +3930,18 @@ void Compiler::optAssertionProp_RangeProperties(ASSERT_VALARG_TP assertions, { AssertionDsc* curAssertion = optGetAssertion(GetAssertionIndex(index)); + // if treeVN has a bound-check assertion where it's an index, then + // it means it's not negative, example: + // + // array[idx] = 42; // creates 'BoundsCheckNoThrow' assertion + // return idx % 8; // idx is known to be never negative here, hence, MOD->UMOD + // + if (curAssertion->IsBoundsCheckNoThrow() && (curAssertion->op1.bnd.vnIdx == treeVN)) + { + *isKnownNonNegative = true; + continue; + } + // First, analyze possible X ==/!= CNS assertions. if (curAssertion->IsConstantInt32Assertion() && (curAssertion->op1.vn == treeVN)) {