Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2014 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 public class Main {
     18 
     19   /// CHECK-START: void Main.InlineVoid() inliner (before)
     20   /// CHECK-DAG:     <<Const42:i\d+>> IntConstant 42
     21   /// CHECK-DAG:                      InvokeStaticOrDirect
     22   /// CHECK-DAG:                      InvokeStaticOrDirect [<<Const42>>{{(,[ij]\d+)?}}]
     23 
     24   /// CHECK-START: void Main.InlineVoid() inliner (after)
     25   /// CHECK-NOT:                      InvokeStaticOrDirect
     26 
     27   public static void InlineVoid() {
     28     returnVoid();
     29     returnVoidWithOneParameter(42);
     30   }
     31 
     32   /// CHECK-START: int Main.InlineParameter(int) inliner (before)
     33   /// CHECK-DAG:     <<Param:i\d+>>  ParameterValue
     34   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect [<<Param>>{{(,[ij]\d+)?}}]
     35   /// CHECK-DAG:                     Return [<<Result>>]
     36 
     37   /// CHECK-START: int Main.InlineParameter(int) inliner (after)
     38   /// CHECK-DAG:     <<Param:i\d+>>  ParameterValue
     39   /// CHECK-DAG:                     Return [<<Param>>]
     40 
     41   public static int InlineParameter(int a) {
     42     return returnParameter(a);
     43   }
     44 
     45   /// CHECK-START: long Main.InlineWideParameter(long) inliner (before)
     46   /// CHECK-DAG:     <<Param:j\d+>>  ParameterValue
     47   /// CHECK-DAG:     <<Result:j\d+>> InvokeStaticOrDirect [<<Param>>{{(,[ij]\d+)?}}]
     48   /// CHECK-DAG:                     Return [<<Result>>]
     49 
     50   /// CHECK-START: long Main.InlineWideParameter(long) inliner (after)
     51   /// CHECK-DAG:     <<Param:j\d+>>  ParameterValue
     52   /// CHECK-DAG:                     Return [<<Param>>]
     53 
     54   public static long InlineWideParameter(long a) {
     55     return returnWideParameter(a);
     56   }
     57 
     58   /// CHECK-START: java.lang.Object Main.InlineReferenceParameter(java.lang.Object) inliner (before)
     59   /// CHECK-DAG:     <<Param:l\d+>>  ParameterValue
     60   /// CHECK-DAG:     <<Result:l\d+>> InvokeStaticOrDirect [<<Param>>{{(,[ij]\d+)?}}]
     61   /// CHECK-DAG:                     Return [<<Result>>]
     62 
     63   /// CHECK-START: java.lang.Object Main.InlineReferenceParameter(java.lang.Object) inliner (after)
     64   /// CHECK-DAG:     <<Param:l\d+>>  ParameterValue
     65   /// CHECK-DAG:                     Return [<<Param>>]
     66 
     67   public static Object InlineReferenceParameter(Object o) {
     68     return returnReferenceParameter(o);
     69   }
     70 
     71   /// CHECK-START: int Main.InlineInt() inliner (before)
     72   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect
     73   /// CHECK-DAG:                     Return [<<Result>>]
     74 
     75   /// CHECK-START: int Main.InlineInt() inliner (after)
     76   /// CHECK-DAG:     <<Const4:i\d+>> IntConstant 4
     77   /// CHECK-DAG:                     Return [<<Const4>>]
     78 
     79   public static int InlineInt() {
     80     return returnInt();
     81   }
     82 
     83   /// CHECK-START: long Main.InlineWide() inliner (before)
     84   /// CHECK-DAG:     <<Result:j\d+>> InvokeStaticOrDirect
     85   /// CHECK-DAG:                     Return [<<Result>>]
     86 
     87   /// CHECK-START: long Main.InlineWide() inliner (after)
     88   /// CHECK-DAG:     <<Const8:j\d+>> LongConstant 8
     89   /// CHECK-DAG:                     Return [<<Const8>>]
     90 
     91   public static long InlineWide() {
     92     return returnWide();
     93   }
     94 
     95   /// CHECK-START: int Main.InlineAdd() inliner (before)
     96   /// CHECK-DAG:     <<Const3:i\d+>> IntConstant 3
     97   /// CHECK-DAG:     <<Const5:i\d+>> IntConstant 5
     98   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect
     99   /// CHECK-DAG:                     Return [<<Result>>]
    100 
    101   /// CHECK-START: int Main.InlineAdd() inliner (after)
    102   /// CHECK-DAG:     <<Const8:i\d+>> IntConstant 8
    103   /// CHECK-DAG:                     Return [<<Const8>>]
    104 
    105   public static int InlineAdd() {
    106     return returnAdd(3, 5);
    107   }
    108 
    109   /// CHECK-START: int Main.InlineFieldAccess() inliner (before)
    110   /// CHECK-DAG:     <<After:i\d+>>  InvokeStaticOrDirect
    111   /// CHECK-DAG:                     Return [<<After>>]
    112 
    113   /// CHECK-START: int Main.InlineFieldAccess() inliner (after)
    114   /// CHECK-DAG:     <<Const1:i\d+>> IntConstant 1
    115   /// CHECK-DAG:     <<Before:i\d+>> StaticFieldGet
    116   /// CHECK-DAG:     <<After:i\d+>>  Add [<<Before>>,<<Const1>>]
    117   /// CHECK-DAG:                     StaticFieldSet [{{l\d+}},<<After>>]
    118   /// CHECK-DAG:                     Return [<<After>>]
    119 
    120   /// CHECK-START: int Main.InlineFieldAccess() inliner (after)
    121   /// CHECK-NOT:                     InvokeStaticOrDirect
    122 
    123   public static int InlineFieldAccess() {
    124     return incCounter();
    125   }
    126 
    127   /// CHECK-START: int Main.InlineWithControlFlow(boolean) inliner (before)
    128   /// CHECK-DAG:     <<Const1:i\d+>> IntConstant 1
    129   /// CHECK-DAG:     <<Const3:i\d+>> IntConstant 3
    130   /// CHECK-DAG:     <<Const5:i\d+>> IntConstant 5
    131   /// CHECK-DAG:     <<Add:i\d+>>    InvokeStaticOrDirect [<<Const1>>,<<Const3>>{{(,[ij]\d+)?}}]
    132   /// CHECK-DAG:     <<Sub:i\d+>>    InvokeStaticOrDirect [<<Const5>>,<<Const3>>{{(,[ij]\d+)?}}]
    133   /// CHECK-DAG:     <<Phi:i\d+>>    Phi [<<Add>>,<<Sub>>]
    134   /// CHECK-DAG:                     Return [<<Phi>>]
    135 
    136   /// CHECK-START: int Main.InlineWithControlFlow(boolean) inliner (after)
    137   /// CHECK-DAG:     <<Const4:i\d+>> IntConstant 4
    138   /// CHECK-DAG:     <<Const2:i\d+>> IntConstant 2
    139   /// CHECK-DAG:     <<Phi:i\d+>>    Phi [<<Const4>>,<<Const2>>]
    140   /// CHECK-DAG:                     Return [<<Phi>>]
    141 
    142   public static int InlineWithControlFlow(boolean cond) {
    143     int x, const1, const3, const5;
    144     const1 = 1;
    145     const3 = 3;
    146     const5 = 5;
    147     if (cond) {
    148       x = returnAdd(const1, const3);
    149     } else {
    150       x = returnSub(const5, const3);
    151     }
    152     return x;
    153   }
    154 
    155   /// CHECK-START: int Main.returnAbs(int) intrinsics_recognition (before)
    156   /// CHECK-DAG:     <<Result:i\d+>>      InvokeStaticOrDirect
    157   /// CHECK-DAG:                          Return [<<Result>>]
    158 
    159   /// CHECK-START: int Main.returnAbs(int) intrinsics_recognition (after)
    160   /// CHECK-DAG:     <<Result:i\d+>>      InvokeStaticOrDirect intrinsic:MathAbsInt
    161   /// CHECK-DAG:                          Return [<<Result>>]
    162 
    163   private static int returnAbs(int i) {
    164     return Math.abs(i);
    165   }
    166 
    167   /// CHECK-START: int Main.InlinedIntrinsicsAreStillIntrinsic() inliner (before)
    168   /// CHECK-DAG:     <<ConstMinus1:i\d+>> IntConstant -1
    169   /// CHECK-DAG:     <<Result:i\d+>>      InvokeStaticOrDirect
    170   /// CHECK-DAG:                          Return [<<Result>>]
    171 
    172   /// CHECK-START: int Main.InlinedIntrinsicsAreStillIntrinsic() inliner (after)
    173   /// CHECK-DAG:     <<ConstMinus1:i\d+>> IntConstant -1
    174   /// CHECK-DAG:     <<Result:i\d+>>      InvokeStaticOrDirect intrinsic:MathAbsInt
    175   /// CHECK-DAG:                          Return [<<Result>>]
    176 
    177   public static int InlinedIntrinsicsAreStillIntrinsic() {
    178     return returnAbs(-1);
    179   }
    180 
    181   private static void returnVoid() {
    182     return;
    183   }
    184 
    185   private static void returnVoidWithOneParameter(int a) {
    186     return;
    187   }
    188 
    189   private static int returnParameter(int a) {
    190     return a;
    191   }
    192 
    193   private static long returnWideParameter(long a) {
    194     return a;
    195   }
    196 
    197   private static Object returnReferenceParameter(Object o) {
    198     return o;
    199   }
    200 
    201   private static int returnInt() {
    202     return 4;
    203   }
    204 
    205   private static long returnWide() {
    206     return 8L;
    207   }
    208 
    209   private static int returnAdd(int a, int b) {
    210     return a + b;
    211   }
    212 
    213   private static int returnSub(int a, int b) {
    214     return a - b;
    215   }
    216 
    217   private static int counter = 42;
    218 
    219   private static int incCounter() {
    220     return ++counter;
    221   }
    222 
    223   public static void main(String[] args) {
    224     InlineVoid();
    225 
    226     if (InlineInt() != 4) {
    227       throw new Error();
    228     }
    229 
    230     if (InlineWide() != 8L) {
    231       throw new Error();
    232     }
    233 
    234     if (InlineParameter(42) != 42) {
    235       throw new Error();
    236     }
    237 
    238     if (InlineWideParameter(0x100000001L) != 0x100000001L) {
    239       throw new Error();
    240     }
    241 
    242     if (InlineReferenceParameter(Main.class) != Main.class) {
    243       throw new Error();
    244     }
    245 
    246     if (InlineAdd() != 8) {
    247       throw new Error();
    248     }
    249 
    250     if (InlineFieldAccess() != 43 || InlineFieldAccess() != 44) {
    251       throw new Error();
    252     }
    253 
    254     if (InlineWithControlFlow(true) != 4) {
    255       throw new Error();
    256     }
    257 
    258     if (InlineWithControlFlow(false) != 2) {
    259       throw new Error();
    260     }
    261 
    262     if (InlinedIntrinsicsAreStillIntrinsic() != 1) {
    263       throw new Error();
    264     }
    265 
    266     if (returnAbs(-1) != 1) {
    267       throw new Error();
    268     }
    269   }
    270 }
    271