1 /* 2 * Copyright (C) 2017 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 assertByteEquals(byte expected, byte result)19 public static void assertByteEquals(byte expected, byte result) { 20 if (expected != result) { 21 throw new Error("Expected: " + expected + ", found: " + result); 22 } 23 } 24 assertShortEquals(short expected, short result)25 public static void assertShortEquals(short expected, short result) { 26 if (expected != result) { 27 throw new Error("Expected: " + expected + ", found: " + result); 28 } 29 } 30 assertIntEquals(int expected, int result)31 public static void assertIntEquals(int expected, int result) { 32 if (expected != result) { 33 throw new Error("Expected: " + expected + ", found: " + result); 34 } 35 } 36 assertLongEquals(long expected, long result)37 public static void assertLongEquals(long expected, long result) { 38 if (expected != result) { 39 throw new Error("Expected: " + expected + ", found: " + result); 40 } 41 } 42 assertCharEquals(char expected, char result)43 public static void assertCharEquals(char expected, char result) { 44 if (expected != result) { 45 // Values are cast to int to display numeric values instead of 46 // (UTF-16 encoded) characters. 47 throw new Error("Expected: " + (int)expected + ", found: " + (int)result); 48 } 49 } 50 51 /// CHECK-START: byte Main.getByte1() constant_folding (before) 52 /// CHECK: TypeConversion 53 /// CHECK: TypeConversion 54 /// CHECK: Add 55 /// CHECK: TypeConversion 56 57 /// CHECK-START: byte Main.getByte1() constant_folding (after) 58 /// CHECK-NOT: TypeConversion 59 /// CHECK-NOT: Add 60 getByte1()61 static byte getByte1() { 62 int i = -2; 63 int j = -3; 64 return (byte)((byte)i + (byte)j); 65 } 66 67 /// CHECK-START: byte Main.getByte2() constant_folding (before) 68 /// CHECK: TypeConversion 69 /// CHECK: TypeConversion 70 /// CHECK: Add 71 /// CHECK: TypeConversion 72 73 /// CHECK-START: byte Main.getByte2() constant_folding (after) 74 /// CHECK-NOT: TypeConversion 75 /// CHECK-NOT: Add 76 getByte2()77 static byte getByte2() { 78 int i = -100; 79 int j = -101; 80 return (byte)((byte)i + (byte)j); 81 } 82 83 /// CHECK-START: byte Main.getByte3() constant_folding (before) 84 /// CHECK: TypeConversion 85 /// CHECK: TypeConversion 86 /// CHECK: Add 87 /// CHECK: TypeConversion 88 89 /// CHECK-START: byte Main.getByte2() constant_folding (after) 90 /// CHECK-NOT: TypeConversion 91 /// CHECK-NOT: Add 92 getByte3()93 static byte getByte3() { 94 long i = 0xabcdabcdabcdL; 95 return (byte)((byte)i + (byte)i); 96 } 97 98 static byte byteVal = -1; 99 static short shortVal = -1; 100 static char charVal = 0xffff; 101 static int intVal = -1; 102 103 static byte[] byteArr = { 0 }; 104 static short[] shortArr = { 0 }; 105 static char[] charArr = { 0 }; 106 static int[] intArr = { 0 }; 107 $noinline$getByte()108 static byte $noinline$getByte() { 109 return byteVal; 110 } 111 $noinline$getShort()112 static short $noinline$getShort() { 113 return shortVal; 114 } 115 $noinline$getChar()116 static char $noinline$getChar() { 117 return charVal; 118 } 119 $noinline$getInt()120 static int $noinline$getInt() { 121 return intVal; 122 } 123 124 static boolean sFlag = true; 125 126 /// CHECK-START: void Main.byteToShort() instruction_simplifier$before_codegen (after) 127 /// CHECK-NOT: TypeConversion byteToShort()128 private static void byteToShort() { 129 shortArr[0] = 0; 130 if (sFlag) { 131 shortArr[0] = $noinline$getByte(); 132 } 133 } 134 135 /// CHECK-START: void Main.byteToChar() instruction_simplifier$before_codegen (after) 136 /// CHECK: TypeConversion byteToChar()137 private static void byteToChar() { 138 charArr[0] = 0; 139 if (sFlag) { 140 charArr[0] = (char)$noinline$getByte(); 141 } 142 } 143 144 /// CHECK-START: void Main.byteToInt() instruction_simplifier$before_codegen (after) 145 /// CHECK-NOT: TypeConversion byteToInt()146 private static void byteToInt() { 147 intArr[0] = 0; 148 if (sFlag) { 149 intArr[0] = $noinline$getByte(); 150 } 151 } 152 153 /// CHECK-START: void Main.charToByte() instruction_simplifier$before_codegen (after) 154 /// CHECK-NOT: TypeConversion charToByte()155 private static void charToByte() { 156 byteArr[0] = 0; 157 if (sFlag) { 158 byteArr[0] = (byte)$noinline$getChar(); 159 } 160 } 161 162 /// CHECK-START: void Main.charToShort() instruction_simplifier$before_codegen (after) 163 /// CHECK-NOT: TypeConversion charToShort()164 private static void charToShort() { 165 shortArr[0] = 0; 166 if (sFlag) { 167 shortArr[0] = (short)$noinline$getChar(); 168 } 169 } 170 171 /// CHECK-START: void Main.charToInt() instruction_simplifier$before_codegen (after) 172 /// CHECK-NOT: TypeConversion charToInt()173 private static void charToInt() { 174 intArr[0] = 0; 175 if (sFlag) { 176 intArr[0] = $noinline$getChar(); 177 } 178 } 179 180 /// CHECK-START: void Main.shortToByte() instruction_simplifier$before_codegen (after) 181 /// CHECK-NOT: TypeConversion shortToByte()182 private static void shortToByte() { 183 byteArr[0] = 0; 184 if (sFlag) { 185 byteArr[0] = (byte)$noinline$getShort(); 186 } 187 } 188 189 /// CHECK-START: void Main.shortToChar() instruction_simplifier$before_codegen (after) 190 /// CHECK-NOT: TypeConversion shortToChar()191 private static void shortToChar() { 192 charArr[0] = 0; 193 if (sFlag) { 194 charArr[0] = (char)$noinline$getShort(); 195 } 196 } 197 198 /// CHECK-START: void Main.shortToInt() instruction_simplifier$before_codegen (after) 199 /// CHECK-NOT: TypeConversion shortToInt()200 private static void shortToInt() { 201 intArr[0] = 0; 202 if (sFlag) { 203 intArr[0] = $noinline$getShort(); 204 } 205 } 206 207 /// CHECK-START: void Main.intToByte() instruction_simplifier$before_codegen (after) 208 /// CHECK-NOT: TypeConversion intToByte()209 private static void intToByte() { 210 byteArr[0] = 0; 211 if (sFlag) { 212 byteArr[0] = (byte)$noinline$getInt(); 213 } 214 } 215 216 /// CHECK-START: void Main.intToShort() instruction_simplifier$before_codegen (after) 217 /// CHECK-NOT: TypeConversion intToShort()218 private static void intToShort() { 219 shortArr[0] = 0; 220 if (sFlag) { 221 shortArr[0] = (short)$noinline$getInt(); 222 } 223 } 224 225 /// CHECK-START: void Main.intToChar() instruction_simplifier$before_codegen (after) 226 /// CHECK-NOT: TypeConversion intToChar()227 private static void intToChar() { 228 charArr[0] = 0; 229 if (sFlag) { 230 charArr[0] = (char)$noinline$getInt(); 231 } 232 } 233 main(String[] args)234 public static void main(String[] args) { 235 assertByteEquals(getByte1(), (byte)-5); 236 assertByteEquals(getByte2(), (byte)(-201)); 237 assertByteEquals(getByte3(), (byte)(0xcd + 0xcd)); 238 239 byteToShort(); 240 assertShortEquals(shortArr[0], (short)-1); 241 byteToChar(); 242 assertCharEquals(charArr[0], (char)-1); 243 byteToInt(); 244 assertIntEquals(intArr[0], -1); 245 charToByte(); 246 assertByteEquals(byteArr[0], (byte)-1); 247 charToShort(); 248 assertShortEquals(shortArr[0], (short)-1); 249 charToInt(); 250 assertIntEquals(intArr[0], 0xffff); 251 shortToByte(); 252 assertByteEquals(byteArr[0], (byte)-1); 253 shortToChar(); 254 assertCharEquals(charArr[0], (char)-1); 255 shortToInt(); 256 assertIntEquals(intArr[0], -1); 257 intToByte(); 258 assertByteEquals(byteArr[0], (byte)-1); 259 intToShort(); 260 assertShortEquals(shortArr[0], (short)-1); 261 intToChar(); 262 assertCharEquals(charArr[0], (char)-1); 263 } 264 } 265