blob: ffeaa576f7d14fda439b808aebb4d519e0db2002 [file] [log] [blame]
Ben Chenge9695e52009-06-16 16:11:47 -07001/*
2 * Copyright (C) 2009 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
17#include "Dalvik.h"
18#include "vm/compiler/CompilerInternals.h"
Bill Buzbee89efc3d2009-07-28 11:22:22 -070019#include "ArmLIR.h"
Ben Cheng5d90c202009-11-22 23:31:11 -080020#include "Codegen.h"
Ben Chenge9695e52009-06-16 16:11:47 -070021
Ben Chengd7d426a2009-09-22 11:23:36 -070022#define DEBUG_OPT(X)
23
Ben Cheng7ab74e12011-02-03 14:02:06 -080024/* Check RAW, WAR, and WAR dependency on the register operands */
25#define CHECK_REG_DEP(use, def, check) ((def & check->useMask) || \
26 ((use | def) & check->defMask))
Bill Buzbee7ea0f642009-08-10 17:06:51 -070027
Ben Cheng7ab74e12011-02-03 14:02:06 -080028/* Scheduler heuristics */
29#define MAX_HOIST_DISTANCE 20
30#define LDLD_DISTANCE 4
31#define LD_LATENCY 2
Bill Buzbee7ea0f642009-08-10 17:06:51 -070032
Ben Chenga4aaf682009-09-30 22:53:44 -070033static inline bool isDalvikRegisterClobbered(ArmLIR *lir1, ArmLIR *lir2)
Bill Buzbee270c1d62009-08-13 16:58:07 -070034{
Ben Cheng7ab74e12011-02-03 14:02:06 -080035 int reg1Lo = DECODE_ALIAS_INFO_REG(lir1->aliasInfo);
36 int reg1Hi = reg1Lo + DECODE_ALIAS_INFO_WIDE(lir1->aliasInfo);
37 int reg2Lo = DECODE_ALIAS_INFO_REG(lir2->aliasInfo);
38 int reg2Hi = reg2Lo + DECODE_ALIAS_INFO_WIDE(lir2->aliasInfo);
Ben Chengd7d426a2009-09-22 11:23:36 -070039
Ben Cheng7ab74e12011-02-03 14:02:06 -080040 return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo);
Bill Buzbee270c1d62009-08-13 16:58:07 -070041}
Ben Chengd7d426a2009-09-22 11:23:36 -070042
Ben Chengfc075c22010-05-28 15:20:08 -070043#if 0
44/* Debugging utility routine */
Ben Chengd7d426a2009-09-22 11:23:36 -070045static void dumpDependentInsnPair(ArmLIR *thisLIR, ArmLIR *checkLIR,
46 const char *optimization)
47{
48 LOGD("************ %s ************", optimization);
49 dvmDumpLIRInsn((LIR *) thisLIR, 0);
50 dvmDumpLIRInsn((LIR *) checkLIR, 0);
51}
Ben Chengfc075c22010-05-28 15:20:08 -070052#endif
Ben Chengd7d426a2009-09-22 11:23:36 -070053
Ben Cheng7ab74e12011-02-03 14:02:06 -080054/* Convert a more expensive instruction (ie load) into a move */
55static void convertMemOpIntoMove(CompilationUnit *cUnit, ArmLIR *origLIR,
56 int dest, int src)
57{
58 /* Insert a move to replace the load */
59 ArmLIR *moveLIR;
60 moveLIR = dvmCompilerRegCopyNoInsert( cUnit, dest, src);
61 /*
62 * Insert the converted instruction after the original since the
63 * optimization is scannng in the top-down order and the new instruction
64 * will need to be re-checked (eg the new dest clobbers the src used in
65 * thisLIR).
66 */
67 dvmCompilerInsertLIRAfter((LIR *) origLIR, (LIR *) moveLIR);
68}
69
Ben Chenge9695e52009-06-16 16:11:47 -070070/*
Ben Cheng7ab74e12011-02-03 14:02:06 -080071 * Perform a pass of top-down walk, from the second-last instruction in the
72 * superblock, to eliminate redundant loads and stores.
73 *
74 * An earlier load can eliminate a later load iff
75 * 1) They are must-aliases
76 * 2) The native register is not clobbered in between
77 * 3) The memory location is not written to in between
78 *
79 * An earlier store can eliminate a later load iff
80 * 1) They are must-aliases
81 * 2) The native register is not clobbered in between
82 * 3) The memory location is not written to in between
83 *
84 * A later store can be eliminated by an earlier store iff
85 * 1) They are must-aliases
86 * 2) The memory location is not written to in between
Ben Chenge9695e52009-06-16 16:11:47 -070087 */
88static void applyLoadStoreElimination(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -070089 ArmLIR *headLIR,
90 ArmLIR *tailLIR)
Ben Chenge9695e52009-06-16 16:11:47 -070091{
Bill Buzbee89efc3d2009-07-28 11:22:22 -070092 ArmLIR *thisLIR;
Ben Chenge9695e52009-06-16 16:11:47 -070093
Ben Cheng7ab74e12011-02-03 14:02:06 -080094 if (headLIR == tailLIR) return;
95
96 for (thisLIR = PREV_LIR(tailLIR);
97 thisLIR != headLIR;
98 thisLIR = PREV_LIR(thisLIR)) {
99 int sinkDistance = 0;
100
101 /* Skip non-interesting instructions */
102 if ((thisLIR->flags.isNop == true) ||
103 isPseudoOpcode(thisLIR->opcode) ||
104 !(EncodingMap[thisLIR->opcode].flags & (IS_LOAD | IS_STORE))) {
Ben Chenge9695e52009-06-16 16:11:47 -0700105 continue;
106 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800107
108 int nativeRegId = thisLIR->operands[0];
109 bool isThisLIRLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
110 ArmLIR *checkLIR;
111 /* Use the mem mask to determine the rough memory location */
112 u8 thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
113
114 /*
115 * Currently only eliminate redundant ld/st for constant and Dalvik
116 * register accesses.
117 */
118 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
119
120 /*
121 * Add r15 (pc) to the resource mask to prevent this instruction
122 * from sinking past branch instructions. Also take out the memory
123 * region bits since stopMask is used to check data/control
124 * dependencies.
125 */
126 u8 stopUseRegMask = (ENCODE_REG_PC | thisLIR->useMask) &
127 ~ENCODE_MEM;
128 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
129
130 for (checkLIR = NEXT_LIR(thisLIR);
131 checkLIR != tailLIR;
132 checkLIR = NEXT_LIR(checkLIR)) {
133
Ben Chengde9e2b92011-04-26 10:00:22 -0700134 /*
135 * Skip already dead instructions (whose dataflow information is
136 * outdated and misleading).
137 */
138 if (checkLIR->flags.isNop) continue;
139
Ben Cheng7ab74e12011-02-03 14:02:06 -0800140 u8 checkMemMask = (checkLIR->useMask | checkLIR->defMask) &
141 ENCODE_MEM;
142 u8 aliasCondition = thisMemMask & checkMemMask;
143 bool stopHere = false;
144
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700145 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800146 * Potential aliases seen - check the alias relations
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700147 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800148 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
149 bool isCheckLIRLoad = EncodingMap[checkLIR->opcode].flags &
150 IS_LOAD;
151 if (aliasCondition == ENCODE_LITERAL) {
Ben Chenge9695e52009-06-16 16:11:47 -0700152 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800153 * Should only see literal loads in the instruction
154 * stream.
Ben Chenge9695e52009-06-16 16:11:47 -0700155 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800156 assert(!(EncodingMap[checkLIR->opcode].flags &
157 IS_STORE));
158 /* Same value && same register type */
159 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
160 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)){
161 /*
162 * Different destination register - insert
163 * a move
164 */
165 if (checkLIR->operands[0] != nativeRegId) {
166 convertMemOpIntoMove(cUnit, checkLIR,
167 checkLIR->operands[0],
168 nativeRegId);
169 }
170 checkLIR->flags.isNop = true;
171 }
172 } else if (aliasCondition == ENCODE_DALVIK_REG) {
173 /* Must alias */
174 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
175 /* Only optimize compatible registers */
176 bool regCompatible =
177 REGTYPE(checkLIR->operands[0]) ==
178 REGTYPE(nativeRegId);
179 if ((isThisLIRLoad && isCheckLIRLoad) ||
180 (!isThisLIRLoad && isCheckLIRLoad)) {
181 /* RAR or RAW */
182 if (regCompatible) {
183 /*
184 * Different destination register -
185 * insert a move
186 */
187 if (checkLIR->operands[0] !=
188 nativeRegId) {
189 convertMemOpIntoMove(cUnit,
190 checkLIR,
191 checkLIR->operands[0],
192 nativeRegId);
193 }
194 checkLIR->flags.isNop = true;
195 } else {
196 /*
197 * Destinaions are of different types -
198 * something complicated going on so
199 * stop looking now.
200 */
201 stopHere = true;
202 }
203 } else if (isThisLIRLoad && !isCheckLIRLoad) {
204 /* WAR - register value is killed */
205 stopHere = true;
206 } else if (!isThisLIRLoad && !isCheckLIRLoad) {
207 /* WAW - nuke the earlier store */
208 thisLIR->flags.isNop = true;
209 stopHere = true;
210 }
211 /* Partial overlap */
212 } else if (isDalvikRegisterClobbered(thisLIR, checkLIR)) {
213 /*
214 * It is actually ok to continue if checkLIR
215 * is a read. But it is hard to make a test
216 * case for this so we just stop here to be
217 * conservative.
218 */
219 stopHere = true;
Ben Chenge9695e52009-06-16 16:11:47 -0700220 }
221 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800222 /* Memory content may be updated. Stop looking now. */
223 if (stopHere) {
224 break;
225 /* The checkLIR has been transformed - check the next one */
226 } else if (checkLIR->flags.isNop) {
227 continue;
228 }
229 }
230
231
232 /*
233 * this and check LIRs have no memory dependency. Now check if
234 * their register operands have any RAW, WAR, and WAW
235 * dependencies. If so, stop looking.
236 */
237 if (stopHere == false) {
238 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
239 checkLIR);
240 }
241
242 if (stopHere == true) {
243 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR,
244 "REG CLOBBERED"));
245 /* Only sink store instructions */
246 if (sinkDistance && !isThisLIRLoad) {
247 ArmLIR *newStoreLIR =
248 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
249 *newStoreLIR = *thisLIR;
250 /*
251 * Stop point found - insert *before* the checkLIR
252 * since the instruction list is scanned in the
253 * top-down order.
254 */
255 dvmCompilerInsertLIRBefore((LIR *) checkLIR,
256 (LIR *) newStoreLIR);
257 thisLIR->flags.isNop = true;
258 }
259 break;
260 } else if (!checkLIR->flags.isNop) {
261 sinkDistance++;
Ben Chenge9695e52009-06-16 16:11:47 -0700262 }
263 }
264 }
265}
266
Ben Cheng7ab74e12011-02-03 14:02:06 -0800267/*
268 * Perform a pass of bottom-up walk, from the second instruction in the
269 * superblock, to try to hoist loads to earlier slots.
270 */
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700271static void applyLoadHoisting(CompilationUnit *cUnit,
272 ArmLIR *headLIR,
273 ArmLIR *tailLIR)
274{
Ben Cheng7ab74e12011-02-03 14:02:06 -0800275 ArmLIR *thisLIR, *checkLIR;
Bill Buzbee1f748632010-03-02 16:14:41 -0800276 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800277 * Store the list of independent instructions that can be hoisted past.
278 * Will decide the best place to insert later.
Bill Buzbee1f748632010-03-02 16:14:41 -0800279 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800280 ArmLIR *prevInstList[MAX_HOIST_DISTANCE];
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700281
Ben Cheng7ab74e12011-02-03 14:02:06 -0800282 /* Empty block */
283 if (headLIR == tailLIR) return;
284
285 /* Start from the second instruction */
286 for (thisLIR = NEXT_LIR(headLIR);
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700287 thisLIR != tailLIR;
288 thisLIR = NEXT_LIR(thisLIR)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -0800289
290 /* Skip non-interesting instructions */
291 if ((thisLIR->flags.isNop == true) ||
292 isPseudoOpcode(thisLIR->opcode) ||
293 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700294 continue;
295 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800296
Ben Cheng7ab74e12011-02-03 14:02:06 -0800297 u8 stopUseAllMask = thisLIR->useMask;
298
299 /*
300 * Branches for null/range checks are marked with the true resource
301 * bits, and loads to Dalvik registers, constant pools, and non-alias
302 * locations are safe to be hoisted. So only mark the heap references
303 * conservatively here.
304 */
305 if (stopUseAllMask & ENCODE_HEAP_REF) {
306 stopUseAllMask |= ENCODE_REG_PC;
Bill Buzbee1f748632010-03-02 16:14:41 -0800307 }
308
Ben Cheng7ab74e12011-02-03 14:02:06 -0800309 /* Similar as above, but just check for pure register dependency */
310 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
311 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
Bill Buzbee1f748632010-03-02 16:14:41 -0800312
Ben Cheng7ab74e12011-02-03 14:02:06 -0800313 int nextSlot = 0;
314 bool stopHere;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700315
Ben Cheng7ab74e12011-02-03 14:02:06 -0800316 /* Try to hoist the load to a good spot */
317 for (checkLIR = PREV_LIR(thisLIR);
318 checkLIR != headLIR;
319 checkLIR = PREV_LIR(checkLIR)) {
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700320
Ben Chengde9e2b92011-04-26 10:00:22 -0700321 /*
322 * Skip already dead instructions (whose dataflow information is
323 * outdated and misleading).
324 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800325 if (checkLIR->flags.isNop) continue;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700326
Ben Cheng7ab74e12011-02-03 14:02:06 -0800327 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
328 u8 aliasCondition = stopUseAllMask & checkMemMask;
329 stopHere = false;
Ben Chengd7d426a2009-09-22 11:23:36 -0700330
Ben Cheng7ab74e12011-02-03 14:02:06 -0800331 /* Potential WAR alias seen - check the exact relation */
332 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
333 /* We can fully disambiguate Dalvik references */
334 if (aliasCondition == ENCODE_DALVIK_REG) {
335 /* Must alias or partually overlap */
336 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
337 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
338 stopHere = true;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700339 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800340 /* Conservatively treat all heap refs as may-alias */
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700341 } else {
Ben Cheng7ab74e12011-02-03 14:02:06 -0800342 assert(aliasCondition == ENCODE_HEAP_REF);
343 stopHere = true;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700344 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800345 /* Memory content may be updated. Stop looking now. */
346 if (stopHere) {
347 prevInstList[nextSlot++] = checkLIR;
Bill Buzbee1f748632010-03-02 16:14:41 -0800348 break;
349 }
350 }
351
Ben Cheng7ab74e12011-02-03 14:02:06 -0800352 if (stopHere == false) {
353 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
354 checkLIR);
355 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800356
357 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800358 * Store the dependent or non-pseudo/indepedent instruction to the
359 * list.
Bill Buzbee1f748632010-03-02 16:14:41 -0800360 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800361 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
362 prevInstList[nextSlot++] = checkLIR;
363 if (nextSlot == MAX_HOIST_DISTANCE) break;
364 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800365
Ben Cheng7ab74e12011-02-03 14:02:06 -0800366 /* Found a new place to put the load - move it here */
367 if (stopHere == true) {
368 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR
369 "HOIST STOP"));
370 break;
371 }
372 }
373
374 /*
375 * Reached the top - use headLIR as the dependent marker as all labels
376 * are barriers.
377 */
378 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
379 prevInstList[nextSlot++] = headLIR;
380 }
381
382 /*
383 * At least one independent instruction is found. Scan in the reversed
384 * direction to find a beneficial slot.
385 */
386 if (nextSlot >= 2) {
387 int firstSlot = nextSlot - 2;
388 int slot;
389 ArmLIR *depLIR = prevInstList[nextSlot-1];
390 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
391 if (!isPseudoOpcode(depLIR->opcode) &&
392 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
393 firstSlot -= LDLD_DISTANCE;
394 }
395 /*
396 * Make sure we check slot >= 0 since firstSlot may be negative
397 * when the loop is first entered.
398 */
399 for (slot = firstSlot; slot >= 0; slot--) {
400 ArmLIR *curLIR = prevInstList[slot];
401 ArmLIR *prevLIR = prevInstList[slot+1];
402
403 /* Check the highest instruction */
404 if (prevLIR->defMask == ENCODE_ALL) {
405 /*
406 * If the first instruction is a load, don't hoist anything
407 * above it since it is unlikely to be beneficial.
408 */
409 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
410 /*
411 * If the remaining number of slots is less than LD_LATENCY,
412 * insert the hoisted load here.
413 */
414 if (slot < LD_LATENCY) break;
415 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800416
417 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800418 * NOTE: now prevLIR is guaranteed to be a non-pseudo
419 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
420 * safe).
421 *
422 * Try to find two instructions with load/use dependency until
423 * the remaining instructions are less than LD_LATENCY.
Bill Buzbee1f748632010-03-02 16:14:41 -0800424 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800425 if (((curLIR->useMask & prevLIR->defMask) &&
426 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
427 (slot < LD_LATENCY)) {
Bill Buzbee1f748632010-03-02 16:14:41 -0800428 break;
429 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800430 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800431
Ben Cheng7ab74e12011-02-03 14:02:06 -0800432 /* Found a slot to hoist to */
433 if (slot >= 0) {
434 ArmLIR *curLIR = prevInstList[slot];
435 ArmLIR *newLoadLIR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR),
436 true);
437 *newLoadLIR = *thisLIR;
Bill Buzbee1f748632010-03-02 16:14:41 -0800438 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800439 * Insertion is guaranteed to succeed since checkLIR
440 * is never the first LIR on the list
Bill Buzbee1f748632010-03-02 16:14:41 -0800441 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800442 dvmCompilerInsertLIRBefore((LIR *) curLIR,
443 (LIR *) newLoadLIR);
444 thisLIR->flags.isNop = true;
Bill Buzbee1f748632010-03-02 16:14:41 -0800445 }
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700446 }
447 }
448}
449
Ben Chenge9695e52009-06-16 16:11:47 -0700450void dvmCompilerApplyLocalOptimizations(CompilationUnit *cUnit, LIR *headLIR,
451 LIR *tailLIR)
452{
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700453 if (!(gDvmJit.disableOpt & (1 << kLoadStoreElimination))) {
454 applyLoadStoreElimination(cUnit, (ArmLIR *) headLIR,
455 (ArmLIR *) tailLIR);
456 }
457 if (!(gDvmJit.disableOpt & (1 << kLoadHoisting))) {
458 applyLoadHoisting(cUnit, (ArmLIR *) headLIR, (ArmLIR *) tailLIR);
459 }
Ben Chenge9695e52009-06-16 16:11:47 -0700460}