Skip to content

Commit

Permalink
Optimize if range check to replace jumps to bit operation (dotnet#87656)
Browse files Browse the repository at this point in the history
Co-authored-by: Julie Lee <[email protected]>
Co-authored-by: EgorBo <[email protected]>
  • Loading branch information
3 people authored Sep 17, 2023
1 parent 1972683 commit 51a694b
Show file tree
Hide file tree
Showing 9 changed files with 558 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/coreclr/jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ set( JIT_SOURCES
objectalloc.cpp
optcse.cpp
optimizebools.cpp
switchrecognition.cpp
optimizer.cpp
patchpoint.cpp
phase.cpp
Expand Down
17 changes: 11 additions & 6 deletions src/coreclr/jit/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,6 @@ bool BasicBlock::IsLIR() const
//------------------------------------------------------------------------
// firstStmt: Returns the first statement in the block
//
// Arguments:
// None.
//
// Return Value:
// The first statement in the block's bbStmtList.
//
Expand All @@ -804,10 +801,18 @@ Statement* BasicBlock::firstStmt() const
}

//------------------------------------------------------------------------
// lastStmt: Returns the last statement in the block
// hasSingleStmt: Returns true if block has a single statement
//
// Arguments:
// None.
// Return Value:
// true if block has a single statement, false otherwise
//
bool BasicBlock::hasSingleStmt() const
{
return (firstStmt() != nullptr) && (firstStmt() == lastStmt());
}

//------------------------------------------------------------------------
// lastStmt: Returns the last statement in the block
//
// Return Value:
// The last statement in the block's bbStmtList.
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ struct BasicBlock : private LIR::Range

Statement* firstStmt() const;
Statement* lastStmt() const;
bool hasSingleStmt() const;

// Statements: convenience method for enabling range-based `for` iteration over the statement list, e.g.:
// for (Statement* const stmt : block->Statements())
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5053,6 +5053,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
// Optimize block order
//
DoPhase(this, PHASE_OPTIMIZE_LAYOUT, &Compiler::optOptimizeLayout);

// Conditional to Switch conversion
//
DoPhase(this, PHASE_SWITCH_RECOGNITION, &Compiler::optSwitchRecognition);
}

// Determine start of cold region if we are hot/cold splitting
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6325,8 +6325,10 @@ class Compiler

public:
PhaseStatus optOptimizeBools();
PhaseStatus optSwitchRecognition();
bool optSwitchConvert(BasicBlock* firstBlock, int testsCount, ssize_t* testValues, GenTree* nodeToTest);
bool optSwitchDetectAndConvert(BasicBlock* firstBlock);

public:
PhaseStatus optInvertLoops(); // Invert loops so they're entered at top and tested at bottom.
PhaseStatus optOptimizeFlow(); // Simplify flow graph and do tail duplication
PhaseStatus optOptimizeLayout(); // Optimize the BasicBlock layout of the method
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 @@ -72,6 +72,7 @@ CompPhaseNameMacro(PHASE_MORPH_MDARR, "Morph array ops",
CompPhaseNameMacro(PHASE_HOIST_LOOP_CODE, "Hoist loop code", false, -1, false)
CompPhaseNameMacro(PHASE_MARK_LOCAL_VARS, "Mark local vars", false, -1, false)
CompPhaseNameMacro(PHASE_OPTIMIZE_BOOLS, "Optimize bools", false, -1, false)
CompPhaseNameMacro(PHASE_SWITCH_RECOGNITION, "Recognize Switch", false, -1, false)
CompPhaseNameMacro(PHASE_FIND_OPER_ORDER, "Find oper order", false, -1, false)
CompPhaseNameMacro(PHASE_SET_BLOCK_ORDER, "Set block order", false, -1, true)
CompPhaseNameMacro(PHASE_BUILD_SSA, "Build SSA representation", true, -1, false)
Expand Down
Loading

0 comments on commit 51a694b

Please sign in to comment.