Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/coreclr/jit/codegencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ bool CodeGen::genCreateAddrMode(
{
ssize_t tmpMul;
GenTree* index;
bool indexIsEffectivelyZero = false;

if ((rv2->gtOper == GT_MUL || rv2->gtOper == GT_LSH) && (rv2->AsOp()->gtOp2->IsCnsIntOrI()))
{
Expand All @@ -1401,6 +1402,12 @@ bool CodeGen::genCreateAddrMode(
{
tmpMul *= mul;
}

if (tmpMul == 0)
{
// "Index * 0" (if it wasn't folded earlier) means the index is zero
indexIsEffectivelyZero = true;
}
}
else
{
Expand All @@ -1410,8 +1417,13 @@ bool CodeGen::genCreateAddrMode(
tmpMul = mul;
}

if (indexIsEffectivelyZero)
{
mul = 0;
rv2 = nullptr;
}
/* Get hold of the array index and see if it's a constant */
if (index->IsIntCnsFitsInI32())
else if (index->IsIntCnsFitsInI32())
{
/* Get hold of the index value */
ssize_t ixv = index->AsIntConCommon()->IconValue();
Expand Down
52 changes: 52 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_75312/Runtime_75312.il
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

.assembly extern System.Runtime {}
.assembly extern System.Runtime.Extensions {}
.assembly extern System.Console {}
.assembly Runtime_75312 {}

.class public abstract auto ansi sealed beforefieldinit Runtime_75312
extends [System.Runtime]System.Object
{
.method private hidebysig static
int32 Main () cil managed
{
.entrypoint
.maxstack 2
.locals init (
[0] int64 a
)
ldc.i8 1234605616436508552
stloc.0
ldc.i4 1146447579
ldloca.s 0
conv.u
newobj instance void [System.Runtime]System.IntPtr::.ctor(void*)
call int32 [System.Runtime]System.IntPtr::op_Explicit(native int)
call int32 Runtime_75312::Test(int32)
sub
ret
}

.method private hidebysig static int32 Test (int32 lcl) cil managed noinlining nooptimization
{
.maxstack 8

// return *(int*)(arg0 + ((3 * 0) << 2) + 1);
// to avoid constant folding in Roslyn (even for Debug) it's written in IL

ldarg.0
ldc.i4.3
ldc.i4.0
mul
ldc.i4.2
shl
add
ldc.i4.1
add
conv.i
ldind.i4
ret
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.IL">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestTargetUnsupported Condition="'$(TargetBits)' != '32'">true</CLRTestTargetUnsupported>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).il" />
</ItemGroup>
</Project>