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