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