1 /* 2 * Copyright (c) 2011, 2019, 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 24 25 package org.graalvm.compiler.jtt.optimize; 26 27 import java.util.ListIterator; 28 29 import org.junit.Test; 30 31 import org.graalvm.compiler.jtt.JTTTest; 32 import org.graalvm.compiler.nodes.StructuredGraph; 33 import org.graalvm.compiler.nodes.virtual.CommitAllocationNode; 34 import org.graalvm.compiler.options.OptionValues; 35 import org.graalvm.compiler.phases.BasePhase; 36 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 37 import org.graalvm.compiler.phases.tiers.HighTierContext; 38 import org.graalvm.compiler.phases.tiers.Suites; 39 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase; 40 41 public class NestedLoop_EA extends JTTTest { 42 43 @Override 44 protected Suites createSuites(OptionValues options) { 45 Suites suites = super.createSuites(options); 46 ListIterator<BasePhase<? super HighTierContext>> position = suites.getHighTier().findPhase(PartialEscapePhase.class); 47 CanonicalizerPhase canonicalizer = new CanonicalizerPhase(); 48 // incremental canonicalizer of PEA is missing some important canonicalization (TODO?) 49 position.add(canonicalizer); 50 position.add(new PartialEscapePhase(true, canonicalizer, options)); 51 return suites; 52 } 53 54 static class Frame { 55 Object[] objects = new Object[10]; 56 } 57 58 static final int RESULT_SLOT = 0; 59 static final int K_SLOT = 1; 60 static final int I_SLOT = 2; 61 static final int ARG_SLOT = 3; 62 static final int STACK_BASE = 4; 63 64 static class Pointer { 65 public int sp = STACK_BASE; 66 } 67 68 public static int simpleLoop(int arg) { 69 Frame f = new Frame(); 70 Pointer p = new Pointer(); 71 f.objects[ARG_SLOT] = arg; 72 f.objects[RESULT_SLOT] = 0; 73 f.objects[K_SLOT] = 0; 74 for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) { 75 76 f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5; 77 78 f.objects[++p.sp] = f.objects[K_SLOT]; 79 f.objects[++p.sp] = 1; 80 int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1]; 81 p.sp--; 82 f.objects[p.sp] = result; 83 f.objects[K_SLOT] = (int) f.objects[p.sp]; 84 p.sp--; 85 } 86 return (int) f.objects[RESULT_SLOT]; 87 } 88 89 @Test 90 public void run0() throws Throwable { 91 runTest("simpleLoop", 5); 92 } 93 94 public static int nestedLoop(int arg) { 95 Frame f = new Frame(); 96 Pointer p = new Pointer(); 97 f.objects[ARG_SLOT] = arg; 98 f.objects[RESULT_SLOT] = 0; 99 f.objects[K_SLOT] = 0; 100 for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) { 101 102 f.objects[I_SLOT] = 0; 103 for (; (int) f.objects[I_SLOT] < (int) f.objects[ARG_SLOT];) { 104 f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5; 105 106 f.objects[++p.sp] = f.objects[I_SLOT]; 107 f.objects[++p.sp] = 1; 108 int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1]; 109 p.sp--; 110 f.objects[p.sp] = result; 111 f.objects[I_SLOT] = (int) f.objects[p.sp]; 112 p.sp--; 113 } 114 115 f.objects[++p.sp] = f.objects[K_SLOT]; 116 f.objects[++p.sp] = 1; 117 int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1]; 118 p.sp--; 119 f.objects[p.sp] = result; 120 f.objects[K_SLOT] = (int) f.objects[p.sp]; 121 p.sp--; 122 } 123 return (int) f.objects[RESULT_SLOT]; 124 } 125 126 @Test 127 public void run1() throws Throwable { 128 runTest("nestedLoop", 5); 129 } 130 131 @Override 132 protected void checkHighTierGraph(StructuredGraph graph) { 133 assert graph.getNodes().filter(CommitAllocationNode.class).count() == 0 : "all allocations should be virtualized"; 134 } 135 }