Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2016 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   public static void main(String args[]) {
     20     expectEqualsByte((byte)1, booleanToByte(true));
     21     expectEqualsShort((short)1, booleanToShort(true));
     22     expectEqualsChar((char)1, booleanToChar(true));
     23     expectEqualsInt(1, booleanToInt(true));
     24     expectEqualsLong(1L, booleanToLong(true));
     25 
     26     expectEqualsInt(1, longToIntOfBoolean());
     27 
     28     System.out.println("passed");
     29   }
     30 
     31   /// CHECK-START: byte Main.booleanToByte(boolean) builder (after)
     32   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     33   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
     34   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
     35   /// CHECK-DAG:     <<Cond:z\d+>>          Equal [<<Arg>>,<<Zero>>]
     36   /// CHECK-DAG:                            If [<<Cond>>]
     37   /// CHECK-DAG:     <<Phi:i\d+>>           Phi [<<One>>,<<Zero>>]
     38   /// CHECK-DAG:     <<IToS:b\d+>>          TypeConversion [<<Phi>>]
     39   /// CHECK-DAG:                            Return [<<IToS>>]
     40 
     41   /// CHECK-START: byte Main.booleanToByte(boolean) select_generator (after)
     42   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     43   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
     44   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
     45   /// CHECK-DAG:     <<Sel:i\d+>>           Select [<<Zero>>,<<One>>,<<Arg>>]
     46   /// CHECK-DAG:     <<IToS:b\d+>>          TypeConversion [<<Sel>>]
     47   /// CHECK-DAG:                            Return [<<IToS>>]
     48 
     49   /// CHECK-START: byte Main.booleanToByte(boolean) instruction_simplifier$after_bce (after)
     50   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     51   /// CHECK-DAG:                            Return [<<Arg>>]
     52 
     53   static byte booleanToByte(boolean b) {
     54     return (byte)(b ? 1 : 0);
     55   }
     56 
     57   /// CHECK-START: short Main.booleanToShort(boolean) builder (after)
     58   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     59   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
     60   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
     61   /// CHECK-DAG:     <<Cond:z\d+>>          Equal [<<Arg>>,<<Zero>>]
     62   /// CHECK-DAG:                            If [<<Cond>>]
     63   /// CHECK-DAG:     <<Phi:i\d+>>           Phi [<<One>>,<<Zero>>]
     64   /// CHECK-DAG:     <<IToS:s\d+>>          TypeConversion [<<Phi>>]
     65   /// CHECK-DAG:                            Return [<<IToS>>]
     66 
     67   /// CHECK-START: short Main.booleanToShort(boolean) select_generator (after)
     68   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     69   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
     70   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
     71   /// CHECK-DAG:     <<Sel:i\d+>>           Select [<<Zero>>,<<One>>,<<Arg>>]
     72   /// CHECK-DAG:     <<IToS:s\d+>>          TypeConversion [<<Sel>>]
     73   /// CHECK-DAG:                            Return [<<IToS>>]
     74 
     75   /// CHECK-START: short Main.booleanToShort(boolean) instruction_simplifier$after_bce (after)
     76   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     77   /// CHECK-DAG:                            Return [<<Arg>>]
     78 
     79   static short booleanToShort(boolean b) {
     80     return (short)(b ? 1 : 0);
     81   }
     82 
     83   /// CHECK-START: char Main.booleanToChar(boolean) builder (after)
     84   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     85   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
     86   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
     87   /// CHECK-DAG:     <<Cond:z\d+>>          Equal [<<Arg>>,<<Zero>>]
     88   /// CHECK-DAG:                            If [<<Cond>>]
     89   /// CHECK-DAG:     <<Phi:i\d+>>           Phi [<<One>>,<<Zero>>]
     90   /// CHECK-DAG:     <<IToC:c\d+>>          TypeConversion [<<Phi>>]
     91   /// CHECK-DAG:                            Return [<<IToC>>]
     92 
     93   /// CHECK-START: char Main.booleanToChar(boolean) select_generator (after)
     94   /// CHECK:         <<Arg:z\d+>>           ParameterValue
     95   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
     96   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
     97   /// CHECK-DAG:     <<Sel:i\d+>>           Select [<<Zero>>,<<One>>,<<Arg>>]
     98   /// CHECK-DAG:     <<IToC:c\d+>>          TypeConversion [<<Sel>>]
     99   /// CHECK-DAG:                            Return [<<IToC>>]
    100 
    101   /// CHECK-START: char Main.booleanToChar(boolean) instruction_simplifier$after_bce (after)
    102   /// CHECK:         <<Arg:z\d+>>           ParameterValue
    103   /// CHECK-DAG:                            Return [<<Arg>>]
    104 
    105   static char booleanToChar(boolean b) {
    106     return (char)(b ? 1 : 0);
    107   }
    108 
    109   /// CHECK-START: int Main.booleanToInt(boolean) builder (after)
    110   /// CHECK:         <<Arg:z\d+>>           ParameterValue
    111   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
    112   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
    113   /// CHECK-DAG:     <<Cond:z\d+>>          Equal [<<Arg>>,<<Zero>>]
    114   /// CHECK-DAG:                            If [<<Cond>>]
    115   /// CHECK-DAG:     <<Phi:i\d+>>           Phi [<<One>>,<<Zero>>]
    116   /// CHECK-DAG:                            Return [<<Phi>>]
    117 
    118   /// CHECK-START: int Main.booleanToInt(boolean) select_generator (after)
    119   /// CHECK:         <<Arg:z\d+>>           ParameterValue
    120   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
    121   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
    122   /// CHECK-DAG:     <<Sel:i\d+>>           Select [<<Zero>>,<<One>>,<<Arg>>]
    123   /// CHECK-DAG:                            Return [<<Sel>>]
    124 
    125   /// CHECK-START: int Main.booleanToInt(boolean) instruction_simplifier$after_bce (after)
    126   /// CHECK:         <<Arg:z\d+>>           ParameterValue
    127   /// CHECK-DAG:                            Return [<<Arg>>]
    128 
    129   static int booleanToInt(boolean b) {
    130     return b ? 1 : 0;
    131   }
    132 
    133   /// CHECK-START: long Main.booleanToLong(boolean) builder (after)
    134   /// CHECK:         <<Arg:z\d+>>           ParameterValue
    135   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
    136   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
    137   /// CHECK-DAG:     <<Cond:z\d+>>          Equal [<<Arg>>,<<Zero>>]
    138   /// CHECK-DAG:                            If [<<Cond>>]
    139   /// CHECK-DAG:     <<Phi:i\d+>>           Phi [<<One>>,<<Zero>>]
    140   /// CHECK-DAG:     <<IToJ:j\d+>>          TypeConversion [<<Phi>>]
    141   /// CHECK-DAG:                            Return [<<IToJ>>]
    142 
    143   /// CHECK-START: long Main.booleanToLong(boolean) select_generator (after)
    144   /// CHECK:         <<Arg:z\d+>>           ParameterValue
    145   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
    146   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
    147   /// CHECK-DAG:     <<Sel:i\d+>>           Select [<<Zero>>,<<One>>,<<Arg>>]
    148   /// CHECK-DAG:     <<IToJ:j\d+>>          TypeConversion [<<Sel>>]
    149   /// CHECK-DAG:                            Return [<<IToJ>>]
    150 
    151   /// CHECK-START: long Main.booleanToLong(boolean) instruction_simplifier$after_bce (after)
    152   /// CHECK:         <<Arg:z\d+>>           ParameterValue
    153   /// CHECK-DAG:     <<ZToJ:j\d+>>          TypeConversion [<<Arg>>]
    154   /// CHECK-DAG:                            Return [<<ZToJ>>]
    155 
    156   static long booleanToLong(boolean b) {
    157     return b ? 1 : 0;
    158   }
    159 
    160   /// CHECK-START: int Main.longToIntOfBoolean() builder (after)
    161   /// CHECK-DAG:     <<Method:[ij]\d+>>     CurrentMethod
    162   /// CHECK-DAG:     <<Sget:z\d+>>          StaticFieldGet
    163   /// CHECK-DAG:     <<ZToJ:j\d+>>          InvokeStaticOrDirect [<<Sget>>,<<Method>>]
    164   /// CHECK-DAG:     <<JToI:i\d+>>          TypeConversion [<<ZToJ>>]
    165   /// CHECK-DAG:                            Return [<<JToI>>]
    166 
    167   /// CHECK-START: int Main.longToIntOfBoolean() inliner (after)
    168   /// CHECK-DAG:     <<Method:[ij]\d+>>     CurrentMethod
    169   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
    170   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
    171   /// CHECK-DAG:     <<Sget:z\d+>>          StaticFieldGet
    172   /// CHECK-DAG:                            If [<<Sget>>]
    173   /// CHECK-DAG:     <<Phi:i\d+>>           Phi [<<One>>,<<Zero>>]
    174   /// CHECK-DAG:     <<IToJ:j\d+>>          TypeConversion [<<Phi>>]
    175   /// CHECK-DAG:     <<JToI:i\d+>>          TypeConversion [<<IToJ>>]
    176   /// CHECK-DAG:                            Return [<<JToI>>]
    177 
    178   /// CHECK-START: int Main.longToIntOfBoolean() select_generator (after)
    179   /// CHECK-DAG:     <<Method:[ij]\d+>>     CurrentMethod
    180   /// CHECK-DAG:     <<Zero:i\d+>>          IntConstant 0
    181   /// CHECK-DAG:     <<One:i\d+>>           IntConstant 1
    182   /// CHECK-DAG:     <<Sget:z\d+>>          StaticFieldGet
    183   /// CHECK-DAG:     <<Sel:i\d+>>           Select [<<Zero>>,<<One>>,<<Sget>>]
    184   /// CHECK-DAG:     <<IToJ:j\d+>>          TypeConversion [<<Sel>>]
    185   /// CHECK-DAG:     <<JToI:i\d+>>          TypeConversion [<<IToJ>>]
    186   /// CHECK-DAG:                            Return [<<JToI>>]
    187 
    188   /// CHECK-START: int Main.longToIntOfBoolean() instruction_simplifier$after_bce (after)
    189   /// CHECK-DAG:     <<Method:[ij]\d+>>     CurrentMethod
    190   /// CHECK-DAG:     <<Sget:z\d+>>          StaticFieldGet
    191   /// CHECK-DAG:                            Return [<<Sget>>]
    192 
    193   static int longToIntOfBoolean() {
    194     long l = booleanToLong(booleanField);
    195     return (int) l;
    196   }
    197 
    198 
    199   private static void expectEqualsByte(byte expected, byte result) {
    200     if (expected != result) {
    201       throw new Error("Expected: " + expected + ", found: " + result);
    202     }
    203   }
    204 
    205   private static void expectEqualsShort(short expected, short result) {
    206     if (expected != result) {
    207       throw new Error("Expected: " + expected + ", found: " + result);
    208     }
    209   }
    210 
    211   private static void expectEqualsChar(char expected, char result) {
    212     if (expected != result) {
    213       throw new Error("Expected: " + expected + ", found: " + result);
    214     }
    215   }
    216 
    217   private static void expectEqualsInt(int expected, int result) {
    218     if (expected != result) {
    219       throw new Error("Expected: " + expected + ", found: " + result);
    220     }
    221   }
    222 
    223   private static void expectEqualsLong(long expected, long result) {
    224     if (expected != result) {
    225       throw new Error("Expected: " + expected + ", found: " + result);
    226     }
    227   }
    228 
    229 
    230   public static boolean booleanField = true;
    231 
    232 }
    233