Skip to content

Commit dc2cb16

Browse files
committed
fix bug, clean up
1 parent b4ae949 commit dc2cb16

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

src/coreclr/jit/rangecheck.cpp

+34-34
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,10 @@ bool RangeCheck::TryGetRangeFromAssertions(Compiler* comp, ValueNum num, ASSERT_
651651
// assertions - the assertions to use
652652
// pRange - the range to tighten with assertions
653653
//
654-
void RangeCheck::MergeEdgeAssertions(Compiler* comp,
655-
ValueNum normalLclVN,
656-
ValueNum preferredBoundVN,
657-
ASSERT_VALARG_TP assertions,
658-
Range* pRange,
659-
bool log)
654+
void RangeCheck::MergeEdgeAssertions(
655+
Compiler* comp, ValueNum normalLclVN, ValueNum preferredBoundVN, ASSERT_VALARG_TP assertions, Range* pRange)
660656
{
661-
Range range = Range(Limit(Limit::keDependent));
657+
Range assertedRange = Range(Limit(Limit::keUnknown));
662658
if (BitVecOps::IsEmpty(comp->apTraits, assertions))
663659
{
664660
return;
@@ -903,10 +899,10 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp,
903899
{
904900
case GT_LT:
905901
case GT_LE:
906-
range.uLimit = limit;
902+
assertedRange.uLimit = limit;
907903
if (isUnsigned)
908904
{
909-
range.lLimit = Limit(Limit::keConstant, 0);
905+
assertedRange.lLimit = Limit(Limit::keConstant, 0);
910906
}
911907
break;
912908

@@ -916,13 +912,13 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp,
916912
// using single Range object.
917913
if (!isUnsigned)
918914
{
919-
range.lLimit = limit;
915+
assertedRange.lLimit = limit;
920916
}
921917
break;
922918

923919
case GT_EQ:
924-
range.uLimit = limit;
925-
range.lLimit = limit;
920+
assertedRange.uLimit = limit;
921+
assertedRange.lLimit = limit;
926922
break;
927923

928924
default:
@@ -931,31 +927,37 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp,
931927
}
932928

933929
// We have two ranges - we need to merge (tighten) them.
930+
934931
auto tightenLimit = [](Limit l1, Limit l2, ValueNum preferredBound, bool isLower) -> Limit {
935-
// 1) if one the limits is completely unknown, use the other one.
936-
if (l1.IsUndef() || l1.IsUnknown() || l2.IsUndef() || l2.IsUnknown())
932+
// 1) One of the limits is undef, unknown or dependent
933+
if (l1.IsUndef() || l2.IsUndef())
937934
{
938-
return (l1.IsUndef() || l1.IsUnknown()) ? l2 : l1;
935+
// Anything is better than undef.
936+
return l1.IsUndef() ? l2 : l1;
937+
}
938+
if (l1.IsUnknown() || l2.IsUnknown())
939+
{
940+
// Anything is better than unknown.
941+
return l1.IsUnknown() ? l2 : l1;
939942
}
940-
941-
// 2) If one limit is dependent and the other is not, use the non-dependent one.
942943
if (l1.IsDependent() || l2.IsDependent())
943944
{
945+
// Anything is better than dependent.
944946
return l1.IsDependent() ? l2 : l1;
945947
}
946948

947-
// 3) Both limits are constant
949+
// 2) Both limits are constants
948950
if (l1.IsConstant() && l2.IsConstant())
949951
{
950952
// isLower: whatever is higher is better.
951953
// !isLower: whatever is lower is better.
952954
return isLower ? (l1.cns > l2.cns ? l1 : l2) : (l1.cns < l2.cns ? l1 : l2);
953955
}
954956

955-
// 4) Both limits are of the form "len + cns"
957+
// 3) Both limits are BinOpArray (which is "arrLen + cns")
956958
if (l1.IsBinOpArray() && l2.IsBinOpArray())
957959
{
958-
// First, if one of them is preferredBound and the other is not, use the preferredBound.
960+
// If one of them is preferredBound and the other is not, use the preferredBound.
959961
if (preferredBound != ValueNumStore::NoVN)
960962
{
961963
if ((l1.vn == preferredBound) && (l2.vn != preferredBound))
@@ -969,22 +971,23 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp,
969971
}
970972

971973
// Otherwise, just use the one with the higher/lower constant.
972-
// even if they use different arrLen - what else can we do?
974+
// even if they use different arrLen.
973975
return isLower ? (l1.cns > l2.cns ? l1 : l2) : (l1.cns < l2.cns ? l1 : l2);
974976
}
975977

976-
// 5) One limit is constant and the other is of the form "len + cns"
978+
// 4) One of the limits is a constant and the other is BinOpArray
977979
if ((l1.IsConstant() && l2.IsBinOpArray()) || (l2.IsConstant() && l1.IsBinOpArray()))
978980
{
979-
// swap them so that l1 is BinOpArray and l2 is constant.
981+
// l1 - BinOpArray, l2 - constant
980982
if (l1.IsConstant())
981983
{
982984
std::swap(l1, l2);
983985
}
984986

985987
if (((preferredBound == ValueNumStore::NoVN) || (l1.vn != preferredBound)))
986988
{
987-
// if we don't have a preferred bound or it doesn't match l1.vn, use l2.
989+
// if we don't have a preferred bound,
990+
// or it doesn't match l1.vn, use the constant (l2).
988991
return l2;
989992
}
990993

@@ -994,16 +997,13 @@ void RangeCheck::MergeEdgeAssertions(Compiler* comp,
994997
unreached();
995998
};
996999

997-
if (log)
998-
{
999-
JITDUMP("Tightening ranges [%s] and [%s] into ", range.ToString(comp), pRange->ToString(comp));
1000-
}
1001-
pRange->lLimit = tightenLimit(range.lLimit, pRange->lLimit, preferredBoundVN, true);
1002-
pRange->uLimit = tightenLimit(range.uLimit, pRange->uLimit, preferredBoundVN, false);
1003-
if (log)
1004-
{
1005-
JITDUMP("[%s]\n", pRange->ToString(comp));
1006-
}
1000+
JITDUMP("Tightening pRange: [%s] with assertedRange: [%s] into ", pRange->ToString(comp),
1001+
assertedRange.ToString(comp));
1002+
1003+
pRange->lLimit = tightenLimit(assertedRange.lLimit, pRange->lLimit, preferredBoundVN, true);
1004+
pRange->uLimit = tightenLimit(assertedRange.uLimit, pRange->uLimit, preferredBoundVN, false);
1005+
1006+
JITDUMP("[%s]\n", pRange->ToString(comp));
10071007
}
10081008
}
10091009

src/coreclr/jit/rangecheck.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -728,12 +728,8 @@ class RangeCheck
728728
void MergeEdgeAssertions(GenTreeLclVarCommon* lcl, ASSERT_VALARG_TP assertions, Range* pRange);
729729

730730
// Inspect the assertions about the current ValueNum to refine pRange
731-
static void MergeEdgeAssertions(Compiler* comp,
732-
ValueNum num,
733-
ValueNum preferredBoundVN,
734-
ASSERT_VALARG_TP assertions,
735-
Range* pRange,
736-
bool log = true);
731+
static void MergeEdgeAssertions(
732+
Compiler* comp, ValueNum num, ValueNum preferredBoundVN, ASSERT_VALARG_TP assertions, Range* pRange);
737733

738734
// The maximum possible value of the given "limit". If such a value could not be determined
739735
// return "false". For example: CORINFO_Array_MaxLength for array length.

0 commit comments

Comments
 (0)