2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "libdex/DexOpcodes.h"
21 /* Convert the reg id from the callee to the original id passed by the caller */
22 static inline u4 convertRegId(const DecodedInstruction *invoke,
23 const Method *calleeMethod,
24 int calleeRegId, bool isRange)
26 /* The order in the original arg passing list */
27 int rank = calleeRegId -
28 (calleeMethod->registersSize - calleeMethod->insSize);
31 return invoke->arg[rank];
33 return invoke->vC + rank;
37 static bool inlineGetter(CompilationUnit *cUnit,
38 const Method *calleeMethod,
44 BasicBlock *moveResultBB = invokeBB->fallThrough;
45 MIR *moveResultMIR = moveResultBB->firstMIRInsn;
46 MIR *newGetterMIR = (MIR *)dvmCompilerNew(sizeof(MIR), true);
47 DecodedInstruction getterInsn;
49 dexDecodeInstruction(calleeMethod->insns, &getterInsn);
51 if (!dvmCompilerCanIncludeThisInstruction(calleeMethod, &getterInsn))
55 * Some getters (especially invoked through interface) are not followed
58 if ((moveResultMIR == NULL) ||
59 (moveResultMIR->dalvikInsn.opcode != OP_MOVE_RESULT &&
60 moveResultMIR->dalvikInsn.opcode != OP_MOVE_RESULT_OBJECT &&
61 moveResultMIR->dalvikInsn.opcode != OP_MOVE_RESULT_WIDE)) {
65 int dfFlags = dvmCompilerDataFlowAttributes[getterInsn.opcode];
67 /* Expecting vA to be the destination register */
68 if (dfFlags & (DF_UA | DF_UA_WIDE)) {
69 LOGE("opcode %d has DF_UA set (not expected)", getterInsn.opcode);
73 if (dfFlags & DF_UB) {
74 getterInsn.vB = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
75 getterInsn.vB, isRange);
78 if (dfFlags & DF_UC) {
79 getterInsn.vC = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
80 getterInsn.vC, isRange);
83 getterInsn.vA = moveResultMIR->dalvikInsn.vA;
85 /* Now setup the Dalvik instruction with converted src/dst registers */
86 newGetterMIR->dalvikInsn = getterInsn;
88 newGetterMIR->width = dexGetWidthFromOpcode(getterInsn.opcode);
90 newGetterMIR->OptimizationFlags |= MIR_CALLEE;
93 * If the getter instruction is about to raise any exception, punt to the
94 * interpreter and re-execute the invoke.
96 newGetterMIR->offset = invokeMIR->offset;
98 newGetterMIR->meta.calleeMethod = calleeMethod;
100 dvmCompilerInsertMIRAfter(invokeBB, invokeMIR, newGetterMIR);
103 MIR *invokeMIRSlow = (MIR *)dvmCompilerNew(sizeof(MIR), true);
104 *invokeMIRSlow = *invokeMIR;
105 invokeMIR->dalvikInsn.opcode = (Opcode)kMirOpCheckInlinePrediction;
107 /* Use vC to denote the first argument (ie this) */
109 invokeMIR->dalvikInsn.vC = invokeMIRSlow->dalvikInsn.arg[0];
112 moveResultMIR->OptimizationFlags |= MIR_INLINED_PRED;
114 dvmCompilerInsertMIRAfter(invokeBB, newGetterMIR, invokeMIRSlow);
115 invokeMIRSlow->OptimizationFlags |= MIR_INLINED_PRED;
116 #if defined(WITH_JIT_TUNING)
117 gDvmJit.invokePolyGetterInlined++;
120 invokeMIR->OptimizationFlags |= MIR_INLINED;
121 moveResultMIR->OptimizationFlags |= MIR_INLINED;
122 #if defined(WITH_JIT_TUNING)
123 gDvmJit.invokeMonoGetterInlined++;
130 static bool inlineSetter(CompilationUnit *cUnit,
131 const Method *calleeMethod,
133 BasicBlock *invokeBB,
137 MIR *newSetterMIR = (MIR *)dvmCompilerNew(sizeof(MIR), true);
138 DecodedInstruction setterInsn;
140 dexDecodeInstruction(calleeMethod->insns, &setterInsn);
142 if (!dvmCompilerCanIncludeThisInstruction(calleeMethod, &setterInsn))
145 int dfFlags = dvmCompilerDataFlowAttributes[setterInsn.opcode];
147 if (dfFlags & (DF_UA | DF_UA_WIDE)) {
148 setterInsn.vA = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
149 setterInsn.vA, isRange);
153 if (dfFlags & DF_UB) {
154 setterInsn.vB = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
155 setterInsn.vB, isRange);
159 if (dfFlags & DF_UC) {
160 setterInsn.vC = convertRegId(&invokeMIR->dalvikInsn, calleeMethod,
161 setterInsn.vC, isRange);
164 /* Now setup the Dalvik instruction with converted src/dst registers */
165 newSetterMIR->dalvikInsn = setterInsn;
167 newSetterMIR->width = dexGetWidthFromOpcode(setterInsn.opcode);
169 newSetterMIR->OptimizationFlags |= MIR_CALLEE;
172 * If the setter instruction is about to raise any exception, punt to the
173 * interpreter and re-execute the invoke.
175 newSetterMIR->offset = invokeMIR->offset;
177 newSetterMIR->meta.calleeMethod = calleeMethod;
179 dvmCompilerInsertMIRAfter(invokeBB, invokeMIR, newSetterMIR);
182 MIR *invokeMIRSlow = (MIR *)dvmCompilerNew(sizeof(MIR), true);
183 *invokeMIRSlow = *invokeMIR;
184 invokeMIR->dalvikInsn.opcode = (Opcode)kMirOpCheckInlinePrediction;
186 /* Use vC to denote the first argument (ie this) */
188 invokeMIR->dalvikInsn.vC = invokeMIRSlow->dalvikInsn.arg[0];
191 dvmCompilerInsertMIRAfter(invokeBB, newSetterMIR, invokeMIRSlow);
192 invokeMIRSlow->OptimizationFlags |= MIR_INLINED_PRED;
193 #if defined(WITH_JIT_TUNING)
194 gDvmJit.invokePolySetterInlined++;
198 * The invoke becomes no-op so it needs an explicit branch to jump to
201 invokeBB->needFallThroughBranch = true;
202 invokeMIR->OptimizationFlags |= MIR_INLINED;
203 #if defined(WITH_JIT_TUNING)
204 gDvmJit.invokeMonoSetterInlined++;
211 static bool tryInlineSingletonCallsite(CompilationUnit *cUnit,
212 const Method *calleeMethod,
214 BasicBlock *invokeBB,
217 /* Not a Java method */
218 if (dvmIsNativeMethod(calleeMethod)) return false;
220 CompilerMethodStats *methodStats =
221 dvmCompilerAnalyzeMethodBody(calleeMethod, true);
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;
228 * Need to insert an explicit branch to catch the falling knife (into
229 * the PC reconstruction or chaining cell).
231 invokeBB->needFallThroughBranch = true;
235 if (methodStats->attributes & METHOD_IS_GETTER) {
236 return inlineGetter(cUnit, calleeMethod, invokeMIR, invokeBB, false,
238 } else if (methodStats->attributes & METHOD_IS_SETTER) {
239 return inlineSetter(cUnit, calleeMethod, invokeMIR, invokeBB, false,
245 static bool inlineEmptyVirtualCallee(CompilationUnit *cUnit,
246 const Method *calleeMethod,
248 BasicBlock *invokeBB)
250 MIR *invokeMIRSlow = (MIR *)dvmCompilerNew(sizeof(MIR), true);
251 *invokeMIRSlow = *invokeMIR;
252 invokeMIR->dalvikInsn.opcode = (Opcode)kMirOpCheckInlinePrediction;
254 dvmCompilerInsertMIRAfter(invokeBB, invokeMIR, invokeMIRSlow);
255 invokeMIRSlow->OptimizationFlags |= MIR_INLINED_PRED;
259 static bool tryInlineVirtualCallsite(CompilationUnit *cUnit,
260 const Method *calleeMethod,
262 BasicBlock *invokeBB,
265 /* Not a Java method */
266 if (dvmIsNativeMethod(calleeMethod)) return false;
268 CompilerMethodStats *methodStats =
269 dvmCompilerAnalyzeMethodBody(calleeMethod, true);
271 /* Empty callee - do nothing by checking the clazz pointer */
272 if (methodStats->attributes & METHOD_IS_EMPTY) {
273 return inlineEmptyVirtualCallee(cUnit, calleeMethod, invokeMIR,
277 if (methodStats->attributes & METHOD_IS_GETTER) {
278 return inlineGetter(cUnit, calleeMethod, invokeMIR, invokeBB, true,
280 } else if (methodStats->attributes & METHOD_IS_SETTER) {
281 return inlineSetter(cUnit, calleeMethod, invokeMIR, invokeBB, true,
288 void dvmCompilerInlineMIR(CompilationUnit *cUnit, JitTranslationInfo *info)
290 bool isRange = false;
291 GrowableListIterator iterator;
293 dvmGrowableListIteratorInit(&cUnit->blockList, &iterator);
295 * Analyze the basic block containing an invoke to see if it can be inlined
298 BasicBlock *bb = (BasicBlock *) dvmGrowableListIteratorNext(&iterator);
299 if (bb == NULL) break;
300 if (bb->blockType != kDalvikByteCode)
302 MIR *lastMIRInsn = bb->lastMIRInsn;
303 Opcode opcode = lastMIRInsn->dalvikInsn.opcode;
304 int flags = (int)dexGetFlagsFromOpcode(opcode);
306 /* No invoke - continue */
307 if ((flags & kInstrInvoke) == 0)
310 /* Disable inlining when doing method tracing */
311 if (gDvmJit.methodTraceSupport)
315 * If the invoke itself is selected for single stepping, don't bother
318 if (SINGLE_STEP_OP(opcode))
321 const Method *calleeMethod;
324 case OP_INVOKE_SUPER:
325 case OP_INVOKE_DIRECT:
326 case OP_INVOKE_STATIC:
327 case OP_INVOKE_SUPER_QUICK:
328 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
330 case OP_INVOKE_SUPER_RANGE:
331 case OP_INVOKE_DIRECT_RANGE:
332 case OP_INVOKE_STATIC_RANGE:
333 case OP_INVOKE_SUPER_QUICK_RANGE:
335 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
343 bool inlined = tryInlineSingletonCallsite(cUnit, calleeMethod,
344 lastMIRInsn, bb, isRange);
346 !(gDvmJit.disableOpt & (1 << kMethodJit)) &&
347 !dvmIsNativeMethod(calleeMethod)) {
348 CompilerMethodStats *methodStats =
349 dvmCompilerAnalyzeMethodBody(calleeMethod, true);
350 if ((methodStats->attributes & METHOD_IS_LEAF) &&
351 !(methodStats->attributes & METHOD_CANNOT_COMPILE)) {
352 /* Callee has been previously compiled */
353 if (dvmJitGetMethodAddr(calleeMethod->insns)) {
354 lastMIRInsn->OptimizationFlags |= MIR_INVOKE_METHOD_JIT;
356 /* Compile the callee first */
357 dvmCompileMethod(calleeMethod, info);
358 if (dvmJitGetMethodAddr(calleeMethod->insns)) {
359 lastMIRInsn->OptimizationFlags |=
360 MIR_INVOKE_METHOD_JIT;
362 methodStats->attributes |= METHOD_CANNOT_COMPILE;
371 case OP_INVOKE_VIRTUAL:
372 case OP_INVOKE_VIRTUAL_QUICK:
373 case OP_INVOKE_INTERFACE:
375 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
377 case OP_INVOKE_VIRTUAL_RANGE:
378 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
379 case OP_INVOKE_INTERFACE_RANGE:
381 calleeMethod = lastMIRInsn->meta.callsiteInfo->method;
388 bool inlined = tryInlineVirtualCallsite(cUnit, calleeMethod,
389 lastMIRInsn, bb, isRange);
391 !(gDvmJit.disableOpt & (1 << kMethodJit)) &&
392 !dvmIsNativeMethod(calleeMethod)) {
393 CompilerMethodStats *methodStats =
394 dvmCompilerAnalyzeMethodBody(calleeMethod, true);
395 if ((methodStats->attributes & METHOD_IS_LEAF) &&
396 !(methodStats->attributes & METHOD_CANNOT_COMPILE)) {
397 /* Callee has been previously compiled */
398 if (dvmJitGetMethodAddr(calleeMethod->insns)) {
399 lastMIRInsn->OptimizationFlags |= MIR_INVOKE_METHOD_JIT;
401 /* Compile the callee first */
402 dvmCompileMethod(calleeMethod, info);
403 if (dvmJitGetMethodAddr(calleeMethod->insns)) {
404 lastMIRInsn->OptimizationFlags |=
405 MIR_INVOKE_METHOD_JIT;
407 methodStats->attributes |= METHOD_CANNOT_COMPILE;