1 /*
2  * Copyright (C) 2014 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 // Note that $opt$reg$ is a marker for the optimizing compiler to ensure
18 // it does use its register allocator.
19 
20 public class Main {
main(String[] args)21   public static void main(String[] args) {
22 
23     expectEquals(4, $opt$reg$TestLostCopy());
24     expectEquals(-10, $opt$reg$TestTwoLive());
25     expectEquals(-20, $opt$reg$TestThreeLive());
26     expectEquals(5, $opt$reg$TestFourLive());
27     expectEquals(10, $opt$reg$TestMultipleLive());
28     expectEquals(1, $opt$reg$TestWithBreakAndContinue());
29     expectEquals(-15, $opt$reg$testSpillInIf(5, 6, 7));
30     expectEquals(-567, $opt$reg$TestAgressiveLive1(1, 2, 3, 4, 5, 6, 7));
31     expectEquals(-77, $opt$reg$TestAgressiveLive2(1, 2, 3, 4, 5, 6, 7));
32   }
33 
$opt$reg$TestLostCopy()34   public static int $opt$reg$TestLostCopy() {
35     int a = 0;
36     int b = 0;
37     do {
38       b = a;
39       a++;
40     } while (a != 5);
41     return b;
42   }
43 
$opt$reg$TestTwoLive()44   public static int $opt$reg$TestTwoLive() {
45     int a = 0;
46     int b = 0;
47     do {
48       a++;
49       b += 3;
50     } while (a != 5);
51     return a - b;
52   }
53 
$opt$reg$TestThreeLive()54   public static int $opt$reg$TestThreeLive() {
55     int a = 0;
56     int b = 0;
57     int c = 0;
58     do {
59       a++;
60       b += 3;
61       c += 2;
62     } while (a != 5);
63     return a - b - c;
64   }
65 
$opt$reg$TestFourLive()66   public static int $opt$reg$TestFourLive() {
67     int a = 0;
68     int b = 0;
69     int c = 0;
70     int d = 0;
71     do {
72       a++;
73       b += 3;
74       c += 2;
75       d++;
76     } while (a != 5);
77     return d;
78   }
79 
$opt$reg$TestMultipleLive()80   public static int $opt$reg$TestMultipleLive() {
81     int a = 0;
82     int b = 0;
83     int c = 0;
84     int d = 0;
85     int e = 0;
86     int f = 0;
87     int g = 0;
88     do {
89       a++;
90       b++;
91       c++;
92       d++;
93       e += 3;
94       f += 2;
95       g += 2;
96     } while (a != 5);
97     return f;
98   }
99 
$opt$reg$TestWithBreakAndContinue()100   public static int $opt$reg$TestWithBreakAndContinue() {
101     int a = 0;
102     int b = 0;
103     do {
104       a++;
105       if (a == 2) {
106         continue;
107       }
108       b++;
109       if (a == 5) {
110         break;
111       }
112     } while (true);
113     return a - b;
114   }
115 
$opt$reg$testSpillInIf(int a, int b, int c)116   public static int $opt$reg$testSpillInIf(int a, int b, int c) {
117     int d = 0;
118     int e = 0;
119     if (a == 5) {
120       b++;
121       c++;
122       d += 2;
123       e += 3;
124     }
125 
126     return a - b - c - d - e;
127   }
128 
$opt$reg$TestAgressiveLive1(int a, int b, int c, int d, int e, int f, int g)129   public static int $opt$reg$TestAgressiveLive1(int a, int b, int c, int d, int e, int f, int g) {
130     int h = a - b;
131     int i = c - d;
132     int j = e - f;
133     int k = 42 + g - a;
134     do {
135       b++;
136       while (k != 1) {
137         --k;
138         ++i;
139         if (i == 9) {
140           ++i;
141         }
142         j += 5;
143       }
144       k = 9;
145       h++;
146     } while (h != 5);
147     return a - b - c - d - e - f - g - h - i - j - k;
148   }
149 
$opt$reg$TestAgressiveLive2(int a, int b, int c, int d, int e, int f, int g)150   public static int $opt$reg$TestAgressiveLive2(int a, int b, int c, int d, int e, int f, int g) {
151     int h = a - b;
152     int i = c - d;
153     int j = e - f;
154     int k = 42 + g - a;
155     do {
156       h++;
157     } while (h != 5);
158     return a - b - c - d - e - f - g - h - i - j - k;
159   }
160 
expectEquals(int expected, int value)161   public static void expectEquals(int expected, int value) {
162     if (expected != value) {
163       throw new Error("Expected: " + expected + ", got: " + value);
164     }
165   }
166 }
167