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
|
/*
* 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"
#include "vm/compiler/Loop.h"
/* Max number of hoistable loop load operations. */
#define MAX_LOAD_HOISTS 25 // Should be more than enough, considering ARM only has 16 registers.
/* Max number of operations that access dalvik registers. */
#define MAX_REGISTER_OPS 50
/* Enable verbose prints for loop load hoisting by setting this define.
#define LOOP_LOAD_HOIST_VERBOSE
*/
/*
* Identify unconditional branches that jump to the immediate successor of the
* branch itself.
*/
static void applyRedundantBranchElimination(CompilationUnit *cUnit)
{
ArmLIR *thisLIR;
for (thisLIR = (ArmLIR *) cUnit->firstLIRInsn;
thisLIR != (ArmLIR *) cUnit->lastLIRInsn;
thisLIR = NEXT_LIR(thisLIR)) {
/* Branch to the next instruction */
if (thisLIR->opcode == kThumbBUncond) {
ArmLIR *nextLIR = thisLIR;
while (true) {
nextLIR = NEXT_LIR(nextLIR);
/*
* Is the branch target the next instruction?
*/
if (nextLIR == (ArmLIR *) 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 == (ArmLIR *) cUnit->lastLIRInsn))
break;
}
}
}
}
/*
* Perform a pass to hoist all frame pointer load instructions that
* are independent, outside the loop.
*/
static void applyLoopLoadHoisting(CompilationUnit *cUnit)
{
ArmLIR *thisLIR, *labelLIR, *lastLIR, *insertLIR;
ArmLIR *loadLIR[MAX_LOAD_HOISTS];
ArmLIR *regLIR[MAX_REGISTER_OPS];
u8 defLoadMask = 0;
u8 defMask = 0;
u8 masterDefMask = ~((1ULL << kRegEnd) - 1);
bool isValidLoop = false;
int loadCount = 0;
int regCount = 0;
int loadindex;
int hoistCount = 0;
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("GlobalOpts LoopLoadHoisting applied on '%s'", cUnit->method->name);
cUnit->printMe = true;
#endif
labelLIR = (ArmLIR *) cUnit->firstLIRInsn;
lastLIR = (ArmLIR *) cUnit->lastLIRInsn;
insertLIR = NULL;
/* Find the insert point */
while ((labelLIR != lastLIR) &&
(labelLIR->flags.isNop ||
(labelLIR->opcode != kArmPseudoNormalBlockLabel))) {
if ((cUnit->loopAnalysis->branchesAdded) &&
(labelLIR->opcode == kThumbBUncond) &&
(insertLIR == NULL) && (!labelLIR->flags.isNop))
insertLIR = labelLIR;
labelLIR = NEXT_LIR(labelLIR);
}
if (labelLIR->opcode != kArmPseudoNormalBlockLabel) {
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Can't hoist - no loop label found!");
#endif
return;
}
if (insertLIR == NULL) {
insertLIR = labelLIR;
}
else if ((ArmLIR *) insertLIR->generic.target != labelLIR) {
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Can't hoist - branch target does not match!");
#endif
return;
}
/* Search for eligible load instructions to hoist */
for (thisLIR = labelLIR;
thisLIR != lastLIR;
thisLIR = NEXT_LIR(thisLIR)) {
bool handled = false;
int flags;
/* Skip non-interesting instructions */
if (thisLIR->flags.isNop || isPseudoOpcode(thisLIR->opcode))
continue;
flags = EncodingMap[thisLIR->opcode].flags;
/* If it's a load instruction, check if it's a hoist candidate. */
if (((flags & IS_LOAD) != 0) &&
((thisLIR->useMask & ENCODE_DALVIK_REG) != 0)) {
if (regCount >= MAX_REGISTER_OPS) {
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Out of register list space!");
#endif
return;
}
regLIR[regCount++] = thisLIR;
if ((((defLoadMask | defMask) & thisLIR->defMask) == 0) &&
(loadCount < MAX_LOAD_HOISTS)) {
defLoadMask |= thisLIR->defMask;
loadLIR[loadCount++] = thisLIR;
handled = true;
} else {
masterDefMask |= thisLIR->defMask;
}
/* If it's a store instruction, check if it matches a previous
hoistable load instruction. If so, reset the global def-flag to
indicate that the load is still hoistable. */
} else if (((flags & IS_STORE) != 0) &&
((thisLIR->defMask & ENCODE_DALVIK_REG) != 0)) {
if (regCount >= MAX_REGISTER_OPS) {
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Out of register list space!");
#endif
return;
}
regLIR[regCount++] = thisLIR;
if ((thisLIR->useMask & defLoadMask) != 0) {
handled = true;
for (int i = loadCount - 1; i >= 0; i--) {
if ((thisLIR->aliasInfo == loadLIR[i]->aliasInfo) &&
(thisLIR->operands[0] == loadLIR[i]->operands[0])) {
defMask &= ~(loadLIR[i]->defMask);
break;
}
}
}
/* If it's a branch instruction, check if it's the loop branch.
If it matches the label, mark it as a valid loop. */
} else if ((flags & IS_BRANCH) != 0) {
handled = true;
if (labelLIR == (ArmLIR *) thisLIR->generic.target) {
isValidLoop = true;
} else if ((thisLIR->opcode >= kThumbBlx1) &&
(thisLIR->opcode <= kThumbBlxR))
{
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Trace contains branch to subroutine!");
#endif
return;
}
} else if (thisLIR->opcode == kThumbUndefined) {
break;
}
/* If it's not a 'special' instruction, accumulate into def-flags. */
if (!handled)
defMask |= thisLIR->defMask;
}
defLoadMask &= ~(defMask | masterDefMask);
if (!isValidLoop || (defLoadMask == 0)) {
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Loop not valid, or defLoadMask (0x%llx) was zero!", defLoadMask);
#endif
return;
}
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Masks: masterDef: 0x%llx, def: 0x%llx, final defLoad: 0x%llx",
masterDefMask, defMask, defLoadMask);
#endif
/* Try to hoist the load operations */
for (loadindex = 0; loadindex < loadCount; loadindex++) {
thisLIR = loadLIR[loadindex];
/* Host this load? */
if ((thisLIR->defMask & defLoadMask) == thisLIR->defMask) {
int i;
bool foundAlias = false;
for (i = 0; i < regCount; i++) {
if ((thisLIR->aliasInfo == regLIR[i]->aliasInfo) &&
(thisLIR->operands[0] != regLIR[i]->operands[0])) {
foundAlias = true;
for (int k = loadindex; k < loadCount; k++) {
if (loadLIR[k] == regLIR[i]) {
loadLIR[k]->defMask = -1;
break;
}
}
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Register alias found between these two load ops:");
dvmDumpLIRInsn((LIR*)thisLIR, NULL);
dvmDumpLIRInsn((LIR*)regLIR[i], NULL);
#endif
break;
}
}
if (!foundAlias) {
#ifdef LOOP_LOAD_HOIST_VERBOSE
ALOGD("Hoisting this load op:");
dvmDumpLIRInsn((LIR*)thisLIR, NULL);
#endif
ArmLIR *newLoadLIR = (ArmLIR *) dvmCompilerNew(sizeof(ArmLIR),
true);
*newLoadLIR = *thisLIR;
dvmCompilerInsertLIRBefore((LIR *) insertLIR,
(LIR *) newLoadLIR);
thisLIR->flags.isNop = true;
hoistCount++;
}
}
}
if (cUnit->printMe)
ALOGD("GlobalOpt LoopLoadHoist hoisted %d load ops.", hoistCount);
}
void dvmCompilerApplyGlobalOptimizations(CompilationUnit *cUnit)
{
applyRedundantBranchElimination(cUnit);
if (cUnit->jitMode == kJitLoop) {
applyLoopLoadHoisting(cUnit);
}
}
|