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
|
/*
* 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 "MipsLIR.h"
/*
* Identify unconditional branches that jump to the immediate successor of the
* branch itself.
*/
static void applyRedundantBranchElimination(CompilationUnit *cUnit)
{
MipsLIR *thisLIR;
for (thisLIR = (MipsLIR *) cUnit->firstLIRInsn;
thisLIR != (MipsLIR *) cUnit->lastLIRInsn;
thisLIR = NEXT_LIR(thisLIR)) {
/* Branch to the next instruction */
if (!thisLIR->flags.isNop && thisLIR->opcode == kMipsB) {
MipsLIR *nextLIR = thisLIR;
while (true) {
nextLIR = NEXT_LIR(nextLIR);
/*
* Is the branch target the next instruction?
*/
if (nextLIR == (MipsLIR *) thisLIR->generic.target) {
thisLIR->flags.isNop = true;
break;
}
/*
* Found real useful stuff between the branch and the target.
* Need to explicitly check the lastLIRInsn here since with
* method-based JIT the branch might be the last real
* instruction.
*/
if (!isPseudoOpCode(nextLIR->opcode) ||
(nextLIR = (MipsLIR *) cUnit->lastLIRInsn))
break;
}
}
}
}
/*
* Do simple a form of copy propagation and elimination.
*/
static void applyCopyPropagation(CompilationUnit *cUnit)
{
MipsLIR *thisLIR;
/* look for copies to possibly eliminate */
for (thisLIR = (MipsLIR *) cUnit->firstLIRInsn;
thisLIR != (MipsLIR *) cUnit->lastLIRInsn;
thisLIR = NEXT_LIR(thisLIR)) {
if (thisLIR->flags.isNop || thisLIR->opcode != kMipsMove)
continue;
const int max_insns = 10;
MipsLIR *savedLIR[max_insns];
int srcRedefined = 0;
int insnCount = 0;
MipsLIR *nextLIR;
/* look for and record all uses of reg defined by the copy */
for (nextLIR = (MipsLIR *) NEXT_LIR(thisLIR);
nextLIR != (MipsLIR *) cUnit->lastLIRInsn;
nextLIR = NEXT_LIR(nextLIR)) {
if (nextLIR->flags.isNop || nextLIR->opcode == kMips32BitData)
continue;
if (isPseudoOpCode(nextLIR->opcode)) {
if (nextLIR->opcode == kMipsPseudoDalvikByteCodeBoundary ||
nextLIR->opcode == kMipsPseudoBarrier ||
nextLIR->opcode == kMipsPseudoExtended ||
nextLIR->opcode == kMipsPseudoSSARep)
continue; /* these pseudos don't pose problems */
else if (nextLIR->opcode == kMipsPseudoTargetLabel ||
nextLIR->opcode == kMipsPseudoEntryBlock ||
nextLIR->opcode == kMipsPseudoExitBlock)
insnCount = 0; /* give up for these pseudos */
break; /* reached end for copy propagation */
}
/* Since instructions with IS_BRANCH flag set will have its */
/* useMask and defMask set to ENCODE_ALL, any checking of */
/* these flags must come after the branching checks. */
/* don't propagate across branch/jump and link case
or jump via register */
if (EncodingMap[nextLIR->opcode].flags & REG_DEF_LR ||
nextLIR->opcode == kMipsJalr ||
nextLIR->opcode == kMipsJr) {
insnCount = 0;
break;
}
/* branches with certain targets ok while others aren't */
if (EncodingMap[nextLIR->opcode].flags & IS_BRANCH) {
MipsLIR *targetLIR = (MipsLIR *) nextLIR->generic.target;
if (targetLIR->opcode != kMipsPseudoEHBlockLabel &&
targetLIR->opcode != kMipsPseudoChainingCellHot &&
targetLIR->opcode != kMipsPseudoChainingCellNormal &&
targetLIR->opcode != kMipsPseudoChainingCellInvokePredicted &&
targetLIR->opcode != kMipsPseudoChainingCellInvokeSingleton &&
targetLIR->opcode != kMipsPseudoPCReconstructionBlockLabel &&
targetLIR->opcode != kMipsPseudoPCReconstructionCell) {
insnCount = 0;
break;
}
/* FIXME - for now don't propagate across any branch/jump. */
insnCount = 0;
break;
}
/* copy def reg used here, so record insn for copy propagation */
if (thisLIR->defMask & nextLIR->useMask) {
if (insnCount == max_insns || srcRedefined) {
insnCount = 0;
break; /* just give up if too many or not possible */
}
savedLIR[insnCount++] = nextLIR;
}
if (thisLIR->defMask & nextLIR->defMask) {
if (nextLIR->opcode == kMipsMovz)
insnCount = 0; /* movz relies on thisLIR setting dst reg so abandon propagation*/
break;
}
/* copy src reg redefined here, so can't propagate further */
if (thisLIR->useMask & nextLIR->defMask) {
if (insnCount == 0)
break; /* nothing to propagate */
srcRedefined = 1;
}
}
/* conditions allow propagation and copy elimination */
if (insnCount) {
int i;
for (i = 0; i < insnCount; i++) {
int flags = EncodingMap[savedLIR[i]->opcode].flags;
savedLIR[i]->useMask &= ~(1 << thisLIR->operands[0]);
savedLIR[i]->useMask |= 1 << thisLIR->operands[1];
if ((flags & REG_USE0) &&
savedLIR[i]->operands[0] == thisLIR->operands[0])
savedLIR[i]->operands[0] = thisLIR->operands[1];
if ((flags & REG_USE1) &&
savedLIR[i]->operands[1] == thisLIR->operands[0])
savedLIR[i]->operands[1] = thisLIR->operands[1];
if ((flags & REG_USE2) &&
savedLIR[i]->operands[2] == thisLIR->operands[0])
savedLIR[i]->operands[2] = thisLIR->operands[1];
if ((flags & REG_USE3) &&
savedLIR[i]->operands[3] == thisLIR->operands[0])
savedLIR[i]->operands[3] = thisLIR->operands[1];
}
thisLIR->flags.isNop = true;
}
}
}
#ifdef __mips_hard_float
/*
* Look for pairs of mov.s instructions that can be combined into mov.d
*/
static void mergeMovs(CompilationUnit *cUnit)
{
MipsLIR *movsLIR = NULL;
MipsLIR *thisLIR;
for (thisLIR = (MipsLIR *) cUnit->firstLIRInsn;
thisLIR != (MipsLIR *) cUnit->lastLIRInsn;
thisLIR = NEXT_LIR(thisLIR)) {
if (thisLIR->flags.isNop)
continue;
if (isPseudoOpCode(thisLIR->opcode)) {
if (thisLIR->opcode == kMipsPseudoDalvikByteCodeBoundary ||
thisLIR->opcode == kMipsPseudoExtended ||
thisLIR->opcode == kMipsPseudoSSARep)
continue; /* ok to move across these pseudos */
movsLIR = NULL; /* don't merge across other pseudos */
continue;
}
/* merge pairs of mov.s instructions */
if (thisLIR->opcode == kMipsFmovs) {
if (movsLIR == NULL)
movsLIR = thisLIR;
else if (((movsLIR->operands[0] & 1) == 0) &&
((movsLIR->operands[1] & 1) == 0) &&
((movsLIR->operands[0] + 1) == thisLIR->operands[0]) &&
((movsLIR->operands[1] + 1) == thisLIR->operands[1])) {
/* movsLIR is handling even register - upgrade to mov.d */
movsLIR->opcode = kMipsFmovd;
movsLIR->operands[0] = S2D(movsLIR->operands[0], movsLIR->operands[0]+1);
movsLIR->operands[1] = S2D(movsLIR->operands[1], movsLIR->operands[1]+1);
thisLIR->flags.isNop = true;
movsLIR = NULL;
}
else if (((movsLIR->operands[0] & 1) == 1) &&
((movsLIR->operands[1] & 1) == 1) &&
((movsLIR->operands[0] - 1) == thisLIR->operands[0]) &&
((movsLIR->operands[1] - 1) == thisLIR->operands[1])) {
/* thissLIR is handling even register - upgrade to mov.d */
thisLIR->opcode = kMipsFmovd;
thisLIR->operands[0] = S2D(thisLIR->operands[0], thisLIR->operands[0]+1);
thisLIR->operands[1] = S2D(thisLIR->operands[1], thisLIR->operands[1]+1);
movsLIR->flags.isNop = true;
movsLIR = NULL;
}
else
/* carry on searching from here */
movsLIR = thisLIR;
continue;
}
/* intervening instruction - start search from scratch */
movsLIR = NULL;
}
}
#endif
/*
* Look back first and then ahead to try to find an instruction to move into
* the branch delay slot. If the analysis can be done cheaply enough, it may be
* be possible to tune this routine to be more beneficial (e.g., being more
* particular about what instruction is speculated).
*/
static MipsLIR *delaySlotLIR(MipsLIR *firstLIR, MipsLIR *branchLIR)
{
int isLoad;
int loadVisited = 0;
int isStore;
int storeVisited = 0;
u8 useMask = branchLIR->useMask;
u8 defMask = branchLIR->defMask;
MipsLIR *thisLIR;
MipsLIR *newLIR = (MipsLIR *) dvmCompilerNew(sizeof(MipsLIR), true);
for (thisLIR = PREV_LIR(branchLIR);
thisLIR != firstLIR;
thisLIR = PREV_LIR(thisLIR)) {
if (thisLIR->flags.isNop)
continue;
if (isPseudoOpCode(thisLIR->opcode)) {
if (thisLIR->opcode == kMipsPseudoDalvikByteCodeBoundary ||
thisLIR->opcode == kMipsPseudoExtended ||
thisLIR->opcode == kMipsPseudoSSARep)
continue; /* ok to move across these pseudos */
break; /* don't move across all other pseudos */
}
/* give up on moving previous instruction down into slot */
if (thisLIR->opcode == kMipsNop ||
thisLIR->opcode == kMips32BitData ||
EncodingMap[thisLIR->opcode].flags & IS_BRANCH)
break;
/* don't reorder loads/stores (the alias info could
possibly be used to allow as a future enhancement) */
isLoad = EncodingMap[thisLIR->opcode].flags & IS_LOAD;
isStore = EncodingMap[thisLIR->opcode].flags & IS_STORE;
if (!(thisLIR->useMask & defMask) &&
!(thisLIR->defMask & useMask) &&
!(thisLIR->defMask & defMask) &&
!(isLoad && storeVisited) &&
!(isStore && loadVisited) &&
!(isStore && storeVisited)) {
*newLIR = *thisLIR;
thisLIR->flags.isNop = true;
return newLIR; /* move into delay slot succeeded */
}
loadVisited |= isLoad;
storeVisited |= isStore;
/* accumulate def/use constraints */
useMask |= thisLIR->useMask;
defMask |= thisLIR->defMask;
}
/* for unconditional branches try to copy the instruction at the
branch target up into the delay slot and adjust the branch */
if (branchLIR->opcode == kMipsB) {
MipsLIR *targetLIR;
for (targetLIR = (MipsLIR *) branchLIR->generic.target;
targetLIR;
targetLIR = NEXT_LIR(targetLIR)) {
if (!targetLIR->flags.isNop &&
(!isPseudoOpCode(targetLIR->opcode) || /* can't pull predicted up */
targetLIR->opcode == kMipsPseudoChainingCellInvokePredicted))
break; /* try to get to next real op at branch target */
}
if (targetLIR && !isPseudoOpCode(targetLIR->opcode) &&
!(EncodingMap[targetLIR->opcode].flags & IS_BRANCH)) {
*newLIR = *targetLIR;
branchLIR->generic.target = (LIR *) NEXT_LIR(targetLIR);
return newLIR;
}
} else if (branchLIR->opcode >= kMipsBeq && branchLIR->opcode <= kMipsBne) {
/* for conditional branches try to fill branch delay slot
via speculative execution when safe */
MipsLIR *targetLIR;
for (targetLIR = (MipsLIR *) branchLIR->generic.target;
targetLIR;
targetLIR = NEXT_LIR(targetLIR)) {
if (!targetLIR->flags.isNop && !isPseudoOpCode(targetLIR->opcode))
break; /* try to get to next real op at branch target */
}
MipsLIR *nextLIR;
for (nextLIR = NEXT_LIR(branchLIR);
nextLIR;
nextLIR = NEXT_LIR(nextLIR)) {
if (!nextLIR->flags.isNop && !isPseudoOpCode(nextLIR->opcode))
break; /* try to get to next real op for fall thru */
}
if (nextLIR && targetLIR) {
int flags = EncodingMap[nextLIR->opcode].flags;
int isLoad = flags & IS_LOAD;
/* common branch and fall thru to normal chaining cells case */
if (isLoad && nextLIR->opcode == targetLIR->opcode &&
nextLIR->operands[0] == targetLIR->operands[0] &&
nextLIR->operands[1] == targetLIR->operands[1] &&
nextLIR->operands[2] == targetLIR->operands[2]) {
*newLIR = *targetLIR;
branchLIR->generic.target = (LIR *) NEXT_LIR(targetLIR);
return newLIR;
}
/* try prefetching (maybe try speculating instructions along the
trace like dalvik frame load which is common and may be safe) */
int isStore = flags & IS_STORE;
if (isLoad || isStore) {
newLIR->opcode = kMipsPref;
newLIR->operands[0] = isLoad ? 0 : 1;
newLIR->operands[1] = nextLIR->operands[1];
newLIR->operands[2] = nextLIR->operands[2];
newLIR->defMask = nextLIR->defMask;
newLIR->useMask = nextLIR->useMask;
return newLIR;
}
}
}
/* couldn't find a useful instruction to move into the delay slot */
newLIR->opcode = kMipsNop;
return newLIR;
}
/*
* The branch delay slot has been ignored thus far. This is the point where
* a useful instruction is moved into it or a nop is inserted. Leave existing
* NOPs alone -- these came from sparse and packed switch ops and are needed
* to maintain the proper offset to the jump table.
*/
static void introduceBranchDelaySlot(CompilationUnit *cUnit)
{
MipsLIR *thisLIR;
MipsLIR *firstLIR =(MipsLIR *) cUnit->firstLIRInsn;
MipsLIR *lastLIR =(MipsLIR *) cUnit->lastLIRInsn;
for (thisLIR = lastLIR; thisLIR != firstLIR; thisLIR = PREV_LIR(thisLIR)) {
if (thisLIR->flags.isNop ||
isPseudoOpCode(thisLIR->opcode) ||
!(EncodingMap[thisLIR->opcode].flags & IS_BRANCH)) {
continue;
} else if (thisLIR == lastLIR) {
dvmCompilerAppendLIR(cUnit,
(LIR *) delaySlotLIR(firstLIR, thisLIR));
} else if (NEXT_LIR(thisLIR)->opcode != kMipsNop) {
dvmCompilerInsertLIRAfter((LIR *) thisLIR,
(LIR *) delaySlotLIR(firstLIR, thisLIR));
}
}
if (!thisLIR->flags.isNop &&
!isPseudoOpCode(thisLIR->opcode) &&
EncodingMap[thisLIR->opcode].flags & IS_BRANCH) {
/* nothing available to move, so insert nop */
MipsLIR *nopLIR = (MipsLIR *) dvmCompilerNew(sizeof(MipsLIR), true);
nopLIR->opcode = kMipsNop;
dvmCompilerInsertLIRAfter((LIR *) thisLIR, (LIR *) nopLIR);
}
}
void dvmCompilerApplyGlobalOptimizations(CompilationUnit *cUnit)
{
applyRedundantBranchElimination(cUnit);
applyCopyPropagation(cUnit);
#ifdef __mips_hard_float
mergeMovs(cUnit);
#endif
introduceBranchDelaySlot(cUnit);
}
|