001/*
002 * Copyright (c) 2012, 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.compiler.test;
024
025import java.lang.reflect.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import org.junit.*;
030
031import com.oracle.graal.phases.common.*;
032
033/**
034 * Tests any optimization that commons loads of non-inlineable constants.
035 */
036public class CommonedConstantsTest extends GraalCompilerTest {
037
038    public static final String[] array = {"1", "2", null};
039
040    // A method where a constant is used on the normal and exception edge of a non-inlined call.
041    // The dominating block of both usages is the block containing the call.
042    public static Object test0Snippet(String[] arr, int i) {
043        Object result = null;
044        try {
045            result = Array.get(arr, i);
046        } catch (ArrayIndexOutOfBoundsException e) {
047            result = array[0];
048        }
049        if (result == null) {
050            result = array[2];
051        }
052        return result;
053    }
054
055    @Test
056    public void test0() {
057        // Ensure the exception path is profiled
058        ResolvedJavaMethod javaMethod = getResolvedJavaMethod("test0Snippet");
059        javaMethod.reprofile();
060        test0Snippet(array, array.length);
061
062        test("test0Snippet", array, 0);
063        test("test0Snippet", array, 2);
064        test("test0Snippet", array, 3);
065        test("test0Snippet", array, 1);
066    }
067
068    public static final char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
069
070    static int noninlineLength(char[] s) {
071        return s.length;
072    }
073
074    /**
075     * A constant with usages before and after a non-inlined call.
076     */
077    public static int test1Snippet(String s) {
078        if (s == null) {
079            return noninlineLength(alphabet) + 1;
080        }
081        char[] sChars = s.toCharArray();
082        int count = 0;
083        for (int i = 0; i < alphabet.length && i < sChars.length; i++) {
084            if (alphabet[i] == sChars[i]) {
085                count++;
086            }
087        }
088        return count;
089    }
090
091    @Test
092    public void test1() {
093        getSuites().getHighTier().findPhase(AbstractInliningPhase.class).remove();
094        test1Snippet(new String(alphabet));
095
096        test("test1Snippet", (Object) null);
097        test("test1Snippet", "test1Snippet");
098        test("test1Snippet", "");
099    }
100
101    /**
102     * A constant with only usage in a loop.
103     */
104    public static int test2Snippet(String s) {
105        char[] sChars = s.toCharArray();
106        int count = 0;
107        for (int i = 0; i < alphabet.length && i < sChars.length; i++) {
108            if (alphabet[i] == sChars[i]) {
109                count++;
110            }
111        }
112        return count;
113    }
114
115    @Test
116    public void test2() {
117        assert getSuites().getHighTier().findPhase(AbstractInliningPhase.class).hasNext();
118        test2Snippet(new String(alphabet));
119
120        test("test2Snippet", (Object) null);
121        test("test2Snippet", "test1Snippet");
122        test("test2Snippet", "");
123    }
124}