1 /*
   2  * Copyright (c) 2012, 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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import java.lang.reflect.Array;
  28 
  29 import jdk.vm.ci.meta.ResolvedJavaMethod;
  30 
  31 import org.junit.Test;
  32 
  33 import org.graalvm.compiler.phases.common.AbstractInliningPhase;
  34 
  35 /**
  36  * Tests any optimization that commons loads of non-inlineable constants.
  37  */
  38 public class CommonedConstantsTest extends GraalCompilerTest {
  39 
  40     public static final String[] array = {"1", "2", null};
  41 
  42     // A method where a constant is used on the normal and exception edge of a non-inlined call.
  43     // The dominating block of both usages is the block containing the call.
  44     public static Object test0Snippet(String[] arr, int i) {
  45         Object result = null;
  46         try {
  47             result = Array.get(arr, i);
  48         } catch (ArrayIndexOutOfBoundsException e) {
  49             result = array[0];
  50         }
  51         if (result == null) {
  52             result = array[2];
  53         }
  54         return result;
  55     }
  56 
  57     @Test
  58     public void test0() {
  59         // Ensure the exception path is profiled
  60         ResolvedJavaMethod javaMethod = getResolvedJavaMethod("test0Snippet");
  61         javaMethod.reprofile();
  62         test0Snippet(array, array.length);
  63 
  64         test("test0Snippet", array, 0);
  65         test("test0Snippet", array, 2);
  66         test("test0Snippet", array, 3);
  67         test("test0Snippet", array, 1);
  68     }
  69 
  70     public static final char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
  71 
  72     static int noninlineLength(char[] s) {
  73         return s.length;
  74     }
  75 
  76     /**
  77      * A constant with usages before and after a non-inlined call.
  78      */
  79     public static int test1Snippet(String s) {
  80         if (s == null) {
  81             return noninlineLength(alphabet) + 1;
  82         }
  83         char[] sChars = s.toCharArray();
  84         int count = 0;
  85         for (int i = 0; i < alphabet.length && i < sChars.length; i++) {
  86             if (alphabet[i] == sChars[i]) {
  87                 count++;
  88             }
  89         }
  90         return count;
  91     }
  92 
  93     @Test
  94     public void test1() {
  95         createSuites(getInitialOptions()).getHighTier().findPhase(AbstractInliningPhase.class).remove();
  96         test1Snippet(new String(alphabet));
  97 
  98         test("test1Snippet", (Object) null);
  99         test("test1Snippet", "test1Snippet");
 100         test("test1Snippet", "");
 101     }
 102 
 103     /**
 104      * A constant with only usage in a loop.
 105      */
 106     public static int test2Snippet(String s) {
 107         char[] sChars = s.toCharArray();
 108         int count = 0;
 109         for (int i = 0; i < alphabet.length && i < sChars.length; i++) {
 110             if (alphabet[i] == sChars[i]) {
 111                 count++;
 112             }
 113         }
 114         return count;
 115     }
 116 
 117     @Test
 118     public void test2() {
 119         assert createSuites(getInitialOptions()).getHighTier().findPhase(AbstractInliningPhase.class).hasNext();
 120         test2Snippet(new String(alphabet));
 121 
 122         test("test2Snippet", (Object) null);
 123         test("test2Snippet", "test1Snippet");
 124         test("test2Snippet", "");
 125     }
 126 }