Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -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 | |
| 17 | #include "dex/compiler_internals.h" |
| 18 | |
| 19 | namespace art { |
| 20 | |
| 21 | #define DEBUG_OPT(X) |
| 22 | |
| 23 | /* Check RAW, WAR, and RAW dependency on the register operands */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 24 | #define CHECK_REG_DEP(use, def, check) (def.Intersects(*check->u.m.use_mask)) || \ |
| 25 | (use.Union(def).Intersects(*check->u.m.def_mask)) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | |
| 27 | /* Scheduler heuristics */ |
| 28 | #define MAX_HOIST_DISTANCE 20 |
| 29 | #define LDLD_DISTANCE 4 |
| 30 | #define LD_LATENCY 2 |
| 31 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 32 | static bool IsDalvikRegisterClobbered(LIR* lir1, LIR* lir2) { |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 33 | int reg1Lo = DECODE_ALIAS_INFO_REG(lir1->flags.alias_info); |
| 34 | int reg1Hi = reg1Lo + DECODE_ALIAS_INFO_WIDE(lir1->flags.alias_info); |
| 35 | int reg2Lo = DECODE_ALIAS_INFO_REG(lir2->flags.alias_info); |
| 36 | int reg2Hi = reg2Lo + DECODE_ALIAS_INFO_WIDE(lir2->flags.alias_info); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 37 | |
| 38 | return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo); |
| 39 | } |
| 40 | |
| 41 | /* Convert a more expensive instruction (ie load) into a move */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 42 | void Mir2Lir::ConvertMemOpIntoMove(LIR* orig_lir, RegStorage dest, RegStorage src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 43 | /* Insert a move to replace the load */ |
| 44 | LIR* move_lir; |
| 45 | move_lir = OpRegCopyNoInsert(dest, src); |
| 46 | /* |
| 47 | * Insert the converted instruction after the original since the |
| 48 | * optimization is scannng in the top-down order and the new instruction |
| 49 | * will need to be re-checked (eg the new dest clobbers the src used in |
| 50 | * this_lir). |
| 51 | */ |
| 52 | InsertLIRAfter(orig_lir, move_lir); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Perform a pass of top-down walk, from the second-last instruction in the |
| 57 | * superblock, to eliminate redundant loads and stores. |
| 58 | * |
| 59 | * An earlier load can eliminate a later load iff |
| 60 | * 1) They are must-aliases |
| 61 | * 2) The native register is not clobbered in between |
| 62 | * 3) The memory location is not written to in between |
| 63 | * |
| 64 | * An earlier store can eliminate a later load iff |
| 65 | * 1) They are must-aliases |
| 66 | * 2) The native register is not clobbered in between |
| 67 | * 3) The memory location is not written to in between |
| 68 | * |
| 69 | * A later store can be eliminated by an earlier store iff |
| 70 | * 1) They are must-aliases |
| 71 | * 2) The memory location is not written to in between |
| 72 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 73 | void Mir2Lir::ApplyLoadStoreElimination(LIR* head_lir, LIR* tail_lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 74 | LIR* this_lir; |
| 75 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 76 | if (head_lir == tail_lir) { |
| 77 | return; |
| 78 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 79 | |
| 80 | for (this_lir = PREV_LIR(tail_lir); this_lir != head_lir; this_lir = PREV_LIR(this_lir)) { |
buzbee | 409fe94 | 2013-10-11 10:49:56 -0700 | [diff] [blame] | 81 | if (IsPseudoLirOp(this_lir->opcode)) { |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 82 | continue; |
| 83 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 84 | |
| 85 | int sink_distance = 0; |
| 86 | |
| 87 | uint64_t target_flags = GetTargetInstFlags(this_lir->opcode); |
| 88 | |
| 89 | /* Skip non-interesting instructions */ |
| 90 | if ((this_lir->flags.is_nop == true) || |
| 91 | (target_flags & IS_BRANCH) || |
| 92 | ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) || // Skip wide loads. |
| 93 | ((target_flags & (REG_USE0 | REG_USE1 | REG_USE2)) == |
| 94 | (REG_USE0 | REG_USE1 | REG_USE2)) || // Skip wide stores. |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 95 | // Skip instructions that are neither loads or stores. |
| 96 | !(target_flags & (IS_LOAD | IS_STORE)) || |
| 97 | // Skip instructions that do both load and store. |
| 98 | ((target_flags & (IS_STORE | IS_LOAD)) == (IS_STORE | IS_LOAD))) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 99 | continue; |
| 100 | } |
| 101 | |
| 102 | int native_reg_id; |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 103 | if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 104 | // If x86, location differs depending on whether memory/reg operation. |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 105 | native_reg_id = (target_flags & IS_STORE) ? this_lir->operands[2] : this_lir->operands[0]; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 106 | } else { |
| 107 | native_reg_id = this_lir->operands[0]; |
| 108 | } |
buzbee | 56c7178 | 2013-09-05 17:13:19 -0700 | [diff] [blame] | 109 | bool is_this_lir_load = target_flags & IS_LOAD; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 110 | LIR* check_lir; |
| 111 | /* Use the mem mask to determine the rough memory location */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 112 | ResourceMask this_mem_mask = kEncodeMem.Intersection( |
| 113 | this_lir->u.m.use_mask->Union(*this_lir->u.m.def_mask)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Currently only eliminate redundant ld/st for constant and Dalvik |
| 117 | * register accesses. |
| 118 | */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 119 | if (!this_mem_mask.Intersects(kEncodeLiteral.Union(kEncodeDalvikReg))) { |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 120 | continue; |
| 121 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 122 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 123 | ResourceMask stop_def_reg_mask = this_lir->u.m.def_mask->Without(kEncodeMem); |
| 124 | ResourceMask stop_use_reg_mask; |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 125 | if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) { |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 126 | // TODO: Stop the abuse of kIsBranch as a bit specification for ResourceMask. |
| 127 | stop_use_reg_mask = ResourceMask::Bit(kIsBranch).Union(*this_lir->u.m.use_mask).Without( |
| 128 | kEncodeMem); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 129 | } else { |
| 130 | /* |
| 131 | * Add pc to the resource mask to prevent this instruction |
| 132 | * from sinking past branch instructions. Also take out the memory |
| 133 | * region bits since stop_mask is used to check data/control |
| 134 | * dependencies. |
| 135 | */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 136 | stop_use_reg_mask = GetPCUseDefEncoding().Union(*this_lir->u.m.use_mask).Without(kEncodeMem); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 140 | /* |
| 141 | * Skip already dead instructions (whose dataflow information is |
| 142 | * outdated and misleading). |
| 143 | */ |
buzbee | 409fe94 | 2013-10-11 10:49:56 -0700 | [diff] [blame] | 144 | if (check_lir->flags.is_nop || IsPseudoLirOp(check_lir->opcode)) { |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 145 | continue; |
| 146 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 147 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 148 | ResourceMask check_mem_mask = kEncodeMem.Intersection( |
| 149 | check_lir->u.m.use_mask->Union(*check_lir->u.m.def_mask)); |
| 150 | ResourceMask alias_condition = this_mem_mask.Intersection(check_mem_mask); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 151 | bool stop_here = false; |
| 152 | |
| 153 | /* |
| 154 | * Potential aliases seen - check the alias relations |
| 155 | */ |
| 156 | uint64_t check_flags = GetTargetInstFlags(check_lir->opcode); |
| 157 | // TUNING: Support instructions with multiple register targets. |
| 158 | if ((check_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) { |
| 159 | stop_here = true; |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 160 | } else if (!check_mem_mask.Equals(kEncodeMem) && !alias_condition.Equals(kEncodeNone)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 161 | bool is_check_lir_load = check_flags & IS_LOAD; |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 162 | if (alias_condition.Equals(kEncodeLiteral)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 163 | /* |
| 164 | * Should only see literal loads in the instruction |
| 165 | * stream. |
| 166 | */ |
| 167 | DCHECK(!(check_flags & IS_STORE)); |
| 168 | /* Same value && same register type */ |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 169 | if (check_lir->flags.alias_info == this_lir->flags.alias_info && |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 170 | RegStorage::SameRegType(check_lir->operands[0], native_reg_id)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Different destination register - insert |
| 173 | * a move |
| 174 | */ |
| 175 | if (check_lir->operands[0] != native_reg_id) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 176 | // TODO: update for 64-bit regs. |
| 177 | ConvertMemOpIntoMove(check_lir, RegStorage::Solo32(check_lir->operands[0]), |
| 178 | RegStorage::Solo32(native_reg_id)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 179 | } |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 180 | NopLIR(check_lir); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 181 | } |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 182 | } else if (alias_condition.Equals(kEncodeDalvikReg)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 183 | /* Must alias */ |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 184 | if (check_lir->flags.alias_info == this_lir->flags.alias_info) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 185 | /* Only optimize compatible registers */ |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 186 | bool reg_compatible = RegStorage::SameRegType(check_lir->operands[0], native_reg_id); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 187 | if ((is_this_lir_load && is_check_lir_load) || |
| 188 | (!is_this_lir_load && is_check_lir_load)) { |
| 189 | /* RAR or RAW */ |
| 190 | if (reg_compatible) { |
| 191 | /* |
| 192 | * Different destination register - |
| 193 | * insert a move |
| 194 | */ |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 195 | if (check_lir->operands[0] != native_reg_id) { |
| 196 | // TODO: update for 64-bit regs. |
| 197 | ConvertMemOpIntoMove(check_lir, RegStorage::Solo32(check_lir->operands[0]), |
| 198 | RegStorage::Solo32(native_reg_id)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 199 | } |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 200 | NopLIR(check_lir); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 201 | } else { |
| 202 | /* |
| 203 | * Destinaions are of different types - |
| 204 | * something complicated going on so |
| 205 | * stop looking now. |
| 206 | */ |
| 207 | stop_here = true; |
| 208 | } |
| 209 | } else if (is_this_lir_load && !is_check_lir_load) { |
| 210 | /* WAR - register value is killed */ |
| 211 | stop_here = true; |
| 212 | } else if (!is_this_lir_load && !is_check_lir_load) { |
| 213 | /* WAW - nuke the earlier store */ |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 214 | NopLIR(this_lir); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 215 | stop_here = true; |
| 216 | } |
| 217 | /* Partial overlap */ |
| 218 | } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) { |
| 219 | /* |
| 220 | * It is actually ok to continue if check_lir |
| 221 | * is a read. But it is hard to make a test |
| 222 | * case for this so we just stop here to be |
| 223 | * conservative. |
| 224 | */ |
| 225 | stop_here = true; |
| 226 | } |
| 227 | } |
| 228 | /* Memory content may be updated. Stop looking now. */ |
| 229 | if (stop_here) { |
| 230 | break; |
| 231 | /* The check_lir has been transformed - check the next one */ |
| 232 | } else if (check_lir->flags.is_nop) { |
| 233 | continue; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /* |
| 239 | * this and check LIRs have no memory dependency. Now check if |
| 240 | * their register operands have any RAW, WAR, and WAW |
| 241 | * dependencies. If so, stop looking. |
| 242 | */ |
| 243 | if (stop_here == false) { |
| 244 | stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir); |
| 245 | } |
| 246 | |
| 247 | if (stop_here == true) { |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 248 | if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 249 | // Prevent stores from being sunk between ops that generate ccodes and |
| 250 | // ops that use them. |
| 251 | uint64_t flags = GetTargetInstFlags(check_lir->opcode); |
| 252 | if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) { |
| 253 | check_lir = PREV_LIR(check_lir); |
| 254 | sink_distance--; |
| 255 | } |
| 256 | } |
| 257 | DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED")); |
| 258 | /* Only sink store instructions */ |
| 259 | if (sink_distance && !is_this_lir_load) { |
| 260 | LIR* new_store_lir = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 261 | static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 262 | *new_store_lir = *this_lir; |
| 263 | /* |
| 264 | * Stop point found - insert *before* the check_lir |
| 265 | * since the instruction list is scanned in the |
| 266 | * top-down order. |
| 267 | */ |
| 268 | InsertLIRBefore(check_lir, new_store_lir); |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 269 | NopLIR(this_lir); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 270 | } |
| 271 | break; |
| 272 | } else if (!check_lir->flags.is_nop) { |
| 273 | sink_distance++; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Perform a pass of bottom-up walk, from the second instruction in the |
| 281 | * superblock, to try to hoist loads to earlier slots. |
| 282 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 283 | void Mir2Lir::ApplyLoadHoisting(LIR* head_lir, LIR* tail_lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 284 | LIR* this_lir, *check_lir; |
| 285 | /* |
| 286 | * Store the list of independent instructions that can be hoisted past. |
| 287 | * Will decide the best place to insert later. |
| 288 | */ |
| 289 | LIR* prev_inst_list[MAX_HOIST_DISTANCE]; |
| 290 | |
| 291 | /* Empty block */ |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 292 | if (head_lir == tail_lir) { |
| 293 | return; |
| 294 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 295 | |
| 296 | /* Start from the second instruction */ |
| 297 | for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) { |
buzbee | 409fe94 | 2013-10-11 10:49:56 -0700 | [diff] [blame] | 298 | if (IsPseudoLirOp(this_lir->opcode)) { |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 299 | continue; |
| 300 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 301 | |
| 302 | uint64_t target_flags = GetTargetInstFlags(this_lir->opcode); |
| 303 | /* Skip non-interesting instructions */ |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 304 | if (!(target_flags & IS_LOAD) || |
| 305 | (this_lir->flags.is_nop == true) || |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 306 | ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) || |
| 307 | ((target_flags & (IS_STORE | IS_LOAD)) == (IS_STORE | IS_LOAD))) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 308 | continue; |
| 309 | } |
| 310 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 311 | ResourceMask stop_use_all_mask = *this_lir->u.m.use_mask; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 312 | |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 313 | if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 314 | /* |
| 315 | * Branches for null/range checks are marked with the true resource |
| 316 | * bits, and loads to Dalvik registers, constant pools, and non-alias |
| 317 | * locations are safe to be hoisted. So only mark the heap references |
| 318 | * conservatively here. |
| 319 | */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 320 | if (stop_use_all_mask.HasBit(ResourceMask::kHeapRef)) { |
| 321 | stop_use_all_mask.SetBits(GetPCUseDefEncoding()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | /* Similar as above, but just check for pure register dependency */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 326 | ResourceMask stop_use_reg_mask = stop_use_all_mask.Without(kEncodeMem); |
| 327 | ResourceMask stop_def_reg_mask = this_lir->u.m.def_mask->Without(kEncodeMem); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 328 | |
| 329 | int next_slot = 0; |
| 330 | bool stop_here = false; |
| 331 | |
| 332 | /* Try to hoist the load to a good spot */ |
| 333 | for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 334 | /* |
| 335 | * Skip already dead instructions (whose dataflow information is |
| 336 | * outdated and misleading). |
| 337 | */ |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 338 | if (check_lir->flags.is_nop) { |
| 339 | continue; |
| 340 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 341 | |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 342 | ResourceMask check_mem_mask = check_lir->u.m.def_mask->Intersection(kEncodeMem); |
| 343 | ResourceMask alias_condition = stop_use_all_mask.Intersection(check_mem_mask); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 344 | stop_here = false; |
| 345 | |
| 346 | /* Potential WAR alias seen - check the exact relation */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 347 | if (!check_mem_mask.Equals(kEncodeMem) && !alias_condition.Equals(kEncodeNone)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 348 | /* We can fully disambiguate Dalvik references */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 349 | if (alias_condition.Equals(kEncodeDalvikReg)) { |
| 350 | /* Must alias or partially overlap */ |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 351 | if ((check_lir->flags.alias_info == this_lir->flags.alias_info) || |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 352 | IsDalvikRegisterClobbered(this_lir, check_lir)) { |
| 353 | stop_here = true; |
| 354 | } |
| 355 | /* Conservatively treat all heap refs as may-alias */ |
| 356 | } else { |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 357 | DCHECK(alias_condition.Equals(kEncodeHeapRef)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 358 | stop_here = true; |
| 359 | } |
| 360 | /* Memory content may be updated. Stop looking now. */ |
| 361 | if (stop_here) { |
| 362 | prev_inst_list[next_slot++] = check_lir; |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (stop_here == false) { |
| 368 | stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, |
| 369 | check_lir); |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Store the dependent or non-pseudo/indepedent instruction to the |
| 374 | * list. |
| 375 | */ |
buzbee | 409fe94 | 2013-10-11 10:49:56 -0700 | [diff] [blame] | 376 | if (stop_here || !IsPseudoLirOp(check_lir->opcode)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 377 | prev_inst_list[next_slot++] = check_lir; |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 378 | if (next_slot == MAX_HOIST_DISTANCE) { |
| 379 | break; |
| 380 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* Found a new place to put the load - move it here */ |
| 384 | if (stop_here == true) { |
| 385 | DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP")); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Reached the top - use head_lir as the dependent marker as all labels |
| 392 | * are barriers. |
| 393 | */ |
| 394 | if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) { |
| 395 | prev_inst_list[next_slot++] = head_lir; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * At least one independent instruction is found. Scan in the reversed |
| 400 | * direction to find a beneficial slot. |
| 401 | */ |
| 402 | if (next_slot >= 2) { |
| 403 | int first_slot = next_slot - 2; |
| 404 | int slot; |
| 405 | LIR* dep_lir = prev_inst_list[next_slot-1]; |
| 406 | /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */ |
buzbee | 409fe94 | 2013-10-11 10:49:56 -0700 | [diff] [blame] | 407 | if (!IsPseudoLirOp(dep_lir->opcode) && |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 408 | (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) { |
| 409 | first_slot -= LDLD_DISTANCE; |
| 410 | } |
| 411 | /* |
| 412 | * Make sure we check slot >= 0 since first_slot may be negative |
| 413 | * when the loop is first entered. |
| 414 | */ |
| 415 | for (slot = first_slot; slot >= 0; slot--) { |
| 416 | LIR* cur_lir = prev_inst_list[slot]; |
| 417 | LIR* prev_lir = prev_inst_list[slot+1]; |
| 418 | |
| 419 | /* Check the highest instruction */ |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 420 | if (prev_lir->u.m.def_mask->Equals(kEncodeAll)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 421 | /* |
| 422 | * If the first instruction is a load, don't hoist anything |
| 423 | * above it since it is unlikely to be beneficial. |
| 424 | */ |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 425 | if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) { |
| 426 | continue; |
| 427 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 428 | /* |
| 429 | * If the remaining number of slots is less than LD_LATENCY, |
| 430 | * insert the hoisted load here. |
| 431 | */ |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 432 | if (slot < LD_LATENCY) { |
| 433 | break; |
| 434 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // Don't look across a barrier label |
| 438 | if ((prev_lir->opcode == kPseudoTargetLabel) || |
| 439 | (prev_lir->opcode == kPseudoSafepointPC) || |
| 440 | (prev_lir->opcode == kPseudoBarrier)) { |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Try to find two instructions with load/use dependency until |
| 446 | * the remaining instructions are less than LD_LATENCY. |
| 447 | */ |
buzbee | 409fe94 | 2013-10-11 10:49:56 -0700 | [diff] [blame] | 448 | bool prev_is_load = IsPseudoLirOp(prev_lir->opcode) ? false : |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 449 | (GetTargetInstFlags(prev_lir->opcode) & IS_LOAD); |
Vladimir Marko | 8dea81c | 2014-06-06 14:50:36 +0100 | [diff] [blame^] | 450 | if ((prev_is_load && (cur_lir->u.m.use_mask->Intersects(*prev_lir->u.m.def_mask))) || |
| 451 | (slot < LD_LATENCY)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* Found a slot to hoist to */ |
| 457 | if (slot >= 0) { |
| 458 | LIR* cur_lir = prev_inst_list[slot]; |
| 459 | LIR* new_load_lir = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 460 | static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 461 | *new_load_lir = *this_lir; |
| 462 | /* |
| 463 | * Insertion is guaranteed to succeed since check_lir |
| 464 | * is never the first LIR on the list |
| 465 | */ |
| 466 | InsertLIRBefore(cur_lir, new_load_lir); |
buzbee | 252254b | 2013-09-08 16:20:53 -0700 | [diff] [blame] | 467 | NopLIR(this_lir); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 473 | void Mir2Lir::ApplyLocalOptimizations(LIR* head_lir, LIR* tail_lir) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 474 | if (!(cu_->disable_opt & (1 << kLoadStoreElimination))) { |
| 475 | ApplyLoadStoreElimination(head_lir, tail_lir); |
| 476 | } |
| 477 | if (!(cu_->disable_opt & (1 << kLoadHoisting))) { |
| 478 | ApplyLoadHoisting(head_lir, tail_lir); |
| 479 | } |
| 480 | } |
| 481 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 482 | } // namespace art |