blob: eba701b1957f31d2d55d33be8e2b2fd33ea88c4c [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 =
239 (ArmLIR* ) oatNew(sizeof(ArmLIR), true);
240 *newStoreLIR = *thisLIR;
241 /*
242 * Stop point found - insert *before* the checkLIR
243 * since the instruction list is scanned in the
244 * top-down order.
245 */
246 oatInsertLIRBefore((LIR*) checkLIR,
247 (LIR*) newStoreLIR);
248 thisLIR->flags.isNop = true;
249 }
250 break;
251 } else if (!checkLIR->flags.isNop) {
252 sinkDistance++;
253 }
254 }
255 }
256}
257
258/*
259 * Perform a pass of bottom-up walk, from the second instruction in the
260 * superblock, to try to hoist loads to earlier slots.
261 */
buzbeeed3e9302011-09-23 17:34:19 -0700262STATIC void applyLoadHoisting(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -0700263 ArmLIR* headLIR,
264 ArmLIR* tailLIR)
265{
266 ArmLIR* thisLIR, *checkLIR;
267 /*
268 * Store the list of independent instructions that can be hoisted past.
269 * Will decide the best place to insert later.
270 */
271 ArmLIR* prevInstList[MAX_HOIST_DISTANCE];
272
273 /* Empty block */
274 if (headLIR == tailLIR) return;
275
276 /* Start from the second instruction */
277 for (thisLIR = NEXT_LIR(headLIR);
278 thisLIR != tailLIR;
279 thisLIR = NEXT_LIR(thisLIR)) {
280
281 /* Skip non-interesting instructions */
282 if ((thisLIR->flags.isNop == true) ||
283 isPseudoOpcode(thisLIR->opcode) ||
284 !(EncodingMap[thisLIR->opcode].flags & IS_LOAD)) {
285 continue;
286 }
287
288 u8 stopUseAllMask = thisLIR->useMask;
289
290 /*
291 * Branches for null/range checks are marked with the true resource
292 * bits, and loads to Dalvik registers, constant pools, and non-alias
293 * locations are safe to be hoisted. So only mark the heap references
294 * conservatively here.
295 */
296 if (stopUseAllMask & ENCODE_HEAP_REF) {
297 stopUseAllMask |= ENCODE_REG_PC;
298 }
299
300 /* Similar as above, but just check for pure register dependency */
301 u8 stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
302 u8 stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
303
304 int nextSlot = 0;
305 bool stopHere = false;
306
307 /* Try to hoist the load to a good spot */
308 for (checkLIR = PREV_LIR(thisLIR);
309 checkLIR != headLIR;
310 checkLIR = PREV_LIR(checkLIR)) {
311
312 /*
313 * Skip already dead instructions (whose dataflow information is
314 * outdated and misleading).
315 */
316 if (checkLIR->flags.isNop) continue;
317
318 u8 checkMemMask = checkLIR->defMask & ENCODE_MEM;
319 u8 aliasCondition = stopUseAllMask & checkMemMask;
320 stopHere = false;
321
322 /* Potential WAR alias seen - check the exact relation */
323 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
324 /* We can fully disambiguate Dalvik references */
325 if (aliasCondition == ENCODE_DALVIK_REG) {
326 /* Must alias or partually overlap */
327 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
328 isDalvikRegisterClobbered(thisLIR, checkLIR)) {
329 stopHere = true;
330 }
331 /* Conservatively treat all heap refs as may-alias */
332 } else {
buzbeeed3e9302011-09-23 17:34:19 -0700333 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
buzbee67bf8852011-08-17 17:51:35 -0700334 stopHere = true;
335 }
336 /* Memory content may be updated. Stop looking now. */
337 if (stopHere) {
338 prevInstList[nextSlot++] = checkLIR;
339 break;
340 }
341 }
342
343 if (stopHere == false) {
344 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
345 checkLIR);
346 }
347
348 /*
349 * Store the dependent or non-pseudo/indepedent instruction to the
350 * list.
351 */
352 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
353 prevInstList[nextSlot++] = checkLIR;
354 if (nextSlot == MAX_HOIST_DISTANCE) break;
355 }
356
357 /* Found a new place to put the load - move it here */
358 if (stopHere == true) {
359 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR
360 "HOIST STOP"));
361 break;
362 }
363 }
364
365 /*
366 * Reached the top - use headLIR as the dependent marker as all labels
367 * are barriers.
368 */
369 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
370 prevInstList[nextSlot++] = headLIR;
371 }
372
373 /*
374 * At least one independent instruction is found. Scan in the reversed
375 * direction to find a beneficial slot.
376 */
377 if (nextSlot >= 2) {
378 int firstSlot = nextSlot - 2;
379 int slot;
380 ArmLIR* depLIR = prevInstList[nextSlot-1];
381 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
382 if (!isPseudoOpcode(depLIR->opcode) &&
383 (EncodingMap[depLIR->opcode].flags & IS_LOAD)) {
384 firstSlot -= LDLD_DISTANCE;
385 }
386 /*
387 * Make sure we check slot >= 0 since firstSlot may be negative
388 * when the loop is first entered.
389 */
390 for (slot = firstSlot; slot >= 0; slot--) {
391 ArmLIR* curLIR = prevInstList[slot];
392 ArmLIR* prevLIR = prevInstList[slot+1];
393
394 /* Check the highest instruction */
395 if (prevLIR->defMask == ENCODE_ALL) {
396 /*
397 * If the first instruction is a load, don't hoist anything
398 * above it since it is unlikely to be beneficial.
399 */
400 if (EncodingMap[curLIR->opcode].flags & IS_LOAD) continue;
401 /*
402 * If the remaining number of slots is less than LD_LATENCY,
403 * insert the hoisted load here.
404 */
405 if (slot < LD_LATENCY) break;
406 }
407
408 /*
409 * NOTE: now prevLIR is guaranteed to be a non-pseudo
410 * instruction (ie accessing EncodingMap[prevLIR->opcode] is
411 * safe).
412 *
413 * Try to find two instructions with load/use dependency until
414 * the remaining instructions are less than LD_LATENCY.
415 */
416 if (((curLIR->useMask & prevLIR->defMask) &&
417 (EncodingMap[prevLIR->opcode].flags & IS_LOAD)) ||
418 (slot < LD_LATENCY)) {
419 break;
420 }
421 }
422
423 /* Found a slot to hoist to */
424 if (slot >= 0) {
425 ArmLIR* curLIR = prevInstList[slot];
426 ArmLIR* newLoadLIR = (ArmLIR* ) oatNew(sizeof(ArmLIR),
427 true);
428 *newLoadLIR = *thisLIR;
429 /*
430 * Insertion is guaranteed to succeed since checkLIR
431 * is never the first LIR on the list
432 */
433 oatInsertLIRBefore((LIR*) curLIR, (LIR*) newLoadLIR);
434 thisLIR->flags.isNop = true;
435 }
436 }
437 }
438}
439
440void oatApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
441 LIR* tailLIR)
442{
443 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
444 applyLoadStoreElimination(cUnit, (ArmLIR* ) headLIR,
445 (ArmLIR* ) tailLIR);
446 }
447 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
buzbee67bf8852011-08-17 17:51:35 -0700448 applyLoadHoisting(cUnit, (ArmLIR* ) headLIR, (ArmLIR* ) tailLIR);
449 }
450}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800451
452} // namespace art