1 /* 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.jtt.optimize; 24 25 import java.util.ListIterator; 26 27 import org.junit.Test; 28 29 import org.graalvm.compiler.jtt.JTTTest; 30 import org.graalvm.compiler.nodes.StructuredGraph; 31 import org.graalvm.compiler.nodes.virtual.CommitAllocationNode; 32 import org.graalvm.compiler.phases.BasePhase; 33 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 34 import org.graalvm.compiler.phases.tiers.HighTierContext; 35 import org.graalvm.compiler.phases.tiers.Suites; 36 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase; 37 38 public class NestedLoop_EA extends JTTTest { 39 40 @Override 41 protected Suites createSuites() { 42 Suites suites = super.createSuites(); 43 ListIterator<BasePhase<? super HighTierContext>> position = suites.getHighTier().findPhase(PartialEscapePhase.class); 44 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 45 // incremental canonicalizer of PEA is missing some important canonicalization (TODO?) 46 position.add(canonicalizer); 47 position.add(new PartialEscapePhase(true, canonicalizer)); 48 return suites; 49 } 50 51 static class Frame { 52 Object[] objects = new Object[10]; 53 } 54 55 static final int RESULT_SLOT = 0; 56 static final int K_SLOT = 1; 57 static final int I_SLOT = 2; 58 static final int ARG_SLOT = 3; 59 static final int STACK_BASE = 4; 60 61 static class Pointer { 62 public int sp = STACK_BASE; 63 } 64 65 public static int simpleLoop(int arg) { 66 Frame f = new Frame(); 67 Pointer p = new Pointer(); 68 f.objects[ARG_SLOT] = arg; 69 f.objects[RESULT_SLOT] = 0; 70 f.objects[K_SLOT] = 0; 71 for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) { 72 73 f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5; 74 75 f.objects[++p.sp] = f.objects[K_SLOT]; 76 f.objects[++p.sp] = 1; 77 int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1]; 78 p.sp--; 79 f.objects[p.sp] = result; 80 f.objects[K_SLOT] = (int) f.objects[p.sp]; 81 p.sp--; 82 } 83 return (int) f.objects[RESULT_SLOT]; 84 } 85 86 @Test 87 public void run0() throws Throwable { 88 runTest("simpleLoop", 5); 89 } 90 91 public static int nestedLoop(int arg) { 92 Frame f = new Frame(); 93 Pointer p = new Pointer(); 94 f.objects[ARG_SLOT] = arg; 95 f.objects[RESULT_SLOT] = 0; 96 f.objects[K_SLOT] = 0; 97 for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) { 98 99 f.objects[I_SLOT] = 0; 100 for (; (int) f.objects[I_SLOT] < (int) f.objects[ARG_SLOT];) { 101 f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5; 102 103 f.objects[++p.sp] = f.objects[I_SLOT]; 104 f.objects[++p.sp] = 1; 105 int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1]; 106 p.sp--; 107 f.objects[p.sp] = result; 108 f.objects[I_SLOT] = (int) f.objects[p.sp]; 109 p.sp--; 110 } 111 112 f.objects[++p.sp] = f.objects[K_SLOT]; 113 f.objects[++p.sp] = 1; 114 int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1]; 115 p.sp--; 116 f.objects[p.sp] = result; 117 f.objects[K_SLOT] = (int) f.objects[p.sp]; 118 p.sp--; 119 } 120 return (int) f.objects[RESULT_SLOT]; 121 } 122 123 @Test 124 public void run1() throws Throwable { 125 runTest("nestedLoop", 5); 126 } 127 128 @Override 129 protected boolean checkHighTierGraph(StructuredGraph graph) { 130 assert graph.getNodes().filter(CommitAllocationNode.class).count() == 0 : "all allocations should be virtualized"; 131 return true; 132 } 133 }