Skip to content

Commit c46c5b8

Browse files
authored
JIT: ARR_LENGTH(new T[CNS]) --> CNS (#85496)
1 parent 2076afe commit c46c5b8

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/coreclr/jit/rangecheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ RangeCheck::OverflowMap* RangeCheck::GetOverflowMap()
7070
int RangeCheck::GetArrLength(ValueNum vn)
7171
{
7272
ValueNum arrRefVN = m_pCompiler->vnStore->GetArrForLenVn(vn);
73-
return m_pCompiler->vnStore->GetNewArrSize(arrRefVN);
73+
int size;
74+
return m_pCompiler->vnStore->TryGetNewArrSize(arrRefVN, &size) ? size : 0;
7475
}
7576

7677
//------------------------------------------------------------------------

src/coreclr/jit/valuenum.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,14 @@ ValueNum ValueNumStore::VNForFunc(var_types typ, VNFunc func, ValueNum arg0VN)
24332433
}
24342434
}
24352435
}
2436+
2437+
// Case 3: ARR_LENGTH(new T[cns])
2438+
// TODO: Add support for MD arrays
2439+
int knownSize;
2440+
if ((resultVN == NoVN) && TryGetNewArrSize(addressVN, &knownSize))
2441+
{
2442+
resultVN = VNForIntCon(knownSize);
2443+
}
24362444
}
24372445

24382446
// Try to perform constant-folding.
@@ -6491,18 +6499,24 @@ bool ValueNumStore::IsVNNewArr(ValueNum vn, VNFuncApp* funcApp)
64916499

64926500
// TODO-MDArray: support array dimension length of a specific dimension for JitNewMdArr, with a GetNewMDArrSize()
64936501
// function.
6494-
int ValueNumStore::GetNewArrSize(ValueNum vn)
6502+
bool ValueNumStore::TryGetNewArrSize(ValueNum vn, int* size)
64956503
{
64966504
VNFuncApp funcApp;
64976505
if (IsVNNewArr(vn, &funcApp))
64986506
{
64996507
ValueNum arg1VN = funcApp.m_args[1];
6500-
if (IsVNConstant(arg1VN) && TypeOfVN(arg1VN) == TYP_INT)
6508+
if (IsVNConstant(arg1VN))
65016509
{
6502-
return ConstantValue<int>(arg1VN);
6510+
ssize_t val = CoercedConstantValue<ssize_t>(arg1VN);
6511+
if ((size_t)val <= INT_MAX)
6512+
{
6513+
*size = (int)val;
6514+
return true;
6515+
}
65036516
}
65046517
}
6505-
return 0;
6518+
*size = 0;
6519+
return false;
65066520
}
65076521

65086522
bool ValueNumStore::IsVNArrLen(ValueNum vn)

src/coreclr/jit/valuenum.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,8 @@ class ValueNumStore
946946
// Check if "vn" is "new [] (type handle, size)"
947947
bool IsVNNewArr(ValueNum vn, VNFuncApp* funcApp);
948948

949-
// Check if "vn" IsVNNewArr and return <= 0 if arr size cannot be determined, else array size.
950-
int GetNewArrSize(ValueNum vn);
949+
// Check if "vn" IsVNNewArr and return false if arr size cannot be determined.
950+
bool TryGetNewArrSize(ValueNum vn, int* size);
951951

952952
// Check if "vn" is "a.Length" or "a.GetLength(n)"
953953
bool IsVNArrLen(ValueNum vn);

0 commit comments

Comments
 (0)