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 : int[])int -> {
 39                 %1 : Var<int[]> = var %0 @"a";
 40                 %2 : int = constant @"0";
 41                 %3 : Var<int> = var %2 @"sum";
 42                 %4 : int = constant @"0";
 43                 %5 : Var<int> = var %4 @"i";
 44                 branch ^block_0;
 45 
 46               ^block_0:
 47                 %6 : int = var.load %5;
 48                 %7 : int[] = var.load %1;
 49                 %8 : int = array.length %7;
 50                 %9 : boolean = lt %6 %8;
 51                 cbranch %9 ^block_1 ^block_2;
 52 
 53               ^block_1:
 54                 %10 : int = var.load %3;
 55                 %11 : int[] = var.load %1;
 56                 %12 : int = var.load %5;
 57                 %13 : int = array.load %11 %12;
 58                 %14 : int = add %10 %13;
 59                 var.store %3 %14;
 60                 branch ^block_3;
 61 
 62               ^block_3:
 63                 %15 : int = var.load %5;
 64                 %16 : int = constant @"1";
 65                 %17 : int = add %15 %16;
 66                 var.store %5 %17;
 67                 branch ^block_0;
 68 
 69               ^block_2:
 70                 %18 : 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 : int[])int -> {
 85                 %1 : int = constant @"0";
 86                 %2 : int = constant @"0";
 87                 branch ^block_1(%2, %1);
 88 
 89               ^block_1(%3 : int, %4 : int):
 90                 %5 : int = array.length %0;
 91                 %6 : boolean = lt %3 %5;
 92                 cbranch %6 ^block_2 ^block_4;
 93 
 94               ^block_2:
 95                 %7 : int = array.load %0 %3;
 96                 %8 : int = add %4 %7;
 97                 branch ^block_3;
 98 
 99               ^block_3:
100                 %9 : int = constant @"1";
101                 %10 : 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 }