blob: 7e83c0c6bc6a2fa31d3e307ca42dd038f434b398 [file] [log] [blame]
buzbee311ca162013-02-28 15:56:43 -08001/*
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 Geoffrayf3e2cc42014-02-18 18:37:26 +000017#include "mir_graph.h"
18
19#include <inttypes.h>
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -070020#include <queue>
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000021
Ian Rogerse77493c2014-08-20 15:08:45 -070022#include "base/bit_vector-inl.h"
Ian Rogers6282dc12013-04-18 15:54:02 -070023#include "base/stl_util.h"
buzbee311ca162013-02-28 15:56:43 -080024#include "compiler_internals.h"
buzbee311ca162013-02-28 15:56:43 -080025#include "dex_file-inl.h"
Ian Rogers29a26482014-05-02 15:27:29 -070026#include "dex_instruction-inl.h"
Vladimir Marko95a05972014-05-30 10:01:32 +010027#include "dex/global_value_numbering.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000028#include "dex/quick/dex_file_to_method_inliner_map.h"
29#include "dex/quick/dex_file_method_inliner.h"
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000030#include "leb128.h"
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070031#include "pass_driver_me_post_opt.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070032#include "stack.h"
Vladimir Marko622bdbe2014-06-19 14:59:05 +010033#include "utils/scoped_arena_containers.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000034
buzbee311ca162013-02-28 15:56:43 -080035namespace art {
36
37#define MAX_PATTERN_LEN 5
38
buzbee1fd33462013-03-25 13:40:45 -070039const char* MIRGraph::extended_mir_op_names_[kMirOpLast - kMirOpFirst] = {
40 "Phi",
41 "Copy",
42 "FusedCmplFloat",
43 "FusedCmpgFloat",
44 "FusedCmplDouble",
45 "FusedCmpgDouble",
46 "FusedCmpLong",
47 "Nop",
48 "OpNullCheck",
49 "OpRangeCheck",
50 "OpDivZeroCheck",
51 "Check1",
52 "Check2",
53 "Select",
Mark Mendelld65c51a2014-04-29 16:55:20 -040054 "ConstVector",
55 "MoveVector",
56 "PackedMultiply",
57 "PackedAddition",
58 "PackedSubtract",
59 "PackedShiftLeft",
60 "PackedSignedShiftRight",
61 "PackedUnsignedShiftRight",
62 "PackedAnd",
63 "PackedOr",
64 "PackedXor",
65 "PackedAddReduce",
66 "PackedReduce",
67 "PackedSet",
Udayan Banerji60bfe7b2014-07-08 19:59:43 -070068 "ReserveVectorRegisters",
69 "ReturnVectorRegisters",
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -070070 "MemBarrier",
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -070071 "PackedArrayGet",
72 "PackedArrayPut",
buzbee1fd33462013-03-25 13:40:45 -070073};
74
buzbee862a7602013-04-05 10:58:54 -070075MIRGraph::MIRGraph(CompilationUnit* cu, ArenaAllocator* arena)
buzbee1fd33462013-03-25 13:40:45 -070076 : reg_location_(NULL),
Vladimir Marko8081d2b2014-07-31 15:33:43 +010077 block_id_map_(std::less<unsigned int>(), arena->Adapter()),
buzbee1fd33462013-03-25 13:40:45 -070078 cu_(cu),
Vladimir Markoe39c54e2014-09-22 14:50:02 +010079 ssa_base_vregs_(arena->Adapter(kArenaAllocSSAToDalvikMap)),
80 ssa_subscripts_(arena->Adapter(kArenaAllocSSAToDalvikMap)),
buzbee311ca162013-02-28 15:56:43 -080081 vreg_to_ssa_map_(NULL),
82 ssa_last_defs_(NULL),
83 is_constant_v_(NULL),
84 constant_values_(NULL),
Vladimir Markoe39c54e2014-09-22 14:50:02 +010085 use_counts_(arena->Adapter()),
86 raw_use_counts_(arena->Adapter()),
buzbee311ca162013-02-28 15:56:43 -080087 num_reachable_blocks_(0),
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -070088 max_num_reachable_blocks_(0),
Vladimir Markoe39c54e2014-09-22 14:50:02 +010089 dfs_order_(arena->Adapter(kArenaAllocDfsPreOrder)),
90 dfs_post_order_(arena->Adapter(kArenaAllocDfsPostOrder)),
91 dom_post_order_traversal_(arena->Adapter(kArenaAllocDomPostOrder)),
92 topological_order_(arena->Adapter(kArenaAllocTopologicalSortOrder)),
93 topological_order_loop_ends_(arena->Adapter(kArenaAllocTopologicalSortOrder)),
94 topological_order_indexes_(arena->Adapter(kArenaAllocTopologicalSortOrder)),
95 topological_order_loop_head_stack_(arena->Adapter(kArenaAllocTopologicalSortOrder)),
buzbee311ca162013-02-28 15:56:43 -080096 i_dom_list_(NULL),
Vladimir Markobfea9c22014-01-17 17:49:33 +000097 temp_scoped_alloc_(),
98 temp_insn_data_(nullptr),
99 temp_bit_vector_size_(0u),
100 temp_bit_vector_(nullptr),
Vladimir Marko5229cf12014-10-09 14:57:59 +0100101 temp_bit_matrix_(nullptr),
Vladimir Marko95a05972014-05-30 10:01:32 +0100102 temp_gvn_(),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100103 block_list_(arena->Adapter(kArenaAllocBBList)),
buzbee311ca162013-02-28 15:56:43 -0800104 try_block_addr_(NULL),
105 entry_block_(NULL),
106 exit_block_(NULL),
buzbee311ca162013-02-28 15:56:43 -0800107 num_blocks_(0),
108 current_code_item_(NULL),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100109 dex_pc_to_block_map_(arena->Adapter()),
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100110 m_units_(arena->Adapter()),
111 method_stack_(arena->Adapter()),
buzbee311ca162013-02-28 15:56:43 -0800112 current_method_(kInvalidEntry),
113 current_offset_(kInvalidEntry),
114 def_count_(0),
115 opcode_count_(NULL),
buzbee1fd33462013-03-25 13:40:45 -0700116 num_ssa_regs_(0),
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100117 extended_basic_blocks_(arena->Adapter()),
buzbee1fd33462013-03-25 13:40:45 -0700118 method_sreg_(0),
buzbee862a7602013-04-05 10:58:54 -0700119 attributes_(METHOD_IS_LEAF), // Start with leaf assumption, change on encountering invoke.
120 checkstats_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700121 arena_(arena),
122 backward_branches_(0),
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800123 forward_branches_(0),
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800124 num_non_special_compiler_temps_(0),
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700125 max_available_special_compiler_temps_(1), // We only need the method ptr as a special temp for now.
126 requested_backend_temp_(false),
127 compiler_temps_committed_(false),
Vladimir Markobe0e5462014-02-26 11:24:15 +0000128 punt_to_interpreter_(false),
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000129 merged_df_flags_(0u),
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100130 ifield_lowering_infos_(arena->Adapter(kArenaAllocLoweringInfo)),
131 sfield_lowering_infos_(arena->Adapter(kArenaAllocLoweringInfo)),
132 method_lowering_infos_(arena->Adapter(kArenaAllocLoweringInfo)),
133 gen_suspend_test_list_(arena->Adapter()) {
134 use_counts_.reserve(256);
135 raw_use_counts_.reserve(256);
136 block_list_.reserve(100);
buzbee862a7602013-04-05 10:58:54 -0700137 try_block_addr_ = new (arena_) ArenaBitVector(arena_, 0, true /* expandable */);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700138
139
140 if (cu_->instruction_set == kX86 || cu_->instruction_set == kX86_64) {
141 // X86 requires a temp to keep track of the method address.
142 // TODO For x86_64, addressing can be done with RIP. When that is implemented,
143 // this needs to be updated to reserve 0 temps for BE.
144 max_available_non_special_compiler_temps_ = cu_->target64 ? 2 : 1;
145 reserved_temps_for_backend_ = max_available_non_special_compiler_temps_;
146 } else {
147 // Other architectures do not have a known lower bound for non-special temps.
148 // We allow the update of the max to happen at BE initialization stage and simply set 0 for now.
149 max_available_non_special_compiler_temps_ = 0;
150 reserved_temps_for_backend_ = 0;
151 }
buzbee311ca162013-02-28 15:56:43 -0800152}
153
Ian Rogers6282dc12013-04-18 15:54:02 -0700154MIRGraph::~MIRGraph() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100155 STLDeleteElements(&block_list_);
Ian Rogers6282dc12013-04-18 15:54:02 -0700156 STLDeleteElements(&m_units_);
157}
158
buzbee311ca162013-02-28 15:56:43 -0800159/*
160 * Parse an instruction, return the length of the instruction
161 */
Ian Rogers29a26482014-05-02 15:27:29 -0700162int MIRGraph::ParseInsn(const uint16_t* code_ptr, MIR::DecodedInstruction* decoded_instruction) {
163 const Instruction* inst = Instruction::At(code_ptr);
164 decoded_instruction->opcode = inst->Opcode();
165 decoded_instruction->vA = inst->HasVRegA() ? inst->VRegA() : 0;
166 decoded_instruction->vB = inst->HasVRegB() ? inst->VRegB() : 0;
167 decoded_instruction->vB_wide = inst->HasWideVRegB() ? inst->WideVRegB() : 0;
168 decoded_instruction->vC = inst->HasVRegC() ? inst->VRegC() : 0;
169 if (inst->HasVarArgs()) {
170 inst->GetVarArgs(decoded_instruction->arg);
171 }
172 return inst->SizeInCodeUnits();
buzbee311ca162013-02-28 15:56:43 -0800173}
174
175
176/* Split an existing block from the specified code offset into two */
buzbee0d829482013-10-11 15:24:55 -0700177BasicBlock* MIRGraph::SplitBlock(DexOffset code_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700178 BasicBlock* orig_block, BasicBlock** immed_pred_block_p) {
buzbee0d829482013-10-11 15:24:55 -0700179 DCHECK_GT(code_offset, orig_block->start_offset);
buzbee311ca162013-02-28 15:56:43 -0800180 MIR* insn = orig_block->first_mir_insn;
Mathew Zaleski33c17022014-09-15 09:44:14 -0400181 MIR* prev = NULL; // Will be set to instruction before split.
buzbee311ca162013-02-28 15:56:43 -0800182 while (insn) {
183 if (insn->offset == code_offset) break;
buzbee0d829482013-10-11 15:24:55 -0700184 prev = insn;
buzbee311ca162013-02-28 15:56:43 -0800185 insn = insn->next;
186 }
187 if (insn == NULL) {
188 LOG(FATAL) << "Break split failed";
189 }
Mathew Zaleski33c17022014-09-15 09:44:14 -0400190 // Now insn is at the instruction where we want to split, namely
191 // insn will be the first instruction of the "bottom" block.
192 // Similarly, prev will be the last instruction of the "top" block
193
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100194 BasicBlock* bottom_block = CreateNewBB(kDalvikByteCode);
buzbee311ca162013-02-28 15:56:43 -0800195
196 bottom_block->start_offset = code_offset;
197 bottom_block->first_mir_insn = insn;
198 bottom_block->last_mir_insn = orig_block->last_mir_insn;
199
Junmo Park90223cc2014-08-04 18:51:21 +0900200 /* If this block was terminated by a return, conditional branch or throw,
201 * the flag needs to go with the bottom block
202 */
buzbee311ca162013-02-28 15:56:43 -0800203 bottom_block->terminated_by_return = orig_block->terminated_by_return;
204 orig_block->terminated_by_return = false;
205
Junmo Park90223cc2014-08-04 18:51:21 +0900206 bottom_block->conditional_branch = orig_block->conditional_branch;
207 orig_block->conditional_branch = false;
208
209 bottom_block->explicit_throw = orig_block->explicit_throw;
210 orig_block->explicit_throw = false;
211
buzbee311ca162013-02-28 15:56:43 -0800212 /* Handle the taken path */
213 bottom_block->taken = orig_block->taken;
buzbee0d829482013-10-11 15:24:55 -0700214 if (bottom_block->taken != NullBasicBlockId) {
215 orig_block->taken = NullBasicBlockId;
216 BasicBlock* bb_taken = GetBasicBlock(bottom_block->taken);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100217 bb_taken->ErasePredecessor(orig_block->id);
218 bb_taken->predecessors.push_back(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800219 }
220
221 /* Handle the fallthrough path */
222 bottom_block->fall_through = orig_block->fall_through;
buzbee0d829482013-10-11 15:24:55 -0700223 orig_block->fall_through = bottom_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100224 bottom_block->predecessors.push_back(orig_block->id);
buzbee0d829482013-10-11 15:24:55 -0700225 if (bottom_block->fall_through != NullBasicBlockId) {
226 BasicBlock* bb_fall_through = GetBasicBlock(bottom_block->fall_through);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100227 bb_fall_through->ErasePredecessor(orig_block->id);
228 bb_fall_through->predecessors.push_back(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800229 }
230
231 /* Handle the successor list */
buzbee0d829482013-10-11 15:24:55 -0700232 if (orig_block->successor_block_list_type != kNotUsed) {
233 bottom_block->successor_block_list_type = orig_block->successor_block_list_type;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100234 bottom_block->successor_blocks.swap(orig_block->successor_blocks);
buzbee0d829482013-10-11 15:24:55 -0700235 orig_block->successor_block_list_type = kNotUsed;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100236 DCHECK(orig_block->successor_blocks.empty()); // Empty after the swap() above.
237 for (SuccessorBlockInfo* successor_block_info : bottom_block->successor_blocks) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700238 BasicBlock* bb = GetBasicBlock(successor_block_info->block);
Niranjan Kumar989367a2014-06-12 12:15:48 -0700239 if (bb != nullptr) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100240 bb->ErasePredecessor(orig_block->id);
241 bb->predecessors.push_back(bottom_block->id);
Niranjan Kumar989367a2014-06-12 12:15:48 -0700242 }
buzbee311ca162013-02-28 15:56:43 -0800243 }
244 }
245
buzbee0d829482013-10-11 15:24:55 -0700246 orig_block->last_mir_insn = prev;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700247 prev->next = nullptr;
buzbee311ca162013-02-28 15:56:43 -0800248
buzbee311ca162013-02-28 15:56:43 -0800249 /*
250 * Update the immediate predecessor block pointer so that outgoing edges
251 * can be applied to the proper block.
252 */
253 if (immed_pred_block_p) {
254 DCHECK_EQ(*immed_pred_block_p, orig_block);
255 *immed_pred_block_p = bottom_block;
256 }
buzbeeb48819d2013-09-14 16:15:25 -0700257
258 // Associate dex instructions in the bottom block with the new container.
Vladimir Marko4376c872014-01-23 12:39:29 +0000259 DCHECK(insn != nullptr);
260 DCHECK(insn != orig_block->first_mir_insn);
261 DCHECK(insn == bottom_block->first_mir_insn);
262 DCHECK_EQ(insn->offset, bottom_block->start_offset);
263 DCHECK(static_cast<int>(insn->dalvikInsn.opcode) == kMirOpCheck ||
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700264 !MIR::DecodedInstruction::IsPseudoMirOp(insn->dalvikInsn.opcode));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100265 DCHECK_EQ(dex_pc_to_block_map_[insn->offset], orig_block->id);
Mathew Zaleski33c17022014-09-15 09:44:14 -0400266 // Scan the "bottom" instructions, remapping them to the
267 // newly created "bottom" block.
Vladimir Marko4376c872014-01-23 12:39:29 +0000268 MIR* p = insn;
Mathew Zaleski33c17022014-09-15 09:44:14 -0400269 p->bb = bottom_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100270 dex_pc_to_block_map_[p->offset] = bottom_block->id;
Vladimir Marko4376c872014-01-23 12:39:29 +0000271 while (p != bottom_block->last_mir_insn) {
272 p = p->next;
273 DCHECK(p != nullptr);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700274 p->bb = bottom_block->id;
buzbeeb48819d2013-09-14 16:15:25 -0700275 int opcode = p->dalvikInsn.opcode;
276 /*
277 * Some messiness here to ensure that we only enter real opcodes and only the
278 * first half of a potentially throwing instruction that has been split into
Vladimir Marko4376c872014-01-23 12:39:29 +0000279 * CHECK and work portions. Since the 2nd half of a split operation is always
280 * the first in a BasicBlock, we can't hit it here.
buzbeeb48819d2013-09-14 16:15:25 -0700281 */
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700282 if ((opcode == kMirOpCheck) || !MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
Mathew Zaleski33c17022014-09-15 09:44:14 -0400283 BasicBlockId mapped_id = dex_pc_to_block_map_[p->offset];
284 // At first glance the instructions should all be mapped to orig_block.
285 // However, multiple instructions may correspond to the same dex, hence an earlier
286 // instruction may have already moved the mapping for dex to bottom_block.
287 DCHECK((mapped_id == orig_block->id) || (mapped_id == bottom_block->id));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100288 dex_pc_to_block_map_[p->offset] = bottom_block->id;
buzbeeb48819d2013-09-14 16:15:25 -0700289 }
buzbeeb48819d2013-09-14 16:15:25 -0700290 }
291
buzbee311ca162013-02-28 15:56:43 -0800292 return bottom_block;
293}
294
295/*
296 * Given a code offset, find out the block that starts with it. If the offset
297 * is in the middle of an existing block, split it into two. If immed_pred_block_p
298 * is not non-null and is the block being split, update *immed_pred_block_p to
299 * point to the bottom block so that outgoing edges can be set up properly
300 * (by the caller)
301 * Utilizes a map for fast lookup of the typical cases.
302 */
buzbee0d829482013-10-11 15:24:55 -0700303BasicBlock* MIRGraph::FindBlock(DexOffset code_offset, bool split, bool create,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700304 BasicBlock** immed_pred_block_p) {
Razvan A Lupusoru09ae0222014-07-07 16:29:37 -0700305 if (code_offset >= current_code_item_->insns_size_in_code_units_) {
buzbee311ca162013-02-28 15:56:43 -0800306 return NULL;
307 }
buzbeeb48819d2013-09-14 16:15:25 -0700308
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100309 int block_id = dex_pc_to_block_map_[code_offset];
310 BasicBlock* bb = GetBasicBlock(block_id);
buzbeeb48819d2013-09-14 16:15:25 -0700311
312 if ((bb != NULL) && (bb->start_offset == code_offset)) {
313 // Does this containing block start with the desired instruction?
buzbeebd663de2013-09-10 15:41:31 -0700314 return bb;
315 }
buzbee311ca162013-02-28 15:56:43 -0800316
buzbeeb48819d2013-09-14 16:15:25 -0700317 // No direct hit.
318 if (!create) {
319 return NULL;
buzbee311ca162013-02-28 15:56:43 -0800320 }
321
buzbeeb48819d2013-09-14 16:15:25 -0700322 if (bb != NULL) {
323 // The target exists somewhere in an existing block.
324 return SplitBlock(code_offset, bb, bb == *immed_pred_block_p ? immed_pred_block_p : NULL);
325 }
326
327 // Create a new block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100328 bb = CreateNewBB(kDalvikByteCode);
buzbee311ca162013-02-28 15:56:43 -0800329 bb->start_offset = code_offset;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100330 dex_pc_to_block_map_[bb->start_offset] = bb->id;
buzbee311ca162013-02-28 15:56:43 -0800331 return bb;
332}
333
buzbeeb48819d2013-09-14 16:15:25 -0700334
buzbee311ca162013-02-28 15:56:43 -0800335/* Identify code range in try blocks and set up the empty catch blocks */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700336void MIRGraph::ProcessTryCatchBlocks() {
buzbee311ca162013-02-28 15:56:43 -0800337 int tries_size = current_code_item_->tries_size_;
buzbee0d829482013-10-11 15:24:55 -0700338 DexOffset offset;
buzbee311ca162013-02-28 15:56:43 -0800339
340 if (tries_size == 0) {
341 return;
342 }
343
344 for (int i = 0; i < tries_size; i++) {
345 const DexFile::TryItem* pTry =
346 DexFile::GetTryItems(*current_code_item_, i);
buzbee0d829482013-10-11 15:24:55 -0700347 DexOffset start_offset = pTry->start_addr_;
348 DexOffset end_offset = start_offset + pTry->insn_count_;
buzbee311ca162013-02-28 15:56:43 -0800349 for (offset = start_offset; offset < end_offset; offset++) {
buzbee862a7602013-04-05 10:58:54 -0700350 try_block_addr_->SetBit(offset);
buzbee311ca162013-02-28 15:56:43 -0800351 }
352 }
353
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700354 // Iterate over each of the handlers to enqueue the empty Catch blocks.
buzbee311ca162013-02-28 15:56:43 -0800355 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*current_code_item_, 0);
356 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
357 for (uint32_t idx = 0; idx < handlers_size; idx++) {
358 CatchHandlerIterator iterator(handlers_ptr);
359 for (; iterator.HasNext(); iterator.Next()) {
360 uint32_t address = iterator.GetHandlerAddress();
361 FindBlock(address, false /* split */, true /*create*/,
362 /* immed_pred_block_p */ NULL);
363 }
364 handlers_ptr = iterator.EndDataPointer();
365 }
366}
367
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100368bool MIRGraph::IsBadMonitorExitCatch(NarrowDexOffset monitor_exit_offset,
369 NarrowDexOffset catch_offset) {
370 // Catches for monitor-exit during stack unwinding have the pattern
371 // move-exception (move)* (goto)? monitor-exit throw
372 // In the currently generated dex bytecode we see these catching a bytecode range including
373 // either its own or an identical monitor-exit, http://b/15745363 . This function checks if
374 // it's the case for a given monitor-exit and catch block so that we can ignore it.
375 // (We don't want to ignore all monitor-exit catches since one could enclose a synchronized
376 // block in a try-block and catch the NPE, Error or Throwable and we should let it through;
377 // even though a throwing monitor-exit certainly indicates a bytecode error.)
Razvan A Lupusoru09ae0222014-07-07 16:29:37 -0700378 const Instruction* monitor_exit = Instruction::At(current_code_item_->insns_ + monitor_exit_offset);
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100379 DCHECK(monitor_exit->Opcode() == Instruction::MONITOR_EXIT);
380 int monitor_reg = monitor_exit->VRegA_11x();
Razvan A Lupusoru09ae0222014-07-07 16:29:37 -0700381 const Instruction* check_insn = Instruction::At(current_code_item_->insns_ + catch_offset);
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100382 DCHECK(check_insn->Opcode() == Instruction::MOVE_EXCEPTION);
383 if (check_insn->VRegA_11x() == monitor_reg) {
384 // Unexpected move-exception to the same register. Probably not the pattern we're looking for.
385 return false;
386 }
387 check_insn = check_insn->Next();
388 while (true) {
389 int dest = -1;
390 bool wide = false;
391 switch (check_insn->Opcode()) {
392 case Instruction::MOVE_WIDE:
393 wide = true;
394 // Intentional fall-through.
395 case Instruction::MOVE_OBJECT:
396 case Instruction::MOVE:
397 dest = check_insn->VRegA_12x();
398 break;
399
400 case Instruction::MOVE_WIDE_FROM16:
401 wide = true;
402 // Intentional fall-through.
403 case Instruction::MOVE_OBJECT_FROM16:
404 case Instruction::MOVE_FROM16:
405 dest = check_insn->VRegA_22x();
406 break;
407
408 case Instruction::MOVE_WIDE_16:
409 wide = true;
410 // Intentional fall-through.
411 case Instruction::MOVE_OBJECT_16:
412 case Instruction::MOVE_16:
413 dest = check_insn->VRegA_32x();
414 break;
415
416 case Instruction::GOTO:
417 case Instruction::GOTO_16:
418 case Instruction::GOTO_32:
419 check_insn = check_insn->RelativeAt(check_insn->GetTargetOffset());
420 // Intentional fall-through.
421 default:
422 return check_insn->Opcode() == Instruction::MONITOR_EXIT &&
423 check_insn->VRegA_11x() == monitor_reg;
424 }
425
426 if (dest == monitor_reg || (wide && dest + 1 == monitor_reg)) {
427 return false;
428 }
429
430 check_insn = check_insn->Next();
431 }
432}
433
buzbee311ca162013-02-28 15:56:43 -0800434/* Process instructions with the kBranch flag */
buzbee0d829482013-10-11 15:24:55 -0700435BasicBlock* MIRGraph::ProcessCanBranch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
436 int width, int flags, const uint16_t* code_ptr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700437 const uint16_t* code_end) {
buzbee0d829482013-10-11 15:24:55 -0700438 DexOffset target = cur_offset;
buzbee311ca162013-02-28 15:56:43 -0800439 switch (insn->dalvikInsn.opcode) {
440 case Instruction::GOTO:
441 case Instruction::GOTO_16:
442 case Instruction::GOTO_32:
443 target += insn->dalvikInsn.vA;
444 break;
445 case Instruction::IF_EQ:
446 case Instruction::IF_NE:
447 case Instruction::IF_LT:
448 case Instruction::IF_GE:
449 case Instruction::IF_GT:
450 case Instruction::IF_LE:
451 cur_block->conditional_branch = true;
452 target += insn->dalvikInsn.vC;
453 break;
454 case Instruction::IF_EQZ:
455 case Instruction::IF_NEZ:
456 case Instruction::IF_LTZ:
457 case Instruction::IF_GEZ:
458 case Instruction::IF_GTZ:
459 case Instruction::IF_LEZ:
460 cur_block->conditional_branch = true;
461 target += insn->dalvikInsn.vB;
462 break;
463 default:
464 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
465 }
buzbeeb48819d2013-09-14 16:15:25 -0700466 CountBranch(target);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700467 BasicBlock* taken_block = FindBlock(target, /* split */ true, /* create */ true,
buzbee311ca162013-02-28 15:56:43 -0800468 /* immed_pred_block_p */ &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700469 cur_block->taken = taken_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100470 taken_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800471
472 /* Always terminate the current block for conditional branches */
473 if (flags & Instruction::kContinue) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700474 BasicBlock* fallthrough_block = FindBlock(cur_offset + width,
buzbee311ca162013-02-28 15:56:43 -0800475 /*
476 * If the method is processed
477 * in sequential order from the
478 * beginning, we don't need to
479 * specify split for continue
480 * blocks. However, this
481 * routine can be called by
482 * compileLoop, which starts
483 * parsing the method from an
484 * arbitrary address in the
485 * method body.
486 */
487 true,
488 /* create */
489 true,
490 /* immed_pred_block_p */
491 &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700492 cur_block->fall_through = fallthrough_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100493 fallthrough_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800494 } else if (code_ptr < code_end) {
buzbee27247762013-07-28 12:03:58 -0700495 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
buzbee311ca162013-02-28 15:56:43 -0800496 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800497 }
498 return cur_block;
499}
500
501/* Process instructions with the kSwitch flag */
buzbee17189ac2013-11-08 11:07:02 -0800502BasicBlock* MIRGraph::ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
503 int width, int flags) {
buzbee311ca162013-02-28 15:56:43 -0800504 const uint16_t* switch_data =
505 reinterpret_cast<const uint16_t*>(GetCurrentInsns() + cur_offset + insn->dalvikInsn.vB);
506 int size;
507 const int* keyTable;
508 const int* target_table;
509 int i;
510 int first_key;
511
512 /*
513 * Packed switch data format:
514 * ushort ident = 0x0100 magic value
515 * ushort size number of entries in the table
516 * int first_key first (and lowest) switch case value
517 * int targets[size] branch targets, relative to switch opcode
518 *
519 * Total size is (4+size*2) 16-bit code units.
520 */
521 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
522 DCHECK_EQ(static_cast<int>(switch_data[0]),
523 static_cast<int>(Instruction::kPackedSwitchSignature));
524 size = switch_data[1];
525 first_key = switch_data[2] | (switch_data[3] << 16);
526 target_table = reinterpret_cast<const int*>(&switch_data[4]);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700527 keyTable = NULL; // Make the compiler happy.
buzbee311ca162013-02-28 15:56:43 -0800528 /*
529 * Sparse switch data format:
530 * ushort ident = 0x0200 magic value
531 * ushort size number of entries in the table; > 0
532 * int keys[size] keys, sorted low-to-high; 32-bit aligned
533 * int targets[size] branch targets, relative to switch opcode
534 *
535 * Total size is (2+size*4) 16-bit code units.
536 */
537 } else {
538 DCHECK_EQ(static_cast<int>(switch_data[0]),
539 static_cast<int>(Instruction::kSparseSwitchSignature));
540 size = switch_data[1];
541 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
542 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700543 first_key = 0; // To make the compiler happy.
buzbee311ca162013-02-28 15:56:43 -0800544 }
545
buzbee0d829482013-10-11 15:24:55 -0700546 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800547 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700548 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800549 }
buzbee0d829482013-10-11 15:24:55 -0700550 cur_block->successor_block_list_type =
551 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? kPackedSwitch : kSparseSwitch;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100552 cur_block->successor_blocks.reserve(size);
buzbee311ca162013-02-28 15:56:43 -0800553
554 for (i = 0; i < size; i++) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700555 BasicBlock* case_block = FindBlock(cur_offset + target_table[i], /* split */ true,
buzbee311ca162013-02-28 15:56:43 -0800556 /* create */ true, /* immed_pred_block_p */ &cur_block);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700557 SuccessorBlockInfo* successor_block_info =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700558 static_cast<SuccessorBlockInfo*>(arena_->Alloc(sizeof(SuccessorBlockInfo),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000559 kArenaAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700560 successor_block_info->block = case_block->id;
buzbee311ca162013-02-28 15:56:43 -0800561 successor_block_info->key =
562 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
563 first_key + i : keyTable[i];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100564 cur_block->successor_blocks.push_back(successor_block_info);
565 case_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800566 }
567
568 /* Fall-through case */
Brian Carlstromdf629502013-07-17 22:39:56 -0700569 BasicBlock* fallthrough_block = FindBlock(cur_offset + width, /* split */ false,
570 /* create */ true, /* immed_pred_block_p */ NULL);
buzbee0d829482013-10-11 15:24:55 -0700571 cur_block->fall_through = fallthrough_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100572 fallthrough_block->predecessors.push_back(cur_block->id);
buzbee17189ac2013-11-08 11:07:02 -0800573 return cur_block;
buzbee311ca162013-02-28 15:56:43 -0800574}
575
576/* Process instructions with the kThrow flag */
buzbee0d829482013-10-11 15:24:55 -0700577BasicBlock* MIRGraph::ProcessCanThrow(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
578 int width, int flags, ArenaBitVector* try_block_addr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700579 const uint16_t* code_ptr, const uint16_t* code_end) {
buzbee862a7602013-04-05 10:58:54 -0700580 bool in_try_block = try_block_addr->IsBitSet(cur_offset);
buzbee17189ac2013-11-08 11:07:02 -0800581 bool is_throw = (insn->dalvikInsn.opcode == Instruction::THROW);
buzbee311ca162013-02-28 15:56:43 -0800582
583 /* In try block */
584 if (in_try_block) {
585 CatchHandlerIterator iterator(*current_code_item_, cur_offset);
586
buzbee0d829482013-10-11 15:24:55 -0700587 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800588 LOG(INFO) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
589 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700590 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800591 }
592
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700593 for (; iterator.HasNext(); iterator.Next()) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700594 BasicBlock* catch_block = FindBlock(iterator.GetHandlerAddress(), false /* split*/,
buzbee311ca162013-02-28 15:56:43 -0800595 false /* creat */, NULL /* immed_pred_block_p */);
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100596 if (insn->dalvikInsn.opcode == Instruction::MONITOR_EXIT &&
597 IsBadMonitorExitCatch(insn->offset, catch_block->start_offset)) {
598 // Don't allow monitor-exit to catch its own exception, http://b/15745363 .
599 continue;
600 }
601 if (cur_block->successor_block_list_type == kNotUsed) {
602 cur_block->successor_block_list_type = kCatch;
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100603 }
buzbee311ca162013-02-28 15:56:43 -0800604 catch_block->catch_entry = true;
605 if (kIsDebugBuild) {
606 catches_.insert(catch_block->start_offset);
607 }
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700608 SuccessorBlockInfo* successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000609 (arena_->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700610 successor_block_info->block = catch_block->id;
buzbee311ca162013-02-28 15:56:43 -0800611 successor_block_info->key = iterator.GetHandlerTypeIndex();
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100612 cur_block->successor_blocks.push_back(successor_block_info);
613 catch_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800614 }
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100615 in_try_block = (cur_block->successor_block_list_type != kNotUsed);
616 }
Vladimir Markoe767f6c2014-10-01 17:38:02 +0100617 bool build_all_edges =
618 (cu_->disable_opt & (1 << kSuppressExceptionEdges)) || is_throw || in_try_block;
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100619 if (!in_try_block && build_all_edges) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100620 BasicBlock* eh_block = CreateNewBB(kExceptionHandling);
buzbee0d829482013-10-11 15:24:55 -0700621 cur_block->taken = eh_block->id;
buzbee311ca162013-02-28 15:56:43 -0800622 eh_block->start_offset = cur_offset;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100623 eh_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800624 }
625
buzbee17189ac2013-11-08 11:07:02 -0800626 if (is_throw) {
buzbee311ca162013-02-28 15:56:43 -0800627 cur_block->explicit_throw = true;
buzbee27247762013-07-28 12:03:58 -0700628 if (code_ptr < code_end) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700629 // Force creation of new block following THROW via side-effect.
buzbee311ca162013-02-28 15:56:43 -0800630 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
631 /* immed_pred_block_p */ NULL);
632 }
633 if (!in_try_block) {
634 // Don't split a THROW that can't rethrow - we're done.
635 return cur_block;
636 }
637 }
638
buzbee17189ac2013-11-08 11:07:02 -0800639 if (!build_all_edges) {
640 /*
641 * Even though there is an exception edge here, control cannot return to this
642 * method. Thus, for the purposes of dataflow analysis and optimization, we can
643 * ignore the edge. Doing this reduces compile time, and increases the scope
644 * of the basic-block level optimization pass.
645 */
646 return cur_block;
647 }
648
buzbee311ca162013-02-28 15:56:43 -0800649 /*
650 * Split the potentially-throwing instruction into two parts.
651 * The first half will be a pseudo-op that captures the exception
652 * edges and terminates the basic block. It always falls through.
653 * Then, create a new basic block that begins with the throwing instruction
654 * (minus exceptions). Note: this new basic block must NOT be entered into
655 * the block_map. If the potentially-throwing instruction is the target of a
656 * future branch, we need to find the check psuedo half. The new
657 * basic block containing the work portion of the instruction should
658 * only be entered via fallthrough from the block containing the
659 * pseudo exception edge MIR. Note also that this new block is
660 * not automatically terminated after the work portion, and may
661 * contain following instructions.
buzbeeb48819d2013-09-14 16:15:25 -0700662 *
663 * Note also that the dex_pc_to_block_map_ entry for the potentially
664 * throwing instruction will refer to the original basic block.
buzbee311ca162013-02-28 15:56:43 -0800665 */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100666 BasicBlock* new_block = CreateNewBB(kDalvikByteCode);
buzbee311ca162013-02-28 15:56:43 -0800667 new_block->start_offset = insn->offset;
buzbee0d829482013-10-11 15:24:55 -0700668 cur_block->fall_through = new_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100669 new_block->predecessors.push_back(cur_block->id);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700670 MIR* new_insn = NewMIR();
buzbee311ca162013-02-28 15:56:43 -0800671 *new_insn = *insn;
buzbee35ba7f32014-05-31 08:59:01 -0700672 insn->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheck);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700673 // Associate the two halves.
buzbee311ca162013-02-28 15:56:43 -0800674 insn->meta.throw_insn = new_insn;
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700675 new_block->AppendMIR(new_insn);
buzbee311ca162013-02-28 15:56:43 -0800676 return new_block;
677}
678
679/* Parse a Dex method and insert it into the MIRGraph at the current insert point. */
680void MIRGraph::InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700681 InvokeType invoke_type, uint16_t class_def_idx,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700682 uint32_t method_idx, jobject class_loader, const DexFile& dex_file) {
buzbee311ca162013-02-28 15:56:43 -0800683 current_code_item_ = code_item;
684 method_stack_.push_back(std::make_pair(current_method_, current_offset_));
685 current_method_ = m_units_.size();
686 current_offset_ = 0;
687 // TODO: will need to snapshot stack image and use that as the mir context identification.
688 m_units_.push_back(new DexCompilationUnit(cu_, class_loader, Runtime::Current()->GetClassLinker(),
Vladimir Marko2730db02014-01-27 11:15:17 +0000689 dex_file, current_code_item_, class_def_idx, method_idx, access_flags,
690 cu_->compiler_driver->GetVerifiedMethod(&dex_file, method_idx)));
buzbee311ca162013-02-28 15:56:43 -0800691 const uint16_t* code_ptr = current_code_item_->insns_;
692 const uint16_t* code_end =
693 current_code_item_->insns_ + current_code_item_->insns_size_in_code_units_;
694
695 // TODO: need to rework expansion of block list & try_block_addr when inlining activated.
buzbeeb48819d2013-09-14 16:15:25 -0700696 // TUNING: use better estimate of basic blocks for following resize.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100697 block_list_.reserve(block_list_.size() + current_code_item_->insns_size_in_code_units_);
698 dex_pc_to_block_map_.resize(dex_pc_to_block_map_.size() + current_code_item_->insns_size_in_code_units_);
buzbeebd663de2013-09-10 15:41:31 -0700699
buzbee311ca162013-02-28 15:56:43 -0800700 // TODO: replace with explicit resize routine. Using automatic extension side effect for now.
buzbee862a7602013-04-05 10:58:54 -0700701 try_block_addr_->SetBit(current_code_item_->insns_size_in_code_units_);
702 try_block_addr_->ClearBit(current_code_item_->insns_size_in_code_units_);
buzbee311ca162013-02-28 15:56:43 -0800703
704 // If this is the first method, set up default entry and exit blocks.
705 if (current_method_ == 0) {
706 DCHECK(entry_block_ == NULL);
707 DCHECK(exit_block_ == NULL);
Andreas Gampe44395962014-06-13 13:44:40 -0700708 DCHECK_EQ(num_blocks_, 0U);
buzbee0d829482013-10-11 15:24:55 -0700709 // Use id 0 to represent a null block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100710 BasicBlock* null_block = CreateNewBB(kNullBlock);
buzbee0d829482013-10-11 15:24:55 -0700711 DCHECK_EQ(null_block->id, NullBasicBlockId);
712 null_block->hidden = true;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100713 entry_block_ = CreateNewBB(kEntryBlock);
714 exit_block_ = CreateNewBB(kExitBlock);
buzbee311ca162013-02-28 15:56:43 -0800715 // TODO: deprecate all "cu->" fields; move what's left to wherever CompilationUnit is allocated.
716 cu_->dex_file = &dex_file;
717 cu_->class_def_idx = class_def_idx;
718 cu_->method_idx = method_idx;
719 cu_->access_flags = access_flags;
720 cu_->invoke_type = invoke_type;
721 cu_->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
buzbee311ca162013-02-28 15:56:43 -0800722 } else {
723 UNIMPLEMENTED(FATAL) << "Nested inlining not implemented.";
724 /*
725 * Will need to manage storage for ins & outs, push prevous state and update
726 * insert point.
727 */
728 }
729
730 /* Current block to record parsed instructions */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100731 BasicBlock* cur_block = CreateNewBB(kDalvikByteCode);
buzbee0d829482013-10-11 15:24:55 -0700732 DCHECK_EQ(current_offset_, 0U);
buzbee311ca162013-02-28 15:56:43 -0800733 cur_block->start_offset = current_offset_;
buzbee0d829482013-10-11 15:24:55 -0700734 // TODO: for inlining support, insert at the insert point rather than entry block.
735 entry_block_->fall_through = cur_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100736 cur_block->predecessors.push_back(entry_block_->id);
buzbee311ca162013-02-28 15:56:43 -0800737
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000738 /* Identify code range in try blocks and set up the empty catch blocks */
buzbee311ca162013-02-28 15:56:43 -0800739 ProcessTryCatchBlocks();
740
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000741 uint64_t merged_df_flags = 0u;
742
buzbee311ca162013-02-28 15:56:43 -0800743 /* Parse all instructions and put them into containing basic blocks */
744 while (code_ptr < code_end) {
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700745 MIR *insn = NewMIR();
buzbee311ca162013-02-28 15:56:43 -0800746 insn->offset = current_offset_;
747 insn->m_unit_index = current_method_;
748 int width = ParseInsn(code_ptr, &insn->dalvikInsn);
buzbee311ca162013-02-28 15:56:43 -0800749 Instruction::Code opcode = insn->dalvikInsn.opcode;
750 if (opcode_count_ != NULL) {
751 opcode_count_[static_cast<int>(opcode)]++;
752 }
753
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -0700754 int flags = insn->dalvikInsn.FlagsOf();
buzbeeb1f1d642014-02-27 12:55:32 -0800755 int verify_flags = Instruction::VerifyFlagsOf(insn->dalvikInsn.opcode);
buzbee311ca162013-02-28 15:56:43 -0800756
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700757 uint64_t df_flags = GetDataFlowAttributes(insn);
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000758 merged_df_flags |= df_flags;
buzbee311ca162013-02-28 15:56:43 -0800759
760 if (df_flags & DF_HAS_DEFS) {
761 def_count_ += (df_flags & DF_A_WIDE) ? 2 : 1;
762 }
763
buzbee1da1e2f2013-11-15 13:37:01 -0800764 if (df_flags & DF_LVN) {
765 cur_block->use_lvn = true; // Run local value numbering on this basic block.
766 }
767
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700768 // Check for inline data block signatures.
buzbee27247762013-07-28 12:03:58 -0700769 if (opcode == Instruction::NOP) {
770 // A simple NOP will have a width of 1 at this point, embedded data NOP > 1.
771 if ((width == 1) && ((current_offset_ & 0x1) == 0x1) && ((code_end - code_ptr) > 1)) {
772 // Could be an aligning nop. If an embedded data NOP follows, treat pair as single unit.
773 uint16_t following_raw_instruction = code_ptr[1];
774 if ((following_raw_instruction == Instruction::kSparseSwitchSignature) ||
775 (following_raw_instruction == Instruction::kPackedSwitchSignature) ||
776 (following_raw_instruction == Instruction::kArrayDataSignature)) {
777 width += Instruction::At(code_ptr + 1)->SizeInCodeUnits();
778 }
779 }
780 if (width == 1) {
781 // It is a simple nop - treat normally.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700782 cur_block->AppendMIR(insn);
buzbee27247762013-07-28 12:03:58 -0700783 } else {
buzbee0d829482013-10-11 15:24:55 -0700784 DCHECK(cur_block->fall_through == NullBasicBlockId);
785 DCHECK(cur_block->taken == NullBasicBlockId);
buzbee27247762013-07-28 12:03:58 -0700786 // Unreachable instruction, mark for no continuation.
787 flags &= ~Instruction::kContinue;
788 }
789 } else {
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700790 cur_block->AppendMIR(insn);
buzbee27247762013-07-28 12:03:58 -0700791 }
792
buzbeeb48819d2013-09-14 16:15:25 -0700793 // Associate the starting dex_pc for this opcode with its containing basic block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100794 dex_pc_to_block_map_[insn->offset] = cur_block->id;
buzbeeb48819d2013-09-14 16:15:25 -0700795
buzbee27247762013-07-28 12:03:58 -0700796 code_ptr += width;
797
buzbee311ca162013-02-28 15:56:43 -0800798 if (flags & Instruction::kBranch) {
799 cur_block = ProcessCanBranch(cur_block, insn, current_offset_,
800 width, flags, code_ptr, code_end);
801 } else if (flags & Instruction::kReturn) {
802 cur_block->terminated_by_return = true;
buzbee0d829482013-10-11 15:24:55 -0700803 cur_block->fall_through = exit_block_->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100804 exit_block_->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800805 /*
806 * Terminate the current block if there are instructions
807 * afterwards.
808 */
809 if (code_ptr < code_end) {
810 /*
811 * Create a fallthrough block for real instructions
812 * (incl. NOP).
813 */
buzbee27247762013-07-28 12:03:58 -0700814 FindBlock(current_offset_ + width, /* split */ false, /* create */ true,
815 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800816 }
817 } else if (flags & Instruction::kThrow) {
818 cur_block = ProcessCanThrow(cur_block, insn, current_offset_, width, flags, try_block_addr_,
819 code_ptr, code_end);
820 } else if (flags & Instruction::kSwitch) {
buzbee17189ac2013-11-08 11:07:02 -0800821 cur_block = ProcessCanSwitch(cur_block, insn, current_offset_, width, flags);
buzbee311ca162013-02-28 15:56:43 -0800822 }
Alexei Zavjalov688e7c52014-07-16 02:17:58 +0700823 if (verify_flags & Instruction::kVerifyVarArgRange ||
824 verify_flags & Instruction::kVerifyVarArgRangeNonZero) {
buzbeeb1f1d642014-02-27 12:55:32 -0800825 /*
826 * The Quick backend's runtime model includes a gap between a method's
827 * argument ("in") vregs and the rest of its vregs. Handling a range instruction
828 * which spans the gap is somewhat complicated, and should not happen
829 * in normal usage of dx. Punt to the interpreter.
830 */
831 int first_reg_in_range = insn->dalvikInsn.vC;
832 int last_reg_in_range = first_reg_in_range + insn->dalvikInsn.vA - 1;
833 if (IsInVReg(first_reg_in_range) != IsInVReg(last_reg_in_range)) {
834 punt_to_interpreter_ = true;
835 }
836 }
buzbee311ca162013-02-28 15:56:43 -0800837 current_offset_ += width;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700838 BasicBlock* next_block = FindBlock(current_offset_, /* split */ false, /* create */
buzbee311ca162013-02-28 15:56:43 -0800839 false, /* immed_pred_block_p */ NULL);
840 if (next_block) {
841 /*
842 * The next instruction could be the target of a previously parsed
843 * forward branch so a block is already created. If the current
844 * instruction is not an unconditional branch, connect them through
845 * the fall-through link.
846 */
buzbee0d829482013-10-11 15:24:55 -0700847 DCHECK(cur_block->fall_through == NullBasicBlockId ||
848 GetBasicBlock(cur_block->fall_through) == next_block ||
849 GetBasicBlock(cur_block->fall_through) == exit_block_);
buzbee311ca162013-02-28 15:56:43 -0800850
buzbee0d829482013-10-11 15:24:55 -0700851 if ((cur_block->fall_through == NullBasicBlockId) && (flags & Instruction::kContinue)) {
852 cur_block->fall_through = next_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100853 next_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800854 }
855 cur_block = next_block;
856 }
857 }
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000858 merged_df_flags_ = merged_df_flags;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000859
buzbee311ca162013-02-28 15:56:43 -0800860 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
861 DumpCFG("/sdcard/1_post_parse_cfg/", true);
862 }
863
864 if (cu_->verbose) {
buzbee1fd33462013-03-25 13:40:45 -0700865 DumpMIRGraph();
buzbee311ca162013-02-28 15:56:43 -0800866 }
867}
868
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700869void MIRGraph::ShowOpcodeStats() {
buzbee311ca162013-02-28 15:56:43 -0800870 DCHECK(opcode_count_ != NULL);
871 LOG(INFO) << "Opcode Count";
872 for (int i = 0; i < kNumPackedOpcodes; i++) {
873 if (opcode_count_[i] != 0) {
874 LOG(INFO) << "-C- " << Instruction::Name(static_cast<Instruction::Code>(i))
875 << " " << opcode_count_[i];
876 }
877 }
878}
879
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700880uint64_t MIRGraph::GetDataFlowAttributes(Instruction::Code opcode) {
881 DCHECK_LT((size_t) opcode, (sizeof(oat_data_flow_attributes_) / sizeof(oat_data_flow_attributes_[0])));
882 return oat_data_flow_attributes_[opcode];
883}
884
885uint64_t MIRGraph::GetDataFlowAttributes(MIR* mir) {
886 DCHECK(mir != nullptr);
887 Instruction::Code opcode = mir->dalvikInsn.opcode;
888 return GetDataFlowAttributes(opcode);
889}
890
buzbee311ca162013-02-28 15:56:43 -0800891// TODO: use a configurable base prefix, and adjust callers to supply pass name.
892/* Dump the CFG into a DOT graph */
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800893void MIRGraph::DumpCFG(const char* dir_prefix, bool all_blocks, const char *suffix) {
buzbee311ca162013-02-28 15:56:43 -0800894 FILE* file;
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700895 static AtomicInteger cnt(0);
896
897 // Increment counter to get a unique file number.
898 cnt++;
899
buzbee311ca162013-02-28 15:56:43 -0800900 std::string fname(PrettyMethod(cu_->method_idx, *cu_->dex_file));
901 ReplaceSpecialChars(fname);
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700902 fname = StringPrintf("%s%s%x%s_%d.dot", dir_prefix, fname.c_str(),
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800903 GetBasicBlock(GetEntryBlock()->fall_through)->start_offset,
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700904 suffix == nullptr ? "" : suffix,
905 cnt.LoadRelaxed());
buzbee311ca162013-02-28 15:56:43 -0800906 file = fopen(fname.c_str(), "w");
907 if (file == NULL) {
908 return;
909 }
910 fprintf(file, "digraph G {\n");
911
912 fprintf(file, " rankdir=TB\n");
913
914 int num_blocks = all_blocks ? GetNumBlocks() : num_reachable_blocks_;
915 int idx;
916
917 for (idx = 0; idx < num_blocks; idx++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100918 int block_idx = all_blocks ? idx : dfs_order_[idx];
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700919 BasicBlock* bb = GetBasicBlock(block_idx);
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700920 if (bb == NULL) continue;
buzbee311ca162013-02-28 15:56:43 -0800921 if (bb->block_type == kDead) continue;
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700922 if (bb->hidden) continue;
buzbee311ca162013-02-28 15:56:43 -0800923 if (bb->block_type == kEntryBlock) {
924 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
925 } else if (bb->block_type == kExitBlock) {
926 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
927 } else if (bb->block_type == kDalvikByteCode) {
928 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
929 bb->start_offset, bb->id);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700930 const MIR* mir;
buzbee311ca162013-02-28 15:56:43 -0800931 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
932 bb->first_mir_insn ? " | " : " ");
933 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
934 int opcode = mir->dalvikInsn.opcode;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -0700935 fprintf(file, " {%04x %s %s %s %s %s %s %s\\l}%s\\\n", mir->offset,
Mark Mendelld65c51a2014-04-29 16:55:20 -0400936 mir->ssa_rep ? GetDalvikDisassembly(mir) :
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700937 !MIR::DecodedInstruction::IsPseudoMirOp(opcode) ?
938 Instruction::Name(mir->dalvikInsn.opcode) :
Mark Mendelld65c51a2014-04-29 16:55:20 -0400939 extended_mir_op_names_[opcode - kMirOpFirst],
940 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
941 (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700942 (mir->optimization_flags & MIR_IGNORE_SUSPEND_CHECK) != 0 ? " no_suspendcheck" : " ",
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700943 (mir->optimization_flags & MIR_STORE_NON_TEMPORAL) != 0 ? " non_temporal" : " ",
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -0700944 (mir->optimization_flags & MIR_CALLEE) != 0 ? " inlined" : " ",
945 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0 ? " no_clinit" : " ",
Mark Mendelld65c51a2014-04-29 16:55:20 -0400946 mir->next ? " | " : " ");
buzbee311ca162013-02-28 15:56:43 -0800947 }
948 fprintf(file, " }\"];\n\n");
949 } else if (bb->block_type == kExceptionHandling) {
950 char block_name[BLOCK_NAME_LEN];
951
952 GetBlockName(bb, block_name);
953 fprintf(file, " %s [shape=invhouse];\n", block_name);
954 }
955
956 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
957
buzbee0d829482013-10-11 15:24:55 -0700958 if (bb->taken != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800959 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700960 GetBlockName(GetBasicBlock(bb->taken), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800961 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
962 block_name1, block_name2);
963 }
buzbee0d829482013-10-11 15:24:55 -0700964 if (bb->fall_through != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800965 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700966 GetBlockName(GetBasicBlock(bb->fall_through), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800967 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
968 }
969
buzbee0d829482013-10-11 15:24:55 -0700970 if (bb->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800971 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
972 bb->start_offset, bb->id,
buzbee0d829482013-10-11 15:24:55 -0700973 (bb->successor_block_list_type == kCatch) ? "Mrecord" : "record");
buzbee311ca162013-02-28 15:56:43 -0800974
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100975 int last_succ_id = static_cast<int>(bb->successor_blocks.size() - 1u);
buzbee311ca162013-02-28 15:56:43 -0800976 int succ_id = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100977 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700978 BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800979 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100980 succ_id,
buzbee311ca162013-02-28 15:56:43 -0800981 successor_block_info->key,
982 dest_block->start_offset,
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100983 (succ_id != last_succ_id) ? " | " : " ");
984 ++succ_id;
buzbee311ca162013-02-28 15:56:43 -0800985 }
986 fprintf(file, " }\"];\n\n");
987
988 GetBlockName(bb, block_name1);
989 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
990 block_name1, bb->start_offset, bb->id);
991
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700992 // Link the successor pseudo-block with all of its potential targets.
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700993 succ_id = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100994 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700995 BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800996
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700997 GetBlockName(dest_block, block_name2);
998 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
999 bb->id, succ_id++, block_name2);
buzbee311ca162013-02-28 15:56:43 -08001000 }
1001 }
1002 fprintf(file, "\n");
1003
1004 if (cu_->verbose) {
1005 /* Display the dominator tree */
1006 GetBlockName(bb, block_name1);
1007 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
1008 block_name1, block_name1);
1009 if (bb->i_dom) {
buzbee0d829482013-10-11 15:24:55 -07001010 GetBlockName(GetBasicBlock(bb->i_dom), block_name2);
buzbee311ca162013-02-28 15:56:43 -08001011 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
1012 }
1013 }
1014 }
1015 fprintf(file, "}\n");
1016 fclose(file);
1017}
1018
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001019/* Insert an MIR instruction to the end of a basic block. */
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001020void BasicBlock::AppendMIR(MIR* mir) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001021 // Insert it after the last MIR.
1022 InsertMIRListAfter(last_mir_insn, mir, mir);
buzbee1fd33462013-03-25 13:40:45 -07001023}
1024
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001025void BasicBlock::AppendMIRList(MIR* first_list_mir, MIR* last_list_mir) {
1026 // Insert it after the last MIR.
1027 InsertMIRListAfter(last_mir_insn, first_list_mir, last_list_mir);
1028}
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001029
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001030void BasicBlock::AppendMIRList(const std::vector<MIR*>& insns) {
1031 for (std::vector<MIR*>::const_iterator it = insns.begin(); it != insns.end(); it++) {
1032 MIR* new_mir = *it;
1033
1034 // Add a copy of each MIR.
1035 InsertMIRListAfter(last_mir_insn, new_mir, new_mir);
1036 }
buzbee1fd33462013-03-25 13:40:45 -07001037}
1038
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001039/* Insert a MIR instruction after the specified MIR. */
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001040void BasicBlock::InsertMIRAfter(MIR* current_mir, MIR* new_mir) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001041 InsertMIRListAfter(current_mir, new_mir, new_mir);
1042}
buzbee1fd33462013-03-25 13:40:45 -07001043
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001044void BasicBlock::InsertMIRListAfter(MIR* insert_after, MIR* first_list_mir, MIR* last_list_mir) {
1045 // If no MIR, we are done.
1046 if (first_list_mir == nullptr || last_list_mir == nullptr) {
1047 return;
buzbee1fd33462013-03-25 13:40:45 -07001048 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001049
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001050 // If insert_after is null, assume BB is empty.
1051 if (insert_after == nullptr) {
1052 first_mir_insn = first_list_mir;
1053 last_mir_insn = last_list_mir;
1054 last_list_mir->next = nullptr;
1055 } else {
1056 MIR* after_list = insert_after->next;
1057 insert_after->next = first_list_mir;
1058 last_list_mir->next = after_list;
1059 if (after_list == nullptr) {
1060 last_mir_insn = last_list_mir;
1061 }
1062 }
1063
1064 // Set this BB to be the basic block of the MIRs.
1065 MIR* last = last_list_mir->next;
1066 for (MIR* mir = first_list_mir; mir != last; mir = mir->next) {
1067 mir->bb = id;
1068 }
1069}
1070
1071/* Insert an MIR instruction to the head of a basic block. */
1072void BasicBlock::PrependMIR(MIR* mir) {
1073 InsertMIRListBefore(first_mir_insn, mir, mir);
1074}
1075
1076void BasicBlock::PrependMIRList(MIR* first_list_mir, MIR* last_list_mir) {
1077 // Insert it before the first MIR.
1078 InsertMIRListBefore(first_mir_insn, first_list_mir, last_list_mir);
1079}
1080
1081void BasicBlock::PrependMIRList(const std::vector<MIR*>& to_add) {
1082 for (std::vector<MIR*>::const_iterator it = to_add.begin(); it != to_add.end(); it++) {
1083 MIR* mir = *it;
1084
1085 InsertMIRListBefore(first_mir_insn, mir, mir);
1086 }
1087}
1088
1089/* Insert a MIR instruction before the specified MIR. */
1090void BasicBlock::InsertMIRBefore(MIR* current_mir, MIR* new_mir) {
1091 // Insert as a single element list.
1092 return InsertMIRListBefore(current_mir, new_mir, new_mir);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001093}
1094
1095MIR* BasicBlock::FindPreviousMIR(MIR* mir) {
1096 MIR* current = first_mir_insn;
1097
1098 while (current != nullptr) {
1099 MIR* next = current->next;
1100
1101 if (next == mir) {
1102 return current;
1103 }
1104
1105 current = next;
1106 }
1107
1108 return nullptr;
1109}
1110
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001111void BasicBlock::InsertMIRListBefore(MIR* insert_before, MIR* first_list_mir, MIR* last_list_mir) {
1112 // If no MIR, we are done.
1113 if (first_list_mir == nullptr || last_list_mir == nullptr) {
1114 return;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001115 }
1116
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001117 // If insert_before is null, assume BB is empty.
1118 if (insert_before == nullptr) {
1119 first_mir_insn = first_list_mir;
1120 last_mir_insn = last_list_mir;
1121 last_list_mir->next = nullptr;
1122 } else {
1123 if (first_mir_insn == insert_before) {
1124 last_list_mir->next = first_mir_insn;
1125 first_mir_insn = first_list_mir;
1126 } else {
1127 // Find the preceding MIR.
1128 MIR* before_list = FindPreviousMIR(insert_before);
1129 DCHECK(before_list != nullptr);
1130 before_list->next = first_list_mir;
1131 last_list_mir->next = insert_before;
1132 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001133 }
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001134
1135 // Set this BB to be the basic block of the MIRs.
1136 for (MIR* mir = first_list_mir; mir != last_list_mir->next; mir = mir->next) {
1137 mir->bb = id;
1138 }
1139}
1140
1141bool BasicBlock::RemoveMIR(MIR* mir) {
1142 // Remove as a single element list.
1143 return RemoveMIRList(mir, mir);
1144}
1145
1146bool BasicBlock::RemoveMIRList(MIR* first_list_mir, MIR* last_list_mir) {
1147 if (first_list_mir == nullptr) {
1148 return false;
1149 }
1150
1151 // Try to find the MIR.
1152 MIR* before_list = nullptr;
1153 MIR* after_list = nullptr;
1154
1155 // If we are removing from the beginning of the MIR list.
1156 if (first_mir_insn == first_list_mir) {
1157 before_list = nullptr;
1158 } else {
1159 before_list = FindPreviousMIR(first_list_mir);
1160 if (before_list == nullptr) {
1161 // We did not find the mir.
1162 return false;
1163 }
1164 }
1165
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001166 // Remove the BB information and also find the after_list.
Chao-ying Fu590c6a42014-09-23 14:54:32 -07001167 for (MIR* mir = first_list_mir; mir != last_list_mir->next; mir = mir->next) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001168 mir->bb = NullBasicBlockId;
1169 }
1170
1171 after_list = last_list_mir->next;
1172
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001173 // If there is nothing before the list, after_list is the first_mir.
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001174 if (before_list == nullptr) {
1175 first_mir_insn = after_list;
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001176 } else {
1177 before_list->next = after_list;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001178 }
1179
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001180 // If there is nothing after the list, before_list is last_mir.
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001181 if (after_list == nullptr) {
1182 last_mir_insn = before_list;
1183 }
1184
1185 return true;
buzbee1fd33462013-03-25 13:40:45 -07001186}
1187
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001188MIR* BasicBlock::GetNextUnconditionalMir(MIRGraph* mir_graph, MIR* current) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001189 MIR* next_mir = nullptr;
1190
1191 if (current != nullptr) {
1192 next_mir = current->next;
1193 }
1194
1195 if (next_mir == nullptr) {
1196 // Only look for next MIR that follows unconditionally.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001197 if ((taken == NullBasicBlockId) && (fall_through != NullBasicBlockId)) {
1198 next_mir = mir_graph->GetBasicBlock(fall_through)->first_mir_insn;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001199 }
1200 }
1201
1202 return next_mir;
1203}
1204
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001205static void FillTypeSizeString(uint32_t type_size, std::string* decoded_mir) {
1206 DCHECK(decoded_mir != nullptr);
1207 OpSize type = static_cast<OpSize>(type_size >> 16);
1208 uint16_t vect_size = (type_size & 0xFFFF);
1209
1210 // Now print the type and vector size.
1211 std::stringstream ss;
1212 ss << " (type:";
1213 ss << type;
1214 ss << " vectsize:";
1215 ss << vect_size;
1216 ss << ")";
1217
1218 decoded_mir->append(ss.str());
1219}
1220
1221void MIRGraph::DisassembleExtendedInstr(const MIR* mir, std::string* decoded_mir) {
1222 DCHECK(decoded_mir != nullptr);
1223 int opcode = mir->dalvikInsn.opcode;
1224 SSARepresentation* ssa_rep = mir->ssa_rep;
1225 int defs = (ssa_rep != nullptr) ? ssa_rep->num_defs : 0;
1226 int uses = (ssa_rep != nullptr) ? ssa_rep->num_uses : 0;
1227
1228 decoded_mir->append(extended_mir_op_names_[opcode - kMirOpFirst]);
1229
1230 switch (opcode) {
1231 case kMirOpPhi: {
1232 if (defs > 0 && uses > 0) {
1233 BasicBlockId* incoming = mir->meta.phi_incoming;
1234 decoded_mir->append(StringPrintf(" %s = (%s",
1235 GetSSANameWithConst(ssa_rep->defs[0], true).c_str(),
1236 GetSSANameWithConst(ssa_rep->uses[0], true).c_str()));
1237 decoded_mir->append(StringPrintf(":%d", incoming[0]));
1238 for (int i = 1; i < uses; i++) {
1239 decoded_mir->append(StringPrintf(", %s:%d", GetSSANameWithConst(ssa_rep->uses[i], true).c_str(), incoming[i]));
1240 }
1241 decoded_mir->append(")");
1242 }
1243 break;
1244 }
1245 case kMirOpCopy:
1246 if (ssa_rep != nullptr) {
1247 decoded_mir->append(" ");
1248 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
1249 if (defs > 1) {
1250 decoded_mir->append(", ");
1251 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
1252 }
1253 decoded_mir->append(" = ");
1254 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[0], false));
1255 if (uses > 1) {
1256 decoded_mir->append(", ");
1257 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
1258 }
1259 } else {
1260 decoded_mir->append(StringPrintf(" v%d = v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1261 }
1262 break;
1263 case kMirOpFusedCmplFloat:
1264 case kMirOpFusedCmpgFloat:
1265 case kMirOpFusedCmplDouble:
1266 case kMirOpFusedCmpgDouble:
1267 case kMirOpFusedCmpLong:
1268 if (ssa_rep != nullptr) {
1269 decoded_mir->append(" ");
1270 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[0], false));
1271 for (int i = 1; i < uses; i++) {
1272 decoded_mir->append(", ");
1273 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[i], false));
1274 }
1275 } else {
1276 decoded_mir->append(StringPrintf(" v%d, v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1277 }
1278 break;
1279 case kMirOpMoveVector:
1280 decoded_mir->append(StringPrintf(" vect%d = vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1281 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1282 break;
1283 case kMirOpPackedAddition:
1284 decoded_mir->append(StringPrintf(" vect%d = vect%d + vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1285 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1286 break;
1287 case kMirOpPackedMultiply:
1288 decoded_mir->append(StringPrintf(" vect%d = vect%d * vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1289 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1290 break;
1291 case kMirOpPackedSubtract:
1292 decoded_mir->append(StringPrintf(" vect%d = vect%d - vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1293 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1294 break;
1295 case kMirOpPackedAnd:
1296 decoded_mir->append(StringPrintf(" vect%d = vect%d & vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1297 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1298 break;
1299 case kMirOpPackedOr:
1300 decoded_mir->append(StringPrintf(" vect%d = vect%d \\| vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1301 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1302 break;
1303 case kMirOpPackedXor:
1304 decoded_mir->append(StringPrintf(" vect%d = vect%d ^ vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1305 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1306 break;
1307 case kMirOpPackedShiftLeft:
1308 decoded_mir->append(StringPrintf(" vect%d = vect%d \\<\\< %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1309 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1310 break;
1311 case kMirOpPackedUnsignedShiftRight:
1312 decoded_mir->append(StringPrintf(" vect%d = vect%d \\>\\>\\> %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1313 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1314 break;
1315 case kMirOpPackedSignedShiftRight:
1316 decoded_mir->append(StringPrintf(" vect%d = vect%d \\>\\> %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1317 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1318 break;
1319 case kMirOpConstVector:
1320 decoded_mir->append(StringPrintf(" vect%d = %x, %x, %x, %x", mir->dalvikInsn.vA, mir->dalvikInsn.arg[0],
1321 mir->dalvikInsn.arg[1], mir->dalvikInsn.arg[2], mir->dalvikInsn.arg[3]));
1322 break;
1323 case kMirOpPackedSet:
1324 if (ssa_rep != nullptr) {
1325 decoded_mir->append(StringPrintf(" vect%d = %s", mir->dalvikInsn.vA,
1326 GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
1327 if (uses > 1) {
1328 decoded_mir->append(", ");
1329 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
1330 }
1331 } else {
1332 decoded_mir->append(StringPrintf(" vect%d = v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1333 }
1334 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1335 break;
1336 case kMirOpPackedAddReduce:
1337 if (ssa_rep != nullptr) {
1338 decoded_mir->append(" ");
1339 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
1340 if (defs > 1) {
1341 decoded_mir->append(", ");
1342 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
1343 }
1344 decoded_mir->append(StringPrintf(" = vect%d + %s", mir->dalvikInsn.vB,
1345 GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
1346 if (uses > 1) {
1347 decoded_mir->append(", ");
1348 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
1349 }
1350 } else {
1351 decoded_mir->append(StringPrintf("v%d = vect%d + v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB, mir->dalvikInsn.vA));
1352 }
1353 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1354 break;
1355 case kMirOpPackedReduce:
1356 if (ssa_rep != nullptr) {
1357 decoded_mir->append(" ");
1358 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
1359 if (defs > 1) {
1360 decoded_mir->append(", ");
1361 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
1362 }
1363 decoded_mir->append(StringPrintf(" = vect%d", mir->dalvikInsn.vB));
1364 } else {
1365 decoded_mir->append(StringPrintf(" v%d = vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1366 }
1367 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1368 break;
1369 case kMirOpReserveVectorRegisters:
1370 case kMirOpReturnVectorRegisters:
1371 decoded_mir->append(StringPrintf(" vect%d - vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1372 break;
1373 case kMirOpMemBarrier: {
1374 decoded_mir->append(" type:");
1375 std::stringstream ss;
1376 ss << static_cast<MemBarrierKind>(mir->dalvikInsn.vA);
1377 decoded_mir->append(ss.str());
1378 break;
1379 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001380 case kMirOpPackedArrayGet:
1381 case kMirOpPackedArrayPut:
1382 decoded_mir->append(StringPrintf(" vect%d", mir->dalvikInsn.vA));
1383 if (ssa_rep != nullptr) {
1384 decoded_mir->append(StringPrintf(", %s[%s]",
1385 GetSSANameWithConst(ssa_rep->uses[0], false).c_str(),
1386 GetSSANameWithConst(ssa_rep->uses[1], false).c_str()));
1387 } else {
1388 decoded_mir->append(StringPrintf(", v%d[v%d]", mir->dalvikInsn.vB, mir->dalvikInsn.vC));
1389 }
1390 FillTypeSizeString(mir->dalvikInsn.arg[0], decoded_mir);
1391 break;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001392 default:
1393 break;
1394 }
1395}
1396
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001397char* MIRGraph::GetDalvikDisassembly(const MIR* mir) {
Ian Rogers29a26482014-05-02 15:27:29 -07001398 MIR::DecodedInstruction insn = mir->dalvikInsn;
buzbee1fd33462013-03-25 13:40:45 -07001399 std::string str;
1400 int flags = 0;
1401 int opcode = insn.opcode;
1402 char* ret;
1403 bool nop = false;
1404 SSARepresentation* ssa_rep = mir->ssa_rep;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001405 Instruction::Format dalvik_format = Instruction::k10x; // Default to no-operand format.
buzbee1fd33462013-03-25 13:40:45 -07001406
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001407 // Handle special cases that recover the original dalvik instruction.
buzbee1fd33462013-03-25 13:40:45 -07001408 if ((opcode == kMirOpCheck) || (opcode == kMirOpCheckPart2)) {
1409 str.append(extended_mir_op_names_[opcode - kMirOpFirst]);
1410 str.append(": ");
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001411 // Recover the original Dex instruction.
buzbee1fd33462013-03-25 13:40:45 -07001412 insn = mir->meta.throw_insn->dalvikInsn;
1413 ssa_rep = mir->meta.throw_insn->ssa_rep;
buzbee1fd33462013-03-25 13:40:45 -07001414 opcode = insn.opcode;
1415 } else if (opcode == kMirOpNop) {
1416 str.append("[");
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001417 if (mir->offset < current_code_item_->insns_size_in_code_units_) {
1418 // Recover original opcode.
1419 insn.opcode = Instruction::At(current_code_item_->insns_ + mir->offset)->Opcode();
1420 opcode = insn.opcode;
1421 }
buzbee1fd33462013-03-25 13:40:45 -07001422 nop = true;
1423 }
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001424 int defs = (ssa_rep != NULL) ? ssa_rep->num_defs : 0;
1425 int uses = (ssa_rep != NULL) ? ssa_rep->num_uses : 0;
buzbee1fd33462013-03-25 13:40:45 -07001426
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -07001427 if (MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001428 // Note that this does not check the MIR's opcode in all cases. In cases where it
1429 // recovered dalvik instruction, it uses opcode of that instead of the extended one.
1430 DisassembleExtendedInstr(mir, &str);
buzbee1fd33462013-03-25 13:40:45 -07001431 } else {
1432 dalvik_format = Instruction::FormatOf(insn.opcode);
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07001433 flags = insn.FlagsOf();
buzbee1fd33462013-03-25 13:40:45 -07001434 str.append(Instruction::Name(insn.opcode));
buzbee1fd33462013-03-25 13:40:45 -07001435
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001436 // For invokes-style formats, treat wide regs as a pair of singles.
buzbee1fd33462013-03-25 13:40:45 -07001437 bool show_singles = ((dalvik_format == Instruction::k35c) ||
1438 (dalvik_format == Instruction::k3rc));
1439 if (defs != 0) {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001440 str.append(" ");
1441 str.append(GetSSANameWithConst(ssa_rep->defs[0], false));
1442 if (defs > 1) {
1443 str.append(", ");
1444 str.append(GetSSANameWithConst(ssa_rep->defs[1], false));
1445 }
buzbee1fd33462013-03-25 13:40:45 -07001446 if (uses != 0) {
1447 str.append(", ");
1448 }
1449 }
1450 for (int i = 0; i < uses; i++) {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001451 str.append(" ");
1452 str.append(GetSSANameWithConst(ssa_rep->uses[i], show_singles));
buzbee1fd33462013-03-25 13:40:45 -07001453 if (!show_singles && (reg_location_ != NULL) && reg_location_[i].wide) {
1454 // For the listing, skip the high sreg.
1455 i++;
1456 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001457 if (i != (uses - 1)) {
buzbee1fd33462013-03-25 13:40:45 -07001458 str.append(",");
1459 }
1460 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001461
buzbee1fd33462013-03-25 13:40:45 -07001462 switch (dalvik_format) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001463 case Instruction::k11n: // Add one immediate from vB.
buzbee1fd33462013-03-25 13:40:45 -07001464 case Instruction::k21s:
1465 case Instruction::k31i:
1466 case Instruction::k21h:
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001467 str.append(StringPrintf(", #0x%x", insn.vB));
buzbee1fd33462013-03-25 13:40:45 -07001468 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001469 case Instruction::k51l: // Add one wide immediate.
Ian Rogers23b03b52014-01-24 11:10:03 -08001470 str.append(StringPrintf(", #%" PRId64, insn.vB_wide));
buzbee1fd33462013-03-25 13:40:45 -07001471 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001472 case Instruction::k21c: // One register, one string/type/method index.
buzbee1fd33462013-03-25 13:40:45 -07001473 case Instruction::k31c:
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001474 str.append(StringPrintf(", index #0x%x", insn.vB));
buzbee1fd33462013-03-25 13:40:45 -07001475 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001476 case Instruction::k22c: // Two registers, one string/type/method index.
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001477 str.append(StringPrintf(", index #0x%x", insn.vC));
buzbee1fd33462013-03-25 13:40:45 -07001478 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001479 case Instruction::k22s: // Add one immediate from vC.
buzbee1fd33462013-03-25 13:40:45 -07001480 case Instruction::k22b:
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001481 str.append(StringPrintf(", #0x%x", insn.vC));
buzbee1fd33462013-03-25 13:40:45 -07001482 break;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001483 default:
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001484 // Nothing left to print.
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001485 break;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001486 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001487
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001488 if ((flags & Instruction::kBranch) != 0) {
1489 // For branches, decode the instructions to print out the branch targets.
1490 int offset = 0;
1491 switch (dalvik_format) {
1492 case Instruction::k21t:
1493 offset = insn.vB;
1494 break;
1495 case Instruction::k22t:
1496 offset = insn.vC;
1497 break;
1498 case Instruction::k10t:
1499 case Instruction::k20t:
1500 case Instruction::k30t:
1501 offset = insn.vA;
1502 break;
1503 default:
1504 LOG(FATAL) << "Unexpected branch format " << dalvik_format << " from " << insn.opcode;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001505 break;
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001506 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001507 str.append(StringPrintf(", 0x%x (%c%x)", mir->offset + offset,
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001508 offset > 0 ? '+' : '-', offset > 0 ? offset : -offset));
1509 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001510
1511 if (nop) {
1512 str.append("]--optimized away");
1513 }
buzbee1fd33462013-03-25 13:40:45 -07001514 }
1515 int length = str.length() + 1;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001516 ret = static_cast<char*>(arena_->Alloc(length, kArenaAllocDFInfo));
buzbee1fd33462013-03-25 13:40:45 -07001517 strncpy(ret, str.c_str(), length);
1518 return ret;
1519}
1520
1521/* Turn method name into a legal Linux file name */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001522void MIRGraph::ReplaceSpecialChars(std::string& str) {
Brian Carlstrom9b7085a2013-07-18 15:15:21 -07001523 static const struct { const char before; const char after; } match[] = {
1524 {'/', '-'}, {';', '#'}, {' ', '#'}, {'$', '+'},
1525 {'(', '@'}, {')', '@'}, {'<', '='}, {'>', '='}
1526 };
buzbee1fd33462013-03-25 13:40:45 -07001527 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
1528 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
1529 }
1530}
1531
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001532std::string MIRGraph::GetSSAName(int ssa_reg) {
Ian Rogers39ebcb82013-05-30 16:57:23 -07001533 // TODO: This value is needed for LLVM and debugging. Currently, we compute this and then copy to
1534 // the arena. We should be smarter and just place straight into the arena, or compute the
1535 // value more lazily.
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001536 int vreg = SRegToVReg(ssa_reg);
1537 if (vreg >= static_cast<int>(GetFirstTempVR())) {
1538 return StringPrintf("t%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1539 } else {
1540 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1541 }
buzbee1fd33462013-03-25 13:40:45 -07001542}
1543
1544// Similar to GetSSAName, but if ssa name represents an immediate show that as well.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001545std::string MIRGraph::GetSSANameWithConst(int ssa_reg, bool singles_only) {
buzbee1fd33462013-03-25 13:40:45 -07001546 if (reg_location_ == NULL) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001547 // Pre-SSA - just use the standard name.
buzbee1fd33462013-03-25 13:40:45 -07001548 return GetSSAName(ssa_reg);
1549 }
1550 if (IsConst(reg_location_[ssa_reg])) {
Mark Mendell9944b3b2014-10-06 10:58:54 -04001551 if (!singles_only && reg_location_[ssa_reg].wide &&
1552 !reg_location_[ssa_reg].high_word) {
Ian Rogers23b03b52014-01-24 11:10:03 -08001553 return StringPrintf("v%d_%d#0x%" PRIx64, SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001554 ConstantValueWide(reg_location_[ssa_reg]));
1555 } else {
Brian Carlstromb1eba212013-07-17 18:07:19 -07001556 return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001557 ConstantValue(reg_location_[ssa_reg]));
1558 }
1559 } else {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001560 int vreg = SRegToVReg(ssa_reg);
1561 if (vreg >= static_cast<int>(GetFirstTempVR())) {
1562 return StringPrintf("t%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1563 } else {
1564 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1565 }
buzbee1fd33462013-03-25 13:40:45 -07001566 }
1567}
1568
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001569void MIRGraph::GetBlockName(BasicBlock* bb, char* name) {
buzbee1fd33462013-03-25 13:40:45 -07001570 switch (bb->block_type) {
1571 case kEntryBlock:
1572 snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id);
1573 break;
1574 case kExitBlock:
1575 snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id);
1576 break;
1577 case kDalvikByteCode:
1578 snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id);
1579 break;
1580 case kExceptionHandling:
1581 snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset,
1582 bb->id);
1583 break;
1584 default:
1585 snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id);
1586 break;
1587 }
1588}
1589
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001590const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) {
buzbee0d829482013-10-11 15:24:55 -07001591 // TODO: for inlining support, use current code unit.
buzbee1fd33462013-03-25 13:40:45 -07001592 const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx);
1593 return cu_->dex_file->GetShorty(method_id.proto_idx_);
1594}
1595
1596/* Debug Utility - dump a compilation unit */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001597void MIRGraph::DumpMIRGraph() {
buzbee1fd33462013-03-25 13:40:45 -07001598 const char* block_type_names[] = {
buzbee17189ac2013-11-08 11:07:02 -08001599 "Null Block",
buzbee1fd33462013-03-25 13:40:45 -07001600 "Entry Block",
1601 "Code Block",
1602 "Exit Block",
1603 "Exception Handling",
1604 "Catch Block"
1605 };
1606
1607 LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001608 LOG(INFO) << GetInsns(0) << " insns";
buzbee1fd33462013-03-25 13:40:45 -07001609 LOG(INFO) << GetNumBlocks() << " blocks in total";
buzbee1fd33462013-03-25 13:40:45 -07001610
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001611 for (BasicBlock* bb : block_list_) {
buzbee1fd33462013-03-25 13:40:45 -07001612 LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)",
1613 bb->id,
1614 block_type_names[bb->block_type],
1615 bb->start_offset,
1616 bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset,
1617 bb->last_mir_insn ? "" : " empty");
buzbee0d829482013-10-11 15:24:55 -07001618 if (bb->taken != NullBasicBlockId) {
1619 LOG(INFO) << " Taken branch: block " << bb->taken
1620 << "(0x" << std::hex << GetBasicBlock(bb->taken)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001621 }
buzbee0d829482013-10-11 15:24:55 -07001622 if (bb->fall_through != NullBasicBlockId) {
1623 LOG(INFO) << " Fallthrough : block " << bb->fall_through
1624 << " (0x" << std::hex << GetBasicBlock(bb->fall_through)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001625 }
1626 }
1627}
1628
1629/*
1630 * Build an array of location records for the incoming arguments.
1631 * Note: one location record per word of arguments, with dummy
1632 * high-word loc for wide arguments. Also pull up any following
1633 * MOVE_RESULT and incorporate it into the invoke.
1634 */
1635CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001636 bool is_range) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001637 CallInfo* info = static_cast<CallInfo*>(arena_->Alloc(sizeof(CallInfo),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001638 kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001639 MIR* move_result_mir = FindMoveResult(bb, mir);
1640 if (move_result_mir == NULL) {
1641 info->result.location = kLocInvalid;
1642 } else {
1643 info->result = GetRawDest(move_result_mir);
buzbee1fd33462013-03-25 13:40:45 -07001644 move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1645 }
1646 info->num_arg_words = mir->ssa_rep->num_uses;
1647 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001648 (arena_->Alloc(sizeof(RegLocation) * info->num_arg_words, kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001649 for (int i = 0; i < info->num_arg_words; i++) {
1650 info->args[i] = GetRawSrc(mir, i);
1651 }
1652 info->opt_flags = mir->optimization_flags;
1653 info->type = type;
1654 info->is_range = is_range;
1655 info->index = mir->dalvikInsn.vB;
1656 info->offset = mir->offset;
Vladimir Markof096aad2014-01-23 15:51:58 +00001657 info->mir = mir;
buzbee1fd33462013-03-25 13:40:45 -07001658 return info;
1659}
1660
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001661// Allocate a new MIR.
1662MIR* MIRGraph::NewMIR() {
1663 MIR* mir = new (arena_) MIR();
1664 return mir;
1665}
1666
buzbee862a7602013-04-05 10:58:54 -07001667// Allocate a new basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001668BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001669 BasicBlock* bb = new (arena_) BasicBlock(block_id, block_type, arena_);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001670
buzbee862a7602013-04-05 10:58:54 -07001671 // TUNING: better estimate of the exit block predecessors?
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001672 bb->predecessors.reserve((block_type == kExitBlock) ? 2048 : 2);
buzbee862a7602013-04-05 10:58:54 -07001673 block_id_map_.Put(block_id, block_id);
1674 return bb;
1675}
buzbee1fd33462013-03-25 13:40:45 -07001676
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001677void MIRGraph::InitializeConstantPropagation() {
1678 is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001679 constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(), kArenaAllocDFInfo));
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001680}
1681
1682void MIRGraph::InitializeMethodUses() {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001683 // The gate starts by initializing the use counts.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001684 int num_ssa_regs = GetNumSSARegs();
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001685 use_counts_.clear();
1686 use_counts_.reserve(num_ssa_regs + 32);
1687 use_counts_.resize(num_ssa_regs, 0u);
1688 raw_use_counts_.clear();
1689 raw_use_counts_.reserve(num_ssa_regs + 32);
1690 raw_use_counts_.resize(num_ssa_regs, 0u);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001691}
1692
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001693void MIRGraph::SSATransformationStart() {
1694 DCHECK(temp_scoped_alloc_.get() == nullptr);
1695 temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack));
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001696 temp_bit_vector_size_ = GetNumOfCodeAndTempVRs();
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001697 temp_bit_vector_ = new (temp_scoped_alloc_.get()) ArenaBitVector(
1698 temp_scoped_alloc_.get(), temp_bit_vector_size_, false, kBitMapRegisterV);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001699}
1700
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001701void MIRGraph::SSATransformationEnd() {
1702 // Verify the dataflow information after the pass.
1703 if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
1704 VerifyDataflow();
1705 }
1706
1707 temp_bit_vector_size_ = 0u;
1708 temp_bit_vector_ = nullptr;
Vladimir Marko5229cf12014-10-09 14:57:59 +01001709 temp_bit_matrix_ = nullptr; // Def block matrix.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001710 DCHECK(temp_scoped_alloc_.get() != nullptr);
1711 temp_scoped_alloc_.reset();
Johnny Qiuc029c982014-06-04 07:23:49 +00001712
1713 // Update the maximum number of reachable blocks.
1714 max_num_reachable_blocks_ = num_reachable_blocks_;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001715}
1716
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001717size_t MIRGraph::GetNumDalvikInsns() const {
1718 size_t cumulative_size = 0u;
1719 bool counted_current_item = false;
1720 const uint8_t size_for_null_code_item = 2u;
1721
1722 for (auto it : m_units_) {
1723 const DexFile::CodeItem* code_item = it->GetCodeItem();
1724 // Even if the code item is null, we still count non-zero value so that
1725 // each m_unit is counted as having impact.
1726 cumulative_size += (code_item == nullptr ?
1727 size_for_null_code_item : code_item->insns_size_in_code_units_);
1728 if (code_item == current_code_item_) {
1729 counted_current_item = true;
1730 }
1731 }
1732
1733 // If the current code item was not counted yet, count it now.
1734 // This can happen for example in unit tests where some fields like m_units_
1735 // are not initialized.
1736 if (counted_current_item == false) {
1737 cumulative_size += (current_code_item_ == nullptr ?
1738 size_for_null_code_item : current_code_item_->insns_size_in_code_units_);
1739 }
1740
1741 return cumulative_size;
1742}
1743
Vladimir Marko55fff042014-07-10 12:42:52 +01001744static BasicBlock* SelectTopologicalSortOrderFallBack(
1745 MIRGraph* mir_graph, const ArenaBitVector* current_loop,
1746 const ScopedArenaVector<size_t>* visited_cnt_values, ScopedArenaAllocator* allocator,
1747 ScopedArenaVector<BasicBlockId>* tmp_stack) {
1748 // No true loop head has been found but there may be true loop heads after the mess we need
1749 // to resolve. To avoid taking one of those, pick the candidate with the highest number of
1750 // reachable unvisited nodes. That candidate will surely be a part of a loop.
1751 BasicBlock* fall_back = nullptr;
1752 size_t fall_back_num_reachable = 0u;
1753 // Reuse the same bit vector for each candidate to mark reachable unvisited blocks.
1754 ArenaBitVector candidate_reachable(allocator, mir_graph->GetNumBlocks(), false, kBitMapMisc);
1755 AllNodesIterator iter(mir_graph);
1756 for (BasicBlock* candidate = iter.Next(); candidate != nullptr; candidate = iter.Next()) {
1757 if (candidate->hidden || // Hidden, or
1758 candidate->visited || // already processed, or
1759 (*visited_cnt_values)[candidate->id] == 0u || // no processed predecessors, or
1760 (current_loop != nullptr && // outside current loop.
1761 !current_loop->IsBitSet(candidate->id))) {
1762 continue;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001763 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001764 DCHECK(tmp_stack->empty());
1765 tmp_stack->push_back(candidate->id);
1766 candidate_reachable.ClearAllBits();
1767 size_t num_reachable = 0u;
1768 while (!tmp_stack->empty()) {
1769 BasicBlockId current_id = tmp_stack->back();
1770 tmp_stack->pop_back();
1771 BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
1772 DCHECK(current_bb != nullptr);
1773 ChildBlockIterator child_iter(current_bb, mir_graph);
1774 BasicBlock* child_bb = child_iter.Next();
1775 for ( ; child_bb != nullptr; child_bb = child_iter.Next()) {
1776 DCHECK(!child_bb->hidden);
1777 if (child_bb->visited || // Already processed, or
1778 (current_loop != nullptr && // outside current loop.
1779 !current_loop->IsBitSet(child_bb->id))) {
1780 continue;
1781 }
1782 if (!candidate_reachable.IsBitSet(child_bb->id)) {
1783 candidate_reachable.SetBit(child_bb->id);
1784 tmp_stack->push_back(child_bb->id);
1785 num_reachable += 1u;
1786 }
1787 }
1788 }
1789 if (fall_back_num_reachable < num_reachable) {
1790 fall_back_num_reachable = num_reachable;
1791 fall_back = candidate;
1792 }
1793 }
1794 return fall_back;
1795}
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001796
Vladimir Marko55fff042014-07-10 12:42:52 +01001797// Compute from which unvisited blocks is bb_id reachable through unvisited blocks.
1798static void ComputeUnvisitedReachableFrom(MIRGraph* mir_graph, BasicBlockId bb_id,
1799 ArenaBitVector* reachable,
1800 ScopedArenaVector<BasicBlockId>* tmp_stack) {
1801 // NOTE: Loop heads indicated by the "visited" flag.
1802 DCHECK(tmp_stack->empty());
1803 reachable->ClearAllBits();
1804 tmp_stack->push_back(bb_id);
1805 while (!tmp_stack->empty()) {
1806 BasicBlockId current_id = tmp_stack->back();
1807 tmp_stack->pop_back();
1808 BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
1809 DCHECK(current_bb != nullptr);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001810 for (BasicBlockId pred_id : current_bb->predecessors) {
1811 BasicBlock* pred_bb = mir_graph->GetBasicBlock(pred_id);
1812 DCHECK(pred_bb != nullptr);
Vladimir Marko55fff042014-07-10 12:42:52 +01001813 if (!pred_bb->visited && !reachable->IsBitSet(pred_bb->id)) {
1814 reachable->SetBit(pred_bb->id);
1815 tmp_stack->push_back(pred_bb->id);
1816 }
1817 }
1818 }
1819}
1820
1821void MIRGraph::ComputeTopologicalSortOrder() {
1822 ScopedArenaAllocator allocator(&cu_->arena_stack);
1823 unsigned int num_blocks = GetNumBlocks();
1824
1825 ScopedArenaQueue<BasicBlock*> q(allocator.Adapter());
1826 ScopedArenaVector<size_t> visited_cnt_values(num_blocks, 0u, allocator.Adapter());
1827 ScopedArenaVector<BasicBlockId> loop_head_stack(allocator.Adapter());
1828 size_t max_nested_loops = 0u;
1829 ArenaBitVector loop_exit_blocks(&allocator, num_blocks, false, kBitMapMisc);
1830 loop_exit_blocks.ClearAllBits();
1831
1832 // Count the number of blocks to process and add the entry block(s).
Vladimir Marko55fff042014-07-10 12:42:52 +01001833 unsigned int num_blocks_to_process = 0u;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001834 for (BasicBlock* bb : block_list_) {
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001835 if (bb->hidden == true) {
1836 continue;
1837 }
1838
Vladimir Marko55fff042014-07-10 12:42:52 +01001839 num_blocks_to_process += 1u;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001840
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001841 if (bb->predecessors.size() == 0u) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001842 // Add entry block to the queue.
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001843 q.push(bb);
1844 }
1845 }
1846
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001847 // Clear the topological order arrays.
1848 topological_order_.clear();
1849 topological_order_.reserve(num_blocks);
1850 topological_order_loop_ends_.clear();
1851 topological_order_loop_ends_.resize(num_blocks, 0u);
1852 topological_order_indexes_.clear();
1853 topological_order_indexes_.resize(num_blocks, static_cast<uint16_t>(-1));
Vladimir Marko55fff042014-07-10 12:42:52 +01001854
1855 // Mark all blocks as unvisited.
1856 ClearAllVisitedFlags();
1857
1858 // For loop heads, keep track from which blocks they are reachable not going through other
1859 // loop heads. Other loop heads are excluded to detect the heads of nested loops. The children
1860 // in this set go into the loop body, the other children are jumping over the loop.
1861 ScopedArenaVector<ArenaBitVector*> loop_head_reachable_from(allocator.Adapter());
1862 loop_head_reachable_from.resize(num_blocks, nullptr);
1863 // Reuse the same temp stack whenever calculating a loop_head_reachable_from[loop_head_id].
1864 ScopedArenaVector<BasicBlockId> tmp_stack(allocator.Adapter());
1865
1866 while (num_blocks_to_process != 0u) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001867 BasicBlock* bb = nullptr;
1868 if (!q.empty()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001869 num_blocks_to_process -= 1u;
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001870 // Get top.
1871 bb = q.front();
1872 q.pop();
Vladimir Marko55fff042014-07-10 12:42:52 +01001873 if (bb->visited) {
1874 // Loop head: it was already processed, mark end and copy exit blocks to the queue.
1875 DCHECK(q.empty()) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001876 uint16_t idx = static_cast<uint16_t>(topological_order_.size());
1877 topological_order_loop_ends_[topological_order_indexes_[bb->id]] = idx;
Vladimir Marko55fff042014-07-10 12:42:52 +01001878 DCHECK_EQ(loop_head_stack.back(), bb->id);
1879 loop_head_stack.pop_back();
1880 ArenaBitVector* reachable =
1881 loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
1882 for (BasicBlockId candidate_id : loop_exit_blocks.Indexes()) {
1883 if (reachable == nullptr || reachable->IsBitSet(candidate_id)) {
1884 q.push(GetBasicBlock(candidate_id));
1885 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1886 // so clearing the bit has no effect on the iterator.
1887 loop_exit_blocks.ClearBit(candidate_id);
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001888 }
1889 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001890 continue;
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001891 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001892 } else {
1893 // Find the new loop head.
1894 AllNodesIterator iter(this);
1895 while (true) {
1896 BasicBlock* candidate = iter.Next();
1897 if (candidate == nullptr) {
1898 // We did not find a true loop head, fall back to a reachable block in any loop.
1899 ArenaBitVector* current_loop =
1900 loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
1901 bb = SelectTopologicalSortOrderFallBack(this, current_loop, &visited_cnt_values,
1902 &allocator, &tmp_stack);
1903 DCHECK(bb != nullptr) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
1904 if (kIsDebugBuild && cu_->dex_file != nullptr) {
1905 LOG(INFO) << "Topological sort order: Using fall-back in "
1906 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " BB #" << bb->id
1907 << " @0x" << std::hex << bb->start_offset
1908 << ", num_blocks = " << std::dec << num_blocks;
1909 }
1910 break;
1911 }
1912 if (candidate->hidden || // Hidden, or
1913 candidate->visited || // already processed, or
1914 visited_cnt_values[candidate->id] == 0u || // no processed predecessors, or
1915 (!loop_head_stack.empty() && // outside current loop.
1916 !loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(candidate->id))) {
1917 continue;
1918 }
1919
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001920 for (BasicBlockId pred_id : candidate->predecessors) {
1921 BasicBlock* pred_bb = GetBasicBlock(pred_id);
1922 DCHECK(pred_bb != nullptr);
Vladimir Marko55fff042014-07-10 12:42:52 +01001923 if (pred_bb != candidate && !pred_bb->visited &&
1924 !pred_bb->dominators->IsBitSet(candidate->id)) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001925 candidate = nullptr; // Set candidate to null to indicate failure.
1926 break;
Vladimir Marko55fff042014-07-10 12:42:52 +01001927 }
1928 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001929 if (candidate != nullptr) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001930 bb = candidate;
1931 break;
1932 }
1933 }
1934 // Compute blocks from which the loop head is reachable and process those blocks first.
1935 ArenaBitVector* reachable =
1936 new (&allocator) ArenaBitVector(&allocator, num_blocks, false, kBitMapMisc);
1937 loop_head_reachable_from[bb->id] = reachable;
1938 ComputeUnvisitedReachableFrom(this, bb->id, reachable, &tmp_stack);
1939 // Now mark as loop head. (Even if it's only a fall back when we don't find a true loop.)
1940 loop_head_stack.push_back(bb->id);
1941 max_nested_loops = std::max(max_nested_loops, loop_head_stack.size());
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001942 }
1943
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001944 DCHECK_EQ(bb->hidden, false);
1945 DCHECK_EQ(bb->visited, false);
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001946 bb->visited = true;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001947
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001948 // Now add the basic block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001949 uint16_t idx = static_cast<uint16_t>(topological_order_.size());
1950 topological_order_indexes_[bb->id] = idx;
1951 topological_order_.push_back(bb->id);
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001952
Vladimir Marko55fff042014-07-10 12:42:52 +01001953 // Update visited_cnt_values for children.
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001954 ChildBlockIterator succIter(bb, this);
1955 BasicBlock* successor = succIter.Next();
1956 for ( ; successor != nullptr; successor = succIter.Next()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001957 if (successor->hidden) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001958 continue;
1959 }
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001960
Vladimir Marko55fff042014-07-10 12:42:52 +01001961 // One more predecessor was visited.
1962 visited_cnt_values[successor->id] += 1u;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001963 if (visited_cnt_values[successor->id] == successor->predecessors.size()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001964 if (loop_head_stack.empty() ||
1965 loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(successor->id)) {
1966 q.push(successor);
1967 } else {
1968 DCHECK(!loop_exit_blocks.IsBitSet(successor->id));
1969 loop_exit_blocks.SetBit(successor->id);
1970 }
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001971 }
1972 }
1973 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001974
1975 // Prepare the loop head stack for iteration.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001976 topological_order_loop_head_stack_.clear();
1977 topological_order_loop_head_stack_.reserve(max_nested_loops);
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001978}
1979
1980bool BasicBlock::IsExceptionBlock() const {
1981 if (block_type == kExceptionHandling) {
1982 return true;
1983 }
1984 return false;
1985}
1986
Wei Jin04f4d8a2014-05-29 18:04:29 -07001987bool MIRGraph::HasSuspendTestBetween(BasicBlock* source, BasicBlockId target_id) {
1988 BasicBlock* target = GetBasicBlock(target_id);
1989
1990 if (source == nullptr || target == nullptr)
1991 return false;
1992
1993 int idx;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001994 for (idx = gen_suspend_test_list_.size() - 1; idx >= 0; idx--) {
1995 BasicBlock* bb = gen_suspend_test_list_[idx];
Wei Jin04f4d8a2014-05-29 18:04:29 -07001996 if (bb == source)
1997 return true; // The block has been inserted by a suspend check before.
1998 if (source->dominators->IsBitSet(bb->id) && bb->dominators->IsBitSet(target_id))
1999 return true;
2000 }
2001
2002 return false;
2003}
2004
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002005ChildBlockIterator::ChildBlockIterator(BasicBlock* bb, MIRGraph* mir_graph)
2006 : basic_block_(bb), mir_graph_(mir_graph), visited_fallthrough_(false),
2007 visited_taken_(false), have_successors_(false) {
2008 // Check if we actually do have successors.
2009 if (basic_block_ != 0 && basic_block_->successor_block_list_type != kNotUsed) {
2010 have_successors_ = true;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002011 successor_iter_ = basic_block_->successor_blocks.cbegin();
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002012 }
2013}
2014
2015BasicBlock* ChildBlockIterator::Next() {
2016 // We check if we have a basic block. If we don't we cannot get next child.
2017 if (basic_block_ == nullptr) {
2018 return nullptr;
2019 }
2020
2021 // If we haven't visited fallthrough, return that.
2022 if (visited_fallthrough_ == false) {
2023 visited_fallthrough_ = true;
2024
2025 BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->fall_through);
2026 if (result != nullptr) {
2027 return result;
2028 }
2029 }
2030
2031 // If we haven't visited taken, return that.
2032 if (visited_taken_ == false) {
2033 visited_taken_ = true;
2034
2035 BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->taken);
2036 if (result != nullptr) {
2037 return result;
2038 }
2039 }
2040
2041 // We visited both taken and fallthrough. Now check if we have successors we need to visit.
2042 if (have_successors_ == true) {
2043 // Get information about next successor block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002044 auto end = basic_block_->successor_blocks.cend();
2045 while (successor_iter_ != end) {
2046 SuccessorBlockInfo* successor_block_info = *successor_iter_;
2047 ++successor_iter_;
Niranjan Kumar989367a2014-06-12 12:15:48 -07002048 // If block was replaced by zero block, take next one.
2049 if (successor_block_info->block != NullBasicBlockId) {
2050 return mir_graph_->GetBasicBlock(successor_block_info->block);
2051 }
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002052 }
2053 }
2054
2055 // We do not have anything.
2056 return nullptr;
2057}
2058
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002059BasicBlock* BasicBlock::Copy(CompilationUnit* c_unit) {
2060 MIRGraph* mir_graph = c_unit->mir_graph.get();
2061 return Copy(mir_graph);
2062}
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002063
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002064BasicBlock* BasicBlock::Copy(MIRGraph* mir_graph) {
2065 BasicBlock* result_bb = mir_graph->CreateNewBB(block_type);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002066
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002067 // We don't do a memcpy style copy here because it would lead to a lot of things
2068 // to clean up. Let us do it by hand instead.
2069 // Copy in taken and fallthrough.
2070 result_bb->fall_through = fall_through;
2071 result_bb->taken = taken;
2072
2073 // Copy successor links if needed.
2074 ArenaAllocator* arena = mir_graph->GetArena();
2075
2076 result_bb->successor_block_list_type = successor_block_list_type;
2077 if (result_bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002078 result_bb->successor_blocks.reserve(successor_blocks.size());
2079 for (SuccessorBlockInfo* sbi_old : successor_blocks) {
2080 SuccessorBlockInfo* sbi_new = static_cast<SuccessorBlockInfo*>(
2081 arena->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002082 memcpy(sbi_new, sbi_old, sizeof(SuccessorBlockInfo));
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002083 result_bb->successor_blocks.push_back(sbi_new);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002084 }
2085 }
2086
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002087 // Copy offset, method.
2088 result_bb->start_offset = start_offset;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002089
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002090 // Now copy instructions.
2091 for (MIR* mir = first_mir_insn; mir != 0; mir = mir->next) {
2092 // Get a copy first.
2093 MIR* copy = mir->Copy(mir_graph);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002094
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002095 // Append it.
2096 result_bb->AppendMIR(copy);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002097 }
2098
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002099 return result_bb;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002100}
2101
2102MIR* MIR::Copy(MIRGraph* mir_graph) {
2103 MIR* res = mir_graph->NewMIR();
2104 *res = *this;
2105
2106 // Remove links
2107 res->next = nullptr;
2108 res->bb = NullBasicBlockId;
2109 res->ssa_rep = nullptr;
2110
2111 return res;
2112}
2113
2114MIR* MIR::Copy(CompilationUnit* c_unit) {
2115 return Copy(c_unit->mir_graph.get());
2116}
2117
2118uint32_t SSARepresentation::GetStartUseIndex(Instruction::Code opcode) {
2119 // Default result.
2120 int res = 0;
2121
2122 // We are basically setting the iputs to their igets counterparts.
2123 switch (opcode) {
2124 case Instruction::IPUT:
2125 case Instruction::IPUT_OBJECT:
2126 case Instruction::IPUT_BOOLEAN:
2127 case Instruction::IPUT_BYTE:
2128 case Instruction::IPUT_CHAR:
2129 case Instruction::IPUT_SHORT:
2130 case Instruction::IPUT_QUICK:
2131 case Instruction::IPUT_OBJECT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -07002132 case Instruction::IPUT_BOOLEAN_QUICK:
2133 case Instruction::IPUT_BYTE_QUICK:
2134 case Instruction::IPUT_CHAR_QUICK:
2135 case Instruction::IPUT_SHORT_QUICK:
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002136 case Instruction::APUT:
2137 case Instruction::APUT_OBJECT:
2138 case Instruction::APUT_BOOLEAN:
2139 case Instruction::APUT_BYTE:
2140 case Instruction::APUT_CHAR:
2141 case Instruction::APUT_SHORT:
2142 case Instruction::SPUT:
2143 case Instruction::SPUT_OBJECT:
2144 case Instruction::SPUT_BOOLEAN:
2145 case Instruction::SPUT_BYTE:
2146 case Instruction::SPUT_CHAR:
2147 case Instruction::SPUT_SHORT:
2148 // Skip the VR containing what to store.
2149 res = 1;
2150 break;
2151 case Instruction::IPUT_WIDE:
2152 case Instruction::IPUT_WIDE_QUICK:
2153 case Instruction::APUT_WIDE:
2154 case Instruction::SPUT_WIDE:
2155 // Skip the two VRs containing what to store.
2156 res = 2;
2157 break;
2158 default:
2159 // Do nothing in the general case.
2160 break;
2161 }
2162
2163 return res;
2164}
2165
Jean Christophe Beylerc3db20b2014-05-05 21:09:40 -07002166/**
2167 * @brief Given a decoded instruction, it checks whether the instruction
2168 * sets a constant and if it does, more information is provided about the
2169 * constant being set.
2170 * @param ptr_value pointer to a 64-bit holder for the constant.
2171 * @param wide Updated by function whether a wide constant is being set by bytecode.
2172 * @return Returns false if the decoded instruction does not represent a constant bytecode.
2173 */
2174bool MIR::DecodedInstruction::GetConstant(int64_t* ptr_value, bool* wide) const {
2175 bool sets_const = true;
2176 int64_t value = vB;
2177
2178 DCHECK(ptr_value != nullptr);
2179 DCHECK(wide != nullptr);
2180
2181 switch (opcode) {
2182 case Instruction::CONST_4:
2183 case Instruction::CONST_16:
2184 case Instruction::CONST:
2185 *wide = false;
2186 value <<= 32; // In order to get the sign extend.
2187 value >>= 32;
2188 break;
2189 case Instruction::CONST_HIGH16:
2190 *wide = false;
2191 value <<= 48; // In order to get the sign extend.
2192 value >>= 32;
2193 break;
2194 case Instruction::CONST_WIDE_16:
2195 case Instruction::CONST_WIDE_32:
2196 *wide = true;
2197 value <<= 32; // In order to get the sign extend.
2198 value >>= 32;
2199 break;
2200 case Instruction::CONST_WIDE:
2201 *wide = true;
2202 value = vB_wide;
2203 break;
2204 case Instruction::CONST_WIDE_HIGH16:
2205 *wide = true;
2206 value <<= 48; // In order to get the sign extend.
2207 break;
2208 default:
2209 sets_const = false;
2210 break;
2211 }
2212
2213 if (sets_const) {
2214 *ptr_value = value;
2215 }
2216
2217 return sets_const;
2218}
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002219
2220void BasicBlock::ResetOptimizationFlags(uint16_t reset_flags) {
2221 // Reset flags for all MIRs in bb.
2222 for (MIR* mir = first_mir_insn; mir != NULL; mir = mir->next) {
2223 mir->optimization_flags &= (~reset_flags);
2224 }
2225}
2226
2227void BasicBlock::Hide(CompilationUnit* c_unit) {
2228 // First lets make it a dalvik bytecode block so it doesn't have any special meaning.
2229 block_type = kDalvikByteCode;
2230
2231 // Mark it as hidden.
2232 hidden = true;
2233
2234 // Detach it from its MIRs so we don't generate code for them. Also detached MIRs
2235 // are updated to know that they no longer have a parent.
2236 for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
2237 mir->bb = NullBasicBlockId;
2238 }
2239 first_mir_insn = nullptr;
2240 last_mir_insn = nullptr;
2241
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002242 MIRGraph* mir_graph = c_unit->mir_graph.get();
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002243 for (BasicBlockId pred_id : predecessors) {
2244 BasicBlock* pred_bb = mir_graph->GetBasicBlock(pred_id);
2245 DCHECK(pred_bb != nullptr);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002246
2247 // Sadly we have to go through the children by hand here.
2248 pred_bb->ReplaceChild(id, NullBasicBlockId);
2249 }
2250
2251 // Iterate through children of bb we are hiding.
2252 ChildBlockIterator successorChildIter(this, mir_graph);
2253
2254 for (BasicBlock* childPtr = successorChildIter.Next(); childPtr != 0; childPtr = successorChildIter.Next()) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002255 // Erase this predecessor from child.
2256 childPtr->ErasePredecessor(id);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002257 }
Jean Christophe Beylerf588b502014-06-18 14:14:15 -07002258
2259 // Remove link to children.
2260 taken = NullBasicBlockId;
2261 fall_through = NullBasicBlockId;
2262 successor_block_list_type = kNotUsed;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002263}
2264
2265bool BasicBlock::IsSSALiveOut(const CompilationUnit* c_unit, int ssa_reg) {
2266 // In order to determine if the ssa reg is live out, we scan all the MIRs. We remember
2267 // the last SSA number of the same dalvik register. At the end, if it is different than ssa_reg,
2268 // then it is not live out of this BB.
2269 int dalvik_reg = c_unit->mir_graph->SRegToVReg(ssa_reg);
2270
2271 int last_ssa_reg = -1;
2272
2273 // Walk through the MIRs backwards.
2274 for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
2275 // Get ssa rep.
2276 SSARepresentation *ssa_rep = mir->ssa_rep;
2277
2278 // Go through the defines for this MIR.
2279 for (int i = 0; i < ssa_rep->num_defs; i++) {
2280 DCHECK(ssa_rep->defs != nullptr);
2281
2282 // Get the ssa reg.
2283 int def_ssa_reg = ssa_rep->defs[i];
2284
2285 // Get dalvik reg.
2286 int def_dalvik_reg = c_unit->mir_graph->SRegToVReg(def_ssa_reg);
2287
2288 // Compare dalvik regs.
2289 if (dalvik_reg == def_dalvik_reg) {
2290 // We found a def of the register that we are being asked about.
2291 // Remember it.
2292 last_ssa_reg = def_ssa_reg;
2293 }
2294 }
2295 }
2296
2297 if (last_ssa_reg == -1) {
2298 // If we get to this point we couldn't find a define of register user asked about.
2299 // Let's assume the user knows what he's doing so we can be safe and say that if we
2300 // couldn't find a def, it is live out.
2301 return true;
2302 }
2303
2304 // If it is not -1, we found a match, is it ssa_reg?
2305 return (ssa_reg == last_ssa_reg);
2306}
2307
2308bool BasicBlock::ReplaceChild(BasicBlockId old_bb, BasicBlockId new_bb) {
2309 // We need to check taken, fall_through, and successor_blocks to replace.
2310 bool found = false;
2311 if (taken == old_bb) {
2312 taken = new_bb;
2313 found = true;
2314 }
2315
2316 if (fall_through == old_bb) {
2317 fall_through = new_bb;
2318 found = true;
2319 }
2320
2321 if (successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002322 for (SuccessorBlockInfo* successor_block_info : successor_blocks) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002323 if (successor_block_info->block == old_bb) {
2324 successor_block_info->block = new_bb;
2325 found = true;
2326 }
2327 }
2328 }
2329
2330 return found;
2331}
2332
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002333void BasicBlock::ErasePredecessor(BasicBlockId old_pred) {
2334 auto pos = std::find(predecessors.begin(), predecessors.end(), old_pred);
2335 DCHECK(pos != predecessors.end());
2336 predecessors.erase(pos);
2337}
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002338
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002339void BasicBlock::UpdatePredecessor(BasicBlockId old_pred, BasicBlockId new_pred) {
2340 DCHECK_NE(new_pred, NullBasicBlockId);
2341 auto pos = std::find(predecessors.begin(), predecessors.end(), old_pred);
2342 if (pos != predecessors.end()) {
2343 *pos = new_pred;
2344 } else {
2345 // If not found, add it.
2346 predecessors.push_back(new_pred);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002347 }
2348}
2349
2350// Create a new basic block with block_id as num_blocks_ that is
2351// post-incremented.
2352BasicBlock* MIRGraph::CreateNewBB(BBType block_type) {
2353 BasicBlock* res = NewMemBB(block_type, num_blocks_++);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002354 block_list_.push_back(res);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002355 return res;
2356}
2357
Jean Christophe Beyler2469e602014-05-06 20:36:55 -07002358void MIRGraph::CalculateBasicBlockInformation() {
2359 PassDriverMEPostOpt driver(cu_);
2360 driver.Launch();
2361}
2362
2363void MIRGraph::InitializeBasicBlockData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002364 num_blocks_ = block_list_.size();
Jean Christophe Beyler2469e602014-05-06 20:36:55 -07002365}
2366
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002367int MIR::DecodedInstruction::FlagsOf() const {
2368 // Calculate new index.
2369 int idx = static_cast<int>(opcode) - kNumPackedOpcodes;
2370
2371 // Check if it is an extended or not.
2372 if (idx < 0) {
2373 return Instruction::FlagsOf(opcode);
2374 }
2375
2376 // For extended, we use a switch.
2377 switch (static_cast<int>(opcode)) {
2378 case kMirOpPhi:
2379 return Instruction::kContinue;
2380 case kMirOpCopy:
2381 return Instruction::kContinue;
2382 case kMirOpFusedCmplFloat:
2383 return Instruction::kContinue | Instruction::kBranch;
2384 case kMirOpFusedCmpgFloat:
2385 return Instruction::kContinue | Instruction::kBranch;
2386 case kMirOpFusedCmplDouble:
2387 return Instruction::kContinue | Instruction::kBranch;
2388 case kMirOpFusedCmpgDouble:
2389 return Instruction::kContinue | Instruction::kBranch;
2390 case kMirOpFusedCmpLong:
2391 return Instruction::kContinue | Instruction::kBranch;
2392 case kMirOpNop:
2393 return Instruction::kContinue;
2394 case kMirOpNullCheck:
2395 return Instruction::kContinue | Instruction::kThrow;
2396 case kMirOpRangeCheck:
2397 return Instruction::kContinue | Instruction::kThrow;
2398 case kMirOpDivZeroCheck:
2399 return Instruction::kContinue | Instruction::kThrow;
2400 case kMirOpCheck:
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002401 return Instruction::kContinue | Instruction::kThrow;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002402 case kMirOpCheckPart2:
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002403 return Instruction::kContinue;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002404 case kMirOpSelect:
2405 return Instruction::kContinue;
2406 case kMirOpConstVector:
2407 return Instruction::kContinue;
2408 case kMirOpMoveVector:
2409 return Instruction::kContinue;
2410 case kMirOpPackedMultiply:
2411 return Instruction::kContinue;
2412 case kMirOpPackedAddition:
2413 return Instruction::kContinue;
2414 case kMirOpPackedSubtract:
2415 return Instruction::kContinue;
2416 case kMirOpPackedShiftLeft:
2417 return Instruction::kContinue;
2418 case kMirOpPackedSignedShiftRight:
2419 return Instruction::kContinue;
2420 case kMirOpPackedUnsignedShiftRight:
2421 return Instruction::kContinue;
2422 case kMirOpPackedAnd:
2423 return Instruction::kContinue;
2424 case kMirOpPackedOr:
2425 return Instruction::kContinue;
2426 case kMirOpPackedXor:
2427 return Instruction::kContinue;
2428 case kMirOpPackedAddReduce:
2429 return Instruction::kContinue;
2430 case kMirOpPackedReduce:
2431 return Instruction::kContinue;
2432 case kMirOpPackedSet:
2433 return Instruction::kContinue;
2434 case kMirOpReserveVectorRegisters:
2435 return Instruction::kContinue;
2436 case kMirOpReturnVectorRegisters:
2437 return Instruction::kContinue;
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002438 case kMirOpMemBarrier:
2439 return Instruction::kContinue;
2440 case kMirOpPackedArrayGet:
2441 return Instruction::kContinue | Instruction::kThrow;
2442 case kMirOpPackedArrayPut:
2443 return Instruction::kContinue | Instruction::kThrow;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002444 default:
2445 LOG(WARNING) << "ExtendedFlagsOf: Unhandled case: " << static_cast<int> (opcode);
2446 return 0;
2447 }
2448}
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002449} // namespace art