Skip to content

Commit 9443dc8

Browse files
committed
Switch to jit-only change
1 parent ee390ff commit 9443dc8

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/coreclr/jit/gentree.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5793,6 +5793,12 @@ GenTree* Compiler::gtNewStringLiteralNode(InfoAccessType iat, void* pValue)
57935793
//
57945794
GenTreeIntCon* Compiler::gtNewStringLiteralLength(GenTreeStrCon* node)
57955795
{
5796+
if (node->IsStringEmptyField())
5797+
{
5798+
JITDUMP("Folded String.Empty.Length to 0");
5799+
return gtNewIconNode(0);
5800+
}
5801+
57965802
int length = -1;
57975803
const char16_t* str = info.compCompHnd->getStringLiteral(node->gtScpHnd, node->gtSconCPX, &length);
57985804
if (length >= 0)
@@ -5802,11 +5808,11 @@ GenTreeIntCon* Compiler::gtNewStringLiteralLength(GenTreeStrCon* node)
58025808
// str can be NULL for dynamic context
58035809
if (str != nullptr)
58045810
{
5805-
JITDUMP("String '\"%ws\".Length' is '%d'\n", str, length)
5811+
JITDUMP("Folded '\"%ws\".Length' to '%d'\n", str, length)
58065812
}
58075813
else
58085814
{
5809-
JITDUMP("String 'CNS_STR.Length' is '%d'\n", length)
5815+
JITDUMP("Folded 'CNS_STR.Length' to '%d'\n", length)
58105816
}
58115817
return iconNode;
58125818
}

src/coreclr/jit/gentree.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,6 +3245,12 @@ struct GenTreeStrCon : public GenTree
32453245
unsigned gtSconCPX;
32463246
CORINFO_MODULE_HANDLE gtScpHnd;
32473247

3248+
// Returns true if this GT_CNS_STR was imported for String.Empty field
3249+
bool IsStringEmptyField()
3250+
{
3251+
return gtSconCPX == 0;
3252+
}
3253+
32483254
// Because this node can come from an inlined method we need to
32493255
// have the scope handle, since it will become a helper call.
32503256
GenTreeStrCon(unsigned sconCPX, CORINFO_MODULE_HANDLE mod DEBUGARG(bool largeNode = false))

src/coreclr/jit/importer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15115,9 +15115,8 @@ void Compiler::impImportBlockCode(BasicBlock* block)
1511515115
{
1511615116
assert(aflags & CORINFO_ACCESS_GET);
1511715117

15118-
LPVOID pValue;
15119-
InfoAccessType iat = info.compCompHnd->emptyStringLiteral(&pValue);
15120-
op1 = gtNewStringLiteralNode(iat, pValue);
15118+
// Import String.Empty as "" (GT_CNS_STR with a fake SconCPX = 0)
15119+
op1 = gtNewSconNode(0, nullptr);
1512115120
goto FIELD_DONE;
1512215121
}
1512315122
break;

src/coreclr/jit/morph.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,7 +5184,9 @@ GenTree* Compiler::fgMorphArrayIndex(GenTree* tree)
51845184
noway_assert(elemTyp != TYP_STRUCT || elemStructType != nullptr);
51855185

51865186
// Fold "cns_str"[cns_index] to ushort constant
5187-
if (opts.OptimizationEnabled() && asIndex->Arr()->OperIs(GT_CNS_STR) && asIndex->Index()->IsIntCnsFitsInI32())
5187+
// NOTE: don't do it for empty string, the operation will fail anyway
5188+
if (opts.OptimizationEnabled() && asIndex->Arr()->OperIs(GT_CNS_STR) &&
5189+
!asIndex->Arr()->AsStrCon()->IsStringEmptyField() && asIndex->Index()->IsIntCnsFitsInI32())
51885190
{
51895191
const int cnsIndex = static_cast<int>(asIndex->Index()->AsIntConCommon()->IconValue());
51905192
if (cnsIndex >= 0)
@@ -9422,11 +9424,18 @@ GenTree* Compiler::fgMorphConst(GenTree* tree)
94229424

94239425
tree->gtFlags &= ~(GTF_ALL_EFFECT | GTF_REVERSE_OPS);
94249426

9425-
if (tree->OperGet() != GT_CNS_STR)
9427+
if (!tree->OperIs(GT_CNS_STR))
94269428
{
94279429
return tree;
94289430
}
94299431

9432+
if (tree->AsStrCon()->IsStringEmptyField())
9433+
{
9434+
LPVOID pValue;
9435+
InfoAccessType iat = info.compCompHnd->emptyStringLiteral(&pValue);
9436+
return fgMorphTree(gtNewStringLiteralNode(iat, pValue));
9437+
}
9438+
94309439
// TODO-CQ: Do this for compCurBB->isRunRarely(). Doing that currently will
94319440
// guarantee slow performance for that block. Instead cache the return value
94329441
// of CORINFO_HELP_STRCNS and go to cache first giving reasonable perf.

0 commit comments

Comments
 (0)