package org.graalvm.compiler.core.test;
import static org.graalvm.compiler.graph.test.matchers.NodeIterableCount.hasCount;
import static org.graalvm.compiler.graph.test.matchers.NodeIterableIsEmpty.isEmpty;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.nodes.LoopBeginNode;
import org.graalvm.compiler.nodes.ParameterNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.extended.IntegerSwitchNode;
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
import org.graalvm.compiler.phases.tiers.HighTierContext;
import org.junit.Test;
import jdk.vm.ci.meta.JavaKind;
public class SwitchDyingLoopTest extends GraalCompilerTest {
@SuppressWarnings("fallthrough")
public static int snippet(int a, int n) {
int r = 0;
loop: for (int i = 0; i < n; i++) {
int v = (i * 167 + 13) & 0xff;
switch (v & a) {
case 0x80:
r += 1;
case 0x40:
r += 2;
case 0x20:
r += 3;
continue;
case 0x08:
r += 5;
case 0x04:
r += 7;
case 0x02:
r += 9;
default:
break loop;
}
}
return r;
}
@Test
public void test() {
CanonicalizerPhase canonicalizerPhase = new CanonicalizerPhase();
HighTierContext highTierContext = getDefaultHighTierContext();
StructuredGraph graph = parseEager("snippet", StructuredGraph.AllowAssumptions.YES);
assertThat(graph.getNodes(LoopBeginNode.TYPE), hasCount(1));
assertThat(graph.getNodes(IntegerSwitchNode.TYPE), hasCount(1));
canonicalizerPhase.apply(graph, highTierContext);
assertThat(graph.getNodes(LoopBeginNode.TYPE), hasCount(1));
assertThat(graph.getNodes(IntegerSwitchNode.TYPE), hasCount(1));
ParameterNode parameter = graph.getParameter(0);
assertNotNull(parameter);
parameter.setStamp(StampFactory.forInteger(JavaKind.Int, 0, 255, 0, 0xf));
canonicalizerPhase.apply(graph, highTierContext);
assertThat(graph.getNodes(LoopBeginNode.TYPE), isEmpty());
assertThat(graph.getNodes(IntegerSwitchNode.TYPE), hasCount(1));
}
}