blob: fc3df6ec91946c4030e573b0d1934261eeea608b [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);
281 if (backward_branch) {
buzbee02031b12012-11-23 09:41:35 -0800282 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 }
buzbee02031b12012-11-23 09:41:35 -0800284 cg->GenCompareAndBranch(cu, opcode, rl_src[0], rl_src[1], taken,
buzbeefa57c472012-11-21 12:06:18 -0800285 fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 break;
287 }
288
289 case Instruction::IF_EQZ:
290 case Instruction::IF_NEZ:
291 case Instruction::IF_LTZ:
292 case Instruction::IF_GEZ:
293 case Instruction::IF_GTZ:
294 case Instruction::IF_LEZ: {
buzbeefa57c472012-11-21 12:06:18 -0800295 LIR* taken = &label_list[bb->taken->id];
296 LIR* fall_through = &label_list[bb->fall_through->id];
297 bool backward_branch;
298 backward_branch = (bb->taken->start_offset <= mir->offset);
299 if (backward_branch) {
buzbee02031b12012-11-23 09:41:35 -0800300 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 }
buzbee02031b12012-11-23 09:41:35 -0800302 cg->GenCompareZeroAndBranch(cu, opcode, rl_src[0], taken, fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 break;
304 }
305
306 case Instruction::AGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800307 cg->GenArrayGet(cu, opt_flags, kLong, rl_src[0], rl_src[1], rl_dest, 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700308 break;
309 case Instruction::AGET:
310 case Instruction::AGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800311 cg->GenArrayGet(cu, opt_flags, kWord, rl_src[0], rl_src[1], rl_dest, 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 break;
313 case Instruction::AGET_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800314 cg->GenArrayGet(cu, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 break;
316 case Instruction::AGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800317 cg->GenArrayGet(cu, opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 break;
319 case Instruction::AGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800320 cg->GenArrayGet(cu, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 break;
322 case Instruction::AGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800323 cg->GenArrayGet(cu, opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 break;
325 case Instruction::APUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800326 cg->GenArrayPut(cu, opt_flags, kLong, rl_src[1], rl_src[2], rl_src[0], 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700327 break;
328 case Instruction::APUT:
buzbee02031b12012-11-23 09:41:35 -0800329 cg->GenArrayPut(cu, opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 break;
331 case Instruction::APUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800332 cg->GenArrayObjPut(cu, opt_flags, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 break;
334 case Instruction::APUT_SHORT:
335 case Instruction::APUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800336 cg->GenArrayPut(cu, opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 break;
338 case Instruction::APUT_BYTE:
339 case Instruction::APUT_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800340 cg->GenArrayPut(cu, opt_flags, kUnsignedByte, rl_src[1], rl_src[2],
buzbeefa57c472012-11-21 12:06:18 -0800341 rl_src[0], 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 break;
343
344 case Instruction::IGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800345 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 break;
347
348 case Instruction::IGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800349 cg->GenIGet(cu, vC, opt_flags, kLong, rl_dest, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 break;
351
352 case Instruction::IGET:
buzbee02031b12012-11-23 09:41:35 -0800353 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 break;
355
356 case Instruction::IGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800357 cg->GenIGet(cu, vC, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 break;
359
360 case Instruction::IGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800361 cg->GenIGet(cu, vC, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 break;
363
364 case Instruction::IGET_BOOLEAN:
365 case Instruction::IGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800366 cg->GenIGet(cu, vC, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 break;
368
369 case Instruction::IPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800370 cg->GenIPut(cu, vC, opt_flags, kLong, rl_src[0], rl_src[1], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700371 break;
372
373 case Instruction::IPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800374 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 break;
376
377 case Instruction::IPUT:
buzbee02031b12012-11-23 09:41:35 -0800378 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 break;
380
381 case Instruction::IPUT_BOOLEAN:
382 case Instruction::IPUT_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800383 cg->GenIPut(cu, vC, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 break;
385
386 case Instruction::IPUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800387 cg->GenIPut(cu, vC, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700388 break;
389
390 case Instruction::IPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800391 cg->GenIPut(cu, vC, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 break;
393
394 case Instruction::SGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800395 cg->GenSget(cu, vB, rl_dest, false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 break;
397 case Instruction::SGET:
398 case Instruction::SGET_BOOLEAN:
399 case Instruction::SGET_BYTE:
400 case Instruction::SGET_CHAR:
401 case Instruction::SGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800402 cg->GenSget(cu, vB, rl_dest, false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 break;
404
405 case Instruction::SGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800406 cg->GenSget(cu, vB, rl_dest, true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700407 break;
408
409 case Instruction::SPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800410 cg->GenSput(cu, vB, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 break;
412
413 case Instruction::SPUT:
414 case Instruction::SPUT_BOOLEAN:
415 case Instruction::SPUT_BYTE:
416 case Instruction::SPUT_CHAR:
417 case Instruction::SPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800418 cg->GenSput(cu, vB, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 break;
420
421 case Instruction::SPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800422 cg->GenSput(cu, vB, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 break;
424
425 case Instruction::INVOKE_STATIC_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800426 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 break;
428 case Instruction::INVOKE_STATIC:
buzbee02031b12012-11-23 09:41:35 -0800429 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 break;
431
432 case Instruction::INVOKE_DIRECT:
buzbee02031b12012-11-23 09:41:35 -0800433 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 break;
435 case Instruction::INVOKE_DIRECT_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800436 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 break;
438
439 case Instruction::INVOKE_VIRTUAL:
buzbee02031b12012-11-23 09:41:35 -0800440 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 break;
442 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800443 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 break;
445
446 case Instruction::INVOKE_SUPER:
buzbee02031b12012-11-23 09:41:35 -0800447 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 break;
449 case Instruction::INVOKE_SUPER_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800450 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700451 break;
452
453 case Instruction::INVOKE_INTERFACE:
buzbee02031b12012-11-23 09:41:35 -0800454 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 break;
456 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800457 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 break;
459
460 case Instruction::NEG_INT:
461 case Instruction::NOT_INT:
buzbee02031b12012-11-23 09:41:35 -0800462 res = cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 break;
464
465 case Instruction::NEG_LONG:
466 case Instruction::NOT_LONG:
buzbee02031b12012-11-23 09:41:35 -0800467 res = cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700468 break;
469
470 case Instruction::NEG_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800471 res = cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 break;
473
474 case Instruction::NEG_DOUBLE:
buzbee02031b12012-11-23 09:41:35 -0800475 res = cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 break;
477
478 case Instruction::INT_TO_LONG:
buzbee02031b12012-11-23 09:41:35 -0800479 cg->GenIntToLong(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 break;
481
482 case Instruction::LONG_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -0800483 rl_src[0] = UpdateLocWide(cu, rl_src[0]);
484 rl_src[0] = WideToNarrow(cu, rl_src[0]);
buzbee02031b12012-11-23 09:41:35 -0800485 cg->StoreValue(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 break;
487
488 case Instruction::INT_TO_BYTE:
489 case Instruction::INT_TO_SHORT:
490 case Instruction::INT_TO_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800491 cg->GenIntNarrowing(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700492 break;
493
494 case Instruction::INT_TO_FLOAT:
495 case Instruction::INT_TO_DOUBLE:
496 case Instruction::LONG_TO_FLOAT:
497 case Instruction::LONG_TO_DOUBLE:
498 case Instruction::FLOAT_TO_INT:
499 case Instruction::FLOAT_TO_LONG:
500 case Instruction::FLOAT_TO_DOUBLE:
501 case Instruction::DOUBLE_TO_INT:
502 case Instruction::DOUBLE_TO_LONG:
503 case Instruction::DOUBLE_TO_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800504 cg->GenConversion(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 break;
506
507 case Instruction::ADD_INT:
508 case Instruction::SUB_INT:
509 case Instruction::MUL_INT:
510 case Instruction::DIV_INT:
511 case Instruction::REM_INT:
512 case Instruction::AND_INT:
513 case Instruction::OR_INT:
514 case Instruction::XOR_INT:
515 case Instruction::SHL_INT:
516 case Instruction::SHR_INT:
517 case Instruction::USHR_INT:
518 case Instruction::ADD_INT_2ADDR:
519 case Instruction::SUB_INT_2ADDR:
520 case Instruction::MUL_INT_2ADDR:
521 case Instruction::DIV_INT_2ADDR:
522 case Instruction::REM_INT_2ADDR:
523 case Instruction::AND_INT_2ADDR:
524 case Instruction::OR_INT_2ADDR:
525 case Instruction::XOR_INT_2ADDR:
526 case Instruction::SHL_INT_2ADDR:
527 case Instruction::SHR_INT_2ADDR:
528 case Instruction::USHR_INT_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800529 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 break;
531
532 case Instruction::ADD_LONG:
533 case Instruction::SUB_LONG:
534 case Instruction::MUL_LONG:
535 case Instruction::DIV_LONG:
536 case Instruction::REM_LONG:
537 case Instruction::AND_LONG:
538 case Instruction::OR_LONG:
539 case Instruction::XOR_LONG:
540 case Instruction::ADD_LONG_2ADDR:
541 case Instruction::SUB_LONG_2ADDR:
542 case Instruction::MUL_LONG_2ADDR:
543 case Instruction::DIV_LONG_2ADDR:
544 case Instruction::REM_LONG_2ADDR:
545 case Instruction::AND_LONG_2ADDR:
546 case Instruction::OR_LONG_2ADDR:
547 case Instruction::XOR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800548 cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 break;
550
551 case Instruction::SHL_LONG:
552 case Instruction::SHR_LONG:
553 case Instruction::USHR_LONG:
554 case Instruction::SHL_LONG_2ADDR:
555 case Instruction::SHR_LONG_2ADDR:
556 case Instruction::USHR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800557 cg->GenShiftOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700558 break;
559
560 case Instruction::ADD_FLOAT:
561 case Instruction::SUB_FLOAT:
562 case Instruction::MUL_FLOAT:
563 case Instruction::DIV_FLOAT:
564 case Instruction::REM_FLOAT:
565 case Instruction::ADD_FLOAT_2ADDR:
566 case Instruction::SUB_FLOAT_2ADDR:
567 case Instruction::MUL_FLOAT_2ADDR:
568 case Instruction::DIV_FLOAT_2ADDR:
569 case Instruction::REM_FLOAT_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800570 cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 break;
572
573 case Instruction::ADD_DOUBLE:
574 case Instruction::SUB_DOUBLE:
575 case Instruction::MUL_DOUBLE:
576 case Instruction::DIV_DOUBLE:
577 case Instruction::REM_DOUBLE:
578 case Instruction::ADD_DOUBLE_2ADDR:
579 case Instruction::SUB_DOUBLE_2ADDR:
580 case Instruction::MUL_DOUBLE_2ADDR:
581 case Instruction::DIV_DOUBLE_2ADDR:
582 case Instruction::REM_DOUBLE_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800583 cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 break;
585
586 case Instruction::RSUB_INT:
587 case Instruction::ADD_INT_LIT16:
588 case Instruction::MUL_INT_LIT16:
589 case Instruction::DIV_INT_LIT16:
590 case Instruction::REM_INT_LIT16:
591 case Instruction::AND_INT_LIT16:
592 case Instruction::OR_INT_LIT16:
593 case Instruction::XOR_INT_LIT16:
594 case Instruction::ADD_INT_LIT8:
595 case Instruction::RSUB_INT_LIT8:
596 case Instruction::MUL_INT_LIT8:
597 case Instruction::DIV_INT_LIT8:
598 case Instruction::REM_INT_LIT8:
599 case Instruction::AND_INT_LIT8:
600 case Instruction::OR_INT_LIT8:
601 case Instruction::XOR_INT_LIT8:
602 case Instruction::SHL_INT_LIT8:
603 case Instruction::SHR_INT_LIT8:
604 case Instruction::USHR_INT_LIT8:
buzbee02031b12012-11-23 09:41:35 -0800605 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0], vC);
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 break;
607
608 default:
609 res = true;
610 }
611 return res;
buzbeee3acd072012-02-25 17:03:10 -0800612}
613
buzbee02031b12012-11-23 09:41:35 -0800614// Process extended MIR instructions (such as PHI).
buzbeefa57c472012-11-21 12:06:18 -0800615static void HandleExtendedMethodMIR(CompilationUnit* cu, BasicBlock* bb, MIR* mir)
buzbeee3acd072012-02-25 17:03:10 -0800616{
buzbee02031b12012-11-23 09:41:35 -0800617 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800618 int op_offset = mir->dalvikInsn.opcode - kMirOpFirst;
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 char* msg = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800620 if (cu->verbose) {
621 msg = static_cast<char*>(NewMem(cu, strlen(extended_mir_op_names[op_offset]) + 1,
buzbeecbd6d442012-11-17 14:11:25 -0800622 false, kAllocDebugInfo));
buzbeefa57c472012-11-21 12:06:18 -0800623 strcpy(msg, extended_mir_op_names[op_offset]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700624 }
buzbeefa57c472012-11-21 12:06:18 -0800625 LIR* op = NewLIR1(cu, kPseudoExtended, reinterpret_cast<uintptr_t>(msg));
buzbeee3acd072012-02-25 17:03:10 -0800626
buzbeecbd6d442012-11-17 14:11:25 -0800627 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 case kMirOpPhi: {
buzbeefa57c472012-11-21 12:06:18 -0800629 char* ssa_string = NULL;
630 if (cu->verbose) {
631 ssa_string = GetSSAString(cu, mir->ssa_rep);
Bill Buzbeea114add2012-05-03 15:00:40 -0700632 }
buzbeefa57c472012-11-21 12:06:18 -0800633 op->flags.is_nop = true;
634 NewLIR1(cu, kPseudoSSARep, reinterpret_cast<uintptr_t>(ssa_string));
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 break;
buzbeee3acd072012-02-25 17:03:10 -0800636 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 case kMirOpCopy: {
buzbeefa57c472012-11-21 12:06:18 -0800638 RegLocation rl_src = GetSrc(cu, mir, 0);
639 RegLocation rl_dest = GetDest(cu, mir);
buzbee02031b12012-11-23 09:41:35 -0800640 cg->StoreValue(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 break;
642 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700643 case kMirOpFusedCmplFloat:
buzbee02031b12012-11-23 09:41:35 -0800644 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 break;
646 case kMirOpFusedCmpgFloat:
buzbee02031b12012-11-23 09:41:35 -0800647 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 break;
649 case kMirOpFusedCmplDouble:
buzbee02031b12012-11-23 09:41:35 -0800650 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 break;
652 case kMirOpFusedCmpgDouble:
buzbee02031b12012-11-23 09:41:35 -0800653 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700654 break;
655 case kMirOpFusedCmpLong:
buzbee02031b12012-11-23 09:41:35 -0800656 cg->GenFusedLongCmpBranch(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700657 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 default:
659 break;
660 }
buzbeee3acd072012-02-25 17:03:10 -0800661}
662
buzbee02031b12012-11-23 09:41:35 -0800663// Handle the content in each basic block.
buzbeefa57c472012-11-21 12:06:18 -0800664static bool MethodBlockCodeGen(CompilationUnit* cu, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800665{
buzbeefa57c472012-11-21 12:06:18 -0800666 if (bb->block_type == kDead) return false;
buzbee02031b12012-11-23 09:41:35 -0800667 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800668 cu->current_dalvik_offset = bb->start_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700669 MIR* mir;
buzbeefa57c472012-11-21 12:06:18 -0800670 LIR* label_list = cu->block_label_list;
671 int block_id = bb->id;
buzbeee3acd072012-02-25 17:03:10 -0800672
buzbeefa57c472012-11-21 12:06:18 -0800673 cu->cur_block = bb;
674 label_list[block_id].operands[0] = bb->start_offset;
buzbeee3acd072012-02-25 17:03:10 -0800675
buzbee02031b12012-11-23 09:41:35 -0800676 // Insert the block label.
buzbeefa57c472012-11-21 12:06:18 -0800677 label_list[block_id].opcode = kPseudoNormalBlockLabel;
678 AppendLIR(cu, &label_list[block_id]);
buzbeee3acd072012-02-25 17:03:10 -0800679
buzbeefa57c472012-11-21 12:06:18 -0800680 LIR* head_lir = NULL;
buzbee8320f382012-09-11 16:29:42 -0700681
buzbee02031b12012-11-23 09:41:35 -0800682 // If this is a catch block, export the start address.
buzbeefa57c472012-11-21 12:06:18 -0800683 if (bb->catch_entry) {
684 head_lir = NewLIR0(cu, kPseudoExportedPC);
buzbee8320f382012-09-11 16:29:42 -0700685 }
686
buzbee02031b12012-11-23 09:41:35 -0800687 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800688 ResetRegPool(cu);
689 ResetDefTracking(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700690
buzbeefa57c472012-11-21 12:06:18 -0800691 ClobberAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700692
buzbeefa57c472012-11-21 12:06:18 -0800693 if (bb->block_type == kEntryBlock) {
694 int start_vreg = cu->num_dalvik_registers - cu->num_ins;
buzbee02031b12012-11-23 09:41:35 -0800695 cg->GenEntrySequence(cu, &cu->reg_location[start_vreg],
696 cu->reg_location[cu->method_sreg]);
buzbeefa57c472012-11-21 12:06:18 -0800697 } else if (bb->block_type == kExitBlock) {
buzbee02031b12012-11-23 09:41:35 -0800698 cg->GenExitSequence(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 }
700
buzbee28c9a832012-11-21 15:39:13 -0800701 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeefa57c472012-11-21 12:06:18 -0800702 ResetRegPool(cu);
703 if (cu->disable_opt & (1 << kTrackLiveTemps)) {
704 ClobberAllRegs(cu);
buzbeee1965672012-03-11 18:39:19 -0700705 }
706
buzbeefa57c472012-11-21 12:06:18 -0800707 if (cu->disable_opt & (1 << kSuppressLoads)) {
708 ResetDefTracking(cu);
buzbeee3acd072012-02-25 17:03:10 -0800709 }
710
buzbee3d661942012-03-14 17:37:27 -0700711#ifndef NDEBUG
buzbee02031b12012-11-23 09:41:35 -0800712 // Reset temp tracking sanity check.
buzbeefa57c472012-11-21 12:06:18 -0800713 cu->live_sreg = INVALID_SREG;
buzbee3d661942012-03-14 17:37:27 -0700714#endif
715
buzbeefa57c472012-11-21 12:06:18 -0800716 cu->current_dalvik_offset = mir->offset;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700717 int opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800718 LIR* boundary_lir;
buzbeee3acd072012-02-25 17:03:10 -0800719
buzbee02031b12012-11-23 09:41:35 -0800720 // Mark the beginning of a Dalvik instruction for line tracking.
buzbeefa57c472012-11-21 12:06:18 -0800721 char* inst_str = cu->verbose ?
722 GetDalvikDisassembly(cu, mir->dalvikInsn, "") : NULL;
723 boundary_lir = MarkBoundary(cu, mir->offset, inst_str);
buzbee02031b12012-11-23 09:41:35 -0800724 // Remember the first LIR for this block.
buzbeefa57c472012-11-21 12:06:18 -0800725 if (head_lir == NULL) {
726 head_lir = boundary_lir;
buzbee02031b12012-11-23 09:41:35 -0800727 // Set the first boundary_lir as a scheduling barrier.
buzbeefa57c472012-11-21 12:06:18 -0800728 head_lir->def_mask = ENCODE_ALL;
buzbeee3acd072012-02-25 17:03:10 -0800729 }
730
buzbee02031b12012-11-23 09:41:35 -0800731 // Don't generate the SSA annotation unless verbose mode is on.
buzbeefa57c472012-11-21 12:06:18 -0800732 if (cu->verbose && mir->ssa_rep) {
733 char* ssa_string = GetSSAString(cu, mir->ssa_rep);
734 NewLIR1(cu, kPseudoSSARep, reinterpret_cast<uintptr_t>(ssa_string));
Bill Buzbeea114add2012-05-03 15:00:40 -0700735 }
736
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700737 if (opcode == kMirOpCheck) {
738 // Combine check and work halves of throwing instruction.
buzbeefa57c472012-11-21 12:06:18 -0800739 MIR* work_half = mir->meta.throw_insn;
740 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
741 opcode = work_half->dalvikInsn.opcode;
742 SSARepresentation* ssa_rep = work_half->ssa_rep;
743 work_half->ssa_rep = mir->ssa_rep;
744 mir->ssa_rep = ssa_rep;
745 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700746 }
747
748 if (opcode >= kMirOpFirst) {
buzbeefa57c472012-11-21 12:06:18 -0800749 HandleExtendedMethodMIR(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700750 continue;
751 }
752
buzbeefa57c472012-11-21 12:06:18 -0800753 bool not_handled = CompileDalvikInstruction(cu, mir, bb, label_list);
754 if (not_handled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700755 LOG(FATAL) << StringPrintf("%#06x: Opcode %#x (%s)",
756 mir->offset, opcode,
757 Instruction::Name(mir->dalvikInsn.opcode));
Bill Buzbeea114add2012-05-03 15:00:40 -0700758 }
759 }
760
buzbeefa57c472012-11-21 12:06:18 -0800761 if (head_lir) {
buzbee02031b12012-11-23 09:41:35 -0800762 // Eliminate redundant loads/stores and delay stores into later slots.
buzbeefa57c472012-11-21 12:06:18 -0800763 ApplyLocalOptimizations(cu, head_lir, cu->last_lir_insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700764
buzbee02031b12012-11-23 09:41:35 -0800765 // Generate an unconditional branch to the fallthrough block.
buzbeefa57c472012-11-21 12:06:18 -0800766 if (bb->fall_through) {
buzbee02031b12012-11-23 09:41:35 -0800767 cg->OpUnconditionalBranch(cu, &label_list[bb->fall_through->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 }
769 }
770 return false;
buzbeee3acd072012-02-25 17:03:10 -0800771}
772
buzbeefa57c472012-11-21 12:06:18 -0800773void SpecialMIR2LIR(CompilationUnit* cu, SpecialCaseHandler special_case)
buzbee16da88c2012-03-20 10:38:17 -0700774{
buzbee02031b12012-11-23 09:41:35 -0800775 Codegen* cg = cu->cg.get();
776 // Find the first DalvikByteCode block.
buzbeefa57c472012-11-21 12:06:18 -0800777 int num_reachable_blocks = cu->num_reachable_blocks;
778 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700779 BasicBlock*bb = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800780 for (int idx = 0; idx < num_reachable_blocks; idx++) {
781 int dfs_index = cu->dfs_order.elem_list[idx];
782 bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, dfs_index));
783 if (bb->block_type == kDalvikByteCode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700784 break;
buzbee16da88c2012-03-20 10:38:17 -0700785 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700786 }
787 if (bb == NULL) {
788 return;
789 }
buzbeefa57c472012-11-21 12:06:18 -0800790 DCHECK_EQ(bb->start_offset, 0);
791 DCHECK(bb->first_mir_insn != NULL);
buzbee16da88c2012-03-20 10:38:17 -0700792
buzbee02031b12012-11-23 09:41:35 -0800793 // Get the first instruction.
buzbeefa57c472012-11-21 12:06:18 -0800794 MIR* mir = bb->first_mir_insn;
buzbee16da88c2012-03-20 10:38:17 -0700795
buzbee02031b12012-11-23 09:41:35 -0800796 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800797 ResetRegPool(cu);
798 ResetDefTracking(cu);
799 ClobberAllRegs(cu);
buzbee16da88c2012-03-20 10:38:17 -0700800
buzbee02031b12012-11-23 09:41:35 -0800801 cg->GenSpecialCase(cu, bb, mir, special_case);
buzbee16da88c2012-03-20 10:38:17 -0700802}
803
buzbeefa57c472012-11-21 12:06:18 -0800804void MethodMIR2LIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800805{
buzbee02031b12012-11-23 09:41:35 -0800806 Codegen* cg = cu->cg.get();
807 // Hold the labels of each block.
buzbeefa57c472012-11-21 12:06:18 -0800808 cu->block_label_list =
809 static_cast<LIR*>(NewMem(cu, sizeof(LIR) * cu->num_blocks, true, kAllocLIR));
buzbeee3acd072012-02-25 17:03:10 -0800810
buzbeefa57c472012-11-21 12:06:18 -0800811 DataFlowAnalysisDispatcher(cu, MethodBlockCodeGen,
Bill Buzbeea114add2012-05-03 15:00:40 -0700812 kPreOrderDFSTraversal, false /* Iterative */);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700813
buzbee02031b12012-11-23 09:41:35 -0800814 cg->HandleSuspendLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800815
buzbee02031b12012-11-23 09:41:35 -0800816 cg->HandleThrowLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800817
buzbee02031b12012-11-23 09:41:35 -0800818 cg->HandleIntrinsicLaunchPads(cu);
buzbeefc9e6fa2012-03-23 15:14:29 -0700819
buzbeefa57c472012-11-21 12:06:18 -0800820 if (!(cu->disable_opt & (1 << kSafeOptimizations))) {
821 RemoveRedundantBranches(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700822 }
buzbeee3acd072012-02-25 17:03:10 -0800823}
824
buzbeee3acd072012-02-25 17:03:10 -0800825} // namespace art