blob: ce5bda1393e7282c29ef948b18adaf3bcaa12864 [file] [log] [blame]
Aart Bik807868e2016-11-03 17:51:43 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Regression tests for loop optimizations.
19 */
20public class Main {
21
22 /// CHECK-START: int Main.earlyExitFirst(int) loop_optimization (before)
23 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
24 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
25 //
26 /// CHECK-START: int Main.earlyExitFirst(int) loop_optimization (after)
27 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
28 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
29 static int earlyExitFirst(int m) {
30 int k = 0;
31 for (int i = 0; i < 10; i++) {
32 if (i == m) {
33 return k;
34 }
35 k++;
36 }
37 return k;
38 }
39
40 /// CHECK-START: int Main.earlyExitLast(int) loop_optimization (before)
41 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
42 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
43 //
44 /// CHECK-START: int Main.earlyExitLast(int) loop_optimization (after)
45 /// CHECK-DAG: Phi loop:<<Loop:B\d+>> outer_loop:none
46 /// CHECK-DAG: Phi loop:<<Loop>> outer_loop:none
47 static int earlyExitLast(int m) {
48 int k = 0;
49 for (int i = 0; i < 10; i++) {
50 k++;
51 if (i == m) {
52 return k;
53 }
54 }
55 return k;
56 }
57
58 /// CHECK-START: int Main.earlyExitNested() loop_optimization (before)
59 /// CHECK-DAG: Phi loop:<<Loop1:B\d+>> outer_loop:none
60 /// CHECK-DAG: Phi loop:<<Loop1>> outer_loop:none
61 /// CHECK-DAG: Phi loop:<<Loop2:B\d+>> outer_loop:<<Loop1>>
62 /// CHECK-DAG: Phi loop:<<Loop2>> outer_loop:<<Loop1>>
63 //
64 /// CHECK-START: int Main.earlyExitNested() loop_optimization (after)
65 /// CHECK-DAG: Phi loop:<<Loop1:B\d+>> outer_loop:none
66 /// CHECK-DAG: Phi loop:<<Loop1>> outer_loop:none
67 //
68 /// CHECK-START: int Main.earlyExitNested() loop_optimization (after)
69 /// CHECK-NOT: Phi loop:{{B\d+}} outer_loop:{{B\d+}}
70 static int earlyExitNested() {
71 int offset = 0;
72 for (int i = 0; i < 2; i++) {
73 int start = offset;
74 // This loop can be removed.
75 for (int j = 0; j < 2; j++) {
76 offset++;
77 }
78 if (i == 1) {
79 return start;
80 }
81 }
82 return 0;
83 }
84
85 public static void main(String[] args) {
86 expectEquals(10, earlyExitFirst(-1));
87 for (int i = 0; i <= 10; i++) {
88 expectEquals(i, earlyExitFirst(i));
89 }
90 expectEquals(10, earlyExitFirst(11));
91
92 expectEquals(10, earlyExitLast(-1));
93 for (int i = 0; i < 10; i++) {
94 expectEquals(i + 1, earlyExitLast(i));
95 }
96 expectEquals(10, earlyExitLast(10));
97 expectEquals(10, earlyExitLast(11));
98
99 expectEquals(2, earlyExitNested());
100
101 System.out.println("passed");
102 }
103
104 private static void expectEquals(int expected, int result) {
105 if (expected != result) {
106 throw new Error("Expected: " + expected + ", found: " + result);
107 }
108 }
109}