blob: 2fc7ae0ccd9ee288a8219c14b91a37f66d045402 [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
jeffhaoe2962482012-06-28 11:29:57 -070094#if defined(TARGET_X86)
95 int nativeRegId = (EncodingMap[thisLIR->opcode].flags & IS_STORE) ? thisLIR->operands[2] : thisLIR->operands[0];
96#else
Bill Buzbeea114add2012-05-03 15:00:40 -070097 int nativeRegId = thisLIR->operands[0];
jeffhaoe2962482012-06-28 11:29:57 -070098#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070099 bool isThisLIRLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
100 LIR* checkLIR;
101 /* Use the mem mask to determine the rough memory location */
102 u8 thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700103
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 /*
105 * Currently only eliminate redundant ld/st for constant and Dalvik
106 * register accesses.
107 */
108 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700109
buzbeea7678db2012-03-05 15:35:46 -0800110#if defined(TARGET_X86)
jeffhaoe2962482012-06-28 11:29:57 -0700111 u8 stopUseRegMask = (IS_BRANCH | thisLIR->useMask) & ~ENCODE_MEM;
buzbeea7678db2012-03-05 15:35:46 -0800112#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 /*
114 * Add r15 (pc) to the resource mask to prevent this instruction
115 * from sinking past branch instructions. Also take out the memory
116 * region bits since stopMask is used to check data/control
117 * dependencies.
118 */
119 u8 stopUseRegMask = (ENCODE_REG_PC | thisLIR->useMask) &
120 ~ENCODE_MEM;
buzbeea7678db2012-03-05 15:35:46 -0800121#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700122 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700123
Bill Buzbeea114add2012-05-03 15:00:40 -0700124 for (checkLIR = NEXT_LIR(thisLIR);
125 checkLIR != tailLIR;
126 checkLIR = NEXT_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700127
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 /*
129 * Skip already dead instructions (whose dataflow information is
130 * outdated and misleading).
131 */
132 if (checkLIR->flags.isNop) continue;
133
134 u8 checkMemMask = (checkLIR->useMask | checkLIR->defMask) & ENCODE_MEM;
135 u8 aliasCondition = thisMemMask & checkMemMask;
136 bool stopHere = false;
137
138 /*
139 * Potential aliases seen - check the alias relations
140 */
141 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
142 bool isCheckLIRLoad = EncodingMap[checkLIR->opcode].flags & IS_LOAD;
143 if (aliasCondition == ENCODE_LITERAL) {
144 /*
145 * Should only see literal loads in the instruction
146 * stream.
147 */
148 DCHECK(!(EncodingMap[checkLIR->opcode].flags & IS_STORE));
149 /* Same value && same register type */
150 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
151 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)) {
buzbee67bf8852011-08-17 17:51:35 -0700152 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 * Different destination register - insert
154 * a move
buzbee67bf8852011-08-17 17:51:35 -0700155 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 if (checkLIR->operands[0] != nativeRegId) {
157 convertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
158 nativeRegId);
159 }
160 checkLIR->flags.isNop = true;
161 }
162 } else if (aliasCondition == ENCODE_DALVIK_REG) {
163 /* Must alias */
164 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
165 /* Only optimize compatible registers */
166 bool regCompatible =
167 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId);
168 if ((isThisLIRLoad && isCheckLIRLoad) ||
169 (!isThisLIRLoad && isCheckLIRLoad)) {
170 /* RAR or RAW */
171 if (regCompatible) {
172 /*
173 * Different destination register -
174 * insert a move
175 */
176 if (checkLIR->operands[0] !=
177 nativeRegId) {
178 convertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
179 nativeRegId);
180 }
181 checkLIR->flags.isNop = true;
182 } else {
183 /*
184 * Destinaions are of different types -
185 * something complicated going on so
186 * stop looking now.
187 */
188 stopHere = true;
189 }
190 } else if (isThisLIRLoad && !isCheckLIRLoad) {
191 /* WAR - register value is killed */
192 stopHere = true;
193 } else if (!isThisLIRLoad && !isCheckLIRLoad) {
194 /* WAW - nuke the earlier store */
195 thisLIR->flags.isNop = true;
196 stopHere = true;
197 }
198 /* Partial overlap */
199 } else if (isDalvikRegisterClobbered(thisLIR, checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700200 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 * It is actually ok to continue if checkLIR
202 * is a read. But it is hard to make a test
203 * case for this so we just stop here to be
204 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700205 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 stopHere = true;
207 }
buzbee67bf8852011-08-17 17:51:35 -0700208 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 /* Memory content may be updated. Stop looking now. */
210 if (stopHere) {
211 break;
212 /* The checkLIR has been transformed - check the next one */
213 } else if (checkLIR->flags.isNop) {
214 continue;
215 }
216 }
217
218
219 /*
220 * this and check LIRs have no memory dependency. Now check if
221 * their register operands have any RAW, WAR, and WAW
222 * dependencies. If so, stop looking.
223 */
224 if (stopHere == false) {
225 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask, checkLIR);
226 }
227
228 if (stopHere == true) {
jeffhao573b4292012-07-30 16:37:41 -0700229#if defined(TARGET_X86)
230 // Prevent stores from being sunk between ops that generate ccodes and
231 // ops that use them.
232 int flags = EncodingMap[checkLIR->opcode].flags;
233 if (sinkDistance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
234 checkLIR = PREV_LIR(checkLIR);
235 sinkDistance--;
236 }
237#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700238 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR, "REG CLOBBERED"));
239 /* Only sink store instructions */
240 if (sinkDistance && !isThisLIRLoad) {
241 LIR* newStoreLIR =
242 (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
243 *newStoreLIR = *thisLIR;
244 /*
245 * Stop point found - insert *before* the checkLIR
246 * since the instruction list is scanned in the
247 * top-down order.
248 */
249 oatInsertLIRBefore((LIR*) checkLIR, (LIR*) newStoreLIR);
250 thisLIR->flags.isNop = true;
251 }
252 break;
253 } else if (!checkLIR->flags.isNop) {
254 sinkDistance++;
255 }
buzbee67bf8852011-08-17 17:51:35 -0700256 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 }
buzbee67bf8852011-08-17 17:51:35 -0700258}
259
260/*
261 * Perform a pass of bottom-up walk, from the second instruction in the
262 * superblock, to try to hoist loads to earlier slots.
263 */
buzbee31a4a6f2012-02-28 15:36:15 -0800264void applyLoadHoisting(CompilationUnit* cUnit, LIR* headLIR, LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700265{
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 LIR* thisLIR, *checkLIR;
267 /*
268 * Store the list of independent instructions that can be hoisted past.
269 * Will decide the best place to insert later.
270 */
271 LIR* prevInstList[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700272
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 /* Empty block */
274 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -0700275
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 /* Start from the second instruction */
277 for (thisLIR = NEXT_LIR(headLIR);
278 thisLIR != tailLIR;
279 thisLIR = NEXT_LIR(thisLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700280
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 /* Skip non-interesting instructions */
282 if ((thisLIR->flags.isNop == true) ||
283 isPseudoOpcode(thisLIR->opcode) ||
284 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
285 continue;
286 }
buzbee67bf8852011-08-17 17:51:35 -0700287
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 u8 stopUseAllMask = thisLIR->useMask;
buzbee67bf8852011-08-17 17:51:35 -0700289
buzbeea7678db2012-03-05 15:35:46 -0800290#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 /*
292 * Branches for null/range checks are marked with the true resource
293 * bits, and loads to Dalvik registers, constant pools, and non-alias
294 * locations are safe to be hoisted. So only mark the heap references
295 * conservatively here.
296 */
297 if (stopUseAllMask & ENCODE_HEAP_REF) {
298 stopUseAllMask |= ENCODE_REG_PC;
299 }
buzbeea7678db2012-03-05 15:35:46 -0800300#endif
buzbee67bf8852011-08-17 17:51:35 -0700301
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 /* Similar as above, but just check for pure register dependency */
303 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
304 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700305
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 int nextSlot = 0;
307 bool stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700308
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 /* Try to hoist the load to a good spot */
310 for (checkLIR = PREV_LIR(thisLIR);
311 checkLIR != headLIR;
312 checkLIR = PREV_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700313
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 /*
315 * Skip already dead instructions (whose dataflow information is
316 * outdated and misleading).
317 */
318 if (checkLIR->flags.isNop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700319
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
321 u8 aliasCondition = stopUseAllMask & checkMemMask;
322 stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700323
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 /* Potential WAR alias seen - check the exact relation */
325 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
326 /* We can fully disambiguate Dalvik references */
327 if (aliasCondition == ENCODE_DALVIK_REG) {
328 /* Must alias or partually overlap */
329 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
330 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
331 stopHere = true;
332 }
333 /* Conservatively treat all heap refs as may-alias */
334 } else {
335 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
336 stopHere = true;
buzbee67bf8852011-08-17 17:51:35 -0700337 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 /* Memory content may be updated. Stop looking now. */
339 if (stopHere) {
340 prevInstList[nextSlot++] = checkLIR;
341 break;
buzbee67bf8852011-08-17 17:51:35 -0700342 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 }
buzbee67bf8852011-08-17 17:51:35 -0700344
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 if (stopHere == false) {
346 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
347 checkLIR);
348 }
buzbee67bf8852011-08-17 17:51:35 -0700349
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 /*
351 * Store the dependent or non-pseudo/indepedent instruction to the
352 * list.
353 */
354 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
355 prevInstList[nextSlot++] = checkLIR;
356 if (nextSlot == MAX_HOIST_DISTANCE) break;
357 }
buzbee67bf8852011-08-17 17:51:35 -0700358
Bill Buzbeea114add2012-05-03 15:00:40 -0700359 /* Found a new place to put the load - move it here */
360 if (stopHere == true) {
361 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR "HOIST STOP"));
362 break;
363 }
buzbee67bf8852011-08-17 17:51:35 -0700364 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700365
366 /*
367 * Reached the top - use headLIR as the dependent marker as all labels
368 * are barriers.
369 */
370 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
371 prevInstList[nextSlot++] = headLIR;
372 }
373
374 /*
375 * At least one independent instruction is found. Scan in the reversed
376 * direction to find a beneficial slot.
377 */
378 if (nextSlot >= 2) {
379 int firstSlot = nextSlot - 2;
380 int slot;
381 LIR* depLIR = prevInstList[nextSlot-1];
382 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
383 if (!isPseudoOpcode(depLIR->opcode) &&
384 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
385 firstSlot -= LDLD_DISTANCE;
386 }
387 /*
388 * Make sure we check slot >= 0 since firstSlot may be negative
389 * when the loop is first entered.
390 */
391 for (slot = firstSlot; slot >= 0; slot--) {
392 LIR* curLIR = prevInstList[slot];
393 LIR* prevLIR = prevInstList[slot+1];
394
395 /* Check the highest instruction */
396 if (prevLIR->defMask == ENCODE_ALL) {
397 /*
398 * If the first instruction is a load, don't hoist anything
399 * above it since it is unlikely to be beneficial.
400 */
401 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
402 /*
403 * If the remaining number of slots is less than LD_LATENCY,
404 * insert the hoisted load here.
405 */
406 if (slot < LD_LATENCY) break;
407 }
408
409 /*
410 * NOTE: now prevLIR is guaranteed to be a non-pseudo
411 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
412 * safe).
413 *
414 * Try to find two instructions with load/use dependency until
415 * the remaining instructions are less than LD_LATENCY.
416 */
417 if (((curLIR->useMask & prevLIR->defMask) &&
418 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
419 (slot < LD_LATENCY)) {
420 break;
421 }
422 }
423
424 /* Found a slot to hoist to */
425 if (slot >= 0) {
426 LIR* curLIR = prevInstList[slot];
427 LIR* newLoadLIR = (LIR* ) oatNew(cUnit, sizeof(LIR),
428 true, kAllocLIR);
429 *newLoadLIR = *thisLIR;
430 /*
431 * Insertion is guaranteed to succeed since checkLIR
432 * is never the first LIR on the list
433 */
434 oatInsertLIRBefore((LIR*) curLIR, (LIR*) newLoadLIR);
435 thisLIR->flags.isNop = true;
436 }
437 }
438 }
buzbee67bf8852011-08-17 17:51:35 -0700439}
440
441void oatApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
Bill Buzbeea114add2012-05-03 15:00:40 -0700442 LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700443{
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
445 applyLoadStoreElimination(cUnit, (LIR* ) headLIR,
446 (LIR* ) tailLIR);
447 }
448 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
449 applyLoadHoisting(cUnit, (LIR* ) headLIR, (LIR* ) tailLIR);
450 }
buzbee67bf8852011-08-17 17:51:35 -0700451}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800452
453} // namespace art