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 * @modules jdk.incubator.code
29 * @summary test lowering of loops
30 * @build TestLoop
31 * @build CodeReflectionTester
32 * @run main CodeReflectionTester TestLoop
33 */
34
35 public class TestLoop {
36 @CodeReflection
37 @LoweredModel(value = """
38 func @"testFor" (%0 : java.type:"int[]")java.type:"int" -> {
39 %1 : Var<java.type:"int[]"> = var %0 @"a";
40 %2 : java.type:"int" = constant @0;
41 %3 : Var<java.type:"int"> = var %2 @"sum";
42 %4 : java.type:"int" = constant @0;
43 %5 : Var<java.type:"int"> = var %4 @"i";
44 branch ^block_0;
45
46 ^block_0:
47 %6 : java.type:"int" = var.load %5;
48 %7 : java.type:"int[]" = var.load %1;
49 %8 : java.type:"int" = array.length %7;
50 %9 : java.type:"boolean" = lt %6 %8;
51 cbranch %9 ^block_1 ^block_2;
52
53 ^block_1:
54 %10 : java.type:"int" = var.load %3;
55 %11 : java.type:"int[]" = var.load %1;
56 %12 : java.type:"int" = var.load %5;
57 %13 : java.type:"int" = array.load %11 %12;
58 %14 : java.type:"int" = add %10 %13;
59 var.store %3 %14;
60 branch ^block_3;
61
62 ^block_3:
63 %15 : java.type:"int" = var.load %5;
64 %16 : java.type:"int" = constant @1;
65 %17 : java.type:"int" = add %15 %16;
66 var.store %5 %17;
67 branch ^block_0;
68
69 ^block_2:
70 %18 : java.type:"int" = var.load %3;
71 return %18;
72 };
73 """, ssa = false)
74 static int testFor(int[] a) {
75 int sum = 0;
76 for (int i = 0; i < a.length; i++) {
77 sum += a[i];
78 }
79 return sum;
80 }
81
82 @CodeReflection
83 @LoweredModel(value = """
84 func @"testForSSA" (%0 : java.type:"int[]")java.type:"int" -> {
85 %1 : java.type:"int" = constant @0;
86 %2 : java.type:"int" = constant @0;
87 branch ^block_1(%2, %1);
88
89 ^block_1(%3 : java.type:"int", %4 : java.type:"int"):
90 %5 : java.type:"int" = array.length %0;
91 %6 : java.type:"boolean" = lt %3 %5;
92 cbranch %6 ^block_2 ^block_4;
93
94 ^block_2:
95 %7 : java.type:"int" = array.load %0 %3;
96 %8 : java.type:"int" = add %4 %7;
97 branch ^block_3;
98
99 ^block_3:
100 %9 : java.type:"int" = constant @1;
101 %10 : java.type:"int" = add %3 %9;
102 branch ^block_1(%10, %8);
103
104 ^block_4:
105 return %4;
106 };
107 """, ssa = true)
108 static int testForSSA(int[] a) {
109 int sum = 0;
110 for (int i = 0; i < a.length; i++) {
111 sum += a[i];
112 }
113 return sum;
114 }
115 }