blob: d1a74441ae342eb4bc2ddd6a62325fd70f954b17 [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 */
buzbeefa57c472012-11-21 12:06:18 -080024#define CHECK_REG_DEP(use, def, check) ((def & check->use_mask) || \
25 ((use | def) & check->def_mask))
buzbee67bf8852011-08-17 17:51:35 -070026
27/* Scheduler heuristics */
28#define MAX_HOIST_DISTANCE 20
29#define LDLD_DISTANCE 4
30#define LD_LATENCY 2
31
buzbeeaad94382012-11-21 07:40:50 -080032static bool IsDalvikRegisterClobbered(LIR* lir1, LIR* lir2)
buzbee67bf8852011-08-17 17:51:35 -070033{
buzbeefa57c472012-11-21 12:06:18 -080034 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);
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 */
buzbeefa57c472012-11-21 12:06:18 -080043static void ConvertMemOpIntoMove(CompilationUnit* cu, LIR* orig_lir, int dest, int src)
buzbee67bf8852011-08-17 17:51:35 -070044{
Bill Buzbeea114add2012-05-03 15:00:40 -070045 /* Insert a move to replace the load */
buzbeefa57c472012-11-21 12:06:18 -080046 LIR* move_lir;
47 move_lir = OpRegCopyNoInsert( cu, dest, src);
Bill Buzbeea114add2012-05-03 15:00:40 -070048 /*
49 * Insert the converted instruction after the original since the
50 * optimization is scannng in the top-down order and the new instruction
51 * will need to be re-checked (eg the new dest clobbers the src used in
buzbeefa57c472012-11-21 12:06:18 -080052 * this_lir).
Bill Buzbeea114add2012-05-03 15:00:40 -070053 */
buzbeefa57c472012-11-21 12:06:18 -080054 InsertLIRAfter(orig_lir, move_lir);
buzbee67bf8852011-08-17 17:51:35 -070055}
56
57/*
58 * Perform a pass of top-down walk, from the second-last instruction in the
59 * superblock, to eliminate redundant loads and stores.
60 *
61 * An earlier load can eliminate a later load iff
62 * 1) They are must-aliases
63 * 2) The native register is not clobbered in between
64 * 3) The memory location is not written to in between
65 *
66 * An earlier store can eliminate a later load iff
67 * 1) They are must-aliases
68 * 2) The native register is not clobbered in between
69 * 3) The memory location is not written to in between
70 *
71 * A later store can be eliminated by an earlier store iff
72 * 1) They are must-aliases
73 * 2) The memory location is not written to in between
74 */
buzbeefa57c472012-11-21 12:06:18 -080075static void ApplyLoadStoreElimination(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -070076{
buzbeefa57c472012-11-21 12:06:18 -080077 LIR* this_lir;
buzbee67bf8852011-08-17 17:51:35 -070078
buzbeefa57c472012-11-21 12:06:18 -080079 if (head_lir == tail_lir) return;
buzbee67bf8852011-08-17 17:51:35 -070080
buzbeefa57c472012-11-21 12:06:18 -080081 for (this_lir = PREV_LIR(tail_lir);
82 this_lir != head_lir;
83 this_lir = PREV_LIR(this_lir)) {
84 int sink_distance = 0;
buzbee67bf8852011-08-17 17:51:35 -070085
Bill Buzbeea114add2012-05-03 15:00:40 -070086 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -080087 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 Buzbeea114add2012-05-03 15:00:40 -070091 continue;
92 }
buzbee67bf8852011-08-17 17:51:35 -070093
buzbeefa57c472012-11-21 12:06:18 -080094 int native_reg_id;
95 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -070096 // If x86, location differs depending on whether memory/reg operation.
buzbeefa57c472012-11-21 12:06:18 -080097 native_reg_id = (GetTargetInstFlags(this_lir->opcode) & IS_STORE) ? this_lir->operands[2]
98 : this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -070099 } else {
buzbeefa57c472012-11-21 12:06:18 -0800100 native_reg_id = this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -0700101 }
buzbeefa57c472012-11-21 12:06:18 -0800102 bool is_this_lir_load = GetTargetInstFlags(this_lir->opcode) & IS_LOAD;
103 LIR* check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 /* Use the mem mask to determine the rough memory location */
buzbeefa57c472012-11-21 12:06:18 -0800105 uint64_t this_mem_mask = (this_lir->use_mask | this_lir->def_mask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700106
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 /*
108 * Currently only eliminate redundant ld/st for constant and Dalvik
109 * register accesses.
110 */
buzbeefa57c472012-11-21 12:06:18 -0800111 if (!(this_mem_mask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700112
buzbeefa57c472012-11-21 12:06:18 -0800113 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;
buzbeeb046e162012-10-30 15:48:42 -0700117 } else {
118 /*
119 * Add pc to the resource mask to prevent this instruction
120 * from sinking past branch instructions. Also take out the memory
buzbeefa57c472012-11-21 12:06:18 -0800121 * region bits since stop_mask is used to check data/control
buzbeeb046e162012-10-30 15:48:42 -0700122 * dependencies.
123 */
buzbeefa57c472012-11-21 12:06:18 -0800124 stop_use_reg_mask = (GetPCUseDefEncoding() | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700125 }
buzbee67bf8852011-08-17 17:51:35 -0700126
buzbeefa57c472012-11-21 12:06:18 -0800127 for (check_lir = NEXT_LIR(this_lir);
128 check_lir != tail_lir;
129 check_lir = NEXT_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700130
Bill Buzbeea114add2012-05-03 15:00:40 -0700131 /*
132 * Skip already dead instructions (whose dataflow information is
133 * outdated and misleading).
134 */
buzbeefa57c472012-11-21 12:06:18 -0800135 if (check_lir->flags.is_nop) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136
buzbeefa57c472012-11-21 12:06:18 -0800137 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 Buzbeea114add2012-05-03 15:00:40 -0700140
141 /*
142 * Potential aliases seen - check the alias relations
143 */
buzbeefa57c472012-11-21 12:06:18 -0800144 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 Buzbeea114add2012-05-03 15:00:40 -0700147 /*
148 * Should only see literal loads in the instruction
149 * stream.
150 */
buzbeefa57c472012-11-21 12:06:18 -0800151 DCHECK(!(GetTargetInstFlags(check_lir->opcode) & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 /* Same value && same register type */
buzbeefa57c472012-11-21 12:06:18 -0800153 if (check_lir->alias_info == this_lir->alias_info &&
154 SameRegType(check_lir->operands[0], native_reg_id)) {
buzbee67bf8852011-08-17 17:51:35 -0700155 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 * Different destination register - insert
157 * a move
buzbee67bf8852011-08-17 17:51:35 -0700158 */
buzbeefa57c472012-11-21 12:06:18 -0800159 if (check_lir->operands[0] != native_reg_id) {
160 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
161 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 }
buzbeefa57c472012-11-21 12:06:18 -0800163 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 }
buzbeefa57c472012-11-21 12:06:18 -0800165 } else if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 /* Must alias */
buzbeefa57c472012-11-21 12:06:18 -0800167 if (check_lir->alias_info == this_lir->alias_info) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 /* Only optimize compatible registers */
buzbeefa57c472012-11-21 12:06:18 -0800169 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 Buzbeea114add2012-05-03 15:00:40 -0700172 /* RAR or RAW */
buzbeefa57c472012-11-21 12:06:18 -0800173 if (reg_compatible) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 /*
175 * Different destination register -
176 * insert a move
177 */
buzbeefa57c472012-11-21 12:06:18 -0800178 if (check_lir->operands[0] !=
179 native_reg_id) {
180 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
181 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 }
buzbeefa57c472012-11-21 12:06:18 -0800183 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 } else {
185 /*
186 * Destinaions are of different types -
187 * something complicated going on so
188 * stop looking now.
189 */
buzbeefa57c472012-11-21 12:06:18 -0800190 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 }
buzbeefa57c472012-11-21 12:06:18 -0800192 } else if (is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700193 /* WAR - register value is killed */
buzbeefa57c472012-11-21 12:06:18 -0800194 stop_here = true;
195 } else if (!is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700196 /* WAW - nuke the earlier store */
buzbeefa57c472012-11-21 12:06:18 -0800197 this_lir->flags.is_nop = true;
198 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 }
200 /* Partial overlap */
buzbeefa57c472012-11-21 12:06:18 -0800201 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700202 /*
buzbeefa57c472012-11-21 12:06:18 -0800203 * It is actually ok to continue if check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 * is a read. But it is hard to make a test
205 * case for this so we just stop here to be
206 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700207 */
buzbeefa57c472012-11-21 12:06:18 -0800208 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 }
buzbee67bf8852011-08-17 17:51:35 -0700210 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800212 if (stop_here) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 break;
buzbeefa57c472012-11-21 12:06:18 -0800214 /* The check_lir has been transformed - check the next one */
215 } else if (check_lir->flags.is_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 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 */
buzbeefa57c472012-11-21 12:06:18 -0800226 if (stop_here == false) {
227 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 }
229
buzbeefa57c472012-11-21 12:06:18 -0800230 if (stop_here == true) {
231 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700232 // Prevent stores from being sunk between ops that generate ccodes and
233 // ops that use them.
buzbeefa57c472012-11-21 12:06:18 -0800234 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--;
buzbeeb046e162012-10-30 15:48:42 -0700238 }
jeffhao573b4292012-07-30 16:37:41 -0700239 }
buzbeefa57c472012-11-21 12:06:18 -0800240 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 /* Only sink store instructions */
buzbeefa57c472012-11-21 12:06:18 -0800242 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 Buzbeea114add2012-05-03 15:00:40 -0700245 /*
buzbeefa57c472012-11-21 12:06:18 -0800246 * Stop point found - insert *before* the check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 * since the instruction list is scanned in the
248 * top-down order.
249 */
buzbeefa57c472012-11-21 12:06:18 -0800250 InsertLIRBefore(check_lir, new_store_lir);
251 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 }
253 break;
buzbeefa57c472012-11-21 12:06:18 -0800254 } else if (!check_lir->flags.is_nop) {
255 sink_distance++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 }
buzbee67bf8852011-08-17 17:51:35 -0700257 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 }
buzbee67bf8852011-08-17 17:51:35 -0700259}
260
261/*
262 * Perform a pass of bottom-up walk, from the second instruction in the
263 * superblock, to try to hoist loads to earlier slots.
264 */
buzbeefa57c472012-11-21 12:06:18 -0800265void ApplyLoadHoisting(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700266{
buzbeefa57c472012-11-21 12:06:18 -0800267 LIR* this_lir, *check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 /*
269 * Store the list of independent instructions that can be hoisted past.
270 * Will decide the best place to insert later.
271 */
buzbeefa57c472012-11-21 12:06:18 -0800272 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700273
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 /* Empty block */
buzbeefa57c472012-11-21 12:06:18 -0800275 if (head_lir == tail_lir) return;
buzbee67bf8852011-08-17 17:51:35 -0700276
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 /* Start from the second instruction */
buzbeefa57c472012-11-21 12:06:18 -0800278 for (this_lir = NEXT_LIR(head_lir);
279 this_lir != tail_lir;
280 this_lir = NEXT_LIR(this_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700281
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -0800283 if ((this_lir->flags.is_nop == true) ||
284 is_pseudo_opcode(this_lir->opcode) ||
285 !(GetTargetInstFlags(this_lir->opcode) & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 continue;
287 }
buzbee67bf8852011-08-17 17:51:35 -0700288
buzbeefa57c472012-11-21 12:06:18 -0800289 uint64_t stop_use_all_mask = this_lir->use_mask;
buzbee67bf8852011-08-17 17:51:35 -0700290
buzbeefa57c472012-11-21 12:06:18 -0800291 if (cu->instruction_set != kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700292 /*
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 */
buzbeefa57c472012-11-21 12:06:18 -0800298 if (stop_use_all_mask & ENCODE_HEAP_REF) {
299 stop_use_all_mask |= GetPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700300 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 }
buzbee67bf8852011-08-17 17:51:35 -0700302
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 /* Similar as above, but just check for pure register dependency */
buzbeefa57c472012-11-21 12:06:18 -0800304 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;
buzbee67bf8852011-08-17 17:51:35 -0700306
buzbeefa57c472012-11-21 12:06:18 -0800307 int next_slot = 0;
308 bool stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700309
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 /* Try to hoist the load to a good spot */
buzbeefa57c472012-11-21 12:06:18 -0800311 for (check_lir = PREV_LIR(this_lir);
312 check_lir != head_lir;
313 check_lir = PREV_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700314
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 /*
316 * Skip already dead instructions (whose dataflow information is
317 * outdated and misleading).
318 */
buzbeefa57c472012-11-21 12:06:18 -0800319 if (check_lir->flags.is_nop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700320
buzbeefa57c472012-11-21 12:06:18 -0800321 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;
buzbee67bf8852011-08-17 17:51:35 -0700324
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 /* Potential WAR alias seen - check the exact relation */
buzbeefa57c472012-11-21 12:06:18 -0800326 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700327 /* We can fully disambiguate Dalvik references */
buzbeefa57c472012-11-21 12:06:18 -0800328 if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 /* Must alias or partually overlap */
buzbeefa57c472012-11-21 12:06:18 -0800330 if ((check_lir->alias_info == this_lir->alias_info) ||
331 IsDalvikRegisterClobbered(this_lir, check_lir)) {
332 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 }
334 /* Conservatively treat all heap refs as may-alias */
335 } else {
buzbeefa57c472012-11-21 12:06:18 -0800336 DCHECK_EQ(alias_condition, ENCODE_HEAP_REF);
337 stop_here = true;
buzbee67bf8852011-08-17 17:51:35 -0700338 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800340 if (stop_here) {
341 prev_inst_list[next_slot++] = check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 break;
buzbee67bf8852011-08-17 17:51:35 -0700343 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 }
buzbee67bf8852011-08-17 17:51:35 -0700345
buzbeefa57c472012-11-21 12:06:18 -0800346 if (stop_here == false) {
347 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
348 check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 }
buzbee67bf8852011-08-17 17:51:35 -0700350
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 /*
352 * Store the dependent or non-pseudo/indepedent instruction to the
353 * list.
354 */
buzbeefa57c472012-11-21 12:06:18 -0800355 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 Buzbeea114add2012-05-03 15:00:40 -0700358 }
buzbee67bf8852011-08-17 17:51:35 -0700359
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 /* Found a new place to put the load - move it here */
buzbeefa57c472012-11-21 12:06:18 -0800361 if (stop_here == true) {
362 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700363 break;
364 }
buzbee67bf8852011-08-17 17:51:35 -0700365 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700366
367 /*
buzbeefa57c472012-11-21 12:06:18 -0800368 * Reached the top - use head_lir as the dependent marker as all labels
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 * are barriers.
370 */
buzbeefa57c472012-11-21 12:06:18 -0800371 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
372 prev_inst_list[next_slot++] = head_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 }
374
375 /*
376 * At least one independent instruction is found. Scan in the reversed
377 * direction to find a beneficial slot.
378 */
buzbeefa57c472012-11-21 12:06:18 -0800379 if (next_slot >= 2) {
380 int first_slot = next_slot - 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 int slot;
buzbeefa57c472012-11-21 12:06:18 -0800382 LIR* dep_lir = prev_inst_list[next_slot-1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
buzbeefa57c472012-11-21 12:06:18 -0800384 if (!is_pseudo_opcode(dep_lir->opcode) &&
385 (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
386 first_slot -= LDLD_DISTANCE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 }
388 /*
buzbeefa57c472012-11-21 12:06:18 -0800389 * Make sure we check slot >= 0 since first_slot may be negative
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 * when the loop is first entered.
391 */
buzbeefa57c472012-11-21 12:06:18 -0800392 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 Buzbeea114add2012-05-03 15:00:40 -0700395
396 /* Check the highest instruction */
buzbeefa57c472012-11-21 12:06:18 -0800397 if (prev_lir->def_mask == ENCODE_ALL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 /*
399 * If the first instruction is a load, don't hoist anything
400 * above it since it is unlikely to be beneficial.
401 */
buzbeefa57c472012-11-21 12:06:18 -0800402 if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 /*
404 * If the remaining number of slots is less than LD_LATENCY,
405 * insert the hoisted load here.
406 */
407 if (slot < LD_LATENCY) break;
408 }
409
buzbee8320f382012-09-11 16:29:42 -0700410 // Don't look across a barrier label
buzbeefa57c472012-11-21 12:06:18 -0800411 if ((prev_lir->opcode == kPseudoTargetLabel) ||
412 (prev_lir->opcode == kPseudoSafepointPC) ||
413 (prev_lir->opcode == kPseudoBarrier)) {
buzbee8320f382012-09-11 16:29:42 -0700414 break;
415 }
416
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 * Try to find two instructions with load/use dependency until
419 * the remaining instructions are less than LD_LATENCY.
420 */
buzbeefa57c472012-11-21 12:06:18 -0800421 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 Buzbeea114add2012-05-03 15:00:40 -0700424 break;
425 }
426 }
427
428 /* Found a slot to hoist to */
429 if (slot >= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800430 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 Buzbeea114add2012-05-03 15:00:40 -0700433 /*
buzbeefa57c472012-11-21 12:06:18 -0800434 * Insertion is guaranteed to succeed since check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 * is never the first LIR on the list
436 */
buzbeefa57c472012-11-21 12:06:18 -0800437 InsertLIRBefore(cur_lir, new_load_lir);
438 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 }
440 }
441 }
buzbee67bf8852011-08-17 17:51:35 -0700442}
443
buzbeefa57c472012-11-21 12:06:18 -0800444void ApplyLocalOptimizations(CompilationUnit* cu, LIR* head_lir,
445 LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700446{
buzbeefa57c472012-11-21 12:06:18 -0800447 if (!(cu->disable_opt & (1 << kLoadStoreElimination))) {
448 ApplyLoadStoreElimination(cu, head_lir, tail_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 }
buzbeefa57c472012-11-21 12:06:18 -0800450 if (!(cu->disable_opt & (1 << kLoadHoisting))) {
451 ApplyLoadHoisting(cu, head_lir, tail_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800452 }
453}
454
455/*
456 * Nop any unconditional branches that go to the next instruction.
457 * Note: new redundant branches may be inserted later, and we'll
458 * use a check in final instruction assembly to nop those out.
459 */
buzbeefa57c472012-11-21 12:06:18 -0800460void RemoveRedundantBranches(CompilationUnit* cu)
buzbeecbd6d442012-11-17 14:11:25 -0800461{
buzbeefa57c472012-11-21 12:06:18 -0800462 LIR* this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800463
buzbeefa57c472012-11-21 12:06:18 -0800464 for (this_lir = cu->first_lir_insn; this_lir != cu->last_lir_insn; this_lir = NEXT_LIR(this_lir)) {
buzbeecbd6d442012-11-17 14:11:25 -0800465
466 /* Branch to the next instruction */
buzbeefa57c472012-11-21 12:06:18 -0800467 if (BranchUnconditional(this_lir)) {
468 LIR* next_lir = this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800469
470 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800471 next_lir = NEXT_LIR(next_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800472
473 /*
474 * Is the branch target the next instruction?
475 */
buzbeefa57c472012-11-21 12:06:18 -0800476 if (next_lir == this_lir->target) {
477 this_lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -0800478 break;
479 }
480
481 /*
482 * Found real useful stuff between the branch and the target.
buzbeefa57c472012-11-21 12:06:18 -0800483 * Need to explicitly check the last_lir_insn here because it
buzbeecbd6d442012-11-17 14:11:25 -0800484 * might be the last real instruction.
485 */
buzbeefa57c472012-11-21 12:06:18 -0800486 if (!is_pseudo_opcode(next_lir->opcode) ||
487 (next_lir == cu->last_lir_insn))
buzbeecbd6d442012-11-17 14:11:25 -0800488 break;
489 }
490 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 }
buzbee67bf8852011-08-17 17:51:35 -0700492}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800493
494} // namespace art