blob: 0b1330f162d8d33ca4827501496025b99dc0f564 [file] [log] [blame]
Ben Cheng7a2697d2010-06-07 13:44:23 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Dalvik.h"
18#include "Dataflow.h"
Dan Bornsteindf4daaf2010-12-01 14:23:44 -080019#include "libdex/DexOpcodes.h"
Ben Cheng7a2697d2010-06-07 13:44:23 -070020
21/* Convert the reg id from the callee to the original id passed by the caller */
22static inline u4 convertRegId(const DecodedInstruction *invoke,
23 const Method *calleeMethod,
24 int calleeRegId, bool isRange)
25{
26 /* The order in the original arg passing list */
27 int rank = calleeRegId -
28 (calleeMethod->registersSize - calleeMethod->insSize);
29 assert(rank >= 0);
30 if (!isRange) {
31 return invoke->arg[rank];
32 } else {
33 return invoke->vC + rank;
34 }
35}
36
37static void inlineGetter(CompilationUnit *cUnit,
38 const Method *calleeMethod,
39 MIR *invokeMIR,
40 BasicBlock *invokeBB,
41 bool isPredicted,
42 bool isRange)
43{
44 BasicBlock *moveResultBB = invokeBB->fallThrough;
45 MIR *moveResultMIR = moveResultBB->firstMIRInsn;
46 MIR *newGetterMIR = dvmCompilerNew(sizeof(MIR), true);
47 DecodedInstruction getterInsn;
48
Dan Bornstein54322392010-11-17 14:16:56 -080049 dexDecodeInstruction(calleeMethod->insns, &getterInsn);
Ben Cheng7a2697d2010-06-07 13:44:23 -070050
51 if (!dvmCompilerCanIncludeThisInstruction(calleeMethod, &getterInsn))
52 return;
53
54 /*
55 * Some getters (especially invoked through interface) are not followed
56 * by a move result.
57 */
58 if ((moveResultMIR == NULL) ||
Dan Bornstein9a1f8162010-12-01 17:02:26 -080059 (moveResultMIR->dalvikInsn.opcode != OP_MOVE_RESULT &&
60 moveResultMIR->dalvikInsn.opcode != OP_MOVE_RESULT_OBJECT &&
61 moveResultMIR->dalvikInsn.opcode != OP_MOVE_RESULT_WIDE)) {
Ben Cheng7a2697d2010-06-07 13:44:23 -070062 return;
63 }
64
Dan Bornstein9a1f8162010-12-01 17:02:26 -080065 int dfFlags = dvmCompilerDataFlowAttributes[getterInsn.opcode];
Ben Cheng7a2697d2010-06-07 13:44:23 -070066
67 /* Expecting vA to be the destination register */
Ben Cheng52d4cd22010-08-16 14:08:49 -070068 if (dfFlags & (DF_UA | DF_UA_WIDE)) {
Dan Bornstein9a1f8162010-12-01 17:02:26 -080069 LOGE("opcode %d has DF_UA set (not expected)", getterInsn.opcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -070070 dvmAbort();
71 }
72
73 if (dfFlags & DF_UB) {
74 getterInsn.vB = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
75 getterInsn.vB, isRange);
76 }
77
78 if (dfFlags & DF_UC) {
79 getterInsn.vC = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
80 getterInsn.vC, isRange);
81 }
82
83 getterInsn.vA = moveResultMIR->dalvikInsn.vA;
84
85 /* Now setup the Dalvik instruction with converted src/dst registers */
86 newGetterMIR->dalvikInsn = getterInsn;
87
Dan Bornsteine4852762010-12-02 12:45:00 -080088 newGetterMIR->width = dexGetWidthFromOpcode(getterInsn.opcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -070089
90 newGetterMIR->OptimizationFlags |= MIR_CALLEE;
91
92 /*
93 * If the getter instruction is about to raise any exception, punt to the
94 * interpreter and re-execute the invoke.
95 */
96 newGetterMIR->offset = invokeMIR->offset;
97
98 newGetterMIR->meta.calleeMethod = calleeMethod;
99
100 dvmCompilerInsertMIRAfter(invokeBB, invokeMIR, newGetterMIR);
101
102 if (isPredicted) {
103 MIR *invokeMIRSlow = dvmCompilerNew(sizeof(MIR), true);
104 *invokeMIRSlow = *invokeMIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800105 invokeMIR->dalvikInsn.opcode = kMirOpCheckInlinePrediction;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700106
107 /* Use vC to denote the first argument (ie this) */
108 if (!isRange) {
109 invokeMIR->dalvikInsn.vC = invokeMIRSlow->dalvikInsn.arg[0];
110 }
111
112 moveResultMIR->OptimizationFlags |= MIR_INLINED_PRED;
113
114 dvmCompilerInsertMIRAfter(invokeBB, newGetterMIR, invokeMIRSlow);
115 invokeMIRSlow->OptimizationFlags |= MIR_INLINED_PRED;
116#if defined(WITH_JIT_TUNING)
117 gDvmJit.invokePolyGetterInlined++;
118#endif
119 } else {
120 invokeMIR->OptimizationFlags |= MIR_INLINED;
121 moveResultMIR->OptimizationFlags |= MIR_INLINED;
122#if defined(WITH_JIT_TUNING)
123 gDvmJit.invokeMonoGetterInlined++;
124#endif
125 }
126
127 return;
128}
129
130static void inlineSetter(CompilationUnit *cUnit,
131 const Method *calleeMethod,
132 MIR *invokeMIR,
133 BasicBlock *invokeBB,
134 bool isPredicted,
135 bool isRange)
136{
137 MIR *newSetterMIR = dvmCompilerNew(sizeof(MIR), true);
138 DecodedInstruction setterInsn;
139
Dan Bornstein54322392010-11-17 14:16:56 -0800140 dexDecodeInstruction(calleeMethod->insns, &setterInsn);
Ben Cheng7a2697d2010-06-07 13:44:23 -0700141
142 if (!dvmCompilerCanIncludeThisInstruction(calleeMethod, &setterInsn))
143 return;
144
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800145 int dfFlags = dvmCompilerDataFlowAttributes[setterInsn.opcode];
Ben Cheng7a2697d2010-06-07 13:44:23 -0700146
Ben Cheng52d4cd22010-08-16 14:08:49 -0700147 if (dfFlags & (DF_UA | DF_UA_WIDE)) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700148 setterInsn.vA = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
149 setterInsn.vA, isRange);
150
151 }
152
153 if (dfFlags & DF_UB) {
154 setterInsn.vB = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
155 setterInsn.vB, isRange);
156
157 }
158
159 if (dfFlags & DF_UC) {
160 setterInsn.vC = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
161 setterInsn.vC, isRange);
162 }
163
164 /* Now setup the Dalvik instruction with converted src/dst registers */
165 newSetterMIR->dalvikInsn = setterInsn;
166
Dan Bornsteine4852762010-12-02 12:45:00 -0800167 newSetterMIR->width = dexGetWidthFromOpcode(setterInsn.opcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -0700168
169 newSetterMIR->OptimizationFlags |= MIR_CALLEE;
170
171 /*
172 * If the setter instruction is about to raise any exception, punt to the
173 * interpreter and re-execute the invoke.
174 */
175 newSetterMIR->offset = invokeMIR->offset;
176
177 newSetterMIR->meta.calleeMethod = calleeMethod;
178
179 dvmCompilerInsertMIRAfter(invokeBB, invokeMIR, newSetterMIR);
180
181 if (isPredicted) {
182 MIR *invokeMIRSlow = dvmCompilerNew(sizeof(MIR), true);
183 *invokeMIRSlow = *invokeMIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800184 invokeMIR->dalvikInsn.opcode = kMirOpCheckInlinePrediction;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700185
186 /* Use vC to denote the first argument (ie this) */
187 if (!isRange) {
188 invokeMIR->dalvikInsn.vC = invokeMIRSlow->dalvikInsn.arg[0];
189 }
190
191 dvmCompilerInsertMIRAfter(invokeBB, newSetterMIR, invokeMIRSlow);
192 invokeMIRSlow->OptimizationFlags |= MIR_INLINED_PRED;
193#if defined(WITH_JIT_TUNING)
194 gDvmJit.invokePolySetterInlined++;
195#endif
196 } else {
Ben Cheng33c1cf92010-08-03 12:54:00 -0700197 /*
198 * The invoke becomes no-op so it needs an explicit branch to jump to
199 * the chaining cell.
200 */
201 invokeBB->needFallThroughBranch = true;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700202 invokeMIR->OptimizationFlags |= MIR_INLINED;
203#if defined(WITH_JIT_TUNING)
204 gDvmJit.invokeMonoSetterInlined++;
205#endif
206 }
207
208 return;
209}
210
211static void tryInlineSingletonCallsite(CompilationUnit *cUnit,
212 const Method *calleeMethod,
213 MIR *invokeMIR,
214 BasicBlock *invokeBB,
215 bool isRange)
216{
217 /* Not a Java method */
218 if (dvmIsNativeMethod(calleeMethod)) return;
219
220 CompilerMethodStats *methodStats =
221 dvmCompilerAnalyzeMethodBody(calleeMethod, true);
222
223 /* Empty callee - do nothing */
224 if (methodStats->attributes & METHOD_IS_EMPTY) {
225 /* The original invoke instruction is effectively turned into NOP */
226 invokeMIR->OptimizationFlags |= MIR_INLINED;
Ben Cheng23608ab2010-09-10 17:11:11 -0700227 /*
228 * Need to insert an explicit branch to catch the falling knife (into
229 * the PC reconstruction or chaining cell).
230 */
231 invokeBB->needFallThroughBranch = true;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700232 return;
233 }
234
235 if (methodStats->attributes & METHOD_IS_GETTER) {
236 inlineGetter(cUnit, calleeMethod, invokeMIR, invokeBB, false, isRange);
237 return;
238 } else if (methodStats->attributes & METHOD_IS_SETTER) {
239 inlineSetter(cUnit, calleeMethod, invokeMIR, invokeBB, false, isRange);
240 return;
241 }
242}
243
244static void inlineEmptyVirtualCallee(CompilationUnit *cUnit,
245 const Method *calleeMethod,
246 MIR *invokeMIR,
247 BasicBlock *invokeBB)
248{
249 MIR *invokeMIRSlow = dvmCompilerNew(sizeof(MIR), true);
250 *invokeMIRSlow = *invokeMIR;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800251 invokeMIR->dalvikInsn.opcode = kMirOpCheckInlinePrediction;
Ben Cheng7a2697d2010-06-07 13:44:23 -0700252
253 dvmCompilerInsertMIRAfter(invokeBB, invokeMIR, invokeMIRSlow);
254 invokeMIRSlow->OptimizationFlags |= MIR_INLINED_PRED;
255}
256
257static void tryInlineVirtualCallsite(CompilationUnit *cUnit,
258 const Method *calleeMethod,
259 MIR *invokeMIR,
260 BasicBlock *invokeBB,
261 bool isRange)
262{
263 /* Not a Java method */
264 if (dvmIsNativeMethod(calleeMethod)) return;
265
266 CompilerMethodStats *methodStats =
267 dvmCompilerAnalyzeMethodBody(calleeMethod, true);
268
269 /* Empty callee - do nothing by checking the clazz pointer */
270 if (methodStats->attributes & METHOD_IS_EMPTY) {
271 inlineEmptyVirtualCallee(cUnit, calleeMethod, invokeMIR, invokeBB);
272 return;
273 }
274
275 if (methodStats->attributes & METHOD_IS_GETTER) {
276 inlineGetter(cUnit, calleeMethod, invokeMIR, invokeBB, true, isRange);
277 return;
278 } else if (methodStats->attributes & METHOD_IS_SETTER) {
279 inlineSetter(cUnit, calleeMethod, invokeMIR, invokeBB, true, isRange);
280 return;
281 }
282}
283
284
285void dvmCompilerInlineMIR(CompilationUnit *cUnit)
286{
287 int i;
288 bool isRange = false;
289
290 /*
291 * Analyze the basic block containing an invoke to see if it can be inlined
292 */
293 for (i = 0; i < cUnit->numBlocks; i++) {
294 BasicBlock *bb = cUnit->blockList[i];
295 if (bb->blockType != kDalvikByteCode)
296 continue;
297 MIR *lastMIRInsn = bb->lastMIRInsn;
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800298 int opcode = lastMIRInsn->dalvikInsn.opcode;
Dan Bornsteine4852762010-12-02 12:45:00 -0800299 int flags = dexGetFlagsFromOpcode(opcode);
Ben Cheng7a2697d2010-06-07 13:44:23 -0700300
301 /* No invoke - continue */
302 if ((flags & kInstrInvoke) == 0)
303 continue;
304
Ben Cheng7eb3f7a2010-08-26 14:56:31 -0700305 /*
306 * If the invoke itself is selected for single stepping, don't bother
307 * to inline it.
308 */
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800309 if (SINGLE_STEP_OP(opcode))
Ben Cheng7eb3f7a2010-08-26 14:56:31 -0700310 continue;
311
Ben Cheng7a2697d2010-06-07 13:44:23 -0700312 const Method *calleeMethod;
313
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800314 switch (opcode) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700315 case OP_INVOKE_SUPER:
316 case OP_INVOKE_DIRECT:
317 case OP_INVOKE_STATIC:
318 case OP_INVOKE_SUPER_QUICK:
319 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
320 break;
321 case OP_INVOKE_SUPER_RANGE:
322 case OP_INVOKE_DIRECT_RANGE:
323 case OP_INVOKE_STATIC_RANGE:
324 case OP_INVOKE_SUPER_QUICK_RANGE:
325 isRange = true;
326 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
327 break;
328 default:
329 calleeMethod = NULL;
330 break;
331 }
332
333 if (calleeMethod) {
334 tryInlineSingletonCallsite(cUnit, calleeMethod, lastMIRInsn, bb,
335 isRange);
336 return;
337 }
338
Dan Bornstein9a1f8162010-12-01 17:02:26 -0800339 switch (opcode) {
Ben Cheng7a2697d2010-06-07 13:44:23 -0700340 case OP_INVOKE_VIRTUAL:
341 case OP_INVOKE_VIRTUAL_QUICK:
342 case OP_INVOKE_INTERFACE:
343 isRange = false;
344 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
345 break;
346 case OP_INVOKE_VIRTUAL_RANGE:
347 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
348 case OP_INVOKE_INTERFACE_RANGE:
349 isRange = true;
350 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
351 break;
352 default:
353 break;
354 }
355
356 if (calleeMethod) {
357 tryInlineVirtualCallsite(cUnit, calleeMethod, lastMIRInsn, bb,
358 isRange);
359 return;
360 }
361 }
362}