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