blob: 3c58937300fbc1ee56b69577822705015182344a [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
buzbee395116c2013-02-27 14:30:25 -080017#include "compiler/dex/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)) {
buzbee4ef3e452012-12-14 13:35:28 -080084
85 if (is_pseudo_opcode(this_lir->opcode)) continue;
86
buzbeefa57c472012-11-21 12:06:18 -080087 int sink_distance = 0;
buzbee67bf8852011-08-17 17:51:35 -070088
buzbee4ef3e452012-12-14 13:35:28 -080089 uint64_t target_flags = cg->GetTargetInstFlags(this_lir->opcode);
90
Bill Buzbeea114add2012-05-03 15:00:40 -070091 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -080092 if ((this_lir->flags.is_nop == true) ||
buzbee4ef3e452012-12-14 13:35:28 -080093 (target_flags & IS_BRANCH) ||
94 ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) || // Skip wide loads.
95 ((target_flags & (REG_USE0 | REG_USE1 | REG_USE2)) ==
96 (REG_USE0 | REG_USE1 | REG_USE2)) || // Skip wide stores.
97 !(target_flags & (IS_LOAD | IS_STORE))) {
Bill Buzbeea114add2012-05-03 15:00:40 -070098 continue;
99 }
buzbee67bf8852011-08-17 17:51:35 -0700100
buzbeefa57c472012-11-21 12:06:18 -0800101 int native_reg_id;
102 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700103 // If x86, location differs depending on whether memory/reg operation.
buzbee02031b12012-11-23 09:41:35 -0800104 native_reg_id = (cg->GetTargetInstFlags(this_lir->opcode) & IS_STORE) ? this_lir->operands[2]
buzbeefa57c472012-11-21 12:06:18 -0800105 : this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -0700106 } else {
buzbeefa57c472012-11-21 12:06:18 -0800107 native_reg_id = this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -0700108 }
buzbee02031b12012-11-23 09:41:35 -0800109 bool is_this_lir_load = cg->GetTargetInstFlags(this_lir->opcode) & IS_LOAD;
buzbeefa57c472012-11-21 12:06:18 -0800110 LIR* check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 /* Use the mem mask to determine the rough memory location */
buzbeefa57c472012-11-21 12:06:18 -0800112 uint64_t this_mem_mask = (this_lir->use_mask | this_lir->def_mask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700113
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 /*
115 * Currently only eliminate redundant ld/st for constant and Dalvik
116 * register accesses.
117 */
buzbeefa57c472012-11-21 12:06:18 -0800118 if (!(this_mem_mask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700119
buzbeefa57c472012-11-21 12:06:18 -0800120 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
121 uint64_t stop_use_reg_mask;
122 if (cu->instruction_set == kX86) {
123 stop_use_reg_mask = (IS_BRANCH | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700124 } else {
125 /*
126 * Add pc to the resource mask to prevent this instruction
127 * from sinking past branch instructions. Also take out the memory
buzbeefa57c472012-11-21 12:06:18 -0800128 * region bits since stop_mask is used to check data/control
buzbeeb046e162012-10-30 15:48:42 -0700129 * dependencies.
130 */
buzbee02031b12012-11-23 09:41:35 -0800131 stop_use_reg_mask = (cg->GetPCUseDefEncoding() | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700132 }
buzbee67bf8852011-08-17 17:51:35 -0700133
buzbee28c9a832012-11-21 15:39:13 -0800134 for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700135
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 /*
137 * Skip already dead instructions (whose dataflow information is
138 * outdated and misleading).
139 */
buzbee4ef3e452012-12-14 13:35:28 -0800140 if (check_lir->flags.is_nop || is_pseudo_opcode(check_lir->opcode)) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700141
buzbeefa57c472012-11-21 12:06:18 -0800142 uint64_t check_mem_mask = (check_lir->use_mask | check_lir->def_mask) & ENCODE_MEM;
143 uint64_t alias_condition = this_mem_mask & check_mem_mask;
144 bool stop_here = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700145
146 /*
147 * Potential aliases seen - check the alias relations
148 */
buzbee4ef3e452012-12-14 13:35:28 -0800149 uint64_t check_flags = cg->GetTargetInstFlags(check_lir->opcode);
150 // TUNING: Support instructions with multiple register targets.
151 if ((check_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) {
152 stop_here = true;
153 } else if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
154 bool is_check_lir_load = check_flags & IS_LOAD;
buzbeefa57c472012-11-21 12:06:18 -0800155 if (alias_condition == ENCODE_LITERAL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 /*
157 * Should only see literal loads in the instruction
158 * stream.
159 */
buzbee4ef3e452012-12-14 13:35:28 -0800160 DCHECK(!(check_flags & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 /* Same value && same register type */
buzbeefa57c472012-11-21 12:06:18 -0800162 if (check_lir->alias_info == this_lir->alias_info &&
buzbee02031b12012-11-23 09:41:35 -0800163 cg->SameRegType(check_lir->operands[0], native_reg_id)) {
buzbee67bf8852011-08-17 17:51:35 -0700164 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 * Different destination register - insert
166 * a move
buzbee67bf8852011-08-17 17:51:35 -0700167 */
buzbeefa57c472012-11-21 12:06:18 -0800168 if (check_lir->operands[0] != native_reg_id) {
169 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
170 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 }
buzbeefa57c472012-11-21 12:06:18 -0800172 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 }
buzbeefa57c472012-11-21 12:06:18 -0800174 } else if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 /* Must alias */
buzbeefa57c472012-11-21 12:06:18 -0800176 if (check_lir->alias_info == this_lir->alias_info) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 /* Only optimize compatible registers */
buzbee02031b12012-11-23 09:41:35 -0800178 bool reg_compatible = cg->SameRegType(check_lir->operands[0], native_reg_id);
buzbeefa57c472012-11-21 12:06:18 -0800179 if ((is_this_lir_load && is_check_lir_load) ||
180 (!is_this_lir_load && is_check_lir_load)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700181 /* RAR or RAW */
buzbeefa57c472012-11-21 12:06:18 -0800182 if (reg_compatible) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 /*
184 * Different destination register -
185 * insert a move
186 */
buzbeefa57c472012-11-21 12:06:18 -0800187 if (check_lir->operands[0] !=
188 native_reg_id) {
189 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
190 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 }
buzbeefa57c472012-11-21 12:06:18 -0800192 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700193 } else {
194 /*
195 * Destinaions are of different types -
196 * something complicated going on so
197 * stop looking now.
198 */
buzbeefa57c472012-11-21 12:06:18 -0800199 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 }
buzbeefa57c472012-11-21 12:06:18 -0800201 } else if (is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 /* WAR - register value is killed */
buzbeefa57c472012-11-21 12:06:18 -0800203 stop_here = true;
204 } else if (!is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 /* WAW - nuke the earlier store */
buzbeefa57c472012-11-21 12:06:18 -0800206 this_lir->flags.is_nop = true;
207 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 }
209 /* Partial overlap */
buzbeefa57c472012-11-21 12:06:18 -0800210 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700211 /*
buzbeefa57c472012-11-21 12:06:18 -0800212 * It is actually ok to continue if check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 * is a read. But it is hard to make a test
214 * case for this so we just stop here to be
215 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700216 */
buzbeefa57c472012-11-21 12:06:18 -0800217 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700218 }
buzbee67bf8852011-08-17 17:51:35 -0700219 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800221 if (stop_here) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 break;
buzbeefa57c472012-11-21 12:06:18 -0800223 /* The check_lir has been transformed - check the next one */
224 } else if (check_lir->flags.is_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 continue;
226 }
227 }
228
229
230 /*
231 * this and check LIRs have no memory dependency. Now check if
232 * their register operands have any RAW, WAR, and WAW
233 * dependencies. If so, stop looking.
234 */
buzbeefa57c472012-11-21 12:06:18 -0800235 if (stop_here == false) {
236 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 }
238
buzbeefa57c472012-11-21 12:06:18 -0800239 if (stop_here == true) {
240 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700241 // Prevent stores from being sunk between ops that generate ccodes and
242 // ops that use them.
buzbee02031b12012-11-23 09:41:35 -0800243 uint64_t flags = cg->GetTargetInstFlags(check_lir->opcode);
buzbeefa57c472012-11-21 12:06:18 -0800244 if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
245 check_lir = PREV_LIR(check_lir);
246 sink_distance--;
buzbeeb046e162012-10-30 15:48:42 -0700247 }
jeffhao573b4292012-07-30 16:37:41 -0700248 }
buzbeefa57c472012-11-21 12:06:18 -0800249 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 /* Only sink store instructions */
buzbeefa57c472012-11-21 12:06:18 -0800251 if (sink_distance && !is_this_lir_load) {
252 LIR* new_store_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
253 *new_store_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 /*
buzbeefa57c472012-11-21 12:06:18 -0800255 * Stop point found - insert *before* the check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 * since the instruction list is scanned in the
257 * top-down order.
258 */
buzbeefa57c472012-11-21 12:06:18 -0800259 InsertLIRBefore(check_lir, new_store_lir);
260 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 }
262 break;
buzbeefa57c472012-11-21 12:06:18 -0800263 } else if (!check_lir->flags.is_nop) {
264 sink_distance++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 }
buzbee67bf8852011-08-17 17:51:35 -0700266 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 }
buzbee67bf8852011-08-17 17:51:35 -0700268}
269
270/*
271 * Perform a pass of bottom-up walk, from the second instruction in the
272 * superblock, to try to hoist loads to earlier slots.
273 */
buzbeefa57c472012-11-21 12:06:18 -0800274void ApplyLoadHoisting(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700275{
buzbee02031b12012-11-23 09:41:35 -0800276 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800277 LIR* this_lir, *check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 /*
279 * Store the list of independent instructions that can be hoisted past.
280 * Will decide the best place to insert later.
281 */
buzbeefa57c472012-11-21 12:06:18 -0800282 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700283
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 /* Empty block */
buzbeefa57c472012-11-21 12:06:18 -0800285 if (head_lir == tail_lir) return;
buzbee67bf8852011-08-17 17:51:35 -0700286
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 /* Start from the second instruction */
buzbee28c9a832012-11-21 15:39:13 -0800288 for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700289
buzbee4ef3e452012-12-14 13:35:28 -0800290 if (is_pseudo_opcode(this_lir->opcode)) continue;
291
292 uint64_t target_flags = cg->GetTargetInstFlags(this_lir->opcode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -0800294 if ((this_lir->flags.is_nop == true) ||
buzbee4ef3e452012-12-14 13:35:28 -0800295 ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) ||
296 !(target_flags & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 continue;
298 }
buzbee67bf8852011-08-17 17:51:35 -0700299
buzbeefa57c472012-11-21 12:06:18 -0800300 uint64_t stop_use_all_mask = this_lir->use_mask;
buzbee67bf8852011-08-17 17:51:35 -0700301
buzbeefa57c472012-11-21 12:06:18 -0800302 if (cu->instruction_set != kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700303 /*
304 * Branches for null/range checks are marked with the true resource
305 * bits, and loads to Dalvik registers, constant pools, and non-alias
306 * locations are safe to be hoisted. So only mark the heap references
307 * conservatively here.
308 */
buzbeefa57c472012-11-21 12:06:18 -0800309 if (stop_use_all_mask & ENCODE_HEAP_REF) {
buzbee02031b12012-11-23 09:41:35 -0800310 stop_use_all_mask |= cg->GetPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700311 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 }
buzbee67bf8852011-08-17 17:51:35 -0700313
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 /* Similar as above, but just check for pure register dependency */
buzbeefa57c472012-11-21 12:06:18 -0800315 uint64_t stop_use_reg_mask = stop_use_all_mask & ~ENCODE_MEM;
316 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700317
buzbeefa57c472012-11-21 12:06:18 -0800318 int next_slot = 0;
319 bool stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700320
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 /* Try to hoist the load to a good spot */
buzbee28c9a832012-11-21 15:39:13 -0800322 for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700323
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 /*
325 * Skip already dead instructions (whose dataflow information is
326 * outdated and misleading).
327 */
buzbeefa57c472012-11-21 12:06:18 -0800328 if (check_lir->flags.is_nop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700329
buzbeefa57c472012-11-21 12:06:18 -0800330 uint64_t check_mem_mask = check_lir->def_mask & ENCODE_MEM;
331 uint64_t alias_condition = stop_use_all_mask & check_mem_mask;
332 stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700333
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 /* Potential WAR alias seen - check the exact relation */
buzbeefa57c472012-11-21 12:06:18 -0800335 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 /* We can fully disambiguate Dalvik references */
buzbeefa57c472012-11-21 12:06:18 -0800337 if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 /* Must alias or partually overlap */
buzbeefa57c472012-11-21 12:06:18 -0800339 if ((check_lir->alias_info == this_lir->alias_info) ||
340 IsDalvikRegisterClobbered(this_lir, check_lir)) {
341 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 }
343 /* Conservatively treat all heap refs as may-alias */
344 } else {
buzbeefa57c472012-11-21 12:06:18 -0800345 DCHECK_EQ(alias_condition, ENCODE_HEAP_REF);
346 stop_here = true;
buzbee67bf8852011-08-17 17:51:35 -0700347 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800349 if (stop_here) {
350 prev_inst_list[next_slot++] = check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 break;
buzbee67bf8852011-08-17 17:51:35 -0700352 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 }
buzbee67bf8852011-08-17 17:51:35 -0700354
buzbeefa57c472012-11-21 12:06:18 -0800355 if (stop_here == false) {
356 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
357 check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 }
buzbee67bf8852011-08-17 17:51:35 -0700359
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 /*
361 * Store the dependent or non-pseudo/indepedent instruction to the
362 * list.
363 */
buzbeefa57c472012-11-21 12:06:18 -0800364 if (stop_here || !is_pseudo_opcode(check_lir->opcode)) {
365 prev_inst_list[next_slot++] = check_lir;
366 if (next_slot == MAX_HOIST_DISTANCE) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 }
buzbee67bf8852011-08-17 17:51:35 -0700368
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 /* Found a new place to put the load - move it here */
buzbeefa57c472012-11-21 12:06:18 -0800370 if (stop_here == true) {
371 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700372 break;
373 }
buzbee67bf8852011-08-17 17:51:35 -0700374 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700375
376 /*
buzbeefa57c472012-11-21 12:06:18 -0800377 * Reached the top - use head_lir as the dependent marker as all labels
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 * are barriers.
379 */
buzbeefa57c472012-11-21 12:06:18 -0800380 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
381 prev_inst_list[next_slot++] = head_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 }
383
384 /*
385 * At least one independent instruction is found. Scan in the reversed
386 * direction to find a beneficial slot.
387 */
buzbeefa57c472012-11-21 12:06:18 -0800388 if (next_slot >= 2) {
389 int first_slot = next_slot - 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 int slot;
buzbeefa57c472012-11-21 12:06:18 -0800391 LIR* dep_lir = prev_inst_list[next_slot-1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
buzbeefa57c472012-11-21 12:06:18 -0800393 if (!is_pseudo_opcode(dep_lir->opcode) &&
buzbee02031b12012-11-23 09:41:35 -0800394 (cg->GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
buzbeefa57c472012-11-21 12:06:18 -0800395 first_slot -= LDLD_DISTANCE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 }
397 /*
buzbeefa57c472012-11-21 12:06:18 -0800398 * Make sure we check slot >= 0 since first_slot may be negative
Bill Buzbeea114add2012-05-03 15:00:40 -0700399 * when the loop is first entered.
400 */
buzbeefa57c472012-11-21 12:06:18 -0800401 for (slot = first_slot; slot >= 0; slot--) {
402 LIR* cur_lir = prev_inst_list[slot];
403 LIR* prev_lir = prev_inst_list[slot+1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700404
405 /* Check the highest instruction */
buzbeefa57c472012-11-21 12:06:18 -0800406 if (prev_lir->def_mask == ENCODE_ALL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700407 /*
408 * If the first instruction is a load, don't hoist anything
409 * above it since it is unlikely to be beneficial.
410 */
buzbee02031b12012-11-23 09:41:35 -0800411 if (cg->GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700412 /*
413 * If the remaining number of slots is less than LD_LATENCY,
414 * insert the hoisted load here.
415 */
416 if (slot < LD_LATENCY) break;
417 }
418
buzbee8320f382012-09-11 16:29:42 -0700419 // Don't look across a barrier label
buzbeefa57c472012-11-21 12:06:18 -0800420 if ((prev_lir->opcode == kPseudoTargetLabel) ||
421 (prev_lir->opcode == kPseudoSafepointPC) ||
422 (prev_lir->opcode == kPseudoBarrier)) {
buzbee8320f382012-09-11 16:29:42 -0700423 break;
424 }
425
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 * Try to find two instructions with load/use dependency until
428 * the remaining instructions are less than LD_LATENCY.
429 */
buzbeefa57c472012-11-21 12:06:18 -0800430 bool prev_is_load = is_pseudo_opcode(prev_lir->opcode) ? false :
buzbee02031b12012-11-23 09:41:35 -0800431 (cg->GetTargetInstFlags(prev_lir->opcode) & IS_LOAD);
buzbeefa57c472012-11-21 12:06:18 -0800432 if (((cur_lir->use_mask & prev_lir->def_mask) && prev_is_load) || (slot < LD_LATENCY)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 break;
434 }
435 }
436
437 /* Found a slot to hoist to */
438 if (slot >= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800439 LIR* cur_lir = prev_inst_list[slot];
440 LIR* new_load_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
441 *new_load_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700442 /*
buzbeefa57c472012-11-21 12:06:18 -0800443 * Insertion is guaranteed to succeed since check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 * is never the first LIR on the list
445 */
buzbeefa57c472012-11-21 12:06:18 -0800446 InsertLIRBefore(cur_lir, new_load_lir);
447 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 }
449 }
450 }
buzbee67bf8852011-08-17 17:51:35 -0700451}
452
buzbeefa57c472012-11-21 12:06:18 -0800453void ApplyLocalOptimizations(CompilationUnit* cu, LIR* head_lir,
454 LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700455{
buzbeefa57c472012-11-21 12:06:18 -0800456 if (!(cu->disable_opt & (1 << kLoadStoreElimination))) {
457 ApplyLoadStoreElimination(cu, head_lir, tail_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 }
buzbeefa57c472012-11-21 12:06:18 -0800459 if (!(cu->disable_opt & (1 << kLoadHoisting))) {
460 ApplyLoadHoisting(cu, head_lir, tail_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800461 }
462}
463
464/*
465 * Nop any unconditional branches that go to the next instruction.
466 * Note: new redundant branches may be inserted later, and we'll
467 * use a check in final instruction assembly to nop those out.
468 */
buzbeefa57c472012-11-21 12:06:18 -0800469void RemoveRedundantBranches(CompilationUnit* cu)
buzbeecbd6d442012-11-17 14:11:25 -0800470{
buzbeefa57c472012-11-21 12:06:18 -0800471 LIR* this_lir;
buzbee02031b12012-11-23 09:41:35 -0800472 Codegen* cg = cu->cg.get();
buzbeecbd6d442012-11-17 14:11:25 -0800473
buzbeefa57c472012-11-21 12:06:18 -0800474 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 -0800475
476 /* Branch to the next instruction */
buzbee02031b12012-11-23 09:41:35 -0800477 if (cg->IsUnconditionalBranch(this_lir)) {
buzbeefa57c472012-11-21 12:06:18 -0800478 LIR* next_lir = this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800479
480 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800481 next_lir = NEXT_LIR(next_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800482
483 /*
484 * Is the branch target the next instruction?
485 */
buzbeefa57c472012-11-21 12:06:18 -0800486 if (next_lir == this_lir->target) {
487 this_lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -0800488 break;
489 }
490
491 /*
492 * Found real useful stuff between the branch and the target.
buzbeefa57c472012-11-21 12:06:18 -0800493 * Need to explicitly check the last_lir_insn here because it
buzbeecbd6d442012-11-17 14:11:25 -0800494 * might be the last real instruction.
495 */
buzbeefa57c472012-11-21 12:06:18 -0800496 if (!is_pseudo_opcode(next_lir->opcode) ||
497 (next_lir == cu->last_lir_insn))
buzbeecbd6d442012-11-17 14:11:25 -0800498 break;
499 }
500 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 }
buzbee67bf8852011-08-17 17:51:35 -0700502}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800503
504} // namespace art