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