blob: cf04b21bd72357a700a1cfb93f0a2d7932c4fee0 [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
buzbee28c9a832012-11-21 15:39:13 -080081 for (this_lir = PREV_LIR(tail_lir); this_lir != head_lir; this_lir = PREV_LIR(this_lir)) {
buzbeefa57c472012-11-21 12:06:18 -080082 int sink_distance = 0;
buzbee67bf8852011-08-17 17:51:35 -070083
Bill Buzbeea114add2012-05-03 15:00:40 -070084 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -080085 if ((this_lir->flags.is_nop == true) ||
86 is_pseudo_opcode(this_lir->opcode) ||
87 (GetTargetInstFlags(this_lir->opcode) & IS_BRANCH) ||
88 !(GetTargetInstFlags(this_lir->opcode) & (IS_LOAD | IS_STORE))) {
Bill Buzbeea114add2012-05-03 15:00:40 -070089 continue;
90 }
buzbee67bf8852011-08-17 17:51:35 -070091
buzbeefa57c472012-11-21 12:06:18 -080092 int native_reg_id;
93 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -070094 // If x86, location differs depending on whether memory/reg operation.
buzbeefa57c472012-11-21 12:06:18 -080095 native_reg_id = (GetTargetInstFlags(this_lir->opcode) & IS_STORE) ? this_lir->operands[2]
96 : this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -070097 } else {
buzbeefa57c472012-11-21 12:06:18 -080098 native_reg_id = this_lir->operands[0];
buzbeeb046e162012-10-30 15:48:42 -070099 }
buzbeefa57c472012-11-21 12:06:18 -0800100 bool is_this_lir_load = GetTargetInstFlags(this_lir->opcode) & IS_LOAD;
101 LIR* check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 /* Use the mem mask to determine the rough memory location */
buzbeefa57c472012-11-21 12:06:18 -0800103 uint64_t this_mem_mask = (this_lir->use_mask | this_lir->def_mask) & ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700104
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 /*
106 * Currently only eliminate redundant ld/st for constant and Dalvik
107 * register accesses.
108 */
buzbeefa57c472012-11-21 12:06:18 -0800109 if (!(this_mem_mask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) continue;
buzbee67bf8852011-08-17 17:51:35 -0700110
buzbeefa57c472012-11-21 12:06:18 -0800111 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
112 uint64_t stop_use_reg_mask;
113 if (cu->instruction_set == kX86) {
114 stop_use_reg_mask = (IS_BRANCH | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700115 } else {
116 /*
117 * Add pc to the resource mask to prevent this instruction
118 * from sinking past branch instructions. Also take out the memory
buzbeefa57c472012-11-21 12:06:18 -0800119 * region bits since stop_mask is used to check data/control
buzbeeb046e162012-10-30 15:48:42 -0700120 * dependencies.
121 */
buzbeefa57c472012-11-21 12:06:18 -0800122 stop_use_reg_mask = (GetPCUseDefEncoding() | this_lir->use_mask) & ~ENCODE_MEM;
buzbeeb046e162012-10-30 15:48:42 -0700123 }
buzbee67bf8852011-08-17 17:51:35 -0700124
buzbee28c9a832012-11-21 15:39:13 -0800125 for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700126
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 /*
128 * Skip already dead instructions (whose dataflow information is
129 * outdated and misleading).
130 */
buzbeefa57c472012-11-21 12:06:18 -0800131 if (check_lir->flags.is_nop) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700132
buzbeefa57c472012-11-21 12:06:18 -0800133 uint64_t check_mem_mask = (check_lir->use_mask | check_lir->def_mask) & ENCODE_MEM;
134 uint64_t alias_condition = this_mem_mask & check_mem_mask;
135 bool stop_here = false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136
137 /*
138 * Potential aliases seen - check the alias relations
139 */
buzbeefa57c472012-11-21 12:06:18 -0800140 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
141 bool is_check_lir_load = GetTargetInstFlags(check_lir->opcode) & IS_LOAD;
142 if (alias_condition == ENCODE_LITERAL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 /*
144 * Should only see literal loads in the instruction
145 * stream.
146 */
buzbeefa57c472012-11-21 12:06:18 -0800147 DCHECK(!(GetTargetInstFlags(check_lir->opcode) & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 /* Same value && same register type */
buzbeefa57c472012-11-21 12:06:18 -0800149 if (check_lir->alias_info == this_lir->alias_info &&
150 SameRegType(check_lir->operands[0], native_reg_id)) {
buzbee67bf8852011-08-17 17:51:35 -0700151 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 * Different destination register - insert
153 * a move
buzbee67bf8852011-08-17 17:51:35 -0700154 */
buzbeefa57c472012-11-21 12:06:18 -0800155 if (check_lir->operands[0] != native_reg_id) {
156 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
157 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 }
buzbeefa57c472012-11-21 12:06:18 -0800159 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 }
buzbeefa57c472012-11-21 12:06:18 -0800161 } else if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 /* Must alias */
buzbeefa57c472012-11-21 12:06:18 -0800163 if (check_lir->alias_info == this_lir->alias_info) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 /* Only optimize compatible registers */
buzbeefa57c472012-11-21 12:06:18 -0800165 bool reg_compatible = SameRegType(check_lir->operands[0], native_reg_id);
166 if ((is_this_lir_load && is_check_lir_load) ||
167 (!is_this_lir_load && is_check_lir_load)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 /* RAR or RAW */
buzbeefa57c472012-11-21 12:06:18 -0800169 if (reg_compatible) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 /*
171 * Different destination register -
172 * insert a move
173 */
buzbeefa57c472012-11-21 12:06:18 -0800174 if (check_lir->operands[0] !=
175 native_reg_id) {
176 ConvertMemOpIntoMove(cu, check_lir, check_lir->operands[0],
177 native_reg_id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 }
buzbeefa57c472012-11-21 12:06:18 -0800179 check_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 } else {
181 /*
182 * Destinaions are of different types -
183 * something complicated going on so
184 * stop looking now.
185 */
buzbeefa57c472012-11-21 12:06:18 -0800186 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 }
buzbeefa57c472012-11-21 12:06:18 -0800188 } else if (is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 /* WAR - register value is killed */
buzbeefa57c472012-11-21 12:06:18 -0800190 stop_here = true;
191 } else if (!is_this_lir_load && !is_check_lir_load) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 /* WAW - nuke the earlier store */
buzbeefa57c472012-11-21 12:06:18 -0800193 this_lir->flags.is_nop = true;
194 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 }
196 /* Partial overlap */
buzbeefa57c472012-11-21 12:06:18 -0800197 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700198 /*
buzbeefa57c472012-11-21 12:06:18 -0800199 * It is actually ok to continue if check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 * is a read. But it is hard to make a test
201 * case for this so we just stop here to be
202 * conservative.
buzbee67bf8852011-08-17 17:51:35 -0700203 */
buzbeefa57c472012-11-21 12:06:18 -0800204 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 }
buzbee67bf8852011-08-17 17:51:35 -0700206 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800208 if (stop_here) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 break;
buzbeefa57c472012-11-21 12:06:18 -0800210 /* The check_lir has been transformed - check the next one */
211 } else if (check_lir->flags.is_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 continue;
213 }
214 }
215
216
217 /*
218 * this and check LIRs have no memory dependency. Now check if
219 * their register operands have any RAW, WAR, and WAW
220 * dependencies. If so, stop looking.
221 */
buzbeefa57c472012-11-21 12:06:18 -0800222 if (stop_here == false) {
223 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 }
225
buzbeefa57c472012-11-21 12:06:18 -0800226 if (stop_here == true) {
227 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700228 // Prevent stores from being sunk between ops that generate ccodes and
229 // ops that use them.
buzbeefa57c472012-11-21 12:06:18 -0800230 uint64_t flags = GetTargetInstFlags(check_lir->opcode);
231 if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
232 check_lir = PREV_LIR(check_lir);
233 sink_distance--;
buzbeeb046e162012-10-30 15:48:42 -0700234 }
jeffhao573b4292012-07-30 16:37:41 -0700235 }
buzbeefa57c472012-11-21 12:06:18 -0800236 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 /* Only sink store instructions */
buzbeefa57c472012-11-21 12:06:18 -0800238 if (sink_distance && !is_this_lir_load) {
239 LIR* new_store_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
240 *new_store_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 /*
buzbeefa57c472012-11-21 12:06:18 -0800242 * Stop point found - insert *before* the check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 * since the instruction list is scanned in the
244 * top-down order.
245 */
buzbeefa57c472012-11-21 12:06:18 -0800246 InsertLIRBefore(check_lir, new_store_lir);
247 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 }
249 break;
buzbeefa57c472012-11-21 12:06:18 -0800250 } else if (!check_lir->flags.is_nop) {
251 sink_distance++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 }
buzbee67bf8852011-08-17 17:51:35 -0700253 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 }
buzbee67bf8852011-08-17 17:51:35 -0700255}
256
257/*
258 * Perform a pass of bottom-up walk, from the second instruction in the
259 * superblock, to try to hoist loads to earlier slots.
260 */
buzbeefa57c472012-11-21 12:06:18 -0800261void ApplyLoadHoisting(CompilationUnit* cu, LIR* head_lir, LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700262{
buzbeefa57c472012-11-21 12:06:18 -0800263 LIR* this_lir, *check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 /*
265 * Store the list of independent instructions that can be hoisted past.
266 * Will decide the best place to insert later.
267 */
buzbeefa57c472012-11-21 12:06:18 -0800268 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
buzbee67bf8852011-08-17 17:51:35 -0700269
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 /* Empty block */
buzbeefa57c472012-11-21 12:06:18 -0800271 if (head_lir == tail_lir) return;
buzbee67bf8852011-08-17 17:51:35 -0700272
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 /* Start from the second instruction */
buzbee28c9a832012-11-21 15:39:13 -0800274 for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700275
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 /* Skip non-interesting instructions */
buzbeefa57c472012-11-21 12:06:18 -0800277 if ((this_lir->flags.is_nop == true) ||
278 is_pseudo_opcode(this_lir->opcode) ||
279 !(GetTargetInstFlags(this_lir->opcode) & IS_LOAD)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 continue;
281 }
buzbee67bf8852011-08-17 17:51:35 -0700282
buzbeefa57c472012-11-21 12:06:18 -0800283 uint64_t stop_use_all_mask = this_lir->use_mask;
buzbee67bf8852011-08-17 17:51:35 -0700284
buzbeefa57c472012-11-21 12:06:18 -0800285 if (cu->instruction_set != kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700286 /*
287 * Branches for null/range checks are marked with the true resource
288 * bits, and loads to Dalvik registers, constant pools, and non-alias
289 * locations are safe to be hoisted. So only mark the heap references
290 * conservatively here.
291 */
buzbeefa57c472012-11-21 12:06:18 -0800292 if (stop_use_all_mask & ENCODE_HEAP_REF) {
293 stop_use_all_mask |= GetPCUseDefEncoding();
buzbeeb046e162012-10-30 15:48:42 -0700294 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700295 }
buzbee67bf8852011-08-17 17:51:35 -0700296
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 /* Similar as above, but just check for pure register dependency */
buzbeefa57c472012-11-21 12:06:18 -0800298 uint64_t stop_use_reg_mask = stop_use_all_mask & ~ENCODE_MEM;
299 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
buzbee67bf8852011-08-17 17:51:35 -0700300
buzbeefa57c472012-11-21 12:06:18 -0800301 int next_slot = 0;
302 bool stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700303
Bill Buzbeea114add2012-05-03 15:00:40 -0700304 /* Try to hoist the load to a good spot */
buzbee28c9a832012-11-21 15:39:13 -0800305 for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) {
buzbee67bf8852011-08-17 17:51:35 -0700306
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 /*
308 * Skip already dead instructions (whose dataflow information is
309 * outdated and misleading).
310 */
buzbeefa57c472012-11-21 12:06:18 -0800311 if (check_lir->flags.is_nop) continue;
buzbee67bf8852011-08-17 17:51:35 -0700312
buzbeefa57c472012-11-21 12:06:18 -0800313 uint64_t check_mem_mask = check_lir->def_mask & ENCODE_MEM;
314 uint64_t alias_condition = stop_use_all_mask & check_mem_mask;
315 stop_here = false;
buzbee67bf8852011-08-17 17:51:35 -0700316
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 /* Potential WAR alias seen - check the exact relation */
buzbeefa57c472012-11-21 12:06:18 -0800318 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 /* We can fully disambiguate Dalvik references */
buzbeefa57c472012-11-21 12:06:18 -0800320 if (alias_condition == ENCODE_DALVIK_REG) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 /* Must alias or partually overlap */
buzbeefa57c472012-11-21 12:06:18 -0800322 if ((check_lir->alias_info == this_lir->alias_info) ||
323 IsDalvikRegisterClobbered(this_lir, check_lir)) {
324 stop_here = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 }
326 /* Conservatively treat all heap refs as may-alias */
327 } else {
buzbeefa57c472012-11-21 12:06:18 -0800328 DCHECK_EQ(alias_condition, ENCODE_HEAP_REF);
329 stop_here = true;
buzbee67bf8852011-08-17 17:51:35 -0700330 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 /* Memory content may be updated. Stop looking now. */
buzbeefa57c472012-11-21 12:06:18 -0800332 if (stop_here) {
333 prev_inst_list[next_slot++] = check_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 break;
buzbee67bf8852011-08-17 17:51:35 -0700335 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 }
buzbee67bf8852011-08-17 17:51:35 -0700337
buzbeefa57c472012-11-21 12:06:18 -0800338 if (stop_here == false) {
339 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
340 check_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 }
buzbee67bf8852011-08-17 17:51:35 -0700342
Bill Buzbeea114add2012-05-03 15:00:40 -0700343 /*
344 * Store the dependent or non-pseudo/indepedent instruction to the
345 * list.
346 */
buzbeefa57c472012-11-21 12:06:18 -0800347 if (stop_here || !is_pseudo_opcode(check_lir->opcode)) {
348 prev_inst_list[next_slot++] = check_lir;
349 if (next_slot == MAX_HOIST_DISTANCE) break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 }
buzbee67bf8852011-08-17 17:51:35 -0700351
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 /* Found a new place to put the load - move it here */
buzbeefa57c472012-11-21 12:06:18 -0800353 if (stop_here == true) {
354 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 break;
356 }
buzbee67bf8852011-08-17 17:51:35 -0700357 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700358
359 /*
buzbeefa57c472012-11-21 12:06:18 -0800360 * Reached the top - use head_lir as the dependent marker as all labels
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 * are barriers.
362 */
buzbeefa57c472012-11-21 12:06:18 -0800363 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
364 prev_inst_list[next_slot++] = head_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 }
366
367 /*
368 * At least one independent instruction is found. Scan in the reversed
369 * direction to find a beneficial slot.
370 */
buzbeefa57c472012-11-21 12:06:18 -0800371 if (next_slot >= 2) {
372 int first_slot = next_slot - 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 int slot;
buzbeefa57c472012-11-21 12:06:18 -0800374 LIR* dep_lir = prev_inst_list[next_slot-1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
buzbeefa57c472012-11-21 12:06:18 -0800376 if (!is_pseudo_opcode(dep_lir->opcode) &&
377 (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
378 first_slot -= LDLD_DISTANCE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 }
380 /*
buzbeefa57c472012-11-21 12:06:18 -0800381 * Make sure we check slot >= 0 since first_slot may be negative
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 * when the loop is first entered.
383 */
buzbeefa57c472012-11-21 12:06:18 -0800384 for (slot = first_slot; slot >= 0; slot--) {
385 LIR* cur_lir = prev_inst_list[slot];
386 LIR* prev_lir = prev_inst_list[slot+1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700387
388 /* Check the highest instruction */
buzbeefa57c472012-11-21 12:06:18 -0800389 if (prev_lir->def_mask == ENCODE_ALL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 /*
391 * If the first instruction is a load, don't hoist anything
392 * above it since it is unlikely to be beneficial.
393 */
buzbeefa57c472012-11-21 12:06:18 -0800394 if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) continue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700395 /*
396 * If the remaining number of slots is less than LD_LATENCY,
397 * insert the hoisted load here.
398 */
399 if (slot < LD_LATENCY) break;
400 }
401
buzbee8320f382012-09-11 16:29:42 -0700402 // Don't look across a barrier label
buzbeefa57c472012-11-21 12:06:18 -0800403 if ((prev_lir->opcode == kPseudoTargetLabel) ||
404 (prev_lir->opcode == kPseudoSafepointPC) ||
405 (prev_lir->opcode == kPseudoBarrier)) {
buzbee8320f382012-09-11 16:29:42 -0700406 break;
407 }
408
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 * Try to find two instructions with load/use dependency until
411 * the remaining instructions are less than LD_LATENCY.
412 */
buzbeefa57c472012-11-21 12:06:18 -0800413 bool prev_is_load = is_pseudo_opcode(prev_lir->opcode) ? false :
414 (GetTargetInstFlags(prev_lir->opcode) & IS_LOAD);
415 if (((cur_lir->use_mask & prev_lir->def_mask) && prev_is_load) || (slot < LD_LATENCY)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700416 break;
417 }
418 }
419
420 /* Found a slot to hoist to */
421 if (slot >= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800422 LIR* cur_lir = prev_inst_list[slot];
423 LIR* new_load_lir = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
424 *new_load_lir = *this_lir;
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 /*
buzbeefa57c472012-11-21 12:06:18 -0800426 * Insertion is guaranteed to succeed since check_lir
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 * is never the first LIR on the list
428 */
buzbeefa57c472012-11-21 12:06:18 -0800429 InsertLIRBefore(cur_lir, new_load_lir);
430 this_lir->flags.is_nop = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 }
432 }
433 }
buzbee67bf8852011-08-17 17:51:35 -0700434}
435
buzbeefa57c472012-11-21 12:06:18 -0800436void ApplyLocalOptimizations(CompilationUnit* cu, LIR* head_lir,
437 LIR* tail_lir)
buzbee67bf8852011-08-17 17:51:35 -0700438{
buzbeefa57c472012-11-21 12:06:18 -0800439 if (!(cu->disable_opt & (1 << kLoadStoreElimination))) {
440 ApplyLoadStoreElimination(cu, head_lir, tail_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 }
buzbeefa57c472012-11-21 12:06:18 -0800442 if (!(cu->disable_opt & (1 << kLoadHoisting))) {
443 ApplyLoadHoisting(cu, head_lir, tail_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800444 }
445}
446
447/*
448 * Nop any unconditional branches that go to the next instruction.
449 * Note: new redundant branches may be inserted later, and we'll
450 * use a check in final instruction assembly to nop those out.
451 */
buzbeefa57c472012-11-21 12:06:18 -0800452void RemoveRedundantBranches(CompilationUnit* cu)
buzbeecbd6d442012-11-17 14:11:25 -0800453{
buzbeefa57c472012-11-21 12:06:18 -0800454 LIR* this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800455
buzbeefa57c472012-11-21 12:06:18 -0800456 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 -0800457
458 /* Branch to the next instruction */
buzbeefa57c472012-11-21 12:06:18 -0800459 if (BranchUnconditional(this_lir)) {
460 LIR* next_lir = this_lir;
buzbeecbd6d442012-11-17 14:11:25 -0800461
462 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800463 next_lir = NEXT_LIR(next_lir);
buzbeecbd6d442012-11-17 14:11:25 -0800464
465 /*
466 * Is the branch target the next instruction?
467 */
buzbeefa57c472012-11-21 12:06:18 -0800468 if (next_lir == this_lir->target) {
469 this_lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -0800470 break;
471 }
472
473 /*
474 * Found real useful stuff between the branch and the target.
buzbeefa57c472012-11-21 12:06:18 -0800475 * Need to explicitly check the last_lir_insn here because it
buzbeecbd6d442012-11-17 14:11:25 -0800476 * might be the last real instruction.
477 */
buzbeefa57c472012-11-21 12:06:18 -0800478 if (!is_pseudo_opcode(next_lir->opcode) ||
479 (next_lir == cu->last_lir_insn))
buzbeecbd6d442012-11-17 14:11:25 -0800480 break;
481 }
482 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 }
buzbee67bf8852011-08-17 17:51:35 -0700484}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800485
486} // namespace art