blob: 2c4ca8885ac79d165f92e2d0df8b368643eb603f [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.
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010040RegStorage Mir2Lir::LoadArg(int in_position, RegisterClass reg_class, bool wide) {
buzbee2700f7e2014-03-07 09:46:20 -080041 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
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000045 int offset = StackVisitor::GetOutVROffset(in_position, cu_->instruction_set);
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()) {
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010059 RegStorage new_regs = AllocTypedTempWide(false, reg_class);
Vladimir Marko3bf7c602014-05-07 14:55:43 +010060 LoadBaseDisp(TargetReg(kSp), offset, new_regs, k64);
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010061 return new_regs; // The reg_class is OK, we can return.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080062 } else {
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010063 // Assume that no ABI allows splitting a wide fp reg between a narrow fp reg and memory,
64 // i.e. the low part is in a core reg. Load the second part in a core reg as well for now.
65 DCHECK(!reg_arg_low.IsFloat());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080066 reg_arg_high = AllocTemp();
67 int offset_high = offset + sizeof(uint32_t);
buzbee695d13a2014-04-19 13:32:20 -070068 Load32Disp(TargetReg(kSp), offset_high, reg_arg_high);
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010069 // Continue below to check the reg_class.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080070 }
71 }
72
73 // If the low part is not in a register yet, we need to load it.
buzbee2700f7e2014-03-07 09:46:20 -080074 if (!reg_arg_low.Valid()) {
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010075 // Assume that if the low part of a wide arg is passed in memory, so is the high part,
76 // thus we don't get here for wide args as it's handled above. Big-endian ABIs could
77 // conceivably break this assumption but Android supports only little-endian architectures.
78 DCHECK(!wide);
79 reg_arg_low = AllocTypedTemp(false, reg_class);
buzbee695d13a2014-04-19 13:32:20 -070080 Load32Disp(TargetReg(kSp), offset, reg_arg_low);
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010081 return reg_arg_low; // The reg_class is OK, we can return.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080082 }
83
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010084 RegStorage reg_arg = wide ? RegStorage::MakeRegPair(reg_arg_low, reg_arg_high) : reg_arg_low;
85 // Check if we need to copy the arg to a different reg_class.
86 if (!RegClassMatches(reg_class, reg_arg)) {
87 if (wide) {
88 RegStorage new_regs = AllocTypedTempWide(false, reg_class);
89 OpRegCopyWide(new_regs, reg_arg);
90 reg_arg = new_regs;
91 } else {
92 RegStorage new_reg = AllocTypedTemp(false, reg_class);
93 OpRegCopy(new_reg, reg_arg);
94 reg_arg = new_reg;
95 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080096 }
Vladimir Markoc93ac8b2014-05-13 17:53:49 +010097 return reg_arg;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080098}
99
100void Mir2Lir::LoadArgDirect(int in_position, RegLocation rl_dest) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000101 int offset = StackVisitor::GetOutVROffset(in_position, cu_->instruction_set);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700102 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800103 /*
104 * When doing a call for x86, it moves the stack pointer in order to push return.
105 * Thus, we add another 4 bytes to figure out the out of caller (in of callee).
106 * TODO: This needs revisited for 64-bit.
107 */
108 offset += sizeof(uint32_t);
109 }
110
111 if (!rl_dest.wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800112 RegStorage reg = GetArgMappingToPhysicalReg(in_position);
113 if (reg.Valid()) {
114 OpRegCopy(rl_dest.reg, reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800115 } else {
buzbee695d13a2014-04-19 13:32:20 -0700116 Load32Disp(TargetReg(kSp), offset, rl_dest.reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800117 }
118 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800119 RegStorage reg_arg_low = GetArgMappingToPhysicalReg(in_position);
120 RegStorage reg_arg_high = GetArgMappingToPhysicalReg(in_position + 1);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800121
buzbee2700f7e2014-03-07 09:46:20 -0800122 if (reg_arg_low.Valid() && reg_arg_high.Valid()) {
123 OpRegCopyWide(rl_dest.reg, RegStorage::MakeRegPair(reg_arg_low, reg_arg_high));
124 } else if (reg_arg_low.Valid() && !reg_arg_high.Valid()) {
125 OpRegCopy(rl_dest.reg, reg_arg_low);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800126 int offset_high = offset + sizeof(uint32_t);
buzbee695d13a2014-04-19 13:32:20 -0700127 Load32Disp(TargetReg(kSp), offset_high, rl_dest.reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800128 } else if (!reg_arg_low.Valid() && reg_arg_high.Valid()) {
129 OpRegCopy(rl_dest.reg.GetHigh(), reg_arg_high);
buzbee695d13a2014-04-19 13:32:20 -0700130 Load32Disp(TargetReg(kSp), offset, rl_dest.reg.GetLow());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800131 } else {
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100132 LoadBaseDisp(TargetReg(kSp), offset, rl_dest.reg, k64);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800133 }
134 }
135}
136
137bool Mir2Lir::GenSpecialIGet(MIR* mir, const InlineMethod& special) {
138 // FastInstance() already checked by DexFileMethodInliner.
139 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100140 if (data.method_is_static != 0u || data.object_arg != 0u) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800141 // The object is not "this" and has to be null-checked.
142 return false;
143 }
144
Vladimir Markoe3e02602014-03-12 15:42:41 +0000145 bool wide = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE));
Vladimir Marko455759b2014-05-06 20:49:36 +0100146 bool ref = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT));
147 OpSize size = LoadStoreOpSize(wide, ref);
Vladimir Marko674744e2014-04-24 15:18:26 +0100148 if (data.is_volatile && !SupportsVolatileLoadStore(size)) {
149 return false;
150 }
Vladimir Marko455759b2014-05-06 20:49:36 +0100151
Vladimir Markoe3e02602014-03-12 15:42:41 +0000152 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800153 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
154
155 // Point of no return - no aborts after this
156 GenPrintLabel(mir);
157 LockArg(data.object_arg);
Vladimir Markoc93ac8b2014-05-13 17:53:49 +0100158 RegStorage reg_obj = LoadArg(data.object_arg, kCoreReg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800159 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
Vladimir Markoc93ac8b2014-05-13 17:53:49 +0100160 RegisterClass reg_class = RegClassForFieldLoadStore(size, data.is_volatile);
161 RegStorage r_result = rl_dest.reg;
162 if (!RegClassMatches(reg_class, r_result)) {
163 r_result = wide ? AllocTypedTempWide(rl_dest.fp, reg_class)
164 : AllocTypedTemp(rl_dest.fp, reg_class);
165 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800166 if (data.is_volatile) {
Vladimir Markoc93ac8b2014-05-13 17:53:49 +0100167 LoadBaseDispVolatile(reg_obj, data.field_offset, r_result, size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800168 // Without context sensitive analysis, we must issue the most conservative barriers.
169 // In this case, either a load or store may follow so we issue both barriers.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800170 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800171 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100172 } else {
Vladimir Markoc93ac8b2014-05-13 17:53:49 +0100173 LoadBaseDisp(reg_obj, data.field_offset, r_result, size);
174 }
175 if (r_result != rl_dest.reg) {
176 if (wide) {
177 OpRegCopyWide(rl_dest.reg, r_result);
178 } else {
179 OpRegCopy(rl_dest.reg, r_result);
180 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800181 }
182 return true;
183}
184
185bool Mir2Lir::GenSpecialIPut(MIR* mir, const InlineMethod& special) {
186 // FastInstance() already checked by DexFileMethodInliner.
187 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100188 if (data.method_is_static != 0u || data.object_arg != 0u) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800189 // The object is not "this" and has to be null-checked.
190 return false;
191 }
Vladimir Markoe1fced12014-04-04 14:52:53 +0100192 if (data.return_arg_plus1 != 0u) {
193 // The setter returns a method argument which we don't support here.
194 return false;
195 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800196
Vladimir Markoe3e02602014-03-12 15:42:41 +0000197 bool wide = (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE));
Vladimir Marko455759b2014-05-06 20:49:36 +0100198 bool ref = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT));
199 OpSize size = LoadStoreOpSize(wide, ref);
Vladimir Marko674744e2014-04-24 15:18:26 +0100200 if (data.is_volatile && !SupportsVolatileLoadStore(size)) {
201 return false;
202 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800203
204 // Point of no return - no aborts after this
205 GenPrintLabel(mir);
206 LockArg(data.object_arg);
207 LockArg(data.src_arg, wide);
Vladimir Markoc93ac8b2014-05-13 17:53:49 +0100208 RegStorage reg_obj = LoadArg(data.object_arg, kCoreReg);
209 RegisterClass reg_class = RegClassForFieldLoadStore(size, data.is_volatile);
210 RegStorage reg_src = LoadArg(data.src_arg, reg_class, wide);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800211 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800212 // There might have been a store before this volatile one so insert StoreStore barrier.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800213 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100214 StoreBaseDispVolatile(reg_obj, data.field_offset, reg_src, size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800215 // A load might follow the volatile store so insert a StoreLoad barrier.
216 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100217 } else {
218 StoreBaseDisp(reg_obj, data.field_offset, reg_src, size);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800219 }
Vladimir Marko455759b2014-05-06 20:49:36 +0100220 if (ref) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800221 MarkGCCard(reg_src, reg_obj);
222 }
223 return true;
224}
225
226bool Mir2Lir::GenSpecialIdentity(MIR* mir, const InlineMethod& special) {
227 const InlineReturnArgData& data = special.d.return_data;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000228 bool wide = (data.is_wide != 0u);
229 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800230 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
231
232 // Point of no return - no aborts after this
233 GenPrintLabel(mir);
234 LockArg(data.arg, wide);
235 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
236 LoadArgDirect(data.arg, rl_dest);
237 return true;
238}
239
240/*
241 * Special-case code generation for simple non-throwing leaf methods.
242 */
243bool Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
244 DCHECK(special.flags & kInlineSpecial);
245 current_dalvik_offset_ = mir->offset;
246 MIR* return_mir = nullptr;
247 bool successful = false;
248
249 switch (special.opcode) {
250 case kInlineOpNop:
251 successful = true;
252 DCHECK_EQ(mir->dalvikInsn.opcode, Instruction::RETURN_VOID);
253 return_mir = mir;
254 break;
255 case kInlineOpNonWideConst: {
256 successful = true;
257 RegLocation rl_dest = GetReturn(cu_->shorty[0] == 'F');
258 GenPrintLabel(mir);
buzbee2700f7e2014-03-07 09:46:20 -0800259 LoadConstant(rl_dest.reg, static_cast<int>(special.d.data));
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700260 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800261 break;
262 }
263 case kInlineOpReturnArg:
264 successful = GenSpecialIdentity(mir, special);
265 return_mir = mir;
266 break;
267 case kInlineOpIGet:
268 successful = GenSpecialIGet(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700269 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800270 break;
271 case kInlineOpIPut:
272 successful = GenSpecialIPut(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700273 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800274 break;
275 default:
276 break;
277 }
278
279 if (successful) {
Vladimir Marko39d95e62014-02-28 12:51:24 +0000280 if (kIsDebugBuild) {
281 // Clear unreachable catch entries.
282 mir_graph_->catches_.clear();
283 }
284
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800285 // Handle verbosity for return MIR.
286 if (return_mir != nullptr) {
287 current_dalvik_offset_ = return_mir->offset;
288 // Not handling special identity case because it already generated code as part
289 // of the return. The label should have been added before any code was generated.
290 if (special.opcode != kInlineOpReturnArg) {
291 GenPrintLabel(return_mir);
292 }
293 }
294 GenSpecialExitSequence();
295
296 core_spill_mask_ = 0;
297 num_core_spills_ = 0;
298 fp_spill_mask_ = 0;
299 num_fp_spills_ = 0;
300 frame_size_ = 0;
301 core_vmap_table_.clear();
302 fp_vmap_table_.clear();
303 }
304
305 return successful;
306}
307
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308/*
309 * Target-independent code generation. Use only high-level
310 * load/store utilities here, or target-dependent genXX() handlers
311 * when necessary.
312 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700313void Mir2Lir::CompileDalvikInstruction(MIR* mir, BasicBlock* bb, LIR* label_list) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 RegLocation rl_src[3];
315 RegLocation rl_dest = mir_graph_->GetBadLoc();
316 RegLocation rl_result = mir_graph_->GetBadLoc();
317 Instruction::Code opcode = mir->dalvikInsn.opcode;
318 int opt_flags = mir->optimization_flags;
319 uint32_t vB = mir->dalvikInsn.vB;
320 uint32_t vC = mir->dalvikInsn.vC;
321
322 // Prep Src and Dest locations.
323 int next_sreg = 0;
324 int next_loc = 0;
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700325 uint64_t attrs = MIRGraph::GetDataFlowAttributes(opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 rl_src[0] = rl_src[1] = rl_src[2] = mir_graph_->GetBadLoc();
327 if (attrs & DF_UA) {
328 if (attrs & DF_A_WIDE) {
329 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
330 next_sreg+= 2;
331 } else {
332 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
333 next_sreg++;
334 }
335 }
336 if (attrs & DF_UB) {
337 if (attrs & DF_B_WIDE) {
338 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
339 next_sreg+= 2;
340 } else {
341 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
342 next_sreg++;
343 }
344 }
345 if (attrs & DF_UC) {
346 if (attrs & DF_C_WIDE) {
347 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
348 } else {
349 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
350 }
351 }
352 if (attrs & DF_DA) {
353 if (attrs & DF_A_WIDE) {
354 rl_dest = mir_graph_->GetDestWide(mir);
355 } else {
356 rl_dest = mir_graph_->GetDest(mir);
357 }
358 }
359 switch (opcode) {
360 case Instruction::NOP:
361 break;
362
363 case Instruction::MOVE_EXCEPTION:
364 GenMoveException(rl_dest);
365 break;
366
367 case Instruction::RETURN_VOID:
368 if (((cu_->access_flags & kAccConstructor) != 0) &&
369 cu_->compiler_driver->RequiresConstructorBarrier(Thread::Current(), cu_->dex_file,
370 cu_->class_def_idx)) {
371 GenMemBarrier(kStoreStore);
372 }
373 if (!mir_graph_->MethodIsLeaf()) {
374 GenSuspendTest(opt_flags);
375 }
376 break;
377
378 case Instruction::RETURN:
379 case Instruction::RETURN_OBJECT:
380 if (!mir_graph_->MethodIsLeaf()) {
381 GenSuspendTest(opt_flags);
382 }
383 StoreValue(GetReturn(cu_->shorty[0] == 'F'), rl_src[0]);
384 break;
385
386 case Instruction::RETURN_WIDE:
387 if (!mir_graph_->MethodIsLeaf()) {
388 GenSuspendTest(opt_flags);
389 }
390 StoreValueWide(GetReturnWide(cu_->shorty[0] == 'D'), rl_src[0]);
391 break;
392
393 case Instruction::MOVE_RESULT_WIDE:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000394 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000396 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 StoreValueWide(rl_dest, GetReturnWide(rl_dest.fp));
398 break;
399
400 case Instruction::MOVE_RESULT:
401 case Instruction::MOVE_RESULT_OBJECT:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000402 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000404 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 StoreValue(rl_dest, GetReturn(rl_dest.fp));
406 break;
407
408 case Instruction::MOVE:
409 case Instruction::MOVE_OBJECT:
410 case Instruction::MOVE_16:
411 case Instruction::MOVE_OBJECT_16:
412 case Instruction::MOVE_FROM16:
413 case Instruction::MOVE_OBJECT_FROM16:
414 StoreValue(rl_dest, rl_src[0]);
415 break;
416
417 case Instruction::MOVE_WIDE:
418 case Instruction::MOVE_WIDE_16:
419 case Instruction::MOVE_WIDE_FROM16:
420 StoreValueWide(rl_dest, rl_src[0]);
421 break;
422
423 case Instruction::CONST:
424 case Instruction::CONST_4:
425 case Instruction::CONST_16:
426 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800427 LoadConstantNoClobber(rl_result.reg, vB);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 StoreValue(rl_dest, rl_result);
429 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800430 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 }
432 break;
433
434 case Instruction::CONST_HIGH16:
435 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800436 LoadConstantNoClobber(rl_result.reg, vB << 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 StoreValue(rl_dest, rl_result);
438 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800439 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 }
441 break;
442
443 case Instruction::CONST_WIDE_16:
444 case Instruction::CONST_WIDE_32:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000445 GenConstWide(rl_dest, static_cast<int64_t>(static_cast<int32_t>(vB)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 break;
447
448 case Instruction::CONST_WIDE:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000449 GenConstWide(rl_dest, mir->dalvikInsn.vB_wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 break;
451
452 case Instruction::CONST_WIDE_HIGH16:
453 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800454 LoadConstantWide(rl_result.reg, static_cast<int64_t>(vB) << 48);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 StoreValueWide(rl_dest, rl_result);
456 break;
457
458 case Instruction::MONITOR_ENTER:
459 GenMonitorEnter(opt_flags, rl_src[0]);
460 break;
461
462 case Instruction::MONITOR_EXIT:
463 GenMonitorExit(opt_flags, rl_src[0]);
464 break;
465
466 case Instruction::CHECK_CAST: {
467 GenCheckCast(mir->offset, vB, rl_src[0]);
468 break;
469 }
470 case Instruction::INSTANCE_OF:
471 GenInstanceof(vC, rl_dest, rl_src[0]);
472 break;
473
474 case Instruction::NEW_INSTANCE:
475 GenNewInstance(vB, rl_dest);
476 break;
477
478 case Instruction::THROW:
479 GenThrow(rl_src[0]);
480 break;
481
482 case Instruction::ARRAY_LENGTH:
483 int len_offset;
484 len_offset = mirror::Array::LengthOffset().Int32Value();
485 rl_src[0] = LoadValue(rl_src[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800486 GenNullCheck(rl_src[0].reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700488 Load32Disp(rl_src[0].reg, len_offset, rl_result.reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700489 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 StoreValue(rl_dest, rl_result);
491 break;
492
493 case Instruction::CONST_STRING:
494 case Instruction::CONST_STRING_JUMBO:
495 GenConstString(vB, rl_dest);
496 break;
497
498 case Instruction::CONST_CLASS:
499 GenConstClass(vB, rl_dest);
500 break;
501
502 case Instruction::FILL_ARRAY_DATA:
503 GenFillArrayData(vB, rl_src[0]);
504 break;
505
506 case Instruction::FILLED_NEW_ARRAY:
507 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
508 false /* not range */));
509 break;
510
511 case Instruction::FILLED_NEW_ARRAY_RANGE:
512 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
513 true /* range */));
514 break;
515
516 case Instruction::NEW_ARRAY:
517 GenNewArray(vC, rl_dest, rl_src[0]);
518 break;
519
520 case Instruction::GOTO:
521 case Instruction::GOTO_16:
522 case Instruction::GOTO_32:
buzbee9329e6d2013-08-19 12:55:10 -0700523 if (mir_graph_->IsBackedge(bb, bb->taken)) {
buzbee0d829482013-10-11 15:24:55 -0700524 GenSuspendTestAndBranch(opt_flags, &label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 } else {
buzbee0d829482013-10-11 15:24:55 -0700526 OpUnconditionalBranch(&label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 }
528 break;
529
530 case Instruction::PACKED_SWITCH:
531 GenPackedSwitch(mir, vB, rl_src[0]);
532 break;
533
534 case Instruction::SPARSE_SWITCH:
535 GenSparseSwitch(mir, vB, rl_src[0]);
536 break;
537
538 case Instruction::CMPL_FLOAT:
539 case Instruction::CMPG_FLOAT:
540 case Instruction::CMPL_DOUBLE:
541 case Instruction::CMPG_DOUBLE:
542 GenCmpFP(opcode, rl_dest, rl_src[0], rl_src[1]);
543 break;
544
545 case Instruction::CMP_LONG:
546 GenCmpLong(rl_dest, rl_src[0], rl_src[1]);
547 break;
548
549 case Instruction::IF_EQ:
550 case Instruction::IF_NE:
551 case Instruction::IF_LT:
552 case Instruction::IF_GE:
553 case Instruction::IF_GT:
554 case Instruction::IF_LE: {
buzbee0d829482013-10-11 15:24:55 -0700555 LIR* taken = &label_list[bb->taken];
556 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 // Result known at compile time?
558 if (rl_src[0].is_const && rl_src[1].is_const) {
559 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg),
560 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
buzbee0d829482013-10-11 15:24:55 -0700561 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
562 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 GenSuspendTest(opt_flags);
564 }
buzbee0d829482013-10-11 15:24:55 -0700565 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700567 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 GenSuspendTest(opt_flags);
569 }
buzbee0d829482013-10-11 15:24:55 -0700570 GenCompareAndBranch(opcode, rl_src[0], rl_src[1], taken, fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 }
572 break;
573 }
574
575 case Instruction::IF_EQZ:
576 case Instruction::IF_NEZ:
577 case Instruction::IF_LTZ:
578 case Instruction::IF_GEZ:
579 case Instruction::IF_GTZ:
580 case Instruction::IF_LEZ: {
buzbee0d829482013-10-11 15:24:55 -0700581 LIR* taken = &label_list[bb->taken];
582 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 // Result known at compile time?
584 if (rl_src[0].is_const) {
585 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg), 0);
buzbee0d829482013-10-11 15:24:55 -0700586 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
587 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 GenSuspendTest(opt_flags);
589 }
buzbee0d829482013-10-11 15:24:55 -0700590 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700591 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700592 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 GenSuspendTest(opt_flags);
594 }
595 GenCompareZeroAndBranch(opcode, rl_src[0], taken, fall_through);
596 }
597 break;
598 }
599
600 case Instruction::AGET_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700601 GenArrayGet(opt_flags, k64, rl_src[0], rl_src[1], rl_dest, 3);
602 break;
603 case Instruction::AGET_OBJECT:
604 GenArrayGet(opt_flags, kReference, rl_src[0], rl_src[1], rl_dest, 2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 break;
606 case Instruction::AGET:
buzbee695d13a2014-04-19 13:32:20 -0700607 GenArrayGet(opt_flags, k32, rl_src[0], rl_src[1], rl_dest, 2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 break;
609 case Instruction::AGET_BOOLEAN:
610 GenArrayGet(opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
611 break;
612 case Instruction::AGET_BYTE:
613 GenArrayGet(opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
614 break;
615 case Instruction::AGET_CHAR:
616 GenArrayGet(opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
617 break;
618 case Instruction::AGET_SHORT:
619 GenArrayGet(opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
620 break;
621 case Instruction::APUT_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700622 GenArrayPut(opt_flags, k64, rl_src[1], rl_src[2], rl_src[0], 3, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 break;
624 case Instruction::APUT:
buzbee695d13a2014-04-19 13:32:20 -0700625 GenArrayPut(opt_flags, k32, rl_src[1], rl_src[2], rl_src[0], 2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700626 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700627 case Instruction::APUT_OBJECT: {
628 bool is_null = mir_graph_->IsConstantNullRef(rl_src[0]);
629 bool is_safe = is_null; // Always safe to store null.
630 if (!is_safe) {
631 // Check safety from verifier type information.
Vladimir Marko2730db02014-01-27 11:15:17 +0000632 const DexCompilationUnit* unit = mir_graph_->GetCurrentDexCompilationUnit();
633 is_safe = cu_->compiler_driver->IsSafeCast(unit, mir->offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700634 }
635 if (is_null || is_safe) {
636 // Store of constant null doesn't require an assignability test and can be generated inline
637 // without fixed register usage or a card mark.
buzbee695d13a2014-04-19 13:32:20 -0700638 GenArrayPut(opt_flags, kReference, rl_src[1], rl_src[2], rl_src[0], 2, !is_null);
Ian Rogersa9a82542013-10-04 11:17:26 -0700639 } else {
640 GenArrayObjPut(opt_flags, rl_src[1], rl_src[2], rl_src[0]);
641 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 case Instruction::APUT_SHORT:
645 case Instruction::APUT_CHAR:
Ian Rogersa9a82542013-10-04 11:17:26 -0700646 GenArrayPut(opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 break;
648 case Instruction::APUT_BYTE:
649 case Instruction::APUT_BOOLEAN:
Ian Rogersa9a82542013-10-04 11:17:26 -0700650 GenArrayPut(opt_flags, kUnsignedByte, rl_src[1], rl_src[2], rl_src[0], 0, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 break;
652
653 case Instruction::IGET_OBJECT:
buzbee695d13a2014-04-19 13:32:20 -0700654 GenIGet(mir, opt_flags, kReference, rl_dest, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 break;
656
657 case Instruction::IGET_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700658 GenIGet(mir, opt_flags, k64, rl_dest, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 break;
660
661 case Instruction::IGET:
buzbee695d13a2014-04-19 13:32:20 -0700662 GenIGet(mir, opt_flags, k32, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 break;
664
665 case Instruction::IGET_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000666 GenIGet(mir, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667 break;
668
669 case Instruction::IGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000670 GenIGet(mir, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 break;
672
673 case Instruction::IGET_BOOLEAN:
674 case Instruction::IGET_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000675 GenIGet(mir, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700676 break;
677
678 case Instruction::IPUT_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700679 GenIPut(mir, opt_flags, k64, rl_src[0], rl_src[1], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700680 break;
681
682 case Instruction::IPUT_OBJECT:
buzbee695d13a2014-04-19 13:32:20 -0700683 GenIPut(mir, opt_flags, kReference, rl_src[0], rl_src[1], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 break;
685
686 case Instruction::IPUT:
buzbee695d13a2014-04-19 13:32:20 -0700687 GenIPut(mir, opt_flags, k32, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 break;
689
690 case Instruction::IPUT_BOOLEAN:
691 case Instruction::IPUT_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000692 GenIPut(mir, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 break;
694
695 case Instruction::IPUT_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000696 GenIPut(mir, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 break;
698
699 case Instruction::IPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000700 GenIPut(mir, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 break;
702
703 case Instruction::SGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000704 GenSget(mir, rl_dest, false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 break;
706 case Instruction::SGET:
707 case Instruction::SGET_BOOLEAN:
708 case Instruction::SGET_BYTE:
709 case Instruction::SGET_CHAR:
710 case Instruction::SGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000711 GenSget(mir, rl_dest, false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700712 break;
713
714 case Instruction::SGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000715 GenSget(mir, rl_dest, true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 break;
717
718 case Instruction::SPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000719 GenSput(mir, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700720 break;
721
722 case Instruction::SPUT:
723 case Instruction::SPUT_BOOLEAN:
724 case Instruction::SPUT_BYTE:
725 case Instruction::SPUT_CHAR:
726 case Instruction::SPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000727 GenSput(mir, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 break;
729
730 case Instruction::SPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000731 GenSput(mir, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 break;
733
734 case Instruction::INVOKE_STATIC_RANGE:
735 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, true));
736 break;
737 case Instruction::INVOKE_STATIC:
738 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, false));
739 break;
740
741 case Instruction::INVOKE_DIRECT:
742 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, false));
743 break;
744 case Instruction::INVOKE_DIRECT_RANGE:
745 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, true));
746 break;
747
748 case Instruction::INVOKE_VIRTUAL:
749 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, false));
750 break;
751 case Instruction::INVOKE_VIRTUAL_RANGE:
752 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, true));
753 break;
754
755 case Instruction::INVOKE_SUPER:
756 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, false));
757 break;
758 case Instruction::INVOKE_SUPER_RANGE:
759 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, true));
760 break;
761
762 case Instruction::INVOKE_INTERFACE:
763 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, false));
764 break;
765 case Instruction::INVOKE_INTERFACE_RANGE:
766 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, true));
767 break;
768
769 case Instruction::NEG_INT:
770 case Instruction::NOT_INT:
771 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[0]);
772 break;
773
774 case Instruction::NEG_LONG:
775 case Instruction::NOT_LONG:
776 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[0]);
777 break;
778
779 case Instruction::NEG_FLOAT:
780 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[0]);
781 break;
782
783 case Instruction::NEG_DOUBLE:
784 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[0]);
785 break;
786
787 case Instruction::INT_TO_LONG:
788 GenIntToLong(rl_dest, rl_src[0]);
789 break;
790
791 case Instruction::LONG_TO_INT:
792 rl_src[0] = UpdateLocWide(rl_src[0]);
793 rl_src[0] = WideToNarrow(rl_src[0]);
794 StoreValue(rl_dest, rl_src[0]);
795 break;
796
797 case Instruction::INT_TO_BYTE:
798 case Instruction::INT_TO_SHORT:
799 case Instruction::INT_TO_CHAR:
800 GenIntNarrowing(opcode, rl_dest, rl_src[0]);
801 break;
802
803 case Instruction::INT_TO_FLOAT:
804 case Instruction::INT_TO_DOUBLE:
805 case Instruction::LONG_TO_FLOAT:
806 case Instruction::LONG_TO_DOUBLE:
807 case Instruction::FLOAT_TO_INT:
808 case Instruction::FLOAT_TO_LONG:
809 case Instruction::FLOAT_TO_DOUBLE:
810 case Instruction::DOUBLE_TO_INT:
811 case Instruction::DOUBLE_TO_LONG:
812 case Instruction::DOUBLE_TO_FLOAT:
813 GenConversion(opcode, rl_dest, rl_src[0]);
814 break;
815
816
817 case Instruction::ADD_INT:
818 case Instruction::ADD_INT_2ADDR:
819 case Instruction::MUL_INT:
820 case Instruction::MUL_INT_2ADDR:
821 case Instruction::AND_INT:
822 case Instruction::AND_INT_2ADDR:
823 case Instruction::OR_INT:
824 case Instruction::OR_INT_2ADDR:
825 case Instruction::XOR_INT:
826 case Instruction::XOR_INT_2ADDR:
827 if (rl_src[0].is_const &&
828 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[0]))) {
829 GenArithOpIntLit(opcode, rl_dest, rl_src[1],
830 mir_graph_->ConstantValue(rl_src[0].orig_sreg));
831 } else if (rl_src[1].is_const &&
832 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
833 GenArithOpIntLit(opcode, rl_dest, rl_src[0],
834 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
835 } else {
836 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
837 }
838 break;
839
840 case Instruction::SUB_INT:
841 case Instruction::SUB_INT_2ADDR:
842 case Instruction::DIV_INT:
843 case Instruction::DIV_INT_2ADDR:
844 case Instruction::REM_INT:
845 case Instruction::REM_INT_2ADDR:
846 case Instruction::SHL_INT:
847 case Instruction::SHL_INT_2ADDR:
848 case Instruction::SHR_INT:
849 case Instruction::SHR_INT_2ADDR:
850 case Instruction::USHR_INT:
851 case Instruction::USHR_INT_2ADDR:
852 if (rl_src[1].is_const &&
853 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
854 GenArithOpIntLit(opcode, rl_dest, rl_src[0], mir_graph_->ConstantValue(rl_src[1]));
855 } else {
856 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
857 }
858 break;
859
860 case Instruction::ADD_LONG:
861 case Instruction::SUB_LONG:
862 case Instruction::AND_LONG:
863 case Instruction::OR_LONG:
864 case Instruction::XOR_LONG:
865 case Instruction::ADD_LONG_2ADDR:
866 case Instruction::SUB_LONG_2ADDR:
867 case Instruction::AND_LONG_2ADDR:
868 case Instruction::OR_LONG_2ADDR:
869 case Instruction::XOR_LONG_2ADDR:
870 if (rl_src[0].is_const || rl_src[1].is_const) {
871 GenArithImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
872 break;
873 }
874 // Note: intentional fallthrough.
875
876 case Instruction::MUL_LONG:
877 case Instruction::DIV_LONG:
878 case Instruction::REM_LONG:
879 case Instruction::MUL_LONG_2ADDR:
880 case Instruction::DIV_LONG_2ADDR:
881 case Instruction::REM_LONG_2ADDR:
882 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
883 break;
884
885 case Instruction::SHL_LONG:
886 case Instruction::SHR_LONG:
887 case Instruction::USHR_LONG:
888 case Instruction::SHL_LONG_2ADDR:
889 case Instruction::SHR_LONG_2ADDR:
890 case Instruction::USHR_LONG_2ADDR:
891 if (rl_src[1].is_const) {
892 GenShiftImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
893 } else {
894 GenShiftOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
895 }
896 break;
897
898 case Instruction::ADD_FLOAT:
899 case Instruction::SUB_FLOAT:
900 case Instruction::MUL_FLOAT:
901 case Instruction::DIV_FLOAT:
902 case Instruction::REM_FLOAT:
903 case Instruction::ADD_FLOAT_2ADDR:
904 case Instruction::SUB_FLOAT_2ADDR:
905 case Instruction::MUL_FLOAT_2ADDR:
906 case Instruction::DIV_FLOAT_2ADDR:
907 case Instruction::REM_FLOAT_2ADDR:
908 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[1]);
909 break;
910
911 case Instruction::ADD_DOUBLE:
912 case Instruction::SUB_DOUBLE:
913 case Instruction::MUL_DOUBLE:
914 case Instruction::DIV_DOUBLE:
915 case Instruction::REM_DOUBLE:
916 case Instruction::ADD_DOUBLE_2ADDR:
917 case Instruction::SUB_DOUBLE_2ADDR:
918 case Instruction::MUL_DOUBLE_2ADDR:
919 case Instruction::DIV_DOUBLE_2ADDR:
920 case Instruction::REM_DOUBLE_2ADDR:
921 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[1]);
922 break;
923
924 case Instruction::RSUB_INT:
925 case Instruction::ADD_INT_LIT16:
926 case Instruction::MUL_INT_LIT16:
927 case Instruction::DIV_INT_LIT16:
928 case Instruction::REM_INT_LIT16:
929 case Instruction::AND_INT_LIT16:
930 case Instruction::OR_INT_LIT16:
931 case Instruction::XOR_INT_LIT16:
932 case Instruction::ADD_INT_LIT8:
933 case Instruction::RSUB_INT_LIT8:
934 case Instruction::MUL_INT_LIT8:
935 case Instruction::DIV_INT_LIT8:
936 case Instruction::REM_INT_LIT8:
937 case Instruction::AND_INT_LIT8:
938 case Instruction::OR_INT_LIT8:
939 case Instruction::XOR_INT_LIT8:
940 case Instruction::SHL_INT_LIT8:
941 case Instruction::SHR_INT_LIT8:
942 case Instruction::USHR_INT_LIT8:
943 GenArithOpIntLit(opcode, rl_dest, rl_src[0], vC);
944 break;
945
946 default:
947 LOG(FATAL) << "Unexpected opcode: " << opcode;
948 }
Brian Carlstrom1895ea32013-07-18 13:28:37 -0700949} // NOLINT(readability/fn_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950
951// Process extended MIR instructions
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700952void Mir2Lir::HandleExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
954 case kMirOpCopy: {
955 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
956 RegLocation rl_dest = mir_graph_->GetDest(mir);
957 StoreValue(rl_dest, rl_src);
958 break;
959 }
960 case kMirOpFusedCmplFloat:
961 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, false /*double*/);
962 break;
963 case kMirOpFusedCmpgFloat:
964 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, false /*double*/);
965 break;
966 case kMirOpFusedCmplDouble:
967 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, true /*double*/);
968 break;
969 case kMirOpFusedCmpgDouble:
970 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, true /*double*/);
971 break;
972 case kMirOpFusedCmpLong:
973 GenFusedLongCmpBranch(bb, mir);
974 break;
975 case kMirOpSelect:
976 GenSelect(bb, mir);
977 break;
978 default:
979 break;
980 }
981}
982
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800983void Mir2Lir::GenPrintLabel(MIR* mir) {
984 // Mark the beginning of a Dalvik instruction for line tracking.
985 if (cu_->verbose) {
986 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
987 MarkBoundary(mir->offset, inst_str);
988 }
989}
990
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991// Handle the content in each basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700992bool Mir2Lir::MethodBlockCodeGen(BasicBlock* bb) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 if (bb->block_type == kDead) return false;
994 current_dalvik_offset_ = bb->start_offset;
995 MIR* mir;
996 int block_id = bb->id;
997
998 block_label_list_[block_id].operands[0] = bb->start_offset;
999
1000 // Insert the block label.
1001 block_label_list_[block_id].opcode = kPseudoNormalBlockLabel;
buzbeeb48819d2013-09-14 16:15:25 -07001002 block_label_list_[block_id].flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003 AppendLIR(&block_label_list_[block_id]);
1004
1005 LIR* head_lir = NULL;
1006
1007 // If this is a catch block, export the start address.
1008 if (bb->catch_entry) {
1009 head_lir = NewLIR0(kPseudoExportedPC);
1010 }
1011
1012 // Free temp registers and reset redundant store tracking.
buzbeeba574512014-05-12 15:13:16 -07001013 ClobberAllTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014
1015 if (bb->block_type == kEntryBlock) {
buzbee56c71782013-09-05 17:13:19 -07001016 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
1018 GenEntrySequence(&mir_graph_->reg_location_[start_vreg],
1019 mir_graph_->reg_location_[mir_graph_->GetMethodSReg()]);
1020 } else if (bb->block_type == kExitBlock) {
buzbee56c71782013-09-05 17:13:19 -07001021 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 GenExitSequence();
1023 }
1024
1025 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
1026 ResetRegPool();
1027 if (cu_->disable_opt & (1 << kTrackLiveTemps)) {
buzbeeba574512014-05-12 15:13:16 -07001028 ClobberAllTemps();
buzbee7a11ab02014-04-28 20:02:38 -07001029 // Reset temp allocation to minimize differences when A/B testing.
buzbee091cc402014-03-31 10:14:40 -07001030 reg_pool_->ResetNextTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 }
1032
1033 if (cu_->disable_opt & (1 << kSuppressLoads)) {
1034 ResetDefTracking();
1035 }
1036
1037 // Reset temp tracking sanity check.
1038 if (kIsDebugBuild) {
1039 live_sreg_ = INVALID_SREG;
1040 }
1041
1042 current_dalvik_offset_ = mir->offset;
1043 int opcode = mir->dalvikInsn.opcode;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001044
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001045 GenPrintLabel(mir);
1046
Brian Carlstrom7940e442013-07-12 13:46:57 -07001047 // Remember the first LIR for this block.
1048 if (head_lir == NULL) {
buzbee252254b2013-09-08 16:20:53 -07001049 head_lir = &block_label_list_[bb->id];
1050 // Set the first label as a scheduling barrier.
buzbeeb48819d2013-09-14 16:15:25 -07001051 DCHECK(!head_lir->flags.use_def_invalid);
1052 head_lir->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001053 }
1054
1055 if (opcode == kMirOpCheck) {
1056 // Combine check and work halves of throwing instruction.
1057 MIR* work_half = mir->meta.throw_insn;
1058 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Vladimir Marko4376c872014-01-23 12:39:29 +00001059 mir->meta = work_half->meta; // Whatever the work_half had, we need to copy it.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001060 opcode = work_half->dalvikInsn.opcode;
1061 SSARepresentation* ssa_rep = work_half->ssa_rep;
1062 work_half->ssa_rep = mir->ssa_rep;
1063 mir->ssa_rep = ssa_rep;
1064 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Vladimir Marko4376c872014-01-23 12:39:29 +00001065 work_half->meta.throw_insn = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001066 }
1067
1068 if (opcode >= kMirOpFirst) {
1069 HandleExtendedMethodMIR(bb, mir);
1070 continue;
1071 }
1072
1073 CompileDalvikInstruction(mir, bb, block_label_list_);
1074 }
1075
1076 if (head_lir) {
1077 // Eliminate redundant loads/stores and delay stores into later slots.
1078 ApplyLocalOptimizations(head_lir, last_lir_insn_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001079 }
1080 return false;
1081}
1082
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001083bool Mir2Lir::SpecialMIR2LIR(const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +00001084 cu_->NewTimingSplit("SpecialMIR2LIR");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 // Find the first DalvikByteCode block.
1086 int num_reachable_blocks = mir_graph_->GetNumReachableBlocks();
1087 BasicBlock*bb = NULL;
1088 for (int idx = 0; idx < num_reachable_blocks; idx++) {
1089 // TODO: no direct access of growable lists.
1090 int dfs_index = mir_graph_->GetDfsOrder()->Get(idx);
1091 bb = mir_graph_->GetBasicBlock(dfs_index);
1092 if (bb->block_type == kDalvikByteCode) {
1093 break;
1094 }
1095 }
1096 if (bb == NULL) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001097 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098 }
1099 DCHECK_EQ(bb->start_offset, 0);
1100 DCHECK(bb->first_mir_insn != NULL);
1101
1102 // Get the first instruction.
1103 MIR* mir = bb->first_mir_insn;
1104
1105 // Free temp registers and reset redundant store tracking.
1106 ResetRegPool();
1107 ResetDefTracking();
buzbeeba574512014-05-12 15:13:16 -07001108 ClobberAllTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001109
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001110 return GenSpecialCase(bb, mir, special);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111}
1112
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001113void Mir2Lir::MethodMIR2LIR() {
buzbeea61f4952013-08-23 14:27:06 -07001114 cu_->NewTimingSplit("MIR2LIR");
1115
Brian Carlstrom7940e442013-07-12 13:46:57 -07001116 // Hold the labels of each block.
1117 block_label_list_ =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001118 static_cast<LIR*>(arena_->Alloc(sizeof(LIR) * mir_graph_->GetNumBlocks(),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001119 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120
buzbee56c71782013-09-05 17:13:19 -07001121 PreOrderDfsIterator iter(mir_graph_);
buzbee252254b2013-09-08 16:20:53 -07001122 BasicBlock* curr_bb = iter.Next();
1123 BasicBlock* next_bb = iter.Next();
1124 while (curr_bb != NULL) {
1125 MethodBlockCodeGen(curr_bb);
1126 // If the fall_through block is no longer laid out consecutively, drop in a branch.
buzbee0d829482013-10-11 15:24:55 -07001127 BasicBlock* curr_bb_fall_through = mir_graph_->GetBasicBlock(curr_bb->fall_through);
1128 if ((curr_bb_fall_through != NULL) && (curr_bb_fall_through != next_bb)) {
1129 OpUnconditionalBranch(&block_label_list_[curr_bb->fall_through]);
buzbee252254b2013-09-08 16:20:53 -07001130 }
1131 curr_bb = next_bb;
1132 do {
1133 next_bb = iter.Next();
1134 } while ((next_bb != NULL) && (next_bb->block_type == kDead));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001135 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001136 HandleSlowPaths();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001137}
1138
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001139//
1140// LIR Slow Path
1141//
1142
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001143LIR* Mir2Lir::LIRSlowPath::GenerateTargetLabel(int opcode) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001144 m2l_->SetCurrentDexPc(current_dex_pc_);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001145 LIR* target = m2l_->NewLIR0(opcode);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001146 fromfast_->target = target;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001147 return target;
1148}
Vladimir Marko3bc86152014-03-13 14:11:28 +00001149
Brian Carlstrom7940e442013-07-12 13:46:57 -07001150} // namespace art