blob: 96918d3e3aba4fb56d150997140a1dc193312f63 [file] [log] [blame]
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001/*
2* Copyright (C) 2015 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
17public class Main {
18
19 // CHECK-START: int Main.div() licm (before)
David Brazdilc74652862015-05-13 17:50:09 +010020 // CHECK-DAG: Div loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000021
22 // CHECK-START: int Main.div() licm (after)
David Brazdilc74652862015-05-13 17:50:09 +010023 // CHECK-NOT: Div loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000024
25 // CHECK-START: int Main.div() licm (after)
David Brazdilc74652862015-05-13 17:50:09 +010026 // CHECK-DAG: Div loop:none
Nicolas Geoffray82091da2015-01-26 10:02:45 +000027
28 public static int div() {
29 int result = 0;
30 for (int i = 0; i < 10; ++i) {
31 result += staticField / 42;
32 }
33 return result;
34 }
35
36 // CHECK-START: int Main.innerDiv() licm (before)
David Brazdilc74652862015-05-13 17:50:09 +010037 // CHECK-DAG: Div loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000038
39 // CHECK-START: int Main.innerDiv() licm (after)
David Brazdilc74652862015-05-13 17:50:09 +010040 // CHECK-NOT: Div loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000041
42 // CHECK-START: int Main.innerDiv() licm (after)
David Brazdilc74652862015-05-13 17:50:09 +010043 // CHECK-DAG: Div loop:none
Nicolas Geoffray82091da2015-01-26 10:02:45 +000044
45 public static int innerDiv() {
46 int result = 0;
47 for (int i = 0; i < 10; ++i) {
48 for (int j = 0; j < 10; ++j) {
49 result += staticField / 42;
50 }
51 }
52 return result;
53 }
54
55 // CHECK-START: int Main.innerDiv2() licm (before)
David Brazdilc74652862015-05-13 17:50:09 +010056 // CHECK-DAG: Mul loop:B4
Nicolas Geoffray82091da2015-01-26 10:02:45 +000057
58 // CHECK-START: int Main.innerDiv2() licm (after)
David Brazdilc74652862015-05-13 17:50:09 +010059 // CHECK-DAG: Mul loop:B2
Nicolas Geoffray82091da2015-01-26 10:02:45 +000060
61 public static int innerDiv2() {
62 int result = 0;
63 for (int i = 0; i < 10; ++i) {
64 for (int j = 0; j < 10; ++j) {
65 // The operation has been hoisted out of the inner loop.
66 // Note that we depend on the compiler's block numbering to
67 // check if it has been moved.
68 result += staticField * i;
69 }
70 }
71 return result;
72 }
73
74 // CHECK-START: int Main.innerDiv3(int, int) licm (before)
David Brazdilc74652862015-05-13 17:50:09 +010075 // CHECK-DAG: Div loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000076
77 // CHECK-START: int Main.innerDiv3(int, int) licm (after)
David Brazdilc74652862015-05-13 17:50:09 +010078 // CHECK-DAG: Div loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000079
80 public static int innerDiv3(int a, int b) {
81 int result = 0;
82 while (b < 5) {
83 // a might be null, so we can't hoist the operation.
84 result += staticField / a;
85 b++;
86 }
87 return result;
88 }
89
90 // CHECK-START: int Main.arrayLength(int[]) licm (before)
David Brazdilc2c48ff2015-05-15 14:24:31 +010091 // CHECK-DAG: <<NullCheck:l\d+>> NullCheck loop:{{B\d+}}
David Brazdilc57397b2015-05-15 16:01:59 +010092 // CHECK-DAG: ArrayLength [<<NullCheck>>] loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000093
94 // CHECK-START: int Main.arrayLength(int[]) licm (after)
David Brazdilc74652862015-05-13 17:50:09 +010095 // CHECK-NOT: NullCheck loop:{{B\d+}}
96 // CHECK-NOT: ArrayLength loop:{{B\d+}}
Nicolas Geoffray82091da2015-01-26 10:02:45 +000097
98 // CHECK-START: int Main.arrayLength(int[]) licm (after)
David Brazdilc2c48ff2015-05-15 14:24:31 +010099 // CHECK-DAG: <<NullCheck:l\d+>> NullCheck loop:none
David Brazdilc57397b2015-05-15 16:01:59 +0100100 // CHECK-DAG: ArrayLength [<<NullCheck>>] loop:none
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000101
102 public static int arrayLength(int[] array) {
103 int result = 0;
104 for (int i = 0; i < array.length; ++i) {
105 result += array[i];
106 }
107 return result;
108 }
109
110 public static int staticField = 42;
111
112 public static void assertEquals(int expected, int actual) {
113 if (expected != actual) {
114 throw new Error("Expected " + expected + ", got " + actual);
115 }
116 }
117
118 public static void main(String[] args) {
119 assertEquals(10, div());
120 assertEquals(100, innerDiv());
121 assertEquals(12, arrayLength(new int[] { 4, 8 }));
122 }
123}