blob: 289315747a350f125c0615fcc1ab732a83d2fc87 [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/*
Bill Buzbeec32447b2014-07-27 17:49:42 +000056 * Perform a pass of top-down walk, from the second-last instruction in the
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 * 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 *
Bill Buzbeec32447b2014-07-27 17:49:42 +000069 * A later store can be eliminated by an earlier store iff
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 * 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) {
Bill Buzbeec32447b2014-07-27 17:49:42 +000074 LIR* this_lir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070075
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070076 if (head_lir == tail_lir) {
77 return;
78 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070079
Bill Buzbeec32447b2014-07-27 17:49:42 +000080 for (this_lir = PREV_LIR(tail_lir); this_lir != head_lir; this_lir = PREV_LIR(this_lir)) {
81 if (IsPseudoLirOp(this_lir->opcode)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070082 continue;
83 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070084
Bill Buzbeec32447b2014-07-27 17:49:42 +000085 int sink_distance = 0;
86
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 uint64_t target_flags = GetTargetInstFlags(this_lir->opcode);
Bill Buzbeec32447b2014-07-27 17:49:42 +000088
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 // 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))) {
Serban Constantinescufcc36ba2014-07-15 17:44:21 +010099 continue;
100 }
Bill Buzbeec32447b2014-07-27 17:49:42 +0000101
102 int native_reg_id;
103 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
104 // If x86, location differs depending on whether memory/reg operation.
105 native_reg_id = (target_flags & IS_STORE) ? this_lir->operands[2] : this_lir->operands[0];
106 } else {
107 native_reg_id = this_lir->operands[0];
108 }
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100109 bool is_this_lir_load = target_flags & IS_LOAD;
Bill Buzbeec32447b2014-07-27 17:49:42 +0000110 LIR* check_lir;
111 /* Use the mem mask to determine the rough memory location */
112 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
Bill Buzbeec32447b2014-07-27 17:49:42 +0000115 /*
116 * Currently only eliminate redundant ld/st for constant and Dalvik
117 * register accesses.
118 */
119 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);
Andreas Gampeaf263df2014-07-11 16:40:54 -0700124
Bill Buzbeec32447b2014-07-27 17:49:42 +0000125 /*
126 * Add pc to the resource mask to prevent this instruction
127 * from sinking past branch instructions. Also take out the memory
128 * region bits since stop_mask is used to check data/control
129 * dependencies.
130 *
131 * Note: on x86(-64) and Arm64 we use the IsBranch bit, as the PC is not exposed.
132 */
133 ResourceMask pc_encoding = GetPCUseDefEncoding();
134 if (pc_encoding == kEncodeNone) {
135 // TODO: Stop the abuse of kIsBranch as a bit specification for ResourceMask.
136 pc_encoding = ResourceMask::Bit(kIsBranch);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 }
Bill Buzbeec32447b2014-07-27 17:49:42 +0000138 ResourceMask stop_use_reg_mask = pc_encoding.Union(*this_lir->u.m.use_mask).
139 Without(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140
141 for (check_lir = NEXT_LIR(this_lir); check_lir != tail_lir; check_lir = NEXT_LIR(check_lir)) {
Bill Buzbeec32447b2014-07-27 17:49:42 +0000142 /*
143 * Skip already dead instructions (whose dataflow information is
144 * outdated and misleading).
145 */
buzbee409fe942013-10-11 10:49:56 -0700146 if (check_lir->flags.is_nop || IsPseudoLirOp(check_lir->opcode)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700147 continue;
148 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149
Bill Buzbeec32447b2014-07-27 17:49:42 +0000150 ResourceMask check_mem_mask = kEncodeMem.Intersection(
151 check_lir->u.m.use_mask->Union(*check_lir->u.m.def_mask));
152 ResourceMask alias_condition = this_mem_mask.Intersection(check_mem_mask);
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100153 bool stop_here = false;
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100154
Bill Buzbeec32447b2014-07-27 17:49:42 +0000155 /*
156 * Potential aliases seen - check the alias relations
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100157 */
Bill Buzbeec32447b2014-07-27 17:49:42 +0000158 uint64_t check_flags = GetTargetInstFlags(check_lir->opcode);
159 // TUNING: Support instructions with multiple register targets.
160 if ((check_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 stop_here = true;
Bill Buzbeec32447b2014-07-27 17:49:42 +0000162 } else if (!check_mem_mask.Equals(kEncodeMem) && !alias_condition.Equals(kEncodeNone)) {
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100163 bool is_check_lir_load = check_flags & IS_LOAD;
Bill Buzbeec32447b2014-07-27 17:49:42 +0000164 if (alias_condition.Equals(kEncodeLiteral)) {
165 /*
166 * Should only see literal loads in the instruction
167 * stream.
168 */
169 DCHECK(!(check_flags & IS_STORE));
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100170 /* Same value && same register type */
Bill Buzbeec32447b2014-07-27 17:49:42 +0000171 if (check_lir->flags.alias_info == this_lir->flags.alias_info &&
172 RegStorage::SameRegType(check_lir->operands[0], native_reg_id)) {
173 /*
174 * Different destination register - insert
175 * a move
176 */
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100177 if (check_lir->operands[0] != native_reg_id) {
Bill Buzbeec32447b2014-07-27 17:49:42 +0000178 // TODO: update for 64-bit regs.
179 ConvertMemOpIntoMove(check_lir, RegStorage::Solo32(check_lir->operands[0]),
180 RegStorage::Solo32(native_reg_id));
181 }
182 NopLIR(check_lir);
183 }
184 } else if (alias_condition.Equals(kEncodeDalvikReg)) {
185 /* Must alias */
186 if (check_lir->flags.alias_info == this_lir->flags.alias_info) {
187 /* Only optimize compatible registers */
188 bool reg_compatible = RegStorage::SameRegType(check_lir->operands[0], native_reg_id);
189 if ((is_this_lir_load && is_check_lir_load) ||
190 (!is_this_lir_load && is_check_lir_load)) {
191 /* RAR or RAW */
192 if (reg_compatible) {
193 /*
194 * Different destination register -
195 * insert a move
196 */
197 if (check_lir->operands[0] != native_reg_id) {
198 // TODO: update for 64-bit regs.
199 ConvertMemOpIntoMove(check_lir, RegStorage::Solo32(check_lir->operands[0]),
200 RegStorage::Solo32(native_reg_id));
201 }
202 NopLIR(check_lir);
203 } else {
204 /*
205 * Destinaions are of different types -
206 * something complicated going on so
207 * stop looking now.
208 */
209 stop_here = true;
210 }
211 } else if (is_this_lir_load && !is_check_lir_load) {
212 /* WAR - register value is killed */
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100213 stop_here = true;
Bill Buzbeec32447b2014-07-27 17:49:42 +0000214 } else if (!is_this_lir_load && !is_check_lir_load) {
215 /* WAW - nuke the earlier store */
216 NopLIR(this_lir);
217 stop_here = true;
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100218 }
Bill Buzbeec32447b2014-07-27 17:49:42 +0000219 /* Partial overlap */
220 } else if (IsDalvikRegisterClobbered(this_lir, check_lir)) {
221 /*
222 * It is actually ok to continue if check_lir
223 * is a read. But it is hard to make a test
224 * case for this so we just stop here to be
225 * conservative.
226 */
227 stop_here = true;
Serban Constantinescufcc36ba2014-07-15 17:44:21 +0100228 }
229 }
Bill Buzbeec32447b2014-07-27 17:49:42 +0000230 /* Memory content may be updated. Stop looking now. */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 if (stop_here) {
232 break;
Bill Buzbeec32447b2014-07-27 17:49:42 +0000233 /* The check_lir has been transformed - check the next one */
234 } else if (check_lir->flags.is_nop) {
235 continue;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 }
Bill Buzbeec32447b2014-07-27 17:49:42 +0000237 }
238
239
240 /*
241 * this and check LIRs have no memory dependency. Now check if
242 * their register operands have any RAW, WAR, and WAW
243 * dependencies. If so, stop looking.
244 */
245 if (stop_here == false) {
246 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask, check_lir);
247 }
248
249 if (stop_here == true) {
250 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
251 // Prevent stores from being sunk between ops that generate ccodes and
252 // ops that use them.
253 uint64_t flags = GetTargetInstFlags(check_lir->opcode);
254 if (sink_distance > 0 && (flags & IS_BRANCH) && (flags & USES_CCODES)) {
255 check_lir = PREV_LIR(check_lir);
256 sink_distance--;
257 }
258 }
259 DEBUG_OPT(dump_dependent_insn_pair(this_lir, check_lir, "REG CLOBBERED"));
260 /* Only sink store instructions */
261 if (sink_distance && !is_this_lir_load) {
262 LIR* new_store_lir =
263 static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
264 *new_store_lir = *this_lir;
265 /*
266 * Stop point found - insert *before* the check_lir
267 * since the instruction list is scanned in the
268 * top-down order.
269 */
270 InsertLIRBefore(check_lir, new_store_lir);
271 NopLIR(this_lir);
272 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 break;
Bill Buzbeec32447b2014-07-27 17:49:42 +0000274 } else if (!check_lir->flags.is_nop) {
275 sink_distance++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 }
277 }
278 }
279}
280
281/*
282 * Perform a pass of bottom-up walk, from the second instruction in the
283 * superblock, to try to hoist loads to earlier slots.
284 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700285void Mir2Lir::ApplyLoadHoisting(LIR* head_lir, LIR* tail_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 LIR* this_lir, *check_lir;
287 /*
288 * Store the list of independent instructions that can be hoisted past.
289 * Will decide the best place to insert later.
290 */
291 LIR* prev_inst_list[MAX_HOIST_DISTANCE];
292
293 /* Empty block */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700294 if (head_lir == tail_lir) {
295 return;
296 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297
298 /* Start from the second instruction */
299 for (this_lir = NEXT_LIR(head_lir); this_lir != tail_lir; this_lir = NEXT_LIR(this_lir)) {
buzbee409fe942013-10-11 10:49:56 -0700300 if (IsPseudoLirOp(this_lir->opcode)) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700301 continue;
302 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303
304 uint64_t target_flags = GetTargetInstFlags(this_lir->opcode);
305 /* Skip non-interesting instructions */
buzbee1da1e2f2013-11-15 13:37:01 -0800306 if (!(target_flags & IS_LOAD) ||
307 (this_lir->flags.is_nop == true) ||
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800308 ((target_flags & (REG_DEF0 | REG_DEF1)) == (REG_DEF0 | REG_DEF1)) ||
309 ((target_flags & (IS_STORE | IS_LOAD)) == (IS_STORE | IS_LOAD))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 continue;
311 }
312
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100313 ResourceMask stop_use_all_mask = *this_lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314
Andreas Gampeaf263df2014-07-11 16:40:54 -0700315 /*
316 * Branches for null/range checks are marked with the true resource
317 * bits, and loads to Dalvik registers, constant pools, and non-alias
318 * locations are safe to be hoisted. So only mark the heap references
319 * conservatively here.
320 *
321 * Note: on x86(-64) and Arm64 this will add kEncodeNone.
322 * TODO: Sanity check. LoadStoreElimination uses kBranchBit to fake a PC.
323 */
324 if (stop_use_all_mask.HasBit(ResourceMask::kHeapRef)) {
325 stop_use_all_mask.SetBits(GetPCUseDefEncoding());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 }
327
328 /* Similar as above, but just check for pure register dependency */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100329 ResourceMask stop_use_reg_mask = stop_use_all_mask.Without(kEncodeMem);
330 ResourceMask stop_def_reg_mask = this_lir->u.m.def_mask->Without(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331
332 int next_slot = 0;
333 bool stop_here = false;
334
335 /* Try to hoist the load to a good spot */
336 for (check_lir = PREV_LIR(this_lir); check_lir != head_lir; check_lir = PREV_LIR(check_lir)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 /*
338 * Skip already dead instructions (whose dataflow information is
339 * outdated and misleading).
340 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700341 if (check_lir->flags.is_nop) {
342 continue;
343 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100345 ResourceMask check_mem_mask = check_lir->u.m.def_mask->Intersection(kEncodeMem);
346 ResourceMask alias_condition = stop_use_all_mask.Intersection(check_mem_mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700347 stop_here = false;
348
349 /* Potential WAR alias seen - check the exact relation */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100350 if (!check_mem_mask.Equals(kEncodeMem) && !alias_condition.Equals(kEncodeNone)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 /* We can fully disambiguate Dalvik references */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100352 if (alias_condition.Equals(kEncodeDalvikReg)) {
353 /* Must alias or partially overlap */
buzbeeb48819d2013-09-14 16:15:25 -0700354 if ((check_lir->flags.alias_info == this_lir->flags.alias_info) ||
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 IsDalvikRegisterClobbered(this_lir, check_lir)) {
356 stop_here = true;
357 }
358 /* Conservatively treat all heap refs as may-alias */
359 } else {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100360 DCHECK(alias_condition.Equals(kEncodeHeapRef));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 stop_here = true;
362 }
363 /* Memory content may be updated. Stop looking now. */
364 if (stop_here) {
365 prev_inst_list[next_slot++] = check_lir;
366 break;
367 }
368 }
369
370 if (stop_here == false) {
371 stop_here = CHECK_REG_DEP(stop_use_reg_mask, stop_def_reg_mask,
372 check_lir);
373 }
374
375 /*
376 * Store the dependent or non-pseudo/indepedent instruction to the
377 * list.
378 */
buzbee409fe942013-10-11 10:49:56 -0700379 if (stop_here || !IsPseudoLirOp(check_lir->opcode)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380 prev_inst_list[next_slot++] = check_lir;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700381 if (next_slot == MAX_HOIST_DISTANCE) {
382 break;
383 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 }
385
386 /* Found a new place to put the load - move it here */
387 if (stop_here == true) {
Bill Buzbeec32447b2014-07-27 17:49:42 +0000388 DEBUG_OPT(dump_dependent_insn_pair(check_lir, this_lir "HOIST STOP"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 break;
390 }
391 }
392
393 /*
394 * Reached the top - use head_lir as the dependent marker as all labels
395 * are barriers.
396 */
397 if (stop_here == false && next_slot < MAX_HOIST_DISTANCE) {
398 prev_inst_list[next_slot++] = head_lir;
399 }
400
401 /*
402 * At least one independent instruction is found. Scan in the reversed
403 * direction to find a beneficial slot.
404 */
405 if (next_slot >= 2) {
406 int first_slot = next_slot - 2;
407 int slot;
408 LIR* dep_lir = prev_inst_list[next_slot-1];
409 /* If there is ld-ld dependency, wait LDLD_DISTANCE cycles */
buzbee409fe942013-10-11 10:49:56 -0700410 if (!IsPseudoLirOp(dep_lir->opcode) &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 (GetTargetInstFlags(dep_lir->opcode) & IS_LOAD)) {
412 first_slot -= LDLD_DISTANCE;
413 }
414 /*
415 * Make sure we check slot >= 0 since first_slot may be negative
416 * when the loop is first entered.
417 */
418 for (slot = first_slot; slot >= 0; slot--) {
419 LIR* cur_lir = prev_inst_list[slot];
420 LIR* prev_lir = prev_inst_list[slot+1];
421
422 /* Check the highest instruction */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100423 if (prev_lir->u.m.def_mask->Equals(kEncodeAll)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 /*
425 * If the first instruction is a load, don't hoist anything
426 * above it since it is unlikely to be beneficial.
427 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700428 if (GetTargetInstFlags(cur_lir->opcode) & IS_LOAD) {
429 continue;
430 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 /*
432 * If the remaining number of slots is less than LD_LATENCY,
433 * insert the hoisted load here.
434 */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700435 if (slot < LD_LATENCY) {
436 break;
437 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 }
439
440 // Don't look across a barrier label
441 if ((prev_lir->opcode == kPseudoTargetLabel) ||
442 (prev_lir->opcode == kPseudoSafepointPC) ||
443 (prev_lir->opcode == kPseudoBarrier)) {
444 break;
445 }
446
447 /*
448 * Try to find two instructions with load/use dependency until
449 * the remaining instructions are less than LD_LATENCY.
450 */
buzbee409fe942013-10-11 10:49:56 -0700451 bool prev_is_load = IsPseudoLirOp(prev_lir->opcode) ? false :
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 (GetTargetInstFlags(prev_lir->opcode) & IS_LOAD);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100453 if ((prev_is_load && (cur_lir->u.m.use_mask->Intersects(*prev_lir->u.m.def_mask))) ||
454 (slot < LD_LATENCY)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 break;
456 }
457 }
458
459 /* Found a slot to hoist to */
460 if (slot >= 0) {
461 LIR* cur_lir = prev_inst_list[slot];
462 LIR* new_load_lir =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000463 static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 *new_load_lir = *this_lir;
465 /*
466 * Insertion is guaranteed to succeed since check_lir
467 * is never the first LIR on the list
468 */
469 InsertLIRBefore(cur_lir, new_load_lir);
buzbee252254b2013-09-08 16:20:53 -0700470 NopLIR(this_lir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 }
472 }
473 }
474}
475
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700476void Mir2Lir::ApplyLocalOptimizations(LIR* head_lir, LIR* tail_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 if (!(cu_->disable_opt & (1 << kLoadStoreElimination))) {
478 ApplyLoadStoreElimination(head_lir, tail_lir);
479 }
480 if (!(cu_->disable_opt & (1 << kLoadHoisting))) {
481 ApplyLoadHoisting(head_lir, tail_lir);
482 }
483}
484
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485} // namespace art