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$ is a marker for the optimizing compiler to test
18 // it does compile the method.
19 
20 public class Main {
21 
expectEquals(int expected, int value)22   public static void expectEquals(int expected, int value) {
23     if (expected != value) {
24       throw new Error("Expected: " + expected + ", found: " + value);
25     }
26   }
27 
main(String[] args)28   public static void main(String[] args) {
29     int result = $opt$testIfEq1(42);
30     expectEquals(42, result);
31 
32     result = $opt$testIfEq2(42);
33     expectEquals(7, result);
34 
35     result = $opt$testWhileLoop(42);
36     expectEquals(45, result);
37 
38     result = $opt$testDoWhileLoop(42);
39     expectEquals(45, result);
40 
41     result = $opt$testForLoop(42);
42     expectEquals(44, result);
43 
44     result = $opt$testIfWithLocal(5);
45     expectEquals(7, result);
46   }
47 
$opt$testIfEq1(int a)48   static int $opt$testIfEq1(int a) {
49     if (a + 1 == 43) {
50       return 42;
51     } else {
52       return 7;
53     }
54   }
55 
$opt$testIfEq2(int a)56   static int $opt$testIfEq2(int a) {
57     if (a + 1 == 41) {
58       return 42;
59     } else {
60       return 7;
61     }
62   }
63 
$opt$testWhileLoop(int a)64   static int $opt$testWhileLoop(int a) {
65     while (a++ != 44) {}
66     return a;
67   }
68 
$opt$testDoWhileLoop(int a)69   static int $opt$testDoWhileLoop(int a) {
70     do {
71     } while (a++ != 44);
72     return a;
73   }
74 
$opt$testForLoop(int a)75   static int $opt$testForLoop(int a) {
76     for (; a != 44; a++) {}
77     return a;
78   }
79 
$opt$testIfWithLocal(int a)80   static int $opt$testIfWithLocal(int a) {
81     if (a == 5) {
82       int f = 2;
83       a += f;
84     }
85     // The SSA builder should not create a phi for f.
86 
87     return a;
88   }
89 }
90