blob: 0ffd1893ead018d11c8b1b7ecc08228f0f33447a [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
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()) {
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();
Vladimir Marko3bf7c602014-05-07 14:55:43 +010062 LoadBaseDisp(TargetReg(kSp), offset, new_regs, k64);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080063 } else {
64 reg_arg_high = AllocTemp();
65 int offset_high = offset + sizeof(uint32_t);
buzbee695d13a2014-04-19 13:32:20 -070066 Load32Disp(TargetReg(kSp), offset_high, reg_arg_high);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080067 }
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();
buzbee695d13a2014-04-19 13:32:20 -070073 Load32Disp(TargetReg(kSp), offset, reg_arg_low);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080074 }
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) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000084 int offset = StackVisitor::GetOutVROffset(in_position, cu_->instruction_set);
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 {
buzbee695d13a2014-04-19 13:32:20 -070099 Load32Disp(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);
buzbee695d13a2014-04-19 13:32:20 -0700110 Load32Disp(TargetReg(kSp), offset_high, rl_dest.reg.GetHigh());
buzbee2700f7e2014-03-07 09:46:20 -0800111 } else if (!reg_arg_low.Valid() && reg_arg_high.Valid()) {
112 OpRegCopy(rl_dest.reg.GetHigh(), reg_arg_high);
buzbee695d13a2014-04-19 13:32:20 -0700113 Load32Disp(TargetReg(kSp), offset, rl_dest.reg.GetLow());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800114 } else {
Vladimir Marko3bf7c602014-05-07 14:55:43 +0100115 LoadBaseDisp(TargetReg(kSp), offset, rl_dest.reg, k64);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800116 }
117 }
118}
119
120bool Mir2Lir::GenSpecialIGet(MIR* mir, const InlineMethod& special) {
121 // FastInstance() already checked by DexFileMethodInliner.
122 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100123 if (data.method_is_static != 0u || data.object_arg != 0u) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800124 // The object is not "this" and has to be null-checked.
125 return false;
126 }
127
Vladimir Markoe3e02602014-03-12 15:42:41 +0000128 bool wide = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_WIDE));
Vladimir Marko455759b2014-05-06 20:49:36 +0100129 bool ref = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT));
130 OpSize size = LoadStoreOpSize(wide, ref);
Vladimir Marko674744e2014-04-24 15:18:26 +0100131 if (data.is_volatile && !SupportsVolatileLoadStore(size)) {
132 return false;
133 }
Vladimir Marko455759b2014-05-06 20:49:36 +0100134
Vladimir Markoe3e02602014-03-12 15:42:41 +0000135 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800136 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
137
138 // Point of no return - no aborts after this
139 GenPrintLabel(mir);
140 LockArg(data.object_arg);
141 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
buzbee2700f7e2014-03-07 09:46:20 -0800142 RegStorage reg_obj = LoadArg(data.object_arg);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800143 if (data.is_volatile) {
Vladimir Marko674744e2014-04-24 15:18:26 +0100144 LoadBaseDispVolatile(reg_obj, data.field_offset, rl_dest.reg, size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800145 // Without context sensitive analysis, we must issue the most conservative barriers.
146 // In this case, either a load or store may follow so we issue both barriers.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800147 GenMemBarrier(kLoadLoad);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800148 GenMemBarrier(kLoadStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100149 } else {
150 LoadBaseDisp(reg_obj, data.field_offset, rl_dest.reg, size);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800151 }
152 return true;
153}
154
155bool Mir2Lir::GenSpecialIPut(MIR* mir, const InlineMethod& special) {
156 // FastInstance() already checked by DexFileMethodInliner.
157 const InlineIGetIPutData& data = special.d.ifield_data;
Vladimir Markoe1fced12014-04-04 14:52:53 +0100158 if (data.method_is_static != 0u || data.object_arg != 0u) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800159 // The object is not "this" and has to be null-checked.
160 return false;
161 }
Vladimir Markoe1fced12014-04-04 14:52:53 +0100162 if (data.return_arg_plus1 != 0u) {
163 // The setter returns a method argument which we don't support here.
164 return false;
165 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800166
Vladimir Markoe3e02602014-03-12 15:42:41 +0000167 bool wide = (data.op_variant == InlineMethodAnalyser::IPutVariant(Instruction::IPUT_WIDE));
Vladimir Marko455759b2014-05-06 20:49:36 +0100168 bool ref = (data.op_variant == InlineMethodAnalyser::IGetVariant(Instruction::IGET_OBJECT));
169 OpSize size = LoadStoreOpSize(wide, ref);
Vladimir Marko674744e2014-04-24 15:18:26 +0100170 if (data.is_volatile && !SupportsVolatileLoadStore(size)) {
171 return false;
172 }
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800173
174 // Point of no return - no aborts after this
175 GenPrintLabel(mir);
176 LockArg(data.object_arg);
177 LockArg(data.src_arg, wide);
buzbee2700f7e2014-03-07 09:46:20 -0800178 RegStorage reg_obj = LoadArg(data.object_arg);
179 RegStorage reg_src = LoadArg(data.src_arg, wide);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800180 if (data.is_volatile) {
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800181 // There might have been a store before this volatile one so insert StoreStore barrier.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800182 GenMemBarrier(kStoreStore);
Vladimir Marko674744e2014-04-24 15:18:26 +0100183 StoreBaseDispVolatile(reg_obj, data.field_offset, reg_src, size);
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800184 // A load might follow the volatile store so insert a StoreLoad barrier.
185 GenMemBarrier(kStoreLoad);
Vladimir Marko674744e2014-04-24 15:18:26 +0100186 } else {
187 StoreBaseDisp(reg_obj, data.field_offset, reg_src, size);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800188 }
Vladimir Marko455759b2014-05-06 20:49:36 +0100189 if (ref) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800190 MarkGCCard(reg_src, reg_obj);
191 }
192 return true;
193}
194
195bool Mir2Lir::GenSpecialIdentity(MIR* mir, const InlineMethod& special) {
196 const InlineReturnArgData& data = special.d.return_data;
Vladimir Markoe3e02602014-03-12 15:42:41 +0000197 bool wide = (data.is_wide != 0u);
198 // The inliner doesn't distinguish kDouble or kFloat, use shorty.
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800199 bool double_or_float = cu_->shorty[0] == 'F' || cu_->shorty[0] == 'D';
200
201 // Point of no return - no aborts after this
202 GenPrintLabel(mir);
203 LockArg(data.arg, wide);
204 RegLocation rl_dest = wide ? GetReturnWide(double_or_float) : GetReturn(double_or_float);
205 LoadArgDirect(data.arg, rl_dest);
206 return true;
207}
208
209/*
210 * Special-case code generation for simple non-throwing leaf methods.
211 */
212bool Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
213 DCHECK(special.flags & kInlineSpecial);
214 current_dalvik_offset_ = mir->offset;
215 MIR* return_mir = nullptr;
216 bool successful = false;
217
218 switch (special.opcode) {
219 case kInlineOpNop:
220 successful = true;
221 DCHECK_EQ(mir->dalvikInsn.opcode, Instruction::RETURN_VOID);
222 return_mir = mir;
223 break;
224 case kInlineOpNonWideConst: {
225 successful = true;
226 RegLocation rl_dest = GetReturn(cu_->shorty[0] == 'F');
227 GenPrintLabel(mir);
buzbee2700f7e2014-03-07 09:46:20 -0800228 LoadConstant(rl_dest.reg, static_cast<int>(special.d.data));
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 }
232 case kInlineOpReturnArg:
233 successful = GenSpecialIdentity(mir, special);
234 return_mir = mir;
235 break;
236 case kInlineOpIGet:
237 successful = GenSpecialIGet(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700238 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800239 break;
240 case kInlineOpIPut:
241 successful = GenSpecialIPut(mir, special);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700242 return_mir = bb->GetNextUnconditionalMir(mir_graph_, mir);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800243 break;
244 default:
245 break;
246 }
247
248 if (successful) {
Vladimir Marko39d95e62014-02-28 12:51:24 +0000249 if (kIsDebugBuild) {
250 // Clear unreachable catch entries.
251 mir_graph_->catches_.clear();
252 }
253
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800254 // Handle verbosity for return MIR.
255 if (return_mir != nullptr) {
256 current_dalvik_offset_ = return_mir->offset;
257 // Not handling special identity case because it already generated code as part
258 // of the return. The label should have been added before any code was generated.
259 if (special.opcode != kInlineOpReturnArg) {
260 GenPrintLabel(return_mir);
261 }
262 }
263 GenSpecialExitSequence();
264
265 core_spill_mask_ = 0;
266 num_core_spills_ = 0;
267 fp_spill_mask_ = 0;
268 num_fp_spills_ = 0;
269 frame_size_ = 0;
270 core_vmap_table_.clear();
271 fp_vmap_table_.clear();
272 }
273
274 return successful;
275}
276
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277/*
278 * Target-independent code generation. Use only high-level
279 * load/store utilities here, or target-dependent genXX() handlers
280 * when necessary.
281 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700282void Mir2Lir::CompileDalvikInstruction(MIR* mir, BasicBlock* bb, LIR* label_list) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 RegLocation rl_src[3];
284 RegLocation rl_dest = mir_graph_->GetBadLoc();
285 RegLocation rl_result = mir_graph_->GetBadLoc();
286 Instruction::Code opcode = mir->dalvikInsn.opcode;
287 int opt_flags = mir->optimization_flags;
288 uint32_t vB = mir->dalvikInsn.vB;
289 uint32_t vC = mir->dalvikInsn.vC;
290
291 // Prep Src and Dest locations.
292 int next_sreg = 0;
293 int next_loc = 0;
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700294 uint64_t attrs = MIRGraph::GetDataFlowAttributes(opcode);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 rl_src[0] = rl_src[1] = rl_src[2] = mir_graph_->GetBadLoc();
296 if (attrs & DF_UA) {
297 if (attrs & DF_A_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_UB) {
306 if (attrs & DF_B_WIDE) {
307 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
308 next_sreg+= 2;
309 } else {
310 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
311 next_sreg++;
312 }
313 }
314 if (attrs & DF_UC) {
315 if (attrs & DF_C_WIDE) {
316 rl_src[next_loc++] = mir_graph_->GetSrcWide(mir, next_sreg);
317 } else {
318 rl_src[next_loc++] = mir_graph_->GetSrc(mir, next_sreg);
319 }
320 }
321 if (attrs & DF_DA) {
322 if (attrs & DF_A_WIDE) {
323 rl_dest = mir_graph_->GetDestWide(mir);
324 } else {
325 rl_dest = mir_graph_->GetDest(mir);
326 }
327 }
328 switch (opcode) {
329 case Instruction::NOP:
330 break;
331
332 case Instruction::MOVE_EXCEPTION:
333 GenMoveException(rl_dest);
334 break;
335
336 case Instruction::RETURN_VOID:
337 if (((cu_->access_flags & kAccConstructor) != 0) &&
338 cu_->compiler_driver->RequiresConstructorBarrier(Thread::Current(), cu_->dex_file,
339 cu_->class_def_idx)) {
340 GenMemBarrier(kStoreStore);
341 }
342 if (!mir_graph_->MethodIsLeaf()) {
343 GenSuspendTest(opt_flags);
344 }
345 break;
346
347 case Instruction::RETURN:
348 case Instruction::RETURN_OBJECT:
349 if (!mir_graph_->MethodIsLeaf()) {
350 GenSuspendTest(opt_flags);
351 }
352 StoreValue(GetReturn(cu_->shorty[0] == 'F'), rl_src[0]);
353 break;
354
355 case Instruction::RETURN_WIDE:
356 if (!mir_graph_->MethodIsLeaf()) {
357 GenSuspendTest(opt_flags);
358 }
359 StoreValueWide(GetReturnWide(cu_->shorty[0] == 'D'), rl_src[0]);
360 break;
361
362 case Instruction::MOVE_RESULT_WIDE:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000363 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000365 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 StoreValueWide(rl_dest, GetReturnWide(rl_dest.fp));
367 break;
368
369 case Instruction::MOVE_RESULT:
370 case Instruction::MOVE_RESULT_OBJECT:
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000371 if ((opt_flags & MIR_INLINED) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 break; // Nop - combined w/ previous invoke.
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000373 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 StoreValue(rl_dest, GetReturn(rl_dest.fp));
375 break;
376
377 case Instruction::MOVE:
378 case Instruction::MOVE_OBJECT:
379 case Instruction::MOVE_16:
380 case Instruction::MOVE_OBJECT_16:
381 case Instruction::MOVE_FROM16:
382 case Instruction::MOVE_OBJECT_FROM16:
383 StoreValue(rl_dest, rl_src[0]);
384 break;
385
386 case Instruction::MOVE_WIDE:
387 case Instruction::MOVE_WIDE_16:
388 case Instruction::MOVE_WIDE_FROM16:
389 StoreValueWide(rl_dest, rl_src[0]);
390 break;
391
392 case Instruction::CONST:
393 case Instruction::CONST_4:
394 case Instruction::CONST_16:
395 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800396 LoadConstantNoClobber(rl_result.reg, vB);
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_HIGH16:
404 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800405 LoadConstantNoClobber(rl_result.reg, vB << 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 StoreValue(rl_dest, rl_result);
407 if (vB == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800408 Workaround7250540(rl_dest, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 }
410 break;
411
412 case Instruction::CONST_WIDE_16:
413 case Instruction::CONST_WIDE_32:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000414 GenConstWide(rl_dest, static_cast<int64_t>(static_cast<int32_t>(vB)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 break;
416
417 case Instruction::CONST_WIDE:
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000418 GenConstWide(rl_dest, mir->dalvikInsn.vB_wide);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700419 break;
420
421 case Instruction::CONST_WIDE_HIGH16:
422 rl_result = EvalLoc(rl_dest, kAnyReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800423 LoadConstantWide(rl_result.reg, static_cast<int64_t>(vB) << 48);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 StoreValueWide(rl_dest, rl_result);
425 break;
426
427 case Instruction::MONITOR_ENTER:
428 GenMonitorEnter(opt_flags, rl_src[0]);
429 break;
430
431 case Instruction::MONITOR_EXIT:
432 GenMonitorExit(opt_flags, rl_src[0]);
433 break;
434
435 case Instruction::CHECK_CAST: {
436 GenCheckCast(mir->offset, vB, rl_src[0]);
437 break;
438 }
439 case Instruction::INSTANCE_OF:
440 GenInstanceof(vC, rl_dest, rl_src[0]);
441 break;
442
443 case Instruction::NEW_INSTANCE:
444 GenNewInstance(vB, rl_dest);
445 break;
446
447 case Instruction::THROW:
448 GenThrow(rl_src[0]);
449 break;
450
451 case Instruction::ARRAY_LENGTH:
452 int len_offset;
453 len_offset = mirror::Array::LengthOffset().Int32Value();
454 rl_src[0] = LoadValue(rl_src[0], kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800455 GenNullCheck(rl_src[0].reg, opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700456 rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee695d13a2014-04-19 13:32:20 -0700457 Load32Disp(rl_src[0].reg, len_offset, rl_result.reg);
Dave Allisonf9439142014-03-27 15:10:22 -0700458 MarkPossibleNullPointerException(opt_flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 StoreValue(rl_dest, rl_result);
460 break;
461
462 case Instruction::CONST_STRING:
463 case Instruction::CONST_STRING_JUMBO:
464 GenConstString(vB, rl_dest);
465 break;
466
467 case Instruction::CONST_CLASS:
468 GenConstClass(vB, rl_dest);
469 break;
470
471 case Instruction::FILL_ARRAY_DATA:
472 GenFillArrayData(vB, rl_src[0]);
473 break;
474
475 case Instruction::FILLED_NEW_ARRAY:
476 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
477 false /* not range */));
478 break;
479
480 case Instruction::FILLED_NEW_ARRAY_RANGE:
481 GenFilledNewArray(mir_graph_->NewMemCallInfo(bb, mir, kStatic,
482 true /* range */));
483 break;
484
485 case Instruction::NEW_ARRAY:
486 GenNewArray(vC, rl_dest, rl_src[0]);
487 break;
488
489 case Instruction::GOTO:
490 case Instruction::GOTO_16:
491 case Instruction::GOTO_32:
buzbee9329e6d2013-08-19 12:55:10 -0700492 if (mir_graph_->IsBackedge(bb, bb->taken)) {
buzbee0d829482013-10-11 15:24:55 -0700493 GenSuspendTestAndBranch(opt_flags, &label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 } else {
buzbee0d829482013-10-11 15:24:55 -0700495 OpUnconditionalBranch(&label_list[bb->taken]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496 }
497 break;
498
499 case Instruction::PACKED_SWITCH:
500 GenPackedSwitch(mir, vB, rl_src[0]);
501 break;
502
503 case Instruction::SPARSE_SWITCH:
504 GenSparseSwitch(mir, vB, rl_src[0]);
505 break;
506
507 case Instruction::CMPL_FLOAT:
508 case Instruction::CMPG_FLOAT:
509 case Instruction::CMPL_DOUBLE:
510 case Instruction::CMPG_DOUBLE:
511 GenCmpFP(opcode, rl_dest, rl_src[0], rl_src[1]);
512 break;
513
514 case Instruction::CMP_LONG:
515 GenCmpLong(rl_dest, rl_src[0], rl_src[1]);
516 break;
517
518 case Instruction::IF_EQ:
519 case Instruction::IF_NE:
520 case Instruction::IF_LT:
521 case Instruction::IF_GE:
522 case Instruction::IF_GT:
523 case Instruction::IF_LE: {
buzbee0d829482013-10-11 15:24:55 -0700524 LIR* taken = &label_list[bb->taken];
525 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 // Result known at compile time?
527 if (rl_src[0].is_const && rl_src[1].is_const) {
528 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg),
529 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
buzbee0d829482013-10-11 15:24:55 -0700530 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
531 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 GenSuspendTest(opt_flags);
533 }
buzbee0d829482013-10-11 15:24:55 -0700534 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700536 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700537 GenSuspendTest(opt_flags);
538 }
buzbee0d829482013-10-11 15:24:55 -0700539 GenCompareAndBranch(opcode, rl_src[0], rl_src[1], taken, fall_through);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 }
541 break;
542 }
543
544 case Instruction::IF_EQZ:
545 case Instruction::IF_NEZ:
546 case Instruction::IF_LTZ:
547 case Instruction::IF_GEZ:
548 case Instruction::IF_GTZ:
549 case Instruction::IF_LEZ: {
buzbee0d829482013-10-11 15:24:55 -0700550 LIR* taken = &label_list[bb->taken];
551 LIR* fall_through = &label_list[bb->fall_through];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 // Result known at compile time?
553 if (rl_src[0].is_const) {
554 bool is_taken = EvaluateBranch(opcode, mir_graph_->ConstantValue(rl_src[0].orig_sreg), 0);
buzbee0d829482013-10-11 15:24:55 -0700555 BasicBlockId target_id = is_taken ? bb->taken : bb->fall_through;
556 if (mir_graph_->IsBackedge(bb, target_id)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 GenSuspendTest(opt_flags);
558 }
buzbee0d829482013-10-11 15:24:55 -0700559 OpUnconditionalBranch(&label_list[target_id]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700560 } else {
buzbee9329e6d2013-08-19 12:55:10 -0700561 if (mir_graph_->IsBackwardsBranch(bb)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 GenSuspendTest(opt_flags);
563 }
564 GenCompareZeroAndBranch(opcode, rl_src[0], taken, fall_through);
565 }
566 break;
567 }
568
569 case Instruction::AGET_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700570 GenArrayGet(opt_flags, k64, rl_src[0], rl_src[1], rl_dest, 3);
571 break;
572 case Instruction::AGET_OBJECT:
573 GenArrayGet(opt_flags, kReference, rl_src[0], rl_src[1], rl_dest, 2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 break;
575 case Instruction::AGET:
buzbee695d13a2014-04-19 13:32:20 -0700576 GenArrayGet(opt_flags, k32, rl_src[0], rl_src[1], rl_dest, 2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 break;
578 case Instruction::AGET_BOOLEAN:
579 GenArrayGet(opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
580 break;
581 case Instruction::AGET_BYTE:
582 GenArrayGet(opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
583 break;
584 case Instruction::AGET_CHAR:
585 GenArrayGet(opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
586 break;
587 case Instruction::AGET_SHORT:
588 GenArrayGet(opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
589 break;
590 case Instruction::APUT_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700591 GenArrayPut(opt_flags, k64, rl_src[1], rl_src[2], rl_src[0], 3, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 break;
593 case Instruction::APUT:
buzbee695d13a2014-04-19 13:32:20 -0700594 GenArrayPut(opt_flags, k32, rl_src[1], rl_src[2], rl_src[0], 2, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700595 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700596 case Instruction::APUT_OBJECT: {
597 bool is_null = mir_graph_->IsConstantNullRef(rl_src[0]);
598 bool is_safe = is_null; // Always safe to store null.
599 if (!is_safe) {
600 // Check safety from verifier type information.
Vladimir Marko2730db02014-01-27 11:15:17 +0000601 const DexCompilationUnit* unit = mir_graph_->GetCurrentDexCompilationUnit();
602 is_safe = cu_->compiler_driver->IsSafeCast(unit, mir->offset);
Ian Rogersa9a82542013-10-04 11:17:26 -0700603 }
604 if (is_null || is_safe) {
605 // Store of constant null doesn't require an assignability test and can be generated inline
606 // without fixed register usage or a card mark.
buzbee695d13a2014-04-19 13:32:20 -0700607 GenArrayPut(opt_flags, kReference, rl_src[1], rl_src[2], rl_src[0], 2, !is_null);
Ian Rogersa9a82542013-10-04 11:17:26 -0700608 } else {
609 GenArrayObjPut(opt_flags, rl_src[1], rl_src[2], rl_src[0]);
610 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700612 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700613 case Instruction::APUT_SHORT:
614 case Instruction::APUT_CHAR:
Ian Rogersa9a82542013-10-04 11:17:26 -0700615 GenArrayPut(opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 break;
617 case Instruction::APUT_BYTE:
618 case Instruction::APUT_BOOLEAN:
Ian Rogersa9a82542013-10-04 11:17:26 -0700619 GenArrayPut(opt_flags, kUnsignedByte, rl_src[1], rl_src[2], rl_src[0], 0, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620 break;
621
622 case Instruction::IGET_OBJECT:
buzbee695d13a2014-04-19 13:32:20 -0700623 GenIGet(mir, opt_flags, kReference, rl_dest, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 break;
625
626 case Instruction::IGET_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700627 GenIGet(mir, opt_flags, k64, rl_dest, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628 break;
629
630 case Instruction::IGET:
buzbee695d13a2014-04-19 13:32:20 -0700631 GenIGet(mir, opt_flags, k32, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700632 break;
633
634 case Instruction::IGET_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000635 GenIGet(mir, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700636 break;
637
638 case Instruction::IGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000639 GenIGet(mir, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640 break;
641
642 case Instruction::IGET_BOOLEAN:
643 case Instruction::IGET_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000644 GenIGet(mir, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 break;
646
647 case Instruction::IPUT_WIDE:
buzbee695d13a2014-04-19 13:32:20 -0700648 GenIPut(mir, opt_flags, k64, rl_src[0], rl_src[1], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 break;
650
651 case Instruction::IPUT_OBJECT:
buzbee695d13a2014-04-19 13:32:20 -0700652 GenIPut(mir, opt_flags, kReference, rl_src[0], rl_src[1], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700653 break;
654
655 case Instruction::IPUT:
buzbee695d13a2014-04-19 13:32:20 -0700656 GenIPut(mir, opt_flags, k32, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700657 break;
658
659 case Instruction::IPUT_BOOLEAN:
660 case Instruction::IPUT_BYTE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000661 GenIPut(mir, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 break;
663
664 case Instruction::IPUT_CHAR:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000665 GenIPut(mir, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 break;
667
668 case Instruction::IPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000669 GenIPut(mir, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 break;
671
672 case Instruction::SGET_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000673 GenSget(mir, rl_dest, false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 break;
675 case Instruction::SGET:
676 case Instruction::SGET_BOOLEAN:
677 case Instruction::SGET_BYTE:
678 case Instruction::SGET_CHAR:
679 case Instruction::SGET_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000680 GenSget(mir, rl_dest, false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 break;
682
683 case Instruction::SGET_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000684 GenSget(mir, rl_dest, true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700685 break;
686
687 case Instruction::SPUT_OBJECT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000688 GenSput(mir, rl_src[0], false, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 break;
690
691 case Instruction::SPUT:
692 case Instruction::SPUT_BOOLEAN:
693 case Instruction::SPUT_BYTE:
694 case Instruction::SPUT_CHAR:
695 case Instruction::SPUT_SHORT:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000696 GenSput(mir, rl_src[0], false, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 break;
698
699 case Instruction::SPUT_WIDE:
Vladimir Markobe0e5462014-02-26 11:24:15 +0000700 GenSput(mir, rl_src[0], true, false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 break;
702
703 case Instruction::INVOKE_STATIC_RANGE:
704 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, true));
705 break;
706 case Instruction::INVOKE_STATIC:
707 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kStatic, false));
708 break;
709
710 case Instruction::INVOKE_DIRECT:
711 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, false));
712 break;
713 case Instruction::INVOKE_DIRECT_RANGE:
714 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kDirect, true));
715 break;
716
717 case Instruction::INVOKE_VIRTUAL:
718 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, false));
719 break;
720 case Instruction::INVOKE_VIRTUAL_RANGE:
721 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kVirtual, true));
722 break;
723
724 case Instruction::INVOKE_SUPER:
725 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, false));
726 break;
727 case Instruction::INVOKE_SUPER_RANGE:
728 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kSuper, true));
729 break;
730
731 case Instruction::INVOKE_INTERFACE:
732 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, false));
733 break;
734 case Instruction::INVOKE_INTERFACE_RANGE:
735 GenInvoke(mir_graph_->NewMemCallInfo(bb, mir, kInterface, true));
736 break;
737
738 case Instruction::NEG_INT:
739 case Instruction::NOT_INT:
740 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[0]);
741 break;
742
743 case Instruction::NEG_LONG:
744 case Instruction::NOT_LONG:
745 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[0]);
746 break;
747
748 case Instruction::NEG_FLOAT:
749 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[0]);
750 break;
751
752 case Instruction::NEG_DOUBLE:
753 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[0]);
754 break;
755
756 case Instruction::INT_TO_LONG:
757 GenIntToLong(rl_dest, rl_src[0]);
758 break;
759
760 case Instruction::LONG_TO_INT:
761 rl_src[0] = UpdateLocWide(rl_src[0]);
762 rl_src[0] = WideToNarrow(rl_src[0]);
763 StoreValue(rl_dest, rl_src[0]);
764 break;
765
766 case Instruction::INT_TO_BYTE:
767 case Instruction::INT_TO_SHORT:
768 case Instruction::INT_TO_CHAR:
769 GenIntNarrowing(opcode, rl_dest, rl_src[0]);
770 break;
771
772 case Instruction::INT_TO_FLOAT:
773 case Instruction::INT_TO_DOUBLE:
774 case Instruction::LONG_TO_FLOAT:
775 case Instruction::LONG_TO_DOUBLE:
776 case Instruction::FLOAT_TO_INT:
777 case Instruction::FLOAT_TO_LONG:
778 case Instruction::FLOAT_TO_DOUBLE:
779 case Instruction::DOUBLE_TO_INT:
780 case Instruction::DOUBLE_TO_LONG:
781 case Instruction::DOUBLE_TO_FLOAT:
782 GenConversion(opcode, rl_dest, rl_src[0]);
783 break;
784
785
786 case Instruction::ADD_INT:
787 case Instruction::ADD_INT_2ADDR:
788 case Instruction::MUL_INT:
789 case Instruction::MUL_INT_2ADDR:
790 case Instruction::AND_INT:
791 case Instruction::AND_INT_2ADDR:
792 case Instruction::OR_INT:
793 case Instruction::OR_INT_2ADDR:
794 case Instruction::XOR_INT:
795 case Instruction::XOR_INT_2ADDR:
796 if (rl_src[0].is_const &&
797 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[0]))) {
798 GenArithOpIntLit(opcode, rl_dest, rl_src[1],
799 mir_graph_->ConstantValue(rl_src[0].orig_sreg));
800 } else if (rl_src[1].is_const &&
801 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
802 GenArithOpIntLit(opcode, rl_dest, rl_src[0],
803 mir_graph_->ConstantValue(rl_src[1].orig_sreg));
804 } else {
805 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
806 }
807 break;
808
809 case Instruction::SUB_INT:
810 case Instruction::SUB_INT_2ADDR:
811 case Instruction::DIV_INT:
812 case Instruction::DIV_INT_2ADDR:
813 case Instruction::REM_INT:
814 case Instruction::REM_INT_2ADDR:
815 case Instruction::SHL_INT:
816 case Instruction::SHL_INT_2ADDR:
817 case Instruction::SHR_INT:
818 case Instruction::SHR_INT_2ADDR:
819 case Instruction::USHR_INT:
820 case Instruction::USHR_INT_2ADDR:
821 if (rl_src[1].is_const &&
822 InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src[1]))) {
823 GenArithOpIntLit(opcode, rl_dest, rl_src[0], mir_graph_->ConstantValue(rl_src[1]));
824 } else {
825 GenArithOpInt(opcode, rl_dest, rl_src[0], rl_src[1]);
826 }
827 break;
828
829 case Instruction::ADD_LONG:
830 case Instruction::SUB_LONG:
831 case Instruction::AND_LONG:
832 case Instruction::OR_LONG:
833 case Instruction::XOR_LONG:
834 case Instruction::ADD_LONG_2ADDR:
835 case Instruction::SUB_LONG_2ADDR:
836 case Instruction::AND_LONG_2ADDR:
837 case Instruction::OR_LONG_2ADDR:
838 case Instruction::XOR_LONG_2ADDR:
839 if (rl_src[0].is_const || rl_src[1].is_const) {
840 GenArithImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
841 break;
842 }
843 // Note: intentional fallthrough.
844
845 case Instruction::MUL_LONG:
846 case Instruction::DIV_LONG:
847 case Instruction::REM_LONG:
848 case Instruction::MUL_LONG_2ADDR:
849 case Instruction::DIV_LONG_2ADDR:
850 case Instruction::REM_LONG_2ADDR:
851 GenArithOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
852 break;
853
854 case Instruction::SHL_LONG:
855 case Instruction::SHR_LONG:
856 case Instruction::USHR_LONG:
857 case Instruction::SHL_LONG_2ADDR:
858 case Instruction::SHR_LONG_2ADDR:
859 case Instruction::USHR_LONG_2ADDR:
860 if (rl_src[1].is_const) {
861 GenShiftImmOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
862 } else {
863 GenShiftOpLong(opcode, rl_dest, rl_src[0], rl_src[1]);
864 }
865 break;
866
867 case Instruction::ADD_FLOAT:
868 case Instruction::SUB_FLOAT:
869 case Instruction::MUL_FLOAT:
870 case Instruction::DIV_FLOAT:
871 case Instruction::REM_FLOAT:
872 case Instruction::ADD_FLOAT_2ADDR:
873 case Instruction::SUB_FLOAT_2ADDR:
874 case Instruction::MUL_FLOAT_2ADDR:
875 case Instruction::DIV_FLOAT_2ADDR:
876 case Instruction::REM_FLOAT_2ADDR:
877 GenArithOpFloat(opcode, rl_dest, rl_src[0], rl_src[1]);
878 break;
879
880 case Instruction::ADD_DOUBLE:
881 case Instruction::SUB_DOUBLE:
882 case Instruction::MUL_DOUBLE:
883 case Instruction::DIV_DOUBLE:
884 case Instruction::REM_DOUBLE:
885 case Instruction::ADD_DOUBLE_2ADDR:
886 case Instruction::SUB_DOUBLE_2ADDR:
887 case Instruction::MUL_DOUBLE_2ADDR:
888 case Instruction::DIV_DOUBLE_2ADDR:
889 case Instruction::REM_DOUBLE_2ADDR:
890 GenArithOpDouble(opcode, rl_dest, rl_src[0], rl_src[1]);
891 break;
892
893 case Instruction::RSUB_INT:
894 case Instruction::ADD_INT_LIT16:
895 case Instruction::MUL_INT_LIT16:
896 case Instruction::DIV_INT_LIT16:
897 case Instruction::REM_INT_LIT16:
898 case Instruction::AND_INT_LIT16:
899 case Instruction::OR_INT_LIT16:
900 case Instruction::XOR_INT_LIT16:
901 case Instruction::ADD_INT_LIT8:
902 case Instruction::RSUB_INT_LIT8:
903 case Instruction::MUL_INT_LIT8:
904 case Instruction::DIV_INT_LIT8:
905 case Instruction::REM_INT_LIT8:
906 case Instruction::AND_INT_LIT8:
907 case Instruction::OR_INT_LIT8:
908 case Instruction::XOR_INT_LIT8:
909 case Instruction::SHL_INT_LIT8:
910 case Instruction::SHR_INT_LIT8:
911 case Instruction::USHR_INT_LIT8:
912 GenArithOpIntLit(opcode, rl_dest, rl_src[0], vC);
913 break;
914
915 default:
916 LOG(FATAL) << "Unexpected opcode: " << opcode;
917 }
Brian Carlstrom1895ea32013-07-18 13:28:37 -0700918} // NOLINT(readability/fn_size)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700919
920// Process extended MIR instructions
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700921void Mir2Lir::HandleExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700922 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
923 case kMirOpCopy: {
924 RegLocation rl_src = mir_graph_->GetSrc(mir, 0);
925 RegLocation rl_dest = mir_graph_->GetDest(mir);
926 StoreValue(rl_dest, rl_src);
927 break;
928 }
929 case kMirOpFusedCmplFloat:
930 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, false /*double*/);
931 break;
932 case kMirOpFusedCmpgFloat:
933 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, false /*double*/);
934 break;
935 case kMirOpFusedCmplDouble:
936 GenFusedFPCmpBranch(bb, mir, false /*gt bias*/, true /*double*/);
937 break;
938 case kMirOpFusedCmpgDouble:
939 GenFusedFPCmpBranch(bb, mir, true /*gt bias*/, true /*double*/);
940 break;
941 case kMirOpFusedCmpLong:
942 GenFusedLongCmpBranch(bb, mir);
943 break;
944 case kMirOpSelect:
945 GenSelect(bb, mir);
946 break;
947 default:
948 break;
949 }
950}
951
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800952void Mir2Lir::GenPrintLabel(MIR* mir) {
953 // Mark the beginning of a Dalvik instruction for line tracking.
954 if (cu_->verbose) {
955 char* inst_str = mir_graph_->GetDalvikDisassembly(mir);
956 MarkBoundary(mir->offset, inst_str);
957 }
958}
959
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960// Handle the content in each basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700961bool Mir2Lir::MethodBlockCodeGen(BasicBlock* bb) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 if (bb->block_type == kDead) return false;
963 current_dalvik_offset_ = bb->start_offset;
964 MIR* mir;
965 int block_id = bb->id;
966
967 block_label_list_[block_id].operands[0] = bb->start_offset;
968
969 // Insert the block label.
970 block_label_list_[block_id].opcode = kPseudoNormalBlockLabel;
buzbeeb48819d2013-09-14 16:15:25 -0700971 block_label_list_[block_id].flags.fixup = kFixupLabel;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 AppendLIR(&block_label_list_[block_id]);
973
974 LIR* head_lir = NULL;
975
976 // If this is a catch block, export the start address.
977 if (bb->catch_entry) {
978 head_lir = NewLIR0(kPseudoExportedPC);
979 }
980
981 // Free temp registers and reset redundant store tracking.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 ClobberAllRegs();
983
984 if (bb->block_type == kEntryBlock) {
buzbee56c71782013-09-05 17:13:19 -0700985 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700986 int start_vreg = cu_->num_dalvik_registers - cu_->num_ins;
987 GenEntrySequence(&mir_graph_->reg_location_[start_vreg],
988 mir_graph_->reg_location_[mir_graph_->GetMethodSReg()]);
989 } else if (bb->block_type == kExitBlock) {
buzbee56c71782013-09-05 17:13:19 -0700990 ResetRegPool();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700991 GenExitSequence();
992 }
993
994 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
995 ResetRegPool();
996 if (cu_->disable_opt & (1 << kTrackLiveTemps)) {
997 ClobberAllRegs();
buzbee7a11ab02014-04-28 20:02:38 -0700998 // Reset temp allocation to minimize differences when A/B testing.
buzbee091cc402014-03-31 10:14:40 -0700999 reg_pool_->ResetNextTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000 }
1001
1002 if (cu_->disable_opt & (1 << kSuppressLoads)) {
1003 ResetDefTracking();
1004 }
1005
1006 // Reset temp tracking sanity check.
1007 if (kIsDebugBuild) {
1008 live_sreg_ = INVALID_SREG;
1009 }
1010
1011 current_dalvik_offset_ = mir->offset;
1012 int opcode = mir->dalvikInsn.opcode;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001014 GenPrintLabel(mir);
1015
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 // Remember the first LIR for this block.
1017 if (head_lir == NULL) {
buzbee252254b2013-09-08 16:20:53 -07001018 head_lir = &block_label_list_[bb->id];
1019 // Set the first label as a scheduling barrier.
buzbeeb48819d2013-09-14 16:15:25 -07001020 DCHECK(!head_lir->flags.use_def_invalid);
1021 head_lir->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 }
1023
1024 if (opcode == kMirOpCheck) {
1025 // Combine check and work halves of throwing instruction.
1026 MIR* work_half = mir->meta.throw_insn;
1027 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
Vladimir Marko4376c872014-01-23 12:39:29 +00001028 mir->meta = work_half->meta; // Whatever the work_half had, we need to copy it.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001029 opcode = work_half->dalvikInsn.opcode;
1030 SSARepresentation* ssa_rep = work_half->ssa_rep;
1031 work_half->ssa_rep = mir->ssa_rep;
1032 mir->ssa_rep = ssa_rep;
1033 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Vladimir Marko4376c872014-01-23 12:39:29 +00001034 work_half->meta.throw_insn = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001035 }
1036
1037 if (opcode >= kMirOpFirst) {
1038 HandleExtendedMethodMIR(bb, mir);
1039 continue;
1040 }
1041
1042 CompileDalvikInstruction(mir, bb, block_label_list_);
1043 }
1044
1045 if (head_lir) {
1046 // Eliminate redundant loads/stores and delay stores into later slots.
1047 ApplyLocalOptimizations(head_lir, last_lir_insn_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 }
1049 return false;
1050}
1051
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001052bool Mir2Lir::SpecialMIR2LIR(const InlineMethod& special) {
Vladimir Marko5816ed42013-11-27 17:04:20 +00001053 cu_->NewTimingSplit("SpecialMIR2LIR");
Brian Carlstrom7940e442013-07-12 13:46:57 -07001054 // Find the first DalvikByteCode block.
1055 int num_reachable_blocks = mir_graph_->GetNumReachableBlocks();
1056 BasicBlock*bb = NULL;
1057 for (int idx = 0; idx < num_reachable_blocks; idx++) {
1058 // TODO: no direct access of growable lists.
1059 int dfs_index = mir_graph_->GetDfsOrder()->Get(idx);
1060 bb = mir_graph_->GetBasicBlock(dfs_index);
1061 if (bb->block_type == kDalvikByteCode) {
1062 break;
1063 }
1064 }
1065 if (bb == NULL) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001066 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001067 }
1068 DCHECK_EQ(bb->start_offset, 0);
1069 DCHECK(bb->first_mir_insn != NULL);
1070
1071 // Get the first instruction.
1072 MIR* mir = bb->first_mir_insn;
1073
1074 // Free temp registers and reset redundant store tracking.
1075 ResetRegPool();
1076 ResetDefTracking();
1077 ClobberAllRegs();
1078
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001079 return GenSpecialCase(bb, mir, special);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001080}
1081
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001082void Mir2Lir::MethodMIR2LIR() {
buzbeea61f4952013-08-23 14:27:06 -07001083 cu_->NewTimingSplit("MIR2LIR");
1084
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 // Hold the labels of each block.
1086 block_label_list_ =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001087 static_cast<LIR*>(arena_->Alloc(sizeof(LIR) * mir_graph_->GetNumBlocks(),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001088 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001089
buzbee56c71782013-09-05 17:13:19 -07001090 PreOrderDfsIterator iter(mir_graph_);
buzbee252254b2013-09-08 16:20:53 -07001091 BasicBlock* curr_bb = iter.Next();
1092 BasicBlock* next_bb = iter.Next();
1093 while (curr_bb != NULL) {
1094 MethodBlockCodeGen(curr_bb);
1095 // If the fall_through block is no longer laid out consecutively, drop in a branch.
buzbee0d829482013-10-11 15:24:55 -07001096 BasicBlock* curr_bb_fall_through = mir_graph_->GetBasicBlock(curr_bb->fall_through);
1097 if ((curr_bb_fall_through != NULL) && (curr_bb_fall_through != next_bb)) {
1098 OpUnconditionalBranch(&block_label_list_[curr_bb->fall_through]);
buzbee252254b2013-09-08 16:20:53 -07001099 }
1100 curr_bb = next_bb;
1101 do {
1102 next_bb = iter.Next();
1103 } while ((next_bb != NULL) && (next_bb->block_type == kDead));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 }
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001105 HandleSlowPaths();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001106}
1107
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001108//
1109// LIR Slow Path
1110//
1111
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001112LIR* Mir2Lir::LIRSlowPath::GenerateTargetLabel(int opcode) {
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001113 m2l_->SetCurrentDexPc(current_dex_pc_);
Mingyao Yang6ffcfa02014-04-25 11:06:00 -07001114 LIR* target = m2l_->NewLIR0(opcode);
Vladimir Marko3bc86152014-03-13 14:11:28 +00001115 fromfast_->target = target;
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001116 return target;
1117}
Vladimir Marko3bc86152014-03-13 14:11:28 +00001118
Brian Carlstrom7940e442013-07-12 13:46:57 -07001119} // namespace art