Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 1 addition & 4 deletions src/coreclr/jit/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -1317,10 +1317,7 @@ class CodeGen final : public CodeGenInterface
#endif // !TARGET_X86

#if defined(DEBUG) && defined(TARGET_XARCH)
void genStackPointerCheck(bool doStackPointerCheck,
unsigned lvaStackPointerVar,
ssize_t offset = 0,
regNumber regTmp = REG_NA);
void genStackPointerCheck(bool doStackPointerCheck, unsigned lvaStackPointerVar, ssize_t offset = 0);
#endif // defined(DEBUG) && defined(TARGET_XARCH)

#ifdef DEBUG
Expand Down
19 changes: 6 additions & 13 deletions src/coreclr/jit/codegencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8223,15 +8223,11 @@ regNumber CodeGen::genRegCopy(GenTree* treeNode, unsigned multiRegIndex)
// lvaStackPointerVar - The local variable number that holds the value of the stack pointer
// we are comparing against.
// offset - the offset from the stack pointer to expect
// regTmp - register we can use for computation if `offset` != 0
//
// Return Value:
// None
//
void CodeGen::genStackPointerCheck(bool doStackPointerCheck,
unsigned lvaStackPointerVar,
ssize_t offset,
regNumber regTmp)
void CodeGen::genStackPointerCheck(bool doStackPointerCheck, unsigned lvaStackPointerVar, ssize_t offset)
{
if (doStackPointerCheck)
{
Expand All @@ -8241,16 +8237,13 @@ void CodeGen::genStackPointerCheck(bool doStackPointerCheck,

if (offset != 0)
{
assert(regTmp != REG_NA);
GetEmitter()->emitIns_Mov(INS_mov, EA_PTRSIZE, regTmp, REG_SPBASE, /* canSkip */ false);
GetEmitter()->emitIns_R_I(INS_sub, EA_PTRSIZE, regTmp, offset);
GetEmitter()->emitIns_S_R(INS_cmp, EA_PTRSIZE, regTmp, lvaStackPointerVar, 0);
}
else
{
GetEmitter()->emitIns_S_R(INS_cmp, EA_PTRSIZE, REG_SPBASE, lvaStackPointerVar, 0);
// Adjust the saved value rather than SP so that no scratch register is needed
// (e.g. after a call, any register may hold its result).
GetEmitter()->emitIns_S_I(INS_add, EA_PTRSIZE, lvaStackPointerVar, 0, (int)offset);
}

GetEmitter()->emitIns_S_R(INS_cmp, EA_PTRSIZE, REG_SPBASE, lvaStackPointerVar, 0);

BasicBlock* sp_check = genCreateTempLabel();
GetEmitter()->emitIns_J(INS_je, sp_check);
instGen(INS_BREAKPOINT);
Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/jit/codegenxarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5976,9 +5976,8 @@ void CodeGen::genCall(GenTreeCall* call)
}

#if defined(DEBUG) && defined(TARGET_X86)
// REG_ARG_0 (ECX) is trashed, so can be used as a temporary register.
genStackPointerCheck(m_compiler->opts.compStackCheckOnCall && (call->gtCallType == CT_USER_FUNC),
m_compiler->lvaCallSpCheck, call->CallerPop() ? 0 : stackArgBytes, REG_ARG_0);
m_compiler->lvaCallSpCheck, call->CallerPop() ? 0 : stackArgBytes);
#endif // defined(DEBUG) && defined(TARGET_X86)

unsigned stackAdjustBias = 0;
Expand Down Expand Up @@ -7536,7 +7535,9 @@ void CodeGen::genRemoveAlignmentAfterCall(GenTreeCall* call, unsigned bias)
#if defined(TARGET_X86)
if (bias != 0)
{
if (bias == sizeof(int))
// LSRA may have allocated the call's result to ECX, so we can only pop into it when it's free.
bool resultInEcx = !call->TypeIs(TYP_VOID) && ((call->gtGetRegMask() & RBM_ECX) != RBM_NONE);
if ((bias == sizeof(int)) && !resultInEcx)
{
inst_RV(INS_pop, REG_ECX, TYP_INT);
}
Expand Down
25 changes: 25 additions & 0 deletions src/tests/JIT/Regression_ro_2/Runtime_137798.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;

public class Runtime_137798
{
[DllImport("msvcrt", CallingConvention = CallingConvention.Cdecl), SuppressGCTransition]
private static extern int abs(int value);

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public static void TestEntryPoint()
{
Assert.Equal(42, Test(-42));
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static int Test(int x) => Consume(abs(x));

[MethodImpl(MethodImplOptions.NoInlining)]
private static int Consume(int value) => value;
}
Loading