1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import jdk.incubator.code.CodeReflection; 25 26 /* 27 * @test 28 * @summary Smoke test for code reflection with if statements. 29 * @modules jdk.incubator.code 30 * @build IfTest 31 * @build CodeReflectionTester 32 * @run main CodeReflectionTester IfTest 33 */ 34 35 public class IfTest { 36 @CodeReflection 37 @IR(""" 38 func @"test1" (%0 : IfTest, %1 : int)void -> { 39 %2 : Var<int> = var %1 @"i"; 40 java.if 41 ()boolean -> { 42 %3 : int = var.load %2; 43 %4 : int = constant @"1"; 44 %5 : boolean = lt %3 %4; 45 yield %5; 46 } 47 ^then()void -> { 48 %6 : int = constant @"1"; 49 var.store %2 %6; 50 yield; 51 } 52 ^else()void -> { 53 yield; 54 }; 55 return; 56 }; 57 """) 58 void test1(int i) { 59 if (i < 1) { 60 i = 1; 61 } 62 } 63 64 @CodeReflection 65 @IR(""" 66 func @"test2" (%0 : IfTest, %1 : int)void -> { 67 %2 : Var<int> = var %1 @"i"; 68 java.if 69 ()boolean -> { 70 %3 : int = var.load %2; 71 %4 : int = constant @"1"; 72 %5 : boolean = lt %3 %4; 73 yield %5; 74 } 75 ^then()void -> { 76 %6 : int = constant @"1"; 77 var.store %2 %6; 78 yield; 79 } 80 ^else()void -> { 81 %7 : int = constant @"2"; 82 var.store %2 %7; 83 yield; 84 }; 85 return; 86 }; 87 """) 88 void test2(int i) { 89 if (i < 1) { 90 i = 1; 91 } else { 92 i = 2; 93 } 94 } 95 96 @CodeReflection 97 @IR(""" 98 func @"test3" (%0 : IfTest, %1 : int)void -> { 99 %2 : Var<int> = var %1 @"i"; 100 java.if 101 ()boolean -> { 102 %3 : int = var.load %2; 103 %4 : int = constant @"1"; 104 %5 : boolean = lt %3 %4; 105 yield %5; 106 } 107 ^then()void -> { 108 %6 : int = constant @"1"; 109 var.store %2 %6; 110 yield; 111 } 112 ^else_if()boolean -> { 113 %7 : int = var.load %2; 114 %8 : int = constant @"2"; 115 %9 : boolean = lt %7 %8; 116 yield %9; 117 } 118 ^then()void -> { 119 %10 : int = constant @"2"; 120 var.store %2 %10; 121 yield; 122 } 123 ^else()void -> { 124 yield; 125 }; 126 return; 127 }; 128 """) 129 void test3(int i) { 130 if (i < 1) { 131 i = 1; 132 } else if (i < 2) { 133 i = 2; 134 } 135 } 136 137 @CodeReflection 138 @IR(""" 139 func @"test4" (%0 : IfTest, %1 : int)void -> { 140 %2 : Var<int> = var %1 @"i"; 141 java.if 142 ()boolean -> { 143 %3 : int = var.load %2; 144 %4 : int = constant @"1"; 145 %5 : boolean = lt %3 %4; 146 yield %5; 147 } 148 ^then()void -> { 149 %6 : int = constant @"1"; 150 var.store %2 %6; 151 yield; 152 } 153 ^else_if()boolean -> { 154 %7 : int = var.load %2; 155 %8 : int = constant @"2"; 156 %9 : boolean = lt %7 %8; 157 yield %9; 158 } 159 ^then()void -> { 160 %10 : int = constant @"2"; 161 var.store %2 %10; 162 yield; 163 } 164 ^else()void -> { 165 %11 : int = constant @"3"; 166 var.store %2 %11; 167 yield; 168 }; 169 return; 170 }; 171 """) 172 void test4(int i) { 173 if (i < 1) { 174 i = 1; 175 } else if (i < 2) { 176 i = 2; 177 } else { 178 i = 3; 179 } 180 } 181 182 @IR(""" 183 func @"test5" (%0 : IfTest, %1 : int)int -> { 184 %2 : Var<int> = var %1 @"i"; 185 java.if 186 ()boolean -> { 187 %3 : int = var.load %2; 188 %4 : int = constant @"1"; 189 %5 : boolean = lt %3 %4; 190 yield %5; 191 } 192 ^then()void -> { 193 %6 : int = constant @"1"; 194 return %6; 195 } 196 ^else_if()boolean -> { 197 %7 : int = var.load %2; 198 %8 : int = constant @"2"; 199 %9 : boolean = lt %7 %8; 200 yield %9; 201 } 202 ^then()void -> { 203 %10 : int = constant @"2"; 204 return %10; 205 } 206 ^else()void -> { 207 %11 : int = constant @"3"; 208 return %11; 209 }; 210 unreachable; 211 }; 212 """) 213 @CodeReflection 214 int test5(int i) { 215 if (i < 1) { 216 return 1; 217 } else if (i < 2) { 218 return 2; 219 } else { 220 return 3; 221 } 222 } 223 224 @CodeReflection 225 @IR(""" 226 func @"test6" (%0 : IfTest, %1 : int)void -> { 227 %2 : Var<int> = var %1 @"i"; 228 java.if 229 ()boolean -> { 230 %3 : int = var.load %2; 231 %4 : int = constant @"1"; 232 %5 : boolean = lt %3 %4; 233 yield %5; 234 } 235 ^then()void -> { 236 %6 : int = constant @"1"; 237 var.store %2 %6; 238 yield; 239 } 240 ^else()void -> { 241 yield; 242 }; 243 return; 244 }; 245 """) 246 void test6(int i) { 247 if (i < 1) 248 i = 1; 249 } 250 251 @CodeReflection 252 @IR(""" 253 func @"test7" (%0 : IfTest, %1 : int)void -> { 254 %2 : Var<int> = var %1 @"i"; 255 java.if 256 ()boolean -> { 257 %3 : int = var.load %2; 258 %4 : int = constant @"1"; 259 %5 : boolean = lt %3 %4; 260 yield %5; 261 } 262 ^then()void -> { 263 %6 : int = constant @"1"; 264 var.store %2 %6; 265 yield; 266 } 267 ^else()void -> { 268 %7 : int = constant @"2"; 269 var.store %2 %7; 270 yield; 271 }; 272 return; 273 }; 274 """) 275 void test7(int i) { 276 if (i < 1) 277 i = 1; 278 else 279 i = 2; 280 } 281 282 @CodeReflection 283 @IR(""" 284 func @"test8" (%0 : IfTest, %1 : int)void -> { 285 %2 : Var<int> = var %1 @"i"; 286 java.if 287 ()boolean -> { 288 %3 : int = var.load %2; 289 %4 : int = constant @"1"; 290 %5 : boolean = lt %3 %4; 291 yield %5; 292 } 293 ^then()void -> { 294 %6 : int = constant @"1"; 295 var.store %2 %6; 296 yield; 297 } 298 ^else_if()boolean -> { 299 %7 : int = var.load %2; 300 %8 : int = constant @"2"; 301 %9 : boolean = lt %7 %8; 302 yield %9; 303 } 304 ^then()void -> { 305 %10 : int = constant @"2"; 306 var.store %2 %10; 307 yield; 308 } 309 ^else()void -> { 310 %11 : int = constant @"3"; 311 var.store %2 %11; 312 yield; 313 }; 314 return; 315 }; 316 """) 317 void test8(int i) { 318 if (i < 1) 319 i = 1; 320 else if (i < 2) 321 i = 2; 322 else 323 i = 3; 324 } 325 326 @IR(""" 327 func @"test9" (%0 : java.lang.Boolean)void -> { 328 %1 : Var<java.lang.Boolean> = var %0 @"b"; 329 %3 : Var<int> = var @"i"; 330 java.if 331 ()boolean -> { 332 %4 : java.lang.Boolean = var.load %1; 333 %5 : boolean = invoke %4 @"java.lang.Boolean::booleanValue()boolean"; 334 yield %5; 335 } 336 ()void -> { 337 %6 : int = constant @"1"; 338 var.store %3 %6; 339 yield; 340 } 341 ()void -> { 342 %7 : int = constant @"2"; 343 var.store %3 %7; 344 yield; 345 }; 346 return; 347 }; 348 """) 349 @CodeReflection 350 static void test9(Boolean b) { 351 int i; 352 if (b) { 353 i = 1; 354 } else { 355 i = 2; 356 } 357 } 358 }