blob: b2bc3337b144ad729351a93573bb2a9421fb0c57 [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
17#include "../../Dalvik.h"
18#include "../../CompilerInternals.h"
19#include "ArmLIR.h"
20#include "Codegen.h"
21
22#define DEBUG_OPT(X)
23
24/* 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))
27
28/* Scheduler heuristics */
29#define MAX_HOIST_DISTANCE 20
30#define LDLD_DISTANCE 4
31#define LD_LATENCY 2
32
buzbeeed3e9302011-09-23 17:34:19 -070033STATIC inline bool isDalvikRegisterClobbered(ArmLIR* lir1, ArmLIR* lir2)
buzbee67bf8852011-08-17 17:51:35 -070034{
35 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);
39
40 return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo);
41}
42
43/* Convert a more expensive instruction (ie load) into a move */
buzbeeed3e9302011-09-23 17:34:19 -070044STATIC void convertMemOpIntoMove(CompilationUnit* cUnit, ArmLIR* origLIR,
buzbee67bf8852011-08-17 17:51:35 -070045 int dest, int src)
46{
47 /* Insert a move to replace the load */
48 ArmLIR* moveLIR;
49 moveLIR = oatRegCopyNoInsert( cUnit, dest, src);
50 /*
51 * Insert the converted instruction after the original since the
52 * optimization is scannng in the top-down order and the new instruction
53 * will need to be re-checked (eg the new dest clobbers the src used in
54 * thisLIR).
55 */
56 oatInsertLIRAfter((LIR*) origLIR, (LIR*) moveLIR);
57}
58
59/*
60 * Perform a pass of top-down walk, from the second-last instruction in the
61 * superblock, to eliminate redundant loads and stores.
62 *
63 * An earlier load can eliminate a later load iff
64 * 1) They are must-aliases
65 * 2) The native register is not clobbered in between
66 * 3) The memory location is not written to in between
67 *
68 * An earlier store can eliminate a later load iff
69 * 1) They are must-aliases
70 * 2) The native register is not clobbered in between
71 * 3) The memory location is not written to in between
72 *
73 * A later store can be eliminated by an earlier store iff
74 * 1) They are must-aliases
75 * 2) The memory location is not written to in between
76 */
buzbeeed3e9302011-09-23 17:34:19 -070077STATIC void applyLoadStoreElimination(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -070078 ArmLIR* headLIR,
79 ArmLIR* tailLIR)
80{
81 ArmLIR* thisLIR;
82
83 if (headLIR == tailLIR) return;
84
85 for (thisLIR = PREV_LIR(tailLIR);
86 thisLIR != headLIR;
87 thisLIR = PREV_LIR(thisLIR)) {
88 int sinkDistance = 0;
89
90 /* Skip non-interesting instructions */
91 if ((thisLIR->flags.isNop == true) ||
92 isPseudoOpcode(thisLIR->opcode) ||
93 !(EncodingMap[thisLIR->opcode].flags & (IS_LOAD | IS_STORE))) {
94 continue;
95 }
96
97 int nativeRegId = thisLIR->operands[0];
98 bool isThisLIRLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
99 ArmLIR* checkLIR;
100 /* Use the mem mask to determine the rough memory location */
101 u8 thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
102
103 /*
104 * Currently only eliminate redundant ld/st for constant and Dalvik
105 * register accesses.
106 */
107 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
108
109 /*
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;
117 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
118
119 for (checkLIR = NEXT_LIR(thisLIR);
120 checkLIR != tailLIR;
121 checkLIR = NEXT_LIR(checkLIR)) {
122
123 /*
124 * Skip already dead instructions (whose dataflow information is
125 * outdated and misleading).
126 */
127 if (checkLIR->flags.isNop) continue;
128
129 u8 checkMemMask = (checkLIR->useMask | checkLIR->defMask) &
130 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 &
139 IS_LOAD;
140 if (aliasCondition == ENCODE_LITERAL) {
141 /*
142 * Should only see literal loads in the instruction
143 * stream.
144 */
buzbeeed3e9302011-09-23 17:34:19 -0700145 DCHECK(!(EncodingMap[checkLIR->opcode].flags &
buzbee67bf8852011-08-17 17:51:35 -0700146 IS_STORE));
147 /* Same value && same register type */
148 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
149 REGTYPE(checkLIR->operands[0]) == REGTYPE(nativeRegId)){
150 /*
151 * Different destination register - insert
152 * a move
153 */
154 if (checkLIR->operands[0] != nativeRegId) {
155 convertMemOpIntoMove(cUnit, checkLIR,
156 checkLIR->operands[0],
157 nativeRegId);
158 }
159 checkLIR->flags.isNop = true;
160 }
161 } else if (aliasCondition == ENCODE_DALVIK_REG) {
162 /* Must alias */
163 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
164 /* Only optimize compatible registers */
165 bool regCompatible =
166 REGTYPE(checkLIR->operands[0]) ==
167 REGTYPE(nativeRegId);
168 if ((isThisLIRLoad && isCheckLIRLoad) ||
169 (!isThisLIRLoad && isCheckLIRLoad)) {
170 /* RAR or RAW */
171 if (regCompatible) {
172 /*
173 * Different destination register -
174 * insert a move
175 */
176 if (checkLIR->operands[0] !=
177 nativeRegId) {
178 convertMemOpIntoMove(cUnit,
179 checkLIR,
180 checkLIR->operands[0],
181 nativeRegId);
182 }
183 checkLIR->flags.isNop = true;
184 } else {
185 /*
186 * Destinaions are of different types -
187 * something complicated going on so
188 * stop looking now.
189 */
190 stopHere = true;
191 }
192 } else if (isThisLIRLoad && !isCheckLIRLoad) {
193 /* WAR - register value is killed */
194 stopHere = true;
195 } else if (!isThisLIRLoad && !isCheckLIRLoad) {
196 /* WAW - nuke the earlier store */
197 thisLIR->flags.isNop = true;
198 stopHere = true;
199 }
200 /* Partial overlap */
201 } else if (isDalvikRegisterClobbered(thisLIR, checkLIR)) {
202 /*
203 * It is actually ok to continue if checkLIR
204 * is a read. But it is hard to make a test
205 * case for this so we just stop here to be
206 * conservative.
207 */
208 stopHere = true;
209 }
210 }
211 /* Memory content may be updated. Stop looking now. */
212 if (stopHere) {
213 break;
214 /* The checkLIR has been transformed - check the next one */
215 } else if (checkLIR->flags.isNop) {
216 continue;
217 }
218 }
219
220
221 /*
222 * this and check LIRs have no memory dependency. Now check if
223 * their register operands have any RAW, WAR, and WAW
224 * dependencies. If so, stop looking.
225 */
226 if (stopHere == false) {
227 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
228 checkLIR);
229 }
230
231 if (stopHere == true) {
232 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR,
233 "REG CLOBBERED"));
234 /* Only sink store instructions */
235 if (sinkDistance && !isThisLIRLoad) {
236 ArmLIR* newStoreLIR =
237 (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
238 *newStoreLIR = *thisLIR;
239 /*
240 * Stop point found - insert *before* the checkLIR
241 * since the instruction list is scanned in the
242 * top-down order.
243 */
244 oatInsertLIRBefore((LIR*) checkLIR,
245 (LIR*) newStoreLIR);
246 thisLIR->flags.isNop = true;
247 }
248 break;
249 } else if (!checkLIR->flags.isNop) {
250 sinkDistance++;
251 }
252 }
253 }
254}
255
256/*
257 * Perform a pass of bottom-up walk, from the second instruction in the
258 * superblock, to try to hoist loads to earlier slots.
259 */
buzbeeed3e9302011-09-23 17:34:19 -0700260STATIC void applyLoadHoisting(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -0700261 ArmLIR* headLIR,
262 ArmLIR* tailLIR)
263{
264 ArmLIR* thisLIR, *checkLIR;
265 /*
266 * Store the list of independent instructions that can be hoisted past.
267 * Will decide the best place to insert later.
268 */
269 ArmLIR* prevInstList[MAX_HOIST_DISTANCE];
270
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
288 /*
289 * Branches for null/range checks are marked with the true resource
290 * bits, and loads to Dalvik registers, constant pools, and non-alias
291 * locations are safe to be hoisted. So only mark the heap references
292 * conservatively here.
293 */
294 if (stopUseAllMask & ENCODE_HEAP_REF) {
295 stopUseAllMask |= ENCODE_REG_PC;
296 }
297
298 /* Similar as above, but just check for pure register dependency */
299 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
300 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
301
302 int nextSlot = 0;
303 bool stopHere = false;
304
305 /* Try to hoist the load to a good spot */
306 for (checkLIR = PREV_LIR(thisLIR);
307 checkLIR != headLIR;
308 checkLIR = PREV_LIR(checkLIR)) {
309
310 /*
311 * Skip already dead instructions (whose dataflow information is
312 * outdated and misleading).
313 */
314 if (checkLIR->flags.isNop) continue;
315
316 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
317 u8 aliasCondition = stopUseAllMask & checkMemMask;
318 stopHere = false;
319
320 /* Potential WAR alias seen - check the exact relation */
321 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
322 /* We can fully disambiguate Dalvik references */
323 if (aliasCondition == ENCODE_DALVIK_REG) {
324 /* Must alias or partually overlap */
325 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
326 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
327 stopHere = true;
328 }
329 /* Conservatively treat all heap refs as may-alias */
330 } else {
buzbeeed3e9302011-09-23 17:34:19 -0700331 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
buzbee67bf8852011-08-17 17:51:35 -0700332 stopHere = true;
333 }
334 /* Memory content may be updated. Stop looking now. */
335 if (stopHere) {
336 prevInstList[nextSlot++] = checkLIR;
337 break;
338 }
339 }
340
341 if (stopHere == false) {
342 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
343 checkLIR);
344 }
345
346 /*
347 * Store the dependent or non-pseudo/indepedent instruction to the
348 * list.
349 */
350 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
351 prevInstList[nextSlot++] = checkLIR;
352 if (nextSlot == MAX_HOIST_DISTANCE) break;
353 }
354
355 /* Found a new place to put the load - move it here */
356 if (stopHere == true) {
357 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR
358 "HOIST STOP"));
359 break;
360 }
361 }
362
363 /*
364 * Reached the top - use headLIR as the dependent marker as all labels
365 * are barriers.
366 */
367 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
368 prevInstList[nextSlot++] = headLIR;
369 }
370
371 /*
372 * At least one independent instruction is found. Scan in the reversed
373 * direction to find a beneficial slot.
374 */
375 if (nextSlot >= 2) {
376 int firstSlot = nextSlot - 2;
377 int slot;
378 ArmLIR* depLIR = prevInstList[nextSlot-1];
379 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
380 if (!isPseudoOpcode(depLIR->opcode) &&
381 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
382 firstSlot -= LDLD_DISTANCE;
383 }
384 /*
385 * Make sure we check slot >= 0 since firstSlot may be negative
386 * when the loop is first entered.
387 */
388 for (slot = firstSlot; slot >= 0; slot--) {
389 ArmLIR* curLIR = prevInstList[slot];
390 ArmLIR* prevLIR = prevInstList[slot+1];
391
392 /* Check the highest instruction */
393 if (prevLIR->defMask == ENCODE_ALL) {
394 /*
395 * If the first instruction is a load, don't hoist anything
396 * above it since it is unlikely to be beneficial.
397 */
398 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
399 /*
400 * If the remaining number of slots is less than LD_LATENCY,
401 * insert the hoisted load here.
402 */
403 if (slot < LD_LATENCY) break;
404 }
405
406 /*
407 * NOTE: now prevLIR is guaranteed to be a non-pseudo
408 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
409 * safe).
410 *
411 * Try to find two instructions with load/use dependency until
412 * the remaining instructions are less than LD_LATENCY.
413 */
414 if (((curLIR->useMask & prevLIR->defMask) &&
415 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
416 (slot < LD_LATENCY)) {
417 break;
418 }
419 }
420
421 /* Found a slot to hoist to */
422 if (slot >= 0) {
423 ArmLIR* curLIR = prevInstList[slot];
424 ArmLIR* newLoadLIR = (ArmLIR* ) oatNew(sizeof(ArmLIR),
425 true);
426 *newLoadLIR = *thisLIR;
427 /*
428 * Insertion is guaranteed to succeed since checkLIR
429 * is never the first LIR on the list
430 */
431 oatInsertLIRBefore((LIR*) curLIR, (LIR*) newLoadLIR);
432 thisLIR->flags.isNop = true;
433 }
434 }
435 }
436}
437
438void oatApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
439 LIR* tailLIR)
440{
441 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
442 applyLoadStoreElimination(cUnit, (ArmLIR* ) headLIR,
443 (ArmLIR* ) tailLIR);
444 }
445 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
buzbee67bf8852011-08-17 17:51:35 -0700446 applyLoadHoisting(cUnit, (ArmLIR* ) headLIR, (ArmLIR* ) tailLIR);
447 }
448}