buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Ian Rogers | 6282dc1 | 2013-04-18 15:54:02 -0700 | [diff] [blame] | 17 | #include "base/stl_util.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 18 | #include "compiler_internals.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 19 | #include "dex_file-inl.h" |
Ian Rogers | 6282dc1 | 2013-04-18 15:54:02 -0700 | [diff] [blame] | 20 | #include "leb128.h" |
| 21 | #include "mir_graph.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | #define MAX_PATTERN_LEN 5 |
| 26 | |
| 27 | struct CodePattern { |
| 28 | const Instruction::Code opcodes[MAX_PATTERN_LEN]; |
| 29 | const SpecialCaseHandler handler_code; |
| 30 | }; |
| 31 | |
| 32 | static const CodePattern special_patterns[] = { |
| 33 | {{Instruction::RETURN_VOID}, kNullMethod}, |
| 34 | {{Instruction::CONST, Instruction::RETURN}, kConstFunction}, |
| 35 | {{Instruction::CONST_4, Instruction::RETURN}, kConstFunction}, |
| 36 | {{Instruction::CONST_4, Instruction::RETURN_OBJECT}, kConstFunction}, |
| 37 | {{Instruction::CONST_16, Instruction::RETURN}, kConstFunction}, |
| 38 | {{Instruction::IGET, Instruction:: RETURN}, kIGet}, |
| 39 | {{Instruction::IGET_BOOLEAN, Instruction::RETURN}, kIGetBoolean}, |
| 40 | {{Instruction::IGET_OBJECT, Instruction::RETURN_OBJECT}, kIGetObject}, |
| 41 | {{Instruction::IGET_BYTE, Instruction::RETURN}, kIGetByte}, |
| 42 | {{Instruction::IGET_CHAR, Instruction::RETURN}, kIGetChar}, |
| 43 | {{Instruction::IGET_SHORT, Instruction::RETURN}, kIGetShort}, |
| 44 | {{Instruction::IGET_WIDE, Instruction::RETURN_WIDE}, kIGetWide}, |
| 45 | {{Instruction::IPUT, Instruction::RETURN_VOID}, kIPut}, |
| 46 | {{Instruction::IPUT_BOOLEAN, Instruction::RETURN_VOID}, kIPutBoolean}, |
| 47 | {{Instruction::IPUT_OBJECT, Instruction::RETURN_VOID}, kIPutObject}, |
| 48 | {{Instruction::IPUT_BYTE, Instruction::RETURN_VOID}, kIPutByte}, |
| 49 | {{Instruction::IPUT_CHAR, Instruction::RETURN_VOID}, kIPutChar}, |
| 50 | {{Instruction::IPUT_SHORT, Instruction::RETURN_VOID}, kIPutShort}, |
| 51 | {{Instruction::IPUT_WIDE, Instruction::RETURN_VOID}, kIPutWide}, |
| 52 | {{Instruction::RETURN}, kIdentity}, |
| 53 | {{Instruction::RETURN_OBJECT}, kIdentity}, |
| 54 | {{Instruction::RETURN_WIDE}, kIdentity}, |
| 55 | }; |
| 56 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 57 | const char* MIRGraph::extended_mir_op_names_[kMirOpLast - kMirOpFirst] = { |
| 58 | "Phi", |
| 59 | "Copy", |
| 60 | "FusedCmplFloat", |
| 61 | "FusedCmpgFloat", |
| 62 | "FusedCmplDouble", |
| 63 | "FusedCmpgDouble", |
| 64 | "FusedCmpLong", |
| 65 | "Nop", |
| 66 | "OpNullCheck", |
| 67 | "OpRangeCheck", |
| 68 | "OpDivZeroCheck", |
| 69 | "Check1", |
| 70 | "Check2", |
| 71 | "Select", |
| 72 | }; |
| 73 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 74 | MIRGraph::MIRGraph(CompilationUnit* cu, ArenaAllocator* arena) |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 75 | : reg_location_(NULL), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 76 | compiler_temps_(arena, 6, kGrowableArrayMisc), |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 77 | cu_(cu), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 78 | ssa_base_vregs_(NULL), |
| 79 | ssa_subscripts_(NULL), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 80 | vreg_to_ssa_map_(NULL), |
| 81 | ssa_last_defs_(NULL), |
| 82 | is_constant_v_(NULL), |
| 83 | constant_values_(NULL), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 84 | use_counts_(arena, 256, kGrowableArrayMisc), |
| 85 | raw_use_counts_(arena, 256, kGrowableArrayMisc), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 86 | num_reachable_blocks_(0), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 87 | dfs_order_(NULL), |
| 88 | dfs_post_order_(NULL), |
| 89 | dom_post_order_traversal_(NULL), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 90 | i_dom_list_(NULL), |
| 91 | def_block_matrix_(NULL), |
| 92 | temp_block_v_(NULL), |
| 93 | temp_dalvik_register_v_(NULL), |
| 94 | temp_ssa_register_v_(NULL), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 95 | block_list_(arena, 100, kGrowableArrayBlockList), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 96 | try_block_addr_(NULL), |
| 97 | entry_block_(NULL), |
| 98 | exit_block_(NULL), |
| 99 | cur_block_(NULL), |
| 100 | num_blocks_(0), |
| 101 | current_code_item_(NULL), |
| 102 | current_method_(kInvalidEntry), |
| 103 | current_offset_(kInvalidEntry), |
| 104 | def_count_(0), |
| 105 | opcode_count_(NULL), |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 106 | num_ssa_regs_(0), |
| 107 | method_sreg_(0), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 108 | attributes_(METHOD_IS_LEAF), // Start with leaf assumption, change on encountering invoke. |
| 109 | checkstats_(NULL), |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 110 | arena_(arena) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 111 | try_block_addr_ = new (arena_) ArenaBitVector(arena_, 0, true /* expandable */); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Ian Rogers | 6282dc1 | 2013-04-18 15:54:02 -0700 | [diff] [blame] | 114 | MIRGraph::~MIRGraph() { |
| 115 | STLDeleteElements(&m_units_); |
| 116 | } |
| 117 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 118 | bool MIRGraph::ContentIsInsn(const uint16_t* code_ptr) { |
| 119 | uint16_t instr = *code_ptr; |
| 120 | Instruction::Code opcode = static_cast<Instruction::Code>(instr & 0xff); |
| 121 | /* |
| 122 | * Since the low 8-bit in metadata may look like NOP, we need to check |
| 123 | * both the low and whole sub-word to determine whether it is code or data. |
| 124 | */ |
| 125 | return (opcode != Instruction::NOP || instr == 0); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Parse an instruction, return the length of the instruction |
| 130 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 131 | int MIRGraph::ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 132 | // Don't parse instruction data |
| 133 | if (!ContentIsInsn(code_ptr)) { |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | const Instruction* instruction = Instruction::At(code_ptr); |
| 138 | *decoded_instruction = DecodedInstruction(instruction); |
| 139 | |
| 140 | return instruction->SizeInCodeUnits(); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* Split an existing block from the specified code offset into two */ |
| 145 | BasicBlock* MIRGraph::SplitBlock(unsigned int code_offset, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 146 | BasicBlock* orig_block, BasicBlock** immed_pred_block_p) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 147 | MIR* insn = orig_block->first_mir_insn; |
| 148 | while (insn) { |
| 149 | if (insn->offset == code_offset) break; |
| 150 | insn = insn->next; |
| 151 | } |
| 152 | if (insn == NULL) { |
| 153 | LOG(FATAL) << "Break split failed"; |
| 154 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 155 | BasicBlock *bottom_block = NewMemBB(kDalvikByteCode, num_blocks_++); |
| 156 | block_list_.Insert(bottom_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 157 | |
| 158 | bottom_block->start_offset = code_offset; |
| 159 | bottom_block->first_mir_insn = insn; |
| 160 | bottom_block->last_mir_insn = orig_block->last_mir_insn; |
| 161 | |
| 162 | /* If this block was terminated by a return, the flag needs to go with the bottom block */ |
| 163 | bottom_block->terminated_by_return = orig_block->terminated_by_return; |
| 164 | orig_block->terminated_by_return = false; |
| 165 | |
| 166 | /* Add it to the quick lookup cache */ |
| 167 | block_map_.Put(bottom_block->start_offset, bottom_block); |
| 168 | |
| 169 | /* Handle the taken path */ |
| 170 | bottom_block->taken = orig_block->taken; |
| 171 | if (bottom_block->taken) { |
| 172 | orig_block->taken = NULL; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 173 | bottom_block->taken->predecessors->Delete(orig_block); |
| 174 | bottom_block->taken->predecessors->Insert(bottom_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Handle the fallthrough path */ |
| 178 | bottom_block->fall_through = orig_block->fall_through; |
| 179 | orig_block->fall_through = bottom_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 180 | bottom_block->predecessors->Insert(orig_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 181 | if (bottom_block->fall_through) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 182 | bottom_block->fall_through->predecessors->Delete(orig_block); |
| 183 | bottom_block->fall_through->predecessors->Insert(bottom_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* Handle the successor list */ |
| 187 | if (orig_block->successor_block_list.block_list_type != kNotUsed) { |
| 188 | bottom_block->successor_block_list = orig_block->successor_block_list; |
| 189 | orig_block->successor_block_list.block_list_type = kNotUsed; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 190 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bottom_block->successor_block_list.blocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 191 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 192 | SuccessorBlockInfo *successor_block_info = iterator.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 193 | if (successor_block_info == NULL) break; |
| 194 | BasicBlock *bb = successor_block_info->block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 195 | bb->predecessors->Delete(orig_block); |
| 196 | bb->predecessors->Insert(bottom_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | orig_block->last_mir_insn = insn->prev; |
| 201 | |
| 202 | insn->prev->next = NULL; |
| 203 | insn->prev = NULL; |
| 204 | /* |
| 205 | * Update the immediate predecessor block pointer so that outgoing edges |
| 206 | * can be applied to the proper block. |
| 207 | */ |
| 208 | if (immed_pred_block_p) { |
| 209 | DCHECK_EQ(*immed_pred_block_p, orig_block); |
| 210 | *immed_pred_block_p = bottom_block; |
| 211 | } |
| 212 | return bottom_block; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Given a code offset, find out the block that starts with it. If the offset |
| 217 | * is in the middle of an existing block, split it into two. If immed_pred_block_p |
| 218 | * is not non-null and is the block being split, update *immed_pred_block_p to |
| 219 | * point to the bottom block so that outgoing edges can be set up properly |
| 220 | * (by the caller) |
| 221 | * Utilizes a map for fast lookup of the typical cases. |
| 222 | */ |
| 223 | BasicBlock* MIRGraph::FindBlock(unsigned int code_offset, bool split, bool create, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 224 | BasicBlock** immed_pred_block_p) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 225 | BasicBlock* bb; |
| 226 | unsigned int i; |
| 227 | SafeMap<unsigned int, BasicBlock*>::iterator it; |
| 228 | |
| 229 | it = block_map_.find(code_offset); |
| 230 | if (it != block_map_.end()) { |
| 231 | return it->second; |
| 232 | } else if (!create) { |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | if (split) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 237 | for (i = 0; i < block_list_.Size(); i++) { |
| 238 | bb = block_list_.Get(i); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 239 | if (bb->block_type != kDalvikByteCode) continue; |
| 240 | /* Check if a branch jumps into the middle of an existing block */ |
| 241 | if ((code_offset > bb->start_offset) && (bb->last_mir_insn != NULL) && |
| 242 | (code_offset <= bb->last_mir_insn->offset)) { |
| 243 | BasicBlock *new_bb = SplitBlock(code_offset, bb, bb == *immed_pred_block_p ? |
| 244 | immed_pred_block_p : NULL); |
| 245 | return new_bb; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* Create a new one */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 251 | bb = NewMemBB(kDalvikByteCode, num_blocks_++); |
| 252 | block_list_.Insert(bb); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 253 | bb->start_offset = code_offset; |
| 254 | block_map_.Put(bb->start_offset, bb); |
| 255 | return bb; |
| 256 | } |
| 257 | |
| 258 | /* Identify code range in try blocks and set up the empty catch blocks */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 259 | void MIRGraph::ProcessTryCatchBlocks() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 260 | int tries_size = current_code_item_->tries_size_; |
| 261 | int offset; |
| 262 | |
| 263 | if (tries_size == 0) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | for (int i = 0; i < tries_size; i++) { |
| 268 | const DexFile::TryItem* pTry = |
| 269 | DexFile::GetTryItems(*current_code_item_, i); |
| 270 | int start_offset = pTry->start_addr_; |
| 271 | int end_offset = start_offset + pTry->insn_count_; |
| 272 | for (offset = start_offset; offset < end_offset; offset++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 273 | try_block_addr_->SetBit(offset); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | // Iterate over each of the handlers to enqueue the empty Catch blocks |
| 278 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*current_code_item_, 0); |
| 279 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 280 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 281 | CatchHandlerIterator iterator(handlers_ptr); |
| 282 | for (; iterator.HasNext(); iterator.Next()) { |
| 283 | uint32_t address = iterator.GetHandlerAddress(); |
| 284 | FindBlock(address, false /* split */, true /*create*/, |
| 285 | /* immed_pred_block_p */ NULL); |
| 286 | } |
| 287 | handlers_ptr = iterator.EndDataPointer(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /* Process instructions with the kBranch flag */ |
| 292 | BasicBlock* MIRGraph::ProcessCanBranch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, |
| 293 | int flags, const uint16_t* code_ptr, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 294 | const uint16_t* code_end) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 295 | int target = cur_offset; |
| 296 | switch (insn->dalvikInsn.opcode) { |
| 297 | case Instruction::GOTO: |
| 298 | case Instruction::GOTO_16: |
| 299 | case Instruction::GOTO_32: |
| 300 | target += insn->dalvikInsn.vA; |
| 301 | break; |
| 302 | case Instruction::IF_EQ: |
| 303 | case Instruction::IF_NE: |
| 304 | case Instruction::IF_LT: |
| 305 | case Instruction::IF_GE: |
| 306 | case Instruction::IF_GT: |
| 307 | case Instruction::IF_LE: |
| 308 | cur_block->conditional_branch = true; |
| 309 | target += insn->dalvikInsn.vC; |
| 310 | break; |
| 311 | case Instruction::IF_EQZ: |
| 312 | case Instruction::IF_NEZ: |
| 313 | case Instruction::IF_LTZ: |
| 314 | case Instruction::IF_GEZ: |
| 315 | case Instruction::IF_GTZ: |
| 316 | case Instruction::IF_LEZ: |
| 317 | cur_block->conditional_branch = true; |
| 318 | target += insn->dalvikInsn.vB; |
| 319 | break; |
| 320 | default: |
| 321 | LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set"; |
| 322 | } |
| 323 | BasicBlock *taken_block = FindBlock(target, /* split */ true, /* create */ true, |
| 324 | /* immed_pred_block_p */ &cur_block); |
| 325 | cur_block->taken = taken_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 326 | taken_block->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 327 | |
| 328 | /* Always terminate the current block for conditional branches */ |
| 329 | if (flags & Instruction::kContinue) { |
| 330 | BasicBlock *fallthrough_block = FindBlock(cur_offset + width, |
| 331 | /* |
| 332 | * If the method is processed |
| 333 | * in sequential order from the |
| 334 | * beginning, we don't need to |
| 335 | * specify split for continue |
| 336 | * blocks. However, this |
| 337 | * routine can be called by |
| 338 | * compileLoop, which starts |
| 339 | * parsing the method from an |
| 340 | * arbitrary address in the |
| 341 | * method body. |
| 342 | */ |
| 343 | true, |
| 344 | /* create */ |
| 345 | true, |
| 346 | /* immed_pred_block_p */ |
| 347 | &cur_block); |
| 348 | cur_block->fall_through = fallthrough_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 349 | fallthrough_block->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 350 | } else if (code_ptr < code_end) { |
| 351 | /* Create a fallthrough block for real instructions (incl. NOP) */ |
| 352 | if (ContentIsInsn(code_ptr)) { |
| 353 | FindBlock(cur_offset + width, /* split */ false, /* create */ true, |
| 354 | /* immed_pred_block_p */ NULL); |
| 355 | } |
| 356 | } |
| 357 | return cur_block; |
| 358 | } |
| 359 | |
| 360 | /* Process instructions with the kSwitch flag */ |
| 361 | void MIRGraph::ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 362 | int flags) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 363 | const uint16_t* switch_data = |
| 364 | reinterpret_cast<const uint16_t*>(GetCurrentInsns() + cur_offset + insn->dalvikInsn.vB); |
| 365 | int size; |
| 366 | const int* keyTable; |
| 367 | const int* target_table; |
| 368 | int i; |
| 369 | int first_key; |
| 370 | |
| 371 | /* |
| 372 | * Packed switch data format: |
| 373 | * ushort ident = 0x0100 magic value |
| 374 | * ushort size number of entries in the table |
| 375 | * int first_key first (and lowest) switch case value |
| 376 | * int targets[size] branch targets, relative to switch opcode |
| 377 | * |
| 378 | * Total size is (4+size*2) 16-bit code units. |
| 379 | */ |
| 380 | if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) { |
| 381 | DCHECK_EQ(static_cast<int>(switch_data[0]), |
| 382 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 383 | size = switch_data[1]; |
| 384 | first_key = switch_data[2] | (switch_data[3] << 16); |
| 385 | target_table = reinterpret_cast<const int*>(&switch_data[4]); |
| 386 | keyTable = NULL; // Make the compiler happy |
| 387 | /* |
| 388 | * Sparse switch data format: |
| 389 | * ushort ident = 0x0200 magic value |
| 390 | * ushort size number of entries in the table; > 0 |
| 391 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 392 | * int targets[size] branch targets, relative to switch opcode |
| 393 | * |
| 394 | * Total size is (2+size*4) 16-bit code units. |
| 395 | */ |
| 396 | } else { |
| 397 | DCHECK_EQ(static_cast<int>(switch_data[0]), |
| 398 | static_cast<int>(Instruction::kSparseSwitchSignature)); |
| 399 | size = switch_data[1]; |
| 400 | keyTable = reinterpret_cast<const int*>(&switch_data[2]); |
| 401 | target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]); |
| 402 | first_key = 0; // To make the compiler happy |
| 403 | } |
| 404 | |
| 405 | if (cur_block->successor_block_list.block_list_type != kNotUsed) { |
| 406 | LOG(FATAL) << "Successor block list already in use: " |
| 407 | << static_cast<int>(cur_block->successor_block_list.block_list_type); |
| 408 | } |
| 409 | cur_block->successor_block_list.block_list_type = |
| 410 | (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? |
| 411 | kPackedSwitch : kSparseSwitch; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 412 | cur_block->successor_block_list.blocks = |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 413 | new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, size, kGrowableArraySuccessorBlocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 414 | |
| 415 | for (i = 0; i < size; i++) { |
| 416 | BasicBlock *case_block = FindBlock(cur_offset + target_table[i], /* split */ true, |
| 417 | /* create */ true, /* immed_pred_block_p */ &cur_block); |
| 418 | SuccessorBlockInfo *successor_block_info = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 419 | static_cast<SuccessorBlockInfo*>(arena_->NewMem(sizeof(SuccessorBlockInfo), false, |
| 420 | ArenaAllocator::kAllocSuccessor)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 421 | successor_block_info->block = case_block; |
| 422 | successor_block_info->key = |
| 423 | (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? |
| 424 | first_key + i : keyTable[i]; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 425 | cur_block->successor_block_list.blocks->Insert(successor_block_info); |
| 426 | case_block->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* Fall-through case */ |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 430 | BasicBlock* fallthrough_block = FindBlock(cur_offset + width, /* split */ false, |
| 431 | /* create */ true, /* immed_pred_block_p */ NULL); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 432 | cur_block->fall_through = fallthrough_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 433 | fallthrough_block->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Process instructions with the kThrow flag */ |
| 437 | BasicBlock* MIRGraph::ProcessCanThrow(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, |
| 438 | int flags, ArenaBitVector* try_block_addr, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 439 | const uint16_t* code_ptr, const uint16_t* code_end) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 440 | bool in_try_block = try_block_addr->IsBitSet(cur_offset); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 441 | |
| 442 | /* In try block */ |
| 443 | if (in_try_block) { |
| 444 | CatchHandlerIterator iterator(*current_code_item_, cur_offset); |
| 445 | |
| 446 | if (cur_block->successor_block_list.block_list_type != kNotUsed) { |
| 447 | LOG(INFO) << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 448 | LOG(FATAL) << "Successor block list already in use: " |
| 449 | << static_cast<int>(cur_block->successor_block_list.block_list_type); |
| 450 | } |
| 451 | |
| 452 | cur_block->successor_block_list.block_list_type = kCatch; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 453 | cur_block->successor_block_list.blocks = |
| 454 | new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, 2, kGrowableArraySuccessorBlocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 455 | |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame^] | 456 | for (; iterator.HasNext(); iterator.Next()) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 457 | BasicBlock *catch_block = FindBlock(iterator.GetHandlerAddress(), false /* split*/, |
| 458 | false /* creat */, NULL /* immed_pred_block_p */); |
| 459 | catch_block->catch_entry = true; |
| 460 | if (kIsDebugBuild) { |
| 461 | catches_.insert(catch_block->start_offset); |
| 462 | } |
| 463 | SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*> |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 464 | (arena_->NewMem(sizeof(SuccessorBlockInfo), false, ArenaAllocator::kAllocSuccessor)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 465 | successor_block_info->block = catch_block; |
| 466 | successor_block_info->key = iterator.GetHandlerTypeIndex(); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 467 | cur_block->successor_block_list.blocks->Insert(successor_block_info); |
| 468 | catch_block->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 469 | } |
| 470 | } else { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 471 | BasicBlock *eh_block = NewMemBB(kExceptionHandling, num_blocks_++); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 472 | cur_block->taken = eh_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 473 | block_list_.Insert(eh_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 474 | eh_block->start_offset = cur_offset; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 475 | eh_block->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 478 | if (insn->dalvikInsn.opcode == Instruction::THROW) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 479 | cur_block->explicit_throw = true; |
| 480 | if ((code_ptr < code_end) && ContentIsInsn(code_ptr)) { |
| 481 | // Force creation of new block following THROW via side-effect |
| 482 | FindBlock(cur_offset + width, /* split */ false, /* create */ true, |
| 483 | /* immed_pred_block_p */ NULL); |
| 484 | } |
| 485 | if (!in_try_block) { |
| 486 | // Don't split a THROW that can't rethrow - we're done. |
| 487 | return cur_block; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Split the potentially-throwing instruction into two parts. |
| 493 | * The first half will be a pseudo-op that captures the exception |
| 494 | * edges and terminates the basic block. It always falls through. |
| 495 | * Then, create a new basic block that begins with the throwing instruction |
| 496 | * (minus exceptions). Note: this new basic block must NOT be entered into |
| 497 | * the block_map. If the potentially-throwing instruction is the target of a |
| 498 | * future branch, we need to find the check psuedo half. The new |
| 499 | * basic block containing the work portion of the instruction should |
| 500 | * only be entered via fallthrough from the block containing the |
| 501 | * pseudo exception edge MIR. Note also that this new block is |
| 502 | * not automatically terminated after the work portion, and may |
| 503 | * contain following instructions. |
| 504 | */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 505 | BasicBlock *new_block = NewMemBB(kDalvikByteCode, num_blocks_++); |
| 506 | block_list_.Insert(new_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 507 | new_block->start_offset = insn->offset; |
| 508 | cur_block->fall_through = new_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 509 | new_block->predecessors->Insert(cur_block); |
| 510 | MIR* new_insn = static_cast<MIR*>(arena_->NewMem(sizeof(MIR), true, ArenaAllocator::kAllocMIR)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 511 | *new_insn = *insn; |
| 512 | insn->dalvikInsn.opcode = |
| 513 | static_cast<Instruction::Code>(kMirOpCheck); |
| 514 | // Associate the two halves |
| 515 | insn->meta.throw_insn = new_insn; |
| 516 | new_insn->meta.throw_insn = insn; |
| 517 | AppendMIR(new_block, new_insn); |
| 518 | return new_block; |
| 519 | } |
| 520 | |
| 521 | /* Parse a Dex method and insert it into the MIRGraph at the current insert point. */ |
| 522 | void MIRGraph::InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 523 | InvokeType invoke_type, uint32_t class_def_idx, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 524 | uint32_t method_idx, jobject class_loader, const DexFile& dex_file) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 525 | current_code_item_ = code_item; |
| 526 | method_stack_.push_back(std::make_pair(current_method_, current_offset_)); |
| 527 | current_method_ = m_units_.size(); |
| 528 | current_offset_ = 0; |
| 529 | // TODO: will need to snapshot stack image and use that as the mir context identification. |
| 530 | m_units_.push_back(new DexCompilationUnit(cu_, class_loader, Runtime::Current()->GetClassLinker(), |
| 531 | dex_file, current_code_item_, class_def_idx, method_idx, access_flags)); |
| 532 | const uint16_t* code_ptr = current_code_item_->insns_; |
| 533 | const uint16_t* code_end = |
| 534 | current_code_item_->insns_ + current_code_item_->insns_size_in_code_units_; |
| 535 | |
| 536 | // TODO: need to rework expansion of block list & try_block_addr when inlining activated. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 537 | block_list_.Resize(block_list_.Size() + current_code_item_->insns_size_in_code_units_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 538 | // TODO: replace with explicit resize routine. Using automatic extension side effect for now. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 539 | try_block_addr_->SetBit(current_code_item_->insns_size_in_code_units_); |
| 540 | try_block_addr_->ClearBit(current_code_item_->insns_size_in_code_units_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 541 | |
| 542 | // If this is the first method, set up default entry and exit blocks. |
| 543 | if (current_method_ == 0) { |
| 544 | DCHECK(entry_block_ == NULL); |
| 545 | DCHECK(exit_block_ == NULL); |
| 546 | DCHECK(num_blocks_ == 0); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 547 | entry_block_ = NewMemBB(kEntryBlock, num_blocks_++); |
| 548 | exit_block_ = NewMemBB(kExitBlock, num_blocks_++); |
| 549 | block_list_.Insert(entry_block_); |
| 550 | block_list_.Insert(exit_block_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 551 | // TODO: deprecate all "cu->" fields; move what's left to wherever CompilationUnit is allocated. |
| 552 | cu_->dex_file = &dex_file; |
| 553 | cu_->class_def_idx = class_def_idx; |
| 554 | cu_->method_idx = method_idx; |
| 555 | cu_->access_flags = access_flags; |
| 556 | cu_->invoke_type = invoke_type; |
| 557 | cu_->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
| 558 | cu_->num_ins = current_code_item_->ins_size_; |
| 559 | cu_->num_regs = current_code_item_->registers_size_ - cu_->num_ins; |
| 560 | cu_->num_outs = current_code_item_->outs_size_; |
| 561 | cu_->num_dalvik_registers = current_code_item_->registers_size_; |
| 562 | cu_->insns = current_code_item_->insns_; |
| 563 | cu_->code_item = current_code_item_; |
| 564 | } else { |
| 565 | UNIMPLEMENTED(FATAL) << "Nested inlining not implemented."; |
| 566 | /* |
| 567 | * Will need to manage storage for ins & outs, push prevous state and update |
| 568 | * insert point. |
| 569 | */ |
| 570 | } |
| 571 | |
| 572 | /* Current block to record parsed instructions */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 573 | BasicBlock *cur_block = NewMemBB(kDalvikByteCode, num_blocks_++); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 574 | DCHECK_EQ(current_offset_, 0); |
| 575 | cur_block->start_offset = current_offset_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 576 | block_list_.Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 577 | /* Add first block to the fast lookup cache */ |
| 578 | // FIXME: block map needs association with offset/method pair rather than just offset |
| 579 | block_map_.Put(cur_block->start_offset, cur_block); |
| 580 | // FIXME: this needs to insert at the insert point rather than entry block. |
| 581 | entry_block_->fall_through = cur_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 582 | cur_block->predecessors->Insert(entry_block_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 583 | |
| 584 | /* Identify code range in try blocks and set up the empty catch blocks */ |
| 585 | ProcessTryCatchBlocks(); |
| 586 | |
| 587 | /* Set up for simple method detection */ |
| 588 | int num_patterns = sizeof(special_patterns)/sizeof(special_patterns[0]); |
| 589 | bool live_pattern = (num_patterns > 0) && !(cu_->disable_opt & (1 << kMatch)); |
| 590 | bool* dead_pattern = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 591 | static_cast<bool*>(arena_->NewMem(sizeof(bool) * num_patterns, true, |
| 592 | ArenaAllocator::kAllocMisc)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 593 | SpecialCaseHandler special_case = kNoHandler; |
| 594 | // FIXME - wire this up |
| 595 | (void)special_case; |
| 596 | int pattern_pos = 0; |
| 597 | |
| 598 | /* Parse all instructions and put them into containing basic blocks */ |
| 599 | while (code_ptr < code_end) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 600 | MIR *insn = static_cast<MIR *>(arena_->NewMem(sizeof(MIR), true, ArenaAllocator::kAllocMIR)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 601 | insn->offset = current_offset_; |
| 602 | insn->m_unit_index = current_method_; |
| 603 | int width = ParseInsn(code_ptr, &insn->dalvikInsn); |
| 604 | insn->width = width; |
| 605 | Instruction::Code opcode = insn->dalvikInsn.opcode; |
| 606 | if (opcode_count_ != NULL) { |
| 607 | opcode_count_[static_cast<int>(opcode)]++; |
| 608 | } |
| 609 | |
| 610 | /* Terminate when the data section is seen */ |
| 611 | if (width == 0) |
| 612 | break; |
| 613 | |
| 614 | /* Possible simple method? */ |
| 615 | if (live_pattern) { |
| 616 | live_pattern = false; |
| 617 | special_case = kNoHandler; |
| 618 | for (int i = 0; i < num_patterns; i++) { |
| 619 | if (!dead_pattern[i]) { |
| 620 | if (special_patterns[i].opcodes[pattern_pos] == opcode) { |
| 621 | live_pattern = true; |
| 622 | special_case = special_patterns[i].handler_code; |
| 623 | } else { |
| 624 | dead_pattern[i] = true; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | pattern_pos++; |
| 629 | } |
| 630 | |
| 631 | AppendMIR(cur_block, insn); |
| 632 | |
| 633 | code_ptr += width; |
| 634 | int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode); |
| 635 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 636 | int df_flags = oat_data_flow_attributes_[insn->dalvikInsn.opcode]; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 637 | |
| 638 | if (df_flags & DF_HAS_DEFS) { |
| 639 | def_count_ += (df_flags & DF_A_WIDE) ? 2 : 1; |
| 640 | } |
| 641 | |
| 642 | if (flags & Instruction::kBranch) { |
| 643 | cur_block = ProcessCanBranch(cur_block, insn, current_offset_, |
| 644 | width, flags, code_ptr, code_end); |
| 645 | } else if (flags & Instruction::kReturn) { |
| 646 | cur_block->terminated_by_return = true; |
| 647 | cur_block->fall_through = exit_block_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 648 | exit_block_->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 649 | /* |
| 650 | * Terminate the current block if there are instructions |
| 651 | * afterwards. |
| 652 | */ |
| 653 | if (code_ptr < code_end) { |
| 654 | /* |
| 655 | * Create a fallthrough block for real instructions |
| 656 | * (incl. NOP). |
| 657 | */ |
| 658 | if (ContentIsInsn(code_ptr)) { |
| 659 | FindBlock(current_offset_ + width, /* split */ false, /* create */ true, |
| 660 | /* immed_pred_block_p */ NULL); |
| 661 | } |
| 662 | } |
| 663 | } else if (flags & Instruction::kThrow) { |
| 664 | cur_block = ProcessCanThrow(cur_block, insn, current_offset_, width, flags, try_block_addr_, |
| 665 | code_ptr, code_end); |
| 666 | } else if (flags & Instruction::kSwitch) { |
| 667 | ProcessCanSwitch(cur_block, insn, current_offset_, width, flags); |
| 668 | } |
| 669 | current_offset_ += width; |
| 670 | BasicBlock *next_block = FindBlock(current_offset_, /* split */ false, /* create */ |
| 671 | false, /* immed_pred_block_p */ NULL); |
| 672 | if (next_block) { |
| 673 | /* |
| 674 | * The next instruction could be the target of a previously parsed |
| 675 | * forward branch so a block is already created. If the current |
| 676 | * instruction is not an unconditional branch, connect them through |
| 677 | * the fall-through link. |
| 678 | */ |
| 679 | DCHECK(cur_block->fall_through == NULL || |
| 680 | cur_block->fall_through == next_block || |
| 681 | cur_block->fall_through == exit_block_); |
| 682 | |
| 683 | if ((cur_block->fall_through == NULL) && (flags & Instruction::kContinue)) { |
| 684 | cur_block->fall_through = next_block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 685 | next_block->predecessors->Insert(cur_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 686 | } |
| 687 | cur_block = next_block; |
| 688 | } |
| 689 | } |
| 690 | if (cu_->enable_debug & (1 << kDebugDumpCFG)) { |
| 691 | DumpCFG("/sdcard/1_post_parse_cfg/", true); |
| 692 | } |
| 693 | |
| 694 | if (cu_->verbose) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 695 | DumpMIRGraph(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 699 | void MIRGraph::ShowOpcodeStats() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 700 | DCHECK(opcode_count_ != NULL); |
| 701 | LOG(INFO) << "Opcode Count"; |
| 702 | for (int i = 0; i < kNumPackedOpcodes; i++) { |
| 703 | if (opcode_count_[i] != 0) { |
| 704 | LOG(INFO) << "-C- " << Instruction::Name(static_cast<Instruction::Code>(i)) |
| 705 | << " " << opcode_count_[i]; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | // TODO: use a configurable base prefix, and adjust callers to supply pass name. |
| 711 | /* Dump the CFG into a DOT graph */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 712 | void MIRGraph::DumpCFG(const char* dir_prefix, bool all_blocks) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 713 | FILE* file; |
| 714 | std::string fname(PrettyMethod(cu_->method_idx, *cu_->dex_file)); |
| 715 | ReplaceSpecialChars(fname); |
| 716 | fname = StringPrintf("%s%s%x.dot", dir_prefix, fname.c_str(), |
| 717 | GetEntryBlock()->fall_through->start_offset); |
| 718 | file = fopen(fname.c_str(), "w"); |
| 719 | if (file == NULL) { |
| 720 | return; |
| 721 | } |
| 722 | fprintf(file, "digraph G {\n"); |
| 723 | |
| 724 | fprintf(file, " rankdir=TB\n"); |
| 725 | |
| 726 | int num_blocks = all_blocks ? GetNumBlocks() : num_reachable_blocks_; |
| 727 | int idx; |
| 728 | |
| 729 | for (idx = 0; idx < num_blocks; idx++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 730 | int block_idx = all_blocks ? idx : dfs_order_->Get(idx); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 731 | BasicBlock *bb = GetBasicBlock(block_idx); |
| 732 | if (bb == NULL) break; |
| 733 | if (bb->block_type == kDead) continue; |
| 734 | if (bb->block_type == kEntryBlock) { |
| 735 | fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id); |
| 736 | } else if (bb->block_type == kExitBlock) { |
| 737 | fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id); |
| 738 | } else if (bb->block_type == kDalvikByteCode) { |
| 739 | fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n", |
| 740 | bb->start_offset, bb->id); |
| 741 | const MIR *mir; |
| 742 | fprintf(file, " {block id %d\\l}%s\\\n", bb->id, |
| 743 | bb->first_mir_insn ? " | " : " "); |
| 744 | for (mir = bb->first_mir_insn; mir; mir = mir->next) { |
| 745 | int opcode = mir->dalvikInsn.opcode; |
| 746 | fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset, |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 747 | mir->ssa_rep ? GetDalvikDisassembly(mir) : |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 748 | (opcode < kMirOpFirst) ? Instruction::Name(mir->dalvikInsn.opcode) : |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 749 | extended_mir_op_names_[opcode - kMirOpFirst], |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 750 | (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ", |
| 751 | (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ", |
| 752 | mir->next ? " | " : " "); |
| 753 | } |
| 754 | fprintf(file, " }\"];\n\n"); |
| 755 | } else if (bb->block_type == kExceptionHandling) { |
| 756 | char block_name[BLOCK_NAME_LEN]; |
| 757 | |
| 758 | GetBlockName(bb, block_name); |
| 759 | fprintf(file, " %s [shape=invhouse];\n", block_name); |
| 760 | } |
| 761 | |
| 762 | char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN]; |
| 763 | |
| 764 | if (bb->taken) { |
| 765 | GetBlockName(bb, block_name1); |
| 766 | GetBlockName(bb->taken, block_name2); |
| 767 | fprintf(file, " %s:s -> %s:n [style=dotted]\n", |
| 768 | block_name1, block_name2); |
| 769 | } |
| 770 | if (bb->fall_through) { |
| 771 | GetBlockName(bb, block_name1); |
| 772 | GetBlockName(bb->fall_through, block_name2); |
| 773 | fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2); |
| 774 | } |
| 775 | |
| 776 | if (bb->successor_block_list.block_list_type != kNotUsed) { |
| 777 | fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n", |
| 778 | bb->start_offset, bb->id, |
| 779 | (bb->successor_block_list.block_list_type == kCatch) ? |
| 780 | "Mrecord" : "record"); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 781 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_block_list.blocks); |
| 782 | SuccessorBlockInfo *successor_block_info = iterator.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 783 | |
| 784 | int succ_id = 0; |
| 785 | while (true) { |
| 786 | if (successor_block_info == NULL) break; |
| 787 | |
| 788 | BasicBlock *dest_block = successor_block_info->block; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 789 | SuccessorBlockInfo *next_successor_block_info = iterator.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 790 | |
| 791 | fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n", |
| 792 | succ_id++, |
| 793 | successor_block_info->key, |
| 794 | dest_block->start_offset, |
| 795 | (next_successor_block_info != NULL) ? " | " : " "); |
| 796 | |
| 797 | successor_block_info = next_successor_block_info; |
| 798 | } |
| 799 | fprintf(file, " }\"];\n\n"); |
| 800 | |
| 801 | GetBlockName(bb, block_name1); |
| 802 | fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n", |
| 803 | block_name1, bb->start_offset, bb->id); |
| 804 | |
| 805 | if (bb->successor_block_list.block_list_type == kPackedSwitch || |
| 806 | bb->successor_block_list.block_list_type == kSparseSwitch) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 807 | GrowableArray<SuccessorBlockInfo*>::Iterator iter(bb->successor_block_list.blocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 808 | |
| 809 | succ_id = 0; |
| 810 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 811 | SuccessorBlockInfo *successor_block_info = iter.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 812 | if (successor_block_info == NULL) break; |
| 813 | |
| 814 | BasicBlock *dest_block = successor_block_info->block; |
| 815 | |
| 816 | GetBlockName(dest_block, block_name2); |
| 817 | fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset, |
| 818 | bb->id, succ_id++, block_name2); |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | fprintf(file, "\n"); |
| 823 | |
| 824 | if (cu_->verbose) { |
| 825 | /* Display the dominator tree */ |
| 826 | GetBlockName(bb, block_name1); |
| 827 | fprintf(file, " cfg%s [label=\"%s\", shape=none];\n", |
| 828 | block_name1, block_name1); |
| 829 | if (bb->i_dom) { |
| 830 | GetBlockName(bb->i_dom, block_name2); |
| 831 | fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | fprintf(file, "}\n"); |
| 836 | fclose(file); |
| 837 | } |
| 838 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 839 | /* Insert an MIR instruction to the end of a basic block */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 840 | void MIRGraph::AppendMIR(BasicBlock* bb, MIR* mir) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 841 | if (bb->first_mir_insn == NULL) { |
| 842 | DCHECK(bb->last_mir_insn == NULL); |
| 843 | bb->last_mir_insn = bb->first_mir_insn = mir; |
| 844 | mir->prev = mir->next = NULL; |
| 845 | } else { |
| 846 | bb->last_mir_insn->next = mir; |
| 847 | mir->prev = bb->last_mir_insn; |
| 848 | mir->next = NULL; |
| 849 | bb->last_mir_insn = mir; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | /* Insert an MIR instruction to the head of a basic block */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 854 | void MIRGraph::PrependMIR(BasicBlock* bb, MIR* mir) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 855 | if (bb->first_mir_insn == NULL) { |
| 856 | DCHECK(bb->last_mir_insn == NULL); |
| 857 | bb->last_mir_insn = bb->first_mir_insn = mir; |
| 858 | mir->prev = mir->next = NULL; |
| 859 | } else { |
| 860 | bb->first_mir_insn->prev = mir; |
| 861 | mir->next = bb->first_mir_insn; |
| 862 | mir->prev = NULL; |
| 863 | bb->first_mir_insn = mir; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* Insert a MIR instruction after the specified MIR */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 868 | void MIRGraph::InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 869 | new_mir->prev = current_mir; |
| 870 | new_mir->next = current_mir->next; |
| 871 | current_mir->next = new_mir; |
| 872 | |
| 873 | if (new_mir->next) { |
| 874 | /* Is not the last MIR in the block */ |
| 875 | new_mir->next->prev = new_mir; |
| 876 | } else { |
| 877 | /* Is the last MIR in the block */ |
| 878 | bb->last_mir_insn = new_mir; |
| 879 | } |
| 880 | } |
| 881 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 882 | char* MIRGraph::GetDalvikDisassembly(const MIR* mir) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 883 | DecodedInstruction insn = mir->dalvikInsn; |
| 884 | std::string str; |
| 885 | int flags = 0; |
| 886 | int opcode = insn.opcode; |
| 887 | char* ret; |
| 888 | bool nop = false; |
| 889 | SSARepresentation* ssa_rep = mir->ssa_rep; |
| 890 | Instruction::Format dalvik_format = Instruction::k10x; // Default to no-operand format |
| 891 | int defs = (ssa_rep != NULL) ? ssa_rep->num_defs : 0; |
| 892 | int uses = (ssa_rep != NULL) ? ssa_rep->num_uses : 0; |
| 893 | |
| 894 | // Handle special cases. |
| 895 | if ((opcode == kMirOpCheck) || (opcode == kMirOpCheckPart2)) { |
| 896 | str.append(extended_mir_op_names_[opcode - kMirOpFirst]); |
| 897 | str.append(": "); |
| 898 | // Recover the original Dex instruction |
| 899 | insn = mir->meta.throw_insn->dalvikInsn; |
| 900 | ssa_rep = mir->meta.throw_insn->ssa_rep; |
| 901 | defs = ssa_rep->num_defs; |
| 902 | uses = ssa_rep->num_uses; |
| 903 | opcode = insn.opcode; |
| 904 | } else if (opcode == kMirOpNop) { |
| 905 | str.append("["); |
| 906 | insn.opcode = mir->meta.original_opcode; |
| 907 | opcode = mir->meta.original_opcode; |
| 908 | nop = true; |
| 909 | } |
| 910 | |
| 911 | if (opcode >= kMirOpFirst) { |
| 912 | str.append(extended_mir_op_names_[opcode - kMirOpFirst]); |
| 913 | } else { |
| 914 | dalvik_format = Instruction::FormatOf(insn.opcode); |
| 915 | flags = Instruction::FlagsOf(insn.opcode); |
| 916 | str.append(Instruction::Name(insn.opcode)); |
| 917 | } |
| 918 | |
| 919 | if (opcode == kMirOpPhi) { |
| 920 | int* incoming = reinterpret_cast<int*>(insn.vB); |
| 921 | str.append(StringPrintf(" %s = (%s", |
| 922 | GetSSANameWithConst(ssa_rep->defs[0], true).c_str(), |
| 923 | GetSSANameWithConst(ssa_rep->uses[0], true).c_str())); |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 924 | str.append(StringPrintf(":%d", incoming[0])); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 925 | int i; |
| 926 | for (i = 1; i < uses; i++) { |
| 927 | str.append(StringPrintf(", %s:%d", |
| 928 | GetSSANameWithConst(ssa_rep->uses[i], true).c_str(), |
| 929 | incoming[i])); |
| 930 | } |
| 931 | str.append(")"); |
| 932 | } else if ((flags & Instruction::kBranch) != 0) { |
| 933 | // For branches, decode the instructions to print out the branch targets. |
| 934 | int offset = 0; |
| 935 | switch (dalvik_format) { |
| 936 | case Instruction::k21t: |
| 937 | str.append(StringPrintf(" %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str())); |
| 938 | offset = insn.vB; |
| 939 | break; |
| 940 | case Instruction::k22t: |
| 941 | str.append(StringPrintf(" %s, %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str(), |
| 942 | GetSSANameWithConst(ssa_rep->uses[1], false).c_str())); |
| 943 | offset = insn.vC; |
| 944 | break; |
| 945 | case Instruction::k10t: |
| 946 | case Instruction::k20t: |
| 947 | case Instruction::k30t: |
| 948 | offset = insn.vA; |
| 949 | break; |
| 950 | default: |
| 951 | LOG(FATAL) << "Unexpected branch format " << dalvik_format << " from " << insn.opcode; |
| 952 | } |
| 953 | str.append(StringPrintf(" 0x%x (%c%x)", mir->offset + offset, |
| 954 | offset > 0 ? '+' : '-', offset > 0 ? offset : -offset)); |
| 955 | } else { |
| 956 | // For invokes-style formats, treat wide regs as a pair of singles |
| 957 | bool show_singles = ((dalvik_format == Instruction::k35c) || |
| 958 | (dalvik_format == Instruction::k3rc)); |
| 959 | if (defs != 0) { |
| 960 | str.append(StringPrintf(" %s", GetSSANameWithConst(ssa_rep->defs[0], false).c_str())); |
| 961 | if (uses != 0) { |
| 962 | str.append(", "); |
| 963 | } |
| 964 | } |
| 965 | for (int i = 0; i < uses; i++) { |
| 966 | str.append( |
| 967 | StringPrintf(" %s", GetSSANameWithConst(ssa_rep->uses[i], show_singles).c_str())); |
| 968 | if (!show_singles && (reg_location_ != NULL) && reg_location_[i].wide) { |
| 969 | // For the listing, skip the high sreg. |
| 970 | i++; |
| 971 | } |
| 972 | if (i != (uses -1)) { |
| 973 | str.append(","); |
| 974 | } |
| 975 | } |
| 976 | switch (dalvik_format) { |
| 977 | case Instruction::k11n: // Add one immediate from vB |
| 978 | case Instruction::k21s: |
| 979 | case Instruction::k31i: |
| 980 | case Instruction::k21h: |
| 981 | str.append(StringPrintf(", #%d", insn.vB)); |
| 982 | break; |
| 983 | case Instruction::k51l: // Add one wide immediate |
| 984 | str.append(StringPrintf(", #%lld", insn.vB_wide)); |
| 985 | break; |
| 986 | case Instruction::k21c: // One register, one string/type/method index |
| 987 | case Instruction::k31c: |
| 988 | str.append(StringPrintf(", index #%d", insn.vB)); |
| 989 | break; |
| 990 | case Instruction::k22c: // Two registers, one string/type/method index |
| 991 | str.append(StringPrintf(", index #%d", insn.vC)); |
| 992 | break; |
| 993 | case Instruction::k22s: // Add one immediate from vC |
| 994 | case Instruction::k22b: |
| 995 | str.append(StringPrintf(", #%d", insn.vC)); |
| 996 | break; |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame^] | 997 | default: { |
| 998 | // Nothing left to print |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 999 | } |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame^] | 1000 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1001 | } |
| 1002 | if (nop) { |
| 1003 | str.append("]--optimized away"); |
| 1004 | } |
| 1005 | int length = str.length() + 1; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1006 | ret = static_cast<char*>(arena_->NewMem(length, false, ArenaAllocator::kAllocDFInfo)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1007 | strncpy(ret, str.c_str(), length); |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | /* Turn method name into a legal Linux file name */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1012 | void MIRGraph::ReplaceSpecialChars(std::string& str) { |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 1013 | static const struct { const char before; const char after; } match[] = { |
| 1014 | {'/', '-'}, {';', '#'}, {' ', '#'}, {'$', '+'}, |
| 1015 | {'(', '@'}, {')', '@'}, {'<', '='}, {'>', '='} |
| 1016 | }; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1017 | for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) { |
| 1018 | std::replace(str.begin(), str.end(), match[i].before, match[i].after); |
| 1019 | } |
| 1020 | } |
| 1021 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1022 | std::string MIRGraph::GetSSAName(int ssa_reg) { |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 1023 | // TODO: This value is needed for LLVM and debugging. Currently, we compute this and then copy to |
| 1024 | // the arena. We should be smarter and just place straight into the arena, or compute the |
| 1025 | // value more lazily. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1026 | return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg)); |
| 1027 | } |
| 1028 | |
| 1029 | // Similar to GetSSAName, but if ssa name represents an immediate show that as well. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1030 | std::string MIRGraph::GetSSANameWithConst(int ssa_reg, bool singles_only) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1031 | if (reg_location_ == NULL) { |
| 1032 | // Pre-SSA - just use the standard name |
| 1033 | return GetSSAName(ssa_reg); |
| 1034 | } |
| 1035 | if (IsConst(reg_location_[ssa_reg])) { |
| 1036 | if (!singles_only && reg_location_[ssa_reg].wide) { |
| 1037 | return StringPrintf("v%d_%d#0x%llx", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg), |
| 1038 | ConstantValueWide(reg_location_[ssa_reg])); |
| 1039 | } else { |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 1040 | return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg), |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1041 | ConstantValue(reg_location_[ssa_reg])); |
| 1042 | } |
| 1043 | } else { |
| 1044 | return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg)); |
| 1045 | } |
| 1046 | } |
| 1047 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1048 | void MIRGraph::GetBlockName(BasicBlock* bb, char* name) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1049 | switch (bb->block_type) { |
| 1050 | case kEntryBlock: |
| 1051 | snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id); |
| 1052 | break; |
| 1053 | case kExitBlock: |
| 1054 | snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id); |
| 1055 | break; |
| 1056 | case kDalvikByteCode: |
| 1057 | snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id); |
| 1058 | break; |
| 1059 | case kExceptionHandling: |
| 1060 | snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset, |
| 1061 | bb->id); |
| 1062 | break; |
| 1063 | default: |
| 1064 | snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id); |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1069 | const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1070 | // FIXME: use current code unit for inline support. |
| 1071 | const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx); |
| 1072 | return cu_->dex_file->GetShorty(method_id.proto_idx_); |
| 1073 | } |
| 1074 | |
| 1075 | /* Debug Utility - dump a compilation unit */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1076 | void MIRGraph::DumpMIRGraph() { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1077 | BasicBlock* bb; |
| 1078 | const char* block_type_names[] = { |
| 1079 | "Entry Block", |
| 1080 | "Code Block", |
| 1081 | "Exit Block", |
| 1082 | "Exception Handling", |
| 1083 | "Catch Block" |
| 1084 | }; |
| 1085 | |
| 1086 | LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 1087 | LOG(INFO) << cu_->insns << " insns"; |
| 1088 | LOG(INFO) << GetNumBlocks() << " blocks in total"; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1089 | GrowableArray<BasicBlock*>::Iterator iterator(&block_list_); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1090 | |
| 1091 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1092 | bb = iterator.Next(); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1093 | if (bb == NULL) break; |
| 1094 | LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)", |
| 1095 | bb->id, |
| 1096 | block_type_names[bb->block_type], |
| 1097 | bb->start_offset, |
| 1098 | bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset, |
| 1099 | bb->last_mir_insn ? "" : " empty"); |
| 1100 | if (bb->taken) { |
| 1101 | LOG(INFO) << " Taken branch: block " << bb->taken->id |
| 1102 | << "(0x" << std::hex << bb->taken->start_offset << ")"; |
| 1103 | } |
| 1104 | if (bb->fall_through) { |
| 1105 | LOG(INFO) << " Fallthrough : block " << bb->fall_through->id |
| 1106 | << " (0x" << std::hex << bb->fall_through->start_offset << ")"; |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Build an array of location records for the incoming arguments. |
| 1113 | * Note: one location record per word of arguments, with dummy |
| 1114 | * high-word loc for wide arguments. Also pull up any following |
| 1115 | * MOVE_RESULT and incorporate it into the invoke. |
| 1116 | */ |
| 1117 | CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1118 | bool is_range) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1119 | CallInfo* info = static_cast<CallInfo*>(arena_->NewMem(sizeof(CallInfo), true, |
| 1120 | ArenaAllocator::kAllocMisc)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1121 | MIR* move_result_mir = FindMoveResult(bb, mir); |
| 1122 | if (move_result_mir == NULL) { |
| 1123 | info->result.location = kLocInvalid; |
| 1124 | } else { |
| 1125 | info->result = GetRawDest(move_result_mir); |
| 1126 | move_result_mir->meta.original_opcode = move_result_mir->dalvikInsn.opcode; |
| 1127 | move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 1128 | } |
| 1129 | info->num_arg_words = mir->ssa_rep->num_uses; |
| 1130 | info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*> |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1131 | (arena_->NewMem(sizeof(RegLocation) * info->num_arg_words, false, |
| 1132 | ArenaAllocator::kAllocMisc)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1133 | for (int i = 0; i < info->num_arg_words; i++) { |
| 1134 | info->args[i] = GetRawSrc(mir, i); |
| 1135 | } |
| 1136 | info->opt_flags = mir->optimization_flags; |
| 1137 | info->type = type; |
| 1138 | info->is_range = is_range; |
| 1139 | info->index = mir->dalvikInsn.vB; |
| 1140 | info->offset = mir->offset; |
| 1141 | return info; |
| 1142 | } |
| 1143 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1144 | // Allocate a new basic block. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1145 | BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1146 | BasicBlock* bb = static_cast<BasicBlock*>(arena_->NewMem(sizeof(BasicBlock), true, |
| 1147 | ArenaAllocator::kAllocBB)); |
| 1148 | bb->block_type = block_type; |
| 1149 | bb->id = block_id; |
| 1150 | // TUNING: better estimate of the exit block predecessors? |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1151 | bb->predecessors = new (arena_) GrowableArray<BasicBlock*>(arena_, |
| 1152 | (block_type == kExitBlock) ? 2048 : 2, |
| 1153 | kGrowableArrayPredecessors); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1154 | bb->successor_block_list.block_list_type = kNotUsed; |
| 1155 | block_id_map_.Put(block_id, block_id); |
| 1156 | return bb; |
| 1157 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1158 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1159 | } // namespace art |