Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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" |
Dan Bornstein | df4daaf | 2010-12-01 14:23:44 -0800 | [diff] [blame] | 18 | #include "libdex/DexOpcodes.h" |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 19 | #include "interp/Jit.h" |
| 20 | #include "CompilerInternals.h" |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 21 | #include "Dataflow.h" |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Parse an instruction, return the length of the instruction |
| 25 | */ |
| 26 | static inline int parseInsn(const u2 *codePtr, DecodedInstruction *decInsn, |
| 27 | bool printMe) |
| 28 | { |
| 29 | u2 instr = *codePtr; |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 30 | Opcode opcode = dexOpcodeFromCodeUnit(instr); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 31 | int insnWidth; |
| 32 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 33 | // Don't parse instruction data |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 34 | if (opcode == OP_NOP && instr != 0) { |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 35 | return 0; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 36 | } else { |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 37 | insnWidth = dexGetWidthFromOpcode(opcode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 38 | if (insnWidth < 0) { |
| 39 | insnWidth = -insnWidth; |
| 40 | } |
| 41 | } |
| 42 | |
Dan Bornstein | 5432239 | 2010-11-17 14:16:56 -0800 | [diff] [blame] | 43 | dexDecodeInstruction(codePtr, decInsn); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 44 | if (printMe) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 45 | char *decodedString = dvmCompilerGetDalvikDisassembly(decInsn, NULL); |
Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 46 | LOGD("%p: %#06x %s\n", codePtr, opcode, decodedString); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 47 | } |
| 48 | return insnWidth; |
| 49 | } |
| 50 | |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 51 | #define UNKNOWN_TARGET 0xffffffff |
| 52 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Identify block-ending instructions and collect supplemental information |
| 55 | * regarding the following instructions. |
| 56 | */ |
| 57 | static inline bool findBlockBoundary(const Method *caller, MIR *insn, |
| 58 | unsigned int curOffset, |
| 59 | unsigned int *target, bool *isInvoke, |
| 60 | const Method **callee) |
| 61 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 62 | switch (insn->dalvikInsn.opcode) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 63 | /* Target is not compile-time constant */ |
| 64 | case OP_RETURN_VOID: |
| 65 | case OP_RETURN: |
| 66 | case OP_RETURN_WIDE: |
| 67 | case OP_RETURN_OBJECT: |
| 68 | case OP_THROW: |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 69 | *target = UNKNOWN_TARGET; |
| 70 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 71 | case OP_INVOKE_VIRTUAL: |
| 72 | case OP_INVOKE_VIRTUAL_RANGE: |
| 73 | case OP_INVOKE_INTERFACE: |
| 74 | case OP_INVOKE_INTERFACE_RANGE: |
| 75 | case OP_INVOKE_VIRTUAL_QUICK: |
| 76 | case OP_INVOKE_VIRTUAL_QUICK_RANGE: |
| 77 | *isInvoke = true; |
| 78 | break; |
| 79 | case OP_INVOKE_SUPER: |
| 80 | case OP_INVOKE_SUPER_RANGE: { |
| 81 | int mIndex = caller->clazz->pDvmDex-> |
| 82 | pResMethods[insn->dalvikInsn.vB]->methodIndex; |
| 83 | const Method *calleeMethod = |
| 84 | caller->clazz->super->vtable[mIndex]; |
| 85 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 86 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 87 | *target = (unsigned int) calleeMethod->insns; |
| 88 | } |
| 89 | *isInvoke = true; |
| 90 | *callee = calleeMethod; |
| 91 | break; |
| 92 | } |
| 93 | case OP_INVOKE_STATIC: |
| 94 | case OP_INVOKE_STATIC_RANGE: { |
| 95 | const Method *calleeMethod = |
| 96 | caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB]; |
| 97 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 98 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 99 | *target = (unsigned int) calleeMethod->insns; |
| 100 | } |
| 101 | *isInvoke = true; |
| 102 | *callee = calleeMethod; |
| 103 | break; |
| 104 | } |
| 105 | case OP_INVOKE_SUPER_QUICK: |
| 106 | case OP_INVOKE_SUPER_QUICK_RANGE: { |
| 107 | const Method *calleeMethod = |
| 108 | caller->clazz->super->vtable[insn->dalvikInsn.vB]; |
| 109 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 110 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 111 | *target = (unsigned int) calleeMethod->insns; |
| 112 | } |
| 113 | *isInvoke = true; |
| 114 | *callee = calleeMethod; |
| 115 | break; |
| 116 | } |
| 117 | case OP_INVOKE_DIRECT: |
| 118 | case OP_INVOKE_DIRECT_RANGE: { |
| 119 | const Method *calleeMethod = |
| 120 | caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB]; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 121 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 122 | *target = (unsigned int) calleeMethod->insns; |
| 123 | } |
| 124 | *isInvoke = true; |
| 125 | *callee = calleeMethod; |
| 126 | break; |
| 127 | } |
| 128 | case OP_GOTO: |
| 129 | case OP_GOTO_16: |
| 130 | case OP_GOTO_32: |
| 131 | *target = curOffset + (int) insn->dalvikInsn.vA; |
| 132 | break; |
| 133 | |
| 134 | case OP_IF_EQ: |
| 135 | case OP_IF_NE: |
| 136 | case OP_IF_LT: |
| 137 | case OP_IF_GE: |
| 138 | case OP_IF_GT: |
| 139 | case OP_IF_LE: |
| 140 | *target = curOffset + (int) insn->dalvikInsn.vC; |
| 141 | break; |
| 142 | |
| 143 | case OP_IF_EQZ: |
| 144 | case OP_IF_NEZ: |
| 145 | case OP_IF_LTZ: |
| 146 | case OP_IF_GEZ: |
| 147 | case OP_IF_GTZ: |
| 148 | case OP_IF_LEZ: |
| 149 | *target = curOffset + (int) insn->dalvikInsn.vB; |
| 150 | break; |
| 151 | |
| 152 | default: |
| 153 | return false; |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 154 | } |
| 155 | return true; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 158 | static inline bool isGoto(MIR *insn) |
| 159 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 160 | switch (insn->dalvikInsn.opcode) { |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 161 | case OP_GOTO: |
| 162 | case OP_GOTO_16: |
| 163 | case OP_GOTO_32: |
| 164 | return true; |
| 165 | default: |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 170 | /* |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 171 | * Identify unconditional branch instructions |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 172 | */ |
| 173 | static inline bool isUnconditionalBranch(MIR *insn) |
| 174 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 175 | switch (insn->dalvikInsn.opcode) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 176 | case OP_RETURN_VOID: |
| 177 | case OP_RETURN: |
| 178 | case OP_RETURN_WIDE: |
| 179 | case OP_RETURN_OBJECT: |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 180 | return true; |
| 181 | default: |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 182 | return isGoto(insn); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | /* |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 187 | * dvmHashTableLookup() callback |
| 188 | */ |
| 189 | static int compareMethod(const CompilerMethodStats *m1, |
| 190 | const CompilerMethodStats *m2) |
| 191 | { |
| 192 | return (int) m1->method - (int) m2->method; |
| 193 | } |
| 194 | |
| 195 | /* |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 196 | * Analyze the body of the method to collect high-level information regarding |
| 197 | * inlining: |
| 198 | * - is empty method? |
| 199 | * - is getter/setter? |
| 200 | * - can throw exception? |
| 201 | * |
| 202 | * Currently the inliner only handles getters and setters. When its capability |
| 203 | * becomes more sophisticated more information will be retrieved here. |
| 204 | */ |
| 205 | static int analyzeInlineTarget(DecodedInstruction *dalvikInsn, int attributes, |
| 206 | int offset) |
| 207 | { |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 208 | int flags = dexGetFlagsFromOpcode(dalvikInsn->opcode); |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 209 | int dalvikOpcode = dalvikInsn->opcode; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 210 | |
Dan Bornstein | 0759f52 | 2010-11-30 14:58:53 -0800 | [diff] [blame] | 211 | if (flags & kInstrInvoke) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 212 | attributes &= ~METHOD_IS_LEAF; |
| 213 | } |
| 214 | |
| 215 | if (!(flags & kInstrCanReturn)) { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 216 | if (!(dvmCompilerDataFlowAttributes[dalvikOpcode] & |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 217 | DF_IS_GETTER)) { |
| 218 | attributes &= ~METHOD_IS_GETTER; |
| 219 | } |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 220 | if (!(dvmCompilerDataFlowAttributes[dalvikOpcode] & |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 221 | DF_IS_SETTER)) { |
| 222 | attributes &= ~METHOD_IS_SETTER; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * The expected instruction sequence is setter will never return value and |
| 228 | * getter will also do. Clear the bits if the behavior is discovered |
| 229 | * otherwise. |
| 230 | */ |
| 231 | if (flags & kInstrCanReturn) { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 232 | if (dalvikOpcode == OP_RETURN_VOID) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 233 | attributes &= ~METHOD_IS_GETTER; |
| 234 | } |
| 235 | else { |
| 236 | attributes &= ~METHOD_IS_SETTER; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (flags & kInstrCanThrow) { |
| 241 | attributes &= ~METHOD_IS_THROW_FREE; |
| 242 | } |
| 243 | |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 244 | if (offset == 0 && dalvikOpcode == OP_RETURN_VOID) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 245 | attributes |= METHOD_IS_EMPTY; |
| 246 | } |
| 247 | |
Ben Cheng | 34dc796 | 2010-08-26 14:56:31 -0700 | [diff] [blame] | 248 | /* |
| 249 | * Check if this opcode is selected for single stepping. |
| 250 | * If so, don't inline the callee as there is no stack frame for the |
| 251 | * interpreter to single-step through the instruction. |
| 252 | */ |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 253 | if (SINGLE_STEP_OP(dalvikOpcode)) { |
Ben Cheng | 34dc796 | 2010-08-26 14:56:31 -0700 | [diff] [blame] | 254 | attributes &= ~(METHOD_IS_GETTER | METHOD_IS_SETTER); |
| 255 | } |
| 256 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 257 | return attributes; |
| 258 | } |
| 259 | |
| 260 | /* |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 261 | * Analyze each method whose traces are ever compiled. Collect a variety of |
| 262 | * statistics like the ratio of exercised vs overall code and code bloat |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 263 | * ratios. If isCallee is true, also analyze each instruction in more details |
| 264 | * to see if it is suitable for inlining. |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 265 | */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 266 | CompilerMethodStats *dvmCompilerAnalyzeMethodBody(const Method *method, |
| 267 | bool isCallee) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 268 | { |
| 269 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 270 | const u2 *codePtr = dexCode->insns; |
| 271 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
| 272 | int insnSize = 0; |
| 273 | int hashValue = dvmComputeUtf8Hash(method->name); |
| 274 | |
| 275 | CompilerMethodStats dummyMethodEntry; // For hash table lookup |
| 276 | CompilerMethodStats *realMethodEntry; // For hash table storage |
| 277 | |
| 278 | /* For lookup only */ |
| 279 | dummyMethodEntry.method = method; |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 280 | realMethodEntry = |
| 281 | (CompilerMethodStats *) dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue, |
| 282 | &dummyMethodEntry, |
| 283 | (HashCompareFunc) compareMethod, |
| 284 | false); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 285 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 286 | /* This method has never been analyzed before - create an entry */ |
| 287 | if (realMethodEntry == NULL) { |
| 288 | realMethodEntry = |
| 289 | (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats)); |
| 290 | realMethodEntry->method = method; |
| 291 | |
| 292 | dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue, |
| 293 | realMethodEntry, |
| 294 | (HashCompareFunc) compareMethod, |
| 295 | true); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 298 | /* This method is invoked as a callee and has been analyzed - just return */ |
| 299 | if ((isCallee == true) && (realMethodEntry->attributes & METHOD_IS_CALLEE)) |
| 300 | return realMethodEntry; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 301 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 302 | /* |
| 303 | * Similarly, return if this method has been compiled before as a hot |
| 304 | * method already. |
| 305 | */ |
| 306 | if ((isCallee == false) && |
| 307 | (realMethodEntry->attributes & METHOD_IS_HOT)) |
| 308 | return realMethodEntry; |
| 309 | |
| 310 | int attributes; |
| 311 | |
| 312 | /* Method hasn't been analyzed for the desired purpose yet */ |
| 313 | if (isCallee) { |
| 314 | /* Aggressively set the attributes until proven otherwise */ |
| 315 | attributes = METHOD_IS_LEAF | METHOD_IS_THROW_FREE | METHOD_IS_CALLEE | |
| 316 | METHOD_IS_GETTER | METHOD_IS_SETTER; |
| 317 | } else { |
| 318 | attributes = METHOD_IS_HOT; |
| 319 | } |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 320 | |
| 321 | /* Count the number of instructions */ |
| 322 | while (codePtr < codeEnd) { |
| 323 | DecodedInstruction dalvikInsn; |
| 324 | int width = parseInsn(codePtr, &dalvikInsn, false); |
| 325 | |
| 326 | /* Terminate when the data section is seen */ |
| 327 | if (width == 0) |
| 328 | break; |
| 329 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 330 | if (isCallee) { |
| 331 | attributes = analyzeInlineTarget(&dalvikInsn, attributes, insnSize); |
| 332 | } |
| 333 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 334 | insnSize += width; |
| 335 | codePtr += width; |
| 336 | } |
| 337 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 338 | /* |
| 339 | * Only handle simple getters/setters with one instruction followed by |
| 340 | * return |
| 341 | */ |
| 342 | if ((attributes & (METHOD_IS_GETTER | METHOD_IS_SETTER)) && |
| 343 | (insnSize != 3)) { |
| 344 | attributes &= ~(METHOD_IS_GETTER | METHOD_IS_SETTER); |
| 345 | } |
| 346 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 347 | realMethodEntry->dalvikSize = insnSize * 2; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 348 | realMethodEntry->attributes |= attributes; |
| 349 | |
| 350 | #if 0 |
| 351 | /* Uncomment the following to explore various callee patterns */ |
| 352 | if (attributes & METHOD_IS_THROW_FREE) { |
| 353 | LOGE("%s%s is inlinable%s", method->clazz->descriptor, method->name, |
| 354 | (attributes & METHOD_IS_EMPTY) ? " empty" : ""); |
| 355 | } |
| 356 | |
| 357 | if (attributes & METHOD_IS_LEAF) { |
| 358 | LOGE("%s%s is leaf %d%s", method->clazz->descriptor, method->name, |
| 359 | insnSize, insnSize < 5 ? " (small)" : ""); |
| 360 | } |
| 361 | |
| 362 | if (attributes & (METHOD_IS_GETTER | METHOD_IS_SETTER)) { |
| 363 | LOGE("%s%s is %s", method->clazz->descriptor, method->name, |
| 364 | attributes & METHOD_IS_GETTER ? "getter": "setter"); |
| 365 | } |
| 366 | if (attributes == |
| 367 | (METHOD_IS_LEAF | METHOD_IS_THROW_FREE | METHOD_IS_CALLEE)) { |
| 368 | LOGE("%s%s is inlinable non setter/getter", method->clazz->descriptor, |
| 369 | method->name); |
| 370 | } |
| 371 | #endif |
| 372 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 373 | return realMethodEntry; |
| 374 | } |
| 375 | |
| 376 | /* |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 377 | * Crawl the stack of the thread that requesed compilation to see if any of the |
| 378 | * ancestors are on the blacklist. |
| 379 | */ |
Andy McFadden | 953a0ed | 2010-09-17 15:48:38 -0700 | [diff] [blame] | 380 | static bool filterMethodByCallGraph(Thread *thread, const char *curMethodName) |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 381 | { |
| 382 | /* Crawl the Dalvik stack frames and compare the method name*/ |
| 383 | StackSaveArea *ssaPtr = ((StackSaveArea *) thread->curFrame) - 1; |
| 384 | while (ssaPtr != ((StackSaveArea *) NULL) - 1) { |
| 385 | const Method *method = ssaPtr->method; |
| 386 | if (method) { |
| 387 | int hashValue = dvmComputeUtf8Hash(method->name); |
| 388 | bool found = |
| 389 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 390 | (char *) method->name, |
| 391 | (HashCompareFunc) strcmp, false) != |
| 392 | NULL; |
| 393 | if (found) { |
| 394 | LOGD("Method %s (--> %s) found on the JIT %s list", |
| 395 | method->name, curMethodName, |
| 396 | gDvmJit.includeSelectedMethod ? "white" : "black"); |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | } |
| 401 | ssaPtr = ((StackSaveArea *) ssaPtr->prevFrame) - 1; |
| 402 | }; |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | /* |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 407 | * Main entry point to start trace compilation. Basic blocks are constructed |
| 408 | * first and they will be passed to the codegen routines to convert Dalvik |
| 409 | * bytecode into machine code. |
| 410 | */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 411 | bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts, |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 412 | JitTranslationInfo *info, jmp_buf *bailPtr, |
| 413 | int optHints) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 414 | { |
| 415 | const DexCode *dexCode = dvmGetMethodCode(desc->method); |
| 416 | const JitTraceRun* currRun = &desc->trace[0]; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 417 | unsigned int curOffset = currRun->frag.startOffset; |
| 418 | unsigned int numInsts = currRun->frag.numInsts; |
| 419 | const u2 *codePtr = dexCode->insns + curOffset; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 420 | int traceSize = 0; // # of half-words |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 421 | const u2 *startCodePtr = codePtr; |
| 422 | BasicBlock *startBB, *curBB, *lastBB; |
| 423 | int numBlocks = 0; |
| 424 | static int compilationId; |
| 425 | CompilationUnit cUnit; |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 426 | #if defined(WITH_JIT_TUNING) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 427 | CompilerMethodStats *methodStats; |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 428 | #endif |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 429 | |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 430 | /* If we've already compiled this trace, just return success */ |
jeffhao | 9e45c0b | 2010-02-03 10:24:05 -0800 | [diff] [blame] | 431 | if (dvmJitGetCodeAddr(startCodePtr) && !info->discardResult) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 432 | /* |
| 433 | * Make sure the codeAddress is NULL so that it won't clobber the |
| 434 | * existing entry. |
| 435 | */ |
| 436 | info->codeAddress = NULL; |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 440 | compilationId++; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 441 | memset(&cUnit, 0, sizeof(CompilationUnit)); |
| 442 | |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 443 | #if defined(WITH_JIT_TUNING) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 444 | /* Locate the entry to store compilation statistics for this method */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 445 | methodStats = dvmCompilerAnalyzeMethodBody(desc->method, false); |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 446 | #endif |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 447 | |
Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 448 | /* Set the recover buffer pointer */ |
| 449 | cUnit.bailPtr = bailPtr; |
| 450 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 451 | /* Initialize the printMe flag */ |
| 452 | cUnit.printMe = gDvmJit.printMe; |
| 453 | |
Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 454 | /* Initialize the profile flag */ |
| 455 | cUnit.executionCount = gDvmJit.profile; |
| 456 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 457 | /* Setup the method */ |
| 458 | cUnit.method = desc->method; |
| 459 | |
| 460 | /* Initialize the PC reconstruction list */ |
| 461 | dvmInitGrowableList(&cUnit.pcReconstructionList, 8); |
| 462 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 463 | /* Identify traces that we don't want to compile */ |
| 464 | if (gDvmJit.methodTable) { |
| 465 | int len = strlen(desc->method->clazz->descriptor) + |
| 466 | strlen(desc->method->name) + 1; |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 467 | char *fullSignature = (char *)dvmCompilerNew(len, true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 468 | strcpy(fullSignature, desc->method->clazz->descriptor); |
| 469 | strcat(fullSignature, desc->method->name); |
| 470 | |
| 471 | int hashValue = dvmComputeUtf8Hash(fullSignature); |
| 472 | |
| 473 | /* |
| 474 | * Doing three levels of screening to see whether we want to skip |
| 475 | * compiling this method |
| 476 | */ |
| 477 | |
| 478 | /* First, check the full "class;method" signature */ |
| 479 | bool methodFound = |
| 480 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 481 | fullSignature, (HashCompareFunc) strcmp, |
| 482 | false) != |
| 483 | NULL; |
| 484 | |
| 485 | /* Full signature not found - check the enclosing class */ |
| 486 | if (methodFound == false) { |
| 487 | int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor); |
| 488 | methodFound = |
| 489 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 490 | (char *) desc->method->clazz->descriptor, |
| 491 | (HashCompareFunc) strcmp, false) != |
| 492 | NULL; |
| 493 | /* Enclosing class not found - check the method name */ |
| 494 | if (methodFound == false) { |
| 495 | int hashValue = dvmComputeUtf8Hash(desc->method->name); |
| 496 | methodFound = |
| 497 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 498 | (char *) desc->method->name, |
| 499 | (HashCompareFunc) strcmp, false) != |
| 500 | NULL; |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * Debug by call-graph is enabled. Check if the debug list |
| 504 | * covers any methods on the VM stack. |
| 505 | */ |
| 506 | if (methodFound == false && gDvmJit.checkCallGraph == true) { |
| 507 | methodFound = |
| 508 | filterMethodByCallGraph(info->requestingThread, |
| 509 | desc->method->name); |
| 510 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Under the following conditions, the trace will be *conservatively* |
| 516 | * compiled by only containing single-step instructions to and from the |
| 517 | * interpreter. |
| 518 | * 1) If includeSelectedMethod == false, the method matches the full or |
| 519 | * partial signature stored in the hash table. |
| 520 | * |
| 521 | * 2) If includeSelectedMethod == true, the method does not match the |
| 522 | * full and partial signature stored in the hash table. |
| 523 | */ |
| 524 | if (gDvmJit.includeSelectedMethod != methodFound) { |
| 525 | cUnit.allSingleStep = true; |
| 526 | } else { |
| 527 | /* Compile the trace as normal */ |
| 528 | |
| 529 | /* Print the method we cherry picked */ |
| 530 | if (gDvmJit.includeSelectedMethod == true) { |
| 531 | cUnit.printMe = true; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 536 | /* Allocate the entry block */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 537 | lastBB = startBB = curBB = dvmCompilerNewBB(kTraceEntryBlock); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 538 | curBB->startOffset = curOffset; |
| 539 | curBB->id = numBlocks++; |
| 540 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 541 | curBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 542 | curBB->startOffset = curOffset; |
| 543 | curBB->id = numBlocks++; |
| 544 | |
| 545 | /* Make the first real dalvik block the fallthrough of the entry block */ |
| 546 | startBB->fallThrough = curBB; |
| 547 | lastBB->next = curBB; |
| 548 | lastBB = curBB; |
| 549 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 550 | if (cUnit.printMe) { |
| 551 | LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n", |
| 552 | desc->method->name, curOffset); |
| 553 | } |
| 554 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 555 | /* |
| 556 | * Analyze the trace descriptor and include up to the maximal number |
| 557 | * of Dalvik instructions into the IR. |
| 558 | */ |
| 559 | while (1) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 560 | MIR *insn; |
| 561 | int width; |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 562 | insn = (MIR *)dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 563 | insn->offset = curOffset; |
| 564 | width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 565 | |
| 566 | /* The trace should never incude instruction data */ |
| 567 | assert(width); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 568 | insn->width = width; |
| 569 | traceSize += width; |
| 570 | dvmCompilerAppendMIR(curBB, insn); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 571 | cUnit.numInsts++; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 572 | |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 573 | int flags = dexGetFlagsFromOpcode(insn->dalvikInsn.opcode); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 574 | |
Dan Bornstein | 0759f52 | 2010-11-30 14:58:53 -0800 | [diff] [blame] | 575 | if (flags & kInstrInvoke) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 576 | assert(numInsts == 1); |
| 577 | CallsiteInfo *callsiteInfo = |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 578 | (CallsiteInfo *)dvmCompilerNew(sizeof(CallsiteInfo), true); |
| 579 | callsiteInfo->clazz = (ClassObject *)currRun[1].meta; |
| 580 | callsiteInfo->method = (Method *)currRun[2].meta; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 581 | insn->meta.callsiteInfo = callsiteInfo; |
| 582 | } |
| 583 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 584 | /* Instruction limit reached - terminate the trace here */ |
| 585 | if (cUnit.numInsts >= numMaxInsts) { |
| 586 | break; |
| 587 | } |
| 588 | if (--numInsts == 0) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 589 | if (currRun->frag.runEnd) { |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 590 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 591 | } else { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 592 | /* Advance to the next trace description (ie non-meta info) */ |
| 593 | do { |
| 594 | currRun++; |
| 595 | } while (!currRun->frag.isCode); |
| 596 | |
| 597 | /* Dummy end-of-run marker seen */ |
| 598 | if (currRun->frag.numInsts == 0) { |
| 599 | break; |
| 600 | } |
| 601 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 602 | curBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 603 | lastBB->next = curBB; |
| 604 | lastBB = curBB; |
| 605 | curBB->id = numBlocks++; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 606 | curOffset = currRun->frag.startOffset; |
| 607 | numInsts = currRun->frag.numInsts; |
| 608 | curBB->startOffset = curOffset; |
| 609 | codePtr = dexCode->insns + curOffset; |
| 610 | } |
| 611 | } else { |
| 612 | curOffset += width; |
| 613 | codePtr += width; |
| 614 | } |
| 615 | } |
| 616 | |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 617 | #if defined(WITH_JIT_TUNING) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 618 | /* Convert # of half-word to bytes */ |
| 619 | methodStats->compiledDalvikSize += traceSize * 2; |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 620 | #endif |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 621 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 622 | /* |
| 623 | * Now scan basic blocks containing real code to connect the |
| 624 | * taken/fallthrough links. Also create chaining cells for code not included |
| 625 | * in the trace. |
| 626 | */ |
| 627 | for (curBB = startBB; curBB; curBB = curBB->next) { |
| 628 | MIR *lastInsn = curBB->lastMIRInsn; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 629 | /* Skip empty blocks */ |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 630 | if (lastInsn == NULL) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 631 | continue; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 632 | } |
| 633 | curOffset = lastInsn->offset; |
| 634 | unsigned int targetOffset = curOffset; |
| 635 | unsigned int fallThroughOffset = curOffset + lastInsn->width; |
| 636 | bool isInvoke = false; |
| 637 | const Method *callee = NULL; |
| 638 | |
| 639 | findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset, |
| 640 | &targetOffset, &isInvoke, &callee); |
| 641 | |
| 642 | /* Link the taken and fallthrough blocks */ |
| 643 | BasicBlock *searchBB; |
| 644 | |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 645 | int flags = dexGetFlagsFromOpcode(lastInsn->dalvikInsn.opcode); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 646 | |
| 647 | if (flags & kInstrInvoke) { |
| 648 | cUnit.hasInvoke = true; |
| 649 | } |
| 650 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 651 | /* No backward branch in the trace - start searching the next BB */ |
| 652 | for (searchBB = curBB->next; searchBB; searchBB = searchBB->next) { |
| 653 | if (targetOffset == searchBB->startOffset) { |
| 654 | curBB->taken = searchBB; |
| 655 | } |
| 656 | if (fallThroughOffset == searchBB->startOffset) { |
| 657 | curBB->fallThrough = searchBB; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 658 | |
| 659 | /* |
| 660 | * Fallthrough block of an invoke instruction needs to be |
| 661 | * aligned to 4-byte boundary (alignment instruction to be |
| 662 | * inserted later. |
| 663 | */ |
| 664 | if (flags & kInstrInvoke) { |
| 665 | searchBB->isFallThroughFromInvoke = true; |
| 666 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 670 | /* |
| 671 | * Some blocks are ended by non-control-flow-change instructions, |
| 672 | * currently only due to trace length constraint. In this case we need |
| 673 | * to generate an explicit branch at the end of the block to jump to |
| 674 | * the chaining cell. |
| 675 | */ |
| 676 | curBB->needFallThroughBranch = |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 677 | ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn | |
Dan Bornstein | 0759f52 | 2010-11-30 14:58:53 -0800 | [diff] [blame] | 678 | kInstrInvoke)) == 0); |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 679 | |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 680 | /* Only form a loop if JIT_OPT_NO_LOOP is not set */ |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 681 | if (curBB->taken == NULL && |
| 682 | curBB->fallThrough == NULL && |
| 683 | flags == (kInstrCanBranch | kInstrCanContinue) && |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 684 | fallThroughOffset == startBB->startOffset && |
| 685 | JIT_OPT_NO_LOOP != (optHints & JIT_OPT_NO_LOOP)) { |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 686 | BasicBlock *loopBranch = curBB; |
| 687 | BasicBlock *exitBB; |
| 688 | BasicBlock *exitChainingCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 689 | |
| 690 | if (cUnit.printMe) { |
| 691 | LOGD("Natural loop detected!"); |
| 692 | } |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 693 | exitBB = dvmCompilerNewBB(kTraceExitBlock); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 694 | lastBB->next = exitBB; |
| 695 | lastBB = exitBB; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 696 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 697 | exitBB->startOffset = targetOffset; |
| 698 | exitBB->id = numBlocks++; |
| 699 | exitBB->needFallThroughBranch = true; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 700 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 701 | loopBranch->taken = exitBB; |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 702 | #if defined(WITH_SELF_VERIFICATION) |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 703 | BasicBlock *backwardCell = |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 704 | dvmCompilerNewBB(kChainingCellBackwardBranch); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 705 | lastBB->next = backwardCell; |
| 706 | lastBB = backwardCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 707 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 708 | backwardCell->startOffset = startBB->startOffset; |
| 709 | backwardCell->id = numBlocks++; |
| 710 | loopBranch->fallThrough = backwardCell; |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 711 | #elif defined(WITH_JIT_TUNING) |
| 712 | if (gDvmJit.profile) { |
| 713 | BasicBlock *backwardCell = |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 714 | dvmCompilerNewBB(kChainingCellBackwardBranch); |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 715 | lastBB->next = backwardCell; |
| 716 | lastBB = backwardCell; |
| 717 | |
| 718 | backwardCell->startOffset = startBB->startOffset; |
| 719 | backwardCell->id = numBlocks++; |
| 720 | loopBranch->fallThrough = backwardCell; |
| 721 | } else { |
| 722 | loopBranch->fallThrough = startBB->next; |
| 723 | } |
| 724 | #else |
| 725 | loopBranch->fallThrough = startBB->next; |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 726 | #endif |
| 727 | |
| 728 | /* Create the chaining cell as the fallthrough of the exit block */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 729 | exitChainingCell = dvmCompilerNewBB(kChainingCellNormal); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 730 | lastBB->next = exitChainingCell; |
| 731 | lastBB = exitChainingCell; |
| 732 | |
| 733 | exitChainingCell->startOffset = targetOffset; |
| 734 | exitChainingCell->id = numBlocks++; |
| 735 | |
| 736 | exitBB->fallThrough = exitChainingCell; |
| 737 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 738 | cUnit.hasLoop = true; |
| 739 | } |
| 740 | |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 741 | if (lastInsn->dalvikInsn.opcode == OP_PACKED_SWITCH || |
| 742 | lastInsn->dalvikInsn.opcode == OP_SPARSE_SWITCH) { |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 743 | int i; |
| 744 | const u2 *switchData = desc->method->insns + lastInsn->offset + |
| 745 | lastInsn->dalvikInsn.vB; |
| 746 | int size = switchData[1]; |
| 747 | int maxChains = MIN(size, MAX_CHAINED_SWITCH_CASES); |
| 748 | |
| 749 | /* |
| 750 | * Generate the landing pad for cases whose ranks are higher than |
| 751 | * MAX_CHAINED_SWITCH_CASES. The code will re-enter the interpreter |
| 752 | * through the NoChain point. |
| 753 | */ |
| 754 | if (maxChains != size) { |
| 755 | cUnit.switchOverflowPad = |
| 756 | desc->method->insns + lastInsn->offset; |
| 757 | } |
| 758 | |
| 759 | s4 *targets = (s4 *) (switchData + 2 + |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 760 | (lastInsn->dalvikInsn.opcode == OP_PACKED_SWITCH ? |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 761 | 2 : size * 2)); |
| 762 | |
| 763 | /* One chaining cell for the first MAX_CHAINED_SWITCH_CASES cases */ |
| 764 | for (i = 0; i < maxChains; i++) { |
| 765 | BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal); |
| 766 | lastBB->next = caseChain; |
| 767 | lastBB = caseChain; |
| 768 | |
| 769 | caseChain->startOffset = lastInsn->offset + targets[i]; |
| 770 | caseChain->id = numBlocks++; |
| 771 | } |
| 772 | |
| 773 | /* One more chaining cell for the default case */ |
| 774 | BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal); |
| 775 | lastBB->next = caseChain; |
| 776 | lastBB = caseChain; |
| 777 | |
| 778 | caseChain->startOffset = lastInsn->offset + lastInsn->width; |
| 779 | caseChain->id = numBlocks++; |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 780 | /* Fallthrough block not included in the trace */ |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 781 | } else if (!isUnconditionalBranch(lastInsn) && |
| 782 | curBB->fallThrough == NULL) { |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 783 | /* |
| 784 | * If the chaining cell is after an invoke or |
| 785 | * instruction that cannot change the control flow, request a hot |
| 786 | * chaining cell. |
| 787 | */ |
| 788 | if (isInvoke || curBB->needFallThroughBranch) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 789 | lastBB->next = dvmCompilerNewBB(kChainingCellHot); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 790 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 791 | lastBB->next = dvmCompilerNewBB(kChainingCellNormal); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 792 | } |
| 793 | lastBB = lastBB->next; |
| 794 | lastBB->id = numBlocks++; |
| 795 | lastBB->startOffset = fallThroughOffset; |
| 796 | curBB->fallThrough = lastBB; |
| 797 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 798 | /* Target block not included in the trace */ |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 799 | if (curBB->taken == NULL && |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 800 | (isGoto(lastInsn) || isInvoke || |
| 801 | (targetOffset != UNKNOWN_TARGET && targetOffset != curOffset))) { |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 802 | BasicBlock *newBB; |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 803 | if (isInvoke) { |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 804 | /* Monomorphic callee */ |
| 805 | if (callee) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 806 | newBB = dvmCompilerNewBB(kChainingCellInvokeSingleton); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 807 | newBB->startOffset = 0; |
| 808 | newBB->containingMethod = callee; |
| 809 | /* Will resolve at runtime */ |
| 810 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 811 | newBB = dvmCompilerNewBB(kChainingCellInvokePredicted); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 812 | newBB->startOffset = 0; |
| 813 | } |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 814 | /* For unconditional branches, request a hot chaining cell */ |
| 815 | } else { |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 816 | #if !defined(WITH_SELF_VERIFICATION) |
Dan Bornstein | c2b486f | 2010-11-12 16:07:16 -0800 | [diff] [blame] | 817 | newBB = dvmCompilerNewBB(dexIsGoto(flags) ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 818 | kChainingCellHot : |
| 819 | kChainingCellNormal); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 820 | newBB->startOffset = targetOffset; |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 821 | #else |
| 822 | /* Handle branches that branch back into the block */ |
| 823 | if (targetOffset >= curBB->firstMIRInsn->offset && |
| 824 | targetOffset <= curBB->lastMIRInsn->offset) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 825 | newBB = dvmCompilerNewBB(kChainingCellBackwardBranch); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 826 | } else { |
Dan Bornstein | c2b486f | 2010-11-12 16:07:16 -0800 | [diff] [blame] | 827 | newBB = dvmCompilerNewBB(dexIsGoto(flags) ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 828 | kChainingCellHot : |
| 829 | kChainingCellNormal); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 830 | } |
| 831 | newBB->startOffset = targetOffset; |
| 832 | #endif |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 833 | } |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 834 | newBB->id = numBlocks++; |
| 835 | curBB->taken = newBB; |
| 836 | lastBB->next = newBB; |
| 837 | lastBB = newBB; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 838 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* Now create a special block to host PC reconstruction code */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 842 | lastBB->next = dvmCompilerNewBB(kPCReconstruction); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 843 | lastBB = lastBB->next; |
| 844 | lastBB->id = numBlocks++; |
| 845 | |
| 846 | /* And one final block that publishes the PC and raise the exception */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 847 | lastBB->next = dvmCompilerNewBB(kExceptionHandling); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 848 | lastBB = lastBB->next; |
| 849 | lastBB->id = numBlocks++; |
| 850 | |
| 851 | if (cUnit.printMe) { |
Elliott Hughes | cc6fac8 | 2010-07-02 13:38:44 -0700 | [diff] [blame] | 852 | char* signature = dexProtoCopyMethodDescriptor(&desc->method->prototype); |
| 853 | LOGD("TRACEINFO (%d): 0x%08x %s%s.%s 0x%x %d of %d, %d blocks", |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 854 | compilationId, |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 855 | (intptr_t) desc->method->insns, |
| 856 | desc->method->clazz->descriptor, |
| 857 | desc->method->name, |
Elliott Hughes | cc6fac8 | 2010-07-02 13:38:44 -0700 | [diff] [blame] | 858 | signature, |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 859 | desc->trace[0].frag.startOffset, |
| 860 | traceSize, |
| 861 | dexCode->insnsSize, |
| 862 | numBlocks); |
Elliott Hughes | cc6fac8 | 2010-07-02 13:38:44 -0700 | [diff] [blame] | 863 | free(signature); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | BasicBlock **blockList; |
| 867 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 868 | cUnit.traceDesc = desc; |
| 869 | cUnit.numBlocks = numBlocks; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 870 | blockList = cUnit.blockList = |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 871 | (BasicBlock **)dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 872 | |
| 873 | int i; |
| 874 | |
| 875 | for (i = 0, curBB = startBB; i < numBlocks; i++) { |
| 876 | blockList[i] = curBB; |
| 877 | curBB = curBB->next; |
| 878 | } |
| 879 | /* Make sure all blocks are added to the cUnit */ |
| 880 | assert(curBB == NULL); |
| 881 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 882 | /* Set the instruction set to use (NOTE: later components may change it) */ |
| 883 | cUnit.instructionSet = dvmCompilerInstructionSet(); |
| 884 | |
| 885 | /* Inline transformation @ the MIR level */ |
| 886 | if (cUnit.hasInvoke && !(gDvmJit.disableOpt & (1 << kMethodInlining))) { |
| 887 | dvmCompilerInlineMIR(&cUnit); |
| 888 | } |
| 889 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 890 | /* Preparation for SSA conversion */ |
| 891 | dvmInitializeSSAConversion(&cUnit); |
| 892 | |
| 893 | if (cUnit.hasLoop) { |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 894 | /* |
| 895 | * Loop is not optimizable (for example lack of a single induction |
| 896 | * variable), punt and recompile the trace with loop optimization |
| 897 | * disabled. |
| 898 | */ |
| 899 | bool loopOpt = dvmCompilerLoopOpt(&cUnit); |
| 900 | if (loopOpt == false) { |
| 901 | if (cUnit.printMe) { |
| 902 | LOGD("Loop is not optimizable - retry codegen"); |
| 903 | } |
| 904 | /* Reset the compiler resource pool */ |
| 905 | dvmCompilerArenaReset(); |
| 906 | return dvmCompileTrace(desc, cUnit.numInsts, info, bailPtr, |
| 907 | optHints | JIT_OPT_NO_LOOP); |
| 908 | } |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 909 | } |
| 910 | else { |
| 911 | dvmCompilerNonLoopAnalysis(&cUnit); |
| 912 | } |
| 913 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 914 | dvmCompilerInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming |
| 915 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 916 | if (cUnit.printMe) { |
| 917 | dvmCompilerDumpCompilationUnit(&cUnit); |
| 918 | } |
| 919 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 920 | /* Allocate Registers */ |
| 921 | dvmCompilerRegAlloc(&cUnit); |
| 922 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 923 | /* Convert MIR to LIR, etc. */ |
| 924 | dvmCompilerMIR2LIR(&cUnit); |
| 925 | |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 926 | /* Convert LIR into machine code. Loop for recoverable retries */ |
| 927 | do { |
| 928 | dvmCompilerAssembleLIR(&cUnit, info); |
| 929 | cUnit.assemblerRetries++; |
| 930 | if (cUnit.printMe && cUnit.assemblerStatus != kSuccess) |
| 931 | LOGD("Assembler abort #%d on %d",cUnit.assemblerRetries, |
| 932 | cUnit.assemblerStatus); |
| 933 | } while (cUnit.assemblerStatus == kRetryAll); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 934 | |
| 935 | if (cUnit.printMe) { |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 936 | dvmCompilerCodegenDump(&cUnit); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 937 | LOGD("End %s%s, %d Dalvik instructions", |
| 938 | desc->method->clazz->descriptor, desc->method->name, |
| 939 | cUnit.numInsts); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* Reset the compiler resource pool */ |
| 943 | dvmCompilerArenaReset(); |
| 944 | |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 945 | if (cUnit.assemblerStatus == kRetryHalve) { |
| 946 | /* Halve the instruction count and start from the top */ |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 947 | return dvmCompileTrace(desc, cUnit.numInsts / 2, info, bailPtr, |
| 948 | optHints); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 949 | } |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 950 | |
| 951 | assert(cUnit.assemblerStatus == kSuccess); |
| 952 | #if defined(WITH_JIT_TUNING) |
| 953 | methodStats->nativeSize += cUnit.totalSize; |
| 954 | #endif |
| 955 | return info->codeAddress != NULL; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /* |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 959 | * Since we are including instructions from possibly a cold method into the |
| 960 | * current trace, we need to make sure that all the associated information |
| 961 | * with the callee is properly initialized. If not, we punt on this inline |
| 962 | * target. |
| 963 | * |
Ben Cheng | 34dc796 | 2010-08-26 14:56:31 -0700 | [diff] [blame] | 964 | * TODO: volatile instructions will be handled later. |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 965 | */ |
| 966 | bool dvmCompilerCanIncludeThisInstruction(const Method *method, |
| 967 | const DecodedInstruction *insn) |
| 968 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 969 | switch (insn->opcode) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 970 | case OP_NEW_INSTANCE: |
| 971 | case OP_CHECK_CAST: { |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 972 | ClassObject *classPtr = (ClassObject *)(void*) |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 973 | (method->clazz->pDvmDex->pResClasses[insn->vB]); |
| 974 | |
| 975 | /* Class hasn't been initialized yet */ |
| 976 | if (classPtr == NULL) { |
| 977 | return false; |
| 978 | } |
| 979 | return true; |
| 980 | } |
| 981 | case OP_SGET_OBJECT: |
| 982 | case OP_SGET_BOOLEAN: |
| 983 | case OP_SGET_CHAR: |
| 984 | case OP_SGET_BYTE: |
| 985 | case OP_SGET_SHORT: |
| 986 | case OP_SGET: |
| 987 | case OP_SGET_WIDE: |
| 988 | case OP_SPUT_OBJECT: |
| 989 | case OP_SPUT_BOOLEAN: |
| 990 | case OP_SPUT_CHAR: |
| 991 | case OP_SPUT_BYTE: |
| 992 | case OP_SPUT_SHORT: |
| 993 | case OP_SPUT: |
| 994 | case OP_SPUT_WIDE: { |
| 995 | void *fieldPtr = (void*) |
| 996 | (method->clazz->pDvmDex->pResFields[insn->vB]); |
| 997 | |
| 998 | if (fieldPtr == NULL) { |
| 999 | return false; |
| 1000 | } |
| 1001 | return true; |
| 1002 | } |
| 1003 | case OP_INVOKE_SUPER: |
| 1004 | case OP_INVOKE_SUPER_RANGE: { |
| 1005 | int mIndex = method->clazz->pDvmDex-> |
| 1006 | pResMethods[insn->vB]->methodIndex; |
| 1007 | const Method *calleeMethod = method->clazz->super->vtable[mIndex]; |
| 1008 | if (calleeMethod == NULL) { |
| 1009 | return false; |
| 1010 | } |
| 1011 | return true; |
| 1012 | } |
| 1013 | case OP_INVOKE_SUPER_QUICK: |
| 1014 | case OP_INVOKE_SUPER_QUICK_RANGE: { |
| 1015 | const Method *calleeMethod = method->clazz->super->vtable[insn->vB]; |
| 1016 | if (calleeMethod == NULL) { |
| 1017 | return false; |
| 1018 | } |
| 1019 | return true; |
| 1020 | } |
| 1021 | case OP_INVOKE_STATIC: |
| 1022 | case OP_INVOKE_STATIC_RANGE: |
| 1023 | case OP_INVOKE_DIRECT: |
| 1024 | case OP_INVOKE_DIRECT_RANGE: { |
| 1025 | const Method *calleeMethod = |
| 1026 | method->clazz->pDvmDex->pResMethods[insn->vB]; |
| 1027 | if (calleeMethod == NULL) { |
| 1028 | return false; |
| 1029 | } |
| 1030 | return true; |
| 1031 | } |
| 1032 | case OP_CONST_CLASS: { |
| 1033 | void *classPtr = (void*) |
| 1034 | (method->clazz->pDvmDex->pResClasses[insn->vB]); |
| 1035 | |
| 1036 | if (classPtr == NULL) { |
| 1037 | return false; |
| 1038 | } |
| 1039 | return true; |
| 1040 | } |
| 1041 | case OP_CONST_STRING_JUMBO: |
| 1042 | case OP_CONST_STRING: { |
| 1043 | void *strPtr = (void*) |
| 1044 | (method->clazz->pDvmDex->pResStrings[insn->vB]); |
| 1045 | |
| 1046 | if (strPtr == NULL) { |
| 1047 | return false; |
| 1048 | } |
| 1049 | return true; |
| 1050 | } |
| 1051 | default: |
| 1052 | return true; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /* |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1057 | * Similar to dvmCompileTrace, but the entity processed here is the whole |
| 1058 | * method. |
| 1059 | * |
| 1060 | * TODO: implementation will be revisited when the trace builder can provide |
| 1061 | * whole-method traces. |
| 1062 | */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1063 | bool dvmCompileMethod(CompilationUnit *cUnit, const Method *method, |
| 1064 | JitTranslationInfo *info) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1065 | { |
| 1066 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 1067 | const u2 *codePtr = dexCode->insns; |
| 1068 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
| 1069 | int blockID = 0; |
| 1070 | unsigned int curOffset = 0; |
| 1071 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1072 | /* If we've already compiled this trace, just return success */ |
| 1073 | if (dvmJitGetCodeAddr(codePtr) && !info->discardResult) { |
| 1074 | return true; |
| 1075 | } |
| 1076 | |
| 1077 | /* Doing method-based compilation */ |
| 1078 | cUnit->wholeMethod = true; |
| 1079 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 1080 | BasicBlock *firstBlock = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1081 | firstBlock->id = blockID++; |
| 1082 | |
| 1083 | /* Allocate the bit-vector to track the beginning of basic blocks */ |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 1084 | BitVector *bbStartAddr = dvmCompilerAllocBitVector(dexCode->insnsSize+1, |
| 1085 | false); |
| 1086 | dvmCompilerSetBit(bbStartAddr, 0); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1087 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1088 | int numInvokeTargets = 0; |
| 1089 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1090 | /* |
| 1091 | * Sequentially go through every instruction first and put them in a single |
| 1092 | * basic block. Identify block boundaries at the mean time. |
| 1093 | */ |
| 1094 | while (codePtr < codeEnd) { |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 1095 | MIR *insn = (MIR *)dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1096 | insn->offset = curOffset; |
| 1097 | int width = parseInsn(codePtr, &insn->dalvikInsn, false); |
| 1098 | bool isInvoke = false; |
| 1099 | const Method *callee; |
| 1100 | insn->width = width; |
| 1101 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 1102 | /* Terminate when the data section is seen */ |
| 1103 | if (width == 0) |
| 1104 | break; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1105 | |
| 1106 | if (!dvmCompilerCanIncludeThisInstruction(cUnit->method, |
| 1107 | &insn->dalvikInsn)) { |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1111 | dvmCompilerAppendMIR(firstBlock, insn); |
| 1112 | /* |
| 1113 | * Check whether this is a block ending instruction and whether it |
| 1114 | * suggests the start of a new block |
| 1115 | */ |
| 1116 | unsigned int target = curOffset; |
| 1117 | |
| 1118 | /* |
| 1119 | * If findBlockBoundary returns true, it means the current instruction |
| 1120 | * is terminating the current block. If it is a branch, the target |
| 1121 | * address will be recorded in target. |
| 1122 | */ |
| 1123 | if (findBlockBoundary(method, insn, curOffset, &target, &isInvoke, |
| 1124 | &callee)) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 1125 | dvmCompilerSetBit(bbStartAddr, curOffset + width); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1126 | /* Each invoke needs a chaining cell block */ |
| 1127 | if (isInvoke) { |
| 1128 | numInvokeTargets++; |
| 1129 | } |
| 1130 | /* A branch will end the current block */ |
| 1131 | else if (target != curOffset && target != UNKNOWN_TARGET) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 1132 | dvmCompilerSetBit(bbStartAddr, target); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | codePtr += width; |
| 1137 | /* each bit represents 16-bit quantity */ |
| 1138 | curOffset += width; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * The number of blocks will be equal to the number of bits set to 1 in the |
| 1143 | * bit vector minus 1, because the bit representing the location after the |
| 1144 | * last instruction is set to one. |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1145 | * |
| 1146 | * We also add additional blocks for invoke chaining and the number is |
| 1147 | * denoted by numInvokeTargets. |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1148 | */ |
| 1149 | int numBlocks = dvmCountSetBits(bbStartAddr); |
| 1150 | if (dvmIsBitSet(bbStartAddr, dexCode->insnsSize)) { |
| 1151 | numBlocks--; |
| 1152 | } |
| 1153 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1154 | BasicBlock **blockList; |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 1155 | blockList = cUnit->blockList = (BasicBlock **) |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1156 | dvmCompilerNew(sizeof(BasicBlock *) * (numBlocks + numInvokeTargets), |
| 1157 | true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1158 | |
| 1159 | /* |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1160 | * Register the first block onto the list and start splitting it into |
| 1161 | * sub-blocks. |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1162 | */ |
| 1163 | blockList[0] = firstBlock; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1164 | cUnit->numBlocks = 1; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1165 | |
| 1166 | int i; |
| 1167 | for (i = 0; i < numBlocks; i++) { |
| 1168 | MIR *insn; |
| 1169 | BasicBlock *curBB = blockList[i]; |
| 1170 | curOffset = curBB->lastMIRInsn->offset; |
| 1171 | |
| 1172 | for (insn = curBB->firstMIRInsn->next; insn; insn = insn->next) { |
| 1173 | /* Found the beginning of a new block, see if it is created yet */ |
| 1174 | if (dvmIsBitSet(bbStartAddr, insn->offset)) { |
| 1175 | int j; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1176 | for (j = 0; j < cUnit->numBlocks; j++) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1177 | if (blockList[j]->firstMIRInsn->offset == insn->offset) |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | /* Block not split yet - do it now */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1182 | if (j == cUnit->numBlocks) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 1183 | BasicBlock *newBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1184 | newBB->id = blockID++; |
| 1185 | newBB->firstMIRInsn = insn; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 1186 | newBB->startOffset = insn->offset; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1187 | newBB->lastMIRInsn = curBB->lastMIRInsn; |
| 1188 | curBB->lastMIRInsn = insn->prev; |
| 1189 | insn->prev->next = NULL; |
| 1190 | insn->prev = NULL; |
| 1191 | |
| 1192 | /* |
| 1193 | * If the insn is not an unconditional branch, set up the |
| 1194 | * fallthrough link. |
| 1195 | */ |
| 1196 | if (!isUnconditionalBranch(curBB->lastMIRInsn)) { |
| 1197 | curBB->fallThrough = newBB; |
| 1198 | } |
| 1199 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1200 | /* |
| 1201 | * Fallthrough block of an invoke instruction needs to be |
| 1202 | * aligned to 4-byte boundary (alignment instruction to be |
| 1203 | * inserted later. |
| 1204 | */ |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 1205 | if (dexGetFlagsFromOpcode(curBB->lastMIRInsn->dalvikInsn.opcode) |
Dan Bornstein | 41e286c | 2010-11-19 12:36:19 -0800 | [diff] [blame] | 1206 | & kInstrInvoke) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1207 | newBB->isFallThroughFromInvoke = true; |
| 1208 | } |
| 1209 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1210 | /* enqueue the new block */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1211 | blockList[cUnit->numBlocks++] = newBB; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1212 | break; |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1218 | if (numBlocks != cUnit->numBlocks) { |
| 1219 | LOGE("Expect %d vs %d basic blocks\n", numBlocks, cUnit->numBlocks); |
| 1220 | dvmCompilerAbort(cUnit); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1223 | /* Connect the basic blocks through the taken links */ |
| 1224 | for (i = 0; i < numBlocks; i++) { |
| 1225 | BasicBlock *curBB = blockList[i]; |
| 1226 | MIR *insn = curBB->lastMIRInsn; |
| 1227 | unsigned int target = insn->offset; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1228 | bool isInvoke = false; |
| 1229 | const Method *callee = NULL; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1230 | |
| 1231 | findBlockBoundary(method, insn, target, &target, &isInvoke, &callee); |
| 1232 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1233 | /* Found a block ended on a branch (not invoke) */ |
| 1234 | if (isInvoke == false && target != insn->offset) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1235 | int j; |
| 1236 | /* Forward branch */ |
| 1237 | if (target > insn->offset) { |
| 1238 | j = i + 1; |
| 1239 | } else { |
| 1240 | /* Backward branch */ |
| 1241 | j = 0; |
| 1242 | } |
| 1243 | for (; j < numBlocks; j++) { |
| 1244 | if (blockList[j]->firstMIRInsn->offset == target) { |
| 1245 | curBB->taken = blockList[j]; |
| 1246 | break; |
| 1247 | } |
| 1248 | } |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1249 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1250 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1251 | if (isInvoke) { |
| 1252 | BasicBlock *newBB; |
| 1253 | /* Monomorphic callee */ |
| 1254 | if (callee) { |
| 1255 | newBB = dvmCompilerNewBB(kChainingCellInvokeSingleton); |
| 1256 | newBB->startOffset = 0; |
| 1257 | newBB->containingMethod = callee; |
| 1258 | /* Will resolve at runtime */ |
| 1259 | } else { |
| 1260 | newBB = dvmCompilerNewBB(kChainingCellInvokePredicted); |
| 1261 | newBB->startOffset = 0; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1262 | } |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1263 | newBB->id = blockID++; |
| 1264 | curBB->taken = newBB; |
| 1265 | /* enqueue the new block */ |
| 1266 | blockList[cUnit->numBlocks++] = newBB; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1270 | if (cUnit->numBlocks != numBlocks + numInvokeTargets) { |
| 1271 | LOGE("Expect %d vs %d total blocks\n", numBlocks + numInvokeTargets, |
| 1272 | cUnit->numBlocks); |
| 1273 | dvmCompilerDumpCompilationUnit(cUnit); |
| 1274 | dvmCompilerAbort(cUnit); |
| 1275 | } |
| 1276 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1277 | /* Set the instruction set to use (NOTE: later components may change it) */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1278 | cUnit->instructionSet = dvmCompilerInstructionSet(); |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1279 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1280 | /* Preparation for SSA conversion */ |
| 1281 | dvmInitializeSSAConversion(cUnit); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1282 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1283 | /* SSA analysis */ |
| 1284 | dvmCompilerNonLoopAnalysis(cUnit); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1285 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1286 | /* Needs to happen after SSA naming */ |
| 1287 | dvmCompilerInitializeRegAlloc(cUnit); |
| 1288 | |
| 1289 | /* Allocate Registers */ |
| 1290 | dvmCompilerRegAlloc(cUnit); |
| 1291 | |
| 1292 | /* Convert MIR to LIR, etc. */ |
| 1293 | dvmCompilerMIR2LIR(cUnit); |
| 1294 | |
| 1295 | /* Convert LIR into machine code. */ |
| 1296 | dvmCompilerAssembleLIR(cUnit, info); |
| 1297 | |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 1298 | if (cUnit->assemblerStatus != kSuccess) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1299 | return false; |
| 1300 | } |
| 1301 | |
| 1302 | dvmCompilerDumpCompilationUnit(cUnit); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1303 | |
| 1304 | dvmCompilerArenaReset(); |
| 1305 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 1306 | return info->codeAddress != NULL; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1307 | } |