1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Dalvik.h"
#include "CompilerInternals.h"
#include "Dataflow.h"
#include "codegen/Ralloc.h"
namespace art {
bool setFp(CompilationUnit* cUnit, int index, bool isFP) {
bool change = false;
if (cUnit->regLocation[index].highWord) {
return change;
}
if (isFP && !cUnit->regLocation[index].fp) {
cUnit->regLocation[index].fp = true;
cUnit->regLocation[index].defined = true;
change = true;
}
return change;
}
bool setCore(CompilationUnit* cUnit, int index, bool isCore) {
bool change = false;
if (cUnit->regLocation[index].highWord) {
return change;
}
if (isCore && !cUnit->regLocation[index].defined) {
cUnit->regLocation[index].core = true;
cUnit->regLocation[index].defined = true;
change = true;
}
return change;
}
bool remapNames(CompilationUnit* cUnit, BasicBlock* bb)
{
if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock &&
bb->blockType != kExitBlock)
return false;
for (MIR* mir = bb->firstMIRInsn; mir; mir = mir->next) {
SSARepresentation *ssaRep = mir->ssaRep;
if (ssaRep) {
for (int i = 0; i < ssaRep->numUses; i++) {
ssaRep->uses[i] = cUnit->phiAliasMap[ssaRep->uses[i]];
}
for (int i = 0; i < ssaRep->numDefs; i++) {
ssaRep->defs[i] = cUnit->phiAliasMap[ssaRep->defs[i]];
}
}
}
return false;
}
// Try to find the next move result which might have an FP target
SSARepresentation* findMoveResult(MIR* mir)
{
SSARepresentation* res = NULL;
for (; mir; mir = mir->next) {
if ((mir->dalvikInsn.opcode == Instruction::MOVE_RESULT) ||
(mir->dalvikInsn.opcode == Instruction::MOVE_RESULT_WIDE)) {
res = mir->ssaRep;
break;
}
}
return res;
}
/*
* Infer types and sizes. We don't need to track change on sizes,
* as it doesn't propagate. We're guaranteed at least one pass through
* the cfg.
*/
bool inferTypeAndSize(CompilationUnit* cUnit, BasicBlock* bb)
{
MIR *mir;
bool changed = false; // Did anything change?
if (bb->dataFlowInfo == NULL) return false;
if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock)
return false;
for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
SSARepresentation *ssaRep = mir->ssaRep;
if (ssaRep) {
int attrs = oatDataFlowAttributes[mir->dalvikInsn.opcode];
// Handle defs
if (attrs & (DF_DA | DF_DA_WIDE)) {
if (attrs & DF_CORE_A) {
changed |= setCore(cUnit, ssaRep->defs[0], true);
}
if (attrs & DF_DA_WIDE) {
cUnit->regLocation[ssaRep->defs[0]].wide = true;
cUnit->regLocation[ssaRep->defs[1]].highWord = true;
DCHECK_EQ(SRegToVReg(cUnit, ssaRep->defs[0])+1,
SRegToVReg(cUnit, ssaRep->defs[1]));
}
}
// Handles uses
int next = 0;
if (attrs & (DF_UA | DF_UA_WIDE)) {
if (attrs & DF_CORE_A) {
changed |= setCore(cUnit, ssaRep->uses[next], true);
}
if (attrs & DF_UA_WIDE) {
cUnit->regLocation[ssaRep->uses[next]].wide = true;
cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
SRegToVReg(cUnit, ssaRep->uses[next + 1]));
next += 2;
} else {
next++;
}
}
if (attrs & (DF_UB | DF_UB_WIDE)) {
if (attrs & DF_CORE_B) {
changed |= setCore(cUnit, ssaRep->uses[next], true);
}
if (attrs & DF_UB_WIDE) {
cUnit->regLocation[ssaRep->uses[next]].wide = true;
cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
SRegToVReg(cUnit, ssaRep->uses[next + 1]));
next += 2;
} else {
next++;
}
}
if (attrs & (DF_UC | DF_UC_WIDE)) {
if (attrs & DF_CORE_C) {
changed |= setCore(cUnit, ssaRep->uses[next], true);
}
if (attrs & DF_UC_WIDE) {
cUnit->regLocation[ssaRep->uses[next]].wide = true;
cUnit->regLocation[ssaRep->uses[next + 1]].highWord = true;
DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[next])+1,
SRegToVReg(cUnit, ssaRep->uses[next + 1]));
}
}
// Special-case handling for format 35c/3rc invokes
Instruction::Code opcode = mir->dalvikInsn.opcode;
int flags = (static_cast<int>(opcode) >= kNumPackedOpcodes) ? 0 : Instruction::Flags(mir->dalvikInsn.opcode);
if ((flags & Instruction::kInvoke) && (attrs & (DF_FORMAT_35C | DF_FORMAT_3RC))) {
DCHECK_EQ(next, 0);
int target_idx = mir->dalvikInsn.vB;
const char* shorty =
oatGetShortyFromTargetIdx(cUnit, target_idx);
// Handle result type if floating point
if ((shorty[0] == 'F') || (shorty[0] == 'D')) {
// Find move-result that consumes this result
SSARepresentation* tgtRep = findMoveResult(mir->next);
// Might be in next basic block
if (!tgtRep) {
tgtRep = findMoveResult(bb->fallThrough->firstMIRInsn);
}
// Result might not be used at all, so no move-result
if (tgtRep) {
tgtRep->fpDef[0] = true;
changed |= setFp(cUnit, tgtRep->defs[0], true);
if (shorty[0] == 'D') {
tgtRep->fpDef[1] = true;
changed |= setFp(cUnit, tgtRep->defs[1], true);
}
}
}
int numUses = mir->dalvikInsn.vA;
// If this is a non-static invoke, skip implicit "this"
if (((mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC) &&
(mir->dalvikInsn.opcode != Instruction::INVOKE_STATIC_RANGE))) {
cUnit->regLocation[ssaRep->uses[next]].defined = true;
cUnit->regLocation[ssaRep->uses[next]].core = true;
next++;
}
uint32_t cpos = 1;
if (strlen(shorty) > 1) {
for (int i = next; i < numUses;) {
DCHECK_LT(cpos, strlen(shorty));
switch(shorty[cpos++]) {
case 'D':
ssaRep->fpUse[i] = true;
ssaRep->fpUse[i+1] = true;
cUnit->regLocation[ssaRep->uses[i]].wide = true;
cUnit->regLocation[ssaRep->uses[i+1]].highWord
= true;
DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[i])+1,
SRegToVReg(cUnit, ssaRep->uses[i+1]));
i++;
break;
case 'J':
cUnit->regLocation[ssaRep->uses[i]].wide = true;
cUnit->regLocation[ssaRep->uses[i+1]].highWord
= true;
DCHECK_EQ(SRegToVReg(cUnit, ssaRep->uses[i])+1,
SRegToVReg(cUnit, ssaRep->uses[i+1]));
changed |= setCore(cUnit, ssaRep->uses[i],true);
i++;
break;
case 'F':
ssaRep->fpUse[i] = true;
break;
default:
changed |= setCore(cUnit,ssaRep->uses[i], true);
break;
}
i++;
}
}
}
for (int i=0; ssaRep->fpUse && i< ssaRep->numUses; i++) {
if (ssaRep->fpUse[i])
changed |= setFp(cUnit, ssaRep->uses[i], true);
}
for (int i=0; ssaRep->fpDef && i< ssaRep->numDefs; i++) {
if (ssaRep->fpDef[i])
changed |= setFp(cUnit, ssaRep->defs[i], true);
}
// Special-case handling for moves & Phi
if (attrs & (DF_IS_MOVE | DF_NULL_TRANSFER_N)) {
// If any of our inputs or outputs is defined, set all
bool definedFP = false;
bool definedCore = false;
definedFP |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
cUnit->regLocation[ssaRep->defs[0]].fp);
definedCore |= (cUnit->regLocation[ssaRep->defs[0]].defined &&
cUnit->regLocation[ssaRep->defs[0]].core);
for (int i = 0; i < ssaRep->numUses; i++) {
definedFP |= (cUnit->regLocation[ssaRep->uses[i]].defined &&
cUnit->regLocation[ssaRep->uses[i]].fp);
definedCore |= (cUnit->regLocation[ssaRep->uses[i]].defined
&& cUnit->regLocation[ssaRep->uses[i]].core);
}
/*
* TODO: cleaner fix
* We don't normally expect to see a Dalvik register
* definition used both as a floating point and core
* value. However, the instruction rewriting that occurs
* during verification can eliminate some type information,
* leaving us confused. The real fix here is either to
* add explicit type information to Dalvik byte codes,
* or to recognize THROW_VERIFICATION_ERROR as
* an unconditional branch and support dead code elimination.
* As a workaround we can detect this situation and
* disable register promotion (which is the only thing that
* relies on distinctions between core and fp usages.
*/
if ((definedFP && definedCore) &&
((cUnit->disableOpt & (1 << kPromoteRegs)) == 0)) {
LOG(WARNING) << PrettyMethod(cUnit->method_idx, *cUnit->dex_file)
<< " op at block " << bb->id
<< " has both fp and core uses for same def.";
cUnit->disableOpt |= (1 << kPromoteRegs);
}
changed |= setFp(cUnit, ssaRep->defs[0], definedFP);
changed |= setCore(cUnit, ssaRep->defs[0], definedCore);
for (int i = 0; i < ssaRep->numUses; i++) {
changed |= setFp(cUnit, ssaRep->uses[i], definedFP);
changed |= setCore(cUnit, ssaRep->uses[i], definedCore);
}
}
}
}
return changed;
}
static const char* storageName[] = {" Frame ", "PhysReg", " Spill "};
void oatDumpRegLocTable(RegLocation* table, int count)
{
for (int i = 0; i < count; i++) {
char buf[100];
snprintf(buf, 100, "Loc[%02d] : %s, %c %c %c %c %c %c%d %c%d S%d",
i, storageName[table[i].location], table[i].wide ? 'W' : 'N',
table[i].defined ? 'D' : 'U', table[i].fp ? 'F' : 'C',
table[i].highWord ? 'H' : 'L', table[i].home ? 'h' : 't',
oatIsFpReg(table[i].lowReg) ? 's' : 'r',
table[i].lowReg & oatFpRegMask(),
oatIsFpReg(table[i].highReg) ? 's' : 'r',
table[i].highReg & oatFpRegMask(), table[i].sRegLow);
LOG(INFO) << buf;
}
}
static const RegLocation freshLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0,
INVALID_REG, INVALID_REG, INVALID_SREG};
/*
* Simple register allocation. Some Dalvik virtual registers may
* be promoted to physical registers. Most of the work for temp
* allocation is done on the fly. We also do some initialization and
* type inference here.
*/
void oatSimpleRegAlloc(CompilationUnit* cUnit)
{
int i;
RegLocation* loc;
/* Allocate the location map */
loc = (RegLocation*)oatNew(cUnit, cUnit->numSSARegs * sizeof(*loc), true,
kAllocRegAlloc);
for (i=0; i< cUnit->numSSARegs; i++) {
loc[i] = freshLoc;
loc[i].sRegLow = i;
}
/* Patch up the locations for Method* and the compiler temps */
loc[cUnit->methodSReg].location = kLocCompilerTemp;
for (i = 0; i < cUnit->numCompilerTemps; i++) {
CompilerTemp* ct = (CompilerTemp*)cUnit->compilerTemps.elemList[i];
loc[ct->sReg].location = kLocCompilerTemp;
}
cUnit->regLocation = loc;
/* Allocation the promotion map */
int numRegs = cUnit->numDalvikRegisters;
PromotionMap* tMap =
(PromotionMap*)oatNew(cUnit, (numRegs + cUnit->numCompilerTemps + 1) *
sizeof(cUnit->promotionMap[0]), true,
kAllocRegAlloc);
// Bias the promotion map
cUnit->promotionMap = &tMap[cUnit->numCompilerTemps + 1];
/* Add types of incoming arguments based on signature */
int numIns = cUnit->numIns;
if (numIns > 0) {
int sReg = numRegs - numIns;
if ((cUnit->access_flags & kAccStatic) == 0) {
// For non-static, skip past "this"
cUnit->regLocation[sReg].defined = true;
cUnit->regLocation[sReg].core = true;
sReg++;
}
const char* shorty = cUnit->shorty;
int shorty_len = strlen(shorty);
for (int i = 1; i < shorty_len; i++) {
switch(shorty[i]) {
case 'D':
cUnit->regLocation[sReg].wide = true;
cUnit->regLocation[sReg+1].highWord = true;
cUnit->regLocation[sReg+1].fp = true;
DCHECK_EQ(SRegToVReg(cUnit, sReg)+1,
SRegToVReg(cUnit, sReg+1));
cUnit->regLocation[sReg].fp = true;
cUnit->regLocation[sReg].defined = true;
sReg++;
break;
case 'J':
cUnit->regLocation[sReg].wide = true;
cUnit->regLocation[sReg+1].highWord = true;
DCHECK_EQ(SRegToVReg(cUnit, sReg)+1,
SRegToVReg(cUnit, sReg+1));
cUnit->regLocation[sReg].core = true;
cUnit->regLocation[sReg].defined = true;
sReg++;
break;
case 'F':
cUnit->regLocation[sReg].fp = true;
cUnit->regLocation[sReg].defined = true;
break;
default:
cUnit->regLocation[sReg].core = true;
cUnit->regLocation[sReg].defined = true;
break;
}
sReg++;
}
}
/* Remap names */
oatDataFlowAnalysisDispatcher(cUnit, remapNames,
kPreOrderDFSTraversal,
false /* isIterative */);
/* Do type & size inference pass */
oatDataFlowAnalysisDispatcher(cUnit, inferTypeAndSize,
kPreOrderDFSTraversal,
true /* isIterative */);
/*
* Set the sRegLow field to refer to the pre-SSA name of the
* base Dalvik virtual register. Once we add a better register
* allocator, remove this remapping.
*/
for (i=0; i < cUnit->numSSARegs; i++) {
if (cUnit->regLocation[i].location != kLocCompilerTemp) {
cUnit->regLocation[i].sRegLow = SRegToVReg(cUnit, loc[i].sRegLow);
}
}
cUnit->coreSpillMask = 0;
cUnit->fpSpillMask = 0;
cUnit->numCoreSpills = 0;
oatDoPromotion(cUnit);
if (cUnit->printMe && !(cUnit->disableOpt & (1 << kPromoteRegs))) {
LOG(INFO) << "After Promotion";
oatDumpRegLocTable(cUnit->regLocation, cUnit->numSSARegs);
}
/* Figure out the frame size */
static const uint32_t kAlignMask = kStackAlignment - 1;
uint32_t size = (cUnit->numCoreSpills + cUnit->numFPSpills +
cUnit->numRegs + cUnit->numOuts + cUnit->numCompilerTemps +
1 /* curMethod* */) * sizeof(uint32_t);
/* Align and set */
cUnit->frameSize = (size + kAlignMask) & ~(kAlignMask);
}
} // namespace art
|