blob: ea8071d11a89817808a501170db0cfed471d3e55 [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;
buzbee082833c2014-05-17 23:16:26 -0700321 DCHECK(CheckCorePoolSanity()) << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " @ 0x:"
322 << std::hex << current_dalvik_offset_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323
324 // Prep Src and Dest locations.
325 int next_sreg = 0;
326 int next_loc = 0;
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700327 uint64_t attrs = MIRGraph::GetDataFlowAttributes(opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 rl_src[0] = rl_src[1] = rl_src[2] = mir_graph_->GetBadLoc();
329 if (attrs & DF_UA) {
330 if (attrs & DF_A_WIDE) {
331 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
332 next_sreg+= 2;
333 } else {
334 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
335 next_sreg++;
336 }
337 }
338 if (attrs & DF_UB) {
339 if (attrs & DF_B_WIDE) {
340 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
341 next_sreg+= 2;
342 } else {
343 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
344 next_sreg++;
345 }
346 }
347 if (attrs & DF_UC) {
348 if (attrs & DF_C_WIDE) {
349 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
350 } else {
351 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
352 }
353 }
354 if (attrs & DF_DA) {
355 if (attrs & DF_A_WIDE) {
356 rl_dest = mir_graph_->GetDestWide(mir);
357 } else {
358 rl_dest = mir_graph_->GetDest(mir);
359 }
360 }
361 switch (opcode) {
362 case Instruction::NOP:
363 break;
364
365 case Instruction::MOVE_EXCEPTION:
366 GenMoveException(rl_dest);
367 break;
368
369 case Instruction::RETURN_VOID:
370 if (((cu_->access_flags & kAccConstructor) != 0) &&
371 cu_->compiler_driver->RequiresConstructorBarrier(Thread::Current(), cu_->dex_file,
372 cu_->class_def_idx)) {
373 GenMemBarrier(kStoreStore);
374 }
375 if (!mir_graph_->MethodIsLeaf()) {
376 GenSuspendTest(opt_flags);
377 }
378 break;
379
380 case Instruction::RETURN:
381 case Instruction::RETURN_OBJECT:
382 if (!mir_graph_->MethodIsLeaf()) {
383 GenSuspendTest(opt_flags);
384 }
385 StoreValue(GetReturn(cu_->shorty[0] == 'F'), rl_src[0]);
386 break;
387
388 case Instruction::RETURN_WIDE:
389 if (!mir_graph_->MethodIsLeaf()) {
390 GenSuspendTest(opt_flags);
391 }
392 StoreValueWide(GetReturnWide(cu_->shorty[0] == 'D'), rl_src[0]);
393 break;
394
395 case Instruction::MOVE_RESULT_WIDE:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000396 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000398 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 StoreValueWide(rl_dest, GetReturnWide(rl_dest.fp));
400 break;
401
402 case Instruction::MOVE_RESULT:
403 case Instruction::MOVE_RESULT_OBJECT:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000404 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000406 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 StoreValue(rl_dest, GetReturn(rl_dest.fp));
408 break;
409
410 case Instruction::MOVE:
411 case Instruction::MOVE_OBJECT:
412 case Instruction::MOVE_16:
413 case Instruction::MOVE_OBJECT_16:
414 case Instruction::MOVE_FROM16:
415 case Instruction::MOVE_OBJECT_FROM16:
416 StoreValue(rl_dest, rl_src[0]);
417 break;
418
419 case Instruction::MOVE_WIDE:
420 case Instruction::MOVE_WIDE_16:
421 case Instruction::MOVE_WIDE_FROM16:
422 StoreValueWide(rl_dest, rl_src[0]);
423 break;
424
425 case Instruction::CONST:
426 case Instruction::CONST_4:
427 case Instruction::CONST_16:
428 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800429 LoadConstantNoClobber(rl_result.reg, vB);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 StoreValue(rl_dest, rl_result);
431 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800432 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
434 break;
435
436 case Instruction::CONST_HIGH16:
437 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800438 LoadConstantNoClobber(rl_result.reg, vB << 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 StoreValue(rl_dest, rl_result);
440 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800441 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 }
443 break;
444
445 case Instruction::CONST_WIDE_16:
446 case Instruction::CONST_WIDE_32:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000447 GenConstWide(rl_dest, static_cast<int64_t>(static_cast<int32_t>(vB)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700448 break;
449
450 case Instruction::CONST_WIDE:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000451 GenConstWide(rl_dest, mir->dalvikInsn.vB_wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 break;
453
454 case Instruction::CONST_WIDE_HIGH16:
455 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800456 LoadConstantWide(rl_result.reg, static_cast<int64_t>(vB) << 48);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 StoreValueWide(rl_dest, rl_result);
458 break;
459
460 case Instruction::MONITOR_ENTER:
461 GenMonitorEnter(opt_flags, rl_src[0]);
462 break;
463
464 case Instruction::MONITOR_EXIT:
465 GenMonitorExit(opt_flags, rl_src[0]);
466 break;
467
468 case Instruction::CHECK_CAST: {
469 GenCheckCast(mir->offset, vB, rl_src[0]);
470 break;
471 }
472 case Instruction::INSTANCE_OF:
473 GenInstanceof(vC, rl_dest, rl_src[0]);
474 break;
475
476 case Instruction::NEW_INSTANCE:
477 GenNewInstance(vB, rl_dest);
478 break;
479
480 case Instruction::THROW:
481 GenThrow(rl_src[0]);
482 break;
483
484 case Instruction::ARRAY_LENGTH:
485 int len_offset;
486 len_offset = mirror::Array::LengthOffset().Int32Value();
487 rl_src[0] = LoadValue(rl_src[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800488 GenNullCheck(rl_src[0].reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700490 Load32Disp(rl_src[0].reg, len_offset, rl_result.reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700491 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 StoreValue(rl_dest, rl_result);
493 break;
494
495 case Instruction::CONST_STRING:
496 case Instruction::CONST_STRING_JUMBO:
497 GenConstString(vB, rl_dest);
498 break;
499
500 case Instruction::CONST_CLASS:
501 GenConstClass(vB, rl_dest);
502 break;
503
504 case Instruction::FILL_ARRAY_DATA:
505 GenFillArrayData(vB, rl_src[0]);
506 break;
507
508 case Instruction::FILLED_NEW_ARRAY:
509 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
510 false /* not range */));
511 break;
512
513 case Instruction::FILLED_NEW_ARRAY_RANGE:
514 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
515 true /* range */));
516 break;
517
518 case Instruction::NEW_ARRAY:
519 GenNewArray(vC, rl_dest, rl_src[0]);
520 break;
521
522 case Instruction::GOTO:
523 case Instruction::GOTO_16:
524 case Instruction::GOTO_32:
buzbee9329e6d2013-08-19 12:55:10 -0700525 if (mir_graph_->IsBackedge(bb, bb->taken)) {
buzbee0d829482013-10-11 15:24:55 -0700526 GenSuspendTestAndBranch(opt_flags, &label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 } else {
buzbee0d829482013-10-11 15:24:55 -0700528 OpUnconditionalBranch(&label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 }
530 break;
531
532 case Instruction::PACKED_SWITCH:
533 GenPackedSwitch(mir, vB, rl_src[0]);
534 break;
535
536 case Instruction::SPARSE_SWITCH:
537 GenSparseSwitch(mir, vB, rl_src[0]);
538 break;
539
540 case Instruction::CMPL_FLOAT:
541 case Instruction::CMPG_FLOAT:
542 case Instruction::CMPL_DOUBLE:
543 case Instruction::CMPG_DOUBLE:
544 GenCmpFP(opcode, rl_dest, rl_src[0], rl_src[1]);
545 break;
546
547 case Instruction::CMP_LONG:
548 GenCmpLong(rl_dest, rl_src[0], rl_src[1]);
549 break;
550
551 case Instruction::IF_EQ:
552 case Instruction::IF_NE:
553 case Instruction::IF_LT:
554 case Instruction::IF_GE:
555 case Instruction::IF_GT:
556 case Instruction::IF_LE: {
buzbee0d829482013-10-11 15:24:55 -0700557 LIR* taken = &label_list[bb->taken];
558 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 // Result known at compile time?
560 if (rl_src[0].is_const && rl_src[1].is_const) {
561 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg),
562 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
buzbee0d829482013-10-11 15:24:55 -0700563 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
564 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 GenSuspendTest(opt_flags);
566 }
buzbee0d829482013-10-11 15:24:55 -0700567 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700569 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700570 GenSuspendTest(opt_flags);
571 }
buzbee0d829482013-10-11 15:24:55 -0700572 GenCompareAndBranch(opcode, rl_src[0], rl_src[1], taken, fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700573 }
574 break;
575 }
576
577 case Instruction::IF_EQZ:
578 case Instruction::IF_NEZ:
579 case Instruction::IF_LTZ:
580 case Instruction::IF_GEZ:
581 case Instruction::IF_GTZ:
582 case Instruction::IF_LEZ: {
buzbee0d829482013-10-11 15:24:55 -0700583 LIR* taken = &label_list[bb->taken];
584 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585 // Result known at compile time?
586 if (rl_src[0].is_const) {
587 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg), 0);
buzbee0d829482013-10-11 15:24:55 -0700588 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
589 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700590 GenSuspendTest(opt_flags);
591 }
buzbee0d829482013-10-11 15:24:55 -0700592 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700594 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 GenSuspendTest(opt_flags);
596 }
597 GenCompareZeroAndBranch(opcode, rl_src[0], taken, fall_through);
598 }
599 break;
600 }
601
602 case Instruction::AGET_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700603 GenArrayGet(opt_flags, k64, rl_src[0], rl_src[1], rl_dest, 3);
604 break;
605 case Instruction::AGET_OBJECT:
606 GenArrayGet(opt_flags, kReference, rl_src[0], rl_src[1], rl_dest, 2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700607 break;
608 case Instruction::AGET:
buzbee695d13a2014-04-19 13:32:20 -0700609 GenArrayGet(opt_flags, k32, rl_src[0], rl_src[1], rl_dest, 2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 break;
611 case Instruction::AGET_BOOLEAN:
612 GenArrayGet(opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
613 break;
614 case Instruction::AGET_BYTE:
615 GenArrayGet(opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
616 break;
617 case Instruction::AGET_CHAR:
618 GenArrayGet(opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
619 break;
620 case Instruction::AGET_SHORT:
621 GenArrayGet(opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
622 break;
623 case Instruction::APUT_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700624 GenArrayPut(opt_flags, k64, rl_src[1], rl_src[2], rl_src[0], 3, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 break;
626 case Instruction::APUT:
buzbee695d13a2014-04-19 13:32:20 -0700627 GenArrayPut(opt_flags, k32, rl_src[1], rl_src[2], rl_src[0], 2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700629 case Instruction::APUT_OBJECT: {
630 bool is_null = mir_graph_->IsConstantNullRef(rl_src[0]);
631 bool is_safe = is_null; // Always safe to store null.
632 if (!is_safe) {
633 // Check safety from verifier type information.
Vladimir Marko2730db02014-01-27 11:15:17 +0000634 const DexCompilationUnit* unit = mir_graph_->GetCurrentDexCompilationUnit();
635 is_safe = cu_->compiler_driver->IsSafeCast(unit, mir->offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700636 }
637 if (is_null || is_safe) {
638 // Store of constant null doesn't require an assignability test and can be generated inline
639 // without fixed register usage or a card mark.
buzbee695d13a2014-04-19 13:32:20 -0700640 GenArrayPut(opt_flags, kReference, rl_src[1], rl_src[2], rl_src[0], 2, !is_null);
Ian Rogersa9a82542013-10-04 11:17:26 -0700641 } else {
642 GenArrayObjPut(opt_flags, rl_src[1], rl_src[2], rl_src[0]);
643 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700644 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700645 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 case Instruction::APUT_SHORT:
647 case Instruction::APUT_CHAR:
Ian Rogersa9a82542013-10-04 11:17:26 -0700648 GenArrayPut(opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 break;
650 case Instruction::APUT_BYTE:
651 case Instruction::APUT_BOOLEAN:
Ian Rogersa9a82542013-10-04 11:17:26 -0700652 GenArrayPut(opt_flags, kUnsignedByte, rl_src[1], rl_src[2], rl_src[0], 0, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 break;
654
655 case Instruction::IGET_OBJECT:
buzbee695d13a2014-04-19 13:32:20 -0700656 GenIGet(mir, opt_flags, kReference, rl_dest, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 break;
658
659 case Instruction::IGET_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700660 GenIGet(mir, opt_flags, k64, rl_dest, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700661 break;
662
663 case Instruction::IGET:
buzbee695d13a2014-04-19 13:32:20 -0700664 GenIGet(mir, opt_flags, k32, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700665 break;
666
667 case Instruction::IGET_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000668 GenIGet(mir, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 break;
670
671 case Instruction::IGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000672 GenIGet(mir, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700673 break;
674
675 case Instruction::IGET_BOOLEAN:
676 case Instruction::IGET_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000677 GenIGet(mir, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 break;
679
680 case Instruction::IPUT_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700681 GenIPut(mir, opt_flags, k64, rl_src[0], rl_src[1], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 break;
683
684 case Instruction::IPUT_OBJECT:
buzbee695d13a2014-04-19 13:32:20 -0700685 GenIPut(mir, opt_flags, kReference, rl_src[0], rl_src[1], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 break;
687
688 case Instruction::IPUT:
buzbee695d13a2014-04-19 13:32:20 -0700689 GenIPut(mir, opt_flags, k32, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 break;
691
692 case Instruction::IPUT_BOOLEAN:
693 case Instruction::IPUT_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000694 GenIPut(mir, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 break;
696
697 case Instruction::IPUT_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000698 GenIPut(mir, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 break;
700
701 case Instruction::IPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000702 GenIPut(mir, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 break;
704
705 case Instruction::SGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000706 GenSget(mir, rl_dest, false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 break;
708 case Instruction::SGET:
709 case Instruction::SGET_BOOLEAN:
710 case Instruction::SGET_BYTE:
711 case Instruction::SGET_CHAR:
712 case Instruction::SGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000713 GenSget(mir, rl_dest, false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700714 break;
715
716 case Instruction::SGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000717 GenSget(mir, rl_dest, true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 break;
719
720 case Instruction::SPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000721 GenSput(mir, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700722 break;
723
724 case Instruction::SPUT:
725 case Instruction::SPUT_BOOLEAN:
726 case Instruction::SPUT_BYTE:
727 case Instruction::SPUT_CHAR:
728 case Instruction::SPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000729 GenSput(mir, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700730 break;
731
732 case Instruction::SPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000733 GenSput(mir, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700734 break;
735
736 case Instruction::INVOKE_STATIC_RANGE:
737 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, true));
738 break;
739 case Instruction::INVOKE_STATIC:
740 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, false));
741 break;
742
743 case Instruction::INVOKE_DIRECT:
744 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, false));
745 break;
746 case Instruction::INVOKE_DIRECT_RANGE:
747 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, true));
748 break;
749
750 case Instruction::INVOKE_VIRTUAL:
751 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, false));
752 break;
753 case Instruction::INVOKE_VIRTUAL_RANGE:
754 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, true));
755 break;
756
757 case Instruction::INVOKE_SUPER:
758 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, false));
759 break;
760 case Instruction::INVOKE_SUPER_RANGE:
761 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, true));
762 break;
763
764 case Instruction::INVOKE_INTERFACE:
765 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, false));
766 break;
767 case Instruction::INVOKE_INTERFACE_RANGE:
768 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, true));
769 break;
770
771 case Instruction::NEG_INT:
772 case Instruction::NOT_INT:
773 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[0]);
774 break;
775
776 case Instruction::NEG_LONG:
777 case Instruction::NOT_LONG:
778 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[0]);
779 break;
780
781 case Instruction::NEG_FLOAT:
782 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[0]);
783 break;
784
785 case Instruction::NEG_DOUBLE:
786 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[0]);
787 break;
788
789 case Instruction::INT_TO_LONG:
790 GenIntToLong(rl_dest, rl_src[0]);
791 break;
792
793 case Instruction::LONG_TO_INT:
794 rl_src[0] = UpdateLocWide(rl_src[0]);
795 rl_src[0] = WideToNarrow(rl_src[0]);
796 StoreValue(rl_dest, rl_src[0]);
797 break;
798
799 case Instruction::INT_TO_BYTE:
800 case Instruction::INT_TO_SHORT:
801 case Instruction::INT_TO_CHAR:
802 GenIntNarrowing(opcode, rl_dest, rl_src[0]);
803 break;
804
805 case Instruction::INT_TO_FLOAT:
806 case Instruction::INT_TO_DOUBLE:
807 case Instruction::LONG_TO_FLOAT:
808 case Instruction::LONG_TO_DOUBLE:
809 case Instruction::FLOAT_TO_INT:
810 case Instruction::FLOAT_TO_LONG:
811 case Instruction::FLOAT_TO_DOUBLE:
812 case Instruction::DOUBLE_TO_INT:
813 case Instruction::DOUBLE_TO_LONG:
814 case Instruction::DOUBLE_TO_FLOAT:
815 GenConversion(opcode, rl_dest, rl_src[0]);
816 break;
817
818
819 case Instruction::ADD_INT:
820 case Instruction::ADD_INT_2ADDR:
821 case Instruction::MUL_INT:
822 case Instruction::MUL_INT_2ADDR:
823 case Instruction::AND_INT:
824 case Instruction::AND_INT_2ADDR:
825 case Instruction::OR_INT:
826 case Instruction::OR_INT_2ADDR:
827 case Instruction::XOR_INT:
828 case Instruction::XOR_INT_2ADDR:
829 if (rl_src[0].is_const &&
830 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[0]))) {
831 GenArithOpIntLit(opcode, rl_dest, rl_src[1],
832 mir_graph_->ConstantValue(rl_src[0].orig_sreg));
833 } else if (rl_src[1].is_const &&
834 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
835 GenArithOpIntLit(opcode, rl_dest, rl_src[0],
836 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
837 } else {
838 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
839 }
840 break;
841
842 case Instruction::SUB_INT:
843 case Instruction::SUB_INT_2ADDR:
844 case Instruction::DIV_INT:
845 case Instruction::DIV_INT_2ADDR:
846 case Instruction::REM_INT:
847 case Instruction::REM_INT_2ADDR:
848 case Instruction::SHL_INT:
849 case Instruction::SHL_INT_2ADDR:
850 case Instruction::SHR_INT:
851 case Instruction::SHR_INT_2ADDR:
852 case Instruction::USHR_INT:
853 case Instruction::USHR_INT_2ADDR:
854 if (rl_src[1].is_const &&
855 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
856 GenArithOpIntLit(opcode, rl_dest, rl_src[0], mir_graph_->ConstantValue(rl_src[1]));
857 } else {
858 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
859 }
860 break;
861
862 case Instruction::ADD_LONG:
863 case Instruction::SUB_LONG:
864 case Instruction::AND_LONG:
865 case Instruction::OR_LONG:
866 case Instruction::XOR_LONG:
867 case Instruction::ADD_LONG_2ADDR:
868 case Instruction::SUB_LONG_2ADDR:
869 case Instruction::AND_LONG_2ADDR:
870 case Instruction::OR_LONG_2ADDR:
871 case Instruction::XOR_LONG_2ADDR:
872 if (rl_src[0].is_const || rl_src[1].is_const) {
873 GenArithImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
874 break;
875 }
876 // Note: intentional fallthrough.
877
878 case Instruction::MUL_LONG:
879 case Instruction::DIV_LONG:
880 case Instruction::REM_LONG:
881 case Instruction::MUL_LONG_2ADDR:
882 case Instruction::DIV_LONG_2ADDR:
883 case Instruction::REM_LONG_2ADDR:
884 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
885 break;
886
887 case Instruction::SHL_LONG:
888 case Instruction::SHR_LONG:
889 case Instruction::USHR_LONG:
890 case Instruction::SHL_LONG_2ADDR:
891 case Instruction::SHR_LONG_2ADDR:
892 case Instruction::USHR_LONG_2ADDR:
893 if (rl_src[1].is_const) {
894 GenShiftImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
895 } else {
896 GenShiftOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
897 }
898 break;
899
900 case Instruction::ADD_FLOAT:
901 case Instruction::SUB_FLOAT:
902 case Instruction::MUL_FLOAT:
903 case Instruction::DIV_FLOAT:
904 case Instruction::REM_FLOAT:
905 case Instruction::ADD_FLOAT_2ADDR:
906 case Instruction::SUB_FLOAT_2ADDR:
907 case Instruction::MUL_FLOAT_2ADDR:
908 case Instruction::DIV_FLOAT_2ADDR:
909 case Instruction::REM_FLOAT_2ADDR:
910 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[1]);
911 break;
912
913 case Instruction::ADD_DOUBLE:
914 case Instruction::SUB_DOUBLE:
915 case Instruction::MUL_DOUBLE:
916 case Instruction::DIV_DOUBLE:
917 case Instruction::REM_DOUBLE:
918 case Instruction::ADD_DOUBLE_2ADDR:
919 case Instruction::SUB_DOUBLE_2ADDR:
920 case Instruction::MUL_DOUBLE_2ADDR:
921 case Instruction::DIV_DOUBLE_2ADDR:
922 case Instruction::REM_DOUBLE_2ADDR:
923 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[1]);
924 break;
925
926 case Instruction::RSUB_INT:
927 case Instruction::ADD_INT_LIT16:
928 case Instruction::MUL_INT_LIT16:
929 case Instruction::DIV_INT_LIT16:
930 case Instruction::REM_INT_LIT16:
931 case Instruction::AND_INT_LIT16:
932 case Instruction::OR_INT_LIT16:
933 case Instruction::XOR_INT_LIT16:
934 case Instruction::ADD_INT_LIT8:
935 case Instruction::RSUB_INT_LIT8:
936 case Instruction::MUL_INT_LIT8:
937 case Instruction::DIV_INT_LIT8:
938 case Instruction::REM_INT_LIT8:
939 case Instruction::AND_INT_LIT8:
940 case Instruction::OR_INT_LIT8:
941 case Instruction::XOR_INT_LIT8:
942 case Instruction::SHL_INT_LIT8:
943 case Instruction::SHR_INT_LIT8:
944 case Instruction::USHR_INT_LIT8:
945 GenArithOpIntLit(opcode, rl_dest, rl_src[0], vC);
946 break;
947
948 default:
949 LOG(FATAL) << "Unexpected opcode: " << opcode;
950 }
buzbee082833c2014-05-17 23:16:26 -0700951 DCHECK(CheckCorePoolSanity());
Brian Carlstrom1895ea32013-07-18 13:28:37 -0700952} // NOLINT(readability/fn_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700953
954// Process extended MIR instructions
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700955void Mir2Lir::HandleExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
957 case kMirOpCopy: {
958 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
959 RegLocation rl_dest = mir_graph_->GetDest(mir);
960 StoreValue(rl_dest, rl_src);
961 break;
962 }
963 case kMirOpFusedCmplFloat:
964 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, false /*double*/);
965 break;
966 case kMirOpFusedCmpgFloat:
967 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, false /*double*/);
968 break;
969 case kMirOpFusedCmplDouble:
970 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, true /*double*/);
971 break;
972 case kMirOpFusedCmpgDouble:
973 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, true /*double*/);
974 break;
975 case kMirOpFusedCmpLong:
976 GenFusedLongCmpBranch(bb, mir);
977 break;
978 case kMirOpSelect:
979 GenSelect(bb, mir);
980 break;
Mark Mendelld65c51a2014-04-29 16:55:20 -0400981 case kMirOpPhi:
982 case kMirOpNop:
983 case kMirOpNullCheck:
984 case kMirOpRangeCheck:
985 case kMirOpDivZeroCheck:
986 case kMirOpCheck:
987 case kMirOpCheckPart2:
988 // Ignore these known opcodes
989 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 default:
Mark Mendelld65c51a2014-04-29 16:55:20 -0400991 // Give the backends a chance to handle unknown extended MIR opcodes.
992 GenMachineSpecificExtendedMethodMIR(bb, mir);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 break;
994 }
995}
996
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800997void Mir2Lir::GenPrintLabel(MIR* mir) {
998 // Mark the beginning of a Dalvik instruction for line tracking.
999 if (cu_->verbose) {
1000 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
1001 MarkBoundary(mir->offset, inst_str);
1002 }
1003}
1004
Brian Carlstrom7940e442013-07-12 13:46:57 -07001005// Handle the content in each basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001006bool Mir2Lir::MethodBlockCodeGen(BasicBlock* bb) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001007 if (bb->block_type == kDead) return false;
1008 current_dalvik_offset_ = bb->start_offset;
1009 MIR* mir;
1010 int block_id = bb->id;
1011
1012 block_label_list_[block_id].operands[0] = bb->start_offset;
1013
1014 // Insert the block label.
1015 block_label_list_[block_id].opcode = kPseudoNormalBlockLabel;
buzbeeb48819d2013-09-14 16:15:25 -07001016 block_label_list_[block_id].flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001017 AppendLIR(&block_label_list_[block_id]);
1018
1019 LIR* head_lir = NULL;
1020
1021 // If this is a catch block, export the start address.
1022 if (bb->catch_entry) {
1023 head_lir = NewLIR0(kPseudoExportedPC);
1024 }
1025
1026 // Free temp registers and reset redundant store tracking.
buzbeeba574512014-05-12 15:13:16 -07001027 ClobberAllTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028
1029 if (bb->block_type == kEntryBlock) {
buzbee56c71782013-09-05 17:13:19 -07001030 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001031 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
1032 GenEntrySequence(&mir_graph_->reg_location_[start_vreg],
1033 mir_graph_->reg_location_[mir_graph_->GetMethodSReg()]);
1034 } else if (bb->block_type == kExitBlock) {
buzbee56c71782013-09-05 17:13:19 -07001035 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001036 GenExitSequence();
1037 }
1038
1039 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
1040 ResetRegPool();
1041 if (cu_->disable_opt & (1 << kTrackLiveTemps)) {
buzbeeba574512014-05-12 15:13:16 -07001042 ClobberAllTemps();
buzbee7a11ab02014-04-28 20:02:38 -07001043 // Reset temp allocation to minimize differences when A/B testing.
buzbee091cc402014-03-31 10:14:40 -07001044 reg_pool_->ResetNextTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 }
1046
1047 if (cu_->disable_opt & (1 << kSuppressLoads)) {
1048 ResetDefTracking();
1049 }
1050
1051 // Reset temp tracking sanity check.
1052 if (kIsDebugBuild) {
1053 live_sreg_ = INVALID_SREG;
1054 }
1055
1056 current_dalvik_offset_ = mir->offset;
1057 int opcode = mir->dalvikInsn.opcode;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001059 GenPrintLabel(mir);
1060
Brian Carlstrom7940e442013-07-12 13:46:57 -07001061 // Remember the first LIR for this block.
1062 if (head_lir == NULL) {
buzbee252254b2013-09-08 16:20:53 -07001063 head_lir = &block_label_list_[bb->id];
1064 // Set the first label as a scheduling barrier.
buzbeeb48819d2013-09-14 16:15:25 -07001065 DCHECK(!head_lir->flags.use_def_invalid);
1066 head_lir->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 }
1068
1069 if (opcode == kMirOpCheck) {
1070 // Combine check and work halves of throwing instruction.
1071 MIR* work_half = mir->meta.throw_insn;
1072 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Vladimir Marko4376c872014-01-23 12:39:29 +00001073 mir->meta = work_half->meta; // Whatever the work_half had, we need to copy it.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001074 opcode = work_half->dalvikInsn.opcode;
1075 SSARepresentation* ssa_rep = work_half->ssa_rep;
1076 work_half->ssa_rep = mir->ssa_rep;
1077 mir->ssa_rep = ssa_rep;
1078 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Vladimir Marko4376c872014-01-23 12:39:29 +00001079 work_half->meta.throw_insn = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080 }
1081
1082 if (opcode >= kMirOpFirst) {
1083 HandleExtendedMethodMIR(bb, mir);
1084 continue;
1085 }
1086
1087 CompileDalvikInstruction(mir, bb, block_label_list_);
1088 }
1089
1090 if (head_lir) {
1091 // Eliminate redundant loads/stores and delay stores into later slots.
1092 ApplyLocalOptimizations(head_lir, last_lir_insn_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001093 }
1094 return false;
1095}
1096
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001097bool Mir2Lir::SpecialMIR2LIR(const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +00001098 cu_->NewTimingSplit("SpecialMIR2LIR");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 // Find the first DalvikByteCode block.
1100 int num_reachable_blocks = mir_graph_->GetNumReachableBlocks();
1101 BasicBlock*bb = NULL;
1102 for (int idx = 0; idx < num_reachable_blocks; idx++) {
1103 // TODO: no direct access of growable lists.
1104 int dfs_index = mir_graph_->GetDfsOrder()->Get(idx);
1105 bb = mir_graph_->GetBasicBlock(dfs_index);
1106 if (bb->block_type == kDalvikByteCode) {
1107 break;
1108 }
1109 }
1110 if (bb == NULL) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001111 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001112 }
1113 DCHECK_EQ(bb->start_offset, 0);
1114 DCHECK(bb->first_mir_insn != NULL);
1115
1116 // Get the first instruction.
1117 MIR* mir = bb->first_mir_insn;
1118
1119 // Free temp registers and reset redundant store tracking.
1120 ResetRegPool();
1121 ResetDefTracking();
buzbeeba574512014-05-12 15:13:16 -07001122 ClobberAllTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001123
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001124 return GenSpecialCase(bb, mir, special);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001125}
1126
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001127void Mir2Lir::MethodMIR2LIR() {
buzbeea61f4952013-08-23 14:27:06 -07001128 cu_->NewTimingSplit("MIR2LIR");
1129
Brian Carlstrom7940e442013-07-12 13:46:57 -07001130 // Hold the labels of each block.
1131 block_label_list_ =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001132 static_cast<LIR*>(arena_->Alloc(sizeof(LIR) * mir_graph_->GetNumBlocks(),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001133 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134
buzbee56c71782013-09-05 17:13:19 -07001135 PreOrderDfsIterator iter(mir_graph_);
buzbee252254b2013-09-08 16:20:53 -07001136 BasicBlock* curr_bb = iter.Next();
1137 BasicBlock* next_bb = iter.Next();
1138 while (curr_bb != NULL) {
1139 MethodBlockCodeGen(curr_bb);
1140 // If the fall_through block is no longer laid out consecutively, drop in a branch.
buzbee0d829482013-10-11 15:24:55 -07001141 BasicBlock* curr_bb_fall_through = mir_graph_->GetBasicBlock(curr_bb->fall_through);
1142 if ((curr_bb_fall_through != NULL) && (curr_bb_fall_through != next_bb)) {
1143 OpUnconditionalBranch(&block_label_list_[curr_bb->fall_through]);
buzbee252254b2013-09-08 16:20:53 -07001144 }
1145 curr_bb = next_bb;
1146 do {
1147 next_bb = iter.Next();
1148 } while ((next_bb != NULL) && (next_bb->block_type == kDead));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001150 HandleSlowPaths();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151}
1152
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001153//
1154// LIR Slow Path
1155//
1156
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001157LIR* Mir2Lir::LIRSlowPath::GenerateTargetLabel(int opcode) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001158 m2l_->SetCurrentDexPc(current_dex_pc_);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001159 LIR* target = m2l_->NewLIR0(opcode);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001160 fromfast_->target = target;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001161 return target;
1162}
Vladimir Marko3bc86152014-03-13 14:11:28 +00001163
Brian Carlstrom7940e442013-07-12 13:46:57 -07001164} // namespace art