package org.graalvm.compiler.hotspot.test;
import org.graalvm.compiler.api.directives.GraalDirectives;
import org.junit.Assert;
import org.junit.Test;
public class GraalOSRTest extends GraalOSRTestBase {
@Test
public void testOSR01() {
try {
testOSR(getInitialOptions(), "testReduceLoop");
} catch (Throwable t) {
Assert.assertEquals("OSR compilation without OSR entry loop.", t.getMessage());
}
}
@Test
public void testOSR02() {
testOSR(getInitialOptions(), "testSequentialLoop");
}
@Test
public void testOSR03() {
testOSR(getInitialOptions(), "testNonReduceLoop");
}
@Test
public void testOSR04() {
testOSR(getInitialOptions(), "testDeoptAfterCountedLoop");
}
static int limit = 10000;
public static int sideEffect;
public static ReturnValue testReduceLoop() {
for (int i = 0; i < limit * limit; i++) {
GraalDirectives.blackhole(i);
if (GraalDirectives.inCompiledCode()) {
return ReturnValue.SUCCESS;
}
}
return ReturnValue.FAILURE;
}
public static ReturnValue testSequentialLoop() {
ReturnValue ret = ReturnValue.FAILURE;
for (int i = 1; i < limit * limit; i++) {
GraalDirectives.blackhole(i);
if (i % 7 == 0) {
ret = ReturnValue.SUCCESS;
}
}
GraalDirectives.controlFlowAnchor();
if (sideEffect == 123) {
return ReturnValue.SIDE;
}
for (int i = 1; i < limit * limit; i++) {
GraalDirectives.blackhole(i);
if (i % 33 == 0) {
ret = ReturnValue.SUCCESS;
}
}
GraalDirectives.controlFlowAnchor();
return ret;
}
public static ReturnValue testNonReduceLoop() {
ReturnValue ret = ReturnValue.FAILURE;
for (int i = 0; i < limit * limit; i++) {
GraalDirectives.blackhole(i);
if (i % 33 == 0) {
ret = ReturnValue.SUCCESS;
}
}
GraalDirectives.controlFlowAnchor();
return ret;
}
public static ReturnValue testDeoptAfterCountedLoop() {
long ret = 0;
for (int i = 0; GraalDirectives.injectBranchProbability(1, i < limit * limit); i++) {
GraalDirectives.blackhole(i);
ret = GraalDirectives.opaque(i);
}
GraalDirectives.controlFlowAnchor();
return ret + 1 == limit * limit ? ReturnValue.SUCCESS : ReturnValue.FAILURE;
}
}