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 | |
Nicolas Geoffray | f3e2cc4 | 2014-02-18 18:37:26 +0000 | [diff] [blame] | 17 | #include "mir_graph.h" |
| 18 | |
| 19 | #include <inttypes.h> |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 20 | #include <queue> |
Nicolas Geoffray | f3e2cc4 | 2014-02-18 18:37:26 +0000 | [diff] [blame] | 21 | |
Ian Rogers | 6282dc1 | 2013-04-18 15:54:02 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 23 | #include "compiler_internals.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 24 | #include "dex_file-inl.h" |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 25 | #include "dex_instruction-inl.h" |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 26 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
| 27 | #include "dex/quick/dex_file_method_inliner.h" |
Nicolas Geoffray | f3e2cc4 | 2014-02-18 18:37:26 +0000 | [diff] [blame] | 28 | #include "leb128.h" |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 29 | #include "pass_driver_me_post_opt.h" |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 30 | #include "utils/scoped_arena_containers.h" |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 31 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 32 | namespace art { |
| 33 | |
| 34 | #define MAX_PATTERN_LEN 5 |
| 35 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 36 | const char* MIRGraph::extended_mir_op_names_[kMirOpLast - kMirOpFirst] = { |
| 37 | "Phi", |
| 38 | "Copy", |
| 39 | "FusedCmplFloat", |
| 40 | "FusedCmpgFloat", |
| 41 | "FusedCmplDouble", |
| 42 | "FusedCmpgDouble", |
| 43 | "FusedCmpLong", |
| 44 | "Nop", |
| 45 | "OpNullCheck", |
| 46 | "OpRangeCheck", |
| 47 | "OpDivZeroCheck", |
| 48 | "Check1", |
| 49 | "Check2", |
| 50 | "Select", |
Mark Mendell | d65c51a | 2014-04-29 16:55:20 -0400 | [diff] [blame] | 51 | "ConstVector", |
| 52 | "MoveVector", |
| 53 | "PackedMultiply", |
| 54 | "PackedAddition", |
| 55 | "PackedSubtract", |
| 56 | "PackedShiftLeft", |
| 57 | "PackedSignedShiftRight", |
| 58 | "PackedUnsignedShiftRight", |
| 59 | "PackedAnd", |
| 60 | "PackedOr", |
| 61 | "PackedXor", |
| 62 | "PackedAddReduce", |
| 63 | "PackedReduce", |
| 64 | "PackedSet", |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 67 | MIRGraph::MIRGraph(CompilationUnit* cu, ArenaAllocator* arena) |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 68 | : reg_location_(NULL), |
| 69 | cu_(cu), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 70 | ssa_base_vregs_(NULL), |
| 71 | ssa_subscripts_(NULL), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 72 | vreg_to_ssa_map_(NULL), |
| 73 | ssa_last_defs_(NULL), |
| 74 | is_constant_v_(NULL), |
| 75 | constant_values_(NULL), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 76 | use_counts_(arena, 256, kGrowableArrayMisc), |
| 77 | raw_use_counts_(arena, 256, kGrowableArrayMisc), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 78 | num_reachable_blocks_(0), |
Jean Christophe Beyler | 4896d7b | 2014-05-01 15:36:22 -0700 | [diff] [blame] | 79 | max_num_reachable_blocks_(0), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 80 | dfs_order_(NULL), |
| 81 | dfs_post_order_(NULL), |
| 82 | dom_post_order_traversal_(NULL), |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 83 | topological_order_(nullptr), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 84 | i_dom_list_(NULL), |
| 85 | def_block_matrix_(NULL), |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 86 | temp_scoped_alloc_(), |
| 87 | temp_insn_data_(nullptr), |
| 88 | temp_bit_vector_size_(0u), |
| 89 | temp_bit_vector_(nullptr), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 90 | block_list_(arena, 100, kGrowableArrayBlockList), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 91 | try_block_addr_(NULL), |
| 92 | entry_block_(NULL), |
| 93 | exit_block_(NULL), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 94 | num_blocks_(0), |
| 95 | current_code_item_(NULL), |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 96 | dex_pc_to_block_map_(arena, 0, kGrowableArrayMisc), |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 97 | current_method_(kInvalidEntry), |
| 98 | current_offset_(kInvalidEntry), |
| 99 | def_count_(0), |
| 100 | opcode_count_(NULL), |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 101 | num_ssa_regs_(0), |
| 102 | method_sreg_(0), |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 103 | attributes_(METHOD_IS_LEAF), // Start with leaf assumption, change on encountering invoke. |
| 104 | checkstats_(NULL), |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 105 | arena_(arena), |
| 106 | backward_branches_(0), |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 107 | forward_branches_(0), |
| 108 | compiler_temps_(arena, 6, kGrowableArrayMisc), |
| 109 | num_non_special_compiler_temps_(0), |
buzbee | b1f1d64 | 2014-02-27 12:55:32 -0800 | [diff] [blame] | 110 | max_available_non_special_compiler_temps_(0), |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 111 | punt_to_interpreter_(false), |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 112 | merged_df_flags_(0u), |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 113 | ifield_lowering_infos_(arena, 0u), |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 114 | sfield_lowering_infos_(arena, 0u), |
Wei Jin | 04f4d8a | 2014-05-29 18:04:29 -0700 | [diff] [blame] | 115 | method_lowering_infos_(arena, 0u), |
| 116 | gen_suspend_test_list_(arena, 0u) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 117 | try_block_addr_ = new (arena_) ArenaBitVector(arena_, 0, true /* expandable */); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 118 | max_available_special_compiler_temps_ = std::abs(static_cast<int>(kVRegNonSpecialTempBaseReg)) |
| 119 | - std::abs(static_cast<int>(kVRegTempBaseReg)); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Ian Rogers | 6282dc1 | 2013-04-18 15:54:02 -0700 | [diff] [blame] | 122 | MIRGraph::~MIRGraph() { |
| 123 | STLDeleteElements(&m_units_); |
| 124 | } |
| 125 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 126 | /* |
| 127 | * Parse an instruction, return the length of the instruction |
| 128 | */ |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 129 | int MIRGraph::ParseInsn(const uint16_t* code_ptr, MIR::DecodedInstruction* decoded_instruction) { |
| 130 | const Instruction* inst = Instruction::At(code_ptr); |
| 131 | decoded_instruction->opcode = inst->Opcode(); |
| 132 | decoded_instruction->vA = inst->HasVRegA() ? inst->VRegA() : 0; |
| 133 | decoded_instruction->vB = inst->HasVRegB() ? inst->VRegB() : 0; |
| 134 | decoded_instruction->vB_wide = inst->HasWideVRegB() ? inst->WideVRegB() : 0; |
| 135 | decoded_instruction->vC = inst->HasVRegC() ? inst->VRegC() : 0; |
| 136 | if (inst->HasVarArgs()) { |
| 137 | inst->GetVarArgs(decoded_instruction->arg); |
| 138 | } |
| 139 | return inst->SizeInCodeUnits(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | /* Split an existing block from the specified code offset into two */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 144 | BasicBlock* MIRGraph::SplitBlock(DexOffset code_offset, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 145 | BasicBlock* orig_block, BasicBlock** immed_pred_block_p) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 146 | DCHECK_GT(code_offset, orig_block->start_offset); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 147 | MIR* insn = orig_block->first_mir_insn; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 148 | MIR* prev = NULL; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 149 | while (insn) { |
| 150 | if (insn->offset == code_offset) break; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 151 | prev = insn; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 152 | insn = insn->next; |
| 153 | } |
| 154 | if (insn == NULL) { |
| 155 | LOG(FATAL) << "Break split failed"; |
| 156 | } |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 157 | BasicBlock* bottom_block = NewMemBB(kDalvikByteCode, num_blocks_++); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 158 | block_list_.Insert(bottom_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 159 | |
| 160 | bottom_block->start_offset = code_offset; |
| 161 | bottom_block->first_mir_insn = insn; |
| 162 | bottom_block->last_mir_insn = orig_block->last_mir_insn; |
| 163 | |
| 164 | /* If this block was terminated by a return, the flag needs to go with the bottom block */ |
| 165 | bottom_block->terminated_by_return = orig_block->terminated_by_return; |
| 166 | orig_block->terminated_by_return = false; |
| 167 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 168 | /* Handle the taken path */ |
| 169 | bottom_block->taken = orig_block->taken; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 170 | if (bottom_block->taken != NullBasicBlockId) { |
| 171 | orig_block->taken = NullBasicBlockId; |
| 172 | BasicBlock* bb_taken = GetBasicBlock(bottom_block->taken); |
| 173 | bb_taken->predecessors->Delete(orig_block->id); |
| 174 | bb_taken->predecessors->Insert(bottom_block->id); |
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; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 179 | orig_block->fall_through = bottom_block->id; |
| 180 | bottom_block->predecessors->Insert(orig_block->id); |
| 181 | if (bottom_block->fall_through != NullBasicBlockId) { |
| 182 | BasicBlock* bb_fall_through = GetBasicBlock(bottom_block->fall_through); |
| 183 | bb_fall_through->predecessors->Delete(orig_block->id); |
| 184 | bb_fall_through->predecessors->Insert(bottom_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* Handle the successor list */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 188 | if (orig_block->successor_block_list_type != kNotUsed) { |
| 189 | bottom_block->successor_block_list_type = orig_block->successor_block_list_type; |
| 190 | bottom_block->successor_blocks = orig_block->successor_blocks; |
| 191 | orig_block->successor_block_list_type = kNotUsed; |
| 192 | orig_block->successor_blocks = NULL; |
| 193 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bottom_block->successor_blocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 194 | while (true) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 195 | SuccessorBlockInfo* successor_block_info = iterator.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 196 | if (successor_block_info == NULL) break; |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 197 | BasicBlock* bb = GetBasicBlock(successor_block_info->block); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 198 | bb->predecessors->Delete(orig_block->id); |
| 199 | bb->predecessors->Insert(bottom_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 203 | orig_block->last_mir_insn = prev; |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 204 | prev->next = nullptr; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 205 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 206 | /* |
| 207 | * Update the immediate predecessor block pointer so that outgoing edges |
| 208 | * can be applied to the proper block. |
| 209 | */ |
| 210 | if (immed_pred_block_p) { |
| 211 | DCHECK_EQ(*immed_pred_block_p, orig_block); |
| 212 | *immed_pred_block_p = bottom_block; |
| 213 | } |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 214 | |
| 215 | // Associate dex instructions in the bottom block with the new container. |
Vladimir Marko | 4376c87 | 2014-01-23 12:39:29 +0000 | [diff] [blame] | 216 | DCHECK(insn != nullptr); |
| 217 | DCHECK(insn != orig_block->first_mir_insn); |
| 218 | DCHECK(insn == bottom_block->first_mir_insn); |
| 219 | DCHECK_EQ(insn->offset, bottom_block->start_offset); |
| 220 | DCHECK(static_cast<int>(insn->dalvikInsn.opcode) == kMirOpCheck || |
| 221 | !IsPseudoMirOp(insn->dalvikInsn.opcode)); |
| 222 | DCHECK_EQ(dex_pc_to_block_map_.Get(insn->offset), orig_block->id); |
| 223 | MIR* p = insn; |
| 224 | dex_pc_to_block_map_.Put(p->offset, bottom_block->id); |
| 225 | while (p != bottom_block->last_mir_insn) { |
| 226 | p = p->next; |
| 227 | DCHECK(p != nullptr); |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 228 | p->bb = bottom_block->id; |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 229 | int opcode = p->dalvikInsn.opcode; |
| 230 | /* |
| 231 | * Some messiness here to ensure that we only enter real opcodes and only the |
| 232 | * first half of a potentially throwing instruction that has been split into |
Vladimir Marko | 4376c87 | 2014-01-23 12:39:29 +0000 | [diff] [blame] | 233 | * CHECK and work portions. Since the 2nd half of a split operation is always |
| 234 | * the first in a BasicBlock, we can't hit it here. |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 235 | */ |
Vladimir Marko | 4376c87 | 2014-01-23 12:39:29 +0000 | [diff] [blame] | 236 | if ((opcode == kMirOpCheck) || !IsPseudoMirOp(opcode)) { |
| 237 | DCHECK_EQ(dex_pc_to_block_map_.Get(p->offset), orig_block->id); |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 238 | dex_pc_to_block_map_.Put(p->offset, bottom_block->id); |
| 239 | } |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 240 | } |
| 241 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 242 | return bottom_block; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Given a code offset, find out the block that starts with it. If the offset |
| 247 | * is in the middle of an existing block, split it into two. If immed_pred_block_p |
| 248 | * is not non-null and is the block being split, update *immed_pred_block_p to |
| 249 | * point to the bottom block so that outgoing edges can be set up properly |
| 250 | * (by the caller) |
| 251 | * Utilizes a map for fast lookup of the typical cases. |
| 252 | */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 253 | BasicBlock* MIRGraph::FindBlock(DexOffset code_offset, bool split, bool create, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 254 | BasicBlock** immed_pred_block_p) { |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 255 | if (code_offset >= cu_->code_item->insns_size_in_code_units_) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 256 | return NULL; |
| 257 | } |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 258 | |
| 259 | int block_id = dex_pc_to_block_map_.Get(code_offset); |
| 260 | BasicBlock* bb = (block_id == 0) ? NULL : block_list_.Get(block_id); |
| 261 | |
| 262 | if ((bb != NULL) && (bb->start_offset == code_offset)) { |
| 263 | // Does this containing block start with the desired instruction? |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 264 | return bb; |
| 265 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 266 | |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 267 | // No direct hit. |
| 268 | if (!create) { |
| 269 | return NULL; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 270 | } |
| 271 | |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 272 | if (bb != NULL) { |
| 273 | // The target exists somewhere in an existing block. |
| 274 | return SplitBlock(code_offset, bb, bb == *immed_pred_block_p ? immed_pred_block_p : NULL); |
| 275 | } |
| 276 | |
| 277 | // Create a new block. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 278 | bb = NewMemBB(kDalvikByteCode, num_blocks_++); |
| 279 | block_list_.Insert(bb); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 280 | bb->start_offset = code_offset; |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 281 | dex_pc_to_block_map_.Put(bb->start_offset, bb->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 282 | return bb; |
| 283 | } |
| 284 | |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 285 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 286 | /* 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] | 287 | void MIRGraph::ProcessTryCatchBlocks() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 288 | int tries_size = current_code_item_->tries_size_; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 289 | DexOffset offset; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 290 | |
| 291 | if (tries_size == 0) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | for (int i = 0; i < tries_size; i++) { |
| 296 | const DexFile::TryItem* pTry = |
| 297 | DexFile::GetTryItems(*current_code_item_, i); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 298 | DexOffset start_offset = pTry->start_addr_; |
| 299 | DexOffset end_offset = start_offset + pTry->insn_count_; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 300 | for (offset = start_offset; offset < end_offset; offset++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 301 | try_block_addr_->SetBit(offset); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 305 | // Iterate over each of the handlers to enqueue the empty Catch blocks. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 306 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*current_code_item_, 0); |
| 307 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 308 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 309 | CatchHandlerIterator iterator(handlers_ptr); |
| 310 | for (; iterator.HasNext(); iterator.Next()) { |
| 311 | uint32_t address = iterator.GetHandlerAddress(); |
| 312 | FindBlock(address, false /* split */, true /*create*/, |
| 313 | /* immed_pred_block_p */ NULL); |
| 314 | } |
| 315 | handlers_ptr = iterator.EndDataPointer(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* Process instructions with the kBranch flag */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 320 | BasicBlock* MIRGraph::ProcessCanBranch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset, |
| 321 | int width, int flags, const uint16_t* code_ptr, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 322 | const uint16_t* code_end) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 323 | DexOffset target = cur_offset; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 324 | switch (insn->dalvikInsn.opcode) { |
| 325 | case Instruction::GOTO: |
| 326 | case Instruction::GOTO_16: |
| 327 | case Instruction::GOTO_32: |
| 328 | target += insn->dalvikInsn.vA; |
| 329 | break; |
| 330 | case Instruction::IF_EQ: |
| 331 | case Instruction::IF_NE: |
| 332 | case Instruction::IF_LT: |
| 333 | case Instruction::IF_GE: |
| 334 | case Instruction::IF_GT: |
| 335 | case Instruction::IF_LE: |
| 336 | cur_block->conditional_branch = true; |
| 337 | target += insn->dalvikInsn.vC; |
| 338 | break; |
| 339 | case Instruction::IF_EQZ: |
| 340 | case Instruction::IF_NEZ: |
| 341 | case Instruction::IF_LTZ: |
| 342 | case Instruction::IF_GEZ: |
| 343 | case Instruction::IF_GTZ: |
| 344 | case Instruction::IF_LEZ: |
| 345 | cur_block->conditional_branch = true; |
| 346 | target += insn->dalvikInsn.vB; |
| 347 | break; |
| 348 | default: |
| 349 | LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set"; |
| 350 | } |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 351 | CountBranch(target); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 352 | BasicBlock* taken_block = FindBlock(target, /* split */ true, /* create */ true, |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 353 | /* immed_pred_block_p */ &cur_block); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 354 | cur_block->taken = taken_block->id; |
| 355 | taken_block->predecessors->Insert(cur_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 356 | |
| 357 | /* Always terminate the current block for conditional branches */ |
| 358 | if (flags & Instruction::kContinue) { |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 359 | BasicBlock* fallthrough_block = FindBlock(cur_offset + width, |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 360 | /* |
| 361 | * If the method is processed |
| 362 | * in sequential order from the |
| 363 | * beginning, we don't need to |
| 364 | * specify split for continue |
| 365 | * blocks. However, this |
| 366 | * routine can be called by |
| 367 | * compileLoop, which starts |
| 368 | * parsing the method from an |
| 369 | * arbitrary address in the |
| 370 | * method body. |
| 371 | */ |
| 372 | true, |
| 373 | /* create */ |
| 374 | true, |
| 375 | /* immed_pred_block_p */ |
| 376 | &cur_block); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 377 | cur_block->fall_through = fallthrough_block->id; |
| 378 | fallthrough_block->predecessors->Insert(cur_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 379 | } else if (code_ptr < code_end) { |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 380 | FindBlock(cur_offset + width, /* split */ false, /* create */ true, |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 381 | /* immed_pred_block_p */ NULL); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 382 | } |
| 383 | return cur_block; |
| 384 | } |
| 385 | |
| 386 | /* Process instructions with the kSwitch flag */ |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 387 | BasicBlock* MIRGraph::ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset, |
| 388 | int width, int flags) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 389 | const uint16_t* switch_data = |
| 390 | reinterpret_cast<const uint16_t*>(GetCurrentInsns() + cur_offset + insn->dalvikInsn.vB); |
| 391 | int size; |
| 392 | const int* keyTable; |
| 393 | const int* target_table; |
| 394 | int i; |
| 395 | int first_key; |
| 396 | |
| 397 | /* |
| 398 | * Packed switch data format: |
| 399 | * ushort ident = 0x0100 magic value |
| 400 | * ushort size number of entries in the table |
| 401 | * int first_key first (and lowest) switch case value |
| 402 | * int targets[size] branch targets, relative to switch opcode |
| 403 | * |
| 404 | * Total size is (4+size*2) 16-bit code units. |
| 405 | */ |
| 406 | if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) { |
| 407 | DCHECK_EQ(static_cast<int>(switch_data[0]), |
| 408 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
| 409 | size = switch_data[1]; |
| 410 | first_key = switch_data[2] | (switch_data[3] << 16); |
| 411 | target_table = reinterpret_cast<const int*>(&switch_data[4]); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 412 | keyTable = NULL; // Make the compiler happy. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 413 | /* |
| 414 | * Sparse switch data format: |
| 415 | * ushort ident = 0x0200 magic value |
| 416 | * ushort size number of entries in the table; > 0 |
| 417 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 418 | * int targets[size] branch targets, relative to switch opcode |
| 419 | * |
| 420 | * Total size is (2+size*4) 16-bit code units. |
| 421 | */ |
| 422 | } else { |
| 423 | DCHECK_EQ(static_cast<int>(switch_data[0]), |
| 424 | static_cast<int>(Instruction::kSparseSwitchSignature)); |
| 425 | size = switch_data[1]; |
| 426 | keyTable = reinterpret_cast<const int*>(&switch_data[2]); |
| 427 | target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 428 | first_key = 0; // To make the compiler happy. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 429 | } |
| 430 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 431 | if (cur_block->successor_block_list_type != kNotUsed) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 432 | LOG(FATAL) << "Successor block list already in use: " |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 433 | << static_cast<int>(cur_block->successor_block_list_type); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 434 | } |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 435 | cur_block->successor_block_list_type = |
| 436 | (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? kPackedSwitch : kSparseSwitch; |
| 437 | cur_block->successor_blocks = |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 438 | new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, size, kGrowableArraySuccessorBlocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 439 | |
| 440 | for (i = 0; i < size; i++) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 441 | BasicBlock* case_block = FindBlock(cur_offset + target_table[i], /* split */ true, |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 442 | /* create */ true, /* immed_pred_block_p */ &cur_block); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 443 | SuccessorBlockInfo* successor_block_info = |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 444 | static_cast<SuccessorBlockInfo*>(arena_->Alloc(sizeof(SuccessorBlockInfo), |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 445 | kArenaAllocSuccessor)); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 446 | successor_block_info->block = case_block->id; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 447 | successor_block_info->key = |
| 448 | (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? |
| 449 | first_key + i : keyTable[i]; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 450 | cur_block->successor_blocks->Insert(successor_block_info); |
| 451 | case_block->predecessors->Insert(cur_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* Fall-through case */ |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 455 | BasicBlock* fallthrough_block = FindBlock(cur_offset + width, /* split */ false, |
| 456 | /* create */ true, /* immed_pred_block_p */ NULL); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 457 | cur_block->fall_through = fallthrough_block->id; |
| 458 | fallthrough_block->predecessors->Insert(cur_block->id); |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 459 | return cur_block; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* Process instructions with the kThrow flag */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 463 | BasicBlock* MIRGraph::ProcessCanThrow(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset, |
| 464 | int width, int flags, ArenaBitVector* try_block_addr, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 465 | const uint16_t* code_ptr, const uint16_t* code_end) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 466 | bool in_try_block = try_block_addr->IsBitSet(cur_offset); |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 467 | bool is_throw = (insn->dalvikInsn.opcode == Instruction::THROW); |
| 468 | bool build_all_edges = |
| 469 | (cu_->disable_opt & (1 << kSuppressExceptionEdges)) || is_throw || in_try_block; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 470 | |
| 471 | /* In try block */ |
| 472 | if (in_try_block) { |
| 473 | CatchHandlerIterator iterator(*current_code_item_, cur_offset); |
| 474 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 475 | if (cur_block->successor_block_list_type != kNotUsed) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 476 | LOG(INFO) << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 477 | LOG(FATAL) << "Successor block list already in use: " |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 478 | << static_cast<int>(cur_block->successor_block_list_type); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 479 | } |
| 480 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 481 | cur_block->successor_block_list_type = kCatch; |
| 482 | cur_block->successor_blocks = |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 483 | new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, 2, kGrowableArraySuccessorBlocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 484 | |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 485 | for (; iterator.HasNext(); iterator.Next()) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 486 | BasicBlock* catch_block = FindBlock(iterator.GetHandlerAddress(), false /* split*/, |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 487 | false /* creat */, NULL /* immed_pred_block_p */); |
| 488 | catch_block->catch_entry = true; |
| 489 | if (kIsDebugBuild) { |
| 490 | catches_.insert(catch_block->start_offset); |
| 491 | } |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 492 | SuccessorBlockInfo* successor_block_info = reinterpret_cast<SuccessorBlockInfo*> |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 493 | (arena_->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor)); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 494 | successor_block_info->block = catch_block->id; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 495 | successor_block_info->key = iterator.GetHandlerTypeIndex(); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 496 | cur_block->successor_blocks->Insert(successor_block_info); |
| 497 | catch_block->predecessors->Insert(cur_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 498 | } |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 499 | } else if (build_all_edges) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 500 | BasicBlock* eh_block = NewMemBB(kExceptionHandling, num_blocks_++); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 501 | cur_block->taken = eh_block->id; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 502 | block_list_.Insert(eh_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 503 | eh_block->start_offset = cur_offset; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 504 | eh_block->predecessors->Insert(cur_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 505 | } |
| 506 | |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 507 | if (is_throw) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 508 | cur_block->explicit_throw = true; |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 509 | if (code_ptr < code_end) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 510 | // Force creation of new block following THROW via side-effect. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 511 | FindBlock(cur_offset + width, /* split */ false, /* create */ true, |
| 512 | /* immed_pred_block_p */ NULL); |
| 513 | } |
| 514 | if (!in_try_block) { |
| 515 | // Don't split a THROW that can't rethrow - we're done. |
| 516 | return cur_block; |
| 517 | } |
| 518 | } |
| 519 | |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 520 | if (!build_all_edges) { |
| 521 | /* |
| 522 | * Even though there is an exception edge here, control cannot return to this |
| 523 | * method. Thus, for the purposes of dataflow analysis and optimization, we can |
| 524 | * ignore the edge. Doing this reduces compile time, and increases the scope |
| 525 | * of the basic-block level optimization pass. |
| 526 | */ |
| 527 | return cur_block; |
| 528 | } |
| 529 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 530 | /* |
| 531 | * Split the potentially-throwing instruction into two parts. |
| 532 | * The first half will be a pseudo-op that captures the exception |
| 533 | * edges and terminates the basic block. It always falls through. |
| 534 | * Then, create a new basic block that begins with the throwing instruction |
| 535 | * (minus exceptions). Note: this new basic block must NOT be entered into |
| 536 | * the block_map. If the potentially-throwing instruction is the target of a |
| 537 | * future branch, we need to find the check psuedo half. The new |
| 538 | * basic block containing the work portion of the instruction should |
| 539 | * only be entered via fallthrough from the block containing the |
| 540 | * pseudo exception edge MIR. Note also that this new block is |
| 541 | * not automatically terminated after the work portion, and may |
| 542 | * contain following instructions. |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 543 | * |
| 544 | * Note also that the dex_pc_to_block_map_ entry for the potentially |
| 545 | * throwing instruction will refer to the original basic block. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 546 | */ |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 547 | BasicBlock* new_block = NewMemBB(kDalvikByteCode, num_blocks_++); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 548 | block_list_.Insert(new_block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 549 | new_block->start_offset = insn->offset; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 550 | cur_block->fall_through = new_block->id; |
| 551 | new_block->predecessors->Insert(cur_block->id); |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 552 | MIR* new_insn = NewMIR(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 553 | *new_insn = *insn; |
buzbee | 35ba7f3 | 2014-05-31 08:59:01 -0700 | [diff] [blame] | 554 | insn->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheck); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 555 | // Associate the two halves. |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 556 | insn->meta.throw_insn = new_insn; |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 557 | new_block->AppendMIR(new_insn); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 558 | return new_block; |
| 559 | } |
| 560 | |
| 561 | /* Parse a Dex method and insert it into the MIRGraph at the current insert point. */ |
| 562 | void MIRGraph::InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 563 | InvokeType invoke_type, uint16_t class_def_idx, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 564 | uint32_t method_idx, jobject class_loader, const DexFile& dex_file) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 565 | current_code_item_ = code_item; |
| 566 | method_stack_.push_back(std::make_pair(current_method_, current_offset_)); |
| 567 | current_method_ = m_units_.size(); |
| 568 | current_offset_ = 0; |
| 569 | // TODO: will need to snapshot stack image and use that as the mir context identification. |
| 570 | m_units_.push_back(new DexCompilationUnit(cu_, class_loader, Runtime::Current()->GetClassLinker(), |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 571 | dex_file, current_code_item_, class_def_idx, method_idx, access_flags, |
| 572 | cu_->compiler_driver->GetVerifiedMethod(&dex_file, method_idx))); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 573 | const uint16_t* code_ptr = current_code_item_->insns_; |
| 574 | const uint16_t* code_end = |
| 575 | current_code_item_->insns_ + current_code_item_->insns_size_in_code_units_; |
| 576 | |
| 577 | // TODO: need to rework expansion of block list & try_block_addr when inlining activated. |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 578 | // TUNING: use better estimate of basic blocks for following resize. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 579 | block_list_.Resize(block_list_.Size() + current_code_item_->insns_size_in_code_units_); |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 580 | dex_pc_to_block_map_.SetSize(dex_pc_to_block_map_.Size() + current_code_item_->insns_size_in_code_units_); |
buzbee | bd663de | 2013-09-10 15:41:31 -0700 | [diff] [blame] | 581 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 582 | // TODO: replace with explicit resize routine. Using automatic extension side effect for now. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 583 | try_block_addr_->SetBit(current_code_item_->insns_size_in_code_units_); |
| 584 | try_block_addr_->ClearBit(current_code_item_->insns_size_in_code_units_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 585 | |
| 586 | // If this is the first method, set up default entry and exit blocks. |
| 587 | if (current_method_ == 0) { |
| 588 | DCHECK(entry_block_ == NULL); |
| 589 | DCHECK(exit_block_ == NULL); |
Andreas Gampe | 4439596 | 2014-06-13 13:44:40 -0700 | [diff] [blame] | 590 | DCHECK_EQ(num_blocks_, 0U); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 591 | // Use id 0 to represent a null block. |
| 592 | BasicBlock* null_block = NewMemBB(kNullBlock, num_blocks_++); |
| 593 | DCHECK_EQ(null_block->id, NullBasicBlockId); |
| 594 | null_block->hidden = true; |
| 595 | block_list_.Insert(null_block); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 596 | entry_block_ = NewMemBB(kEntryBlock, num_blocks_++); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 597 | block_list_.Insert(entry_block_); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 598 | exit_block_ = NewMemBB(kExitBlock, num_blocks_++); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 599 | block_list_.Insert(exit_block_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 600 | // TODO: deprecate all "cu->" fields; move what's left to wherever CompilationUnit is allocated. |
| 601 | cu_->dex_file = &dex_file; |
| 602 | cu_->class_def_idx = class_def_idx; |
| 603 | cu_->method_idx = method_idx; |
| 604 | cu_->access_flags = access_flags; |
| 605 | cu_->invoke_type = invoke_type; |
| 606 | cu_->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
| 607 | cu_->num_ins = current_code_item_->ins_size_; |
| 608 | cu_->num_regs = current_code_item_->registers_size_ - cu_->num_ins; |
| 609 | cu_->num_outs = current_code_item_->outs_size_; |
| 610 | cu_->num_dalvik_registers = current_code_item_->registers_size_; |
| 611 | cu_->insns = current_code_item_->insns_; |
| 612 | cu_->code_item = current_code_item_; |
| 613 | } else { |
| 614 | UNIMPLEMENTED(FATAL) << "Nested inlining not implemented."; |
| 615 | /* |
| 616 | * Will need to manage storage for ins & outs, push prevous state and update |
| 617 | * insert point. |
| 618 | */ |
| 619 | } |
| 620 | |
| 621 | /* Current block to record parsed instructions */ |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 622 | BasicBlock* cur_block = NewMemBB(kDalvikByteCode, num_blocks_++); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 623 | DCHECK_EQ(current_offset_, 0U); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 624 | cur_block->start_offset = current_offset_; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 625 | block_list_.Insert(cur_block); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 626 | // TODO: for inlining support, insert at the insert point rather than entry block. |
| 627 | entry_block_->fall_through = cur_block->id; |
| 628 | cur_block->predecessors->Insert(entry_block_->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 629 | |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 630 | /* Identify code range in try blocks and set up the empty catch blocks */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 631 | ProcessTryCatchBlocks(); |
| 632 | |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 633 | uint64_t merged_df_flags = 0u; |
| 634 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 635 | /* Parse all instructions and put them into containing basic blocks */ |
| 636 | while (code_ptr < code_end) { |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 637 | MIR *insn = NewMIR(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 638 | insn->offset = current_offset_; |
| 639 | insn->m_unit_index = current_method_; |
| 640 | int width = ParseInsn(code_ptr, &insn->dalvikInsn); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 641 | Instruction::Code opcode = insn->dalvikInsn.opcode; |
| 642 | if (opcode_count_ != NULL) { |
| 643 | opcode_count_[static_cast<int>(opcode)]++; |
| 644 | } |
| 645 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 646 | int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode); |
buzbee | b1f1d64 | 2014-02-27 12:55:32 -0800 | [diff] [blame] | 647 | int verify_flags = Instruction::VerifyFlagsOf(insn->dalvikInsn.opcode); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 648 | |
Jean Christophe Beyler | cc794c3 | 2014-05-02 09:34:13 -0700 | [diff] [blame] | 649 | uint64_t df_flags = GetDataFlowAttributes(insn); |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 650 | merged_df_flags |= df_flags; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 651 | |
| 652 | if (df_flags & DF_HAS_DEFS) { |
| 653 | def_count_ += (df_flags & DF_A_WIDE) ? 2 : 1; |
| 654 | } |
| 655 | |
buzbee | 1da1e2f | 2013-11-15 13:37:01 -0800 | [diff] [blame] | 656 | if (df_flags & DF_LVN) { |
| 657 | cur_block->use_lvn = true; // Run local value numbering on this basic block. |
| 658 | } |
| 659 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 660 | // Check for inline data block signatures. |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 661 | if (opcode == Instruction::NOP) { |
| 662 | // A simple NOP will have a width of 1 at this point, embedded data NOP > 1. |
| 663 | if ((width == 1) && ((current_offset_ & 0x1) == 0x1) && ((code_end - code_ptr) > 1)) { |
| 664 | // Could be an aligning nop. If an embedded data NOP follows, treat pair as single unit. |
| 665 | uint16_t following_raw_instruction = code_ptr[1]; |
| 666 | if ((following_raw_instruction == Instruction::kSparseSwitchSignature) || |
| 667 | (following_raw_instruction == Instruction::kPackedSwitchSignature) || |
| 668 | (following_raw_instruction == Instruction::kArrayDataSignature)) { |
| 669 | width += Instruction::At(code_ptr + 1)->SizeInCodeUnits(); |
| 670 | } |
| 671 | } |
| 672 | if (width == 1) { |
| 673 | // It is a simple nop - treat normally. |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 674 | cur_block->AppendMIR(insn); |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 675 | } else { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 676 | DCHECK(cur_block->fall_through == NullBasicBlockId); |
| 677 | DCHECK(cur_block->taken == NullBasicBlockId); |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 678 | // Unreachable instruction, mark for no continuation. |
| 679 | flags &= ~Instruction::kContinue; |
| 680 | } |
| 681 | } else { |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 682 | cur_block->AppendMIR(insn); |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 683 | } |
| 684 | |
buzbee | b48819d | 2013-09-14 16:15:25 -0700 | [diff] [blame] | 685 | // Associate the starting dex_pc for this opcode with its containing basic block. |
| 686 | dex_pc_to_block_map_.Put(insn->offset, cur_block->id); |
| 687 | |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 688 | code_ptr += width; |
| 689 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 690 | if (flags & Instruction::kBranch) { |
| 691 | cur_block = ProcessCanBranch(cur_block, insn, current_offset_, |
| 692 | width, flags, code_ptr, code_end); |
| 693 | } else if (flags & Instruction::kReturn) { |
| 694 | cur_block->terminated_by_return = true; |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 695 | cur_block->fall_through = exit_block_->id; |
| 696 | exit_block_->predecessors->Insert(cur_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 697 | /* |
| 698 | * Terminate the current block if there are instructions |
| 699 | * afterwards. |
| 700 | */ |
| 701 | if (code_ptr < code_end) { |
| 702 | /* |
| 703 | * Create a fallthrough block for real instructions |
| 704 | * (incl. NOP). |
| 705 | */ |
buzbee | 2724776 | 2013-07-28 12:03:58 -0700 | [diff] [blame] | 706 | FindBlock(current_offset_ + width, /* split */ false, /* create */ true, |
| 707 | /* immed_pred_block_p */ NULL); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 708 | } |
| 709 | } else if (flags & Instruction::kThrow) { |
| 710 | cur_block = ProcessCanThrow(cur_block, insn, current_offset_, width, flags, try_block_addr_, |
| 711 | code_ptr, code_end); |
| 712 | } else if (flags & Instruction::kSwitch) { |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 713 | cur_block = ProcessCanSwitch(cur_block, insn, current_offset_, width, flags); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 714 | } |
buzbee | b1f1d64 | 2014-02-27 12:55:32 -0800 | [diff] [blame] | 715 | if (verify_flags & Instruction::kVerifyVarArgRange) { |
| 716 | /* |
| 717 | * The Quick backend's runtime model includes a gap between a method's |
| 718 | * argument ("in") vregs and the rest of its vregs. Handling a range instruction |
| 719 | * which spans the gap is somewhat complicated, and should not happen |
| 720 | * in normal usage of dx. Punt to the interpreter. |
| 721 | */ |
| 722 | int first_reg_in_range = insn->dalvikInsn.vC; |
| 723 | int last_reg_in_range = first_reg_in_range + insn->dalvikInsn.vA - 1; |
| 724 | if (IsInVReg(first_reg_in_range) != IsInVReg(last_reg_in_range)) { |
| 725 | punt_to_interpreter_ = true; |
| 726 | } |
| 727 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 728 | current_offset_ += width; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 729 | BasicBlock* next_block = FindBlock(current_offset_, /* split */ false, /* create */ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 730 | false, /* immed_pred_block_p */ NULL); |
| 731 | if (next_block) { |
| 732 | /* |
| 733 | * The next instruction could be the target of a previously parsed |
| 734 | * forward branch so a block is already created. If the current |
| 735 | * instruction is not an unconditional branch, connect them through |
| 736 | * the fall-through link. |
| 737 | */ |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 738 | DCHECK(cur_block->fall_through == NullBasicBlockId || |
| 739 | GetBasicBlock(cur_block->fall_through) == next_block || |
| 740 | GetBasicBlock(cur_block->fall_through) == exit_block_); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 741 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 742 | if ((cur_block->fall_through == NullBasicBlockId) && (flags & Instruction::kContinue)) { |
| 743 | cur_block->fall_through = next_block->id; |
| 744 | next_block->predecessors->Insert(cur_block->id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 745 | } |
| 746 | cur_block = next_block; |
| 747 | } |
| 748 | } |
Vladimir Marko | 3d73ba2 | 2014-03-06 15:18:04 +0000 | [diff] [blame] | 749 | merged_df_flags_ = merged_df_flags; |
Vladimir Marko | 5816ed4 | 2013-11-27 17:04:20 +0000 | [diff] [blame] | 750 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 751 | if (cu_->enable_debug & (1 << kDebugDumpCFG)) { |
| 752 | DumpCFG("/sdcard/1_post_parse_cfg/", true); |
| 753 | } |
| 754 | |
| 755 | if (cu_->verbose) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 756 | DumpMIRGraph(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 760 | void MIRGraph::ShowOpcodeStats() { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 761 | DCHECK(opcode_count_ != NULL); |
| 762 | LOG(INFO) << "Opcode Count"; |
| 763 | for (int i = 0; i < kNumPackedOpcodes; i++) { |
| 764 | if (opcode_count_[i] != 0) { |
| 765 | LOG(INFO) << "-C- " << Instruction::Name(static_cast<Instruction::Code>(i)) |
| 766 | << " " << opcode_count_[i]; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
Jean Christophe Beyler | cc794c3 | 2014-05-02 09:34:13 -0700 | [diff] [blame] | 771 | uint64_t MIRGraph::GetDataFlowAttributes(Instruction::Code opcode) { |
| 772 | DCHECK_LT((size_t) opcode, (sizeof(oat_data_flow_attributes_) / sizeof(oat_data_flow_attributes_[0]))); |
| 773 | return oat_data_flow_attributes_[opcode]; |
| 774 | } |
| 775 | |
| 776 | uint64_t MIRGraph::GetDataFlowAttributes(MIR* mir) { |
| 777 | DCHECK(mir != nullptr); |
| 778 | Instruction::Code opcode = mir->dalvikInsn.opcode; |
| 779 | return GetDataFlowAttributes(opcode); |
| 780 | } |
| 781 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 782 | // TODO: use a configurable base prefix, and adjust callers to supply pass name. |
| 783 | /* Dump the CFG into a DOT graph */ |
Jean Christophe Beyler | d0a5155 | 2014-01-10 14:18:31 -0800 | [diff] [blame] | 784 | void MIRGraph::DumpCFG(const char* dir_prefix, bool all_blocks, const char *suffix) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 785 | FILE* file; |
| 786 | std::string fname(PrettyMethod(cu_->method_idx, *cu_->dex_file)); |
| 787 | ReplaceSpecialChars(fname); |
Jean Christophe Beyler | d0a5155 | 2014-01-10 14:18:31 -0800 | [diff] [blame] | 788 | fname = StringPrintf("%s%s%x%s.dot", dir_prefix, fname.c_str(), |
| 789 | GetBasicBlock(GetEntryBlock()->fall_through)->start_offset, |
| 790 | suffix == nullptr ? "" : suffix); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 791 | file = fopen(fname.c_str(), "w"); |
| 792 | if (file == NULL) { |
| 793 | return; |
| 794 | } |
| 795 | fprintf(file, "digraph G {\n"); |
| 796 | |
| 797 | fprintf(file, " rankdir=TB\n"); |
| 798 | |
| 799 | int num_blocks = all_blocks ? GetNumBlocks() : num_reachable_blocks_; |
| 800 | int idx; |
| 801 | |
| 802 | for (idx = 0; idx < num_blocks; idx++) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 803 | int block_idx = all_blocks ? idx : dfs_order_->Get(idx); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 804 | BasicBlock* bb = GetBasicBlock(block_idx); |
Razvan A Lupusoru | 25bc279 | 2014-03-19 14:27:59 -0700 | [diff] [blame] | 805 | if (bb == NULL) continue; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 806 | if (bb->block_type == kDead) continue; |
| 807 | if (bb->block_type == kEntryBlock) { |
| 808 | fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id); |
| 809 | } else if (bb->block_type == kExitBlock) { |
| 810 | fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id); |
| 811 | } else if (bb->block_type == kDalvikByteCode) { |
| 812 | fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n", |
| 813 | bb->start_offset, bb->id); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 814 | const MIR* mir; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 815 | fprintf(file, " {block id %d\\l}%s\\\n", bb->id, |
| 816 | bb->first_mir_insn ? " | " : " "); |
| 817 | for (mir = bb->first_mir_insn; mir; mir = mir->next) { |
| 818 | int opcode = mir->dalvikInsn.opcode; |
Mark Mendell | d65c51a | 2014-04-29 16:55:20 -0400 | [diff] [blame] | 819 | if (opcode > kMirOpSelect && opcode < kMirOpLast) { |
| 820 | if (opcode == kMirOpConstVector) { |
| 821 | fprintf(file, " {%04x %s %d %d %d %d %d %d\\l}%s\\\n", mir->offset, |
| 822 | extended_mir_op_names_[kMirOpConstVector - kMirOpFirst], |
| 823 | mir->dalvikInsn.vA, |
| 824 | mir->dalvikInsn.vB, |
| 825 | mir->dalvikInsn.arg[0], |
| 826 | mir->dalvikInsn.arg[1], |
| 827 | mir->dalvikInsn.arg[2], |
| 828 | mir->dalvikInsn.arg[3], |
| 829 | mir->next ? " | " : " "); |
| 830 | } else { |
| 831 | fprintf(file, " {%04x %s %d %d %d\\l}%s\\\n", mir->offset, |
| 832 | extended_mir_op_names_[opcode - kMirOpFirst], |
| 833 | mir->dalvikInsn.vA, |
| 834 | mir->dalvikInsn.vB, |
| 835 | mir->dalvikInsn.vC, |
| 836 | mir->next ? " | " : " "); |
| 837 | } |
| 838 | } else { |
| 839 | fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset, |
| 840 | mir->ssa_rep ? GetDalvikDisassembly(mir) : |
buzbee | 35ba7f3 | 2014-05-31 08:59:01 -0700 | [diff] [blame] | 841 | !IsPseudoMirOp(opcode) ? Instruction::Name(mir->dalvikInsn.opcode) : |
Mark Mendell | d65c51a | 2014-04-29 16:55:20 -0400 | [diff] [blame] | 842 | extended_mir_op_names_[opcode - kMirOpFirst], |
| 843 | (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ", |
| 844 | (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ", |
| 845 | mir->next ? " | " : " "); |
| 846 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 847 | } |
| 848 | fprintf(file, " }\"];\n\n"); |
| 849 | } else if (bb->block_type == kExceptionHandling) { |
| 850 | char block_name[BLOCK_NAME_LEN]; |
| 851 | |
| 852 | GetBlockName(bb, block_name); |
| 853 | fprintf(file, " %s [shape=invhouse];\n", block_name); |
| 854 | } |
| 855 | |
| 856 | char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN]; |
| 857 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 858 | if (bb->taken != NullBasicBlockId) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 859 | GetBlockName(bb, block_name1); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 860 | GetBlockName(GetBasicBlock(bb->taken), block_name2); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 861 | fprintf(file, " %s:s -> %s:n [style=dotted]\n", |
| 862 | block_name1, block_name2); |
| 863 | } |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 864 | if (bb->fall_through != NullBasicBlockId) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 865 | GetBlockName(bb, block_name1); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 866 | GetBlockName(GetBasicBlock(bb->fall_through), block_name2); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 867 | fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2); |
| 868 | } |
| 869 | |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 870 | if (bb->successor_block_list_type != kNotUsed) { |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 871 | fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n", |
| 872 | bb->start_offset, bb->id, |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 873 | (bb->successor_block_list_type == kCatch) ? "Mrecord" : "record"); |
| 874 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 875 | SuccessorBlockInfo* successor_block_info = iterator.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 876 | |
| 877 | int succ_id = 0; |
| 878 | while (true) { |
| 879 | if (successor_block_info == NULL) break; |
| 880 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 881 | BasicBlock* dest_block = GetBasicBlock(successor_block_info->block); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 882 | SuccessorBlockInfo *next_successor_block_info = iterator.Next(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 883 | |
| 884 | fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n", |
| 885 | succ_id++, |
| 886 | successor_block_info->key, |
| 887 | dest_block->start_offset, |
| 888 | (next_successor_block_info != NULL) ? " | " : " "); |
| 889 | |
| 890 | successor_block_info = next_successor_block_info; |
| 891 | } |
| 892 | fprintf(file, " }\"];\n\n"); |
| 893 | |
| 894 | GetBlockName(bb, block_name1); |
| 895 | fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n", |
| 896 | block_name1, bb->start_offset, bb->id); |
| 897 | |
Razvan A Lupusoru | 25bc279 | 2014-03-19 14:27:59 -0700 | [diff] [blame] | 898 | // Link the successor pseudo-block with all of its potential targets. |
| 899 | GrowableArray<SuccessorBlockInfo*>::Iterator iter(bb->successor_blocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 900 | |
Razvan A Lupusoru | 25bc279 | 2014-03-19 14:27:59 -0700 | [diff] [blame] | 901 | succ_id = 0; |
| 902 | while (true) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 903 | SuccessorBlockInfo* successor_block_info = iter.Next(); |
Razvan A Lupusoru | 25bc279 | 2014-03-19 14:27:59 -0700 | [diff] [blame] | 904 | if (successor_block_info == NULL) break; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 905 | |
Razvan A Lupusoru | 25bc279 | 2014-03-19 14:27:59 -0700 | [diff] [blame] | 906 | BasicBlock* dest_block = GetBasicBlock(successor_block_info->block); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 907 | |
Razvan A Lupusoru | 25bc279 | 2014-03-19 14:27:59 -0700 | [diff] [blame] | 908 | GetBlockName(dest_block, block_name2); |
| 909 | fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset, |
| 910 | bb->id, succ_id++, block_name2); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | fprintf(file, "\n"); |
| 914 | |
| 915 | if (cu_->verbose) { |
| 916 | /* Display the dominator tree */ |
| 917 | GetBlockName(bb, block_name1); |
| 918 | fprintf(file, " cfg%s [label=\"%s\", shape=none];\n", |
| 919 | block_name1, block_name1); |
| 920 | if (bb->i_dom) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 921 | GetBlockName(GetBasicBlock(bb->i_dom), block_name2); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 922 | fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | fprintf(file, "}\n"); |
| 927 | fclose(file); |
| 928 | } |
| 929 | |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 930 | /* Insert an MIR instruction to the end of a basic block. */ |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 931 | void BasicBlock::AppendMIR(MIR* mir) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 932 | // Insert it after the last MIR. |
| 933 | InsertMIRListAfter(last_mir_insn, mir, mir); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 936 | void BasicBlock::AppendMIRList(MIR* first_list_mir, MIR* last_list_mir) { |
| 937 | // Insert it after the last MIR. |
| 938 | InsertMIRListAfter(last_mir_insn, first_list_mir, last_list_mir); |
| 939 | } |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 940 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 941 | void BasicBlock::AppendMIRList(const std::vector<MIR*>& insns) { |
| 942 | for (std::vector<MIR*>::const_iterator it = insns.begin(); it != insns.end(); it++) { |
| 943 | MIR* new_mir = *it; |
| 944 | |
| 945 | // Add a copy of each MIR. |
| 946 | InsertMIRListAfter(last_mir_insn, new_mir, new_mir); |
| 947 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 950 | /* Insert a MIR instruction after the specified MIR. */ |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 951 | void BasicBlock::InsertMIRAfter(MIR* current_mir, MIR* new_mir) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 952 | InsertMIRListAfter(current_mir, new_mir, new_mir); |
| 953 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 954 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 955 | void BasicBlock::InsertMIRListAfter(MIR* insert_after, MIR* first_list_mir, MIR* last_list_mir) { |
| 956 | // If no MIR, we are done. |
| 957 | if (first_list_mir == nullptr || last_list_mir == nullptr) { |
| 958 | return; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 959 | } |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 960 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 961 | // If insert_after is null, assume BB is empty. |
| 962 | if (insert_after == nullptr) { |
| 963 | first_mir_insn = first_list_mir; |
| 964 | last_mir_insn = last_list_mir; |
| 965 | last_list_mir->next = nullptr; |
| 966 | } else { |
| 967 | MIR* after_list = insert_after->next; |
| 968 | insert_after->next = first_list_mir; |
| 969 | last_list_mir->next = after_list; |
| 970 | if (after_list == nullptr) { |
| 971 | last_mir_insn = last_list_mir; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // Set this BB to be the basic block of the MIRs. |
| 976 | MIR* last = last_list_mir->next; |
| 977 | for (MIR* mir = first_list_mir; mir != last; mir = mir->next) { |
| 978 | mir->bb = id; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | /* Insert an MIR instruction to the head of a basic block. */ |
| 983 | void BasicBlock::PrependMIR(MIR* mir) { |
| 984 | InsertMIRListBefore(first_mir_insn, mir, mir); |
| 985 | } |
| 986 | |
| 987 | void BasicBlock::PrependMIRList(MIR* first_list_mir, MIR* last_list_mir) { |
| 988 | // Insert it before the first MIR. |
| 989 | InsertMIRListBefore(first_mir_insn, first_list_mir, last_list_mir); |
| 990 | } |
| 991 | |
| 992 | void BasicBlock::PrependMIRList(const std::vector<MIR*>& to_add) { |
| 993 | for (std::vector<MIR*>::const_iterator it = to_add.begin(); it != to_add.end(); it++) { |
| 994 | MIR* mir = *it; |
| 995 | |
| 996 | InsertMIRListBefore(first_mir_insn, mir, mir); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | /* Insert a MIR instruction before the specified MIR. */ |
| 1001 | void BasicBlock::InsertMIRBefore(MIR* current_mir, MIR* new_mir) { |
| 1002 | // Insert as a single element list. |
| 1003 | return InsertMIRListBefore(current_mir, new_mir, new_mir); |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | MIR* BasicBlock::FindPreviousMIR(MIR* mir) { |
| 1007 | MIR* current = first_mir_insn; |
| 1008 | |
| 1009 | while (current != nullptr) { |
| 1010 | MIR* next = current->next; |
| 1011 | |
| 1012 | if (next == mir) { |
| 1013 | return current; |
| 1014 | } |
| 1015 | |
| 1016 | current = next; |
| 1017 | } |
| 1018 | |
| 1019 | return nullptr; |
| 1020 | } |
| 1021 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1022 | void BasicBlock::InsertMIRListBefore(MIR* insert_before, MIR* first_list_mir, MIR* last_list_mir) { |
| 1023 | // If no MIR, we are done. |
| 1024 | if (first_list_mir == nullptr || last_list_mir == nullptr) { |
| 1025 | return; |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1028 | // If insert_before is null, assume BB is empty. |
| 1029 | if (insert_before == nullptr) { |
| 1030 | first_mir_insn = first_list_mir; |
| 1031 | last_mir_insn = last_list_mir; |
| 1032 | last_list_mir->next = nullptr; |
| 1033 | } else { |
| 1034 | if (first_mir_insn == insert_before) { |
| 1035 | last_list_mir->next = first_mir_insn; |
| 1036 | first_mir_insn = first_list_mir; |
| 1037 | } else { |
| 1038 | // Find the preceding MIR. |
| 1039 | MIR* before_list = FindPreviousMIR(insert_before); |
| 1040 | DCHECK(before_list != nullptr); |
| 1041 | before_list->next = first_list_mir; |
| 1042 | last_list_mir->next = insert_before; |
| 1043 | } |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1044 | } |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1045 | |
| 1046 | // Set this BB to be the basic block of the MIRs. |
| 1047 | for (MIR* mir = first_list_mir; mir != last_list_mir->next; mir = mir->next) { |
| 1048 | mir->bb = id; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | bool BasicBlock::RemoveMIR(MIR* mir) { |
| 1053 | // Remove as a single element list. |
| 1054 | return RemoveMIRList(mir, mir); |
| 1055 | } |
| 1056 | |
| 1057 | bool BasicBlock::RemoveMIRList(MIR* first_list_mir, MIR* last_list_mir) { |
| 1058 | if (first_list_mir == nullptr) { |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
| 1062 | // Try to find the MIR. |
| 1063 | MIR* before_list = nullptr; |
| 1064 | MIR* after_list = nullptr; |
| 1065 | |
| 1066 | // If we are removing from the beginning of the MIR list. |
| 1067 | if (first_mir_insn == first_list_mir) { |
| 1068 | before_list = nullptr; |
| 1069 | } else { |
| 1070 | before_list = FindPreviousMIR(first_list_mir); |
| 1071 | if (before_list == nullptr) { |
| 1072 | // We did not find the mir. |
| 1073 | return false; |
| 1074 | } |
| 1075 | } |
| 1076 | |
Jean Christophe Beyler | a4307ac | 2014-06-02 09:04:32 -0700 | [diff] [blame] | 1077 | // Remove the BB information and also find the after_list. |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1078 | for (MIR* mir = first_list_mir; mir != last_list_mir; mir = mir->next) { |
| 1079 | mir->bb = NullBasicBlockId; |
| 1080 | } |
| 1081 | |
| 1082 | after_list = last_list_mir->next; |
| 1083 | |
Jean Christophe Beyler | a4307ac | 2014-06-02 09:04:32 -0700 | [diff] [blame] | 1084 | // If there is nothing before the list, after_list is the first_mir. |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1085 | if (before_list == nullptr) { |
| 1086 | first_mir_insn = after_list; |
Jean Christophe Beyler | a4307ac | 2014-06-02 09:04:32 -0700 | [diff] [blame] | 1087 | } else { |
| 1088 | before_list->next = after_list; |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Jean Christophe Beyler | a4307ac | 2014-06-02 09:04:32 -0700 | [diff] [blame] | 1091 | // If there is nothing after the list, before_list is last_mir. |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1092 | if (after_list == nullptr) { |
| 1093 | last_mir_insn = before_list; |
| 1094 | } |
| 1095 | |
| 1096 | return true; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 1099 | MIR* BasicBlock::GetNextUnconditionalMir(MIRGraph* mir_graph, MIR* current) { |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 1100 | MIR* next_mir = nullptr; |
| 1101 | |
| 1102 | if (current != nullptr) { |
| 1103 | next_mir = current->next; |
| 1104 | } |
| 1105 | |
| 1106 | if (next_mir == nullptr) { |
| 1107 | // Only look for next MIR that follows unconditionally. |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 1108 | if ((taken == NullBasicBlockId) && (fall_through != NullBasicBlockId)) { |
| 1109 | next_mir = mir_graph->GetBasicBlock(fall_through)->first_mir_insn; |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | return next_mir; |
| 1114 | } |
| 1115 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1116 | char* MIRGraph::GetDalvikDisassembly(const MIR* mir) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1117 | MIR::DecodedInstruction insn = mir->dalvikInsn; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1118 | std::string str; |
| 1119 | int flags = 0; |
| 1120 | int opcode = insn.opcode; |
| 1121 | char* ret; |
| 1122 | bool nop = false; |
| 1123 | SSARepresentation* ssa_rep = mir->ssa_rep; |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1124 | Instruction::Format dalvik_format = Instruction::k10x; // Default to no-operand format. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1125 | int defs = (ssa_rep != NULL) ? ssa_rep->num_defs : 0; |
| 1126 | int uses = (ssa_rep != NULL) ? ssa_rep->num_uses : 0; |
| 1127 | |
| 1128 | // Handle special cases. |
| 1129 | if ((opcode == kMirOpCheck) || (opcode == kMirOpCheckPart2)) { |
| 1130 | str.append(extended_mir_op_names_[opcode - kMirOpFirst]); |
| 1131 | str.append(": "); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1132 | // Recover the original Dex instruction. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1133 | insn = mir->meta.throw_insn->dalvikInsn; |
| 1134 | ssa_rep = mir->meta.throw_insn->ssa_rep; |
| 1135 | defs = ssa_rep->num_defs; |
| 1136 | uses = ssa_rep->num_uses; |
| 1137 | opcode = insn.opcode; |
| 1138 | } else if (opcode == kMirOpNop) { |
| 1139 | str.append("["); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1140 | // Recover original opcode. |
| 1141 | insn.opcode = Instruction::At(current_code_item_->insns_ + mir->offset)->Opcode(); |
| 1142 | opcode = insn.opcode; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1143 | nop = true; |
| 1144 | } |
| 1145 | |
buzbee | 35ba7f3 | 2014-05-31 08:59:01 -0700 | [diff] [blame] | 1146 | if (IsPseudoMirOp(opcode)) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1147 | str.append(extended_mir_op_names_[opcode - kMirOpFirst]); |
| 1148 | } else { |
| 1149 | dalvik_format = Instruction::FormatOf(insn.opcode); |
| 1150 | flags = Instruction::FlagsOf(insn.opcode); |
| 1151 | str.append(Instruction::Name(insn.opcode)); |
| 1152 | } |
| 1153 | |
| 1154 | if (opcode == kMirOpPhi) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1155 | BasicBlockId* incoming = mir->meta.phi_incoming; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1156 | str.append(StringPrintf(" %s = (%s", |
| 1157 | GetSSANameWithConst(ssa_rep->defs[0], true).c_str(), |
| 1158 | GetSSANameWithConst(ssa_rep->uses[0], true).c_str())); |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 1159 | str.append(StringPrintf(":%d", incoming[0])); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1160 | int i; |
| 1161 | for (i = 1; i < uses; i++) { |
| 1162 | str.append(StringPrintf(", %s:%d", |
| 1163 | GetSSANameWithConst(ssa_rep->uses[i], true).c_str(), |
| 1164 | incoming[i])); |
| 1165 | } |
| 1166 | str.append(")"); |
| 1167 | } else if ((flags & Instruction::kBranch) != 0) { |
| 1168 | // For branches, decode the instructions to print out the branch targets. |
| 1169 | int offset = 0; |
| 1170 | switch (dalvik_format) { |
| 1171 | case Instruction::k21t: |
| 1172 | str.append(StringPrintf(" %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str())); |
| 1173 | offset = insn.vB; |
| 1174 | break; |
| 1175 | case Instruction::k22t: |
| 1176 | str.append(StringPrintf(" %s, %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str(), |
| 1177 | GetSSANameWithConst(ssa_rep->uses[1], false).c_str())); |
| 1178 | offset = insn.vC; |
| 1179 | break; |
| 1180 | case Instruction::k10t: |
| 1181 | case Instruction::k20t: |
| 1182 | case Instruction::k30t: |
| 1183 | offset = insn.vA; |
| 1184 | break; |
| 1185 | default: |
| 1186 | LOG(FATAL) << "Unexpected branch format " << dalvik_format << " from " << insn.opcode; |
| 1187 | } |
| 1188 | str.append(StringPrintf(" 0x%x (%c%x)", mir->offset + offset, |
| 1189 | offset > 0 ? '+' : '-', offset > 0 ? offset : -offset)); |
| 1190 | } else { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1191 | // For invokes-style formats, treat wide regs as a pair of singles. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1192 | bool show_singles = ((dalvik_format == Instruction::k35c) || |
| 1193 | (dalvik_format == Instruction::k3rc)); |
| 1194 | if (defs != 0) { |
| 1195 | str.append(StringPrintf(" %s", GetSSANameWithConst(ssa_rep->defs[0], false).c_str())); |
| 1196 | if (uses != 0) { |
| 1197 | str.append(", "); |
| 1198 | } |
| 1199 | } |
| 1200 | for (int i = 0; i < uses; i++) { |
| 1201 | str.append( |
| 1202 | StringPrintf(" %s", GetSSANameWithConst(ssa_rep->uses[i], show_singles).c_str())); |
| 1203 | if (!show_singles && (reg_location_ != NULL) && reg_location_[i].wide) { |
| 1204 | // For the listing, skip the high sreg. |
| 1205 | i++; |
| 1206 | } |
| 1207 | if (i != (uses -1)) { |
| 1208 | str.append(","); |
| 1209 | } |
| 1210 | } |
| 1211 | switch (dalvik_format) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1212 | case Instruction::k11n: // Add one immediate from vB. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1213 | case Instruction::k21s: |
| 1214 | case Instruction::k31i: |
| 1215 | case Instruction::k21h: |
| 1216 | str.append(StringPrintf(", #%d", insn.vB)); |
| 1217 | break; |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1218 | case Instruction::k51l: // Add one wide immediate. |
Ian Rogers | 23b03b5 | 2014-01-24 11:10:03 -0800 | [diff] [blame] | 1219 | str.append(StringPrintf(", #%" PRId64, insn.vB_wide)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1220 | break; |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1221 | case Instruction::k21c: // One register, one string/type/method index. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1222 | case Instruction::k31c: |
| 1223 | str.append(StringPrintf(", index #%d", insn.vB)); |
| 1224 | break; |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1225 | case Instruction::k22c: // Two registers, one string/type/method index. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1226 | str.append(StringPrintf(", index #%d", insn.vC)); |
| 1227 | break; |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1228 | case Instruction::k22s: // Add one immediate from vC. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1229 | case Instruction::k22b: |
| 1230 | str.append(StringPrintf(", #%d", insn.vC)); |
| 1231 | break; |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 1232 | default: { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1233 | // Nothing left to print. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1234 | } |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 1235 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1236 | } |
| 1237 | if (nop) { |
| 1238 | str.append("]--optimized away"); |
| 1239 | } |
| 1240 | int length = str.length() + 1; |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 1241 | ret = static_cast<char*>(arena_->Alloc(length, kArenaAllocDFInfo)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1242 | strncpy(ret, str.c_str(), length); |
| 1243 | return ret; |
| 1244 | } |
| 1245 | |
| 1246 | /* Turn method name into a legal Linux file name */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1247 | void MIRGraph::ReplaceSpecialChars(std::string& str) { |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 1248 | static const struct { const char before; const char after; } match[] = { |
| 1249 | {'/', '-'}, {';', '#'}, {' ', '#'}, {'$', '+'}, |
| 1250 | {'(', '@'}, {')', '@'}, {'<', '='}, {'>', '='} |
| 1251 | }; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1252 | for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) { |
| 1253 | std::replace(str.begin(), str.end(), match[i].before, match[i].after); |
| 1254 | } |
| 1255 | } |
| 1256 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1257 | std::string MIRGraph::GetSSAName(int ssa_reg) { |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 1258 | // TODO: This value is needed for LLVM and debugging. Currently, we compute this and then copy to |
| 1259 | // the arena. We should be smarter and just place straight into the arena, or compute the |
| 1260 | // value more lazily. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1261 | return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg)); |
| 1262 | } |
| 1263 | |
| 1264 | // 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] | 1265 | std::string MIRGraph::GetSSANameWithConst(int ssa_reg, bool singles_only) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1266 | if (reg_location_ == NULL) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1267 | // Pre-SSA - just use the standard name. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1268 | return GetSSAName(ssa_reg); |
| 1269 | } |
| 1270 | if (IsConst(reg_location_[ssa_reg])) { |
| 1271 | if (!singles_only && reg_location_[ssa_reg].wide) { |
Ian Rogers | 23b03b5 | 2014-01-24 11:10:03 -0800 | [diff] [blame] | 1272 | return StringPrintf("v%d_%d#0x%" PRIx64, SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg), |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1273 | ConstantValueWide(reg_location_[ssa_reg])); |
| 1274 | } else { |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 1275 | return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg), |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1276 | ConstantValue(reg_location_[ssa_reg])); |
| 1277 | } |
| 1278 | } else { |
| 1279 | return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg)); |
| 1280 | } |
| 1281 | } |
| 1282 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1283 | void MIRGraph::GetBlockName(BasicBlock* bb, char* name) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1284 | switch (bb->block_type) { |
| 1285 | case kEntryBlock: |
| 1286 | snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id); |
| 1287 | break; |
| 1288 | case kExitBlock: |
| 1289 | snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id); |
| 1290 | break; |
| 1291 | case kDalvikByteCode: |
| 1292 | snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id); |
| 1293 | break; |
| 1294 | case kExceptionHandling: |
| 1295 | snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset, |
| 1296 | bb->id); |
| 1297 | break; |
| 1298 | default: |
| 1299 | snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id); |
| 1300 | break; |
| 1301 | } |
| 1302 | } |
| 1303 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1304 | const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) { |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1305 | // TODO: for inlining support, use current code unit. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1306 | const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx); |
| 1307 | return cu_->dex_file->GetShorty(method_id.proto_idx_); |
| 1308 | } |
| 1309 | |
| 1310 | /* Debug Utility - dump a compilation unit */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1311 | void MIRGraph::DumpMIRGraph() { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1312 | BasicBlock* bb; |
| 1313 | const char* block_type_names[] = { |
buzbee | 17189ac | 2013-11-08 11:07:02 -0800 | [diff] [blame] | 1314 | "Null Block", |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1315 | "Entry Block", |
| 1316 | "Code Block", |
| 1317 | "Exit Block", |
| 1318 | "Exception Handling", |
| 1319 | "Catch Block" |
| 1320 | }; |
| 1321 | |
| 1322 | LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file); |
| 1323 | LOG(INFO) << cu_->insns << " insns"; |
| 1324 | LOG(INFO) << GetNumBlocks() << " blocks in total"; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1325 | GrowableArray<BasicBlock*>::Iterator iterator(&block_list_); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1326 | |
| 1327 | while (true) { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1328 | bb = iterator.Next(); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1329 | if (bb == NULL) break; |
| 1330 | LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)", |
| 1331 | bb->id, |
| 1332 | block_type_names[bb->block_type], |
| 1333 | bb->start_offset, |
| 1334 | bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset, |
| 1335 | bb->last_mir_insn ? "" : " empty"); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1336 | if (bb->taken != NullBasicBlockId) { |
| 1337 | LOG(INFO) << " Taken branch: block " << bb->taken |
| 1338 | << "(0x" << std::hex << GetBasicBlock(bb->taken)->start_offset << ")"; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1339 | } |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1340 | if (bb->fall_through != NullBasicBlockId) { |
| 1341 | LOG(INFO) << " Fallthrough : block " << bb->fall_through |
| 1342 | << " (0x" << std::hex << GetBasicBlock(bb->fall_through)->start_offset << ")"; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * Build an array of location records for the incoming arguments. |
| 1349 | * Note: one location record per word of arguments, with dummy |
| 1350 | * high-word loc for wide arguments. Also pull up any following |
| 1351 | * MOVE_RESULT and incorporate it into the invoke. |
| 1352 | */ |
| 1353 | CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1354 | bool is_range) { |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 1355 | CallInfo* info = static_cast<CallInfo*>(arena_->Alloc(sizeof(CallInfo), |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 1356 | kArenaAllocMisc)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1357 | MIR* move_result_mir = FindMoveResult(bb, mir); |
| 1358 | if (move_result_mir == NULL) { |
| 1359 | info->result.location = kLocInvalid; |
| 1360 | } else { |
| 1361 | info->result = GetRawDest(move_result_mir); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1362 | move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop); |
| 1363 | } |
| 1364 | info->num_arg_words = mir->ssa_rep->num_uses; |
| 1365 | info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*> |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 1366 | (arena_->Alloc(sizeof(RegLocation) * info->num_arg_words, kArenaAllocMisc)); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1367 | for (int i = 0; i < info->num_arg_words; i++) { |
| 1368 | info->args[i] = GetRawSrc(mir, i); |
| 1369 | } |
| 1370 | info->opt_flags = mir->optimization_flags; |
| 1371 | info->type = type; |
| 1372 | info->is_range = is_range; |
| 1373 | info->index = mir->dalvikInsn.vB; |
| 1374 | info->offset = mir->offset; |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 1375 | info->mir = mir; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1376 | return info; |
| 1377 | } |
| 1378 | |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1379 | // Allocate a new MIR. |
| 1380 | MIR* MIRGraph::NewMIR() { |
| 1381 | MIR* mir = new (arena_) MIR(); |
| 1382 | return mir; |
| 1383 | } |
| 1384 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1385 | // Allocate a new basic block. |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 1386 | BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1387 | BasicBlock* bb = new (arena_) BasicBlock(); |
| 1388 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1389 | bb->block_type = block_type; |
| 1390 | bb->id = block_id; |
| 1391 | // TUNING: better estimate of the exit block predecessors? |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1392 | bb->predecessors = new (arena_) GrowableArray<BasicBlockId>(arena_, |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1393 | (block_type == kExitBlock) ? 2048 : 2, |
| 1394 | kGrowableArrayPredecessors); |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 1395 | bb->successor_block_list_type = kNotUsed; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1396 | block_id_map_.Put(block_id, block_id); |
| 1397 | return bb; |
| 1398 | } |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 1399 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1400 | void MIRGraph::InitializeConstantPropagation() { |
| 1401 | is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 1402 | constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(), kArenaAllocDFInfo)); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | void MIRGraph::InitializeMethodUses() { |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1406 | // The gate starts by initializing the use counts. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1407 | int num_ssa_regs = GetNumSSARegs(); |
| 1408 | use_counts_.Resize(num_ssa_regs + 32); |
| 1409 | raw_use_counts_.Resize(num_ssa_regs + 32); |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1410 | // Initialize list. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1411 | for (int i = 0; i < num_ssa_regs; i++) { |
| 1412 | use_counts_.Insert(0); |
| 1413 | raw_use_counts_.Insert(0); |
| 1414 | } |
| 1415 | } |
| 1416 | |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 1417 | void MIRGraph::SSATransformationStart() { |
| 1418 | DCHECK(temp_scoped_alloc_.get() == nullptr); |
| 1419 | temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack)); |
| 1420 | temp_bit_vector_size_ = cu_->num_dalvik_registers; |
| 1421 | temp_bit_vector_ = new (temp_scoped_alloc_.get()) ArenaBitVector( |
| 1422 | temp_scoped_alloc_.get(), temp_bit_vector_size_, false, kBitMapRegisterV); |
| 1423 | |
Jean Christophe Beyler | 4896d7b | 2014-05-01 15:36:22 -0700 | [diff] [blame] | 1424 | // Update the maximum number of reachable blocks. |
| 1425 | max_num_reachable_blocks_ = num_reachable_blocks_; |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1426 | } |
| 1427 | |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 1428 | void MIRGraph::SSATransformationEnd() { |
| 1429 | // Verify the dataflow information after the pass. |
| 1430 | if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) { |
| 1431 | VerifyDataflow(); |
| 1432 | } |
| 1433 | |
| 1434 | temp_bit_vector_size_ = 0u; |
| 1435 | temp_bit_vector_ = nullptr; |
| 1436 | DCHECK(temp_scoped_alloc_.get() != nullptr); |
| 1437 | temp_scoped_alloc_.reset(); |
| 1438 | } |
| 1439 | |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1440 | void MIRGraph::ComputeTopologicalSortOrder() { |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1441 | // Clear the nodes. |
| 1442 | ClearAllVisitedFlags(); |
| 1443 | |
| 1444 | // Create the topological order if need be. |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1445 | if (topological_order_ == nullptr) { |
| 1446 | topological_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks()); |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1447 | } |
| 1448 | topological_order_->Reset(); |
| 1449 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1450 | ScopedArenaAllocator allocator(&cu_->arena_stack); |
| 1451 | ScopedArenaQueue<BasicBlock*> q(allocator.Adapter()); |
| 1452 | ScopedArenaVector<size_t> visited_cnt_values(GetNumBlocks(), 0u, allocator.Adapter()); |
| 1453 | |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1454 | // Set up visitedCntValues map for all BB. The default value for this counters in the map is zero. |
| 1455 | // also fill initial queue. |
| 1456 | GrowableArray<BasicBlock*>::Iterator iterator(&block_list_); |
| 1457 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1458 | size_t num_blocks = 0u; |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1459 | while (true) { |
| 1460 | BasicBlock* bb = iterator.Next(); |
| 1461 | |
| 1462 | if (bb == nullptr) { |
| 1463 | break; |
| 1464 | } |
| 1465 | |
| 1466 | if (bb->hidden == true) { |
| 1467 | continue; |
| 1468 | } |
| 1469 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1470 | num_blocks += 1u; |
| 1471 | size_t unvisited_predecessor_count = bb->predecessors->Size(); |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1472 | |
| 1473 | GrowableArray<BasicBlockId>::Iterator pred_iterator(bb->predecessors); |
| 1474 | // To process loops we should not wait for dominators. |
| 1475 | while (true) { |
| 1476 | BasicBlock* pred_bb = GetBasicBlock(pred_iterator.Next()); |
| 1477 | |
| 1478 | if (pred_bb == nullptr) { |
| 1479 | break; |
| 1480 | } |
| 1481 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1482 | // Skip the backward branch or hidden predecessor. |
| 1483 | if (pred_bb->hidden || |
| 1484 | (pred_bb->dominators != nullptr && pred_bb->dominators->IsBitSet(bb->id))) { |
| 1485 | unvisited_predecessor_count -= 1u; |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1489 | visited_cnt_values[bb->id] = unvisited_predecessor_count; |
| 1490 | |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1491 | // Add entry block to queue. |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1492 | if (unvisited_predecessor_count == 0) { |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1493 | q.push(bb); |
| 1494 | } |
| 1495 | } |
| 1496 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1497 | // We can theoretically get a cycle where none of the blocks dominates the other. Therefore |
| 1498 | // don't stop when the queue is empty, continue until we've processed all the blocks. |
| 1499 | // (In practice, we've seen this for a monitor-exit catch handler that erroneously tries to |
| 1500 | // handle its own exceptions being broken into two blocks by a jump to to the monitor-exit |
| 1501 | // from another catch hanler. http://b/15745363 .) |
| 1502 | AllNodesIterator candidate_iter(this); // For the empty queue case. |
| 1503 | while (num_blocks != 0u) { |
| 1504 | num_blocks -= 1u; |
| 1505 | BasicBlock* bb = nullptr; |
| 1506 | if (!q.empty()) { |
| 1507 | // Get top. |
| 1508 | bb = q.front(); |
| 1509 | q.pop(); |
| 1510 | } else { |
| 1511 | // Find some block we didn't visit yet that has at least one visited predecessor. |
| 1512 | while (bb == nullptr) { |
| 1513 | BasicBlock* candidate = candidate_iter.Next(); |
| 1514 | DCHECK(candidate != nullptr); |
| 1515 | if (candidate->visited || candidate->hidden) { |
| 1516 | continue; |
| 1517 | } |
| 1518 | GrowableArray<BasicBlockId>::Iterator iter(candidate->predecessors); |
| 1519 | for (BasicBlock* pred_bb = GetBasicBlock(iter.Next()); pred_bb != nullptr; |
| 1520 | pred_bb = GetBasicBlock(iter.Next())) { |
| 1521 | if (!pred_bb->hidden && pred_bb->visited) { |
| 1522 | bb = candidate; |
| 1523 | break; |
| 1524 | } |
| 1525 | } |
| 1526 | } |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1529 | DCHECK_EQ(bb->hidden, false); |
| 1530 | DCHECK_EQ(bb->visited, false); |
| 1531 | |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1532 | // We've visited all the predecessors. So, we can visit bb. |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1533 | bb->visited = true; |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1534 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1535 | // Now add the basic block. |
| 1536 | topological_order_->Insert(bb->id); |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1537 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1538 | // Reduce visitedCnt for all the successors and add into the queue ones with visitedCnt equals to zero. |
| 1539 | ChildBlockIterator succIter(bb, this); |
| 1540 | BasicBlock* successor = succIter.Next(); |
| 1541 | for ( ; successor != nullptr; successor = succIter.Next()) { |
| 1542 | if (successor->visited || successor->hidden) { |
| 1543 | continue; |
| 1544 | } |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1545 | |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame^] | 1546 | // one more predecessor was visited. |
| 1547 | DCHECK_NE(visited_cnt_values[successor->id], 0u); |
| 1548 | visited_cnt_values[successor->id] -= 1u; |
| 1549 | if (visited_cnt_values[successor->id] == 0u) { |
| 1550 | q.push(successor); |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | bool BasicBlock::IsExceptionBlock() const { |
| 1557 | if (block_type == kExceptionHandling) { |
| 1558 | return true; |
| 1559 | } |
| 1560 | return false; |
| 1561 | } |
| 1562 | |
Wei Jin | 04f4d8a | 2014-05-29 18:04:29 -0700 | [diff] [blame] | 1563 | bool MIRGraph::HasSuspendTestBetween(BasicBlock* source, BasicBlockId target_id) { |
| 1564 | BasicBlock* target = GetBasicBlock(target_id); |
| 1565 | |
| 1566 | if (source == nullptr || target == nullptr) |
| 1567 | return false; |
| 1568 | |
| 1569 | int idx; |
| 1570 | for (idx = gen_suspend_test_list_.Size() - 1; idx >= 0; idx--) { |
| 1571 | BasicBlock* bb = gen_suspend_test_list_.Get(idx); |
| 1572 | if (bb == source) |
| 1573 | return true; // The block has been inserted by a suspend check before. |
| 1574 | if (source->dominators->IsBitSet(bb->id) && bb->dominators->IsBitSet(target_id)) |
| 1575 | return true; |
| 1576 | } |
| 1577 | |
| 1578 | return false; |
| 1579 | } |
| 1580 | |
Jean Christophe Beyler | f8c762b | 2014-05-02 12:54:37 -0700 | [diff] [blame] | 1581 | ChildBlockIterator::ChildBlockIterator(BasicBlock* bb, MIRGraph* mir_graph) |
| 1582 | : basic_block_(bb), mir_graph_(mir_graph), visited_fallthrough_(false), |
| 1583 | visited_taken_(false), have_successors_(false) { |
| 1584 | // Check if we actually do have successors. |
| 1585 | if (basic_block_ != 0 && basic_block_->successor_block_list_type != kNotUsed) { |
| 1586 | have_successors_ = true; |
| 1587 | successor_iter_.Reset(basic_block_->successor_blocks); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | BasicBlock* ChildBlockIterator::Next() { |
| 1592 | // We check if we have a basic block. If we don't we cannot get next child. |
| 1593 | if (basic_block_ == nullptr) { |
| 1594 | return nullptr; |
| 1595 | } |
| 1596 | |
| 1597 | // If we haven't visited fallthrough, return that. |
| 1598 | if (visited_fallthrough_ == false) { |
| 1599 | visited_fallthrough_ = true; |
| 1600 | |
| 1601 | BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->fall_through); |
| 1602 | if (result != nullptr) { |
| 1603 | return result; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | // If we haven't visited taken, return that. |
| 1608 | if (visited_taken_ == false) { |
| 1609 | visited_taken_ = true; |
| 1610 | |
| 1611 | BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->taken); |
| 1612 | if (result != nullptr) { |
| 1613 | return result; |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | // We visited both taken and fallthrough. Now check if we have successors we need to visit. |
| 1618 | if (have_successors_ == true) { |
| 1619 | // Get information about next successor block. |
| 1620 | SuccessorBlockInfo* successor_block_info = successor_iter_.Next(); |
| 1621 | |
| 1622 | // If we don't have anymore successors, return nullptr. |
| 1623 | if (successor_block_info != nullptr) { |
| 1624 | return mir_graph_->GetBasicBlock(successor_block_info->block); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // We do not have anything. |
| 1629 | return nullptr; |
| 1630 | } |
| 1631 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1632 | BasicBlock* BasicBlock::Copy(CompilationUnit* c_unit) { |
| 1633 | MIRGraph* mir_graph = c_unit->mir_graph.get(); |
| 1634 | return Copy(mir_graph); |
| 1635 | } |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1636 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1637 | BasicBlock* BasicBlock::Copy(MIRGraph* mir_graph) { |
| 1638 | BasicBlock* result_bb = mir_graph->CreateNewBB(block_type); |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1639 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1640 | // We don't do a memcpy style copy here because it would lead to a lot of things |
| 1641 | // to clean up. Let us do it by hand instead. |
| 1642 | // Copy in taken and fallthrough. |
| 1643 | result_bb->fall_through = fall_through; |
| 1644 | result_bb->taken = taken; |
| 1645 | |
| 1646 | // Copy successor links if needed. |
| 1647 | ArenaAllocator* arena = mir_graph->GetArena(); |
| 1648 | |
| 1649 | result_bb->successor_block_list_type = successor_block_list_type; |
| 1650 | if (result_bb->successor_block_list_type != kNotUsed) { |
| 1651 | size_t size = successor_blocks->Size(); |
| 1652 | result_bb->successor_blocks = new (arena) GrowableArray<SuccessorBlockInfo*>(arena, size, kGrowableArraySuccessorBlocks); |
| 1653 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(successor_blocks); |
| 1654 | while (true) { |
| 1655 | SuccessorBlockInfo* sbi_old = iterator.Next(); |
| 1656 | if (sbi_old == nullptr) { |
| 1657 | break; |
| 1658 | } |
| 1659 | SuccessorBlockInfo* sbi_new = static_cast<SuccessorBlockInfo*>(arena->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor)); |
| 1660 | memcpy(sbi_new, sbi_old, sizeof(SuccessorBlockInfo)); |
| 1661 | result_bb->successor_blocks->Insert(sbi_new); |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1665 | // Copy offset, method. |
| 1666 | result_bb->start_offset = start_offset; |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1667 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1668 | // Now copy instructions. |
| 1669 | for (MIR* mir = first_mir_insn; mir != 0; mir = mir->next) { |
| 1670 | // Get a copy first. |
| 1671 | MIR* copy = mir->Copy(mir_graph); |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1672 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1673 | // Append it. |
| 1674 | result_bb->AppendMIR(copy); |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1677 | return result_bb; |
Jean Christophe Beyler | 3aa5773 | 2014-04-17 12:47:24 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | MIR* MIR::Copy(MIRGraph* mir_graph) { |
| 1681 | MIR* res = mir_graph->NewMIR(); |
| 1682 | *res = *this; |
| 1683 | |
| 1684 | // Remove links |
| 1685 | res->next = nullptr; |
| 1686 | res->bb = NullBasicBlockId; |
| 1687 | res->ssa_rep = nullptr; |
| 1688 | |
| 1689 | return res; |
| 1690 | } |
| 1691 | |
| 1692 | MIR* MIR::Copy(CompilationUnit* c_unit) { |
| 1693 | return Copy(c_unit->mir_graph.get()); |
| 1694 | } |
| 1695 | |
| 1696 | uint32_t SSARepresentation::GetStartUseIndex(Instruction::Code opcode) { |
| 1697 | // Default result. |
| 1698 | int res = 0; |
| 1699 | |
| 1700 | // We are basically setting the iputs to their igets counterparts. |
| 1701 | switch (opcode) { |
| 1702 | case Instruction::IPUT: |
| 1703 | case Instruction::IPUT_OBJECT: |
| 1704 | case Instruction::IPUT_BOOLEAN: |
| 1705 | case Instruction::IPUT_BYTE: |
| 1706 | case Instruction::IPUT_CHAR: |
| 1707 | case Instruction::IPUT_SHORT: |
| 1708 | case Instruction::IPUT_QUICK: |
| 1709 | case Instruction::IPUT_OBJECT_QUICK: |
| 1710 | case Instruction::APUT: |
| 1711 | case Instruction::APUT_OBJECT: |
| 1712 | case Instruction::APUT_BOOLEAN: |
| 1713 | case Instruction::APUT_BYTE: |
| 1714 | case Instruction::APUT_CHAR: |
| 1715 | case Instruction::APUT_SHORT: |
| 1716 | case Instruction::SPUT: |
| 1717 | case Instruction::SPUT_OBJECT: |
| 1718 | case Instruction::SPUT_BOOLEAN: |
| 1719 | case Instruction::SPUT_BYTE: |
| 1720 | case Instruction::SPUT_CHAR: |
| 1721 | case Instruction::SPUT_SHORT: |
| 1722 | // Skip the VR containing what to store. |
| 1723 | res = 1; |
| 1724 | break; |
| 1725 | case Instruction::IPUT_WIDE: |
| 1726 | case Instruction::IPUT_WIDE_QUICK: |
| 1727 | case Instruction::APUT_WIDE: |
| 1728 | case Instruction::SPUT_WIDE: |
| 1729 | // Skip the two VRs containing what to store. |
| 1730 | res = 2; |
| 1731 | break; |
| 1732 | default: |
| 1733 | // Do nothing in the general case. |
| 1734 | break; |
| 1735 | } |
| 1736 | |
| 1737 | return res; |
| 1738 | } |
| 1739 | |
Jean Christophe Beyler | c3db20b | 2014-05-05 21:09:40 -0700 | [diff] [blame] | 1740 | /** |
| 1741 | * @brief Given a decoded instruction, it checks whether the instruction |
| 1742 | * sets a constant and if it does, more information is provided about the |
| 1743 | * constant being set. |
| 1744 | * @param ptr_value pointer to a 64-bit holder for the constant. |
| 1745 | * @param wide Updated by function whether a wide constant is being set by bytecode. |
| 1746 | * @return Returns false if the decoded instruction does not represent a constant bytecode. |
| 1747 | */ |
| 1748 | bool MIR::DecodedInstruction::GetConstant(int64_t* ptr_value, bool* wide) const { |
| 1749 | bool sets_const = true; |
| 1750 | int64_t value = vB; |
| 1751 | |
| 1752 | DCHECK(ptr_value != nullptr); |
| 1753 | DCHECK(wide != nullptr); |
| 1754 | |
| 1755 | switch (opcode) { |
| 1756 | case Instruction::CONST_4: |
| 1757 | case Instruction::CONST_16: |
| 1758 | case Instruction::CONST: |
| 1759 | *wide = false; |
| 1760 | value <<= 32; // In order to get the sign extend. |
| 1761 | value >>= 32; |
| 1762 | break; |
| 1763 | case Instruction::CONST_HIGH16: |
| 1764 | *wide = false; |
| 1765 | value <<= 48; // In order to get the sign extend. |
| 1766 | value >>= 32; |
| 1767 | break; |
| 1768 | case Instruction::CONST_WIDE_16: |
| 1769 | case Instruction::CONST_WIDE_32: |
| 1770 | *wide = true; |
| 1771 | value <<= 32; // In order to get the sign extend. |
| 1772 | value >>= 32; |
| 1773 | break; |
| 1774 | case Instruction::CONST_WIDE: |
| 1775 | *wide = true; |
| 1776 | value = vB_wide; |
| 1777 | break; |
| 1778 | case Instruction::CONST_WIDE_HIGH16: |
| 1779 | *wide = true; |
| 1780 | value <<= 48; // In order to get the sign extend. |
| 1781 | break; |
| 1782 | default: |
| 1783 | sets_const = false; |
| 1784 | break; |
| 1785 | } |
| 1786 | |
| 1787 | if (sets_const) { |
| 1788 | *ptr_value = value; |
| 1789 | } |
| 1790 | |
| 1791 | return sets_const; |
| 1792 | } |
Jean Christophe Beyler | 8512758 | 2014-05-11 23:36:41 -0700 | [diff] [blame] | 1793 | |
| 1794 | void BasicBlock::ResetOptimizationFlags(uint16_t reset_flags) { |
| 1795 | // Reset flags for all MIRs in bb. |
| 1796 | for (MIR* mir = first_mir_insn; mir != NULL; mir = mir->next) { |
| 1797 | mir->optimization_flags &= (~reset_flags); |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | void BasicBlock::Hide(CompilationUnit* c_unit) { |
| 1802 | // First lets make it a dalvik bytecode block so it doesn't have any special meaning. |
| 1803 | block_type = kDalvikByteCode; |
| 1804 | |
| 1805 | // Mark it as hidden. |
| 1806 | hidden = true; |
| 1807 | |
| 1808 | // Detach it from its MIRs so we don't generate code for them. Also detached MIRs |
| 1809 | // are updated to know that they no longer have a parent. |
| 1810 | for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) { |
| 1811 | mir->bb = NullBasicBlockId; |
| 1812 | } |
| 1813 | first_mir_insn = nullptr; |
| 1814 | last_mir_insn = nullptr; |
| 1815 | |
| 1816 | GrowableArray<BasicBlockId>::Iterator iterator(predecessors); |
| 1817 | |
| 1818 | MIRGraph* mir_graph = c_unit->mir_graph.get(); |
| 1819 | while (true) { |
| 1820 | BasicBlock* pred_bb = mir_graph->GetBasicBlock(iterator.Next()); |
| 1821 | if (pred_bb == nullptr) { |
| 1822 | break; |
| 1823 | } |
| 1824 | |
| 1825 | // Sadly we have to go through the children by hand here. |
| 1826 | pred_bb->ReplaceChild(id, NullBasicBlockId); |
| 1827 | } |
| 1828 | |
| 1829 | // Iterate through children of bb we are hiding. |
| 1830 | ChildBlockIterator successorChildIter(this, mir_graph); |
| 1831 | |
| 1832 | for (BasicBlock* childPtr = successorChildIter.Next(); childPtr != 0; childPtr = successorChildIter.Next()) { |
| 1833 | // Replace child with null child. |
| 1834 | childPtr->predecessors->Delete(id); |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | bool BasicBlock::IsSSALiveOut(const CompilationUnit* c_unit, int ssa_reg) { |
| 1839 | // In order to determine if the ssa reg is live out, we scan all the MIRs. We remember |
| 1840 | // the last SSA number of the same dalvik register. At the end, if it is different than ssa_reg, |
| 1841 | // then it is not live out of this BB. |
| 1842 | int dalvik_reg = c_unit->mir_graph->SRegToVReg(ssa_reg); |
| 1843 | |
| 1844 | int last_ssa_reg = -1; |
| 1845 | |
| 1846 | // Walk through the MIRs backwards. |
| 1847 | for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) { |
| 1848 | // Get ssa rep. |
| 1849 | SSARepresentation *ssa_rep = mir->ssa_rep; |
| 1850 | |
| 1851 | // Go through the defines for this MIR. |
| 1852 | for (int i = 0; i < ssa_rep->num_defs; i++) { |
| 1853 | DCHECK(ssa_rep->defs != nullptr); |
| 1854 | |
| 1855 | // Get the ssa reg. |
| 1856 | int def_ssa_reg = ssa_rep->defs[i]; |
| 1857 | |
| 1858 | // Get dalvik reg. |
| 1859 | int def_dalvik_reg = c_unit->mir_graph->SRegToVReg(def_ssa_reg); |
| 1860 | |
| 1861 | // Compare dalvik regs. |
| 1862 | if (dalvik_reg == def_dalvik_reg) { |
| 1863 | // We found a def of the register that we are being asked about. |
| 1864 | // Remember it. |
| 1865 | last_ssa_reg = def_ssa_reg; |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | if (last_ssa_reg == -1) { |
| 1871 | // If we get to this point we couldn't find a define of register user asked about. |
| 1872 | // Let's assume the user knows what he's doing so we can be safe and say that if we |
| 1873 | // couldn't find a def, it is live out. |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
| 1877 | // If it is not -1, we found a match, is it ssa_reg? |
| 1878 | return (ssa_reg == last_ssa_reg); |
| 1879 | } |
| 1880 | |
| 1881 | bool BasicBlock::ReplaceChild(BasicBlockId old_bb, BasicBlockId new_bb) { |
| 1882 | // We need to check taken, fall_through, and successor_blocks to replace. |
| 1883 | bool found = false; |
| 1884 | if (taken == old_bb) { |
| 1885 | taken = new_bb; |
| 1886 | found = true; |
| 1887 | } |
| 1888 | |
| 1889 | if (fall_through == old_bb) { |
| 1890 | fall_through = new_bb; |
| 1891 | found = true; |
| 1892 | } |
| 1893 | |
| 1894 | if (successor_block_list_type != kNotUsed) { |
| 1895 | GrowableArray<SuccessorBlockInfo*>::Iterator iterator(successor_blocks); |
| 1896 | while (true) { |
| 1897 | SuccessorBlockInfo* successor_block_info = iterator.Next(); |
| 1898 | if (successor_block_info == nullptr) { |
| 1899 | break; |
| 1900 | } |
| 1901 | if (successor_block_info->block == old_bb) { |
| 1902 | successor_block_info->block = new_bb; |
| 1903 | found = true; |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | return found; |
| 1909 | } |
| 1910 | |
| 1911 | void BasicBlock::UpdatePredecessor(BasicBlockId old_parent, BasicBlockId new_parent) { |
| 1912 | GrowableArray<BasicBlockId>::Iterator iterator(predecessors); |
| 1913 | bool found = false; |
| 1914 | |
| 1915 | while (true) { |
| 1916 | BasicBlockId pred_bb_id = iterator.Next(); |
| 1917 | |
| 1918 | if (pred_bb_id == NullBasicBlockId) { |
| 1919 | break; |
| 1920 | } |
| 1921 | |
| 1922 | if (pred_bb_id == old_parent) { |
| 1923 | size_t idx = iterator.GetIndex() - 1; |
| 1924 | predecessors->Put(idx, new_parent); |
| 1925 | found = true; |
| 1926 | break; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | // If not found, add it. |
| 1931 | if (found == false) { |
| 1932 | predecessors->Insert(new_parent); |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | // Create a new basic block with block_id as num_blocks_ that is |
| 1937 | // post-incremented. |
| 1938 | BasicBlock* MIRGraph::CreateNewBB(BBType block_type) { |
| 1939 | BasicBlock* res = NewMemBB(block_type, num_blocks_++); |
| 1940 | block_list_.Insert(res); |
| 1941 | return res; |
| 1942 | } |
| 1943 | |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 1944 | void MIRGraph::CalculateBasicBlockInformation() { |
| 1945 | PassDriverMEPostOpt driver(cu_); |
| 1946 | driver.Launch(); |
| 1947 | } |
| 1948 | |
| 1949 | void MIRGraph::InitializeBasicBlockData() { |
| 1950 | num_blocks_ = block_list_.Size(); |
| 1951 | } |
| 1952 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1953 | } // namespace art |