blob: cc276f2394b1a63423bc1935fc613ffc30e4e21e [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
buzbeee3acd072012-02-25 17:03:10 -080030STATIC inline bool isDalvikRegisterClobbered(TGT_LIR* lir1, TGT_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 */
buzbeee3acd072012-02-25 17:03:10 -080041STATIC void convertMemOpIntoMove(CompilationUnit* cUnit, TGT_LIR* origLIR,
buzbee67bf8852011-08-17 17:51:35 -070042 int dest, int src)
43{
44 /* Insert a move to replace the load */
buzbeee3acd072012-02-25 17:03:10 -080045 TGT_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 */
buzbeeed3e9302011-09-23 17:34:19 -070074STATIC void applyLoadStoreElimination(CompilationUnit* cUnit,
buzbeee3acd072012-02-25 17:03:10 -080075 TGT_LIR* headLIR,
76 TGT_LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -070077{
buzbeee3acd072012-02-25 17:03:10 -080078 TGT_LIR* thisLIR;
buzbee67bf8852011-08-17 17:51:35 -070079
80 if (headLIR == tailLIR) return;
81
82 for (thisLIR = PREV_LIR(tailLIR);
83 thisLIR != headLIR;
84 thisLIR = PREV_LIR(thisLIR)) {
85 int sinkDistance = 0;
86
87 /* Skip non-interesting instructions */
88 if ((thisLIR->flags.isNop == true) ||
89 isPseudoOpcode(thisLIR->opcode) ||
90 !(EncodingMap[thisLIR->opcode].flags & (IS_LOAD | IS_STORE))) {
91 continue;
92 }
93
94 int nativeRegId = thisLIR->operands[0];
95 bool isThisLIRLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
buzbeee3acd072012-02-25 17:03:10 -080096 TGT_LIR* checkLIR;
buzbee67bf8852011-08-17 17:51:35 -070097 /* Use the mem mask to determine the rough memory location */
98 u8 thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
99
100 /*
101 * Currently only eliminate redundant ld/st for constant and Dalvik
102 * register accesses.
103 */
104 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
105
106 /*
107 * Add r15 (pc) to the resource mask to prevent this instruction
108 * from sinking past branch instructions. Also take out the memory
109 * region bits since stopMask is used to check data/control
110 * dependencies.
111 */
112 u8 stopUseRegMask = (ENCODE_REG_PC | thisLIR->useMask) &
113 ~ENCODE_MEM;
114 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
115
116 for (checkLIR = NEXT_LIR(thisLIR);
117 checkLIR != tailLIR;
118 checkLIR = NEXT_LIR(checkLIR)) {
119
120 /*
121 * Skip already dead instructions (whose dataflow information is
122 * outdated and misleading).
123 */
124 if (checkLIR->flags.isNop) continue;
125
126 u8 checkMemMask = (checkLIR->useMask | checkLIR->defMask) &
127 ENCODE_MEM;
128 u8 aliasCondition = thisMemMask & checkMemMask;
129 bool stopHere = false;
130
131 /*
132 * Potential aliases seen - check the alias relations
133 */
134 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
135 bool isCheckLIRLoad = EncodingMap[checkLIR->opcode].flags &
136 IS_LOAD;
137 if (aliasCondition == ENCODE_LITERAL) {
138 /*
139 * Should only see literal loads in the instruction
140 * stream.
141 */
buzbeeed3e9302011-09-23 17:34:19 -0700142 DCHECK(!(EncodingMap[checkLIR->opcode].flags &
buzbee67bf8852011-08-17 17:51:35 -0700143 IS_STORE));
144 /* Same value && same register type */
145 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
146 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)){
147 /*
148 * Different destination register - insert
149 * a move
150 */
151 if (checkLIR->operands[0] != nativeRegId) {
152 convertMemOpIntoMove(cUnit, checkLIR,
153 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]) ==
164 REGTYPE(nativeRegId);
165 if ((isThisLIRLoad && isCheckLIRLoad) ||
166 (!isThisLIRLoad && isCheckLIRLoad)) {
167 /* RAR or RAW */
168 if (regCompatible) {
169 /*
170 * Different destination register -
171 * insert a move
172 */
173 if (checkLIR->operands[0] !=
174 nativeRegId) {
175 convertMemOpIntoMove(cUnit,
176 checkLIR,
177 checkLIR->operands[0],
178 nativeRegId);
179 }
180 checkLIR->flags.isNop = true;
181 } else {
182 /*
183 * Destinaions are of different types -
184 * something complicated going on so
185 * stop looking now.
186 */
187 stopHere = true;
188 }
189 } else if (isThisLIRLoad && !isCheckLIRLoad) {
190 /* WAR - register value is killed */
191 stopHere = true;
192 } else if (!isThisLIRLoad && !isCheckLIRLoad) {
193 /* WAW - nuke the earlier store */
194 thisLIR->flags.isNop = true;
195 stopHere = true;
196 }
197 /* Partial overlap */
198 } else if (isDalvikRegisterClobbered(thisLIR, checkLIR)) {
199 /*
200 * It is actually ok to continue if checkLIR
201 * is a read. But it is hard to make a test
202 * case for this so we just stop here to be
203 * conservative.
204 */
205 stopHere = true;
206 }
207 }
208 /* Memory content may be updated. Stop looking now. */
209 if (stopHere) {
210 break;
211 /* The checkLIR has been transformed - check the next one */
212 } else if (checkLIR->flags.isNop) {
213 continue;
214 }
215 }
216
217
218 /*
219 * this and check LIRs have no memory dependency. Now check if
220 * their register operands have any RAW, WAR, and WAW
221 * dependencies. If so, stop looking.
222 */
223 if (stopHere == false) {
224 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
225 checkLIR);
226 }
227
228 if (stopHere == true) {
229 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR,
230 "REG CLOBBERED"));
231 /* Only sink store instructions */
232 if (sinkDistance && !isThisLIRLoad) {
buzbeee3acd072012-02-25 17:03:10 -0800233 TGT_LIR* newStoreLIR =
234 (TGT_LIR* ) oatNew(cUnit, sizeof(TGT_LIR), true,
buzbeeba938cb2012-02-03 14:47:55 -0800235 kAllocLIR);
buzbee67bf8852011-08-17 17:51:35 -0700236 *newStoreLIR = *thisLIR;
237 /*
238 * Stop point found - insert *before* the checkLIR
239 * since the instruction list is scanned in the
240 * top-down order.
241 */
242 oatInsertLIRBefore((LIR*) checkLIR,
243 (LIR*) newStoreLIR);
244 thisLIR->flags.isNop = true;
245 }
246 break;
247 } else if (!checkLIR->flags.isNop) {
248 sinkDistance++;
249 }
250 }
251 }
252}
253
254/*
255 * Perform a pass of bottom-up walk, from the second instruction in the
256 * superblock, to try to hoist loads to earlier slots.
257 */
buzbeeed3e9302011-09-23 17:34:19 -0700258STATIC void applyLoadHoisting(CompilationUnit* cUnit,
buzbeee3acd072012-02-25 17:03:10 -0800259 TGT_LIR* headLIR,
260 TGT_LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700261{
buzbeee3acd072012-02-25 17:03:10 -0800262 TGT_LIR* thisLIR, *checkLIR;
buzbee67bf8852011-08-17 17:51:35 -0700263 /*
264 * Store the list of independent instructions that can be hoisted past.
265 * Will decide the best place to insert later.
266 */
buzbeee3acd072012-02-25 17:03:10 -0800267 TGT_LIR* prevInstList[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700268
269 /* Empty block */
270 if (headLIR == tailLIR) return;
271
272 /* Start from the second instruction */
273 for (thisLIR = NEXT_LIR(headLIR);
274 thisLIR != tailLIR;
275 thisLIR = NEXT_LIR(thisLIR)) {
276
277 /* Skip non-interesting instructions */
278 if ((thisLIR->flags.isNop == true) ||
279 isPseudoOpcode(thisLIR->opcode) ||
280 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
281 continue;
282 }
283
284 u8 stopUseAllMask = thisLIR->useMask;
285
286 /*
287 * Branches for null/range checks are marked with the true resource
288 * bits, and loads to Dalvik registers, constant pools, and non-alias
289 * locations are safe to be hoisted. So only mark the heap references
290 * conservatively here.
291 */
292 if (stopUseAllMask & ENCODE_HEAP_REF) {
293 stopUseAllMask |= ENCODE_REG_PC;
294 }
295
296 /* Similar as above, but just check for pure register dependency */
297 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
298 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
299
300 int nextSlot = 0;
301 bool stopHere = false;
302
303 /* Try to hoist the load to a good spot */
304 for (checkLIR = PREV_LIR(thisLIR);
305 checkLIR != headLIR;
306 checkLIR = PREV_LIR(checkLIR)) {
307
308 /*
309 * Skip already dead instructions (whose dataflow information is
310 * outdated and misleading).
311 */
312 if (checkLIR->flags.isNop) continue;
313
314 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
315 u8 aliasCondition = stopUseAllMask & checkMemMask;
316 stopHere = false;
317
318 /* Potential WAR alias seen - check the exact relation */
319 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
320 /* We can fully disambiguate Dalvik references */
321 if (aliasCondition == ENCODE_DALVIK_REG) {
322 /* Must alias or partually overlap */
323 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
324 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
325 stopHere = true;
326 }
327 /* Conservatively treat all heap refs as may-alias */
328 } else {
buzbeeed3e9302011-09-23 17:34:19 -0700329 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
buzbee67bf8852011-08-17 17:51:35 -0700330 stopHere = true;
331 }
332 /* Memory content may be updated. Stop looking now. */
333 if (stopHere) {
334 prevInstList[nextSlot++] = checkLIR;
335 break;
336 }
337 }
338
339 if (stopHere == false) {
340 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
341 checkLIR);
342 }
343
344 /*
345 * Store the dependent or non-pseudo/indepedent instruction to the
346 * list.
347 */
348 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
349 prevInstList[nextSlot++] = checkLIR;
350 if (nextSlot == MAX_HOIST_DISTANCE) break;
351 }
352
353 /* Found a new place to put the load - move it here */
354 if (stopHere == true) {
355 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR
356 "HOIST STOP"));
357 break;
358 }
359 }
360
361 /*
362 * Reached the top - use headLIR as the dependent marker as all labels
363 * are barriers.
364 */
365 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
366 prevInstList[nextSlot++] = headLIR;
367 }
368
369 /*
370 * At least one independent instruction is found. Scan in the reversed
371 * direction to find a beneficial slot.
372 */
373 if (nextSlot >= 2) {
374 int firstSlot = nextSlot - 2;
375 int slot;
buzbeee3acd072012-02-25 17:03:10 -0800376 TGT_LIR* depLIR = prevInstList[nextSlot-1];
buzbee67bf8852011-08-17 17:51:35 -0700377 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
378 if (!isPseudoOpcode(depLIR->opcode) &&
379 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
380 firstSlot -= LDLD_DISTANCE;
381 }
382 /*
383 * Make sure we check slot >= 0 since firstSlot may be negative
384 * when the loop is first entered.
385 */
386 for (slot = firstSlot; slot >= 0; slot--) {
buzbeee3acd072012-02-25 17:03:10 -0800387 TGT_LIR* curLIR = prevInstList[slot];
388 TGT_LIR* prevLIR = prevInstList[slot+1];
buzbee67bf8852011-08-17 17:51:35 -0700389
390 /* Check the highest instruction */
391 if (prevLIR->defMask == ENCODE_ALL) {
392 /*
393 * If the first instruction is a load, don't hoist anything
394 * above it since it is unlikely to be beneficial.
395 */
396 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
397 /*
398 * If the remaining number of slots is less than LD_LATENCY,
399 * insert the hoisted load here.
400 */
401 if (slot < LD_LATENCY) break;
402 }
403
404 /*
405 * NOTE: now prevLIR is guaranteed to be a non-pseudo
406 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
407 * safe).
408 *
409 * Try to find two instructions with load/use dependency until
410 * the remaining instructions are less than LD_LATENCY.
411 */
412 if (((curLIR->useMask & prevLIR->defMask) &&
413 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
414 (slot < LD_LATENCY)) {
415 break;
416 }
417 }
418
419 /* Found a slot to hoist to */
420 if (slot >= 0) {
buzbeee3acd072012-02-25 17:03:10 -0800421 TGT_LIR* curLIR = prevInstList[slot];
422 TGT_LIR* newLoadLIR = (TGT_LIR* ) oatNew(cUnit, sizeof(TGT_LIR),
buzbee5abfa3e2012-01-31 17:01:43 -0800423 true, kAllocLIR);
buzbee67bf8852011-08-17 17:51:35 -0700424 *newLoadLIR = *thisLIR;
425 /*
426 * Insertion is guaranteed to succeed since checkLIR
427 * is never the first LIR on the list
428 */
429 oatInsertLIRBefore((LIR*) curLIR, (LIR*) newLoadLIR);
430 thisLIR->flags.isNop = true;
431 }
432 }
433 }
434}
435
436void oatApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
437 LIR* tailLIR)
438{
439 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
buzbeee3acd072012-02-25 17:03:10 -0800440 applyLoadStoreElimination(cUnit, (TGT_LIR* ) headLIR,
441 (TGT_LIR* ) tailLIR);
buzbee67bf8852011-08-17 17:51:35 -0700442 }
443 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
buzbeee3acd072012-02-25 17:03:10 -0800444 applyLoadHoisting(cUnit, (TGT_LIR* ) headLIR, (TGT_LIR* ) tailLIR);
buzbee67bf8852011-08-17 17:51:35 -0700445 }
446}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800447
448} // namespace art