blob: 08f1f35dc39c788bd0e954774e518b16c6c2867a [file] [log] [blame]
buzbee311ca162013-02-28 15:56:43 -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 "compiler_internals.h"
18#include "dataflow_iterator.h"
19#include "quick/ralloc_util.h"
20
21namespace art {
22
23static bool SetFp(CompilationUnit* cu, int index, bool is_fp) {
24 bool change = false;
25 if (is_fp && !cu->reg_location[index].fp) {
26 cu->reg_location[index].fp = true;
27 cu->reg_location[index].defined = true;
28 change = true;
29 }
30 return change;
31}
32
33static bool SetCore(CompilationUnit* cu, int index, bool is_core) {
34 bool change = false;
35 if (is_core && !cu->reg_location[index].defined) {
36 cu->reg_location[index].core = true;
37 cu->reg_location[index].defined = true;
38 change = true;
39 }
40 return change;
41}
42
43static bool SetRef(CompilationUnit* cu, int index, bool is_ref) {
44 bool change = false;
45 if (is_ref && !cu->reg_location[index].defined) {
46 cu->reg_location[index].ref = true;
47 cu->reg_location[index].defined = true;
48 change = true;
49 }
50 return change;
51}
52
53static bool SetWide(CompilationUnit* cu, int index, bool is_wide) {
54 bool change = false;
55 if (is_wide && !cu->reg_location[index].wide) {
56 cu->reg_location[index].wide = true;
57 change = true;
58 }
59 return change;
60}
61
62static bool SetHigh(CompilationUnit* cu, int index, bool is_high) {
63 bool change = false;
64 if (is_high && !cu->reg_location[index].high_word) {
65 cu->reg_location[index].high_word = true;
66 change = true;
67 }
68 return change;
69}
70
71/*
72 * Infer types and sizes. We don't need to track change on sizes,
73 * as it doesn't propagate. We're guaranteed at least one pass through
74 * the cfg.
75 */
76bool MIRGraph::InferTypeAndSize(BasicBlock* bb)
77{
78 MIR *mir;
79 bool changed = false; // Did anything change?
80
81 if (bb->data_flow_info == NULL) return false;
82 if (bb->block_type != kDalvikByteCode && bb->block_type != kEntryBlock)
83 return false;
84
85 for (mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
86 SSARepresentation *ssa_rep = mir->ssa_rep;
87 if (ssa_rep) {
88 int attrs = oat_data_flow_attributes[mir->dalvikInsn.opcode];
89
90 // Handle defs
91 if (attrs & DF_DA) {
92 if (attrs & DF_CORE_A) {
93 changed |= SetCore(cu_, ssa_rep->defs[0], true);
94 }
95 if (attrs & DF_REF_A) {
96 changed |= SetRef(cu_, ssa_rep->defs[0], true);
97 }
98 if (attrs & DF_A_WIDE) {
99 cu_->reg_location[ssa_rep->defs[0]].wide = true;
100 cu_->reg_location[ssa_rep->defs[1]].wide = true;
101 cu_->reg_location[ssa_rep->defs[1]].high_word = true;
102 DCHECK_EQ(SRegToVReg(ssa_rep->defs[0])+1,
103 SRegToVReg(ssa_rep->defs[1]));
104 }
105 }
106
107 // Handles uses
108 int next = 0;
109 if (attrs & DF_UA) {
110 if (attrs & DF_CORE_A) {
111 changed |= SetCore(cu_, ssa_rep->uses[next], true);
112 }
113 if (attrs & DF_REF_A) {
114 changed |= SetRef(cu_, ssa_rep->uses[next], true);
115 }
116 if (attrs & DF_A_WIDE) {
117 cu_->reg_location[ssa_rep->uses[next]].wide = true;
118 cu_->reg_location[ssa_rep->uses[next + 1]].wide = true;
119 cu_->reg_location[ssa_rep->uses[next + 1]].high_word = true;
120 DCHECK_EQ(SRegToVReg(ssa_rep->uses[next])+1,
121 SRegToVReg(ssa_rep->uses[next + 1]));
122 next += 2;
123 } else {
124 next++;
125 }
126 }
127 if (attrs & DF_UB) {
128 if (attrs & DF_CORE_B) {
129 changed |= SetCore(cu_, ssa_rep->uses[next], true);
130 }
131 if (attrs & DF_REF_B) {
132 changed |= SetRef(cu_, ssa_rep->uses[next], true);
133 }
134 if (attrs & DF_B_WIDE) {
135 cu_->reg_location[ssa_rep->uses[next]].wide = true;
136 cu_->reg_location[ssa_rep->uses[next + 1]].wide = true;
137 cu_->reg_location[ssa_rep->uses[next + 1]].high_word = true;
138 DCHECK_EQ(SRegToVReg(ssa_rep->uses[next])+1,
139 SRegToVReg(ssa_rep->uses[next + 1]));
140 next += 2;
141 } else {
142 next++;
143 }
144 }
145 if (attrs & DF_UC) {
146 if (attrs & DF_CORE_C) {
147 changed |= SetCore(cu_, ssa_rep->uses[next], true);
148 }
149 if (attrs & DF_REF_C) {
150 changed |= SetRef(cu_, ssa_rep->uses[next], true);
151 }
152 if (attrs & DF_C_WIDE) {
153 cu_->reg_location[ssa_rep->uses[next]].wide = true;
154 cu_->reg_location[ssa_rep->uses[next + 1]].wide = true;
155 cu_->reg_location[ssa_rep->uses[next + 1]].high_word = true;
156 DCHECK_EQ(SRegToVReg(ssa_rep->uses[next])+1,
157 SRegToVReg(ssa_rep->uses[next + 1]));
158 }
159 }
160
161 // Special-case return handling
162 if ((mir->dalvikInsn.opcode == Instruction::RETURN) ||
163 (mir->dalvikInsn.opcode == Instruction::RETURN_WIDE) ||
164 (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT)) {
165 switch(cu_->shorty[0]) {
166 case 'I':
167 changed |= SetCore(cu_, ssa_rep->uses[0], true);
168 break;
169 case 'J':
170 changed |= SetCore(cu_, ssa_rep->uses[0], true);
171 changed |= SetCore(cu_, ssa_rep->uses[1], true);
172 cu_->reg_location[ssa_rep->uses[0]].wide = true;
173 cu_->reg_location[ssa_rep->uses[1]].wide = true;
174 cu_->reg_location[ssa_rep->uses[1]].high_word = true;
175 break;
176 case 'F':
177 changed |= SetFp(cu_, ssa_rep->uses[0], true);
178 break;
179 case 'D':
180 changed |= SetFp(cu_, ssa_rep->uses[0], true);
181 changed |= SetFp(cu_, ssa_rep->uses[1], true);
182 cu_->reg_location[ssa_rep->uses[0]].wide = true;
183 cu_->reg_location[ssa_rep->uses[1]].wide = true;
184 cu_->reg_location[ssa_rep->uses[1]].high_word = true;
185 break;
186 case 'L':
187 changed |= SetRef(cu_, ssa_rep->uses[0], true);
188 break;
189 default: break;
190 }
191 }
192
193 // Special-case handling for format 35c/3rc invokes
194 Instruction::Code opcode = mir->dalvikInsn.opcode;
195 int flags = (static_cast<int>(opcode) >= kNumPackedOpcodes)
196 ? 0 : Instruction::FlagsOf(mir->dalvikInsn.opcode);
197 if ((flags & Instruction::kInvoke) &&
198 (attrs & (DF_FORMAT_35C | DF_FORMAT_3RC))) {
199 DCHECK_EQ(next, 0);
200 int target_idx = mir->dalvikInsn.vB;
201 const char* shorty = GetShortyFromTargetIdx(cu_, target_idx);
202 // Handle result type if floating point
203 if ((shorty[0] == 'F') || (shorty[0] == 'D')) {
204 MIR* move_result_mir = FindMoveResult(bb, mir);
205 // Result might not be used at all, so no move-result
206 if (move_result_mir && (move_result_mir->dalvikInsn.opcode !=
207 Instruction::MOVE_RESULT_OBJECT)) {
208 SSARepresentation* tgt_rep = move_result_mir->ssa_rep;
209 DCHECK(tgt_rep != NULL);
210 tgt_rep->fp_def[0] = true;
211 changed |= SetFp(cu_, tgt_rep->defs[0], true);
212 if (shorty[0] == 'D') {
213 tgt_rep->fp_def[1] = true;
214 changed |= SetFp(cu_, tgt_rep->defs[1], true);
215 }
216 }
217 }
218 int num_uses = mir->dalvikInsn.vA;
219 // If this is a non-static invoke, mark implicit "this"
220 if (((mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC) &&
221 (mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC_RANGE))) {
222 cu_->reg_location[ssa_rep->uses[next]].defined = true;
223 cu_->reg_location[ssa_rep->uses[next]].ref = true;
224 next++;
225 }
226 uint32_t cpos = 1;
227 if (strlen(shorty) > 1) {
228 for (int i = next; i < num_uses;) {
229 DCHECK_LT(cpos, strlen(shorty));
230 switch (shorty[cpos++]) {
231 case 'D':
232 ssa_rep->fp_use[i] = true;
233 ssa_rep->fp_use[i+1] = true;
234 cu_->reg_location[ssa_rep->uses[i]].wide = true;
235 cu_->reg_location[ssa_rep->uses[i+1]].wide = true;
236 cu_->reg_location[ssa_rep->uses[i+1]].high_word = true;
237 DCHECK_EQ(SRegToVReg(ssa_rep->uses[i])+1, SRegToVReg(ssa_rep->uses[i+1]));
238 i++;
239 break;
240 case 'J':
241 cu_->reg_location[ssa_rep->uses[i]].wide = true;
242 cu_->reg_location[ssa_rep->uses[i+1]].wide = true;
243 cu_->reg_location[ssa_rep->uses[i+1]].high_word = true;
244 DCHECK_EQ(SRegToVReg(ssa_rep->uses[i])+1, SRegToVReg(ssa_rep->uses[i+1]));
245 changed |= SetCore(cu_, ssa_rep->uses[i],true);
246 i++;
247 break;
248 case 'F':
249 ssa_rep->fp_use[i] = true;
250 break;
251 case 'L':
252 changed |= SetRef(cu_,ssa_rep->uses[i], true);
253 break;
254 default:
255 changed |= SetCore(cu_,ssa_rep->uses[i], true);
256 break;
257 }
258 i++;
259 }
260 }
261 }
262
263 for (int i=0; ssa_rep->fp_use && i< ssa_rep->num_uses; i++) {
264 if (ssa_rep->fp_use[i])
265 changed |= SetFp(cu_, ssa_rep->uses[i], true);
266 }
267 for (int i=0; ssa_rep->fp_def && i< ssa_rep->num_defs; i++) {
268 if (ssa_rep->fp_def[i])
269 changed |= SetFp(cu_, ssa_rep->defs[i], true);
270 }
271 // Special-case handling for moves & Phi
272 if (attrs & (DF_IS_MOVE | DF_NULL_TRANSFER_N)) {
273 /*
274 * If any of our inputs or outputs is defined, set all.
275 * Some ugliness related to Phi nodes and wide values.
276 * The Phi set will include all low words or all high
277 * words, so we have to treat them specially.
278 */
279 bool is_phi = (static_cast<int>(mir->dalvikInsn.opcode) ==
280 kMirOpPhi);
281 RegLocation rl_temp = cu_->reg_location[ssa_rep->defs[0]];
282 bool defined_fp = rl_temp.defined && rl_temp.fp;
283 bool defined_core = rl_temp.defined && rl_temp.core;
284 bool defined_ref = rl_temp.defined && rl_temp.ref;
285 bool is_wide = rl_temp.wide || ((attrs & DF_A_WIDE) != 0);
286 bool is_high = is_phi && rl_temp.wide && rl_temp.high_word;
287 for (int i = 0; i < ssa_rep->num_uses;i++) {
288 rl_temp = cu_->reg_location[ssa_rep->uses[i]];
289 defined_fp |= rl_temp.defined && rl_temp.fp;
290 defined_core |= rl_temp.defined && rl_temp.core;
291 defined_ref |= rl_temp.defined && rl_temp.ref;
292 is_wide |= rl_temp.wide;
293 is_high |= is_phi && rl_temp.wide && rl_temp.high_word;
294 }
295 /*
296 * TODO: cleaner fix
297 * We don't normally expect to see a Dalvik register
298 * definition used both as a floating point and core
299 * value. However, the instruction rewriting that occurs
300 * during verification can eliminate some type information,
301 * leaving us confused. The real fix here is either to
302 * add explicit type information to Dalvik byte codes,
303 * or to recognize THROW_VERIFICATION_ERROR as
304 * an unconditional branch and support dead code elimination.
305 * As a workaround we can detect this situation and
306 * disable register promotion (which is the only thing that
307 * relies on distinctions between core and fp usages.
308 */
309 if ((defined_fp && (defined_core | defined_ref)) &&
310 ((cu_->disable_opt & (1 << kPromoteRegs)) == 0)) {
311 LOG(WARNING) << PrettyMethod(cu_->method_idx, *cu_->dex_file)
312 << " op at block " << bb->id
313 << " has both fp and core/ref uses for same def.";
314 cu_->disable_opt |= (1 << kPromoteRegs);
315 }
316 changed |= SetFp(cu_, ssa_rep->defs[0], defined_fp);
317 changed |= SetCore(cu_, ssa_rep->defs[0], defined_core);
318 changed |= SetRef(cu_, ssa_rep->defs[0], defined_ref);
319 changed |= SetWide(cu_, ssa_rep->defs[0], is_wide);
320 changed |= SetHigh(cu_, ssa_rep->defs[0], is_high);
321 if (attrs & DF_A_WIDE) {
322 changed |= SetWide(cu_, ssa_rep->defs[1], true);
323 changed |= SetHigh(cu_, ssa_rep->defs[1], true);
324 }
325 for (int i = 0; i < ssa_rep->num_uses; i++) {
326 changed |= SetFp(cu_, ssa_rep->uses[i], defined_fp);
327 changed |= SetCore(cu_, ssa_rep->uses[i], defined_core);
328 changed |= SetRef(cu_, ssa_rep->uses[i], defined_ref);
329 changed |= SetWide(cu_, ssa_rep->uses[i], is_wide);
330 changed |= SetHigh(cu_, ssa_rep->uses[i], is_high);
331 }
332 if (attrs & DF_A_WIDE) {
333 DCHECK_EQ(ssa_rep->num_uses, 2);
334 changed |= SetWide(cu_, ssa_rep->uses[1], true);
335 changed |= SetHigh(cu_, ssa_rep->uses[1], true);
336 }
337 }
338 }
339 }
340 return changed;
341}
342
343static const char* storage_name[] = {" Frame ", "PhysReg", " Spill "};
344
345void MIRGraph::DumpRegLocTable(RegLocation* table, int count)
346{
347 Codegen* cg = cu_->cg.get();
348 if (cg != NULL) {
349 for (int i = 0; i < count; i++) {
350 LOG(INFO) << StringPrintf("Loc[%02d] : %s, %c %c %c %c %c %c %c%d %c%d S%d",
351 table[i].orig_sreg, storage_name[table[i].location],
352 table[i].wide ? 'W' : 'N', table[i].defined ? 'D' : 'U',
353 table[i].fp ? 'F' : table[i].ref ? 'R' :'C',
354 table[i].is_const ? 'c' : 'n',
355 table[i].high_word ? 'H' : 'L', table[i].home ? 'h' : 't',
356 cg->IsFpReg(table[i].low_reg) ? 's' : 'r',
357 table[i].low_reg & cg->FpRegMask(),
358 cg->IsFpReg(table[i].high_reg) ? 's' : 'r',
359 table[i].high_reg & cg->FpRegMask(), table[i].s_reg_low);
360 }
361 } else {
362 // Either pre-regalloc or Portable.
363 for (int i = 0; i < count; i++) {
364 LOG(INFO) << StringPrintf("Loc[%02d] : %s, %c %c %c %c %c %c S%d",
365 table[i].orig_sreg, storage_name[table[i].location],
366 table[i].wide ? 'W' : 'N', table[i].defined ? 'D' : 'U',
367 table[i].fp ? 'F' : table[i].ref ? 'R' :'C',
368 table[i].is_const ? 'c' : 'n',
369 table[i].high_word ? 'H' : 'L', table[i].home ? 'h' : 't',
370 table[i].s_reg_low);
371 }
372 }
373}
374
375static const RegLocation fresh_loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
376 INVALID_REG, INVALID_REG, INVALID_SREG,
377 INVALID_SREG};
378
379int MIRGraph::ComputeFrameSize() {
380 /* Figure out the frame size */
381 static const uint32_t kAlignMask = kStackAlignment - 1;
382 uint32_t size = (cu_->num_core_spills + cu_->num_fp_spills +
383 1 /* filler word */ + cu_->num_regs + cu_->num_outs +
384 cu_->num_compiler_temps + 1 /* cur_method* */)
385 * sizeof(uint32_t);
386 /* Align and set */
387 return (size + kAlignMask) & ~(kAlignMask);
388}
389
390/*
391 * Simple register allocation. Some Dalvik virtual registers may
392 * be promoted to physical registers. Most of the work for temp
393 * allocation is done on the fly. We also do some initialization and
394 * type inference here.
395 */
396void MIRGraph::BuildRegLocations()
397{
398 int i;
399 RegLocation* loc;
400
401 /* Allocate the location map */
402 loc = static_cast<RegLocation*>(NewMem(cu_, GetNumSSARegs() * sizeof(*loc),
403 true, kAllocRegAlloc));
404 for (i=0; i < GetNumSSARegs(); i++) {
405 loc[i] = fresh_loc;
406 loc[i].s_reg_low = i;
407 loc[i].is_const = IsBitSet(is_constant_v_, i);
408 }
409
410 /* Patch up the locations for Method* and the compiler temps */
411 loc[cu_->method_sreg].location = kLocCompilerTemp;
412 loc[cu_->method_sreg].defined = true;
413 for (i = 0; i < cu_->num_compiler_temps; i++) {
414 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(cu_->compiler_temps.elem_list[i]);
415 loc[ct->s_reg].location = kLocCompilerTemp;
416 loc[ct->s_reg].defined = true;
417 }
418
419 cu_->reg_location = loc;
420
421 /* Allocation the promotion map */
422 int num_regs = cu_->num_dalvik_registers;
423 cu_->promotion_map = static_cast<PromotionMap*>
424 (NewMem(cu_, (num_regs + cu_->num_compiler_temps + 1) * sizeof(cu_->promotion_map[0]),
425 true, kAllocRegAlloc));
426
427 /* Add types of incoming arguments based on signature */
428 int num_ins = cu_->num_ins;
429 if (num_ins > 0) {
430 int s_reg = num_regs - num_ins;
431 if ((cu_->access_flags & kAccStatic) == 0) {
432 // For non-static, skip past "this"
433 cu_->reg_location[s_reg].defined = true;
434 cu_->reg_location[s_reg].ref = true;
435 s_reg++;
436 }
437 const char* shorty = cu_->shorty;
438 int shorty_len = strlen(shorty);
439 for (int i = 1; i < shorty_len; i++) {
440 switch (shorty[i]) {
441 case 'D':
442 cu_->reg_location[s_reg].wide = true;
443 cu_->reg_location[s_reg+1].high_word = true;
444 cu_->reg_location[s_reg+1].fp = true;
445 DCHECK_EQ(SRegToVReg(s_reg)+1, SRegToVReg(s_reg+1));
446 cu_->reg_location[s_reg].fp = true;
447 cu_->reg_location[s_reg].defined = true;
448 s_reg++;
449 break;
450 case 'J':
451 cu_->reg_location[s_reg].wide = true;
452 cu_->reg_location[s_reg+1].high_word = true;
453 DCHECK_EQ(SRegToVReg(s_reg)+1, SRegToVReg(s_reg+1));
454 cu_->reg_location[s_reg].core = true;
455 cu_->reg_location[s_reg].defined = true;
456 s_reg++;
457 break;
458 case 'F':
459 cu_->reg_location[s_reg].fp = true;
460 cu_->reg_location[s_reg].defined = true;
461 break;
462 case 'L':
463 cu_->reg_location[s_reg].ref = true;
464 cu_->reg_location[s_reg].defined = true;
465 break;
466 default:
467 cu_->reg_location[s_reg].core = true;
468 cu_->reg_location[s_reg].defined = true;
469 break;
470 }
471 s_reg++;
472 }
473 }
474
475 /* Do type & size inference pass */
buzbee0665fe02013-03-21 12:32:21 -0700476 PreOrderDfsIterator iter(this, true /* iterative */);
buzbee311ca162013-02-28 15:56:43 -0800477 bool change = false;
478 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
479 change = InferTypeAndSize(bb);
480 }
481
482 /*
483 * Set the s_reg_low field to refer to the pre-SSA name of the
484 * base Dalvik virtual register. Once we add a better register
485 * allocator, remove this remapping.
486 */
487 for (i=0; i < GetNumSSARegs(); i++) {
488 if (cu_->reg_location[i].location != kLocCompilerTemp) {
489 int orig_sreg = cu_->reg_location[i].s_reg_low;
490 cu_->reg_location[i].orig_sreg = orig_sreg;
491 cu_->reg_location[i].s_reg_low = SRegToVReg(orig_sreg);
492 }
493 }
494}
495
496} // namespace art