From bdfbed6de953957fd4dbba9c8cba0a3c47dde1f8 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Sat, 11 May 2024 12:45:25 +0200 Subject: [PATCH] Use BoundsCheckNoThrow assertion in optAssertionProp_RangeProperties (#102089) --- 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)) {