blob: 54728db60712ba67cc5508090af58b38bc688f28 [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
17#include "Dalvik.h"
18#include "CompilerInternals.h"
19#include "Dataflow.h"
20#include "codegen/Ralloc.h"
21
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080022namespace art {
23
buzbee31a4a6f2012-02-28 15:36:15 -080024bool setFp(CompilationUnit* cUnit, int index, bool isFP) {
Bill Buzbeea114add2012-05-03 15:00:40 -070025 bool change = false;
26 if (cUnit->regLocation[index].highWord) {
buzbee67bc2362011-10-11 18:08:40 -070027 return change;
Bill Buzbeea114add2012-05-03 15:00:40 -070028 }
29 if (isFP && !cUnit->regLocation[index].fp) {
30 cUnit->regLocation[index].fp = true;
31 cUnit->regLocation[index].defined = true;
32 change = true;
33 }
34 return change;
buzbee67bc2362011-10-11 18:08:40 -070035}
36
buzbee31a4a6f2012-02-28 15:36:15 -080037bool setCore(CompilationUnit* cUnit, int index, bool isCore) {
Bill Buzbeea114add2012-05-03 15:00:40 -070038 bool change = false;
39 if (cUnit->regLocation[index].highWord) {
buzbee03fa2632011-09-20 17:10:57 -070040 return change;
Bill Buzbeea114add2012-05-03 15:00:40 -070041 }
42 if (isCore && !cUnit->regLocation[index].defined) {
43 cUnit->regLocation[index].core = true;
44 cUnit->regLocation[index].defined = true;
45 change = true;
46 }
47 return change;
buzbee03fa2632011-09-20 17:10:57 -070048}
49
buzbee31a4a6f2012-02-28 15:36:15 -080050bool remapNames(CompilationUnit* cUnit, BasicBlock* bb)
buzbeec0ecd652011-09-25 18:11:54 -070051{
Bill Buzbeea114add2012-05-03 15:00:40 -070052 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock &&
53 bb->blockType != kExitBlock)
buzbeec0ecd652011-09-25 18:11:54 -070054 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -070055
56 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
57 SSARepresentation *ssaRep = mir->ssaRep;
58 if (ssaRep) {
59 for (int i = 0; i < ssaRep->numUses; i++) {
60 ssaRep->uses[i] = cUnit->phiAliasMap[ssaRep->uses[i]];
61 }
62 for (int i = 0; i < ssaRep->numDefs; i++) {
63 ssaRep->defs[i] = cUnit->phiAliasMap[ssaRep->defs[i]];
64 }
65 }
66 }
67 return false;
buzbeec0ecd652011-09-25 18:11:54 -070068}
69
buzbee769fde12012-01-05 17:35:23 -080070// Try to find the next move result which might have an FP target
buzbee31a4a6f2012-02-28 15:36:15 -080071SSARepresentation* findMoveResult(MIR* mir)
buzbee769fde12012-01-05 17:35:23 -080072{
Bill Buzbeea114add2012-05-03 15:00:40 -070073 SSARepresentation* res = NULL;
74 for (; mir; mir = mir->next) {
75 if ((mir->dalvikInsn.opcode == Instruction::MOVE_RESULT) ||
76 (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_WIDE)) {
77 res = mir->ssaRep;
78 break;
buzbee769fde12012-01-05 17:35:23 -080079 }
Bill Buzbeea114add2012-05-03 15:00:40 -070080 }
81 return res;
buzbee769fde12012-01-05 17:35:23 -080082}
83
buzbee67bf8852011-08-17 17:51:35 -070084/*
buzbee03fa2632011-09-20 17:10:57 -070085 * Infer types and sizes. We don't need to track change on sizes,
86 * as it doesn't propagate. We're guaranteed at least one pass through
87 * the cfg.
buzbee67bf8852011-08-17 17:51:35 -070088 */
buzbee31a4a6f2012-02-28 15:36:15 -080089bool inferTypeAndSize(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -070090{
Bill Buzbeea114add2012-05-03 15:00:40 -070091 MIR *mir;
92 bool changed = false; // Did anything change?
buzbee03fa2632011-09-20 17:10:57 -070093
Bill Buzbeea114add2012-05-03 15:00:40 -070094 if (bb->dataFlowInfo == NULL) return false;
95 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock)
96 return false;
buzbee67bf8852011-08-17 17:51:35 -070097
Bill Buzbeea114add2012-05-03 15:00:40 -070098 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
99 SSARepresentation *ssaRep = mir->ssaRep;
100 if (ssaRep) {
101 int attrs = oatDataFlowAttributes[mir->dalvikInsn.opcode];
buzbee67bc2362011-10-11 18:08:40 -0700102
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 // Handle defs
104 if (attrs & (DF_DA | DF_DA_WIDE)) {
105 if (attrs & DF_CORE_A) {
106 changed |= setCore(cUnit, ssaRep->defs[0], true);
buzbee67bf8852011-08-17 17:51:35 -0700107 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 if (attrs & DF_DA_WIDE) {
109 cUnit->regLocation[ssaRep->defs[0]].wide = true;
110 cUnit->regLocation[ssaRep->defs[1]].highWord = true;
111 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->defs[0])+1,
112 SRegToVReg(cUnit, ssaRep->defs[1]));
113 }
114 }
115
116 // Handles uses
117 int next = 0;
118 if (attrs & (DF_UA | DF_UA_WIDE)) {
119 if (attrs & DF_CORE_A) {
120 changed |= setCore(cUnit, ssaRep->uses[next], true);
121 }
122 if (attrs & DF_UA_WIDE) {
123 cUnit->regLocation[ssaRep->uses[next]].wide = true;
124 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
125 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
126 SRegToVReg(cUnit, ssaRep->uses[next + 1]));
127 next += 2;
128 } else {
129 next++;
130 }
131 }
132 if (attrs & (DF_UB | DF_UB_WIDE)) {
133 if (attrs & DF_CORE_B) {
134 changed |= setCore(cUnit, ssaRep->uses[next], true);
135 }
136 if (attrs & DF_UB_WIDE) {
137 cUnit->regLocation[ssaRep->uses[next]].wide = true;
138 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
139 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
140 SRegToVReg(cUnit, ssaRep->uses[next + 1]));
141 next += 2;
142 } else {
143 next++;
144 }
145 }
146 if (attrs & (DF_UC | DF_UC_WIDE)) {
147 if (attrs & DF_CORE_C) {
148 changed |= setCore(cUnit, ssaRep->uses[next], true);
149 }
150 if (attrs & DF_UC_WIDE) {
151 cUnit->regLocation[ssaRep->uses[next]].wide = true;
152 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
153 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
154 SRegToVReg(cUnit, ssaRep->uses[next + 1]));
155 }
156 }
157
158 // Special-case handling for format 35c/3rc invokes
159 Instruction::Code opcode = mir->dalvikInsn.opcode;
160 int flags = (static_cast<int>(opcode) >= kNumPackedOpcodes)
161 ? 0 : Instruction::Flags(mir->dalvikInsn.opcode);
162 if ((flags & Instruction::kInvoke) &&
163 (attrs & (DF_FORMAT_35C | DF_FORMAT_3RC))) {
164 DCHECK_EQ(next, 0);
165 int target_idx = mir->dalvikInsn.vB;
166 const char* shorty = oatGetShortyFromTargetIdx(cUnit, target_idx);
167 // Handle result type if floating point
168 if ((shorty[0] == 'F') || (shorty[0] == 'D')) {
169 // Find move-result that consumes this result
170 SSARepresentation* tgtRep = findMoveResult(mir->next);
171 // Might be in next basic block
172 if (!tgtRep) {
173 tgtRep = findMoveResult(bb->fallThrough->firstMIRInsn);
174 }
175 // Result might not be used at all, so no move-result
176 if (tgtRep) {
177 tgtRep->fpDef[0] = true;
178 changed |= setFp(cUnit, tgtRep->defs[0], true);
179 if (shorty[0] == 'D') {
180 tgtRep->fpDef[1] = true;
181 changed |= setFp(cUnit, tgtRep->defs[1], true);
182 }
183 }
184 }
185 int numUses = mir->dalvikInsn.vA;
186 // If this is a non-static invoke, skip implicit "this"
187 if (((mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC) &&
188 (mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC_RANGE))) {
189 cUnit->regLocation[ssaRep->uses[next]].defined = true;
190 cUnit->regLocation[ssaRep->uses[next]].core = true;
191 next++;
192 }
193 uint32_t cpos = 1;
194 if (strlen(shorty) > 1) {
195 for (int i = next; i < numUses;) {
196 DCHECK_LT(cpos, strlen(shorty));
197 switch (shorty[cpos++]) {
198 case 'D':
199 ssaRep->fpUse[i] = true;
200 ssaRep->fpUse[i+1] = true;
201 cUnit->regLocation[ssaRep->uses[i]].wide = true;
202 cUnit->regLocation[ssaRep->uses[i+1]].highWord = true;
203 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[i])+1,
204 SRegToVReg(cUnit, ssaRep->uses[i+1]));
205 i++;
206 break;
207 case 'J':
208 cUnit->regLocation[ssaRep->uses[i]].wide = true;
209 cUnit->regLocation[ssaRep->uses[i+1]].highWord = true;
210 DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[i])+1,
211 SRegToVReg(cUnit, ssaRep->uses[i+1]));
212 changed |= setCore(cUnit, ssaRep->uses[i],true);
213 i++;
214 break;
215 case 'F':
216 ssaRep->fpUse[i] = true;
217 break;
218 default:
219 changed |= setCore(cUnit,ssaRep->uses[i], true);
220 break;
221 }
222 i++;
223 }
224 }
225 }
226
227 for (int i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) {
228 if (ssaRep->fpUse[i])
229 changed |= setFp(cUnit, ssaRep->uses[i], true);
230 }
231 for (int i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) {
232 if (ssaRep->fpDef[i])
233 changed |= setFp(cUnit, ssaRep->defs[i], true);
234 }
235 // Special-case handling for moves & Phi
236 if (attrs & (DF_IS_MOVE | DF_NULL_TRANSFER_N)) {
237 // If any of our inputs or outputs is defined, set all
238 bool definedFP = false;
239 bool definedCore = false;
240 definedFP |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
241 cUnit->regLocation[ssaRep->defs[0]].fp);
242 definedCore |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
243 cUnit->regLocation[ssaRep->defs[0]].core);
244 for (int i = 0; i < ssaRep->numUses; i++) {
245 definedFP |= (cUnit->regLocation[ssaRep->uses[i]].defined &&
246 cUnit->regLocation[ssaRep->uses[i]].fp);
247 definedCore |= (cUnit->regLocation[ssaRep->uses[i]].defined
248 && cUnit->regLocation[ssaRep->uses[i]].core);
249 }
250 /*
251 * TODO: cleaner fix
252 * We don't normally expect to see a Dalvik register
253 * definition used both as a floating point and core
254 * value. However, the instruction rewriting that occurs
255 * during verification can eliminate some type information,
256 * leaving us confused. The real fix here is either to
257 * add explicit type information to Dalvik byte codes,
258 * or to recognize THROW_VERIFICATION_ERROR as
259 * an unconditional branch and support dead code elimination.
260 * As a workaround we can detect this situation and
261 * disable register promotion (which is the only thing that
262 * relies on distinctions between core and fp usages.
263 */
264 if ((definedFP && definedCore) &&
265 ((cUnit->disableOpt & (1 << kPromoteRegs)) == 0)) {
266 LOG(WARNING) << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
267 << " op at block " << bb->id
268 << " has both fp and core uses for same def.";
269 cUnit->disableOpt |= (1 << kPromoteRegs);
270 }
271 changed |= setFp(cUnit, ssaRep->defs[0], definedFP);
272 changed |= setCore(cUnit, ssaRep->defs[0], definedCore);
273 for (int i = 0; i < ssaRep->numUses; i++) {
274 changed |= setFp(cUnit, ssaRep->uses[i], definedFP);
275 changed |= setCore(cUnit, ssaRep->uses[i], definedCore);
276 }
277 }
buzbee67bf8852011-08-17 17:51:35 -0700278 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 }
280 return changed;
buzbee67bf8852011-08-17 17:51:35 -0700281}
282
283static const char* storageName[] = {" Frame ", "PhysReg", " Spill "};
284
buzbeedfd3d702011-08-28 12:56:51 -0700285void oatDumpRegLocTable(RegLocation* table, int count)
buzbee67bf8852011-08-17 17:51:35 -0700286{
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 for (int i = 0; i < count; i++) {
288 LOG(INFO) << StringPrintf("Loc[%02d] : %s, %c %c %c %c %c %c%d %c%d S%d",
289 i, storageName[table[i].location], table[i].wide ? 'W' : 'N',
290 table[i].defined ? 'D' : 'U', table[i].fp ? 'F' : 'C',
291 table[i].highWord ? 'H' : 'L', table[i].home ? 'h' : 't',
292 oatIsFpReg(table[i].lowReg) ? 's' : 'r',
293 table[i].lowReg & oatFpRegMask(),
294 oatIsFpReg(table[i].highReg) ? 's' : 'r',
295 table[i].highReg & oatFpRegMask(), table[i].sRegLow);
296 }
buzbee67bf8852011-08-17 17:51:35 -0700297}
298
buzbee67bc2362011-10-11 18:08:40 -0700299static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0,
300 INVALID_REG, INVALID_REG, INVALID_SREG};
buzbee67bf8852011-08-17 17:51:35 -0700301
302/*
303 * Simple register allocation. Some Dalvik virtual registers may
304 * be promoted to physical registers. Most of the work for temp
Ian Rogersb5d09b22012-03-06 22:14:17 -0800305 * allocation is done on the fly. We also do some initialization and
buzbee67bf8852011-08-17 17:51:35 -0700306 * type inference here.
307 */
308void oatSimpleRegAlloc(CompilationUnit* cUnit)
309{
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 int i;
311 RegLocation* loc;
buzbee67bf8852011-08-17 17:51:35 -0700312
Bill Buzbeea114add2012-05-03 15:00:40 -0700313 /* Allocate the location map */
314 loc = (RegLocation*)oatNew(cUnit, cUnit->numSSARegs * sizeof(*loc), true,
315 kAllocRegAlloc);
316 for (i=0; i< cUnit->numSSARegs; i++) {
317 loc[i] = freshLoc;
318 loc[i].sRegLow = i;
319 }
320
321 /* Patch up the locations for Method* and the compiler temps */
322 loc[cUnit->methodSReg].location = kLocCompilerTemp;
323 loc[cUnit->methodSReg].defined = true;
324 for (i = 0; i < cUnit->numCompilerTemps; i++) {
325 CompilerTemp* ct = (CompilerTemp*)cUnit->compilerTemps.elemList[i];
326 loc[ct->sReg].location = kLocCompilerTemp;
327 loc[ct->sReg].defined = true;
328 }
329
330 cUnit->regLocation = loc;
331
332 /* Allocation the promotion map */
333 int numRegs = cUnit->numDalvikRegisters;
334 cUnit->promotionMap =
335 (PromotionMap*)oatNew(cUnit, (numRegs + cUnit->numCompilerTemps + 1) *
336 sizeof(cUnit->promotionMap[0]), true,
337 kAllocRegAlloc);
338
339 /* Add types of incoming arguments based on signature */
340 int numIns = cUnit->numIns;
341 if (numIns > 0) {
342 int sReg = numRegs - numIns;
343 if ((cUnit->access_flags & kAccStatic) == 0) {
344 // For non-static, skip past "this"
345 cUnit->regLocation[sReg].defined = true;
346 cUnit->regLocation[sReg].core = true;
347 sReg++;
buzbee67bf8852011-08-17 17:51:35 -0700348 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 const char* shorty = cUnit->shorty;
350 int shorty_len = strlen(shorty);
351 for (int i = 1; i < shorty_len; i++) {
352 switch (shorty[i]) {
353 case 'D':
354 cUnit->regLocation[sReg].wide = true;
355 cUnit->regLocation[sReg+1].highWord = true;
356 cUnit->regLocation[sReg+1].fp = true;
357 DCHECK_EQ(SRegToVReg(cUnit, sReg)+1, SRegToVReg(cUnit, sReg+1));
358 cUnit->regLocation[sReg].fp = true;
359 cUnit->regLocation[sReg].defined = true;
360 sReg++;
361 break;
362 case 'J':
363 cUnit->regLocation[sReg].wide = true;
364 cUnit->regLocation[sReg+1].highWord = true;
365 DCHECK_EQ(SRegToVReg(cUnit, sReg)+1, SRegToVReg(cUnit, sReg+1));
366 cUnit->regLocation[sReg].core = true;
367 cUnit->regLocation[sReg].defined = true;
368 sReg++;
369 break;
370 case 'F':
371 cUnit->regLocation[sReg].fp = true;
buzbee67bc2362011-10-11 18:08:40 -0700372 cUnit->regLocation[sReg].defined = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 break;
374 default:
buzbee67bc2362011-10-11 18:08:40 -0700375 cUnit->regLocation[sReg].core = true;
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 cUnit->regLocation[sReg].defined = true;
377 break;
buzbeee9a72f62011-09-04 17:59:07 -0700378 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700379 sReg++;
380 }
381 }
382
383 /* Remap names */
384 oatDataFlowAnalysisDispatcher(cUnit, remapNames,
385 kPreOrderDFSTraversal,
386 false /* isIterative */);
387
388 /* Do type & size inference pass */
389 oatDataFlowAnalysisDispatcher(cUnit, inferTypeAndSize,
390 kPreOrderDFSTraversal,
391 true /* isIterative */);
392
393 /*
394 * Set the sRegLow field to refer to the pre-SSA name of the
395 * base Dalvik virtual register. Once we add a better register
396 * allocator, remove this remapping.
397 */
398 for (i=0; i < cUnit->numSSARegs; i++) {
399 if (cUnit->regLocation[i].location != kLocCompilerTemp) {
400 cUnit->regLocation[i].sRegLow = SRegToVReg(cUnit, loc[i].sRegLow);
buzbeee9a72f62011-09-04 17:59:07 -0700401 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 }
buzbeee9a72f62011-09-04 17:59:07 -0700403
Bill Buzbeea114add2012-05-03 15:00:40 -0700404 cUnit->coreSpillMask = 0;
405 cUnit->fpSpillMask = 0;
406 cUnit->numCoreSpills = 0;
buzbeec0ecd652011-09-25 18:11:54 -0700407
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 oatDoPromotion(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700409
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 if (cUnit->printMe && !(cUnit->disableOpt & (1 << kPromoteRegs))) {
411 LOG(INFO) << "After Promotion";
412 oatDumpRegLocTable(cUnit->regLocation, cUnit->numSSARegs);
413 }
buzbee67bf8852011-08-17 17:51:35 -0700414
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 /* Figure out the frame size */
416 static const uint32_t kAlignMask = kStackAlignment - 1;
417 uint32_t size = (cUnit->numCoreSpills + cUnit->numFPSpills +
418 1 /* filler word */ + cUnit->numRegs + cUnit->numOuts +
419 cUnit->numCompilerTemps + 1 /* curMethod* */)
420 * sizeof(uint32_t);
421 /* Align and set */
422 cUnit->frameSize = (size + kAlignMask) & ~(kAlignMask);
buzbee67bf8852011-08-17 17:51:35 -0700423}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800424
425} // namespace art