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 | |
| 157 | /* |
| 158 | * Identify conditional branch instructions |
| 159 | */ |
| 160 | static inline bool isUnconditionalBranch(MIR *insn) |
| 161 | { |
| 162 | switch (insn->dalvikInsn.opCode) { |
| 163 | case OP_RETURN_VOID: |
| 164 | case OP_RETURN: |
| 165 | case OP_RETURN_WIDE: |
| 166 | case OP_RETURN_OBJECT: |
| 167 | case OP_GOTO: |
| 168 | case OP_GOTO_16: |
| 169 | case OP_GOTO_32: |
| 170 | return true; |
| 171 | default: |
| 172 | return false; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 177 | * dvmHashTableLookup() callback |
| 178 | */ |
| 179 | static int compareMethod(const CompilerMethodStats *m1, |
| 180 | const CompilerMethodStats *m2) |
| 181 | { |
| 182 | return (int) m1->method - (int) m2->method; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Analyze each method whose traces are ever compiled. Collect a variety of |
| 187 | * statistics like the ratio of exercised vs overall code and code bloat |
| 188 | * ratios. |
| 189 | */ |
| 190 | static CompilerMethodStats *analyzeMethodBody(const Method *method) |
| 191 | { |
| 192 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 193 | const u2 *codePtr = dexCode->insns; |
| 194 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
| 195 | int insnSize = 0; |
| 196 | int hashValue = dvmComputeUtf8Hash(method->name); |
| 197 | |
| 198 | CompilerMethodStats dummyMethodEntry; // For hash table lookup |
| 199 | CompilerMethodStats *realMethodEntry; // For hash table storage |
| 200 | |
| 201 | /* For lookup only */ |
| 202 | dummyMethodEntry.method = method; |
| 203 | realMethodEntry = dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue, |
| 204 | &dummyMethodEntry, |
| 205 | (HashCompareFunc) compareMethod, |
| 206 | false); |
| 207 | |
| 208 | /* Part of this method has been compiled before - just return the entry */ |
| 209 | if (realMethodEntry != NULL) { |
| 210 | return realMethodEntry; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * First time to compile this method - set up a new entry in the hash table |
| 215 | */ |
| 216 | realMethodEntry = |
| 217 | (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats)); |
| 218 | realMethodEntry->method = method; |
| 219 | |
| 220 | dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue, |
| 221 | realMethodEntry, |
| 222 | (HashCompareFunc) compareMethod, |
| 223 | true); |
| 224 | |
| 225 | /* Count the number of instructions */ |
| 226 | while (codePtr < codeEnd) { |
| 227 | DecodedInstruction dalvikInsn; |
| 228 | int width = parseInsn(codePtr, &dalvikInsn, false); |
| 229 | |
| 230 | /* Terminate when the data section is seen */ |
| 231 | if (width == 0) |
| 232 | break; |
| 233 | |
| 234 | insnSize += width; |
| 235 | codePtr += width; |
| 236 | } |
| 237 | |
| 238 | realMethodEntry->dalvikSize = insnSize * 2; |
| 239 | return realMethodEntry; |
| 240 | } |
| 241 | |
| 242 | /* |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 243 | * Main entry point to start trace compilation. Basic blocks are constructed |
| 244 | * first and they will be passed to the codegen routines to convert Dalvik |
| 245 | * bytecode into machine code. |
| 246 | */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 247 | bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts, |
| 248 | JitTranslationInfo *info) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 249 | { |
| 250 | const DexCode *dexCode = dvmGetMethodCode(desc->method); |
| 251 | const JitTraceRun* currRun = &desc->trace[0]; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 252 | unsigned int curOffset = currRun->frag.startOffset; |
| 253 | unsigned int numInsts = currRun->frag.numInsts; |
| 254 | const u2 *codePtr = dexCode->insns + curOffset; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 255 | int traceSize = 0; // # of half-words |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 256 | const u2 *startCodePtr = codePtr; |
| 257 | BasicBlock *startBB, *curBB, *lastBB; |
| 258 | int numBlocks = 0; |
| 259 | static int compilationId; |
| 260 | CompilationUnit cUnit; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 261 | CompilerMethodStats *methodStats; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 262 | |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 263 | compilationId++; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 264 | memset(&cUnit, 0, sizeof(CompilationUnit)); |
| 265 | |
| 266 | /* Locate the entry to store compilation statistics for this method */ |
| 267 | methodStats = analyzeMethodBody(desc->method); |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 268 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 269 | /* Initialize the printMe flag */ |
| 270 | cUnit.printMe = gDvmJit.printMe; |
| 271 | |
Bill Buzbee | 6e963e1 | 2009-06-17 16:56:19 -0700 | [diff] [blame] | 272 | /* Initialize the profile flag */ |
| 273 | cUnit.executionCount = gDvmJit.profile; |
| 274 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 275 | /* Identify traces that we don't want to compile */ |
| 276 | if (gDvmJit.methodTable) { |
| 277 | int len = strlen(desc->method->clazz->descriptor) + |
| 278 | strlen(desc->method->name) + 1; |
| 279 | char *fullSignature = dvmCompilerNew(len, true); |
| 280 | strcpy(fullSignature, desc->method->clazz->descriptor); |
| 281 | strcat(fullSignature, desc->method->name); |
| 282 | |
| 283 | int hashValue = dvmComputeUtf8Hash(fullSignature); |
| 284 | |
| 285 | /* |
| 286 | * Doing three levels of screening to see whether we want to skip |
| 287 | * compiling this method |
| 288 | */ |
| 289 | |
| 290 | /* First, check the full "class;method" signature */ |
| 291 | bool methodFound = |
| 292 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 293 | fullSignature, (HashCompareFunc) strcmp, |
| 294 | false) != |
| 295 | NULL; |
| 296 | |
| 297 | /* Full signature not found - check the enclosing class */ |
| 298 | if (methodFound == false) { |
| 299 | int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor); |
| 300 | methodFound = |
| 301 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 302 | (char *) desc->method->clazz->descriptor, |
| 303 | (HashCompareFunc) strcmp, false) != |
| 304 | NULL; |
| 305 | /* Enclosing class not found - check the method name */ |
| 306 | if (methodFound == false) { |
| 307 | int hashValue = dvmComputeUtf8Hash(desc->method->name); |
| 308 | methodFound = |
| 309 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 310 | (char *) desc->method->name, |
| 311 | (HashCompareFunc) strcmp, false) != |
| 312 | NULL; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Under the following conditions, the trace will be *conservatively* |
| 318 | * compiled by only containing single-step instructions to and from the |
| 319 | * interpreter. |
| 320 | * 1) If includeSelectedMethod == false, the method matches the full or |
| 321 | * partial signature stored in the hash table. |
| 322 | * |
| 323 | * 2) If includeSelectedMethod == true, the method does not match the |
| 324 | * full and partial signature stored in the hash table. |
| 325 | */ |
| 326 | if (gDvmJit.includeSelectedMethod != methodFound) { |
| 327 | cUnit.allSingleStep = true; |
| 328 | } else { |
| 329 | /* Compile the trace as normal */ |
| 330 | |
| 331 | /* Print the method we cherry picked */ |
| 332 | if (gDvmJit.includeSelectedMethod == true) { |
| 333 | cUnit.printMe = true; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 338 | /* Allocate the entry block */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 339 | lastBB = startBB = curBB = dvmCompilerNewBB(kEntryBlock); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 340 | curBB->startOffset = curOffset; |
| 341 | curBB->id = numBlocks++; |
| 342 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 343 | curBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 344 | curBB->startOffset = curOffset; |
| 345 | curBB->id = numBlocks++; |
| 346 | |
| 347 | /* Make the first real dalvik block the fallthrough of the entry block */ |
| 348 | startBB->fallThrough = curBB; |
| 349 | lastBB->next = curBB; |
| 350 | lastBB = curBB; |
| 351 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 352 | if (cUnit.printMe) { |
| 353 | LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n", |
| 354 | desc->method->name, curOffset); |
| 355 | } |
| 356 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 357 | /* |
| 358 | * Analyze the trace descriptor and include up to the maximal number |
| 359 | * of Dalvik instructions into the IR. |
| 360 | */ |
| 361 | while (1) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 362 | MIR *insn; |
| 363 | int width; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 364 | insn = dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 365 | insn->offset = curOffset; |
| 366 | width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 367 | |
| 368 | /* The trace should never incude instruction data */ |
| 369 | assert(width); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 370 | insn->width = width; |
| 371 | traceSize += width; |
| 372 | dvmCompilerAppendMIR(curBB, insn); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 373 | cUnit.numInsts++; |
| 374 | /* Instruction limit reached - terminate the trace here */ |
| 375 | if (cUnit.numInsts >= numMaxInsts) { |
| 376 | break; |
| 377 | } |
| 378 | if (--numInsts == 0) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 379 | if (currRun->frag.runEnd) { |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 380 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 381 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 382 | curBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 383 | lastBB->next = curBB; |
| 384 | lastBB = curBB; |
| 385 | curBB->id = numBlocks++; |
| 386 | currRun++; |
| 387 | curOffset = currRun->frag.startOffset; |
| 388 | numInsts = currRun->frag.numInsts; |
| 389 | curBB->startOffset = curOffset; |
| 390 | codePtr = dexCode->insns + curOffset; |
| 391 | } |
| 392 | } else { |
| 393 | curOffset += width; |
| 394 | codePtr += width; |
| 395 | } |
| 396 | } |
| 397 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 398 | /* Convert # of half-word to bytes */ |
| 399 | methodStats->compiledDalvikSize += traceSize * 2; |
| 400 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 401 | /* |
| 402 | * Now scan basic blocks containing real code to connect the |
| 403 | * taken/fallthrough links. Also create chaining cells for code not included |
| 404 | * in the trace. |
| 405 | */ |
| 406 | for (curBB = startBB; curBB; curBB = curBB->next) { |
| 407 | MIR *lastInsn = curBB->lastMIRInsn; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 408 | /* Skip empty blocks */ |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 409 | if (lastInsn == NULL) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 410 | continue; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 411 | } |
| 412 | curOffset = lastInsn->offset; |
| 413 | unsigned int targetOffset = curOffset; |
| 414 | unsigned int fallThroughOffset = curOffset + lastInsn->width; |
| 415 | bool isInvoke = false; |
| 416 | const Method *callee = NULL; |
| 417 | |
| 418 | findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset, |
| 419 | &targetOffset, &isInvoke, &callee); |
| 420 | |
| 421 | /* Link the taken and fallthrough blocks */ |
| 422 | BasicBlock *searchBB; |
| 423 | |
| 424 | /* No backward branch in the trace - start searching the next BB */ |
| 425 | for (searchBB = curBB->next; searchBB; searchBB = searchBB->next) { |
| 426 | if (targetOffset == searchBB->startOffset) { |
| 427 | curBB->taken = searchBB; |
| 428 | } |
| 429 | if (fallThroughOffset == searchBB->startOffset) { |
| 430 | curBB->fallThrough = searchBB; |
| 431 | } |
| 432 | } |
| 433 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 434 | int flags = dexGetInstrFlags(gDvm.instrFlags, |
| 435 | lastInsn->dalvikInsn.opCode); |
| 436 | |
| 437 | /* |
| 438 | * Some blocks are ended by non-control-flow-change instructions, |
| 439 | * currently only due to trace length constraint. In this case we need |
| 440 | * to generate an explicit branch at the end of the block to jump to |
| 441 | * the chaining cell. |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 442 | * |
| 443 | * NOTE: INVOKE_DIRECT_EMPTY is actually not an invoke but a nop |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 444 | */ |
| 445 | curBB->needFallThroughBranch = |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 446 | ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn | |
| 447 | kInstrInvoke)) == 0) || |
| 448 | (lastInsn->dalvikInsn.opCode == OP_INVOKE_DIRECT_EMPTY); |
| 449 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 450 | if (curBB->taken == NULL && |
| 451 | curBB->fallThrough == NULL && |
| 452 | flags == (kInstrCanBranch | kInstrCanContinue) && |
| 453 | fallThroughOffset == startBB->startOffset) { |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 454 | BasicBlock *loopBranch = curBB; |
| 455 | BasicBlock *exitBB; |
| 456 | BasicBlock *exitChainingCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 457 | |
| 458 | if (cUnit.printMe) { |
| 459 | LOGD("Natural loop detected!"); |
| 460 | } |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 461 | exitBB = dvmCompilerNewBB(kExitBlock); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 462 | lastBB->next = exitBB; |
| 463 | lastBB = exitBB; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 464 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 465 | exitBB->startOffset = targetOffset; |
| 466 | exitBB->id = numBlocks++; |
| 467 | exitBB->needFallThroughBranch = true; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 468 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 469 | loopBranch->taken = exitBB; |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 470 | #if defined(WITH_SELF_VERIFICATION) |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 471 | BasicBlock *backwardCell = |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 472 | dvmCompilerNewBB(kChainingCellBackwardBranch); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 473 | lastBB->next = backwardCell; |
| 474 | lastBB = backwardCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 475 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 476 | backwardCell->startOffset = startBB->startOffset; |
| 477 | backwardCell->id = numBlocks++; |
| 478 | loopBranch->fallThrough = backwardCell; |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 479 | #elif defined(WITH_JIT_TUNING) |
| 480 | if (gDvmJit.profile) { |
| 481 | BasicBlock *backwardCell = |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 482 | dvmCompilerNewBB(kChainingCellBackwardBranch); |
Bill Buzbee | 9c4b7c8 | 2009-09-10 10:10:38 -0700 | [diff] [blame] | 483 | lastBB->next = backwardCell; |
| 484 | lastBB = backwardCell; |
| 485 | |
| 486 | backwardCell->startOffset = startBB->startOffset; |
| 487 | backwardCell->id = numBlocks++; |
| 488 | loopBranch->fallThrough = backwardCell; |
| 489 | } else { |
| 490 | loopBranch->fallThrough = startBB->next; |
| 491 | } |
| 492 | #else |
| 493 | loopBranch->fallThrough = startBB->next; |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 494 | #endif |
| 495 | |
| 496 | /* Create the chaining cell as the fallthrough of the exit block */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 497 | exitChainingCell = dvmCompilerNewBB(kChainingCellNormal); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 498 | lastBB->next = exitChainingCell; |
| 499 | lastBB = exitChainingCell; |
| 500 | |
| 501 | exitChainingCell->startOffset = targetOffset; |
| 502 | exitChainingCell->id = numBlocks++; |
| 503 | |
| 504 | exitBB->fallThrough = exitChainingCell; |
| 505 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 506 | cUnit.hasLoop = true; |
| 507 | } |
| 508 | |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 509 | /* Fallthrough block not included in the trace */ |
| 510 | if (!isUnconditionalBranch(lastInsn) && curBB->fallThrough == NULL) { |
| 511 | /* |
| 512 | * If the chaining cell is after an invoke or |
| 513 | * instruction that cannot change the control flow, request a hot |
| 514 | * chaining cell. |
| 515 | */ |
| 516 | if (isInvoke || curBB->needFallThroughBranch) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 517 | lastBB->next = dvmCompilerNewBB(kChainingCellHot); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 518 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 519 | lastBB->next = dvmCompilerNewBB(kChainingCellNormal); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 520 | } |
| 521 | lastBB = lastBB->next; |
| 522 | lastBB->id = numBlocks++; |
| 523 | lastBB->startOffset = fallThroughOffset; |
| 524 | curBB->fallThrough = lastBB; |
| 525 | } |
| 526 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 527 | /* Target block not included in the trace */ |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 528 | if (curBB->taken == NULL && |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 529 | (isInvoke || (targetOffset != UNKNOWN_TARGET && |
| 530 | targetOffset != curOffset))) { |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 531 | BasicBlock *newBB; |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 532 | if (isInvoke) { |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 533 | /* Monomorphic callee */ |
| 534 | if (callee) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 535 | newBB = dvmCompilerNewBB(kChainingCellInvokeSingleton); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 536 | newBB->startOffset = 0; |
| 537 | newBB->containingMethod = callee; |
| 538 | /* Will resolve at runtime */ |
| 539 | } else { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 540 | newBB = dvmCompilerNewBB(kChainingCellInvokePredicted); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 541 | newBB->startOffset = 0; |
| 542 | } |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 543 | /* For unconditional branches, request a hot chaining cell */ |
| 544 | } else { |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 545 | #if !defined(WITH_SELF_VERIFICATION) |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 546 | newBB = dvmCompilerNewBB(flags & kInstrUnconditional ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 547 | kChainingCellHot : |
| 548 | kChainingCellNormal); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 549 | newBB->startOffset = targetOffset; |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 550 | #else |
| 551 | /* Handle branches that branch back into the block */ |
| 552 | if (targetOffset >= curBB->firstMIRInsn->offset && |
| 553 | targetOffset <= curBB->lastMIRInsn->offset) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 554 | newBB = dvmCompilerNewBB(kChainingCellBackwardBranch); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 555 | } else { |
| 556 | newBB = dvmCompilerNewBB(flags & kInstrUnconditional ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 557 | kChainingCellHot : |
| 558 | kChainingCellNormal); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 559 | } |
| 560 | newBB->startOffset = targetOffset; |
| 561 | #endif |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 562 | } |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 563 | newBB->id = numBlocks++; |
| 564 | curBB->taken = newBB; |
| 565 | lastBB->next = newBB; |
| 566 | lastBB = newBB; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 567 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* Now create a special block to host PC reconstruction code */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 571 | lastBB->next = dvmCompilerNewBB(kPCReconstruction); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 572 | lastBB = lastBB->next; |
| 573 | lastBB->id = numBlocks++; |
| 574 | |
| 575 | /* And one final block that publishes the PC and raise the exception */ |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 576 | lastBB->next = dvmCompilerNewBB(kExceptionHandling); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 577 | lastBB = lastBB->next; |
| 578 | lastBB->id = numBlocks++; |
| 579 | |
| 580 | if (cUnit.printMe) { |
| 581 | 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] | 582 | compilationId, |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 583 | (intptr_t) desc->method->insns, |
| 584 | desc->method->clazz->descriptor, |
| 585 | desc->method->name, |
| 586 | desc->trace[0].frag.startOffset, |
| 587 | traceSize, |
| 588 | dexCode->insnsSize, |
| 589 | numBlocks); |
| 590 | } |
| 591 | |
| 592 | BasicBlock **blockList; |
| 593 | |
| 594 | cUnit.method = desc->method; |
| 595 | cUnit.traceDesc = desc; |
| 596 | cUnit.numBlocks = numBlocks; |
| 597 | dvmInitGrowableList(&cUnit.pcReconstructionList, 8); |
| 598 | blockList = cUnit.blockList = |
| 599 | dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true); |
| 600 | |
| 601 | int i; |
| 602 | |
| 603 | for (i = 0, curBB = startBB; i < numBlocks; i++) { |
| 604 | blockList[i] = curBB; |
| 605 | curBB = curBB->next; |
| 606 | } |
| 607 | /* Make sure all blocks are added to the cUnit */ |
| 608 | assert(curBB == NULL); |
| 609 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 610 | /* Preparation for SSA conversion */ |
| 611 | dvmInitializeSSAConversion(&cUnit); |
| 612 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 613 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 614 | if (cUnit.hasLoop) { |
| 615 | dvmCompilerLoopOpt(&cUnit); |
| 616 | } |
| 617 | else { |
| 618 | dvmCompilerNonLoopAnalysis(&cUnit); |
| 619 | } |
| 620 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 621 | dvmCompilerInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming |
| 622 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 623 | if (cUnit.printMe) { |
| 624 | dvmCompilerDumpCompilationUnit(&cUnit); |
| 625 | } |
| 626 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 627 | /* Set the instruction set to use (NOTE: later components may change it) */ |
| 628 | cUnit.instructionSet = dvmCompilerInstructionSet(&cUnit); |
| 629 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 630 | /* Allocate Registers */ |
| 631 | dvmCompilerRegAlloc(&cUnit); |
| 632 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 633 | /* Convert MIR to LIR, etc. */ |
| 634 | dvmCompilerMIR2LIR(&cUnit); |
| 635 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 636 | /* Convert LIR into machine code. */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 637 | dvmCompilerAssembleLIR(&cUnit, info); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 638 | |
| 639 | if (cUnit.printMe) { |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 640 | if (cUnit.halveInstCount) { |
| 641 | LOGD("Assembler aborted"); |
| 642 | } else { |
| 643 | dvmCompilerCodegenDump(&cUnit); |
| 644 | } |
| 645 | LOGD("End %s%s, %d Dalvik instructions", |
| 646 | desc->method->clazz->descriptor, desc->method->name, |
| 647 | cUnit.numInsts); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* Reset the compiler resource pool */ |
| 651 | dvmCompilerArenaReset(); |
| 652 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 653 | /* Success */ |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 654 | if (!cUnit.halveInstCount) { |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 655 | methodStats->nativeSize += cUnit.totalSize; |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 656 | return info->codeAddress != NULL; |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 657 | |
| 658 | /* Halve the instruction count and retry again */ |
| 659 | } else { |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 660 | return dvmCompileTrace(desc, cUnit.numInsts / 2, info); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 661 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Similar to dvmCompileTrace, but the entity processed here is the whole |
| 666 | * method. |
| 667 | * |
| 668 | * TODO: implementation will be revisited when the trace builder can provide |
| 669 | * whole-method traces. |
| 670 | */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 671 | bool dvmCompileMethod(const Method *method, JitTranslationInfo *info) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 672 | { |
| 673 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 674 | const u2 *codePtr = dexCode->insns; |
| 675 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
| 676 | int blockID = 0; |
| 677 | unsigned int curOffset = 0; |
| 678 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 679 | BasicBlock *firstBlock = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 680 | firstBlock->id = blockID++; |
| 681 | |
| 682 | /* Allocate the bit-vector to track the beginning of basic blocks */ |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 683 | BitVector *bbStartAddr = dvmCompilerAllocBitVector(dexCode->insnsSize+1, |
| 684 | false); |
| 685 | dvmCompilerSetBit(bbStartAddr, 0); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 686 | |
| 687 | /* |
| 688 | * Sequentially go through every instruction first and put them in a single |
| 689 | * basic block. Identify block boundaries at the mean time. |
| 690 | */ |
| 691 | while (codePtr < codeEnd) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 692 | MIR *insn = dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 693 | insn->offset = curOffset; |
| 694 | int width = parseInsn(codePtr, &insn->dalvikInsn, false); |
| 695 | bool isInvoke = false; |
| 696 | const Method *callee; |
| 697 | insn->width = width; |
| 698 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 699 | /* Terminate when the data section is seen */ |
| 700 | if (width == 0) |
| 701 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 702 | dvmCompilerAppendMIR(firstBlock, insn); |
| 703 | /* |
| 704 | * Check whether this is a block ending instruction and whether it |
| 705 | * suggests the start of a new block |
| 706 | */ |
| 707 | unsigned int target = curOffset; |
| 708 | |
| 709 | /* |
| 710 | * If findBlockBoundary returns true, it means the current instruction |
| 711 | * is terminating the current block. If it is a branch, the target |
| 712 | * address will be recorded in target. |
| 713 | */ |
| 714 | if (findBlockBoundary(method, insn, curOffset, &target, &isInvoke, |
| 715 | &callee)) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 716 | dvmCompilerSetBit(bbStartAddr, curOffset + width); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 717 | if (target != curOffset) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 718 | dvmCompilerSetBit(bbStartAddr, target); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | codePtr += width; |
| 723 | /* each bit represents 16-bit quantity */ |
| 724 | curOffset += width; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * The number of blocks will be equal to the number of bits set to 1 in the |
| 729 | * bit vector minus 1, because the bit representing the location after the |
| 730 | * last instruction is set to one. |
| 731 | */ |
| 732 | int numBlocks = dvmCountSetBits(bbStartAddr); |
| 733 | if (dvmIsBitSet(bbStartAddr, dexCode->insnsSize)) { |
| 734 | numBlocks--; |
| 735 | } |
| 736 | |
| 737 | CompilationUnit cUnit; |
| 738 | BasicBlock **blockList; |
| 739 | |
| 740 | memset(&cUnit, 0, sizeof(CompilationUnit)); |
| 741 | cUnit.method = method; |
| 742 | blockList = cUnit.blockList = |
| 743 | dvmCompilerNew(sizeof(BasicBlock *) * numBlocks, true); |
| 744 | |
| 745 | /* |
| 746 | * Register the first block onto the list and start split it into block |
| 747 | * boundaries from there. |
| 748 | */ |
| 749 | blockList[0] = firstBlock; |
| 750 | cUnit.numBlocks = 1; |
| 751 | |
| 752 | int i; |
| 753 | for (i = 0; i < numBlocks; i++) { |
| 754 | MIR *insn; |
| 755 | BasicBlock *curBB = blockList[i]; |
| 756 | curOffset = curBB->lastMIRInsn->offset; |
| 757 | |
| 758 | for (insn = curBB->firstMIRInsn->next; insn; insn = insn->next) { |
| 759 | /* Found the beginning of a new block, see if it is created yet */ |
| 760 | if (dvmIsBitSet(bbStartAddr, insn->offset)) { |
| 761 | int j; |
| 762 | for (j = 0; j < cUnit.numBlocks; j++) { |
| 763 | if (blockList[j]->firstMIRInsn->offset == insn->offset) |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | /* Block not split yet - do it now */ |
| 768 | if (j == cUnit.numBlocks) { |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame^] | 769 | BasicBlock *newBB = dvmCompilerNewBB(kDalvikByteCode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 770 | newBB->id = blockID++; |
| 771 | newBB->firstMIRInsn = insn; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 772 | newBB->startOffset = insn->offset; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 773 | newBB->lastMIRInsn = curBB->lastMIRInsn; |
| 774 | curBB->lastMIRInsn = insn->prev; |
| 775 | insn->prev->next = NULL; |
| 776 | insn->prev = NULL; |
| 777 | |
| 778 | /* |
| 779 | * If the insn is not an unconditional branch, set up the |
| 780 | * fallthrough link. |
| 781 | */ |
| 782 | if (!isUnconditionalBranch(curBB->lastMIRInsn)) { |
| 783 | curBB->fallThrough = newBB; |
| 784 | } |
| 785 | |
| 786 | /* enqueue the new block */ |
| 787 | blockList[cUnit.numBlocks++] = newBB; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (numBlocks != cUnit.numBlocks) { |
| 795 | LOGE("Expect %d vs %d basic blocks\n", numBlocks, cUnit.numBlocks); |
| 796 | dvmAbort(); |
| 797 | } |
| 798 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 799 | /* Connect the basic blocks through the taken links */ |
| 800 | for (i = 0; i < numBlocks; i++) { |
| 801 | BasicBlock *curBB = blockList[i]; |
| 802 | MIR *insn = curBB->lastMIRInsn; |
| 803 | unsigned int target = insn->offset; |
| 804 | bool isInvoke; |
| 805 | const Method *callee; |
| 806 | |
| 807 | findBlockBoundary(method, insn, target, &target, &isInvoke, &callee); |
| 808 | |
| 809 | /* Found a block ended on a branch */ |
| 810 | if (target != insn->offset) { |
| 811 | int j; |
| 812 | /* Forward branch */ |
| 813 | if (target > insn->offset) { |
| 814 | j = i + 1; |
| 815 | } else { |
| 816 | /* Backward branch */ |
| 817 | j = 0; |
| 818 | } |
| 819 | for (; j < numBlocks; j++) { |
| 820 | if (blockList[j]->firstMIRInsn->offset == target) { |
| 821 | curBB->taken = blockList[j]; |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 826 | /* Don't create dummy block for the callee yet */ |
| 827 | if (j == numBlocks && !isInvoke) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 828 | LOGE("Target not found for insn %x: expect target %x\n", |
| 829 | curBB->lastMIRInsn->offset, target); |
| 830 | dvmAbort(); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 835 | /* Set the instruction set to use (NOTE: later components may change it) */ |
| 836 | cUnit.instructionSet = dvmCompilerInstructionSet(&cUnit); |
| 837 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 838 | dvmCompilerMIR2LIR(&cUnit); |
| 839 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 840 | dvmCompilerAssembleLIR(&cUnit, info); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 841 | |
| 842 | dvmCompilerDumpCompilationUnit(&cUnit); |
| 843 | |
| 844 | dvmCompilerArenaReset(); |
| 845 | |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 846 | return info->codeAddress != NULL; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 847 | } |