blob: 184db9fcb831742bccb5ff56e9fd6ec76d8e0b8d [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
buzbeeed3e9302011-09-23 17:34:19 -070022STATIC bool setFp(CompilationUnit* cUnit, int index, bool isFP) {
buzbee03fa2632011-09-20 17:10:57 -070023 bool change = false;
buzbee67bc2362011-10-11 18:08:40 -070024 if (cUnit->regLocation[index].highWord) {
25 return change;
26 }
buzbee03fa2632011-09-20 17:10:57 -070027 if (isFP && !cUnit->regLocation[index].fp) {
28 cUnit->regLocation[index].fp = true;
buzbee67bc2362011-10-11 18:08:40 -070029 cUnit->regLocation[index].defined = true;
30 change = true;
31 }
32 return change;
33}
34
35STATIC bool setCore(CompilationUnit* cUnit, int index, bool isCore) {
36 bool change = false;
37 if (cUnit->regLocation[index].highWord) {
38 return change;
39 }
40 if (isCore && !cUnit->regLocation[index].defined) {
41 cUnit->regLocation[index].core = true;
42 cUnit->regLocation[index].defined = true;
buzbee03fa2632011-09-20 17:10:57 -070043 change = true;
44 }
45 return change;
46}
47
buzbeec0ecd652011-09-25 18:11:54 -070048STATIC bool remapNames(CompilationUnit* cUnit, BasicBlock* bb)
49{
50 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock &&
51 bb->blockType != kExitBlock)
52 return false;
53
54 for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
55 SSARepresentation *ssaRep = mir->ssaRep;
56 if (ssaRep) {
57 for (int i = 0; i < ssaRep->numUses; i++) {
58 ssaRep->uses[i] = cUnit->phiAliasMap[ssaRep->uses[i]];
59 }
60 for (int i = 0; i < ssaRep->numDefs; i++) {
61 ssaRep->defs[i] = cUnit->phiAliasMap[ssaRep->defs[i]];
62 }
63 }
64 }
65 return false;
66}
67
buzbee67bf8852011-08-17 17:51:35 -070068/*
buzbee03fa2632011-09-20 17:10:57 -070069 * Infer types and sizes. We don't need to track change on sizes,
70 * as it doesn't propagate. We're guaranteed at least one pass through
71 * the cfg.
buzbee67bf8852011-08-17 17:51:35 -070072 */
buzbeeed3e9302011-09-23 17:34:19 -070073STATIC bool inferTypeAndSize(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -070074{
75 MIR *mir;
buzbee03fa2632011-09-20 17:10:57 -070076 bool changed = false; // Did anything change?
77
78 if (bb->dataFlowInfo == NULL) return false;
buzbee67bf8852011-08-17 17:51:35 -070079 if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock)
buzbee03fa2632011-09-20 17:10:57 -070080 return false;
buzbee67bf8852011-08-17 17:51:35 -070081
82 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
83 SSARepresentation *ssaRep = mir->ssaRep;
84 if (ssaRep) {
buzbee03fa2632011-09-20 17:10:57 -070085 int attrs = oatDataFlowAttributes[mir->dalvikInsn.opcode];
buzbee67bc2362011-10-11 18:08:40 -070086
87 // Handle defs
88 if (attrs & (DF_DA | DF_DA_WIDE)) {
89 if (attrs & DF_CORE_A) {
90 changed |= setCore(cUnit, ssaRep->defs[0], true);
91 }
92 if (attrs & DF_DA_WIDE) {
93 cUnit->regLocation[ssaRep->defs[0]].wide = true;
94 cUnit->regLocation[ssaRep->defs[1]].highWord = true;
95 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->defs[0])+1,
96 oatS2VReg(cUnit, ssaRep->defs[1]));
97 }
98 }
99
100 // Handles uses
buzbee03fa2632011-09-20 17:10:57 -0700101 int next = 0;
buzbee67bc2362011-10-11 18:08:40 -0700102 if (attrs & (DF_UA | DF_UA_WIDE)) {
103 if (attrs & DF_CORE_A) {
104 changed |= setCore(cUnit, ssaRep->uses[next], true);
105 }
106 if (attrs & DF_UA_WIDE) {
107 cUnit->regLocation[ssaRep->uses[next]].wide = true;
108 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
109 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->uses[next])+1,
110 oatS2VReg(cUnit, ssaRep->uses[next + 1]));
111 next += 2;
112 } else {
113 next++;
114 }
buzbee67bf8852011-08-17 17:51:35 -0700115 }
buzbee67bc2362011-10-11 18:08:40 -0700116 if (attrs & (DF_UB | DF_UB_WIDE)) {
117 if (attrs & DF_CORE_B) {
118 changed |= setCore(cUnit, ssaRep->uses[next], true);
119 }
120 if (attrs & DF_UB_WIDE) {
121 cUnit->regLocation[ssaRep->uses[next]].wide = true;
122 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
123 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->uses[next])+1,
124 oatS2VReg(cUnit, ssaRep->uses[next + 1]));
125 next += 2;
126 } else {
127 next++;
128 }
buzbee03fa2632011-09-20 17:10:57 -0700129 }
buzbee67bc2362011-10-11 18:08:40 -0700130 if (attrs & (DF_UC | DF_UC_WIDE)) {
131 if (attrs & DF_CORE_C) {
132 changed |= setCore(cUnit, ssaRep->uses[next], true);
133 }
134 if (attrs & DF_UC_WIDE) {
135 cUnit->regLocation[ssaRep->uses[next]].wide = true;
136 cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
137 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->uses[next])+1,
138 oatS2VReg(cUnit, ssaRep->uses[next + 1]));
139 }
buzbee03fa2632011-09-20 17:10:57 -0700140 }
buzbeeed3e9302011-09-23 17:34:19 -0700141
142 // Special-case handling for format 35c/3rc invokes
143 Opcode opcode = mir->dalvikInsn.opcode;
144 int flags = (opcode >= kNumPackedOpcodes) ? 0 :
145 dexGetFlagsFromOpcode(opcode);
146 if ((flags & kInstrInvoke) &&
147 (attrs & (DF_FORMAT_35C | DF_FORMAT_3RC))) {
148 DCHECK_EQ(next, 0);
149 int target_idx = mir->dalvikInsn.vB;
150 const char* shorty =
151 oatGetShortyFromTargetIdx(cUnit, target_idx);
152 int numUses = mir->dalvikInsn.vA;
153 // If this is a non-static invoke, skip implicit "this"
154 if (((mir->dalvikInsn.opcode != OP_INVOKE_STATIC) &&
155 (mir->dalvikInsn.opcode != OP_INVOKE_STATIC_RANGE))) {
buzbee67bc2362011-10-11 18:08:40 -0700156 cUnit->regLocation[ssaRep->uses[next]].defined = true;
157 cUnit->regLocation[ssaRep->uses[next]].core = true;
buzbeeed3e9302011-09-23 17:34:19 -0700158 next++;
159 }
160 uint32_t cpos = 1;
161 if (strlen(shorty) > 1) {
162 for (int i = next; i < numUses;) {
163 DCHECK_LT(cpos, strlen(shorty));
164 switch(shorty[cpos++]) {
165 case 'D':
166 ssaRep->fpUse[i] = true;
167 ssaRep->fpUse[i+1] = true;
168 cUnit->regLocation[ssaRep->uses[i]].wide = true;
buzbee67bc2362011-10-11 18:08:40 -0700169 cUnit->regLocation[ssaRep->uses[i+1]].highWord
170 = true;
171 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->uses[i])+1,
172 oatS2VReg(cUnit, ssaRep->uses[i+1]));
buzbeeed3e9302011-09-23 17:34:19 -0700173 i++;
174 break;
175 case 'J':
176 cUnit->regLocation[ssaRep->uses[i]].wide = true;
buzbee67bc2362011-10-11 18:08:40 -0700177 cUnit->regLocation[ssaRep->uses[i+1]].highWord
178 = true;
179 DCHECK_EQ(oatS2VReg(cUnit, ssaRep->uses[i])+1,
180 oatS2VReg(cUnit, ssaRep->uses[i+1]));
181 changed |= setCore(cUnit, ssaRep->uses[i],true);
buzbeeed3e9302011-09-23 17:34:19 -0700182 i++;
183 break;
184 case 'F':
185 ssaRep->fpUse[i] = true;
186 break;
187 default:
buzbee67bc2362011-10-11 18:08:40 -0700188 changed |= setCore(cUnit,ssaRep->uses[i], true);
buzbeeed3e9302011-09-23 17:34:19 -0700189 break;
190 }
191 i++;
192 }
193 }
194 }
195
buzbee03fa2632011-09-20 17:10:57 -0700196 for (int i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) {
197 if (ssaRep->fpUse[i])
198 changed |= setFp(cUnit, ssaRep->uses[i], true);
199 }
200 for (int i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) {
buzbee67bf8852011-08-17 17:51:35 -0700201 if (ssaRep->fpDef[i])
buzbee03fa2632011-09-20 17:10:57 -0700202 changed |= setFp(cUnit, ssaRep->defs[i], true);
203 }
204 // Special-case handling for moves & Phi
205 if (attrs & (DF_IS_MOVE | DF_NULL_TRANSFER_N)) {
buzbee67bc2362011-10-11 18:08:40 -0700206 // If any of our inputs or outputs is defined, set all
207 bool definedFP = false;
208 bool definedCore = false;
209 definedFP |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
210 cUnit->regLocation[ssaRep->defs[0]].fp);
211 definedCore |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
212 cUnit->regLocation[ssaRep->defs[0]].core);
buzbee03fa2632011-09-20 17:10:57 -0700213 for (int i = 0; i < ssaRep->numUses; i++) {
buzbee67bc2362011-10-11 18:08:40 -0700214 definedFP |= (cUnit->regLocation[ssaRep->uses[i]].defined &&
215 cUnit->regLocation[ssaRep->uses[i]].fp);
216 definedCore |= (cUnit->regLocation[ssaRep->uses[i]].defined
217 && cUnit->regLocation[ssaRep->uses[i]].core);
buzbee03fa2632011-09-20 17:10:57 -0700218 }
buzbee23d5bc92011-10-31 12:41:02 -0700219 /*
220 * TODO: cleaner fix
221 * We don't normally expect to see a Dalvik register
222 * definition used both as a floating point and core
223 * value. However, the instruction rewriting that occurs
224 * during verification can eliminate some type information,
225 * leaving us confused. The real fix here is either to
226 * add explicit type information to Dalvik byte codes,
227 * or to recognize OP_THROW_VERIFICATION_ERROR as
228 * an unconditional branch and support dead code elimination.
229 * As a workaround we can detect this situation and
230 * disable register promotion (which is the only thing that
231 * relies on distinctions between core and fp usages.
232 */
233 if ((definedFP && definedCore) &&
234 ((cUnit->disableOpt & (1 << kPromoteRegs)) == 0)) {
235 LOG(WARNING) << art::PrettyMethod(cUnit->method)
236 << " op at block " << bb->id
237 << " has both fp and core uses for same def.";
238 cUnit->disableOpt |= (1 << kPromoteRegs);
239 }
buzbee67bc2362011-10-11 18:08:40 -0700240 changed |= setFp(cUnit, ssaRep->defs[0], definedFP);
241 changed |= setCore(cUnit, ssaRep->defs[0], definedCore);
buzbee03fa2632011-09-20 17:10:57 -0700242 for (int i = 0; i < ssaRep->numUses; i++) {
buzbee67bc2362011-10-11 18:08:40 -0700243 changed |= setFp(cUnit, ssaRep->uses[i], definedFP);
244 changed |= setCore(cUnit, ssaRep->uses[i], definedCore);
buzbee03fa2632011-09-20 17:10:57 -0700245 }
buzbee67bf8852011-08-17 17:51:35 -0700246 }
247 }
248 }
buzbee03fa2632011-09-20 17:10:57 -0700249 return changed;
buzbee67bf8852011-08-17 17:51:35 -0700250}
251
252static const char* storageName[] = {" Frame ", "PhysReg", " Spill "};
253
buzbeedfd3d702011-08-28 12:56:51 -0700254void oatDumpRegLocTable(RegLocation* table, int count)
buzbee67bf8852011-08-17 17:51:35 -0700255{
256 for (int i = 0; i < count; i++) {
257 char buf[100];
buzbee67bc2362011-10-11 18:08:40 -0700258 snprintf(buf, 100, "Loc[%02d] : %s, %c %c %c %c %c %c%d %c%d S%d",
buzbee67bf8852011-08-17 17:51:35 -0700259 i, storageName[table[i].location], table[i].wide ? 'W' : 'N',
buzbee67bc2362011-10-11 18:08:40 -0700260 table[i].defined ? 'D' : 'U', table[i].fp ? 'F' : 'C',
261 table[i].highWord ? 'H' : 'L', table[i].home ? 'h' : 't',
262 FPREG(table[i].lowReg) ? 's' : 'r', table[i].lowReg & FP_REG_MASK,
263 FPREG(table[i].highReg) ? 's' : 'r', table[i].highReg & FP_REG_MASK,
264 table[i].sRegLow);
buzbee67bf8852011-08-17 17:51:35 -0700265 LOG(INFO) << buf;
266 }
267}
268
buzbee67bc2362011-10-11 18:08:40 -0700269static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0,
270 INVALID_REG, INVALID_REG, INVALID_SREG};
buzbee67bf8852011-08-17 17:51:35 -0700271
272/*
273 * Simple register allocation. Some Dalvik virtual registers may
274 * be promoted to physical registers. Most of the work for temp
275 * allocation is done on the fly. We also do some initilization and
276 * type inference here.
277 */
278void oatSimpleRegAlloc(CompilationUnit* cUnit)
279{
280 int i;
281 RegLocation* loc;
282
283 /* Allocate the location map */
284 loc = (RegLocation*)oatNew(cUnit->numSSARegs * sizeof(*loc), true);
285 for (i=0; i< cUnit->numSSARegs; i++) {
286 loc[i] = freshLoc;
287 loc[i].sRegLow = i;
288 }
289 cUnit->regLocation = loc;
290
buzbee67bc2362011-10-11 18:08:40 -0700291 /* Allocation the promotion map */
292 cUnit->promotionMap = (PromotionMap*)oatNew( cUnit->method->NumRegisters()
293 * sizeof(cUnit->promotionMap[0]), true);
294
buzbeee9a72f62011-09-04 17:59:07 -0700295 /* Add types of incoming arguments based on signature */
296 int numRegs = cUnit->method->NumRegisters();
297 int numIns = cUnit->method->NumIns();
298 if (numIns > 0) {
299 int sReg = numRegs - numIns;
300 if (!cUnit->method->IsStatic()) {
301 // Skip past "this"
buzbee67bc2362011-10-11 18:08:40 -0700302 cUnit->regLocation[sReg].defined = true;
303 cUnit->regLocation[sReg].core = true;
buzbeee9a72f62011-09-04 17:59:07 -0700304 sReg++;
305 }
Brian Carlstromc74255f2011-09-11 22:47:39 -0700306 const String* shorty = cUnit->method->GetShorty();
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700307 for (int i = 1; i < shorty->GetLength(); i++) {
buzbee67bc2362011-10-11 18:08:40 -0700308 switch(shorty->CharAt(i)) {
309 case 'D':
310 cUnit->regLocation[sReg].wide = true;
311 cUnit->regLocation[sReg+1].highWord = true;
312 DCHECK_EQ(oatS2VReg(cUnit, sReg)+1,
313 oatS2VReg(cUnit, sReg+1));
314 cUnit->regLocation[sReg].fp = true;
315 cUnit->regLocation[sReg].defined = true;
316 sReg++;
317 break;
318 case 'J':
319 cUnit->regLocation[sReg].wide = true;
320 cUnit->regLocation[sReg+1].highWord = true;
321 DCHECK_EQ(oatS2VReg(cUnit, sReg)+1,
322 oatS2VReg(cUnit, sReg+1));
323 cUnit->regLocation[sReg].core = true;
324 cUnit->regLocation[sReg].defined = true;
325 sReg++;
326 break;
327 case 'F':
328 cUnit->regLocation[sReg].fp = true;
329 cUnit->regLocation[sReg].defined = true;
330 break;
331 default:
332 cUnit->regLocation[sReg].core = true;
333 cUnit->regLocation[sReg].defined = true;
334 break;
buzbeee9a72f62011-09-04 17:59:07 -0700335 }
336 sReg++;
337 }
338 }
339
buzbeec0ecd652011-09-25 18:11:54 -0700340 /* Remap names */
341 oatDataFlowAnalysisDispatcher(cUnit, remapNames,
342 kPreOrderDFSTraversal,
343 false /* isIterative */);
344
buzbee03fa2632011-09-20 17:10:57 -0700345 /* Do type & size inference pass */
346 oatDataFlowAnalysisDispatcher(cUnit, inferTypeAndSize,
347 kPreOrderDFSTraversal,
348 true /* isIterative */);
buzbee67bf8852011-08-17 17:51:35 -0700349
350 /*
351 * Set the sRegLow field to refer to the pre-SSA name of the
352 * base Dalvik virtual register. Once we add a better register
353 * allocator, remove this remapping.
354 */
355 for (i=0; i < cUnit->numSSARegs; i++) {
356 cUnit->regLocation[i].sRegLow =
357 DECODE_REG(oatConvertSSARegToDalvik(cUnit, loc[i].sRegLow));
358 }
359
360 cUnit->coreSpillMask = 0;
361 cUnit->fpSpillMask = 0;
buzbeebbaf8942011-10-02 13:08:29 -0700362 cUnit->numCoreSpills = 0;
buzbee67bf8852011-08-17 17:51:35 -0700363
364 oatDoPromotion(cUnit);
365
366 if (cUnit->printMe && !(cUnit->disableOpt & (1 << kPromoteRegs))) {
367 LOG(INFO) << "After Promotion";
buzbeedfd3d702011-08-28 12:56:51 -0700368 oatDumpRegLocTable(cUnit->regLocation, cUnit->numSSARegs);
buzbee67bf8852011-08-17 17:51:35 -0700369 }
370
371 /* Figure out the frame size */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700372 cUnit->numIns = cUnit->method->NumIns();
373 cUnit->numRegs = cUnit->method->NumRegisters() - cUnit->numIns;
374 cUnit->numOuts = cUnit->method->NumOuts();
buzbee67bf8852011-08-17 17:51:35 -0700375 cUnit->numPadding = (STACK_ALIGN_WORDS -
buzbeebbaf8942011-10-02 13:08:29 -0700376 (cUnit->numCoreSpills + cUnit->numFPSpills + cUnit->numRegs +
buzbee67bf8852011-08-17 17:51:35 -0700377 cUnit->numOuts + 2)) & (STACK_ALIGN_WORDS-1);
buzbeebbaf8942011-10-02 13:08:29 -0700378 cUnit->frameSize = (cUnit->numCoreSpills + cUnit->numFPSpills +
379 cUnit->numRegs + cUnit->numOuts +
buzbee67bf8852011-08-17 17:51:35 -0700380 cUnit->numPadding + 2) * 4;
381 cUnit->insOffset = cUnit->frameSize + 4;
382 cUnit->regsOffset = (cUnit->numOuts + cUnit->numPadding + 1) * 4;
buzbee67bf8852011-08-17 17:51:35 -0700383}