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