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