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