blob: cb7694de6870a443cfd9bfefc840ae86632335ef [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -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
17#include "dex/compiler_internals.h"
18
19namespace art {
20
21#define DEBUG_OPT(X)
22
23/* Check RAW, WAR, and RAW dependency on the register operands */
24#define CHECK_REG_DEP(use, def, check) ((def & check->use_mask) || \
25 ((use | def) & check->def_mask))
26
27/* Scheduler heuristics */
28#define MAX_HOIST_DISTANCE 20
29#define LDLD_DISTANCE 4
30#define LD_LATENCY 2
31
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070032static bool IsDalvikRegisterClobbered(LIR* lir1, LIR* lir2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070033 int reg1Lo = DECODE_ALIAS_INFO_REG(lir1->alias_info);
34 int reg1Hi = reg1Lo + DECODE_ALIAS_INFO_WIDE(lir1->alias_info);
35 int reg2Lo = DECODE_ALIAS_INFO_REG(lir2->alias_info);
36 int reg2Hi = reg2Lo + DECODE_ALIAS_INFO_WIDE(lir2->alias_info);
37
38 return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo);
39}
40
41/* Convert a more expensive instruction (ie load) into a move */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070042void Mir2Lir::ConvertMemOpIntoMove(LIR* orig_lir, int dest, int src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 /* Insert a move to replace the load */
44 LIR* move_lir;
45 move_lir = OpRegCopyNoInsert(dest, src);
46 /*
47 * Insert the converted instruction after the original since the
48 * optimization is scannng in the top-down order and the new instruction
49 * will need to be re-checked (eg the new dest clobbers the src used in
50 * this_lir).
51 */
52 InsertLIRAfter(orig_lir, move_lir);
53}
54
55/*
56 * Perform a pass of top-down walk, from the second-last instruction in the
57 * superblock, to eliminate redundant loads and stores.
58 *
59 * An earlier load can eliminate a later load iff
60 * 1) They are must-aliases
61 * 2) The native register is not clobbered in between
62 * 3) The memory location is not written to in between
63 *
64 * An earlier store can eliminate a later load iff
65 * 1) They are must-aliases
66 * 2) The native register is not clobbered in between
67 * 3) The memory location is not written to in between
68 *
69 * A later store can be eliminated by an earlier store iff
70 * 1) They are must-aliases
71 * 2) The memory location is not written to in between
72 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070073void Mir2Lir::ApplyLoadStoreElimination(LIR* head_lir, LIR* tail_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 LIR* this_lir;
75
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070076 if (head_lir == tail_lir) {
77 return;
78 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070079
80 for (this_lir = PREV_LIR(tail_lir); this_lir != head_lir; this_lir = PREV_LIR(this_lir)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070081 if (is_pseudo_opcode(this_lir->opcode)) {
82 continue;
83 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070084
85 int sink_distance = 0;
86
87 uint64_t target_flags = GetTargetInstFlags(this_lir->opcode);
88
89 /* Skip non-interesting instructions */
90 if ((this_lir->flags.is_nop == true) ||
91 (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))) {
96 continue;
97 }
98
99 int native_reg_id;
100 if (cu_->instruction_set == kX86) {
101 // If x86, location differs depending on whether memory/reg operation.
buzbee56c71782013-09-05 17:13:19 -0700102 native_reg_id = (target_flags & IS_STORE) ? this_lir->operands[2] : this_lir->operands[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 } else {
104 native_reg_id = this_lir->operands[0];
105 }
buzbee56c71782013-09-05 17:13:19 -0700106 bool is_this_lir_load = target_flags & IS_LOAD;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 LIR* check_lir;
108 /* Use the mem mask to determine the rough memory location */
109 uint64_t this_mem_mask = (this_lir->use_mask | this_lir->def_mask) & ENCODE_MEM;
110
111 /*
112 * Currently only eliminate redundant ld/st for constant and Dalvik
113 * register accesses.
114 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700115 if (!(this_mem_mask & (ENCODE_LITERAL | ENCODE_DALVIK_REG))) {
116 continue;
117 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118
119 uint64_t stop_def_reg_mask = this_lir->def_mask & ~ENCODE_MEM;
120 uint64_t stop_use_reg_mask;
121 if (cu_->instruction_set == kX86) {
122 stop_use_reg_mask = (IS_BRANCH | this_lir->use_mask) & ~ENCODE_MEM;
123 } else {
124 /*
125 * Add pc to the resource mask to prevent this instruction
126 * from sinking past branch instructions. Also take out the memory
127 * region bits since stop_mask is used to check data/control
128 * dependencies.
129 */
130 stop_use_reg_mask = (GetPCUseDefEncoding() | this_lir->use_mask) & ~ENCODE_MEM;
131 }
132
133 for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 /*
135 * Skip already dead instructions (whose dataflow information is
136 * outdated and misleading).
137 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700138 if (check_lir->flags.is_nop || is_pseudo_opcode(check_lir->opcode)) {
139 continue;
140 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141
142 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;
145
146 /*
147 * Potential aliases seen - check the alias relations
148 */
149 uint64_t check_flags = 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;
155 if (alias_condition == ENCODE_LITERAL) {
156 /*
157 * Should only see literal loads in the instruction
158 * stream.
159 */
160 DCHECK(!(check_flags & IS_STORE));
161 /* Same value && same register type */
162 if (check_lir->alias_info == this_lir->alias_info &&
163 SameRegType(check_lir->operands[0], native_reg_id)) {
164 /*
165 * Different destination register - insert
166 * a move
167 */
168 if (check_lir->operands[0] != native_reg_id) {
169 ConvertMemOpIntoMove(check_lir, check_lir->operands[0], native_reg_id);
170 }
buzbee252254b2013-09-08 16:20:53 -0700171 NopLIR(check_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 }
173 } else if (alias_condition == ENCODE_DALVIK_REG) {
174 /* Must alias */
175 if (check_lir->alias_info == this_lir->alias_info) {
176 /* Only optimize compatible registers */
177 bool reg_compatible = SameRegType(check_lir->operands[0], native_reg_id);
178 if ((is_this_lir_load && is_check_lir_load) ||
179 (!is_this_lir_load && is_check_lir_load)) {
180 /* RAR or RAW */
181 if (reg_compatible) {
182 /*
183 * Different destination register -
184 * insert a move
185 */
186 if (check_lir->operands[0] !=
187 native_reg_id) {
188 ConvertMemOpIntoMove(check_lir, check_lir->operands[0], native_reg_id);
189 }
buzbee252254b2013-09-08 16:20:53 -0700190 NopLIR(check_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 } else {
192 /*
193 * Destinaions are of different types -
194 * something complicated going on so
195 * stop looking now.
196 */
197 stop_here = true;
198 }
199 } else if (is_this_lir_load && !is_check_lir_load) {
200 /* WAR - register value is killed */
201 stop_here = true;
202 } else if (!is_this_lir_load && !is_check_lir_load) {
203 /* WAW - nuke the earlier store */
buzbee252254b2013-09-08 16:20:53 -0700204 NopLIR(this_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 stop_here = true;
206 }
207 /* Partial overlap */
208 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
209 /*
210 * It is actually ok to continue if check_lir
211 * is a read. But it is hard to make a test
212 * case for this so we just stop here to be
213 * conservative.
214 */
215 stop_here = true;
216 }
217 }
218 /* Memory content may be updated. Stop looking now. */
219 if (stop_here) {
220 break;
221 /* The check_lir has been transformed - check the next one */
222 } else if (check_lir->flags.is_nop) {
223 continue;
224 }
225 }
226
227
228 /*
229 * this and check LIRs have no memory dependency. Now check if
230 * their register operands have any RAW, WAR, and WAW
231 * dependencies. If so, stop looking.
232 */
233 if (stop_here == false) {
234 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
235 }
236
237 if (stop_here == true) {
238 if (cu_->instruction_set == kX86) {
239 // Prevent stores from being sunk between ops that generate ccodes and
240 // ops that use them.
241 uint64_t flags = GetTargetInstFlags(check_lir->opcode);
242 if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
243 check_lir = PREV_LIR(check_lir);
244 sink_distance--;
245 }
246 }
247 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
248 /* Only sink store instructions */
249 if (sink_distance && !is_this_lir_load) {
250 LIR* new_store_lir =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700251 static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 *new_store_lir = *this_lir;
253 /*
254 * Stop point found - insert *before* the check_lir
255 * since the instruction list is scanned in the
256 * top-down order.
257 */
258 InsertLIRBefore(check_lir, new_store_lir);
buzbee252254b2013-09-08 16:20:53 -0700259 NopLIR(this_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 }
261 break;
262 } else if (!check_lir->flags.is_nop) {
263 sink_distance++;
264 }
265 }
266 }
267}
268
269/*
270 * Perform a pass of bottom-up walk, from the second instruction in the
271 * superblock, to try to hoist loads to earlier slots.
272 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273void Mir2Lir::ApplyLoadHoisting(LIR* head_lir, LIR* tail_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 LIR* this_lir, *check_lir;
275 /*
276 * Store the list of independent instructions that can be hoisted past.
277 * Will decide the best place to insert later.
278 */
279 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
280
281 /* Empty block */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700282 if (head_lir == tail_lir) {
283 return;
284 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285
286 /* Start from the second instruction */
287 for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700288 if (is_pseudo_opcode(this_lir->opcode)) {
289 continue;
290 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291
292 uint64_t target_flags = GetTargetInstFlags(this_lir->opcode);
293 /* Skip non-interesting instructions */
294 if ((this_lir->flags.is_nop == true) ||
295 ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) ||
296 !(target_flags & IS_LOAD)) {
297 continue;
298 }
299
300 uint64_t stop_use_all_mask = this_lir->use_mask;
301
302 if (cu_->instruction_set != kX86) {
303 /*
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 */
309 if (stop_use_all_mask & ENCODE_HEAP_REF) {
310 stop_use_all_mask |= GetPCUseDefEncoding();
311 }
312 }
313
314 /* Similar as above, but just check for pure register dependency */
315 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;
317
318 int next_slot = 0;
319 bool stop_here = false;
320
321 /* Try to hoist the load to a good spot */
322 for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 /*
324 * Skip already dead instructions (whose dataflow information is
325 * outdated and misleading).
326 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700327 if (check_lir->flags.is_nop) {
328 continue;
329 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330
331 uint64_t check_mem_mask = check_lir->def_mask & ENCODE_MEM;
332 uint64_t alias_condition = stop_use_all_mask & check_mem_mask;
333 stop_here = false;
334
335 /* Potential WAR alias seen - check the exact relation */
336 if (check_mem_mask != ENCODE_MEM && alias_condition != 0) {
337 /* We can fully disambiguate Dalvik references */
338 if (alias_condition == ENCODE_DALVIK_REG) {
339 /* Must alias or partually overlap */
340 if ((check_lir->alias_info == this_lir->alias_info) ||
341 IsDalvikRegisterClobbered(this_lir, check_lir)) {
342 stop_here = true;
343 }
344 /* Conservatively treat all heap refs as may-alias */
345 } else {
346 DCHECK_EQ(alias_condition, ENCODE_HEAP_REF);
347 stop_here = true;
348 }
349 /* Memory content may be updated. Stop looking now. */
350 if (stop_here) {
351 prev_inst_list[next_slot++] = check_lir;
352 break;
353 }
354 }
355
356 if (stop_here == false) {
357 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
358 check_lir);
359 }
360
361 /*
362 * Store the dependent or non-pseudo/indepedent instruction to the
363 * list.
364 */
365 if (stop_here || !is_pseudo_opcode(check_lir->opcode)) {
366 prev_inst_list[next_slot++] = check_lir;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700367 if (next_slot == MAX_HOIST_DISTANCE) {
368 break;
369 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 }
371
372 /* Found a new place to put the load - move it here */
373 if (stop_here == true) {
374 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
375 break;
376 }
377 }
378
379 /*
380 * Reached the top - use head_lir as the dependent marker as all labels
381 * are barriers.
382 */
383 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
384 prev_inst_list[next_slot++] = head_lir;
385 }
386
387 /*
388 * At least one independent instruction is found. Scan in the reversed
389 * direction to find a beneficial slot.
390 */
391 if (next_slot >= 2) {
392 int first_slot = next_slot - 2;
393 int slot;
394 LIR* dep_lir = prev_inst_list[next_slot-1];
395 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
396 if (!is_pseudo_opcode(dep_lir->opcode) &&
397 (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
398 first_slot -= LDLD_DISTANCE;
399 }
400 /*
401 * Make sure we check slot >= 0 since first_slot may be negative
402 * when the loop is first entered.
403 */
404 for (slot = first_slot; slot >= 0; slot--) {
405 LIR* cur_lir = prev_inst_list[slot];
406 LIR* prev_lir = prev_inst_list[slot+1];
407
408 /* Check the highest instruction */
409 if (prev_lir->def_mask == ENCODE_ALL) {
410 /*
411 * If the first instruction is a load, don't hoist anything
412 * above it since it is unlikely to be beneficial.
413 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700414 if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) {
415 continue;
416 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 /*
418 * If the remaining number of slots is less than LD_LATENCY,
419 * insert the hoisted load here.
420 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700421 if (slot < LD_LATENCY) {
422 break;
423 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 }
425
426 // Don't look across a barrier label
427 if ((prev_lir->opcode == kPseudoTargetLabel) ||
428 (prev_lir->opcode == kPseudoSafepointPC) ||
429 (prev_lir->opcode == kPseudoBarrier)) {
430 break;
431 }
432
433 /*
434 * Try to find two instructions with load/use dependency until
435 * the remaining instructions are less than LD_LATENCY.
436 */
437 bool prev_is_load = is_pseudo_opcode(prev_lir->opcode) ? false :
438 (GetTargetInstFlags(prev_lir->opcode) & IS_LOAD);
439 if (((cur_lir->use_mask & prev_lir->def_mask) && prev_is_load) || (slot < LD_LATENCY)) {
440 break;
441 }
442 }
443
444 /* Found a slot to hoist to */
445 if (slot >= 0) {
446 LIR* cur_lir = prev_inst_list[slot];
447 LIR* new_load_lir =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700448 static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 *new_load_lir = *this_lir;
450 /*
451 * Insertion is guaranteed to succeed since check_lir
452 * is never the first LIR on the list
453 */
454 InsertLIRBefore(cur_lir, new_load_lir);
buzbee252254b2013-09-08 16:20:53 -0700455 NopLIR(this_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 }
457 }
458 }
459}
460
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700461void Mir2Lir::ApplyLocalOptimizations(LIR* head_lir, LIR* tail_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 if (!(cu_->disable_opt & (1 << kLoadStoreElimination))) {
463 ApplyLoadStoreElimination(head_lir, tail_lir);
464 }
465 if (!(cu_->disable_opt & (1 << kLoadHoisting))) {
466 ApplyLoadHoisting(head_lir, tail_lir);
467 }
468}
469
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470} // namespace art