Skip to content

Commit

Permalink
JIT: simple forward substitution pass
Browse files Browse the repository at this point in the history
Extend ref counting done by local morph so that we can determine
single-def single-use locals.

Add a phase that runs just after local morph that will attempt to
forward single-def single-use local defs to uses when they are in
adjacent statements.

Fix or work around issues uncovered elsewhere:
* `gtFoldExprCompare` might fold "identical" volatile subtrees
* `fgGetStubAddrArg` cannot handle complex trees
* some simd/hw operations can lose struct handles
* some calls cannot handle struct local args

Addresses dotnet#6973 and related issues. Still sorting through exactly
which ones are fixed, so list below may need revising.

Fixes dotnet#48605.
Fixes dotnet#51599.
Fixes dotnet#55472.

Improves some but not all cases in dotnet#12280 and dotnet#62064.

Does not fix dotnet#33002, dotnet#47082, or dotnet#63116; these require handling multiple
uses or bypassing statements.
  • Loading branch information
AndyAyersMS committed Jan 13, 2022
1 parent e0287b7 commit 86992b6
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/coreclr/jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ set( JIT_SOURCES
fgprofile.cpp
fgstmt.cpp
flowgraph.cpp
forwardsub.cpp
gcdecode.cpp
gcencode.cpp
gcinfo.cpp
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4796,6 +4796,13 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_STR_ADRLCL, &Compiler::fgMarkAddressExposedLocals);

// Run a simple forward sub pass.
//
if (opts.OptimizationEnabled())
{
DoPhase(this, PHASE_FWD_SUB, &Compiler::fgForwardSub);
}

// Apply the type update to implicit byref parameters; also choose (based on address-exposed
// analysis) which implicit byref promotions to keep (requires copy to initialize) or discard.
//
Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6411,7 +6411,7 @@ class Compiler

bool fgMorphCanUseLclFldForCopy(unsigned lclNum1, unsigned lclNum2);

GenTreeLclVar* fgMorphTryFoldObjAsLclVar(GenTreeObj* obj);
GenTreeLclVar* fgMorphTryFoldObjAsLclVar(GenTreeObj* obj, bool destroyNodes = true);
GenTree* fgMorphCommutative(GenTreeOp* tree);
GenTree* fgMorphCastedBitwiseOp(GenTreeOp* tree);

Expand Down Expand Up @@ -6550,6 +6550,10 @@ class Compiler
void fgMarkAddressExposedLocals();
void fgMarkAddressExposedLocals(Statement* stmt);

PhaseStatus fgForwardSub();
bool fgForwardSub(BasicBlock* block);
bool fgForwardSub(Statement* statement);

static fgWalkPreFn fgUpdateSideEffectsPre;
static fgWalkPostFn fgUpdateSideEffectsPost;

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/compphases.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ CompPhaseNameMacro(PHASE_UPDATE_FINALLY_FLAGS, "Update finally target flags",
CompPhaseNameMacro(PHASE_COMPUTE_PREDS, "Compute preds", "PREDS", false, -1, false)
CompPhaseNameMacro(PHASE_EARLY_UPDATE_FLOW_GRAPH,"Update flow graph early pass", "UPD-FG-E", false, -1, false)
CompPhaseNameMacro(PHASE_STR_ADRLCL, "Morph - Structs/AddrExp", "MOR-STRAL",false, -1, false)
CompPhaseNameMacro(PHASE_FWD_SUB, "Forward Sub", "FWD-SUB", false, -1, false)
CompPhaseNameMacro(PHASE_MORPH_IMPBYREF, "Morph - ByRefs", "MOR-BYREF",false, -1, false)
CompPhaseNameMacro(PHASE_PROMOTE_STRUCTS, "Morph - Promote Structs", "PROMOTER" ,false, -1, false)
CompPhaseNameMacro(PHASE_MORPH_GLOBAL, "Morph - Global", "MOR-GLOB", false, -1, false)
Expand Down
Loading

0 comments on commit 86992b6

Please sign in to comment.