Skip to content

Commit

Permalink
JIT: implement tail merging (#77103)
Browse files Browse the repository at this point in the history
Add a phase that looks for common tail statements in a block's
predecessors and merges them.

Run it both before and after morph.

Also
* add range enable config and overall `JitEnableTailMerge` config
* add indir flag checking to `GenTree::Compare`
* remove an apparently unnecessary assert from loop recognition.

Closes #8795.
Closes #76872.
  • Loading branch information
AndyAyersMS authored Oct 25, 2022
1 parent e468b22 commit 9b16818
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4549,6 +4549,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
if (opts.OptimizationEnabled())
{
// Tail merge
//
DoPhase(this, PHASE_TAIL_MERGE, &Compiler::fgTailMerge);

// Merge common throw blocks
//
DoPhase(this, PHASE_MERGE_THROWS, &Compiler::fgTailMergeThrows);
Expand Down Expand Up @@ -4650,6 +4654,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_OPTIMIZE_FLOW, &Compiler::optOptimizeFlow);

// Second pass of tail merge
//
DoPhase(this, PHASE_TAIL_MERGE2, &Compiler::fgTailMerge);

// Compute reachability sets and dominators.
//
DoPhase(this, PHASE_COMPUTE_REACHABILITY, &Compiler::fgComputeReachability);
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5224,6 +5224,8 @@ class Compiler

void fgMoveBlocksAfter(BasicBlock* bStart, BasicBlock* bEnd, BasicBlock* insertAfterBlk);

PhaseStatus fgTailMerge();

enum FG_RELOCATE_TYPE
{
FG_RELOCATE_TRY, // relocate the 'try' region
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/compphases.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ CompPhaseNameMacro(PHASE_COMPUTE_EDGE_WEIGHTS, "Compute edge weights (1, f
#if defined(FEATURE_EH_FUNCLETS)
CompPhaseNameMacro(PHASE_CREATE_FUNCLETS, "Create EH funclets", false, -1, false)
#endif // FEATURE_EH_FUNCLETS
CompPhaseNameMacro(PHASE_TAIL_MERGE, "Tail merge", false, -1, false)
CompPhaseNameMacro(PHASE_MERGE_THROWS, "Merge throw blocks", false, -1, false)
CompPhaseNameMacro(PHASE_INVERT_LOOPS, "Invert loops", false, -1, false)
CompPhaseNameMacro(PHASE_TAIL_MERGE2, "Post-morph tail merge", false, -1, false)
CompPhaseNameMacro(PHASE_OPTIMIZE_FLOW, "Optimize control flow", false, -1, false)
CompPhaseNameMacro(PHASE_OPTIMIZE_LAYOUT, "Optimize layout", false, -1, false)
CompPhaseNameMacro(PHASE_COMPUTE_REACHABILITY, "Compute blocks reachability", false, -1, false)
Expand Down
Loading

0 comments on commit 9b16818

Please sign in to comment.