blob: 5a6a5fc8c4efa5652a1926cae74dacf3f1cbd46e [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);
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 break;
145
146 case Instruction::CONST_HIGH16:
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 << 16);
149 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 break;
151
152 case Instruction::CONST_WIDE_16:
153 case Instruction::CONST_WIDE_32:
buzbeefa57c472012-11-21 12:06:18 -0800154 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800155 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg, vB,
buzbee408ad162012-06-06 16:45:18 -0700156 (vB & 0x80000000) ? -1 : 0);
buzbee02031b12012-11-23 09:41:35 -0800157 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 break;
159
160 case Instruction::CONST_WIDE:
buzbeefa57c472012-11-21 12:06:18 -0800161 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800162 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg,
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 mir->dalvikInsn.vB_wide & 0xffffffff,
164 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
buzbee02031b12012-11-23 09:41:35 -0800165 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 break;
167
168 case Instruction::CONST_WIDE_HIGH16:
buzbeefa57c472012-11-21 12:06:18 -0800169 rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
buzbee02031b12012-11-23 09:41:35 -0800170 cg->LoadConstantValueWide(cu, rl_result.low_reg, rl_result.high_reg,
buzbee408ad162012-06-06 16:45:18 -0700171 0, vB << 16);
buzbee02031b12012-11-23 09:41:35 -0800172 cg->StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 break;
174
175 case Instruction::MONITOR_ENTER:
buzbee02031b12012-11-23 09:41:35 -0800176 cg->GenMonitorEnter(cu, opt_flags, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 break;
178
179 case Instruction::MONITOR_EXIT:
buzbee02031b12012-11-23 09:41:35 -0800180 cg->GenMonitorExit(cu, opt_flags, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700181 break;
182
183 case Instruction::CHECK_CAST:
buzbee02031b12012-11-23 09:41:35 -0800184 cg->GenCheckCast(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 break;
186
187 case Instruction::INSTANCE_OF:
buzbee02031b12012-11-23 09:41:35 -0800188 cg->GenInstanceof(cu, vC, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 break;
190
191 case Instruction::NEW_INSTANCE:
buzbee02031b12012-11-23 09:41:35 -0800192 cg->GenNewInstance(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700193 break;
194
195 case Instruction::THROW:
buzbee02031b12012-11-23 09:41:35 -0800196 cg->GenThrow(cu, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 break;
198
Bill Buzbeea114add2012-05-03 15:00:40 -0700199 case Instruction::ARRAY_LENGTH:
buzbeefa57c472012-11-21 12:06:18 -0800200 int len_offset;
201 len_offset = Array::LengthOffset().Int32Value();
buzbee02031b12012-11-23 09:41:35 -0800202 rl_src[0] = cg->LoadValue(cu, rl_src[0], kCoreReg);
203 cg->GenNullCheck(cu, rl_src[0].s_reg_low, rl_src[0].low_reg, opt_flags);
buzbeefa57c472012-11-21 12:06:18 -0800204 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
buzbee02031b12012-11-23 09:41:35 -0800205 cg->LoadWordDisp(cu, rl_src[0].low_reg, len_offset, rl_result.low_reg);
206 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 break;
208
209 case Instruction::CONST_STRING:
210 case Instruction::CONST_STRING_JUMBO:
buzbee02031b12012-11-23 09:41:35 -0800211 cg->GenConstString(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 break;
213
214 case Instruction::CONST_CLASS:
buzbee02031b12012-11-23 09:41:35 -0800215 cg->GenConstClass(cu, vB, rl_dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700216 break;
217
218 case Instruction::FILL_ARRAY_DATA:
buzbee02031b12012-11-23 09:41:35 -0800219 cg->GenFillArrayData(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 break;
221
222 case Instruction::FILLED_NEW_ARRAY:
buzbee02031b12012-11-23 09:41:35 -0800223 cg->GenFilledNewArray(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic,
buzbee3b3dbdd2012-06-13 13:39:34 -0700224 false /* not range */));
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 break;
226
227 case Instruction::FILLED_NEW_ARRAY_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800228 cg->GenFilledNewArray(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic,
buzbee3b3dbdd2012-06-13 13:39:34 -0700229 true /* range */));
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 break;
231
232 case Instruction::NEW_ARRAY:
buzbee02031b12012-11-23 09:41:35 -0800233 cg->GenNewArray(cu, vC, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 break;
235
236 case Instruction::GOTO:
237 case Instruction::GOTO_16:
238 case Instruction::GOTO_32:
buzbeefa57c472012-11-21 12:06:18 -0800239 if (bb->taken->start_offset <= mir->offset) {
buzbee02031b12012-11-23 09:41:35 -0800240 cg->GenSuspendTestAndBranch(cu, opt_flags, &label_list[bb->taken->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 } else {
buzbee02031b12012-11-23 09:41:35 -0800242 cg->OpUnconditionalBranch(cu, &label_list[bb->taken->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 }
244 break;
245
246 case Instruction::PACKED_SWITCH:
buzbee02031b12012-11-23 09:41:35 -0800247 cg->GenPackedSwitch(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 break;
249
250 case Instruction::SPARSE_SWITCH:
buzbee02031b12012-11-23 09:41:35 -0800251 cg->GenSparseSwitch(cu, vB, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 break;
253
254 case Instruction::CMPL_FLOAT:
255 case Instruction::CMPG_FLOAT:
256 case Instruction::CMPL_DOUBLE:
257 case Instruction::CMPG_DOUBLE:
buzbee02031b12012-11-23 09:41:35 -0800258 res = cg->GenCmpFP(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 break;
260
261 case Instruction::CMP_LONG:
buzbee02031b12012-11-23 09:41:35 -0800262 cg->GenCmpLong(cu, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 break;
264
265 case Instruction::IF_EQ:
266 case Instruction::IF_NE:
267 case Instruction::IF_LT:
268 case Instruction::IF_GE:
269 case Instruction::IF_GT:
270 case Instruction::IF_LE: {
buzbeefa57c472012-11-21 12:06:18 -0800271 LIR* taken = &label_list[bb->taken->id];
272 LIR* fall_through = &label_list[bb->fall_through->id];
273 bool backward_branch;
274 backward_branch = (bb->taken->start_offset <= mir->offset);
275 if (backward_branch) {
buzbee02031b12012-11-23 09:41:35 -0800276 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 }
buzbee02031b12012-11-23 09:41:35 -0800278 cg->GenCompareAndBranch(cu, opcode, rl_src[0], rl_src[1], taken,
buzbeefa57c472012-11-21 12:06:18 -0800279 fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 break;
281 }
282
283 case Instruction::IF_EQZ:
284 case Instruction::IF_NEZ:
285 case Instruction::IF_LTZ:
286 case Instruction::IF_GEZ:
287 case Instruction::IF_GTZ:
288 case Instruction::IF_LEZ: {
buzbeefa57c472012-11-21 12:06:18 -0800289 LIR* taken = &label_list[bb->taken->id];
290 LIR* fall_through = &label_list[bb->fall_through->id];
291 bool backward_branch;
292 backward_branch = (bb->taken->start_offset <= mir->offset);
293 if (backward_branch) {
buzbee02031b12012-11-23 09:41:35 -0800294 cg->GenSuspendTest(cu, opt_flags);
Bill Buzbeea114add2012-05-03 15:00:40 -0700295 }
buzbee02031b12012-11-23 09:41:35 -0800296 cg->GenCompareZeroAndBranch(cu, opcode, rl_src[0], taken, fall_through);
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 break;
298 }
299
300 case Instruction::AGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800301 cg->GenArrayGet(cu, opt_flags, kLong, rl_src[0], rl_src[1], rl_dest, 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 break;
303 case Instruction::AGET:
304 case Instruction::AGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800305 cg->GenArrayGet(cu, opt_flags, kWord, rl_src[0], rl_src[1], rl_dest, 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 break;
307 case Instruction::AGET_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800308 cg->GenArrayGet(cu, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700309 break;
310 case Instruction::AGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800311 cg->GenArrayGet(cu, opt_flags, kSignedByte, rl_src[0], rl_src[1], rl_dest, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 break;
313 case Instruction::AGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800314 cg->GenArrayGet(cu, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 break;
316 case Instruction::AGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800317 cg->GenArrayGet(cu, opt_flags, kSignedHalf, rl_src[0], rl_src[1], rl_dest, 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 break;
319 case Instruction::APUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800320 cg->GenArrayPut(cu, opt_flags, kLong, rl_src[1], rl_src[2], rl_src[0], 3);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 break;
322 case Instruction::APUT:
buzbee02031b12012-11-23 09:41:35 -0800323 cg->GenArrayPut(cu, opt_flags, kWord, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 break;
325 case Instruction::APUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800326 cg->GenArrayObjPut(cu, opt_flags, rl_src[1], rl_src[2], rl_src[0], 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700327 break;
328 case Instruction::APUT_SHORT:
329 case Instruction::APUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800330 cg->GenArrayPut(cu, opt_flags, kUnsignedHalf, rl_src[1], rl_src[2], rl_src[0], 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 break;
332 case Instruction::APUT_BYTE:
333 case Instruction::APUT_BOOLEAN:
buzbee02031b12012-11-23 09:41:35 -0800334 cg->GenArrayPut(cu, opt_flags, kUnsignedByte, rl_src[1], rl_src[2],
buzbeefa57c472012-11-21 12:06:18 -0800335 rl_src[0], 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 break;
337
338 case Instruction::IGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800339 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700340 break;
341
342 case Instruction::IGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800343 cg->GenIGet(cu, vC, opt_flags, kLong, rl_dest, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 break;
345
346 case Instruction::IGET:
buzbee02031b12012-11-23 09:41:35 -0800347 cg->GenIGet(cu, vC, opt_flags, kWord, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 break;
349
350 case Instruction::IGET_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800351 cg->GenIGet(cu, vC, opt_flags, kUnsignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 break;
353
354 case Instruction::IGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800355 cg->GenIGet(cu, vC, opt_flags, kSignedHalf, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700356 break;
357
358 case Instruction::IGET_BOOLEAN:
359 case Instruction::IGET_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800360 cg->GenIGet(cu, vC, opt_flags, kUnsignedByte, rl_dest, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 break;
362
363 case Instruction::IPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800364 cg->GenIPut(cu, vC, opt_flags, kLong, rl_src[0], rl_src[1], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 break;
366
367 case Instruction::IPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800368 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 break;
370
371 case Instruction::IPUT:
buzbee02031b12012-11-23 09:41:35 -0800372 cg->GenIPut(cu, vC, opt_flags, kWord, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 break;
374
375 case Instruction::IPUT_BOOLEAN:
376 case Instruction::IPUT_BYTE:
buzbee02031b12012-11-23 09:41:35 -0800377 cg->GenIPut(cu, vC, opt_flags, kUnsignedByte, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 break;
379
380 case Instruction::IPUT_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800381 cg->GenIPut(cu, vC, opt_flags, kUnsignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 break;
383
384 case Instruction::IPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800385 cg->GenIPut(cu, vC, opt_flags, kSignedHalf, rl_src[0], rl_src[1], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700386 break;
387
388 case Instruction::SGET_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800389 cg->GenSget(cu, vB, rl_dest, false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 break;
391 case Instruction::SGET:
392 case Instruction::SGET_BOOLEAN:
393 case Instruction::SGET_BYTE:
394 case Instruction::SGET_CHAR:
395 case Instruction::SGET_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800396 cg->GenSget(cu, vB, rl_dest, false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700397 break;
398
399 case Instruction::SGET_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800400 cg->GenSget(cu, vB, rl_dest, true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700401 break;
402
403 case Instruction::SPUT_OBJECT:
buzbee02031b12012-11-23 09:41:35 -0800404 cg->GenSput(cu, vB, rl_src[0], false, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 break;
406
407 case Instruction::SPUT:
408 case Instruction::SPUT_BOOLEAN:
409 case Instruction::SPUT_BYTE:
410 case Instruction::SPUT_CHAR:
411 case Instruction::SPUT_SHORT:
buzbee02031b12012-11-23 09:41:35 -0800412 cg->GenSput(cu, vB, rl_src[0], false, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 break;
414
415 case Instruction::SPUT_WIDE:
buzbee02031b12012-11-23 09:41:35 -0800416 cg->GenSput(cu, vB, rl_src[0], true, false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 break;
418
419 case Instruction::INVOKE_STATIC_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800420 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 break;
422 case Instruction::INVOKE_STATIC:
buzbee02031b12012-11-23 09:41:35 -0800423 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kStatic, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 break;
425
426 case Instruction::INVOKE_DIRECT:
buzbee02031b12012-11-23 09:41:35 -0800427 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 break;
429 case Instruction::INVOKE_DIRECT_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800430 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kDirect, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 break;
432
433 case Instruction::INVOKE_VIRTUAL:
buzbee02031b12012-11-23 09:41:35 -0800434 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 break;
436 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800437 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kVirtual, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 break;
439
440 case Instruction::INVOKE_SUPER:
buzbee02031b12012-11-23 09:41:35 -0800441 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700442 break;
443 case Instruction::INVOKE_SUPER_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800444 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kSuper, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 break;
446
447 case Instruction::INVOKE_INTERFACE:
buzbee02031b12012-11-23 09:41:35 -0800448 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 break;
450 case Instruction::INVOKE_INTERFACE_RANGE:
buzbee02031b12012-11-23 09:41:35 -0800451 cg->GenInvoke(cu, cg->NewMemCallInfo(cu, bb, mir, kInterface, true));
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 break;
453
454 case Instruction::NEG_INT:
455 case Instruction::NOT_INT:
buzbee02031b12012-11-23 09:41:35 -0800456 res = cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 break;
458
459 case Instruction::NEG_LONG:
460 case Instruction::NOT_LONG:
buzbee02031b12012-11-23 09:41:35 -0800461 res = cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 break;
463
464 case Instruction::NEG_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800465 res = cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700466 break;
467
468 case Instruction::NEG_DOUBLE:
buzbee02031b12012-11-23 09:41:35 -0800469 res = cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700470 break;
471
472 case Instruction::INT_TO_LONG:
buzbee02031b12012-11-23 09:41:35 -0800473 cg->GenIntToLong(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 break;
475
476 case Instruction::LONG_TO_INT:
buzbeefa57c472012-11-21 12:06:18 -0800477 rl_src[0] = UpdateLocWide(cu, rl_src[0]);
478 rl_src[0] = WideToNarrow(cu, rl_src[0]);
buzbee02031b12012-11-23 09:41:35 -0800479 cg->StoreValue(cu, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 break;
481
482 case Instruction::INT_TO_BYTE:
483 case Instruction::INT_TO_SHORT:
484 case Instruction::INT_TO_CHAR:
buzbee02031b12012-11-23 09:41:35 -0800485 cg->GenIntNarrowing(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 break;
487
488 case Instruction::INT_TO_FLOAT:
489 case Instruction::INT_TO_DOUBLE:
490 case Instruction::LONG_TO_FLOAT:
491 case Instruction::LONG_TO_DOUBLE:
492 case Instruction::FLOAT_TO_INT:
493 case Instruction::FLOAT_TO_LONG:
494 case Instruction::FLOAT_TO_DOUBLE:
495 case Instruction::DOUBLE_TO_INT:
496 case Instruction::DOUBLE_TO_LONG:
497 case Instruction::DOUBLE_TO_FLOAT:
buzbee02031b12012-11-23 09:41:35 -0800498 cg->GenConversion(cu, opcode, rl_dest, rl_src[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 break;
500
501 case Instruction::ADD_INT:
502 case Instruction::SUB_INT:
503 case Instruction::MUL_INT:
504 case Instruction::DIV_INT:
505 case Instruction::REM_INT:
506 case Instruction::AND_INT:
507 case Instruction::OR_INT:
508 case Instruction::XOR_INT:
509 case Instruction::SHL_INT:
510 case Instruction::SHR_INT:
511 case Instruction::USHR_INT:
512 case Instruction::ADD_INT_2ADDR:
513 case Instruction::SUB_INT_2ADDR:
514 case Instruction::MUL_INT_2ADDR:
515 case Instruction::DIV_INT_2ADDR:
516 case Instruction::REM_INT_2ADDR:
517 case Instruction::AND_INT_2ADDR:
518 case Instruction::OR_INT_2ADDR:
519 case Instruction::XOR_INT_2ADDR:
520 case Instruction::SHL_INT_2ADDR:
521 case Instruction::SHR_INT_2ADDR:
522 case Instruction::USHR_INT_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800523 cg->GenArithOpInt(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 break;
525
526 case Instruction::ADD_LONG:
527 case Instruction::SUB_LONG:
528 case Instruction::MUL_LONG:
529 case Instruction::DIV_LONG:
530 case Instruction::REM_LONG:
531 case Instruction::AND_LONG:
532 case Instruction::OR_LONG:
533 case Instruction::XOR_LONG:
534 case Instruction::ADD_LONG_2ADDR:
535 case Instruction::SUB_LONG_2ADDR:
536 case Instruction::MUL_LONG_2ADDR:
537 case Instruction::DIV_LONG_2ADDR:
538 case Instruction::REM_LONG_2ADDR:
539 case Instruction::AND_LONG_2ADDR:
540 case Instruction::OR_LONG_2ADDR:
541 case Instruction::XOR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800542 cg->GenArithOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700543 break;
544
545 case Instruction::SHL_LONG:
546 case Instruction::SHR_LONG:
547 case Instruction::USHR_LONG:
548 case Instruction::SHL_LONG_2ADDR:
549 case Instruction::SHR_LONG_2ADDR:
550 case Instruction::USHR_LONG_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800551 cg->GenShiftOpLong(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700552 break;
553
554 case Instruction::ADD_FLOAT:
555 case Instruction::SUB_FLOAT:
556 case Instruction::MUL_FLOAT:
557 case Instruction::DIV_FLOAT:
558 case Instruction::REM_FLOAT:
559 case Instruction::ADD_FLOAT_2ADDR:
560 case Instruction::SUB_FLOAT_2ADDR:
561 case Instruction::MUL_FLOAT_2ADDR:
562 case Instruction::DIV_FLOAT_2ADDR:
563 case Instruction::REM_FLOAT_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800564 cg->GenArithOpFloat(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 break;
566
567 case Instruction::ADD_DOUBLE:
568 case Instruction::SUB_DOUBLE:
569 case Instruction::MUL_DOUBLE:
570 case Instruction::DIV_DOUBLE:
571 case Instruction::REM_DOUBLE:
572 case Instruction::ADD_DOUBLE_2ADDR:
573 case Instruction::SUB_DOUBLE_2ADDR:
574 case Instruction::MUL_DOUBLE_2ADDR:
575 case Instruction::DIV_DOUBLE_2ADDR:
576 case Instruction::REM_DOUBLE_2ADDR:
buzbee02031b12012-11-23 09:41:35 -0800577 cg->GenArithOpDouble(cu, opcode, rl_dest, rl_src[0], rl_src[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700578 break;
579
580 case Instruction::RSUB_INT:
581 case Instruction::ADD_INT_LIT16:
582 case Instruction::MUL_INT_LIT16:
583 case Instruction::DIV_INT_LIT16:
584 case Instruction::REM_INT_LIT16:
585 case Instruction::AND_INT_LIT16:
586 case Instruction::OR_INT_LIT16:
587 case Instruction::XOR_INT_LIT16:
588 case Instruction::ADD_INT_LIT8:
589 case Instruction::RSUB_INT_LIT8:
590 case Instruction::MUL_INT_LIT8:
591 case Instruction::DIV_INT_LIT8:
592 case Instruction::REM_INT_LIT8:
593 case Instruction::AND_INT_LIT8:
594 case Instruction::OR_INT_LIT8:
595 case Instruction::XOR_INT_LIT8:
596 case Instruction::SHL_INT_LIT8:
597 case Instruction::SHR_INT_LIT8:
598 case Instruction::USHR_INT_LIT8:
buzbee02031b12012-11-23 09:41:35 -0800599 cg->GenArithOpIntLit(cu, opcode, rl_dest, rl_src[0], vC);
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 break;
601
602 default:
603 res = true;
604 }
605 return res;
buzbeee3acd072012-02-25 17:03:10 -0800606}
607
buzbee02031b12012-11-23 09:41:35 -0800608// Process extended MIR instructions (such as PHI).
buzbeefa57c472012-11-21 12:06:18 -0800609static void HandleExtendedMethodMIR(CompilationUnit* cu, BasicBlock* bb, MIR* mir)
buzbeee3acd072012-02-25 17:03:10 -0800610{
buzbee02031b12012-11-23 09:41:35 -0800611 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800612 int op_offset = mir->dalvikInsn.opcode - kMirOpFirst;
Bill Buzbeea114add2012-05-03 15:00:40 -0700613 char* msg = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800614 if (cu->verbose) {
615 msg = static_cast<char*>(NewMem(cu, strlen(extended_mir_op_names[op_offset]) + 1,
buzbeecbd6d442012-11-17 14:11:25 -0800616 false, kAllocDebugInfo));
buzbeefa57c472012-11-21 12:06:18 -0800617 strcpy(msg, extended_mir_op_names[op_offset]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 }
buzbeefa57c472012-11-21 12:06:18 -0800619 LIR* op = NewLIR1(cu, kPseudoExtended, reinterpret_cast<uintptr_t>(msg));
buzbeee3acd072012-02-25 17:03:10 -0800620
buzbeecbd6d442012-11-17 14:11:25 -0800621 switch (static_cast<ExtendedMIROpcode>(mir->dalvikInsn.opcode)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 case kMirOpPhi: {
buzbeefa57c472012-11-21 12:06:18 -0800623 char* ssa_string = NULL;
624 if (cu->verbose) {
625 ssa_string = GetSSAString(cu, mir->ssa_rep);
Bill Buzbeea114add2012-05-03 15:00:40 -0700626 }
buzbeefa57c472012-11-21 12:06:18 -0800627 op->flags.is_nop = true;
628 NewLIR1(cu, kPseudoSSARep, reinterpret_cast<uintptr_t>(ssa_string));
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 break;
buzbeee3acd072012-02-25 17:03:10 -0800630 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700631 case kMirOpCopy: {
buzbeefa57c472012-11-21 12:06:18 -0800632 RegLocation rl_src = GetSrc(cu, mir, 0);
633 RegLocation rl_dest = GetDest(cu, mir);
buzbee02031b12012-11-23 09:41:35 -0800634 cg->StoreValue(cu, rl_dest, rl_src);
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 break;
636 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 case kMirOpFusedCmplFloat:
buzbee02031b12012-11-23 09:41:35 -0800638 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 break;
640 case kMirOpFusedCmpgFloat:
buzbee02031b12012-11-23 09:41:35 -0800641 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, false /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 break;
643 case kMirOpFusedCmplDouble:
buzbee02031b12012-11-23 09:41:35 -0800644 cg->GenFusedFPCmpBranch(cu, bb, mir, false /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 break;
646 case kMirOpFusedCmpgDouble:
buzbee02031b12012-11-23 09:41:35 -0800647 cg->GenFusedFPCmpBranch(cu, bb, mir, true /*gt bias*/, true /*double*/);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 break;
649 case kMirOpFusedCmpLong:
buzbee02031b12012-11-23 09:41:35 -0800650 cg->GenFusedLongCmpBranch(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 default:
653 break;
654 }
buzbeee3acd072012-02-25 17:03:10 -0800655}
656
buzbee02031b12012-11-23 09:41:35 -0800657// Handle the content in each basic block.
buzbeefa57c472012-11-21 12:06:18 -0800658static bool MethodBlockCodeGen(CompilationUnit* cu, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800659{
buzbeefa57c472012-11-21 12:06:18 -0800660 if (bb->block_type == kDead) return false;
buzbee02031b12012-11-23 09:41:35 -0800661 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800662 cu->current_dalvik_offset = bb->start_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 MIR* mir;
buzbeefa57c472012-11-21 12:06:18 -0800664 LIR* label_list = cu->block_label_list;
665 int block_id = bb->id;
buzbeee3acd072012-02-25 17:03:10 -0800666
buzbeefa57c472012-11-21 12:06:18 -0800667 cu->cur_block = bb;
668 label_list[block_id].operands[0] = bb->start_offset;
buzbeee3acd072012-02-25 17:03:10 -0800669
buzbee02031b12012-11-23 09:41:35 -0800670 // Insert the block label.
buzbeefa57c472012-11-21 12:06:18 -0800671 label_list[block_id].opcode = kPseudoNormalBlockLabel;
672 AppendLIR(cu, &label_list[block_id]);
buzbeee3acd072012-02-25 17:03:10 -0800673
buzbeefa57c472012-11-21 12:06:18 -0800674 LIR* head_lir = NULL;
buzbee8320f382012-09-11 16:29:42 -0700675
buzbee02031b12012-11-23 09:41:35 -0800676 // If this is a catch block, export the start address.
buzbeefa57c472012-11-21 12:06:18 -0800677 if (bb->catch_entry) {
678 head_lir = NewLIR0(cu, kPseudoExportedPC);
buzbee8320f382012-09-11 16:29:42 -0700679 }
680
buzbee02031b12012-11-23 09:41:35 -0800681 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800682 ResetRegPool(cu);
683 ResetDefTracking(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700684
buzbeefa57c472012-11-21 12:06:18 -0800685 ClobberAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700686
buzbeefa57c472012-11-21 12:06:18 -0800687 if (bb->block_type == kEntryBlock) {
688 int start_vreg = cu->num_dalvik_registers - cu->num_ins;
buzbee02031b12012-11-23 09:41:35 -0800689 cg->GenEntrySequence(cu, &cu->reg_location[start_vreg],
690 cu->reg_location[cu->method_sreg]);
buzbeefa57c472012-11-21 12:06:18 -0800691 } else if (bb->block_type == kExitBlock) {
buzbee02031b12012-11-23 09:41:35 -0800692 cg->GenExitSequence(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700693 }
694
buzbee28c9a832012-11-21 15:39:13 -0800695 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeefa57c472012-11-21 12:06:18 -0800696 ResetRegPool(cu);
697 if (cu->disable_opt & (1 << kTrackLiveTemps)) {
698 ClobberAllRegs(cu);
buzbeee1965672012-03-11 18:39:19 -0700699 }
700
buzbeefa57c472012-11-21 12:06:18 -0800701 if (cu->disable_opt & (1 << kSuppressLoads)) {
702 ResetDefTracking(cu);
buzbeee3acd072012-02-25 17:03:10 -0800703 }
704
buzbee3d661942012-03-14 17:37:27 -0700705#ifndef NDEBUG
buzbee02031b12012-11-23 09:41:35 -0800706 // Reset temp tracking sanity check.
buzbeefa57c472012-11-21 12:06:18 -0800707 cu->live_sreg = INVALID_SREG;
buzbee3d661942012-03-14 17:37:27 -0700708#endif
709
buzbeefa57c472012-11-21 12:06:18 -0800710 cu->current_dalvik_offset = mir->offset;
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700711 int opcode = mir->dalvikInsn.opcode;
buzbeefa57c472012-11-21 12:06:18 -0800712 LIR* boundary_lir;
buzbeee3acd072012-02-25 17:03:10 -0800713
buzbee02031b12012-11-23 09:41:35 -0800714 // Mark the beginning of a Dalvik instruction for line tracking.
buzbeefa57c472012-11-21 12:06:18 -0800715 char* inst_str = cu->verbose ?
716 GetDalvikDisassembly(cu, mir->dalvikInsn, "") : NULL;
717 boundary_lir = MarkBoundary(cu, mir->offset, inst_str);
buzbee02031b12012-11-23 09:41:35 -0800718 // Remember the first LIR for this block.
buzbeefa57c472012-11-21 12:06:18 -0800719 if (head_lir == NULL) {
720 head_lir = boundary_lir;
buzbee02031b12012-11-23 09:41:35 -0800721 // Set the first boundary_lir as a scheduling barrier.
buzbeefa57c472012-11-21 12:06:18 -0800722 head_lir->def_mask = ENCODE_ALL;
buzbeee3acd072012-02-25 17:03:10 -0800723 }
724
buzbee02031b12012-11-23 09:41:35 -0800725 // Don't generate the SSA annotation unless verbose mode is on.
buzbeefa57c472012-11-21 12:06:18 -0800726 if (cu->verbose && mir->ssa_rep) {
727 char* ssa_string = GetSSAString(cu, mir->ssa_rep);
728 NewLIR1(cu, kPseudoSSARep, reinterpret_cast<uintptr_t>(ssa_string));
Bill Buzbeea114add2012-05-03 15:00:40 -0700729 }
730
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700731 if (opcode == kMirOpCheck) {
732 // Combine check and work halves of throwing instruction.
buzbeefa57c472012-11-21 12:06:18 -0800733 MIR* work_half = mir->meta.throw_insn;
734 mir->dalvikInsn.opcode = work_half->dalvikInsn.opcode;
735 opcode = work_half->dalvikInsn.opcode;
736 SSARepresentation* ssa_rep = work_half->ssa_rep;
737 work_half->ssa_rep = mir->ssa_rep;
738 mir->ssa_rep = ssa_rep;
739 work_half->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700740 }
741
742 if (opcode >= kMirOpFirst) {
buzbeefa57c472012-11-21 12:06:18 -0800743 HandleExtendedMethodMIR(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700744 continue;
745 }
746
buzbeefa57c472012-11-21 12:06:18 -0800747 bool not_handled = CompileDalvikInstruction(cu, mir, bb, label_list);
748 if (not_handled) {
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700749 LOG(FATAL) << StringPrintf("%#06x: Opcode %#x (%s)",
750 mir->offset, opcode,
751 Instruction::Name(mir->dalvikInsn.opcode));
Bill Buzbeea114add2012-05-03 15:00:40 -0700752 }
753 }
754
buzbeefa57c472012-11-21 12:06:18 -0800755 if (head_lir) {
buzbee02031b12012-11-23 09:41:35 -0800756 // Eliminate redundant loads/stores and delay stores into later slots.
buzbeefa57c472012-11-21 12:06:18 -0800757 ApplyLocalOptimizations(cu, head_lir, cu->last_lir_insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700758
buzbee02031b12012-11-23 09:41:35 -0800759 // Generate an unconditional branch to the fallthrough block.
buzbeefa57c472012-11-21 12:06:18 -0800760 if (bb->fall_through) {
buzbee02031b12012-11-23 09:41:35 -0800761 cg->OpUnconditionalBranch(cu, &label_list[bb->fall_through->id]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700762 }
763 }
764 return false;
buzbeee3acd072012-02-25 17:03:10 -0800765}
766
buzbeefa57c472012-11-21 12:06:18 -0800767void SpecialMIR2LIR(CompilationUnit* cu, SpecialCaseHandler special_case)
buzbee16da88c2012-03-20 10:38:17 -0700768{
buzbee02031b12012-11-23 09:41:35 -0800769 Codegen* cg = cu->cg.get();
770 // Find the first DalvikByteCode block.
buzbeefa57c472012-11-21 12:06:18 -0800771 int num_reachable_blocks = cu->num_reachable_blocks;
772 const GrowableList *block_list = &cu->block_list;
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 BasicBlock*bb = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800774 for (int idx = 0; idx < num_reachable_blocks; idx++) {
775 int dfs_index = cu->dfs_order.elem_list[idx];
776 bb = reinterpret_cast<BasicBlock*>(GrowableListGetElement(block_list, dfs_index));
777 if (bb->block_type == kDalvikByteCode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700778 break;
buzbee16da88c2012-03-20 10:38:17 -0700779 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700780 }
781 if (bb == NULL) {
782 return;
783 }
buzbeefa57c472012-11-21 12:06:18 -0800784 DCHECK_EQ(bb->start_offset, 0);
785 DCHECK(bb->first_mir_insn != NULL);
buzbee16da88c2012-03-20 10:38:17 -0700786
buzbee02031b12012-11-23 09:41:35 -0800787 // Get the first instruction.
buzbeefa57c472012-11-21 12:06:18 -0800788 MIR* mir = bb->first_mir_insn;
buzbee16da88c2012-03-20 10:38:17 -0700789
buzbee02031b12012-11-23 09:41:35 -0800790 // Free temp registers and reset redundant store tracking.
buzbeefa57c472012-11-21 12:06:18 -0800791 ResetRegPool(cu);
792 ResetDefTracking(cu);
793 ClobberAllRegs(cu);
buzbee16da88c2012-03-20 10:38:17 -0700794
buzbee02031b12012-11-23 09:41:35 -0800795 cg->GenSpecialCase(cu, bb, mir, special_case);
buzbee16da88c2012-03-20 10:38:17 -0700796}
797
buzbeefa57c472012-11-21 12:06:18 -0800798void MethodMIR2LIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800799{
buzbee02031b12012-11-23 09:41:35 -0800800 Codegen* cg = cu->cg.get();
801 // Hold the labels of each block.
buzbeefa57c472012-11-21 12:06:18 -0800802 cu->block_label_list =
803 static_cast<LIR*>(NewMem(cu, sizeof(LIR) * cu->num_blocks, true, kAllocLIR));
buzbeee3acd072012-02-25 17:03:10 -0800804
buzbeefa57c472012-11-21 12:06:18 -0800805 DataFlowAnalysisDispatcher(cu, MethodBlockCodeGen,
Bill Buzbeea114add2012-05-03 15:00:40 -0700806 kPreOrderDFSTraversal, false /* Iterative */);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700807
buzbee02031b12012-11-23 09:41:35 -0800808 cg->HandleSuspendLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800809
buzbee02031b12012-11-23 09:41:35 -0800810 cg->HandleThrowLaunchPads(cu);
buzbeee3acd072012-02-25 17:03:10 -0800811
buzbee02031b12012-11-23 09:41:35 -0800812 cg->HandleIntrinsicLaunchPads(cu);
buzbeefc9e6fa2012-03-23 15:14:29 -0700813
buzbeefa57c472012-11-21 12:06:18 -0800814 if (!(cu->disable_opt & (1 << kSafeOptimizations))) {
815 RemoveRedundantBranches(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700816 }
buzbeee3acd072012-02-25 17:03:10 -0800817}
818
buzbeee3acd072012-02-25 17:03:10 -0800819} // namespace art