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 java.lang.runtime.CodeReflection;
 25 
 26 /*
 27  * @test
 28  * @summary Smoke test for code reflection with if statements.
 29  * @build IfTest
 30  * @build CodeReflectionTester
 31  * @run main CodeReflectionTester IfTest
 32  */
 33 
 34 public class IfTest {
 35     @CodeReflection
 36     @IR("""
 37             func @"test1" (%0 : IfTest, %1 : int)void -> {
 38                 %2 : Var<int> = var %1 @"i";
 39                 java.if
 40                     ()boolean -> {
 41                         %3 : int = var.load %2;
 42                         %4 : int = constant @"1";
 43                         %5 : boolean = lt %3 %4;
 44                         yield %5;
 45                     }
 46                     ^then()void -> {
 47                         %6 : int = constant @"1";
 48                         var.store %2 %6;
 49                         yield;
 50                     }
 51                     ^else()void -> {
 52                         yield;
 53                     };
 54                 return;
 55             };
 56             """)
 57     void test1(int i) {
 58         if (i < 1) {
 59             i = 1;
 60         }
 61     }
 62 
 63     @CodeReflection
 64     @IR("""
 65             func @"test2" (%0 : IfTest, %1 : int)void -> {
 66                 %2 : Var<int> = var %1 @"i";
 67                 java.if
 68                     ()boolean -> {
 69                         %3 : int = var.load %2;
 70                         %4 : int = constant @"1";
 71                         %5 : boolean = lt %3 %4;
 72                         yield %5;
 73                     }
 74                     ^then()void -> {
 75                         %6 : int = constant @"1";
 76                         var.store %2 %6;
 77                         yield;
 78                     }
 79                     ^else()void -> {
 80                         %7 : int = constant @"2";
 81                         var.store %2 %7;
 82                         yield;
 83                     };
 84                 return;
 85             };
 86             """)
 87     void test2(int i) {
 88         if (i < 1) {
 89             i = 1;
 90         } else {
 91             i = 2;
 92         }
 93     }
 94 
 95     @CodeReflection
 96     @IR("""
 97             func @"test3" (%0 : IfTest, %1 : int)void -> {
 98                 %2 : Var<int> = var %1 @"i";
 99                 java.if
100                     ()boolean -> {
101                         %3 : int = var.load %2;
102                         %4 : int = constant @"1";
103                         %5 : boolean = lt %3 %4;
104                         yield %5;
105                     }
106                     ^then()void -> {
107                         %6 : int = constant @"1";
108                         var.store %2 %6;
109                         yield;
110                     }
111                     ^else_if()boolean -> {
112                         %7 : int = var.load %2;
113                         %8 : int = constant @"2";
114                         %9 : boolean = lt %7 %8;
115                         yield %9;
116                     }
117                     ^then()void -> {
118                         %10 : int = constant @"2";
119                         var.store %2 %10;
120                         yield;
121                     }
122                     ^else()void -> {
123                         yield;
124                     };
125                 return;
126             };
127             """)
128     void test3(int i) {
129         if (i < 1) {
130             i = 1;
131         } else if (i < 2) {
132             i = 2;
133         }
134     }
135 
136     @CodeReflection
137     @IR("""
138             func @"test4" (%0 : IfTest, %1 : int)void -> {
139                 %2 : Var<int> = var %1 @"i";
140                 java.if
141                     ()boolean -> {
142                         %3 : int = var.load %2;
143                         %4 : int = constant @"1";
144                         %5 : boolean = lt %3 %4;
145                         yield %5;
146                     }
147                     ^then()void -> {
148                         %6 : int = constant @"1";
149                         var.store %2 %6;
150                         yield;
151                     }
152                     ^else_if()boolean -> {
153                         %7 : int = var.load %2;
154                         %8 : int = constant @"2";
155                         %9 : boolean = lt %7 %8;
156                         yield %9;
157                     }
158                     ^then()void -> {
159                         %10 : int = constant @"2";
160                         var.store %2 %10;
161                         yield;
162                     }
163                     ^else()void -> {
164                         %11 : int = constant @"3";
165                         var.store %2 %11;
166                         yield;
167                     };
168                 return;
169             };
170             """)
171     void test4(int i) {
172         if (i < 1) {
173             i = 1;
174         } else if (i < 2) {
175             i = 2;
176         } else {
177             i = 3;
178         }
179     }
180 
181     @IR("""
182             func @"test5" (%0 : IfTest, %1 : int)int -> {
183               %2 : Var<int> = var %1 @"i";
184               java.if
185                   ()boolean -> {
186                       %3 : int = var.load %2;
187                       %4 : int = constant @"1";
188                       %5 : boolean = lt %3 %4;
189                       yield %5;
190                   }
191                   ^then()void -> {
192                       %6 : int = constant @"1";
193                       return %6;
194                   }
195                   ^else_if()boolean -> {
196                       %7 : int = var.load %2;
197                       %8 : int = constant @"2";
198                       %9 : boolean = lt %7 %8;
199                       yield %9;
200                   }
201                   ^then()void -> {
202                       %10 : int = constant @"2";
203                       return %10;
204                   }
205                   ^else()void -> {
206                       %11 : int = constant @"3";
207                       return %11;
208                   };
209               return;
210             };
211             """)
212     @CodeReflection
213     int test5(int i) {
214         if (i < 1) {
215             return 1;
216         } else if (i < 2) {
217             return 2;
218         } else {
219             return 3;
220         }
221     }
222 }