Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51a694b

Browse files
JulieLeeMSFTewhapdxEgorBo
authored
Optimize if range check to replace jumps to bit operation (#87656)
Co-authored-by: Julie Lee <[email protected]> Co-authored-by: EgorBo <[email protected]>
1 parent 1972683 commit 51a694b

File tree

9 files changed

+558
-7
lines changed

9 files changed

+558
-7
lines changed

src/coreclr/jit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ set( JIT_SOURCES
151151
objectalloc.cpp
152152
optcse.cpp
153153
optimizebools.cpp
154+
switchrecognition.cpp
154155
optimizer.cpp
155156
patchpoint.cpp
156157
phase.cpp

src/coreclr/jit/block.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,6 @@ bool BasicBlock::IsLIR() const
792792
//------------------------------------------------------------------------
793793
// firstStmt: Returns the first statement in the block
794794
//
795-
// Arguments:
796-
// None.
797-
//
798795
// Return Value:
799796
// The first statement in the block's bbStmtList.
800797
//
@@ -804,10 +801,18 @@ Statement* BasicBlock::firstStmt() const
804801
}
805802

806803
//------------------------------------------------------------------------
807-
// lastStmt: Returns the last statement in the block
804+
// hasSingleStmt: Returns true if block has a single statement
808805
//
809-
// Arguments:
810-
// None.
806+
// Return Value:
807+
// true if block has a single statement, false otherwise
808+
//
809+
bool BasicBlock::hasSingleStmt() const
810+
{
811+
return (firstStmt() != nullptr) && (firstStmt() == lastStmt());
812+
}
813+
814+
//------------------------------------------------------------------------
815+
// lastStmt: Returns the last statement in the block
811816
//
812817
// Return Value:
813818
// The last statement in the block's bbStmtList.

src/coreclr/jit/block.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ struct BasicBlock : private LIR::Range
11271127

11281128
Statement* firstStmt() const;
11291129
Statement* lastStmt() const;
1130+
bool hasSingleStmt() const;
11301131

11311132
// Statements: convenience method for enabling range-based `for` iteration over the statement list, e.g.:
11321133
// for (Statement* const stmt : block->Statements())

src/coreclr/jit/compiler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5053,6 +5053,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
50535053
// Optimize block order
50545054
//
50555055
DoPhase(this, PHASE_OPTIMIZE_LAYOUT, &Compiler::optOptimizeLayout);
5056+
5057+
// Conditional to Switch conversion
5058+
//
5059+
DoPhase(this, PHASE_SWITCH_RECOGNITION, &Compiler::optSwitchRecognition);
50565060
}
50575061

50585062
// Determine start of cold region if we are hot/cold splitting

src/coreclr/jit/compiler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6325,8 +6325,10 @@ class Compiler
63256325

63266326
public:
63276327
PhaseStatus optOptimizeBools();
6328+
PhaseStatus optSwitchRecognition();
6329+
bool optSwitchConvert(BasicBlock* firstBlock, int testsCount, ssize_t* testValues, GenTree* nodeToTest);
6330+
bool optSwitchDetectAndConvert(BasicBlock* firstBlock);
63286331

6329-
public:
63306332
PhaseStatus optInvertLoops(); // Invert loops so they're entered at top and tested at bottom.
63316333
PhaseStatus optOptimizeFlow(); // Simplify flow graph and do tail duplication
63326334
PhaseStatus optOptimizeLayout(); // Optimize the BasicBlock layout of the method

src/coreclr/jit/compphases.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ CompPhaseNameMacro(PHASE_MORPH_MDARR, "Morph array ops",
7272
CompPhaseNameMacro(PHASE_HOIST_LOOP_CODE, "Hoist loop code", false, -1, false)
7373
CompPhaseNameMacro(PHASE_MARK_LOCAL_VARS, "Mark local vars", false, -1, false)
7474
CompPhaseNameMacro(PHASE_OPTIMIZE_BOOLS, "Optimize bools", false, -1, false)
75+
CompPhaseNameMacro(PHASE_SWITCH_RECOGNITION, "Recognize Switch", false, -1, false)
7576
CompPhaseNameMacro(PHASE_FIND_OPER_ORDER, "Find oper order", false, -1, false)
7677
CompPhaseNameMacro(PHASE_SET_BLOCK_ORDER, "Set block order", false, -1, true)
7778
CompPhaseNameMacro(PHASE_BUILD_SSA, "Build SSA representation", true, -1, false)

0 commit comments

Comments
 (0)