blob: 078f3810899f137e2697327bec969bf30c69c2e5 [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),
97 def_block_matrix_(NULL),
Vladimir Markobfea9c22014-01-17 17:49:33 +000098 temp_scoped_alloc_(),
99 temp_insn_data_(nullptr),
100 temp_bit_vector_size_(0u),
101 temp_bit_vector_(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])) {
1551 if (!singles_only && reg_location_[ssa_reg].wide) {
Ian Rogers23b03b52014-01-24 11:10:03 -08001552 return StringPrintf("v%d_%d#0x%" PRIx64, SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001553 ConstantValueWide(reg_location_[ssa_reg]));
1554 } else {
Brian Carlstromb1eba212013-07-17 18:07:19 -07001555 return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001556 ConstantValue(reg_location_[ssa_reg]));
1557 }
1558 } else {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001559 int vreg = SRegToVReg(ssa_reg);
1560 if (vreg >= static_cast<int>(GetFirstTempVR())) {
1561 return StringPrintf("t%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1562 } else {
1563 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1564 }
buzbee1fd33462013-03-25 13:40:45 -07001565 }
1566}
1567
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001568void MIRGraph::GetBlockName(BasicBlock* bb, char* name) {
buzbee1fd33462013-03-25 13:40:45 -07001569 switch (bb->block_type) {
1570 case kEntryBlock:
1571 snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id);
1572 break;
1573 case kExitBlock:
1574 snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id);
1575 break;
1576 case kDalvikByteCode:
1577 snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id);
1578 break;
1579 case kExceptionHandling:
1580 snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset,
1581 bb->id);
1582 break;
1583 default:
1584 snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id);
1585 break;
1586 }
1587}
1588
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001589const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) {
buzbee0d829482013-10-11 15:24:55 -07001590 // TODO: for inlining support, use current code unit.
buzbee1fd33462013-03-25 13:40:45 -07001591 const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx);
1592 return cu_->dex_file->GetShorty(method_id.proto_idx_);
1593}
1594
1595/* Debug Utility - dump a compilation unit */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001596void MIRGraph::DumpMIRGraph() {
buzbee1fd33462013-03-25 13:40:45 -07001597 const char* block_type_names[] = {
buzbee17189ac2013-11-08 11:07:02 -08001598 "Null Block",
buzbee1fd33462013-03-25 13:40:45 -07001599 "Entry Block",
1600 "Code Block",
1601 "Exit Block",
1602 "Exception Handling",
1603 "Catch Block"
1604 };
1605
1606 LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001607 LOG(INFO) << GetInsns(0) << " insns";
buzbee1fd33462013-03-25 13:40:45 -07001608 LOG(INFO) << GetNumBlocks() << " blocks in total";
buzbee1fd33462013-03-25 13:40:45 -07001609
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001610 for (BasicBlock* bb : block_list_) {
buzbee1fd33462013-03-25 13:40:45 -07001611 LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)",
1612 bb->id,
1613 block_type_names[bb->block_type],
1614 bb->start_offset,
1615 bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset,
1616 bb->last_mir_insn ? "" : " empty");
buzbee0d829482013-10-11 15:24:55 -07001617 if (bb->taken != NullBasicBlockId) {
1618 LOG(INFO) << " Taken branch: block " << bb->taken
1619 << "(0x" << std::hex << GetBasicBlock(bb->taken)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001620 }
buzbee0d829482013-10-11 15:24:55 -07001621 if (bb->fall_through != NullBasicBlockId) {
1622 LOG(INFO) << " Fallthrough : block " << bb->fall_through
1623 << " (0x" << std::hex << GetBasicBlock(bb->fall_through)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001624 }
1625 }
1626}
1627
1628/*
1629 * Build an array of location records for the incoming arguments.
1630 * Note: one location record per word of arguments, with dummy
1631 * high-word loc for wide arguments. Also pull up any following
1632 * MOVE_RESULT and incorporate it into the invoke.
1633 */
1634CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001635 bool is_range) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001636 CallInfo* info = static_cast<CallInfo*>(arena_->Alloc(sizeof(CallInfo),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001637 kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001638 MIR* move_result_mir = FindMoveResult(bb, mir);
1639 if (move_result_mir == NULL) {
1640 info->result.location = kLocInvalid;
1641 } else {
1642 info->result = GetRawDest(move_result_mir);
buzbee1fd33462013-03-25 13:40:45 -07001643 move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1644 }
1645 info->num_arg_words = mir->ssa_rep->num_uses;
1646 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001647 (arena_->Alloc(sizeof(RegLocation) * info->num_arg_words, kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001648 for (int i = 0; i < info->num_arg_words; i++) {
1649 info->args[i] = GetRawSrc(mir, i);
1650 }
1651 info->opt_flags = mir->optimization_flags;
1652 info->type = type;
1653 info->is_range = is_range;
1654 info->index = mir->dalvikInsn.vB;
1655 info->offset = mir->offset;
Vladimir Markof096aad2014-01-23 15:51:58 +00001656 info->mir = mir;
buzbee1fd33462013-03-25 13:40:45 -07001657 return info;
1658}
1659
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001660// Allocate a new MIR.
1661MIR* MIRGraph::NewMIR() {
1662 MIR* mir = new (arena_) MIR();
1663 return mir;
1664}
1665
buzbee862a7602013-04-05 10:58:54 -07001666// Allocate a new basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001667BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001668 BasicBlock* bb = new (arena_) BasicBlock(block_id, block_type, arena_);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001669
buzbee862a7602013-04-05 10:58:54 -07001670 // TUNING: better estimate of the exit block predecessors?
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001671 bb->predecessors.reserve((block_type == kExitBlock) ? 2048 : 2);
buzbee862a7602013-04-05 10:58:54 -07001672 block_id_map_.Put(block_id, block_id);
1673 return bb;
1674}
buzbee1fd33462013-03-25 13:40:45 -07001675
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001676void MIRGraph::InitializeConstantPropagation() {
1677 is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001678 constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(), kArenaAllocDFInfo));
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001679}
1680
1681void MIRGraph::InitializeMethodUses() {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001682 // The gate starts by initializing the use counts.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001683 int num_ssa_regs = GetNumSSARegs();
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001684 use_counts_.clear();
1685 use_counts_.reserve(num_ssa_regs + 32);
1686 use_counts_.resize(num_ssa_regs, 0u);
1687 raw_use_counts_.clear();
1688 raw_use_counts_.reserve(num_ssa_regs + 32);
1689 raw_use_counts_.resize(num_ssa_regs, 0u);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001690}
1691
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001692void MIRGraph::SSATransformationStart() {
1693 DCHECK(temp_scoped_alloc_.get() == nullptr);
1694 temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack));
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001695 temp_bit_vector_size_ = GetNumOfCodeAndTempVRs();
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001696 temp_bit_vector_ = new (temp_scoped_alloc_.get()) ArenaBitVector(
1697 temp_scoped_alloc_.get(), temp_bit_vector_size_, false, kBitMapRegisterV);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001698}
1699
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001700void MIRGraph::SSATransformationEnd() {
1701 // Verify the dataflow information after the pass.
1702 if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
1703 VerifyDataflow();
1704 }
1705
1706 temp_bit_vector_size_ = 0u;
1707 temp_bit_vector_ = nullptr;
1708 DCHECK(temp_scoped_alloc_.get() != nullptr);
1709 temp_scoped_alloc_.reset();
Johnny Qiuc029c982014-06-04 07:23:49 +00001710
1711 // Update the maximum number of reachable blocks.
1712 max_num_reachable_blocks_ = num_reachable_blocks_;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001713}
1714
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001715size_t MIRGraph::GetNumDalvikInsns() const {
1716 size_t cumulative_size = 0u;
1717 bool counted_current_item = false;
1718 const uint8_t size_for_null_code_item = 2u;
1719
1720 for (auto it : m_units_) {
1721 const DexFile::CodeItem* code_item = it->GetCodeItem();
1722 // Even if the code item is null, we still count non-zero value so that
1723 // each m_unit is counted as having impact.
1724 cumulative_size += (code_item == nullptr ?
1725 size_for_null_code_item : code_item->insns_size_in_code_units_);
1726 if (code_item == current_code_item_) {
1727 counted_current_item = true;
1728 }
1729 }
1730
1731 // If the current code item was not counted yet, count it now.
1732 // This can happen for example in unit tests where some fields like m_units_
1733 // are not initialized.
1734 if (counted_current_item == false) {
1735 cumulative_size += (current_code_item_ == nullptr ?
1736 size_for_null_code_item : current_code_item_->insns_size_in_code_units_);
1737 }
1738
1739 return cumulative_size;
1740}
1741
Vladimir Marko55fff042014-07-10 12:42:52 +01001742static BasicBlock* SelectTopologicalSortOrderFallBack(
1743 MIRGraph* mir_graph, const ArenaBitVector* current_loop,
1744 const ScopedArenaVector<size_t>* visited_cnt_values, ScopedArenaAllocator* allocator,
1745 ScopedArenaVector<BasicBlockId>* tmp_stack) {
1746 // No true loop head has been found but there may be true loop heads after the mess we need
1747 // to resolve. To avoid taking one of those, pick the candidate with the highest number of
1748 // reachable unvisited nodes. That candidate will surely be a part of a loop.
1749 BasicBlock* fall_back = nullptr;
1750 size_t fall_back_num_reachable = 0u;
1751 // Reuse the same bit vector for each candidate to mark reachable unvisited blocks.
1752 ArenaBitVector candidate_reachable(allocator, mir_graph->GetNumBlocks(), false, kBitMapMisc);
1753 AllNodesIterator iter(mir_graph);
1754 for (BasicBlock* candidate = iter.Next(); candidate != nullptr; candidate = iter.Next()) {
1755 if (candidate->hidden || // Hidden, or
1756 candidate->visited || // already processed, or
1757 (*visited_cnt_values)[candidate->id] == 0u || // no processed predecessors, or
1758 (current_loop != nullptr && // outside current loop.
1759 !current_loop->IsBitSet(candidate->id))) {
1760 continue;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001761 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001762 DCHECK(tmp_stack->empty());
1763 tmp_stack->push_back(candidate->id);
1764 candidate_reachable.ClearAllBits();
1765 size_t num_reachable = 0u;
1766 while (!tmp_stack->empty()) {
1767 BasicBlockId current_id = tmp_stack->back();
1768 tmp_stack->pop_back();
1769 BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
1770 DCHECK(current_bb != nullptr);
1771 ChildBlockIterator child_iter(current_bb, mir_graph);
1772 BasicBlock* child_bb = child_iter.Next();
1773 for ( ; child_bb != nullptr; child_bb = child_iter.Next()) {
1774 DCHECK(!child_bb->hidden);
1775 if (child_bb->visited || // Already processed, or
1776 (current_loop != nullptr && // outside current loop.
1777 !current_loop->IsBitSet(child_bb->id))) {
1778 continue;
1779 }
1780 if (!candidate_reachable.IsBitSet(child_bb->id)) {
1781 candidate_reachable.SetBit(child_bb->id);
1782 tmp_stack->push_back(child_bb->id);
1783 num_reachable += 1u;
1784 }
1785 }
1786 }
1787 if (fall_back_num_reachable < num_reachable) {
1788 fall_back_num_reachable = num_reachable;
1789 fall_back = candidate;
1790 }
1791 }
1792 return fall_back;
1793}
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001794
Vladimir Marko55fff042014-07-10 12:42:52 +01001795// Compute from which unvisited blocks is bb_id reachable through unvisited blocks.
1796static void ComputeUnvisitedReachableFrom(MIRGraph* mir_graph, BasicBlockId bb_id,
1797 ArenaBitVector* reachable,
1798 ScopedArenaVector<BasicBlockId>* tmp_stack) {
1799 // NOTE: Loop heads indicated by the "visited" flag.
1800 DCHECK(tmp_stack->empty());
1801 reachable->ClearAllBits();
1802 tmp_stack->push_back(bb_id);
1803 while (!tmp_stack->empty()) {
1804 BasicBlockId current_id = tmp_stack->back();
1805 tmp_stack->pop_back();
1806 BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
1807 DCHECK(current_bb != nullptr);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001808 for (BasicBlockId pred_id : current_bb->predecessors) {
1809 BasicBlock* pred_bb = mir_graph->GetBasicBlock(pred_id);
1810 DCHECK(pred_bb != nullptr);
Vladimir Marko55fff042014-07-10 12:42:52 +01001811 if (!pred_bb->visited && !reachable->IsBitSet(pred_bb->id)) {
1812 reachable->SetBit(pred_bb->id);
1813 tmp_stack->push_back(pred_bb->id);
1814 }
1815 }
1816 }
1817}
1818
1819void MIRGraph::ComputeTopologicalSortOrder() {
1820 ScopedArenaAllocator allocator(&cu_->arena_stack);
1821 unsigned int num_blocks = GetNumBlocks();
1822
1823 ScopedArenaQueue<BasicBlock*> q(allocator.Adapter());
1824 ScopedArenaVector<size_t> visited_cnt_values(num_blocks, 0u, allocator.Adapter());
1825 ScopedArenaVector<BasicBlockId> loop_head_stack(allocator.Adapter());
1826 size_t max_nested_loops = 0u;
1827 ArenaBitVector loop_exit_blocks(&allocator, num_blocks, false, kBitMapMisc);
1828 loop_exit_blocks.ClearAllBits();
1829
1830 // Count the number of blocks to process and add the entry block(s).
Vladimir Marko55fff042014-07-10 12:42:52 +01001831 unsigned int num_blocks_to_process = 0u;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001832 for (BasicBlock* bb : block_list_) {
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001833 if (bb->hidden == true) {
1834 continue;
1835 }
1836
Vladimir Marko55fff042014-07-10 12:42:52 +01001837 num_blocks_to_process += 1u;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001838
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001839 if (bb->predecessors.size() == 0u) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001840 // Add entry block to the queue.
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001841 q.push(bb);
1842 }
1843 }
1844
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001845 // Clear the topological order arrays.
1846 topological_order_.clear();
1847 topological_order_.reserve(num_blocks);
1848 topological_order_loop_ends_.clear();
1849 topological_order_loop_ends_.resize(num_blocks, 0u);
1850 topological_order_indexes_.clear();
1851 topological_order_indexes_.resize(num_blocks, static_cast<uint16_t>(-1));
Vladimir Marko55fff042014-07-10 12:42:52 +01001852
1853 // Mark all blocks as unvisited.
1854 ClearAllVisitedFlags();
1855
1856 // For loop heads, keep track from which blocks they are reachable not going through other
1857 // loop heads. Other loop heads are excluded to detect the heads of nested loops. The children
1858 // in this set go into the loop body, the other children are jumping over the loop.
1859 ScopedArenaVector<ArenaBitVector*> loop_head_reachable_from(allocator.Adapter());
1860 loop_head_reachable_from.resize(num_blocks, nullptr);
1861 // Reuse the same temp stack whenever calculating a loop_head_reachable_from[loop_head_id].
1862 ScopedArenaVector<BasicBlockId> tmp_stack(allocator.Adapter());
1863
1864 while (num_blocks_to_process != 0u) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001865 BasicBlock* bb = nullptr;
1866 if (!q.empty()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001867 num_blocks_to_process -= 1u;
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001868 // Get top.
1869 bb = q.front();
1870 q.pop();
Vladimir Marko55fff042014-07-10 12:42:52 +01001871 if (bb->visited) {
1872 // Loop head: it was already processed, mark end and copy exit blocks to the queue.
1873 DCHECK(q.empty()) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001874 uint16_t idx = static_cast<uint16_t>(topological_order_.size());
1875 topological_order_loop_ends_[topological_order_indexes_[bb->id]] = idx;
Vladimir Marko55fff042014-07-10 12:42:52 +01001876 DCHECK_EQ(loop_head_stack.back(), bb->id);
1877 loop_head_stack.pop_back();
1878 ArenaBitVector* reachable =
1879 loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
1880 for (BasicBlockId candidate_id : loop_exit_blocks.Indexes()) {
1881 if (reachable == nullptr || reachable->IsBitSet(candidate_id)) {
1882 q.push(GetBasicBlock(candidate_id));
1883 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1884 // so clearing the bit has no effect on the iterator.
1885 loop_exit_blocks.ClearBit(candidate_id);
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001886 }
1887 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001888 continue;
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001889 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001890 } else {
1891 // Find the new loop head.
1892 AllNodesIterator iter(this);
1893 while (true) {
1894 BasicBlock* candidate = iter.Next();
1895 if (candidate == nullptr) {
1896 // We did not find a true loop head, fall back to a reachable block in any loop.
1897 ArenaBitVector* current_loop =
1898 loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
1899 bb = SelectTopologicalSortOrderFallBack(this, current_loop, &visited_cnt_values,
1900 &allocator, &tmp_stack);
1901 DCHECK(bb != nullptr) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
1902 if (kIsDebugBuild && cu_->dex_file != nullptr) {
1903 LOG(INFO) << "Topological sort order: Using fall-back in "
1904 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " BB #" << bb->id
1905 << " @0x" << std::hex << bb->start_offset
1906 << ", num_blocks = " << std::dec << num_blocks;
1907 }
1908 break;
1909 }
1910 if (candidate->hidden || // Hidden, or
1911 candidate->visited || // already processed, or
1912 visited_cnt_values[candidate->id] == 0u || // no processed predecessors, or
1913 (!loop_head_stack.empty() && // outside current loop.
1914 !loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(candidate->id))) {
1915 continue;
1916 }
1917
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001918 for (BasicBlockId pred_id : candidate->predecessors) {
1919 BasicBlock* pred_bb = GetBasicBlock(pred_id);
1920 DCHECK(pred_bb != nullptr);
Vladimir Marko55fff042014-07-10 12:42:52 +01001921 if (pred_bb != candidate && !pred_bb->visited &&
1922 !pred_bb->dominators->IsBitSet(candidate->id)) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001923 candidate = nullptr; // Set candidate to null to indicate failure.
1924 break;
Vladimir Marko55fff042014-07-10 12:42:52 +01001925 }
1926 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001927 if (candidate != nullptr) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001928 bb = candidate;
1929 break;
1930 }
1931 }
1932 // Compute blocks from which the loop head is reachable and process those blocks first.
1933 ArenaBitVector* reachable =
1934 new (&allocator) ArenaBitVector(&allocator, num_blocks, false, kBitMapMisc);
1935 loop_head_reachable_from[bb->id] = reachable;
1936 ComputeUnvisitedReachableFrom(this, bb->id, reachable, &tmp_stack);
1937 // Now mark as loop head. (Even if it's only a fall back when we don't find a true loop.)
1938 loop_head_stack.push_back(bb->id);
1939 max_nested_loops = std::max(max_nested_loops, loop_head_stack.size());
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001940 }
1941
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001942 DCHECK_EQ(bb->hidden, false);
1943 DCHECK_EQ(bb->visited, false);
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001944 bb->visited = true;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001945
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001946 // Now add the basic block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001947 uint16_t idx = static_cast<uint16_t>(topological_order_.size());
1948 topological_order_indexes_[bb->id] = idx;
1949 topological_order_.push_back(bb->id);
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001950
Vladimir Marko55fff042014-07-10 12:42:52 +01001951 // Update visited_cnt_values for children.
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001952 ChildBlockIterator succIter(bb, this);
1953 BasicBlock* successor = succIter.Next();
1954 for ( ; successor != nullptr; successor = succIter.Next()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001955 if (successor->hidden) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001956 continue;
1957 }
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001958
Vladimir Marko55fff042014-07-10 12:42:52 +01001959 // One more predecessor was visited.
1960 visited_cnt_values[successor->id] += 1u;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001961 if (visited_cnt_values[successor->id] == successor->predecessors.size()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001962 if (loop_head_stack.empty() ||
1963 loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(successor->id)) {
1964 q.push(successor);
1965 } else {
1966 DCHECK(!loop_exit_blocks.IsBitSet(successor->id));
1967 loop_exit_blocks.SetBit(successor->id);
1968 }
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001969 }
1970 }
1971 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001972
1973 // Prepare the loop head stack for iteration.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001974 topological_order_loop_head_stack_.clear();
1975 topological_order_loop_head_stack_.reserve(max_nested_loops);
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001976}
1977
1978bool BasicBlock::IsExceptionBlock() const {
1979 if (block_type == kExceptionHandling) {
1980 return true;
1981 }
1982 return false;
1983}
1984
Wei Jin04f4d8a2014-05-29 18:04:29 -07001985bool MIRGraph::HasSuspendTestBetween(BasicBlock* source, BasicBlockId target_id) {
1986 BasicBlock* target = GetBasicBlock(target_id);
1987
1988 if (source == nullptr || target == nullptr)
1989 return false;
1990
1991 int idx;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001992 for (idx = gen_suspend_test_list_.size() - 1; idx >= 0; idx--) {
1993 BasicBlock* bb = gen_suspend_test_list_[idx];
Wei Jin04f4d8a2014-05-29 18:04:29 -07001994 if (bb == source)
1995 return true; // The block has been inserted by a suspend check before.
1996 if (source->dominators->IsBitSet(bb->id) && bb->dominators->IsBitSet(target_id))
1997 return true;
1998 }
1999
2000 return false;
2001}
2002
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002003ChildBlockIterator::ChildBlockIterator(BasicBlock* bb, MIRGraph* mir_graph)
2004 : basic_block_(bb), mir_graph_(mir_graph), visited_fallthrough_(false),
2005 visited_taken_(false), have_successors_(false) {
2006 // Check if we actually do have successors.
2007 if (basic_block_ != 0 && basic_block_->successor_block_list_type != kNotUsed) {
2008 have_successors_ = true;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002009 successor_iter_ = basic_block_->successor_blocks.cbegin();
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002010 }
2011}
2012
2013BasicBlock* ChildBlockIterator::Next() {
2014 // We check if we have a basic block. If we don't we cannot get next child.
2015 if (basic_block_ == nullptr) {
2016 return nullptr;
2017 }
2018
2019 // If we haven't visited fallthrough, return that.
2020 if (visited_fallthrough_ == false) {
2021 visited_fallthrough_ = true;
2022
2023 BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->fall_through);
2024 if (result != nullptr) {
2025 return result;
2026 }
2027 }
2028
2029 // If we haven't visited taken, return that.
2030 if (visited_taken_ == false) {
2031 visited_taken_ = true;
2032
2033 BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->taken);
2034 if (result != nullptr) {
2035 return result;
2036 }
2037 }
2038
2039 // We visited both taken and fallthrough. Now check if we have successors we need to visit.
2040 if (have_successors_ == true) {
2041 // Get information about next successor block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002042 auto end = basic_block_->successor_blocks.cend();
2043 while (successor_iter_ != end) {
2044 SuccessorBlockInfo* successor_block_info = *successor_iter_;
2045 ++successor_iter_;
Niranjan Kumar989367a2014-06-12 12:15:48 -07002046 // If block was replaced by zero block, take next one.
2047 if (successor_block_info->block != NullBasicBlockId) {
2048 return mir_graph_->GetBasicBlock(successor_block_info->block);
2049 }
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002050 }
2051 }
2052
2053 // We do not have anything.
2054 return nullptr;
2055}
2056
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002057BasicBlock* BasicBlock::Copy(CompilationUnit* c_unit) {
2058 MIRGraph* mir_graph = c_unit->mir_graph.get();
2059 return Copy(mir_graph);
2060}
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002061
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002062BasicBlock* BasicBlock::Copy(MIRGraph* mir_graph) {
2063 BasicBlock* result_bb = mir_graph->CreateNewBB(block_type);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002064
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002065 // We don't do a memcpy style copy here because it would lead to a lot of things
2066 // to clean up. Let us do it by hand instead.
2067 // Copy in taken and fallthrough.
2068 result_bb->fall_through = fall_through;
2069 result_bb->taken = taken;
2070
2071 // Copy successor links if needed.
2072 ArenaAllocator* arena = mir_graph->GetArena();
2073
2074 result_bb->successor_block_list_type = successor_block_list_type;
2075 if (result_bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002076 result_bb->successor_blocks.reserve(successor_blocks.size());
2077 for (SuccessorBlockInfo* sbi_old : successor_blocks) {
2078 SuccessorBlockInfo* sbi_new = static_cast<SuccessorBlockInfo*>(
2079 arena->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002080 memcpy(sbi_new, sbi_old, sizeof(SuccessorBlockInfo));
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002081 result_bb->successor_blocks.push_back(sbi_new);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002082 }
2083 }
2084
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002085 // Copy offset, method.
2086 result_bb->start_offset = start_offset;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002087
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002088 // Now copy instructions.
2089 for (MIR* mir = first_mir_insn; mir != 0; mir = mir->next) {
2090 // Get a copy first.
2091 MIR* copy = mir->Copy(mir_graph);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002092
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002093 // Append it.
2094 result_bb->AppendMIR(copy);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002095 }
2096
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002097 return result_bb;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002098}
2099
2100MIR* MIR::Copy(MIRGraph* mir_graph) {
2101 MIR* res = mir_graph->NewMIR();
2102 *res = *this;
2103
2104 // Remove links
2105 res->next = nullptr;
2106 res->bb = NullBasicBlockId;
2107 res->ssa_rep = nullptr;
2108
2109 return res;
2110}
2111
2112MIR* MIR::Copy(CompilationUnit* c_unit) {
2113 return Copy(c_unit->mir_graph.get());
2114}
2115
2116uint32_t SSARepresentation::GetStartUseIndex(Instruction::Code opcode) {
2117 // Default result.
2118 int res = 0;
2119
2120 // We are basically setting the iputs to their igets counterparts.
2121 switch (opcode) {
2122 case Instruction::IPUT:
2123 case Instruction::IPUT_OBJECT:
2124 case Instruction::IPUT_BOOLEAN:
2125 case Instruction::IPUT_BYTE:
2126 case Instruction::IPUT_CHAR:
2127 case Instruction::IPUT_SHORT:
2128 case Instruction::IPUT_QUICK:
2129 case Instruction::IPUT_OBJECT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -07002130 case Instruction::IPUT_BOOLEAN_QUICK:
2131 case Instruction::IPUT_BYTE_QUICK:
2132 case Instruction::IPUT_CHAR_QUICK:
2133 case Instruction::IPUT_SHORT_QUICK:
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002134 case Instruction::APUT:
2135 case Instruction::APUT_OBJECT:
2136 case Instruction::APUT_BOOLEAN:
2137 case Instruction::APUT_BYTE:
2138 case Instruction::APUT_CHAR:
2139 case Instruction::APUT_SHORT:
2140 case Instruction::SPUT:
2141 case Instruction::SPUT_OBJECT:
2142 case Instruction::SPUT_BOOLEAN:
2143 case Instruction::SPUT_BYTE:
2144 case Instruction::SPUT_CHAR:
2145 case Instruction::SPUT_SHORT:
2146 // Skip the VR containing what to store.
2147 res = 1;
2148 break;
2149 case Instruction::IPUT_WIDE:
2150 case Instruction::IPUT_WIDE_QUICK:
2151 case Instruction::APUT_WIDE:
2152 case Instruction::SPUT_WIDE:
2153 // Skip the two VRs containing what to store.
2154 res = 2;
2155 break;
2156 default:
2157 // Do nothing in the general case.
2158 break;
2159 }
2160
2161 return res;
2162}
2163
Jean Christophe Beylerc3db20b2014-05-05 21:09:40 -07002164/**
2165 * @brief Given a decoded instruction, it checks whether the instruction
2166 * sets a constant and if it does, more information is provided about the
2167 * constant being set.
2168 * @param ptr_value pointer to a 64-bit holder for the constant.
2169 * @param wide Updated by function whether a wide constant is being set by bytecode.
2170 * @return Returns false if the decoded instruction does not represent a constant bytecode.
2171 */
2172bool MIR::DecodedInstruction::GetConstant(int64_t* ptr_value, bool* wide) const {
2173 bool sets_const = true;
2174 int64_t value = vB;
2175
2176 DCHECK(ptr_value != nullptr);
2177 DCHECK(wide != nullptr);
2178
2179 switch (opcode) {
2180 case Instruction::CONST_4:
2181 case Instruction::CONST_16:
2182 case Instruction::CONST:
2183 *wide = false;
2184 value <<= 32; // In order to get the sign extend.
2185 value >>= 32;
2186 break;
2187 case Instruction::CONST_HIGH16:
2188 *wide = false;
2189 value <<= 48; // In order to get the sign extend.
2190 value >>= 32;
2191 break;
2192 case Instruction::CONST_WIDE_16:
2193 case Instruction::CONST_WIDE_32:
2194 *wide = true;
2195 value <<= 32; // In order to get the sign extend.
2196 value >>= 32;
2197 break;
2198 case Instruction::CONST_WIDE:
2199 *wide = true;
2200 value = vB_wide;
2201 break;
2202 case Instruction::CONST_WIDE_HIGH16:
2203 *wide = true;
2204 value <<= 48; // In order to get the sign extend.
2205 break;
2206 default:
2207 sets_const = false;
2208 break;
2209 }
2210
2211 if (sets_const) {
2212 *ptr_value = value;
2213 }
2214
2215 return sets_const;
2216}
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002217
2218void BasicBlock::ResetOptimizationFlags(uint16_t reset_flags) {
2219 // Reset flags for all MIRs in bb.
2220 for (MIR* mir = first_mir_insn; mir != NULL; mir = mir->next) {
2221 mir->optimization_flags &= (~reset_flags);
2222 }
2223}
2224
2225void BasicBlock::Hide(CompilationUnit* c_unit) {
2226 // First lets make it a dalvik bytecode block so it doesn't have any special meaning.
2227 block_type = kDalvikByteCode;
2228
2229 // Mark it as hidden.
2230 hidden = true;
2231
2232 // Detach it from its MIRs so we don't generate code for them. Also detached MIRs
2233 // are updated to know that they no longer have a parent.
2234 for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
2235 mir->bb = NullBasicBlockId;
2236 }
2237 first_mir_insn = nullptr;
2238 last_mir_insn = nullptr;
2239
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002240 MIRGraph* mir_graph = c_unit->mir_graph.get();
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002241 for (BasicBlockId pred_id : predecessors) {
2242 BasicBlock* pred_bb = mir_graph->GetBasicBlock(pred_id);
2243 DCHECK(pred_bb != nullptr);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002244
2245 // Sadly we have to go through the children by hand here.
2246 pred_bb->ReplaceChild(id, NullBasicBlockId);
2247 }
2248
2249 // Iterate through children of bb we are hiding.
2250 ChildBlockIterator successorChildIter(this, mir_graph);
2251
2252 for (BasicBlock* childPtr = successorChildIter.Next(); childPtr != 0; childPtr = successorChildIter.Next()) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002253 // Erase this predecessor from child.
2254 childPtr->ErasePredecessor(id);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002255 }
Jean Christophe Beylerf588b502014-06-18 14:14:15 -07002256
2257 // Remove link to children.
2258 taken = NullBasicBlockId;
2259 fall_through = NullBasicBlockId;
2260 successor_block_list_type = kNotUsed;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002261}
2262
2263bool BasicBlock::IsSSALiveOut(const CompilationUnit* c_unit, int ssa_reg) {
2264 // In order to determine if the ssa reg is live out, we scan all the MIRs. We remember
2265 // the last SSA number of the same dalvik register. At the end, if it is different than ssa_reg,
2266 // then it is not live out of this BB.
2267 int dalvik_reg = c_unit->mir_graph->SRegToVReg(ssa_reg);
2268
2269 int last_ssa_reg = -1;
2270
2271 // Walk through the MIRs backwards.
2272 for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
2273 // Get ssa rep.
2274 SSARepresentation *ssa_rep = mir->ssa_rep;
2275
2276 // Go through the defines for this MIR.
2277 for (int i = 0; i < ssa_rep->num_defs; i++) {
2278 DCHECK(ssa_rep->defs != nullptr);
2279
2280 // Get the ssa reg.
2281 int def_ssa_reg = ssa_rep->defs[i];
2282
2283 // Get dalvik reg.
2284 int def_dalvik_reg = c_unit->mir_graph->SRegToVReg(def_ssa_reg);
2285
2286 // Compare dalvik regs.
2287 if (dalvik_reg == def_dalvik_reg) {
2288 // We found a def of the register that we are being asked about.
2289 // Remember it.
2290 last_ssa_reg = def_ssa_reg;
2291 }
2292 }
2293 }
2294
2295 if (last_ssa_reg == -1) {
2296 // If we get to this point we couldn't find a define of register user asked about.
2297 // Let's assume the user knows what he's doing so we can be safe and say that if we
2298 // couldn't find a def, it is live out.
2299 return true;
2300 }
2301
2302 // If it is not -1, we found a match, is it ssa_reg?
2303 return (ssa_reg == last_ssa_reg);
2304}
2305
2306bool BasicBlock::ReplaceChild(BasicBlockId old_bb, BasicBlockId new_bb) {
2307 // We need to check taken, fall_through, and successor_blocks to replace.
2308 bool found = false;
2309 if (taken == old_bb) {
2310 taken = new_bb;
2311 found = true;
2312 }
2313
2314 if (fall_through == old_bb) {
2315 fall_through = new_bb;
2316 found = true;
2317 }
2318
2319 if (successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002320 for (SuccessorBlockInfo* successor_block_info : successor_blocks) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002321 if (successor_block_info->block == old_bb) {
2322 successor_block_info->block = new_bb;
2323 found = true;
2324 }
2325 }
2326 }
2327
2328 return found;
2329}
2330
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002331void BasicBlock::ErasePredecessor(BasicBlockId old_pred) {
2332 auto pos = std::find(predecessors.begin(), predecessors.end(), old_pred);
2333 DCHECK(pos != predecessors.end());
2334 predecessors.erase(pos);
2335}
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002336
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002337void BasicBlock::UpdatePredecessor(BasicBlockId old_pred, BasicBlockId new_pred) {
2338 DCHECK_NE(new_pred, NullBasicBlockId);
2339 auto pos = std::find(predecessors.begin(), predecessors.end(), old_pred);
2340 if (pos != predecessors.end()) {
2341 *pos = new_pred;
2342 } else {
2343 // If not found, add it.
2344 predecessors.push_back(new_pred);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002345 }
2346}
2347
2348// Create a new basic block with block_id as num_blocks_ that is
2349// post-incremented.
2350BasicBlock* MIRGraph::CreateNewBB(BBType block_type) {
2351 BasicBlock* res = NewMemBB(block_type, num_blocks_++);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002352 block_list_.push_back(res);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002353 return res;
2354}
2355
Jean Christophe Beyler2469e602014-05-06 20:36:55 -07002356void MIRGraph::CalculateBasicBlockInformation() {
2357 PassDriverMEPostOpt driver(cu_);
2358 driver.Launch();
2359}
2360
2361void MIRGraph::InitializeBasicBlockData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002362 num_blocks_ = block_list_.size();
Jean Christophe Beyler2469e602014-05-06 20:36:55 -07002363}
2364
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002365int MIR::DecodedInstruction::FlagsOf() const {
2366 // Calculate new index.
2367 int idx = static_cast<int>(opcode) - kNumPackedOpcodes;
2368
2369 // Check if it is an extended or not.
2370 if (idx < 0) {
2371 return Instruction::FlagsOf(opcode);
2372 }
2373
2374 // For extended, we use a switch.
2375 switch (static_cast<int>(opcode)) {
2376 case kMirOpPhi:
2377 return Instruction::kContinue;
2378 case kMirOpCopy:
2379 return Instruction::kContinue;
2380 case kMirOpFusedCmplFloat:
2381 return Instruction::kContinue | Instruction::kBranch;
2382 case kMirOpFusedCmpgFloat:
2383 return Instruction::kContinue | Instruction::kBranch;
2384 case kMirOpFusedCmplDouble:
2385 return Instruction::kContinue | Instruction::kBranch;
2386 case kMirOpFusedCmpgDouble:
2387 return Instruction::kContinue | Instruction::kBranch;
2388 case kMirOpFusedCmpLong:
2389 return Instruction::kContinue | Instruction::kBranch;
2390 case kMirOpNop:
2391 return Instruction::kContinue;
2392 case kMirOpNullCheck:
2393 return Instruction::kContinue | Instruction::kThrow;
2394 case kMirOpRangeCheck:
2395 return Instruction::kContinue | Instruction::kThrow;
2396 case kMirOpDivZeroCheck:
2397 return Instruction::kContinue | Instruction::kThrow;
2398 case kMirOpCheck:
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002399 return Instruction::kContinue | Instruction::kThrow;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002400 case kMirOpCheckPart2:
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002401 return Instruction::kContinue;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002402 case kMirOpSelect:
2403 return Instruction::kContinue;
2404 case kMirOpConstVector:
2405 return Instruction::kContinue;
2406 case kMirOpMoveVector:
2407 return Instruction::kContinue;
2408 case kMirOpPackedMultiply:
2409 return Instruction::kContinue;
2410 case kMirOpPackedAddition:
2411 return Instruction::kContinue;
2412 case kMirOpPackedSubtract:
2413 return Instruction::kContinue;
2414 case kMirOpPackedShiftLeft:
2415 return Instruction::kContinue;
2416 case kMirOpPackedSignedShiftRight:
2417 return Instruction::kContinue;
2418 case kMirOpPackedUnsignedShiftRight:
2419 return Instruction::kContinue;
2420 case kMirOpPackedAnd:
2421 return Instruction::kContinue;
2422 case kMirOpPackedOr:
2423 return Instruction::kContinue;
2424 case kMirOpPackedXor:
2425 return Instruction::kContinue;
2426 case kMirOpPackedAddReduce:
2427 return Instruction::kContinue;
2428 case kMirOpPackedReduce:
2429 return Instruction::kContinue;
2430 case kMirOpPackedSet:
2431 return Instruction::kContinue;
2432 case kMirOpReserveVectorRegisters:
2433 return Instruction::kContinue;
2434 case kMirOpReturnVectorRegisters:
2435 return Instruction::kContinue;
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002436 case kMirOpMemBarrier:
2437 return Instruction::kContinue;
2438 case kMirOpPackedArrayGet:
2439 return Instruction::kContinue | Instruction::kThrow;
2440 case kMirOpPackedArrayPut:
2441 return Instruction::kContinue | Instruction::kThrow;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002442 default:
2443 LOG(WARNING) << "ExtendedFlagsOf: Unhandled case: " << static_cast<int> (opcode);
2444 return 0;
2445 }
2446}
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002447} // namespace art