blob: 73fdc82854cc9f77168e7653ae8e8a023433ed3e [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "dex/compiler_internals.h"
18#include "dex/dataflow_iterator-inl.h"
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080019#include "dex/quick/dex_file_method_inliner.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070020#include "mir_to_lir-inl.h"
21#include "object_utils.h"
Ian Rogers02ed4c02013-09-06 13:10:04 -070022#include "thread-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
24namespace art {
25
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080026void Mir2Lir::LockArg(int in_position, bool wide) {
buzbee2700f7e2014-03-07 09:46:20 -080027 RegStorage reg_arg_low = GetArgMappingToPhysicalReg(in_position);
28 RegStorage reg_arg_high = wide ? GetArgMappingToPhysicalReg(in_position + 1) :
29 RegStorage::InvalidReg();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080030
buzbee2700f7e2014-03-07 09:46:20 -080031 if (reg_arg_low.Valid()) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080032 LockTemp(reg_arg_low);
33 }
buzbee2700f7e2014-03-07 09:46:20 -080034 if (reg_arg_high.Valid() && reg_arg_low != reg_arg_high) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080035 LockTemp(reg_arg_high);
36 }
37}
38
buzbee2700f7e2014-03-07 09:46:20 -080039// TODO: needs revisit for 64-bit.
40RegStorage Mir2Lir::LoadArg(int in_position, bool wide) {
41 RegStorage reg_arg_low = GetArgMappingToPhysicalReg(in_position);
42 RegStorage reg_arg_high = wide ? GetArgMappingToPhysicalReg(in_position + 1) :
43 RegStorage::InvalidReg();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080044
45 int offset = StackVisitor::GetOutVROffset(in_position);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +070046 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080047 /*
48 * When doing a call for x86, it moves the stack pointer in order to push return.
49 * Thus, we add another 4 bytes to figure out the out of caller (in of callee).
50 * TODO: This needs revisited for 64-bit.
51 */
52 offset += sizeof(uint32_t);
53 }
54
55 // If the VR is wide and there is no register for high part, we need to load it.
buzbee2700f7e2014-03-07 09:46:20 -080056 if (wide && !reg_arg_high.Valid()) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080057 // If the low part is not in a reg, we allocate a pair. Otherwise, we just load to high reg.
buzbee2700f7e2014-03-07 09:46:20 -080058 if (!reg_arg_low.Valid()) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +000059 RegStorage new_regs = AllocTypedTempWide(false, kAnyReg);
buzbee2700f7e2014-03-07 09:46:20 -080060 reg_arg_low = new_regs.GetLow();
61 reg_arg_high = new_regs.GetHigh();
62 LoadBaseDispWide(TargetReg(kSp), offset, new_regs, INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080063 } else {
64 reg_arg_high = AllocTemp();
65 int offset_high = offset + sizeof(uint32_t);
66 LoadWordDisp(TargetReg(kSp), offset_high, reg_arg_high);
67 }
68 }
69
70 // If the low part is not in a register yet, we need to load it.
buzbee2700f7e2014-03-07 09:46:20 -080071 if (!reg_arg_low.Valid()) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080072 reg_arg_low = AllocTemp();
73 LoadWordDisp(TargetReg(kSp), offset, reg_arg_low);
74 }
75
76 if (wide) {
buzbee2700f7e2014-03-07 09:46:20 -080077 return RegStorage::MakeRegPair(reg_arg_low, reg_arg_high);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080078 } else {
79 return reg_arg_low;
80 }
81}
82
83void Mir2Lir::LoadArgDirect(int in_position, RegLocation rl_dest) {
84 int offset = StackVisitor::GetOutVROffset(in_position);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +070085 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080086 /*
87 * When doing a call for x86, it moves the stack pointer in order to push return.
88 * Thus, we add another 4 bytes to figure out the out of caller (in of callee).
89 * TODO: This needs revisited for 64-bit.
90 */
91 offset += sizeof(uint32_t);
92 }
93
94 if (!rl_dest.wide) {
buzbee2700f7e2014-03-07 09:46:20 -080095 RegStorage reg = GetArgMappingToPhysicalReg(in_position);
96 if (reg.Valid()) {
97 OpRegCopy(rl_dest.reg, reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080098 } else {
buzbee2700f7e2014-03-07 09:46:20 -080099 LoadWordDisp(TargetReg(kSp), offset, rl_dest.reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800100 }
101 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800102 RegStorage reg_arg_low = GetArgMappingToPhysicalReg(in_position);
103 RegStorage reg_arg_high = GetArgMappingToPhysicalReg(in_position + 1);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800104
buzbee2700f7e2014-03-07 09:46:20 -0800105 if (reg_arg_low.Valid() && reg_arg_high.Valid()) {
106 OpRegCopyWide(rl_dest.reg, RegStorage::MakeRegPair(reg_arg_low, reg_arg_high));
107 } else if (reg_arg_low.Valid() && !reg_arg_high.Valid()) {
108 OpRegCopy(rl_dest.reg, reg_arg_low);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800109 int offset_high = offset + sizeof(uint32_t);
buzbee2700f7e2014-03-07 09:46:20 -0800110 LoadWordDisp(TargetReg(kSp), offset_high, rl_dest.reg.GetHigh());
111 } else if (!reg_arg_low.Valid() && reg_arg_high.Valid()) {
112 OpRegCopy(rl_dest.reg.GetHigh(), reg_arg_high);
113 LoadWordDisp(TargetReg(kSp), offset, rl_dest.reg.GetLow());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800114 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800115 LoadBaseDispWide(TargetReg(kSp), offset, rl_dest.reg, INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800116 }
117 }
118}
119
120bool Mir2Lir::GenSpecialIGet(MIR* mir, const InlineMethod& special) {
121 // FastInstance() already checked by DexFileMethodInliner.
122 const InlineIGetIPutData& data = special.d.ifield_data;
Mathieu Chartier73ed7182014-04-04 18:02:08 -0700123 if (data.method_is_static || data.object_arg != 0) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800124 // The object is not "this" and has to be null-checked.
125 return false;
126 }
127
Vladimir Markoe3e02602014-03-12 15:42:41 +0000128 bool wide = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE));
129 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800130 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
131
132 // Point of no return - no aborts after this
133 GenPrintLabel(mir);
134 LockArg(data.object_arg);
135 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
buzbee2700f7e2014-03-07 09:46:20 -0800136 RegStorage reg_obj = LoadArg(data.object_arg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800137 if (wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800138 LoadBaseDispWide(reg_obj, data.field_offset, rl_dest.reg, INVALID_SREG);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800139 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800140 LoadWordDisp(reg_obj, data.field_offset, rl_dest.reg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800141 }
142 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800143 // Without context sensitive analysis, we must issue the most conservative barriers.
144 // In this case, either a load or store may follow so we issue both barriers.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800145 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800146 GenMemBarrier(kLoadStore);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800147 }
148 return true;
149}
150
151bool Mir2Lir::GenSpecialIPut(MIR* mir, const InlineMethod& special) {
152 // FastInstance() already checked by DexFileMethodInliner.
153 const InlineIGetIPutData& data = special.d.ifield_data;
Mathieu Chartier73ed7182014-04-04 18:02:08 -0700154 if (data.method_is_static || data.object_arg != 0) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800155 // The object is not "this" and has to be null-checked.
156 return false;
157 }
158
Vladimir Markoe3e02602014-03-12 15:42:41 +0000159 bool wide = (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800160
161 // Point of no return - no aborts after this
162 GenPrintLabel(mir);
163 LockArg(data.object_arg);
164 LockArg(data.src_arg, wide);
buzbee2700f7e2014-03-07 09:46:20 -0800165 RegStorage reg_obj = LoadArg(data.object_arg);
166 RegStorage reg_src = LoadArg(data.src_arg, wide);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800167 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800168 // There might have been a store before this volatile one so insert StoreStore barrier.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800169 GenMemBarrier(kStoreStore);
170 }
171 if (wide) {
buzbee2700f7e2014-03-07 09:46:20 -0800172 StoreBaseDispWide(reg_obj, data.field_offset, reg_src);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800173 } else {
174 StoreBaseDisp(reg_obj, data.field_offset, reg_src, kWord);
175 }
176 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800177 // A load might follow the volatile store so insert a StoreLoad barrier.
178 GenMemBarrier(kStoreLoad);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800179 }
Vladimir Markoe3e02602014-03-12 15:42:41 +0000180 if (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_OBJECT)) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800181 MarkGCCard(reg_src, reg_obj);
182 }
183 return true;
184}
185
186bool Mir2Lir::GenSpecialIdentity(MIR* mir, const InlineMethod& special) {
187 const InlineReturnArgData& data = special.d.return_data;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000188 bool wide = (data.is_wide != 0u);
189 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800190 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
191
192 // Point of no return - no aborts after this
193 GenPrintLabel(mir);
194 LockArg(data.arg, wide);
195 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
196 LoadArgDirect(data.arg, rl_dest);
197 return true;
198}
199
200/*
201 * Special-case code generation for simple non-throwing leaf methods.
202 */
203bool Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
204 DCHECK(special.flags & kInlineSpecial);
205 current_dalvik_offset_ = mir->offset;
206 MIR* return_mir = nullptr;
207 bool successful = false;
208
209 switch (special.opcode) {
210 case kInlineOpNop:
211 successful = true;
212 DCHECK_EQ(mir->dalvikInsn.opcode, Instruction::RETURN_VOID);
213 return_mir = mir;
214 break;
215 case kInlineOpNonWideConst: {
216 successful = true;
217 RegLocation rl_dest = GetReturn(cu_->shorty[0] == 'F');
218 GenPrintLabel(mir);
buzbee2700f7e2014-03-07 09:46:20 -0800219 LoadConstant(rl_dest.reg, static_cast<int>(special.d.data));
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700220 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800221 break;
222 }
223 case kInlineOpReturnArg:
224 successful = GenSpecialIdentity(mir, special);
225 return_mir = mir;
226 break;
227 case kInlineOpIGet:
228 successful = GenSpecialIGet(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700229 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800230 break;
231 case kInlineOpIPut:
232 successful = GenSpecialIPut(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700233 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800234 break;
235 default:
236 break;
237 }
238
239 if (successful) {
Vladimir Marko39d95e62014-02-28 12:51:24 +0000240 if (kIsDebugBuild) {
241 // Clear unreachable catch entries.
242 mir_graph_->catches_.clear();
243 }
244
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800245 // Handle verbosity for return MIR.
246 if (return_mir != nullptr) {
247 current_dalvik_offset_ = return_mir->offset;
248 // Not handling special identity case because it already generated code as part
249 // of the return. The label should have been added before any code was generated.
250 if (special.opcode != kInlineOpReturnArg) {
251 GenPrintLabel(return_mir);
252 }
253 }
254 GenSpecialExitSequence();
255
256 core_spill_mask_ = 0;
257 num_core_spills_ = 0;
258 fp_spill_mask_ = 0;
259 num_fp_spills_ = 0;
260 frame_size_ = 0;
261 core_vmap_table_.clear();
262 fp_vmap_table_.clear();
263 }
264
265 return successful;
266}
267
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268/*
269 * Target-independent code generation. Use only high-level
270 * load/store utilities here, or target-dependent genXX() handlers
271 * when necessary.
272 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273void Mir2Lir::CompileDalvikInstruction(MIR* mir, BasicBlock* bb, LIR* label_list) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 RegLocation rl_src[3];
275 RegLocation rl_dest = mir_graph_->GetBadLoc();
276 RegLocation rl_result = mir_graph_->GetBadLoc();
277 Instruction::Code opcode = mir->dalvikInsn.opcode;
278 int opt_flags = mir->optimization_flags;
279 uint32_t vB = mir->dalvikInsn.vB;
280 uint32_t vC = mir->dalvikInsn.vC;
281
282 // Prep Src and Dest locations.
283 int next_sreg = 0;
284 int next_loc = 0;
buzbee1da1e2f2013-11-15 13:37:01 -0800285 uint64_t attrs = mir_graph_->oat_data_flow_attributes_[opcode];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 rl_src[0] = rl_src[1] = rl_src[2] = mir_graph_->GetBadLoc();
287 if (attrs & DF_UA) {
288 if (attrs & DF_A_WIDE) {
289 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
290 next_sreg+= 2;
291 } else {
292 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
293 next_sreg++;
294 }
295 }
296 if (attrs & DF_UB) {
297 if (attrs & DF_B_WIDE) {
298 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
299 next_sreg+= 2;
300 } else {
301 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
302 next_sreg++;
303 }
304 }
305 if (attrs & DF_UC) {
306 if (attrs & DF_C_WIDE) {
307 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
308 } else {
309 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
310 }
311 }
312 if (attrs & DF_DA) {
313 if (attrs & DF_A_WIDE) {
314 rl_dest = mir_graph_->GetDestWide(mir);
315 } else {
316 rl_dest = mir_graph_->GetDest(mir);
317 }
318 }
319 switch (opcode) {
320 case Instruction::NOP:
321 break;
322
323 case Instruction::MOVE_EXCEPTION:
324 GenMoveException(rl_dest);
325 break;
326
327 case Instruction::RETURN_VOID:
328 if (((cu_->access_flags & kAccConstructor) != 0) &&
329 cu_->compiler_driver->RequiresConstructorBarrier(Thread::Current(), cu_->dex_file,
330 cu_->class_def_idx)) {
331 GenMemBarrier(kStoreStore);
332 }
333 if (!mir_graph_->MethodIsLeaf()) {
334 GenSuspendTest(opt_flags);
335 }
336 break;
337
338 case Instruction::RETURN:
339 case Instruction::RETURN_OBJECT:
340 if (!mir_graph_->MethodIsLeaf()) {
341 GenSuspendTest(opt_flags);
342 }
343 StoreValue(GetReturn(cu_->shorty[0] == 'F'), rl_src[0]);
344 break;
345
346 case Instruction::RETURN_WIDE:
347 if (!mir_graph_->MethodIsLeaf()) {
348 GenSuspendTest(opt_flags);
349 }
350 StoreValueWide(GetReturnWide(cu_->shorty[0] == 'D'), rl_src[0]);
351 break;
352
353 case Instruction::MOVE_RESULT_WIDE:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000354 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000356 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 StoreValueWide(rl_dest, GetReturnWide(rl_dest.fp));
358 break;
359
360 case Instruction::MOVE_RESULT:
361 case Instruction::MOVE_RESULT_OBJECT:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000362 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000364 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 StoreValue(rl_dest, GetReturn(rl_dest.fp));
366 break;
367
368 case Instruction::MOVE:
369 case Instruction::MOVE_OBJECT:
370 case Instruction::MOVE_16:
371 case Instruction::MOVE_OBJECT_16:
372 case Instruction::MOVE_FROM16:
373 case Instruction::MOVE_OBJECT_FROM16:
374 StoreValue(rl_dest, rl_src[0]);
375 break;
376
377 case Instruction::MOVE_WIDE:
378 case Instruction::MOVE_WIDE_16:
379 case Instruction::MOVE_WIDE_FROM16:
380 StoreValueWide(rl_dest, rl_src[0]);
381 break;
382
383 case Instruction::CONST:
384 case Instruction::CONST_4:
385 case Instruction::CONST_16:
386 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800387 LoadConstantNoClobber(rl_result.reg, vB);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 StoreValue(rl_dest, rl_result);
389 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800390 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 }
392 break;
393
394 case Instruction::CONST_HIGH16:
395 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800396 LoadConstantNoClobber(rl_result.reg, vB << 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 StoreValue(rl_dest, rl_result);
398 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800399 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 }
401 break;
402
403 case Instruction::CONST_WIDE_16:
404 case Instruction::CONST_WIDE_32:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000405 GenConstWide(rl_dest, static_cast<int64_t>(static_cast<int32_t>(vB)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 break;
407
408 case Instruction::CONST_WIDE:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000409 GenConstWide(rl_dest, mir->dalvikInsn.vB_wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 break;
411
412 case Instruction::CONST_WIDE_HIGH16:
413 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800414 LoadConstantWide(rl_result.reg, static_cast<int64_t>(vB) << 48);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 StoreValueWide(rl_dest, rl_result);
416 break;
417
418 case Instruction::MONITOR_ENTER:
419 GenMonitorEnter(opt_flags, rl_src[0]);
420 break;
421
422 case Instruction::MONITOR_EXIT:
423 GenMonitorExit(opt_flags, rl_src[0]);
424 break;
425
426 case Instruction::CHECK_CAST: {
427 GenCheckCast(mir->offset, vB, rl_src[0]);
428 break;
429 }
430 case Instruction::INSTANCE_OF:
431 GenInstanceof(vC, rl_dest, rl_src[0]);
432 break;
433
434 case Instruction::NEW_INSTANCE:
435 GenNewInstance(vB, rl_dest);
436 break;
437
438 case Instruction::THROW:
439 GenThrow(rl_src[0]);
440 break;
441
442 case Instruction::ARRAY_LENGTH:
443 int len_offset;
444 len_offset = mirror::Array::LengthOffset().Int32Value();
445 rl_src[0] = LoadValue(rl_src[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800446 GenNullCheck(rl_src[0].reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800448 LoadWordDisp(rl_src[0].reg, len_offset, rl_result.reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700449 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 StoreValue(rl_dest, rl_result);
451 break;
452
453 case Instruction::CONST_STRING:
454 case Instruction::CONST_STRING_JUMBO:
455 GenConstString(vB, rl_dest);
456 break;
457
458 case Instruction::CONST_CLASS:
459 GenConstClass(vB, rl_dest);
460 break;
461
462 case Instruction::FILL_ARRAY_DATA:
463 GenFillArrayData(vB, rl_src[0]);
464 break;
465
466 case Instruction::FILLED_NEW_ARRAY:
467 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
468 false /* not range */));
469 break;
470
471 case Instruction::FILLED_NEW_ARRAY_RANGE:
472 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
473 true /* range */));
474 break;
475
476 case Instruction::NEW_ARRAY:
477 GenNewArray(vC, rl_dest, rl_src[0]);
478 break;
479
480 case Instruction::GOTO:
481 case Instruction::GOTO_16:
482 case Instruction::GOTO_32:
buzbee9329e6d2013-08-19 12:55:10 -0700483 if (mir_graph_->IsBackedge(bb, bb->taken)) {
buzbee0d829482013-10-11 15:24:55 -0700484 GenSuspendTestAndBranch(opt_flags, &label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 } else {
buzbee0d829482013-10-11 15:24:55 -0700486 OpUnconditionalBranch(&label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 }
488 break;
489
490 case Instruction::PACKED_SWITCH:
491 GenPackedSwitch(mir, vB, rl_src[0]);
492 break;
493
494 case Instruction::SPARSE_SWITCH:
495 GenSparseSwitch(mir, vB, rl_src[0]);
496 break;
497
498 case Instruction::CMPL_FLOAT:
499 case Instruction::CMPG_FLOAT:
500 case Instruction::CMPL_DOUBLE:
501 case Instruction::CMPG_DOUBLE:
502 GenCmpFP(opcode, rl_dest, rl_src[0], rl_src[1]);
503 break;
504
505 case Instruction::CMP_LONG:
506 GenCmpLong(rl_dest, rl_src[0], rl_src[1]);
507 break;
508
509 case Instruction::IF_EQ:
510 case Instruction::IF_NE:
511 case Instruction::IF_LT:
512 case Instruction::IF_GE:
513 case Instruction::IF_GT:
514 case Instruction::IF_LE: {
buzbee0d829482013-10-11 15:24:55 -0700515 LIR* taken = &label_list[bb->taken];
516 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 // Result known at compile time?
518 if (rl_src[0].is_const && rl_src[1].is_const) {
519 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg),
520 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
buzbee0d829482013-10-11 15:24:55 -0700521 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
522 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 GenSuspendTest(opt_flags);
524 }
buzbee0d829482013-10-11 15:24:55 -0700525 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700527 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700528 GenSuspendTest(opt_flags);
529 }
buzbee0d829482013-10-11 15:24:55 -0700530 GenCompareAndBranch(opcode, rl_src[0], rl_src[1], taken, fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 }
532 break;
533 }
534
535 case Instruction::IF_EQZ:
536 case Instruction::IF_NEZ:
537 case Instruction::IF_LTZ:
538 case Instruction::IF_GEZ:
539 case Instruction::IF_GTZ:
540 case Instruction::IF_LEZ: {
buzbee0d829482013-10-11 15:24:55 -0700541 LIR* taken = &label_list[bb->taken];
542 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 // Result known at compile time?
544 if (rl_src[0].is_const) {
545 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg), 0);
buzbee0d829482013-10-11 15:24:55 -0700546 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
547 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 GenSuspendTest(opt_flags);
549 }
buzbee0d829482013-10-11 15:24:55 -0700550 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700552 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700553 GenSuspendTest(opt_flags);
554 }
555 GenCompareZeroAndBranch(opcode, rl_src[0], taken, fall_through);
556 }
557 break;
558 }
559
560 case Instruction::AGET_WIDE:
561 GenArrayGet(opt_flags, kLong, rl_src[0], rl_src[1], rl_dest, 3);
562 break;
563 case Instruction::AGET:
564 case Instruction::AGET_OBJECT:
565 GenArrayGet(opt_flags, kWord, rl_src[0], rl_src[1], rl_dest, 2);
566 break;
567 case Instruction::AGET_BOOLEAN:
568 GenArrayGet(opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
569 break;
570 case Instruction::AGET_BYTE:
571 GenArrayGet(opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
572 break;
573 case Instruction::AGET_CHAR:
574 GenArrayGet(opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
575 break;
576 case Instruction::AGET_SHORT:
577 GenArrayGet(opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
578 break;
579 case Instruction::APUT_WIDE:
Ian Rogersa9a82542013-10-04 11:17:26 -0700580 GenArrayPut(opt_flags, kLong, rl_src[1], rl_src[2], rl_src[0], 3, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700581 break;
582 case Instruction::APUT:
Ian Rogersa9a82542013-10-04 11:17:26 -0700583 GenArrayPut(opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700584 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700585 case Instruction::APUT_OBJECT: {
586 bool is_null = mir_graph_->IsConstantNullRef(rl_src[0]);
587 bool is_safe = is_null; // Always safe to store null.
588 if (!is_safe) {
589 // Check safety from verifier type information.
Vladimir Marko2730db02014-01-27 11:15:17 +0000590 const DexCompilationUnit* unit = mir_graph_->GetCurrentDexCompilationUnit();
591 is_safe = cu_->compiler_driver->IsSafeCast(unit, mir->offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700592 }
593 if (is_null || is_safe) {
594 // Store of constant null doesn't require an assignability test and can be generated inline
595 // without fixed register usage or a card mark.
596 GenArrayPut(opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2, !is_null);
597 } else {
598 GenArrayObjPut(opt_flags, rl_src[1], rl_src[2], rl_src[0]);
599 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700601 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 case Instruction::APUT_SHORT:
603 case Instruction::APUT_CHAR:
Ian Rogersa9a82542013-10-04 11:17:26 -0700604 GenArrayPut(opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 break;
606 case Instruction::APUT_BYTE:
607 case Instruction::APUT_BOOLEAN:
Ian Rogersa9a82542013-10-04 11:17:26 -0700608 GenArrayPut(opt_flags, kUnsignedByte, rl_src[1], rl_src[2], rl_src[0], 0, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 break;
610
611 case Instruction::IGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000612 GenIGet(mir, opt_flags, kWord, rl_dest, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 break;
614
615 case Instruction::IGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000616 GenIGet(mir, opt_flags, kLong, rl_dest, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 break;
618
619 case Instruction::IGET:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000620 GenIGet(mir, opt_flags, kWord, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 break;
622
623 case Instruction::IGET_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000624 GenIGet(mir, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 break;
626
627 case Instruction::IGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000628 GenIGet(mir, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 break;
630
631 case Instruction::IGET_BOOLEAN:
632 case Instruction::IGET_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000633 GenIGet(mir, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700634 break;
635
636 case Instruction::IPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000637 GenIPut(mir, opt_flags, kLong, rl_src[0], rl_src[1], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 break;
639
640 case Instruction::IPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000641 GenIPut(mir, opt_flags, kWord, rl_src[0], rl_src[1], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 break;
643
644 case Instruction::IPUT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000645 GenIPut(mir, opt_flags, kWord, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 break;
647
648 case Instruction::IPUT_BOOLEAN:
649 case Instruction::IPUT_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000650 GenIPut(mir, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 break;
652
653 case Instruction::IPUT_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000654 GenIPut(mir, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 break;
656
657 case Instruction::IPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000658 GenIPut(mir, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700659 break;
660
661 case Instruction::SGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000662 GenSget(mir, rl_dest, false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700663 break;
664 case Instruction::SGET:
665 case Instruction::SGET_BOOLEAN:
666 case Instruction::SGET_BYTE:
667 case Instruction::SGET_CHAR:
668 case Instruction::SGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000669 GenSget(mir, rl_dest, false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 break;
671
672 case Instruction::SGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000673 GenSget(mir, rl_dest, true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 break;
675
676 case Instruction::SPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000677 GenSput(mir, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 break;
679
680 case Instruction::SPUT:
681 case Instruction::SPUT_BOOLEAN:
682 case Instruction::SPUT_BYTE:
683 case Instruction::SPUT_CHAR:
684 case Instruction::SPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000685 GenSput(mir, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700686 break;
687
688 case Instruction::SPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000689 GenSput(mir, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 break;
691
692 case Instruction::INVOKE_STATIC_RANGE:
693 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, true));
694 break;
695 case Instruction::INVOKE_STATIC:
696 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, false));
697 break;
698
699 case Instruction::INVOKE_DIRECT:
700 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, false));
701 break;
702 case Instruction::INVOKE_DIRECT_RANGE:
703 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, true));
704 break;
705
706 case Instruction::INVOKE_VIRTUAL:
707 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, false));
708 break;
709 case Instruction::INVOKE_VIRTUAL_RANGE:
710 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, true));
711 break;
712
713 case Instruction::INVOKE_SUPER:
714 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, false));
715 break;
716 case Instruction::INVOKE_SUPER_RANGE:
717 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, true));
718 break;
719
720 case Instruction::INVOKE_INTERFACE:
721 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, false));
722 break;
723 case Instruction::INVOKE_INTERFACE_RANGE:
724 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, true));
725 break;
726
727 case Instruction::NEG_INT:
728 case Instruction::NOT_INT:
729 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[0]);
730 break;
731
732 case Instruction::NEG_LONG:
733 case Instruction::NOT_LONG:
734 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[0]);
735 break;
736
737 case Instruction::NEG_FLOAT:
738 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[0]);
739 break;
740
741 case Instruction::NEG_DOUBLE:
742 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[0]);
743 break;
744
745 case Instruction::INT_TO_LONG:
746 GenIntToLong(rl_dest, rl_src[0]);
747 break;
748
749 case Instruction::LONG_TO_INT:
750 rl_src[0] = UpdateLocWide(rl_src[0]);
751 rl_src[0] = WideToNarrow(rl_src[0]);
752 StoreValue(rl_dest, rl_src[0]);
753 break;
754
755 case Instruction::INT_TO_BYTE:
756 case Instruction::INT_TO_SHORT:
757 case Instruction::INT_TO_CHAR:
758 GenIntNarrowing(opcode, rl_dest, rl_src[0]);
759 break;
760
761 case Instruction::INT_TO_FLOAT:
762 case Instruction::INT_TO_DOUBLE:
763 case Instruction::LONG_TO_FLOAT:
764 case Instruction::LONG_TO_DOUBLE:
765 case Instruction::FLOAT_TO_INT:
766 case Instruction::FLOAT_TO_LONG:
767 case Instruction::FLOAT_TO_DOUBLE:
768 case Instruction::DOUBLE_TO_INT:
769 case Instruction::DOUBLE_TO_LONG:
770 case Instruction::DOUBLE_TO_FLOAT:
771 GenConversion(opcode, rl_dest, rl_src[0]);
772 break;
773
774
775 case Instruction::ADD_INT:
776 case Instruction::ADD_INT_2ADDR:
777 case Instruction::MUL_INT:
778 case Instruction::MUL_INT_2ADDR:
779 case Instruction::AND_INT:
780 case Instruction::AND_INT_2ADDR:
781 case Instruction::OR_INT:
782 case Instruction::OR_INT_2ADDR:
783 case Instruction::XOR_INT:
784 case Instruction::XOR_INT_2ADDR:
785 if (rl_src[0].is_const &&
786 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[0]))) {
787 GenArithOpIntLit(opcode, rl_dest, rl_src[1],
788 mir_graph_->ConstantValue(rl_src[0].orig_sreg));
789 } else if (rl_src[1].is_const &&
790 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
791 GenArithOpIntLit(opcode, rl_dest, rl_src[0],
792 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
793 } else {
794 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
795 }
796 break;
797
798 case Instruction::SUB_INT:
799 case Instruction::SUB_INT_2ADDR:
800 case Instruction::DIV_INT:
801 case Instruction::DIV_INT_2ADDR:
802 case Instruction::REM_INT:
803 case Instruction::REM_INT_2ADDR:
804 case Instruction::SHL_INT:
805 case Instruction::SHL_INT_2ADDR:
806 case Instruction::SHR_INT:
807 case Instruction::SHR_INT_2ADDR:
808 case Instruction::USHR_INT:
809 case Instruction::USHR_INT_2ADDR:
810 if (rl_src[1].is_const &&
811 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
812 GenArithOpIntLit(opcode, rl_dest, rl_src[0], mir_graph_->ConstantValue(rl_src[1]));
813 } else {
814 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
815 }
816 break;
817
818 case Instruction::ADD_LONG:
819 case Instruction::SUB_LONG:
820 case Instruction::AND_LONG:
821 case Instruction::OR_LONG:
822 case Instruction::XOR_LONG:
823 case Instruction::ADD_LONG_2ADDR:
824 case Instruction::SUB_LONG_2ADDR:
825 case Instruction::AND_LONG_2ADDR:
826 case Instruction::OR_LONG_2ADDR:
827 case Instruction::XOR_LONG_2ADDR:
828 if (rl_src[0].is_const || rl_src[1].is_const) {
829 GenArithImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
830 break;
831 }
832 // Note: intentional fallthrough.
833
834 case Instruction::MUL_LONG:
835 case Instruction::DIV_LONG:
836 case Instruction::REM_LONG:
837 case Instruction::MUL_LONG_2ADDR:
838 case Instruction::DIV_LONG_2ADDR:
839 case Instruction::REM_LONG_2ADDR:
840 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
841 break;
842
843 case Instruction::SHL_LONG:
844 case Instruction::SHR_LONG:
845 case Instruction::USHR_LONG:
846 case Instruction::SHL_LONG_2ADDR:
847 case Instruction::SHR_LONG_2ADDR:
848 case Instruction::USHR_LONG_2ADDR:
849 if (rl_src[1].is_const) {
850 GenShiftImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
851 } else {
852 GenShiftOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
853 }
854 break;
855
856 case Instruction::ADD_FLOAT:
857 case Instruction::SUB_FLOAT:
858 case Instruction::MUL_FLOAT:
859 case Instruction::DIV_FLOAT:
860 case Instruction::REM_FLOAT:
861 case Instruction::ADD_FLOAT_2ADDR:
862 case Instruction::SUB_FLOAT_2ADDR:
863 case Instruction::MUL_FLOAT_2ADDR:
864 case Instruction::DIV_FLOAT_2ADDR:
865 case Instruction::REM_FLOAT_2ADDR:
866 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[1]);
867 break;
868
869 case Instruction::ADD_DOUBLE:
870 case Instruction::SUB_DOUBLE:
871 case Instruction::MUL_DOUBLE:
872 case Instruction::DIV_DOUBLE:
873 case Instruction::REM_DOUBLE:
874 case Instruction::ADD_DOUBLE_2ADDR:
875 case Instruction::SUB_DOUBLE_2ADDR:
876 case Instruction::MUL_DOUBLE_2ADDR:
877 case Instruction::DIV_DOUBLE_2ADDR:
878 case Instruction::REM_DOUBLE_2ADDR:
879 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[1]);
880 break;
881
882 case Instruction::RSUB_INT:
883 case Instruction::ADD_INT_LIT16:
884 case Instruction::MUL_INT_LIT16:
885 case Instruction::DIV_INT_LIT16:
886 case Instruction::REM_INT_LIT16:
887 case Instruction::AND_INT_LIT16:
888 case Instruction::OR_INT_LIT16:
889 case Instruction::XOR_INT_LIT16:
890 case Instruction::ADD_INT_LIT8:
891 case Instruction::RSUB_INT_LIT8:
892 case Instruction::MUL_INT_LIT8:
893 case Instruction::DIV_INT_LIT8:
894 case Instruction::REM_INT_LIT8:
895 case Instruction::AND_INT_LIT8:
896 case Instruction::OR_INT_LIT8:
897 case Instruction::XOR_INT_LIT8:
898 case Instruction::SHL_INT_LIT8:
899 case Instruction::SHR_INT_LIT8:
900 case Instruction::USHR_INT_LIT8:
901 GenArithOpIntLit(opcode, rl_dest, rl_src[0], vC);
902 break;
903
904 default:
905 LOG(FATAL) << "Unexpected opcode: " << opcode;
906 }
Brian Carlstrom1895ea32013-07-18 13:28:37 -0700907} // NOLINT(readability/fn_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908
909// Process extended MIR instructions
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700910void Mir2Lir::HandleExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
912 case kMirOpCopy: {
913 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
914 RegLocation rl_dest = mir_graph_->GetDest(mir);
915 StoreValue(rl_dest, rl_src);
916 break;
917 }
918 case kMirOpFusedCmplFloat:
919 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, false /*double*/);
920 break;
921 case kMirOpFusedCmpgFloat:
922 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, false /*double*/);
923 break;
924 case kMirOpFusedCmplDouble:
925 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, true /*double*/);
926 break;
927 case kMirOpFusedCmpgDouble:
928 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, true /*double*/);
929 break;
930 case kMirOpFusedCmpLong:
931 GenFusedLongCmpBranch(bb, mir);
932 break;
933 case kMirOpSelect:
934 GenSelect(bb, mir);
935 break;
936 default:
937 break;
938 }
939}
940
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800941void Mir2Lir::GenPrintLabel(MIR* mir) {
942 // Mark the beginning of a Dalvik instruction for line tracking.
943 if (cu_->verbose) {
944 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
945 MarkBoundary(mir->offset, inst_str);
946 }
947}
948
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949// Handle the content in each basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700950bool Mir2Lir::MethodBlockCodeGen(BasicBlock* bb) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700951 if (bb->block_type == kDead) return false;
952 current_dalvik_offset_ = bb->start_offset;
953 MIR* mir;
954 int block_id = bb->id;
955
956 block_label_list_[block_id].operands[0] = bb->start_offset;
957
958 // Insert the block label.
959 block_label_list_[block_id].opcode = kPseudoNormalBlockLabel;
buzbeeb48819d2013-09-14 16:15:25 -0700960 block_label_list_[block_id].flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700961 AppendLIR(&block_label_list_[block_id]);
962
963 LIR* head_lir = NULL;
964
965 // If this is a catch block, export the start address.
966 if (bb->catch_entry) {
967 head_lir = NewLIR0(kPseudoExportedPC);
968 }
969
970 // Free temp registers and reset redundant store tracking.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 ClobberAllRegs();
972
973 if (bb->block_type == kEntryBlock) {
buzbee56c71782013-09-05 17:13:19 -0700974 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
976 GenEntrySequence(&mir_graph_->reg_location_[start_vreg],
977 mir_graph_->reg_location_[mir_graph_->GetMethodSReg()]);
978 } else if (bb->block_type == kExitBlock) {
buzbee56c71782013-09-05 17:13:19 -0700979 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 GenExitSequence();
981 }
982
983 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
984 ResetRegPool();
985 if (cu_->disable_opt & (1 << kTrackLiveTemps)) {
986 ClobberAllRegs();
987 }
988
989 if (cu_->disable_opt & (1 << kSuppressLoads)) {
990 ResetDefTracking();
991 }
992
993 // Reset temp tracking sanity check.
994 if (kIsDebugBuild) {
995 live_sreg_ = INVALID_SREG;
996 }
997
998 current_dalvik_offset_ = mir->offset;
999 int opcode = mir->dalvikInsn.opcode;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001001 GenPrintLabel(mir);
1002
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003 // Remember the first LIR for this block.
1004 if (head_lir == NULL) {
buzbee252254b2013-09-08 16:20:53 -07001005 head_lir = &block_label_list_[bb->id];
1006 // Set the first label as a scheduling barrier.
buzbeeb48819d2013-09-14 16:15:25 -07001007 DCHECK(!head_lir->flags.use_def_invalid);
1008 head_lir->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 }
1010
1011 if (opcode == kMirOpCheck) {
1012 // Combine check and work halves of throwing instruction.
1013 MIR* work_half = mir->meta.throw_insn;
1014 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Vladimir Marko4376c872014-01-23 12:39:29 +00001015 mir->meta = work_half->meta; // Whatever the work_half had, we need to copy it.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 opcode = work_half->dalvikInsn.opcode;
1017 SSARepresentation* ssa_rep = work_half->ssa_rep;
1018 work_half->ssa_rep = mir->ssa_rep;
1019 mir->ssa_rep = ssa_rep;
1020 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Vladimir Marko4376c872014-01-23 12:39:29 +00001021 work_half->meta.throw_insn = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 }
1023
1024 if (opcode >= kMirOpFirst) {
1025 HandleExtendedMethodMIR(bb, mir);
1026 continue;
1027 }
1028
1029 CompileDalvikInstruction(mir, bb, block_label_list_);
1030 }
1031
1032 if (head_lir) {
1033 // Eliminate redundant loads/stores and delay stores into later slots.
1034 ApplyLocalOptimizations(head_lir, last_lir_insn_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 }
1036 return false;
1037}
1038
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001039bool Mir2Lir::SpecialMIR2LIR(const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +00001040 cu_->NewTimingSplit("SpecialMIR2LIR");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 // Find the first DalvikByteCode block.
1042 int num_reachable_blocks = mir_graph_->GetNumReachableBlocks();
1043 BasicBlock*bb = NULL;
1044 for (int idx = 0; idx < num_reachable_blocks; idx++) {
1045 // TODO: no direct access of growable lists.
1046 int dfs_index = mir_graph_->GetDfsOrder()->Get(idx);
1047 bb = mir_graph_->GetBasicBlock(dfs_index);
1048 if (bb->block_type == kDalvikByteCode) {
1049 break;
1050 }
1051 }
1052 if (bb == NULL) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001053 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 }
1055 DCHECK_EQ(bb->start_offset, 0);
1056 DCHECK(bb->first_mir_insn != NULL);
1057
1058 // Get the first instruction.
1059 MIR* mir = bb->first_mir_insn;
1060
1061 // Free temp registers and reset redundant store tracking.
1062 ResetRegPool();
1063 ResetDefTracking();
1064 ClobberAllRegs();
1065
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001066 return GenSpecialCase(bb, mir, special);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067}
1068
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001069void Mir2Lir::MethodMIR2LIR() {
buzbeea61f4952013-08-23 14:27:06 -07001070 cu_->NewTimingSplit("MIR2LIR");
1071
Brian Carlstrom7940e442013-07-12 13:46:57 -07001072 // Hold the labels of each block.
1073 block_label_list_ =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001074 static_cast<LIR*>(arena_->Alloc(sizeof(LIR) * mir_graph_->GetNumBlocks(),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001075 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076
buzbee56c71782013-09-05 17:13:19 -07001077 PreOrderDfsIterator iter(mir_graph_);
buzbee252254b2013-09-08 16:20:53 -07001078 BasicBlock* curr_bb = iter.Next();
1079 BasicBlock* next_bb = iter.Next();
1080 while (curr_bb != NULL) {
1081 MethodBlockCodeGen(curr_bb);
1082 // If the fall_through block is no longer laid out consecutively, drop in a branch.
buzbee0d829482013-10-11 15:24:55 -07001083 BasicBlock* curr_bb_fall_through = mir_graph_->GetBasicBlock(curr_bb->fall_through);
1084 if ((curr_bb_fall_through != NULL) && (curr_bb_fall_through != next_bb)) {
1085 OpUnconditionalBranch(&block_label_list_[curr_bb->fall_through]);
buzbee252254b2013-09-08 16:20:53 -07001086 }
1087 curr_bb = next_bb;
1088 do {
1089 next_bb = iter.Next();
1090 } while ((next_bb != NULL) && (next_bb->block_type == kDead));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001091 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001092 HandleSlowPaths();
1093
buzbeea61f4952013-08-23 14:27:06 -07001094 cu_->NewTimingSplit("Launchpads");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001095 HandleSuspendLaunchPads();
1096
1097 HandleThrowLaunchPads();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001098}
1099
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001100//
1101// LIR Slow Path
1102//
1103
1104LIR* Mir2Lir::LIRSlowPath::GenerateTargetLabel() {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001105 m2l_->SetCurrentDexPc(current_dex_pc_);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001106 LIR* target = m2l_->NewLIR0(kPseudoTargetLabel);
1107 fromfast_->target = target;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001108 return target;
1109}
Vladimir Marko3bc86152014-03-13 14:11:28 +00001110
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111} // namespace art