blob: 4c0354ac8017c466c825e6c848592b53a47af45e [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
134 u8 checkMemMask = (checkLIR->useMask | checkLIR->defMask) &
135 ENCODE_MEM;
136 u8 aliasCondition = thisMemMask & checkMemMask;
137 bool stopHere = false;
138
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700139 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800140 * Potential aliases seen - check the alias relations
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700141 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800142 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
143 bool isCheckLIRLoad = EncodingMap[checkLIR->opcode].flags &
144 IS_LOAD;
145 if (aliasCondition == ENCODE_LITERAL) {
Ben Chenge9695e52009-06-16 16:11:47 -0700146 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800147 * Should only see literal loads in the instruction
148 * stream.
Ben Chenge9695e52009-06-16 16:11:47 -0700149 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800150 assert(!(EncodingMap[checkLIR->opcode].flags &
151 IS_STORE));
152 /* Same value && same register type */
153 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
154 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)){
155 /*
156 * Different destination register - insert
157 * a move
158 */
159 if (checkLIR->operands[0] != nativeRegId) {
160 convertMemOpIntoMove(cUnit, checkLIR,
161 checkLIR->operands[0],
162 nativeRegId);
163 }
164 checkLIR->flags.isNop = true;
165 }
166 } else if (aliasCondition == ENCODE_DALVIK_REG) {
167 /* Must alias */
168 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
169 /* Only optimize compatible registers */
170 bool regCompatible =
171 REGTYPE(checkLIR->operands[0]) ==
172 REGTYPE(nativeRegId);
173 if ((isThisLIRLoad && isCheckLIRLoad) ||
174 (!isThisLIRLoad && isCheckLIRLoad)) {
175 /* RAR or RAW */
176 if (regCompatible) {
177 /*
178 * Different destination register -
179 * insert a move
180 */
181 if (checkLIR->operands[0] !=
182 nativeRegId) {
183 convertMemOpIntoMove(cUnit,
184 checkLIR,
185 checkLIR->operands[0],
186 nativeRegId);
187 }
188 checkLIR->flags.isNop = true;
189 } else {
190 /*
191 * Destinaions are of different types -
192 * something complicated going on so
193 * stop looking now.
194 */
195 stopHere = true;
196 }
197 } else if (isThisLIRLoad && !isCheckLIRLoad) {
198 /* WAR - register value is killed */
199 stopHere = true;
200 } else if (!isThisLIRLoad && !isCheckLIRLoad) {
201 /* WAW - nuke the earlier store */
202 thisLIR->flags.isNop = true;
203 stopHere = true;
204 }
205 /* Partial overlap */
206 } else if (isDalvikRegisterClobbered(thisLIR, checkLIR)) {
207 /*
208 * It is actually ok to continue if checkLIR
209 * is a read. But it is hard to make a test
210 * case for this so we just stop here to be
211 * conservative.
212 */
213 stopHere = true;
Ben Chenge9695e52009-06-16 16:11:47 -0700214 }
215 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800216 /* Memory content may be updated. Stop looking now. */
217 if (stopHere) {
218 break;
219 /* The checkLIR has been transformed - check the next one */
220 } else if (checkLIR->flags.isNop) {
221 continue;
222 }
223 }
224
225
226 /*
227 * this and check LIRs have no memory dependency. Now check if
228 * their register operands have any RAW, WAR, and WAW
229 * dependencies. If so, stop looking.
230 */
231 if (stopHere == false) {
232 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
233 checkLIR);
234 }
235
236 if (stopHere == true) {
237 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR,
238 "REG CLOBBERED"));
239 /* Only sink store instructions */
240 if (sinkDistance && !isThisLIRLoad) {
241 ArmLIR *newStoreLIR =
242 (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR), true);
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 dvmCompilerInsertLIRBefore((LIR *) checkLIR,
250 (LIR *) newStoreLIR);
251 thisLIR->flags.isNop = true;
252 }
253 break;
254 } else if (!checkLIR->flags.isNop) {
255 sinkDistance++;
Ben Chenge9695e52009-06-16 16:11:47 -0700256 }
257 }
258 }
259}
260
Ben Cheng7ab74e12011-02-03 14:02:06 -0800261/*
262 * Perform a pass of bottom-up walk, from the second instruction in the
263 * superblock, to try to hoist loads to earlier slots.
264 */
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700265static void applyLoadHoisting(CompilationUnit *cUnit,
266 ArmLIR *headLIR,
267 ArmLIR *tailLIR)
268{
Ben Cheng7ab74e12011-02-03 14:02:06 -0800269 ArmLIR *thisLIR, *checkLIR;
Bill Buzbee1f748632010-03-02 16:14:41 -0800270 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800271 * Store the list of independent instructions that can be hoisted past.
272 * Will decide the best place to insert later.
Bill Buzbee1f748632010-03-02 16:14:41 -0800273 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800274 ArmLIR *prevInstList[MAX_HOIST_DISTANCE];
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700275
Ben Cheng7ab74e12011-02-03 14:02:06 -0800276 /* Empty block */
277 if (headLIR == tailLIR) return;
278
279 /* Start from the second instruction */
280 for (thisLIR = NEXT_LIR(headLIR);
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700281 thisLIR != tailLIR;
282 thisLIR = NEXT_LIR(thisLIR)) {
Ben Cheng7ab74e12011-02-03 14:02:06 -0800283
284 /* Skip non-interesting instructions */
285 if ((thisLIR->flags.isNop == true) ||
286 isPseudoOpcode(thisLIR->opcode) ||
287 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700288 continue;
289 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800290
Ben Cheng7ab74e12011-02-03 14:02:06 -0800291 u8 stopUseAllMask = thisLIR->useMask;
292
293 /*
294 * Branches for null/range checks are marked with the true resource
295 * bits, and loads to Dalvik registers, constant pools, and non-alias
296 * locations are safe to be hoisted. So only mark the heap references
297 * conservatively here.
298 */
299 if (stopUseAllMask & ENCODE_HEAP_REF) {
300 stopUseAllMask |= ENCODE_REG_PC;
Bill Buzbee1f748632010-03-02 16:14:41 -0800301 }
302
Ben Cheng7ab74e12011-02-03 14:02:06 -0800303 /* Similar as above, but just check for pure register dependency */
304 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
305 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
Bill Buzbee1f748632010-03-02 16:14:41 -0800306
Ben Cheng7ab74e12011-02-03 14:02:06 -0800307 int nextSlot = 0;
308 bool stopHere;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700309
Ben Cheng7ab74e12011-02-03 14:02:06 -0800310 /* Try to hoist the load to a good spot */
311 for (checkLIR = PREV_LIR(thisLIR);
312 checkLIR != headLIR;
313 checkLIR = PREV_LIR(checkLIR)) {
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700314
Ben Cheng7ab74e12011-02-03 14:02:06 -0800315 if (checkLIR->flags.isNop) continue;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700316
Ben Cheng7ab74e12011-02-03 14:02:06 -0800317 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
318 u8 aliasCondition = stopUseAllMask & checkMemMask;
319 stopHere = false;
Ben Chengd7d426a2009-09-22 11:23:36 -0700320
Ben Cheng7ab74e12011-02-03 14:02:06 -0800321 /* Potential WAR alias seen - check the exact relation */
322 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
323 /* We can fully disambiguate Dalvik references */
324 if (aliasCondition == ENCODE_DALVIK_REG) {
325 /* Must alias or partually overlap */
326 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
327 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
328 stopHere = true;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700329 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800330 /* Conservatively treat all heap refs as may-alias */
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700331 } else {
Ben Cheng7ab74e12011-02-03 14:02:06 -0800332 assert(aliasCondition == ENCODE_HEAP_REF);
333 stopHere = true;
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700334 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800335 /* Memory content may be updated. Stop looking now. */
336 if (stopHere) {
337 prevInstList[nextSlot++] = checkLIR;
Bill Buzbee1f748632010-03-02 16:14:41 -0800338 break;
339 }
340 }
341
Ben Cheng7ab74e12011-02-03 14:02:06 -0800342 if (stopHere == false) {
343 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
344 checkLIR);
345 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800346
347 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800348 * Store the dependent or non-pseudo/indepedent instruction to the
349 * list.
Bill Buzbee1f748632010-03-02 16:14:41 -0800350 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800351 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
352 prevInstList[nextSlot++] = checkLIR;
353 if (nextSlot == MAX_HOIST_DISTANCE) break;
354 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800355
Ben Cheng7ab74e12011-02-03 14:02:06 -0800356 /* Found a new place to put the load - move it here */
357 if (stopHere == true) {
358 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR
359 "HOIST STOP"));
360 break;
361 }
362 }
363
364 /*
365 * Reached the top - use headLIR as the dependent marker as all labels
366 * are barriers.
367 */
368 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
369 prevInstList[nextSlot++] = headLIR;
370 }
371
372 /*
373 * At least one independent instruction is found. Scan in the reversed
374 * direction to find a beneficial slot.
375 */
376 if (nextSlot >= 2) {
377 int firstSlot = nextSlot - 2;
378 int slot;
379 ArmLIR *depLIR = prevInstList[nextSlot-1];
380 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
381 if (!isPseudoOpcode(depLIR->opcode) &&
382 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
383 firstSlot -= LDLD_DISTANCE;
384 }
385 /*
386 * Make sure we check slot >= 0 since firstSlot may be negative
387 * when the loop is first entered.
388 */
389 for (slot = firstSlot; slot >= 0; slot--) {
390 ArmLIR *curLIR = prevInstList[slot];
391 ArmLIR *prevLIR = prevInstList[slot+1];
392
393 /* Check the highest instruction */
394 if (prevLIR->defMask == ENCODE_ALL) {
395 /*
396 * If the first instruction is a load, don't hoist anything
397 * above it since it is unlikely to be beneficial.
398 */
399 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
400 /*
401 * If the remaining number of slots is less than LD_LATENCY,
402 * insert the hoisted load here.
403 */
404 if (slot < LD_LATENCY) break;
405 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800406
407 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800408 * NOTE: now prevLIR is guaranteed to be a non-pseudo
409 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
410 * safe).
411 *
412 * Try to find two instructions with load/use dependency until
413 * the remaining instructions are less than LD_LATENCY.
Bill Buzbee1f748632010-03-02 16:14:41 -0800414 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800415 if (((curLIR->useMask & prevLIR->defMask) &&
416 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
417 (slot < LD_LATENCY)) {
Bill Buzbee1f748632010-03-02 16:14:41 -0800418 break;
419 }
Ben Cheng7ab74e12011-02-03 14:02:06 -0800420 }
Bill Buzbee1f748632010-03-02 16:14:41 -0800421
Ben Cheng7ab74e12011-02-03 14:02:06 -0800422 /* Found a slot to hoist to */
423 if (slot >= 0) {
424 ArmLIR *curLIR = prevInstList[slot];
425 ArmLIR *newLoadLIR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR),
426 true);
427 *newLoadLIR = *thisLIR;
Bill Buzbee1f748632010-03-02 16:14:41 -0800428 /*
Ben Cheng7ab74e12011-02-03 14:02:06 -0800429 * Insertion is guaranteed to succeed since checkLIR
430 * is never the first LIR on the list
Bill Buzbee1f748632010-03-02 16:14:41 -0800431 */
Ben Cheng7ab74e12011-02-03 14:02:06 -0800432 dvmCompilerInsertLIRBefore((LIR *) curLIR,
433 (LIR *) newLoadLIR);
434 thisLIR->flags.isNop = true;
Bill Buzbee1f748632010-03-02 16:14:41 -0800435 }
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700436 }
437 }
438}
439
Ben Chenge9695e52009-06-16 16:11:47 -0700440void dvmCompilerApplyLocalOptimizations(CompilationUnit *cUnit, LIR *headLIR,
441 LIR *tailLIR)
442{
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700443 if (!(gDvmJit.disableOpt & (1 << kLoadStoreElimination))) {
444 applyLoadStoreElimination(cUnit, (ArmLIR *) headLIR,
445 (ArmLIR *) tailLIR);
446 }
447 if (!(gDvmJit.disableOpt & (1 << kLoadHoisting))) {
448 applyLoadHoisting(cUnit, (ArmLIR *) headLIR, (ArmLIR *) tailLIR);
449 }
Ben Chenge9695e52009-06-16 16:11:47 -0700450}