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