blob: 77d581fd6e118aafbfb22b587f41bde9f97ea40b [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
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 "object_utils.h"
18
buzbee1bc37c62012-11-20 13:35:41 -080019#include "../compiler_internals.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080020#include "local_optimizations.h"
buzbee1bc37c62012-11-20 13:35:41 -080021#include "codegen_util.h"
22#include "ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080023
buzbeee3acd072012-02-25 17:03:10 -080024namespace art {
25
buzbeee3acd072012-02-25 17:03:10 -080026/*
27 * Target-independent code generation. Use only high-level
28 * load/store utilities here, or target-dependent genXX() handlers
29 * when necessary.
30 */
buzbeefa57c472012-11-21 12:06:18 -080031static bool CompileDalvikInstruction(CompilationUnit* cu, MIR* mir, BasicBlock* bb,
32 LIR* label_list)
buzbeee3acd072012-02-25 17:03:10 -080033{
buzbee02031b12012-11-23 09:41:35 -080034 Codegen* cg = cu->cg.get();
Bill Buzbeea114add2012-05-03 15:00:40 -070035 bool res = false; // Assume success
buzbeefa57c472012-11-21 12:06:18 -080036 RegLocation rl_src[3];
buzbee02031b12012-11-23 09:41:35 -080037 RegLocation rl_dest = GetBadLoc();
38 RegLocation rl_result = GetBadLoc();
Bill Buzbeea114add2012-05-03 15:00:40 -070039 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -080040 int opt_flags = mir->optimization_flags;
buzbee408ad162012-06-06 16:45:18 -070041 uint32_t vB = mir->dalvikInsn.vB;
42 uint32_t vC = mir->dalvikInsn.vC;
buzbeee3acd072012-02-25 17:03:10 -080043
buzbee02031b12012-11-23 09:41:35 -080044 // Prep Src and Dest locations.
buzbeefa57c472012-11-21 12:06:18 -080045 int next_sreg = 0;
46 int next_loc = 0;
47 int attrs = oat_data_flow_attributes[opcode];
buzbee02031b12012-11-23 09:41:35 -080048 rl_src[0] = rl_src[1] = rl_src[2] = GetBadLoc();
Bill Buzbeea114add2012-05-03 15:00:40 -070049 if (attrs & DF_UA) {
buzbeebff24652012-05-06 16:22:05 -070050 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -080051 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
52 next_sreg+= 2;
buzbeebff24652012-05-06 16:22:05 -070053 } else {
buzbeefa57c472012-11-21 12:06:18 -080054 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
55 next_sreg++;
buzbeebff24652012-05-06 16:22:05 -070056 }
Bill Buzbeea114add2012-05-03 15:00:40 -070057 }
58 if (attrs & DF_UB) {
buzbeebff24652012-05-06 16:22:05 -070059 if (attrs & DF_B_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -080060 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
61 next_sreg+= 2;
buzbeebff24652012-05-06 16:22:05 -070062 } else {
buzbeefa57c472012-11-21 12:06:18 -080063 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
64 next_sreg++;
buzbeebff24652012-05-06 16:22:05 -070065 }
Bill Buzbeea114add2012-05-03 15:00:40 -070066 }
67 if (attrs & DF_UC) {
buzbeebff24652012-05-06 16:22:05 -070068 if (attrs & DF_C_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -080069 rl_src[next_loc++] = GetSrcWide(cu, mir, next_sreg);
buzbeebff24652012-05-06 16:22:05 -070070 } else {
buzbeefa57c472012-11-21 12:06:18 -080071 rl_src[next_loc++] = GetSrc(cu, mir, next_sreg);
buzbeebff24652012-05-06 16:22:05 -070072 }
Bill Buzbeea114add2012-05-03 15:00:40 -070073 }
74 if (attrs & DF_DA) {
buzbeebff24652012-05-06 16:22:05 -070075 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -080076 rl_dest = GetDestWide(cu, mir);
buzbeebff24652012-05-06 16:22:05 -070077 } else {
buzbeefa57c472012-11-21 12:06:18 -080078 rl_dest = GetDest(cu, mir);
buzbeebff24652012-05-06 16:22:05 -070079 }
Bill Buzbeea114add2012-05-03 15:00:40 -070080 }
Bill Buzbeea114add2012-05-03 15:00:40 -070081 switch (opcode) {
82 case Instruction::NOP:
83 break;
buzbeee3acd072012-02-25 17:03:10 -080084
Ian Rogers474b6da2012-09-25 00:20:38 -070085 case Instruction::MOVE_EXCEPTION:
buzbee02031b12012-11-23 09:41:35 -080086 cg->GenMoveException(cu, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -070087 break;
jeffhao1eab9582013-01-22 13:33:52 -080088
Bill Buzbeea114add2012-05-03 15:00:40 -070089 case Instruction::RETURN_VOID:
Ian Rogersfffdb022013-01-04 15:14:08 -080090 if (((cu->access_flags & kAccConstructor) != 0) &&
91 cu->compiler->RequiresConstructorBarrier(Thread::Current(), cu->dex_file,
92 cu->class_def_idx)) {
93 cg->GenMemBarrier(cu, kStoreStore);
94 }
buzbeefa57c472012-11-21 12:06:18 -080095 if (!(cu->attrs & METHOD_IS_LEAF)) {
buzbee02031b12012-11-23 09:41:35 -080096 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -070097 }
98 break;
99
100 case Instruction::RETURN:
101 case Instruction::RETURN_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -0800102 if (!(cu->attrs & METHOD_IS_LEAF)) {
buzbee02031b12012-11-23 09:41:35 -0800103 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700104 }
buzbee02031b12012-11-23 09:41:35 -0800105 cg->StoreValue(cu, GetReturn(cu, cu->shorty[0] == 'F'), rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 break;
107
108 case Instruction::RETURN_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800109 if (!(cu->attrs & METHOD_IS_LEAF)) {
buzbee02031b12012-11-23 09:41:35 -0800110 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 }
buzbee02031b12012-11-23 09:41:35 -0800112 cg->StoreValueWide(cu, GetReturnWide(cu,
buzbeefa57c472012-11-21 12:06:18 -0800113 cu->shorty[0] == 'D'), rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 break;
115
116 case Instruction::MOVE_RESULT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800117 if (opt_flags & MIR_INLINED)
buzbee02031b12012-11-23 09:41:35 -0800118 break; // Nop - combined w/ previous invoke.
119 cg->StoreValueWide(cu, rl_dest, GetReturnWide(cu, rl_dest.fp));
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 break;
121
122 case Instruction::MOVE_RESULT:
123 case Instruction::MOVE_RESULT_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -0800124 if (opt_flags & MIR_INLINED)
buzbee02031b12012-11-23 09:41:35 -0800125 break; // Nop - combined w/ previous invoke.
126 cg->StoreValue(cu, rl_dest, GetReturn(cu, rl_dest.fp));
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 break;
128
129 case Instruction::MOVE:
130 case Instruction::MOVE_OBJECT:
131 case Instruction::MOVE_16:
132 case Instruction::MOVE_OBJECT_16:
133 case Instruction::MOVE_FROM16:
134 case Instruction::MOVE_OBJECT_FROM16:
buzbee02031b12012-11-23 09:41:35 -0800135 cg->StoreValue(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 break;
137
138 case Instruction::MOVE_WIDE:
139 case Instruction::MOVE_WIDE_16:
140 case Instruction::MOVE_WIDE_FROM16:
buzbee02031b12012-11-23 09:41:35 -0800141 cg->StoreValueWide(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 break;
143
144 case Instruction::CONST:
145 case Instruction::CONST_4:
146 case Instruction::CONST_16:
buzbeefa57c472012-11-21 12:06:18 -0800147 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800148 cg->LoadConstantNoClobber(cu, rl_result.low_reg, vB);
149 cg->StoreValue(cu, rl_dest, rl_result);
buzbee7da142f2012-11-29 16:33:42 -0800150 if (vB == 0) {
151 cg->Workaround7250540(cu, rl_dest, rl_result.low_reg);
152 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 break;
154
155 case Instruction::CONST_HIGH16:
buzbeefa57c472012-11-21 12:06:18 -0800156 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800157 cg->LoadConstantNoClobber(cu, rl_result.low_reg, vB << 16);
158 cg->StoreValue(cu, rl_dest, rl_result);
buzbee7da142f2012-11-29 16:33:42 -0800159 if (vB == 0) {
160 cg->Workaround7250540(cu, rl_dest, rl_result.low_reg);
161 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 break;
163
164 case Instruction::CONST_WIDE_16:
165 case Instruction::CONST_WIDE_32:
buzbeefa57c472012-11-21 12:06:18 -0800166 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800167 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg, vB,
buzbee408ad162012-06-06 16:45:18 -0700168 (vB & 0x80000000) ? -1 : 0);
buzbee02031b12012-11-23 09:41:35 -0800169 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 break;
171
172 case Instruction::CONST_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800173 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800174 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 mir->dalvikInsn.vB_wide & 0xffffffff,
176 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee02031b12012-11-23 09:41:35 -0800177 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 break;
179
180 case Instruction::CONST_WIDE_HIGH16:
buzbeefa57c472012-11-21 12:06:18 -0800181 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800182 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg,
buzbee408ad162012-06-06 16:45:18 -0700183 0, vB << 16);
buzbee02031b12012-11-23 09:41:35 -0800184 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 break;
186
187 case Instruction::MONITOR_ENTER:
buzbee02031b12012-11-23 09:41:35 -0800188 cg->GenMonitorEnter(cu, opt_flags, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 break;
190
191 case Instruction::MONITOR_EXIT:
buzbee02031b12012-11-23 09:41:35 -0800192 cg->GenMonitorExit(cu, opt_flags, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700193 break;
194
195 case Instruction::CHECK_CAST:
buzbee02031b12012-11-23 09:41:35 -0800196 cg->GenCheckCast(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 break;
198
199 case Instruction::INSTANCE_OF:
buzbee02031b12012-11-23 09:41:35 -0800200 cg->GenInstanceof(cu, vC, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 break;
202
203 case Instruction::NEW_INSTANCE:
buzbee02031b12012-11-23 09:41:35 -0800204 cg->GenNewInstance(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 break;
206
207 case Instruction::THROW:
buzbee02031b12012-11-23 09:41:35 -0800208 cg->GenThrow(cu, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 break;
210
Bill Buzbeea114add2012-05-03 15:00:40 -0700211 case Instruction::ARRAY_LENGTH:
buzbeefa57c472012-11-21 12:06:18 -0800212 int len_offset;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 len_offset = mirror::Array::LengthOffset().Int32Value();
buzbee02031b12012-11-23 09:41:35 -0800214 rl_src[0] = cg->LoadValue(cu, rl_src[0], kCoreReg);
215 cg->GenNullCheck(cu, rl_src[0].s_reg_low, rl_src[0].low_reg, opt_flags);
buzbeefa57c472012-11-21 12:06:18 -0800216 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
buzbee02031b12012-11-23 09:41:35 -0800217 cg->LoadWordDisp(cu, rl_src[0].low_reg, len_offset, rl_result.low_reg);
218 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700219 break;
220
221 case Instruction::CONST_STRING:
222 case Instruction::CONST_STRING_JUMBO:
buzbee02031b12012-11-23 09:41:35 -0800223 cg->GenConstString(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 break;
225
226 case Instruction::CONST_CLASS:
buzbee02031b12012-11-23 09:41:35 -0800227 cg->GenConstClass(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 break;
229
230 case Instruction::FILL_ARRAY_DATA:
buzbee02031b12012-11-23 09:41:35 -0800231 cg->GenFillArrayData(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 break;
233
234 case Instruction::FILLED_NEW_ARRAY:
buzbee02031b12012-11-23 09:41:35 -0800235 cg->GenFilledNewArray(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic,
buzbee3b3dbdd2012-06-13 13:39:34 -0700236 false /* not range */));
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 break;
238
239 case Instruction::FILLED_NEW_ARRAY_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800240 cg->GenFilledNewArray(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic,
buzbee3b3dbdd2012-06-13 13:39:34 -0700241 true /* range */));
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 break;
243
244 case Instruction::NEW_ARRAY:
buzbee02031b12012-11-23 09:41:35 -0800245 cg->GenNewArray(cu, vC, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 break;
247
248 case Instruction::GOTO:
249 case Instruction::GOTO_16:
250 case Instruction::GOTO_32:
buzbeefa57c472012-11-21 12:06:18 -0800251 if (bb->taken->start_offset <= mir->offset) {
buzbee02031b12012-11-23 09:41:35 -0800252 cg->GenSuspendTestAndBranch(cu, opt_flags, &label_list[bb->taken->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 } else {
buzbee02031b12012-11-23 09:41:35 -0800254 cg->OpUnconditionalBranch(cu, &label_list[bb->taken->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 }
256 break;
257
258 case Instruction::PACKED_SWITCH:
buzbee02031b12012-11-23 09:41:35 -0800259 cg->GenPackedSwitch(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 break;
261
262 case Instruction::SPARSE_SWITCH:
buzbee02031b12012-11-23 09:41:35 -0800263 cg->GenSparseSwitch(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 break;
265
266 case Instruction::CMPL_FLOAT:
267 case Instruction::CMPG_FLOAT:
268 case Instruction::CMPL_DOUBLE:
269 case Instruction::CMPG_DOUBLE:
buzbee02031b12012-11-23 09:41:35 -0800270 res = cg->GenCmpFP(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 break;
272
273 case Instruction::CMP_LONG:
buzbee02031b12012-11-23 09:41:35 -0800274 cg->GenCmpLong(cu, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 break;
276
277 case Instruction::IF_EQ:
278 case Instruction::IF_NE:
279 case Instruction::IF_LT:
280 case Instruction::IF_GE:
281 case Instruction::IF_GT:
282 case Instruction::IF_LE: {
buzbeefa57c472012-11-21 12:06:18 -0800283 LIR* taken = &label_list[bb->taken->id];
284 LIR* fall_through = &label_list[bb->fall_through->id];
285 bool backward_branch;
286 backward_branch = (bb->taken->start_offset <= mir->offset);
buzbeee6285f92012-12-06 15:57:46 -0800287 // Result known at compile time?
288 if (rl_src[0].is_const && rl_src[1].is_const) {
289 bool is_taken = EvaluateBranch(opcode, cu->constant_values[rl_src[0].orig_sreg],
290 cu->constant_values[rl_src[1].orig_sreg]);
291 if (is_taken && backward_branch) {
292 cg->GenSuspendTest(cu, opt_flags);
293 }
294 int id = is_taken ? bb->taken->id : bb->fall_through->id;
295 cg->OpUnconditionalBranch(cu, &label_list[id]);
296 } else {
297 if (backward_branch) {
298 cg->GenSuspendTest(cu, opt_flags);
299 }
300 cg->GenCompareAndBranch(cu, opcode, rl_src[0], rl_src[1], taken,
301 fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 break;
304 }
305
306 case Instruction::IF_EQZ:
307 case Instruction::IF_NEZ:
308 case Instruction::IF_LTZ:
309 case Instruction::IF_GEZ:
310 case Instruction::IF_GTZ:
311 case Instruction::IF_LEZ: {
buzbeefa57c472012-11-21 12:06:18 -0800312 LIR* taken = &label_list[bb->taken->id];
313 LIR* fall_through = &label_list[bb->fall_through->id];
314 bool backward_branch;
315 backward_branch = (bb->taken->start_offset <= mir->offset);
buzbeee6285f92012-12-06 15:57:46 -0800316 // Result known at compile time?
317 if (rl_src[0].is_const) {
318 bool is_taken = EvaluateBranch(opcode, cu->constant_values[rl_src[0].orig_sreg], 0);
319 if (is_taken && backward_branch) {
320 cg->GenSuspendTest(cu, opt_flags);
321 }
322 int id = is_taken ? bb->taken->id : bb->fall_through->id;
323 cg->OpUnconditionalBranch(cu, &label_list[id]);
324 } else {
325 if (backward_branch) {
326 cg->GenSuspendTest(cu, opt_flags);
327 }
328 cg->GenCompareZeroAndBranch(cu, opcode, rl_src[0], taken, fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 break;
331 }
332
333 case Instruction::AGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800334 cg->GenArrayGet(cu, opt_flags, kLong, rl_src[0], rl_src[1], rl_dest, 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700335 break;
336 case Instruction::AGET:
337 case Instruction::AGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800338 cg->GenArrayGet(cu, opt_flags, kWord, rl_src[0], rl_src[1], rl_dest, 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 break;
340 case Instruction::AGET_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800341 cg->GenArrayGet(cu, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 break;
343 case Instruction::AGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800344 cg->GenArrayGet(cu, opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 break;
346 case Instruction::AGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800347 cg->GenArrayGet(cu, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 break;
349 case Instruction::AGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800350 cg->GenArrayGet(cu, opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 break;
352 case Instruction::APUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800353 cg->GenArrayPut(cu, opt_flags, kLong, rl_src[1], rl_src[2], rl_src[0], 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 break;
355 case Instruction::APUT:
buzbee02031b12012-11-23 09:41:35 -0800356 cg->GenArrayPut(cu, opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700357 break;
358 case Instruction::APUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800359 cg->GenArrayObjPut(cu, opt_flags, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 break;
361 case Instruction::APUT_SHORT:
362 case Instruction::APUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800363 cg->GenArrayPut(cu, opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 break;
365 case Instruction::APUT_BYTE:
366 case Instruction::APUT_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800367 cg->GenArrayPut(cu, opt_flags, kUnsignedByte, rl_src[1], rl_src[2],
buzbeefa57c472012-11-21 12:06:18 -0800368 rl_src[0], 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 break;
370
371 case Instruction::IGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800372 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 break;
374
375 case Instruction::IGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800376 cg->GenIGet(cu, vC, opt_flags, kLong, rl_dest, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 break;
378
379 case Instruction::IGET:
buzbee02031b12012-11-23 09:41:35 -0800380 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 break;
382
383 case Instruction::IGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800384 cg->GenIGet(cu, vC, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 break;
386
387 case Instruction::IGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800388 cg->GenIGet(cu, vC, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 break;
390
391 case Instruction::IGET_BOOLEAN:
392 case Instruction::IGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800393 cg->GenIGet(cu, vC, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700394 break;
395
396 case Instruction::IPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800397 cg->GenIPut(cu, vC, opt_flags, kLong, rl_src[0], rl_src[1], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 break;
399
400 case Instruction::IPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800401 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 break;
403
404 case Instruction::IPUT:
buzbee02031b12012-11-23 09:41:35 -0800405 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700406 break;
407
408 case Instruction::IPUT_BOOLEAN:
409 case Instruction::IPUT_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800410 cg->GenIPut(cu, vC, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 break;
412
413 case Instruction::IPUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800414 cg->GenIPut(cu, vC, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 break;
416
417 case Instruction::IPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800418 cg->GenIPut(cu, vC, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 break;
420
421 case Instruction::SGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800422 cg->GenSget(cu, vB, rl_dest, false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 break;
424 case Instruction::SGET:
425 case Instruction::SGET_BOOLEAN:
426 case Instruction::SGET_BYTE:
427 case Instruction::SGET_CHAR:
428 case Instruction::SGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800429 cg->GenSget(cu, vB, rl_dest, false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 break;
431
432 case Instruction::SGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800433 cg->GenSget(cu, vB, rl_dest, true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 break;
435
436 case Instruction::SPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800437 cg->GenSput(cu, vB, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 break;
439
440 case Instruction::SPUT:
441 case Instruction::SPUT_BOOLEAN:
442 case Instruction::SPUT_BYTE:
443 case Instruction::SPUT_CHAR:
444 case Instruction::SPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800445 cg->GenSput(cu, vB, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700446 break;
447
448 case Instruction::SPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800449 cg->GenSput(cu, vB, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 break;
451
452 case Instruction::INVOKE_STATIC_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800453 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 break;
455 case Instruction::INVOKE_STATIC:
buzbee02031b12012-11-23 09:41:35 -0800456 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 break;
458
459 case Instruction::INVOKE_DIRECT:
buzbee02031b12012-11-23 09:41:35 -0800460 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 break;
462 case Instruction::INVOKE_DIRECT_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800463 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700464 break;
465
466 case Instruction::INVOKE_VIRTUAL:
buzbee02031b12012-11-23 09:41:35 -0800467 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700468 break;
469 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800470 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 break;
472
473 case Instruction::INVOKE_SUPER:
buzbee02031b12012-11-23 09:41:35 -0800474 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 break;
476 case Instruction::INVOKE_SUPER_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800477 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 break;
479
480 case Instruction::INVOKE_INTERFACE:
buzbee02031b12012-11-23 09:41:35 -0800481 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 break;
483 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800484 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 break;
486
487 case Instruction::NEG_INT:
488 case Instruction::NOT_INT:
buzbee02031b12012-11-23 09:41:35 -0800489 res = cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700490 break;
491
492 case Instruction::NEG_LONG:
493 case Instruction::NOT_LONG:
buzbee02031b12012-11-23 09:41:35 -0800494 res = cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 break;
496
497 case Instruction::NEG_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800498 res = cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 break;
500
501 case Instruction::NEG_DOUBLE:
buzbee02031b12012-11-23 09:41:35 -0800502 res = cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 break;
504
505 case Instruction::INT_TO_LONG:
buzbee02031b12012-11-23 09:41:35 -0800506 cg->GenIntToLong(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700507 break;
508
509 case Instruction::LONG_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -0800510 rl_src[0] = UpdateLocWide(cu, rl_src[0]);
511 rl_src[0] = WideToNarrow(cu, rl_src[0]);
buzbee02031b12012-11-23 09:41:35 -0800512 cg->StoreValue(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700513 break;
514
515 case Instruction::INT_TO_BYTE:
516 case Instruction::INT_TO_SHORT:
517 case Instruction::INT_TO_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800518 cg->GenIntNarrowing(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700519 break;
520
521 case Instruction::INT_TO_FLOAT:
522 case Instruction::INT_TO_DOUBLE:
523 case Instruction::LONG_TO_FLOAT:
524 case Instruction::LONG_TO_DOUBLE:
525 case Instruction::FLOAT_TO_INT:
526 case Instruction::FLOAT_TO_LONG:
527 case Instruction::FLOAT_TO_DOUBLE:
528 case Instruction::DOUBLE_TO_INT:
529 case Instruction::DOUBLE_TO_LONG:
530 case Instruction::DOUBLE_TO_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800531 cg->GenConversion(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700532 break;
533
buzbeee6285f92012-12-06 15:57:46 -0800534
Bill Buzbeea114add2012-05-03 15:00:40 -0700535 case Instruction::ADD_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700536 case Instruction::ADD_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800537 case Instruction::MUL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 case Instruction::MUL_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800539 case Instruction::AND_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700540 case Instruction::AND_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800541 case Instruction::OR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700542 case Instruction::OR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800543 case Instruction::XOR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 case Instruction::XOR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800545 if (rl_src[0].is_const &&
546 cu->cg->InexpensiveConstant(0, cu->constant_values[rl_src[0].orig_sreg])) {
547 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[1],
548 cu->constant_values[rl_src[0].orig_sreg]);
549 } else if (rl_src[1].is_const &&
550 cu->cg->InexpensiveConstant(0, cu->constant_values[rl_src[1].orig_sreg])) {
551 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0],
552 cu->constant_values[rl_src[1].orig_sreg]);
553 } else {
554 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
555 }
556 break;
557
558 case Instruction::SUB_INT:
559 case Instruction::SUB_INT_2ADDR:
560 case Instruction::DIV_INT:
561 case Instruction::DIV_INT_2ADDR:
562 case Instruction::REM_INT:
563 case Instruction::REM_INT_2ADDR:
564 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 case Instruction::SHL_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800566 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700567 case Instruction::SHR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800568 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700569 case Instruction::USHR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800570 if (rl_src[1].is_const &&
571 cu->cg->InexpensiveConstant(0, cu->constant_values[rl_src[1].orig_sreg])) {
572 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0],
573 cu->constant_values[rl_src[1].orig_sreg]);
574 } else {
575 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
576 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 break;
578
579 case Instruction::ADD_LONG:
580 case Instruction::SUB_LONG:
581 case Instruction::MUL_LONG:
582 case Instruction::DIV_LONG:
583 case Instruction::REM_LONG:
584 case Instruction::AND_LONG:
585 case Instruction::OR_LONG:
586 case Instruction::XOR_LONG:
587 case Instruction::ADD_LONG_2ADDR:
588 case Instruction::SUB_LONG_2ADDR:
589 case Instruction::MUL_LONG_2ADDR:
590 case Instruction::DIV_LONG_2ADDR:
591 case Instruction::REM_LONG_2ADDR:
592 case Instruction::AND_LONG_2ADDR:
593 case Instruction::OR_LONG_2ADDR:
594 case Instruction::XOR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800595 cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 break;
597
598 case Instruction::SHL_LONG:
599 case Instruction::SHR_LONG:
600 case Instruction::USHR_LONG:
601 case Instruction::SHL_LONG_2ADDR:
602 case Instruction::SHR_LONG_2ADDR:
603 case Instruction::USHR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800604 cg->GenShiftOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700605 break;
606
607 case Instruction::ADD_FLOAT:
608 case Instruction::SUB_FLOAT:
609 case Instruction::MUL_FLOAT:
610 case Instruction::DIV_FLOAT:
611 case Instruction::REM_FLOAT:
612 case Instruction::ADD_FLOAT_2ADDR:
613 case Instruction::SUB_FLOAT_2ADDR:
614 case Instruction::MUL_FLOAT_2ADDR:
615 case Instruction::DIV_FLOAT_2ADDR:
616 case Instruction::REM_FLOAT_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800617 cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 break;
619
620 case Instruction::ADD_DOUBLE:
621 case Instruction::SUB_DOUBLE:
622 case Instruction::MUL_DOUBLE:
623 case Instruction::DIV_DOUBLE:
624 case Instruction::REM_DOUBLE:
625 case Instruction::ADD_DOUBLE_2ADDR:
626 case Instruction::SUB_DOUBLE_2ADDR:
627 case Instruction::MUL_DOUBLE_2ADDR:
628 case Instruction::DIV_DOUBLE_2ADDR:
629 case Instruction::REM_DOUBLE_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800630 cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 break;
632
633 case Instruction::RSUB_INT:
634 case Instruction::ADD_INT_LIT16:
635 case Instruction::MUL_INT_LIT16:
636 case Instruction::DIV_INT_LIT16:
637 case Instruction::REM_INT_LIT16:
638 case Instruction::AND_INT_LIT16:
639 case Instruction::OR_INT_LIT16:
640 case Instruction::XOR_INT_LIT16:
641 case Instruction::ADD_INT_LIT8:
642 case Instruction::RSUB_INT_LIT8:
643 case Instruction::MUL_INT_LIT8:
644 case Instruction::DIV_INT_LIT8:
645 case Instruction::REM_INT_LIT8:
646 case Instruction::AND_INT_LIT8:
647 case Instruction::OR_INT_LIT8:
648 case Instruction::XOR_INT_LIT8:
649 case Instruction::SHL_INT_LIT8:
650 case Instruction::SHR_INT_LIT8:
651 case Instruction::USHR_INT_LIT8:
buzbee02031b12012-11-23 09:41:35 -0800652 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0], vC);
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 break;
654
655 default:
656 res = true;
657 }
658 return res;
buzbeee3acd072012-02-25 17:03:10 -0800659}
660
buzbeea169e1d2012-12-05 14:26:44 -0800661// Process extended MIR instructions
buzbeefa57c472012-11-21 12:06:18 -0800662static void HandleExtendedMethodMIR(CompilationUnit* cu, BasicBlock* bb, MIR* mir)
buzbeee3acd072012-02-25 17:03:10 -0800663{
buzbee02031b12012-11-23 09:41:35 -0800664 Codegen* cg = cu->cg.get();
buzbeecbd6d442012-11-17 14:11:25 -0800665 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 case kMirOpCopy: {
buzbeefa57c472012-11-21 12:06:18 -0800667 RegLocation rl_src = GetSrc(cu, mir, 0);
668 RegLocation rl_dest = GetDest(cu, mir);
buzbee02031b12012-11-23 09:41:35 -0800669 cg->StoreValue(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -0700670 break;
671 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 case kMirOpFusedCmplFloat:
buzbee02031b12012-11-23 09:41:35 -0800673 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 break;
675 case kMirOpFusedCmpgFloat:
buzbee02031b12012-11-23 09:41:35 -0800676 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700677 break;
678 case kMirOpFusedCmplDouble:
buzbee02031b12012-11-23 09:41:35 -0800679 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 break;
681 case kMirOpFusedCmpgDouble:
buzbee02031b12012-11-23 09:41:35 -0800682 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 break;
684 case kMirOpFusedCmpLong:
buzbee02031b12012-11-23 09:41:35 -0800685 cg->GenFusedLongCmpBranch(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700686 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700687 default:
688 break;
689 }
buzbeee3acd072012-02-25 17:03:10 -0800690}
691
buzbee02031b12012-11-23 09:41:35 -0800692// Handle the content in each basic block.
buzbeefa57c472012-11-21 12:06:18 -0800693static bool MethodBlockCodeGen(CompilationUnit* cu, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800694{
buzbeefa57c472012-11-21 12:06:18 -0800695 if (bb->block_type == kDead) return false;
buzbee02031b12012-11-23 09:41:35 -0800696 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800697 cu->current_dalvik_offset = bb->start_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 MIR* mir;
buzbeefa57c472012-11-21 12:06:18 -0800699 LIR* label_list = cu->block_label_list;
700 int block_id = bb->id;
buzbeee3acd072012-02-25 17:03:10 -0800701
buzbeefa57c472012-11-21 12:06:18 -0800702 cu->cur_block = bb;
703 label_list[block_id].operands[0] = bb->start_offset;
buzbeee3acd072012-02-25 17:03:10 -0800704
buzbee02031b12012-11-23 09:41:35 -0800705 // Insert the block label.
buzbeefa57c472012-11-21 12:06:18 -0800706 label_list[block_id].opcode = kPseudoNormalBlockLabel;
707 AppendLIR(cu, &label_list[block_id]);
buzbeee3acd072012-02-25 17:03:10 -0800708
buzbeefa57c472012-11-21 12:06:18 -0800709 LIR* head_lir = NULL;
buzbee8320f382012-09-11 16:29:42 -0700710
buzbee02031b12012-11-23 09:41:35 -0800711 // If this is a catch block, export the start address.
buzbeefa57c472012-11-21 12:06:18 -0800712 if (bb->catch_entry) {
713 head_lir = NewLIR0(cu, kPseudoExportedPC);
buzbee8320f382012-09-11 16:29:42 -0700714 }
715
buzbee02031b12012-11-23 09:41:35 -0800716 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800717 ResetRegPool(cu);
718 ResetDefTracking(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700719
buzbeefa57c472012-11-21 12:06:18 -0800720 ClobberAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700721
buzbeefa57c472012-11-21 12:06:18 -0800722 if (bb->block_type == kEntryBlock) {
723 int start_vreg = cu->num_dalvik_registers - cu->num_ins;
buzbee02031b12012-11-23 09:41:35 -0800724 cg->GenEntrySequence(cu, &cu->reg_location[start_vreg],
725 cu->reg_location[cu->method_sreg]);
buzbeefa57c472012-11-21 12:06:18 -0800726 } else if (bb->block_type == kExitBlock) {
buzbee02031b12012-11-23 09:41:35 -0800727 cg->GenExitSequence(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 }
729
buzbee28c9a832012-11-21 15:39:13 -0800730 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeefa57c472012-11-21 12:06:18 -0800731 ResetRegPool(cu);
732 if (cu->disable_opt & (1 << kTrackLiveTemps)) {
733 ClobberAllRegs(cu);
buzbeee1965672012-03-11 18:39:19 -0700734 }
735
buzbeefa57c472012-11-21 12:06:18 -0800736 if (cu->disable_opt & (1 << kSuppressLoads)) {
737 ResetDefTracking(cu);
buzbeee3acd072012-02-25 17:03:10 -0800738 }
739
buzbee3d661942012-03-14 17:37:27 -0700740#ifndef NDEBUG
buzbee02031b12012-11-23 09:41:35 -0800741 // Reset temp tracking sanity check.
buzbeefa57c472012-11-21 12:06:18 -0800742 cu->live_sreg = INVALID_SREG;
buzbee3d661942012-03-14 17:37:27 -0700743#endif
744
buzbeefa57c472012-11-21 12:06:18 -0800745 cu->current_dalvik_offset = mir->offset;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700746 int opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800747 LIR* boundary_lir;
buzbeee3acd072012-02-25 17:03:10 -0800748
buzbee02031b12012-11-23 09:41:35 -0800749 // Mark the beginning of a Dalvik instruction for line tracking.
buzbeefa57c472012-11-21 12:06:18 -0800750 char* inst_str = cu->verbose ?
buzbeea169e1d2012-12-05 14:26:44 -0800751 GetDalvikDisassembly(cu, mir) : NULL;
buzbeefa57c472012-11-21 12:06:18 -0800752 boundary_lir = MarkBoundary(cu, mir->offset, inst_str);
buzbee02031b12012-11-23 09:41:35 -0800753 // Remember the first LIR for this block.
buzbeefa57c472012-11-21 12:06:18 -0800754 if (head_lir == NULL) {
755 head_lir = boundary_lir;
buzbee02031b12012-11-23 09:41:35 -0800756 // Set the first boundary_lir as a scheduling barrier.
buzbeefa57c472012-11-21 12:06:18 -0800757 head_lir->def_mask = ENCODE_ALL;
buzbeee3acd072012-02-25 17:03:10 -0800758 }
759
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700760 if (opcode == kMirOpCheck) {
761 // Combine check and work halves of throwing instruction.
buzbeefa57c472012-11-21 12:06:18 -0800762 MIR* work_half = mir->meta.throw_insn;
763 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
764 opcode = work_half->dalvikInsn.opcode;
765 SSARepresentation* ssa_rep = work_half->ssa_rep;
766 work_half->ssa_rep = mir->ssa_rep;
767 mir->ssa_rep = ssa_rep;
buzbeea169e1d2012-12-05 14:26:44 -0800768 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700769 }
770
771 if (opcode >= kMirOpFirst) {
buzbeefa57c472012-11-21 12:06:18 -0800772 HandleExtendedMethodMIR(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 continue;
774 }
775
buzbeefa57c472012-11-21 12:06:18 -0800776 bool not_handled = CompileDalvikInstruction(cu, mir, bb, label_list);
777 if (not_handled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700778 LOG(FATAL) << StringPrintf("%#06x: Opcode %#x (%s)",
779 mir->offset, opcode,
780 Instruction::Name(mir->dalvikInsn.opcode));
Bill Buzbeea114add2012-05-03 15:00:40 -0700781 }
782 }
783
buzbeefa57c472012-11-21 12:06:18 -0800784 if (head_lir) {
buzbee02031b12012-11-23 09:41:35 -0800785 // Eliminate redundant loads/stores and delay stores into later slots.
buzbeefa57c472012-11-21 12:06:18 -0800786 ApplyLocalOptimizations(cu, head_lir, cu->last_lir_insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700787
buzbee02031b12012-11-23 09:41:35 -0800788 // Generate an unconditional branch to the fallthrough block.
buzbeefa57c472012-11-21 12:06:18 -0800789 if (bb->fall_through) {
buzbee02031b12012-11-23 09:41:35 -0800790 cg->OpUnconditionalBranch(cu, &label_list[bb->fall_through->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700791 }
792 }
793 return false;
buzbeee3acd072012-02-25 17:03:10 -0800794}
795
buzbeefa57c472012-11-21 12:06:18 -0800796void SpecialMIR2LIR(CompilationUnit* cu, SpecialCaseHandler special_case)
buzbee16da88c2012-03-20 10:38:17 -0700797{
buzbee02031b12012-11-23 09:41:35 -0800798 Codegen* cg = cu->cg.get();
799 // Find the first DalvikByteCode block.
buzbeefa57c472012-11-21 12:06:18 -0800800 int num_reachable_blocks = cu->num_reachable_blocks;
801 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700802 BasicBlock*bb = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800803 for (int idx = 0; idx < num_reachable_blocks; idx++) {
804 int dfs_index = cu->dfs_order.elem_list[idx];
805 bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, dfs_index));
806 if (bb->block_type == kDalvikByteCode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 break;
buzbee16da88c2012-03-20 10:38:17 -0700808 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700809 }
810 if (bb == NULL) {
811 return;
812 }
buzbeefa57c472012-11-21 12:06:18 -0800813 DCHECK_EQ(bb->start_offset, 0);
814 DCHECK(bb->first_mir_insn != NULL);
buzbee16da88c2012-03-20 10:38:17 -0700815
buzbee02031b12012-11-23 09:41:35 -0800816 // Get the first instruction.
buzbeefa57c472012-11-21 12:06:18 -0800817 MIR* mir = bb->first_mir_insn;
buzbee16da88c2012-03-20 10:38:17 -0700818
buzbee02031b12012-11-23 09:41:35 -0800819 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800820 ResetRegPool(cu);
821 ResetDefTracking(cu);
822 ClobberAllRegs(cu);
buzbee16da88c2012-03-20 10:38:17 -0700823
buzbee02031b12012-11-23 09:41:35 -0800824 cg->GenSpecialCase(cu, bb, mir, special_case);
buzbee16da88c2012-03-20 10:38:17 -0700825}
826
buzbeefa57c472012-11-21 12:06:18 -0800827void MethodMIR2LIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800828{
buzbee02031b12012-11-23 09:41:35 -0800829 Codegen* cg = cu->cg.get();
830 // Hold the labels of each block.
buzbeefa57c472012-11-21 12:06:18 -0800831 cu->block_label_list =
832 static_cast<LIR*>(NewMem(cu, sizeof(LIR) * cu->num_blocks, true, kAllocLIR));
buzbeee3acd072012-02-25 17:03:10 -0800833
buzbeefa57c472012-11-21 12:06:18 -0800834 DataFlowAnalysisDispatcher(cu, MethodBlockCodeGen,
Bill Buzbeea114add2012-05-03 15:00:40 -0700835 kPreOrderDFSTraversal, false /* Iterative */);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700836
buzbee02031b12012-11-23 09:41:35 -0800837 cg->HandleSuspendLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800838
buzbee02031b12012-11-23 09:41:35 -0800839 cg->HandleThrowLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800840
buzbee02031b12012-11-23 09:41:35 -0800841 cg->HandleIntrinsicLaunchPads(cu);
buzbeefc9e6fa2012-03-23 15:14:29 -0700842
buzbeefa57c472012-11-21 12:06:18 -0800843 if (!(cu->disable_opt & (1 << kSafeOptimizations))) {
844 RemoveRedundantBranches(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 }
buzbeee3acd072012-02-25 17:03:10 -0800846}
847
buzbeee3acd072012-02-25 17:03:10 -0800848} // namespace art