summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyungwoo Lee <kyulee@microsoft.com>2016-01-24 19:19:55 -0800
committerKyungwoo Lee <kyulee@microsoft.com>2016-01-24 19:19:55 -0800
commit9489767df726d4bde4900c56734b19fecea087cd (patch)
tree70dec5408bf6639f0dea28b6332cbe80ba59da90
parent28d80a45a8d32ac98aad1dc552d6a8f7d23789bf (diff)
parentcbb877984d56c48358859f3cb3ab8cdc536809c6 (diff)
downloadcoreclr-9489767df726d4bde4900c56734b19fecea087cd.tar.gz
coreclr-9489767df726d4bde4900c56734b19fecea087cd.tar.bz2
coreclr-9489767df726d4bde4900c56734b19fecea087cd.zip
Merge pull request #2817 from pgavlin/CoreRTPInvoke
-rw-r--r--src/inc/corinfo.h20
-rw-r--r--src/inc/corjit.h8
-rw-r--r--src/inc/jithelpers.h3
-rw-r--r--src/jit/codegencommon.cpp24
-rw-r--r--src/jit/codegenlegacy.cpp14
-rw-r--r--src/jit/compiler.h4
-rw-r--r--src/jit/flowgraph.cpp12
-rw-r--r--src/jit/gentree.h2
-rw-r--r--src/jit/lclvars.cpp80
-rw-r--r--src/jit/liveness.cpp115
-rw-r--r--src/jit/lower.cpp96
-rw-r--r--src/jit/regalloc.cpp20
-rw-r--r--src/vm/jitinterface.cpp14
-rw-r--r--src/vm/jitinterface.h2
-rw-r--r--src/zap/zapinfo.cpp13
-rw-r--r--src/zap/zapinfo.h4
16 files changed, 272 insertions, 159 deletions
diff --git a/src/inc/corinfo.h b/src/inc/corinfo.h
index fef412678a..f6481a7e39 100644
--- a/src/inc/corinfo.h
+++ b/src/inc/corinfo.h
@@ -232,11 +232,11 @@ TODO: Talk about initializing strutures before use
#if COR_JIT_EE_VERSION > 460
// Update this one
-SELECTANY const GUID JITEEVersionIdentifier = { /* f7be09f3-9ca7-42fd-b0ca-f97c0499f5a3 */
- 0xf7be09f3,
- 0x9ca7,
- 0x42fd,
- {0xb0, 0xca, 0xf9, 0x7c, 0x04, 0x99, 0xf5, 0xa3}
+SELECTANY const GUID JITEEVersionIdentifier = { /* b26841f8-74d6-4fc9-9d81-6500cd662549 */
+ 0xb26841f8,
+ 0x74d6,
+ 0x4fc9,
+ { 0x9d, 0x81, 0x65, 0x0, 0xcd, 0x66, 0x25, 0x49 }
};
#else
@@ -695,6 +695,9 @@ enum CorInfoHelpFunc
CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION, // throw ArgumentOutOfRangeException
#endif
+ CORINFO_HELP_JIT_PINVOKE_BEGIN, // Transition to preemptive mode before a P/Invoke, frame is the first argument
+ CORINFO_HELP_JIT_PINVOKE_END, // Transition to cooperative mode after a P/Invoke, frame is the first argument
+
CORINFO_HELP_COUNT,
};
@@ -3644,10 +3647,9 @@ public:
) = 0;
// return address of fixup area for late-bound PInvoke calls.
- virtual void* getAddressOfPInvokeFixup(
- CORINFO_METHOD_HANDLE method,
- void **ppIndirection = NULL
- ) = 0;
+ virtual void getAddressOfPInvokeFixup(
+ CORINFO_METHOD_HANDLE method,
+ CORINFO_CONST_LOOKUP *pLookup) = 0;
// Generate a cookie based on the signature that would needs to be passed
// to CORINFO_HELP_PINVOKE_CALLI
diff --git a/src/inc/corjit.h b/src/inc/corjit.h
index 8612d954df..4313e2e38a 100644
--- a/src/inc/corjit.h
+++ b/src/inc/corjit.h
@@ -124,17 +124,15 @@ enum CorJitFlag
#ifdef MDIL
CORJIT_FLG_MDIL = 0x00004000, // Generate MDIL code instead of machine code
-#else // MDIL
- CORJIT_FLG_UNUSED7 = 0x00004000,
-#endif // MDIL
-#ifdef MDIL
// Safe to overlap with CORJIT_FLG_MAKEFINALCODE below. Not used by the JIT, used internally by NGen only.
CORJIT_FLG_MINIMAL_MDIL = 0x00008000, // Generate MDIL code suitable for use to bind other assemblies.
// Safe to overlap with CORJIT_FLG_READYTORUN below. Not used by the JIT, used internally by NGen only.
CORJIT_FLG_NO_MDIL = 0x00010000, // Generate an MDIL section but no code or CTL. Not used by the JIT, used internally by NGen only.
-#endif // MDIL
+#else // MDIL
+ CORJIT_FLG_PINVOKE_USE_HELPERS = 0x00004000, // Use JIT_PINVOKE_{BEGIN,END} helpers instead of generating transitions inline.
+#endif
#if defined(FEATURE_INTERPRETER)
CORJIT_FLG_MAKEFINALCODE = 0x00008000, // Use the final code generator, i.e., not the interpreter.
diff --git a/src/inc/jithelpers.h b/src/inc/jithelpers.h
index bd3ad544fb..eac9c45151 100644
--- a/src/inc/jithelpers.h
+++ b/src/inc/jithelpers.h
@@ -403,6 +403,9 @@
#endif // COR_JIT_EE_VERSION
+ JITHELPER1(CORINFO_HELP_JIT_PINVOKE_BEGIN, NULL, CORINFO_HELP_SIG_UNDEF, MDIL_HELP_UNDEF)
+ JITHELPER1(CORINFO_HELP_JIT_PINVOKE_END, NULL, CORINFO_HELP_SIG_UNDEF, MDIL_HELP_UNDEF)
+
#undef JITHELPER1
#undef DYNAMICJITHELPER1
#undef JITHELPER
diff --git a/src/jit/codegencommon.cpp b/src/jit/codegencommon.cpp
index 4830342e84..71b324327e 100644
--- a/src/jit/codegencommon.cpp
+++ b/src/jit/codegencommon.cpp
@@ -7557,6 +7557,7 @@ void CodeGen::genPrologPadForReJit()
regMaskTP CodeGen::genPInvokeMethodProlog(regMaskTP initRegs)
{
assert(compiler->compGeneratingProlog);
+ noway_assert(!compiler->opts.ShouldUsePInvokeHelpers());
noway_assert(compiler->info.compCallUnmanaged);
CORINFO_EE_INFO * pInfo = compiler->eeGetEEInfo();
@@ -7776,6 +7777,7 @@ regMaskTP CodeGen::genPInvokeMethodProlog(regMaskTP initRegs)
void CodeGen::genPInvokeMethodEpilog()
{
noway_assert(compiler->info.compCallUnmanaged);
+ noway_assert(!compiler->opts.ShouldUsePInvokeHelpers());
noway_assert(compiler->compCurBB == compiler->genReturnBB ||
(compiler->compTailCallUsed && (compiler->compCurBB->bbJumpKind == BBJ_THROW)) ||
(compiler->compJmpOpUsed && (compiler->compCurBB->bbFlags & BBF_HAS_JMP)));
@@ -8517,13 +8519,23 @@ void CodeGen::genFnProlog()
// since they are trashed by the jithelper call to setup the PINVOKE frame
if (compiler->info.compCallUnmanaged)
{
- excludeMask |= (RBM_PINVOKE_FRAME | RBM_PINVOKE_TCB | RBM_PINVOKE_SCRATCH);
+ excludeMask |= RBM_PINVOKE_FRAME;
- // We also must exclude the register used by compLvFrameListRoot when it is enregistered
- //
- LclVarDsc * varDsc = &compiler->lvaTable[compiler->info.compLvFrameListRoot];
- if (varDsc->lvRegister)
- excludeMask |= genRegMask(varDsc->lvRegNum);
+ assert(!compiler->opts.ShouldUsePInvokeHelpers() || compiler->info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!compiler->opts.ShouldUsePInvokeHelpers())
+ {
+ noway_assert(compiler->info.compLvFrameListRoot < compiler->lvaCount);
+
+ excludeMask |= (RBM_PINVOKE_TCB | RBM_PINVOKE_SCRATCH);
+
+ // We also must exclude the register used by compLvFrameListRoot when it is enregistered
+ //
+ LclVarDsc * varDsc = &compiler->lvaTable[compiler->info.compLvFrameListRoot];
+ if (varDsc->lvRegister)
+ {
+ excludeMask |= genRegMask(varDsc->lvRegNum);
+ }
+ }
}
#endif // INLINE_NDIRECT
diff --git a/src/jit/codegenlegacy.cpp b/src/jit/codegenlegacy.cpp
index 5d518a1e19..233c1631df 100644
--- a/src/jit/codegenlegacy.cpp
+++ b/src/jit/codegenlegacy.cpp
@@ -19684,15 +19684,15 @@ regMaskTP CodeGen::genCodeForCall(GenTreePtr call,
else
{
noway_assert(callType == CT_USER_FUNC);
-
- void* pAddr;
- addr = compiler->info.compCompHnd->getAddressOfPInvokeFixup(methHnd, (void**)&pAddr);
- if (addr != NULL)
+
+ CORINFO_CONST_LOOKUP lookup;
+ comp->info.compCompHnd->getAddressOfPInvokeFixup(methHnd, &lookup);
+ if (lookup.accessType == IAT_PVALUE)
{
#if CPU_LOAD_STORE_ARCH
// Load the address into a register, indirect it and call through a register
indCallReg = regSet.rsGrabReg(RBM_ALLINT); // Grab an available register to use for the CALL indirection
- instGen_Set_Reg_To_Imm(EA_HANDLE_CNS_RELOC, indCallReg, (ssize_t)addr);
+ instGen_Set_Reg_To_Imm(EA_HANDLE_CNS_RELOC, indCallReg, (ssize_t)lookup.addr);
getEmitter()->emitIns_R_R_I(INS_ldr, EA_PTRSIZE, indCallReg, indCallReg, 0);
regTracker.rsTrackRegTrash(indCallReg);
// Now make the call "call indCallReg"
@@ -19718,12 +19718,14 @@ regMaskTP CodeGen::genCodeForCall(GenTreePtr call,
}
else
{
+ assert(lookup.accessType == IAT_PPVALUE);
+
// Double-indirection. Load the address into a register
// and call indirectly through a register
indCallReg = regSet.rsGrabReg(RBM_ALLINT); // Grab an available register to use for the CALL indirection
#if CPU_LOAD_STORE_ARCH
- instGen_Set_Reg_To_Imm(EA_HANDLE_CNS_RELOC, indCallReg, (ssize_t)pAddr);
+ instGen_Set_Reg_To_Imm(EA_HANDLE_CNS_RELOC, indCallReg, (ssize_t)lookup.addr);
getEmitter()->emitIns_R_R_I(INS_ldr, EA_PTRSIZE, indCallReg, indCallReg, 0);
getEmitter()->emitIns_R_R_I(INS_ldr, EA_PTRSIZE, indCallReg, indCallReg, 0);
regTracker.rsTrackRegTrash(indCallReg);
diff --git a/src/jit/compiler.h b/src/jit/compiler.h
index 71cb1a30cd..b9babbddbc 100644
--- a/src/jit/compiler.h
+++ b/src/jit/compiler.h
@@ -7527,6 +7527,10 @@ public :
inline bool IsReadyToRun() { return false; }
#endif
+ // true if we should use the PINVOKE_{BEGIN,END} helpers instead of generating
+ // PInvoke transitions inline (e.g. when targeting CoreRT).
+ inline bool ShouldUsePInvokeHelpers() { return (eeFlags & CORJIT_FLG_PINVOKE_USE_HELPERS) != 0; }
+
// true if we must generate compatible code with Jit64 quirks
inline bool IsJit64Compat()
{
diff --git a/src/jit/flowgraph.cpp b/src/jit/flowgraph.cpp
index 39fd0a6f49..8cdbd96c70 100644
--- a/src/jit/flowgraph.cpp
+++ b/src/jit/flowgraph.cpp
@@ -8237,7 +8237,12 @@ void Compiler::fgAddInternal()
#if INLINE_NDIRECT
if (info.compCallUnmanaged != 0)
{
- info.compLvFrameListRoot = lvaGrabTemp(false DEBUGARG("Pinvoke FrameListRoot"));
+ // The P/Invoke helpers only require a frame variable, so only allocate the
+ // TCB variable if we're not using them.
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ info.compLvFrameListRoot = lvaGrabTemp(false DEBUGARG("Pinvoke FrameListRoot"));
+ }
lvaInlinedPInvokeFrameVar = lvaGrabTempWithImplicitUse(false DEBUGARG("Pinvoke FrameVar"));
@@ -8248,8 +8253,9 @@ void Compiler::fgAddInternal()
varDsc->lvExactSize = eeGetEEInfo()->inlinedCallFrameInfo.size;
#if FEATURE_FIXED_OUT_ARGS
// Grab and reserve space for TCB, Frame regs used in PInvoke epilog to pop the inlined frame.
- // See genPInvokeMethodEpilog() for use of the grabbed var.
- if (compJmpOpUsed)
+ // See genPInvokeMethodEpilog() for use of the grabbed var. This is only necessary if we are
+ // not using the P/Invoke helpers.
+ if (!opts.ShouldUsePInvokeHelpers() && compJmpOpUsed)
{
lvaPInvokeFrameRegSaveVar = lvaGrabTempWithImplicitUse(false DEBUGARG("PInvokeFrameRegSave Var"));
varDsc = &lvaTable[lvaPInvokeFrameRegSaveVar];
diff --git a/src/jit/gentree.h b/src/jit/gentree.h
index 9756dcbd6a..c41e15209b 100644
--- a/src/jit/gentree.h
+++ b/src/jit/gentree.h
@@ -2108,7 +2108,7 @@ struct GenTreeLclVar: public GenTreeLclVarCommon
DEBUG_ARG(largeNode)),
gtLclILoffs(ilOffs)
{
- assert(OperIsLocal(oper));
+ assert(OperIsLocal(oper) || OperIsLocalAddr(oper));
}
#if DEBUGGABLE_GENTREE
diff --git a/src/jit/lclvars.cpp b/src/jit/lclvars.cpp
index afe8aaf408..024fb41fa3 100644
--- a/src/jit/lclvars.cpp
+++ b/src/jit/lclvars.cpp
@@ -2159,17 +2159,21 @@ void Compiler::lvaDecRefCnts(GenTreePtr tree)
if ((tree->gtOper == GT_CALL) && (tree->gtFlags & GTF_CALL_UNMANAGED))
{
- /* Get the special variable descriptor */
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ /* Get the special variable descriptor */
- lclNum = info.compLvFrameListRoot;
-
- noway_assert(lclNum <= lvaCount);
- varDsc = lvaTable + lclNum;
-
- /* Decrement the reference counts twice */
+ lclNum = info.compLvFrameListRoot;
- varDsc->decRefCnts(compCurBB->getBBWeight(this), this);
- varDsc->decRefCnts(compCurBB->getBBWeight(this), this);
+ noway_assert(lclNum <= lvaCount);
+ varDsc = lvaTable + lclNum;
+
+ /* Decrement the reference counts twice */
+
+ varDsc->decRefCnts(compCurBB->getBBWeight(this), this);
+ varDsc->decRefCnts(compCurBB->getBBWeight(this), this);
+ }
}
else
{
@@ -2214,17 +2218,21 @@ void Compiler::lvaIncRefCnts(GenTreePtr tree)
if ((tree->gtOper == GT_CALL) && (tree->gtFlags & GTF_CALL_UNMANAGED))
{
- /* Get the special variable descriptor */
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ /* Get the special variable descriptor */
- lclNum = info.compLvFrameListRoot;
-
- noway_assert(lclNum <= lvaCount);
- varDsc = lvaTable + lclNum;
-
- /* Increment the reference counts twice */
+ lclNum = info.compLvFrameListRoot;
- varDsc->incRefCnts(compCurBB->getBBWeight(this), this);
- varDsc->incRefCnts(compCurBB->getBBWeight(this), this);
+ noway_assert(lclNum <= lvaCount);
+ varDsc = lvaTable + lclNum;
+
+ /* Increment the reference counts twice */
+
+ varDsc->incRefCnts(compCurBB->getBBWeight(this), this);
+ varDsc->incRefCnts(compCurBB->getBBWeight(this), this);
+ }
}
else
{
@@ -2800,16 +2808,20 @@ void Compiler::lvaMarkLclRefs(GenTreePtr tree)
/* Is this a call to unmanaged code ? */
if (tree->gtOper == GT_CALL && tree->gtFlags & GTF_CALL_UNMANAGED)
{
- /* Get the special variable descriptor */
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ /* Get the special variable descriptor */
- unsigned lclNum = info.compLvFrameListRoot;
-
- noway_assert(lclNum <= lvaCount);
- LclVarDsc * varDsc = lvaTable + lclNum;
+ unsigned lclNum = info.compLvFrameListRoot;
+
+ noway_assert(lclNum <= lvaCount);
+ LclVarDsc * varDsc = lvaTable + lclNum;
- /* Increment the ref counts twice */
- varDsc->incRefCnts(lvaMarkRefsWeight, this);
- varDsc->incRefCnts(lvaMarkRefsWeight, this);
+ /* Increment the ref counts twice */
+ varDsc->incRefCnts(lvaMarkRefsWeight, this);
+ varDsc->incRefCnts(lvaMarkRefsWeight, this);
+ }
}
#endif
@@ -3166,15 +3178,19 @@ void Compiler::lvaMarkLocalVars()
if (info.compCallUnmanaged != 0)
{
- noway_assert(info.compLvFrameListRoot >= info.compLocalsCount &&
- info.compLvFrameListRoot < lvaCount);
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ noway_assert(info.compLvFrameListRoot >= info.compLocalsCount &&
+ info.compLvFrameListRoot < lvaCount);
- lvaTable[info.compLvFrameListRoot].lvType = TYP_I_IMPL;
+ lvaTable[info.compLvFrameListRoot].lvType = TYP_I_IMPL;
- /* Set the refCnt, it is used in the prolog and return block(s) */
+ /* Set the refCnt, it is used in the prolog and return block(s) */
- lvaTable[info.compLvFrameListRoot].lvRefCnt = 2;
- lvaTable[info.compLvFrameListRoot].lvRefCntWtd = 2 * BB_UNITY_WEIGHT;
+ lvaTable[info.compLvFrameListRoot].lvRefCnt = 2;
+ lvaTable[info.compLvFrameListRoot].lvRefCntWtd = 2 * BB_UNITY_WEIGHT;
+ }
}
#endif
diff --git a/src/jit/liveness.cpp b/src/jit/liveness.cpp
index fc22a69eba..7de58587f4 100644
--- a/src/jit/liveness.cpp
+++ b/src/jit/liveness.cpp
@@ -437,19 +437,23 @@ void Compiler::fgPerStatementLocalVarLiveness(GenTreePtr startNode, GenTreePtr a
// This ensures that the block->bbVarUse will contain
// the FrameRoot local var if is it a tracked variable.
- if (tree->gtCall.IsUnmanaged() || (tree->gtCall.IsTailCall() && info.compCallUnmanaged))
+ if ((tree->gtCall.IsUnmanaged() || (tree->gtCall.IsTailCall() && info.compCallUnmanaged)))
{
- /* Get the TCB local and mark it as used */
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ /* Get the TCB local and mark it as used */
- noway_assert(info.compLvFrameListRoot < lvaCount);
+ noway_assert(info.compLvFrameListRoot < lvaCount);
- LclVarDsc* varDsc = &lvaTable[info.compLvFrameListRoot];
+ LclVarDsc* varDsc = &lvaTable[info.compLvFrameListRoot];
- if (varDsc->lvTracked)
- {
- if (!VarSetOps::IsMember(this, fgCurDefSet, varDsc->lvVarIndex))
+ if (varDsc->lvTracked)
{
- VarSetOps::AddElemD(this, fgCurUseSet, varDsc->lvVarIndex);
+ if (!VarSetOps::IsMember(this, fgCurDefSet, varDsc->lvVarIndex))
+ {
+ VarSetOps::AddElemD(this, fgCurUseSet, varDsc->lvVarIndex);
+ }
}
}
}
@@ -629,17 +633,21 @@ void Compiler::fgPerBlockLocalVarLiveness()
/* Get the TCB local and mark it as used */
- if (block->bbJumpKind == BBJ_RETURN && info.compCallUnmanaged)
+ if (block->bbJumpKind == BBJ_RETURN && info.compCallUnmanaged)
{
- noway_assert(info.compLvFrameListRoot < lvaCount);
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ noway_assert(info.compLvFrameListRoot < lvaCount);
- LclVarDsc * varDsc = &lvaTable[info.compLvFrameListRoot];
+ LclVarDsc * varDsc = &lvaTable[info.compLvFrameListRoot];
- if (varDsc->lvTracked)
- {
- if (!VarSetOps::IsMember(this, fgCurDefSet, varDsc->lvVarIndex))
+ if (varDsc->lvTracked)
{
- VarSetOps::AddElemD(this, fgCurUseSet, varDsc->lvVarIndex);
+ if (!VarSetOps::IsMember(this, fgCurDefSet, varDsc->lvVarIndex))
+ {
+ VarSetOps::AddElemD(this, fgCurUseSet, varDsc->lvVarIndex);
+ }
}
}
}
@@ -1742,21 +1750,25 @@ SKIP_QMARK:
if (tree->gtCall.IsTailCall() && info.compCallUnmanaged)
{
- /* Get the TCB local and make it live */
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
+ {
+ /* Get the TCB local and make it live */
- noway_assert(info.compLvFrameListRoot < lvaCount);
+ noway_assert(info.compLvFrameListRoot < lvaCount);
- LclVarDsc* frameVarDsc = &lvaTable[info.compLvFrameListRoot];
+ LclVarDsc* frameVarDsc = &lvaTable[info.compLvFrameListRoot];
- if (frameVarDsc->lvTracked)
- {
- VARSET_TP VARSET_INIT_NOCOPY(varBit, VarSetOps::MakeSingleton(this, frameVarDsc->lvVarIndex));
+ if (frameVarDsc->lvTracked)
+ {
+ VARSET_TP VARSET_INIT_NOCOPY(varBit, VarSetOps::MakeSingleton(this, frameVarDsc->lvVarIndex));
- VarSetOps::AddElemD(this, life, frameVarDsc->lvVarIndex);
+ VarSetOps::AddElemD(this, life, frameVarDsc->lvVarIndex);
- /* Record interference with other live variables */
+ /* Record interference with other live variables */
- fgMarkIntf(life, varBit);
+ fgMarkIntf(life, varBit);
+ }
}
}
@@ -1770,38 +1782,41 @@ SKIP_QMARK:
if (tree->gtCall.IsUnmanaged())
{
/* Get the TCB local and make it live */
-
- noway_assert(info.compLvFrameListRoot < lvaCount);
-
- LclVarDsc* frameVarDsc = &lvaTable[info.compLvFrameListRoot];
-
- if (frameVarDsc->lvTracked)
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
{
- unsigned varIndex = frameVarDsc->lvVarIndex;
- noway_assert(varIndex < lvaTrackedCount);
+ noway_assert(info.compLvFrameListRoot < lvaCount);
- // Is the variable already known to be alive?
- //
- if (VarSetOps::IsMember(this, life, varIndex))
+ LclVarDsc* frameVarDsc = &lvaTable[info.compLvFrameListRoot];
+
+ if (frameVarDsc->lvTracked)
{
- // Since we may call this multiple times, clear the GTF_CALL_M_FRAME_VAR_DEATH if set.
+ unsigned varIndex = frameVarDsc->lvVarIndex;
+ noway_assert(varIndex < lvaTrackedCount);
+
+ // Is the variable already known to be alive?
//
- tree->gtCall.gtCallMoreFlags &= ~GTF_CALL_M_FRAME_VAR_DEATH;
- }
- else
- {
- // The variable is just coming to life
- // Since this is a backwards walk of the trees
- // that makes this change in liveness a 'last-use'
+ if (VarSetOps::IsMember(this, life, varIndex))
+ {
+ // Since we may call this multiple times, clear the GTF_CALL_M_FRAME_VAR_DEATH if set.
+ //
+ tree->gtCall.gtCallMoreFlags &= ~GTF_CALL_M_FRAME_VAR_DEATH;
+ }
+ else
+ {
+ // The variable is just coming to life
+ // Since this is a backwards walk of the trees
+ // that makes this change in liveness a 'last-use'
+ //
+ VarSetOps::AddElemD(this, life, varIndex);
+ tree->gtCall.gtCallMoreFlags |= GTF_CALL_M_FRAME_VAR_DEATH;
+ }
+
+ // Record an interference with the other live variables
//
- VarSetOps::AddElemD(this, life, varIndex);
- tree->gtCall.gtCallMoreFlags |= GTF_CALL_M_FRAME_VAR_DEATH;
+ VARSET_TP VARSET_INIT_NOCOPY(varBit, VarSetOps::MakeSingleton(this, varIndex));
+ fgMarkIntf(life, varBit);
}
-
- // Record an interference with the other live variables
- //
- VARSET_TP VARSET_INIT_NOCOPY(varBit, VarSetOps::MakeSingleton(this, varIndex));
- fgMarkIntf(life, varBit);
}
/* Do we have any live variables? */
diff --git a/src/jit/lower.cpp b/src/jit/lower.cpp
index 68fa2c335a..ae02b1fc5e 100644
--- a/src/jit/lower.cpp
+++ b/src/jit/lower.cpp
@@ -2475,6 +2475,21 @@ void Lowering::InsertPInvokeMethodProlog()
noway_assert(comp->info.compCallUnmanaged);
noway_assert(comp->lvaInlinedPInvokeFrameVar != BAD_VAR_NUM);
+ if (comp->opts.ShouldUsePInvokeHelpers())
+ {
+ // Initialize the P/Invoke frame by calling CORINFO_HELP_INIT_PINVOKE_FRAME
+
+ GenTree* frameAddr = new(comp, GT_LCL_VAR_ADDR)
+ GenTreeLclVar(GT_LCL_VAR_ADDR, TYP_BYREF, comp->lvaInlinedPInvokeFrameVar, BAD_IL_OFFSET);
+
+ GenTree* helperCall = comp->gtNewHelperCallNode(CORINFO_HELP_INIT_PINVOKE_FRAME, TYP_VOID, 0, comp->gtNewArgList(frameAddr));
+
+ GenTreeStmt* stmt = LowerMorphAndSeqTree(helperCall);
+ comp->fgInsertStmtAtBeg(comp->fgFirstBB, stmt);
+
+ return;
+ }
+
CORINFO_EE_INFO* pInfo = comp->eeGetEEInfo();
const CORINFO_EE_INFO::InlinedCallFrameInfo& callFrameInfo = pInfo->inlinedCallFrameInfo;
@@ -2555,6 +2570,11 @@ void Lowering::InsertPInvokeMethodEpilog(BasicBlock *returnBB
assert(returnBB != nullptr);
assert(comp->info.compCallUnmanaged);
+ if (comp->opts.ShouldUsePInvokeHelpers())
+ {
+ return;
+ }
+
// Method doing Pinvoke calls has exactly one return block unless it has "jmp" or tail calls.
#ifdef DEBUG
bool endsWithTailCallOrJmp = false;
@@ -2612,14 +2632,6 @@ void Lowering::InsertPInvokeMethodEpilog(BasicBlock *returnBB
// field of the frame (methodHandle may be indirected & have a reloc)
void Lowering::InsertPInvokeCallProlog(GenTreeCall* call)
{
- // emit the following sequence
- //
- // frame.callTarget := methodHandle
- // (x86) frame.callSiteTracker = SP
- // frame.callSiteReturnAddress = return address
- // thread->gcState = 0
- // (non-stub) - update top Frame on TCB
- //
GenTree* insertBefore = call;
if (call->gtCallType == CT_INDIRECT)
{
@@ -2633,6 +2645,29 @@ void Lowering::InsertPInvokeCallProlog(GenTreeCall* call)
noway_assert(comp->lvaInlinedPInvokeFrameVar != BAD_VAR_NUM);
+ if (comp->opts.ShouldUsePInvokeHelpers())
+ {
+ // First argument is the address of the frame variable.
+ GenTree* frameAddr = new(comp, GT_LCL_VAR_ADDR)
+ GenTreeLclVar(GT_LCL_VAR_ADDR, TYP_BYREF, comp->lvaInlinedPInvokeFrameVar, BAD_IL_OFFSET);
+
+ // Insert call to CORINFO_HELP_JIT_PINVOKE_BEGIN
+ GenTree* helperCall = comp->gtNewHelperCallNode(CORINFO_HELP_JIT_PINVOKE_BEGIN, TYP_VOID, 0, comp->gtNewArgList(frameAddr));
+
+ comp->fgMorphTree(helperCall);
+ comp->fgInsertTreeBeforeAsEmbedded(helperCall, insertBefore, comp->compCurStmt->AsStmt(), currBlock);
+ return;
+ }
+
+ // emit the following sequence
+ //
+ // frame.callTarget := methodHandle
+ // (x86) frame.callSiteTracker = SP
+ // frame.callSiteReturnAddress = return address
+ // thread->gcState = 0
+ // (non-stub) - update top Frame on TCB
+ //
+
// ----------------------------------------------------------------------------------
// Setup frame.callSiteTarget (what it referred to in the JIT)
// The actual field is Frame.m_Datum which has many different uses and meanings.
@@ -2751,10 +2786,26 @@ void Lowering::InsertPInvokeCallProlog(GenTreeCall* call)
comp->fgInsertTreeBeforeAsEmbedded(storeGCState, insertBefore, comp->compCurStmt->AsStmt(), currBlock);
}
-
// insert the code that goes after every inlined pinvoke call
void Lowering::InsertPInvokeCallEpilog(GenTreeCall* call)
{
+ if (comp->opts.ShouldUsePInvokeHelpers())
+ {
+ noway_assert(comp->lvaInlinedPInvokeFrameVar != BAD_VAR_NUM);
+
+ // First argument is the address of the frame variable.
+ GenTree* frameAddr = new(comp, GT_LCL_VAR)
+ GenTreeLclVar(GT_LCL_VAR, TYP_BYREF, comp->lvaInlinedPInvokeFrameVar, BAD_IL_OFFSET);
+ frameAddr->gtOper = GT_LCL_VAR_ADDR;
+
+ // Insert call to CORINFO_HELP_JIT_PINVOKE_END
+ GenTree* helperCall = comp->gtNewHelperCallNode(CORINFO_HELP_JIT_PINVOKE_END, TYP_VOID, 0, comp->gtNewArgList(frameAddr));
+
+ comp->fgMorphTree(helperCall);
+ comp->fgInsertTreeAfterAsEmbedded(helperCall, call, comp->compCurStmt->AsStmt(), currBlock);
+ return;
+ }
+
CORINFO_EE_INFO* pInfo = comp->eeGetEEInfo();
GenTreeStmt* newStmt;
GenTreeStmt* topStmt = comp->compCurStmt->AsStmt();
@@ -2811,25 +2862,28 @@ GenTree* Lowering::LowerNonvirtPinvokeCall(GenTreeCall* call)
if (call->gtCallType != CT_INDIRECT)
{
- GenTree* indir = nullptr;
-
noway_assert(call->gtCallType == CT_USER_FUNC);
CORINFO_METHOD_HANDLE methHnd = call->gtCallMethHnd;
- void* pAddr;
- addr = comp->info.compCompHnd->getAddressOfPInvokeFixup(methHnd, (void**)&pAddr);
+ CORINFO_CONST_LOOKUP lookup;
+ comp->info.compCompHnd->getAddressOfPInvokeFixup(methHnd, &lookup);
- if (addr != nullptr)
- {
- indir = Ind(AddrGen(addr));
- }
- else
+ switch (lookup.accessType)
{
- // double indirection
- indir = Ind(Ind(AddrGen(pAddr)));
+ case IAT_VALUE:
+ // No need for any indirection. The target is already correct.
+ break;
+
+ case IAT_PVALUE:
+ result = Ind(AddrGen(lookup.addr));
+ break;
+
+ case IAT_PPVALUE:
+ result = Ind(Ind(AddrGen(lookup.addr)));
+ break;
}
- result = indir;
}
+
InsertPInvokeCallEpilog(call);
return result;
diff --git a/src/jit/regalloc.cpp b/src/jit/regalloc.cpp
index 78ca194ff3..0059362e60 100644
--- a/src/jit/regalloc.cpp
+++ b/src/jit/regalloc.cpp
@@ -6439,15 +6439,21 @@ void Compiler::rpPredictRegUse()
// it must not be in a register trashed by the callee
if (info.compCallUnmanaged != 0)
{
- LclVarDsc * pinvokeVarDsc = &lvaTable[info.compLvFrameListRoot];
-
- if (pinvokeVarDsc->lvTracked)
+ assert(!opts.ShouldUsePInvokeHelpers() || info.compLvFrameListRoot == BAD_VAR_NUM);
+ if (!opts.ShouldUsePInvokeHelpers())
{
- rpRecordRegIntf(RBM_CALLEE_TRASH, VarSetOps::MakeSingleton(this, pinvokeVarDsc->lvVarIndex)
- DEBUGARG("compLvFrameListRoot"));
+ noway_assert(info.compLvFrameListRoot < lvaCount);
+
+ LclVarDsc * pinvokeVarDsc = &lvaTable[info.compLvFrameListRoot];
- // We would prefer to have this be enregister in the PINVOKE_TCB register
- pinvokeVarDsc->addPrefReg(RBM_PINVOKE_TCB, this);
+ if (pinvokeVarDsc->lvTracked)
+ {
+ rpRecordRegIntf(RBM_CALLEE_TRASH, VarSetOps::MakeSingleton(this, pinvokeVarDsc->lvVarIndex)
+ DEBUGARG("compLvFrameListRoot"));
+
+ // We would prefer to have this be enregister in the PINVOKE_TCB register
+ pinvokeVarDsc->addPrefReg(RBM_PINVOKE_TCB, this);
+ }
}
//If we're using a single return block, the p/invoke epilog code trashes ESI and EDI (in the
diff --git a/src/vm/jitinterface.cpp b/src/vm/jitinterface.cpp
index 519ccc266c..ab5c59a8f8 100644
--- a/src/vm/jitinterface.cpp
+++ b/src/vm/jitinterface.cpp
@@ -9953,8 +9953,8 @@ void* CEEInfo::getPInvokeUnmanagedTarget(CORINFO_METHOD_HANDLE method,
/*********************************************************************/
// return address of fixup area for late-bound N/Direct calls.
-void* CEEInfo::getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,
- void **ppIndirection)
+void CEEInfo::getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,
+ CORINFO_CONST_LOOKUP *pLookup)
{
CONTRACTL {
SO_TOLERANT;
@@ -9963,22 +9963,16 @@ void* CEEInfo::getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,
MODE_PREEMPTIVE;
} CONTRACTL_END;
- void * result = NULL;
-
- if (ppIndirection != NULL)
- *ppIndirection = NULL;
-
JIT_TO_EE_TRANSITION_LEAF();
MethodDesc* ftn = GetMethod(method);
_ASSERTE(ftn->IsNDirect());
NDirectMethodDesc *pMD = (NDirectMethodDesc*)ftn;
- result = (LPVOID)&(pMD->GetWriteableData()->m_pNDirectTarget);
+ pLookup->accessType = IAT_PVALUE;
+ pLookup->addr = (LPVOID)&(pMD->GetWriteableData()->m_pNDirectTarget);
EE_TO_JIT_TRANSITION_LEAF();
-
- return result;
}
diff --git a/src/vm/jitinterface.h b/src/vm/jitinterface.h
index 0035715e34..c2fa7a1744 100644
--- a/src/vm/jitinterface.h
+++ b/src/vm/jitinterface.h
@@ -911,7 +911,7 @@ public:
CORINFO_VARARGS_HANDLE getVarArgsHandle(CORINFO_SIG_INFO *sig, void **ppIndirection);
bool canGetVarArgsHandle(CORINFO_SIG_INFO *sig);
void* getPInvokeUnmanagedTarget(CORINFO_METHOD_HANDLE method, void **ppIndirection);
- void* getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method, void **ppIndirection);
+ void getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method, CORINFO_CONST_LOOKUP *pLookup);
CORINFO_JUST_MY_CODE_HANDLE getJustMyCodeHandle(CORINFO_METHOD_HANDLE method, CORINFO_JUST_MY_CODE_HANDLE **ppIndirection);
void GetProfilingHandle(
diff --git a/src/zap/zapinfo.cpp b/src/zap/zapinfo.cpp
index e3c2759e82..5258dfef24 100644
--- a/src/zap/zapinfo.cpp
+++ b/src/zap/zapinfo.cpp
@@ -2731,9 +2731,9 @@ void * ZapInfo::getPInvokeUnmanagedTarget(CORINFO_METHOD_HANDLE method, void **p
return NULL;
}
-void * ZapInfo::getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,void **ppIndirection)
+void ZapInfo::getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method, CORINFO_CONST_LOOKUP *pLookup)
{
- _ASSERTE(ppIndirection != NULL);
+ _ASSERTE(pLookup != NULL);
m_pImage->m_pPreloader->AddMethodToTransitiveClosureOfInstantiations(method);
@@ -2741,8 +2741,9 @@ void * ZapInfo::getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,void **ppI
if (moduleHandle == m_pImage->m_hModule
&& m_pImage->m_pPreloader->CanEmbedMethodHandle(method, m_currentMethodHandle))
{
- *ppIndirection = NULL;
- return PVOID(m_pImage->GetWrappers()->GetAddrOfPInvokeFixup(method));
+ pLookup->accessType = IAT_PVALUE;
+ pLookup->addr = PVOID(m_pImage->GetWrappers()->GetAddrOfPInvokeFixup(method));
+ return;
}
//
@@ -2755,8 +2756,8 @@ void * ZapInfo::getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,void **ppI
ZapImport * pImport = m_pImage->GetImportTable()->GetIndirectPInvokeTargetImport(method);
AppendConditionalImport(pImport);
- *ppIndirection = pImport;
- return NULL;
+ pLookup->accessType = IAT_PPVALUE;
+ pLookup->addr = pImport;
}
CORINFO_JUST_MY_CODE_HANDLE ZapInfo::getJustMyCodeHandle(
diff --git a/src/zap/zapinfo.h b/src/zap/zapinfo.h
index 1d16c9b760..e32d2068d5 100644
--- a/src/zap/zapinfo.h
+++ b/src/zap/zapinfo.h
@@ -430,8 +430,8 @@ public:
void **ppIndirection);
void * getPInvokeUnmanagedTarget(CORINFO_METHOD_HANDLE method,
void **ppIndirection);
- void * getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,
- void **ppIndirection);
+ void getAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE method,
+ CORINFO_CONST_LOOKUP *pLookup);
CORINFO_JUST_MY_CODE_HANDLE getJustMyCodeHandle(
CORINFO_METHOD_HANDLE method,
CORINFO_JUST_MY_CODE_HANDLE **ppIndirection);