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