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