blob: 4eab177d1e1575aa14c8d728ab6085537dad59e2 [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
buzbee52a77fc2012-11-20 19:50:46 -080023static bool SetFp(CompilationUnit* cUnit, int index, bool isFP) {
Bill Buzbeea114add2012-05-03 15:00:40 -070024 bool change = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070025 if (isFP && !cUnit->regLocation[index].fp) {
26 cUnit->regLocation[index].fp = true;
27 cUnit->regLocation[index].defined = true;
28 change = true;
29 }
30 return change;
buzbee67bc2362011-10-11 18:08:40 -070031}
32
buzbee52a77fc2012-11-20 19:50:46 -080033static bool SetCore(CompilationUnit* cUnit, int index, bool isCore) {
Bill Buzbeea114add2012-05-03 15:00:40 -070034 bool change = false;
Bill Buzbeea114add2012-05-03 15:00:40 -070035 if (isCore && !cUnit->regLocation[index].defined) {
36 cUnit->regLocation[index].core = true;
37 cUnit->regLocation[index].defined = true;
38 change = true;
39 }
40 return change;
buzbee03fa2632011-09-20 17:10:57 -070041}
42
buzbee52a77fc2012-11-20 19:50:46 -080043static bool SetRef(CompilationUnit* cUnit, int index, bool isRef) {
buzbeebff24652012-05-06 16:22:05 -070044 bool change = false;
buzbeebff24652012-05-06 16:22:05 -070045 if (isRef && !cUnit->regLocation[index].defined) {
46 cUnit->regLocation[index].ref = true;
47 cUnit->regLocation[index].defined = true;
48 change = true;
49 }
50 return change;
51}
52
buzbee52a77fc2012-11-20 19:50:46 -080053static bool SetWide(CompilationUnit* cUnit, int index, bool isWide) {
buzbee2a83e8f2012-07-13 16:42:30 -070054 bool change = false;
55 if (isWide && !cUnit->regLocation[index].wide) {
56 cUnit->regLocation[index].wide = true;
57 change = true;
58 }
59 return change;
60}
61
buzbee52a77fc2012-11-20 19:50:46 -080062static bool SetHigh(CompilationUnit* cUnit, int index, bool isHigh) {
buzbee2a83e8f2012-07-13 16:42:30 -070063 bool change = false;
64 if (isHigh && !cUnit->regLocation[index].highWord) {
65 cUnit->regLocation[index].highWord = true;
66 change = true;
67 }
68 return change;
69}
70
buzbee52a77fc2012-11-20 19:50:46 -080071static bool RemapNames(CompilationUnit* cUnit, BasicBlock* bb)
buzbeec0ecd652011-09-25 18:11:54 -070072{
Bill Buzbeea114add2012-05-03 15:00:40 -070073 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock &&
74 bb->blockType != kExitBlock)
buzbeec0ecd652011-09-25 18:11:54 -070075 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -070076
77 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
78 SSARepresentation *ssaRep = mir->ssaRep;
79 if (ssaRep) {
80 for (int i = 0; i < ssaRep->numUses; i++) {
81 ssaRep->uses[i] = cUnit->phiAliasMap[ssaRep->uses[i]];
82 }
83 for (int i = 0; i < ssaRep->numDefs; i++) {
84 ssaRep->defs[i] = cUnit->phiAliasMap[ssaRep->defs[i]];
85 }
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 */
buzbee52a77fc2012-11-20 19:50:46 -080096static bool InferTypeAndSize(CompilationUnit* cUnit, 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
Bill Buzbeea114add2012-05-03 15:00:40 -0700101 if (bb->dataFlowInfo == NULL) return false;
102 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock)
103 return false;
buzbee67bf8852011-08-17 17:51:35 -0700104
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
106 SSARepresentation *ssaRep = mir->ssaRep;
107 if (ssaRep) {
108 int attrs = oatDataFlowAttributes[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) {
buzbee52a77fc2012-11-20 19:50:46 -0800113 changed |= SetCore(cUnit, ssaRep->defs[0], true);
buzbee67bf8852011-08-17 17:51:35 -0700114 }
buzbeebff24652012-05-06 16:22:05 -0700115 if (attrs & DF_REF_A) {
buzbee52a77fc2012-11-20 19:50:46 -0800116 changed |= SetRef(cUnit, ssaRep->defs[0], true);
buzbeebff24652012-05-06 16:22:05 -0700117 }
118 if (attrs & DF_A_WIDE) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 cUnit->regLocation[ssaRep->defs[0]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700120 cUnit->regLocation[ssaRep->defs[1]].wide = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700121 cUnit->regLocation[ssaRep->defs[1]].highWord = true;
122 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->defs[0])+1,
123 SRegToVReg(cUnit, ssaRep->defs[1]));
124 }
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) {
buzbee52a77fc2012-11-20 19:50:46 -0800131 changed |= SetCore(cUnit, ssaRep->uses[next], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 }
buzbeebff24652012-05-06 16:22:05 -0700133 if (attrs & DF_REF_A) {
buzbee52a77fc2012-11-20 19:50:46 -0800134 changed |= SetRef(cUnit, ssaRep->uses[next], true);
buzbeebff24652012-05-06 16:22:05 -0700135 }
136 if (attrs & DF_A_WIDE) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 cUnit->regLocation[ssaRep->uses[next]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700138 cUnit->regLocation[ssaRep->uses[next + 1]].wide = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700139 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
140 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
141 SRegToVReg(cUnit, ssaRep->uses[next + 1]));
142 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800149 changed |= SetCore(cUnit, ssaRep->uses[next], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 }
buzbeebff24652012-05-06 16:22:05 -0700151 if (attrs & DF_REF_B) {
buzbee52a77fc2012-11-20 19:50:46 -0800152 changed |= SetRef(cUnit, ssaRep->uses[next], true);
buzbeebff24652012-05-06 16:22:05 -0700153 }
154 if (attrs & DF_B_WIDE) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 cUnit->regLocation[ssaRep->uses[next]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700156 cUnit->regLocation[ssaRep->uses[next + 1]].wide = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
158 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
159 SRegToVReg(cUnit, ssaRep->uses[next + 1]));
160 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800167 changed |= SetCore(cUnit, ssaRep->uses[next], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 }
buzbeebff24652012-05-06 16:22:05 -0700169 if (attrs & DF_REF_C) {
buzbee52a77fc2012-11-20 19:50:46 -0800170 changed |= SetRef(cUnit, ssaRep->uses[next], true);
buzbeebff24652012-05-06 16:22:05 -0700171 }
172 if (attrs & DF_C_WIDE) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 cUnit->regLocation[ssaRep->uses[next]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700174 cUnit->regLocation[ssaRep->uses[next + 1]].wide = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
176 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
177 SRegToVReg(cUnit, ssaRep->uses[next + 1]));
178 }
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)) {
185 switch(cUnit->shorty[0]) {
186 case 'I':
buzbee52a77fc2012-11-20 19:50:46 -0800187 changed |= SetCore(cUnit, ssaRep->uses[0], true);
buzbeed5018892012-07-11 14:23:40 -0700188 break;
189 case 'J':
buzbee52a77fc2012-11-20 19:50:46 -0800190 changed |= SetCore(cUnit, ssaRep->uses[0], true);
191 changed |= SetCore(cUnit, ssaRep->uses[1], true);
buzbeed5018892012-07-11 14:23:40 -0700192 cUnit->regLocation[ssaRep->uses[0]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700193 cUnit->regLocation[ssaRep->uses[1]].wide = true;
buzbeed5018892012-07-11 14:23:40 -0700194 cUnit->regLocation[ssaRep->uses[1]].highWord = true;
195 break;
196 case 'F':
buzbee52a77fc2012-11-20 19:50:46 -0800197 changed |= SetFp(cUnit, ssaRep->uses[0], true);
buzbeed5018892012-07-11 14:23:40 -0700198 break;
199 case 'D':
buzbee52a77fc2012-11-20 19:50:46 -0800200 changed |= SetFp(cUnit, ssaRep->uses[0], true);
201 changed |= SetFp(cUnit, ssaRep->uses[1], true);
buzbeed5018892012-07-11 14:23:40 -0700202 cUnit->regLocation[ssaRep->uses[0]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700203 cUnit->regLocation[ssaRep->uses[1]].wide = true;
buzbeed5018892012-07-11 14:23:40 -0700204 cUnit->regLocation[ssaRep->uses[1]].highWord = true;
205 break;
206 case 'L':
buzbee52a77fc2012-11-20 19:50:46 -0800207 changed |= SetRef(cUnit, ssaRep->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;
buzbee52a77fc2012-11-20 19:50:46 -0800221 const char* shorty = GetShortyFromTargetIdx(cUnit, target_idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 // Handle result type if floating point
223 if ((shorty[0] == 'F') || (shorty[0] == 'D')) {
buzbee52a77fc2012-11-20 19:50:46 -0800224 MIR* moveResultMIR = FindMoveResult(cUnit, bb, mir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 // Result might not be used at all, so no move-result
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700226 if (moveResultMIR && (moveResultMIR->dalvikInsn.opcode !=
227 Instruction::MOVE_RESULT_OBJECT)) {
228 SSARepresentation* tgtRep = moveResultMIR->ssaRep;
229 DCHECK(tgtRep != NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 tgtRep->fpDef[0] = true;
buzbee52a77fc2012-11-20 19:50:46 -0800231 changed |= SetFp(cUnit, tgtRep->defs[0], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 if (shorty[0] == 'D') {
233 tgtRep->fpDef[1] = true;
buzbee52a77fc2012-11-20 19:50:46 -0800234 changed |= SetFp(cUnit, tgtRep->defs[1], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 }
236 }
237 }
238 int numUses = 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))) {
242 cUnit->regLocation[ssaRep->uses[next]].defined = true;
buzbeebff24652012-05-06 16:22:05 -0700243 cUnit->regLocation[ssaRep->uses[next]].ref = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 next++;
245 }
246 uint32_t cpos = 1;
247 if (strlen(shorty) > 1) {
248 for (int i = next; i < numUses;) {
249 DCHECK_LT(cpos, strlen(shorty));
250 switch (shorty[cpos++]) {
251 case 'D':
252 ssaRep->fpUse[i] = true;
253 ssaRep->fpUse[i+1] = true;
254 cUnit->regLocation[ssaRep->uses[i]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700255 cUnit->regLocation[ssaRep->uses[i+1]].wide = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 cUnit->regLocation[ssaRep->uses[i+1]].highWord = true;
257 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[i])+1,
258 SRegToVReg(cUnit, ssaRep->uses[i+1]));
259 i++;
260 break;
261 case 'J':
262 cUnit->regLocation[ssaRep->uses[i]].wide = true;
buzbee2a83e8f2012-07-13 16:42:30 -0700263 cUnit->regLocation[ssaRep->uses[i+1]].wide = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 cUnit->regLocation[ssaRep->uses[i+1]].highWord = true;
265 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[i])+1,
266 SRegToVReg(cUnit, ssaRep->uses[i+1]));
buzbee52a77fc2012-11-20 19:50:46 -0800267 changed |= SetCore(cUnit, ssaRep->uses[i],true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 i++;
269 break;
270 case 'F':
271 ssaRep->fpUse[i] = true;
272 break;
buzbeebff24652012-05-06 16:22:05 -0700273 case 'L':
buzbee52a77fc2012-11-20 19:50:46 -0800274 changed |= SetRef(cUnit,ssaRep->uses[i], true);
buzbeebff24652012-05-06 16:22:05 -0700275 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 default:
buzbee52a77fc2012-11-20 19:50:46 -0800277 changed |= SetCore(cUnit,ssaRep->uses[i], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 break;
279 }
280 i++;
281 }
282 }
283 }
284
285 for (int i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) {
286 if (ssaRep->fpUse[i])
buzbee52a77fc2012-11-20 19:50:46 -0800287 changed |= SetFp(cUnit, ssaRep->uses[i], true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 }
289 for (int i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) {
290 if (ssaRep->fpDef[i])
buzbee52a77fc2012-11-20 19:50:46 -0800291 changed |= SetFp(cUnit, ssaRep->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 */
301 bool isPhi = (static_cast<int>(mir->dalvikInsn.opcode) ==
302 kMirOpPhi);
303 RegLocation rlTemp = cUnit->regLocation[ssaRep->defs[0]];
304 bool definedFP = rlTemp.defined && rlTemp.fp;
305 bool definedCore = rlTemp.defined && rlTemp.core;
306 bool definedRef = rlTemp.defined && rlTemp.ref;
307 bool isWide = rlTemp.wide || ((attrs & DF_A_WIDE) != 0);
308 bool isHigh = isPhi && rlTemp.wide && rlTemp.highWord;
309 for (int i = 0; i < ssaRep->numUses;i++) {
310 rlTemp = cUnit->regLocation[ssaRep->uses[i]];
311 definedFP |= rlTemp.defined && rlTemp.fp;
312 definedCore |= rlTemp.defined && rlTemp.core;
313 definedRef |= rlTemp.defined && rlTemp.ref;
314 isWide |= rlTemp.wide;
315 isHigh |= isPhi && rlTemp.wide && rlTemp.highWord;
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 */
buzbeebff24652012-05-06 16:22:05 -0700331 if ((definedFP && (definedCore | definedRef)) &&
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 ((cUnit->disableOpt & (1 << kPromoteRegs)) == 0)) {
333 LOG(WARNING) << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
334 << " op at block " << bb->id
buzbeebff24652012-05-06 16:22:05 -0700335 << " has both fp and core/ref uses for same def.";
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 cUnit->disableOpt |= (1 << kPromoteRegs);
337 }
buzbee52a77fc2012-11-20 19:50:46 -0800338 changed |= SetFp(cUnit, ssaRep->defs[0], definedFP);
339 changed |= SetCore(cUnit, ssaRep->defs[0], definedCore);
340 changed |= SetRef(cUnit, ssaRep->defs[0], definedRef);
341 changed |= SetWide(cUnit, ssaRep->defs[0], isWide);
342 changed |= SetHigh(cUnit, ssaRep->defs[0], isHigh);
buzbee2a83e8f2012-07-13 16:42:30 -0700343 if (attrs & DF_A_WIDE) {
buzbee52a77fc2012-11-20 19:50:46 -0800344 changed |= SetWide(cUnit, ssaRep->defs[1], true);
345 changed |= SetHigh(cUnit, ssaRep->defs[1], true);
buzbee2a83e8f2012-07-13 16:42:30 -0700346 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700347 for (int i = 0; i < ssaRep->numUses; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800348 changed |= SetFp(cUnit, ssaRep->uses[i], definedFP);
349 changed |= SetCore(cUnit, ssaRep->uses[i], definedCore);
350 changed |= SetRef(cUnit, ssaRep->uses[i], definedRef);
351 changed |= SetWide(cUnit, ssaRep->uses[i], isWide);
352 changed |= SetHigh(cUnit, ssaRep->uses[i], isHigh);
buzbee2a83e8f2012-07-13 16:42:30 -0700353 }
354 if (attrs & DF_A_WIDE) {
355 DCHECK_EQ(ssaRep->numUses, 2);
buzbee52a77fc2012-11-20 19:50:46 -0800356 changed |= SetWide(cUnit, ssaRep->uses[1], true);
357 changed |= SetHigh(cUnit, ssaRep->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
365static const char* storageName[] = {" Frame ", "PhysReg", " Spill "};
366
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",
buzbeeca7a5e42012-08-20 11:12:18 -0700371 table[i].origSReg, storageName[table[i].location],
372 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',
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 table[i].highWord ? 'H' : 'L', table[i].home ? 'h' : 't',
buzbee52a77fc2012-11-20 19:50:46 -0800375 IsFpReg(table[i].lowReg) ? 's' : 'r',
376 table[i].lowReg & FpRegMask(),
377 IsFpReg(table[i].highReg) ? 's' : 'r',
378 table[i].highReg & FpRegMask(), table[i].sRegLow);
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 }
buzbee67bf8852011-08-17 17:51:35 -0700380}
381
buzbee2cfc6392012-05-07 14:51:40 -0700382static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
383 INVALID_REG, INVALID_REG, INVALID_SREG,
384 INVALID_SREG};
buzbee67bf8852011-08-17 17:51:35 -0700385
buzbee52a77fc2012-11-20 19:50:46 -0800386int ComputeFrameSize(CompilationUnit* cUnit) {
buzbeead8f15e2012-06-18 14:49:45 -0700387 /* Figure out the frame size */
388 static const uint32_t kAlignMask = kStackAlignment - 1;
389 uint32_t size = (cUnit->numCoreSpills + cUnit->numFPSpills +
390 1 /* filler word */ + cUnit->numRegs + cUnit->numOuts +
391 cUnit->numCompilerTemps + 1 /* curMethod* */)
392 * 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800403void SimpleRegAlloc(CompilationUnit* cUnit)
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800409 loc = static_cast<RegLocation*>(NewMem(cUnit, cUnit->numSSARegs * sizeof(*loc),
buzbeecbd6d442012-11-17 14:11:25 -0800410 true, kAllocRegAlloc));
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 for (i=0; i< cUnit->numSSARegs; i++) {
412 loc[i] = freshLoc;
413 loc[i].sRegLow = i;
buzbee52a77fc2012-11-20 19:50:46 -0800414 loc[i].isConst = IsBitSet(cUnit->isConstantV, i);
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 }
416
417 /* Patch up the locations for Method* and the compiler temps */
418 loc[cUnit->methodSReg].location = kLocCompilerTemp;
419 loc[cUnit->methodSReg].defined = true;
420 for (i = 0; i < cUnit->numCompilerTemps; i++) {
buzbeecbd6d442012-11-17 14:11:25 -0800421 CompilerTemp* ct = reinterpret_cast<CompilerTemp*>(cUnit->compilerTemps.elemList[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 loc[ct->sReg].location = kLocCompilerTemp;
423 loc[ct->sReg].defined = true;
424 }
425
426 cUnit->regLocation = loc;
427
428 /* Allocation the promotion map */
429 int numRegs = cUnit->numDalvikRegisters;
buzbeecbd6d442012-11-17 14:11:25 -0800430 cUnit->promotionMap = static_cast<PromotionMap*>
buzbee52a77fc2012-11-20 19:50:46 -0800431 (NewMem(cUnit, (numRegs + cUnit->numCompilerTemps + 1) * sizeof(cUnit->promotionMap[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 */
435 int numIns = cUnit->numIns;
436 if (numIns > 0) {
437 int sReg = numRegs - numIns;
438 if ((cUnit->access_flags & kAccStatic) == 0) {
439 // For non-static, skip past "this"
440 cUnit->regLocation[sReg].defined = true;
buzbeebff24652012-05-06 16:22:05 -0700441 cUnit->regLocation[sReg].ref = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700442 sReg++;
buzbee67bf8852011-08-17 17:51:35 -0700443 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 const char* shorty = cUnit->shorty;
445 int shorty_len = strlen(shorty);
446 for (int i = 1; i < shorty_len; i++) {
447 switch (shorty[i]) {
448 case 'D':
449 cUnit->regLocation[sReg].wide = true;
450 cUnit->regLocation[sReg+1].highWord = true;
451 cUnit->regLocation[sReg+1].fp = true;
452 DCHECK_EQ(SRegToVReg(cUnit, sReg)+1, SRegToVReg(cUnit, sReg+1));
453 cUnit->regLocation[sReg].fp = true;
454 cUnit->regLocation[sReg].defined = true;
455 sReg++;
456 break;
457 case 'J':
458 cUnit->regLocation[sReg].wide = true;
459 cUnit->regLocation[sReg+1].highWord = true;
460 DCHECK_EQ(SRegToVReg(cUnit, sReg)+1, SRegToVReg(cUnit, sReg+1));
461 cUnit->regLocation[sReg].core = true;
462 cUnit->regLocation[sReg].defined = true;
463 sReg++;
buzbeed5018892012-07-11 14:23:40 -0700464 break;
465 case 'F':
466 cUnit->regLocation[sReg].fp = true;
467 cUnit->regLocation[sReg].defined = true;
468 break;
469 case 'L':
470 cUnit->regLocation[sReg].ref = true;
471 cUnit->regLocation[sReg].defined = true;
472 break;
473 default:
474 cUnit->regLocation[sReg].core = true;
475 cUnit->regLocation[sReg].defined = true;
476 break;
buzbeee9a72f62011-09-04 17:59:07 -0700477 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 sReg++;
479 }
480 }
481
buzbee2cfc6392012-05-07 14:51:40 -0700482 if (!cUnit->genBitcode) {
483 /* Remap names */
buzbee52a77fc2012-11-20 19:50:46 -0800484 DataFlowAnalysisDispatcher(cUnit, RemapNames,
buzbee2cfc6392012-05-07 14:51:40 -0700485 kPreOrderDFSTraversal,
486 false /* isIterative */);
487 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700488
489 /* Do type & size inference pass */
buzbee52a77fc2012-11-20 19:50:46 -0800490 DataFlowAnalysisDispatcher(cUnit, InferTypeAndSize,
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 kPreOrderDFSTraversal,
492 true /* isIterative */);
493
494 /*
495 * Set the sRegLow field to refer to the pre-SSA name of the
496 * base Dalvik virtual register. Once we add a better register
497 * allocator, remove this remapping.
498 */
499 for (i=0; i < cUnit->numSSARegs; i++) {
500 if (cUnit->regLocation[i].location != kLocCompilerTemp) {
buzbee2cfc6392012-05-07 14:51:40 -0700501 int origSReg = cUnit->regLocation[i].sRegLow;
502 cUnit->regLocation[i].origSReg = origSReg;
503 cUnit->regLocation[i].sRegLow = SRegToVReg(cUnit, origSReg);
buzbeee9a72f62011-09-04 17:59:07 -0700504 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 }
buzbeee9a72f62011-09-04 17:59:07 -0700506
Bill Buzbeea114add2012-05-03 15:00:40 -0700507 cUnit->coreSpillMask = 0;
508 cUnit->fpSpillMask = 0;
509 cUnit->numCoreSpills = 0;
buzbeec0ecd652011-09-25 18:11:54 -0700510
buzbee52a77fc2012-11-20 19:50:46 -0800511 DoPromotion(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700512
buzbeead8f15e2012-06-18 14:49:45 -0700513 /* Get easily-accessable post-promotion copy of RegLocation for Method* */
514 cUnit->methodLoc = cUnit->regLocation[cUnit->methodSReg];
515
Bill Buzbeea114add2012-05-03 15:00:40 -0700516 if (cUnit->printMe && !(cUnit->disableOpt & (1 << kPromoteRegs))) {
517 LOG(INFO) << "After Promotion";
buzbee52a77fc2012-11-20 19:50:46 -0800518 DumpRegLocTable(cUnit->regLocation, cUnit->numSSARegs);
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800522 cUnit->frameSize = ComputeFrameSize(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700523}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800524
525} // namespace art