001/*
002 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.jtt.optimize;
024
025import java.util.*;
026
027import org.junit.*;
028
029import com.oracle.graal.jtt.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.virtual.*;
032import com.oracle.graal.phases.*;
033import com.oracle.graal.phases.common.*;
034import com.oracle.graal.phases.tiers.*;
035import com.oracle.graal.virtual.phases.ea.*;
036
037public class NestedLoop_EA extends JTTTest {
038
039    @Override
040    protected Suites createSuites() {
041        Suites suites = super.createSuites();
042        ListIterator<BasePhase<? super HighTierContext>> position = suites.getHighTier().findPhase(PartialEscapePhase.class);
043        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
044        // incremental canonicalizer of PEA is missing some important canonicalization (TODO?)
045        position.add(canonicalizer);
046        position.add(new PartialEscapePhase(true, canonicalizer));
047        return suites;
048    }
049
050    static class Frame {
051        Object[] objects = new Object[10];
052    }
053
054    static final int RESULT_SLOT = 0;
055    static final int K_SLOT = 1;
056    static final int I_SLOT = 2;
057    static final int ARG_SLOT = 3;
058    static final int STACK_BASE = 4;
059
060    static class Pointer {
061        public int sp = STACK_BASE;
062    }
063
064    public static int simpleLoop(int arg) {
065        Frame f = new Frame();
066        Pointer p = new Pointer();
067        f.objects[ARG_SLOT] = arg;
068        f.objects[RESULT_SLOT] = 0;
069        f.objects[K_SLOT] = 0;
070        for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) {
071
072            f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5;
073
074            f.objects[++p.sp] = f.objects[K_SLOT];
075            f.objects[++p.sp] = 1;
076            int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1];
077            p.sp--;
078            f.objects[p.sp] = result;
079            f.objects[K_SLOT] = (int) f.objects[p.sp];
080            p.sp--;
081        }
082        return (int) f.objects[RESULT_SLOT];
083    }
084
085    @Test
086    public void run0() throws Throwable {
087        runTest("simpleLoop", 5);
088    }
089
090    public static int nestedLoop(int arg) {
091        Frame f = new Frame();
092        Pointer p = new Pointer();
093        f.objects[ARG_SLOT] = arg;
094        f.objects[RESULT_SLOT] = 0;
095        f.objects[K_SLOT] = 0;
096        for (; (int) f.objects[K_SLOT] < (int) f.objects[ARG_SLOT];) {
097
098            f.objects[I_SLOT] = 0;
099            for (; (int) f.objects[I_SLOT] < (int) f.objects[ARG_SLOT];) {
100                f.objects[RESULT_SLOT] = (int) f.objects[RESULT_SLOT] + 5;
101
102                f.objects[++p.sp] = f.objects[I_SLOT];
103                f.objects[++p.sp] = 1;
104                int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1];
105                p.sp--;
106                f.objects[p.sp] = result;
107                f.objects[I_SLOT] = (int) f.objects[p.sp];
108                p.sp--;
109            }
110
111            f.objects[++p.sp] = f.objects[K_SLOT];
112            f.objects[++p.sp] = 1;
113            int result = (int) f.objects[p.sp] + (int) f.objects[p.sp - 1];
114            p.sp--;
115            f.objects[p.sp] = result;
116            f.objects[K_SLOT] = (int) f.objects[p.sp];
117            p.sp--;
118        }
119        return (int) f.objects[RESULT_SLOT];
120    }
121
122    @Test
123    public void run1() throws Throwable {
124        runTest("nestedLoop", 5);
125    }
126
127    @Override
128    protected boolean checkHighTierGraph(StructuredGraph graph) {
129        assert graph.getNodes().filter(CommitAllocationNode.class).count() == 0 : "all allocations should be virtualized";
130        return true;
131    }
132}