blob: c2e663ee46a1d6305189cd34027574c19d9d7a1f [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbeeefc63692012-11-14 16:31:52 -080017#include "compiler_internals.h"
18#include "dataflow.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080019#include "codegen/ralloc_util.h"
buzbee67bf8852011-08-17 17:51:35 -070020
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080021namespace art {
22
buzbeefa57c472012-11-21 12:06:18 -080023static bool SetFp(CompilationUnit* cu, int index, bool is_fp) {
Bill Buzbeea114add2012-05-03 15:00:40 -070024 bool change = false;
buzbeefa57c472012-11-21 12:06:18 -080025 if (is_fp && !cu->reg_location[index].fp) {
26 cu->reg_location[index].fp = true;
27 cu->reg_location[index].defined = true;
Bill Buzbeea114add2012-05-03 15:00:40 -070028 change = true;
29 }
30 return change;
buzbee67bc2362011-10-11 18:08:40 -070031}
32
buzbeefa57c472012-11-21 12:06:18 -080033static bool SetCore(CompilationUnit* cu, int index, bool is_core) {
Bill Buzbeea114add2012-05-03 15:00:40 -070034 bool change = false;
buzbeefa57c472012-11-21 12:06:18 -080035 if (is_core && !cu->reg_location[index].defined) {
36 cu->reg_location[index].core = true;
37 cu->reg_location[index].defined = true;
Bill Buzbeea114add2012-05-03 15:00:40 -070038 change = true;
39 }
40 return change;
buzbee03fa2632011-09-20 17:10:57 -070041}
42
buzbeefa57c472012-11-21 12:06:18 -080043static bool SetRef(CompilationUnit* cu, int index, bool is_ref) {
buzbeebff24652012-05-06 16:22:05 -070044 bool change = false;
buzbeefa57c472012-11-21 12:06:18 -080045 if (is_ref && !cu->reg_location[index].defined) {
46 cu->reg_location[index].ref = true;
47 cu->reg_location[index].defined = true;
buzbeebff24652012-05-06 16:22:05 -070048 change = true;
49 }
50 return change;
51}
52
buzbeefa57c472012-11-21 12:06:18 -080053static bool SetWide(CompilationUnit* cu, int index, bool is_wide) {
buzbee2a83e8f2012-07-13 16:42:30 -070054 bool change = false;
buzbeefa57c472012-11-21 12:06:18 -080055 if (is_wide && !cu->reg_location[index].wide) {
56 cu->reg_location[index].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -070057 change = true;
58 }
59 return change;
60}
61
buzbeefa57c472012-11-21 12:06:18 -080062static bool SetHigh(CompilationUnit* cu, int index, bool is_high) {
buzbee2a83e8f2012-07-13 16:42:30 -070063 bool change = false;
buzbeefa57c472012-11-21 12:06:18 -080064 if (is_high && !cu->reg_location[index].high_word) {
65 cu->reg_location[index].high_word = true;
buzbee2a83e8f2012-07-13 16:42:30 -070066 change = true;
67 }
68 return change;
69}
70
buzbeefa57c472012-11-21 12:06:18 -080071static bool RemapNames(CompilationUnit* cu, BasicBlock* bb)
buzbeec0ecd652011-09-25 18:11:54 -070072{
buzbeefa57c472012-11-21 12:06:18 -080073 if (bb->block_type != kDalvikByteCode && bb->block_type != kEntryBlock &&
74 bb->block_type != kExitBlock)
buzbeec0ecd652011-09-25 18:11:54 -070075 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -070076
buzbee28c9a832012-11-21 15:39:13 -080077 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeefa57c472012-11-21 12:06:18 -080078 SSARepresentation *ssa_rep = mir->ssa_rep;
79 if (ssa_rep) {
80 for (int i = 0; i < ssa_rep->num_uses; i++) {
81 ssa_rep->uses[i] = cu->phi_alias_map[ssa_rep->uses[i]];
Bill Buzbeea114add2012-05-03 15:00:40 -070082 }
buzbeefa57c472012-11-21 12:06:18 -080083 for (int i = 0; i < ssa_rep->num_defs; i++) {
84 ssa_rep->defs[i] = cu->phi_alias_map[ssa_rep->defs[i]];
Bill Buzbeea114add2012-05-03 15:00:40 -070085 }
86 }
87 }
88 return false;
buzbeec0ecd652011-09-25 18:11:54 -070089}
90
buzbee67bf8852011-08-17 17:51:35 -070091/*
buzbee03fa2632011-09-20 17:10:57 -070092 * Infer types and sizes. We don't need to track change on sizes,
93 * as it doesn't propagate. We're guaranteed at least one pass through
94 * the cfg.
buzbee67bf8852011-08-17 17:51:35 -070095 */
buzbeefa57c472012-11-21 12:06:18 -080096static bool InferTypeAndSize(CompilationUnit* cu, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -070097{
Bill Buzbeea114add2012-05-03 15:00:40 -070098 MIR *mir;
99 bool changed = false; // Did anything change?
buzbee03fa2632011-09-20 17:10:57 -0700100
buzbeefa57c472012-11-21 12:06:18 -0800101 if (bb->data_flow_info == NULL) return false;
102 if (bb->block_type != kDalvikByteCode && bb->block_type != kEntryBlock)
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 return false;
buzbee67bf8852011-08-17 17:51:35 -0700104
buzbee28c9a832012-11-21 15:39:13 -0800105 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeefa57c472012-11-21 12:06:18 -0800106 SSARepresentation *ssa_rep = mir->ssa_rep;
107 if (ssa_rep) {
108 int attrs = oat_data_flow_attributes[mir->dalvikInsn.opcode];
buzbee67bc2362011-10-11 18:08:40 -0700109
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 // Handle defs
buzbeebff24652012-05-06 16:22:05 -0700111 if (attrs & DF_DA) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 if (attrs & DF_CORE_A) {
buzbeefa57c472012-11-21 12:06:18 -0800113 changed |= SetCore(cu, ssa_rep->defs[0], true);
buzbee67bf8852011-08-17 17:51:35 -0700114 }
buzbeebff24652012-05-06 16:22:05 -0700115 if (attrs & DF_REF_A) {
buzbeefa57c472012-11-21 12:06:18 -0800116 changed |= SetRef(cu, ssa_rep->defs[0], true);
buzbeebff24652012-05-06 16:22:05 -0700117 }
118 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800119 cu->reg_location[ssa_rep->defs[0]].wide = true;
120 cu->reg_location[ssa_rep->defs[1]].wide = true;
121 cu->reg_location[ssa_rep->defs[1]].high_word = true;
122 DCHECK_EQ(SRegToVReg(cu, ssa_rep->defs[0])+1,
123 SRegToVReg(cu, ssa_rep->defs[1]));
Bill Buzbeea114add2012-05-03 15:00:40 -0700124 }
125 }
126
127 // Handles uses
128 int next = 0;
buzbeebff24652012-05-06 16:22:05 -0700129 if (attrs & DF_UA) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 if (attrs & DF_CORE_A) {
buzbeefa57c472012-11-21 12:06:18 -0800131 changed |= SetCore(cu, ssa_rep->uses[next], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 }
buzbeebff24652012-05-06 16:22:05 -0700133 if (attrs & DF_REF_A) {
buzbeefa57c472012-11-21 12:06:18 -0800134 changed |= SetRef(cu, ssa_rep->uses[next], true);
buzbeebff24652012-05-06 16:22:05 -0700135 }
136 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800137 cu->reg_location[ssa_rep->uses[next]].wide = true;
138 cu->reg_location[ssa_rep->uses[next + 1]].wide = true;
139 cu->reg_location[ssa_rep->uses[next + 1]].high_word = true;
140 DCHECK_EQ(SRegToVReg(cu, ssa_rep->uses[next])+1,
141 SRegToVReg(cu, ssa_rep->uses[next + 1]));
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 next += 2;
143 } else {
144 next++;
145 }
146 }
buzbeebff24652012-05-06 16:22:05 -0700147 if (attrs & DF_UB) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 if (attrs & DF_CORE_B) {
buzbeefa57c472012-11-21 12:06:18 -0800149 changed |= SetCore(cu, ssa_rep->uses[next], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 }
buzbeebff24652012-05-06 16:22:05 -0700151 if (attrs & DF_REF_B) {
buzbeefa57c472012-11-21 12:06:18 -0800152 changed |= SetRef(cu, ssa_rep->uses[next], true);
buzbeebff24652012-05-06 16:22:05 -0700153 }
154 if (attrs & DF_B_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800155 cu->reg_location[ssa_rep->uses[next]].wide = true;
156 cu->reg_location[ssa_rep->uses[next + 1]].wide = true;
157 cu->reg_location[ssa_rep->uses[next + 1]].high_word = true;
158 DCHECK_EQ(SRegToVReg(cu, ssa_rep->uses[next])+1,
159 SRegToVReg(cu, ssa_rep->uses[next + 1]));
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 next += 2;
161 } else {
162 next++;
163 }
164 }
buzbeebff24652012-05-06 16:22:05 -0700165 if (attrs & DF_UC) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 if (attrs & DF_CORE_C) {
buzbeefa57c472012-11-21 12:06:18 -0800167 changed |= SetCore(cu, ssa_rep->uses[next], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 }
buzbeebff24652012-05-06 16:22:05 -0700169 if (attrs & DF_REF_C) {
buzbeefa57c472012-11-21 12:06:18 -0800170 changed |= SetRef(cu, ssa_rep->uses[next], true);
buzbeebff24652012-05-06 16:22:05 -0700171 }
172 if (attrs & DF_C_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800173 cu->reg_location[ssa_rep->uses[next]].wide = true;
174 cu->reg_location[ssa_rep->uses[next + 1]].wide = true;
175 cu->reg_location[ssa_rep->uses[next + 1]].high_word = true;
176 DCHECK_EQ(SRegToVReg(cu, ssa_rep->uses[next])+1,
177 SRegToVReg(cu, ssa_rep->uses[next + 1]));
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 }
179 }
180
buzbeed5018892012-07-11 14:23:40 -0700181 // Special-case return handling
182 if ((mir->dalvikInsn.opcode == Instruction::RETURN) ||
183 (mir->dalvikInsn.opcode == Instruction::RETURN_WIDE) ||
184 (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT)) {
buzbeefa57c472012-11-21 12:06:18 -0800185 switch(cu->shorty[0]) {
buzbeed5018892012-07-11 14:23:40 -0700186 case 'I':
buzbeefa57c472012-11-21 12:06:18 -0800187 changed |= SetCore(cu, ssa_rep->uses[0], true);
buzbeed5018892012-07-11 14:23:40 -0700188 break;
189 case 'J':
buzbeefa57c472012-11-21 12:06:18 -0800190 changed |= SetCore(cu, ssa_rep->uses[0], true);
191 changed |= SetCore(cu, ssa_rep->uses[1], true);
192 cu->reg_location[ssa_rep->uses[0]].wide = true;
193 cu->reg_location[ssa_rep->uses[1]].wide = true;
194 cu->reg_location[ssa_rep->uses[1]].high_word = true;
buzbeed5018892012-07-11 14:23:40 -0700195 break;
196 case 'F':
buzbeefa57c472012-11-21 12:06:18 -0800197 changed |= SetFp(cu, ssa_rep->uses[0], true);
buzbeed5018892012-07-11 14:23:40 -0700198 break;
199 case 'D':
buzbeefa57c472012-11-21 12:06:18 -0800200 changed |= SetFp(cu, ssa_rep->uses[0], true);
201 changed |= SetFp(cu, ssa_rep->uses[1], true);
202 cu->reg_location[ssa_rep->uses[0]].wide = true;
203 cu->reg_location[ssa_rep->uses[1]].wide = true;
204 cu->reg_location[ssa_rep->uses[1]].high_word = true;
buzbeed5018892012-07-11 14:23:40 -0700205 break;
206 case 'L':
buzbeefa57c472012-11-21 12:06:18 -0800207 changed |= SetRef(cu, ssa_rep->uses[0], true);
buzbeed5018892012-07-11 14:23:40 -0700208 break;
209 default: break;
210 }
211 }
212
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 // Special-case handling for format 35c/3rc invokes
214 Instruction::Code opcode = mir->dalvikInsn.opcode;
215 int flags = (static_cast<int>(opcode) >= kNumPackedOpcodes)
Ian Rogersa75a0132012-09-28 11:41:42 -0700216 ? 0 : Instruction::FlagsOf(mir->dalvikInsn.opcode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700217 if ((flags & Instruction::kInvoke) &&
218 (attrs & (DF_FORMAT_35C | DF_FORMAT_3RC))) {
219 DCHECK_EQ(next, 0);
220 int target_idx = mir->dalvikInsn.vB;
buzbeefa57c472012-11-21 12:06:18 -0800221 const char* shorty = GetShortyFromTargetIdx(cu, target_idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 // Handle result type if floating point
223 if ((shorty[0] == 'F') || (shorty[0] == 'D')) {
buzbeefa57c472012-11-21 12:06:18 -0800224 MIR* move_result_mir = FindMoveResult(cu, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 // Result might not be used at all, so no move-result
buzbeefa57c472012-11-21 12:06:18 -0800226 if (move_result_mir && (move_result_mir->dalvikInsn.opcode !=
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700227 Instruction::MOVE_RESULT_OBJECT)) {
buzbeefa57c472012-11-21 12:06:18 -0800228 SSARepresentation* tgt_rep = move_result_mir->ssa_rep;
229 DCHECK(tgt_rep != NULL);
230 tgt_rep->fp_def[0] = true;
231 changed |= SetFp(cu, tgt_rep->defs[0], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 if (shorty[0] == 'D') {
buzbeefa57c472012-11-21 12:06:18 -0800233 tgt_rep->fp_def[1] = true;
234 changed |= SetFp(cu, tgt_rep->defs[1], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 }
236 }
237 }
buzbeefa57c472012-11-21 12:06:18 -0800238 int num_uses = mir->dalvikInsn.vA;
buzbeebff24652012-05-06 16:22:05 -0700239 // If this is a non-static invoke, mark implicit "this"
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 if (((mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC) &&
241 (mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC_RANGE))) {
buzbeefa57c472012-11-21 12:06:18 -0800242 cu->reg_location[ssa_rep->uses[next]].defined = true;
243 cu->reg_location[ssa_rep->uses[next]].ref = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 next++;
245 }
246 uint32_t cpos = 1;
247 if (strlen(shorty) > 1) {
buzbeefa57c472012-11-21 12:06:18 -0800248 for (int i = next; i < num_uses;) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 DCHECK_LT(cpos, strlen(shorty));
250 switch (shorty[cpos++]) {
251 case 'D':
buzbeefa57c472012-11-21 12:06:18 -0800252 ssa_rep->fp_use[i] = true;
253 ssa_rep->fp_use[i+1] = true;
254 cu->reg_location[ssa_rep->uses[i]].wide = true;
255 cu->reg_location[ssa_rep->uses[i+1]].wide = true;
256 cu->reg_location[ssa_rep->uses[i+1]].high_word = true;
257 DCHECK_EQ(SRegToVReg(cu, ssa_rep->uses[i])+1,
258 SRegToVReg(cu, ssa_rep->uses[i+1]));
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 i++;
260 break;
261 case 'J':
buzbeefa57c472012-11-21 12:06:18 -0800262 cu->reg_location[ssa_rep->uses[i]].wide = true;
263 cu->reg_location[ssa_rep->uses[i+1]].wide = true;
264 cu->reg_location[ssa_rep->uses[i+1]].high_word = true;
265 DCHECK_EQ(SRegToVReg(cu, ssa_rep->uses[i])+1,
266 SRegToVReg(cu, ssa_rep->uses[i+1]));
267 changed |= SetCore(cu, ssa_rep->uses[i],true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 i++;
269 break;
270 case 'F':
buzbeefa57c472012-11-21 12:06:18 -0800271 ssa_rep->fp_use[i] = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 break;
buzbeebff24652012-05-06 16:22:05 -0700273 case 'L':
buzbeefa57c472012-11-21 12:06:18 -0800274 changed |= SetRef(cu,ssa_rep->uses[i], true);
buzbeebff24652012-05-06 16:22:05 -0700275 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 default:
buzbeefa57c472012-11-21 12:06:18 -0800277 changed |= SetCore(cu,ssa_rep->uses[i], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 break;
279 }
280 i++;
281 }
282 }
283 }
284
buzbeefa57c472012-11-21 12:06:18 -0800285 for (int i=0; ssa_rep->fp_use && i< ssa_rep->num_uses; i++) {
286 if (ssa_rep->fp_use[i])
287 changed |= SetFp(cu, ssa_rep->uses[i], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 }
buzbeefa57c472012-11-21 12:06:18 -0800289 for (int i=0; ssa_rep->fp_def && i< ssa_rep->num_defs; i++) {
290 if (ssa_rep->fp_def[i])
291 changed |= SetFp(cu, ssa_rep->defs[i], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 }
293 // Special-case handling for moves & Phi
294 if (attrs & (DF_IS_MOVE | DF_NULL_TRANSFER_N)) {
buzbee2a83e8f2012-07-13 16:42:30 -0700295 /*
296 * If any of our inputs or outputs is defined, set all.
297 * Some ugliness related to Phi nodes and wide values.
298 * The Phi set will include all low words or all high
299 * words, so we have to treat them specially.
300 */
buzbeefa57c472012-11-21 12:06:18 -0800301 bool is_phi = (static_cast<int>(mir->dalvikInsn.opcode) ==
buzbee2a83e8f2012-07-13 16:42:30 -0700302 kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800303 RegLocation rl_temp = cu->reg_location[ssa_rep->defs[0]];
304 bool defined_fp = rl_temp.defined && rl_temp.fp;
305 bool defined_core = rl_temp.defined && rl_temp.core;
306 bool defined_ref = rl_temp.defined && rl_temp.ref;
307 bool is_wide = rl_temp.wide || ((attrs & DF_A_WIDE) != 0);
308 bool is_high = is_phi && rl_temp.wide && rl_temp.high_word;
309 for (int i = 0; i < ssa_rep->num_uses;i++) {
310 rl_temp = cu->reg_location[ssa_rep->uses[i]];
311 defined_fp |= rl_temp.defined && rl_temp.fp;
312 defined_core |= rl_temp.defined && rl_temp.core;
313 defined_ref |= rl_temp.defined && rl_temp.ref;
314 is_wide |= rl_temp.wide;
315 is_high |= is_phi && rl_temp.wide && rl_temp.high_word;
Bill Buzbeea114add2012-05-03 15:00:40 -0700316 }
317 /*
318 * TODO: cleaner fix
319 * We don't normally expect to see a Dalvik register
320 * definition used both as a floating point and core
321 * value. However, the instruction rewriting that occurs
322 * during verification can eliminate some type information,
323 * leaving us confused. The real fix here is either to
324 * add explicit type information to Dalvik byte codes,
325 * or to recognize THROW_VERIFICATION_ERROR as
326 * an unconditional branch and support dead code elimination.
327 * As a workaround we can detect this situation and
328 * disable register promotion (which is the only thing that
329 * relies on distinctions between core and fp usages.
330 */
buzbeefa57c472012-11-21 12:06:18 -0800331 if ((defined_fp && (defined_core | defined_ref)) &&
332 ((cu->disable_opt & (1 << kPromoteRegs)) == 0)) {
333 LOG(WARNING) << PrettyMethod(cu->method_idx, *cu->dex_file)
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 << " op at block " << bb->id
buzbeebff24652012-05-06 16:22:05 -0700335 << " has both fp and core/ref uses for same def.";
buzbeefa57c472012-11-21 12:06:18 -0800336 cu->disable_opt |= (1 << kPromoteRegs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 }
buzbeefa57c472012-11-21 12:06:18 -0800338 changed |= SetFp(cu, ssa_rep->defs[0], defined_fp);
339 changed |= SetCore(cu, ssa_rep->defs[0], defined_core);
340 changed |= SetRef(cu, ssa_rep->defs[0], defined_ref);
341 changed |= SetWide(cu, ssa_rep->defs[0], is_wide);
342 changed |= SetHigh(cu, ssa_rep->defs[0], is_high);
buzbee2a83e8f2012-07-13 16:42:30 -0700343 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800344 changed |= SetWide(cu, ssa_rep->defs[1], true);
345 changed |= SetHigh(cu, ssa_rep->defs[1], true);
buzbee2a83e8f2012-07-13 16:42:30 -0700346 }
buzbeefa57c472012-11-21 12:06:18 -0800347 for (int i = 0; i < ssa_rep->num_uses; i++) {
348 changed |= SetFp(cu, ssa_rep->uses[i], defined_fp);
349 changed |= SetCore(cu, ssa_rep->uses[i], defined_core);
350 changed |= SetRef(cu, ssa_rep->uses[i], defined_ref);
351 changed |= SetWide(cu, ssa_rep->uses[i], is_wide);
352 changed |= SetHigh(cu, ssa_rep->uses[i], is_high);
buzbee2a83e8f2012-07-13 16:42:30 -0700353 }
354 if (attrs & DF_A_WIDE) {
buzbeefa57c472012-11-21 12:06:18 -0800355 DCHECK_EQ(ssa_rep->num_uses, 2);
356 changed |= SetWide(cu, ssa_rep->uses[1], true);
357 changed |= SetHigh(cu, ssa_rep->uses[1], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 }
359 }
buzbee67bf8852011-08-17 17:51:35 -0700360 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 }
362 return changed;
buzbee67bf8852011-08-17 17:51:35 -0700363}
364
buzbeefa57c472012-11-21 12:06:18 -0800365static const char* storage_name[] = {" Frame ", "PhysReg", " Spill "};
buzbee67bf8852011-08-17 17:51:35 -0700366
buzbee52a77fc2012-11-20 19:50:46 -0800367static void DumpRegLocTable(RegLocation* table, int count)
buzbee67bf8852011-08-17 17:51:35 -0700368{
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 for (int i = 0; i < count; i++) {
370 LOG(INFO) << StringPrintf("Loc[%02d] : %s, %c %c %c %c %c %c%d %c%d S%d",
buzbeefa57c472012-11-21 12:06:18 -0800371 table[i].orig_sreg, storage_name[table[i].location],
buzbeeca7a5e42012-08-20 11:12:18 -0700372 table[i].wide ? 'W' : 'N', table[i].defined ? 'D' : 'U',
buzbeed5018892012-07-11 14:23:40 -0700373 table[i].fp ? 'F' : table[i].ref ? 'R' :'C',
buzbeefa57c472012-11-21 12:06:18 -0800374 table[i].high_word ? 'H' : 'L', table[i].home ? 'h' : 't',
375 IsFpReg(table[i].low_reg) ? 's' : 'r',
376 table[i].low_reg & FpRegMask(),
377 IsFpReg(table[i].high_reg) ? 's' : 'r',
378 table[i].high_reg & FpRegMask(), table[i].s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 }
buzbee67bf8852011-08-17 17:51:35 -0700380}
381
buzbeefa57c472012-11-21 12:06:18 -0800382static const RegLocation fresh_loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
buzbee2cfc6392012-05-07 14:51:40 -0700383 INVALID_REG, INVALID_REG, INVALID_SREG,
384 INVALID_SREG};
buzbee67bf8852011-08-17 17:51:35 -0700385
buzbeefa57c472012-11-21 12:06:18 -0800386int ComputeFrameSize(CompilationUnit* cu) {
buzbeead8f15e2012-06-18 14:49:45 -0700387 /* Figure out the frame size */
388 static const uint32_t kAlignMask = kStackAlignment - 1;
buzbeefa57c472012-11-21 12:06:18 -0800389 uint32_t size = (cu->num_core_spills + cu->num_fp_spills +
390 1 /* filler word */ + cu->num_regs + cu->num_outs +
391 cu->num_compiler_temps + 1 /* cur_method* */)
buzbeead8f15e2012-06-18 14:49:45 -0700392 * sizeof(uint32_t);
393 /* Align and set */
394 return (size + kAlignMask) & ~(kAlignMask);
395}
396
buzbee67bf8852011-08-17 17:51:35 -0700397/*
398 * Simple register allocation. Some Dalvik virtual registers may
399 * be promoted to physical registers. Most of the work for temp
Ian Rogersb5d09b22012-03-06 22:14:17 -0800400 * allocation is done on the fly. We also do some initialization and
buzbee67bf8852011-08-17 17:51:35 -0700401 * type inference here.
402 */
buzbeefa57c472012-11-21 12:06:18 -0800403void SimpleRegAlloc(CompilationUnit* cu)
buzbee67bf8852011-08-17 17:51:35 -0700404{
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 int i;
406 RegLocation* loc;
buzbee67bf8852011-08-17 17:51:35 -0700407
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 /* Allocate the location map */
buzbeefa57c472012-11-21 12:06:18 -0800409 loc = static_cast<RegLocation*>(NewMem(cu, cu->num_ssa_regs * sizeof(*loc),
buzbeecbd6d442012-11-17 14:11:25 -0800410 true, kAllocRegAlloc));
buzbeefa57c472012-11-21 12:06:18 -0800411 for (i=0; i< cu->num_ssa_regs; i++) {
412 loc[i] = fresh_loc;
413 loc[i].s_reg_low = i;
414 loc[i].is_const = IsBitSet(cu->is_constant_v, i);
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 }
416
417 /* Patch up the locations for Method* and the compiler temps */
buzbeefa57c472012-11-21 12:06:18 -0800418 loc[cu->method_sreg].location = kLocCompilerTemp;
419 loc[cu->method_sreg].defined = true;
420 for (i = 0; i < cu->num_compiler_temps; i++) {
421 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(cu->compiler_temps.elem_list[i]);
422 loc[ct->s_reg].location = kLocCompilerTemp;
423 loc[ct->s_reg].defined = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 }
425
buzbeefa57c472012-11-21 12:06:18 -0800426 cu->reg_location = loc;
Bill Buzbeea114add2012-05-03 15:00:40 -0700427
428 /* Allocation the promotion map */
buzbeefa57c472012-11-21 12:06:18 -0800429 int num_regs = cu->num_dalvik_registers;
430 cu->promotion_map = static_cast<PromotionMap*>
431 (NewMem(cu, (num_regs + cu->num_compiler_temps + 1) * sizeof(cu->promotion_map[0]),
buzbeecbd6d442012-11-17 14:11:25 -0800432 true, kAllocRegAlloc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700433
434 /* Add types of incoming arguments based on signature */
buzbeefa57c472012-11-21 12:06:18 -0800435 int num_ins = cu->num_ins;
436 if (num_ins > 0) {
437 int s_reg = num_regs - num_ins;
438 if ((cu->access_flags & kAccStatic) == 0) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 // For non-static, skip past "this"
buzbeefa57c472012-11-21 12:06:18 -0800440 cu->reg_location[s_reg].defined = true;
441 cu->reg_location[s_reg].ref = true;
442 s_reg++;
buzbee67bf8852011-08-17 17:51:35 -0700443 }
buzbeefa57c472012-11-21 12:06:18 -0800444 const char* shorty = cu->shorty;
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 int shorty_len = strlen(shorty);
446 for (int i = 1; i < shorty_len; i++) {
447 switch (shorty[i]) {
448 case 'D':
buzbeefa57c472012-11-21 12:06:18 -0800449 cu->reg_location[s_reg].wide = true;
450 cu->reg_location[s_reg+1].high_word = true;
451 cu->reg_location[s_reg+1].fp = true;
452 DCHECK_EQ(SRegToVReg(cu, s_reg)+1, SRegToVReg(cu, s_reg+1));
453 cu->reg_location[s_reg].fp = true;
454 cu->reg_location[s_reg].defined = true;
455 s_reg++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 break;
457 case 'J':
buzbeefa57c472012-11-21 12:06:18 -0800458 cu->reg_location[s_reg].wide = true;
459 cu->reg_location[s_reg+1].high_word = true;
460 DCHECK_EQ(SRegToVReg(cu, s_reg)+1, SRegToVReg(cu, s_reg+1));
461 cu->reg_location[s_reg].core = true;
462 cu->reg_location[s_reg].defined = true;
463 s_reg++;
buzbeed5018892012-07-11 14:23:40 -0700464 break;
465 case 'F':
buzbeefa57c472012-11-21 12:06:18 -0800466 cu->reg_location[s_reg].fp = true;
467 cu->reg_location[s_reg].defined = true;
buzbeed5018892012-07-11 14:23:40 -0700468 break;
469 case 'L':
buzbeefa57c472012-11-21 12:06:18 -0800470 cu->reg_location[s_reg].ref = true;
471 cu->reg_location[s_reg].defined = true;
buzbeed5018892012-07-11 14:23:40 -0700472 break;
473 default:
buzbeefa57c472012-11-21 12:06:18 -0800474 cu->reg_location[s_reg].core = true;
475 cu->reg_location[s_reg].defined = true;
buzbeed5018892012-07-11 14:23:40 -0700476 break;
buzbeee9a72f62011-09-04 17:59:07 -0700477 }
buzbeefa57c472012-11-21 12:06:18 -0800478 s_reg++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 }
480 }
481
buzbeefa57c472012-11-21 12:06:18 -0800482 if (!cu->gen_bitcode) {
buzbee2cfc6392012-05-07 14:51:40 -0700483 /* Remap names */
buzbeefa57c472012-11-21 12:06:18 -0800484 DataFlowAnalysisDispatcher(cu, RemapNames,
buzbee2cfc6392012-05-07 14:51:40 -0700485 kPreOrderDFSTraversal,
buzbeefa57c472012-11-21 12:06:18 -0800486 false /* is_iterative */);
buzbee2cfc6392012-05-07 14:51:40 -0700487 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700488
489 /* Do type & size inference pass */
buzbeefa57c472012-11-21 12:06:18 -0800490 DataFlowAnalysisDispatcher(cu, InferTypeAndSize,
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 kPreOrderDFSTraversal,
buzbeefa57c472012-11-21 12:06:18 -0800492 true /* is_iterative */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700493
494 /*
buzbeefa57c472012-11-21 12:06:18 -0800495 * Set the s_reg_low field to refer to the pre-SSA name of the
Bill Buzbeea114add2012-05-03 15:00:40 -0700496 * base Dalvik virtual register. Once we add a better register
497 * allocator, remove this remapping.
498 */
buzbeefa57c472012-11-21 12:06:18 -0800499 for (i=0; i < cu->num_ssa_regs; i++) {
500 if (cu->reg_location[i].location != kLocCompilerTemp) {
501 int orig_sreg = cu->reg_location[i].s_reg_low;
502 cu->reg_location[i].orig_sreg = orig_sreg;
503 cu->reg_location[i].s_reg_low = SRegToVReg(cu, orig_sreg);
buzbeee9a72f62011-09-04 17:59:07 -0700504 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 }
buzbeee9a72f62011-09-04 17:59:07 -0700506
buzbeefa57c472012-11-21 12:06:18 -0800507 cu->core_spill_mask = 0;
508 cu->fp_spill_mask = 0;
509 cu->num_core_spills = 0;
buzbeec0ecd652011-09-25 18:11:54 -0700510
buzbeefa57c472012-11-21 12:06:18 -0800511 DoPromotion(cu);
buzbee67bf8852011-08-17 17:51:35 -0700512
buzbeead8f15e2012-06-18 14:49:45 -0700513 /* Get easily-accessable post-promotion copy of RegLocation for Method* */
buzbeefa57c472012-11-21 12:06:18 -0800514 cu->method_loc = cu->reg_location[cu->method_sreg];
buzbeead8f15e2012-06-18 14:49:45 -0700515
buzbeefa57c472012-11-21 12:06:18 -0800516 if (cu->verbose && !(cu->disable_opt & (1 << kPromoteRegs))) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 LOG(INFO) << "After Promotion";
buzbeefa57c472012-11-21 12:06:18 -0800518 DumpRegLocTable(cu->reg_location, cu->num_ssa_regs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700519 }
buzbee67bf8852011-08-17 17:51:35 -0700520
buzbeead8f15e2012-06-18 14:49:45 -0700521 /* Set the frame size */
buzbeefa57c472012-11-21 12:06:18 -0800522 cu->frame_size = ComputeFrameSize(cu);
buzbee67bf8852011-08-17 17:51:35 -0700523}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800524
525} // namespace art