blob: 3bed72f6a53ad3bb4cd458ad2f8c242a6621ba9e [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{
32 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);
36
37 return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo);
38}
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{
44 /* Insert a move to replace the load */
buzbee31a4a6f2012-02-28 15:36:15 -080045 LIR* moveLIR;
buzbee67bf8852011-08-17 17:51:35 -070046 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);
54}
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{
buzbee31a4a6f2012-02-28 15:36:15 -080077 LIR* thisLIR;
buzbee67bf8852011-08-17 17:51:35 -070078
79 if (headLIR == tailLIR) return;
80
81 for (thisLIR = PREV_LIR(tailLIR);
82 thisLIR != headLIR;
83 thisLIR = PREV_LIR(thisLIR)) {
84 int sinkDistance = 0;
85
86 /* 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 }
92
93 int nativeRegId = thisLIR->operands[0];
94 bool isThisLIRLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
buzbee31a4a6f2012-02-28 15:36:15 -080095 LIR* checkLIR;
buzbee67bf8852011-08-17 17:51:35 -070096 /* Use the mem mask to determine the rough memory location */
97 u8 thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
98
99 /*
100 * Currently only eliminate redundant ld/st for constant and Dalvik
101 * register accesses.
102 */
103 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
104
buzbeea7678db2012-03-05 15:35:46 -0800105// FIXME: make sure we have a branch barrier for x86
106#if defined(TARGET_X86)
107 u8 stopUseRegMask = (thisLIR->useMask) & ~ENCODE_MEM;
108#else
buzbee67bf8852011-08-17 17:51:35 -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
buzbee67bf8852011-08-17 17:51:35 -0700118 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
119
120 for (checkLIR = NEXT_LIR(thisLIR);
121 checkLIR != tailLIR;
122 checkLIR = NEXT_LIR(checkLIR)) {
123
124 /*
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) &
131 ENCODE_MEM;
132 u8 aliasCondition = thisMemMask & checkMemMask;
133 bool stopHere = false;
134
135 /*
136 * Potential aliases seen - check the alias relations
137 */
138 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
139 bool isCheckLIRLoad = EncodingMap[checkLIR->opcode].flags &
140 IS_LOAD;
141 if (aliasCondition == ENCODE_LITERAL) {
142 /*
143 * Should only see literal loads in the instruction
144 * stream.
145 */
buzbeeed3e9302011-09-23 17:34:19 -0700146 DCHECK(!(EncodingMap[checkLIR->opcode].flags &
buzbee67bf8852011-08-17 17:51:35 -0700147 IS_STORE));
148 /* Same value && same register type */
149 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
150 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)){
151 /*
152 * Different destination register - insert
153 * a move
154 */
155 if (checkLIR->operands[0] != nativeRegId) {
156 convertMemOpIntoMove(cUnit, checkLIR,
157 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]) ==
168 REGTYPE(nativeRegId);
169 if ((isThisLIRLoad && isCheckLIRLoad) ||
170 (!isThisLIRLoad && isCheckLIRLoad)) {
171 /* RAR or RAW */
172 if (regCompatible) {
173 /*
174 * Different destination register -
175 * insert a move
176 */
177 if (checkLIR->operands[0] !=
178 nativeRegId) {
179 convertMemOpIntoMove(cUnit,
180 checkLIR,
181 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)) {
203 /*
204 * 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.
208 */
209 stopHere = true;
210 }
211 }
212 /* 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,
229 checkLIR);
230 }
231
232 if (stopHere == true) {
233 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR,
234 "REG CLOBBERED"));
235 /* Only sink store instructions */
236 if (sinkDistance && !isThisLIRLoad) {
buzbee31a4a6f2012-02-28 15:36:15 -0800237 LIR* newStoreLIR =
238 (LIR* ) oatNew(cUnit, sizeof(LIR), true,
239 kAllocLIR);
buzbee67bf8852011-08-17 17:51:35 -0700240 *newStoreLIR = *thisLIR;
241 /*
242 * Stop point found - insert *before* the checkLIR
243 * since the instruction list is scanned in the
244 * top-down order.
245 */
246 oatInsertLIRBefore((LIR*) checkLIR,
247 (LIR*) newStoreLIR);
248 thisLIR->flags.isNop = true;
249 }
250 break;
251 } else if (!checkLIR->flags.isNop) {
252 sinkDistance++;
253 }
254 }
255 }
256}
257
258/*
259 * Perform a pass of bottom-up walk, from the second instruction in the
260 * superblock, to try to hoist loads to earlier slots.
261 */
buzbee31a4a6f2012-02-28 15:36:15 -0800262void applyLoadHoisting(CompilationUnit* cUnit, LIR* headLIR, LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700263{
buzbee31a4a6f2012-02-28 15:36:15 -0800264 LIR* thisLIR, *checkLIR;
buzbee67bf8852011-08-17 17:51:35 -0700265 /*
266 * Store the list of independent instructions that can be hoisted past.
267 * Will decide the best place to insert later.
268 */
buzbee31a4a6f2012-02-28 15:36:15 -0800269 LIR* prevInstList[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700270
271 /* Empty block */
272 if (headLIR == tailLIR) return;
273
274 /* Start from the second instruction */
275 for (thisLIR = NEXT_LIR(headLIR);
276 thisLIR != tailLIR;
277 thisLIR = NEXT_LIR(thisLIR)) {
278
279 /* Skip non-interesting instructions */
280 if ((thisLIR->flags.isNop == true) ||
281 isPseudoOpcode(thisLIR->opcode) ||
282 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
283 continue;
284 }
285
286 u8 stopUseAllMask = thisLIR->useMask;
287
buzbeea7678db2012-03-05 15:35:46 -0800288#if !defined(TARGET_X86)
buzbee67bf8852011-08-17 17:51:35 -0700289 /*
290 * Branches for null/range checks are marked with the true resource
291 * bits, and loads to Dalvik registers, constant pools, and non-alias
292 * locations are safe to be hoisted. So only mark the heap references
293 * conservatively here.
294 */
295 if (stopUseAllMask & ENCODE_HEAP_REF) {
296 stopUseAllMask |= ENCODE_REG_PC;
297 }
buzbeea7678db2012-03-05 15:35:46 -0800298#endif
buzbee67bf8852011-08-17 17:51:35 -0700299
300 /* Similar as above, but just check for pure register dependency */
301 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
302 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
303
304 int nextSlot = 0;
305 bool stopHere = false;
306
307 /* Try to hoist the load to a good spot */
308 for (checkLIR = PREV_LIR(thisLIR);
309 checkLIR != headLIR;
310 checkLIR = PREV_LIR(checkLIR)) {
311
312 /*
313 * Skip already dead instructions (whose dataflow information is
314 * outdated and misleading).
315 */
316 if (checkLIR->flags.isNop) continue;
317
318 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
319 u8 aliasCondition = stopUseAllMask & checkMemMask;
320 stopHere = false;
321
322 /* Potential WAR alias seen - check the exact relation */
323 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
324 /* We can fully disambiguate Dalvik references */
325 if (aliasCondition == ENCODE_DALVIK_REG) {
326 /* Must alias or partually overlap */
327 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
328 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
329 stopHere = true;
330 }
331 /* Conservatively treat all heap refs as may-alias */
332 } else {
buzbeeed3e9302011-09-23 17:34:19 -0700333 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
buzbee67bf8852011-08-17 17:51:35 -0700334 stopHere = true;
335 }
336 /* Memory content may be updated. Stop looking now. */
337 if (stopHere) {
338 prevInstList[nextSlot++] = checkLIR;
339 break;
340 }
341 }
342
343 if (stopHere == false) {
344 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
345 checkLIR);
346 }
347
348 /*
349 * Store the dependent or non-pseudo/indepedent instruction to the
350 * list.
351 */
352 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
353 prevInstList[nextSlot++] = checkLIR;
354 if (nextSlot == MAX_HOIST_DISTANCE) break;
355 }
356
357 /* Found a new place to put the load - move it here */
358 if (stopHere == true) {
359 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR
360 "HOIST STOP"));
361 break;
362 }
363 }
364
365 /*
366 * Reached the top - use headLIR as the dependent marker as all labels
367 * are barriers.
368 */
369 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
370 prevInstList[nextSlot++] = headLIR;
371 }
372
373 /*
374 * At least one independent instruction is found. Scan in the reversed
375 * direction to find a beneficial slot.
376 */
377 if (nextSlot >= 2) {
378 int firstSlot = nextSlot - 2;
379 int slot;
buzbee31a4a6f2012-02-28 15:36:15 -0800380 LIR* depLIR = prevInstList[nextSlot-1];
buzbee67bf8852011-08-17 17:51:35 -0700381 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
382 if (!isPseudoOpcode(depLIR->opcode) &&
383 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
384 firstSlot -= LDLD_DISTANCE;
385 }
386 /*
387 * Make sure we check slot >= 0 since firstSlot may be negative
388 * when the loop is first entered.
389 */
390 for (slot = firstSlot; slot >= 0; slot--) {
buzbee31a4a6f2012-02-28 15:36:15 -0800391 LIR* curLIR = prevInstList[slot];
392 LIR* prevLIR = prevInstList[slot+1];
buzbee67bf8852011-08-17 17:51:35 -0700393
394 /* Check the highest instruction */
395 if (prevLIR->defMask == ENCODE_ALL) {
396 /*
397 * If the first instruction is a load, don't hoist anything
398 * above it since it is unlikely to be beneficial.
399 */
400 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
401 /*
402 * If the remaining number of slots is less than LD_LATENCY,
403 * insert the hoisted load here.
404 */
405 if (slot < LD_LATENCY) break;
406 }
407
408 /*
409 * NOTE: now prevLIR is guaranteed to be a non-pseudo
410 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
411 * safe).
412 *
413 * Try to find two instructions with load/use dependency until
414 * the remaining instructions are less than LD_LATENCY.
415 */
416 if (((curLIR->useMask & prevLIR->defMask) &&
417 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
418 (slot < LD_LATENCY)) {
419 break;
420 }
421 }
422
423 /* Found a slot to hoist to */
424 if (slot >= 0) {
buzbee31a4a6f2012-02-28 15:36:15 -0800425 LIR* curLIR = prevInstList[slot];
426 LIR* newLoadLIR = (LIR* ) oatNew(cUnit, sizeof(LIR),
buzbee5abfa3e2012-01-31 17:01:43 -0800427 true, kAllocLIR);
buzbee67bf8852011-08-17 17:51:35 -0700428 *newLoadLIR = *thisLIR;
429 /*
430 * Insertion is guaranteed to succeed since checkLIR
431 * is never the first LIR on the list
432 */
433 oatInsertLIRBefore((LIR*) curLIR, (LIR*) newLoadLIR);
434 thisLIR->flags.isNop = true;
435 }
436 }
437 }
438}
439
440void oatApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
441 LIR* tailLIR)
442{
443 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
buzbee31a4a6f2012-02-28 15:36:15 -0800444 applyLoadStoreElimination(cUnit, (LIR* ) headLIR,
445 (LIR* ) tailLIR);
buzbee67bf8852011-08-17 17:51:35 -0700446 }
447 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
buzbee31a4a6f2012-02-28 15:36:15 -0800448 applyLoadHoisting(cUnit, (LIR* ) headLIR, (LIR* ) tailLIR);
buzbee67bf8852011-08-17 17:51:35 -0700449 }
450}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800451
452} // namespace art