| 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 |
// Check that we don't generate a select since we don't have no Phi (not even at the builder |
| 19 |
// stage) since both values are the same. |
| 20 |
|
| 21 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) builder (after) |
| 22 |
/// CHECK-NOT: Phi |
| 23 |
|
| 24 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) select_generator (before) |
| 25 |
/// CHECK-NOT: Phi |
| 26 |
|
| 27 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) select_generator (after) |
| 28 |
/// CHECK-NOT: Phi |
| 29 |
|
| 30 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValue(boolean) select_generator (after) |
| 31 |
/// CHECK-NOT: Select |
| 32 |
private static int $noinline$testSimpleDiamondSameValue(boolean bool_param) { |
| 33 |
int return_value; |
| 34 |
if (bool_param) { |
| 35 |
return_value = 10; |
| 36 |
} else { |
| 37 |
return_value = 10; |
| 38 |
} |
| 39 |
return return_value; |
| 40 |
} |
| 41 |
|
| 42 |
// Check that we generate a select for a simple diamond pattern, with different values. |
| 43 |
|
| 44 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValue(boolean) select_generator (before) |
| 45 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 46 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 47 |
/// CHECK-DAG: <<Phi:i\d+>> Phi [<<Arg1:i\d+>>,<<Arg2:i\d+>>] |
| 48 |
/// CHECK-DAG: Return [<<Phi>>] |
| 49 |
/// CHECK-EVAL: set(["<<Arg1>>","<<Arg2>>"]) == set(["<<Const10>>","<<Const20>>"]) |
| 50 |
|
| 51 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValue(boolean) select_generator (after) |
| 52 |
/// CHECK-DAG: <<Bool:z\d+>> ParameterValue |
| 53 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 54 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 55 |
/// CHECK-DAG: <<Select:i\d+>> Select [<<Const20>>,<<Const10>>,<<Bool>>] |
| 56 |
/// CHECK-DAG: Return [<<Select>>] |
| 57 |
private static int $noinline$testSimpleDiamondDifferentValue(boolean bool_param) { |
| 58 |
int return_value; |
| 59 |
if (bool_param) { |
| 60 |
return_value = 10; |
| 61 |
} else { |
| 62 |
return_value = 20; |
| 63 |
} |
| 64 |
return return_value; |
| 65 |
} |
| 66 |
|
| 67 |
// Check that we don't generate a select since we don't have no Phi (not even at the builder |
| 68 |
// stage) since all values are the same. |
| 69 |
|
| 70 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValue(boolean, boolean) builder (after) |
| 71 |
/// CHECK-NOT: Phi |
| 72 |
|
| 73 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValue(boolean, boolean) select_generator (before) |
| 74 |
/// CHECK-NOT: Phi |
| 75 |
|
| 76 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValue(boolean, boolean) select_generator (after) |
| 77 |
/// CHECK-NOT: Phi |
| 78 |
|
| 79 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValue(boolean, boolean) select_generator (after) |
| 80 |
/// CHECK-NOT: Select |
| 81 |
private static int $noinline$testDoubleDiamondSameValue(boolean bool_param_1, boolean bool_param_2) { |
| 82 |
int return_value; |
| 83 |
if (bool_param_1) { |
| 84 |
return_value = 10; |
| 85 |
} else { |
| 86 |
if (bool_param_2) { |
| 87 |
return_value = 10; |
| 88 |
} else { |
| 89 |
return_value = 10; |
| 90 |
} |
| 91 |
} |
| 92 |
return return_value; |
| 93 |
} |
| 94 |
|
| 95 |
// Check that we generate a select for a double diamond pattern, with a different value in the outer branch. |
| 96 |
|
| 97 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuter(boolean, boolean) select_generator (before) |
| 98 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 99 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 100 |
/// CHECK-DAG: <<Phi:i\d+>> Phi [<<Arg1:i\d+>>,<<Arg2:i\d+>>,<<Arg3:i\d+>>] |
| 101 |
/// CHECK-DAG: Return [<<Phi>>] |
| 102 |
/// CHECK-EVAL: set(["<<Arg1>>","<<Arg2>>","<<Arg3>>"]) == set(["<<Const10>>","<<Const20>>","<<Const20>>"]) |
| 103 |
|
| 104 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuter(boolean, boolean) select_generator (after) |
| 105 |
/// CHECK-DAG: <<Bool1:z\d+>> ParameterValue |
| 106 |
/// CHECK-DAG: <<Bool2:z\d+>> ParameterValue |
| 107 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 108 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 109 |
/// CHECK-DAG: <<Select:i\d+>> Select [<<Const20>>,<<Const20>>,<<Bool2>>] |
| 110 |
/// CHECK-DAG: <<Select2:i\d+>> Select [<<Select>>,<<Const10>>,<<Bool1>>] |
| 111 |
/// CHECK-DAG: Return [<<Select2>>] |
| 112 |
private static int $noinline$testDoubleDiamondSameValueButNotAllOuter(boolean bool_param_1, boolean bool_param_2) { |
| 113 |
int return_value; |
| 114 |
if (bool_param_1) { |
| 115 |
return_value = 10; |
| 116 |
} else { |
| 117 |
if (bool_param_2) { |
| 118 |
return_value = 20; |
| 119 |
} else { |
| 120 |
return_value = 20; |
| 121 |
} |
| 122 |
} |
| 123 |
return return_value; |
| 124 |
} |
| 125 |
|
| 126 |
// Check that we generate a select for a double diamond pattern, with a different value in the inner branch. |
| 127 |
|
| 128 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllInner(boolean, boolean) select_generator (before) |
| 129 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 130 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 131 |
/// CHECK-DAG: <<Phi:i\d+>> Phi [<<Arg1:i\d+>>,<<Arg2:i\d+>>,<<Arg3:i\d+>>] |
| 132 |
/// CHECK-DAG: Return [<<Phi>>] |
| 133 |
/// CHECK-EVAL: set(["<<Arg1>>","<<Arg2>>","<<Arg3>>"]) == set(["<<Const10>>","<<Const20>>","<<Const20>>"]) |
| 134 |
|
| 135 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllInner(boolean, boolean) select_generator (after) |
| 136 |
/// CHECK-DAG: <<Bool1:z\d+>> ParameterValue |
| 137 |
/// CHECK-DAG: <<Bool2:z\d+>> ParameterValue |
| 138 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 139 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 140 |
/// CHECK-DAG: <<Select:i\d+>> Select [<<Const20>>,<<Const10>>,<<Bool2>>] |
| 141 |
/// CHECK-DAG: <<Select2:i\d+>> Select [<<Select>>,<<Const20>>,<<Bool1>>] |
| 142 |
/// CHECK-DAG: Return [<<Select2>>] |
| 143 |
private static int $noinline$testDoubleDiamondSameValueButNotAllInner(boolean bool_param_1, boolean bool_param_2) { |
| 144 |
int return_value; |
| 145 |
if (bool_param_1) { |
| 146 |
return_value = 20; |
| 147 |
} else { |
| 148 |
if (bool_param_2) { |
| 149 |
return_value = 10; |
| 150 |
} else { |
| 151 |
return_value = 20; |
| 152 |
} |
| 153 |
} |
| 154 |
return return_value; |
| 155 |
} |
| 156 |
|
| 157 |
// Check that we generate a select for a double diamond pattern, with a all different values. |
| 158 |
|
| 159 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondDifferentValue(boolean, boolean) select_generator (before) |
| 160 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 161 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 162 |
/// CHECK-DAG: <<Const30:i\d+>> IntConstant 30 |
| 163 |
/// CHECK-DAG: <<Phi:i\d+>> Phi [<<Arg1:i\d+>>,<<Arg2:i\d+>>,<<Arg3:i\d+>>] |
| 164 |
/// CHECK-DAG: Return [<<Phi>>] |
| 165 |
/// CHECK-EVAL: set(["<<Arg1>>","<<Arg2>>","<<Arg3>>"]) == set(["<<Const10>>","<<Const20>>","<<Const30>>"]) |
| 166 |
|
| 167 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondDifferentValue(boolean, boolean) select_generator (after) |
| 168 |
/// CHECK-DAG: <<Bool1:z\d+>> ParameterValue |
| 169 |
/// CHECK-DAG: <<Bool2:z\d+>> ParameterValue |
| 170 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 171 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 172 |
/// CHECK-DAG: <<Const30:i\d+>> IntConstant 30 |
| 173 |
/// CHECK-DAG: <<Select:i\d+>> Select [<<Const30>>,<<Const20>>,<<Bool2>>] |
| 174 |
/// CHECK-DAG: <<Select2:i\d+>> Select [<<Select>>,<<Const10>>,<<Bool1>>] |
| 175 |
/// CHECK-DAG: Return [<<Select2>>] |
| 176 |
private static int $noinline$testDoubleDiamondDifferentValue(boolean bool_param_1, boolean bool_param_2) { |
| 177 |
int return_value; |
| 178 |
if (bool_param_1) { |
| 179 |
return_value = 10; |
| 180 |
} else { |
| 181 |
if (bool_param_2) { |
| 182 |
return_value = 20; |
| 183 |
} else { |
| 184 |
return_value = 30; |
| 185 |
} |
| 186 |
} |
| 187 |
return return_value; |
| 188 |
} |
| 189 |
|
| 190 |
private static void assertEquals(int expected, int actual) { |
| 191 |
if (expected != actual) { |
| 192 |
throw new AssertionError("Expected " + expected + " got " + actual); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// Check that we don't generate a select since we only have a single return. |
| 197 |
|
| 198 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) builder (after) |
| 199 |
/// CHECK: <<Const10:i\d+>> IntConstant 10 |
| 200 |
/// CHECK: Return [<<Const10>>] |
| 201 |
|
| 202 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) builder (after) |
| 203 |
/// CHECK: Return |
| 204 |
/// CHECK-NOT: Return |
| 205 |
|
| 206 |
private static int $noinline$testSimpleDiamondSameValueWithReturn(boolean bool_param) { |
| 207 |
if (bool_param) { |
| 208 |
return 10; |
| 209 |
} else { |
| 210 |
return 10; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// Same as testSimpleDiamondDifferentValue, but branches return. |
| 215 |
|
| 216 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValueWithReturn(boolean) select_generator (before) |
| 217 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 218 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 219 |
/// CHECK-DAG: Return [<<Const10>>] |
| 220 |
/// CHECK-DAG: Return [<<Const20>>] |
| 221 |
|
| 222 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondDifferentValueWithReturn(boolean) select_generator (after) |
| 223 |
/// CHECK-DAG: <<Bool:z\d+>> ParameterValue |
| 224 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 225 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 226 |
/// CHECK-DAG: <<Select:i\d+>> Select [<<Const20>>,<<Const10>>,<<Bool>>] |
| 227 |
/// CHECK-DAG: Return [<<Select>>] |
| 228 |
private static int $noinline$testSimpleDiamondDifferentValueWithReturn(boolean bool_param) { |
| 229 |
if (bool_param) { |
| 230 |
return 10; |
| 231 |
} else { |
| 232 |
return 20; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// Check that we don't generate a select since we only have a single return. |
| 237 |
|
| 238 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) builder (after) |
| 239 |
/// CHECK: <<Const10:i\d+>> IntConstant 10 |
| 240 |
/// CHECK: Return [<<Const10>>] |
| 241 |
|
| 242 |
/// CHECK-START: int Main.$noinline$testSimpleDiamondSameValueWithReturn(boolean) builder (after) |
| 243 |
/// CHECK: Return |
| 244 |
/// CHECK-NOT: Return |
| 245 |
private static int $noinline$testDoubleDiamondSameValueWithReturn(boolean bool_param_1, boolean bool_param_2) { |
| 246 |
if (bool_param_1) { |
| 247 |
return 10; |
| 248 |
} else { |
| 249 |
if (bool_param_2) { |
| 250 |
return 10; |
| 251 |
} else { |
| 252 |
return 10; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// Same as testDoubleDiamondSameValueButNotAllOuter, but branches return. |
| 258 |
|
| 259 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(boolean, boolean) select_generator (before) |
| 260 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 261 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 262 |
/// CHECK-DAG: Return [<<Const10>>] |
| 263 |
/// CHECK-DAG: Return [<<Const20>>] |
| 264 |
|
| 265 |
// Note that we have 2 returns instead of 3 as the two `return 20;` get merged into one before `select_generator`. |
| 266 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(boolean, boolean) select_generator (before) |
| 267 |
/// CHECK: Return |
| 268 |
/// CHECK: Return |
| 269 |
/// CHECK-NOT: Return |
| 270 |
|
| 271 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(boolean, boolean) select_generator (after) |
| 272 |
/// CHECK-DAG: <<Bool1:z\d+>> ParameterValue |
| 273 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 274 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 275 |
/// CHECK-DAG: <<Select2:i\d+>> Select [<<Const20>>,<<Const10>>,<<Bool1>>] |
| 276 |
/// CHECK-DAG: Return [<<Select2>>] |
| 277 |
private static int $noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(boolean bool_param_1, boolean bool_param_2) { |
| 278 |
if (bool_param_1) { |
| 279 |
return 10; |
| 280 |
} else { |
| 281 |
if (bool_param_2) { |
| 282 |
return 20; |
| 283 |
} else { |
| 284 |
return 20; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
// Same as testDoubleDiamondSameValueButNotAllInner, but branches return. |
| 290 |
|
| 291 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllInnerWithReturn(boolean, boolean) select_generator (before) |
| 292 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 293 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 294 |
/// CHECK-DAG: Return [<<Const10>>] |
| 295 |
/// CHECK-DAG: Return [<<Const20>>] |
| 296 |
/// CHECK-DAG: Return [<<Const20>>] |
| 297 |
|
| 298 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondSameValueButNotAllInnerWithReturn(boolean, boolean) select_generator (after) |
| 299 |
/// CHECK-DAG: <<Bool1:z\d+>> ParameterValue |
| 300 |
/// CHECK-DAG: <<Bool2:z\d+>> ParameterValue |
| 301 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 302 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 303 |
/// CHECK-DAG: <<Select:i\d+>> Select [<<Const20>>,<<Const10>>,<<Bool2>>] |
| 304 |
/// CHECK-DAG: <<Select2:i\d+>> Select [<<Select>>,<<Const20>>,<<Bool1>>] |
| 305 |
/// CHECK-DAG: Return [<<Select2>>] |
| 306 |
private static int $noinline$testDoubleDiamondSameValueButNotAllInnerWithReturn(boolean bool_param_1, boolean bool_param_2) { |
| 307 |
if (bool_param_1) { |
| 308 |
return 20; |
| 309 |
} else { |
| 310 |
if (bool_param_2) { |
| 311 |
return 10; |
| 312 |
} else { |
| 313 |
return 20; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
// Same as testDoubleDiamondDifferentValue, but branches return. |
| 319 |
|
| 320 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondDifferentValueWithReturn(boolean, boolean) select_generator (before) |
| 321 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 322 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 323 |
/// CHECK-DAG: <<Const30:i\d+>> IntConstant 30 |
| 324 |
/// CHECK-DAG: Return [<<Const10>>] |
| 325 |
/// CHECK-DAG: Return [<<Const20>>] |
| 326 |
/// CHECK-DAG: Return [<<Const30>>] |
| 327 |
|
| 328 |
/// CHECK-START: int Main.$noinline$testDoubleDiamondDifferentValueWithReturn(boolean, boolean) select_generator (after) |
| 329 |
/// CHECK-DAG: <<Bool1:z\d+>> ParameterValue |
| 330 |
/// CHECK-DAG: <<Bool2:z\d+>> ParameterValue |
| 331 |
/// CHECK-DAG: <<Const10:i\d+>> IntConstant 10 |
| 332 |
/// CHECK-DAG: <<Const20:i\d+>> IntConstant 20 |
| 333 |
/// CHECK-DAG: <<Const30:i\d+>> IntConstant 30 |
| 334 |
/// CHECK-DAG: <<Select:i\d+>> Select [<<Const30>>,<<Const20>>,<<Bool2>>] |
| 335 |
/// CHECK-DAG: <<Select2:i\d+>> Select [<<Select>>,<<Const10>>,<<Bool1>>] |
| 336 |
/// CHECK-DAG: Return [<<Select2>>] |
| 337 |
private static int $noinline$testDoubleDiamondDifferentValueWithReturn(boolean bool_param_1, boolean bool_param_2) { |
| 338 |
if (bool_param_1) { |
| 339 |
return 10; |
| 340 |
} else { |
| 341 |
if (bool_param_2) { |
| 342 |
return 20; |
| 343 |
} else { |
| 344 |
return 30; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
public static void main(String[] args) throws Throwable { |
| 350 |
// With phi |
| 351 |
assertEquals(10, $noinline$testSimpleDiamondSameValue(false)); |
| 352 |
assertEquals(20, $noinline$testSimpleDiamondDifferentValue(false)); |
| 353 |
assertEquals(10, $noinline$testDoubleDiamondSameValue(false, false)); |
| 354 |
assertEquals(20, $noinline$testDoubleDiamondSameValueButNotAllOuter(false, false)); |
| 355 |
assertEquals(20, $noinline$testDoubleDiamondSameValueButNotAllInner(false, false)); |
| 356 |
assertEquals(30, $noinline$testDoubleDiamondDifferentValue(false, false)); |
| 357 |
|
| 358 |
// With return |
| 359 |
assertEquals(10, $noinline$testSimpleDiamondSameValueWithReturn(false)); |
| 360 |
assertEquals(20, $noinline$testSimpleDiamondDifferentValueWithReturn(false)); |
| 361 |
assertEquals(10, $noinline$testDoubleDiamondSameValueWithReturn(false, false)); |
| 362 |
assertEquals(20, $noinline$testDoubleDiamondSameValueButNotAllOuterWithReturn(false, false)); |
| 363 |
assertEquals(20, $noinline$testDoubleDiamondSameValueButNotAllInnerWithReturn(false, false)); |
| 364 |
assertEquals(30, $noinline$testDoubleDiamondDifferentValueWithReturn(false, false)); |
| 365 |
} |
| 366 |
} |