Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Dalvik.h" |
Dan Bornstein | df4daaf | 2010-12-01 14:23:44 -0800 | [diff] [blame] | 18 | #include "libdex/DexOpcodes.h" |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 19 | #include "libdex/DexCatch.h" |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 20 | #include "interp/Jit.h" |
| 21 | #include "CompilerInternals.h" |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 22 | #include "Dataflow.h" |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 23 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 24 | static inline bool contentIsInsn(const u2 *codePtr) { |
| 25 | u2 instr = *codePtr; |
| 26 | Opcode opcode = instr & 0xff; |
| 27 | |
| 28 | /* |
| 29 | * Since the low 8-bit in metadata may look like OP_NOP, we need to check |
| 30 | * both the low and whole sub-word to determine whether it is code or data. |
| 31 | */ |
| 32 | return (opcode != OP_NOP || instr == 0); |
| 33 | } |
| 34 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Parse an instruction, return the length of the instruction |
| 37 | */ |
| 38 | static inline int parseInsn(const u2 *codePtr, DecodedInstruction *decInsn, |
| 39 | bool printMe) |
| 40 | { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 41 | // Don't parse instruction data |
| 42 | if (!contentIsInsn(codePtr)) { |
| 43 | return 0; |
| 44 | } |
| 45 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 46 | u2 instr = *codePtr; |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 47 | Opcode opcode = dexOpcodeFromCodeUnit(instr); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 48 | |
Dan Bornstein | 5432239 | 2010-11-17 14:16:56 -0800 | [diff] [blame] | 49 | dexDecodeInstruction(codePtr, decInsn); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 50 | if (printMe) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 51 | char *decodedString = dvmCompilerGetDalvikDisassembly(decInsn, NULL); |
Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 52 | LOGD("%p: %#06x %s\n", codePtr, opcode, decodedString); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 53 | } |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 54 | return dexGetWidthFromOpcode(opcode); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 57 | #define UNKNOWN_TARGET 0xffffffff |
| 58 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Identify block-ending instructions and collect supplemental information |
| 61 | * regarding the following instructions. |
| 62 | */ |
| 63 | static inline bool findBlockBoundary(const Method *caller, MIR *insn, |
| 64 | unsigned int curOffset, |
| 65 | unsigned int *target, bool *isInvoke, |
| 66 | const Method **callee) |
| 67 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 68 | switch (insn->dalvikInsn.opcode) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 69 | /* Target is not compile-time constant */ |
| 70 | case OP_RETURN_VOID: |
| 71 | case OP_RETURN: |
| 72 | case OP_RETURN_WIDE: |
| 73 | case OP_RETURN_OBJECT: |
| 74 | case OP_THROW: |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 75 | *target = UNKNOWN_TARGET; |
| 76 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 77 | case OP_INVOKE_VIRTUAL: |
| 78 | case OP_INVOKE_VIRTUAL_RANGE: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 79 | case OP_INVOKE_VIRTUAL_JUMBO: |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 80 | case OP_INVOKE_INTERFACE: |
| 81 | case OP_INVOKE_INTERFACE_RANGE: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 82 | case OP_INVOKE_INTERFACE_JUMBO: |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 83 | case OP_INVOKE_VIRTUAL_QUICK: |
| 84 | case OP_INVOKE_VIRTUAL_QUICK_RANGE: |
| 85 | *isInvoke = true; |
| 86 | break; |
| 87 | case OP_INVOKE_SUPER: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 88 | case OP_INVOKE_SUPER_RANGE: |
| 89 | case OP_INVOKE_SUPER_JUMBO: { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 90 | int mIndex = caller->clazz->pDvmDex-> |
| 91 | pResMethods[insn->dalvikInsn.vB]->methodIndex; |
| 92 | const Method *calleeMethod = |
| 93 | caller->clazz->super->vtable[mIndex]; |
| 94 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 95 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 96 | *target = (unsigned int) calleeMethod->insns; |
| 97 | } |
| 98 | *isInvoke = true; |
| 99 | *callee = calleeMethod; |
| 100 | break; |
| 101 | } |
| 102 | case OP_INVOKE_STATIC: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 103 | case OP_INVOKE_STATIC_RANGE: |
| 104 | case OP_INVOKE_STATIC_JUMBO: { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 105 | const Method *calleeMethod = |
| 106 | caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB]; |
| 107 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 108 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 109 | *target = (unsigned int) calleeMethod->insns; |
| 110 | } |
| 111 | *isInvoke = true; |
| 112 | *callee = calleeMethod; |
| 113 | break; |
| 114 | } |
| 115 | case OP_INVOKE_SUPER_QUICK: |
| 116 | case OP_INVOKE_SUPER_QUICK_RANGE: { |
| 117 | const Method *calleeMethod = |
| 118 | caller->clazz->super->vtable[insn->dalvikInsn.vB]; |
| 119 | |
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_INVOKE_DIRECT: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 128 | case OP_INVOKE_DIRECT_RANGE: |
| 129 | case OP_INVOKE_DIRECT_JUMBO: { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 130 | const Method *calleeMethod = |
| 131 | caller->clazz->pDvmDex->pResMethods[insn->dalvikInsn.vB]; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 132 | if (calleeMethod && !dvmIsNativeMethod(calleeMethod)) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 133 | *target = (unsigned int) calleeMethod->insns; |
| 134 | } |
| 135 | *isInvoke = true; |
| 136 | *callee = calleeMethod; |
| 137 | break; |
| 138 | } |
| 139 | case OP_GOTO: |
| 140 | case OP_GOTO_16: |
| 141 | case OP_GOTO_32: |
| 142 | *target = curOffset + (int) insn->dalvikInsn.vA; |
| 143 | break; |
| 144 | |
| 145 | case OP_IF_EQ: |
| 146 | case OP_IF_NE: |
| 147 | case OP_IF_LT: |
| 148 | case OP_IF_GE: |
| 149 | case OP_IF_GT: |
| 150 | case OP_IF_LE: |
| 151 | *target = curOffset + (int) insn->dalvikInsn.vC; |
| 152 | break; |
| 153 | |
| 154 | case OP_IF_EQZ: |
| 155 | case OP_IF_NEZ: |
| 156 | case OP_IF_LTZ: |
| 157 | case OP_IF_GEZ: |
| 158 | case OP_IF_GTZ: |
| 159 | case OP_IF_LEZ: |
| 160 | *target = curOffset + (int) insn->dalvikInsn.vB; |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | return false; |
Ben Cheng | 9c147b8 | 2009-10-07 16:41:46 -0700 | [diff] [blame] | 165 | } |
| 166 | return true; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 169 | static inline bool isGoto(MIR *insn) |
| 170 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 171 | switch (insn->dalvikInsn.opcode) { |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 172 | case OP_GOTO: |
| 173 | case OP_GOTO_16: |
| 174 | case OP_GOTO_32: |
| 175 | return true; |
| 176 | default: |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 181 | /* |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 182 | * Identify unconditional branch instructions |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 183 | */ |
| 184 | static inline bool isUnconditionalBranch(MIR *insn) |
| 185 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 186 | switch (insn->dalvikInsn.opcode) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 187 | case OP_RETURN_VOID: |
| 188 | case OP_RETURN: |
| 189 | case OP_RETURN_WIDE: |
| 190 | case OP_RETURN_OBJECT: |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 191 | return true; |
| 192 | default: |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 193 | return isGoto(insn); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 198 | * dvmHashTableLookup() callback |
| 199 | */ |
| 200 | static int compareMethod(const CompilerMethodStats *m1, |
| 201 | const CompilerMethodStats *m2) |
| 202 | { |
| 203 | return (int) m1->method - (int) m2->method; |
| 204 | } |
| 205 | |
| 206 | /* |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 207 | * Analyze the body of the method to collect high-level information regarding |
| 208 | * inlining: |
| 209 | * - is empty method? |
| 210 | * - is getter/setter? |
| 211 | * - can throw exception? |
| 212 | * |
| 213 | * Currently the inliner only handles getters and setters. When its capability |
| 214 | * becomes more sophisticated more information will be retrieved here. |
| 215 | */ |
| 216 | static int analyzeInlineTarget(DecodedInstruction *dalvikInsn, int attributes, |
| 217 | int offset) |
| 218 | { |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 219 | int flags = dexGetFlagsFromOpcode(dalvikInsn->opcode); |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 220 | int dalvikOpcode = dalvikInsn->opcode; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 221 | |
Dan Bornstein | 0759f52 | 2010-11-30 14:58:53 -0800 | [diff] [blame] | 222 | if (flags & kInstrInvoke) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 223 | attributes &= ~METHOD_IS_LEAF; |
| 224 | } |
| 225 | |
| 226 | if (!(flags & kInstrCanReturn)) { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 227 | if (!(dvmCompilerDataFlowAttributes[dalvikOpcode] & |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 228 | DF_IS_GETTER)) { |
| 229 | attributes &= ~METHOD_IS_GETTER; |
| 230 | } |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 231 | if (!(dvmCompilerDataFlowAttributes[dalvikOpcode] & |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 232 | DF_IS_SETTER)) { |
| 233 | attributes &= ~METHOD_IS_SETTER; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * The expected instruction sequence is setter will never return value and |
| 239 | * getter will also do. Clear the bits if the behavior is discovered |
| 240 | * otherwise. |
| 241 | */ |
| 242 | if (flags & kInstrCanReturn) { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 243 | if (dalvikOpcode == OP_RETURN_VOID) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 244 | attributes &= ~METHOD_IS_GETTER; |
| 245 | } |
| 246 | else { |
| 247 | attributes &= ~METHOD_IS_SETTER; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (flags & kInstrCanThrow) { |
| 252 | attributes &= ~METHOD_IS_THROW_FREE; |
| 253 | } |
| 254 | |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 255 | if (offset == 0 && dalvikOpcode == OP_RETURN_VOID) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 256 | attributes |= METHOD_IS_EMPTY; |
| 257 | } |
| 258 | |
Ben Cheng | 34dc796 | 2010-08-26 14:56:31 -0700 | [diff] [blame] | 259 | /* |
| 260 | * Check if this opcode is selected for single stepping. |
| 261 | * If so, don't inline the callee as there is no stack frame for the |
| 262 | * interpreter to single-step through the instruction. |
| 263 | */ |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 264 | if (SINGLE_STEP_OP(dalvikOpcode)) { |
Ben Cheng | 34dc796 | 2010-08-26 14:56:31 -0700 | [diff] [blame] | 265 | attributes &= ~(METHOD_IS_GETTER | METHOD_IS_SETTER); |
| 266 | } |
| 267 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 268 | return attributes; |
| 269 | } |
| 270 | |
| 271 | /* |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 272 | * Analyze each method whose traces are ever compiled. Collect a variety of |
| 273 | * statistics like the ratio of exercised vs overall code and code bloat |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 274 | * ratios. If isCallee is true, also analyze each instruction in more details |
| 275 | * to see if it is suitable for inlining. |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 276 | */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 277 | CompilerMethodStats *dvmCompilerAnalyzeMethodBody(const Method *method, |
| 278 | bool isCallee) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 279 | { |
| 280 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 281 | const u2 *codePtr = dexCode->insns; |
| 282 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
| 283 | int insnSize = 0; |
| 284 | int hashValue = dvmComputeUtf8Hash(method->name); |
| 285 | |
| 286 | CompilerMethodStats dummyMethodEntry; // For hash table lookup |
| 287 | CompilerMethodStats *realMethodEntry; // For hash table storage |
| 288 | |
| 289 | /* For lookup only */ |
| 290 | dummyMethodEntry.method = method; |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 291 | realMethodEntry = (CompilerMethodStats *) |
| 292 | dvmHashTableLookup(gDvmJit.methodStatsTable, |
| 293 | hashValue, |
| 294 | &dummyMethodEntry, |
| 295 | (HashCompareFunc) compareMethod, |
| 296 | false); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 297 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 298 | /* This method has never been analyzed before - create an entry */ |
| 299 | if (realMethodEntry == NULL) { |
| 300 | realMethodEntry = |
| 301 | (CompilerMethodStats *) calloc(1, sizeof(CompilerMethodStats)); |
| 302 | realMethodEntry->method = method; |
| 303 | |
| 304 | dvmHashTableLookup(gDvmJit.methodStatsTable, hashValue, |
| 305 | realMethodEntry, |
| 306 | (HashCompareFunc) compareMethod, |
| 307 | true); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 310 | /* This method is invoked as a callee and has been analyzed - just return */ |
| 311 | if ((isCallee == true) && (realMethodEntry->attributes & METHOD_IS_CALLEE)) |
| 312 | return realMethodEntry; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 313 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 314 | /* |
| 315 | * Similarly, return if this method has been compiled before as a hot |
| 316 | * method already. |
| 317 | */ |
| 318 | if ((isCallee == false) && |
| 319 | (realMethodEntry->attributes & METHOD_IS_HOT)) |
| 320 | return realMethodEntry; |
| 321 | |
| 322 | int attributes; |
| 323 | |
| 324 | /* Method hasn't been analyzed for the desired purpose yet */ |
| 325 | if (isCallee) { |
| 326 | /* Aggressively set the attributes until proven otherwise */ |
| 327 | attributes = METHOD_IS_LEAF | METHOD_IS_THROW_FREE | METHOD_IS_CALLEE | |
| 328 | METHOD_IS_GETTER | METHOD_IS_SETTER; |
| 329 | } else { |
| 330 | attributes = METHOD_IS_HOT; |
| 331 | } |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 332 | |
| 333 | /* Count the number of instructions */ |
| 334 | while (codePtr < codeEnd) { |
| 335 | DecodedInstruction dalvikInsn; |
| 336 | int width = parseInsn(codePtr, &dalvikInsn, false); |
| 337 | |
| 338 | /* Terminate when the data section is seen */ |
| 339 | if (width == 0) |
| 340 | break; |
| 341 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 342 | if (isCallee) { |
| 343 | attributes = analyzeInlineTarget(&dalvikInsn, attributes, insnSize); |
| 344 | } |
| 345 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 346 | insnSize += width; |
| 347 | codePtr += width; |
| 348 | } |
| 349 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 350 | /* |
| 351 | * Only handle simple getters/setters with one instruction followed by |
| 352 | * return |
| 353 | */ |
| 354 | if ((attributes & (METHOD_IS_GETTER | METHOD_IS_SETTER)) && |
| 355 | (insnSize != 3)) { |
| 356 | attributes &= ~(METHOD_IS_GETTER | METHOD_IS_SETTER); |
| 357 | } |
| 358 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 359 | realMethodEntry->dalvikSize = insnSize * 2; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 360 | realMethodEntry->attributes |= attributes; |
| 361 | |
| 362 | #if 0 |
| 363 | /* Uncomment the following to explore various callee patterns */ |
| 364 | if (attributes & METHOD_IS_THROW_FREE) { |
| 365 | LOGE("%s%s is inlinable%s", method->clazz->descriptor, method->name, |
| 366 | (attributes & METHOD_IS_EMPTY) ? " empty" : ""); |
| 367 | } |
| 368 | |
| 369 | if (attributes & METHOD_IS_LEAF) { |
| 370 | LOGE("%s%s is leaf %d%s", method->clazz->descriptor, method->name, |
| 371 | insnSize, insnSize < 5 ? " (small)" : ""); |
| 372 | } |
| 373 | |
| 374 | if (attributes & (METHOD_IS_GETTER | METHOD_IS_SETTER)) { |
| 375 | LOGE("%s%s is %s", method->clazz->descriptor, method->name, |
| 376 | attributes & METHOD_IS_GETTER ? "getter": "setter"); |
| 377 | } |
| 378 | if (attributes == |
| 379 | (METHOD_IS_LEAF | METHOD_IS_THROW_FREE | METHOD_IS_CALLEE)) { |
| 380 | LOGE("%s%s is inlinable non setter/getter", method->clazz->descriptor, |
| 381 | method->name); |
| 382 | } |
| 383 | #endif |
| 384 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 385 | return realMethodEntry; |
| 386 | } |
| 387 | |
| 388 | /* |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 389 | * Crawl the stack of the thread that requesed compilation to see if any of the |
| 390 | * ancestors are on the blacklist. |
| 391 | */ |
Andy McFadden | 953a0ed | 2010-09-17 15:48:38 -0700 | [diff] [blame] | 392 | static bool filterMethodByCallGraph(Thread *thread, const char *curMethodName) |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 393 | { |
| 394 | /* Crawl the Dalvik stack frames and compare the method name*/ |
| 395 | StackSaveArea *ssaPtr = ((StackSaveArea *) thread->curFrame) - 1; |
| 396 | while (ssaPtr != ((StackSaveArea *) NULL) - 1) { |
| 397 | const Method *method = ssaPtr->method; |
| 398 | if (method) { |
| 399 | int hashValue = dvmComputeUtf8Hash(method->name); |
| 400 | bool found = |
| 401 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 402 | (char *) method->name, |
| 403 | (HashCompareFunc) strcmp, false) != |
| 404 | NULL; |
| 405 | if (found) { |
| 406 | LOGD("Method %s (--> %s) found on the JIT %s list", |
| 407 | method->name, curMethodName, |
| 408 | gDvmJit.includeSelectedMethod ? "white" : "black"); |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | ssaPtr = ((StackSaveArea *) ssaPtr->prevFrame) - 1; |
| 414 | }; |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | /* |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 419 | * Main entry point to start trace compilation. Basic blocks are constructed |
| 420 | * first and they will be passed to the codegen routines to convert Dalvik |
| 421 | * bytecode into machine code. |
| 422 | */ |
Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 423 | bool dvmCompileTrace(JitTraceDescription *desc, int numMaxInsts, |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 424 | JitTranslationInfo *info, jmp_buf *bailPtr, |
| 425 | int optHints) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 426 | { |
| 427 | const DexCode *dexCode = dvmGetMethodCode(desc->method); |
| 428 | const JitTraceRun* currRun = &desc->trace[0]; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 429 | unsigned int curOffset = currRun->frag.startOffset; |
| 430 | unsigned int numInsts = currRun->frag.numInsts; |
| 431 | const u2 *codePtr = dexCode->insns + curOffset; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 432 | int traceSize = 0; // # of half-words |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 433 | const u2 *startCodePtr = codePtr; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 434 | BasicBlock *curBB, *entryCodeBB; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 435 | int numBlocks = 0; |
| 436 | static int compilationId; |
| 437 | CompilationUnit cUnit; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 438 | GrowableList *blockList; |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 439 | #if defined(WITH_JIT_TUNING) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 440 | CompilerMethodStats *methodStats; |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 441 | #endif |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 442 | |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 443 | /* If we've already compiled this trace, just return success */ |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 444 | if (dvmJitGetTraceAddr(startCodePtr) && !info->discardResult) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 445 | /* |
| 446 | * Make sure the codeAddress is NULL so that it won't clobber the |
| 447 | * existing entry. |
| 448 | */ |
| 449 | info->codeAddress = NULL; |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 450 | return true; |
| 451 | } |
| 452 | |
buzbee | 18fba34 | 2011-01-19 15:31:15 -0800 | [diff] [blame] | 453 | /* If the work order is stale, discard it */ |
| 454 | if (info->cacheVersion != gDvmJit.cacheVersion) { |
| 455 | return false; |
| 456 | } |
| 457 | |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 458 | compilationId++; |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 459 | memset(&cUnit, 0, sizeof(CompilationUnit)); |
| 460 | |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 461 | #if defined(WITH_JIT_TUNING) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 462 | /* Locate the entry to store compilation statistics for this method */ |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 463 | methodStats = dvmCompilerAnalyzeMethodBody(desc->method, false); |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 464 | #endif |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 465 | |
Bill Buzbee | fc519dc | 2010-03-06 23:30:57 -0800 | [diff] [blame] | 466 | /* Set the recover buffer pointer */ |
| 467 | cUnit.bailPtr = bailPtr; |
| 468 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 469 | /* Initialize the printMe flag */ |
| 470 | cUnit.printMe = gDvmJit.printMe; |
| 471 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 472 | /* Setup the method */ |
| 473 | cUnit.method = desc->method; |
| 474 | |
| 475 | /* Initialize the PC reconstruction list */ |
| 476 | dvmInitGrowableList(&cUnit.pcReconstructionList, 8); |
| 477 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 478 | /* Initialize the basic block list */ |
| 479 | blockList = &cUnit.blockList; |
| 480 | dvmInitGrowableList(blockList, 8); |
| 481 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 482 | /* Identify traces that we don't want to compile */ |
| 483 | if (gDvmJit.methodTable) { |
| 484 | int len = strlen(desc->method->clazz->descriptor) + |
| 485 | strlen(desc->method->name) + 1; |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 486 | char *fullSignature = (char *)dvmCompilerNew(len, true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 487 | strcpy(fullSignature, desc->method->clazz->descriptor); |
| 488 | strcat(fullSignature, desc->method->name); |
| 489 | |
| 490 | int hashValue = dvmComputeUtf8Hash(fullSignature); |
| 491 | |
| 492 | /* |
| 493 | * Doing three levels of screening to see whether we want to skip |
| 494 | * compiling this method |
| 495 | */ |
| 496 | |
| 497 | /* First, check the full "class;method" signature */ |
| 498 | bool methodFound = |
| 499 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 500 | fullSignature, (HashCompareFunc) strcmp, |
| 501 | false) != |
| 502 | NULL; |
| 503 | |
| 504 | /* Full signature not found - check the enclosing class */ |
| 505 | if (methodFound == false) { |
| 506 | int hashValue = dvmComputeUtf8Hash(desc->method->clazz->descriptor); |
| 507 | methodFound = |
| 508 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 509 | (char *) desc->method->clazz->descriptor, |
| 510 | (HashCompareFunc) strcmp, false) != |
| 511 | NULL; |
| 512 | /* Enclosing class not found - check the method name */ |
| 513 | if (methodFound == false) { |
| 514 | int hashValue = dvmComputeUtf8Hash(desc->method->name); |
| 515 | methodFound = |
| 516 | dvmHashTableLookup(gDvmJit.methodTable, hashValue, |
| 517 | (char *) desc->method->name, |
| 518 | (HashCompareFunc) strcmp, false) != |
| 519 | NULL; |
Ben Cheng | 3367245 | 2010-01-12 14:59:30 -0800 | [diff] [blame] | 520 | |
| 521 | /* |
| 522 | * Debug by call-graph is enabled. Check if the debug list |
| 523 | * covers any methods on the VM stack. |
| 524 | */ |
| 525 | if (methodFound == false && gDvmJit.checkCallGraph == true) { |
| 526 | methodFound = |
| 527 | filterMethodByCallGraph(info->requestingThread, |
| 528 | desc->method->name); |
| 529 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Under the following conditions, the trace will be *conservatively* |
| 535 | * compiled by only containing single-step instructions to and from the |
| 536 | * interpreter. |
| 537 | * 1) If includeSelectedMethod == false, the method matches the full or |
| 538 | * partial signature stored in the hash table. |
| 539 | * |
| 540 | * 2) If includeSelectedMethod == true, the method does not match the |
| 541 | * full and partial signature stored in the hash table. |
| 542 | */ |
| 543 | if (gDvmJit.includeSelectedMethod != methodFound) { |
| 544 | cUnit.allSingleStep = true; |
| 545 | } else { |
| 546 | /* Compile the trace as normal */ |
| 547 | |
| 548 | /* Print the method we cherry picked */ |
| 549 | if (gDvmJit.includeSelectedMethod == true) { |
| 550 | cUnit.printMe = true; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 555 | /* Allocate the entry block */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 556 | curBB = dvmCompilerNewBB(kTraceEntryBlock, numBlocks++); |
| 557 | dvmInsertGrowableList(blockList, (intptr_t) curBB); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 558 | curBB->startOffset = curOffset; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 559 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 560 | entryCodeBB = dvmCompilerNewBB(kDalvikByteCode, numBlocks++); |
| 561 | dvmInsertGrowableList(blockList, (intptr_t) entryCodeBB); |
| 562 | entryCodeBB->startOffset = curOffset; |
| 563 | curBB->fallThrough = entryCodeBB; |
| 564 | curBB = entryCodeBB; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 565 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 566 | if (cUnit.printMe) { |
| 567 | LOGD("--------\nCompiler: Building trace for %s, offset 0x%x\n", |
| 568 | desc->method->name, curOffset); |
| 569 | } |
| 570 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 571 | /* |
| 572 | * Analyze the trace descriptor and include up to the maximal number |
| 573 | * of Dalvik instructions into the IR. |
| 574 | */ |
| 575 | while (1) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 576 | MIR *insn; |
| 577 | int width; |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 578 | insn = (MIR *)dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 579 | insn->offset = curOffset; |
| 580 | width = parseInsn(codePtr, &insn->dalvikInsn, cUnit.printMe); |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 581 | |
| 582 | /* The trace should never incude instruction data */ |
| 583 | assert(width); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 584 | insn->width = width; |
| 585 | traceSize += width; |
| 586 | dvmCompilerAppendMIR(curBB, insn); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 587 | cUnit.numInsts++; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 588 | |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 589 | int flags = dexGetFlagsFromOpcode(insn->dalvikInsn.opcode); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 590 | |
Dan Bornstein | 0759f52 | 2010-11-30 14:58:53 -0800 | [diff] [blame] | 591 | if (flags & kInstrInvoke) { |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 592 | const Method *calleeMethod = (const Method *) currRun[2].meta; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 593 | assert(numInsts == 1); |
| 594 | CallsiteInfo *callsiteInfo = |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 595 | (CallsiteInfo *)dvmCompilerNew(sizeof(CallsiteInfo), true); |
| 596 | callsiteInfo->clazz = (ClassObject *)currRun[1].meta; |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 597 | callsiteInfo->method = calleeMethod; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 598 | insn->meta.callsiteInfo = callsiteInfo; |
| 599 | } |
| 600 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 601 | /* Instruction limit reached - terminate the trace here */ |
| 602 | if (cUnit.numInsts >= numMaxInsts) { |
| 603 | break; |
| 604 | } |
| 605 | if (--numInsts == 0) { |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 606 | if (currRun->frag.runEnd) { |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 607 | break; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 608 | } else { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 609 | /* Advance to the next trace description (ie non-meta info) */ |
| 610 | do { |
| 611 | currRun++; |
| 612 | } while (!currRun->frag.isCode); |
| 613 | |
| 614 | /* Dummy end-of-run marker seen */ |
| 615 | if (currRun->frag.numInsts == 0) { |
| 616 | break; |
| 617 | } |
| 618 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 619 | curBB = dvmCompilerNewBB(kDalvikByteCode, numBlocks++); |
| 620 | dvmInsertGrowableList(blockList, (intptr_t) curBB); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 621 | curOffset = currRun->frag.startOffset; |
| 622 | numInsts = currRun->frag.numInsts; |
| 623 | curBB->startOffset = curOffset; |
| 624 | codePtr = dexCode->insns + curOffset; |
| 625 | } |
| 626 | } else { |
| 627 | curOffset += width; |
| 628 | codePtr += width; |
| 629 | } |
| 630 | } |
| 631 | |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 632 | #if defined(WITH_JIT_TUNING) |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 633 | /* Convert # of half-word to bytes */ |
| 634 | methodStats->compiledDalvikSize += traceSize * 2; |
Ben Cheng | 1357e94 | 2010-02-10 17:21:39 -0800 | [diff] [blame] | 635 | #endif |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 636 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 637 | /* |
| 638 | * Now scan basic blocks containing real code to connect the |
| 639 | * taken/fallthrough links. Also create chaining cells for code not included |
| 640 | * in the trace. |
| 641 | */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 642 | size_t blockId; |
| 643 | for (blockId = 0; blockId < blockList->numUsed; blockId++) { |
| 644 | curBB = (BasicBlock *) dvmGrowableListGetElement(blockList, blockId); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 645 | MIR *lastInsn = curBB->lastMIRInsn; |
buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 646 | BasicBlock *backwardCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 647 | /* Skip empty blocks */ |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 648 | if (lastInsn == NULL) { |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 649 | continue; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 650 | } |
| 651 | curOffset = lastInsn->offset; |
| 652 | unsigned int targetOffset = curOffset; |
| 653 | unsigned int fallThroughOffset = curOffset + lastInsn->width; |
| 654 | bool isInvoke = false; |
| 655 | const Method *callee = NULL; |
| 656 | |
| 657 | findBlockBoundary(desc->method, curBB->lastMIRInsn, curOffset, |
| 658 | &targetOffset, &isInvoke, &callee); |
| 659 | |
| 660 | /* Link the taken and fallthrough blocks */ |
| 661 | BasicBlock *searchBB; |
| 662 | |
Dan Bornstein | e485276 | 2010-12-02 12:45:00 -0800 | [diff] [blame] | 663 | int flags = dexGetFlagsFromOpcode(lastInsn->dalvikInsn.opcode); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 664 | |
| 665 | if (flags & kInstrInvoke) { |
| 666 | cUnit.hasInvoke = true; |
| 667 | } |
| 668 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 669 | /* No backward branch in the trace - start searching the next BB */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 670 | size_t searchBlockId; |
| 671 | for (searchBlockId = blockId+1; searchBlockId < blockList->numUsed; |
| 672 | searchBlockId++) { |
| 673 | searchBB = (BasicBlock *) dvmGrowableListGetElement(blockList, |
| 674 | searchBlockId); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 675 | if (targetOffset == searchBB->startOffset) { |
| 676 | curBB->taken = searchBB; |
Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame^] | 677 | dvmCompilerSetBit(searchBB->predecessors, curBB->id); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 678 | } |
| 679 | if (fallThroughOffset == searchBB->startOffset) { |
| 680 | curBB->fallThrough = searchBB; |
Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame^] | 681 | dvmCompilerSetBit(searchBB->predecessors, curBB->id); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * Fallthrough block of an invoke instruction needs to be |
| 685 | * aligned to 4-byte boundary (alignment instruction to be |
| 686 | * inserted later. |
| 687 | */ |
| 688 | if (flags & kInstrInvoke) { |
| 689 | searchBB->isFallThroughFromInvoke = true; |
| 690 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 694 | /* |
| 695 | * Some blocks are ended by non-control-flow-change instructions, |
| 696 | * currently only due to trace length constraint. In this case we need |
| 697 | * to generate an explicit branch at the end of the block to jump to |
| 698 | * the chaining cell. |
| 699 | */ |
| 700 | curBB->needFallThroughBranch = |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 701 | ((flags & (kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn | |
Dan Bornstein | 0759f52 | 2010-11-30 14:58:53 -0800 | [diff] [blame] | 702 | kInstrInvoke)) == 0); |
Ben Cheng | 17f15ce | 2009-07-27 16:21:52 -0700 | [diff] [blame] | 703 | |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 704 | /* Only form a loop if JIT_OPT_NO_LOOP is not set */ |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 705 | if (curBB->taken == NULL && |
| 706 | curBB->fallThrough == NULL && |
| 707 | flags == (kInstrCanBranch | kInstrCanContinue) && |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 708 | fallThroughOffset == entryCodeBB->startOffset && |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 709 | JIT_OPT_NO_LOOP != (optHints & JIT_OPT_NO_LOOP)) { |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 710 | BasicBlock *loopBranch = curBB; |
| 711 | BasicBlock *exitBB; |
| 712 | BasicBlock *exitChainingCell; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 713 | |
| 714 | if (cUnit.printMe) { |
| 715 | LOGD("Natural loop detected!"); |
| 716 | } |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 717 | exitBB = dvmCompilerNewBB(kTraceExitBlock, numBlocks++); |
| 718 | dvmInsertGrowableList(blockList, (intptr_t) exitBB); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 719 | exitBB->startOffset = targetOffset; |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 720 | exitBB->needFallThroughBranch = true; |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 721 | |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 722 | loopBranch->taken = exitBB; |
Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame^] | 723 | dvmCompilerSetBit(exitBB->predecessors, loopBranch->id); |
buzbee | 2e152ba | 2010-12-15 16:32:35 -0800 | [diff] [blame] | 724 | backwardCell = |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 725 | dvmCompilerNewBB(kChainingCellBackwardBranch, numBlocks++); |
| 726 | dvmInsertGrowableList(blockList, (intptr_t) backwardCell); |
| 727 | backwardCell->startOffset = entryCodeBB->startOffset; |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 728 | loopBranch->fallThrough = backwardCell; |
Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame^] | 729 | dvmCompilerSetBit(backwardCell->predecessors, loopBranch->id); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 730 | |
| 731 | /* Create the chaining cell as the fallthrough of the exit block */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 732 | exitChainingCell = dvmCompilerNewBB(kChainingCellNormal, |
| 733 | numBlocks++); |
| 734 | dvmInsertGrowableList(blockList, (intptr_t) exitChainingCell); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 735 | exitChainingCell->startOffset = targetOffset; |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 736 | |
| 737 | exitBB->fallThrough = exitChainingCell; |
Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame^] | 738 | dvmCompilerSetBit(exitChainingCell->predecessors, exitBB->id); |
Ben Cheng | 0fd31e4 | 2009-09-03 14:40:16 -0700 | [diff] [blame] | 739 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 740 | cUnit.hasLoop = true; |
| 741 | } |
| 742 | |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 743 | if (lastInsn->dalvikInsn.opcode == OP_PACKED_SWITCH || |
| 744 | lastInsn->dalvikInsn.opcode == OP_SPARSE_SWITCH) { |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 745 | int i; |
| 746 | const u2 *switchData = desc->method->insns + lastInsn->offset + |
| 747 | lastInsn->dalvikInsn.vB; |
| 748 | int size = switchData[1]; |
| 749 | int maxChains = MIN(size, MAX_CHAINED_SWITCH_CASES); |
| 750 | |
| 751 | /* |
| 752 | * Generate the landing pad for cases whose ranks are higher than |
| 753 | * MAX_CHAINED_SWITCH_CASES. The code will re-enter the interpreter |
| 754 | * through the NoChain point. |
| 755 | */ |
| 756 | if (maxChains != size) { |
| 757 | cUnit.switchOverflowPad = |
| 758 | desc->method->insns + lastInsn->offset; |
| 759 | } |
| 760 | |
| 761 | s4 *targets = (s4 *) (switchData + 2 + |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 762 | (lastInsn->dalvikInsn.opcode == OP_PACKED_SWITCH ? |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 763 | 2 : size * 2)); |
| 764 | |
| 765 | /* One chaining cell for the first MAX_CHAINED_SWITCH_CASES cases */ |
| 766 | for (i = 0; i < maxChains; i++) { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 767 | BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal, |
| 768 | numBlocks++); |
| 769 | dvmInsertGrowableList(blockList, (intptr_t) caseChain); |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 770 | caseChain->startOffset = lastInsn->offset + targets[i]; |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* One more chaining cell for the default case */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 774 | BasicBlock *caseChain = dvmCompilerNewBB(kChainingCellNormal, |
| 775 | numBlocks++); |
| 776 | dvmInsertGrowableList(blockList, (intptr_t) caseChain); |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 777 | caseChain->startOffset = lastInsn->offset + lastInsn->width; |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 778 | /* Fallthrough block not included in the trace */ |
Ben Cheng | 6c10a97 | 2009-10-29 14:39:18 -0700 | [diff] [blame] | 779 | } else if (!isUnconditionalBranch(lastInsn) && |
| 780 | curBB->fallThrough == NULL) { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 781 | BasicBlock *fallThroughBB; |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 782 | /* |
| 783 | * If the chaining cell is after an invoke or |
| 784 | * instruction that cannot change the control flow, request a hot |
| 785 | * chaining cell. |
| 786 | */ |
| 787 | if (isInvoke || curBB->needFallThroughBranch) { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 788 | fallThroughBB = dvmCompilerNewBB(kChainingCellHot, numBlocks++); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 789 | } else { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 790 | fallThroughBB = dvmCompilerNewBB(kChainingCellNormal, |
| 791 | numBlocks++); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 792 | } |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 793 | dvmInsertGrowableList(blockList, (intptr_t) fallThroughBB); |
| 794 | fallThroughBB->startOffset = fallThroughOffset; |
| 795 | curBB->fallThrough = fallThroughBB; |
Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame^] | 796 | dvmCompilerSetBit(fallThroughBB->predecessors, curBB->id); |
Ben Cheng | 6d57609 | 2009-09-01 17:01:58 -0700 | [diff] [blame] | 797 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 798 | /* Target block not included in the trace */ |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 799 | if (curBB->taken == NULL && |
Bill Buzbee | 324b3ac | 2009-12-04 13:17:36 -0800 | [diff] [blame] | 800 | (isGoto(lastInsn) || isInvoke || |
| 801 | (targetOffset != UNKNOWN_TARGET && targetOffset != curOffset))) { |
Ben Cheng | 7e2c3c7 | 2010-12-22 16:40:46 -0800 | [diff] [blame] | 802 | BasicBlock *newBB = NULL; |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 803 | if (isInvoke) { |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 804 | /* Monomorphic callee */ |
| 805 | if (callee) { |
Ben Cheng | 7e2c3c7 | 2010-12-22 16:40:46 -0800 | [diff] [blame] | 806 | /* JNI call doesn't need a chaining cell */ |
| 807 | if (!dvmIsNativeMethod(callee)) { |
| 808 | newBB = dvmCompilerNewBB(kChainingCellInvokeSingleton, |
| 809 | numBlocks++); |
| 810 | newBB->startOffset = 0; |
| 811 | newBB->containingMethod = callee; |
| 812 | } |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 813 | /* Will resolve at runtime */ |
| 814 | } else { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 815 | newBB = dvmCompilerNewBB(kChainingCellInvokePredicted, |
| 816 | numBlocks++); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 817 | newBB->startOffset = 0; |
| 818 | } |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 819 | /* For unconditional branches, request a hot chaining cell */ |
| 820 | } else { |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 821 | #if !defined(WITH_SELF_VERIFICATION) |
Dan Bornstein | c2b486f | 2010-11-12 16:07:16 -0800 | [diff] [blame] | 822 | newBB = dvmCompilerNewBB(dexIsGoto(flags) ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 823 | kChainingCellHot : |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 824 | kChainingCellNormal, |
| 825 | numBlocks++); |
Ben Cheng | 38329f5 | 2009-07-07 14:19:20 -0700 | [diff] [blame] | 826 | newBB->startOffset = targetOffset; |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 827 | #else |
| 828 | /* Handle branches that branch back into the block */ |
| 829 | if (targetOffset >= curBB->firstMIRInsn->offset && |
| 830 | targetOffset <= curBB->lastMIRInsn->offset) { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 831 | newBB = dvmCompilerNewBB(kChainingCellBackwardBranch, |
| 832 | numBlocks++); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 833 | } else { |
Dan Bornstein | c2b486f | 2010-11-12 16:07:16 -0800 | [diff] [blame] | 834 | newBB = dvmCompilerNewBB(dexIsGoto(flags) ? |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 835 | kChainingCellHot : |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 836 | kChainingCellNormal, |
| 837 | numBlocks++); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 838 | } |
| 839 | newBB->startOffset = targetOffset; |
| 840 | #endif |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 841 | } |
Ben Cheng | 7e2c3c7 | 2010-12-22 16:40:46 -0800 | [diff] [blame] | 842 | if (newBB) { |
| 843 | curBB->taken = newBB; |
Ben Cheng | 7ab74e1 | 2011-02-03 14:02:06 -0800 | [diff] [blame^] | 844 | dvmCompilerSetBit(newBB->predecessors, curBB->id); |
Ben Cheng | 7e2c3c7 | 2010-12-22 16:40:46 -0800 | [diff] [blame] | 845 | dvmInsertGrowableList(blockList, (intptr_t) newBB); |
| 846 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 847 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /* Now create a special block to host PC reconstruction code */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 851 | curBB = dvmCompilerNewBB(kPCReconstruction, numBlocks++); |
| 852 | dvmInsertGrowableList(blockList, (intptr_t) curBB); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 853 | |
| 854 | /* And one final block that publishes the PC and raise the exception */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 855 | curBB = dvmCompilerNewBB(kExceptionHandling, numBlocks++); |
| 856 | dvmInsertGrowableList(blockList, (intptr_t) curBB); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 857 | |
| 858 | if (cUnit.printMe) { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 859 | char* signature = |
| 860 | dexProtoCopyMethodDescriptor(&desc->method->prototype); |
Elliott Hughes | cc6fac8 | 2010-07-02 13:38:44 -0700 | [diff] [blame] | 861 | LOGD("TRACEINFO (%d): 0x%08x %s%s.%s 0x%x %d of %d, %d blocks", |
Ben Cheng | e9695e5 | 2009-06-16 16:11:47 -0700 | [diff] [blame] | 862 | compilationId, |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 863 | (intptr_t) desc->method->insns, |
| 864 | desc->method->clazz->descriptor, |
| 865 | desc->method->name, |
Elliott Hughes | cc6fac8 | 2010-07-02 13:38:44 -0700 | [diff] [blame] | 866 | signature, |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 867 | desc->trace[0].frag.startOffset, |
| 868 | traceSize, |
| 869 | dexCode->insnsSize, |
| 870 | numBlocks); |
Elliott Hughes | cc6fac8 | 2010-07-02 13:38:44 -0700 | [diff] [blame] | 871 | free(signature); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 874 | cUnit.traceDesc = desc; |
| 875 | cUnit.numBlocks = numBlocks; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 876 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 877 | /* Set the instruction set to use (NOTE: later components may change it) */ |
| 878 | cUnit.instructionSet = dvmCompilerInstructionSet(); |
| 879 | |
| 880 | /* Inline transformation @ the MIR level */ |
| 881 | if (cUnit.hasInvoke && !(gDvmJit.disableOpt & (1 << kMethodInlining))) { |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 882 | dvmCompilerInlineMIR(&cUnit, info); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 885 | cUnit.numDalvikRegisters = cUnit.method->registersSize; |
| 886 | |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 887 | /* Preparation for SSA conversion */ |
| 888 | dvmInitializeSSAConversion(&cUnit); |
| 889 | |
| 890 | if (cUnit.hasLoop) { |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 891 | /* |
| 892 | * Loop is not optimizable (for example lack of a single induction |
| 893 | * variable), punt and recompile the trace with loop optimization |
| 894 | * disabled. |
| 895 | */ |
| 896 | bool loopOpt = dvmCompilerLoopOpt(&cUnit); |
| 897 | if (loopOpt == false) { |
| 898 | if (cUnit.printMe) { |
| 899 | LOGD("Loop is not optimizable - retry codegen"); |
| 900 | } |
| 901 | /* Reset the compiler resource pool */ |
| 902 | dvmCompilerArenaReset(); |
| 903 | return dvmCompileTrace(desc, cUnit.numInsts, info, bailPtr, |
| 904 | optHints | JIT_OPT_NO_LOOP); |
| 905 | } |
Ben Cheng | 4238ec2 | 2009-08-24 16:32:22 -0700 | [diff] [blame] | 906 | } |
| 907 | else { |
| 908 | dvmCompilerNonLoopAnalysis(&cUnit); |
| 909 | } |
| 910 | |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 911 | dvmCompilerInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming |
| 912 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 913 | if (cUnit.printMe) { |
| 914 | dvmCompilerDumpCompilationUnit(&cUnit); |
| 915 | } |
| 916 | |
buzbee | 23d95d0 | 2010-12-14 13:16:43 -0800 | [diff] [blame] | 917 | /* Allocate Registers using simple local allocation scheme */ |
| 918 | dvmCompilerLocalRegAlloc(&cUnit); |
Bill Buzbee | 1465db5 | 2009-09-23 17:17:35 -0700 | [diff] [blame] | 919 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 920 | /* Convert MIR to LIR, etc. */ |
| 921 | dvmCompilerMIR2LIR(&cUnit); |
| 922 | |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 923 | /* Convert LIR into machine code. Loop for recoverable retries */ |
| 924 | do { |
| 925 | dvmCompilerAssembleLIR(&cUnit, info); |
| 926 | cUnit.assemblerRetries++; |
| 927 | if (cUnit.printMe && cUnit.assemblerStatus != kSuccess) |
| 928 | LOGD("Assembler abort #%d on %d",cUnit.assemblerRetries, |
| 929 | cUnit.assemblerStatus); |
| 930 | } while (cUnit.assemblerStatus == kRetryAll); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 931 | |
| 932 | if (cUnit.printMe) { |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 933 | LOGD("Trace Dalvik PC: %p", startCodePtr); |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 934 | dvmCompilerCodegenDump(&cUnit); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 935 | LOGD("End %s%s, %d Dalvik instructions", |
| 936 | desc->method->clazz->descriptor, desc->method->name, |
| 937 | cUnit.numInsts); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /* Reset the compiler resource pool */ |
| 941 | dvmCompilerArenaReset(); |
| 942 | |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 943 | if (cUnit.assemblerStatus == kRetryHalve) { |
| 944 | /* Halve the instruction count and start from the top */ |
Ben Cheng | 4a41958 | 2010-08-04 13:23:09 -0700 | [diff] [blame] | 945 | return dvmCompileTrace(desc, cUnit.numInsts / 2, info, bailPtr, |
| 946 | optHints); |
Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 947 | } |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 948 | |
| 949 | assert(cUnit.assemblerStatus == kSuccess); |
| 950 | #if defined(WITH_JIT_TUNING) |
| 951 | methodStats->nativeSize += cUnit.totalSize; |
| 952 | #endif |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 953 | |
buzbee | bff121a | 2010-08-04 15:25:06 -0700 | [diff] [blame] | 954 | return info->codeAddress != NULL; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /* |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 958 | * Since we are including instructions from possibly a cold method into the |
| 959 | * current trace, we need to make sure that all the associated information |
| 960 | * with the callee is properly initialized. If not, we punt on this inline |
| 961 | * target. |
| 962 | * |
Ben Cheng | 34dc796 | 2010-08-26 14:56:31 -0700 | [diff] [blame] | 963 | * TODO: volatile instructions will be handled later. |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 964 | */ |
| 965 | bool dvmCompilerCanIncludeThisInstruction(const Method *method, |
| 966 | const DecodedInstruction *insn) |
| 967 | { |
Dan Bornstein | 9a1f816 | 2010-12-01 17:02:26 -0800 | [diff] [blame] | 968 | switch (insn->opcode) { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 969 | case OP_NEW_INSTANCE: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 970 | case OP_NEW_INSTANCE_JUMBO: |
| 971 | case OP_CHECK_CAST: |
| 972 | case OP_CHECK_CAST_JUMBO: { |
Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 973 | ClassObject *classPtr = (ClassObject *)(void*) |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 974 | (method->clazz->pDvmDex->pResClasses[insn->vB]); |
| 975 | |
| 976 | /* Class hasn't been initialized yet */ |
| 977 | if (classPtr == NULL) { |
| 978 | return false; |
| 979 | } |
| 980 | return true; |
| 981 | } |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 982 | case OP_SGET: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 983 | case OP_SGET_JUMBO: |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 984 | case OP_SGET_WIDE: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 985 | case OP_SGET_WIDE_JUMBO: |
| 986 | case OP_SGET_OBJECT: |
| 987 | case OP_SGET_OBJECT_JUMBO: |
| 988 | case OP_SGET_BOOLEAN: |
| 989 | case OP_SGET_BOOLEAN_JUMBO: |
| 990 | case OP_SGET_BYTE: |
| 991 | case OP_SGET_BYTE_JUMBO: |
| 992 | case OP_SGET_CHAR: |
| 993 | case OP_SGET_CHAR_JUMBO: |
| 994 | case OP_SGET_SHORT: |
| 995 | case OP_SGET_SHORT_JUMBO: |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 996 | case OP_SPUT: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 997 | case OP_SPUT_JUMBO: |
| 998 | case OP_SPUT_WIDE: |
| 999 | case OP_SPUT_WIDE_JUMBO: |
| 1000 | case OP_SPUT_OBJECT: |
| 1001 | case OP_SPUT_OBJECT_JUMBO: |
| 1002 | case OP_SPUT_BOOLEAN: |
| 1003 | case OP_SPUT_BOOLEAN_JUMBO: |
| 1004 | case OP_SPUT_BYTE: |
| 1005 | case OP_SPUT_BYTE_JUMBO: |
| 1006 | case OP_SPUT_CHAR: |
| 1007 | case OP_SPUT_CHAR_JUMBO: |
| 1008 | case OP_SPUT_SHORT: |
| 1009 | case OP_SPUT_SHORT_JUMBO: { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1010 | void *fieldPtr = (void*) |
| 1011 | (method->clazz->pDvmDex->pResFields[insn->vB]); |
| 1012 | |
| 1013 | if (fieldPtr == NULL) { |
| 1014 | return false; |
| 1015 | } |
| 1016 | return true; |
| 1017 | } |
| 1018 | case OP_INVOKE_SUPER: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 1019 | case OP_INVOKE_SUPER_RANGE: |
| 1020 | case OP_INVOKE_SUPER_JUMBO: { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1021 | int mIndex = method->clazz->pDvmDex-> |
| 1022 | pResMethods[insn->vB]->methodIndex; |
| 1023 | const Method *calleeMethod = method->clazz->super->vtable[mIndex]; |
| 1024 | if (calleeMethod == NULL) { |
| 1025 | return false; |
| 1026 | } |
| 1027 | return true; |
| 1028 | } |
| 1029 | case OP_INVOKE_SUPER_QUICK: |
| 1030 | case OP_INVOKE_SUPER_QUICK_RANGE: { |
| 1031 | const Method *calleeMethod = method->clazz->super->vtable[insn->vB]; |
| 1032 | if (calleeMethod == NULL) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | return true; |
| 1036 | } |
| 1037 | case OP_INVOKE_STATIC: |
| 1038 | case OP_INVOKE_STATIC_RANGE: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 1039 | case OP_INVOKE_STATIC_JUMBO: |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1040 | case OP_INVOKE_DIRECT: |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 1041 | case OP_INVOKE_DIRECT_RANGE: |
| 1042 | case OP_INVOKE_DIRECT_JUMBO: { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1043 | const Method *calleeMethod = |
| 1044 | method->clazz->pDvmDex->pResMethods[insn->vB]; |
| 1045 | if (calleeMethod == NULL) { |
| 1046 | return false; |
| 1047 | } |
| 1048 | return true; |
| 1049 | } |
jeffhao | 71eee1f | 2011-01-04 14:18:54 -0800 | [diff] [blame] | 1050 | case OP_CONST_CLASS: |
| 1051 | case OP_CONST_CLASS_JUMBO: { |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1052 | void *classPtr = (void*) |
| 1053 | (method->clazz->pDvmDex->pResClasses[insn->vB]); |
| 1054 | |
| 1055 | if (classPtr == NULL) { |
| 1056 | return false; |
| 1057 | } |
| 1058 | return true; |
| 1059 | } |
| 1060 | case OP_CONST_STRING_JUMBO: |
| 1061 | case OP_CONST_STRING: { |
| 1062 | void *strPtr = (void*) |
| 1063 | (method->clazz->pDvmDex->pResStrings[insn->vB]); |
| 1064 | |
| 1065 | if (strPtr == NULL) { |
| 1066 | return false; |
| 1067 | } |
| 1068 | return true; |
| 1069 | } |
| 1070 | default: |
| 1071 | return true; |
| 1072 | } |
| 1073 | } |
| 1074 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1075 | /* Split an existing block from the specified code offset into two */ |
| 1076 | static BasicBlock *splitBlock(CompilationUnit *cUnit, |
| 1077 | unsigned int codeOffset, |
| 1078 | BasicBlock *origBlock) |
| 1079 | { |
| 1080 | MIR *insn = origBlock->firstMIRInsn; |
| 1081 | while (insn) { |
| 1082 | if (insn->offset == codeOffset) break; |
| 1083 | insn = insn->next; |
| 1084 | } |
| 1085 | if (insn == NULL) { |
| 1086 | LOGE("Break split failed"); |
| 1087 | dvmAbort(); |
| 1088 | } |
| 1089 | BasicBlock *bottomBlock = dvmCompilerNewBB(kDalvikByteCode, |
| 1090 | cUnit->numBlocks++); |
| 1091 | dvmInsertGrowableList(&cUnit->blockList, (intptr_t) bottomBlock); |
| 1092 | |
| 1093 | bottomBlock->startOffset = codeOffset; |
| 1094 | bottomBlock->firstMIRInsn = insn; |
| 1095 | bottomBlock->lastMIRInsn = origBlock->lastMIRInsn; |
| 1096 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1097 | /* Handle the taken path */ |
| 1098 | bottomBlock->taken = origBlock->taken; |
| 1099 | if (bottomBlock->taken) { |
| 1100 | origBlock->taken = NULL; |
| 1101 | dvmCompilerClearBit(bottomBlock->taken->predecessors, origBlock->id); |
| 1102 | dvmCompilerSetBit(bottomBlock->taken->predecessors, bottomBlock->id); |
| 1103 | } |
| 1104 | |
| 1105 | /* Handle the fallthrough path */ |
| 1106 | bottomBlock->fallThrough = origBlock->fallThrough; |
| 1107 | origBlock->fallThrough = bottomBlock; |
| 1108 | dvmCompilerSetBit(bottomBlock->predecessors, origBlock->id); |
| 1109 | if (bottomBlock->fallThrough) { |
| 1110 | dvmCompilerClearBit(bottomBlock->fallThrough->predecessors, |
| 1111 | origBlock->id); |
| 1112 | dvmCompilerSetBit(bottomBlock->fallThrough->predecessors, |
| 1113 | bottomBlock->id); |
| 1114 | } |
| 1115 | |
| 1116 | /* Handle the successor list */ |
| 1117 | if (origBlock->successorBlockList.blockListType != kNotUsed) { |
| 1118 | bottomBlock->successorBlockList = origBlock->successorBlockList; |
| 1119 | origBlock->successorBlockList.blockListType = kNotUsed; |
| 1120 | GrowableListIterator iterator; |
| 1121 | |
| 1122 | dvmGrowableListIteratorInit(&bottomBlock->successorBlockList.blocks, |
| 1123 | &iterator); |
| 1124 | while (true) { |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1125 | SuccessorBlockInfo *successorBlockInfo = |
| 1126 | (SuccessorBlockInfo *) dvmGrowableListIteratorNext(&iterator); |
| 1127 | if (successorBlockInfo == NULL) break; |
| 1128 | BasicBlock *bb = successorBlockInfo->block; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1129 | dvmCompilerClearBit(bb->predecessors, origBlock->id); |
| 1130 | dvmCompilerSetBit(bb->predecessors, bottomBlock->id); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | origBlock->lastMIRInsn = insn->prev; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1135 | |
| 1136 | insn->prev->next = NULL; |
| 1137 | insn->prev = NULL; |
| 1138 | return bottomBlock; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * Given a code offset, find out the block that starts with it. If the offset |
| 1143 | * is in the middle of an existing block, split it into two. |
| 1144 | */ |
| 1145 | static BasicBlock *findBlock(CompilationUnit *cUnit, |
| 1146 | unsigned int codeOffset, |
| 1147 | bool split, bool create) |
| 1148 | { |
| 1149 | GrowableList *blockList = &cUnit->blockList; |
| 1150 | BasicBlock *bb; |
| 1151 | unsigned int i; |
| 1152 | |
| 1153 | for (i = 0; i < blockList->numUsed; i++) { |
| 1154 | bb = (BasicBlock *) blockList->elemList[i]; |
| 1155 | if (bb->blockType != kDalvikByteCode) continue; |
| 1156 | if (bb->startOffset == codeOffset) return bb; |
| 1157 | /* Check if a branch jumps into the middle of an existing block */ |
| 1158 | if ((split == true) && (codeOffset > bb->startOffset) && |
| 1159 | (bb->lastMIRInsn != NULL) && |
| 1160 | (codeOffset <= bb->lastMIRInsn->offset)) { |
| 1161 | BasicBlock *newBB = splitBlock(cUnit, codeOffset, bb); |
| 1162 | return newBB; |
| 1163 | } |
| 1164 | } |
| 1165 | if (create) { |
| 1166 | bb = dvmCompilerNewBB(kDalvikByteCode, cUnit->numBlocks++); |
| 1167 | dvmInsertGrowableList(&cUnit->blockList, (intptr_t) bb); |
| 1168 | bb->startOffset = codeOffset; |
| 1169 | return bb; |
| 1170 | } |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | |
| 1174 | /* Dump the CFG into a DOT graph */ |
| 1175 | void dumpCFG(CompilationUnit *cUnit, const char *dirPrefix) |
| 1176 | { |
| 1177 | const Method *method = cUnit->method; |
| 1178 | FILE *file; |
| 1179 | char* signature = dexProtoCopyMethodDescriptor(&method->prototype); |
| 1180 | char *fileName = (char *) dvmCompilerNew( |
| 1181 | strlen(dirPrefix) + |
| 1182 | strlen(method->clazz->descriptor) + |
| 1183 | strlen(method->name) + |
| 1184 | strlen(signature) + |
| 1185 | strlen(".dot") + 1, true); |
| 1186 | sprintf(fileName, "%s%s%s%s.dot", dirPrefix, |
| 1187 | method->clazz->descriptor, method->name, signature); |
| 1188 | free(signature); |
| 1189 | |
| 1190 | /* |
| 1191 | * Convert the special characters into a filesystem- and shell-friendly |
| 1192 | * format. |
| 1193 | */ |
| 1194 | int i; |
| 1195 | for (i = strlen(dirPrefix); fileName[i]; i++) { |
| 1196 | if (fileName[i] == '/') { |
| 1197 | fileName[i] = '_'; |
| 1198 | } else if (fileName[i] == ';') { |
| 1199 | fileName[i] = '#'; |
| 1200 | } else if (fileName[i] == '$') { |
| 1201 | fileName[i] = '+'; |
| 1202 | } else if (fileName[i] == '(' || fileName[i] == ')') { |
| 1203 | fileName[i] = '@'; |
| 1204 | } else if (fileName[i] == '<' || fileName[i] == '>') { |
| 1205 | fileName[i] = '='; |
| 1206 | } |
| 1207 | } |
| 1208 | file = fopen(fileName, "w"); |
| 1209 | if (file == NULL) { |
| 1210 | return; |
| 1211 | } |
| 1212 | fprintf(file, "digraph G {\n"); |
| 1213 | |
| 1214 | fprintf(file, " rankdir=TB\n"); |
| 1215 | |
| 1216 | int numReachableBlocks = cUnit->numReachableBlocks; |
| 1217 | int idx; |
| 1218 | const GrowableList *blockList = &cUnit->blockList; |
| 1219 | |
| 1220 | for (idx = 0; idx < numReachableBlocks; idx++) { |
| 1221 | int blockIdx = cUnit->dfsOrder.elemList[idx]; |
| 1222 | BasicBlock *bb = (BasicBlock *) dvmGrowableListGetElement(blockList, |
| 1223 | blockIdx); |
| 1224 | if (bb == NULL) break; |
| 1225 | if (bb->blockType == kMethodEntryBlock) { |
| 1226 | fprintf(file, " entry [shape=Mdiamond];\n"); |
| 1227 | } else if (bb->blockType == kMethodExitBlock) { |
| 1228 | fprintf(file, " exit [shape=Mdiamond];\n"); |
| 1229 | } else if (bb->blockType == kDalvikByteCode) { |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1230 | fprintf(file, " block%04x [shape=record,label = \"{ \\\n", |
| 1231 | bb->startOffset); |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1232 | const MIR *mir; |
| 1233 | for (mir = bb->firstMIRInsn; mir; mir = mir->next) { |
| 1234 | fprintf(file, " {%04x %s\\l}%s\\\n", mir->offset, |
| 1235 | dvmCompilerFullDisassembler(cUnit, mir), |
| 1236 | mir->next ? " | " : " "); |
| 1237 | } |
| 1238 | fprintf(file, " }\"];\n\n"); |
| 1239 | } else if (bb->blockType == kExceptionHandling) { |
| 1240 | char blockName[BLOCK_NAME_LEN]; |
| 1241 | |
| 1242 | dvmGetBlockName(bb, blockName); |
| 1243 | fprintf(file, " %s [shape=invhouse];\n", blockName); |
| 1244 | } |
| 1245 | |
| 1246 | char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN]; |
| 1247 | |
| 1248 | if (bb->taken) { |
| 1249 | dvmGetBlockName(bb, blockName1); |
| 1250 | dvmGetBlockName(bb->taken, blockName2); |
| 1251 | fprintf(file, " %s:s -> %s:n [style=dotted]\n", |
| 1252 | blockName1, blockName2); |
| 1253 | } |
| 1254 | if (bb->fallThrough) { |
| 1255 | dvmGetBlockName(bb, blockName1); |
| 1256 | dvmGetBlockName(bb->fallThrough, blockName2); |
| 1257 | fprintf(file, " %s:s -> %s:n\n", blockName1, blockName2); |
| 1258 | } |
| 1259 | |
| 1260 | if (bb->successorBlockList.blockListType != kNotUsed) { |
| 1261 | fprintf(file, " succ%04x [shape=%s,label = \"{ \\\n", |
| 1262 | bb->startOffset, |
| 1263 | (bb->successorBlockList.blockListType == kCatch) ? |
| 1264 | "Mrecord" : "record"); |
| 1265 | GrowableListIterator iterator; |
| 1266 | dvmGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 1267 | &iterator); |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1268 | SuccessorBlockInfo *successorBlockInfo = |
| 1269 | (SuccessorBlockInfo *) dvmGrowableListIteratorNext(&iterator); |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1270 | |
| 1271 | int succId = 0; |
| 1272 | while (true) { |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1273 | if (successorBlockInfo == NULL) break; |
| 1274 | |
| 1275 | BasicBlock *destBlock = successorBlockInfo->block; |
| 1276 | SuccessorBlockInfo *nextSuccessorBlockInfo = |
| 1277 | (SuccessorBlockInfo *) dvmGrowableListIteratorNext(&iterator); |
| 1278 | |
| 1279 | fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n", |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1280 | succId++, |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1281 | successorBlockInfo->key, |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1282 | destBlock->startOffset, |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1283 | (nextSuccessorBlockInfo != NULL) ? " | " : " "); |
| 1284 | |
| 1285 | successorBlockInfo = nextSuccessorBlockInfo; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1286 | } |
| 1287 | fprintf(file, " }\"];\n\n"); |
| 1288 | |
| 1289 | dvmGetBlockName(bb, blockName1); |
| 1290 | fprintf(file, " %s:s -> succ%04x:n [style=dashed]\n", |
| 1291 | blockName1, bb->startOffset); |
| 1292 | |
| 1293 | if (bb->successorBlockList.blockListType == kPackedSwitch || |
| 1294 | bb->successorBlockList.blockListType == kSparseSwitch) { |
| 1295 | |
| 1296 | dvmGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 1297 | &iterator); |
| 1298 | |
| 1299 | succId = 0; |
| 1300 | while (true) { |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1301 | SuccessorBlockInfo *successorBlockInfo = |
| 1302 | (SuccessorBlockInfo *) |
| 1303 | dvmGrowableListIteratorNext(&iterator); |
| 1304 | if (successorBlockInfo == NULL) break; |
| 1305 | |
| 1306 | BasicBlock *destBlock = successorBlockInfo->block; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1307 | |
| 1308 | dvmGetBlockName(destBlock, blockName2); |
| 1309 | fprintf(file, " succ%04x:f%d:e -> %s:n\n", |
| 1310 | bb->startOffset, succId++, |
| 1311 | blockName2); |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | fprintf(file, "\n"); |
| 1316 | |
| 1317 | /* |
| 1318 | * If we need to debug the dominator tree, uncomment the following code |
| 1319 | */ |
| 1320 | #if 0 |
| 1321 | dvmGetBlockName(bb, blockName1); |
| 1322 | fprintf(file, " cfg%s [label=\"%s\", shape=none];\n", |
| 1323 | blockName1, blockName1); |
| 1324 | if (bb->iDom) { |
| 1325 | dvmGetBlockName(bb->iDom, blockName2); |
| 1326 | fprintf(file, " cfg%s:s -> cfg%s:n\n\n", |
| 1327 | blockName2, blockName1); |
| 1328 | } |
| 1329 | #endif |
| 1330 | } |
| 1331 | fprintf(file, "}\n"); |
| 1332 | fclose(file); |
| 1333 | } |
| 1334 | |
| 1335 | /* Verify if all the successor is connected with all the claimed predecessors */ |
| 1336 | static bool verifyPredInfo(CompilationUnit *cUnit, BasicBlock *bb) |
| 1337 | { |
| 1338 | BitVectorIterator bvIterator; |
| 1339 | |
| 1340 | dvmBitVectorIteratorInit(bb->predecessors, &bvIterator); |
| 1341 | while (true) { |
| 1342 | int blockIdx = dvmBitVectorIteratorNext(&bvIterator); |
| 1343 | if (blockIdx == -1) break; |
| 1344 | BasicBlock *predBB = (BasicBlock *) |
| 1345 | dvmGrowableListGetElement(&cUnit->blockList, blockIdx); |
| 1346 | bool found = false; |
| 1347 | if (predBB->taken == bb) { |
| 1348 | found = true; |
| 1349 | } else if (predBB->fallThrough == bb) { |
| 1350 | found = true; |
| 1351 | } else if (predBB->successorBlockList.blockListType != kNotUsed) { |
| 1352 | GrowableListIterator iterator; |
| 1353 | dvmGrowableListIteratorInit(&predBB->successorBlockList.blocks, |
| 1354 | &iterator); |
| 1355 | while (true) { |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1356 | SuccessorBlockInfo *successorBlockInfo = |
| 1357 | (SuccessorBlockInfo *) |
| 1358 | dvmGrowableListIteratorNext(&iterator); |
| 1359 | if (successorBlockInfo == NULL) break; |
| 1360 | BasicBlock *succBB = successorBlockInfo->block; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1361 | if (succBB == bb) { |
| 1362 | found = true; |
| 1363 | break; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | if (found == false) { |
| 1368 | char blockName1[BLOCK_NAME_LEN], blockName2[BLOCK_NAME_LEN]; |
| 1369 | dvmGetBlockName(bb, blockName1); |
| 1370 | dvmGetBlockName(predBB, blockName2); |
| 1371 | dumpCFG(cUnit, "/data/tombstones/"); |
| 1372 | LOGE("Successor %s not found from %s", |
| 1373 | blockName1, blockName2); |
| 1374 | dvmAbort(); |
| 1375 | } |
| 1376 | } |
| 1377 | return true; |
| 1378 | } |
| 1379 | |
| 1380 | /* Identify code range in try blocks and set up the empty catch blocks */ |
| 1381 | static void processTryCatchBlocks(CompilationUnit *cUnit) |
| 1382 | { |
| 1383 | const Method *meth = cUnit->method; |
| 1384 | const DexCode *pCode = dvmGetMethodCode(meth); |
| 1385 | int triesSize = pCode->triesSize; |
| 1386 | int i; |
| 1387 | int offset; |
| 1388 | |
| 1389 | if (triesSize == 0) { |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | const DexTry *pTries = dexGetTries(pCode); |
| 1394 | BitVector *tryBlockAddr = cUnit->tryBlockAddr; |
| 1395 | |
| 1396 | /* Mark all the insn offsets in Try blocks */ |
| 1397 | for (i = 0; i < triesSize; i++) { |
| 1398 | const DexTry* pTry = &pTries[i]; |
| 1399 | /* all in 16-bit units */ |
| 1400 | int startOffset = pTry->startAddr; |
| 1401 | int endOffset = startOffset + pTry->insnCount; |
| 1402 | |
| 1403 | for (offset = startOffset; offset < endOffset; offset++) { |
| 1404 | dvmCompilerSetBit(tryBlockAddr, offset); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | /* Iterate over each of the handlers to enqueue the empty Catch blocks */ |
| 1409 | offset = dexGetFirstHandlerOffset(pCode); |
| 1410 | int handlersSize = dexGetHandlersSize(pCode); |
| 1411 | |
| 1412 | for (i = 0; i < handlersSize; i++) { |
| 1413 | DexCatchIterator iterator; |
| 1414 | dexCatchIteratorInit(&iterator, pCode, offset); |
| 1415 | |
| 1416 | for (;;) { |
| 1417 | DexCatchHandler* handler = dexCatchIteratorNext(&iterator); |
| 1418 | |
| 1419 | if (handler == NULL) { |
| 1420 | break; |
| 1421 | } |
| 1422 | |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1423 | /* |
| 1424 | * Create dummy catch blocks first. Since these are created before |
| 1425 | * other blocks are processed, "split" is specified as false. |
| 1426 | */ |
| 1427 | findBlock(cUnit, handler->address, |
| 1428 | /* split */ |
| 1429 | false, |
| 1430 | /* create */ |
| 1431 | true); |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | offset = dexCatchIteratorGetEndOffset(&iterator, pCode); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* Process instructions with the kInstrCanBranch flag */ |
| 1439 | static void processCanBranch(CompilationUnit *cUnit, BasicBlock *curBlock, |
| 1440 | MIR *insn, int curOffset, int width, int flags, |
| 1441 | const u2* codePtr, const u2* codeEnd) |
| 1442 | { |
| 1443 | int target = curOffset; |
| 1444 | switch (insn->dalvikInsn.opcode) { |
| 1445 | case OP_GOTO: |
| 1446 | case OP_GOTO_16: |
| 1447 | case OP_GOTO_32: |
| 1448 | target += (int) insn->dalvikInsn.vA; |
| 1449 | break; |
| 1450 | case OP_IF_EQ: |
| 1451 | case OP_IF_NE: |
| 1452 | case OP_IF_LT: |
| 1453 | case OP_IF_GE: |
| 1454 | case OP_IF_GT: |
| 1455 | case OP_IF_LE: |
| 1456 | target += (int) insn->dalvikInsn.vC; |
| 1457 | break; |
| 1458 | case OP_IF_EQZ: |
| 1459 | case OP_IF_NEZ: |
| 1460 | case OP_IF_LTZ: |
| 1461 | case OP_IF_GEZ: |
| 1462 | case OP_IF_GTZ: |
| 1463 | case OP_IF_LEZ: |
| 1464 | target += (int) insn->dalvikInsn.vB; |
| 1465 | break; |
| 1466 | default: |
| 1467 | LOGE("Unexpected opcode(%d) with kInstrCanBranch set", |
| 1468 | insn->dalvikInsn.opcode); |
| 1469 | dvmAbort(); |
| 1470 | } |
| 1471 | BasicBlock *takenBlock = findBlock(cUnit, target, |
| 1472 | /* split */ |
| 1473 | true, |
| 1474 | /* create */ |
| 1475 | true); |
| 1476 | curBlock->taken = takenBlock; |
| 1477 | dvmCompilerSetBit(takenBlock->predecessors, curBlock->id); |
| 1478 | |
| 1479 | /* Always terminate the current block for conditional branches */ |
| 1480 | if (flags & kInstrCanContinue) { |
| 1481 | BasicBlock *fallthroughBlock = findBlock(cUnit, |
| 1482 | curOffset + width, |
| 1483 | /* split */ |
| 1484 | false, |
| 1485 | /* create */ |
| 1486 | true); |
| 1487 | curBlock->fallThrough = fallthroughBlock; |
| 1488 | dvmCompilerSetBit(fallthroughBlock->predecessors, curBlock->id); |
| 1489 | } else if (codePtr < codeEnd) { |
| 1490 | /* Create a fallthrough block for real instructions (incl. OP_NOP) */ |
| 1491 | if (contentIsInsn(codePtr)) { |
| 1492 | findBlock(cUnit, curOffset + width, |
| 1493 | /* split */ |
| 1494 | false, |
| 1495 | /* create */ |
| 1496 | true); |
| 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | /* Process instructions with the kInstrCanSwitch flag */ |
| 1502 | static void processCanSwitch(CompilationUnit *cUnit, BasicBlock *curBlock, |
| 1503 | MIR *insn, int curOffset, int width, int flags) |
| 1504 | { |
| 1505 | u2 *switchData= (u2 *) (cUnit->method->insns + curOffset + |
| 1506 | insn->dalvikInsn.vB); |
| 1507 | int size; |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1508 | int *keyTable; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1509 | int *targetTable; |
| 1510 | int i; |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1511 | int firstKey; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1512 | |
| 1513 | /* |
| 1514 | * Packed switch data format: |
| 1515 | * ushort ident = 0x0100 magic value |
| 1516 | * ushort size number of entries in the table |
| 1517 | * int first_key first (and lowest) switch case value |
| 1518 | * int targets[size] branch targets, relative to switch opcode |
| 1519 | * |
| 1520 | * Total size is (4+size*2) 16-bit code units. |
| 1521 | */ |
| 1522 | if (insn->dalvikInsn.opcode == OP_PACKED_SWITCH) { |
| 1523 | assert(switchData[0] == kPackedSwitchSignature); |
| 1524 | size = switchData[1]; |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1525 | firstKey = switchData[2] | (switchData[3] << 16); |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1526 | targetTable = (int *) &switchData[4]; |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1527 | keyTable = NULL; // Make the compiler happy |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1528 | /* |
| 1529 | * Sparse switch data format: |
| 1530 | * ushort ident = 0x0200 magic value |
| 1531 | * ushort size number of entries in the table; > 0 |
| 1532 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 1533 | * int targets[size] branch targets, relative to switch opcode |
| 1534 | * |
| 1535 | * Total size is (2+size*4) 16-bit code units. |
| 1536 | */ |
| 1537 | } else { |
| 1538 | assert(switchData[0] == kSparseSwitchSignature); |
| 1539 | size = switchData[1]; |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1540 | keyTable = (int *) &switchData[2]; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1541 | targetTable = (int *) &switchData[2 + size*2]; |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1542 | firstKey = 0; // To make the compiler happy |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | if (curBlock->successorBlockList.blockListType != kNotUsed) { |
| 1546 | LOGE("Successor block list already in use: %d", |
| 1547 | curBlock->successorBlockList.blockListType); |
| 1548 | dvmAbort(); |
| 1549 | } |
| 1550 | curBlock->successorBlockList.blockListType = |
| 1551 | (insn->dalvikInsn.opcode == OP_PACKED_SWITCH) ? |
| 1552 | kPackedSwitch : kSparseSwitch; |
| 1553 | dvmInitGrowableList(&curBlock->successorBlockList.blocks, size); |
| 1554 | |
| 1555 | for (i = 0; i < size; i++) { |
| 1556 | BasicBlock *caseBlock = findBlock(cUnit, curOffset + targetTable[i], |
| 1557 | /* split */ |
| 1558 | true, |
| 1559 | /* create */ |
| 1560 | true); |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1561 | SuccessorBlockInfo *successorBlockInfo = |
| 1562 | (SuccessorBlockInfo *) dvmCompilerNew(sizeof(SuccessorBlockInfo), |
| 1563 | false); |
| 1564 | successorBlockInfo->block = caseBlock; |
| 1565 | successorBlockInfo->key = (insn->dalvikInsn.opcode == OP_PACKED_SWITCH)? |
| 1566 | firstKey + i : keyTable[i]; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1567 | dvmInsertGrowableList(&curBlock->successorBlockList.blocks, |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1568 | (intptr_t) successorBlockInfo); |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1569 | dvmCompilerSetBit(caseBlock->predecessors, curBlock->id); |
| 1570 | } |
| 1571 | |
| 1572 | /* Fall-through case */ |
| 1573 | BasicBlock *fallthroughBlock = findBlock(cUnit, |
| 1574 | curOffset + width, |
| 1575 | /* split */ |
| 1576 | false, |
| 1577 | /* create */ |
| 1578 | true); |
| 1579 | curBlock->fallThrough = fallthroughBlock; |
| 1580 | dvmCompilerSetBit(fallthroughBlock->predecessors, curBlock->id); |
| 1581 | } |
| 1582 | |
| 1583 | /* Process instructions with the kInstrCanThrow flag */ |
| 1584 | static void processCanThrow(CompilationUnit *cUnit, BasicBlock *curBlock, |
| 1585 | MIR *insn, int curOffset, int width, int flags, |
| 1586 | BitVector *tryBlockAddr, const u2 *codePtr, |
| 1587 | const u2* codeEnd) |
| 1588 | { |
| 1589 | const Method *method = cUnit->method; |
| 1590 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 1591 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1592 | /* In try block */ |
| 1593 | if (dvmIsBitSet(tryBlockAddr, curOffset)) { |
| 1594 | DexCatchIterator iterator; |
| 1595 | |
| 1596 | if (!dexFindCatchHandler(&iterator, dexCode, curOffset)) { |
| 1597 | LOGE("Catch block not found in dexfile for insn %x in %s", |
| 1598 | curOffset, method->name); |
| 1599 | dvmAbort(); |
| 1600 | |
| 1601 | } |
| 1602 | if (curBlock->successorBlockList.blockListType != kNotUsed) { |
| 1603 | LOGE("Successor block list already in use: %d", |
| 1604 | curBlock->successorBlockList.blockListType); |
| 1605 | dvmAbort(); |
| 1606 | } |
| 1607 | curBlock->successorBlockList.blockListType = kCatch; |
| 1608 | dvmInitGrowableList(&curBlock->successorBlockList.blocks, 2); |
| 1609 | |
| 1610 | for (;;) { |
| 1611 | DexCatchHandler* handler = dexCatchIteratorNext(&iterator); |
| 1612 | |
| 1613 | if (handler == NULL) { |
| 1614 | break; |
| 1615 | } |
| 1616 | |
| 1617 | BasicBlock *catchBlock = findBlock(cUnit, handler->address, |
| 1618 | /* split */ |
| 1619 | false, |
| 1620 | /* create */ |
| 1621 | false); |
| 1622 | |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1623 | SuccessorBlockInfo *successorBlockInfo = |
| 1624 | (SuccessorBlockInfo *) dvmCompilerNew(sizeof(SuccessorBlockInfo), |
| 1625 | false); |
| 1626 | successorBlockInfo->block = catchBlock; |
| 1627 | successorBlockInfo->key = handler->typeIdx; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1628 | dvmInsertGrowableList(&curBlock->successorBlockList.blocks, |
Ben Cheng | 7a02cb1 | 2010-12-15 14:18:31 -0800 | [diff] [blame] | 1629 | (intptr_t) successorBlockInfo); |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1630 | dvmCompilerSetBit(catchBlock->predecessors, curBlock->id); |
| 1631 | } |
| 1632 | } else { |
| 1633 | BasicBlock *ehBlock = dvmCompilerNewBB(kExceptionHandling, |
| 1634 | cUnit->numBlocks++); |
| 1635 | curBlock->taken = ehBlock; |
| 1636 | dvmInsertGrowableList(&cUnit->blockList, (intptr_t) ehBlock); |
| 1637 | ehBlock->startOffset = curOffset; |
| 1638 | dvmCompilerSetBit(ehBlock->predecessors, curBlock->id); |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * Force the current block to terminate. |
| 1643 | * |
| 1644 | * Data may be present before codeEnd, so we need to parse it to know |
| 1645 | * whether it is code or data. |
| 1646 | */ |
| 1647 | if (codePtr < codeEnd) { |
| 1648 | /* Create a fallthrough block for real instructions (incl. OP_NOP) */ |
| 1649 | if (contentIsInsn(codePtr)) { |
| 1650 | BasicBlock *fallthroughBlock = findBlock(cUnit, |
| 1651 | curOffset + width, |
| 1652 | /* split */ |
| 1653 | false, |
| 1654 | /* create */ |
| 1655 | true); |
| 1656 | /* |
| 1657 | * OP_THROW and OP_THROW_VERIFICATION_ERROR are unconditional |
| 1658 | * branches. |
| 1659 | */ |
| 1660 | if (insn->dalvikInsn.opcode != OP_THROW_VERIFICATION_ERROR && |
| 1661 | insn->dalvikInsn.opcode != OP_THROW) { |
| 1662 | curBlock->fallThrough = fallthroughBlock; |
| 1663 | dvmCompilerSetBit(fallthroughBlock->predecessors, curBlock->id); |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1669 | /* |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1670 | * Similar to dvmCompileTrace, but the entity processed here is the whole |
| 1671 | * method. |
| 1672 | * |
| 1673 | * TODO: implementation will be revisited when the trace builder can provide |
| 1674 | * whole-method traces. |
| 1675 | */ |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 1676 | bool dvmCompileMethod(const Method *method, JitTranslationInfo *info) |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1677 | { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1678 | CompilationUnit cUnit; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1679 | const DexCode *dexCode = dvmGetMethodCode(method); |
| 1680 | const u2 *codePtr = dexCode->insns; |
| 1681 | const u2 *codeEnd = dexCode->insns + dexCode->insnsSize; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1682 | int numBlocks = 0; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1683 | unsigned int curOffset = 0; |
| 1684 | |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 1685 | /* Method already compiled */ |
| 1686 | if (dvmJitGetMethodAddr(codePtr)) { |
| 1687 | info->codeAddress = NULL; |
| 1688 | return false; |
| 1689 | } |
| 1690 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1691 | memset(&cUnit, 0, sizeof(cUnit)); |
| 1692 | cUnit.method = method; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1693 | |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 1694 | cUnit.methodJitMode = true; |
| 1695 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1696 | /* Initialize the block list */ |
| 1697 | dvmInitGrowableList(&cUnit.blockList, 4); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1698 | |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 1699 | /* |
| 1700 | * FIXME - PC reconstruction list won't be needed after the codegen routines |
| 1701 | * are enhanced to true method mode. |
| 1702 | */ |
| 1703 | /* Initialize the PC reconstruction list */ |
| 1704 | dvmInitGrowableList(&cUnit.pcReconstructionList, 8); |
| 1705 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1706 | /* Allocate the bit-vector to track the beginning of basic blocks */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1707 | BitVector *tryBlockAddr = dvmCompilerAllocBitVector(dexCode->insnsSize, |
| 1708 | true /* expandable */); |
| 1709 | cUnit.tryBlockAddr = tryBlockAddr; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1710 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1711 | /* Create the default entry and exit blocks and enter them to the list */ |
| 1712 | BasicBlock *entryBlock = dvmCompilerNewBB(kMethodEntryBlock, numBlocks++); |
| 1713 | BasicBlock *exitBlock = dvmCompilerNewBB(kMethodExitBlock, numBlocks++); |
| 1714 | |
| 1715 | cUnit.entryBlock = entryBlock; |
| 1716 | cUnit.exitBlock = exitBlock; |
| 1717 | |
| 1718 | dvmInsertGrowableList(&cUnit.blockList, (intptr_t) entryBlock); |
| 1719 | dvmInsertGrowableList(&cUnit.blockList, (intptr_t) exitBlock); |
| 1720 | |
| 1721 | /* Current block to record parsed instructions */ |
| 1722 | BasicBlock *curBlock = dvmCompilerNewBB(kDalvikByteCode, numBlocks++); |
| 1723 | curBlock->startOffset = 0; |
| 1724 | dvmInsertGrowableList(&cUnit.blockList, (intptr_t) curBlock); |
| 1725 | entryBlock->fallThrough = curBlock; |
| 1726 | dvmCompilerSetBit(curBlock->predecessors, entryBlock->id); |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1727 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1728 | /* |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1729 | * Store back the number of blocks since new blocks may be created of |
| 1730 | * accessing cUnit. |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1731 | */ |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1732 | cUnit.numBlocks = numBlocks; |
| 1733 | |
| 1734 | /* Identify code range in try blocks and set up the empty catch blocks */ |
| 1735 | processTryCatchBlocks(&cUnit); |
| 1736 | |
| 1737 | /* Parse all instructions and put them into containing basic blocks */ |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1738 | while (codePtr < codeEnd) { |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1739 | MIR *insn = (MIR *) dvmCompilerNew(sizeof(MIR), true); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1740 | insn->offset = curOffset; |
| 1741 | int width = parseInsn(codePtr, &insn->dalvikInsn, false); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1742 | insn->width = width; |
| 1743 | |
Ben Cheng | 8b258bf | 2009-06-24 17:27:07 -0700 | [diff] [blame] | 1744 | /* Terminate when the data section is seen */ |
| 1745 | if (width == 0) |
| 1746 | break; |
Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 1747 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1748 | dvmCompilerAppendMIR(curBlock, insn); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1749 | |
| 1750 | codePtr += width; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1751 | int flags = dexGetFlagsFromOpcode(insn->dalvikInsn.opcode); |
| 1752 | |
| 1753 | if (flags & kInstrCanBranch) { |
| 1754 | processCanBranch(&cUnit, curBlock, insn, curOffset, width, flags, |
| 1755 | codePtr, codeEnd); |
| 1756 | } else if (flags & kInstrCanReturn) { |
| 1757 | curBlock->fallThrough = exitBlock; |
| 1758 | dvmCompilerSetBit(exitBlock->predecessors, curBlock->id); |
| 1759 | /* |
| 1760 | * Terminate the current block if there are instructions |
| 1761 | * afterwards. |
| 1762 | */ |
| 1763 | if (codePtr < codeEnd) { |
| 1764 | /* |
| 1765 | * Create a fallthrough block for real instructions |
| 1766 | * (incl. OP_NOP). |
| 1767 | */ |
| 1768 | if (contentIsInsn(codePtr)) { |
| 1769 | findBlock(&cUnit, curOffset + width, |
| 1770 | /* split */ |
| 1771 | false, |
| 1772 | /* create */ |
| 1773 | true); |
| 1774 | } |
| 1775 | } |
| 1776 | } else if (flags & kInstrCanThrow) { |
| 1777 | processCanThrow(&cUnit, curBlock, insn, curOffset, width, flags, |
| 1778 | tryBlockAddr, codePtr, codeEnd); |
| 1779 | } else if (flags & kInstrCanSwitch) { |
| 1780 | processCanSwitch(&cUnit, curBlock, insn, curOffset, width, flags); |
| 1781 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1782 | curOffset += width; |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1783 | BasicBlock *nextBlock = findBlock(&cUnit, curOffset, |
| 1784 | /* split */ |
| 1785 | false, |
| 1786 | /* create */ |
| 1787 | false); |
| 1788 | if (nextBlock) { |
| 1789 | /* |
| 1790 | * The next instruction could be the target of a previously parsed |
| 1791 | * forward branch so a block is already created. If the current |
| 1792 | * instruction is not an unconditional branch, connect them through |
| 1793 | * the fall-through link. |
| 1794 | */ |
| 1795 | assert(curBlock->fallThrough == NULL || |
| 1796 | curBlock->fallThrough == nextBlock || |
| 1797 | curBlock->fallThrough == exitBlock); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1798 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1799 | if ((curBlock->fallThrough == NULL) && |
| 1800 | !dexIsGoto(flags) && |
| 1801 | !(flags & kInstrCanReturn)) { |
| 1802 | curBlock->fallThrough = nextBlock; |
| 1803 | dvmCompilerSetBit(nextBlock->predecessors, curBlock->id); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1804 | } |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1805 | curBlock = nextBlock; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 1809 | if (cUnit.printMe) { |
| 1810 | dvmCompilerDumpCompilationUnit(&cUnit); |
| 1811 | } |
| 1812 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1813 | /* Adjust this value accordingly once inlining is performed */ |
| 1814 | cUnit.numDalvikRegisters = cUnit.method->registersSize; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1815 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1816 | /* Verify if all blocks are connected as claimed */ |
| 1817 | /* FIXME - to be disabled in the future */ |
| 1818 | dvmCompilerDataFlowAnalysisDispatcher(&cUnit, verifyPredInfo, |
| 1819 | kAllNodes, |
| 1820 | false /* isIterative */); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1821 | |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1822 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1823 | /* Perform SSA transformation for the whole method */ |
| 1824 | dvmCompilerMethodSSATransformation(&cUnit); |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1825 | |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 1826 | dvmCompilerInitializeRegAlloc(&cUnit); // Needs to happen after SSA naming |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1827 | |
Ben Cheng | cfdeca3 | 2011-01-14 11:36:46 -0800 | [diff] [blame] | 1828 | /* Allocate Registers using simple local allocation scheme */ |
| 1829 | dvmCompilerLocalRegAlloc(&cUnit); |
| 1830 | |
| 1831 | /* Convert MIR to LIR, etc. */ |
| 1832 | dvmCompilerMethodMIR2LIR(&cUnit); |
| 1833 | |
| 1834 | // Debugging only |
| 1835 | //dumpCFG(&cUnit, "/data/tombstones/"); |
| 1836 | |
| 1837 | /* Method is not empty */ |
| 1838 | if (cUnit.firstLIRInsn) { |
| 1839 | /* Convert LIR into machine code. Loop for recoverable retries */ |
| 1840 | do { |
| 1841 | dvmCompilerAssembleLIR(&cUnit, info); |
| 1842 | cUnit.assemblerRetries++; |
| 1843 | if (cUnit.printMe && cUnit.assemblerStatus != kSuccess) |
| 1844 | LOGD("Assembler abort #%d on %d",cUnit.assemblerRetries, |
| 1845 | cUnit.assemblerStatus); |
| 1846 | } while (cUnit.assemblerStatus == kRetryAll); |
| 1847 | |
| 1848 | if (cUnit.printMe) { |
| 1849 | dvmCompilerCodegenDump(&cUnit); |
| 1850 | } |
| 1851 | |
| 1852 | if (info->codeAddress) { |
| 1853 | dvmJitSetCodeAddr(dexCode->insns, info->codeAddress, |
| 1854 | info->instructionSet, true, 0); |
| 1855 | /* |
| 1856 | * Clear the codeAddress for the enclosing trace to reuse the info |
| 1857 | */ |
| 1858 | info->codeAddress = NULL; |
| 1859 | } |
| 1860 | } |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1861 | |
Ben Cheng | 0060307 | 2010-10-28 11:13:58 -0700 | [diff] [blame] | 1862 | return false; |
Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1863 | } |