diff --git a/src/coreclr/jit/flowgraph.cpp b/src/coreclr/jit/flowgraph.cpp index 91d62de8316b75..c82bdaf8b1c6de 100644 --- a/src/coreclr/jit/flowgraph.cpp +++ b/src/coreclr/jit/flowgraph.cpp @@ -2307,15 +2307,6 @@ PhaseStatus Compiler::fgAddInternal() // type with a runtime lookup madeChanges |= fgCreateFiltersForGenericExceptions(); - // The backend requires a scratch BB into which it can safely insert a P/Invoke method prolog if one is - // required. Similarly, we need a scratch BB for poisoning and when we have Swift parameters to reassemble. - // Create it here. - if (compMethodRequiresPInvokeFrame() || compShouldPoisonFrame() || lvaHasAnySwiftStackParamToReassemble()) - { - madeChanges |= fgEnsureFirstBBisScratch(); - fgFirstBB->SetFlags(BBF_DONT_REMOVE); - } - /* VSW441487 diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index 53e43858ace0a9..2946c2b5188e74 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -7812,6 +7812,18 @@ void Lowering::WidenSIMD12IfNecessary(GenTreeLclVarCommon* node) PhaseStatus Lowering::DoPhase() { + // We need a scratch BB into which it can safely insert a P/Invoke method + // prolog if one is required. Similarly, we need a scratch BB for poisoning + // and when we have Swift parameters to reassemble. Create it here. + if (comp->compMethodRequiresPInvokeFrame() || comp->compShouldPoisonFrame() || + comp->lvaHasAnySwiftStackParamToReassemble()) + { + if (comp->fgEnsureFirstBBisScratch()) + { + comp->fgInvalidateDfsTree(); + } + } + // If we have any PInvoke calls, insert the one-time prolog code. We'll insert the epilog code in the // appropriate spots later. NOTE: there is a minor optimization opportunity here, as we still create p/invoke // data structures and setup/teardown even if we've eliminated all p/invoke calls due to dead code elimination.