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 public class Main { 18 19 // Note #1: `javac` flips the conditions of If statements. 20 // Note #2: In the optimizing compiler, the first input of Phi is always 21 // the fall-through path, i.e. the false branch. 22 assertBoolEquals(boolean expected, boolean result)23 public static void assertBoolEquals(boolean expected, boolean result) { 24 if (expected != result) { 25 throw new Error("Expected: " + expected + ", found: " + result); 26 } 27 } 28 assertIntEquals(int expected, int result)29 public static void assertIntEquals(int expected, int result) { 30 if (expected != result) { 31 throw new Error("Expected: " + expected + ", found: " + result); 32 } 33 } 34 35 /* 36 * Elementary test negating a boolean. Verifies that blocks are merged and 37 * empty branches removed. 38 */ 39 40 /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (before) 41 /// CHECK-DAG: <<Param:z\d+>> ParameterValue 42 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 43 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 44 /// CHECK-DAG: If [<<Param>>] 45 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Const1>>,<<Const0>>] 46 /// CHECK-DAG: Return [<<Phi>>] 47 48 /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (before) 49 /// CHECK: Goto 50 /// CHECK: Goto 51 /// CHECK: Goto 52 /// CHECK-NOT: Goto 53 54 /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (after) 55 /// CHECK-DAG: <<Param:z\d+>> ParameterValue 56 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 57 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 58 /// CHECK-DAG: <<NotParam:i\d+>> Select [<<Const1>>,<<Const0>>,<<Param>>] 59 /// CHECK-DAG: Return [<<NotParam>>] 60 61 /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (after) 62 /// CHECK-NOT: If 63 /// CHECK-NOT: Phi 64 65 /// CHECK-START: boolean Main.BooleanNot(boolean) select_generator (after) 66 /// CHECK: Goto 67 /// CHECK-NOT: Goto 68 BooleanNot(boolean x)69 public static boolean BooleanNot(boolean x) { 70 return !x; 71 } 72 73 /* 74 * Program which only delegates the condition, i.e. returns 1 when True 75 * and 0 when False. 76 */ 77 78 /// CHECK-START: boolean Main.GreaterThan(int, int) select_generator (before) 79 /// CHECK-DAG: <<ParamX:i\d+>> ParameterValue 80 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 81 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 82 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 83 /// CHECK-DAG: <<Cond:z\d+>> GreaterThan [<<ParamX>>,<<ParamY>>] 84 /// CHECK-DAG: If [<<Cond>>] 85 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Const0>>,<<Const1>>] 86 /// CHECK-DAG: Return [<<Phi>>] 87 88 /// CHECK-START: boolean Main.GreaterThan(int, int) select_generator (after) 89 /// CHECK-DAG: <<ParamX:i\d+>> ParameterValue 90 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 91 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 92 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 93 /// CHECK-DAG: <<Cond:z\d+>> GreaterThan [<<ParamX>>,<<ParamY>>] 94 /// CHECK-DAG: <<Select:i\d+>> Select [<<Const0>>,<<Const1>>,<<Cond>>] 95 /// CHECK-DAG: Return [<<Select>>] 96 GreaterThan(int x, int y)97 public static boolean GreaterThan(int x, int y) { 98 return (x <= y) ? false : true; 99 } 100 101 /* 102 * Program which negates a condition, i.e. returns 0 when True 103 * and 1 when False. 104 */ 105 106 /// CHECK-START: boolean Main.LessThan(int, int) select_generator (before) 107 /// CHECK-DAG: <<ParamX:i\d+>> ParameterValue 108 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 109 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 110 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 111 /// CHECK-DAG: <<Cond:z\d+>> GreaterThanOrEqual [<<ParamX>>,<<ParamY>>] 112 /// CHECK-DAG: If [<<Cond>>] 113 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Const1>>,<<Const0>>] 114 /// CHECK-DAG: Return [<<Phi>>] 115 116 /// CHECK-START: boolean Main.LessThan(int, int) select_generator (after) 117 /// CHECK-DAG: <<ParamX:i\d+>> ParameterValue 118 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 119 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 120 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 121 /// CHECK-DAG: <<Cond:z\d+>> GreaterThanOrEqual [<<ParamX>>,<<ParamY>>] 122 /// CHECK-DAG: <<Select:i\d+>> Select [<<Const1>>,<<Const0>>,<<Cond>>] 123 /// CHECK-DAG: Return [<<Select>>] 124 LessThan(int x, int y)125 public static boolean LessThan(int x, int y) { 126 return (x < y) ? true : false; 127 } 128 129 /* 130 * Program which further uses negated conditions. 131 * Note that Phis are discovered retrospectively. 132 */ 133 134 /// CHECK-START: boolean Main.ValuesOrdered(int, int, int) select_generator (before) 135 /// CHECK-DAG: <<ParamX:i\d+>> ParameterValue 136 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 137 /// CHECK-DAG: <<ParamZ:i\d+>> ParameterValue 138 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 139 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 140 /// CHECK-DAG: <<CondXY:z\d+>> GreaterThan [<<ParamX>>,<<ParamY>>] 141 /// CHECK-DAG: If [<<CondXY>>] 142 /// CHECK-DAG: <<CondYZ:z\d+>> GreaterThan [<<ParamY>>,<<ParamZ>>] 143 /// CHECK-DAG: If [<<CondYZ>>] 144 /// CHECK-DAG: <<CondXYZ:z\d+>> NotEqual [<<PhiXY:i\d+>>,<<PhiYZ:i\d+>>] 145 /// CHECK-DAG: If [<<CondXYZ>>] 146 /// CHECK-DAG: Return [<<PhiXYZ:i\d+>>] 147 /// CHECK-DAG: <<PhiXY>> Phi [<<Const1>>,<<Const0>>] 148 /// CHECK-DAG: <<PhiYZ>> Phi [<<Const1>>,<<Const0>>] 149 /// CHECK-DAG: <<PhiXYZ>> Phi [<<Const1>>,<<Const0>>] 150 151 /// CHECK-START: boolean Main.ValuesOrdered(int, int, int) select_generator (after) 152 /// CHECK-DAG: <<ParamX:i\d+>> ParameterValue 153 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 154 /// CHECK-DAG: <<ParamZ:i\d+>> ParameterValue 155 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 156 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 157 /// CHECK-DAG: <<CmpXY:z\d+>> GreaterThan [<<ParamX>>,<<ParamY>>] 158 /// CHECK-DAG: <<SelXY:i\d+>> Select [<<Const1>>,<<Const0>>,<<CmpXY>>] 159 /// CHECK-DAG: <<CmpYZ:z\d+>> GreaterThan [<<ParamY>>,<<ParamZ>>] 160 /// CHECK-DAG: <<SelYZ:i\d+>> Select [<<Const1>>,<<Const0>>,<<CmpYZ>>] 161 /// CHECK-DAG: <<CmpXYZ:z\d+>> NotEqual [<<SelXY>>,<<SelYZ>>] 162 /// CHECK-DAG: <<SelXYZ:i\d+>> Select [<<Const1>>,<<Const0>>,<<CmpXYZ>>] 163 /// CHECK-DAG: Return [<<SelXYZ>>] 164 ValuesOrdered(int x, int y, int z)165 public static boolean ValuesOrdered(int x, int y, int z) { 166 return (x <= y) == (y <= z); 167 } 168 169 /// CHECK-START: int Main.NegatedCondition(boolean) select_generator (before) 170 /// CHECK-DAG: <<Param:z\d+>> ParameterValue 171 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 172 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 173 /// CHECK-DAG: If [<<Param>>] 174 /// CHECK-DAG: <<Phi:i\d+>> Phi [<<Const42>>,<<Const43>>] 175 /// CHECK-DAG: Return [<<Phi>>] 176 177 /// CHECK-START: int Main.NegatedCondition(boolean) select_generator (after) 178 /// CHECK-DAG: <<Param:z\d+>> ParameterValue 179 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 180 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 181 /// CHECK-DAG: <<Select:i\d+>> Select [<<Const43>>,<<Const42>>,<<Param>>] 182 /// CHECK-DAG: Return [<<Select>>] 183 184 /// CHECK-START: int Main.NegatedCondition(boolean) select_generator (after) 185 /// CHECK-NOT: BooleanNot 186 NegatedCondition(boolean x)187 public static int NegatedCondition(boolean x) { 188 if (x != false) { 189 return 42; 190 } else { 191 return 43; 192 } 193 } 194 195 /// CHECK-START: int Main.SimpleTrueBlock(boolean, int) select_generator (after) 196 /// CHECK-DAG: <<ParamX:z\d+>> ParameterValue 197 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 198 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 199 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 200 /// CHECK-DAG: <<Add:i\d+>> Add [<<ParamY>>,<<Const42>>] 201 /// CHECK-DAG: <<Select:i\d+>> Select [<<Const43>>,<<Add>>,<<ParamX>>] 202 /// CHECK-DAG: Return [<<Select>>] 203 204 /// CHECK-START: int Main.SimpleTrueBlock(boolean, int) select_generator (after) 205 /// CHECK-NOT: If 206 SimpleTrueBlock(boolean x, int y)207 public static int SimpleTrueBlock(boolean x, int y) { 208 return x ? y + 42 : 43; 209 } 210 211 /// CHECK-START: int Main.SimpleFalseBlock(boolean, int) select_generator (after) 212 /// CHECK-DAG: <<ParamX:z\d+>> ParameterValue 213 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 214 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 215 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 216 /// CHECK-DAG: <<Add:i\d+>> Add [<<ParamY>>,<<Const43>>] 217 /// CHECK-DAG: <<Select:i\d+>> Select [<<Add>>,<<Const42>>,<<ParamX>>] 218 /// CHECK-DAG: Return [<<Select>>] 219 220 /// CHECK-START: int Main.SimpleFalseBlock(boolean, int) select_generator (after) 221 /// CHECK-NOT: If 222 SimpleFalseBlock(boolean x, int y)223 public static int SimpleFalseBlock(boolean x, int y) { 224 return x ? 42 : y + 43; 225 } 226 227 /// CHECK-START: int Main.SimpleBothBlocks(boolean, int, int) select_generator (after) 228 /// CHECK-DAG: <<ParamX:z\d+>> ParameterValue 229 /// CHECK-DAG: <<ParamY:i\d+>> ParameterValue 230 /// CHECK-DAG: <<ParamZ:i\d+>> ParameterValue 231 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 232 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 233 /// CHECK-DAG: <<AddTrue:i\d+>> Add [<<ParamY>>,<<Const42>>] 234 /// CHECK-DAG: <<AddFalse:i\d+>> Add [<<ParamZ>>,<<Const43>>] 235 /// CHECK-DAG: <<Select:i\d+>> Select [<<AddFalse>>,<<AddTrue>>,<<ParamX>>] 236 /// CHECK-DAG: Return [<<Select>>] 237 238 /// CHECK-START: int Main.SimpleBothBlocks(boolean, int, int) select_generator (after) 239 /// CHECK-NOT: If 240 SimpleBothBlocks(boolean x, int y, int z)241 public static int SimpleBothBlocks(boolean x, int y, int z) { 242 return x ? y + 42 : z + 43; 243 } 244 245 /// CHECK-START: int Main.ThreeBlocks(boolean, boolean) select_generator (after) 246 /// CHECK-DAG: <<ParamX:z\d+>> ParameterValue 247 /// CHECK-DAG: <<ParamY:z\d+>> ParameterValue 248 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 249 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2 250 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3 251 /// CHECK-DAG: <<Select23:i\d+>> Select [<<Const3>>,<<Const2>>,<<ParamY>>] 252 /// CHECK-DAG: <<Select123:i\d+>> Select [<<Select23>>,<<Const1>>,<<ParamX>>] 253 /// CHECK-DAG: Return [<<Select123>>] 254 ThreeBlocks(boolean x, boolean y)255 public static int ThreeBlocks(boolean x, boolean y) { 256 if (x) { 257 return 1; 258 } else if (y) { 259 return 2; 260 } else { 261 return 3; 262 } 263 } 264 265 /// CHECK-START: int Main.MultiplePhis() select_generator (before) 266 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 267 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 268 /// CHECK-DAG: <<Const13:i\d+>> IntConstant 13 269 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 270 /// CHECK-DAG: <<PhiX:i\d+>> Phi [<<Const0>>,<<Const13>>,<<Const42>>] 271 /// CHECK-DAG: <<PhiY:i\d+>> Phi [<<Const1>>,<<Add:i\d+>>,<<Add>>] 272 /// CHECK-DAG: <<Add>> Add [<<PhiY>>,<<Const1>>] 273 /// CHECK-DAG: <<Cond:z\d+>> LessThanOrEqual [<<Add>>,<<Const1>>] 274 /// CHECK-DAG: If [<<Cond>>] 275 /// CHECK-DAG: Return [<<PhiX>>] 276 277 /// CHECK-START: int Main.MultiplePhis() select_generator (after) 278 /// CHECK-DAG: <<Const0:i\d+>> IntConstant 0 279 /// CHECK-DAG: <<Const1:i\d+>> IntConstant 1 280 /// CHECK-DAG: <<Const13:i\d+>> IntConstant 13 281 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 282 /// CHECK-DAG: <<PhiX:i\d+>> Phi [<<Const0>>,<<Select:i\d+>>] 283 /// CHECK-DAG: <<PhiY:i\d+>> Phi [<<Const1>>,<<Add:i\d+>>] 284 /// CHECK-DAG: <<Add>> Add [<<PhiY>>,<<Const1>>] 285 /// CHECK-DAG: <<Cond:z\d+>> LessThanOrEqual [<<Add>>,<<Const1>>] 286 /// CHECK-DAG: <<Select>> Select [<<Const13>>,<<Const42>>,<<Cond>>] 287 /// CHECK-DAG: Return [<<PhiX>>] 288 MultiplePhis()289 public static int MultiplePhis() { 290 int x = 0; 291 int y = 1; 292 while (y++ < 10) { 293 if (y > 1) { 294 x = 13; 295 } else { 296 x = 42; 297 } 298 } 299 return x; 300 } 301 302 /// CHECK-START: int Main.TrueBlockWithTooManyInstructions(boolean) select_generator (before) 303 /// CHECK-DAG: <<This:l\d+>> ParameterValue 304 /// CHECK-DAG: <<Cond:z\d+>> ParameterValue 305 /// CHECK-DAG: <<Const2:i\d+>> IntConstant 2 306 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 307 /// CHECK-DAG: If [<<Cond>>] 308 /// CHECK-DAG: <<Iget:i\d+>> InstanceFieldGet [<<This>>] 309 /// CHECK-DAG: <<Add:i\d+>> Add [<<Iget>>,<<Const2>>] 310 /// CHECK-DAG: Phi [<<Add>>,<<Const43>>] 311 312 /// CHECK-START: int Main.TrueBlockWithTooManyInstructions(boolean) select_generator (after) 313 /// CHECK-NOT: Select 314 TrueBlockWithTooManyInstructions(boolean x)315 public int TrueBlockWithTooManyInstructions(boolean x) { 316 return x ? (read_field + 2) : 43; 317 } 318 319 /// CHECK-START: int Main.FalseBlockWithTooManyInstructions(boolean) select_generator (before) 320 /// CHECK-DAG: <<This:l\d+>> ParameterValue 321 /// CHECK-DAG: <<Cond:z\d+>> ParameterValue 322 /// CHECK-DAG: <<Const3:i\d+>> IntConstant 3 323 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 324 /// CHECK-DAG: If [<<Cond>>] 325 /// CHECK-DAG: <<Iget:i\d+>> InstanceFieldGet [<<This>>] 326 /// CHECK-DAG: <<Add:i\d+>> Add [<<Iget>>,<<Const3>>] 327 /// CHECK-DAG: Phi [<<Const42>>,<<Add>>] 328 329 /// CHECK-START: int Main.FalseBlockWithTooManyInstructions(boolean) select_generator (after) 330 /// CHECK-NOT: Select 331 FalseBlockWithTooManyInstructions(boolean x)332 public int FalseBlockWithTooManyInstructions(boolean x) { 333 return x ? 42 : (read_field + 3); 334 } 335 336 /// CHECK-START: int Main.TrueBlockWithSideEffects(boolean) select_generator (before) 337 /// CHECK-DAG: <<This:l\d+>> ParameterValue 338 /// CHECK-DAG: <<Cond:z\d+>> ParameterValue 339 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 340 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 341 /// CHECK-DAG: If [<<Cond>>] 342 /// CHECK-DAG: InstanceFieldSet [<<This>>,<<Const42>>] 343 /// CHECK-DAG: Phi [<<Const42>>,<<Const43>>] 344 345 /// CHECK-START: int Main.TrueBlockWithSideEffects(boolean) select_generator (after) 346 /// CHECK-NOT: Select 347 TrueBlockWithSideEffects(boolean x)348 public int TrueBlockWithSideEffects(boolean x) { 349 return x ? (write_field = 42) : 43; 350 } 351 352 /// CHECK-START: int Main.FalseBlockWithSideEffects(boolean) select_generator (before) 353 /// CHECK-DAG: <<This:l\d+>> ParameterValue 354 /// CHECK-DAG: <<Cond:z\d+>> ParameterValue 355 /// CHECK-DAG: <<Const42:i\d+>> IntConstant 42 356 /// CHECK-DAG: <<Const43:i\d+>> IntConstant 43 357 /// CHECK-DAG: If [<<Cond>>] 358 /// CHECK-DAG: InstanceFieldSet [<<This>>,<<Const43>>] 359 /// CHECK-DAG: Phi [<<Const42>>,<<Const43>>] 360 361 /// CHECK-START: int Main.FalseBlockWithSideEffects(boolean) select_generator (after) 362 /// CHECK-NOT: Select 363 FalseBlockWithSideEffects(boolean x)364 public int FalseBlockWithSideEffects(boolean x) { 365 return x ? 42 : (write_field = 43); 366 } 367 main(String[] args)368 public static void main(String[] args) throws Exception { 369 assertBoolEquals(false, BooleanNot(true)); 370 assertBoolEquals(true, BooleanNot(false)); 371 assertBoolEquals(true, GreaterThan(10, 5)); 372 assertBoolEquals(false, GreaterThan(10, 10)); 373 assertBoolEquals(false, GreaterThan(5, 10)); 374 assertBoolEquals(true, LessThan(5, 10)); 375 assertBoolEquals(false, LessThan(10, 10)); 376 assertBoolEquals(false, LessThan(10, 5)); 377 assertBoolEquals(true, ValuesOrdered(1, 3, 5)); 378 assertBoolEquals(true, ValuesOrdered(5, 3, 1)); 379 assertBoolEquals(false, ValuesOrdered(1, 3, 2)); 380 assertBoolEquals(false, ValuesOrdered(2, 3, 1)); 381 assertBoolEquals(true, ValuesOrdered(3, 3, 3)); 382 assertBoolEquals(true, ValuesOrdered(3, 3, 5)); 383 assertBoolEquals(false, ValuesOrdered(5, 5, 3)); 384 assertIntEquals(42, NegatedCondition(true)); 385 assertIntEquals(43, NegatedCondition(false)); 386 assertIntEquals(46, SimpleTrueBlock(true, 4)); 387 assertIntEquals(43, SimpleTrueBlock(false, 4)); 388 assertIntEquals(42, SimpleFalseBlock(true, 7)); 389 assertIntEquals(50, SimpleFalseBlock(false, 7)); 390 assertIntEquals(48, SimpleBothBlocks(true, 6, 2)); 391 assertIntEquals(45, SimpleBothBlocks(false, 6, 2)); 392 assertIntEquals(1, ThreeBlocks(true, true)); 393 assertIntEquals(1, ThreeBlocks(true, false)); 394 assertIntEquals(2, ThreeBlocks(false, true)); 395 assertIntEquals(3, ThreeBlocks(false, false)); 396 assertIntEquals(13, MultiplePhis()); 397 398 Main m = new Main(); 399 assertIntEquals(42, m.TrueBlockWithTooManyInstructions(true)); 400 assertIntEquals(43, m.TrueBlockWithTooManyInstructions(false)); 401 assertIntEquals(42, m.FalseBlockWithTooManyInstructions(true)); 402 assertIntEquals(43, m.FalseBlockWithTooManyInstructions(false)); 403 assertIntEquals(42, m.TrueBlockWithSideEffects(true)); 404 assertIntEquals(43, m.TrueBlockWithSideEffects(false)); 405 assertIntEquals(42, m.FalseBlockWithSideEffects(true)); 406 assertIntEquals(43, m.FalseBlockWithSideEffects(false)); 407 } 408 409 // These need to be instance fields so as to not generate a LoadClass for iget/iput. 410 public int read_field = 40; 411 public int write_field = 42; 412 } 413