blob: 5176edced8cac9b82ffe8d5270a937331deb1216 [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) {
buzbee03fa2632011-09-20 17:10:57 -070025 bool change = false;
buzbee67bc2362011-10-11 18:08:40 -070026 if (cUnit->regLocation[index].highWord) {
27 return change;
28 }
buzbee03fa2632011-09-20 17:10:57 -070029 if (isFP && !cUnit->regLocation[index].fp) {
30 cUnit->regLocation[index].fp = true;
buzbee67bc2362011-10-11 18:08:40 -070031 cUnit->regLocation[index].defined = true;
32 change = true;
33 }
34 return change;
35}
36
buzbee31a4a6f2012-02-28 15:36:15 -080037bool setCore(CompilationUnit* cUnit, int index, bool isCore) {
buzbee67bc2362011-10-11 18:08:40 -070038 bool change = false;
39 if (cUnit->regLocation[index].highWord) {
40 return change;
41 }
42 if (isCore && !cUnit->regLocation[index].defined) {
43 cUnit->regLocation[index].core = true;
44 cUnit->regLocation[index].defined = true;
buzbee03fa2632011-09-20 17:10:57 -070045 change = true;
46 }
47 return change;
48}
49
buzbee31a4a6f2012-02-28 15:36:15 -080050bool remapNames(CompilationUnit* cUnit, BasicBlock* bb)
buzbeec0ecd652011-09-25 18:11:54 -070051{
52 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock &&
53 bb->blockType != kExitBlock)
54 return false;
55
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;
68}
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{
73 SSARepresentation* res = NULL;
74 for (; mir; mir = mir->next) {
Elliott Hughesadb8c672012-03-06 16:49:32 -080075 if ((mir->dalvikInsn.opcode == Instruction::MOVE_RESULT) ||
76 (mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_WIDE)) {
buzbee769fde12012-01-05 17:35:23 -080077 res = mir->ssaRep;
78 break;
79 }
80 }
81 return res;
82}
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{
91 MIR *mir;
buzbee03fa2632011-09-20 17:10:57 -070092 bool changed = false; // Did anything change?
93
94 if (bb->dataFlowInfo == NULL) return false;
buzbee67bf8852011-08-17 17:51:35 -070095 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock)
buzbee03fa2632011-09-20 17:10:57 -070096 return false;
buzbee67bf8852011-08-17 17:51:35 -070097
98 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
99 SSARepresentation *ssaRep = mir->ssaRep;
100 if (ssaRep) {
buzbee03fa2632011-09-20 17:10:57 -0700101 int attrs = oatDataFlowAttributes[mir->dalvikInsn.opcode];
buzbee67bc2362011-10-11 18:08:40 -0700102
103 // Handle defs
104 if (attrs & (DF_DA | DF_DA_WIDE)) {
105 if (attrs & DF_CORE_A) {
106 changed |= setCore(cUnit, ssaRep->defs[0], true);
107 }
108 if (attrs & DF_DA_WIDE) {
109 cUnit->regLocation[ssaRep->defs[0]].wide = true;
110 cUnit->regLocation[ssaRep->defs[1]].highWord = true;
111 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->defs[0])+1,
112 oatS2VReg(cUnit, ssaRep->defs[1]));
113 }
114 }
115
116 // Handles uses
buzbee03fa2632011-09-20 17:10:57 -0700117 int next = 0;
buzbee67bc2362011-10-11 18:08:40 -0700118 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(oatS2VReg(cUnit, ssaRep->uses[next])+1,
126 oatS2VReg(cUnit, ssaRep->uses[next + 1]));
127 next += 2;
128 } else {
129 next++;
130 }
buzbee67bf8852011-08-17 17:51:35 -0700131 }
buzbee67bc2362011-10-11 18:08:40 -0700132 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(oatS2VReg(cUnit, ssaRep->uses[next])+1,
140 oatS2VReg(cUnit, ssaRep->uses[next + 1]));
141 next += 2;
142 } else {
143 next++;
144 }
buzbee03fa2632011-09-20 17:10:57 -0700145 }
buzbee67bc2362011-10-11 18:08:40 -0700146 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(oatS2VReg(cUnit, ssaRep->uses[next])+1,
154 oatS2VReg(cUnit, ssaRep->uses[next + 1]));
155 }
buzbee03fa2632011-09-20 17:10:57 -0700156 }
buzbeeed3e9302011-09-23 17:34:19 -0700157
buzbee769fde12012-01-05 17:35:23 -0800158 // Special-case handling for format 35c/3rc invokes
Elliott Hughesadb8c672012-03-06 16:49:32 -0800159 Instruction::Code opcode = mir->dalvikInsn.opcode;
160 int flags = (static_cast<int>(opcode) >= kNumPackedOpcodes) ? 0 : Instruction::Flags(mir->dalvikInsn.opcode);
161 if ((flags & Instruction::kInvoke) && (attrs & (DF_FORMAT_35C | DF_FORMAT_3RC))) {
buzbeeed3e9302011-09-23 17:34:19 -0700162 DCHECK_EQ(next, 0);
163 int target_idx = mir->dalvikInsn.vB;
164 const char* shorty =
165 oatGetShortyFromTargetIdx(cUnit, target_idx);
buzbee769fde12012-01-05 17:35:23 -0800166 // Handle result type if floating point
167 if ((shorty[0] == 'F') || (shorty[0] == 'D')) {
168 // Find move-result that consumes this result
169 SSARepresentation* tgtRep = findMoveResult(mir->next);
170 // Might be in next basic block
171 if (!tgtRep) {
172 tgtRep = findMoveResult(bb->fallThrough->firstMIRInsn);
173 }
174 // Result might not be used at all, so no move-result
175 if (tgtRep) {
176 tgtRep->fpDef[0] = true;
177 changed |= setFp(cUnit, tgtRep->defs[0], true);
178 if (shorty[0] == 'D') {
179 tgtRep->fpDef[1] = true;
180 changed |= setFp(cUnit, tgtRep->defs[1], true);
181 }
182 }
183 }
buzbeeed3e9302011-09-23 17:34:19 -0700184 int numUses = mir->dalvikInsn.vA;
185 // If this is a non-static invoke, skip implicit "this"
Elliott Hughesadb8c672012-03-06 16:49:32 -0800186 if (((mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC) &&
187 (mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC_RANGE))) {
buzbee67bc2362011-10-11 18:08:40 -0700188 cUnit->regLocation[ssaRep->uses[next]].defined = true;
189 cUnit->regLocation[ssaRep->uses[next]].core = true;
buzbeeed3e9302011-09-23 17:34:19 -0700190 next++;
191 }
192 uint32_t cpos = 1;
193 if (strlen(shorty) > 1) {
194 for (int i = next; i < numUses;) {
195 DCHECK_LT(cpos, strlen(shorty));
196 switch(shorty[cpos++]) {
197 case 'D':
198 ssaRep->fpUse[i] = true;
199 ssaRep->fpUse[i+1] = true;
200 cUnit->regLocation[ssaRep->uses[i]].wide = true;
buzbee67bc2362011-10-11 18:08:40 -0700201 cUnit->regLocation[ssaRep->uses[i+1]].highWord
202 = true;
203 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->uses[i])+1,
204 oatS2VReg(cUnit, ssaRep->uses[i+1]));
buzbeeed3e9302011-09-23 17:34:19 -0700205 i++;
206 break;
207 case 'J':
208 cUnit->regLocation[ssaRep->uses[i]].wide = true;
buzbee67bc2362011-10-11 18:08:40 -0700209 cUnit->regLocation[ssaRep->uses[i+1]].highWord
210 = true;
211 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->uses[i])+1,
212 oatS2VReg(cUnit, ssaRep->uses[i+1]));
213 changed |= setCore(cUnit, ssaRep->uses[i],true);
buzbeeed3e9302011-09-23 17:34:19 -0700214 i++;
215 break;
216 case 'F':
217 ssaRep->fpUse[i] = true;
218 break;
219 default:
buzbee67bc2362011-10-11 18:08:40 -0700220 changed |= setCore(cUnit,ssaRep->uses[i], true);
buzbeeed3e9302011-09-23 17:34:19 -0700221 break;
222 }
223 i++;
224 }
225 }
226 }
227
buzbee03fa2632011-09-20 17:10:57 -0700228 for (int i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) {
229 if (ssaRep->fpUse[i])
230 changed |= setFp(cUnit, ssaRep->uses[i], true);
231 }
232 for (int i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) {
buzbee67bf8852011-08-17 17:51:35 -0700233 if (ssaRep->fpDef[i])
buzbee03fa2632011-09-20 17:10:57 -0700234 changed |= setFp(cUnit, ssaRep->defs[i], true);
235 }
236 // Special-case handling for moves & Phi
237 if (attrs & (DF_IS_MOVE | DF_NULL_TRANSFER_N)) {
buzbee67bc2362011-10-11 18:08:40 -0700238 // If any of our inputs or outputs is defined, set all
239 bool definedFP = false;
240 bool definedCore = false;
241 definedFP |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
242 cUnit->regLocation[ssaRep->defs[0]].fp);
243 definedCore |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
244 cUnit->regLocation[ssaRep->defs[0]].core);
buzbee03fa2632011-09-20 17:10:57 -0700245 for (int i = 0; i < ssaRep->numUses; i++) {
buzbee67bc2362011-10-11 18:08:40 -0700246 definedFP |= (cUnit->regLocation[ssaRep->uses[i]].defined &&
247 cUnit->regLocation[ssaRep->uses[i]].fp);
248 definedCore |= (cUnit->regLocation[ssaRep->uses[i]].defined
249 && cUnit->regLocation[ssaRep->uses[i]].core);
buzbee03fa2632011-09-20 17:10:57 -0700250 }
buzbee23d5bc92011-10-31 12:41:02 -0700251 /*
252 * TODO: cleaner fix
253 * We don't normally expect to see a Dalvik register
254 * definition used both as a floating point and core
255 * value. However, the instruction rewriting that occurs
256 * during verification can eliminate some type information,
257 * leaving us confused. The real fix here is either to
258 * add explicit type information to Dalvik byte codes,
Elliott Hughesadb8c672012-03-06 16:49:32 -0800259 * or to recognize THROW_VERIFICATION_ERROR as
buzbee23d5bc92011-10-31 12:41:02 -0700260 * an unconditional branch and support dead code elimination.
261 * As a workaround we can detect this situation and
262 * disable register promotion (which is the only thing that
263 * relies on distinctions between core and fp usages.
264 */
265 if ((definedFP && definedCore) &&
266 ((cUnit->disableOpt & (1 << kPromoteRegs)) == 0)) {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800267 LOG(WARNING) << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
buzbee23d5bc92011-10-31 12:41:02 -0700268 << " op at block " << bb->id
269 << " has both fp and core uses for same def.";
270 cUnit->disableOpt |= (1 << kPromoteRegs);
271 }
buzbee67bc2362011-10-11 18:08:40 -0700272 changed |= setFp(cUnit, ssaRep->defs[0], definedFP);
273 changed |= setCore(cUnit, ssaRep->defs[0], definedCore);
buzbee03fa2632011-09-20 17:10:57 -0700274 for (int i = 0; i < ssaRep->numUses; i++) {
buzbee67bc2362011-10-11 18:08:40 -0700275 changed |= setFp(cUnit, ssaRep->uses[i], definedFP);
276 changed |= setCore(cUnit, ssaRep->uses[i], definedCore);
buzbee03fa2632011-09-20 17:10:57 -0700277 }
buzbee67bf8852011-08-17 17:51:35 -0700278 }
279 }
280 }
buzbee03fa2632011-09-20 17:10:57 -0700281 return changed;
buzbee67bf8852011-08-17 17:51:35 -0700282}
283
284static const char* storageName[] = {" Frame ", "PhysReg", " Spill "};
285
buzbeedfd3d702011-08-28 12:56:51 -0700286void oatDumpRegLocTable(RegLocation* table, int count)
buzbee67bf8852011-08-17 17:51:35 -0700287{
288 for (int i = 0; i < count; i++) {
289 char buf[100];
buzbee67bc2362011-10-11 18:08:40 -0700290 snprintf(buf, 100, "Loc[%02d] : %s, %c %c %c %c %c %c%d %c%d S%d",
buzbee67bf8852011-08-17 17:51:35 -0700291 i, storageName[table[i].location], table[i].wide ? 'W' : 'N',
buzbee67bc2362011-10-11 18:08:40 -0700292 table[i].defined ? 'D' : 'U', table[i].fp ? 'F' : 'C',
293 table[i].highWord ? 'H' : 'L', table[i].home ? 'h' : 't',
buzbeee3acd072012-02-25 17:03:10 -0800294 oatIsFpReg(table[i].lowReg) ? 's' : 'r',
295 table[i].lowReg & oatFpRegMask(),
296 oatIsFpReg(table[i].highReg) ? 's' : 'r',
297 table[i].highReg & oatFpRegMask(), table[i].sRegLow);
buzbee67bf8852011-08-17 17:51:35 -0700298 LOG(INFO) << buf;
299 }
300}
301
buzbee67bc2362011-10-11 18:08:40 -0700302static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0,
303 INVALID_REG, INVALID_REG, INVALID_SREG};
buzbee67bf8852011-08-17 17:51:35 -0700304
305/*
306 * Simple register allocation. Some Dalvik virtual registers may
307 * be promoted to physical registers. Most of the work for temp
308 * allocation is done on the fly. We also do some initilization and
309 * type inference here.
310 */
311void oatSimpleRegAlloc(CompilationUnit* cUnit)
312{
313 int i;
314 RegLocation* loc;
315
316 /* Allocate the location map */
buzbeeba938cb2012-02-03 14:47:55 -0800317 loc = (RegLocation*)oatNew(cUnit, cUnit->numSSARegs * sizeof(*loc), true,
buzbee5abfa3e2012-01-31 17:01:43 -0800318 kAllocRegAlloc);
buzbee67bf8852011-08-17 17:51:35 -0700319 for (i=0; i< cUnit->numSSARegs; i++) {
320 loc[i] = freshLoc;
321 loc[i].sRegLow = i;
322 }
323 cUnit->regLocation = loc;
324
buzbee67bc2362011-10-11 18:08:40 -0700325 /* Allocation the promotion map */
Ian Rogersa3760aa2011-11-14 14:32:37 -0800326 int numRegs = cUnit->numDalvikRegisters;
327 cUnit->promotionMap =
buzbeeba938cb2012-02-03 14:47:55 -0800328 (PromotionMap*)oatNew(cUnit, numRegs * sizeof(cUnit->promotionMap[0]),
329 true, kAllocRegAlloc);
buzbee67bc2362011-10-11 18:08:40 -0700330
buzbeee9a72f62011-09-04 17:59:07 -0700331 /* Add types of incoming arguments based on signature */
Ian Rogersa3760aa2011-11-14 14:32:37 -0800332 int numIns = cUnit->numIns;
buzbeee9a72f62011-09-04 17:59:07 -0700333 if (numIns > 0) {
334 int sReg = numRegs - numIns;
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800335 if ((cUnit->access_flags & kAccStatic) == 0) {
Ian Rogersa3760aa2011-11-14 14:32:37 -0800336 // For non-static, skip past "this"
buzbee67bc2362011-10-11 18:08:40 -0700337 cUnit->regLocation[sReg].defined = true;
338 cUnit->regLocation[sReg].core = true;
buzbeee9a72f62011-09-04 17:59:07 -0700339 sReg++;
340 }
Ian Rogersa3760aa2011-11-14 14:32:37 -0800341 const char* shorty = cUnit->shorty;
342 int shorty_len = strlen(shorty);
343 for (int i = 1; i < shorty_len; i++) {
344 switch(shorty[i]) {
buzbee67bc2362011-10-11 18:08:40 -0700345 case 'D':
346 cUnit->regLocation[sReg].wide = true;
347 cUnit->regLocation[sReg+1].highWord = true;
buzbee86a4bce2012-03-06 18:15:00 -0800348 cUnit->regLocation[sReg+1].fp = true;
buzbee67bc2362011-10-11 18:08:40 -0700349 DCHECK_EQ(oatS2VReg(cUnit, sReg)+1,
350 oatS2VReg(cUnit, sReg+1));
351 cUnit->regLocation[sReg].fp = true;
352 cUnit->regLocation[sReg].defined = true;
353 sReg++;
354 break;
355 case 'J':
356 cUnit->regLocation[sReg].wide = true;
357 cUnit->regLocation[sReg+1].highWord = true;
358 DCHECK_EQ(oatS2VReg(cUnit, sReg)+1,
359 oatS2VReg(cUnit, sReg+1));
360 cUnit->regLocation[sReg].core = true;
361 cUnit->regLocation[sReg].defined = true;
362 sReg++;
363 break;
364 case 'F':
365 cUnit->regLocation[sReg].fp = true;
366 cUnit->regLocation[sReg].defined = true;
367 break;
368 default:
369 cUnit->regLocation[sReg].core = true;
370 cUnit->regLocation[sReg].defined = true;
371 break;
buzbeee9a72f62011-09-04 17:59:07 -0700372 }
373 sReg++;
374 }
375 }
376
buzbeec0ecd652011-09-25 18:11:54 -0700377 /* Remap names */
378 oatDataFlowAnalysisDispatcher(cUnit, remapNames,
379 kPreOrderDFSTraversal,
380 false /* isIterative */);
381
buzbee03fa2632011-09-20 17:10:57 -0700382 /* Do type & size inference pass */
383 oatDataFlowAnalysisDispatcher(cUnit, inferTypeAndSize,
384 kPreOrderDFSTraversal,
385 true /* isIterative */);
buzbee67bf8852011-08-17 17:51:35 -0700386
387 /*
388 * Set the sRegLow field to refer to the pre-SSA name of the
389 * base Dalvik virtual register. Once we add a better register
390 * allocator, remove this remapping.
391 */
392 for (i=0; i < cUnit->numSSARegs; i++) {
393 cUnit->regLocation[i].sRegLow =
394 DECODE_REG(oatConvertSSARegToDalvik(cUnit, loc[i].sRegLow));
395 }
396
397 cUnit->coreSpillMask = 0;
398 cUnit->fpSpillMask = 0;
buzbeebbaf8942011-10-02 13:08:29 -0700399 cUnit->numCoreSpills = 0;
buzbee67bf8852011-08-17 17:51:35 -0700400
401 oatDoPromotion(cUnit);
402
403 if (cUnit->printMe && !(cUnit->disableOpt & (1 << kPromoteRegs))) {
404 LOG(INFO) << "After Promotion";
buzbeedfd3d702011-08-28 12:56:51 -0700405 oatDumpRegLocTable(cUnit->regLocation, cUnit->numSSARegs);
buzbee67bf8852011-08-17 17:51:35 -0700406 }
407
408 /* Figure out the frame size */
buzbeeefccc562012-03-11 11:19:28 -0700409 static const uint32_t kAlignMask = kStackAlignment - 1;
410 uint32_t size = (cUnit->numCoreSpills + cUnit->numFPSpills +
411 cUnit->numRegs + cUnit->numOuts + cUnit->numCompilerTemps +
412 1 /* curMethod* */) * sizeof(uint32_t);
413 /* Align and set */
414 cUnit->frameSize = (size + kAlignMask) & ~(kAlignMask);
buzbee67bf8852011-08-17 17:51:35 -0700415}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800416
417} // namespace art