blob: 6fcdf70b12a56c6c24b40f84f9d492bd28a32535 [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) {
buzbee2700f7e2014-03-07 09:46:20 -080027 RegStorage reg_arg_low = GetArgMappingToPhysicalReg(in_position);
28 RegStorage reg_arg_high = wide ? GetArgMappingToPhysicalReg(in_position + 1) :
29 RegStorage::InvalidReg();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080030
buzbee2700f7e2014-03-07 09:46:20 -080031 if (reg_arg_low.Valid()) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080032 LockTemp(reg_arg_low);
33 }
buzbee2700f7e2014-03-07 09:46:20 -080034 if (reg_arg_high.Valid() && reg_arg_low != reg_arg_high) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080035 LockTemp(reg_arg_high);
36 }
37}
38
buzbee2700f7e2014-03-07 09:46:20 -080039// TODO: needs revisit for 64-bit.
40RegStorage Mir2Lir::LoadArg(int in_position, bool wide) {
41 RegStorage reg_arg_low = GetArgMappingToPhysicalReg(in_position);
42 RegStorage reg_arg_high = wide ? GetArgMappingToPhysicalReg(in_position + 1) :
43 RegStorage::InvalidReg();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080044
45 int offset = StackVisitor::GetOutVROffset(in_position);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +070046 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080047 /*
48 * When doing a call for x86, it moves the stack pointer in order to push return.
49 * Thus, we add another 4 bytes to figure out the out of caller (in of callee).
50 * TODO: This needs revisited for 64-bit.
51 */
52 offset += sizeof(uint32_t);
53 }
54
55 // If the VR is wide and there is no register for high part, we need to load it.
buzbee2700f7e2014-03-07 09:46:20 -080056 if (wide && !reg_arg_high.Valid()) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080057 // If the low part is not in a reg, we allocate a pair. Otherwise, we just load to high reg.
buzbee2700f7e2014-03-07 09:46:20 -080058 if (!reg_arg_low.Valid()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +000059 RegStorage new_regs = AllocTypedTempWide(false, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -080060 reg_arg_low = new_regs.GetLow();
61 reg_arg_high = new_regs.GetHigh();
62 LoadBaseDispWide(TargetReg(kSp), offset, new_regs, INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080063 } else {
64 reg_arg_high = AllocTemp();
65 int offset_high = offset + sizeof(uint32_t);
66 LoadWordDisp(TargetReg(kSp), offset_high, reg_arg_high);
67 }
68 }
69
70 // If the low part is not in a register yet, we need to load it.
buzbee2700f7e2014-03-07 09:46:20 -080071 if (!reg_arg_low.Valid()) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080072 reg_arg_low = AllocTemp();
73 LoadWordDisp(TargetReg(kSp), offset, reg_arg_low);
74 }
75
76 if (wide) {
buzbee2700f7e2014-03-07 09:46:20 -080077 return RegStorage::MakeRegPair(reg_arg_low, reg_arg_high);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080078 } else {
79 return reg_arg_low;
80 }
81}
82
83void Mir2Lir::LoadArgDirect(int in_position, RegLocation rl_dest) {
84 int offset = StackVisitor::GetOutVROffset(in_position);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +070085 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080086 /*
87 * When doing a call for x86, it moves the stack pointer in order to push return.
88 * Thus, we add another 4 bytes to figure out the out of caller (in of callee).
89 * TODO: This needs revisited for 64-bit.
90 */
91 offset += sizeof(uint32_t);
92 }
93
94 if (!rl_dest.wide) {
buzbee2700f7e2014-03-07 09:46:20 -080095 RegStorage reg = GetArgMappingToPhysicalReg(in_position);
96 if (reg.Valid()) {
97 OpRegCopy(rl_dest.reg, reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080098 } else {
buzbee2700f7e2014-03-07 09:46:20 -080099 LoadWordDisp(TargetReg(kSp), offset, rl_dest.reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800100 }
101 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800102 RegStorage reg_arg_low = GetArgMappingToPhysicalReg(in_position);
103 RegStorage reg_arg_high = GetArgMappingToPhysicalReg(in_position + 1);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800104
buzbee2700f7e2014-03-07 09:46:20 -0800105 if (reg_arg_low.Valid() && reg_arg_high.Valid()) {
106 OpRegCopyWide(rl_dest.reg, RegStorage::MakeRegPair(reg_arg_low, reg_arg_high));
107 } else if (reg_arg_low.Valid() && !reg_arg_high.Valid()) {
108 OpRegCopy(rl_dest.reg, reg_arg_low);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800109 int offset_high = offset + sizeof(uint32_t);
buzbee2700f7e2014-03-07 09:46:20 -0800110 LoadWordDisp(TargetReg(kSp), offset_high, rl_dest.reg.GetHigh());
111 } else if (!reg_arg_low.Valid() && reg_arg_high.Valid()) {
112 OpRegCopy(rl_dest.reg.GetHigh(), reg_arg_high);
113 LoadWordDisp(TargetReg(kSp), offset, rl_dest.reg.GetLow());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800114 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800115 LoadBaseDispWide(TargetReg(kSp), offset, rl_dest.reg, INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800116 }
117 }
118}
119
120bool Mir2Lir::GenSpecialIGet(MIR* mir, const InlineMethod& special) {
121 // FastInstance() already checked by DexFileMethodInliner.
122 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100123 if (data.method_is_static != 0u || data.object_arg != 0u) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800124 // The object is not "this" and has to be null-checked.
125 return false;
126 }
127
Vladimir Markoe3e02602014-03-12 15:42:41 +0000128 bool wide = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE));
129 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800130 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
131
132 // Point of no return - no aborts after this
133 GenPrintLabel(mir);
134 LockArg(data.object_arg);
135 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
buzbee2700f7e2014-03-07 09:46:20 -0800136 RegStorage reg_obj = LoadArg(data.object_arg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800137 if (wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800138 LoadBaseDispWide(reg_obj, data.field_offset, rl_dest.reg, INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800139 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800140 LoadWordDisp(reg_obj, data.field_offset, rl_dest.reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800141 }
142 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800143 // Without context sensitive analysis, we must issue the most conservative barriers.
144 // In this case, either a load or store may follow so we issue both barriers.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800145 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800146 GenMemBarrier(kLoadStore);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800147 }
148 return true;
149}
150
151bool Mir2Lir::GenSpecialIPut(MIR* mir, const InlineMethod& special) {
152 // FastInstance() already checked by DexFileMethodInliner.
153 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100154 if (data.method_is_static != 0u || data.object_arg != 0u) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800155 // The object is not "this" and has to be null-checked.
156 return false;
157 }
Vladimir Markoe1fced12014-04-04 14:52:53 +0100158 if (data.return_arg_plus1 != 0u) {
159 // The setter returns a method argument which we don't support here.
160 return false;
161 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800162
Vladimir Markoe3e02602014-03-12 15:42:41 +0000163 bool wide = (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800164
165 // Point of no return - no aborts after this
166 GenPrintLabel(mir);
167 LockArg(data.object_arg);
168 LockArg(data.src_arg, wide);
buzbee2700f7e2014-03-07 09:46:20 -0800169 RegStorage reg_obj = LoadArg(data.object_arg);
170 RegStorage reg_src = LoadArg(data.src_arg, wide);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800171 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800172 // There might have been a store before this volatile one so insert StoreStore barrier.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800173 GenMemBarrier(kStoreStore);
174 }
175 if (wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800176 StoreBaseDispWide(reg_obj, data.field_offset, reg_src);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800177 } else {
178 StoreBaseDisp(reg_obj, data.field_offset, reg_src, kWord);
179 }
180 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800181 // A load might follow the volatile store so insert a StoreLoad barrier.
182 GenMemBarrier(kStoreLoad);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800183 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000184 if (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT)) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800185 MarkGCCard(reg_src, reg_obj);
186 }
187 return true;
188}
189
190bool Mir2Lir::GenSpecialIdentity(MIR* mir, const InlineMethod& special) {
191 const InlineReturnArgData& data = special.d.return_data;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000192 bool wide = (data.is_wide != 0u);
193 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800194 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
195
196 // Point of no return - no aborts after this
197 GenPrintLabel(mir);
198 LockArg(data.arg, wide);
199 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
200 LoadArgDirect(data.arg, rl_dest);
201 return true;
202}
203
204/*
205 * Special-case code generation for simple non-throwing leaf methods.
206 */
207bool Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
208 DCHECK(special.flags & kInlineSpecial);
209 current_dalvik_offset_ = mir->offset;
210 MIR* return_mir = nullptr;
211 bool successful = false;
212
213 switch (special.opcode) {
214 case kInlineOpNop:
215 successful = true;
216 DCHECK_EQ(mir->dalvikInsn.opcode, Instruction::RETURN_VOID);
217 return_mir = mir;
218 break;
219 case kInlineOpNonWideConst: {
220 successful = true;
221 RegLocation rl_dest = GetReturn(cu_->shorty[0] == 'F');
222 GenPrintLabel(mir);
buzbee2700f7e2014-03-07 09:46:20 -0800223 LoadConstant(rl_dest.reg, static_cast<int>(special.d.data));
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700224 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800225 break;
226 }
227 case kInlineOpReturnArg:
228 successful = GenSpecialIdentity(mir, special);
229 return_mir = mir;
230 break;
231 case kInlineOpIGet:
232 successful = GenSpecialIGet(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700233 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800234 break;
235 case kInlineOpIPut:
236 successful = GenSpecialIPut(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700237 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800238 break;
239 default:
240 break;
241 }
242
243 if (successful) {
Vladimir Marko39d95e62014-02-28 12:51:24 +0000244 if (kIsDebugBuild) {
245 // Clear unreachable catch entries.
246 mir_graph_->catches_.clear();
247 }
248
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800249 // Handle verbosity for return MIR.
250 if (return_mir != nullptr) {
251 current_dalvik_offset_ = return_mir->offset;
252 // Not handling special identity case because it already generated code as part
253 // of the return. The label should have been added before any code was generated.
254 if (special.opcode != kInlineOpReturnArg) {
255 GenPrintLabel(return_mir);
256 }
257 }
258 GenSpecialExitSequence();
259
260 core_spill_mask_ = 0;
261 num_core_spills_ = 0;
262 fp_spill_mask_ = 0;
263 num_fp_spills_ = 0;
264 frame_size_ = 0;
265 core_vmap_table_.clear();
266 fp_vmap_table_.clear();
267 }
268
269 return successful;
270}
271
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272/*
273 * Target-independent code generation. Use only high-level
274 * load/store utilities here, or target-dependent genXX() handlers
275 * when necessary.
276 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700277void Mir2Lir::CompileDalvikInstruction(MIR* mir, BasicBlock* bb, LIR* label_list) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 RegLocation rl_src[3];
279 RegLocation rl_dest = mir_graph_->GetBadLoc();
280 RegLocation rl_result = mir_graph_->GetBadLoc();
281 Instruction::Code opcode = mir->dalvikInsn.opcode;
282 int opt_flags = mir->optimization_flags;
283 uint32_t vB = mir->dalvikInsn.vB;
284 uint32_t vC = mir->dalvikInsn.vC;
285
286 // Prep Src and Dest locations.
287 int next_sreg = 0;
288 int next_loc = 0;
buzbee1da1e2f2013-11-15 13:37:01 -0800289 uint64_t attrs = mir_graph_->oat_data_flow_attributes_[opcode];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700290 rl_src[0] = rl_src[1] = rl_src[2] = mir_graph_->GetBadLoc();
291 if (attrs & DF_UA) {
292 if (attrs & DF_A_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_UB) {
301 if (attrs & DF_B_WIDE) {
302 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
303 next_sreg+= 2;
304 } else {
305 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
306 next_sreg++;
307 }
308 }
309 if (attrs & DF_UC) {
310 if (attrs & DF_C_WIDE) {
311 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
312 } else {
313 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
314 }
315 }
316 if (attrs & DF_DA) {
317 if (attrs & DF_A_WIDE) {
318 rl_dest = mir_graph_->GetDestWide(mir);
319 } else {
320 rl_dest = mir_graph_->GetDest(mir);
321 }
322 }
323 switch (opcode) {
324 case Instruction::NOP:
325 break;
326
327 case Instruction::MOVE_EXCEPTION:
328 GenMoveException(rl_dest);
329 break;
330
331 case Instruction::RETURN_VOID:
332 if (((cu_->access_flags & kAccConstructor) != 0) &&
333 cu_->compiler_driver->RequiresConstructorBarrier(Thread::Current(), cu_->dex_file,
334 cu_->class_def_idx)) {
335 GenMemBarrier(kStoreStore);
336 }
337 if (!mir_graph_->MethodIsLeaf()) {
338 GenSuspendTest(opt_flags);
339 }
340 break;
341
342 case Instruction::RETURN:
343 case Instruction::RETURN_OBJECT:
344 if (!mir_graph_->MethodIsLeaf()) {
345 GenSuspendTest(opt_flags);
346 }
347 StoreValue(GetReturn(cu_->shorty[0] == 'F'), rl_src[0]);
348 break;
349
350 case Instruction::RETURN_WIDE:
351 if (!mir_graph_->MethodIsLeaf()) {
352 GenSuspendTest(opt_flags);
353 }
354 StoreValueWide(GetReturnWide(cu_->shorty[0] == 'D'), rl_src[0]);
355 break;
356
357 case Instruction::MOVE_RESULT_WIDE:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000358 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000360 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 StoreValueWide(rl_dest, GetReturnWide(rl_dest.fp));
362 break;
363
364 case Instruction::MOVE_RESULT:
365 case Instruction::MOVE_RESULT_OBJECT:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000366 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000368 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 StoreValue(rl_dest, GetReturn(rl_dest.fp));
370 break;
371
372 case Instruction::MOVE:
373 case Instruction::MOVE_OBJECT:
374 case Instruction::MOVE_16:
375 case Instruction::MOVE_OBJECT_16:
376 case Instruction::MOVE_FROM16:
377 case Instruction::MOVE_OBJECT_FROM16:
378 StoreValue(rl_dest, rl_src[0]);
379 break;
380
381 case Instruction::MOVE_WIDE:
382 case Instruction::MOVE_WIDE_16:
383 case Instruction::MOVE_WIDE_FROM16:
384 StoreValueWide(rl_dest, rl_src[0]);
385 break;
386
387 case Instruction::CONST:
388 case Instruction::CONST_4:
389 case Instruction::CONST_16:
390 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800391 LoadConstantNoClobber(rl_result.reg, vB);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700392 StoreValue(rl_dest, rl_result);
393 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800394 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 }
396 break;
397
398 case Instruction::CONST_HIGH16:
399 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800400 LoadConstantNoClobber(rl_result.reg, vB << 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 StoreValue(rl_dest, rl_result);
402 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800403 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 }
405 break;
406
407 case Instruction::CONST_WIDE_16:
408 case Instruction::CONST_WIDE_32:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000409 GenConstWide(rl_dest, static_cast<int64_t>(static_cast<int32_t>(vB)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 break;
411
412 case Instruction::CONST_WIDE:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000413 GenConstWide(rl_dest, mir->dalvikInsn.vB_wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 break;
415
416 case Instruction::CONST_WIDE_HIGH16:
417 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800418 LoadConstantWide(rl_result.reg, static_cast<int64_t>(vB) << 48);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 StoreValueWide(rl_dest, rl_result);
420 break;
421
422 case Instruction::MONITOR_ENTER:
423 GenMonitorEnter(opt_flags, rl_src[0]);
424 break;
425
426 case Instruction::MONITOR_EXIT:
427 GenMonitorExit(opt_flags, rl_src[0]);
428 break;
429
430 case Instruction::CHECK_CAST: {
431 GenCheckCast(mir->offset, vB, rl_src[0]);
432 break;
433 }
434 case Instruction::INSTANCE_OF:
435 GenInstanceof(vC, rl_dest, rl_src[0]);
436 break;
437
438 case Instruction::NEW_INSTANCE:
439 GenNewInstance(vB, rl_dest);
440 break;
441
442 case Instruction::THROW:
443 GenThrow(rl_src[0]);
444 break;
445
446 case Instruction::ARRAY_LENGTH:
447 int len_offset;
448 len_offset = mirror::Array::LengthOffset().Int32Value();
449 rl_src[0] = LoadValue(rl_src[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800450 GenNullCheck(rl_src[0].reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800452 LoadWordDisp(rl_src[0].reg, len_offset, rl_result.reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700453 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 StoreValue(rl_dest, rl_result);
455 break;
456
457 case Instruction::CONST_STRING:
458 case Instruction::CONST_STRING_JUMBO:
459 GenConstString(vB, rl_dest);
460 break;
461
462 case Instruction::CONST_CLASS:
463 GenConstClass(vB, rl_dest);
464 break;
465
466 case Instruction::FILL_ARRAY_DATA:
467 GenFillArrayData(vB, rl_src[0]);
468 break;
469
470 case Instruction::FILLED_NEW_ARRAY:
471 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
472 false /* not range */));
473 break;
474
475 case Instruction::FILLED_NEW_ARRAY_RANGE:
476 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
477 true /* range */));
478 break;
479
480 case Instruction::NEW_ARRAY:
481 GenNewArray(vC, rl_dest, rl_src[0]);
482 break;
483
484 case Instruction::GOTO:
485 case Instruction::GOTO_16:
486 case Instruction::GOTO_32:
buzbee9329e6d2013-08-19 12:55:10 -0700487 if (mir_graph_->IsBackedge(bb, bb->taken)) {
buzbee0d829482013-10-11 15:24:55 -0700488 GenSuspendTestAndBranch(opt_flags, &label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 } else {
buzbee0d829482013-10-11 15:24:55 -0700490 OpUnconditionalBranch(&label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 }
492 break;
493
494 case Instruction::PACKED_SWITCH:
495 GenPackedSwitch(mir, vB, rl_src[0]);
496 break;
497
498 case Instruction::SPARSE_SWITCH:
499 GenSparseSwitch(mir, vB, rl_src[0]);
500 break;
501
502 case Instruction::CMPL_FLOAT:
503 case Instruction::CMPG_FLOAT:
504 case Instruction::CMPL_DOUBLE:
505 case Instruction::CMPG_DOUBLE:
506 GenCmpFP(opcode, rl_dest, rl_src[0], rl_src[1]);
507 break;
508
509 case Instruction::CMP_LONG:
510 GenCmpLong(rl_dest, rl_src[0], rl_src[1]);
511 break;
512
513 case Instruction::IF_EQ:
514 case Instruction::IF_NE:
515 case Instruction::IF_LT:
516 case Instruction::IF_GE:
517 case Instruction::IF_GT:
518 case Instruction::IF_LE: {
buzbee0d829482013-10-11 15:24:55 -0700519 LIR* taken = &label_list[bb->taken];
520 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 // Result known at compile time?
522 if (rl_src[0].is_const && rl_src[1].is_const) {
523 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg),
524 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
buzbee0d829482013-10-11 15:24:55 -0700525 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
526 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 GenSuspendTest(opt_flags);
528 }
buzbee0d829482013-10-11 15:24:55 -0700529 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700531 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 GenSuspendTest(opt_flags);
533 }
buzbee0d829482013-10-11 15:24:55 -0700534 GenCompareAndBranch(opcode, rl_src[0], rl_src[1], taken, fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 }
536 break;
537 }
538
539 case Instruction::IF_EQZ:
540 case Instruction::IF_NEZ:
541 case Instruction::IF_LTZ:
542 case Instruction::IF_GEZ:
543 case Instruction::IF_GTZ:
544 case Instruction::IF_LEZ: {
buzbee0d829482013-10-11 15:24:55 -0700545 LIR* taken = &label_list[bb->taken];
546 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 // Result known at compile time?
548 if (rl_src[0].is_const) {
549 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg), 0);
buzbee0d829482013-10-11 15:24:55 -0700550 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
551 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 GenSuspendTest(opt_flags);
553 }
buzbee0d829482013-10-11 15:24:55 -0700554 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700556 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 GenSuspendTest(opt_flags);
558 }
559 GenCompareZeroAndBranch(opcode, rl_src[0], taken, fall_through);
560 }
561 break;
562 }
563
564 case Instruction::AGET_WIDE:
565 GenArrayGet(opt_flags, kLong, rl_src[0], rl_src[1], rl_dest, 3);
566 break;
567 case Instruction::AGET:
568 case Instruction::AGET_OBJECT:
569 GenArrayGet(opt_flags, kWord, rl_src[0], rl_src[1], rl_dest, 2);
570 break;
571 case Instruction::AGET_BOOLEAN:
572 GenArrayGet(opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
573 break;
574 case Instruction::AGET_BYTE:
575 GenArrayGet(opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
576 break;
577 case Instruction::AGET_CHAR:
578 GenArrayGet(opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
579 break;
580 case Instruction::AGET_SHORT:
581 GenArrayGet(opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
582 break;
583 case Instruction::APUT_WIDE:
Ian Rogersa9a82542013-10-04 11:17:26 -0700584 GenArrayPut(opt_flags, kLong, rl_src[1], rl_src[2], rl_src[0], 3, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 break;
586 case Instruction::APUT:
Ian Rogersa9a82542013-10-04 11:17:26 -0700587 GenArrayPut(opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700589 case Instruction::APUT_OBJECT: {
590 bool is_null = mir_graph_->IsConstantNullRef(rl_src[0]);
591 bool is_safe = is_null; // Always safe to store null.
592 if (!is_safe) {
593 // Check safety from verifier type information.
Vladimir Marko2730db02014-01-27 11:15:17 +0000594 const DexCompilationUnit* unit = mir_graph_->GetCurrentDexCompilationUnit();
595 is_safe = cu_->compiler_driver->IsSafeCast(unit, mir->offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700596 }
597 if (is_null || is_safe) {
598 // Store of constant null doesn't require an assignability test and can be generated inline
599 // without fixed register usage or a card mark.
600 GenArrayPut(opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2, !is_null);
601 } else {
602 GenArrayObjPut(opt_flags, rl_src[1], rl_src[2], rl_src[0]);
603 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700605 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 case Instruction::APUT_SHORT:
607 case Instruction::APUT_CHAR:
Ian Rogersa9a82542013-10-04 11:17:26 -0700608 GenArrayPut(opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 break;
610 case Instruction::APUT_BYTE:
611 case Instruction::APUT_BOOLEAN:
Ian Rogersa9a82542013-10-04 11:17:26 -0700612 GenArrayPut(opt_flags, kUnsignedByte, rl_src[1], rl_src[2], rl_src[0], 0, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 break;
614
615 case Instruction::IGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000616 GenIGet(mir, opt_flags, kWord, rl_dest, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 break;
618
619 case Instruction::IGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000620 GenIGet(mir, opt_flags, kLong, rl_dest, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 break;
622
623 case Instruction::IGET:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000624 GenIGet(mir, opt_flags, kWord, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 break;
626
627 case Instruction::IGET_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000628 GenIGet(mir, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 break;
630
631 case Instruction::IGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000632 GenIGet(mir, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 break;
634
635 case Instruction::IGET_BOOLEAN:
636 case Instruction::IGET_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000637 GenIGet(mir, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 break;
639
640 case Instruction::IPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000641 GenIPut(mir, opt_flags, kLong, rl_src[0], rl_src[1], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 break;
643
644 case Instruction::IPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000645 GenIPut(mir, opt_flags, kWord, rl_src[0], rl_src[1], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 break;
647
648 case Instruction::IPUT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000649 GenIPut(mir, opt_flags, kWord, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 break;
651
652 case Instruction::IPUT_BOOLEAN:
653 case Instruction::IPUT_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000654 GenIPut(mir, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 break;
656
657 case Instruction::IPUT_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000658 GenIPut(mir, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 break;
660
661 case Instruction::IPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000662 GenIPut(mir, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 break;
664
665 case Instruction::SGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000666 GenSget(mir, rl_dest, false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 break;
668 case Instruction::SGET:
669 case Instruction::SGET_BOOLEAN:
670 case Instruction::SGET_BYTE:
671 case Instruction::SGET_CHAR:
672 case Instruction::SGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000673 GenSget(mir, rl_dest, false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 break;
675
676 case Instruction::SGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000677 GenSget(mir, rl_dest, true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 break;
679
680 case Instruction::SPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000681 GenSput(mir, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 break;
683
684 case Instruction::SPUT:
685 case Instruction::SPUT_BOOLEAN:
686 case Instruction::SPUT_BYTE:
687 case Instruction::SPUT_CHAR:
688 case Instruction::SPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000689 GenSput(mir, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 break;
691
692 case Instruction::SPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000693 GenSput(mir, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700694 break;
695
696 case Instruction::INVOKE_STATIC_RANGE:
697 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, true));
698 break;
699 case Instruction::INVOKE_STATIC:
700 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, false));
701 break;
702
703 case Instruction::INVOKE_DIRECT:
704 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, false));
705 break;
706 case Instruction::INVOKE_DIRECT_RANGE:
707 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, true));
708 break;
709
710 case Instruction::INVOKE_VIRTUAL:
711 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, false));
712 break;
713 case Instruction::INVOKE_VIRTUAL_RANGE:
714 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, true));
715 break;
716
717 case Instruction::INVOKE_SUPER:
718 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, false));
719 break;
720 case Instruction::INVOKE_SUPER_RANGE:
721 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, true));
722 break;
723
724 case Instruction::INVOKE_INTERFACE:
725 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, false));
726 break;
727 case Instruction::INVOKE_INTERFACE_RANGE:
728 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, true));
729 break;
730
731 case Instruction::NEG_INT:
732 case Instruction::NOT_INT:
733 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[0]);
734 break;
735
736 case Instruction::NEG_LONG:
737 case Instruction::NOT_LONG:
738 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[0]);
739 break;
740
741 case Instruction::NEG_FLOAT:
742 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[0]);
743 break;
744
745 case Instruction::NEG_DOUBLE:
746 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[0]);
747 break;
748
749 case Instruction::INT_TO_LONG:
750 GenIntToLong(rl_dest, rl_src[0]);
751 break;
752
753 case Instruction::LONG_TO_INT:
754 rl_src[0] = UpdateLocWide(rl_src[0]);
755 rl_src[0] = WideToNarrow(rl_src[0]);
756 StoreValue(rl_dest, rl_src[0]);
757 break;
758
759 case Instruction::INT_TO_BYTE:
760 case Instruction::INT_TO_SHORT:
761 case Instruction::INT_TO_CHAR:
762 GenIntNarrowing(opcode, rl_dest, rl_src[0]);
763 break;
764
765 case Instruction::INT_TO_FLOAT:
766 case Instruction::INT_TO_DOUBLE:
767 case Instruction::LONG_TO_FLOAT:
768 case Instruction::LONG_TO_DOUBLE:
769 case Instruction::FLOAT_TO_INT:
770 case Instruction::FLOAT_TO_LONG:
771 case Instruction::FLOAT_TO_DOUBLE:
772 case Instruction::DOUBLE_TO_INT:
773 case Instruction::DOUBLE_TO_LONG:
774 case Instruction::DOUBLE_TO_FLOAT:
775 GenConversion(opcode, rl_dest, rl_src[0]);
776 break;
777
778
779 case Instruction::ADD_INT:
780 case Instruction::ADD_INT_2ADDR:
781 case Instruction::MUL_INT:
782 case Instruction::MUL_INT_2ADDR:
783 case Instruction::AND_INT:
784 case Instruction::AND_INT_2ADDR:
785 case Instruction::OR_INT:
786 case Instruction::OR_INT_2ADDR:
787 case Instruction::XOR_INT:
788 case Instruction::XOR_INT_2ADDR:
789 if (rl_src[0].is_const &&
790 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[0]))) {
791 GenArithOpIntLit(opcode, rl_dest, rl_src[1],
792 mir_graph_->ConstantValue(rl_src[0].orig_sreg));
793 } else if (rl_src[1].is_const &&
794 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
795 GenArithOpIntLit(opcode, rl_dest, rl_src[0],
796 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
797 } else {
798 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
799 }
800 break;
801
802 case Instruction::SUB_INT:
803 case Instruction::SUB_INT_2ADDR:
804 case Instruction::DIV_INT:
805 case Instruction::DIV_INT_2ADDR:
806 case Instruction::REM_INT:
807 case Instruction::REM_INT_2ADDR:
808 case Instruction::SHL_INT:
809 case Instruction::SHL_INT_2ADDR:
810 case Instruction::SHR_INT:
811 case Instruction::SHR_INT_2ADDR:
812 case Instruction::USHR_INT:
813 case Instruction::USHR_INT_2ADDR:
814 if (rl_src[1].is_const &&
815 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
816 GenArithOpIntLit(opcode, rl_dest, rl_src[0], mir_graph_->ConstantValue(rl_src[1]));
817 } else {
818 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
819 }
820 break;
821
822 case Instruction::ADD_LONG:
823 case Instruction::SUB_LONG:
824 case Instruction::AND_LONG:
825 case Instruction::OR_LONG:
826 case Instruction::XOR_LONG:
827 case Instruction::ADD_LONG_2ADDR:
828 case Instruction::SUB_LONG_2ADDR:
829 case Instruction::AND_LONG_2ADDR:
830 case Instruction::OR_LONG_2ADDR:
831 case Instruction::XOR_LONG_2ADDR:
832 if (rl_src[0].is_const || rl_src[1].is_const) {
833 GenArithImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
834 break;
835 }
836 // Note: intentional fallthrough.
837
838 case Instruction::MUL_LONG:
839 case Instruction::DIV_LONG:
840 case Instruction::REM_LONG:
841 case Instruction::MUL_LONG_2ADDR:
842 case Instruction::DIV_LONG_2ADDR:
843 case Instruction::REM_LONG_2ADDR:
844 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
845 break;
846
847 case Instruction::SHL_LONG:
848 case Instruction::SHR_LONG:
849 case Instruction::USHR_LONG:
850 case Instruction::SHL_LONG_2ADDR:
851 case Instruction::SHR_LONG_2ADDR:
852 case Instruction::USHR_LONG_2ADDR:
853 if (rl_src[1].is_const) {
854 GenShiftImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
855 } else {
856 GenShiftOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
857 }
858 break;
859
860 case Instruction::ADD_FLOAT:
861 case Instruction::SUB_FLOAT:
862 case Instruction::MUL_FLOAT:
863 case Instruction::DIV_FLOAT:
864 case Instruction::REM_FLOAT:
865 case Instruction::ADD_FLOAT_2ADDR:
866 case Instruction::SUB_FLOAT_2ADDR:
867 case Instruction::MUL_FLOAT_2ADDR:
868 case Instruction::DIV_FLOAT_2ADDR:
869 case Instruction::REM_FLOAT_2ADDR:
870 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[1]);
871 break;
872
873 case Instruction::ADD_DOUBLE:
874 case Instruction::SUB_DOUBLE:
875 case Instruction::MUL_DOUBLE:
876 case Instruction::DIV_DOUBLE:
877 case Instruction::REM_DOUBLE:
878 case Instruction::ADD_DOUBLE_2ADDR:
879 case Instruction::SUB_DOUBLE_2ADDR:
880 case Instruction::MUL_DOUBLE_2ADDR:
881 case Instruction::DIV_DOUBLE_2ADDR:
882 case Instruction::REM_DOUBLE_2ADDR:
883 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[1]);
884 break;
885
886 case Instruction::RSUB_INT:
887 case Instruction::ADD_INT_LIT16:
888 case Instruction::MUL_INT_LIT16:
889 case Instruction::DIV_INT_LIT16:
890 case Instruction::REM_INT_LIT16:
891 case Instruction::AND_INT_LIT16:
892 case Instruction::OR_INT_LIT16:
893 case Instruction::XOR_INT_LIT16:
894 case Instruction::ADD_INT_LIT8:
895 case Instruction::RSUB_INT_LIT8:
896 case Instruction::MUL_INT_LIT8:
897 case Instruction::DIV_INT_LIT8:
898 case Instruction::REM_INT_LIT8:
899 case Instruction::AND_INT_LIT8:
900 case Instruction::OR_INT_LIT8:
901 case Instruction::XOR_INT_LIT8:
902 case Instruction::SHL_INT_LIT8:
903 case Instruction::SHR_INT_LIT8:
904 case Instruction::USHR_INT_LIT8:
905 GenArithOpIntLit(opcode, rl_dest, rl_src[0], vC);
906 break;
907
908 default:
909 LOG(FATAL) << "Unexpected opcode: " << opcode;
910 }
Brian Carlstrom1895ea32013-07-18 13:28:37 -0700911} // NOLINT(readability/fn_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700912
913// Process extended MIR instructions
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700914void Mir2Lir::HandleExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700915 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
916 case kMirOpCopy: {
917 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
918 RegLocation rl_dest = mir_graph_->GetDest(mir);
919 StoreValue(rl_dest, rl_src);
920 break;
921 }
922 case kMirOpFusedCmplFloat:
923 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, false /*double*/);
924 break;
925 case kMirOpFusedCmpgFloat:
926 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, false /*double*/);
927 break;
928 case kMirOpFusedCmplDouble:
929 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, true /*double*/);
930 break;
931 case kMirOpFusedCmpgDouble:
932 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, true /*double*/);
933 break;
934 case kMirOpFusedCmpLong:
935 GenFusedLongCmpBranch(bb, mir);
936 break;
937 case kMirOpSelect:
938 GenSelect(bb, mir);
939 break;
940 default:
941 break;
942 }
943}
944
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800945void Mir2Lir::GenPrintLabel(MIR* mir) {
946 // Mark the beginning of a Dalvik instruction for line tracking.
947 if (cu_->verbose) {
948 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
949 MarkBoundary(mir->offset, inst_str);
950 }
951}
952
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953// Handle the content in each basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700954bool Mir2Lir::MethodBlockCodeGen(BasicBlock* bb) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 if (bb->block_type == kDead) return false;
956 current_dalvik_offset_ = bb->start_offset;
957 MIR* mir;
958 int block_id = bb->id;
959
960 block_label_list_[block_id].operands[0] = bb->start_offset;
961
962 // Insert the block label.
963 block_label_list_[block_id].opcode = kPseudoNormalBlockLabel;
buzbeeb48819d2013-09-14 16:15:25 -0700964 block_label_list_[block_id].flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700965 AppendLIR(&block_label_list_[block_id]);
966
967 LIR* head_lir = NULL;
968
969 // If this is a catch block, export the start address.
970 if (bb->catch_entry) {
971 head_lir = NewLIR0(kPseudoExportedPC);
972 }
973
974 // Free temp registers and reset redundant store tracking.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 ClobberAllRegs();
976
977 if (bb->block_type == kEntryBlock) {
buzbee56c71782013-09-05 17:13:19 -0700978 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700979 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
980 GenEntrySequence(&mir_graph_->reg_location_[start_vreg],
981 mir_graph_->reg_location_[mir_graph_->GetMethodSReg()]);
982 } else if (bb->block_type == kExitBlock) {
buzbee56c71782013-09-05 17:13:19 -0700983 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700984 GenExitSequence();
985 }
986
987 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
988 ResetRegPool();
989 if (cu_->disable_opt & (1 << kTrackLiveTemps)) {
990 ClobberAllRegs();
991 }
992
993 if (cu_->disable_opt & (1 << kSuppressLoads)) {
994 ResetDefTracking();
995 }
996
997 // Reset temp tracking sanity check.
998 if (kIsDebugBuild) {
999 live_sreg_ = INVALID_SREG;
1000 }
1001
1002 current_dalvik_offset_ = mir->offset;
1003 int opcode = mir->dalvikInsn.opcode;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001005 GenPrintLabel(mir);
1006
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 // Remember the first LIR for this block.
1008 if (head_lir == NULL) {
buzbee252254b2013-09-08 16:20:53 -07001009 head_lir = &block_label_list_[bb->id];
1010 // Set the first label as a scheduling barrier.
buzbeeb48819d2013-09-14 16:15:25 -07001011 DCHECK(!head_lir->flags.use_def_invalid);
1012 head_lir->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
1014
1015 if (opcode == kMirOpCheck) {
1016 // Combine check and work halves of throwing instruction.
1017 MIR* work_half = mir->meta.throw_insn;
1018 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Vladimir Marko4376c872014-01-23 12:39:29 +00001019 mir->meta = work_half->meta; // Whatever the work_half had, we need to copy it.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001020 opcode = work_half->dalvikInsn.opcode;
1021 SSARepresentation* ssa_rep = work_half->ssa_rep;
1022 work_half->ssa_rep = mir->ssa_rep;
1023 mir->ssa_rep = ssa_rep;
1024 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Vladimir Marko4376c872014-01-23 12:39:29 +00001025 work_half->meta.throw_insn = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001026 }
1027
1028 if (opcode >= kMirOpFirst) {
1029 HandleExtendedMethodMIR(bb, mir);
1030 continue;
1031 }
1032
1033 CompileDalvikInstruction(mir, bb, block_label_list_);
1034 }
1035
1036 if (head_lir) {
1037 // Eliminate redundant loads/stores and delay stores into later slots.
1038 ApplyLocalOptimizations(head_lir, last_lir_insn_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 }
1040 return false;
1041}
1042
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001043bool Mir2Lir::SpecialMIR2LIR(const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +00001044 cu_->NewTimingSplit("SpecialMIR2LIR");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 // Find the first DalvikByteCode block.
1046 int num_reachable_blocks = mir_graph_->GetNumReachableBlocks();
1047 BasicBlock*bb = NULL;
1048 for (int idx = 0; idx < num_reachable_blocks; idx++) {
1049 // TODO: no direct access of growable lists.
1050 int dfs_index = mir_graph_->GetDfsOrder()->Get(idx);
1051 bb = mir_graph_->GetBasicBlock(dfs_index);
1052 if (bb->block_type == kDalvikByteCode) {
1053 break;
1054 }
1055 }
1056 if (bb == NULL) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001057 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058 }
1059 DCHECK_EQ(bb->start_offset, 0);
1060 DCHECK(bb->first_mir_insn != NULL);
1061
1062 // Get the first instruction.
1063 MIR* mir = bb->first_mir_insn;
1064
1065 // Free temp registers and reset redundant store tracking.
1066 ResetRegPool();
1067 ResetDefTracking();
1068 ClobberAllRegs();
1069
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001070 return GenSpecialCase(bb, mir, special);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001071}
1072
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001073void Mir2Lir::MethodMIR2LIR() {
buzbeea61f4952013-08-23 14:27:06 -07001074 cu_->NewTimingSplit("MIR2LIR");
1075
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 // Hold the labels of each block.
1077 block_label_list_ =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001078 static_cast<LIR*>(arena_->Alloc(sizeof(LIR) * mir_graph_->GetNumBlocks(),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001079 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080
buzbee56c71782013-09-05 17:13:19 -07001081 PreOrderDfsIterator iter(mir_graph_);
buzbee252254b2013-09-08 16:20:53 -07001082 BasicBlock* curr_bb = iter.Next();
1083 BasicBlock* next_bb = iter.Next();
1084 while (curr_bb != NULL) {
1085 MethodBlockCodeGen(curr_bb);
1086 // If the fall_through block is no longer laid out consecutively, drop in a branch.
buzbee0d829482013-10-11 15:24:55 -07001087 BasicBlock* curr_bb_fall_through = mir_graph_->GetBasicBlock(curr_bb->fall_through);
1088 if ((curr_bb_fall_through != NULL) && (curr_bb_fall_through != next_bb)) {
1089 OpUnconditionalBranch(&block_label_list_[curr_bb->fall_through]);
buzbee252254b2013-09-08 16:20:53 -07001090 }
1091 curr_bb = next_bb;
1092 do {
1093 next_bb = iter.Next();
1094 } while ((next_bb != NULL) && (next_bb->block_type == kDead));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001096 HandleSlowPaths();
1097
buzbeea61f4952013-08-23 14:27:06 -07001098 cu_->NewTimingSplit("Launchpads");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 HandleSuspendLaunchPads();
1100
1101 HandleThrowLaunchPads();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001102}
1103
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001104//
1105// LIR Slow Path
1106//
1107
1108LIR* Mir2Lir::LIRSlowPath::GenerateTargetLabel() {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001109 m2l_->SetCurrentDexPc(current_dex_pc_);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001110 LIR* target = m2l_->NewLIR0(kPseudoTargetLabel);
1111 fromfast_->target = target;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001112 return target;
1113}
Vladimir Marko3bc86152014-03-13 14:11:28 +00001114
Brian Carlstrom7940e442013-07-12 13:46:57 -07001115} // namespace art