From 3a3829d60a1e49466fc2d02918261854f3009516 Mon Sep 17 00:00:00 2001 From: SingleAccretion Date: Mon, 18 Jan 2021 22:41:07 +0300 Subject: [PATCH 1/4] Fold casts of constants in the importer --- src/coreclr/jit/importer.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 078a745f39f407..1031c8f5dab5b1 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -13479,7 +13479,6 @@ void Compiler::impImportBlockCode(BasicBlock* block) */ type = genActualType(lclTyp); - // If this is a no-op cast, just use op1. if (!ovfl && (type == op1->TypeGet()) && (genTypeSize(type) == genTypeSize(lclTyp))) { @@ -13488,19 +13487,26 @@ void Compiler::impImportBlockCode(BasicBlock* block) // Work is evidently required, add cast node else { + GenTree* castOp = op1; if (callNode) { - op1 = gtNewCastNodeL(type, op1, uns, lclTyp); + op1 = gtNewCastNodeL(type, castOp, uns, lclTyp); } else { - op1 = gtNewCastNode(type, op1, uns, lclTyp); + op1 = gtNewCastNode(type, castOp, uns, lclTyp); } if (ovfl) { op1->gtFlags |= (GTF_OVERFLOW | GTF_EXCEPT); } + + if (castOp->OperIsConst() && opts.OptimizationEnabled()) + { + // Try and fold the introduced cast + op1 = gtFoldExprConst(op1); + } } impPushOnStack(op1, tiRetVal); From beb589d3a5d6f6094404b4434c6becc318572424 Mon Sep 17 00:00:00 2001 From: SingleAccretion Date: Thu, 21 Jan 2021 00:58:48 +0300 Subject: [PATCH 2/4] Do not use a separate local for the cast operand --- src/coreclr/jit/importer.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 1031c8f5dab5b1..32b6eab0355e76 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -13487,14 +13487,13 @@ void Compiler::impImportBlockCode(BasicBlock* block) // Work is evidently required, add cast node else { - GenTree* castOp = op1; if (callNode) { - op1 = gtNewCastNodeL(type, castOp, uns, lclTyp); + op1 = gtNewCastNodeL(type, op1, uns, lclTyp); } else { - op1 = gtNewCastNode(type, castOp, uns, lclTyp); + op1 = gtNewCastNode(type, op1, uns, lclTyp); } if (ovfl) @@ -13502,7 +13501,7 @@ void Compiler::impImportBlockCode(BasicBlock* block) op1->gtFlags |= (GTF_OVERFLOW | GTF_EXCEPT); } - if (castOp->OperIsConst() && opts.OptimizationEnabled()) + if (op1->gtGetOp1()->OperIsConst() && opts.OptimizationEnabled()) { // Try and fold the introduced cast op1 = gtFoldExprConst(op1); From e329cae3ad54dd4aaddb95d97d960e2766868d98 Mon Sep 17 00:00:00 2001 From: SingleAccretion Date: Thu, 21 Jan 2021 01:01:23 +0300 Subject: [PATCH 3/4] Do not try to access the operation that has been folded --- src/coreclr/jit/importer.cpp | 1 + src/coreclr/jit/morph.cpp | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 32b6eab0355e76..0ea01bc931627e 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -13479,6 +13479,7 @@ void Compiler::impImportBlockCode(BasicBlock* block) */ type = genActualType(lclTyp); + // If this is a no-op cast, just use op1. if (!ovfl && (type == op1->TypeGet()) && (genTypeSize(type) == genTypeSize(lclTyp))) { diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index debbe3b85f01e1..5a4b142914700b 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -12211,8 +12211,11 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) { tree = gtFoldExpr(tree); } + else + { + tree->AsOp()->CheckDivideByConstOptimized(this); + } - tree->AsOp()->CheckDivideByConstOptimized(this); return tree; } } From 061a2fb92b93707b1f2af7490e73427371dfb272 Mon Sep 17 00:00:00 2001 From: SingleAccretion Date: Sat, 23 Jan 2021 00:59:46 +0300 Subject: [PATCH 4/4] Condition the call to CheckDivideByConstOptimized on success of the folding --- src/coreclr/jit/morph.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index 5a4b142914700b..9816306ebb6c1c 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -12211,7 +12211,9 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac) { tree = gtFoldExpr(tree); } - else + + // We may fail to fold + if (!tree->OperIsConst()) { tree->AsOp()->CheckDivideByConstOptimized(this); }