blob: d2275aad4ac9c896ab88597c34a40c1fcdb35159 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 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
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080017namespace art {
18
buzbee67bf8852011-08-17 17:51:35 -070019#define DEBUG_OPT(X)
20
21/* Check RAW, WAR, and WAR dependency on the register operands */
22#define CHECK_REG_DEP(use, def, check) ((def & check->useMask) || \
23 ((use | def) & check->defMask))
24
25/* Scheduler heuristics */
26#define MAX_HOIST_DISTANCE 20
27#define LDLD_DISTANCE 4
28#define LD_LATENCY 2
29
buzbee31a4a6f2012-02-28 15:36:15 -080030inline bool isDalvikRegisterClobbered(LIR* lir1, LIR* lir2)
buzbee67bf8852011-08-17 17:51:35 -070031{
Bill Buzbeea114add2012-05-03 15:00:40 -070032 int reg1Lo = DECODE_ALIAS_INFO_REG(lir1->aliasInfo);
33 int reg1Hi = reg1Lo + DECODE_ALIAS_INFO_WIDE(lir1->aliasInfo);
34 int reg2Lo = DECODE_ALIAS_INFO_REG(lir2->aliasInfo);
35 int reg2Hi = reg2Lo + DECODE_ALIAS_INFO_WIDE(lir2->aliasInfo);
buzbee67bf8852011-08-17 17:51:35 -070036
Bill Buzbeea114add2012-05-03 15:00:40 -070037 return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo);
buzbee67bf8852011-08-17 17:51:35 -070038}
39
40/* Convert a more expensive instruction (ie load) into a move */
buzbee31a4a6f2012-02-28 15:36:15 -080041void convertMemOpIntoMove(CompilationUnit* cUnit, LIR* origLIR, int dest,
42 int src)
buzbee67bf8852011-08-17 17:51:35 -070043{
Bill Buzbeea114add2012-05-03 15:00:40 -070044 /* Insert a move to replace the load */
45 LIR* moveLIR;
46 moveLIR = oatRegCopyNoInsert( cUnit, dest, src);
47 /*
48 * Insert the converted instruction after the original since the
49 * optimization is scannng in the top-down order and the new instruction
50 * will need to be re-checked (eg the new dest clobbers the src used in
51 * thisLIR).
52 */
53 oatInsertLIRAfter((LIR*) origLIR, (LIR*) moveLIR);
buzbee67bf8852011-08-17 17:51:35 -070054}
55
56/*
57 * Perform a pass of top-down walk, from the second-last instruction in the
58 * superblock, to eliminate redundant loads and stores.
59 *
60 * An earlier load can eliminate a later load iff
61 * 1) They are must-aliases
62 * 2) The native register is not clobbered in between
63 * 3) The memory location is not written to in between
64 *
65 * An earlier store can eliminate a later load iff
66 * 1) They are must-aliases
67 * 2) The native register is not clobbered in between
68 * 3) The memory location is not written to in between
69 *
70 * A later store can be eliminated by an earlier store iff
71 * 1) They are must-aliases
72 * 2) The memory location is not written to in between
73 */
buzbee31a4a6f2012-02-28 15:36:15 -080074void applyLoadStoreElimination(CompilationUnit* cUnit, LIR* headLIR,
75 LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -070076{
Bill Buzbeea114add2012-05-03 15:00:40 -070077 LIR* thisLIR;
buzbee67bf8852011-08-17 17:51:35 -070078
Bill Buzbeea114add2012-05-03 15:00:40 -070079 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -070080
Bill Buzbeea114add2012-05-03 15:00:40 -070081 for (thisLIR = PREV_LIR(tailLIR);
82 thisLIR != headLIR;
83 thisLIR = PREV_LIR(thisLIR)) {
84 int sinkDistance = 0;
buzbee67bf8852011-08-17 17:51:35 -070085
Bill Buzbeea114add2012-05-03 15:00:40 -070086 /* Skip non-interesting instructions */
87 if ((thisLIR->flags.isNop == true) ||
88 isPseudoOpcode(thisLIR->opcode) ||
jeffhaoe2962482012-06-28 11:29:57 -070089 (EncodingMap[thisLIR->opcode].flags & IS_BRANCH) ||
Bill Buzbeea114add2012-05-03 15:00:40 -070090 !(EncodingMap[thisLIR->opcode].flags & (IS_LOAD | IS_STORE))) {
91 continue;
92 }
buzbee67bf8852011-08-17 17:51:35 -070093
buzbeeb046e162012-10-30 15:48:42 -070094 int nativeRegId;
95 if (cUnit->instructionSet == kX86) {
96 // If x86, location differs depending on whether memory/reg operation.
97 nativeRegId = (EncodingMap[thisLIR->opcode].flags & IS_STORE) ? thisLIR->operands[2]
98 : thisLIR->operands[0];
99 } else {
100 nativeRegId = thisLIR->operands[0];
101 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 bool isThisLIRLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
103 LIR* checkLIR;
104 /* Use the mem mask to determine the rough memory location */
105 u8 thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700106
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 /*
108 * Currently only eliminate redundant ld/st for constant and Dalvik
109 * register accesses.
110 */
111 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700112
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700114 u8 stopUseRegMask;
115 if (cUnit->instructionSet == kX86) {
116 stopUseRegMask = (IS_BRANCH | thisLIR->useMask) & ~ENCODE_MEM;
117 } else {
118 /*
119 * Add pc to the resource mask to prevent this instruction
120 * from sinking past branch instructions. Also take out the memory
121 * region bits since stopMask is used to check data/control
122 * dependencies.
123 */
buzbeeec137432012-11-13 12:13:16 -0800124 stopUseRegMask = (getPCUseDefEncoding() | thisLIR->useMask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700125 }
buzbee67bf8852011-08-17 17:51:35 -0700126
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 for (checkLIR = NEXT_LIR(thisLIR);
128 checkLIR != tailLIR;
129 checkLIR = NEXT_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700130
Bill Buzbeea114add2012-05-03 15:00:40 -0700131 /*
132 * Skip already dead instructions (whose dataflow information is
133 * outdated and misleading).
134 */
135 if (checkLIR->flags.isNop) continue;
136
137 u8 checkMemMask = (checkLIR->useMask | checkLIR->defMask) & ENCODE_MEM;
138 u8 aliasCondition = thisMemMask & checkMemMask;
139 bool stopHere = false;
140
141 /*
142 * Potential aliases seen - check the alias relations
143 */
144 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
145 bool isCheckLIRLoad = EncodingMap[checkLIR->opcode].flags & IS_LOAD;
146 if (aliasCondition == ENCODE_LITERAL) {
147 /*
148 * Should only see literal loads in the instruction
149 * stream.
150 */
151 DCHECK(!(EncodingMap[checkLIR->opcode].flags & IS_STORE));
152 /* Same value && same register type */
153 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
154 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)) {
buzbee67bf8852011-08-17 17:51:35 -0700155 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 * Different destination register - insert
157 * a move
buzbee67bf8852011-08-17 17:51:35 -0700158 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 if (checkLIR->operands[0] != nativeRegId) {
160 convertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
161 nativeRegId);
162 }
163 checkLIR->flags.isNop = true;
164 }
165 } else if (aliasCondition == ENCODE_DALVIK_REG) {
166 /* Must alias */
167 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
168 /* Only optimize compatible registers */
169 bool regCompatible =
170 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId);
171 if ((isThisLIRLoad && isCheckLIRLoad) ||
172 (!isThisLIRLoad && isCheckLIRLoad)) {
173 /* RAR or RAW */
174 if (regCompatible) {
175 /*
176 * Different destination register -
177 * insert a move
178 */
179 if (checkLIR->operands[0] !=
180 nativeRegId) {
181 convertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
182 nativeRegId);
183 }
184 checkLIR->flags.isNop = true;
185 } else {
186 /*
187 * Destinaions are of different types -
188 * something complicated going on so
189 * stop looking now.
190 */
191 stopHere = true;
192 }
193 } else if (isThisLIRLoad && !isCheckLIRLoad) {
194 /* WAR - register value is killed */
195 stopHere = true;
196 } else if (!isThisLIRLoad && !isCheckLIRLoad) {
197 /* WAW - nuke the earlier store */
198 thisLIR->flags.isNop = true;
199 stopHere = true;
200 }
201 /* Partial overlap */
202 } else if (isDalvikRegisterClobbered(thisLIR, checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700203 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 * It is actually ok to continue if checkLIR
205 * is a read. But it is hard to make a test
206 * case for this so we just stop here to be
207 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700208 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 stopHere = true;
210 }
buzbee67bf8852011-08-17 17:51:35 -0700211 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 /* Memory content may be updated. Stop looking now. */
213 if (stopHere) {
214 break;
215 /* The checkLIR has been transformed - check the next one */
216 } else if (checkLIR->flags.isNop) {
217 continue;
218 }
219 }
220
221
222 /*
223 * this and check LIRs have no memory dependency. Now check if
224 * their register operands have any RAW, WAR, and WAW
225 * dependencies. If so, stop looking.
226 */
227 if (stopHere == false) {
228 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask, checkLIR);
229 }
230
231 if (stopHere == true) {
buzbeeb046e162012-10-30 15:48:42 -0700232 if (cUnit->instructionSet == kX86) {
233 // Prevent stores from being sunk between ops that generate ccodes and
234 // ops that use them.
buzbeeec137432012-11-13 12:13:16 -0800235 uint64_t flags = EncodingMap[checkLIR->opcode].flags;
buzbeeb046e162012-10-30 15:48:42 -0700236 if (sinkDistance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
237 checkLIR = PREV_LIR(checkLIR);
238 sinkDistance--;
239 }
jeffhao573b4292012-07-30 16:37:41 -0700240 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR, "REG CLOBBERED"));
242 /* Only sink store instructions */
243 if (sinkDistance && !isThisLIRLoad) {
244 LIR* newStoreLIR =
245 (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
246 *newStoreLIR = *thisLIR;
247 /*
248 * Stop point found - insert *before* the checkLIR
249 * since the instruction list is scanned in the
250 * top-down order.
251 */
252 oatInsertLIRBefore((LIR*) checkLIR, (LIR*) newStoreLIR);
253 thisLIR->flags.isNop = true;
254 }
255 break;
256 } else if (!checkLIR->flags.isNop) {
257 sinkDistance++;
258 }
buzbee67bf8852011-08-17 17:51:35 -0700259 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 }
buzbee67bf8852011-08-17 17:51:35 -0700261}
262
263/*
264 * Perform a pass of bottom-up walk, from the second instruction in the
265 * superblock, to try to hoist loads to earlier slots.
266 */
buzbee31a4a6f2012-02-28 15:36:15 -0800267void applyLoadHoisting(CompilationUnit* cUnit, LIR* headLIR, LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700268{
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 LIR* thisLIR, *checkLIR;
270 /*
271 * Store the list of independent instructions that can be hoisted past.
272 * Will decide the best place to insert later.
273 */
274 LIR* prevInstList[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700275
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 /* Empty block */
277 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -0700278
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 /* Start from the second instruction */
280 for (thisLIR = NEXT_LIR(headLIR);
281 thisLIR != tailLIR;
282 thisLIR = NEXT_LIR(thisLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700283
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 /* Skip non-interesting instructions */
285 if ((thisLIR->flags.isNop == true) ||
286 isPseudoOpcode(thisLIR->opcode) ||
287 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
288 continue;
289 }
buzbee67bf8852011-08-17 17:51:35 -0700290
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 u8 stopUseAllMask = thisLIR->useMask;
buzbee67bf8852011-08-17 17:51:35 -0700292
buzbeeb046e162012-10-30 15:48:42 -0700293 if (cUnit->instructionSet != kX86) {
294 /*
295 * Branches for null/range checks are marked with the true resource
296 * bits, and loads to Dalvik registers, constant pools, and non-alias
297 * locations are safe to be hoisted. So only mark the heap references
298 * conservatively here.
299 */
300 if (stopUseAllMask & ENCODE_HEAP_REF) {
buzbeeec137432012-11-13 12:13:16 -0800301 stopUseAllMask |= getPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700302 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 }
buzbee67bf8852011-08-17 17:51:35 -0700304
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 /* Similar as above, but just check for pure register dependency */
306 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
307 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700308
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 int nextSlot = 0;
310 bool stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700311
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 /* Try to hoist the load to a good spot */
313 for (checkLIR = PREV_LIR(thisLIR);
314 checkLIR != headLIR;
315 checkLIR = PREV_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700316
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 /*
318 * Skip already dead instructions (whose dataflow information is
319 * outdated and misleading).
320 */
321 if (checkLIR->flags.isNop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700322
Bill Buzbeea114add2012-05-03 15:00:40 -0700323 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
324 u8 aliasCondition = stopUseAllMask & checkMemMask;
325 stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700326
Bill Buzbeea114add2012-05-03 15:00:40 -0700327 /* Potential WAR alias seen - check the exact relation */
328 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
329 /* We can fully disambiguate Dalvik references */
330 if (aliasCondition == ENCODE_DALVIK_REG) {
331 /* Must alias or partually overlap */
332 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
333 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
334 stopHere = true;
335 }
336 /* Conservatively treat all heap refs as may-alias */
337 } else {
338 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
339 stopHere = true;
buzbee67bf8852011-08-17 17:51:35 -0700340 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 /* Memory content may be updated. Stop looking now. */
342 if (stopHere) {
343 prevInstList[nextSlot++] = checkLIR;
344 break;
buzbee67bf8852011-08-17 17:51:35 -0700345 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 }
buzbee67bf8852011-08-17 17:51:35 -0700347
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 if (stopHere == false) {
349 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
350 checkLIR);
351 }
buzbee67bf8852011-08-17 17:51:35 -0700352
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 /*
354 * Store the dependent or non-pseudo/indepedent instruction to the
355 * list.
356 */
357 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
358 prevInstList[nextSlot++] = checkLIR;
359 if (nextSlot == MAX_HOIST_DISTANCE) break;
360 }
buzbee67bf8852011-08-17 17:51:35 -0700361
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 /* Found a new place to put the load - move it here */
363 if (stopHere == true) {
364 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR "HOIST STOP"));
365 break;
366 }
buzbee67bf8852011-08-17 17:51:35 -0700367 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700368
369 /*
370 * Reached the top - use headLIR as the dependent marker as all labels
371 * are barriers.
372 */
373 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
374 prevInstList[nextSlot++] = headLIR;
375 }
376
377 /*
378 * At least one independent instruction is found. Scan in the reversed
379 * direction to find a beneficial slot.
380 */
381 if (nextSlot >= 2) {
382 int firstSlot = nextSlot - 2;
383 int slot;
384 LIR* depLIR = prevInstList[nextSlot-1];
385 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
386 if (!isPseudoOpcode(depLIR->opcode) &&
387 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
388 firstSlot -= LDLD_DISTANCE;
389 }
390 /*
391 * Make sure we check slot >= 0 since firstSlot may be negative
392 * when the loop is first entered.
393 */
394 for (slot = firstSlot; slot >= 0; slot--) {
395 LIR* curLIR = prevInstList[slot];
396 LIR* prevLIR = prevInstList[slot+1];
397
398 /* Check the highest instruction */
399 if (prevLIR->defMask == ENCODE_ALL) {
400 /*
401 * If the first instruction is a load, don't hoist anything
402 * above it since it is unlikely to be beneficial.
403 */
404 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
405 /*
406 * If the remaining number of slots is less than LD_LATENCY,
407 * insert the hoisted load here.
408 */
409 if (slot < LD_LATENCY) break;
410 }
411
buzbee8320f382012-09-11 16:29:42 -0700412 // Don't look across a barrier label
413 if ((prevLIR->opcode == kPseudoTargetLabel) ||
414 (prevLIR->opcode == kPseudoSafepointPC) ||
415 (prevLIR->opcode == kPseudoBarrier)) {
416 break;
417 }
418
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 * Try to find two instructions with load/use dependency until
421 * the remaining instructions are less than LD_LATENCY.
422 */
buzbee8320f382012-09-11 16:29:42 -0700423 bool prevIsLoad = isPseudoOpcode(prevLIR->opcode) ? false :
424 (EncodingMap[prevLIR->opcode].flags & IS_LOAD);
425 if (((curLIR->useMask & prevLIR->defMask) && prevIsLoad) || (slot < LD_LATENCY)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 break;
427 }
428 }
429
430 /* Found a slot to hoist to */
431 if (slot >= 0) {
432 LIR* curLIR = prevInstList[slot];
433 LIR* newLoadLIR = (LIR* ) oatNew(cUnit, sizeof(LIR),
434 true, kAllocLIR);
435 *newLoadLIR = *thisLIR;
436 /*
437 * Insertion is guaranteed to succeed since checkLIR
438 * is never the first LIR on the list
439 */
440 oatInsertLIRBefore((LIR*) curLIR, (LIR*) newLoadLIR);
441 thisLIR->flags.isNop = true;
442 }
443 }
444 }
buzbee67bf8852011-08-17 17:51:35 -0700445}
446
447void oatApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700449{
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
451 applyLoadStoreElimination(cUnit, (LIR* ) headLIR,
452 (LIR* ) tailLIR);
453 }
454 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
455 applyLoadHoisting(cUnit, (LIR* ) headLIR, (LIR* ) tailLIR);
456 }
buzbee67bf8852011-08-17 17:51:35 -0700457}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800458
459} // namespace art