Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 public class Main {
     19   public static void main(String[] args) {
     20     arraycopy();
     21     try {
     22       arraycopy(new Object());
     23       throw new Error("Should not be here");
     24     } catch (ArrayStoreException ase) {
     25       // Ignore.
     26     }
     27     try {
     28       arraycopy(null);
     29       throw new Error("Should not be here");
     30     } catch (NullPointerException npe) {
     31       // Ignore.
     32     }
     33 
     34     try {
     35       arraycopy(new Object[1]);
     36       throw new Error("Should not be here");
     37     } catch (ArrayIndexOutOfBoundsException aiooe) {
     38       // Ignore.
     39     }
     40 
     41     arraycopy(new Object[2]);
     42     arraycopy(new Object[2], 0);
     43 
     44     try {
     45       arraycopy(new Object[1], 1);
     46       throw new Error("Should not be here");
     47     } catch (ArrayIndexOutOfBoundsException aiooe) {
     48       // Ignore.
     49     }
     50   }
     51 
     52   /// CHECK-START-X86_64: void Main.arraycopy() disassembly (after)
     53   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
     54   /// CHECK-NOT:      test {{^[^\[].*}}, {{^[^\[].*}}
     55   /// CHECK-NOT:      call
     56   /// CHECK:          ReturnVoid
     57   // Checks that the call is intrinsified and that there is no register test instruction
     58   // when we know the source and destination are not null.
     59   public static void arraycopy() {
     60     Object[] obj = new Object[4];
     61     System.arraycopy(obj, 1, obj, 0, 1);
     62   }
     63 
     64   public static void arraycopy(Object obj) {
     65     System.arraycopy(obj, 1, obj, 0, 1);
     66   }
     67 
     68   // Test case for having enough registers on x86 for the arraycopy intrinsic.
     69   /// CHECK-START-X86: void Main.arraycopy(java.lang.Object[], int) disassembly (after)
     70   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
     71   /// CHECK-NOT:      mov {{[a-z]+}}, [esp + {{[0-9]+}}]
     72   /// CHECK:          ReturnVoid
     73   public static void arraycopy(Object[] obj, int pos) {
     74     System.arraycopy(obj, pos, obj, 0, obj.length);
     75   }
     76 
     77   // Test case for having enough registers on x86 for the arraycopy intrinsic
     78   // when an input is passed twice.
     79   /// CHECK-START-X86: int Main.arraycopy2(java.lang.Object[], int) disassembly (after)
     80   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
     81   /// CHECK-NOT:      mov {{[a-z]+}}, [esp + {{[0-9]+}}]
     82   /// CHECK:          Return
     83   public static int arraycopy2(Object[] obj, int pos) {
     84     System.arraycopy(obj, pos, obj, pos - 1, obj.length);
     85     return pos;
     86   }
     87 
     88   // Test case for not having enough registers on x86. The arraycopy intrinsic
     89   // will ask for length to be in stack and load it.
     90   /// CHECK-START-X86: int Main.arraycopy3(java.lang.Object[], java.lang.Object[], int, int, int) disassembly (after)
     91   /// CHECK:          InvokeStaticOrDirect intrinsic:SystemArrayCopy
     92   /// CHECK:          mov {{[a-z]+}}, [esp + {{[0-9]+}}]
     93   /// CHECK:          Return
     94   public static int arraycopy3(Object[] obj1, Object[] obj2, int input1, int input3, int input4) {
     95     System.arraycopy(obj1, input1, obj2, input3, input4);
     96     System.out.println(obj1);
     97     System.out.println(obj2);
     98     return input1 + input3 + input4;
     99   }
    100 }
    101