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" |
| 18 | #include "libdex/OpCode.h" |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 19 | #include "interp/Jit.h" |
| 20 | #include "CompilerInternals.h" |
| 21 | |
| 22 | /* |
| 23 | * Parse an instruction, return the length of the instruction |
| 24 | */ |
| 25 | static inline int parseInsn(const u2 *codePtr, DecodedInstruction *decInsn, |
| 26 | bool printMe) |
| 27 | { |
| 28 | u2 instr = *codePtr; |
| 29 | OpCode opcode = instr & 0xff; |
| 30 | int insnWidth; |
| 31 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 32 | // Don't parse instruction data |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 33 | if (opcode == OP_NOP && instr != 0) { |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 34 | return 0; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 35 | } else { |
| 36 | insnWidth = gDvm.instrWidth[opcode]; |
| 37 | if (insnWidth < 0) { |
| 38 | insnWidth = -insnWidth; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | dexDecodeInstruction(gDvm.instrFormat, codePtr, decInsn); |
| 43 | if (printMe) { |
Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 44 | char *decodedString = dvmCompilerGetDalvikDisassembly(decInsn); |
| 45 | LOGD("%p: %#06x %s\n", codePtr, opcode, decodedString); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 46 | } |
| 47 | return insnWidth; |
| 48 | } |
| 49 | |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 50 | #define UNKNOWN_TARGET 0xffffffff |
| 51 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Identify block-ending instructions and collect supplemental information |
| 54 | * regarding the following instructions. |
| 55 | */ |
| 56 | static inline bool findBlockBoundary(const Method *caller, MIR *insn, |
| 57 | unsigned int curOffset, |
| 58 | unsigned int *target, bool *isInvoke, |
| 59 | const Method **callee) |
| 60 | { |
| 61 | switch (insn->dalvikInsn.opCode) { |
| 62 | /* Target is not compile-time constant */ |
| 63 | case OP_RETURN_VOID: |
| 64 | case OP_RETURN: |
| 65 | case OP_RETURN_WIDE: |
| 66 | case OP_RETURN_OBJECT: |
| 67 | case OP_THROW: |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 68 | *target = UNKNOWN_TARGET; |
| 69 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 70 | case OP_INVOKE_VIRTUAL: |
| 71 | case OP_INVOKE_VIRTUAL_RANGE: |
| 72 | case OP_INVOKE_INTERFACE: |
| 73 | case OP_INVOKE_INTERFACE_RANGE: |
| 74 | case OP_INVOKE_VIRTUAL_QUICK: |
| 75 | case OP_INVOKE_VIRTUAL_QUICK_RANGE: |
| 76 | *isInvoke = true; |
| 77 | break; |
| 78 | case OP_INVOKE_SUPER: |
| 79 | case OP_INVOKE_SUPER_RANGE: { |
| 80 | int mIndex = caller->clazz->pDvmDex-> |
| 81 | pResMethods[insn->dalvikInsn.vB]->methodIndex; |
| 82 | const Method *calleeMethod = |
| 83 | caller->clazz->super->vtable[mIndex]; |
| 84 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 85 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 86 | *target = (unsigned int) calleeMethod->insns; |
| 87 | } |
| 88 | *isInvoke = true; |
| 89 | *callee = calleeMethod; |
| 90 | break; |
| 91 | } |
| 92 | case OP_INVOKE_STATIC: |
| 93 | case OP_INVOKE_STATIC_RANGE: { |
| 94 | const Method *calleeMethod = |
| 95 | caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB]; |
| 96 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 97 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 98 | *target = (unsigned int) calleeMethod->insns; |
| 99 | } |
| 100 | *isInvoke = true; |
| 101 | *callee = calleeMethod; |
| 102 | break; |
| 103 | } |
| 104 | case OP_INVOKE_SUPER_QUICK: |
| 105 | case OP_INVOKE_SUPER_QUICK_RANGE: { |
| 106 | const Method *calleeMethod = |
| 107 | caller->clazz->super->vtable[insn->dalvikInsn.vB]; |
| 108 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 109 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 110 | *target = (unsigned int) calleeMethod->insns; |
| 111 | } |
| 112 | *isInvoke = true; |
| 113 | *callee = calleeMethod; |
| 114 | break; |
| 115 | } |
| 116 | case OP_INVOKE_DIRECT: |
| 117 | case OP_INVOKE_DIRECT_RANGE: { |
| 118 | const Method *calleeMethod = |
| 119 | caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB]; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 120 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 121 | *target = (unsigned int) calleeMethod->insns; |
| 122 | } |
| 123 | *isInvoke = true; |
| 124 | *callee = calleeMethod; |
| 125 | break; |
| 126 | } |
| 127 | case OP_GOTO: |
| 128 | case OP_GOTO_16: |
| 129 | case OP_GOTO_32: |
| 130 | *target = curOffset + (int) insn->dalvikInsn.vA; |
| 131 | break; |
| 132 | |
| 133 | case OP_IF_EQ: |
| 134 | case OP_IF_NE: |
| 135 | case OP_IF_LT: |
| 136 | case OP_IF_GE: |
| 137 | case OP_IF_GT: |
| 138 | case OP_IF_LE: |
| 139 | *target = curOffset + (int) insn->dalvikInsn.vC; |
| 140 | break; |
| 141 | |
| 142 | case OP_IF_EQZ: |
| 143 | case OP_IF_NEZ: |
| 144 | case OP_IF_LTZ: |
| 145 | case OP_IF_GEZ: |
| 146 | case OP_IF_GTZ: |
| 147 | case OP_IF_LEZ: |
| 148 | *target = curOffset + (int) insn->dalvikInsn.vB; |
| 149 | break; |
| 150 | |
| 151 | default: |
| 152 | return false; |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 153 | } |
| 154 | return true; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 157 | static inline bool isGoto(MIR *insn) |
| 158 | { |
| 159 | switch (insn->dalvikInsn.opCode) { |
| 160 | case OP_GOTO: |
| 161 | case OP_GOTO_16: |
| 162 | case OP_GOTO_32: |
| 163 | return true; |
| 164 | default: |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 169 | /* |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 170 | * Identify unconditional branch instructions |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 171 | */ |
| 172 | static inline bool isUnconditionalBranch(MIR *insn) |
| 173 | { |
| 174 | switch (insn->dalvikInsn.opCode) { |
| 175 | case OP_RETURN_VOID: |
| 176 | case OP_RETURN: |
| 177 | case OP_RETURN_WIDE: |
| 178 | case OP_RETURN_OBJECT: |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 179 | return true; |
| 180 | default: |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 181 | return isGoto(insn); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 186 | * dvmHashTableLookup() callback |
| 187 | */ |
| 188 | static int compareMethod(const CompilerMethodStats *m1, |
| 189 | const CompilerMethodStats *m2) |
| 190 | { |
| 191 | return (int) m1->method - (int) m2->method; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Analyze each method whose traces are ever compiled. Collect a variety of |
| 196 | * statistics like the ratio of exercised vs overall code and code bloat |
| 197 | * ratios. |
| 198 | */ |
| 199 | static CompilerMethodStats *analyzeMethodBody(const Method *method) |
| 200 | { |
| 201 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 202 | const u2 *codePtr = dexCode->insns; |
| 203 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
| 204 | int insnSize = 0; |
| 205 | int hashValue = dvmComputeUtf8Hash(method->name); |
| 206 | |
| 207 | CompilerMethodStats dummyMethodEntry; // For hash table lookup |
| 208 | CompilerMethodStats *realMethodEntry; // For hash table storage |
| 209 | |
| 210 | /* For lookup only */ |
| 211 | dummyMethodEntry.method = method; |
| 212 | realMethodEntry = dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue, |
| 213 | &dummyMethodEntry, |
| 214 | (HashCompareFunc) compareMethod, |
| 215 | false); |
| 216 | |
| 217 | /* Part of this method has been compiled before - just return the entry */ |
| 218 | if (realMethodEntry != NULL) { |
| 219 | return realMethodEntry; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * First time to compile this method - set up a new entry in the hash table |
| 224 | */ |
| 225 | realMethodEntry = |
| 226 | (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats)); |
| 227 | realMethodEntry->method = method; |
| 228 | |
| 229 | dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue, |
| 230 | realMethodEntry, |
| 231 | (HashCompareFunc) compareMethod, |
| 232 | true); |
| 233 | |
| 234 | /* Count the number of instructions */ |
| 235 | while (codePtr < codeEnd) { |
| 236 | DecodedInstruction dalvikInsn; |
| 237 | int width = parseInsn(codePtr, &dalvikInsn, false); |
| 238 | |
| 239 | /* Terminate when the data section is seen */ |
| 240 | if (width == 0) |
| 241 | break; |
| 242 | |
| 243 | insnSize += width; |
| 244 | codePtr += width; |
| 245 | } |
| 246 | |
| 247 | realMethodEntry->dalvikSize = insnSize * 2; |
| 248 | return realMethodEntry; |
| 249 | } |
| 250 | |
| 251 | /* |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 252 | * Crawl the stack of the thread that requesed compilation to see if any of the |
| 253 | * ancestors are on the blacklist. |
| 254 | */ |
| 255 | bool filterMethodByCallGraph(Thread *thread, const char *curMethodName) |
| 256 | { |
| 257 | /* Crawl the Dalvik stack frames and compare the method name*/ |
| 258 | StackSaveArea *ssaPtr = ((StackSaveArea *) thread->curFrame) - 1; |
| 259 | while (ssaPtr != ((StackSaveArea *) NULL) - 1) { |
| 260 | const Method *method = ssaPtr->method; |
| 261 | if (method) { |
| 262 | int hashValue = dvmComputeUtf8Hash(method->name); |
| 263 | bool found = |
| 264 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 265 | (char *) method->name, |
| 266 | (HashCompareFunc) strcmp, false) != |
| 267 | NULL; |
| 268 | if (found) { |
| 269 | LOGD("Method %s (--> %s) found on the JIT %s list", |
| 270 | method->name, curMethodName, |
| 271 | gDvmJit.includeSelectedMethod ? "white" : "black"); |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | } |
| 276 | ssaPtr = ((StackSaveArea *) ssaPtr->prevFrame) - 1; |
| 277 | }; |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | /* |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 282 | * Main entry point to start trace compilation. Basic blocks are constructed |
| 283 | * first and they will be passed to the codegen routines to convert Dalvik |
| 284 | * bytecode into machine code. |
| 285 | */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 286 | bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts, |
| 287 | JitTranslationInfo *info) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 288 | { |
| 289 | const DexCode *dexCode = dvmGetMethodCode(desc->method); |
| 290 | const JitTraceRun* currRun = &desc->trace[0]; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 291 | unsigned int curOffset = currRun->frag.startOffset; |
| 292 | unsigned int numInsts = currRun->frag.numInsts; |
| 293 | const u2 *codePtr = dexCode->insns + curOffset; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 294 | int traceSize = 0; // # of half-words |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 295 | const u2 *startCodePtr = codePtr; |
| 296 | BasicBlock *startBB, *curBB, *lastBB; |
| 297 | int numBlocks = 0; |
| 298 | static int compilationId; |
| 299 | CompilationUnit cUnit; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 300 | CompilerMethodStats *methodStats; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 301 | |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 302 | /* If we've already compiled this trace, just return success */ |
jeffhao | 9e45c0b | 2010-02-03 10:24:05 -0800 | [diff] [blame^] | 303 | if (dvmJitGetCodeAddr(startCodePtr) && !info->discardResult) { |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 304 | return true; |
| 305 | } |
| 306 | |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 307 | compilationId++; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 308 | memset(&cUnit, 0, sizeof(CompilationUnit)); |
| 309 | |
| 310 | /* Locate the entry to store compilation statistics for this method */ |
| 311 | methodStats = analyzeMethodBody(desc->method); |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 312 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 313 | /* Initialize the printMe flag */ |
| 314 | cUnit.printMe = gDvmJit.printMe; |
| 315 | |
Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 316 | /* Initialize the profile flag */ |
| 317 | cUnit.executionCount = gDvmJit.profile; |
| 318 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 319 | /* Identify traces that we don't want to compile */ |
| 320 | if (gDvmJit.methodTable) { |
| 321 | int len = strlen(desc->method->clazz->descriptor) + |
| 322 | strlen(desc->method->name) + 1; |
| 323 | char *fullSignature = dvmCompilerNew(len, true); |
| 324 | strcpy(fullSignature, desc->method->clazz->descriptor); |
| 325 | strcat(fullSignature, desc->method->name); |
| 326 | |
| 327 | int hashValue = dvmComputeUtf8Hash(fullSignature); |
| 328 | |
| 329 | /* |
| 330 | * Doing three levels of screening to see whether we want to skip |
| 331 | * compiling this method |
| 332 | */ |
| 333 | |
| 334 | /* First, check the full "class;method" signature */ |
| 335 | bool methodFound = |
| 336 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 337 | fullSignature, (HashCompareFunc) strcmp, |
| 338 | false) != |
| 339 | NULL; |
| 340 | |
| 341 | /* Full signature not found - check the enclosing class */ |
| 342 | if (methodFound == false) { |
| 343 | int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor); |
| 344 | methodFound = |
| 345 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 346 | (char *) desc->method->clazz->descriptor, |
| 347 | (HashCompareFunc) strcmp, false) != |
| 348 | NULL; |
| 349 | /* Enclosing class not found - check the method name */ |
| 350 | if (methodFound == false) { |
| 351 | int hashValue = dvmComputeUtf8Hash(desc->method->name); |
| 352 | methodFound = |
| 353 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 354 | (char *) desc->method->name, |
| 355 | (HashCompareFunc) strcmp, false) != |
| 356 | NULL; |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * Debug by call-graph is enabled. Check if the debug list |
| 360 | * covers any methods on the VM stack. |
| 361 | */ |
| 362 | if (methodFound == false && gDvmJit.checkCallGraph == true) { |
| 363 | methodFound = |
| 364 | filterMethodByCallGraph(info->requestingThread, |
| 365 | desc->method->name); |
| 366 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Under the following conditions, the trace will be *conservatively* |
| 372 | * compiled by only containing single-step instructions to and from the |
| 373 | * interpreter. |
| 374 | * 1) If includeSelectedMethod == false, the method matches the full or |
| 375 | * partial signature stored in the hash table. |
| 376 | * |
| 377 | * 2) If includeSelectedMethod == true, the method does not match the |
| 378 | * full and partial signature stored in the hash table. |
| 379 | */ |
| 380 | if (gDvmJit.includeSelectedMethod != methodFound) { |
| 381 | cUnit.allSingleStep = true; |
| 382 | } else { |
| 383 | /* Compile the trace as normal */ |
| 384 | |
| 385 | /* Print the method we cherry picked */ |
| 386 | if (gDvmJit.includeSelectedMethod == true) { |
| 387 | cUnit.printMe = true; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 392 | /* Allocate the entry block */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 393 | lastBB = startBB = curBB = dvmCompilerNewBB(kEntryBlock); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 394 | curBB->startOffset = curOffset; |
| 395 | curBB->id = numBlocks++; |
| 396 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 397 | curBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 398 | curBB->startOffset = curOffset; |
| 399 | curBB->id = numBlocks++; |
| 400 | |
| 401 | /* Make the first real dalvik block the fallthrough of the entry block */ |
| 402 | startBB->fallThrough = curBB; |
| 403 | lastBB->next = curBB; |
| 404 | lastBB = curBB; |
| 405 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 406 | if (cUnit.printMe) { |
| 407 | LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n", |
| 408 | desc->method->name, curOffset); |
| 409 | } |
| 410 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Analyze the trace descriptor and include up to the maximal number |
| 413 | * of Dalvik instructions into the IR. |
| 414 | */ |
| 415 | while (1) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 416 | MIR *insn; |
| 417 | int width; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 418 | insn = dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 419 | insn->offset = curOffset; |
| 420 | width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 421 | |
| 422 | /* The trace should never incude instruction data */ |
| 423 | assert(width); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 424 | insn->width = width; |
| 425 | traceSize += width; |
| 426 | dvmCompilerAppendMIR(curBB, insn); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 427 | cUnit.numInsts++; |
| 428 | /* Instruction limit reached - terminate the trace here */ |
| 429 | if (cUnit.numInsts >= numMaxInsts) { |
| 430 | break; |
| 431 | } |
| 432 | if (--numInsts == 0) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 433 | if (currRun->frag.runEnd) { |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 434 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 435 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 436 | curBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 437 | lastBB->next = curBB; |
| 438 | lastBB = curBB; |
| 439 | curBB->id = numBlocks++; |
| 440 | currRun++; |
| 441 | curOffset = currRun->frag.startOffset; |
| 442 | numInsts = currRun->frag.numInsts; |
| 443 | curBB->startOffset = curOffset; |
| 444 | codePtr = dexCode->insns + curOffset; |
| 445 | } |
| 446 | } else { |
| 447 | curOffset += width; |
| 448 | codePtr += width; |
| 449 | } |
| 450 | } |
| 451 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 452 | /* Convert # of half-word to bytes */ |
| 453 | methodStats->compiledDalvikSize += traceSize * 2; |
| 454 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 455 | /* |
| 456 | * Now scan basic blocks containing real code to connect the |
| 457 | * taken/fallthrough links. Also create chaining cells for code not included |
| 458 | * in the trace. |
| 459 | */ |
| 460 | for (curBB = startBB; curBB; curBB = curBB->next) { |
| 461 | MIR *lastInsn = curBB->lastMIRInsn; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 462 | /* Skip empty blocks */ |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 463 | if (lastInsn == NULL) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 464 | continue; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 465 | } |
| 466 | curOffset = lastInsn->offset; |
| 467 | unsigned int targetOffset = curOffset; |
| 468 | unsigned int fallThroughOffset = curOffset + lastInsn->width; |
| 469 | bool isInvoke = false; |
| 470 | const Method *callee = NULL; |
| 471 | |
| 472 | findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset, |
| 473 | &targetOffset, &isInvoke, &callee); |
| 474 | |
| 475 | /* Link the taken and fallthrough blocks */ |
| 476 | BasicBlock *searchBB; |
| 477 | |
| 478 | /* No backward branch in the trace - start searching the next BB */ |
| 479 | for (searchBB = curBB->next; searchBB; searchBB = searchBB->next) { |
| 480 | if (targetOffset == searchBB->startOffset) { |
| 481 | curBB->taken = searchBB; |
| 482 | } |
| 483 | if (fallThroughOffset == searchBB->startOffset) { |
| 484 | curBB->fallThrough = searchBB; |
| 485 | } |
| 486 | } |
| 487 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 488 | int flags = dexGetInstrFlags(gDvm.instrFlags, |
| 489 | lastInsn->dalvikInsn.opCode); |
| 490 | |
| 491 | /* |
| 492 | * Some blocks are ended by non-control-flow-change instructions, |
| 493 | * currently only due to trace length constraint. In this case we need |
| 494 | * to generate an explicit branch at the end of the block to jump to |
| 495 | * the chaining cell. |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 496 | * |
| 497 | * NOTE: INVOKE_DIRECT_EMPTY is actually not an invoke but a nop |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 498 | */ |
| 499 | curBB->needFallThroughBranch = |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 500 | ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn | |
| 501 | kInstrInvoke)) == 0) || |
| 502 | (lastInsn->dalvikInsn.opCode == OP_INVOKE_DIRECT_EMPTY); |
| 503 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 504 | if (curBB->taken == NULL && |
| 505 | curBB->fallThrough == NULL && |
| 506 | flags == (kInstrCanBranch | kInstrCanContinue) && |
| 507 | fallThroughOffset == startBB->startOffset) { |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 508 | BasicBlock *loopBranch = curBB; |
| 509 | BasicBlock *exitBB; |
| 510 | BasicBlock *exitChainingCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 511 | |
| 512 | if (cUnit.printMe) { |
| 513 | LOGD("Natural loop detected!"); |
| 514 | } |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 515 | exitBB = dvmCompilerNewBB(kExitBlock); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 516 | lastBB->next = exitBB; |
| 517 | lastBB = exitBB; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 518 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 519 | exitBB->startOffset = targetOffset; |
| 520 | exitBB->id = numBlocks++; |
| 521 | exitBB->needFallThroughBranch = true; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 522 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 523 | loopBranch->taken = exitBB; |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 524 | #if defined(WITH_SELF_VERIFICATION) |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 525 | BasicBlock *backwardCell = |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 526 | dvmCompilerNewBB(kChainingCellBackwardBranch); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 527 | lastBB->next = backwardCell; |
| 528 | lastBB = backwardCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 529 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 530 | backwardCell->startOffset = startBB->startOffset; |
| 531 | backwardCell->id = numBlocks++; |
| 532 | loopBranch->fallThrough = backwardCell; |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 533 | #elif defined(WITH_JIT_TUNING) |
| 534 | if (gDvmJit.profile) { |
| 535 | BasicBlock *backwardCell = |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 536 | dvmCompilerNewBB(kChainingCellBackwardBranch); |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 537 | lastBB->next = backwardCell; |
| 538 | lastBB = backwardCell; |
| 539 | |
| 540 | backwardCell->startOffset = startBB->startOffset; |
| 541 | backwardCell->id = numBlocks++; |
| 542 | loopBranch->fallThrough = backwardCell; |
| 543 | } else { |
| 544 | loopBranch->fallThrough = startBB->next; |
| 545 | } |
| 546 | #else |
| 547 | loopBranch->fallThrough = startBB->next; |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 548 | #endif |
| 549 | |
| 550 | /* Create the chaining cell as the fallthrough of the exit block */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 551 | exitChainingCell = dvmCompilerNewBB(kChainingCellNormal); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 552 | lastBB->next = exitChainingCell; |
| 553 | lastBB = exitChainingCell; |
| 554 | |
| 555 | exitChainingCell->startOffset = targetOffset; |
| 556 | exitChainingCell->id = numBlocks++; |
| 557 | |
| 558 | exitBB->fallThrough = exitChainingCell; |
| 559 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 560 | cUnit.hasLoop = true; |
| 561 | } |
| 562 | |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 563 | if (lastInsn->dalvikInsn.opCode == OP_PACKED_SWITCH || |
| 564 | lastInsn->dalvikInsn.opCode == OP_SPARSE_SWITCH) { |
| 565 | int i; |
| 566 | const u2 *switchData = desc->method->insns + lastInsn->offset + |
| 567 | lastInsn->dalvikInsn.vB; |
| 568 | int size = switchData[1]; |
| 569 | int maxChains = MIN(size, MAX_CHAINED_SWITCH_CASES); |
| 570 | |
| 571 | /* |
| 572 | * Generate the landing pad for cases whose ranks are higher than |
| 573 | * MAX_CHAINED_SWITCH_CASES. The code will re-enter the interpreter |
| 574 | * through the NoChain point. |
| 575 | */ |
| 576 | if (maxChains != size) { |
| 577 | cUnit.switchOverflowPad = |
| 578 | desc->method->insns + lastInsn->offset; |
| 579 | } |
| 580 | |
| 581 | s4 *targets = (s4 *) (switchData + 2 + |
| 582 | (lastInsn->dalvikInsn.opCode == OP_PACKED_SWITCH ? |
| 583 | 2 : size * 2)); |
| 584 | |
| 585 | /* One chaining cell for the first MAX_CHAINED_SWITCH_CASES cases */ |
| 586 | for (i = 0; i < maxChains; i++) { |
| 587 | BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal); |
| 588 | lastBB->next = caseChain; |
| 589 | lastBB = caseChain; |
| 590 | |
| 591 | caseChain->startOffset = lastInsn->offset + targets[i]; |
| 592 | caseChain->id = numBlocks++; |
| 593 | } |
| 594 | |
| 595 | /* One more chaining cell for the default case */ |
| 596 | BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal); |
| 597 | lastBB->next = caseChain; |
| 598 | lastBB = caseChain; |
| 599 | |
| 600 | caseChain->startOffset = lastInsn->offset + lastInsn->width; |
| 601 | caseChain->id = numBlocks++; |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 602 | /* Fallthrough block not included in the trace */ |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 603 | } else if (!isUnconditionalBranch(lastInsn) && |
| 604 | curBB->fallThrough == NULL) { |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 605 | /* |
| 606 | * If the chaining cell is after an invoke or |
| 607 | * instruction that cannot change the control flow, request a hot |
| 608 | * chaining cell. |
| 609 | */ |
| 610 | if (isInvoke || curBB->needFallThroughBranch) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 611 | lastBB->next = dvmCompilerNewBB(kChainingCellHot); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 612 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 613 | lastBB->next = dvmCompilerNewBB(kChainingCellNormal); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 614 | } |
| 615 | lastBB = lastBB->next; |
| 616 | lastBB->id = numBlocks++; |
| 617 | lastBB->startOffset = fallThroughOffset; |
| 618 | curBB->fallThrough = lastBB; |
| 619 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 620 | /* Target block not included in the trace */ |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 621 | if (curBB->taken == NULL && |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 622 | (isGoto(lastInsn) || isInvoke || |
| 623 | (targetOffset != UNKNOWN_TARGET && targetOffset != curOffset))) { |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 624 | BasicBlock *newBB; |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 625 | if (isInvoke) { |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 626 | /* Monomorphic callee */ |
| 627 | if (callee) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 628 | newBB = dvmCompilerNewBB(kChainingCellInvokeSingleton); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 629 | newBB->startOffset = 0; |
| 630 | newBB->containingMethod = callee; |
| 631 | /* Will resolve at runtime */ |
| 632 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 633 | newBB = dvmCompilerNewBB(kChainingCellInvokePredicted); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 634 | newBB->startOffset = 0; |
| 635 | } |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 636 | /* For unconditional branches, request a hot chaining cell */ |
| 637 | } else { |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 638 | #if !defined(WITH_SELF_VERIFICATION) |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 639 | newBB = dvmCompilerNewBB(flags & kInstrUnconditional ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 640 | kChainingCellHot : |
| 641 | kChainingCellNormal); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 642 | newBB->startOffset = targetOffset; |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 643 | #else |
| 644 | /* Handle branches that branch back into the block */ |
| 645 | if (targetOffset >= curBB->firstMIRInsn->offset && |
| 646 | targetOffset <= curBB->lastMIRInsn->offset) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 647 | newBB = dvmCompilerNewBB(kChainingCellBackwardBranch); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 648 | } else { |
| 649 | newBB = dvmCompilerNewBB(flags & kInstrUnconditional ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 650 | kChainingCellHot : |
| 651 | kChainingCellNormal); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 652 | } |
| 653 | newBB->startOffset = targetOffset; |
| 654 | #endif |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 655 | } |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 656 | newBB->id = numBlocks++; |
| 657 | curBB->taken = newBB; |
| 658 | lastBB->next = newBB; |
| 659 | lastBB = newBB; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 660 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* Now create a special block to host PC reconstruction code */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 664 | lastBB->next = dvmCompilerNewBB(kPCReconstruction); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 665 | lastBB = lastBB->next; |
| 666 | lastBB->id = numBlocks++; |
| 667 | |
| 668 | /* And one final block that publishes the PC and raise the exception */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 669 | lastBB->next = dvmCompilerNewBB(kExceptionHandling); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 670 | lastBB = lastBB->next; |
| 671 | lastBB->id = numBlocks++; |
| 672 | |
| 673 | if (cUnit.printMe) { |
| 674 | LOGD("TRACEINFO (%d): 0x%08x %s%s 0x%x %d of %d, %d blocks", |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 675 | compilationId, |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 676 | (intptr_t) desc->method->insns, |
| 677 | desc->method->clazz->descriptor, |
| 678 | desc->method->name, |
| 679 | desc->trace[0].frag.startOffset, |
| 680 | traceSize, |
| 681 | dexCode->insnsSize, |
| 682 | numBlocks); |
| 683 | } |
| 684 | |
| 685 | BasicBlock **blockList; |
| 686 | |
| 687 | cUnit.method = desc->method; |
| 688 | cUnit.traceDesc = desc; |
| 689 | cUnit.numBlocks = numBlocks; |
| 690 | dvmInitGrowableList(&cUnit.pcReconstructionList, 8); |
| 691 | blockList = cUnit.blockList = |
| 692 | dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true); |
| 693 | |
| 694 | int i; |
| 695 | |
| 696 | for (i = 0, curBB = startBB; i < numBlocks; i++) { |
| 697 | blockList[i] = curBB; |
| 698 | curBB = curBB->next; |
| 699 | } |
| 700 | /* Make sure all blocks are added to the cUnit */ |
| 701 | assert(curBB == NULL); |
| 702 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 703 | /* Preparation for SSA conversion */ |
| 704 | dvmInitializeSSAConversion(&cUnit); |
| 705 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 706 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 707 | if (cUnit.hasLoop) { |
| 708 | dvmCompilerLoopOpt(&cUnit); |
| 709 | } |
| 710 | else { |
| 711 | dvmCompilerNonLoopAnalysis(&cUnit); |
| 712 | } |
| 713 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 714 | dvmCompilerInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming |
| 715 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 716 | if (cUnit.printMe) { |
| 717 | dvmCompilerDumpCompilationUnit(&cUnit); |
| 718 | } |
| 719 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 720 | /* Set the instruction set to use (NOTE: later components may change it) */ |
Ben Cheng | 5d90c20 | 2009-11-22 23:31:11 -0800 | [diff] [blame] | 721 | cUnit.instructionSet = dvmCompilerInstructionSet(); |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 722 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 723 | /* Allocate Registers */ |
| 724 | dvmCompilerRegAlloc(&cUnit); |
| 725 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 726 | /* Convert MIR to LIR, etc. */ |
| 727 | dvmCompilerMIR2LIR(&cUnit); |
| 728 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 729 | /* Convert LIR into machine code. */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 730 | dvmCompilerAssembleLIR(&cUnit, info); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 731 | |
| 732 | if (cUnit.printMe) { |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 733 | if (cUnit.halveInstCount) { |
| 734 | LOGD("Assembler aborted"); |
| 735 | } else { |
| 736 | dvmCompilerCodegenDump(&cUnit); |
| 737 | } |
| 738 | LOGD("End %s%s, %d Dalvik instructions", |
| 739 | desc->method->clazz->descriptor, desc->method->name, |
| 740 | cUnit.numInsts); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* Reset the compiler resource pool */ |
| 744 | dvmCompilerArenaReset(); |
| 745 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 746 | /* Success */ |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 747 | if (!cUnit.halveInstCount) { |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 748 | methodStats->nativeSize += cUnit.totalSize; |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 749 | return info->codeAddress != NULL; |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 750 | |
| 751 | /* Halve the instruction count and retry again */ |
| 752 | } else { |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 753 | return dvmCompileTrace(desc, cUnit.numInsts / 2, info); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 754 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Similar to dvmCompileTrace, but the entity processed here is the whole |
| 759 | * method. |
| 760 | * |
| 761 | * TODO: implementation will be revisited when the trace builder can provide |
| 762 | * whole-method traces. |
| 763 | */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 764 | bool dvmCompileMethod(const Method *method, JitTranslationInfo *info) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 765 | { |
| 766 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 767 | const u2 *codePtr = dexCode->insns; |
| 768 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
| 769 | int blockID = 0; |
| 770 | unsigned int curOffset = 0; |
| 771 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 772 | BasicBlock *firstBlock = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 773 | firstBlock->id = blockID++; |
| 774 | |
| 775 | /* Allocate the bit-vector to track the beginning of basic blocks */ |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 776 | BitVector *bbStartAddr = dvmCompilerAllocBitVector(dexCode->insnsSize+1, |
| 777 | false); |
| 778 | dvmCompilerSetBit(bbStartAddr, 0); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 779 | |
| 780 | /* |
| 781 | * Sequentially go through every instruction first and put them in a single |
| 782 | * basic block. Identify block boundaries at the mean time. |
| 783 | */ |
| 784 | while (codePtr < codeEnd) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 785 | MIR *insn = dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 786 | insn->offset = curOffset; |
| 787 | int width = parseInsn(codePtr, &insn->dalvikInsn, false); |
| 788 | bool isInvoke = false; |
| 789 | const Method *callee; |
| 790 | insn->width = width; |
| 791 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 792 | /* Terminate when the data section is seen */ |
| 793 | if (width == 0) |
| 794 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 795 | dvmCompilerAppendMIR(firstBlock, insn); |
| 796 | /* |
| 797 | * Check whether this is a block ending instruction and whether it |
| 798 | * suggests the start of a new block |
| 799 | */ |
| 800 | unsigned int target = curOffset; |
| 801 | |
| 802 | /* |
| 803 | * If findBlockBoundary returns true, it means the current instruction |
| 804 | * is terminating the current block. If it is a branch, the target |
| 805 | * address will be recorded in target. |
| 806 | */ |
| 807 | if (findBlockBoundary(method, insn, curOffset, &target, &isInvoke, |
| 808 | &callee)) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 809 | dvmCompilerSetBit(bbStartAddr, curOffset + width); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 810 | if (target != curOffset) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 811 | dvmCompilerSetBit(bbStartAddr, target); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
| 815 | codePtr += width; |
| 816 | /* each bit represents 16-bit quantity */ |
| 817 | curOffset += width; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * The number of blocks will be equal to the number of bits set to 1 in the |
| 822 | * bit vector minus 1, because the bit representing the location after the |
| 823 | * last instruction is set to one. |
| 824 | */ |
| 825 | int numBlocks = dvmCountSetBits(bbStartAddr); |
| 826 | if (dvmIsBitSet(bbStartAddr, dexCode->insnsSize)) { |
| 827 | numBlocks--; |
| 828 | } |
| 829 | |
| 830 | CompilationUnit cUnit; |
| 831 | BasicBlock **blockList; |
| 832 | |
| 833 | memset(&cUnit, 0, sizeof(CompilationUnit)); |
| 834 | cUnit.method = method; |
| 835 | blockList = cUnit.blockList = |
| 836 | dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true); |
| 837 | |
| 838 | /* |
| 839 | * Register the first block onto the list and start split it into block |
| 840 | * boundaries from there. |
| 841 | */ |
| 842 | blockList[0] = firstBlock; |
| 843 | cUnit.numBlocks = 1; |
| 844 | |
| 845 | int i; |
| 846 | for (i = 0; i < numBlocks; i++) { |
| 847 | MIR *insn; |
| 848 | BasicBlock *curBB = blockList[i]; |
| 849 | curOffset = curBB->lastMIRInsn->offset; |
| 850 | |
| 851 | for (insn = curBB->firstMIRInsn->next; insn; insn = insn->next) { |
| 852 | /* Found the beginning of a new block, see if it is created yet */ |
| 853 | if (dvmIsBitSet(bbStartAddr, insn->offset)) { |
| 854 | int j; |
| 855 | for (j = 0; j < cUnit.numBlocks; j++) { |
| 856 | if (blockList[j]->firstMIRInsn->offset == insn->offset) |
| 857 | break; |
| 858 | } |
| 859 | |
| 860 | /* Block not split yet - do it now */ |
| 861 | if (j == cUnit.numBlocks) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 862 | BasicBlock *newBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 863 | newBB->id = blockID++; |
| 864 | newBB->firstMIRInsn = insn; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 865 | newBB->startOffset = insn->offset; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 866 | newBB->lastMIRInsn = curBB->lastMIRInsn; |
| 867 | curBB->lastMIRInsn = insn->prev; |
| 868 | insn->prev->next = NULL; |
| 869 | insn->prev = NULL; |
| 870 | |
| 871 | /* |
| 872 | * If the insn is not an unconditional branch, set up the |
| 873 | * fallthrough link. |
| 874 | */ |
| 875 | if (!isUnconditionalBranch(curBB->lastMIRInsn)) { |
| 876 | curBB->fallThrough = newBB; |
| 877 | } |
| 878 | |
| 879 | /* enqueue the new block */ |
| 880 | blockList[cUnit.numBlocks++] = newBB; |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | if (numBlocks != cUnit.numBlocks) { |
| 888 | LOGE("Expect %d vs %d basic blocks\n", numBlocks, cUnit.numBlocks); |
| 889 | dvmAbort(); |
| 890 | } |
| 891 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 892 | /* Connect the basic blocks through the taken links */ |
| 893 | for (i = 0; i < numBlocks; i++) { |
| 894 | BasicBlock *curBB = blockList[i]; |
| 895 | MIR *insn = curBB->lastMIRInsn; |
| 896 | unsigned int target = insn->offset; |
| 897 | bool isInvoke; |
| 898 | const Method *callee; |
| 899 | |
| 900 | findBlockBoundary(method, insn, target, &target, &isInvoke, &callee); |
| 901 | |
| 902 | /* Found a block ended on a branch */ |
| 903 | if (target != insn->offset) { |
| 904 | int j; |
| 905 | /* Forward branch */ |
| 906 | if (target > insn->offset) { |
| 907 | j = i + 1; |
| 908 | } else { |
| 909 | /* Backward branch */ |
| 910 | j = 0; |
| 911 | } |
| 912 | for (; j < numBlocks; j++) { |
| 913 | if (blockList[j]->firstMIRInsn->offset == target) { |
| 914 | curBB->taken = blockList[j]; |
| 915 | break; |
| 916 | } |
| 917 | } |
| 918 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 919 | /* Don't create dummy block for the callee yet */ |
| 920 | if (j == numBlocks && !isInvoke) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 921 | LOGE("Target not found for insn %x: expect target %x\n", |
| 922 | curBB->lastMIRInsn->offset, target); |
| 923 | dvmAbort(); |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 928 | /* Set the instruction set to use (NOTE: later components may change it) */ |
Ben Cheng | 5d90c20 | 2009-11-22 23:31:11 -0800 | [diff] [blame] | 929 | cUnit.instructionSet = dvmCompilerInstructionSet(); |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 930 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 931 | dvmCompilerMIR2LIR(&cUnit); |
| 932 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 933 | dvmCompilerAssembleLIR(&cUnit, info); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 934 | |
| 935 | dvmCompilerDumpCompilationUnit(&cUnit); |
| 936 | |
| 937 | dvmCompilerArenaReset(); |
| 938 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 939 | return info->codeAddress != NULL; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 940 | } |