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