blob: 1e6e0d8fc5380d14329a87922fc2f50dc6974936 [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
buzbee1bc37c62012-11-20 13:35:41 -080017#include "../compiler_internals.h"
18
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080019namespace art {
20
buzbee67bf8852011-08-17 17:51:35 -070021#define DEBUG_OPT(X)
22
23/* Check RAW, WAR, and WAR dependency on the register operands */
24#define CHECK_REG_DEP(use, def, check) ((def & check->useMask) || \
25 ((use | def) & check->defMask))
26
27/* Scheduler heuristics */
28#define MAX_HOIST_DISTANCE 20
29#define LDLD_DISTANCE 4
30#define LD_LATENCY 2
31
buzbeeaad94382012-11-21 07:40:50 -080032static bool IsDalvikRegisterClobbered(LIR* lir1, LIR* lir2)
buzbee67bf8852011-08-17 17:51:35 -070033{
Bill Buzbeea114add2012-05-03 15:00:40 -070034 int reg1Lo = DECODE_ALIAS_INFO_REG(lir1->aliasInfo);
35 int reg1Hi = reg1Lo + DECODE_ALIAS_INFO_WIDE(lir1->aliasInfo);
36 int reg2Lo = DECODE_ALIAS_INFO_REG(lir2->aliasInfo);
37 int reg2Hi = reg2Lo + DECODE_ALIAS_INFO_WIDE(lir2->aliasInfo);
buzbee67bf8852011-08-17 17:51:35 -070038
Bill Buzbeea114add2012-05-03 15:00:40 -070039 return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo);
buzbee67bf8852011-08-17 17:51:35 -070040}
41
42/* Convert a more expensive instruction (ie load) into a move */
buzbeeaad94382012-11-21 07:40:50 -080043static void ConvertMemOpIntoMove(CompilationUnit* cUnit, LIR* origLIR, int dest, int src)
buzbee67bf8852011-08-17 17:51:35 -070044{
Bill Buzbeea114add2012-05-03 15:00:40 -070045 /* Insert a move to replace the load */
46 LIR* moveLIR;
buzbee52a77fc2012-11-20 19:50:46 -080047 moveLIR = OpRegCopyNoInsert( cUnit, dest, src);
Bill Buzbeea114add2012-05-03 15:00:40 -070048 /*
49 * Insert the converted instruction after the original since the
50 * optimization is scannng in the top-down order and the new instruction
51 * will need to be re-checked (eg the new dest clobbers the src used in
52 * thisLIR).
53 */
buzbee52a77fc2012-11-20 19:50:46 -080054 InsertLIRAfter(origLIR, moveLIR);
buzbee67bf8852011-08-17 17:51:35 -070055}
56
57/*
58 * Perform a pass of top-down walk, from the second-last instruction in the
59 * superblock, to eliminate redundant loads and stores.
60 *
61 * An earlier load can eliminate a later load iff
62 * 1) They are must-aliases
63 * 2) The native register is not clobbered in between
64 * 3) The memory location is not written to in between
65 *
66 * An earlier store can eliminate a later load iff
67 * 1) They are must-aliases
68 * 2) The native register is not clobbered in between
69 * 3) The memory location is not written to in between
70 *
71 * A later store can be eliminated by an earlier store iff
72 * 1) They are must-aliases
73 * 2) The memory location is not written to in between
74 */
buzbeeaad94382012-11-21 07:40:50 -080075static void ApplyLoadStoreElimination(CompilationUnit* cUnit, LIR* headLIR, LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -070076{
Bill Buzbeea114add2012-05-03 15:00:40 -070077 LIR* thisLIR;
buzbee67bf8852011-08-17 17:51:35 -070078
Bill Buzbeea114add2012-05-03 15:00:40 -070079 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -070080
Bill Buzbeea114add2012-05-03 15:00:40 -070081 for (thisLIR = PREV_LIR(tailLIR);
82 thisLIR != headLIR;
83 thisLIR = PREV_LIR(thisLIR)) {
84 int sinkDistance = 0;
buzbee67bf8852011-08-17 17:51:35 -070085
Bill Buzbeea114add2012-05-03 15:00:40 -070086 /* Skip non-interesting instructions */
87 if ((thisLIR->flags.isNop == true) ||
88 isPseudoOpcode(thisLIR->opcode) ||
buzbee52a77fc2012-11-20 19:50:46 -080089 (GetTargetInstFlags(thisLIR->opcode) & IS_BRANCH) ||
90 !(GetTargetInstFlags(thisLIR->opcode) & (IS_LOAD | IS_STORE))) {
Bill Buzbeea114add2012-05-03 15:00:40 -070091 continue;
92 }
buzbee67bf8852011-08-17 17:51:35 -070093
buzbeeb046e162012-10-30 15:48:42 -070094 int nativeRegId;
95 if (cUnit->instructionSet == kX86) {
96 // If x86, location differs depending on whether memory/reg operation.
buzbee52a77fc2012-11-20 19:50:46 -080097 nativeRegId = (GetTargetInstFlags(thisLIR->opcode) & IS_STORE) ? thisLIR->operands[2]
buzbeeb046e162012-10-30 15:48:42 -070098 : thisLIR->operands[0];
99 } else {
100 nativeRegId = thisLIR->operands[0];
101 }
buzbee52a77fc2012-11-20 19:50:46 -0800102 bool isThisLIRLoad = GetTargetInstFlags(thisLIR->opcode) & IS_LOAD;
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 LIR* checkLIR;
104 /* Use the mem mask to determine the rough memory location */
buzbeeeaf09bc2012-11-15 14:51:41 -0800105 uint64_t thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700106
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 /*
108 * Currently only eliminate redundant ld/st for constant and Dalvik
109 * register accesses.
110 */
111 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700112
buzbeeeaf09bc2012-11-15 14:51:41 -0800113 uint64_t stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
114 uint64_t stopUseRegMask;
buzbeeb046e162012-10-30 15:48:42 -0700115 if (cUnit->instructionSet == kX86) {
116 stopUseRegMask = (IS_BRANCH | thisLIR->useMask) & ~ENCODE_MEM;
117 } else {
118 /*
119 * Add pc to the resource mask to prevent this instruction
120 * from sinking past branch instructions. Also take out the memory
121 * region bits since stopMask is used to check data/control
122 * dependencies.
123 */
buzbee52a77fc2012-11-20 19:50:46 -0800124 stopUseRegMask = (GetPCUseDefEncoding() | thisLIR->useMask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700125 }
buzbee67bf8852011-08-17 17:51:35 -0700126
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 for (checkLIR = NEXT_LIR(thisLIR);
128 checkLIR != tailLIR;
129 checkLIR = NEXT_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700130
Bill Buzbeea114add2012-05-03 15:00:40 -0700131 /*
132 * Skip already dead instructions (whose dataflow information is
133 * outdated and misleading).
134 */
135 if (checkLIR->flags.isNop) continue;
136
buzbeeeaf09bc2012-11-15 14:51:41 -0800137 uint64_t checkMemMask = (checkLIR->useMask | checkLIR->defMask) & ENCODE_MEM;
138 uint64_t aliasCondition = thisMemMask & checkMemMask;
Bill Buzbeea114add2012-05-03 15:00:40 -0700139 bool stopHere = false;
140
141 /*
142 * Potential aliases seen - check the alias relations
143 */
144 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
buzbee52a77fc2012-11-20 19:50:46 -0800145 bool isCheckLIRLoad = GetTargetInstFlags(checkLIR->opcode) & IS_LOAD;
Bill Buzbeea114add2012-05-03 15:00:40 -0700146 if (aliasCondition == ENCODE_LITERAL) {
147 /*
148 * Should only see literal loads in the instruction
149 * stream.
150 */
buzbee52a77fc2012-11-20 19:50:46 -0800151 DCHECK(!(GetTargetInstFlags(checkLIR->opcode) & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 /* Same value && same register type */
153 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
buzbee52a77fc2012-11-20 19:50:46 -0800154 SameRegType(checkLIR->operands[0], nativeRegId)) {
buzbee67bf8852011-08-17 17:51:35 -0700155 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 * Different destination register - insert
157 * a move
buzbee67bf8852011-08-17 17:51:35 -0700158 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 if (checkLIR->operands[0] != nativeRegId) {
buzbee52a77fc2012-11-20 19:50:46 -0800160 ConvertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 nativeRegId);
162 }
163 checkLIR->flags.isNop = true;
164 }
165 } else if (aliasCondition == ENCODE_DALVIK_REG) {
166 /* Must alias */
167 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
168 /* Only optimize compatible registers */
buzbee52a77fc2012-11-20 19:50:46 -0800169 bool regCompatible = SameRegType(checkLIR->operands[0], nativeRegId);
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800180 ConvertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
Bill Buzbeea114add2012-05-03 15:00:40 -0700181 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800201 } else if (IsDalvikRegisterClobbered(thisLIR, checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700202 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 * 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.
buzbee67bf8852011-08-17 17:51:35 -0700207 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 stopHere = true;
209 }
buzbee67bf8852011-08-17 17:51:35 -0700210 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 /* 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, checkLIR);
228 }
229
230 if (stopHere == true) {
buzbeeb046e162012-10-30 15:48:42 -0700231 if (cUnit->instructionSet == kX86) {
232 // Prevent stores from being sunk between ops that generate ccodes and
233 // ops that use them.
buzbee52a77fc2012-11-20 19:50:46 -0800234 uint64_t flags = GetTargetInstFlags(checkLIR->opcode);
buzbeeb046e162012-10-30 15:48:42 -0700235 if (sinkDistance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
236 checkLIR = PREV_LIR(checkLIR);
237 sinkDistance--;
238 }
jeffhao573b4292012-07-30 16:37:41 -0700239 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR, "REG CLOBBERED"));
241 /* Only sink store instructions */
242 if (sinkDistance && !isThisLIRLoad) {
buzbee52a77fc2012-11-20 19:50:46 -0800243 LIR* newStoreLIR = static_cast<LIR*>(NewMem(cUnit, sizeof(LIR), true, kAllocLIR));
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 *newStoreLIR = *thisLIR;
245 /*
246 * Stop point found - insert *before* the checkLIR
247 * since the instruction list is scanned in the
248 * top-down order.
249 */
buzbee52a77fc2012-11-20 19:50:46 -0800250 InsertLIRBefore(checkLIR, newStoreLIR);
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 thisLIR->flags.isNop = true;
252 }
253 break;
254 } else if (!checkLIR->flags.isNop) {
255 sinkDistance++;
256 }
buzbee67bf8852011-08-17 17:51:35 -0700257 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 }
buzbee67bf8852011-08-17 17:51:35 -0700259}
260
261/*
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800265void ApplyLoadHoisting(CompilationUnit* cUnit, LIR* headLIR, LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700266{
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 LIR* 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 LIR* prevInstList[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700273
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 /* Empty block */
275 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -0700276
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 /* Start from the second instruction */
278 for (thisLIR = NEXT_LIR(headLIR);
279 thisLIR != tailLIR;
280 thisLIR = NEXT_LIR(thisLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700281
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 /* Skip non-interesting instructions */
283 if ((thisLIR->flags.isNop == true) ||
284 isPseudoOpcode(thisLIR->opcode) ||
buzbee52a77fc2012-11-20 19:50:46 -0800285 !(GetTargetInstFlags(thisLIR->opcode) & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 continue;
287 }
buzbee67bf8852011-08-17 17:51:35 -0700288
buzbeeeaf09bc2012-11-15 14:51:41 -0800289 uint64_t stopUseAllMask = thisLIR->useMask;
buzbee67bf8852011-08-17 17:51:35 -0700290
buzbeeb046e162012-10-30 15:48:42 -0700291 if (cUnit->instructionSet != kX86) {
292 /*
293 * Branches for null/range checks are marked with the true resource
294 * bits, and loads to Dalvik registers, constant pools, and non-alias
295 * locations are safe to be hoisted. So only mark the heap references
296 * conservatively here.
297 */
298 if (stopUseAllMask & ENCODE_HEAP_REF) {
buzbee52a77fc2012-11-20 19:50:46 -0800299 stopUseAllMask |= GetPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700300 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 }
buzbee67bf8852011-08-17 17:51:35 -0700302
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 /* Similar as above, but just check for pure register dependency */
buzbeeeaf09bc2012-11-15 14:51:41 -0800304 uint64_t stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
305 uint64_t stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700306
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 int nextSlot = 0;
308 bool stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700309
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 /* Try to hoist the load to a good spot */
311 for (checkLIR = PREV_LIR(thisLIR);
312 checkLIR != headLIR;
313 checkLIR = PREV_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700314
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 /*
316 * Skip already dead instructions (whose dataflow information is
317 * outdated and misleading).
318 */
319 if (checkLIR->flags.isNop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700320
buzbeeeaf09bc2012-11-15 14:51:41 -0800321 uint64_t checkMemMask = checkLIR->defMask & ENCODE_MEM;
322 uint64_t aliasCondition = stopUseAllMask & checkMemMask;
Bill Buzbeea114add2012-05-03 15:00:40 -0700323 stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700324
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 /* Potential WAR alias seen - check the exact relation */
326 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
327 /* We can fully disambiguate Dalvik references */
328 if (aliasCondition == ENCODE_DALVIK_REG) {
329 /* Must alias or partually overlap */
330 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
buzbee52a77fc2012-11-20 19:50:46 -0800331 IsDalvikRegisterClobbered(thisLIR, checkLIR)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 stopHere = true;
333 }
334 /* Conservatively treat all heap refs as may-alias */
335 } else {
336 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
337 stopHere = true;
buzbee67bf8852011-08-17 17:51:35 -0700338 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 /* Memory content may be updated. Stop looking now. */
340 if (stopHere) {
341 prevInstList[nextSlot++] = checkLIR;
342 break;
buzbee67bf8852011-08-17 17:51:35 -0700343 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 }
buzbee67bf8852011-08-17 17:51:35 -0700345
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 if (stopHere == false) {
347 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
348 checkLIR);
349 }
buzbee67bf8852011-08-17 17:51:35 -0700350
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 /*
352 * Store the dependent or non-pseudo/indepedent instruction to the
353 * list.
354 */
355 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
356 prevInstList[nextSlot++] = checkLIR;
357 if (nextSlot == MAX_HOIST_DISTANCE) break;
358 }
buzbee67bf8852011-08-17 17:51:35 -0700359
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 /* Found a new place to put the load - move it here */
361 if (stopHere == true) {
362 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR "HOIST STOP"));
363 break;
364 }
buzbee67bf8852011-08-17 17:51:35 -0700365 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700366
367 /*
368 * Reached the top - use headLIR as the dependent marker as all labels
369 * are barriers.
370 */
371 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
372 prevInstList[nextSlot++] = headLIR;
373 }
374
375 /*
376 * At least one independent instruction is found. Scan in the reversed
377 * direction to find a beneficial slot.
378 */
379 if (nextSlot >= 2) {
380 int firstSlot = nextSlot - 2;
381 int slot;
382 LIR* depLIR = prevInstList[nextSlot-1];
383 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
384 if (!isPseudoOpcode(depLIR->opcode) &&
buzbee52a77fc2012-11-20 19:50:46 -0800385 (GetTargetInstFlags(depLIR->opcode) & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700386 firstSlot -= LDLD_DISTANCE;
387 }
388 /*
389 * Make sure we check slot >= 0 since firstSlot may be negative
390 * when the loop is first entered.
391 */
392 for (slot = firstSlot; slot >= 0; slot--) {
393 LIR* curLIR = prevInstList[slot];
394 LIR* prevLIR = prevInstList[slot+1];
395
396 /* Check the highest instruction */
397 if (prevLIR->defMask == ENCODE_ALL) {
398 /*
399 * If the first instruction is a load, don't hoist anything
400 * above it since it is unlikely to be beneficial.
401 */
buzbee52a77fc2012-11-20 19:50:46 -0800402 if (GetTargetInstFlags(curLIR->opcode) & IS_LOAD) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 /*
404 * If the remaining number of slots is less than LD_LATENCY,
405 * insert the hoisted load here.
406 */
407 if (slot < LD_LATENCY) break;
408 }
409
buzbee8320f382012-09-11 16:29:42 -0700410 // Don't look across a barrier label
411 if ((prevLIR->opcode == kPseudoTargetLabel) ||
412 (prevLIR->opcode == kPseudoSafepointPC) ||
413 (prevLIR->opcode == kPseudoBarrier)) {
414 break;
415 }
416
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 * Try to find two instructions with load/use dependency until
419 * the remaining instructions are less than LD_LATENCY.
420 */
buzbee8320f382012-09-11 16:29:42 -0700421 bool prevIsLoad = isPseudoOpcode(prevLIR->opcode) ? false :
buzbee52a77fc2012-11-20 19:50:46 -0800422 (GetTargetInstFlags(prevLIR->opcode) & IS_LOAD);
buzbee8320f382012-09-11 16:29:42 -0700423 if (((curLIR->useMask & prevLIR->defMask) && prevIsLoad) || (slot < LD_LATENCY)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 break;
425 }
426 }
427
428 /* Found a slot to hoist to */
429 if (slot >= 0) {
430 LIR* curLIR = prevInstList[slot];
buzbee52a77fc2012-11-20 19:50:46 -0800431 LIR* newLoadLIR = static_cast<LIR*>(NewMem(cUnit, sizeof(LIR), true, kAllocLIR));
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 *newLoadLIR = *thisLIR;
433 /*
434 * Insertion is guaranteed to succeed since checkLIR
435 * is never the first LIR on the list
436 */
buzbee52a77fc2012-11-20 19:50:46 -0800437 InsertLIRBefore(curLIR, newLoadLIR);
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 thisLIR->flags.isNop = true;
439 }
440 }
441 }
buzbee67bf8852011-08-17 17:51:35 -0700442}
443
buzbee52a77fc2012-11-20 19:50:46 -0800444void ApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700446{
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
buzbee52a77fc2012-11-20 19:50:46 -0800448 ApplyLoadStoreElimination(cUnit, headLIR, tailLIR);
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 }
450 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
buzbee52a77fc2012-11-20 19:50:46 -0800451 ApplyLoadHoisting(cUnit, headLIR, tailLIR);
buzbeecbd6d442012-11-17 14:11:25 -0800452 }
453}
454
455/*
456 * Nop any unconditional branches that go to the next instruction.
457 * Note: new redundant branches may be inserted later, and we'll
458 * use a check in final instruction assembly to nop those out.
459 */
buzbee52a77fc2012-11-20 19:50:46 -0800460void RemoveRedundantBranches(CompilationUnit* cUnit)
buzbeecbd6d442012-11-17 14:11:25 -0800461{
462 LIR* thisLIR;
463
464 for (thisLIR = cUnit->firstLIRInsn; thisLIR != cUnit->lastLIRInsn; thisLIR = NEXT_LIR(thisLIR)) {
465
466 /* Branch to the next instruction */
buzbee52a77fc2012-11-20 19:50:46 -0800467 if (BranchUnconditional(thisLIR)) {
buzbeecbd6d442012-11-17 14:11:25 -0800468 LIR* nextLIR = thisLIR;
469
470 while (true) {
471 nextLIR = NEXT_LIR(nextLIR);
472
473 /*
474 * Is the branch target the next instruction?
475 */
476 if (nextLIR == thisLIR->target) {
477 thisLIR->flags.isNop = true;
478 break;
479 }
480
481 /*
482 * Found real useful stuff between the branch and the target.
483 * Need to explicitly check the lastLIRInsn here because it
484 * might be the last real instruction.
485 */
486 if (!isPseudoOpcode(nextLIR->opcode) ||
487 (nextLIR == cUnit->lastLIRInsn))
488 break;
489 }
490 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 }
buzbee67bf8852011-08-17 17:51:35 -0700492}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800493
494} // namespace art