blob: 55ba03aea8687dff1adae787a93eb37dbfc01ad5 [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) ||
89 !(EncodingMap[thisLIR->opcode].flags & (IS_LOAD | IS_STORE))) {
90 continue;
91 }
buzbee67bf8852011-08-17 17:51:35 -070092
Bill Buzbeea114add2012-05-03 15:00:40 -070093 int nativeRegId = thisLIR->operands[0];
94 bool isThisLIRLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
95 LIR* checkLIR;
96 /* Use the mem mask to determine the rough memory location */
97 u8 thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -070098
Bill Buzbeea114add2012-05-03 15:00:40 -070099 /*
100 * Currently only eliminate redundant ld/st for constant and Dalvik
101 * register accesses.
102 */
103 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700104
buzbeea7678db2012-03-05 15:35:46 -0800105// FIXME: make sure we have a branch barrier for x86
106#if defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 u8 stopUseRegMask = (thisLIR->useMask) & ~ENCODE_MEM;
buzbeea7678db2012-03-05 15:35:46 -0800108#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 /*
110 * Add r15 (pc) to the resource mask to prevent this instruction
111 * from sinking past branch instructions. Also take out the memory
112 * region bits since stopMask is used to check data/control
113 * dependencies.
114 */
115 u8 stopUseRegMask = (ENCODE_REG_PC | thisLIR->useMask) &
116 ~ENCODE_MEM;
buzbeea7678db2012-03-05 15:35:46 -0800117#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700119
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 for (checkLIR = NEXT_LIR(thisLIR);
121 checkLIR != tailLIR;
122 checkLIR = NEXT_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700123
Bill Buzbeea114add2012-05-03 15:00:40 -0700124 /*
125 * Skip already dead instructions (whose dataflow information is
126 * outdated and misleading).
127 */
128 if (checkLIR->flags.isNop) continue;
129
130 u8 checkMemMask = (checkLIR->useMask | checkLIR->defMask) & ENCODE_MEM;
131 u8 aliasCondition = thisMemMask & checkMemMask;
132 bool stopHere = false;
133
134 /*
135 * Potential aliases seen - check the alias relations
136 */
137 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
138 bool isCheckLIRLoad = EncodingMap[checkLIR->opcode].flags & IS_LOAD;
139 if (aliasCondition == ENCODE_LITERAL) {
140 /*
141 * Should only see literal loads in the instruction
142 * stream.
143 */
144 DCHECK(!(EncodingMap[checkLIR->opcode].flags & IS_STORE));
145 /* Same value && same register type */
146 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
147 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)) {
buzbee67bf8852011-08-17 17:51:35 -0700148 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700149 * Different destination register - insert
150 * a move
buzbee67bf8852011-08-17 17:51:35 -0700151 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 if (checkLIR->operands[0] != nativeRegId) {
153 convertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
154 nativeRegId);
155 }
156 checkLIR->flags.isNop = true;
157 }
158 } else if (aliasCondition == ENCODE_DALVIK_REG) {
159 /* Must alias */
160 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
161 /* Only optimize compatible registers */
162 bool regCompatible =
163 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId);
164 if ((isThisLIRLoad && isCheckLIRLoad) ||
165 (!isThisLIRLoad && isCheckLIRLoad)) {
166 /* RAR or RAW */
167 if (regCompatible) {
168 /*
169 * Different destination register -
170 * insert a move
171 */
172 if (checkLIR->operands[0] !=
173 nativeRegId) {
174 convertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
175 nativeRegId);
176 }
177 checkLIR->flags.isNop = true;
178 } else {
179 /*
180 * Destinaions are of different types -
181 * something complicated going on so
182 * stop looking now.
183 */
184 stopHere = true;
185 }
186 } else if (isThisLIRLoad && !isCheckLIRLoad) {
187 /* WAR - register value is killed */
188 stopHere = true;
189 } else if (!isThisLIRLoad && !isCheckLIRLoad) {
190 /* WAW - nuke the earlier store */
191 thisLIR->flags.isNop = true;
192 stopHere = true;
193 }
194 /* Partial overlap */
195 } else if (isDalvikRegisterClobbered(thisLIR, checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700196 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 * It is actually ok to continue if checkLIR
198 * is a read. But it is hard to make a test
199 * case for this so we just stop here to be
200 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700201 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 stopHere = true;
203 }
buzbee67bf8852011-08-17 17:51:35 -0700204 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 /* Memory content may be updated. Stop looking now. */
206 if (stopHere) {
207 break;
208 /* The checkLIR has been transformed - check the next one */
209 } else if (checkLIR->flags.isNop) {
210 continue;
211 }
212 }
213
214
215 /*
216 * this and check LIRs have no memory dependency. Now check if
217 * their register operands have any RAW, WAR, and WAW
218 * dependencies. If so, stop looking.
219 */
220 if (stopHere == false) {
221 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask, checkLIR);
222 }
223
224 if (stopHere == true) {
225 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR, "REG CLOBBERED"));
226 /* Only sink store instructions */
227 if (sinkDistance && !isThisLIRLoad) {
228 LIR* newStoreLIR =
229 (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
230 *newStoreLIR = *thisLIR;
231 /*
232 * Stop point found - insert *before* the checkLIR
233 * since the instruction list is scanned in the
234 * top-down order.
235 */
236 oatInsertLIRBefore((LIR*) checkLIR, (LIR*) newStoreLIR);
237 thisLIR->flags.isNop = true;
238 }
239 break;
240 } else if (!checkLIR->flags.isNop) {
241 sinkDistance++;
242 }
buzbee67bf8852011-08-17 17:51:35 -0700243 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 }
buzbee67bf8852011-08-17 17:51:35 -0700245}
246
247/*
248 * Perform a pass of bottom-up walk, from the second instruction in the
249 * superblock, to try to hoist loads to earlier slots.
250 */
buzbee31a4a6f2012-02-28 15:36:15 -0800251void applyLoadHoisting(CompilationUnit* cUnit, LIR* headLIR, LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700252{
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 LIR* thisLIR, *checkLIR;
254 /*
255 * Store the list of independent instructions that can be hoisted past.
256 * Will decide the best place to insert later.
257 */
258 LIR* prevInstList[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700259
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 /* Empty block */
261 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -0700262
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 /* Start from the second instruction */
264 for (thisLIR = NEXT_LIR(headLIR);
265 thisLIR != tailLIR;
266 thisLIR = NEXT_LIR(thisLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700267
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 /* Skip non-interesting instructions */
269 if ((thisLIR->flags.isNop == true) ||
270 isPseudoOpcode(thisLIR->opcode) ||
271 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
272 continue;
273 }
buzbee67bf8852011-08-17 17:51:35 -0700274
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 u8 stopUseAllMask = thisLIR->useMask;
buzbee67bf8852011-08-17 17:51:35 -0700276
buzbeea7678db2012-03-05 15:35:46 -0800277#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 /*
279 * Branches for null/range checks are marked with the true resource
280 * bits, and loads to Dalvik registers, constant pools, and non-alias
281 * locations are safe to be hoisted. So only mark the heap references
282 * conservatively here.
283 */
284 if (stopUseAllMask & ENCODE_HEAP_REF) {
285 stopUseAllMask |= ENCODE_REG_PC;
286 }
buzbeea7678db2012-03-05 15:35:46 -0800287#endif
buzbee67bf8852011-08-17 17:51:35 -0700288
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 /* Similar as above, but just check for pure register dependency */
290 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
291 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700292
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 int nextSlot = 0;
294 bool stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700295
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 /* Try to hoist the load to a good spot */
297 for (checkLIR = PREV_LIR(thisLIR);
298 checkLIR != headLIR;
299 checkLIR = PREV_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700300
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 /*
302 * Skip already dead instructions (whose dataflow information is
303 * outdated and misleading).
304 */
305 if (checkLIR->flags.isNop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700306
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
308 u8 aliasCondition = stopUseAllMask & checkMemMask;
309 stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700310
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 /* Potential WAR alias seen - check the exact relation */
312 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
313 /* We can fully disambiguate Dalvik references */
314 if (aliasCondition == ENCODE_DALVIK_REG) {
315 /* Must alias or partually overlap */
316 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
317 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
318 stopHere = true;
319 }
320 /* Conservatively treat all heap refs as may-alias */
321 } else {
322 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
323 stopHere = true;
buzbee67bf8852011-08-17 17:51:35 -0700324 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 /* Memory content may be updated. Stop looking now. */
326 if (stopHere) {
327 prevInstList[nextSlot++] = checkLIR;
328 break;
buzbee67bf8852011-08-17 17:51:35 -0700329 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 }
buzbee67bf8852011-08-17 17:51:35 -0700331
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 if (stopHere == false) {
333 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
334 checkLIR);
335 }
buzbee67bf8852011-08-17 17:51:35 -0700336
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 /*
338 * Store the dependent or non-pseudo/indepedent instruction to the
339 * list.
340 */
341 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
342 prevInstList[nextSlot++] = checkLIR;
343 if (nextSlot == MAX_HOIST_DISTANCE) break;
344 }
buzbee67bf8852011-08-17 17:51:35 -0700345
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 /* Found a new place to put the load - move it here */
347 if (stopHere == true) {
348 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR "HOIST STOP"));
349 break;
350 }
buzbee67bf8852011-08-17 17:51:35 -0700351 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700352
353 /*
354 * Reached the top - use headLIR as the dependent marker as all labels
355 * are barriers.
356 */
357 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
358 prevInstList[nextSlot++] = headLIR;
359 }
360
361 /*
362 * At least one independent instruction is found. Scan in the reversed
363 * direction to find a beneficial slot.
364 */
365 if (nextSlot >= 2) {
366 int firstSlot = nextSlot - 2;
367 int slot;
368 LIR* depLIR = prevInstList[nextSlot-1];
369 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
370 if (!isPseudoOpcode(depLIR->opcode) &&
371 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
372 firstSlot -= LDLD_DISTANCE;
373 }
374 /*
375 * Make sure we check slot >= 0 since firstSlot may be negative
376 * when the loop is first entered.
377 */
378 for (slot = firstSlot; slot >= 0; slot--) {
379 LIR* curLIR = prevInstList[slot];
380 LIR* prevLIR = prevInstList[slot+1];
381
382 /* Check the highest instruction */
383 if (prevLIR->defMask == ENCODE_ALL) {
384 /*
385 * If the first instruction is a load, don't hoist anything
386 * above it since it is unlikely to be beneficial.
387 */
388 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
389 /*
390 * If the remaining number of slots is less than LD_LATENCY,
391 * insert the hoisted load here.
392 */
393 if (slot < LD_LATENCY) break;
394 }
395
396 /*
397 * NOTE: now prevLIR is guaranteed to be a non-pseudo
398 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
399 * safe).
400 *
401 * Try to find two instructions with load/use dependency until
402 * the remaining instructions are less than LD_LATENCY.
403 */
404 if (((curLIR->useMask & prevLIR->defMask) &&
405 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
406 (slot < LD_LATENCY)) {
407 break;
408 }
409 }
410
411 /* Found a slot to hoist to */
412 if (slot >= 0) {
413 LIR* curLIR = prevInstList[slot];
414 LIR* newLoadLIR = (LIR* ) oatNew(cUnit, sizeof(LIR),
415 true, kAllocLIR);
416 *newLoadLIR = *thisLIR;
417 /*
418 * Insertion is guaranteed to succeed since checkLIR
419 * is never the first LIR on the list
420 */
421 oatInsertLIRBefore((LIR*) curLIR, (LIR*) newLoadLIR);
422 thisLIR->flags.isNop = true;
423 }
424 }
425 }
buzbee67bf8852011-08-17 17:51:35 -0700426}
427
428void oatApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700430{
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
432 applyLoadStoreElimination(cUnit, (LIR* ) headLIR,
433 (LIR* ) tailLIR);
434 }
435 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
436 applyLoadHoisting(cUnit, (LIR* ) headLIR, (LIR* ) tailLIR);
437 }
buzbee67bf8852011-08-17 17:51:35 -0700438}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800439
440} // namespace art