1 /* 2 * Copyright (C) 2017 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 * 18 * L: Break operator doesn't break one basic block limitation for int, 19 * for some reasons (most probably not a bug), there are two basic blocks for long type, 20 * 1 sinking expected for int, 0 for long. 21 * M: no limitations on basic blocks number, 1 constant calculation sinking expected for 22 * each method 23 * 24 **/ 25 26 public class Main { 27 28 final int iterations = 1100; 29 assertIntEquals(int expected, int result)30 public static void assertIntEquals(int expected, int result) { 31 if (expected != result) { 32 throw new Error("Expected: " + expected + ", found: " + result); 33 } 34 } 35 assertLongEquals(long expected, long result)36 public static void assertLongEquals(long expected, long result) { 37 if (expected != result) { 38 throw new Error("Expected: " + expected + ", found: " + result); 39 } 40 } 41 42 testLoopAddInt()43 public int testLoopAddInt() { 44 int testVar = 10000; 45 int additionalVar = 10; 46 47 outer: 48 for (int i = 0; i < iterations; i++) { 49 additionalVar += i; 50 for (int k = 0; k < iterations; k++) { 51 additionalVar += k; 52 testVar += 5; 53 continue outer; 54 } 55 } 56 assertIntEquals(testVar + additionalVar, 619960); 57 return testVar + additionalVar; 58 } 59 testLoopSubInt()60 public int testLoopSubInt() { 61 int testVar = 10000; 62 int additionalVar = 10; 63 64 outer: 65 for (int i = 0; i < iterations; i++) { 66 additionalVar += i; 67 for (int k = 0; k < iterations; k++) { 68 additionalVar += k; 69 testVar -= 5; 70 continue outer; 71 } 72 } 73 assertIntEquals(testVar + additionalVar, 608960); 74 return testVar + additionalVar; 75 } 76 testLoopSubLong()77 public long testLoopSubLong() { 78 long testVar = 10000; 79 long additionalVar = 10; 80 81 outer: 82 for (long i = 0; i < iterations; i++) { 83 additionalVar += i; 84 for (long k = 0; k < iterations; k++) { 85 additionalVar += k; 86 testVar -= 5; 87 continue outer; 88 } 89 } 90 assertLongEquals(testVar + additionalVar, 608960); 91 return testVar + additionalVar; 92 } 93 testLoopMulInt(int n)94 public int testLoopMulInt(int n) { 95 int testVar = 1; 96 int additionalVar = 10; 97 98 outer: 99 for (int i = 0; i < 3; i++) { 100 additionalVar += i + n * 2; 101 for (int k = 0; k < 5; k++) { 102 additionalVar += k + n + k % 3 - i % 2 + n % 4 - i % 5 103 + (k + 2) / 7 - (i - 5) / 3 + k * 3 - k / 2; 104 testVar *= 6; 105 continue outer; 106 } 107 } 108 assertIntEquals(testVar + additionalVar, 324); 109 return testVar + additionalVar; 110 } 111 testLoopMulLong(long n)112 public long testLoopMulLong(long n) { 113 long testVar = 1; 114 long additionalVar = 10; 115 116 outer: 117 for (long i = 0; i < 5; i++) { 118 additionalVar += i + n; 119 for (long k = 0; k < 5; k++) { 120 additionalVar += k + n + k % 3 - i % 2 + n % 4 - i % 5 121 + (k + 2) / 7 - (i - 5) / 3 + k * 3 - k / 2; 122 testVar *= 6L; 123 continue outer; 124 } 125 } 126 assertLongEquals(testVar + additionalVar, 7897); 127 return testVar + additionalVar; 128 } 129 testLoopDivInt()130 public int testLoopDivInt() { 131 int testVar = 10000; 132 int additionalVar = 10; 133 134 outer: 135 for (int i = 0; i < iterations; i++) { 136 additionalVar += i; 137 for (int k = 0; k < iterations; k++) { 138 additionalVar += k; 139 testVar /= 5; 140 continue outer; 141 } 142 } 143 assertIntEquals(testVar + additionalVar, 604460); 144 return testVar + additionalVar; 145 } 146 testLoopDivLong()147 public long testLoopDivLong() { 148 long testVar = 10000; 149 long additionalVar = 10; 150 151 outer: 152 for (long i = 0; i < iterations; i++) { 153 additionalVar += i; 154 for (long k = 0; k < iterations; k++) { 155 additionalVar += k; 156 testVar /= 5; 157 continue outer; 158 } 159 } 160 assertLongEquals(testVar + additionalVar, 604460); 161 return testVar + additionalVar; 162 } 163 testLoopRemInt()164 public int testLoopRemInt() { 165 int testVar = 10000; 166 int additionalVar = 10; 167 168 outer: 169 for (int i = 0; i < iterations; i++) { 170 additionalVar += i; 171 for (int k = 0; k < iterations; k++) { 172 additionalVar += k; 173 testVar %= 5; 174 continue outer; 175 } 176 } 177 assertIntEquals(testVar + additionalVar, 604460); 178 return testVar + additionalVar; 179 } 180 testLoopRemLong()181 public long testLoopRemLong() { 182 long testVar = 10000; 183 long additionalVar = 10; 184 185 outer: 186 for (long i = 0; i < iterations; i++) { 187 additionalVar += i; 188 for (long k = 0; k < iterations; k++) { 189 additionalVar += k; 190 testVar %= 5; 191 continue outer; 192 } 193 } 194 assertLongEquals(testVar + additionalVar, 604460); 195 return testVar + additionalVar; 196 } 197 testLoopAddLong()198 public long testLoopAddLong() { 199 long testVar = 10000; 200 long additionalVar = 10; 201 202 outer: 203 for (long i = 0; i < iterations; i++) { 204 additionalVar += i; 205 for (long k = 0; k < iterations; k++) { 206 additionalVar += k; 207 testVar += 5; 208 continue outer; 209 } 210 } 211 assertLongEquals(testVar + additionalVar, 619960); 212 return testVar + additionalVar; 213 } 214 main(String[] args)215 public static void main(String[] args) { 216 Main obj = new Main(); 217 obj.testLoopAddInt(); 218 obj.testLoopAddLong(); 219 obj.testLoopRemLong(); 220 obj.testLoopRemInt(); 221 obj.testLoopDivLong(); 222 obj.testLoopDivInt(); 223 obj.testLoopMulLong(10); 224 obj.testLoopMulInt(10); 225 obj.testLoopSubLong(); 226 obj.testLoopSubInt(); 227 } 228 229 } 230