blob: b6981cac032d7262b2d79a18fa1564a70261c9c1 [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
Brian Carlstrom641ce032013-01-31 15:21:37 -080017#include "compiler/compiler_internals.h"
buzbee1bc37c62012-11-20 13:35:41 -080018
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080019namespace art {
20
buzbee67bf8852011-08-17 17:51:35 -070021#define DEBUG_OPT(X)
22
Ian Rogers07ec8e12012-12-01 01:26:51 -080023/* Check RAW, WAR, and RAW 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{
buzbee02031b12012-11-23 09:41:35 -080045 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -070046 /* Insert a move to replace the load */
buzbeefa57c472012-11-21 12:06:18 -080047 LIR* move_lir;
buzbee02031b12012-11-23 09:41:35 -080048 move_lir = cg->OpRegCopyNoInsert( cu, dest, src);
Bill Buzbeea114add2012-05-03 15:00:40 -070049 /*
50 * Insert the converted instruction after the original since the
51 * optimization is scannng in the top-down order and the new instruction
52 * will need to be re-checked (eg the new dest clobbers the src used in
buzbeefa57c472012-11-21 12:06:18 -080053 * this_lir).
Bill Buzbeea114add2012-05-03 15:00:40 -070054 */
buzbeefa57c472012-11-21 12:06:18 -080055 InsertLIRAfter(orig_lir, move_lir);
buzbee67bf8852011-08-17 17:51:35 -070056}
57
58/*
59 * Perform a pass of top-down walk, from the second-last instruction in the
60 * superblock, to eliminate redundant loads and stores.
61 *
62 * An earlier load can eliminate a later load iff
63 * 1) They are must-aliases
64 * 2) The native register is not clobbered in between
65 * 3) The memory location is not written to in between
66 *
67 * An earlier store can eliminate a later load iff
68 * 1) They are must-aliases
69 * 2) The native register is not clobbered in between
70 * 3) The memory location is not written to in between
71 *
72 * A later store can be eliminated by an earlier store iff
73 * 1) They are must-aliases
74 * 2) The memory location is not written to in between
75 */
buzbeefa57c472012-11-21 12:06:18 -080076static void ApplyLoadStoreElimination(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -070077{
buzbee02031b12012-11-23 09:41:35 -080078 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -080079 LIR* this_lir;
buzbee67bf8852011-08-17 17:51:35 -070080
buzbeefa57c472012-11-21 12:06:18 -080081 if (head_lir == tail_lir) return;
buzbee67bf8852011-08-17 17:51:35 -070082
buzbee28c9a832012-11-21 15:39:13 -080083 for (this_lir = PREV_LIR(tail_lir); this_lir != head_lir; this_lir = PREV_LIR(this_lir)) {
buzbeefa57c472012-11-21 12:06:18 -080084 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) ||
buzbee02031b12012-11-23 09:41:35 -080089 (cg->GetTargetInstFlags(this_lir->opcode) & IS_BRANCH) ||
90 !(cg->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.
buzbee02031b12012-11-23 09:41:35 -080097 native_reg_id = (cg->GetTargetInstFlags(this_lir->opcode) & IS_STORE) ? this_lir->operands[2]
buzbeefa57c472012-11-21 12:06:18 -080098 : 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 }
buzbee02031b12012-11-23 09:41:35 -0800102 bool is_this_lir_load = cg->GetTargetInstFlags(this_lir->opcode) & IS_LOAD;
buzbeefa57c472012-11-21 12:06:18 -0800103 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 */
buzbee02031b12012-11-23 09:41:35 -0800124 stop_use_reg_mask = (cg->GetPCUseDefEncoding() | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700125 }
buzbee67bf8852011-08-17 17:51:35 -0700126
buzbee28c9a832012-11-21 15:39:13 -0800127 for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700128
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 /*
130 * Skip already dead instructions (whose dataflow information is
131 * outdated and misleading).
132 */
buzbeefa57c472012-11-21 12:06:18 -0800133 if (check_lir->flags.is_nop) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700134
buzbeefa57c472012-11-21 12:06:18 -0800135 uint64_t check_mem_mask = (check_lir->use_mask | check_lir->def_mask) & ENCODE_MEM;
136 uint64_t alias_condition = this_mem_mask & check_mem_mask;
137 bool stop_here = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700138
139 /*
140 * Potential aliases seen - check the alias relations
141 */
buzbeefa57c472012-11-21 12:06:18 -0800142 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
buzbee02031b12012-11-23 09:41:35 -0800143 bool is_check_lir_load = cg->GetTargetInstFlags(check_lir->opcode) & IS_LOAD;
buzbeefa57c472012-11-21 12:06:18 -0800144 if (alias_condition == ENCODE_LITERAL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 /*
146 * Should only see literal loads in the instruction
147 * stream.
148 */
buzbee02031b12012-11-23 09:41:35 -0800149 DCHECK(!(cg->GetTargetInstFlags(check_lir->opcode) & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 /* Same value && same register type */
buzbeefa57c472012-11-21 12:06:18 -0800151 if (check_lir->alias_info == this_lir->alias_info &&
buzbee02031b12012-11-23 09:41:35 -0800152 cg->SameRegType(check_lir->operands[0], native_reg_id)) {
buzbee67bf8852011-08-17 17:51:35 -0700153 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 * Different destination register - insert
155 * a move
buzbee67bf8852011-08-17 17:51:35 -0700156 */
buzbeefa57c472012-11-21 12:06:18 -0800157 if (check_lir->operands[0] != native_reg_id) {
158 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
159 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 }
buzbeefa57c472012-11-21 12:06:18 -0800161 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 }
buzbeefa57c472012-11-21 12:06:18 -0800163 } else if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 /* Must alias */
buzbeefa57c472012-11-21 12:06:18 -0800165 if (check_lir->alias_info == this_lir->alias_info) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 /* Only optimize compatible registers */
buzbee02031b12012-11-23 09:41:35 -0800167 bool reg_compatible = cg->SameRegType(check_lir->operands[0], native_reg_id);
buzbeefa57c472012-11-21 12:06:18 -0800168 if ((is_this_lir_load && is_check_lir_load) ||
169 (!is_this_lir_load && is_check_lir_load)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 /* RAR or RAW */
buzbeefa57c472012-11-21 12:06:18 -0800171 if (reg_compatible) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 /*
173 * Different destination register -
174 * insert a move
175 */
buzbeefa57c472012-11-21 12:06:18 -0800176 if (check_lir->operands[0] !=
177 native_reg_id) {
178 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
179 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 }
buzbeefa57c472012-11-21 12:06:18 -0800181 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 } else {
183 /*
184 * Destinaions are of different types -
185 * something complicated going on so
186 * stop looking now.
187 */
buzbeefa57c472012-11-21 12:06:18 -0800188 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 }
buzbeefa57c472012-11-21 12:06:18 -0800190 } else if (is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 /* WAR - register value is killed */
buzbeefa57c472012-11-21 12:06:18 -0800192 stop_here = true;
193 } else if (!is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 /* WAW - nuke the earlier store */
buzbeefa57c472012-11-21 12:06:18 -0800195 this_lir->flags.is_nop = true;
196 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 }
198 /* Partial overlap */
buzbeefa57c472012-11-21 12:06:18 -0800199 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700200 /*
buzbeefa57c472012-11-21 12:06:18 -0800201 * It is actually ok to continue if check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 * is a read. But it is hard to make a test
203 * case for this so we just stop here to be
204 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700205 */
buzbeefa57c472012-11-21 12:06:18 -0800206 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 }
buzbee67bf8852011-08-17 17:51:35 -0700208 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800210 if (stop_here) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 break;
buzbeefa57c472012-11-21 12:06:18 -0800212 /* The check_lir has been transformed - check the next one */
213 } else if (check_lir->flags.is_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 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 */
buzbeefa57c472012-11-21 12:06:18 -0800224 if (stop_here == false) {
225 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 }
227
buzbeefa57c472012-11-21 12:06:18 -0800228 if (stop_here == true) {
229 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700230 // Prevent stores from being sunk between ops that generate ccodes and
231 // ops that use them.
buzbee02031b12012-11-23 09:41:35 -0800232 uint64_t flags = cg->GetTargetInstFlags(check_lir->opcode);
buzbeefa57c472012-11-21 12:06:18 -0800233 if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
234 check_lir = PREV_LIR(check_lir);
235 sink_distance--;
buzbeeb046e162012-10-30 15:48:42 -0700236 }
jeffhao573b4292012-07-30 16:37:41 -0700237 }
buzbeefa57c472012-11-21 12:06:18 -0800238 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 /* Only sink store instructions */
buzbeefa57c472012-11-21 12:06:18 -0800240 if (sink_distance && !is_this_lir_load) {
241 LIR* new_store_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
242 *new_store_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 /*
buzbeefa57c472012-11-21 12:06:18 -0800244 * Stop point found - insert *before* the check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 * since the instruction list is scanned in the
246 * top-down order.
247 */
buzbeefa57c472012-11-21 12:06:18 -0800248 InsertLIRBefore(check_lir, new_store_lir);
249 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 }
251 break;
buzbeefa57c472012-11-21 12:06:18 -0800252 } else if (!check_lir->flags.is_nop) {
253 sink_distance++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 }
buzbee67bf8852011-08-17 17:51:35 -0700255 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 }
buzbee67bf8852011-08-17 17:51:35 -0700257}
258
259/*
260 * Perform a pass of bottom-up walk, from the second instruction in the
261 * superblock, to try to hoist loads to earlier slots.
262 */
buzbeefa57c472012-11-21 12:06:18 -0800263void ApplyLoadHoisting(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700264{
buzbee02031b12012-11-23 09:41:35 -0800265 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800266 LIR* this_lir, *check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 /*
268 * Store the list of independent instructions that can be hoisted past.
269 * Will decide the best place to insert later.
270 */
buzbeefa57c472012-11-21 12:06:18 -0800271 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700272
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 /* Empty block */
buzbeefa57c472012-11-21 12:06:18 -0800274 if (head_lir == tail_lir) return;
buzbee67bf8852011-08-17 17:51:35 -0700275
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 /* Start from the second instruction */
buzbee28c9a832012-11-21 15:39:13 -0800277 for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700278
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -0800280 if ((this_lir->flags.is_nop == true) ||
281 is_pseudo_opcode(this_lir->opcode) ||
buzbee02031b12012-11-23 09:41:35 -0800282 !(cg->GetTargetInstFlags(this_lir->opcode) & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 continue;
284 }
buzbee67bf8852011-08-17 17:51:35 -0700285
buzbeefa57c472012-11-21 12:06:18 -0800286 uint64_t stop_use_all_mask = this_lir->use_mask;
buzbee67bf8852011-08-17 17:51:35 -0700287
buzbeefa57c472012-11-21 12:06:18 -0800288 if (cu->instruction_set != kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700289 /*
290 * Branches for null/range checks are marked with the true resource
291 * bits, and loads to Dalvik registers, constant pools, and non-alias
292 * locations are safe to be hoisted. So only mark the heap references
293 * conservatively here.
294 */
buzbeefa57c472012-11-21 12:06:18 -0800295 if (stop_use_all_mask & ENCODE_HEAP_REF) {
buzbee02031b12012-11-23 09:41:35 -0800296 stop_use_all_mask |= cg->GetPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700297 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 }
buzbee67bf8852011-08-17 17:51:35 -0700299
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 /* Similar as above, but just check for pure register dependency */
buzbeefa57c472012-11-21 12:06:18 -0800301 uint64_t stop_use_reg_mask = stop_use_all_mask & ~ENCODE_MEM;
302 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700303
buzbeefa57c472012-11-21 12:06:18 -0800304 int next_slot = 0;
305 bool stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700306
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 /* Try to hoist the load to a good spot */
buzbee28c9a832012-11-21 15:39:13 -0800308 for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700309
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 /*
311 * Skip already dead instructions (whose dataflow information is
312 * outdated and misleading).
313 */
buzbeefa57c472012-11-21 12:06:18 -0800314 if (check_lir->flags.is_nop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700315
buzbeefa57c472012-11-21 12:06:18 -0800316 uint64_t check_mem_mask = check_lir->def_mask & ENCODE_MEM;
317 uint64_t alias_condition = stop_use_all_mask & check_mem_mask;
318 stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700319
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 /* Potential WAR alias seen - check the exact relation */
buzbeefa57c472012-11-21 12:06:18 -0800321 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700322 /* We can fully disambiguate Dalvik references */
buzbeefa57c472012-11-21 12:06:18 -0800323 if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 /* Must alias or partually overlap */
buzbeefa57c472012-11-21 12:06:18 -0800325 if ((check_lir->alias_info == this_lir->alias_info) ||
326 IsDalvikRegisterClobbered(this_lir, check_lir)) {
327 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700328 }
329 /* Conservatively treat all heap refs as may-alias */
330 } else {
buzbeefa57c472012-11-21 12:06:18 -0800331 DCHECK_EQ(alias_condition, ENCODE_HEAP_REF);
332 stop_here = true;
buzbee67bf8852011-08-17 17:51:35 -0700333 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800335 if (stop_here) {
336 prev_inst_list[next_slot++] = check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 break;
buzbee67bf8852011-08-17 17:51:35 -0700338 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 }
buzbee67bf8852011-08-17 17:51:35 -0700340
buzbeefa57c472012-11-21 12:06:18 -0800341 if (stop_here == false) {
342 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
343 check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 }
buzbee67bf8852011-08-17 17:51:35 -0700345
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 /*
347 * Store the dependent or non-pseudo/indepedent instruction to the
348 * list.
349 */
buzbeefa57c472012-11-21 12:06:18 -0800350 if (stop_here || !is_pseudo_opcode(check_lir->opcode)) {
351 prev_inst_list[next_slot++] = check_lir;
352 if (next_slot == MAX_HOIST_DISTANCE) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 }
buzbee67bf8852011-08-17 17:51:35 -0700354
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 /* Found a new place to put the load - move it here */
buzbeefa57c472012-11-21 12:06:18 -0800356 if (stop_here == true) {
357 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 break;
359 }
buzbee67bf8852011-08-17 17:51:35 -0700360 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700361
362 /*
buzbeefa57c472012-11-21 12:06:18 -0800363 * Reached the top - use head_lir as the dependent marker as all labels
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 * are barriers.
365 */
buzbeefa57c472012-11-21 12:06:18 -0800366 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
367 prev_inst_list[next_slot++] = head_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700368 }
369
370 /*
371 * At least one independent instruction is found. Scan in the reversed
372 * direction to find a beneficial slot.
373 */
buzbeefa57c472012-11-21 12:06:18 -0800374 if (next_slot >= 2) {
375 int first_slot = next_slot - 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 int slot;
buzbeefa57c472012-11-21 12:06:18 -0800377 LIR* dep_lir = prev_inst_list[next_slot-1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
buzbeefa57c472012-11-21 12:06:18 -0800379 if (!is_pseudo_opcode(dep_lir->opcode) &&
buzbee02031b12012-11-23 09:41:35 -0800380 (cg->GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
buzbeefa57c472012-11-21 12:06:18 -0800381 first_slot -= LDLD_DISTANCE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 }
383 /*
buzbeefa57c472012-11-21 12:06:18 -0800384 * Make sure we check slot >= 0 since first_slot may be negative
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 * when the loop is first entered.
386 */
buzbeefa57c472012-11-21 12:06:18 -0800387 for (slot = first_slot; slot >= 0; slot--) {
388 LIR* cur_lir = prev_inst_list[slot];
389 LIR* prev_lir = prev_inst_list[slot+1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700390
391 /* Check the highest instruction */
buzbeefa57c472012-11-21 12:06:18 -0800392 if (prev_lir->def_mask == ENCODE_ALL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700393 /*
394 * If the first instruction is a load, don't hoist anything
395 * above it since it is unlikely to be beneficial.
396 */
buzbee02031b12012-11-23 09:41:35 -0800397 if (cg->GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 /*
399 * If the remaining number of slots is less than LD_LATENCY,
400 * insert the hoisted load here.
401 */
402 if (slot < LD_LATENCY) break;
403 }
404
buzbee8320f382012-09-11 16:29:42 -0700405 // Don't look across a barrier label
buzbeefa57c472012-11-21 12:06:18 -0800406 if ((prev_lir->opcode == kPseudoTargetLabel) ||
407 (prev_lir->opcode == kPseudoSafepointPC) ||
408 (prev_lir->opcode == kPseudoBarrier)) {
buzbee8320f382012-09-11 16:29:42 -0700409 break;
410 }
411
Bill Buzbeea114add2012-05-03 15:00:40 -0700412 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 * Try to find two instructions with load/use dependency until
414 * the remaining instructions are less than LD_LATENCY.
415 */
buzbeefa57c472012-11-21 12:06:18 -0800416 bool prev_is_load = is_pseudo_opcode(prev_lir->opcode) ? false :
buzbee02031b12012-11-23 09:41:35 -0800417 (cg->GetTargetInstFlags(prev_lir->opcode) & IS_LOAD);
buzbeefa57c472012-11-21 12:06:18 -0800418 if (((cur_lir->use_mask & prev_lir->def_mask) && prev_is_load) || (slot < LD_LATENCY)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 break;
420 }
421 }
422
423 /* Found a slot to hoist to */
424 if (slot >= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800425 LIR* cur_lir = prev_inst_list[slot];
426 LIR* new_load_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
427 *new_load_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 /*
buzbeefa57c472012-11-21 12:06:18 -0800429 * Insertion is guaranteed to succeed since check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 * is never the first LIR on the list
431 */
buzbeefa57c472012-11-21 12:06:18 -0800432 InsertLIRBefore(cur_lir, new_load_lir);
433 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 }
435 }
436 }
buzbee67bf8852011-08-17 17:51:35 -0700437}
438
buzbeefa57c472012-11-21 12:06:18 -0800439void ApplyLocalOptimizations(CompilationUnit* cu, LIR* head_lir,
440 LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700441{
buzbeefa57c472012-11-21 12:06:18 -0800442 if (!(cu->disable_opt & (1 << kLoadStoreElimination))) {
443 ApplyLoadStoreElimination(cu, head_lir, tail_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 }
buzbeefa57c472012-11-21 12:06:18 -0800445 if (!(cu->disable_opt & (1 << kLoadHoisting))) {
446 ApplyLoadHoisting(cu, head_lir, tail_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800447 }
448}
449
450/*
451 * Nop any unconditional branches that go to the next instruction.
452 * Note: new redundant branches may be inserted later, and we'll
453 * use a check in final instruction assembly to nop those out.
454 */
buzbeefa57c472012-11-21 12:06:18 -0800455void RemoveRedundantBranches(CompilationUnit* cu)
buzbeecbd6d442012-11-17 14:11:25 -0800456{
buzbeefa57c472012-11-21 12:06:18 -0800457 LIR* this_lir;
buzbee02031b12012-11-23 09:41:35 -0800458 Codegen* cg = cu->cg.get();
buzbeecbd6d442012-11-17 14:11:25 -0800459
buzbeefa57c472012-11-21 12:06:18 -0800460 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 -0800461
462 /* Branch to the next instruction */
buzbee02031b12012-11-23 09:41:35 -0800463 if (cg->IsUnconditionalBranch(this_lir)) {
buzbeefa57c472012-11-21 12:06:18 -0800464 LIR* next_lir = this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800465
466 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800467 next_lir = NEXT_LIR(next_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800468
469 /*
470 * Is the branch target the next instruction?
471 */
buzbeefa57c472012-11-21 12:06:18 -0800472 if (next_lir == this_lir->target) {
473 this_lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -0800474 break;
475 }
476
477 /*
478 * Found real useful stuff between the branch and the target.
buzbeefa57c472012-11-21 12:06:18 -0800479 * Need to explicitly check the last_lir_insn here because it
buzbeecbd6d442012-11-17 14:11:25 -0800480 * might be the last real instruction.
481 */
buzbeefa57c472012-11-21 12:06:18 -0800482 if (!is_pseudo_opcode(next_lir->opcode) ||
483 (next_lir == cu->last_lir_insn))
buzbeecbd6d442012-11-17 14:11:25 -0800484 break;
485 }
486 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 }
buzbee67bf8852011-08-17 17:51:35 -0700488}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800489
490} // namespace art