package org.graalvm.compiler.jtt.optimize;
import java.util.ListIterator;
import org.junit.Test;
import org.graalvm.compiler.jtt.JTTTest;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.virtual.CommitAllocationNode;
import org.graalvm.compiler.phases.BasePhase;
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
import org.graalvm.compiler.phases.tiers.HighTierContext;
import org.graalvm.compiler.phases.tiers.Suites;
import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
public class NestedLoop_EA extends JTTTest {
@Override
protected Suites createSuites() {
Suites suites = super.createSuites();
ListIterator<BasePhase<? super HighTierContext>> position = suites.getHighTier().findPhase(PartialEscapePhase.class);
CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
position.add(canonicalizer);
position.add(new PartialEscapePhase(true, canonicalizer));
return suites;
}
static class Frame {
Object[] objects = new Object[10];
}
static final int RESULT_SLOT = 0;
static final int K_SLOT = 1;
static final int I_SLOT = 2;
static final int ARG_SLOT = 3;
static final int STACK_BASE = 4;
static class Pointer {
public int sp = STACK_BASE;
}
public static int simpleLoop(int arg) {
Frame f = new Frame();
Pointer p = new Pointer();
f.objects[ARG_SLOT] = arg;
f.objects[RESULT_SLOT] = 0;
f.objects[K_SLOT] = 0;
for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) {
f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5;
f.objects[++p.sp] = f.objects[K_SLOT];
f.objects[++p.sp] = 1;
int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1];
p.sp--;
f.objects[p.sp] = result;
f.objects[K_SLOT] = (int) f.objects[p.sp];
p.sp--;
}
return (int) f.objects[RESULT_SLOT];
}
@Test
public void run0() throws Throwable {
runTest("simpleLoop", 5);
}
public static int nestedLoop(int arg) {
Frame f = new Frame();
Pointer p = new Pointer();
f.objects[ARG_SLOT] = arg;
f.objects[RESULT_SLOT] = 0;
f.objects[K_SLOT] = 0;
for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) {
f.objects[I_SLOT] = 0;
for (; (int) f.objects[I_SLOT] < (int) f.objects[ARG_SLOT];) {
f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5;
f.objects[++p.sp] = f.objects[I_SLOT];
f.objects[++p.sp] = 1;
int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1];
p.sp--;
f.objects[p.sp] = result;
f.objects[I_SLOT] = (int) f.objects[p.sp];
p.sp--;
}
f.objects[++p.sp] = f.objects[K_SLOT];
f.objects[++p.sp] = 1;
int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1];
p.sp--;
f.objects[p.sp] = result;
f.objects[K_SLOT] = (int) f.objects[p.sp];
p.sp--;
}
return (int) f.objects[RESULT_SLOT];
}
@Test
public void run1() throws Throwable {
runTest("nestedLoop", 5);
}
@Override
protected boolean checkHighTierGraph(StructuredGraph graph) {
assert graph.getNodes().filter(CommitAllocationNode.class).count() == 0 : "all allocations should be virtualized";
return true;
}
}