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