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