1b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray/*
2b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray *
4b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * you may not use this file except in compliance with the License.
6b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * You may obtain a copy of the License at
7b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray *
8b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray *
10b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * See the License for the specific language governing permissions and
14b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray * limitations under the License.
15b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray */
16b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
17b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray// Note that $opt$ is a marker for the optimizing compiler to ensure
18b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray// it does compile the method.
19b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
20b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffraypublic class Main {
21b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
22b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  public static void expectEquals(int expected, int value) {
23b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    if (expected != value) {
24b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray      throw new Error("Expected: " + expected + ", found: " + value);
25b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    }
26b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  }
27b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
28b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  public static void main(String[] args) {
29b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    int result = $opt$testIfEq1(42);
30b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    expectEquals(42, result);
31b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
32b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    result = $opt$testIfEq2(42);
33b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    expectEquals(7, result);
34b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
35b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    result = $opt$testWhileLoop(42);
36b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    expectEquals(45, result);
37b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
38b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    result = $opt$testDoWhileLoop(42);
39b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    expectEquals(45, result);
40b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
41b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    result = $opt$testForLoop(42);
42b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    expectEquals(44, result);
437c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray
447c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray    result = $opt$testIfWithLocal(5);
457c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray    expectEquals(7, result);
46b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  }
47b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
48b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  static int $opt$testIfEq1(int a) {
49b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    if (a + 1 == 43) {
50b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray      return 42;
51b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    } else {
52b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray      return 7;
53b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    }
54b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  }
55b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
56b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  static int $opt$testIfEq2(int a) {
57b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    if (a + 1 == 41) {
58b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray      return 42;
59b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    } else {
60b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray      return 7;
61b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    }
62b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  }
63b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
64b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  static int $opt$testWhileLoop(int a) {
65b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    while (a++ != 44) {}
66b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    return a;
67b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  }
68b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
69b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  static int $opt$testDoWhileLoop(int a) {
70b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    do {
71b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    } while (a++ != 44);
72b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    return a;
73b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  }
74b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray
75b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  static int $opt$testForLoop(int a) {
76b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    for (; a != 44; a++) {}
77b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray    return a;
78b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray  }
797c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray
807c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray  static int $opt$testIfWithLocal(int a) {
817c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray    if (a == 5) {
827c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray      int f = 2;
837c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray      a += f;
847c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray    }
857c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray    // The SSA builder should not create a phi for f.
867c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray
877c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray    return a;
887c3560f2ce0ec9484004d05a94bfaa6e02f5a96aNicolas Geoffray  }
89b55f835d66a61e5da6fc1895ba5a0482868c9552Nicolas Geoffray}
90