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
|
/*
* 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.
*/
/*
* This file contains Arm-specific register alloction support.
*/
#include "../../CompilerUtility.h"
#include "../../CompilerIR.h"
#include "../..//Dataflow.h"
#include "ArmLIR.h"
#include "Codegen.h"
#include "../Ralloc.h"
/*
* Placeholder routine until we do proper register allocation.
*/
typedef struct RefCounts {
int count;
int sReg;
bool doubleStart; // Starting vReg for a double
} RefCounts;
/* USE SSA names to count references of base Dalvik vRegs. */
STATIC void countRefs(CompilationUnit *cUnit, BasicBlock* bb,
RefCounts* counts, bool fp)
{
MIR* mir;
if (bb->blockType != kDalvikByteCode && bb->blockType != kEntryBlock &&
bb->blockType != kExitBlock)
return;
for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
SSARepresentation *ssaRep = mir->ssaRep;
if (ssaRep) {
int i;
int attrs = oatDataFlowAttributes[mir->dalvikInsn.opcode];
if (fp) {
// Mark 1st reg of double pairs
int first = 0;
int sReg;
if ((attrs & (DF_DA_WIDE|DF_FP_A)) == (DF_DA_WIDE|DF_FP_A)) {
sReg = DECODE_REG(
oatConvertSSARegToDalvik(cUnit, ssaRep->defs[0]));
counts[sReg].doubleStart = true;
}
if ((attrs & (DF_UA_WIDE|DF_FP_A)) == (DF_UA_WIDE|DF_FP_A)) {
sReg = DECODE_REG(
oatConvertSSARegToDalvik(cUnit, ssaRep->uses[first]));
counts[sReg].doubleStart = true;
}
if (attrs & DF_UA_WIDE) {
first += 2;
}
if ((attrs & (DF_UB_WIDE|DF_FP_B)) == (DF_UB_WIDE|DF_FP_B)) {
sReg = DECODE_REG(
oatConvertSSARegToDalvik(cUnit, ssaRep->uses[first]));
counts[sReg].doubleStart = true;
}
if (attrs & DF_UB_WIDE) {
first += 2;
}
if ((attrs & (DF_UC_WIDE|DF_FP_C)) == (DF_UC_WIDE|DF_FP_C)) {
sReg = DECODE_REG(
oatConvertSSARegToDalvik(cUnit, ssaRep->uses[first]));
counts[sReg].doubleStart = true;
}
}
for (i=0; i< ssaRep->numUses; i++) {
int origSreg = DECODE_REG(
oatConvertSSARegToDalvik(cUnit, ssaRep->uses[i]));
DCHECK_LT(origSreg, cUnit->method->NumRegisters());
bool fpUse = ssaRep->fpUse ? ssaRep->fpUse[i] : false;
if (fp == fpUse) {
counts[origSreg].count++;
}
}
for (i=0; i< ssaRep->numDefs; i++) {
if (attrs & DF_SETS_CONST) {
// CONST opcodes are untyped - don't pollute the counts
continue;
}
int origSreg = DECODE_REG(
oatConvertSSARegToDalvik(cUnit, ssaRep->defs[i]));
DCHECK_LT(origSreg, cUnit->method->NumRegisters());
bool fpDef = ssaRep->fpDef ? ssaRep->fpDef[i] : false;
if (fp == fpDef) {
counts[origSreg].count++;
}
}
}
}
}
/* qsort callback function, sort descending */
STATIC int sortCounts(const void *val1, const void *val2)
{
const RefCounts* op1 = (const RefCounts*)val1;
const RefCounts* op2 = (const RefCounts*)val2;
return (op1->count == op2->count) ? 0 : (op1->count < op2->count ? 1 : -1);
}
STATIC void dumpCounts(const RefCounts* arr, int size, const char* msg)
{
LOG(INFO) << msg;
for (int i = 0; i < size; i++) {
LOG(INFO) << "sReg[" << arr[i].sReg << "]: " << arr[i].count;
}
}
/*
* Note: some portions of this code required even if the kPromoteRegs
* optimization is disabled.
*/
extern void oatDoPromotion(CompilationUnit* cUnit)
{
int numRegs = cUnit->method->NumRegisters();
/*
* TUNING: is leaf? Can't just use "hasInvoke" to determine as some
* instructions might call out to C/assembly helper functions. Until
* machinery is in place, always spill lr.
*/
cUnit->coreSpillMask |= (1 << rLR);
cUnit->numSpills++;
/*
* Simple hack for testing register allocation. Just do a static
* count of the uses of Dalvik registers. Note that we examine
* the SSA names, but count based on original Dalvik register name.
* Count refs separately based on type in order to give allocation
* preference to fp doubles - which must be allocated sequential
* physical single fp registers started with an even-numbered
* reg.
*/
RefCounts *coreRegs = (RefCounts *)
oatNew(sizeof(RefCounts) * numRegs, true);
RefCounts *fpRegs = (RefCounts *)
oatNew(sizeof(RefCounts) * numRegs, true);
for (int i = 0; i < numRegs; i++) {
coreRegs[i].sReg = fpRegs[i].sReg = i;
}
GrowableListIterator iterator;
oatGrowableListIteratorInit(&cUnit->blockList, &iterator);
while (true) {
BasicBlock* bb;
bb = (BasicBlock*)oatGrowableListIteratorNext(&iterator);
if (bb == NULL) break;
countRefs(cUnit, bb, coreRegs, false);
countRefs(cUnit, bb, fpRegs, true);
}
/*
* Ideally, we'd allocate doubles starting with an even-numbered
* register. Bias the counts to try to allocate any vreg that's
* used as the start of a pair first.
*/
for (int i = 0; i < numRegs; i++) {
if (fpRegs[i].doubleStart) {
fpRegs[i].count *= 2;
}
}
// Sort the count arrays
qsort(coreRegs, numRegs, sizeof(RefCounts), sortCounts);
qsort(fpRegs, numRegs, sizeof(RefCounts), sortCounts);
if (cUnit->printMeVerbose) {
dumpCounts(coreRegs, numRegs, "coreRegs");
dumpCounts(fpRegs, numRegs, "fpRegs");
}
if (!(cUnit->disableOpt & (1 << kPromoteRegs))) {
// Promote fpRegs
for (int i = 0; (fpRegs[i].count > 0) && (i < numRegs); i++) {
if (cUnit->regLocation[fpRegs[i].sReg].fpLocation != kLocPhysReg) {
int reg = oatAllocPreservedFPReg(cUnit, fpRegs[i].sReg,
fpRegs[i].doubleStart);
if (reg < 0) {
break; // No more left
}
}
}
// Promote core regs
for (int i = 0; (coreRegs[i].count > 0) && i < numRegs; i++) {
if (cUnit->regLocation[i].location != kLocPhysReg) {
int reg = oatAllocPreservedCoreReg(cUnit, coreRegs[i].sReg);
if (reg < 0) {
break; // No more left
}
}
}
}
// Now, update SSA names to new home locations
for (int i = 0; i < cUnit->numSSARegs; i++) {
int baseSreg = cUnit->regLocation[i].sRegLow;
RegLocation *base = &cUnit->regLocation[baseSreg];
RegLocation *baseNext = &cUnit->regLocation[baseSreg+1];
RegLocation *curr = &cUnit->regLocation[i];
if (curr->fp) {
/* Single or double, check fpLocation of base */
if (base->fpLocation == kLocPhysReg) {
if (curr->wide) {
/* TUNING: consider alignment during allocation */
if ((base->fpLowReg & 1) ||
(baseNext->fpLocation != kLocPhysReg)) {
/* Half-promoted or bad alignment - demote */
curr->location = kLocDalvikFrame;
curr->lowReg = INVALID_REG;
curr->highReg = INVALID_REG;
continue;
}
curr->highReg = baseNext->fpLowReg;
}
curr->location = kLocPhysReg;
curr->lowReg = base->fpLowReg;
curr->home = true;
}
} else {
/* Core or wide */
if (base->location == kLocPhysReg) {
if (curr->wide) {
/* Make sure upper half is also in reg or skip */
if (baseNext->location != kLocPhysReg) {
/* Only half promoted; demote to frame */
curr->location = kLocDalvikFrame;
curr->lowReg = INVALID_REG;
curr->highReg = INVALID_REG;
continue;
}
curr->highReg = baseNext->lowReg;
}
curr->location = kLocPhysReg;
curr->lowReg = base->lowReg;
curr->home = true;
}
}
}
}
/* Returns sp-relative offset in bytes */
extern int oatVRegOffset(CompilationUnit* cUnit, int reg)
{
return (reg < cUnit->numRegs) ? cUnit->regsOffset + (reg << 2) :
cUnit->insOffset + ((reg - cUnit->numRegs) << 2);
}
/* Return sp-relative offset in bytes using Method* */
extern int oatVRegOffsetFromMethod(Method* method, int reg)
{
int numIns = method->NumIns();
int numRegs = method->NumRegisters() - numIns;
int numOuts = method->NumOuts();
int numSpills = __builtin_popcount(method->GetCoreSpillMask()) +
__builtin_popcount(method->GetFpSpillMask());
int numPadding = (STACK_ALIGN_WORDS -
(numSpills + numRegs + numOuts + 2)) & (STACK_ALIGN_WORDS-1);
int regsOffset = (numOuts + numPadding + 1) * 4;
int insOffset = method->GetFrameSizeInBytes() + 4;
return (reg < numRegs) ? regsOffset + (reg << 2) :
insOffset + ((reg - numRegs) << 2);
}
/* Clobber all regs that might be used by an external C call */
extern void oatClobberCallRegs(CompilationUnit *cUnit)
{
oatClobber(cUnit, r0);
oatClobber(cUnit, r1);
oatClobber(cUnit, r2);
oatClobber(cUnit, r3);
oatClobber(cUnit, r12);
oatClobber(cUnit, r14lr);
}
extern RegLocation oatGetReturnWide(CompilationUnit* cUnit)
{
RegLocation res = LOC_C_RETURN_WIDE;
oatClobber(cUnit, r0);
oatClobber(cUnit, r1);
oatMarkInUse(cUnit, r0);
oatMarkInUse(cUnit, r1);
oatMarkPair(cUnit, res.lowReg, res.highReg);
return res;
}
extern RegLocation oatGetReturnWideAlt(CompilationUnit* cUnit)
{
RegLocation res = LOC_C_RETURN_WIDE;
res.lowReg = r2;
res.highReg = r3;
oatClobber(cUnit, r2);
oatClobber(cUnit, r3);
oatMarkInUse(cUnit, r2);
oatMarkInUse(cUnit, r3);
oatMarkPair(cUnit, res.lowReg, res.highReg);
return res;
}
extern RegLocation oatGetReturn(CompilationUnit* cUnit)
{
RegLocation res = LOC_C_RETURN;
oatClobber(cUnit, r0);
oatMarkInUse(cUnit, r0);
return res;
}
extern RegLocation oatGetReturnAlt(CompilationUnit* cUnit)
{
RegLocation res = LOC_C_RETURN;
res.lowReg = r1;
oatClobber(cUnit, r1);
oatMarkInUse(cUnit, r1);
return res;
}
|