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 | |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 17 | #include "../compiler_internals.h" |
| 18 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 19 | namespace art { |
| 20 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 21 | #define DEBUG_OPT(X) |
| 22 | |
| 23 | /* Check RAW, WAR, and WAR dependency on the register operands */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 24 | #define CHECK_REG_DEP(use, def, check) ((def & check->use_mask) || \ |
| 25 | ((use | def) & check->def_mask)) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 26 | |
| 27 | /* Scheduler heuristics */ |
| 28 | #define MAX_HOIST_DISTANCE 20 |
| 29 | #define LDLD_DISTANCE 4 |
| 30 | #define LD_LATENCY 2 |
| 31 | |
buzbee | aad9438 | 2012-11-21 07:40:50 -0800 | [diff] [blame] | 32 | static bool IsDalvikRegisterClobbered(LIR* lir1, LIR* lir2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 33 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 34 | int reg1Lo = DECODE_ALIAS_INFO_REG(lir1->alias_info); |
| 35 | int reg1Hi = reg1Lo + DECODE_ALIAS_INFO_WIDE(lir1->alias_info); |
| 36 | int reg2Lo = DECODE_ALIAS_INFO_REG(lir2->alias_info); |
| 37 | int reg2Hi = reg2Lo + DECODE_ALIAS_INFO_WIDE(lir2->alias_info); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 38 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 39 | return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* Convert a more expensive instruction (ie load) into a move */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 43 | static void ConvertMemOpIntoMove(CompilationUnit* cu, LIR* orig_lir, int dest, int src) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 44 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 45 | /* Insert a move to replace the load */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 46 | LIR* move_lir; |
| 47 | move_lir = OpRegCopyNoInsert( cu, dest, src); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 48 | /* |
| 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 |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 52 | * this_lir). |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 53 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 54 | InsertLIRAfter(orig_lir, move_lir); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 55 | } |
| 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 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 75 | static void ApplyLoadStoreElimination(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 76 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 77 | LIR* this_lir; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 78 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 79 | if (head_lir == tail_lir) return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 80 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 81 | for (this_lir = PREV_LIR(tail_lir); |
| 82 | this_lir != head_lir; |
| 83 | this_lir = PREV_LIR(this_lir)) { |
| 84 | int sink_distance = 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 */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 87 | if ((this_lir->flags.is_nop == true) || |
| 88 | is_pseudo_opcode(this_lir->opcode) || |
| 89 | (GetTargetInstFlags(this_lir->opcode) & IS_BRANCH) || |
| 90 | !(GetTargetInstFlags(this_lir->opcode) & (IS_LOAD | IS_STORE))) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 91 | continue; |
| 92 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 93 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 94 | int native_reg_id; |
| 95 | if (cu->instruction_set == kX86) { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 96 | // If x86, location differs depending on whether memory/reg operation. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 97 | native_reg_id = (GetTargetInstFlags(this_lir->opcode) & IS_STORE) ? this_lir->operands[2] |
| 98 | : this_lir->operands[0]; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 99 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 100 | native_reg_id = this_lir->operands[0]; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 101 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 102 | bool is_this_lir_load = GetTargetInstFlags(this_lir->opcode) & IS_LOAD; |
| 103 | LIR* check_lir; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 104 | /* Use the mem mask to determine the rough memory location */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 105 | uint64_t this_mem_mask = (this_lir->use_mask | this_lir->def_mask) & ENCODE_MEM; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 106 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 107 | /* |
| 108 | * Currently only eliminate redundant ld/st for constant and Dalvik |
| 109 | * register accesses. |
| 110 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 111 | if (!(this_mem_mask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 112 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 113 | uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM; |
| 114 | uint64_t stop_use_reg_mask; |
| 115 | if (cu->instruction_set == kX86) { |
| 116 | stop_use_reg_mask = (IS_BRANCH | this_lir->use_mask) & ~ENCODE_MEM; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 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 |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 121 | * region bits since stop_mask is used to check data/control |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 122 | * dependencies. |
| 123 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 124 | stop_use_reg_mask = (GetPCUseDefEncoding() | this_lir->use_mask) & ~ENCODE_MEM; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 125 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 126 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 127 | for (check_lir = NEXT_LIR(this_lir); |
| 128 | check_lir != tail_lir; |
| 129 | check_lir = NEXT_LIR(check_lir)) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 130 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Skip already dead instructions (whose dataflow information is |
| 133 | * outdated and misleading). |
| 134 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 135 | if (check_lir->flags.is_nop) continue; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 136 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 137 | uint64_t check_mem_mask = (check_lir->use_mask | check_lir->def_mask) & ENCODE_MEM; |
| 138 | uint64_t alias_condition = this_mem_mask & check_mem_mask; |
| 139 | bool stop_here = false; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * Potential aliases seen - check the alias relations |
| 143 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 144 | if (check_mem_mask != ENCODE_MEM && alias_condition != 0) { |
| 145 | bool is_check_lir_load = GetTargetInstFlags(check_lir->opcode) & IS_LOAD; |
| 146 | if (alias_condition == ENCODE_LITERAL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 147 | /* |
| 148 | * Should only see literal loads in the instruction |
| 149 | * stream. |
| 150 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 151 | DCHECK(!(GetTargetInstFlags(check_lir->opcode) & IS_STORE)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 152 | /* Same value && same register type */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 153 | if (check_lir->alias_info == this_lir->alias_info && |
| 154 | SameRegType(check_lir->operands[0], native_reg_id)) { |
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 | * Different destination register - insert |
| 157 | * a move |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 158 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 159 | if (check_lir->operands[0] != native_reg_id) { |
| 160 | ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0], |
| 161 | native_reg_id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 162 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 163 | check_lir->flags.is_nop = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 164 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 165 | } else if (alias_condition == ENCODE_DALVIK_REG) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 166 | /* Must alias */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 167 | if (check_lir->alias_info == this_lir->alias_info) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 168 | /* Only optimize compatible registers */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 169 | bool reg_compatible = SameRegType(check_lir->operands[0], native_reg_id); |
| 170 | if ((is_this_lir_load && is_check_lir_load) || |
| 171 | (!is_this_lir_load && is_check_lir_load)) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 172 | /* RAR or RAW */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 173 | if (reg_compatible) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 174 | /* |
| 175 | * Different destination register - |
| 176 | * insert a move |
| 177 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 178 | if (check_lir->operands[0] != |
| 179 | native_reg_id) { |
| 180 | ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0], |
| 181 | native_reg_id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 182 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 183 | check_lir->flags.is_nop = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 184 | } else { |
| 185 | /* |
| 186 | * Destinaions are of different types - |
| 187 | * something complicated going on so |
| 188 | * stop looking now. |
| 189 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 190 | stop_here = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 191 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 192 | } else if (is_this_lir_load && !is_check_lir_load) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 193 | /* WAR - register value is killed */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 194 | stop_here = true; |
| 195 | } else if (!is_this_lir_load && !is_check_lir_load) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 196 | /* WAW - nuke the earlier store */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 197 | this_lir->flags.is_nop = true; |
| 198 | stop_here = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 199 | } |
| 200 | /* Partial overlap */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 201 | } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 202 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 203 | * It is actually ok to continue if check_lir |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 207 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 208 | stop_here = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 209 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 210 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 211 | /* Memory content may be updated. Stop looking now. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 212 | if (stop_here) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 213 | break; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 214 | /* The check_lir has been transformed - check the next one */ |
| 215 | } else if (check_lir->flags.is_nop) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 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 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 226 | if (stop_here == false) { |
| 227 | stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 228 | } |
| 229 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 230 | if (stop_here == true) { |
| 231 | if (cu->instruction_set == kX86) { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 232 | // Prevent stores from being sunk between ops that generate ccodes and |
| 233 | // ops that use them. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 234 | uint64_t flags = GetTargetInstFlags(check_lir->opcode); |
| 235 | if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) { |
| 236 | check_lir = PREV_LIR(check_lir); |
| 237 | sink_distance--; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 238 | } |
jeffhao | 573b429 | 2012-07-30 16:37:41 -0700 | [diff] [blame] | 239 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 240 | DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED")); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 241 | /* Only sink store instructions */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 242 | if (sink_distance && !is_this_lir_load) { |
| 243 | LIR* new_store_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR)); |
| 244 | *new_store_lir = *this_lir; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 245 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 246 | * Stop point found - insert *before* the check_lir |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 247 | * since the instruction list is scanned in the |
| 248 | * top-down order. |
| 249 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 250 | InsertLIRBefore(check_lir, new_store_lir); |
| 251 | this_lir->flags.is_nop = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 252 | } |
| 253 | break; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 254 | } else if (!check_lir->flags.is_nop) { |
| 255 | sink_distance++; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 256 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 257 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 258 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 259 | } |
| 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 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 265 | void ApplyLoadHoisting(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 266 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 267 | LIR* this_lir, *check_lir; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 268 | /* |
| 269 | * Store the list of independent instructions that can be hoisted past. |
| 270 | * Will decide the best place to insert later. |
| 271 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 272 | LIR* prev_inst_list[MAX_HOIST_DISTANCE]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 273 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 274 | /* Empty block */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 275 | if (head_lir == tail_lir) return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 276 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 277 | /* Start from the second instruction */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 278 | for (this_lir = NEXT_LIR(head_lir); |
| 279 | this_lir != tail_lir; |
| 280 | this_lir = NEXT_LIR(this_lir)) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 281 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 282 | /* Skip non-interesting instructions */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 283 | if ((this_lir->flags.is_nop == true) || |
| 284 | is_pseudo_opcode(this_lir->opcode) || |
| 285 | !(GetTargetInstFlags(this_lir->opcode) & IS_LOAD)) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 286 | continue; |
| 287 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 288 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 289 | uint64_t stop_use_all_mask = this_lir->use_mask; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 290 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 291 | if (cu->instruction_set != kX86) { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 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 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 298 | if (stop_use_all_mask & ENCODE_HEAP_REF) { |
| 299 | stop_use_all_mask |= GetPCUseDefEncoding(); |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 300 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 301 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 302 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 303 | /* Similar as above, but just check for pure register dependency */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 304 | uint64_t stop_use_reg_mask = stop_use_all_mask & ~ENCODE_MEM; |
| 305 | uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 306 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 307 | int next_slot = 0; |
| 308 | bool stop_here = false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 309 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 310 | /* Try to hoist the load to a good spot */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 311 | for (check_lir = PREV_LIR(this_lir); |
| 312 | check_lir != head_lir; |
| 313 | check_lir = PREV_LIR(check_lir)) { |
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 | /* |
| 316 | * Skip already dead instructions (whose dataflow information is |
| 317 | * outdated and misleading). |
| 318 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 319 | if (check_lir->flags.is_nop) continue; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 320 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 321 | uint64_t check_mem_mask = check_lir->def_mask & ENCODE_MEM; |
| 322 | uint64_t alias_condition = stop_use_all_mask & check_mem_mask; |
| 323 | stop_here = false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 324 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 325 | /* Potential WAR alias seen - check the exact relation */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 326 | if (check_mem_mask != ENCODE_MEM && alias_condition != 0) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 327 | /* We can fully disambiguate Dalvik references */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 328 | if (alias_condition == ENCODE_DALVIK_REG) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 329 | /* Must alias or partually overlap */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 330 | if ((check_lir->alias_info == this_lir->alias_info) || |
| 331 | IsDalvikRegisterClobbered(this_lir, check_lir)) { |
| 332 | stop_here = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 333 | } |
| 334 | /* Conservatively treat all heap refs as may-alias */ |
| 335 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 336 | DCHECK_EQ(alias_condition, ENCODE_HEAP_REF); |
| 337 | stop_here = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 338 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 339 | /* Memory content may be updated. Stop looking now. */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 340 | if (stop_here) { |
| 341 | prev_inst_list[next_slot++] = check_lir; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 342 | break; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 343 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 344 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 345 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 346 | if (stop_here == false) { |
| 347 | stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, |
| 348 | check_lir); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 349 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 350 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 351 | /* |
| 352 | * Store the dependent or non-pseudo/indepedent instruction to the |
| 353 | * list. |
| 354 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 355 | if (stop_here || !is_pseudo_opcode(check_lir->opcode)) { |
| 356 | prev_inst_list[next_slot++] = check_lir; |
| 357 | if (next_slot == MAX_HOIST_DISTANCE) break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 358 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 359 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 360 | /* Found a new place to put the load - move it here */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 361 | if (stop_here == true) { |
| 362 | DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP")); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 363 | break; |
| 364 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 365 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 366 | |
| 367 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 368 | * Reached the top - use head_lir as the dependent marker as all labels |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 369 | * are barriers. |
| 370 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 371 | if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) { |
| 372 | prev_inst_list[next_slot++] = head_lir; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
| 376 | * At least one independent instruction is found. Scan in the reversed |
| 377 | * direction to find a beneficial slot. |
| 378 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 379 | if (next_slot >= 2) { |
| 380 | int first_slot = next_slot - 2; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 381 | int slot; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 382 | LIR* dep_lir = prev_inst_list[next_slot-1]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 383 | /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 384 | if (!is_pseudo_opcode(dep_lir->opcode) && |
| 385 | (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) { |
| 386 | first_slot -= LDLD_DISTANCE; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 387 | } |
| 388 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 389 | * Make sure we check slot >= 0 since first_slot may be negative |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 390 | * when the loop is first entered. |
| 391 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 392 | for (slot = first_slot; slot >= 0; slot--) { |
| 393 | LIR* cur_lir = prev_inst_list[slot]; |
| 394 | LIR* prev_lir = prev_inst_list[slot+1]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 395 | |
| 396 | /* Check the highest instruction */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 397 | if (prev_lir->def_mask == ENCODE_ALL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 398 | /* |
| 399 | * If the first instruction is a load, don't hoist anything |
| 400 | * above it since it is unlikely to be beneficial. |
| 401 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 402 | if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) continue; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 403 | /* |
| 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 | |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 410 | // Don't look across a barrier label |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 411 | if ((prev_lir->opcode == kPseudoTargetLabel) || |
| 412 | (prev_lir->opcode == kPseudoSafepointPC) || |
| 413 | (prev_lir->opcode == kPseudoBarrier)) { |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 417 | /* |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 418 | * Try to find two instructions with load/use dependency until |
| 419 | * the remaining instructions are less than LD_LATENCY. |
| 420 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 421 | bool prev_is_load = is_pseudo_opcode(prev_lir->opcode) ? false : |
| 422 | (GetTargetInstFlags(prev_lir->opcode) & IS_LOAD); |
| 423 | if (((cur_lir->use_mask & prev_lir->def_mask) && prev_is_load) || (slot < LD_LATENCY)) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /* Found a slot to hoist to */ |
| 429 | if (slot >= 0) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 430 | LIR* cur_lir = prev_inst_list[slot]; |
| 431 | LIR* new_load_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR)); |
| 432 | *new_load_lir = *this_lir; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 433 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 434 | * Insertion is guaranteed to succeed since check_lir |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 435 | * is never the first LIR on the list |
| 436 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 437 | InsertLIRBefore(cur_lir, new_load_lir); |
| 438 | this_lir->flags.is_nop = true; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 442 | } |
| 443 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 444 | void ApplyLocalOptimizations(CompilationUnit* cu, LIR* head_lir, |
| 445 | LIR* tail_lir) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 446 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 447 | if (!(cu->disable_opt & (1 << kLoadStoreElimination))) { |
| 448 | ApplyLoadStoreElimination(cu, head_lir, tail_lir); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 449 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 450 | if (!(cu->disable_opt & (1 << kLoadHoisting))) { |
| 451 | ApplyLoadHoisting(cu, head_lir, tail_lir); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 452 | } |
| 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 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 460 | void RemoveRedundantBranches(CompilationUnit* cu) |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 461 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 462 | LIR* this_lir; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 463 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 464 | for (this_lir = cu->first_lir_insn; this_lir != cu->last_lir_insn; this_lir = NEXT_LIR(this_lir)) { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 465 | |
| 466 | /* Branch to the next instruction */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 467 | if (BranchUnconditional(this_lir)) { |
| 468 | LIR* next_lir = this_lir; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 469 | |
| 470 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 471 | next_lir = NEXT_LIR(next_lir); |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 472 | |
| 473 | /* |
| 474 | * Is the branch target the next instruction? |
| 475 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 476 | if (next_lir == this_lir->target) { |
| 477 | this_lir->flags.is_nop = true; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 478 | break; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Found real useful stuff between the branch and the target. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 483 | * Need to explicitly check the last_lir_insn here because it |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 484 | * might be the last real instruction. |
| 485 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 486 | if (!is_pseudo_opcode(next_lir->opcode) || |
| 487 | (next_lir == cu->last_lir_insn)) |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 488 | break; |
| 489 | } |
| 490 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 491 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 492 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 493 | |
| 494 | } // namespace art |