blob: 31f5c28316a4710406e4c7d9913bfbcbbf4f2c49 [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#include "dex/dataflow_iterator-inl.h"
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080019#include "dex/quick/dex_file_method_inliner.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "mir_to_lir-inl.h"
21#include "object_utils.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070022#include "thread-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
24namespace art {
25
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080026void Mir2Lir::LockArg(int in_position, bool wide) {
27 int reg_arg_low = GetArgMappingToPhysicalReg(in_position);
28 int reg_arg_high = wide ? GetArgMappingToPhysicalReg(in_position + 1) : INVALID_REG;
29
30 if (reg_arg_low != INVALID_REG) {
31 LockTemp(reg_arg_low);
32 }
33 if (reg_arg_high != INVALID_REG && reg_arg_low != reg_arg_high) {
34 LockTemp(reg_arg_high);
35 }
36}
37
38int Mir2Lir::LoadArg(int in_position, bool wide) {
39 int reg_arg_low = GetArgMappingToPhysicalReg(in_position);
40 int reg_arg_high = wide ? GetArgMappingToPhysicalReg(in_position + 1) : INVALID_REG;
41
42 int offset = StackVisitor::GetOutVROffset(in_position);
43 if (cu_->instruction_set == kX86) {
44 /*
45 * When doing a call for x86, it moves the stack pointer in order to push return.
46 * Thus, we add another 4 bytes to figure out the out of caller (in of callee).
47 * TODO: This needs revisited for 64-bit.
48 */
49 offset += sizeof(uint32_t);
50 }
51
52 // If the VR is wide and there is no register for high part, we need to load it.
53 if (wide && reg_arg_high == INVALID_REG) {
54 // If the low part is not in a reg, we allocate a pair. Otherwise, we just load to high reg.
55 if (reg_arg_low == INVALID_REG) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +000056 RegStorage new_regs = AllocTypedTempWide(false, kAnyReg);
57 reg_arg_low = new_regs.GetReg();
58 reg_arg_high = new_regs.GetHighReg();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080059 LoadBaseDispWide(TargetReg(kSp), offset, reg_arg_low, reg_arg_high, INVALID_SREG);
60 } else {
61 reg_arg_high = AllocTemp();
62 int offset_high = offset + sizeof(uint32_t);
63 LoadWordDisp(TargetReg(kSp), offset_high, reg_arg_high);
64 }
65 }
66
67 // If the low part is not in a register yet, we need to load it.
68 if (reg_arg_low == INVALID_REG) {
69 reg_arg_low = AllocTemp();
70 LoadWordDisp(TargetReg(kSp), offset, reg_arg_low);
71 }
72
73 if (wide) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +000074 // TODO: replace w/ RegStorage.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080075 return ENCODE_REG_PAIR(reg_arg_low, reg_arg_high);
76 } else {
77 return reg_arg_low;
78 }
79}
80
81void Mir2Lir::LoadArgDirect(int in_position, RegLocation rl_dest) {
82 int offset = StackVisitor::GetOutVROffset(in_position);
83 if (cu_->instruction_set == kX86) {
84 /*
85 * When doing a call for x86, it moves the stack pointer in order to push return.
86 * Thus, we add another 4 bytes to figure out the out of caller (in of callee).
87 * TODO: This needs revisited for 64-bit.
88 */
89 offset += sizeof(uint32_t);
90 }
91
92 if (!rl_dest.wide) {
93 int reg = GetArgMappingToPhysicalReg(in_position);
94 if (reg != INVALID_REG) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +000095 OpRegCopy(rl_dest.reg.GetReg(), reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080096 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +000097 LoadWordDisp(TargetReg(kSp), offset, rl_dest.reg.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080098 }
99 } else {
100 int reg_arg_low = GetArgMappingToPhysicalReg(in_position);
101 int reg_arg_high = GetArgMappingToPhysicalReg(in_position + 1);
102
103 if (reg_arg_low != INVALID_REG && reg_arg_high != INVALID_REG) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000104 OpRegCopyWide(rl_dest.reg.GetReg(), rl_dest.reg.GetHighReg(), reg_arg_low, reg_arg_high);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800105 } else if (reg_arg_low != INVALID_REG && reg_arg_high == INVALID_REG) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000106 OpRegCopy(rl_dest.reg.GetReg(), reg_arg_low);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800107 int offset_high = offset + sizeof(uint32_t);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000108 LoadWordDisp(TargetReg(kSp), offset_high, rl_dest.reg.GetHighReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800109 } else if (reg_arg_low == INVALID_REG && reg_arg_high != INVALID_REG) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000110 OpRegCopy(rl_dest.reg.GetHighReg(), reg_arg_high);
111 LoadWordDisp(TargetReg(kSp), offset, rl_dest.reg.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800112 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000113 LoadBaseDispWide(TargetReg(kSp), offset, rl_dest.reg.GetReg(), rl_dest.reg.GetHighReg(), INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800114 }
115 }
116}
117
118bool Mir2Lir::GenSpecialIGet(MIR* mir, const InlineMethod& special) {
119 // FastInstance() already checked by DexFileMethodInliner.
120 const InlineIGetIPutData& data = special.d.ifield_data;
121 if (data.method_is_static || data.object_arg != 0) {
122 // The object is not "this" and has to be null-checked.
123 return false;
124 }
125
Vladimir Markoe3e02602014-03-12 15:42:41 +0000126 bool wide = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE));
127 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800128 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
129
130 // Point of no return - no aborts after this
131 GenPrintLabel(mir);
132 LockArg(data.object_arg);
133 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
134 int reg_obj = LoadArg(data.object_arg);
135 if (wide) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000136 LoadBaseDispWide(reg_obj, data.field_offset, rl_dest.reg.GetReg(), rl_dest.reg.GetHighReg(), INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800137 } else {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000138 LoadBaseDisp(reg_obj, data.field_offset, rl_dest.reg.GetReg(), kWord, INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800139 }
140 if (data.is_volatile) {
141 GenMemBarrier(kLoadLoad);
142 }
143 return true;
144}
145
146bool Mir2Lir::GenSpecialIPut(MIR* mir, const InlineMethod& special) {
147 // FastInstance() already checked by DexFileMethodInliner.
148 const InlineIGetIPutData& data = special.d.ifield_data;
149 if (data.method_is_static || data.object_arg != 0) {
150 // The object is not "this" and has to be null-checked.
151 return false;
152 }
153
Vladimir Markoe3e02602014-03-12 15:42:41 +0000154 bool wide = (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800155
156 // Point of no return - no aborts after this
157 GenPrintLabel(mir);
158 LockArg(data.object_arg);
159 LockArg(data.src_arg, wide);
160 int reg_obj = LoadArg(data.object_arg);
161 int reg_src = LoadArg(data.src_arg, wide);
162 if (data.is_volatile) {
163 GenMemBarrier(kStoreStore);
164 }
165 if (wide) {
166 int low_reg, high_reg;
167 DECODE_REG_PAIR(reg_src, low_reg, high_reg);
168 StoreBaseDispWide(reg_obj, data.field_offset, low_reg, high_reg);
169 } else {
170 StoreBaseDisp(reg_obj, data.field_offset, reg_src, kWord);
171 }
172 if (data.is_volatile) {
173 GenMemBarrier(kLoadLoad);
174 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000175 if (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT)) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800176 MarkGCCard(reg_src, reg_obj);
177 }
178 return true;
179}
180
181bool Mir2Lir::GenSpecialIdentity(MIR* mir, const InlineMethod& special) {
182 const InlineReturnArgData& data = special.d.return_data;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000183 bool wide = (data.is_wide != 0u);
184 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800185 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
186
187 // Point of no return - no aborts after this
188 GenPrintLabel(mir);
189 LockArg(data.arg, wide);
190 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
191 LoadArgDirect(data.arg, rl_dest);
192 return true;
193}
194
195/*
196 * Special-case code generation for simple non-throwing leaf methods.
197 */
198bool Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
199 DCHECK(special.flags & kInlineSpecial);
200 current_dalvik_offset_ = mir->offset;
201 MIR* return_mir = nullptr;
202 bool successful = false;
203
204 switch (special.opcode) {
205 case kInlineOpNop:
206 successful = true;
207 DCHECK_EQ(mir->dalvikInsn.opcode, Instruction::RETURN_VOID);
208 return_mir = mir;
209 break;
210 case kInlineOpNonWideConst: {
211 successful = true;
212 RegLocation rl_dest = GetReturn(cu_->shorty[0] == 'F');
213 GenPrintLabel(mir);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000214 LoadConstant(rl_dest.reg.GetReg(), static_cast<int>(special.d.data));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800215 return_mir = mir_graph_->GetNextUnconditionalMir(bb, mir);
216 break;
217 }
218 case kInlineOpReturnArg:
219 successful = GenSpecialIdentity(mir, special);
220 return_mir = mir;
221 break;
222 case kInlineOpIGet:
223 successful = GenSpecialIGet(mir, special);
224 return_mir = mir_graph_->GetNextUnconditionalMir(bb, mir);
225 break;
226 case kInlineOpIPut:
227 successful = GenSpecialIPut(mir, special);
228 return_mir = mir_graph_->GetNextUnconditionalMir(bb, mir);
229 break;
230 default:
231 break;
232 }
233
234 if (successful) {
Vladimir Marko39d95e62014-02-28 12:51:24 +0000235 if (kIsDebugBuild) {
236 // Clear unreachable catch entries.
237 mir_graph_->catches_.clear();
238 }
239
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800240 // Handle verbosity for return MIR.
241 if (return_mir != nullptr) {
242 current_dalvik_offset_ = return_mir->offset;
243 // Not handling special identity case because it already generated code as part
244 // of the return. The label should have been added before any code was generated.
245 if (special.opcode != kInlineOpReturnArg) {
246 GenPrintLabel(return_mir);
247 }
248 }
249 GenSpecialExitSequence();
250
251 core_spill_mask_ = 0;
252 num_core_spills_ = 0;
253 fp_spill_mask_ = 0;
254 num_fp_spills_ = 0;
255 frame_size_ = 0;
256 core_vmap_table_.clear();
257 fp_vmap_table_.clear();
258 }
259
260 return successful;
261}
262
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263/*
264 * Target-independent code generation. Use only high-level
265 * load/store utilities here, or target-dependent genXX() handlers
266 * when necessary.
267 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700268void Mir2Lir::CompileDalvikInstruction(MIR* mir, BasicBlock* bb, LIR* label_list) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700269 RegLocation rl_src[3];
270 RegLocation rl_dest = mir_graph_->GetBadLoc();
271 RegLocation rl_result = mir_graph_->GetBadLoc();
272 Instruction::Code opcode = mir->dalvikInsn.opcode;
273 int opt_flags = mir->optimization_flags;
274 uint32_t vB = mir->dalvikInsn.vB;
275 uint32_t vC = mir->dalvikInsn.vC;
276
277 // Prep Src and Dest locations.
278 int next_sreg = 0;
279 int next_loc = 0;
buzbee1da1e2f2013-11-15 13:37:01 -0800280 uint64_t attrs = mir_graph_->oat_data_flow_attributes_[opcode];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 rl_src[0] = rl_src[1] = rl_src[2] = mir_graph_->GetBadLoc();
282 if (attrs & DF_UA) {
283 if (attrs & DF_A_WIDE) {
284 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
285 next_sreg+= 2;
286 } else {
287 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
288 next_sreg++;
289 }
290 }
291 if (attrs & DF_UB) {
292 if (attrs & DF_B_WIDE) {
293 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
294 next_sreg+= 2;
295 } else {
296 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
297 next_sreg++;
298 }
299 }
300 if (attrs & DF_UC) {
301 if (attrs & DF_C_WIDE) {
302 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
303 } else {
304 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
305 }
306 }
307 if (attrs & DF_DA) {
308 if (attrs & DF_A_WIDE) {
309 rl_dest = mir_graph_->GetDestWide(mir);
310 } else {
311 rl_dest = mir_graph_->GetDest(mir);
312 }
313 }
314 switch (opcode) {
315 case Instruction::NOP:
316 break;
317
318 case Instruction::MOVE_EXCEPTION:
319 GenMoveException(rl_dest);
320 break;
321
322 case Instruction::RETURN_VOID:
323 if (((cu_->access_flags & kAccConstructor) != 0) &&
324 cu_->compiler_driver->RequiresConstructorBarrier(Thread::Current(), cu_->dex_file,
325 cu_->class_def_idx)) {
326 GenMemBarrier(kStoreStore);
327 }
328 if (!mir_graph_->MethodIsLeaf()) {
329 GenSuspendTest(opt_flags);
330 }
331 break;
332
333 case Instruction::RETURN:
334 case Instruction::RETURN_OBJECT:
335 if (!mir_graph_->MethodIsLeaf()) {
336 GenSuspendTest(opt_flags);
337 }
338 StoreValue(GetReturn(cu_->shorty[0] == 'F'), rl_src[0]);
339 break;
340
341 case Instruction::RETURN_WIDE:
342 if (!mir_graph_->MethodIsLeaf()) {
343 GenSuspendTest(opt_flags);
344 }
345 StoreValueWide(GetReturnWide(cu_->shorty[0] == 'D'), rl_src[0]);
346 break;
347
348 case Instruction::MOVE_RESULT_WIDE:
349 if (opt_flags & MIR_INLINED)
350 break; // Nop - combined w/ previous invoke.
351 StoreValueWide(rl_dest, GetReturnWide(rl_dest.fp));
352 break;
353
354 case Instruction::MOVE_RESULT:
355 case Instruction::MOVE_RESULT_OBJECT:
356 if (opt_flags & MIR_INLINED)
357 break; // Nop - combined w/ previous invoke.
358 StoreValue(rl_dest, GetReturn(rl_dest.fp));
359 break;
360
361 case Instruction::MOVE:
362 case Instruction::MOVE_OBJECT:
363 case Instruction::MOVE_16:
364 case Instruction::MOVE_OBJECT_16:
365 case Instruction::MOVE_FROM16:
366 case Instruction::MOVE_OBJECT_FROM16:
367 StoreValue(rl_dest, rl_src[0]);
368 break;
369
370 case Instruction::MOVE_WIDE:
371 case Instruction::MOVE_WIDE_16:
372 case Instruction::MOVE_WIDE_FROM16:
373 StoreValueWide(rl_dest, rl_src[0]);
374 break;
375
376 case Instruction::CONST:
377 case Instruction::CONST_4:
378 case Instruction::CONST_16:
379 rl_result = EvalLoc(rl_dest, kAnyReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000380 LoadConstantNoClobber(rl_result.reg.GetReg(), vB);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 StoreValue(rl_dest, rl_result);
382 if (vB == 0) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000383 Workaround7250540(rl_dest, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 }
385 break;
386
387 case Instruction::CONST_HIGH16:
388 rl_result = EvalLoc(rl_dest, kAnyReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000389 LoadConstantNoClobber(rl_result.reg.GetReg(), vB << 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 StoreValue(rl_dest, rl_result);
391 if (vB == 0) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000392 Workaround7250540(rl_dest, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 }
394 break;
395
396 case Instruction::CONST_WIDE_16:
397 case Instruction::CONST_WIDE_32:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000398 GenConstWide(rl_dest, static_cast<int64_t>(static_cast<int32_t>(vB)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 break;
400
401 case Instruction::CONST_WIDE:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000402 GenConstWide(rl_dest, mir->dalvikInsn.vB_wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 break;
404
405 case Instruction::CONST_WIDE_HIGH16:
406 rl_result = EvalLoc(rl_dest, kAnyReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000407 LoadConstantWide(rl_result.reg.GetReg(), rl_result.reg.GetHighReg(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 static_cast<int64_t>(vB) << 48);
409 StoreValueWide(rl_dest, rl_result);
410 break;
411
412 case Instruction::MONITOR_ENTER:
413 GenMonitorEnter(opt_flags, rl_src[0]);
414 break;
415
416 case Instruction::MONITOR_EXIT:
417 GenMonitorExit(opt_flags, rl_src[0]);
418 break;
419
420 case Instruction::CHECK_CAST: {
421 GenCheckCast(mir->offset, vB, rl_src[0]);
422 break;
423 }
424 case Instruction::INSTANCE_OF:
425 GenInstanceof(vC, rl_dest, rl_src[0]);
426 break;
427
428 case Instruction::NEW_INSTANCE:
429 GenNewInstance(vB, rl_dest);
430 break;
431
432 case Instruction::THROW:
433 GenThrow(rl_src[0]);
434 break;
435
436 case Instruction::ARRAY_LENGTH:
437 int len_offset;
438 len_offset = mirror::Array::LengthOffset().Int32Value();
439 rl_src[0] = LoadValue(rl_src[0], kCoreReg);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000440 GenNullCheck(rl_src[0].s_reg_low, rl_src[0].reg.GetReg(), opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 rl_result = EvalLoc(rl_dest, kCoreReg, true);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000442 LoadWordDisp(rl_src[0].reg.GetReg(), len_offset, rl_result.reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 StoreValue(rl_dest, rl_result);
444 break;
445
446 case Instruction::CONST_STRING:
447 case Instruction::CONST_STRING_JUMBO:
448 GenConstString(vB, rl_dest);
449 break;
450
451 case Instruction::CONST_CLASS:
452 GenConstClass(vB, rl_dest);
453 break;
454
455 case Instruction::FILL_ARRAY_DATA:
456 GenFillArrayData(vB, rl_src[0]);
457 break;
458
459 case Instruction::FILLED_NEW_ARRAY:
460 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
461 false /* not range */));
462 break;
463
464 case Instruction::FILLED_NEW_ARRAY_RANGE:
465 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
466 true /* range */));
467 break;
468
469 case Instruction::NEW_ARRAY:
470 GenNewArray(vC, rl_dest, rl_src[0]);
471 break;
472
473 case Instruction::GOTO:
474 case Instruction::GOTO_16:
475 case Instruction::GOTO_32:
buzbee9329e6d2013-08-19 12:55:10 -0700476 if (mir_graph_->IsBackedge(bb, bb->taken)) {
buzbee0d829482013-10-11 15:24:55 -0700477 GenSuspendTestAndBranch(opt_flags, &label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 } else {
buzbee0d829482013-10-11 15:24:55 -0700479 OpUnconditionalBranch(&label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 }
481 break;
482
483 case Instruction::PACKED_SWITCH:
484 GenPackedSwitch(mir, vB, rl_src[0]);
485 break;
486
487 case Instruction::SPARSE_SWITCH:
488 GenSparseSwitch(mir, vB, rl_src[0]);
489 break;
490
491 case Instruction::CMPL_FLOAT:
492 case Instruction::CMPG_FLOAT:
493 case Instruction::CMPL_DOUBLE:
494 case Instruction::CMPG_DOUBLE:
495 GenCmpFP(opcode, rl_dest, rl_src[0], rl_src[1]);
496 break;
497
498 case Instruction::CMP_LONG:
499 GenCmpLong(rl_dest, rl_src[0], rl_src[1]);
500 break;
501
502 case Instruction::IF_EQ:
503 case Instruction::IF_NE:
504 case Instruction::IF_LT:
505 case Instruction::IF_GE:
506 case Instruction::IF_GT:
507 case Instruction::IF_LE: {
buzbee0d829482013-10-11 15:24:55 -0700508 LIR* taken = &label_list[bb->taken];
509 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 // Result known at compile time?
511 if (rl_src[0].is_const && rl_src[1].is_const) {
512 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg),
513 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
buzbee0d829482013-10-11 15:24:55 -0700514 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
515 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 GenSuspendTest(opt_flags);
517 }
buzbee0d829482013-10-11 15:24:55 -0700518 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700520 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 GenSuspendTest(opt_flags);
522 }
buzbee0d829482013-10-11 15:24:55 -0700523 GenCompareAndBranch(opcode, rl_src[0], rl_src[1], taken, fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524 }
525 break;
526 }
527
528 case Instruction::IF_EQZ:
529 case Instruction::IF_NEZ:
530 case Instruction::IF_LTZ:
531 case Instruction::IF_GEZ:
532 case Instruction::IF_GTZ:
533 case Instruction::IF_LEZ: {
buzbee0d829482013-10-11 15:24:55 -0700534 LIR* taken = &label_list[bb->taken];
535 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 // Result known at compile time?
537 if (rl_src[0].is_const) {
538 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg), 0);
buzbee0d829482013-10-11 15:24:55 -0700539 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
540 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 GenSuspendTest(opt_flags);
542 }
buzbee0d829482013-10-11 15:24:55 -0700543 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700545 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 GenSuspendTest(opt_flags);
547 }
548 GenCompareZeroAndBranch(opcode, rl_src[0], taken, fall_through);
549 }
550 break;
551 }
552
553 case Instruction::AGET_WIDE:
554 GenArrayGet(opt_flags, kLong, rl_src[0], rl_src[1], rl_dest, 3);
555 break;
556 case Instruction::AGET:
557 case Instruction::AGET_OBJECT:
558 GenArrayGet(opt_flags, kWord, rl_src[0], rl_src[1], rl_dest, 2);
559 break;
560 case Instruction::AGET_BOOLEAN:
561 GenArrayGet(opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
562 break;
563 case Instruction::AGET_BYTE:
564 GenArrayGet(opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
565 break;
566 case Instruction::AGET_CHAR:
567 GenArrayGet(opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
568 break;
569 case Instruction::AGET_SHORT:
570 GenArrayGet(opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
571 break;
572 case Instruction::APUT_WIDE:
Ian Rogersa9a82542013-10-04 11:17:26 -0700573 GenArrayPut(opt_flags, kLong, rl_src[1], rl_src[2], rl_src[0], 3, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 break;
575 case Instruction::APUT:
Ian Rogersa9a82542013-10-04 11:17:26 -0700576 GenArrayPut(opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700578 case Instruction::APUT_OBJECT: {
579 bool is_null = mir_graph_->IsConstantNullRef(rl_src[0]);
580 bool is_safe = is_null; // Always safe to store null.
581 if (!is_safe) {
582 // Check safety from verifier type information.
Vladimir Marko2730db02014-01-27 11:15:17 +0000583 const DexCompilationUnit* unit = mir_graph_->GetCurrentDexCompilationUnit();
584 is_safe = cu_->compiler_driver->IsSafeCast(unit, mir->offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700585 }
586 if (is_null || is_safe) {
587 // Store of constant null doesn't require an assignability test and can be generated inline
588 // without fixed register usage or a card mark.
589 GenArrayPut(opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2, !is_null);
590 } else {
591 GenArrayObjPut(opt_flags, rl_src[1], rl_src[2], rl_src[0]);
592 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700594 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 case Instruction::APUT_SHORT:
596 case Instruction::APUT_CHAR:
Ian Rogersa9a82542013-10-04 11:17:26 -0700597 GenArrayPut(opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 break;
599 case Instruction::APUT_BYTE:
600 case Instruction::APUT_BOOLEAN:
Ian Rogersa9a82542013-10-04 11:17:26 -0700601 GenArrayPut(opt_flags, kUnsignedByte, rl_src[1], rl_src[2], rl_src[0], 0, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 break;
603
604 case Instruction::IGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000605 GenIGet(mir, opt_flags, kWord, rl_dest, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 break;
607
608 case Instruction::IGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000609 GenIGet(mir, opt_flags, kLong, rl_dest, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 break;
611
612 case Instruction::IGET:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000613 GenIGet(mir, opt_flags, kWord, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700614 break;
615
616 case Instruction::IGET_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000617 GenIGet(mir, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 break;
619
620 case Instruction::IGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000621 GenIGet(mir, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700622 break;
623
624 case Instruction::IGET_BOOLEAN:
625 case Instruction::IGET_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000626 GenIGet(mir, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 break;
628
629 case Instruction::IPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000630 GenIPut(mir, opt_flags, kLong, rl_src[0], rl_src[1], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631 break;
632
633 case Instruction::IPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000634 GenIPut(mir, opt_flags, kWord, rl_src[0], rl_src[1], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700635 break;
636
637 case Instruction::IPUT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000638 GenIPut(mir, opt_flags, kWord, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 break;
640
641 case Instruction::IPUT_BOOLEAN:
642 case Instruction::IPUT_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000643 GenIPut(mir, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 break;
645
646 case Instruction::IPUT_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000647 GenIPut(mir, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 break;
649
650 case Instruction::IPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000651 GenIPut(mir, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652 break;
653
654 case Instruction::SGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000655 GenSget(mir, rl_dest, false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 break;
657 case Instruction::SGET:
658 case Instruction::SGET_BOOLEAN:
659 case Instruction::SGET_BYTE:
660 case Instruction::SGET_CHAR:
661 case Instruction::SGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000662 GenSget(mir, rl_dest, false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 break;
664
665 case Instruction::SGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000666 GenSget(mir, rl_dest, true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 break;
668
669 case Instruction::SPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000670 GenSput(mir, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 break;
672
673 case Instruction::SPUT:
674 case Instruction::SPUT_BOOLEAN:
675 case Instruction::SPUT_BYTE:
676 case Instruction::SPUT_CHAR:
677 case Instruction::SPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000678 GenSput(mir, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700679 break;
680
681 case Instruction::SPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000682 GenSput(mir, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700683 break;
684
685 case Instruction::INVOKE_STATIC_RANGE:
686 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, true));
687 break;
688 case Instruction::INVOKE_STATIC:
689 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, false));
690 break;
691
692 case Instruction::INVOKE_DIRECT:
693 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, false));
694 break;
695 case Instruction::INVOKE_DIRECT_RANGE:
696 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, true));
697 break;
698
699 case Instruction::INVOKE_VIRTUAL:
700 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, false));
701 break;
702 case Instruction::INVOKE_VIRTUAL_RANGE:
703 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, true));
704 break;
705
706 case Instruction::INVOKE_SUPER:
707 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, false));
708 break;
709 case Instruction::INVOKE_SUPER_RANGE:
710 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, true));
711 break;
712
713 case Instruction::INVOKE_INTERFACE:
714 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, false));
715 break;
716 case Instruction::INVOKE_INTERFACE_RANGE:
717 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, true));
718 break;
719
720 case Instruction::NEG_INT:
721 case Instruction::NOT_INT:
722 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[0]);
723 break;
724
725 case Instruction::NEG_LONG:
726 case Instruction::NOT_LONG:
727 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[0]);
728 break;
729
730 case Instruction::NEG_FLOAT:
731 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[0]);
732 break;
733
734 case Instruction::NEG_DOUBLE:
735 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[0]);
736 break;
737
738 case Instruction::INT_TO_LONG:
739 GenIntToLong(rl_dest, rl_src[0]);
740 break;
741
742 case Instruction::LONG_TO_INT:
743 rl_src[0] = UpdateLocWide(rl_src[0]);
744 rl_src[0] = WideToNarrow(rl_src[0]);
745 StoreValue(rl_dest, rl_src[0]);
746 break;
747
748 case Instruction::INT_TO_BYTE:
749 case Instruction::INT_TO_SHORT:
750 case Instruction::INT_TO_CHAR:
751 GenIntNarrowing(opcode, rl_dest, rl_src[0]);
752 break;
753
754 case Instruction::INT_TO_FLOAT:
755 case Instruction::INT_TO_DOUBLE:
756 case Instruction::LONG_TO_FLOAT:
757 case Instruction::LONG_TO_DOUBLE:
758 case Instruction::FLOAT_TO_INT:
759 case Instruction::FLOAT_TO_LONG:
760 case Instruction::FLOAT_TO_DOUBLE:
761 case Instruction::DOUBLE_TO_INT:
762 case Instruction::DOUBLE_TO_LONG:
763 case Instruction::DOUBLE_TO_FLOAT:
764 GenConversion(opcode, rl_dest, rl_src[0]);
765 break;
766
767
768 case Instruction::ADD_INT:
769 case Instruction::ADD_INT_2ADDR:
770 case Instruction::MUL_INT:
771 case Instruction::MUL_INT_2ADDR:
772 case Instruction::AND_INT:
773 case Instruction::AND_INT_2ADDR:
774 case Instruction::OR_INT:
775 case Instruction::OR_INT_2ADDR:
776 case Instruction::XOR_INT:
777 case Instruction::XOR_INT_2ADDR:
778 if (rl_src[0].is_const &&
779 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[0]))) {
780 GenArithOpIntLit(opcode, rl_dest, rl_src[1],
781 mir_graph_->ConstantValue(rl_src[0].orig_sreg));
782 } else if (rl_src[1].is_const &&
783 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
784 GenArithOpIntLit(opcode, rl_dest, rl_src[0],
785 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
786 } else {
787 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
788 }
789 break;
790
791 case Instruction::SUB_INT:
792 case Instruction::SUB_INT_2ADDR:
793 case Instruction::DIV_INT:
794 case Instruction::DIV_INT_2ADDR:
795 case Instruction::REM_INT:
796 case Instruction::REM_INT_2ADDR:
797 case Instruction::SHL_INT:
798 case Instruction::SHL_INT_2ADDR:
799 case Instruction::SHR_INT:
800 case Instruction::SHR_INT_2ADDR:
801 case Instruction::USHR_INT:
802 case Instruction::USHR_INT_2ADDR:
803 if (rl_src[1].is_const &&
804 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
805 GenArithOpIntLit(opcode, rl_dest, rl_src[0], mir_graph_->ConstantValue(rl_src[1]));
806 } else {
807 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
808 }
809 break;
810
811 case Instruction::ADD_LONG:
812 case Instruction::SUB_LONG:
813 case Instruction::AND_LONG:
814 case Instruction::OR_LONG:
815 case Instruction::XOR_LONG:
816 case Instruction::ADD_LONG_2ADDR:
817 case Instruction::SUB_LONG_2ADDR:
818 case Instruction::AND_LONG_2ADDR:
819 case Instruction::OR_LONG_2ADDR:
820 case Instruction::XOR_LONG_2ADDR:
821 if (rl_src[0].is_const || rl_src[1].is_const) {
822 GenArithImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
823 break;
824 }
825 // Note: intentional fallthrough.
826
827 case Instruction::MUL_LONG:
828 case Instruction::DIV_LONG:
829 case Instruction::REM_LONG:
830 case Instruction::MUL_LONG_2ADDR:
831 case Instruction::DIV_LONG_2ADDR:
832 case Instruction::REM_LONG_2ADDR:
833 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
834 break;
835
836 case Instruction::SHL_LONG:
837 case Instruction::SHR_LONG:
838 case Instruction::USHR_LONG:
839 case Instruction::SHL_LONG_2ADDR:
840 case Instruction::SHR_LONG_2ADDR:
841 case Instruction::USHR_LONG_2ADDR:
842 if (rl_src[1].is_const) {
843 GenShiftImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
844 } else {
845 GenShiftOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
846 }
847 break;
848
849 case Instruction::ADD_FLOAT:
850 case Instruction::SUB_FLOAT:
851 case Instruction::MUL_FLOAT:
852 case Instruction::DIV_FLOAT:
853 case Instruction::REM_FLOAT:
854 case Instruction::ADD_FLOAT_2ADDR:
855 case Instruction::SUB_FLOAT_2ADDR:
856 case Instruction::MUL_FLOAT_2ADDR:
857 case Instruction::DIV_FLOAT_2ADDR:
858 case Instruction::REM_FLOAT_2ADDR:
859 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[1]);
860 break;
861
862 case Instruction::ADD_DOUBLE:
863 case Instruction::SUB_DOUBLE:
864 case Instruction::MUL_DOUBLE:
865 case Instruction::DIV_DOUBLE:
866 case Instruction::REM_DOUBLE:
867 case Instruction::ADD_DOUBLE_2ADDR:
868 case Instruction::SUB_DOUBLE_2ADDR:
869 case Instruction::MUL_DOUBLE_2ADDR:
870 case Instruction::DIV_DOUBLE_2ADDR:
871 case Instruction::REM_DOUBLE_2ADDR:
872 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[1]);
873 break;
874
875 case Instruction::RSUB_INT:
876 case Instruction::ADD_INT_LIT16:
877 case Instruction::MUL_INT_LIT16:
878 case Instruction::DIV_INT_LIT16:
879 case Instruction::REM_INT_LIT16:
880 case Instruction::AND_INT_LIT16:
881 case Instruction::OR_INT_LIT16:
882 case Instruction::XOR_INT_LIT16:
883 case Instruction::ADD_INT_LIT8:
884 case Instruction::RSUB_INT_LIT8:
885 case Instruction::MUL_INT_LIT8:
886 case Instruction::DIV_INT_LIT8:
887 case Instruction::REM_INT_LIT8:
888 case Instruction::AND_INT_LIT8:
889 case Instruction::OR_INT_LIT8:
890 case Instruction::XOR_INT_LIT8:
891 case Instruction::SHL_INT_LIT8:
892 case Instruction::SHR_INT_LIT8:
893 case Instruction::USHR_INT_LIT8:
894 GenArithOpIntLit(opcode, rl_dest, rl_src[0], vC);
895 break;
896
897 default:
898 LOG(FATAL) << "Unexpected opcode: " << opcode;
899 }
Brian Carlstrom1895ea32013-07-18 13:28:37 -0700900} // NOLINT(readability/fn_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901
902// Process extended MIR instructions
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700903void Mir2Lir::HandleExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700904 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
905 case kMirOpCopy: {
906 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
907 RegLocation rl_dest = mir_graph_->GetDest(mir);
908 StoreValue(rl_dest, rl_src);
909 break;
910 }
911 case kMirOpFusedCmplFloat:
912 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, false /*double*/);
913 break;
914 case kMirOpFusedCmpgFloat:
915 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, false /*double*/);
916 break;
917 case kMirOpFusedCmplDouble:
918 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, true /*double*/);
919 break;
920 case kMirOpFusedCmpgDouble:
921 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, true /*double*/);
922 break;
923 case kMirOpFusedCmpLong:
924 GenFusedLongCmpBranch(bb, mir);
925 break;
926 case kMirOpSelect:
927 GenSelect(bb, mir);
928 break;
929 default:
930 break;
931 }
932}
933
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800934void Mir2Lir::GenPrintLabel(MIR* mir) {
935 // Mark the beginning of a Dalvik instruction for line tracking.
936 if (cu_->verbose) {
937 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
938 MarkBoundary(mir->offset, inst_str);
939 }
940}
941
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942// Handle the content in each basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700943bool Mir2Lir::MethodBlockCodeGen(BasicBlock* bb) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700944 if (bb->block_type == kDead) return false;
945 current_dalvik_offset_ = bb->start_offset;
946 MIR* mir;
947 int block_id = bb->id;
948
949 block_label_list_[block_id].operands[0] = bb->start_offset;
950
951 // Insert the block label.
952 block_label_list_[block_id].opcode = kPseudoNormalBlockLabel;
buzbeeb48819d2013-09-14 16:15:25 -0700953 block_label_list_[block_id].flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700954 AppendLIR(&block_label_list_[block_id]);
955
956 LIR* head_lir = NULL;
957
958 // If this is a catch block, export the start address.
959 if (bb->catch_entry) {
960 head_lir = NewLIR0(kPseudoExportedPC);
961 }
962
963 // Free temp registers and reset redundant store tracking.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 ClobberAllRegs();
965
966 if (bb->block_type == kEntryBlock) {
buzbee56c71782013-09-05 17:13:19 -0700967 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
969 GenEntrySequence(&mir_graph_->reg_location_[start_vreg],
970 mir_graph_->reg_location_[mir_graph_->GetMethodSReg()]);
971 } else if (bb->block_type == kExitBlock) {
buzbee56c71782013-09-05 17:13:19 -0700972 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700973 GenExitSequence();
974 }
975
976 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
977 ResetRegPool();
978 if (cu_->disable_opt & (1 << kTrackLiveTemps)) {
979 ClobberAllRegs();
980 }
981
982 if (cu_->disable_opt & (1 << kSuppressLoads)) {
983 ResetDefTracking();
984 }
985
986 // Reset temp tracking sanity check.
987 if (kIsDebugBuild) {
988 live_sreg_ = INVALID_SREG;
989 }
990
991 current_dalvik_offset_ = mir->offset;
992 int opcode = mir->dalvikInsn.opcode;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800994 GenPrintLabel(mir);
995
Brian Carlstrom7940e442013-07-12 13:46:57 -0700996 // Remember the first LIR for this block.
997 if (head_lir == NULL) {
buzbee252254b2013-09-08 16:20:53 -0700998 head_lir = &block_label_list_[bb->id];
999 // Set the first label as a scheduling barrier.
buzbeeb48819d2013-09-14 16:15:25 -07001000 DCHECK(!head_lir->flags.use_def_invalid);
1001 head_lir->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001002 }
1003
1004 if (opcode == kMirOpCheck) {
1005 // Combine check and work halves of throwing instruction.
1006 MIR* work_half = mir->meta.throw_insn;
1007 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Vladimir Marko4376c872014-01-23 12:39:29 +00001008 mir->meta = work_half->meta; // Whatever the work_half had, we need to copy it.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 opcode = work_half->dalvikInsn.opcode;
1010 SSARepresentation* ssa_rep = work_half->ssa_rep;
1011 work_half->ssa_rep = mir->ssa_rep;
1012 mir->ssa_rep = ssa_rep;
1013 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Vladimir Marko4376c872014-01-23 12:39:29 +00001014 work_half->meta.throw_insn = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 }
1016
1017 if (opcode >= kMirOpFirst) {
1018 HandleExtendedMethodMIR(bb, mir);
1019 continue;
1020 }
1021
1022 CompileDalvikInstruction(mir, bb, block_label_list_);
1023 }
1024
1025 if (head_lir) {
1026 // Eliminate redundant loads/stores and delay stores into later slots.
1027 ApplyLocalOptimizations(head_lir, last_lir_insn_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 }
1029 return false;
1030}
1031
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001032bool Mir2Lir::SpecialMIR2LIR(const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +00001033 cu_->NewTimingSplit("SpecialMIR2LIR");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001034 // Find the first DalvikByteCode block.
1035 int num_reachable_blocks = mir_graph_->GetNumReachableBlocks();
1036 BasicBlock*bb = NULL;
1037 for (int idx = 0; idx < num_reachable_blocks; idx++) {
1038 // TODO: no direct access of growable lists.
1039 int dfs_index = mir_graph_->GetDfsOrder()->Get(idx);
1040 bb = mir_graph_->GetBasicBlock(dfs_index);
1041 if (bb->block_type == kDalvikByteCode) {
1042 break;
1043 }
1044 }
1045 if (bb == NULL) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001046 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 }
1048 DCHECK_EQ(bb->start_offset, 0);
1049 DCHECK(bb->first_mir_insn != NULL);
1050
1051 // Get the first instruction.
1052 MIR* mir = bb->first_mir_insn;
1053
1054 // Free temp registers and reset redundant store tracking.
1055 ResetRegPool();
1056 ResetDefTracking();
1057 ClobberAllRegs();
1058
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001059 return GenSpecialCase(bb, mir, special);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060}
1061
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001062void Mir2Lir::MethodMIR2LIR() {
buzbeea61f4952013-08-23 14:27:06 -07001063 cu_->NewTimingSplit("MIR2LIR");
1064
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 // Hold the labels of each block.
1066 block_label_list_ =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001067 static_cast<LIR*>(arena_->Alloc(sizeof(LIR) * mir_graph_->GetNumBlocks(),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001068 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069
buzbee56c71782013-09-05 17:13:19 -07001070 PreOrderDfsIterator iter(mir_graph_);
buzbee252254b2013-09-08 16:20:53 -07001071 BasicBlock* curr_bb = iter.Next();
1072 BasicBlock* next_bb = iter.Next();
1073 while (curr_bb != NULL) {
1074 MethodBlockCodeGen(curr_bb);
1075 // If the fall_through block is no longer laid out consecutively, drop in a branch.
buzbee0d829482013-10-11 15:24:55 -07001076 BasicBlock* curr_bb_fall_through = mir_graph_->GetBasicBlock(curr_bb->fall_through);
1077 if ((curr_bb_fall_through != NULL) && (curr_bb_fall_through != next_bb)) {
1078 OpUnconditionalBranch(&block_label_list_[curr_bb->fall_through]);
buzbee252254b2013-09-08 16:20:53 -07001079 }
1080 curr_bb = next_bb;
1081 do {
1082 next_bb = iter.Next();
1083 } while ((next_bb != NULL) && (next_bb->block_type == kDead));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001085 HandleSlowPaths();
1086
buzbeea61f4952013-08-23 14:27:06 -07001087 cu_->NewTimingSplit("Launchpads");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001088 HandleSuspendLaunchPads();
1089
1090 HandleThrowLaunchPads();
1091
1092 HandleIntrinsicLaunchPads();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093}
1094
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001095//
1096// LIR Slow Path
1097//
1098
1099LIR* Mir2Lir::LIRSlowPath::GenerateTargetLabel() {
1100 LIR* target = m2l_->RawLIR(current_dex_pc_, kPseudoTargetLabel);
1101 m2l_->AppendLIR(target);
1102 fromfast_->target = target;
1103 m2l_->SetCurrentDexPc(current_dex_pc_);
1104 return target;
1105}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106} // namespace art