diff --git a/src/coreclr/jit/codegenlinear.cpp b/src/coreclr/jit/codegenlinear.cpp index 8e476a23f1acdf..67f4bb73c0eb18 100644 --- a/src/coreclr/jit/codegenlinear.cpp +++ b/src/coreclr/jit/codegenlinear.cpp @@ -835,28 +835,29 @@ void CodeGen::genCodeForBBlist() #endif // DEBUG } //------------------ END-FOR each block of the method ------------------- - // There could be variables alive at this point. For example see lvaKeepAliveAndReportThis. - // This call is for cleaning the GC refs - genUpdateLife(VarSetOps::MakeEmpty(compiler)); - #if !defined(FEATURE_EH_FUNCLETS) // If this is a synchronized method on x86, and we generated all the code without // generating the "exit monitor" call, then we must have deleted the single return block // with that call because it was dead code. We still need to report the monitor range // to the VM in the GC info, so create a label at the very end so we have a marker for // the monitor end range. + // + // Do this before cleaning the GC refs below; we don't want to create an IG that clears + // the `this` pointer for lvaKeepAliveAndReportThis. if ((compiler->info.compFlags & CORINFO_FLG_SYNCH) && (compiler->syncEndEmitCookie == nullptr)) { JITDUMP("Synchronized method with missing exit monitor call; adding final label\n"); - - // The GC liveness sets should all be empty. compiler->syncEndEmitCookie = GetEmitter()->emitAddLabel(gcInfo.gcVarPtrSetCur, gcInfo.gcRegGCrefSetCur, gcInfo.gcRegByrefSetCur); noway_assert(compiler->syncEndEmitCookie != nullptr); } #endif // !FEATURE_EH_FUNCLETS + // There could be variables alive at this point. For example see lvaKeepAliveAndReportThis. + // This call is for cleaning the GC refs + genUpdateLife(VarSetOps::MakeEmpty(compiler)); + /* Finalize the spill tracking logic */ regSet.rsSpillEnd(); diff --git a/src/coreclr/jit/compiler.hpp b/src/coreclr/jit/compiler.hpp index c9ce2dbe2e9bf0..b00f3e66139216 100644 --- a/src/coreclr/jit/compiler.hpp +++ b/src/coreclr/jit/compiler.hpp @@ -2320,7 +2320,7 @@ inline void LclVarDsc::incRefCnts(weight_t weight, Compiler* comp, RefCountState inline bool Compiler::lvaKeepAliveAndReportThis() { - if (info.compIsStatic || lvaTable[0].TypeGet() != TYP_REF) + if (info.compIsStatic || (lvaTable[0].TypeGet() != TYP_REF)) { return false; } @@ -2354,7 +2354,7 @@ inline bool Compiler::lvaKeepAliveAndReportThis() // We keep it alive in the lookup scenario, even when the VM didn't ask us to, // because collectible types need the generics context when gc-ing. // - // Methoods that can inspire OSR methods must always report context as live + // Methods that can inspire OSR methods must always report context as live // if (genericsContextIsThis) {