blob: acdeafe32c10981176f479e435d361065c8d56e7 [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;
Bill Buzbeea114add2012-05-03 15:00:40 -070088 case Instruction::RETURN_VOID:
buzbeefa57c472012-11-21 12:06:18 -080089 if (!(cu->attrs & METHOD_IS_LEAF)) {
buzbee02031b12012-11-23 09:41:35 -080090 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -070091 }
92 break;
93
94 case Instruction::RETURN:
95 case Instruction::RETURN_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -080096 if (!(cu->attrs & METHOD_IS_LEAF)) {
buzbee02031b12012-11-23 09:41:35 -080097 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -070098 }
buzbee02031b12012-11-23 09:41:35 -080099 cg->StoreValue(cu, GetReturn(cu, cu->shorty[0] == 'F'), rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 break;
101
102 case Instruction::RETURN_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800103 if (!(cu->attrs & METHOD_IS_LEAF)) {
buzbee02031b12012-11-23 09:41:35 -0800104 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 }
buzbee02031b12012-11-23 09:41:35 -0800106 cg->StoreValueWide(cu, GetReturnWide(cu,
buzbeefa57c472012-11-21 12:06:18 -0800107 cu->shorty[0] == 'D'), rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 break;
109
110 case Instruction::MOVE_RESULT_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800111 if (opt_flags & MIR_INLINED)
buzbee02031b12012-11-23 09:41:35 -0800112 break; // Nop - combined w/ previous invoke.
113 cg->StoreValueWide(cu, rl_dest, GetReturnWide(cu, rl_dest.fp));
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 break;
115
116 case Instruction::MOVE_RESULT:
117 case Instruction::MOVE_RESULT_OBJECT:
buzbeefa57c472012-11-21 12:06:18 -0800118 if (opt_flags & MIR_INLINED)
buzbee02031b12012-11-23 09:41:35 -0800119 break; // Nop - combined w/ previous invoke.
120 cg->StoreValue(cu, rl_dest, GetReturn(cu, rl_dest.fp));
Bill Buzbeea114add2012-05-03 15:00:40 -0700121 break;
122
123 case Instruction::MOVE:
124 case Instruction::MOVE_OBJECT:
125 case Instruction::MOVE_16:
126 case Instruction::MOVE_OBJECT_16:
127 case Instruction::MOVE_FROM16:
128 case Instruction::MOVE_OBJECT_FROM16:
buzbee02031b12012-11-23 09:41:35 -0800129 cg->StoreValue(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 break;
131
132 case Instruction::MOVE_WIDE:
133 case Instruction::MOVE_WIDE_16:
134 case Instruction::MOVE_WIDE_FROM16:
buzbee02031b12012-11-23 09:41:35 -0800135 cg->StoreValueWide(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 break;
137
138 case Instruction::CONST:
139 case Instruction::CONST_4:
140 case Instruction::CONST_16:
buzbeefa57c472012-11-21 12:06:18 -0800141 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800142 cg->LoadConstantNoClobber(cu, rl_result.low_reg, vB);
143 cg->StoreValue(cu, rl_dest, rl_result);
buzbee7da142f2012-11-29 16:33:42 -0800144 if (vB == 0) {
145 cg->Workaround7250540(cu, rl_dest, rl_result.low_reg);
146 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 break;
148
149 case Instruction::CONST_HIGH16:
buzbeefa57c472012-11-21 12:06:18 -0800150 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800151 cg->LoadConstantNoClobber(cu, rl_result.low_reg, vB << 16);
152 cg->StoreValue(cu, rl_dest, rl_result);
buzbee7da142f2012-11-29 16:33:42 -0800153 if (vB == 0) {
154 cg->Workaround7250540(cu, rl_dest, rl_result.low_reg);
155 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 break;
157
158 case Instruction::CONST_WIDE_16:
159 case Instruction::CONST_WIDE_32:
buzbeefa57c472012-11-21 12:06:18 -0800160 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800161 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg, vB,
buzbee408ad162012-06-06 16:45:18 -0700162 (vB & 0x80000000) ? -1 : 0);
buzbee02031b12012-11-23 09:41:35 -0800163 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 break;
165
166 case Instruction::CONST_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800167 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800168 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 mir->dalvikInsn.vB_wide & 0xffffffff,
170 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee02031b12012-11-23 09:41:35 -0800171 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 break;
173
174 case Instruction::CONST_WIDE_HIGH16:
buzbeefa57c472012-11-21 12:06:18 -0800175 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800176 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg,
buzbee408ad162012-06-06 16:45:18 -0700177 0, vB << 16);
buzbee02031b12012-11-23 09:41:35 -0800178 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 break;
180
181 case Instruction::MONITOR_ENTER:
buzbee02031b12012-11-23 09:41:35 -0800182 cg->GenMonitorEnter(cu, opt_flags, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 break;
184
185 case Instruction::MONITOR_EXIT:
buzbee02031b12012-11-23 09:41:35 -0800186 cg->GenMonitorExit(cu, opt_flags, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 break;
188
189 case Instruction::CHECK_CAST:
buzbee02031b12012-11-23 09:41:35 -0800190 cg->GenCheckCast(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 break;
192
193 case Instruction::INSTANCE_OF:
buzbee02031b12012-11-23 09:41:35 -0800194 cg->GenInstanceof(cu, vC, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 break;
196
197 case Instruction::NEW_INSTANCE:
buzbee02031b12012-11-23 09:41:35 -0800198 cg->GenNewInstance(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 break;
200
201 case Instruction::THROW:
buzbee02031b12012-11-23 09:41:35 -0800202 cg->GenThrow(cu, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 break;
204
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 case Instruction::ARRAY_LENGTH:
buzbeefa57c472012-11-21 12:06:18 -0800206 int len_offset;
207 len_offset = Array::LengthOffset().Int32Value();
buzbee02031b12012-11-23 09:41:35 -0800208 rl_src[0] = cg->LoadValue(cu, rl_src[0], kCoreReg);
209 cg->GenNullCheck(cu, rl_src[0].s_reg_low, rl_src[0].low_reg, opt_flags);
buzbeefa57c472012-11-21 12:06:18 -0800210 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
buzbee02031b12012-11-23 09:41:35 -0800211 cg->LoadWordDisp(cu, rl_src[0].low_reg, len_offset, rl_result.low_reg);
212 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 break;
214
215 case Instruction::CONST_STRING:
216 case Instruction::CONST_STRING_JUMBO:
buzbee02031b12012-11-23 09:41:35 -0800217 cg->GenConstString(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700218 break;
219
220 case Instruction::CONST_CLASS:
buzbee02031b12012-11-23 09:41:35 -0800221 cg->GenConstClass(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 break;
223
224 case Instruction::FILL_ARRAY_DATA:
buzbee02031b12012-11-23 09:41:35 -0800225 cg->GenFillArrayData(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 break;
227
228 case Instruction::FILLED_NEW_ARRAY:
buzbee02031b12012-11-23 09:41:35 -0800229 cg->GenFilledNewArray(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic,
buzbee3b3dbdd2012-06-13 13:39:34 -0700230 false /* not range */));
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 break;
232
233 case Instruction::FILLED_NEW_ARRAY_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800234 cg->GenFilledNewArray(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic,
buzbee3b3dbdd2012-06-13 13:39:34 -0700235 true /* range */));
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 break;
237
238 case Instruction::NEW_ARRAY:
buzbee02031b12012-11-23 09:41:35 -0800239 cg->GenNewArray(cu, vC, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 break;
241
242 case Instruction::GOTO:
243 case Instruction::GOTO_16:
244 case Instruction::GOTO_32:
buzbeefa57c472012-11-21 12:06:18 -0800245 if (bb->taken->start_offset <= mir->offset) {
buzbee02031b12012-11-23 09:41:35 -0800246 cg->GenSuspendTestAndBranch(cu, opt_flags, &label_list[bb->taken->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 } else {
buzbee02031b12012-11-23 09:41:35 -0800248 cg->OpUnconditionalBranch(cu, &label_list[bb->taken->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 }
250 break;
251
252 case Instruction::PACKED_SWITCH:
buzbee02031b12012-11-23 09:41:35 -0800253 cg->GenPackedSwitch(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 break;
255
256 case Instruction::SPARSE_SWITCH:
buzbee02031b12012-11-23 09:41:35 -0800257 cg->GenSparseSwitch(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 break;
259
260 case Instruction::CMPL_FLOAT:
261 case Instruction::CMPG_FLOAT:
262 case Instruction::CMPL_DOUBLE:
263 case Instruction::CMPG_DOUBLE:
buzbee02031b12012-11-23 09:41:35 -0800264 res = cg->GenCmpFP(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 break;
266
267 case Instruction::CMP_LONG:
buzbee02031b12012-11-23 09:41:35 -0800268 cg->GenCmpLong(cu, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 break;
270
271 case Instruction::IF_EQ:
272 case Instruction::IF_NE:
273 case Instruction::IF_LT:
274 case Instruction::IF_GE:
275 case Instruction::IF_GT:
276 case Instruction::IF_LE: {
buzbeefa57c472012-11-21 12:06:18 -0800277 LIR* taken = &label_list[bb->taken->id];
278 LIR* fall_through = &label_list[bb->fall_through->id];
279 bool backward_branch;
280 backward_branch = (bb->taken->start_offset <= mir->offset);
buzbeee6285f92012-12-06 15:57:46 -0800281 // Result known at compile time?
282 if (rl_src[0].is_const && rl_src[1].is_const) {
283 bool is_taken = EvaluateBranch(opcode, cu->constant_values[rl_src[0].orig_sreg],
284 cu->constant_values[rl_src[1].orig_sreg]);
285 if (is_taken && backward_branch) {
286 cg->GenSuspendTest(cu, opt_flags);
287 }
288 int id = is_taken ? bb->taken->id : bb->fall_through->id;
289 cg->OpUnconditionalBranch(cu, &label_list[id]);
290 } else {
291 if (backward_branch) {
292 cg->GenSuspendTest(cu, opt_flags);
293 }
294 cg->GenCompareAndBranch(cu, opcode, rl_src[0], rl_src[1], taken,
295 fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 break;
298 }
299
300 case Instruction::IF_EQZ:
301 case Instruction::IF_NEZ:
302 case Instruction::IF_LTZ:
303 case Instruction::IF_GEZ:
304 case Instruction::IF_GTZ:
305 case Instruction::IF_LEZ: {
buzbeefa57c472012-11-21 12:06:18 -0800306 LIR* taken = &label_list[bb->taken->id];
307 LIR* fall_through = &label_list[bb->fall_through->id];
308 bool backward_branch;
309 backward_branch = (bb->taken->start_offset <= mir->offset);
buzbeee6285f92012-12-06 15:57:46 -0800310 // Result known at compile time?
311 if (rl_src[0].is_const) {
312 bool is_taken = EvaluateBranch(opcode, cu->constant_values[rl_src[0].orig_sreg], 0);
313 if (is_taken && backward_branch) {
314 cg->GenSuspendTest(cu, opt_flags);
315 }
316 int id = is_taken ? bb->taken->id : bb->fall_through->id;
317 cg->OpUnconditionalBranch(cu, &label_list[id]);
318 } else {
319 if (backward_branch) {
320 cg->GenSuspendTest(cu, opt_flags);
321 }
322 cg->GenCompareZeroAndBranch(cu, opcode, rl_src[0], taken, fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700323 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 break;
325 }
326
327 case Instruction::AGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800328 cg->GenArrayGet(cu, opt_flags, kLong, rl_src[0], rl_src[1], rl_dest, 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 break;
330 case Instruction::AGET:
331 case Instruction::AGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800332 cg->GenArrayGet(cu, opt_flags, kWord, rl_src[0], rl_src[1], rl_dest, 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 break;
334 case Instruction::AGET_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800335 cg->GenArrayGet(cu, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 break;
337 case Instruction::AGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800338 cg->GenArrayGet(cu, opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 break;
340 case Instruction::AGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800341 cg->GenArrayGet(cu, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 break;
343 case Instruction::AGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800344 cg->GenArrayGet(cu, opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 break;
346 case Instruction::APUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800347 cg->GenArrayPut(cu, opt_flags, kLong, rl_src[1], rl_src[2], rl_src[0], 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 break;
349 case Instruction::APUT:
buzbee02031b12012-11-23 09:41:35 -0800350 cg->GenArrayPut(cu, opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 break;
352 case Instruction::APUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800353 cg->GenArrayObjPut(cu, opt_flags, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 break;
355 case Instruction::APUT_SHORT:
356 case Instruction::APUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800357 cg->GenArrayPut(cu, opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 break;
359 case Instruction::APUT_BYTE:
360 case Instruction::APUT_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800361 cg->GenArrayPut(cu, opt_flags, kUnsignedByte, rl_src[1], rl_src[2],
buzbeefa57c472012-11-21 12:06:18 -0800362 rl_src[0], 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700363 break;
364
365 case Instruction::IGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800366 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 break;
368
369 case Instruction::IGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800370 cg->GenIGet(cu, vC, opt_flags, kLong, rl_dest, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700371 break;
372
373 case Instruction::IGET:
buzbee02031b12012-11-23 09:41:35 -0800374 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 break;
376
377 case Instruction::IGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800378 cg->GenIGet(cu, vC, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 break;
380
381 case Instruction::IGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800382 cg->GenIGet(cu, vC, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 break;
384
385 case Instruction::IGET_BOOLEAN:
386 case Instruction::IGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800387 cg->GenIGet(cu, vC, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700388 break;
389
390 case Instruction::IPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800391 cg->GenIPut(cu, vC, opt_flags, kLong, rl_src[0], rl_src[1], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 break;
393
394 case Instruction::IPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800395 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 break;
397
398 case Instruction::IPUT:
buzbee02031b12012-11-23 09:41:35 -0800399 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 break;
401
402 case Instruction::IPUT_BOOLEAN:
403 case Instruction::IPUT_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800404 cg->GenIPut(cu, vC, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 break;
406
407 case Instruction::IPUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800408 cg->GenIPut(cu, vC, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 break;
410
411 case Instruction::IPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800412 cg->GenIPut(cu, vC, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 break;
414
415 case Instruction::SGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800416 cg->GenSget(cu, vB, rl_dest, false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 break;
418 case Instruction::SGET:
419 case Instruction::SGET_BOOLEAN:
420 case Instruction::SGET_BYTE:
421 case Instruction::SGET_CHAR:
422 case Instruction::SGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800423 cg->GenSget(cu, vB, rl_dest, false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 break;
425
426 case Instruction::SGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800427 cg->GenSget(cu, vB, rl_dest, true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 break;
429
430 case Instruction::SPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800431 cg->GenSput(cu, vB, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 break;
433
434 case Instruction::SPUT:
435 case Instruction::SPUT_BOOLEAN:
436 case Instruction::SPUT_BYTE:
437 case Instruction::SPUT_CHAR:
438 case Instruction::SPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800439 cg->GenSput(cu, vB, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700440 break;
441
442 case Instruction::SPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800443 cg->GenSput(cu, vB, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 break;
445
446 case Instruction::INVOKE_STATIC_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800447 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 break;
449 case Instruction::INVOKE_STATIC:
buzbee02031b12012-11-23 09:41:35 -0800450 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700451 break;
452
453 case Instruction::INVOKE_DIRECT:
buzbee02031b12012-11-23 09:41:35 -0800454 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 break;
456 case Instruction::INVOKE_DIRECT_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800457 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 break;
459
460 case Instruction::INVOKE_VIRTUAL:
buzbee02031b12012-11-23 09:41:35 -0800461 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 break;
463 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800464 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700465 break;
466
467 case Instruction::INVOKE_SUPER:
buzbee02031b12012-11-23 09:41:35 -0800468 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700469 break;
470 case Instruction::INVOKE_SUPER_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800471 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 break;
473
474 case Instruction::INVOKE_INTERFACE:
buzbee02031b12012-11-23 09:41:35 -0800475 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 break;
477 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800478 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 break;
480
481 case Instruction::NEG_INT:
482 case Instruction::NOT_INT:
buzbee02031b12012-11-23 09:41:35 -0800483 res = cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700484 break;
485
486 case Instruction::NEG_LONG:
487 case Instruction::NOT_LONG:
buzbee02031b12012-11-23 09:41:35 -0800488 res = cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700489 break;
490
491 case Instruction::NEG_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800492 res = cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 break;
494
495 case Instruction::NEG_DOUBLE:
buzbee02031b12012-11-23 09:41:35 -0800496 res = cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700497 break;
498
499 case Instruction::INT_TO_LONG:
buzbee02031b12012-11-23 09:41:35 -0800500 cg->GenIntToLong(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 break;
502
503 case Instruction::LONG_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -0800504 rl_src[0] = UpdateLocWide(cu, rl_src[0]);
505 rl_src[0] = WideToNarrow(cu, rl_src[0]);
buzbee02031b12012-11-23 09:41:35 -0800506 cg->StoreValue(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700507 break;
508
509 case Instruction::INT_TO_BYTE:
510 case Instruction::INT_TO_SHORT:
511 case Instruction::INT_TO_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800512 cg->GenIntNarrowing(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700513 break;
514
515 case Instruction::INT_TO_FLOAT:
516 case Instruction::INT_TO_DOUBLE:
517 case Instruction::LONG_TO_FLOAT:
518 case Instruction::LONG_TO_DOUBLE:
519 case Instruction::FLOAT_TO_INT:
520 case Instruction::FLOAT_TO_LONG:
521 case Instruction::FLOAT_TO_DOUBLE:
522 case Instruction::DOUBLE_TO_INT:
523 case Instruction::DOUBLE_TO_LONG:
524 case Instruction::DOUBLE_TO_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800525 cg->GenConversion(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 break;
527
buzbeee6285f92012-12-06 15:57:46 -0800528
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 case Instruction::ADD_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 case Instruction::ADD_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800531 case Instruction::MUL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700532 case Instruction::MUL_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800533 case Instruction::AND_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700534 case Instruction::AND_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800535 case Instruction::OR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700536 case Instruction::OR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800537 case Instruction::XOR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 case Instruction::XOR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800539 if (rl_src[0].is_const &&
540 cu->cg->InexpensiveConstant(0, cu->constant_values[rl_src[0].orig_sreg])) {
541 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[1],
542 cu->constant_values[rl_src[0].orig_sreg]);
543 } else if (rl_src[1].is_const &&
544 cu->cg->InexpensiveConstant(0, cu->constant_values[rl_src[1].orig_sreg])) {
545 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0],
546 cu->constant_values[rl_src[1].orig_sreg]);
547 } else {
548 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
549 }
550 break;
551
552 case Instruction::SUB_INT:
553 case Instruction::SUB_INT_2ADDR:
554 case Instruction::DIV_INT:
555 case Instruction::DIV_INT_2ADDR:
556 case Instruction::REM_INT:
557 case Instruction::REM_INT_2ADDR:
558 case Instruction::SHL_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 case Instruction::SHL_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800560 case Instruction::SHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700561 case Instruction::SHR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800562 case Instruction::USHR_INT:
Bill Buzbeea114add2012-05-03 15:00:40 -0700563 case Instruction::USHR_INT_2ADDR:
buzbeee6285f92012-12-06 15:57:46 -0800564 if (rl_src[1].is_const &&
565 cu->cg->InexpensiveConstant(0, cu->constant_values[rl_src[1].orig_sreg])) {
566 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0],
567 cu->constant_values[rl_src[1].orig_sreg]);
568 } else {
569 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
570 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 break;
572
573 case Instruction::ADD_LONG:
574 case Instruction::SUB_LONG:
575 case Instruction::MUL_LONG:
576 case Instruction::DIV_LONG:
577 case Instruction::REM_LONG:
578 case Instruction::AND_LONG:
579 case Instruction::OR_LONG:
580 case Instruction::XOR_LONG:
581 case Instruction::ADD_LONG_2ADDR:
582 case Instruction::SUB_LONG_2ADDR:
583 case Instruction::MUL_LONG_2ADDR:
584 case Instruction::DIV_LONG_2ADDR:
585 case Instruction::REM_LONG_2ADDR:
586 case Instruction::AND_LONG_2ADDR:
587 case Instruction::OR_LONG_2ADDR:
588 case Instruction::XOR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800589 cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700590 break;
591
592 case Instruction::SHL_LONG:
593 case Instruction::SHR_LONG:
594 case Instruction::USHR_LONG:
595 case Instruction::SHL_LONG_2ADDR:
596 case Instruction::SHR_LONG_2ADDR:
597 case Instruction::USHR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800598 cg->GenShiftOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 break;
600
601 case Instruction::ADD_FLOAT:
602 case Instruction::SUB_FLOAT:
603 case Instruction::MUL_FLOAT:
604 case Instruction::DIV_FLOAT:
605 case Instruction::REM_FLOAT:
606 case Instruction::ADD_FLOAT_2ADDR:
607 case Instruction::SUB_FLOAT_2ADDR:
608 case Instruction::MUL_FLOAT_2ADDR:
609 case Instruction::DIV_FLOAT_2ADDR:
610 case Instruction::REM_FLOAT_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800611 cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 break;
613
614 case Instruction::ADD_DOUBLE:
615 case Instruction::SUB_DOUBLE:
616 case Instruction::MUL_DOUBLE:
617 case Instruction::DIV_DOUBLE:
618 case Instruction::REM_DOUBLE:
619 case Instruction::ADD_DOUBLE_2ADDR:
620 case Instruction::SUB_DOUBLE_2ADDR:
621 case Instruction::MUL_DOUBLE_2ADDR:
622 case Instruction::DIV_DOUBLE_2ADDR:
623 case Instruction::REM_DOUBLE_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800624 cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700625 break;
626
627 case Instruction::RSUB_INT:
628 case Instruction::ADD_INT_LIT16:
629 case Instruction::MUL_INT_LIT16:
630 case Instruction::DIV_INT_LIT16:
631 case Instruction::REM_INT_LIT16:
632 case Instruction::AND_INT_LIT16:
633 case Instruction::OR_INT_LIT16:
634 case Instruction::XOR_INT_LIT16:
635 case Instruction::ADD_INT_LIT8:
636 case Instruction::RSUB_INT_LIT8:
637 case Instruction::MUL_INT_LIT8:
638 case Instruction::DIV_INT_LIT8:
639 case Instruction::REM_INT_LIT8:
640 case Instruction::AND_INT_LIT8:
641 case Instruction::OR_INT_LIT8:
642 case Instruction::XOR_INT_LIT8:
643 case Instruction::SHL_INT_LIT8:
644 case Instruction::SHR_INT_LIT8:
645 case Instruction::USHR_INT_LIT8:
buzbee02031b12012-11-23 09:41:35 -0800646 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0], vC);
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 break;
648
649 default:
650 res = true;
651 }
652 return res;
buzbeee3acd072012-02-25 17:03:10 -0800653}
654
buzbeea169e1d2012-12-05 14:26:44 -0800655// Process extended MIR instructions
buzbeefa57c472012-11-21 12:06:18 -0800656static void HandleExtendedMethodMIR(CompilationUnit* cu, BasicBlock* bb, MIR* mir)
buzbeee3acd072012-02-25 17:03:10 -0800657{
buzbee02031b12012-11-23 09:41:35 -0800658 Codegen* cg = cu->cg.get();
buzbeecbd6d442012-11-17 14:11:25 -0800659 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700660 case kMirOpCopy: {
buzbeefa57c472012-11-21 12:06:18 -0800661 RegLocation rl_src = GetSrc(cu, mir, 0);
662 RegLocation rl_dest = GetDest(cu, mir);
buzbee02031b12012-11-23 09:41:35 -0800663 cg->StoreValue(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -0700664 break;
665 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 case kMirOpFusedCmplFloat:
buzbee02031b12012-11-23 09:41:35 -0800667 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700668 break;
669 case kMirOpFusedCmpgFloat:
buzbee02031b12012-11-23 09:41:35 -0800670 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700671 break;
672 case kMirOpFusedCmplDouble:
buzbee02031b12012-11-23 09:41:35 -0800673 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 break;
675 case kMirOpFusedCmpgDouble:
buzbee02031b12012-11-23 09:41:35 -0800676 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700677 break;
678 case kMirOpFusedCmpLong:
buzbee02031b12012-11-23 09:41:35 -0800679 cg->GenFusedLongCmpBranch(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 default:
682 break;
683 }
buzbeee3acd072012-02-25 17:03:10 -0800684}
685
buzbee02031b12012-11-23 09:41:35 -0800686// Handle the content in each basic block.
buzbeefa57c472012-11-21 12:06:18 -0800687static bool MethodBlockCodeGen(CompilationUnit* cu, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800688{
buzbeefa57c472012-11-21 12:06:18 -0800689 if (bb->block_type == kDead) return false;
buzbee02031b12012-11-23 09:41:35 -0800690 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800691 cu->current_dalvik_offset = bb->start_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700692 MIR* mir;
buzbeefa57c472012-11-21 12:06:18 -0800693 LIR* label_list = cu->block_label_list;
694 int block_id = bb->id;
buzbeee3acd072012-02-25 17:03:10 -0800695
buzbeefa57c472012-11-21 12:06:18 -0800696 cu->cur_block = bb;
697 label_list[block_id].operands[0] = bb->start_offset;
buzbeee3acd072012-02-25 17:03:10 -0800698
buzbee02031b12012-11-23 09:41:35 -0800699 // Insert the block label.
buzbeefa57c472012-11-21 12:06:18 -0800700 label_list[block_id].opcode = kPseudoNormalBlockLabel;
701 AppendLIR(cu, &label_list[block_id]);
buzbeee3acd072012-02-25 17:03:10 -0800702
buzbeefa57c472012-11-21 12:06:18 -0800703 LIR* head_lir = NULL;
buzbee8320f382012-09-11 16:29:42 -0700704
buzbee02031b12012-11-23 09:41:35 -0800705 // If this is a catch block, export the start address.
buzbeefa57c472012-11-21 12:06:18 -0800706 if (bb->catch_entry) {
707 head_lir = NewLIR0(cu, kPseudoExportedPC);
buzbee8320f382012-09-11 16:29:42 -0700708 }
709
buzbee02031b12012-11-23 09:41:35 -0800710 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800711 ResetRegPool(cu);
712 ResetDefTracking(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700713
buzbeefa57c472012-11-21 12:06:18 -0800714 ClobberAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700715
buzbeefa57c472012-11-21 12:06:18 -0800716 if (bb->block_type == kEntryBlock) {
717 int start_vreg = cu->num_dalvik_registers - cu->num_ins;
buzbee02031b12012-11-23 09:41:35 -0800718 cg->GenEntrySequence(cu, &cu->reg_location[start_vreg],
719 cu->reg_location[cu->method_sreg]);
buzbeefa57c472012-11-21 12:06:18 -0800720 } else if (bb->block_type == kExitBlock) {
buzbee02031b12012-11-23 09:41:35 -0800721 cg->GenExitSequence(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700722 }
723
buzbee28c9a832012-11-21 15:39:13 -0800724 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeefa57c472012-11-21 12:06:18 -0800725 ResetRegPool(cu);
726 if (cu->disable_opt & (1 << kTrackLiveTemps)) {
727 ClobberAllRegs(cu);
buzbeee1965672012-03-11 18:39:19 -0700728 }
729
buzbeefa57c472012-11-21 12:06:18 -0800730 if (cu->disable_opt & (1 << kSuppressLoads)) {
731 ResetDefTracking(cu);
buzbeee3acd072012-02-25 17:03:10 -0800732 }
733
buzbee3d661942012-03-14 17:37:27 -0700734#ifndef NDEBUG
buzbee02031b12012-11-23 09:41:35 -0800735 // Reset temp tracking sanity check.
buzbeefa57c472012-11-21 12:06:18 -0800736 cu->live_sreg = INVALID_SREG;
buzbee3d661942012-03-14 17:37:27 -0700737#endif
738
buzbeefa57c472012-11-21 12:06:18 -0800739 cu->current_dalvik_offset = mir->offset;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700740 int opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800741 LIR* boundary_lir;
buzbeee3acd072012-02-25 17:03:10 -0800742
buzbee02031b12012-11-23 09:41:35 -0800743 // Mark the beginning of a Dalvik instruction for line tracking.
buzbeefa57c472012-11-21 12:06:18 -0800744 char* inst_str = cu->verbose ?
buzbeea169e1d2012-12-05 14:26:44 -0800745 GetDalvikDisassembly(cu, mir) : NULL;
buzbeefa57c472012-11-21 12:06:18 -0800746 boundary_lir = MarkBoundary(cu, mir->offset, inst_str);
buzbee02031b12012-11-23 09:41:35 -0800747 // Remember the first LIR for this block.
buzbeefa57c472012-11-21 12:06:18 -0800748 if (head_lir == NULL) {
749 head_lir = boundary_lir;
buzbee02031b12012-11-23 09:41:35 -0800750 // Set the first boundary_lir as a scheduling barrier.
buzbeefa57c472012-11-21 12:06:18 -0800751 head_lir->def_mask = ENCODE_ALL;
buzbeee3acd072012-02-25 17:03:10 -0800752 }
753
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700754 if (opcode == kMirOpCheck) {
755 // Combine check and work halves of throwing instruction.
buzbeefa57c472012-11-21 12:06:18 -0800756 MIR* work_half = mir->meta.throw_insn;
757 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
758 opcode = work_half->dalvikInsn.opcode;
759 SSARepresentation* ssa_rep = work_half->ssa_rep;
760 work_half->ssa_rep = mir->ssa_rep;
761 mir->ssa_rep = ssa_rep;
buzbeea169e1d2012-12-05 14:26:44 -0800762 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheckPart2);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700763 }
764
765 if (opcode >= kMirOpFirst) {
buzbeefa57c472012-11-21 12:06:18 -0800766 HandleExtendedMethodMIR(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700767 continue;
768 }
769
buzbeefa57c472012-11-21 12:06:18 -0800770 bool not_handled = CompileDalvikInstruction(cu, mir, bb, label_list);
771 if (not_handled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700772 LOG(FATAL) << StringPrintf("%#06x: Opcode %#x (%s)",
773 mir->offset, opcode,
774 Instruction::Name(mir->dalvikInsn.opcode));
Bill Buzbeea114add2012-05-03 15:00:40 -0700775 }
776 }
777
buzbeefa57c472012-11-21 12:06:18 -0800778 if (head_lir) {
buzbee02031b12012-11-23 09:41:35 -0800779 // Eliminate redundant loads/stores and delay stores into later slots.
buzbeefa57c472012-11-21 12:06:18 -0800780 ApplyLocalOptimizations(cu, head_lir, cu->last_lir_insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700781
buzbee02031b12012-11-23 09:41:35 -0800782 // Generate an unconditional branch to the fallthrough block.
buzbeefa57c472012-11-21 12:06:18 -0800783 if (bb->fall_through) {
buzbee02031b12012-11-23 09:41:35 -0800784 cg->OpUnconditionalBranch(cu, &label_list[bb->fall_through->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 }
786 }
787 return false;
buzbeee3acd072012-02-25 17:03:10 -0800788}
789
buzbeefa57c472012-11-21 12:06:18 -0800790void SpecialMIR2LIR(CompilationUnit* cu, SpecialCaseHandler special_case)
buzbee16da88c2012-03-20 10:38:17 -0700791{
buzbee02031b12012-11-23 09:41:35 -0800792 Codegen* cg = cu->cg.get();
793 // Find the first DalvikByteCode block.
buzbeefa57c472012-11-21 12:06:18 -0800794 int num_reachable_blocks = cu->num_reachable_blocks;
795 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700796 BasicBlock*bb = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800797 for (int idx = 0; idx < num_reachable_blocks; idx++) {
798 int dfs_index = cu->dfs_order.elem_list[idx];
799 bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, dfs_index));
800 if (bb->block_type == kDalvikByteCode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 break;
buzbee16da88c2012-03-20 10:38:17 -0700802 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700803 }
804 if (bb == NULL) {
805 return;
806 }
buzbeefa57c472012-11-21 12:06:18 -0800807 DCHECK_EQ(bb->start_offset, 0);
808 DCHECK(bb->first_mir_insn != NULL);
buzbee16da88c2012-03-20 10:38:17 -0700809
buzbee02031b12012-11-23 09:41:35 -0800810 // Get the first instruction.
buzbeefa57c472012-11-21 12:06:18 -0800811 MIR* mir = bb->first_mir_insn;
buzbee16da88c2012-03-20 10:38:17 -0700812
buzbee02031b12012-11-23 09:41:35 -0800813 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800814 ResetRegPool(cu);
815 ResetDefTracking(cu);
816 ClobberAllRegs(cu);
buzbee16da88c2012-03-20 10:38:17 -0700817
buzbee02031b12012-11-23 09:41:35 -0800818 cg->GenSpecialCase(cu, bb, mir, special_case);
buzbee16da88c2012-03-20 10:38:17 -0700819}
820
buzbeefa57c472012-11-21 12:06:18 -0800821void MethodMIR2LIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800822{
buzbee02031b12012-11-23 09:41:35 -0800823 Codegen* cg = cu->cg.get();
824 // Hold the labels of each block.
buzbeefa57c472012-11-21 12:06:18 -0800825 cu->block_label_list =
826 static_cast<LIR*>(NewMem(cu, sizeof(LIR) * cu->num_blocks, true, kAllocLIR));
buzbeee3acd072012-02-25 17:03:10 -0800827
buzbeefa57c472012-11-21 12:06:18 -0800828 DataFlowAnalysisDispatcher(cu, MethodBlockCodeGen,
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 kPreOrderDFSTraversal, false /* Iterative */);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700830
buzbee02031b12012-11-23 09:41:35 -0800831 cg->HandleSuspendLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800832
buzbee02031b12012-11-23 09:41:35 -0800833 cg->HandleThrowLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800834
buzbee02031b12012-11-23 09:41:35 -0800835 cg->HandleIntrinsicLaunchPads(cu);
buzbeefc9e6fa2012-03-23 15:14:29 -0700836
buzbeefa57c472012-11-21 12:06:18 -0800837 if (!(cu->disable_opt & (1 << kSafeOptimizations))) {
838 RemoveRedundantBranches(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 }
buzbeee3acd072012-02-25 17:03:10 -0800840}
841
buzbeee3acd072012-02-25 17:03:10 -0800842} // namespace art