oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.37k stars 1.63k forks source link

Micro-optimization opportunity: Optimizing out default branch from RangeTableSwitchOp #8425

Open smarr opened 8 months ago

smarr commented 8 months ago

The RangeTableSwitchOp currently always emits a default branch.

This is not always necessary, for instance for bytecode interpreters that do not have a default branch, or for switch statements where a stamp indicates that the default branch is not reachable.

Though, the NodeLIRBuilder assumes for instance that a SwitchNode always has a defaultSuccessor(), which I naively read as the default case for a switch.

The default branch is currently generated unconditionally when emitting the code (AMD64, AArch64).

I would assume it can be omitted in cases where the compiler knows that it's not reachable. For instance based on the stamp of the value over which the switch operates.

On the current bytecode interpreter I looked at, removing the branch "works" and is "safe", however, except for saving the extra 2-3 instructions, it doesn't give a benefit. This might be different for switches in tighter loops though, or once we can generate tighter code for bytecode loops.

/cc @chumer @teshull

smarr commented 8 months ago

@fniephaus Just to note, this isn't solely for the benefit of Truffle interpreters. This is something for any switch/case on the Java level that compiles with the RangeTableSwitchOP strategy.

So, this would also be a benefit for Java compilation in general.

fniephaus commented 8 months ago

@smarr sure, @chumer also knows his way around Java compilation but fair enough.