blob: 695b12c552ec1ce03d7463c46cb126457239c9cc [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 */
buzbee1fd33462013-03-25 13:40:45 -070043void Mir2Lir::ConvertMemOpIntoMove(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;
buzbee1fd33462013-03-25 13:40:45 -070047 move_lir = OpRegCopyNoInsert(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 */
buzbee1fd33462013-03-25 13:40:45 -070075void Mir2Lir::ApplyLoadStoreElimination(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
buzbee28c9a832012-11-21 15:39:13 -080081 for (this_lir = PREV_LIR(tail_lir); this_lir != head_lir; this_lir = PREV_LIR(this_lir)) {
buzbee4ef3e452012-12-14 13:35:28 -080082
83 if (is_pseudo_opcode(this_lir->opcode)) continue;
84
buzbeefa57c472012-11-21 12:06:18 -080085 int sink_distance = 0;
buzbee67bf8852011-08-17 17:51:35 -070086
buzbee1fd33462013-03-25 13:40:45 -070087 uint64_t target_flags = GetTargetInstFlags(this_lir->opcode);
buzbee4ef3e452012-12-14 13:35:28 -080088
Bill Buzbeea114add2012-05-03 15:00:40 -070089 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -080090 if ((this_lir->flags.is_nop == true) ||
buzbee4ef3e452012-12-14 13:35:28 -080091 (target_flags & IS_BRANCH) ||
92 ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) || // Skip wide loads.
93 ((target_flags & (REG_USE0 | REG_USE1 | REG_USE2)) ==
94 (REG_USE0 | REG_USE1 | REG_USE2)) || // Skip wide stores.
95 !(target_flags & (IS_LOAD | IS_STORE))) {
Bill Buzbeea114add2012-05-03 15:00:40 -070096 continue;
97 }
buzbee67bf8852011-08-17 17:51:35 -070098
buzbeefa57c472012-11-21 12:06:18 -080099 int native_reg_id;
buzbee1fd33462013-03-25 13:40:45 -0700100 if (cu_->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700101 // If x86, location differs depending on whether memory/reg operation.
buzbee1fd33462013-03-25 13:40:45 -0700102 native_reg_id = (GetTargetInstFlags(this_lir->opcode) & IS_STORE) ? this_lir->operands[2]
buzbeefa57c472012-11-21 12:06:18 -0800103 : this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -0700104 } else {
buzbeefa57c472012-11-21 12:06:18 -0800105 native_reg_id = this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -0700106 }
buzbee1fd33462013-03-25 13:40:45 -0700107 bool is_this_lir_load = GetTargetInstFlags(this_lir->opcode) & IS_LOAD;
buzbeefa57c472012-11-21 12:06:18 -0800108 LIR* check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 /* Use the mem mask to determine the rough memory location */
buzbeefa57c472012-11-21 12:06:18 -0800110 uint64_t this_mem_mask = (this_lir->use_mask | this_lir->def_mask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700111
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 /*
113 * Currently only eliminate redundant ld/st for constant and Dalvik
114 * register accesses.
115 */
buzbeefa57c472012-11-21 12:06:18 -0800116 if (!(this_mem_mask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700117
buzbeefa57c472012-11-21 12:06:18 -0800118 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
119 uint64_t stop_use_reg_mask;
buzbee1fd33462013-03-25 13:40:45 -0700120 if (cu_->instruction_set == kX86) {
buzbeefa57c472012-11-21 12:06:18 -0800121 stop_use_reg_mask = (IS_BRANCH | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700122 } else {
123 /*
124 * Add pc to the resource mask to prevent this instruction
125 * from sinking past branch instructions. Also take out the memory
buzbeefa57c472012-11-21 12:06:18 -0800126 * region bits since stop_mask is used to check data/control
buzbeeb046e162012-10-30 15:48:42 -0700127 * dependencies.
128 */
buzbee1fd33462013-03-25 13:40:45 -0700129 stop_use_reg_mask = (GetPCUseDefEncoding() | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700130 }
buzbee67bf8852011-08-17 17:51:35 -0700131
buzbee28c9a832012-11-21 15:39:13 -0800132 for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700133
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 /*
135 * Skip already dead instructions (whose dataflow information is
136 * outdated and misleading).
137 */
buzbee4ef3e452012-12-14 13:35:28 -0800138 if (check_lir->flags.is_nop || is_pseudo_opcode(check_lir->opcode)) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700139
buzbeefa57c472012-11-21 12:06:18 -0800140 uint64_t check_mem_mask = (check_lir->use_mask | check_lir->def_mask) & ENCODE_MEM;
141 uint64_t alias_condition = this_mem_mask & check_mem_mask;
142 bool stop_here = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700143
144 /*
145 * Potential aliases seen - check the alias relations
146 */
buzbee1fd33462013-03-25 13:40:45 -0700147 uint64_t check_flags = GetTargetInstFlags(check_lir->opcode);
buzbee4ef3e452012-12-14 13:35:28 -0800148 // TUNING: Support instructions with multiple register targets.
149 if ((check_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) {
150 stop_here = true;
151 } else if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
152 bool is_check_lir_load = check_flags & IS_LOAD;
buzbeefa57c472012-11-21 12:06:18 -0800153 if (alias_condition == ENCODE_LITERAL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 /*
155 * Should only see literal loads in the instruction
156 * stream.
157 */
buzbee4ef3e452012-12-14 13:35:28 -0800158 DCHECK(!(check_flags & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 /* Same value && same register type */
buzbeefa57c472012-11-21 12:06:18 -0800160 if (check_lir->alias_info == this_lir->alias_info &&
buzbee1fd33462013-03-25 13:40:45 -0700161 SameRegType(check_lir->operands[0], native_reg_id)) {
buzbee67bf8852011-08-17 17:51:35 -0700162 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 * Different destination register - insert
164 * a move
buzbee67bf8852011-08-17 17:51:35 -0700165 */
buzbeefa57c472012-11-21 12:06:18 -0800166 if (check_lir->operands[0] != native_reg_id) {
buzbee1fd33462013-03-25 13:40:45 -0700167 ConvertMemOpIntoMove(check_lir, check_lir->operands[0], native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 }
buzbeefa57c472012-11-21 12:06:18 -0800169 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 }
buzbeefa57c472012-11-21 12:06:18 -0800171 } else if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 /* Must alias */
buzbeefa57c472012-11-21 12:06:18 -0800173 if (check_lir->alias_info == this_lir->alias_info) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 /* Only optimize compatible registers */
buzbee1fd33462013-03-25 13:40:45 -0700175 bool reg_compatible = SameRegType(check_lir->operands[0], native_reg_id);
buzbeefa57c472012-11-21 12:06:18 -0800176 if ((is_this_lir_load && is_check_lir_load) ||
177 (!is_this_lir_load && is_check_lir_load)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 /* RAR or RAW */
buzbeefa57c472012-11-21 12:06:18 -0800179 if (reg_compatible) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 /*
181 * Different destination register -
182 * insert a move
183 */
buzbeefa57c472012-11-21 12:06:18 -0800184 if (check_lir->operands[0] !=
185 native_reg_id) {
buzbee1fd33462013-03-25 13:40:45 -0700186 ConvertMemOpIntoMove(check_lir, check_lir->operands[0], native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 }
buzbeefa57c472012-11-21 12:06:18 -0800188 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 } else {
190 /*
191 * Destinaions are of different types -
192 * something complicated going on so
193 * stop looking now.
194 */
buzbeefa57c472012-11-21 12:06:18 -0800195 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700196 }
buzbeefa57c472012-11-21 12:06:18 -0800197 } else if (is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 /* WAR - register value is killed */
buzbeefa57c472012-11-21 12:06:18 -0800199 stop_here = true;
200 } else if (!is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 /* WAW - nuke the earlier store */
buzbeefa57c472012-11-21 12:06:18 -0800202 this_lir->flags.is_nop = true;
203 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 }
205 /* Partial overlap */
buzbeefa57c472012-11-21 12:06:18 -0800206 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700207 /*
buzbeefa57c472012-11-21 12:06:18 -0800208 * It is actually ok to continue if check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 * is a read. But it is hard to make a test
210 * case for this so we just stop here to be
211 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700212 */
buzbeefa57c472012-11-21 12:06:18 -0800213 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 }
buzbee67bf8852011-08-17 17:51:35 -0700215 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800217 if (stop_here) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700218 break;
buzbeefa57c472012-11-21 12:06:18 -0800219 /* The check_lir has been transformed - check the next one */
220 } else if (check_lir->flags.is_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700221 continue;
222 }
223 }
224
225
226 /*
227 * this and check LIRs have no memory dependency. Now check if
228 * their register operands have any RAW, WAR, and WAW
229 * dependencies. If so, stop looking.
230 */
buzbeefa57c472012-11-21 12:06:18 -0800231 if (stop_here == false) {
232 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 }
234
buzbeefa57c472012-11-21 12:06:18 -0800235 if (stop_here == true) {
buzbee1fd33462013-03-25 13:40:45 -0700236 if (cu_->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700237 // Prevent stores from being sunk between ops that generate ccodes and
238 // ops that use them.
buzbee1fd33462013-03-25 13:40:45 -0700239 uint64_t flags = GetTargetInstFlags(check_lir->opcode);
buzbeefa57c472012-11-21 12:06:18 -0800240 if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
241 check_lir = PREV_LIR(check_lir);
242 sink_distance--;
buzbeeb046e162012-10-30 15:48:42 -0700243 }
jeffhao573b4292012-07-30 16:37:41 -0700244 }
buzbeefa57c472012-11-21 12:06:18 -0800245 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 /* Only sink store instructions */
buzbeefa57c472012-11-21 12:06:18 -0800247 if (sink_distance && !is_this_lir_load) {
buzbee1fd33462013-03-25 13:40:45 -0700248 LIR* new_store_lir = static_cast<LIR*>(NewMem(cu_, sizeof(LIR), true, kAllocLIR));
buzbeefa57c472012-11-21 12:06:18 -0800249 *new_store_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 /*
buzbeefa57c472012-11-21 12:06:18 -0800251 * Stop point found - insert *before* the check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 * since the instruction list is scanned in the
253 * top-down order.
254 */
buzbeefa57c472012-11-21 12:06:18 -0800255 InsertLIRBefore(check_lir, new_store_lir);
256 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 }
258 break;
buzbeefa57c472012-11-21 12:06:18 -0800259 } else if (!check_lir->flags.is_nop) {
260 sink_distance++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 }
buzbee67bf8852011-08-17 17:51:35 -0700262 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 }
buzbee67bf8852011-08-17 17:51:35 -0700264}
265
266/*
267 * Perform a pass of bottom-up walk, from the second instruction in the
268 * superblock, to try to hoist loads to earlier slots.
269 */
buzbee1fd33462013-03-25 13:40:45 -0700270void Mir2Lir::ApplyLoadHoisting(LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700271{
buzbeefa57c472012-11-21 12:06:18 -0800272 LIR* this_lir, *check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 /*
274 * Store the list of independent instructions that can be hoisted past.
275 * Will decide the best place to insert later.
276 */
buzbeefa57c472012-11-21 12:06:18 -0800277 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700278
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 /* Empty block */
buzbeefa57c472012-11-21 12:06:18 -0800280 if (head_lir == tail_lir) return;
buzbee67bf8852011-08-17 17:51:35 -0700281
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 /* Start from the second instruction */
buzbee28c9a832012-11-21 15:39:13 -0800283 for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700284
buzbee4ef3e452012-12-14 13:35:28 -0800285 if (is_pseudo_opcode(this_lir->opcode)) continue;
286
buzbee1fd33462013-03-25 13:40:45 -0700287 uint64_t target_flags = GetTargetInstFlags(this_lir->opcode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -0800289 if ((this_lir->flags.is_nop == true) ||
buzbee4ef3e452012-12-14 13:35:28 -0800290 ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) ||
291 !(target_flags & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 continue;
293 }
buzbee67bf8852011-08-17 17:51:35 -0700294
buzbeefa57c472012-11-21 12:06:18 -0800295 uint64_t stop_use_all_mask = this_lir->use_mask;
buzbee67bf8852011-08-17 17:51:35 -0700296
buzbee1fd33462013-03-25 13:40:45 -0700297 if (cu_->instruction_set != kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700298 /*
299 * Branches for null/range checks are marked with the true resource
300 * bits, and loads to Dalvik registers, constant pools, and non-alias
301 * locations are safe to be hoisted. So only mark the heap references
302 * conservatively here.
303 */
buzbeefa57c472012-11-21 12:06:18 -0800304 if (stop_use_all_mask & ENCODE_HEAP_REF) {
buzbee1fd33462013-03-25 13:40:45 -0700305 stop_use_all_mask |= GetPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700306 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 }
buzbee67bf8852011-08-17 17:51:35 -0700308
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 /* Similar as above, but just check for pure register dependency */
buzbeefa57c472012-11-21 12:06:18 -0800310 uint64_t stop_use_reg_mask = stop_use_all_mask & ~ENCODE_MEM;
311 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700312
buzbeefa57c472012-11-21 12:06:18 -0800313 int next_slot = 0;
314 bool stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700315
Bill Buzbeea114add2012-05-03 15:00:40 -0700316 /* Try to hoist the load to a good spot */
buzbee28c9a832012-11-21 15:39:13 -0800317 for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700318
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 /*
320 * Skip already dead instructions (whose dataflow information is
321 * outdated and misleading).
322 */
buzbeefa57c472012-11-21 12:06:18 -0800323 if (check_lir->flags.is_nop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700324
buzbeefa57c472012-11-21 12:06:18 -0800325 uint64_t check_mem_mask = check_lir->def_mask & ENCODE_MEM;
326 uint64_t alias_condition = stop_use_all_mask & check_mem_mask;
327 stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700328
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 /* Potential WAR alias seen - check the exact relation */
buzbeefa57c472012-11-21 12:06:18 -0800330 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 /* We can fully disambiguate Dalvik references */
buzbeefa57c472012-11-21 12:06:18 -0800332 if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 /* Must alias or partually overlap */
buzbeefa57c472012-11-21 12:06:18 -0800334 if ((check_lir->alias_info == this_lir->alias_info) ||
335 IsDalvikRegisterClobbered(this_lir, check_lir)) {
336 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 }
338 /* Conservatively treat all heap refs as may-alias */
339 } else {
buzbeefa57c472012-11-21 12:06:18 -0800340 DCHECK_EQ(alias_condition, ENCODE_HEAP_REF);
341 stop_here = true;
buzbee67bf8852011-08-17 17:51:35 -0700342 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800344 if (stop_here) {
345 prev_inst_list[next_slot++] = check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 break;
buzbee67bf8852011-08-17 17:51:35 -0700347 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 }
buzbee67bf8852011-08-17 17:51:35 -0700349
buzbeefa57c472012-11-21 12:06:18 -0800350 if (stop_here == false) {
351 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
352 check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 }
buzbee67bf8852011-08-17 17:51:35 -0700354
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 /*
356 * Store the dependent or non-pseudo/indepedent instruction to the
357 * list.
358 */
buzbeefa57c472012-11-21 12:06:18 -0800359 if (stop_here || !is_pseudo_opcode(check_lir->opcode)) {
360 prev_inst_list[next_slot++] = check_lir;
361 if (next_slot == MAX_HOIST_DISTANCE) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 }
buzbee67bf8852011-08-17 17:51:35 -0700363
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 /* Found a new place to put the load - move it here */
buzbeefa57c472012-11-21 12:06:18 -0800365 if (stop_here == true) {
366 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 break;
368 }
buzbee67bf8852011-08-17 17:51:35 -0700369 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700370
371 /*
buzbeefa57c472012-11-21 12:06:18 -0800372 * Reached the top - use head_lir as the dependent marker as all labels
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 * are barriers.
374 */
buzbeefa57c472012-11-21 12:06:18 -0800375 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
376 prev_inst_list[next_slot++] = head_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 }
378
379 /*
380 * At least one independent instruction is found. Scan in the reversed
381 * direction to find a beneficial slot.
382 */
buzbeefa57c472012-11-21 12:06:18 -0800383 if (next_slot >= 2) {
384 int first_slot = next_slot - 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 int slot;
buzbeefa57c472012-11-21 12:06:18 -0800386 LIR* dep_lir = prev_inst_list[next_slot-1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
buzbeefa57c472012-11-21 12:06:18 -0800388 if (!is_pseudo_opcode(dep_lir->opcode) &&
buzbee1fd33462013-03-25 13:40:45 -0700389 (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
buzbeefa57c472012-11-21 12:06:18 -0800390 first_slot -= LDLD_DISTANCE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700391 }
392 /*
buzbeefa57c472012-11-21 12:06:18 -0800393 * Make sure we check slot >= 0 since first_slot may be negative
Bill Buzbeea114add2012-05-03 15:00:40 -0700394 * when the loop is first entered.
395 */
buzbeefa57c472012-11-21 12:06:18 -0800396 for (slot = first_slot; slot >= 0; slot--) {
397 LIR* cur_lir = prev_inst_list[slot];
398 LIR* prev_lir = prev_inst_list[slot+1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700399
400 /* Check the highest instruction */
buzbeefa57c472012-11-21 12:06:18 -0800401 if (prev_lir->def_mask == ENCODE_ALL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 /*
403 * If the first instruction is a load, don't hoist anything
404 * above it since it is unlikely to be beneficial.
405 */
buzbee1fd33462013-03-25 13:40:45 -0700406 if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700407 /*
408 * If the remaining number of slots is less than LD_LATENCY,
409 * insert the hoisted load here.
410 */
411 if (slot < LD_LATENCY) break;
412 }
413
buzbee8320f382012-09-11 16:29:42 -0700414 // Don't look across a barrier label
buzbeefa57c472012-11-21 12:06:18 -0800415 if ((prev_lir->opcode == kPseudoTargetLabel) ||
416 (prev_lir->opcode == kPseudoSafepointPC) ||
417 (prev_lir->opcode == kPseudoBarrier)) {
buzbee8320f382012-09-11 16:29:42 -0700418 break;
419 }
420
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 * Try to find two instructions with load/use dependency until
423 * the remaining instructions are less than LD_LATENCY.
424 */
buzbeefa57c472012-11-21 12:06:18 -0800425 bool prev_is_load = is_pseudo_opcode(prev_lir->opcode) ? false :
buzbee1fd33462013-03-25 13:40:45 -0700426 (GetTargetInstFlags(prev_lir->opcode) & IS_LOAD);
buzbeefa57c472012-11-21 12:06:18 -0800427 if (((cur_lir->use_mask & prev_lir->def_mask) && prev_is_load) || (slot < LD_LATENCY)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 break;
429 }
430 }
431
432 /* Found a slot to hoist to */
433 if (slot >= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800434 LIR* cur_lir = prev_inst_list[slot];
buzbee1fd33462013-03-25 13:40:45 -0700435 LIR* new_load_lir = static_cast<LIR*>(NewMem(cu_, sizeof(LIR), true, kAllocLIR));
buzbeefa57c472012-11-21 12:06:18 -0800436 *new_load_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 /*
buzbeefa57c472012-11-21 12:06:18 -0800438 * Insertion is guaranteed to succeed since check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 * is never the first LIR on the list
440 */
buzbeefa57c472012-11-21 12:06:18 -0800441 InsertLIRBefore(cur_lir, new_load_lir);
442 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700443 }
444 }
445 }
buzbee67bf8852011-08-17 17:51:35 -0700446}
447
buzbee1fd33462013-03-25 13:40:45 -0700448void Mir2Lir::ApplyLocalOptimizations(LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700449{
buzbee1fd33462013-03-25 13:40:45 -0700450 if (!(cu_->disable_opt & (1 << kLoadStoreElimination))) {
451 ApplyLoadStoreElimination(head_lir, tail_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 }
buzbee1fd33462013-03-25 13:40:45 -0700453 if (!(cu_->disable_opt & (1 << kLoadHoisting))) {
454 ApplyLoadHoisting(head_lir, tail_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800455 }
456}
457
458/*
459 * Nop any unconditional branches that go to the next instruction.
460 * Note: new redundant branches may be inserted later, and we'll
461 * use a check in final instruction assembly to nop those out.
462 */
buzbee1fd33462013-03-25 13:40:45 -0700463void Mir2Lir::RemoveRedundantBranches()
buzbeecbd6d442012-11-17 14:11:25 -0800464{
buzbeefa57c472012-11-21 12:06:18 -0800465 LIR* this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800466
buzbee1fd33462013-03-25 13:40:45 -0700467 for (this_lir = first_lir_insn_; this_lir != last_lir_insn_; this_lir = NEXT_LIR(this_lir)) {
buzbeecbd6d442012-11-17 14:11:25 -0800468
469 /* Branch to the next instruction */
buzbee1fd33462013-03-25 13:40:45 -0700470 if (IsUnconditionalBranch(this_lir)) {
buzbeefa57c472012-11-21 12:06:18 -0800471 LIR* next_lir = this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800472
473 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800474 next_lir = NEXT_LIR(next_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800475
476 /*
477 * Is the branch target the next instruction?
478 */
buzbeefa57c472012-11-21 12:06:18 -0800479 if (next_lir == this_lir->target) {
480 this_lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -0800481 break;
482 }
483
484 /*
485 * Found real useful stuff between the branch and the target.
buzbee1fd33462013-03-25 13:40:45 -0700486 * Need to explicitly check the last_lir_insn_ here because it
buzbeecbd6d442012-11-17 14:11:25 -0800487 * might be the last real instruction.
488 */
buzbeefa57c472012-11-21 12:06:18 -0800489 if (!is_pseudo_opcode(next_lir->opcode) ||
buzbee1fd33462013-03-25 13:40:45 -0700490 (next_lir == last_lir_insn_))
buzbeecbd6d442012-11-17 14:11:25 -0800491 break;
492 }
493 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700494 }
buzbee67bf8852011-08-17 17:51:35 -0700495}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800496
497} // namespace art