blob: b97ff2a4470a8c697f52caec60d347cb140cc980 [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 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +010024#define CHECK_REG_DEP(use, def, check) (def.Intersects(*check->u.m.use_mask)) || \
25 (use.Union(def).Intersects(*check->u.m.def_mask))
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
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) {
buzbeeb48819d2013-09-14 16:15:25 -070033 int reg1Lo = DECODE_ALIAS_INFO_REG(lir1->flags.alias_info);
34 int reg1Hi = reg1Lo + DECODE_ALIAS_INFO_WIDE(lir1->flags.alias_info);
35 int reg2Lo = DECODE_ALIAS_INFO_REG(lir2->flags.alias_info);
36 int reg2Hi = reg2Lo + DECODE_ALIAS_INFO_WIDE(lir2->flags.alias_info);
Brian Carlstrom7940e442013-07-12 13:46:57 -070037
38 return (reg1Lo == reg2Lo) || (reg1Lo == reg2Hi) || (reg1Hi == reg2Lo);
39}
40
41/* Convert a more expensive instruction (ie load) into a move */
buzbee2700f7e2014-03-07 09:46:20 -080042void Mir2Lir::ConvertMemOpIntoMove(LIR* orig_lir, RegStorage dest, RegStorage 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)) {
buzbee409fe942013-10-11 10:49:56 -070081 if (IsPseudoLirOp(this_lir->opcode)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070082 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.
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -080095 // Skip instructions that are neither loads or stores.
96 !(target_flags & (IS_LOAD | IS_STORE)) ||
97 // Skip instructions that do both load and store.
98 ((target_flags & (IS_STORE | IS_LOAD)) == (IS_STORE | IS_LOAD))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 continue;
100 }
101
102 int native_reg_id;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700103 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 // If x86, location differs depending on whether memory/reg operation.
buzbee56c71782013-09-05 17:13:19 -0700105 native_reg_id = (target_flags & IS_STORE) ? this_lir->operands[2] : this_lir->operands[0];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 } else {
107 native_reg_id = this_lir->operands[0];
108 }
buzbee56c71782013-09-05 17:13:19 -0700109 bool is_this_lir_load = target_flags & IS_LOAD;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 LIR* check_lir;
111 /* Use the mem mask to determine the rough memory location */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100112 ResourceMask this_mem_mask = kEncodeMem.Intersection(
113 this_lir->u.m.use_mask->Union(*this_lir->u.m.def_mask));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114
115 /*
116 * Currently only eliminate redundant ld/st for constant and Dalvik
117 * register accesses.
118 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100119 if (!this_mem_mask.Intersects(kEncodeLiteral.Union(kEncodeDalvikReg))) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700120 continue;
121 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100123 ResourceMask stop_def_reg_mask = this_lir->u.m.def_mask->Without(kEncodeMem);
124 ResourceMask stop_use_reg_mask;
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700125 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100126 // TODO: Stop the abuse of kIsBranch as a bit specification for ResourceMask.
127 stop_use_reg_mask = ResourceMask::Bit(kIsBranch).Union(*this_lir->u.m.use_mask).Without(
128 kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 } else {
130 /*
131 * Add pc to the resource mask to prevent this instruction
132 * from sinking past branch instructions. Also take out the memory
133 * region bits since stop_mask is used to check data/control
134 * dependencies.
135 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100136 stop_use_reg_mask = GetPCUseDefEncoding().Union(*this_lir->u.m.use_mask).Without(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 }
138
139 for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 /*
141 * Skip already dead instructions (whose dataflow information is
142 * outdated and misleading).
143 */
buzbee409fe942013-10-11 10:49:56 -0700144 if (check_lir->flags.is_nop || IsPseudoLirOp(check_lir->opcode)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700145 continue;
146 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100148 ResourceMask check_mem_mask = kEncodeMem.Intersection(
149 check_lir->u.m.use_mask->Union(*check_lir->u.m.def_mask));
150 ResourceMask alias_condition = this_mem_mask.Intersection(check_mem_mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 bool stop_here = false;
152
153 /*
154 * Potential aliases seen - check the alias relations
155 */
156 uint64_t check_flags = GetTargetInstFlags(check_lir->opcode);
157 // TUNING: Support instructions with multiple register targets.
158 if ((check_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) {
159 stop_here = true;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100160 } else if (!check_mem_mask.Equals(kEncodeMem) && !alias_condition.Equals(kEncodeNone)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 bool is_check_lir_load = check_flags & IS_LOAD;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100162 if (alias_condition.Equals(kEncodeLiteral)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 /*
164 * Should only see literal loads in the instruction
165 * stream.
166 */
167 DCHECK(!(check_flags & IS_STORE));
168 /* Same value && same register type */
buzbeeb48819d2013-09-14 16:15:25 -0700169 if (check_lir->flags.alias_info == this_lir->flags.alias_info &&
buzbee091cc402014-03-31 10:14:40 -0700170 RegStorage::SameRegType(check_lir->operands[0], native_reg_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 /*
172 * Different destination register - insert
173 * a move
174 */
175 if (check_lir->operands[0] != native_reg_id) {
buzbee2700f7e2014-03-07 09:46:20 -0800176 // TODO: update for 64-bit regs.
177 ConvertMemOpIntoMove(check_lir, RegStorage::Solo32(check_lir->operands[0]),
178 RegStorage::Solo32(native_reg_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 }
buzbee252254b2013-09-08 16:20:53 -0700180 NopLIR(check_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100182 } else if (alias_condition.Equals(kEncodeDalvikReg)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 /* Must alias */
buzbeeb48819d2013-09-14 16:15:25 -0700184 if (check_lir->flags.alias_info == this_lir->flags.alias_info) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 /* Only optimize compatible registers */
buzbee091cc402014-03-31 10:14:40 -0700186 bool reg_compatible = RegStorage::SameRegType(check_lir->operands[0], native_reg_id);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187 if ((is_this_lir_load && is_check_lir_load) ||
188 (!is_this_lir_load && is_check_lir_load)) {
189 /* RAR or RAW */
190 if (reg_compatible) {
191 /*
192 * Different destination register -
193 * insert a move
194 */
buzbee2700f7e2014-03-07 09:46:20 -0800195 if (check_lir->operands[0] != native_reg_id) {
196 // TODO: update for 64-bit regs.
197 ConvertMemOpIntoMove(check_lir, RegStorage::Solo32(check_lir->operands[0]),
198 RegStorage::Solo32(native_reg_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 }
buzbee252254b2013-09-08 16:20:53 -0700200 NopLIR(check_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 } else {
202 /*
203 * Destinaions are of different types -
204 * something complicated going on so
205 * stop looking now.
206 */
207 stop_here = true;
208 }
209 } else if (is_this_lir_load && !is_check_lir_load) {
210 /* WAR - register value is killed */
211 stop_here = true;
212 } else if (!is_this_lir_load && !is_check_lir_load) {
213 /* WAW - nuke the earlier store */
buzbee252254b2013-09-08 16:20:53 -0700214 NopLIR(this_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 stop_here = true;
216 }
217 /* Partial overlap */
218 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
219 /*
220 * It is actually ok to continue if check_lir
221 * is a read. But it is hard to make a test
222 * case for this so we just stop here to be
223 * conservative.
224 */
225 stop_here = true;
226 }
227 }
228 /* Memory content may be updated. Stop looking now. */
229 if (stop_here) {
230 break;
231 /* The check_lir has been transformed - check the next one */
232 } else if (check_lir->flags.is_nop) {
233 continue;
234 }
235 }
236
237
238 /*
239 * this and check LIRs have no memory dependency. Now check if
240 * their register operands have any RAW, WAR, and WAW
241 * dependencies. If so, stop looking.
242 */
243 if (stop_here == false) {
244 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
245 }
246
247 if (stop_here == true) {
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700248 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 // Prevent stores from being sunk between ops that generate ccodes and
250 // ops that use them.
251 uint64_t flags = GetTargetInstFlags(check_lir->opcode);
252 if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
253 check_lir = PREV_LIR(check_lir);
254 sink_distance--;
255 }
256 }
257 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
258 /* Only sink store instructions */
259 if (sink_distance && !is_this_lir_load) {
260 LIR* new_store_lir =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000261 static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 *new_store_lir = *this_lir;
263 /*
264 * Stop point found - insert *before* the check_lir
265 * since the instruction list is scanned in the
266 * top-down order.
267 */
268 InsertLIRBefore(check_lir, new_store_lir);
buzbee252254b2013-09-08 16:20:53 -0700269 NopLIR(this_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 }
271 break;
272 } else if (!check_lir->flags.is_nop) {
273 sink_distance++;
274 }
275 }
276 }
277}
278
279/*
280 * Perform a pass of bottom-up walk, from the second instruction in the
281 * superblock, to try to hoist loads to earlier slots.
282 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700283void Mir2Lir::ApplyLoadHoisting(LIR* head_lir, LIR* tail_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 LIR* this_lir, *check_lir;
285 /*
286 * Store the list of independent instructions that can be hoisted past.
287 * Will decide the best place to insert later.
288 */
289 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
290
291 /* Empty block */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700292 if (head_lir == tail_lir) {
293 return;
294 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295
296 /* Start from the second instruction */
297 for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) {
buzbee409fe942013-10-11 10:49:56 -0700298 if (IsPseudoLirOp(this_lir->opcode)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700299 continue;
300 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301
302 uint64_t target_flags = GetTargetInstFlags(this_lir->opcode);
303 /* Skip non-interesting instructions */
buzbee1da1e2f2013-11-15 13:37:01 -0800304 if (!(target_flags & IS_LOAD) ||
305 (this_lir->flags.is_nop == true) ||
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800306 ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) ||
307 ((target_flags & (IS_STORE | IS_LOAD)) == (IS_STORE | IS_LOAD))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 continue;
309 }
310
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100311 ResourceMask stop_use_all_mask = *this_lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700313 if (cu_->instruction_set != kX86 && cu_->instruction_set != kX86_64) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 /*
315 * Branches for null/range checks are marked with the true resource
316 * bits, and loads to Dalvik registers, constant pools, and non-alias
317 * locations are safe to be hoisted. So only mark the heap references
318 * conservatively here.
319 */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100320 if (stop_use_all_mask.HasBit(ResourceMask::kHeapRef)) {
321 stop_use_all_mask.SetBits(GetPCUseDefEncoding());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 }
323 }
324
325 /* Similar as above, but just check for pure register dependency */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100326 ResourceMask stop_use_reg_mask = stop_use_all_mask.Without(kEncodeMem);
327 ResourceMask stop_def_reg_mask = this_lir->u.m.def_mask->Without(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328
329 int next_slot = 0;
330 bool stop_here = false;
331
332 /* Try to hoist the load to a good spot */
333 for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 /*
335 * Skip already dead instructions (whose dataflow information is
336 * outdated and misleading).
337 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700338 if (check_lir->flags.is_nop) {
339 continue;
340 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100342 ResourceMask check_mem_mask = check_lir->u.m.def_mask->Intersection(kEncodeMem);
343 ResourceMask alias_condition = stop_use_all_mask.Intersection(check_mem_mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 stop_here = false;
345
346 /* Potential WAR alias seen - check the exact relation */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100347 if (!check_mem_mask.Equals(kEncodeMem) && !alias_condition.Equals(kEncodeNone)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 /* We can fully disambiguate Dalvik references */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100349 if (alias_condition.Equals(kEncodeDalvikReg)) {
350 /* Must alias or partially overlap */
buzbeeb48819d2013-09-14 16:15:25 -0700351 if ((check_lir->flags.alias_info == this_lir->flags.alias_info) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 IsDalvikRegisterClobbered(this_lir, check_lir)) {
353 stop_here = true;
354 }
355 /* Conservatively treat all heap refs as may-alias */
356 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100357 DCHECK(alias_condition.Equals(kEncodeHeapRef));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 stop_here = true;
359 }
360 /* Memory content may be updated. Stop looking now. */
361 if (stop_here) {
362 prev_inst_list[next_slot++] = check_lir;
363 break;
364 }
365 }
366
367 if (stop_here == false) {
368 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
369 check_lir);
370 }
371
372 /*
373 * Store the dependent or non-pseudo/indepedent instruction to the
374 * list.
375 */
buzbee409fe942013-10-11 10:49:56 -0700376 if (stop_here || !IsPseudoLirOp(check_lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 prev_inst_list[next_slot++] = check_lir;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700378 if (next_slot == MAX_HOIST_DISTANCE) {
379 break;
380 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 }
382
383 /* Found a new place to put the load - move it here */
384 if (stop_here == true) {
385 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
386 break;
387 }
388 }
389
390 /*
391 * Reached the top - use head_lir as the dependent marker as all labels
392 * are barriers.
393 */
394 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
395 prev_inst_list[next_slot++] = head_lir;
396 }
397
398 /*
399 * At least one independent instruction is found. Scan in the reversed
400 * direction to find a beneficial slot.
401 */
402 if (next_slot >= 2) {
403 int first_slot = next_slot - 2;
404 int slot;
405 LIR* dep_lir = prev_inst_list[next_slot-1];
406 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
buzbee409fe942013-10-11 10:49:56 -0700407 if (!IsPseudoLirOp(dep_lir->opcode) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
409 first_slot -= LDLD_DISTANCE;
410 }
411 /*
412 * Make sure we check slot >= 0 since first_slot may be negative
413 * when the loop is first entered.
414 */
415 for (slot = first_slot; slot >= 0; slot--) {
416 LIR* cur_lir = prev_inst_list[slot];
417 LIR* prev_lir = prev_inst_list[slot+1];
418
419 /* Check the highest instruction */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100420 if (prev_lir->u.m.def_mask->Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 /*
422 * If the first instruction is a load, don't hoist anything
423 * above it since it is unlikely to be beneficial.
424 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700425 if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) {
426 continue;
427 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 /*
429 * If the remaining number of slots is less than LD_LATENCY,
430 * insert the hoisted load here.
431 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700432 if (slot < LD_LATENCY) {
433 break;
434 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 }
436
437 // Don't look across a barrier label
438 if ((prev_lir->opcode == kPseudoTargetLabel) ||
439 (prev_lir->opcode == kPseudoSafepointPC) ||
440 (prev_lir->opcode == kPseudoBarrier)) {
441 break;
442 }
443
444 /*
445 * Try to find two instructions with load/use dependency until
446 * the remaining instructions are less than LD_LATENCY.
447 */
buzbee409fe942013-10-11 10:49:56 -0700448 bool prev_is_load = IsPseudoLirOp(prev_lir->opcode) ? false :
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 (GetTargetInstFlags(prev_lir->opcode) & IS_LOAD);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100450 if ((prev_is_load && (cur_lir->u.m.use_mask->Intersects(*prev_lir->u.m.def_mask))) ||
451 (slot < LD_LATENCY)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 break;
453 }
454 }
455
456 /* Found a slot to hoist to */
457 if (slot >= 0) {
458 LIR* cur_lir = prev_inst_list[slot];
459 LIR* new_load_lir =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000460 static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 *new_load_lir = *this_lir;
462 /*
463 * Insertion is guaranteed to succeed since check_lir
464 * is never the first LIR on the list
465 */
466 InsertLIRBefore(cur_lir, new_load_lir);
buzbee252254b2013-09-08 16:20:53 -0700467 NopLIR(this_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468 }
469 }
470 }
471}
472
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700473void Mir2Lir::ApplyLocalOptimizations(LIR* head_lir, LIR* tail_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 if (!(cu_->disable_opt & (1 << kLoadStoreElimination))) {
475 ApplyLoadStoreElimination(head_lir, tail_lir);
476 }
477 if (!(cu_->disable_opt & (1 << kLoadHoisting))) {
478 ApplyLoadHoisting(head_lir, tail_lir);
479 }
480}
481
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482} // namespace art