blob: d3897e7563283290cefb2c78f767de6dfa3964d8 [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
buzbee52a77fc2012-11-20 19:50:46 -080032inline 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 */
buzbee52a77fc2012-11-20 19:50:46 -080043void ConvertMemOpIntoMove(CompilationUnit* cUnit, LIR* origLIR, int dest,
buzbee31a4a6f2012-02-28 15:36:15 -080044 int src)
buzbee67bf8852011-08-17 17:51:35 -070045{
Bill Buzbeea114add2012-05-03 15:00:40 -070046 /* Insert a move to replace the load */
47 LIR* moveLIR;
buzbee52a77fc2012-11-20 19:50:46 -080048 moveLIR = OpRegCopyNoInsert( cUnit, dest, src);
Bill Buzbeea114add2012-05-03 15:00:40 -070049 /*
50 * Insert the converted instruction after the original since the
51 * optimization is scannng in the top-down order and the new instruction
52 * will need to be re-checked (eg the new dest clobbers the src used in
53 * thisLIR).
54 */
buzbee52a77fc2012-11-20 19:50:46 -080055 InsertLIRAfter(origLIR, moveLIR);
buzbee67bf8852011-08-17 17:51:35 -070056}
57
58/*
59 * Perform a pass of top-down walk, from the second-last instruction in the
60 * superblock, to eliminate redundant loads and stores.
61 *
62 * An earlier load can eliminate a later load iff
63 * 1) They are must-aliases
64 * 2) The native register is not clobbered in between
65 * 3) The memory location is not written to in between
66 *
67 * An earlier store can eliminate a later load iff
68 * 1) They are must-aliases
69 * 2) The native register is not clobbered in between
70 * 3) The memory location is not written to in between
71 *
72 * A later store can be eliminated by an earlier store iff
73 * 1) They are must-aliases
74 * 2) The memory location is not written to in between
75 */
buzbee52a77fc2012-11-20 19:50:46 -080076void ApplyLoadStoreElimination(CompilationUnit* cUnit, LIR* headLIR,
buzbee31a4a6f2012-02-28 15:36:15 -080077 LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -070078{
Bill Buzbeea114add2012-05-03 15:00:40 -070079 LIR* thisLIR;
buzbee67bf8852011-08-17 17:51:35 -070080
Bill Buzbeea114add2012-05-03 15:00:40 -070081 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -070082
Bill Buzbeea114add2012-05-03 15:00:40 -070083 for (thisLIR = PREV_LIR(tailLIR);
84 thisLIR != headLIR;
85 thisLIR = PREV_LIR(thisLIR)) {
86 int sinkDistance = 0;
buzbee67bf8852011-08-17 17:51:35 -070087
Bill Buzbeea114add2012-05-03 15:00:40 -070088 /* Skip non-interesting instructions */
89 if ((thisLIR->flags.isNop == true) ||
90 isPseudoOpcode(thisLIR->opcode) ||
buzbee52a77fc2012-11-20 19:50:46 -080091 (GetTargetInstFlags(thisLIR->opcode) & IS_BRANCH) ||
92 !(GetTargetInstFlags(thisLIR->opcode) & (IS_LOAD | IS_STORE))) {
Bill Buzbeea114add2012-05-03 15:00:40 -070093 continue;
94 }
buzbee67bf8852011-08-17 17:51:35 -070095
buzbeeb046e162012-10-30 15:48:42 -070096 int nativeRegId;
97 if (cUnit->instructionSet == kX86) {
98 // If x86, location differs depending on whether memory/reg operation.
buzbee52a77fc2012-11-20 19:50:46 -080099 nativeRegId = (GetTargetInstFlags(thisLIR->opcode) & IS_STORE) ? thisLIR->operands[2]
buzbeeb046e162012-10-30 15:48:42 -0700100 : thisLIR->operands[0];
101 } else {
102 nativeRegId = thisLIR->operands[0];
103 }
buzbee52a77fc2012-11-20 19:50:46 -0800104 bool isThisLIRLoad = GetTargetInstFlags(thisLIR->opcode) & IS_LOAD;
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 LIR* checkLIR;
106 /* Use the mem mask to determine the rough memory location */
buzbeeeaf09bc2012-11-15 14:51:41 -0800107 uint64_t thisMemMask = (thisLIR->useMask | thisLIR->defMask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700108
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 /*
110 * Currently only eliminate redundant ld/st for constant and Dalvik
111 * register accesses.
112 */
113 if (!(thisMemMask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700114
buzbeeeaf09bc2012-11-15 14:51:41 -0800115 uint64_t stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
116 uint64_t stopUseRegMask;
buzbeeb046e162012-10-30 15:48:42 -0700117 if (cUnit->instructionSet == kX86) {
118 stopUseRegMask = (IS_BRANCH | thisLIR->useMask) & ~ENCODE_MEM;
119 } else {
120 /*
121 * Add pc to the resource mask to prevent this instruction
122 * from sinking past branch instructions. Also take out the memory
123 * region bits since stopMask is used to check data/control
124 * dependencies.
125 */
buzbee52a77fc2012-11-20 19:50:46 -0800126 stopUseRegMask = (GetPCUseDefEncoding() | thisLIR->useMask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700127 }
buzbee67bf8852011-08-17 17:51:35 -0700128
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 for (checkLIR = NEXT_LIR(thisLIR);
130 checkLIR != tailLIR;
131 checkLIR = NEXT_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700132
Bill Buzbeea114add2012-05-03 15:00:40 -0700133 /*
134 * Skip already dead instructions (whose dataflow information is
135 * outdated and misleading).
136 */
137 if (checkLIR->flags.isNop) continue;
138
buzbeeeaf09bc2012-11-15 14:51:41 -0800139 uint64_t checkMemMask = (checkLIR->useMask | checkLIR->defMask) & ENCODE_MEM;
140 uint64_t aliasCondition = thisMemMask & checkMemMask;
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 bool stopHere = false;
142
143 /*
144 * Potential aliases seen - check the alias relations
145 */
146 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
buzbee52a77fc2012-11-20 19:50:46 -0800147 bool isCheckLIRLoad = GetTargetInstFlags(checkLIR->opcode) & IS_LOAD;
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 if (aliasCondition == ENCODE_LITERAL) {
149 /*
150 * Should only see literal loads in the instruction
151 * stream.
152 */
buzbee52a77fc2012-11-20 19:50:46 -0800153 DCHECK(!(GetTargetInstFlags(checkLIR->opcode) & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 /* Same value && same register type */
155 if (checkLIR->aliasInfo == thisLIR->aliasInfo &&
buzbee52a77fc2012-11-20 19:50:46 -0800156 SameRegType(checkLIR->operands[0], nativeRegId)) {
buzbee67bf8852011-08-17 17:51:35 -0700157 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 * Different destination register - insert
159 * a move
buzbee67bf8852011-08-17 17:51:35 -0700160 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 if (checkLIR->operands[0] != nativeRegId) {
buzbee52a77fc2012-11-20 19:50:46 -0800162 ConvertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 nativeRegId);
164 }
165 checkLIR->flags.isNop = true;
166 }
167 } else if (aliasCondition == ENCODE_DALVIK_REG) {
168 /* Must alias */
169 if (checkLIR->aliasInfo == thisLIR->aliasInfo) {
170 /* Only optimize compatible registers */
buzbee52a77fc2012-11-20 19:50:46 -0800171 bool regCompatible = SameRegType(checkLIR->operands[0], nativeRegId);
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 if ((isThisLIRLoad && isCheckLIRLoad) ||
173 (!isThisLIRLoad && isCheckLIRLoad)) {
174 /* RAR or RAW */
175 if (regCompatible) {
176 /*
177 * Different destination register -
178 * insert a move
179 */
180 if (checkLIR->operands[0] !=
181 nativeRegId) {
buzbee52a77fc2012-11-20 19:50:46 -0800182 ConvertMemOpIntoMove(cUnit, checkLIR, checkLIR->operands[0],
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800203 } else if (IsDalvikRegisterClobbered(thisLIR, checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700204 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 * 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.
buzbee67bf8852011-08-17 17:51:35 -0700209 */
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 stopHere = true;
211 }
buzbee67bf8852011-08-17 17:51:35 -0700212 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 /* 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, checkLIR);
230 }
231
232 if (stopHere == true) {
buzbeeb046e162012-10-30 15:48:42 -0700233 if (cUnit->instructionSet == kX86) {
234 // Prevent stores from being sunk between ops that generate ccodes and
235 // ops that use them.
buzbee52a77fc2012-11-20 19:50:46 -0800236 uint64_t flags = GetTargetInstFlags(checkLIR->opcode);
buzbeeb046e162012-10-30 15:48:42 -0700237 if (sinkDistance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
238 checkLIR = PREV_LIR(checkLIR);
239 sinkDistance--;
240 }
jeffhao573b4292012-07-30 16:37:41 -0700241 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 DEBUG_OPT(dumpDependentInsnPair(thisLIR, checkLIR, "REG CLOBBERED"));
243 /* Only sink store instructions */
244 if (sinkDistance && !isThisLIRLoad) {
buzbee52a77fc2012-11-20 19:50:46 -0800245 LIR* newStoreLIR = static_cast<LIR*>(NewMem(cUnit, sizeof(LIR), true, kAllocLIR));
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 *newStoreLIR = *thisLIR;
247 /*
248 * Stop point found - insert *before* the checkLIR
249 * since the instruction list is scanned in the
250 * top-down order.
251 */
buzbee52a77fc2012-11-20 19:50:46 -0800252 InsertLIRBefore(checkLIR, newStoreLIR);
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 thisLIR->flags.isNop = true;
254 }
255 break;
256 } else if (!checkLIR->flags.isNop) {
257 sinkDistance++;
258 }
buzbee67bf8852011-08-17 17:51:35 -0700259 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 }
buzbee67bf8852011-08-17 17:51:35 -0700261}
262
263/*
264 * Perform a pass of bottom-up walk, from the second instruction in the
265 * superblock, to try to hoist loads to earlier slots.
266 */
buzbee52a77fc2012-11-20 19:50:46 -0800267void ApplyLoadHoisting(CompilationUnit* cUnit, LIR* headLIR, LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700268{
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 LIR* thisLIR, *checkLIR;
270 /*
271 * Store the list of independent instructions that can be hoisted past.
272 * Will decide the best place to insert later.
273 */
274 LIR* prevInstList[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700275
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 /* Empty block */
277 if (headLIR == tailLIR) return;
buzbee67bf8852011-08-17 17:51:35 -0700278
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 /* Start from the second instruction */
280 for (thisLIR = NEXT_LIR(headLIR);
281 thisLIR != tailLIR;
282 thisLIR = NEXT_LIR(thisLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700283
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 /* Skip non-interesting instructions */
285 if ((thisLIR->flags.isNop == true) ||
286 isPseudoOpcode(thisLIR->opcode) ||
buzbee52a77fc2012-11-20 19:50:46 -0800287 !(GetTargetInstFlags(thisLIR->opcode) & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 continue;
289 }
buzbee67bf8852011-08-17 17:51:35 -0700290
buzbeeeaf09bc2012-11-15 14:51:41 -0800291 uint64_t stopUseAllMask = thisLIR->useMask;
buzbee67bf8852011-08-17 17:51:35 -0700292
buzbeeb046e162012-10-30 15:48:42 -0700293 if (cUnit->instructionSet != kX86) {
294 /*
295 * Branches for null/range checks are marked with the true resource
296 * bits, and loads to Dalvik registers, constant pools, and non-alias
297 * locations are safe to be hoisted. So only mark the heap references
298 * conservatively here.
299 */
300 if (stopUseAllMask & ENCODE_HEAP_REF) {
buzbee52a77fc2012-11-20 19:50:46 -0800301 stopUseAllMask |= GetPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700302 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 }
buzbee67bf8852011-08-17 17:51:35 -0700304
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 /* Similar as above, but just check for pure register dependency */
buzbeeeaf09bc2012-11-15 14:51:41 -0800306 uint64_t stopUseRegMask = stopUseAllMask & ~ENCODE_MEM;
307 uint64_t stopDefRegMask = thisLIR->defMask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700308
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 int nextSlot = 0;
310 bool stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700311
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 /* Try to hoist the load to a good spot */
313 for (checkLIR = PREV_LIR(thisLIR);
314 checkLIR != headLIR;
315 checkLIR = PREV_LIR(checkLIR)) {
buzbee67bf8852011-08-17 17:51:35 -0700316
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 /*
318 * Skip already dead instructions (whose dataflow information is
319 * outdated and misleading).
320 */
321 if (checkLIR->flags.isNop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700322
buzbeeeaf09bc2012-11-15 14:51:41 -0800323 uint64_t checkMemMask = checkLIR->defMask & ENCODE_MEM;
324 uint64_t aliasCondition = stopUseAllMask & checkMemMask;
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 stopHere = false;
buzbee67bf8852011-08-17 17:51:35 -0700326
Bill Buzbeea114add2012-05-03 15:00:40 -0700327 /* Potential WAR alias seen - check the exact relation */
328 if (checkMemMask != ENCODE_MEM && aliasCondition != 0) {
329 /* We can fully disambiguate Dalvik references */
330 if (aliasCondition == ENCODE_DALVIK_REG) {
331 /* Must alias or partually overlap */
332 if ((checkLIR->aliasInfo == thisLIR->aliasInfo) ||
buzbee52a77fc2012-11-20 19:50:46 -0800333 IsDalvikRegisterClobbered(thisLIR, checkLIR)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 stopHere = true;
335 }
336 /* Conservatively treat all heap refs as may-alias */
337 } else {
338 DCHECK_EQ(aliasCondition, ENCODE_HEAP_REF);
339 stopHere = true;
buzbee67bf8852011-08-17 17:51:35 -0700340 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 /* Memory content may be updated. Stop looking now. */
342 if (stopHere) {
343 prevInstList[nextSlot++] = checkLIR;
344 break;
buzbee67bf8852011-08-17 17:51:35 -0700345 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 }
buzbee67bf8852011-08-17 17:51:35 -0700347
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 if (stopHere == false) {
349 stopHere = CHECK_REG_DEP(stopUseRegMask, stopDefRegMask,
350 checkLIR);
351 }
buzbee67bf8852011-08-17 17:51:35 -0700352
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 /*
354 * Store the dependent or non-pseudo/indepedent instruction to the
355 * list.
356 */
357 if (stopHere || !isPseudoOpcode(checkLIR->opcode)) {
358 prevInstList[nextSlot++] = checkLIR;
359 if (nextSlot == MAX_HOIST_DISTANCE) break;
360 }
buzbee67bf8852011-08-17 17:51:35 -0700361
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 /* Found a new place to put the load - move it here */
363 if (stopHere == true) {
364 DEBUG_OPT(dumpDependentInsnPair(checkLIR, thisLIR "HOIST STOP"));
365 break;
366 }
buzbee67bf8852011-08-17 17:51:35 -0700367 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700368
369 /*
370 * Reached the top - use headLIR as the dependent marker as all labels
371 * are barriers.
372 */
373 if (stopHere == false && nextSlot < MAX_HOIST_DISTANCE) {
374 prevInstList[nextSlot++] = headLIR;
375 }
376
377 /*
378 * At least one independent instruction is found. Scan in the reversed
379 * direction to find a beneficial slot.
380 */
381 if (nextSlot >= 2) {
382 int firstSlot = nextSlot - 2;
383 int slot;
384 LIR* depLIR = prevInstList[nextSlot-1];
385 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
386 if (!isPseudoOpcode(depLIR->opcode) &&
buzbee52a77fc2012-11-20 19:50:46 -0800387 (GetTargetInstFlags(depLIR->opcode) & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700388 firstSlot -= LDLD_DISTANCE;
389 }
390 /*
391 * Make sure we check slot >= 0 since firstSlot may be negative
392 * when the loop is first entered.
393 */
394 for (slot = firstSlot; slot >= 0; slot--) {
395 LIR* curLIR = prevInstList[slot];
396 LIR* prevLIR = prevInstList[slot+1];
397
398 /* Check the highest instruction */
399 if (prevLIR->defMask == ENCODE_ALL) {
400 /*
401 * If the first instruction is a load, don't hoist anything
402 * above it since it is unlikely to be beneficial.
403 */
buzbee52a77fc2012-11-20 19:50:46 -0800404 if (GetTargetInstFlags(curLIR->opcode) & IS_LOAD) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 /*
406 * If the remaining number of slots is less than LD_LATENCY,
407 * insert the hoisted load here.
408 */
409 if (slot < LD_LATENCY) break;
410 }
411
buzbee8320f382012-09-11 16:29:42 -0700412 // Don't look across a barrier label
413 if ((prevLIR->opcode == kPseudoTargetLabel) ||
414 (prevLIR->opcode == kPseudoSafepointPC) ||
415 (prevLIR->opcode == kPseudoBarrier)) {
416 break;
417 }
418
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 * Try to find two instructions with load/use dependency until
421 * the remaining instructions are less than LD_LATENCY.
422 */
buzbee8320f382012-09-11 16:29:42 -0700423 bool prevIsLoad = isPseudoOpcode(prevLIR->opcode) ? false :
buzbee52a77fc2012-11-20 19:50:46 -0800424 (GetTargetInstFlags(prevLIR->opcode) & IS_LOAD);
buzbee8320f382012-09-11 16:29:42 -0700425 if (((curLIR->useMask & prevLIR->defMask) && prevIsLoad) || (slot < LD_LATENCY)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 break;
427 }
428 }
429
430 /* Found a slot to hoist to */
431 if (slot >= 0) {
432 LIR* curLIR = prevInstList[slot];
buzbee52a77fc2012-11-20 19:50:46 -0800433 LIR* newLoadLIR = static_cast<LIR*>(NewMem(cUnit, sizeof(LIR), true, kAllocLIR));
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 *newLoadLIR = *thisLIR;
435 /*
436 * Insertion is guaranteed to succeed since checkLIR
437 * is never the first LIR on the list
438 */
buzbee52a77fc2012-11-20 19:50:46 -0800439 InsertLIRBefore(curLIR, newLoadLIR);
Bill Buzbeea114add2012-05-03 15:00:40 -0700440 thisLIR->flags.isNop = true;
441 }
442 }
443 }
buzbee67bf8852011-08-17 17:51:35 -0700444}
445
buzbee52a77fc2012-11-20 19:50:46 -0800446void ApplyLocalOptimizations(CompilationUnit* cUnit, LIR* headLIR,
Bill Buzbeea114add2012-05-03 15:00:40 -0700447 LIR* tailLIR)
buzbee67bf8852011-08-17 17:51:35 -0700448{
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 if (!(cUnit->disableOpt & (1 << kLoadStoreElimination))) {
buzbee52a77fc2012-11-20 19:50:46 -0800450 ApplyLoadStoreElimination(cUnit, headLIR, tailLIR);
Bill Buzbeea114add2012-05-03 15:00:40 -0700451 }
452 if (!(cUnit->disableOpt & (1 << kLoadHoisting))) {
buzbee52a77fc2012-11-20 19:50:46 -0800453 ApplyLoadHoisting(cUnit, headLIR, tailLIR);
buzbeecbd6d442012-11-17 14:11:25 -0800454 }
455}
456
457/*
458 * Nop any unconditional branches that go to the next instruction.
459 * Note: new redundant branches may be inserted later, and we'll
460 * use a check in final instruction assembly to nop those out.
461 */
buzbee52a77fc2012-11-20 19:50:46 -0800462void RemoveRedundantBranches(CompilationUnit* cUnit)
buzbeecbd6d442012-11-17 14:11:25 -0800463{
464 LIR* thisLIR;
465
466 for (thisLIR = cUnit->firstLIRInsn; thisLIR != cUnit->lastLIRInsn; thisLIR = NEXT_LIR(thisLIR)) {
467
468 /* Branch to the next instruction */
buzbee52a77fc2012-11-20 19:50:46 -0800469 if (BranchUnconditional(thisLIR)) {
buzbeecbd6d442012-11-17 14:11:25 -0800470 LIR* nextLIR = thisLIR;
471
472 while (true) {
473 nextLIR = NEXT_LIR(nextLIR);
474
475 /*
476 * Is the branch target the next instruction?
477 */
478 if (nextLIR == thisLIR->target) {
479 thisLIR->flags.isNop = true;
480 break;
481 }
482
483 /*
484 * Found real useful stuff between the branch and the target.
485 * Need to explicitly check the lastLIRInsn here because it
486 * might be the last real instruction.
487 */
488 if (!isPseudoOpcode(nextLIR->opcode) ||
489 (nextLIR == cUnit->lastLIRInsn))
490 break;
491 }
492 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 }
buzbee67bf8852011-08-17 17:51:35 -0700494}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800495
496} // namespace art