blob: bef966c8ff701e411f7203873ee8cd84cf875fef [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
buzbee28c23002013-09-07 09:12:27 -070032bool MIRGraph::SetFp(int index) {
33 bool change = false;
34 if (!reg_location_[index].fp) {
35 reg_location_[index].fp = true;
36 reg_location_[index].defined = true;
37 change = true;
38 }
39 return change;
40}
41
Brian Carlstrom7940e442013-07-12 13:46:57 -070042bool MIRGraph::SetCore(int index, bool is_core) {
43 bool change = false;
44 if (is_core && !reg_location_[index].defined) {
45 reg_location_[index].core = true;
46 reg_location_[index].defined = true;
47 change = true;
48 }
49 return change;
50}
51
buzbee28c23002013-09-07 09:12:27 -070052bool MIRGraph::SetCore(int index) {
53 bool change = false;
54 if (!reg_location_[index].defined) {
55 reg_location_[index].core = true;
56 reg_location_[index].defined = true;
57 change = true;
58 }
59 return change;
60}
61
Brian Carlstrom7940e442013-07-12 13:46:57 -070062bool MIRGraph::SetRef(int index, bool is_ref) {
63 bool change = false;
64 if (is_ref && !reg_location_[index].defined) {
65 reg_location_[index].ref = true;
66 reg_location_[index].defined = true;
67 change = true;
68 }
69 return change;
70}
71
buzbee28c23002013-09-07 09:12:27 -070072bool MIRGraph::SetRef(int index) {
73 bool change = false;
74 if (!reg_location_[index].defined) {
75 reg_location_[index].ref = true;
76 reg_location_[index].defined = true;
77 change = true;
78 }
79 return change;
80}
81
Brian Carlstrom7940e442013-07-12 13:46:57 -070082bool MIRGraph::SetWide(int index, bool is_wide) {
83 bool change = false;
84 if (is_wide && !reg_location_[index].wide) {
85 reg_location_[index].wide = true;
86 change = true;
87 }
88 return change;
89}
90
buzbee28c23002013-09-07 09:12:27 -070091bool MIRGraph::SetWide(int index) {
92 bool change = false;
93 if (!reg_location_[index].wide) {
94 reg_location_[index].wide = true;
95 change = true;
96 }
97 return change;
98}
99
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100bool MIRGraph::SetHigh(int index, bool is_high) {
101 bool change = false;
102 if (is_high && !reg_location_[index].high_word) {
103 reg_location_[index].high_word = true;
104 change = true;
105 }
106 return change;
107}
108
buzbee28c23002013-09-07 09:12:27 -0700109bool MIRGraph::SetHigh(int index) {
110 bool change = false;
111 if (!reg_location_[index].high_word) {
112 reg_location_[index].high_word = true;
113 change = true;
114 }
115 return change;
116}
117
118
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119/*
120 * Infer types and sizes. We don't need to track change on sizes,
121 * as it doesn't propagate. We're guaranteed at least one pass through
122 * the cfg.
123 */
buzbee1da1e2f2013-11-15 13:37:01 -0800124bool MIRGraph::InferTypeAndSize(BasicBlock* bb, MIR* mir, bool changed) {
125 SSARepresentation *ssa_rep = mir->ssa_rep;
126 if (ssa_rep) {
127 uint64_t attrs = oat_data_flow_attributes_[mir->dalvikInsn.opcode];
128 const int* uses = ssa_rep->uses;
129 const int* defs = ssa_rep->defs;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130
buzbee1da1e2f2013-11-15 13:37:01 -0800131 // Handle defs
132 if (attrs & DF_DA) {
133 if (attrs & DF_CORE_A) {
134 changed |= SetCore(defs[0]);
135 }
136 if (attrs & DF_REF_A) {
137 changed |= SetRef(defs[0]);
138 }
139 if (attrs & DF_A_WIDE) {
140 reg_location_[defs[0]].wide = true;
141 reg_location_[defs[1]].wide = true;
142 reg_location_[defs[1]].high_word = true;
143 DCHECK_EQ(SRegToVReg(defs[0])+1,
144 SRegToVReg(defs[1]));
145 }
146 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147
buzbee1da1e2f2013-11-15 13:37:01 -0800148 // Handles uses
149 int next = 0;
150 if (attrs & DF_UA) {
151 if (attrs & DF_CORE_A) {
152 changed |= SetCore(uses[next]);
153 }
154 if (attrs & DF_REF_A) {
155 changed |= SetRef(uses[next]);
156 }
157 if (attrs & DF_A_WIDE) {
158 reg_location_[uses[next]].wide = true;
159 reg_location_[uses[next + 1]].wide = true;
160 reg_location_[uses[next + 1]].high_word = true;
161 DCHECK_EQ(SRegToVReg(uses[next])+1,
162 SRegToVReg(uses[next + 1]));
163 next += 2;
164 } else {
165 next++;
166 }
167 }
168 if (attrs & DF_UB) {
169 if (attrs & DF_CORE_B) {
170 changed |= SetCore(uses[next]);
171 }
172 if (attrs & DF_REF_B) {
173 changed |= SetRef(uses[next]);
174 }
175 if (attrs & DF_B_WIDE) {
176 reg_location_[uses[next]].wide = true;
177 reg_location_[uses[next + 1]].wide = true;
178 reg_location_[uses[next + 1]].high_word = true;
179 DCHECK_EQ(SRegToVReg(uses[next])+1,
180 SRegToVReg(uses[next + 1]));
181 next += 2;
182 } else {
183 next++;
184 }
185 }
186 if (attrs & DF_UC) {
187 if (attrs & DF_CORE_C) {
188 changed |= SetCore(uses[next]);
189 }
190 if (attrs & DF_REF_C) {
191 changed |= SetRef(uses[next]);
192 }
193 if (attrs & DF_C_WIDE) {
194 reg_location_[uses[next]].wide = true;
195 reg_location_[uses[next + 1]].wide = true;
196 reg_location_[uses[next + 1]].high_word = true;
197 DCHECK_EQ(SRegToVReg(uses[next])+1,
198 SRegToVReg(uses[next + 1]));
199 }
200 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201
buzbee1da1e2f2013-11-15 13:37:01 -0800202 // Special-case return handling
203 if ((mir->dalvikInsn.opcode == Instruction::RETURN) ||
204 (mir->dalvikInsn.opcode == Instruction::RETURN_WIDE) ||
205 (mir->dalvikInsn.opcode == Instruction::RETURN_OBJECT)) {
206 switch (cu_->shorty[0]) {
207 case 'I':
208 changed |= SetCore(uses[0]);
209 break;
210 case 'J':
211 changed |= SetCore(uses[0]);
212 changed |= SetCore(uses[1]);
213 reg_location_[uses[0]].wide = true;
214 reg_location_[uses[1]].wide = true;
215 reg_location_[uses[1]].high_word = true;
216 break;
217 case 'F':
218 changed |= SetFp(uses[0]);
219 break;
220 case 'D':
221 changed |= SetFp(uses[0]);
222 changed |= SetFp(uses[1]);
223 reg_location_[uses[0]].wide = true;
224 reg_location_[uses[1]].wide = true;
225 reg_location_[uses[1]].high_word = true;
226 break;
227 case 'L':
228 changed |= SetRef(uses[0]);
229 break;
230 default: break;
231 }
232 }
233
234 // Special-case handling for format 35c/3rc invokes
235 Instruction::Code opcode = mir->dalvikInsn.opcode;
236 int flags = (static_cast<int>(opcode) >= kNumPackedOpcodes)
237 ? 0 : Instruction::FlagsOf(mir->dalvikInsn.opcode);
238 if ((flags & Instruction::kInvoke) &&
239 (attrs & (DF_FORMAT_35C | DF_FORMAT_3RC))) {
240 DCHECK_EQ(next, 0);
241 int target_idx = mir->dalvikInsn.vB;
242 const char* shorty = GetShortyFromTargetIdx(target_idx);
243 // Handle result type if floating point
244 if ((shorty[0] == 'F') || (shorty[0] == 'D')) {
245 MIR* move_result_mir = FindMoveResult(bb, mir);
246 // Result might not be used at all, so no move-result
247 if (move_result_mir && (move_result_mir->dalvikInsn.opcode !=
248 Instruction::MOVE_RESULT_OBJECT)) {
249 SSARepresentation* tgt_rep = move_result_mir->ssa_rep;
250 DCHECK(tgt_rep != NULL);
251 tgt_rep->fp_def[0] = true;
252 changed |= SetFp(tgt_rep->defs[0]);
253 if (shorty[0] == 'D') {
254 tgt_rep->fp_def[1] = true;
255 changed |= SetFp(tgt_rep->defs[1]);
256 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 }
258 }
buzbee1da1e2f2013-11-15 13:37:01 -0800259 int num_uses = mir->dalvikInsn.vA;
260 // If this is a non-static invoke, mark implicit "this"
261 if (((mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC) &&
262 (mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC_RANGE))) {
263 reg_location_[uses[next]].defined = true;
264 reg_location_[uses[next]].ref = true;
265 next++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 }
buzbee1da1e2f2013-11-15 13:37:01 -0800267 uint32_t cpos = 1;
268 if (strlen(shorty) > 1) {
269 for (int i = next; i < num_uses;) {
270 DCHECK_LT(cpos, strlen(shorty));
271 switch (shorty[cpos++]) {
272 case 'D':
273 ssa_rep->fp_use[i] = true;
274 ssa_rep->fp_use[i+1] = true;
275 reg_location_[uses[i]].wide = true;
276 reg_location_[uses[i+1]].wide = true;
277 reg_location_[uses[i+1]].high_word = true;
278 DCHECK_EQ(SRegToVReg(uses[i])+1, SRegToVReg(uses[i+1]));
279 i++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 break;
281 case 'J':
buzbee1da1e2f2013-11-15 13:37:01 -0800282 reg_location_[uses[i]].wide = true;
283 reg_location_[uses[i+1]].wide = true;
284 reg_location_[uses[i+1]].high_word = true;
285 DCHECK_EQ(SRegToVReg(uses[i])+1, SRegToVReg(uses[i+1]));
286 changed |= SetCore(uses[i]);
287 i++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 break;
289 case 'F':
buzbee1da1e2f2013-11-15 13:37:01 -0800290 ssa_rep->fp_use[i] = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 break;
292 case 'L':
buzbee1da1e2f2013-11-15 13:37:01 -0800293 changed |= SetRef(uses[i]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 break;
buzbee1da1e2f2013-11-15 13:37:01 -0800295 default:
296 changed |= SetCore(uses[i]);
297 break;
298 }
299 i++;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 }
301 }
buzbee1da1e2f2013-11-15 13:37:01 -0800302 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303
buzbee1da1e2f2013-11-15 13:37:01 -0800304 for (int i = 0; ssa_rep->fp_use && i< ssa_rep->num_uses; i++) {
305 if (ssa_rep->fp_use[i])
306 changed |= SetFp(uses[i]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 }
buzbee1da1e2f2013-11-15 13:37:01 -0800308 for (int i = 0; ssa_rep->fp_def && i< ssa_rep->num_defs; i++) {
309 if (ssa_rep->fp_def[i])
310 changed |= SetFp(defs[i]);
311 }
312 // Special-case handling for moves & Phi
313 if (attrs & (DF_IS_MOVE | DF_NULL_TRANSFER_N)) {
314 /*
315 * If any of our inputs or outputs is defined, set all.
316 * Some ugliness related to Phi nodes and wide values.
317 * The Phi set will include all low words or all high
318 * words, so we have to treat them specially.
319 */
320 bool is_phi = (static_cast<int>(mir->dalvikInsn.opcode) ==
321 kMirOpPhi);
322 RegLocation rl_temp = reg_location_[defs[0]];
323 bool defined_fp = rl_temp.defined && rl_temp.fp;
324 bool defined_core = rl_temp.defined && rl_temp.core;
325 bool defined_ref = rl_temp.defined && rl_temp.ref;
326 bool is_wide = rl_temp.wide || ((attrs & DF_A_WIDE) != 0);
327 bool is_high = is_phi && rl_temp.wide && rl_temp.high_word;
328 for (int i = 0; i < ssa_rep->num_uses; i++) {
329 rl_temp = reg_location_[uses[i]];
330 defined_fp |= rl_temp.defined && rl_temp.fp;
331 defined_core |= rl_temp.defined && rl_temp.core;
332 defined_ref |= rl_temp.defined && rl_temp.ref;
333 is_wide |= rl_temp.wide;
334 is_high |= is_phi && rl_temp.wide && rl_temp.high_word;
335 }
336 /*
337 * We don't normally expect to see a Dalvik register definition used both as a
338 * floating point and core value, though technically it could happen with constants.
339 * Until we have proper typing, detect this situation and disable register promotion
340 * (which relies on the distinction between core a fp usages).
341 */
342 if ((defined_fp && (defined_core | defined_ref)) &&
343 ((cu_->disable_opt & (1 << kPromoteRegs)) == 0)) {
344 LOG(WARNING) << PrettyMethod(cu_->method_idx, *cu_->dex_file)
345 << " op at block " << bb->id
346 << " has both fp and core/ref uses for same def.";
347 cu_->disable_opt |= (1 << kPromoteRegs);
348 }
349 changed |= SetFp(defs[0], defined_fp);
350 changed |= SetCore(defs[0], defined_core);
351 changed |= SetRef(defs[0], defined_ref);
352 changed |= SetWide(defs[0], is_wide);
353 changed |= SetHigh(defs[0], is_high);
354 if (attrs & DF_A_WIDE) {
355 changed |= SetWide(defs[1]);
356 changed |= SetHigh(defs[1]);
357 }
358 for (int i = 0; i < ssa_rep->num_uses; i++) {
359 changed |= SetFp(uses[i], defined_fp);
360 changed |= SetCore(uses[i], defined_core);
361 changed |= SetRef(uses[i], defined_ref);
362 changed |= SetWide(uses[i], is_wide);
363 changed |= SetHigh(uses[i], is_high);
364 }
365 if (attrs & DF_A_WIDE) {
366 DCHECK_EQ(ssa_rep->num_uses, 2);
367 changed |= SetWide(uses[1]);
368 changed |= SetHigh(uses[1]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 }
370 }
371 }
372 return changed;
373}
374
375static const char* storage_name[] = {" Frame ", "PhysReg", " Spill "};
376
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700377void MIRGraph::DumpRegLocTable(RegLocation* table, int count) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700378 // FIXME: Quick-specific. Move to Quick (and make a generic version for MIRGraph?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379 Mir2Lir* cg = static_cast<Mir2Lir*>(cu_->cg.get());
380 if (cg != NULL) {
381 for (int i = 0; i < count; i++) {
382 LOG(INFO) << StringPrintf("Loc[%02d] : %s, %c %c %c %c %c %c %c%d %c%d S%d",
383 table[i].orig_sreg, storage_name[table[i].location],
384 table[i].wide ? 'W' : 'N', table[i].defined ? 'D' : 'U',
385 table[i].fp ? 'F' : table[i].ref ? 'R' :'C',
386 table[i].is_const ? 'c' : 'n',
387 table[i].high_word ? 'H' : 'L', table[i].home ? 'h' : 't',
388 cg->IsFpReg(table[i].low_reg) ? 's' : 'r',
389 table[i].low_reg & cg->FpRegMask(),
390 cg->IsFpReg(table[i].high_reg) ? 's' : 'r',
391 table[i].high_reg & cg->FpRegMask(), table[i].s_reg_low);
392 }
393 } else {
394 // Either pre-regalloc or Portable.
395 for (int i = 0; i < count; i++) {
396 LOG(INFO) << StringPrintf("Loc[%02d] : %s, %c %c %c %c %c %c S%d",
397 table[i].orig_sreg, storage_name[table[i].location],
398 table[i].wide ? 'W' : 'N', table[i].defined ? 'D' : 'U',
399 table[i].fp ? 'F' : table[i].ref ? 'R' :'C',
400 table[i].is_const ? 'c' : 'n',
401 table[i].high_word ? 'H' : 'L', table[i].home ? 'h' : 't',
402 table[i].s_reg_low);
403 }
404 }
405}
406
407static const RegLocation fresh_loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
408 INVALID_REG, INVALID_REG, INVALID_SREG,
409 INVALID_SREG};
410
buzbee1da1e2f2013-11-15 13:37:01 -0800411void MIRGraph::InitRegLocations() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 /* Allocate the location map */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700413 RegLocation* loc = static_cast<RegLocation*>(arena_->Alloc(GetNumSSARegs() * sizeof(*loc),
414 ArenaAllocator::kAllocRegAlloc));
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700415 for (int i = 0; i < GetNumSSARegs(); i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 loc[i] = fresh_loc;
417 loc[i].s_reg_low = i;
418 loc[i].is_const = is_constant_v_->IsBitSet(i);
419 }
420
421 /* Patch up the locations for Method* and the compiler temps */
422 loc[method_sreg_].location = kLocCompilerTemp;
423 loc[method_sreg_].defined = true;
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700424 for (int i = 0; i < cu_->num_compiler_temps; i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 CompilerTemp* ct = compiler_temps_.Get(i);
426 loc[ct->s_reg].location = kLocCompilerTemp;
427 loc[ct->s_reg].defined = true;
428 }
429
430 reg_location_ = loc;
431
432 int num_regs = cu_->num_dalvik_registers;
433
434 /* Add types of incoming arguments based on signature */
435 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) {
439 // For non-static, skip past "this"
440 reg_location_[s_reg].defined = true;
441 reg_location_[s_reg].ref = true;
442 s_reg++;
443 }
444 const char* shorty = cu_->shorty;
445 int shorty_len = strlen(shorty);
446 for (int i = 1; i < shorty_len; i++) {
447 switch (shorty[i]) {
448 case 'D':
449 reg_location_[s_reg].wide = true;
450 reg_location_[s_reg+1].high_word = true;
451 reg_location_[s_reg+1].fp = true;
452 DCHECK_EQ(SRegToVReg(s_reg)+1, SRegToVReg(s_reg+1));
453 reg_location_[s_reg].fp = true;
454 reg_location_[s_reg].defined = true;
455 s_reg++;
456 break;
457 case 'J':
458 reg_location_[s_reg].wide = true;
459 reg_location_[s_reg+1].high_word = true;
460 DCHECK_EQ(SRegToVReg(s_reg)+1, SRegToVReg(s_reg+1));
461 reg_location_[s_reg].core = true;
462 reg_location_[s_reg].defined = true;
463 s_reg++;
464 break;
465 case 'F':
466 reg_location_[s_reg].fp = true;
467 reg_location_[s_reg].defined = true;
468 break;
469 case 'L':
470 reg_location_[s_reg].ref = true;
471 reg_location_[s_reg].defined = true;
472 break;
473 default:
474 reg_location_[s_reg].core = true;
475 reg_location_[s_reg].defined = true;
476 break;
477 }
478 s_reg++;
479 }
480 }
buzbee1da1e2f2013-11-15 13:37:01 -0800481}
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482
buzbee1da1e2f2013-11-15 13:37:01 -0800483/*
484 * Set the s_reg_low field to refer to the pre-SSA name of the
485 * base Dalvik virtual register. Once we add a better register
486 * allocator, remove this remapping.
487 */
488void MIRGraph::RemapRegLocations() {
Brian Carlstrom38f85e42013-07-18 14:45:22 -0700489 for (int i = 0; i < GetNumSSARegs(); i++) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 if (reg_location_[i].location != kLocCompilerTemp) {
491 int orig_sreg = reg_location_[i].s_reg_low;
492 reg_location_[i].orig_sreg = orig_sreg;
493 reg_location_[i].s_reg_low = SRegToVReg(orig_sreg);
494 }
495 }
496}
497
498} // namespace art