Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ private Val tryRemoveTrivialPhi(Phi phi) {
phis.remove(phi);
}
for (Phi user : phiUsers) {
tryRemoveTrivialPhi(user);
Val res = tryRemoveTrivialPhi(user);
if (same == user) {
same = res;
}
}
return same;
}
Expand Down
172 changes: 172 additions & 0 deletions test/jdk/java/lang/reflect/code/TestSSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,178 @@ public void testNestedLambdaCapture() {
Assert.assertEquals((int) Interpreter.invoke(MethodHandles.lookup(), lf, 10), nestedLambdaCapture(10));
}

@CodeReflection
static int deadCode(int n) {
int factorial = 1;
int unused = factorial;
int unusedLoop = 0;
for (int i = 0; i < 4; i++) {
unusedLoop++;
}
while (n > 0) {
factorial *= n;
n--;
if (factorial == 0) {
factorial = -1;
int unusedNested = factorial;
}
}
return factorial;
}

@Test
public void testDeadCode() {
CoreOp.FuncOp f = getFuncOp("deadCode");

CoreOp.FuncOp lf = generate(f);

Assert.assertEquals((int) Interpreter.invoke(MethodHandles.lookup(), lf, 10), deadCode(10));
}

@CodeReflection
static int ifelseLoopNested(int n) {
int counter = 10;
while (n > 0) {
if (n % 2 == 0) {
int sum = n;
for (int i = 0; i < 5; i++) {
if (sum > n / 2) {
sum -= i;
} else {
sum += i;
break;
}
}
n += sum;
} else {
int difference = (n % 3 == 0) ? n / 2 : n * 2;
n -= difference;
}
counter--;
}
return n;
}

@Test
public void testIfelseLoopNested() {
CoreOp.FuncOp f = getFuncOp("ifelseLoopNested");

CoreOp.FuncOp lf = generate(f);

Assert.assertEquals((int) Interpreter.invoke(MethodHandles.lookup(), lf, 10), ifelseLoopNested(10));
}

@CodeReflection
static int violaJones(int x, int maxX, int length, int integral) {
int scale = 0;
scale++;
while (x > scale && scale < length) {
}
for (int i = 0; i < integral; i++) {
scale--;
}
return scale;
}

@Test
public void testViolaJones() {
CoreOp.FuncOp f = getFuncOp("violaJones");

CoreOp.FuncOp lf = generate(f);

Assert.assertEquals((int) Interpreter.invoke(MethodHandles.lookup(), lf, 0, 1, 0, 0), violaJones(0, 1, 0, 0));
}

@CodeReflection
static int violaJonesTwo(int x, int maxX, int length, int integral) {
int scale = 0, scale_extra = 1;
scale++;
int j = 9;
while (x > scale && scale < length) {
j = 1;
}
for (int i = 0; i < integral; i++) {
scale--;
}
return scale + scale_extra + j;
}

@Test
public void testViolaJonesTwo() {
CoreOp.FuncOp f = getFuncOp("violaJonesTwo");

CoreOp.FuncOp lf = generate(f);

Assert.assertEquals((int) Interpreter.invoke(MethodHandles.lookup(), lf, 0, 1, 0, 0), violaJonesTwo(0, 1, 0, 0));
}

@CodeReflection
static boolean binarySearch(int[] arr, int target) {
int l = 0;
int r = arr.length - 1;
while (l < r) {
int m = (r - l) / 2;
m += l;
if (arr[m] < target) {
l = m + 1;
} else if (arr[m] > target) {
r = m - 1;
} else {
return true;
}
}
return false;
}

@Test
public void testBinarySearch() {
CoreOp.FuncOp f = getFuncOp("binarySearch");

CoreOp.FuncOp lf = generate(f);

int[] arr = new int[]{1, 2, 4, 7, 11, 19, 21, 29, 30, 36};

Assert.assertEquals((boolean) Interpreter.invoke(MethodHandles.lookup(), lf, arr, 4), binarySearch(arr, 4));
}

@CodeReflection
static void quicksort(int[] arr, int lo, int hi) {
if (lo >= hi || lo < 0) {
return;
}

int pivot = arr[hi];
int i = lo;
for (int j = lo; j < hi; j++) {
if (arr[j] <= pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[hi];
arr[hi] = temp;

quicksort(arr, lo, i - 1);
quicksort(arr, i + 1, hi);
}

@Test
public void testQuicksort() {
CoreOp.FuncOp f = getFuncOp("quicksort");

CoreOp.FuncOp lf = generate(f);

int[] arr1 = new int[]{5, 2, 7, 45, 34, 14, 0, 27, 43, 11, 38, 56, 81};
int[] arr2 = new int[]{2, 11, 45, 34, 0, 27, 38, 56, 7, 43, 14, 5, 81};

Interpreter.invoke(MethodHandles.lookup(), lf, arr1, 0, arr1.length - 1);
quicksort(arr2, 0, arr2.length - 1);
Assert.assertEquals(arr1, arr2);
}

static CoreOp.FuncOp generate(CoreOp.FuncOp f) {
System.out.println(f.toText());

Expand Down