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
|
/*
* Copyright (C) 2009 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 "vm/compiler/CompilerInternals.h"
#include "ArmLIR.h"
ArmLIR* dvmCompilerGenCopy(CompilationUnit *cUnit, int rDest, int rSrc);
/* Is this a Dalvik register access? */
static inline bool isDalvikLoad(ArmLIR *lir)
{
return ((lir->operands[1] == rFP) &&
((lir->opCode == THUMB_LDR_RRI5) ||
(lir->opCode == THUMB2_LDR_RRI12) ||
(lir->opCode == THUMB2_VLDRS) ||
(lir->opCode == THUMB2_VLDRD)));
}
static inline bool isDalvikStore(ArmLIR *lir)
{
return ((lir->operands[1] == rFP) &&
((lir->opCode == THUMB_STR_RRI5) ||
(lir->opCode == THUMB2_STR_RRI12) ||
(lir->opCode == THUMB2_VSTRS) ||
(lir->opCode == THUMB2_VSTRD)));
}
/* Double regs overlap float regs. Return true if collision */
static bool regClobber(int reg1, int reg2)
{
int reg1a, reg1b;
int reg2a, reg2b;
if (!FPREG(reg1) || !FPREG(reg2))
return (reg1 == reg2);
if (DOUBLEREG(reg1)) {
reg1a = reg1 & FP_REG_MASK;
reg1b = reg1a + 1;
} else {
reg1a = reg1b = reg1 & FP_REG_MASK;
}
if (DOUBLEREG(reg2)) {
reg2a = reg2 & FP_REG_MASK;
reg2b = reg2a + 1;
} else {
reg2a = reg2b = reg2 & FP_REG_MASK;
}
return (reg1a == reg2a) || (reg1a == reg2b) ||
(reg1b == reg2a) || (reg1b == reg2b);
}
/*
* Perform a pass of top-down walk to
* 1) Eliminate redundant loads and stores
* 2) Sink stores to latest possible slot
*/
static void applyLoadStoreElimination(CompilationUnit *cUnit,
ArmLIR *headLIR,
ArmLIR *tailLIR)
{
ArmLIR *thisLIR;
cUnit->optRound++;
for (thisLIR = headLIR;
thisLIR != tailLIR;
thisLIR = NEXT_LIR(thisLIR)) {
/* Skip newly added instructions */
if (thisLIR->age >= cUnit->optRound) {
continue;
}
if (isDalvikStore(thisLIR)) {
int dRegId = thisLIR->operands[2];
int nativeRegId = thisLIR->operands[0];
ArmLIR *checkLIR;
int sinkDistance = 0;
/*
* Add r15 (pc) to the mask to prevent this instruction
* from sinking past branch instructions.
*/
u8 stopMask = ENCODE_GP_REG(rpc) | thisLIR->useMask;
for (checkLIR = NEXT_LIR(thisLIR);
checkLIR != tailLIR;
checkLIR = NEXT_LIR(checkLIR)) {
/* Check if a Dalvik register load is redundant */
if (isDalvikLoad(checkLIR) &&
checkLIR->operands[2] == dRegId ) {
if (FPREG(nativeRegId) != FPREG(checkLIR->operands[0])) {
break; // TODO: handle gen<=>float copies
}
/* Insert a move to replace the load */
if (checkLIR->operands[0] != nativeRegId) {
ArmLIR *moveLIR;
moveLIR = dvmCompilerRegCopy(cUnit,
checkLIR->operands[0],
nativeRegId);
/*
* Insertion is guaranteed to succeed since checkLIR
* is never the first LIR on the list
*/
dvmCompilerInsertLIRBefore((LIR *) checkLIR,
(LIR *) moveLIR);
}
checkLIR->isNop = true;
continue;
/* Found a true output dependency - nuke the previous store */
} else if (isDalvikStore(checkLIR) &&
checkLIR->operands[2] == dRegId) {
thisLIR->isNop = true;
break;
/* Find out the latest slot that the store can be sunk into */
} else {
bool stopHere = false;
/* Last instruction reached */
stopHere |= NEXT_LIR(checkLIR) == tailLIR;
/*
* Conservatively assume there is a memory dependency
* for st/ld multiples and reg+reg address mode
*/
stopHere |= checkLIR->opCode == THUMB_STMIA ||
checkLIR->opCode == THUMB_LDMIA ||
checkLIR->opCode == THUMB_STR_RRR ||
checkLIR->opCode == THUMB_LDR_RRR ||
checkLIR->opCode == THUMB2_STR_RRR ||
checkLIR->opCode == THUMB2_LDR_RRR ||
checkLIR->opCode == THUMB2_STMIA ||
checkLIR->opCode == THUMB2_LDMIA ||
checkLIR->opCode == THUMB2_VLDRD ||
checkLIR->opCode == THUMB2_VSTRD;
/* Store data is clobbered */
stopHere |= (stopMask & checkLIR->defMask) != 0;
/* Found a new place to put the store - move it here */
if (stopHere == true) {
/* The store can be sunk for at least one cycle */
if (sinkDistance != 0) {
ArmLIR *newStoreLIR =
dvmCompilerNew(sizeof(ArmLIR), true);
*newStoreLIR = *thisLIR;
newStoreLIR->age = cUnit->optRound;
/*
* Insertion is guaranteed to succeed since checkLIR
* is never the first LIR on the list
*/
dvmCompilerInsertLIRBefore((LIR *) checkLIR,
(LIR *) newStoreLIR);
thisLIR->isNop = true;
}
break;
}
/*
* Saw a real instruction that the store can be sunk after
*/
if (!isPseudoOpCode(checkLIR->opCode)) {
sinkDistance++;
}
}
}
}
}
}
static void applyLoadHoisting(CompilationUnit *cUnit,
ArmLIR *headLIR,
ArmLIR *tailLIR)
{
ArmLIR *thisLIR;
cUnit->optRound++;
for (thisLIR = headLIR;
thisLIR != tailLIR;
thisLIR = NEXT_LIR(thisLIR)) {
/* Skip newly added instructions */
if (thisLIR->age >= cUnit->optRound ||
thisLIR->isNop == true) {
continue;
}
if (isDalvikLoad(thisLIR)) {
int dRegId = thisLIR->operands[2];
int nativeRegId = thisLIR->operands[0];
ArmLIR *checkLIR;
int hoistDistance = 0;
u8 stopUseMask = ENCODE_GP_REG(rpc) | thisLIR->useMask;
u8 stopDefMask = thisLIR->defMask;
for (checkLIR = PREV_LIR(thisLIR);
checkLIR != headLIR;
checkLIR = PREV_LIR(checkLIR)) {
if (checkLIR->isNop) continue;
/* Check if the current load is redundant */
if ((isDalvikLoad(checkLIR) || isDalvikStore(checkLIR)) &&
checkLIR->operands[2] == dRegId ) {
if (FPREG(nativeRegId) != FPREG(checkLIR->operands[0])) {
break; // TODO: handle gen<=>float copies
}
/* Insert a move to replace the load */
if (checkLIR->operands[0] != nativeRegId) {
ArmLIR *moveLIR;
moveLIR = dvmCompilerRegCopy(cUnit,
nativeRegId,
checkLIR->operands[0]);
/*
* Convert *thisLIR* load into a move
*/
dvmCompilerInsertLIRAfter((LIR *) checkLIR,
(LIR *) moveLIR);
}
cUnit->printMe = true;
thisLIR->isNop = true;
break;
/* Find out if the load can be yanked past the checkLIR */
} else {
bool stopHere = false;
/* Last instruction reached */
stopHere |= PREV_LIR(checkLIR) == headLIR;
/*
* Conservatively assume there is a memory dependency
* for st/ld multiples and reg+reg address mode
*/
stopHere |= checkLIR->opCode == THUMB_STMIA ||
checkLIR->opCode == THUMB_LDMIA ||
checkLIR->opCode == THUMB_STR_RRR ||
checkLIR->opCode == THUMB_LDR_RRR ||
checkLIR->opCode == THUMB2_STR_RRR ||
checkLIR->opCode == THUMB2_LDR_RRR ||
checkLIR->opCode == THUMB2_STMIA ||
checkLIR->opCode == THUMB2_LDMIA ||
checkLIR->opCode == THUMB2_VLDRD ||
checkLIR->opCode == THUMB2_VSTRD;
/* Base address is clobbered by checkLIR */
stopHere |= (stopUseMask & checkLIR->defMask) != 0;
/* Load target clobbers use/def in checkLIR */
stopHere |= (stopDefMask &
(checkLIR->useMask | checkLIR->defMask)) != 0;
/* Found a new place to put the load - move it here */
if (stopHere == true) {
/* The store can be hoisted for at least one cycle */
if (hoistDistance != 0) {
ArmLIR *newLoadLIR =
dvmCompilerNew(sizeof(ArmLIR), true);
*newLoadLIR = *thisLIR;
newLoadLIR->age = cUnit->optRound;
/*
* Insertion is guaranteed to succeed since checkLIR
* is never the first LIR on the list
*/
dvmCompilerInsertLIRAfter((LIR *) checkLIR,
(LIR *) newLoadLIR);
thisLIR->isNop = true;
cUnit->printMe = true;
}
break;
}
/*
* Saw a real instruction that the store can be sunk after
*/
if (!isPseudoOpCode(checkLIR->opCode)) {
hoistDistance++;
}
}
}
}
}
}
void dvmCompilerApplyLocalOptimizations(CompilationUnit *cUnit, LIR *headLIR,
LIR *tailLIR)
{
if (!(gDvmJit.disableOpt & (1 << kLoadStoreElimination))) {
applyLoadStoreElimination(cUnit, (ArmLIR *) headLIR,
(ArmLIR *) tailLIR);
}
if (!(gDvmJit.disableOpt & (1 << kLoadHoisting))) {
applyLoadHoisting(cUnit, (ArmLIR *) headLIR, (ArmLIR *) tailLIR);
}
}
|