Skip to content

Commit 3d7e89c

Browse files
Remove the "anything + null => null" optimization (#61518)
This optimization is only legal if: 1) "Anything" is a sufficiently small constant itself. 2) We are in a context where we know the address will in fact be used for an indirection. It is the second point that is problematic - one would like to use MorphAddrContext, but it is not suitable for this purpose, as an unknown context is counted as an indirecting one. Additionally, the value of this optimization is rather low. I am guessing it was meant to support the legacy nullchecks, before GT_NULLCHECK was introduced, and had higher impact then. So, just remove the optimization and leave the 5 small regressions across all of SPMI be.
1 parent 4cf86c2 commit 3d7e89c

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/coreclr/jit/morph.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12401,27 +12401,9 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac)
1240112401

1240212402
if (op2->IsCnsIntOrI() && varTypeIsIntegralOrI(typ))
1240312403
{
12404-
CLANG_FORMAT_COMMENT_ANCHOR;
12405-
1240612404
// Fold (x + 0).
12407-
1240812405
if ((op2->AsIntConCommon()->IconValue() == 0) && !gtIsActiveCSE_Candidate(tree))
1240912406
{
12410-
12411-
// If this addition is adding an offset to a null pointer,
12412-
// avoid the work and yield the null pointer immediately.
12413-
// Dereferencing the pointer in either case will have the
12414-
// same effect.
12415-
12416-
if (!optValnumCSE_phase && varTypeIsGC(op2->TypeGet()) &&
12417-
((op1->gtFlags & GTF_ALL_EFFECT) == 0))
12418-
{
12419-
op2->gtType = tree->gtType;
12420-
DEBUG_DESTROY_NODE(op1);
12421-
DEBUG_DESTROY_NODE(tree);
12422-
return op2;
12423-
}
12424-
1242512407
// Remove the addition iff it won't change the tree type
1242612408
// to TYP_REF.
1242712409

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
unsafe class Runtime_61510
7+
{
8+
[FixedAddressValueType]
9+
private static byte s_field;
10+
11+
public static int Main()
12+
{
13+
ref byte result = ref AddZeroByrefToNativeInt((nint)Unsafe.AsPointer(ref s_field));
14+
15+
return Unsafe.AreSame(ref s_field, ref result) ? 100 : 101;
16+
}
17+
18+
[MethodImpl(MethodImplOptions.NoInlining)]
19+
private static ref byte AddZeroByrefToNativeInt(nint addr)
20+
{
21+
return ref Unsafe.Add(ref Unsafe.NullRef<byte>(), addr);
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<Optimize>True</Optimize>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="$(MSBuildProjectName).cs" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)