use of org.graalvm.compiler.lir.SwitchStrategy in project graal by oracle.
the class LIRGenerator method emitStrategySwitch.
@Override
public void emitStrategySwitch(JavaConstant[] keyConstants, double[] keyProbabilities, LabelRef[] keyTargets, LabelRef defaultTarget, Variable value) {
int keyCount = keyConstants.length;
SwitchStrategy strategy = SwitchStrategy.getBestStrategy(keyProbabilities, keyConstants, keyTargets);
long valueRange = keyConstants[keyCount - 1].asLong() - keyConstants[0].asLong() + 1;
double tableSwitchDensity = keyCount / (double) valueRange;
/*
* This heuristic tries to find a compromise between the effort for the best switch strategy
* and the density of a tableswitch. If the effort for the strategy is at least 4, then a
* tableswitch is preferred if better than a certain value that starts at 0.5 and lowers
* gradually with additional effort.
*/
if (strategy.getAverageEffort() < 4 || tableSwitchDensity < (1 / Math.sqrt(strategy.getAverageEffort()))) {
emitStrategySwitch(strategy, value, keyTargets, defaultTarget);
} else {
int minValue = keyConstants[0].asInt();
assert valueRange < Integer.MAX_VALUE;
LabelRef[] targets = new LabelRef[(int) valueRange];
for (int i = 0; i < valueRange; i++) {
targets[i] = defaultTarget;
}
for (int i = 0; i < keyCount; i++) {
targets[keyConstants[i].asInt() - minValue] = keyTargets[i];
}
emitTableSwitch(minValue, defaultTarget, targets, value);
}
}
Aggregations