blob: 563b64a33568edb51756655a4becc107dcdbe922 [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;
buzbee0d829482013-10-11 15:24:55 -0700181 MIR* prev = NULL;
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 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100190 BasicBlock* bottom_block = CreateNewBB(kDalvikByteCode);
buzbee311ca162013-02-28 15:56:43 -0800191
192 bottom_block->start_offset = code_offset;
193 bottom_block->first_mir_insn = insn;
194 bottom_block->last_mir_insn = orig_block->last_mir_insn;
195
Junmo Park90223cc2014-08-04 18:51:21 +0900196 /* If this block was terminated by a return, conditional branch or throw,
197 * the flag needs to go with the bottom block
198 */
buzbee311ca162013-02-28 15:56:43 -0800199 bottom_block->terminated_by_return = orig_block->terminated_by_return;
200 orig_block->terminated_by_return = false;
201
Junmo Park90223cc2014-08-04 18:51:21 +0900202 bottom_block->conditional_branch = orig_block->conditional_branch;
203 orig_block->conditional_branch = false;
204
205 bottom_block->explicit_throw = orig_block->explicit_throw;
206 orig_block->explicit_throw = false;
207
buzbee311ca162013-02-28 15:56:43 -0800208 /* Handle the taken path */
209 bottom_block->taken = orig_block->taken;
buzbee0d829482013-10-11 15:24:55 -0700210 if (bottom_block->taken != NullBasicBlockId) {
211 orig_block->taken = NullBasicBlockId;
212 BasicBlock* bb_taken = GetBasicBlock(bottom_block->taken);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100213 bb_taken->ErasePredecessor(orig_block->id);
214 bb_taken->predecessors.push_back(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800215 }
216
217 /* Handle the fallthrough path */
218 bottom_block->fall_through = orig_block->fall_through;
buzbee0d829482013-10-11 15:24:55 -0700219 orig_block->fall_through = bottom_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100220 bottom_block->predecessors.push_back(orig_block->id);
buzbee0d829482013-10-11 15:24:55 -0700221 if (bottom_block->fall_through != NullBasicBlockId) {
222 BasicBlock* bb_fall_through = GetBasicBlock(bottom_block->fall_through);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100223 bb_fall_through->ErasePredecessor(orig_block->id);
224 bb_fall_through->predecessors.push_back(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800225 }
226
227 /* Handle the successor list */
buzbee0d829482013-10-11 15:24:55 -0700228 if (orig_block->successor_block_list_type != kNotUsed) {
229 bottom_block->successor_block_list_type = orig_block->successor_block_list_type;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100230 bottom_block->successor_blocks.swap(orig_block->successor_blocks);
buzbee0d829482013-10-11 15:24:55 -0700231 orig_block->successor_block_list_type = kNotUsed;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100232 DCHECK(orig_block->successor_blocks.empty()); // Empty after the swap() above.
233 for (SuccessorBlockInfo* successor_block_info : bottom_block->successor_blocks) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700234 BasicBlock* bb = GetBasicBlock(successor_block_info->block);
Niranjan Kumar989367a2014-06-12 12:15:48 -0700235 if (bb != nullptr) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100236 bb->ErasePredecessor(orig_block->id);
237 bb->predecessors.push_back(bottom_block->id);
Niranjan Kumar989367a2014-06-12 12:15:48 -0700238 }
buzbee311ca162013-02-28 15:56:43 -0800239 }
240 }
241
buzbee0d829482013-10-11 15:24:55 -0700242 orig_block->last_mir_insn = prev;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700243 prev->next = nullptr;
buzbee311ca162013-02-28 15:56:43 -0800244
buzbee311ca162013-02-28 15:56:43 -0800245 /*
246 * Update the immediate predecessor block pointer so that outgoing edges
247 * can be applied to the proper block.
248 */
249 if (immed_pred_block_p) {
250 DCHECK_EQ(*immed_pred_block_p, orig_block);
251 *immed_pred_block_p = bottom_block;
252 }
buzbeeb48819d2013-09-14 16:15:25 -0700253
254 // Associate dex instructions in the bottom block with the new container.
Vladimir Marko4376c872014-01-23 12:39:29 +0000255 DCHECK(insn != nullptr);
256 DCHECK(insn != orig_block->first_mir_insn);
257 DCHECK(insn == bottom_block->first_mir_insn);
258 DCHECK_EQ(insn->offset, bottom_block->start_offset);
259 DCHECK(static_cast<int>(insn->dalvikInsn.opcode) == kMirOpCheck ||
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700260 !MIR::DecodedInstruction::IsPseudoMirOp(insn->dalvikInsn.opcode));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100261 DCHECK_EQ(dex_pc_to_block_map_[insn->offset], orig_block->id);
Vladimir Marko4376c872014-01-23 12:39:29 +0000262 MIR* p = insn;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100263 dex_pc_to_block_map_[p->offset] = bottom_block->id;
Vladimir Marko4376c872014-01-23 12:39:29 +0000264 while (p != bottom_block->last_mir_insn) {
265 p = p->next;
266 DCHECK(p != nullptr);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700267 p->bb = bottom_block->id;
buzbeeb48819d2013-09-14 16:15:25 -0700268 int opcode = p->dalvikInsn.opcode;
269 /*
270 * Some messiness here to ensure that we only enter real opcodes and only the
271 * first half of a potentially throwing instruction that has been split into
Vladimir Marko4376c872014-01-23 12:39:29 +0000272 * CHECK and work portions. Since the 2nd half of a split operation is always
273 * the first in a BasicBlock, we can't hit it here.
buzbeeb48819d2013-09-14 16:15:25 -0700274 */
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700275 if ((opcode == kMirOpCheck) || !MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100276 DCHECK_EQ(dex_pc_to_block_map_[p->offset], orig_block->id);
277 dex_pc_to_block_map_[p->offset] = bottom_block->id;
buzbeeb48819d2013-09-14 16:15:25 -0700278 }
buzbeeb48819d2013-09-14 16:15:25 -0700279 }
280
buzbee311ca162013-02-28 15:56:43 -0800281 return bottom_block;
282}
283
284/*
285 * Given a code offset, find out the block that starts with it. If the offset
286 * is in the middle of an existing block, split it into two. If immed_pred_block_p
287 * is not non-null and is the block being split, update *immed_pred_block_p to
288 * point to the bottom block so that outgoing edges can be set up properly
289 * (by the caller)
290 * Utilizes a map for fast lookup of the typical cases.
291 */
buzbee0d829482013-10-11 15:24:55 -0700292BasicBlock* MIRGraph::FindBlock(DexOffset code_offset, bool split, bool create,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700293 BasicBlock** immed_pred_block_p) {
Razvan A Lupusoru09ae0222014-07-07 16:29:37 -0700294 if (code_offset >= current_code_item_->insns_size_in_code_units_) {
buzbee311ca162013-02-28 15:56:43 -0800295 return NULL;
296 }
buzbeeb48819d2013-09-14 16:15:25 -0700297
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100298 int block_id = dex_pc_to_block_map_[code_offset];
299 BasicBlock* bb = GetBasicBlock(block_id);
buzbeeb48819d2013-09-14 16:15:25 -0700300
301 if ((bb != NULL) && (bb->start_offset == code_offset)) {
302 // Does this containing block start with the desired instruction?
buzbeebd663de2013-09-10 15:41:31 -0700303 return bb;
304 }
buzbee311ca162013-02-28 15:56:43 -0800305
buzbeeb48819d2013-09-14 16:15:25 -0700306 // No direct hit.
307 if (!create) {
308 return NULL;
buzbee311ca162013-02-28 15:56:43 -0800309 }
310
buzbeeb48819d2013-09-14 16:15:25 -0700311 if (bb != NULL) {
312 // The target exists somewhere in an existing block.
313 return SplitBlock(code_offset, bb, bb == *immed_pred_block_p ? immed_pred_block_p : NULL);
314 }
315
316 // Create a new block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100317 bb = CreateNewBB(kDalvikByteCode);
buzbee311ca162013-02-28 15:56:43 -0800318 bb->start_offset = code_offset;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100319 dex_pc_to_block_map_[bb->start_offset] = bb->id;
buzbee311ca162013-02-28 15:56:43 -0800320 return bb;
321}
322
buzbeeb48819d2013-09-14 16:15:25 -0700323
buzbee311ca162013-02-28 15:56:43 -0800324/* Identify code range in try blocks and set up the empty catch blocks */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700325void MIRGraph::ProcessTryCatchBlocks() {
buzbee311ca162013-02-28 15:56:43 -0800326 int tries_size = current_code_item_->tries_size_;
buzbee0d829482013-10-11 15:24:55 -0700327 DexOffset offset;
buzbee311ca162013-02-28 15:56:43 -0800328
329 if (tries_size == 0) {
330 return;
331 }
332
333 for (int i = 0; i < tries_size; i++) {
334 const DexFile::TryItem* pTry =
335 DexFile::GetTryItems(*current_code_item_, i);
buzbee0d829482013-10-11 15:24:55 -0700336 DexOffset start_offset = pTry->start_addr_;
337 DexOffset end_offset = start_offset + pTry->insn_count_;
buzbee311ca162013-02-28 15:56:43 -0800338 for (offset = start_offset; offset < end_offset; offset++) {
buzbee862a7602013-04-05 10:58:54 -0700339 try_block_addr_->SetBit(offset);
buzbee311ca162013-02-28 15:56:43 -0800340 }
341 }
342
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700343 // Iterate over each of the handlers to enqueue the empty Catch blocks.
buzbee311ca162013-02-28 15:56:43 -0800344 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*current_code_item_, 0);
345 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
346 for (uint32_t idx = 0; idx < handlers_size; idx++) {
347 CatchHandlerIterator iterator(handlers_ptr);
348 for (; iterator.HasNext(); iterator.Next()) {
349 uint32_t address = iterator.GetHandlerAddress();
350 FindBlock(address, false /* split */, true /*create*/,
351 /* immed_pred_block_p */ NULL);
352 }
353 handlers_ptr = iterator.EndDataPointer();
354 }
355}
356
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100357bool MIRGraph::IsBadMonitorExitCatch(NarrowDexOffset monitor_exit_offset,
358 NarrowDexOffset catch_offset) {
359 // Catches for monitor-exit during stack unwinding have the pattern
360 // move-exception (move)* (goto)? monitor-exit throw
361 // In the currently generated dex bytecode we see these catching a bytecode range including
362 // either its own or an identical monitor-exit, http://b/15745363 . This function checks if
363 // it's the case for a given monitor-exit and catch block so that we can ignore it.
364 // (We don't want to ignore all monitor-exit catches since one could enclose a synchronized
365 // block in a try-block and catch the NPE, Error or Throwable and we should let it through;
366 // even though a throwing monitor-exit certainly indicates a bytecode error.)
Razvan A Lupusoru09ae0222014-07-07 16:29:37 -0700367 const Instruction* monitor_exit = Instruction::At(current_code_item_->insns_ + monitor_exit_offset);
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100368 DCHECK(monitor_exit->Opcode() == Instruction::MONITOR_EXIT);
369 int monitor_reg = monitor_exit->VRegA_11x();
Razvan A Lupusoru09ae0222014-07-07 16:29:37 -0700370 const Instruction* check_insn = Instruction::At(current_code_item_->insns_ + catch_offset);
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100371 DCHECK(check_insn->Opcode() == Instruction::MOVE_EXCEPTION);
372 if (check_insn->VRegA_11x() == monitor_reg) {
373 // Unexpected move-exception to the same register. Probably not the pattern we're looking for.
374 return false;
375 }
376 check_insn = check_insn->Next();
377 while (true) {
378 int dest = -1;
379 bool wide = false;
380 switch (check_insn->Opcode()) {
381 case Instruction::MOVE_WIDE:
382 wide = true;
383 // Intentional fall-through.
384 case Instruction::MOVE_OBJECT:
385 case Instruction::MOVE:
386 dest = check_insn->VRegA_12x();
387 break;
388
389 case Instruction::MOVE_WIDE_FROM16:
390 wide = true;
391 // Intentional fall-through.
392 case Instruction::MOVE_OBJECT_FROM16:
393 case Instruction::MOVE_FROM16:
394 dest = check_insn->VRegA_22x();
395 break;
396
397 case Instruction::MOVE_WIDE_16:
398 wide = true;
399 // Intentional fall-through.
400 case Instruction::MOVE_OBJECT_16:
401 case Instruction::MOVE_16:
402 dest = check_insn->VRegA_32x();
403 break;
404
405 case Instruction::GOTO:
406 case Instruction::GOTO_16:
407 case Instruction::GOTO_32:
408 check_insn = check_insn->RelativeAt(check_insn->GetTargetOffset());
409 // Intentional fall-through.
410 default:
411 return check_insn->Opcode() == Instruction::MONITOR_EXIT &&
412 check_insn->VRegA_11x() == monitor_reg;
413 }
414
415 if (dest == monitor_reg || (wide && dest + 1 == monitor_reg)) {
416 return false;
417 }
418
419 check_insn = check_insn->Next();
420 }
421}
422
buzbee311ca162013-02-28 15:56:43 -0800423/* Process instructions with the kBranch flag */
buzbee0d829482013-10-11 15:24:55 -0700424BasicBlock* MIRGraph::ProcessCanBranch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
425 int width, int flags, const uint16_t* code_ptr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700426 const uint16_t* code_end) {
buzbee0d829482013-10-11 15:24:55 -0700427 DexOffset target = cur_offset;
buzbee311ca162013-02-28 15:56:43 -0800428 switch (insn->dalvikInsn.opcode) {
429 case Instruction::GOTO:
430 case Instruction::GOTO_16:
431 case Instruction::GOTO_32:
432 target += insn->dalvikInsn.vA;
433 break;
434 case Instruction::IF_EQ:
435 case Instruction::IF_NE:
436 case Instruction::IF_LT:
437 case Instruction::IF_GE:
438 case Instruction::IF_GT:
439 case Instruction::IF_LE:
440 cur_block->conditional_branch = true;
441 target += insn->dalvikInsn.vC;
442 break;
443 case Instruction::IF_EQZ:
444 case Instruction::IF_NEZ:
445 case Instruction::IF_LTZ:
446 case Instruction::IF_GEZ:
447 case Instruction::IF_GTZ:
448 case Instruction::IF_LEZ:
449 cur_block->conditional_branch = true;
450 target += insn->dalvikInsn.vB;
451 break;
452 default:
453 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
454 }
buzbeeb48819d2013-09-14 16:15:25 -0700455 CountBranch(target);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700456 BasicBlock* taken_block = FindBlock(target, /* split */ true, /* create */ true,
buzbee311ca162013-02-28 15:56:43 -0800457 /* immed_pred_block_p */ &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700458 cur_block->taken = taken_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100459 taken_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800460
461 /* Always terminate the current block for conditional branches */
462 if (flags & Instruction::kContinue) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700463 BasicBlock* fallthrough_block = FindBlock(cur_offset + width,
buzbee311ca162013-02-28 15:56:43 -0800464 /*
465 * If the method is processed
466 * in sequential order from the
467 * beginning, we don't need to
468 * specify split for continue
469 * blocks. However, this
470 * routine can be called by
471 * compileLoop, which starts
472 * parsing the method from an
473 * arbitrary address in the
474 * method body.
475 */
476 true,
477 /* create */
478 true,
479 /* immed_pred_block_p */
480 &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700481 cur_block->fall_through = fallthrough_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100482 fallthrough_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800483 } else if (code_ptr < code_end) {
buzbee27247762013-07-28 12:03:58 -0700484 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
buzbee311ca162013-02-28 15:56:43 -0800485 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800486 }
487 return cur_block;
488}
489
490/* Process instructions with the kSwitch flag */
buzbee17189ac2013-11-08 11:07:02 -0800491BasicBlock* MIRGraph::ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
492 int width, int flags) {
buzbee311ca162013-02-28 15:56:43 -0800493 const uint16_t* switch_data =
494 reinterpret_cast<const uint16_t*>(GetCurrentInsns() + cur_offset + insn->dalvikInsn.vB);
495 int size;
496 const int* keyTable;
497 const int* target_table;
498 int i;
499 int first_key;
500
501 /*
502 * Packed switch data format:
503 * ushort ident = 0x0100 magic value
504 * ushort size number of entries in the table
505 * int first_key first (and lowest) switch case value
506 * int targets[size] branch targets, relative to switch opcode
507 *
508 * Total size is (4+size*2) 16-bit code units.
509 */
510 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
511 DCHECK_EQ(static_cast<int>(switch_data[0]),
512 static_cast<int>(Instruction::kPackedSwitchSignature));
513 size = switch_data[1];
514 first_key = switch_data[2] | (switch_data[3] << 16);
515 target_table = reinterpret_cast<const int*>(&switch_data[4]);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700516 keyTable = NULL; // Make the compiler happy.
buzbee311ca162013-02-28 15:56:43 -0800517 /*
518 * Sparse switch data format:
519 * ushort ident = 0x0200 magic value
520 * ushort size number of entries in the table; > 0
521 * int keys[size] keys, sorted low-to-high; 32-bit aligned
522 * int targets[size] branch targets, relative to switch opcode
523 *
524 * Total size is (2+size*4) 16-bit code units.
525 */
526 } else {
527 DCHECK_EQ(static_cast<int>(switch_data[0]),
528 static_cast<int>(Instruction::kSparseSwitchSignature));
529 size = switch_data[1];
530 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
531 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700532 first_key = 0; // To make the compiler happy.
buzbee311ca162013-02-28 15:56:43 -0800533 }
534
buzbee0d829482013-10-11 15:24:55 -0700535 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800536 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700537 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800538 }
buzbee0d829482013-10-11 15:24:55 -0700539 cur_block->successor_block_list_type =
540 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? kPackedSwitch : kSparseSwitch;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100541 cur_block->successor_blocks.reserve(size);
buzbee311ca162013-02-28 15:56:43 -0800542
543 for (i = 0; i < size; i++) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700544 BasicBlock* case_block = FindBlock(cur_offset + target_table[i], /* split */ true,
buzbee311ca162013-02-28 15:56:43 -0800545 /* create */ true, /* immed_pred_block_p */ &cur_block);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700546 SuccessorBlockInfo* successor_block_info =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700547 static_cast<SuccessorBlockInfo*>(arena_->Alloc(sizeof(SuccessorBlockInfo),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000548 kArenaAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700549 successor_block_info->block = case_block->id;
buzbee311ca162013-02-28 15:56:43 -0800550 successor_block_info->key =
551 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
552 first_key + i : keyTable[i];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100553 cur_block->successor_blocks.push_back(successor_block_info);
554 case_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800555 }
556
557 /* Fall-through case */
Brian Carlstromdf629502013-07-17 22:39:56 -0700558 BasicBlock* fallthrough_block = FindBlock(cur_offset + width, /* split */ false,
559 /* create */ true, /* immed_pred_block_p */ NULL);
buzbee0d829482013-10-11 15:24:55 -0700560 cur_block->fall_through = fallthrough_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100561 fallthrough_block->predecessors.push_back(cur_block->id);
buzbee17189ac2013-11-08 11:07:02 -0800562 return cur_block;
buzbee311ca162013-02-28 15:56:43 -0800563}
564
565/* Process instructions with the kThrow flag */
buzbee0d829482013-10-11 15:24:55 -0700566BasicBlock* MIRGraph::ProcessCanThrow(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
567 int width, int flags, ArenaBitVector* try_block_addr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700568 const uint16_t* code_ptr, const uint16_t* code_end) {
buzbee862a7602013-04-05 10:58:54 -0700569 bool in_try_block = try_block_addr->IsBitSet(cur_offset);
buzbee17189ac2013-11-08 11:07:02 -0800570 bool is_throw = (insn->dalvikInsn.opcode == Instruction::THROW);
buzbee311ca162013-02-28 15:56:43 -0800571
572 /* In try block */
573 if (in_try_block) {
574 CatchHandlerIterator iterator(*current_code_item_, cur_offset);
575
buzbee0d829482013-10-11 15:24:55 -0700576 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800577 LOG(INFO) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
578 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700579 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800580 }
581
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700582 for (; iterator.HasNext(); iterator.Next()) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700583 BasicBlock* catch_block = FindBlock(iterator.GetHandlerAddress(), false /* split*/,
buzbee311ca162013-02-28 15:56:43 -0800584 false /* creat */, NULL /* immed_pred_block_p */);
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100585 if (insn->dalvikInsn.opcode == Instruction::MONITOR_EXIT &&
586 IsBadMonitorExitCatch(insn->offset, catch_block->start_offset)) {
587 // Don't allow monitor-exit to catch its own exception, http://b/15745363 .
588 continue;
589 }
590 if (cur_block->successor_block_list_type == kNotUsed) {
591 cur_block->successor_block_list_type = kCatch;
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100592 }
buzbee311ca162013-02-28 15:56:43 -0800593 catch_block->catch_entry = true;
594 if (kIsDebugBuild) {
595 catches_.insert(catch_block->start_offset);
596 }
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700597 SuccessorBlockInfo* successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000598 (arena_->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700599 successor_block_info->block = catch_block->id;
buzbee311ca162013-02-28 15:56:43 -0800600 successor_block_info->key = iterator.GetHandlerTypeIndex();
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100601 cur_block->successor_blocks.push_back(successor_block_info);
602 catch_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800603 }
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100604 in_try_block = (cur_block->successor_block_list_type != kNotUsed);
605 }
Vladimir Markoe767f6c2014-10-01 17:38:02 +0100606 bool build_all_edges =
607 (cu_->disable_opt & (1 << kSuppressExceptionEdges)) || is_throw || in_try_block;
Vladimir Markoe8ae8142014-07-08 18:06:45 +0100608 if (!in_try_block && build_all_edges) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100609 BasicBlock* eh_block = CreateNewBB(kExceptionHandling);
buzbee0d829482013-10-11 15:24:55 -0700610 cur_block->taken = eh_block->id;
buzbee311ca162013-02-28 15:56:43 -0800611 eh_block->start_offset = cur_offset;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100612 eh_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800613 }
614
buzbee17189ac2013-11-08 11:07:02 -0800615 if (is_throw) {
buzbee311ca162013-02-28 15:56:43 -0800616 cur_block->explicit_throw = true;
buzbee27247762013-07-28 12:03:58 -0700617 if (code_ptr < code_end) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700618 // Force creation of new block following THROW via side-effect.
buzbee311ca162013-02-28 15:56:43 -0800619 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
620 /* immed_pred_block_p */ NULL);
621 }
622 if (!in_try_block) {
623 // Don't split a THROW that can't rethrow - we're done.
624 return cur_block;
625 }
626 }
627
buzbee17189ac2013-11-08 11:07:02 -0800628 if (!build_all_edges) {
629 /*
630 * Even though there is an exception edge here, control cannot return to this
631 * method. Thus, for the purposes of dataflow analysis and optimization, we can
632 * ignore the edge. Doing this reduces compile time, and increases the scope
633 * of the basic-block level optimization pass.
634 */
635 return cur_block;
636 }
637
buzbee311ca162013-02-28 15:56:43 -0800638 /*
639 * Split the potentially-throwing instruction into two parts.
640 * The first half will be a pseudo-op that captures the exception
641 * edges and terminates the basic block. It always falls through.
642 * Then, create a new basic block that begins with the throwing instruction
643 * (minus exceptions). Note: this new basic block must NOT be entered into
644 * the block_map. If the potentially-throwing instruction is the target of a
645 * future branch, we need to find the check psuedo half. The new
646 * basic block containing the work portion of the instruction should
647 * only be entered via fallthrough from the block containing the
648 * pseudo exception edge MIR. Note also that this new block is
649 * not automatically terminated after the work portion, and may
650 * contain following instructions.
buzbeeb48819d2013-09-14 16:15:25 -0700651 *
652 * Note also that the dex_pc_to_block_map_ entry for the potentially
653 * throwing instruction will refer to the original basic block.
buzbee311ca162013-02-28 15:56:43 -0800654 */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100655 BasicBlock* new_block = CreateNewBB(kDalvikByteCode);
buzbee311ca162013-02-28 15:56:43 -0800656 new_block->start_offset = insn->offset;
buzbee0d829482013-10-11 15:24:55 -0700657 cur_block->fall_through = new_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100658 new_block->predecessors.push_back(cur_block->id);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700659 MIR* new_insn = NewMIR();
buzbee311ca162013-02-28 15:56:43 -0800660 *new_insn = *insn;
buzbee35ba7f32014-05-31 08:59:01 -0700661 insn->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpCheck);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700662 // Associate the two halves.
buzbee311ca162013-02-28 15:56:43 -0800663 insn->meta.throw_insn = new_insn;
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700664 new_block->AppendMIR(new_insn);
buzbee311ca162013-02-28 15:56:43 -0800665 return new_block;
666}
667
668/* Parse a Dex method and insert it into the MIRGraph at the current insert point. */
669void MIRGraph::InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700670 InvokeType invoke_type, uint16_t class_def_idx,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700671 uint32_t method_idx, jobject class_loader, const DexFile& dex_file) {
buzbee311ca162013-02-28 15:56:43 -0800672 current_code_item_ = code_item;
673 method_stack_.push_back(std::make_pair(current_method_, current_offset_));
674 current_method_ = m_units_.size();
675 current_offset_ = 0;
676 // TODO: will need to snapshot stack image and use that as the mir context identification.
677 m_units_.push_back(new DexCompilationUnit(cu_, class_loader, Runtime::Current()->GetClassLinker(),
Vladimir Marko2730db02014-01-27 11:15:17 +0000678 dex_file, current_code_item_, class_def_idx, method_idx, access_flags,
679 cu_->compiler_driver->GetVerifiedMethod(&dex_file, method_idx)));
buzbee311ca162013-02-28 15:56:43 -0800680 const uint16_t* code_ptr = current_code_item_->insns_;
681 const uint16_t* code_end =
682 current_code_item_->insns_ + current_code_item_->insns_size_in_code_units_;
683
684 // TODO: need to rework expansion of block list & try_block_addr when inlining activated.
buzbeeb48819d2013-09-14 16:15:25 -0700685 // TUNING: use better estimate of basic blocks for following resize.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100686 block_list_.reserve(block_list_.size() + current_code_item_->insns_size_in_code_units_);
687 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 -0700688
buzbee311ca162013-02-28 15:56:43 -0800689 // TODO: replace with explicit resize routine. Using automatic extension side effect for now.
buzbee862a7602013-04-05 10:58:54 -0700690 try_block_addr_->SetBit(current_code_item_->insns_size_in_code_units_);
691 try_block_addr_->ClearBit(current_code_item_->insns_size_in_code_units_);
buzbee311ca162013-02-28 15:56:43 -0800692
693 // If this is the first method, set up default entry and exit blocks.
694 if (current_method_ == 0) {
695 DCHECK(entry_block_ == NULL);
696 DCHECK(exit_block_ == NULL);
Andreas Gampe44395962014-06-13 13:44:40 -0700697 DCHECK_EQ(num_blocks_, 0U);
buzbee0d829482013-10-11 15:24:55 -0700698 // Use id 0 to represent a null block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100699 BasicBlock* null_block = CreateNewBB(kNullBlock);
buzbee0d829482013-10-11 15:24:55 -0700700 DCHECK_EQ(null_block->id, NullBasicBlockId);
701 null_block->hidden = true;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100702 entry_block_ = CreateNewBB(kEntryBlock);
703 exit_block_ = CreateNewBB(kExitBlock);
buzbee311ca162013-02-28 15:56:43 -0800704 // TODO: deprecate all "cu->" fields; move what's left to wherever CompilationUnit is allocated.
705 cu_->dex_file = &dex_file;
706 cu_->class_def_idx = class_def_idx;
707 cu_->method_idx = method_idx;
708 cu_->access_flags = access_flags;
709 cu_->invoke_type = invoke_type;
710 cu_->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
buzbee311ca162013-02-28 15:56:43 -0800711 } else {
712 UNIMPLEMENTED(FATAL) << "Nested inlining not implemented.";
713 /*
714 * Will need to manage storage for ins & outs, push prevous state and update
715 * insert point.
716 */
717 }
718
719 /* Current block to record parsed instructions */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100720 BasicBlock* cur_block = CreateNewBB(kDalvikByteCode);
buzbee0d829482013-10-11 15:24:55 -0700721 DCHECK_EQ(current_offset_, 0U);
buzbee311ca162013-02-28 15:56:43 -0800722 cur_block->start_offset = current_offset_;
buzbee0d829482013-10-11 15:24:55 -0700723 // TODO: for inlining support, insert at the insert point rather than entry block.
724 entry_block_->fall_through = cur_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100725 cur_block->predecessors.push_back(entry_block_->id);
buzbee311ca162013-02-28 15:56:43 -0800726
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000727 /* Identify code range in try blocks and set up the empty catch blocks */
buzbee311ca162013-02-28 15:56:43 -0800728 ProcessTryCatchBlocks();
729
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000730 uint64_t merged_df_flags = 0u;
731
buzbee311ca162013-02-28 15:56:43 -0800732 /* Parse all instructions and put them into containing basic blocks */
733 while (code_ptr < code_end) {
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700734 MIR *insn = NewMIR();
buzbee311ca162013-02-28 15:56:43 -0800735 insn->offset = current_offset_;
736 insn->m_unit_index = current_method_;
737 int width = ParseInsn(code_ptr, &insn->dalvikInsn);
buzbee311ca162013-02-28 15:56:43 -0800738 Instruction::Code opcode = insn->dalvikInsn.opcode;
739 if (opcode_count_ != NULL) {
740 opcode_count_[static_cast<int>(opcode)]++;
741 }
742
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -0700743 int flags = insn->dalvikInsn.FlagsOf();
buzbeeb1f1d642014-02-27 12:55:32 -0800744 int verify_flags = Instruction::VerifyFlagsOf(insn->dalvikInsn.opcode);
buzbee311ca162013-02-28 15:56:43 -0800745
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700746 uint64_t df_flags = GetDataFlowAttributes(insn);
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000747 merged_df_flags |= df_flags;
buzbee311ca162013-02-28 15:56:43 -0800748
749 if (df_flags & DF_HAS_DEFS) {
750 def_count_ += (df_flags & DF_A_WIDE) ? 2 : 1;
751 }
752
buzbee1da1e2f2013-11-15 13:37:01 -0800753 if (df_flags & DF_LVN) {
754 cur_block->use_lvn = true; // Run local value numbering on this basic block.
755 }
756
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700757 // Check for inline data block signatures.
buzbee27247762013-07-28 12:03:58 -0700758 if (opcode == Instruction::NOP) {
759 // A simple NOP will have a width of 1 at this point, embedded data NOP > 1.
760 if ((width == 1) && ((current_offset_ & 0x1) == 0x1) && ((code_end - code_ptr) > 1)) {
761 // Could be an aligning nop. If an embedded data NOP follows, treat pair as single unit.
762 uint16_t following_raw_instruction = code_ptr[1];
763 if ((following_raw_instruction == Instruction::kSparseSwitchSignature) ||
764 (following_raw_instruction == Instruction::kPackedSwitchSignature) ||
765 (following_raw_instruction == Instruction::kArrayDataSignature)) {
766 width += Instruction::At(code_ptr + 1)->SizeInCodeUnits();
767 }
768 }
769 if (width == 1) {
770 // It is a simple nop - treat normally.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700771 cur_block->AppendMIR(insn);
buzbee27247762013-07-28 12:03:58 -0700772 } else {
buzbee0d829482013-10-11 15:24:55 -0700773 DCHECK(cur_block->fall_through == NullBasicBlockId);
774 DCHECK(cur_block->taken == NullBasicBlockId);
buzbee27247762013-07-28 12:03:58 -0700775 // Unreachable instruction, mark for no continuation.
776 flags &= ~Instruction::kContinue;
777 }
778 } else {
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700779 cur_block->AppendMIR(insn);
buzbee27247762013-07-28 12:03:58 -0700780 }
781
buzbeeb48819d2013-09-14 16:15:25 -0700782 // Associate the starting dex_pc for this opcode with its containing basic block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100783 dex_pc_to_block_map_[insn->offset] = cur_block->id;
buzbeeb48819d2013-09-14 16:15:25 -0700784
buzbee27247762013-07-28 12:03:58 -0700785 code_ptr += width;
786
buzbee311ca162013-02-28 15:56:43 -0800787 if (flags & Instruction::kBranch) {
788 cur_block = ProcessCanBranch(cur_block, insn, current_offset_,
789 width, flags, code_ptr, code_end);
790 } else if (flags & Instruction::kReturn) {
791 cur_block->terminated_by_return = true;
buzbee0d829482013-10-11 15:24:55 -0700792 cur_block->fall_through = exit_block_->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100793 exit_block_->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800794 /*
795 * Terminate the current block if there are instructions
796 * afterwards.
797 */
798 if (code_ptr < code_end) {
799 /*
800 * Create a fallthrough block for real instructions
801 * (incl. NOP).
802 */
buzbee27247762013-07-28 12:03:58 -0700803 FindBlock(current_offset_ + width, /* split */ false, /* create */ true,
804 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800805 }
806 } else if (flags & Instruction::kThrow) {
807 cur_block = ProcessCanThrow(cur_block, insn, current_offset_, width, flags, try_block_addr_,
808 code_ptr, code_end);
809 } else if (flags & Instruction::kSwitch) {
buzbee17189ac2013-11-08 11:07:02 -0800810 cur_block = ProcessCanSwitch(cur_block, insn, current_offset_, width, flags);
buzbee311ca162013-02-28 15:56:43 -0800811 }
Alexei Zavjalov688e7c52014-07-16 02:17:58 +0700812 if (verify_flags & Instruction::kVerifyVarArgRange ||
813 verify_flags & Instruction::kVerifyVarArgRangeNonZero) {
buzbeeb1f1d642014-02-27 12:55:32 -0800814 /*
815 * The Quick backend's runtime model includes a gap between a method's
816 * argument ("in") vregs and the rest of its vregs. Handling a range instruction
817 * which spans the gap is somewhat complicated, and should not happen
818 * in normal usage of dx. Punt to the interpreter.
819 */
820 int first_reg_in_range = insn->dalvikInsn.vC;
821 int last_reg_in_range = first_reg_in_range + insn->dalvikInsn.vA - 1;
822 if (IsInVReg(first_reg_in_range) != IsInVReg(last_reg_in_range)) {
823 punt_to_interpreter_ = true;
824 }
825 }
buzbee311ca162013-02-28 15:56:43 -0800826 current_offset_ += width;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700827 BasicBlock* next_block = FindBlock(current_offset_, /* split */ false, /* create */
buzbee311ca162013-02-28 15:56:43 -0800828 false, /* immed_pred_block_p */ NULL);
829 if (next_block) {
830 /*
831 * The next instruction could be the target of a previously parsed
832 * forward branch so a block is already created. If the current
833 * instruction is not an unconditional branch, connect them through
834 * the fall-through link.
835 */
buzbee0d829482013-10-11 15:24:55 -0700836 DCHECK(cur_block->fall_through == NullBasicBlockId ||
837 GetBasicBlock(cur_block->fall_through) == next_block ||
838 GetBasicBlock(cur_block->fall_through) == exit_block_);
buzbee311ca162013-02-28 15:56:43 -0800839
buzbee0d829482013-10-11 15:24:55 -0700840 if ((cur_block->fall_through == NullBasicBlockId) && (flags & Instruction::kContinue)) {
841 cur_block->fall_through = next_block->id;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100842 next_block->predecessors.push_back(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800843 }
844 cur_block = next_block;
845 }
846 }
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000847 merged_df_flags_ = merged_df_flags;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000848
buzbee311ca162013-02-28 15:56:43 -0800849 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
850 DumpCFG("/sdcard/1_post_parse_cfg/", true);
851 }
852
853 if (cu_->verbose) {
buzbee1fd33462013-03-25 13:40:45 -0700854 DumpMIRGraph();
buzbee311ca162013-02-28 15:56:43 -0800855 }
856}
857
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700858void MIRGraph::ShowOpcodeStats() {
buzbee311ca162013-02-28 15:56:43 -0800859 DCHECK(opcode_count_ != NULL);
860 LOG(INFO) << "Opcode Count";
861 for (int i = 0; i < kNumPackedOpcodes; i++) {
862 if (opcode_count_[i] != 0) {
863 LOG(INFO) << "-C- " << Instruction::Name(static_cast<Instruction::Code>(i))
864 << " " << opcode_count_[i];
865 }
866 }
867}
868
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700869uint64_t MIRGraph::GetDataFlowAttributes(Instruction::Code opcode) {
870 DCHECK_LT((size_t) opcode, (sizeof(oat_data_flow_attributes_) / sizeof(oat_data_flow_attributes_[0])));
871 return oat_data_flow_attributes_[opcode];
872}
873
874uint64_t MIRGraph::GetDataFlowAttributes(MIR* mir) {
875 DCHECK(mir != nullptr);
876 Instruction::Code opcode = mir->dalvikInsn.opcode;
877 return GetDataFlowAttributes(opcode);
878}
879
buzbee311ca162013-02-28 15:56:43 -0800880// TODO: use a configurable base prefix, and adjust callers to supply pass name.
881/* Dump the CFG into a DOT graph */
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800882void MIRGraph::DumpCFG(const char* dir_prefix, bool all_blocks, const char *suffix) {
buzbee311ca162013-02-28 15:56:43 -0800883 FILE* file;
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700884 static AtomicInteger cnt(0);
885
886 // Increment counter to get a unique file number.
887 cnt++;
888
buzbee311ca162013-02-28 15:56:43 -0800889 std::string fname(PrettyMethod(cu_->method_idx, *cu_->dex_file));
890 ReplaceSpecialChars(fname);
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700891 fname = StringPrintf("%s%s%x%s_%d.dot", dir_prefix, fname.c_str(),
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800892 GetBasicBlock(GetEntryBlock()->fall_through)->start_offset,
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700893 suffix == nullptr ? "" : suffix,
894 cnt.LoadRelaxed());
buzbee311ca162013-02-28 15:56:43 -0800895 file = fopen(fname.c_str(), "w");
896 if (file == NULL) {
897 return;
898 }
899 fprintf(file, "digraph G {\n");
900
901 fprintf(file, " rankdir=TB\n");
902
903 int num_blocks = all_blocks ? GetNumBlocks() : num_reachable_blocks_;
904 int idx;
905
906 for (idx = 0; idx < num_blocks; idx++) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100907 int block_idx = all_blocks ? idx : dfs_order_[idx];
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700908 BasicBlock* bb = GetBasicBlock(block_idx);
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700909 if (bb == NULL) continue;
buzbee311ca162013-02-28 15:56:43 -0800910 if (bb->block_type == kDead) continue;
Jean Christophe Beylera8869e62014-06-19 09:34:51 -0700911 if (bb->hidden) continue;
buzbee311ca162013-02-28 15:56:43 -0800912 if (bb->block_type == kEntryBlock) {
913 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
914 } else if (bb->block_type == kExitBlock) {
915 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
916 } else if (bb->block_type == kDalvikByteCode) {
917 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
918 bb->start_offset, bb->id);
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700919 const MIR* mir;
buzbee311ca162013-02-28 15:56:43 -0800920 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
921 bb->first_mir_insn ? " | " : " ");
922 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
923 int opcode = mir->dalvikInsn.opcode;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -0700924 fprintf(file, " {%04x %s %s %s %s %s %s %s\\l}%s\\\n", mir->offset,
Mark Mendelld65c51a2014-04-29 16:55:20 -0400925 mir->ssa_rep ? GetDalvikDisassembly(mir) :
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700926 !MIR::DecodedInstruction::IsPseudoMirOp(opcode) ?
927 Instruction::Name(mir->dalvikInsn.opcode) :
Mark Mendelld65c51a2014-04-29 16:55:20 -0400928 extended_mir_op_names_[opcode - kMirOpFirst],
929 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
930 (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
Udayan Banerji60bfe7b2014-07-08 19:59:43 -0700931 (mir->optimization_flags & MIR_IGNORE_SUSPEND_CHECK) != 0 ? " no_suspendcheck" : " ",
Jean Christophe Beylerb5bce7c2014-07-25 12:32:18 -0700932 (mir->optimization_flags & MIR_STORE_NON_TEMPORAL) != 0 ? " non_temporal" : " ",
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -0700933 (mir->optimization_flags & MIR_CALLEE) != 0 ? " inlined" : " ",
934 (mir->optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0 ? " no_clinit" : " ",
Mark Mendelld65c51a2014-04-29 16:55:20 -0400935 mir->next ? " | " : " ");
buzbee311ca162013-02-28 15:56:43 -0800936 }
937 fprintf(file, " }\"];\n\n");
938 } else if (bb->block_type == kExceptionHandling) {
939 char block_name[BLOCK_NAME_LEN];
940
941 GetBlockName(bb, block_name);
942 fprintf(file, " %s [shape=invhouse];\n", block_name);
943 }
944
945 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
946
buzbee0d829482013-10-11 15:24:55 -0700947 if (bb->taken != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800948 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700949 GetBlockName(GetBasicBlock(bb->taken), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800950 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
951 block_name1, block_name2);
952 }
buzbee0d829482013-10-11 15:24:55 -0700953 if (bb->fall_through != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800954 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700955 GetBlockName(GetBasicBlock(bb->fall_through), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800956 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
957 }
958
buzbee0d829482013-10-11 15:24:55 -0700959 if (bb->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800960 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
961 bb->start_offset, bb->id,
buzbee0d829482013-10-11 15:24:55 -0700962 (bb->successor_block_list_type == kCatch) ? "Mrecord" : "record");
buzbee311ca162013-02-28 15:56:43 -0800963
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100964 int last_succ_id = static_cast<int>(bb->successor_blocks.size() - 1u);
buzbee311ca162013-02-28 15:56:43 -0800965 int succ_id = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100966 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -0700967 BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800968 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100969 succ_id,
buzbee311ca162013-02-28 15:56:43 -0800970 successor_block_info->key,
971 dest_block->start_offset,
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100972 (succ_id != last_succ_id) ? " | " : " ");
973 ++succ_id;
buzbee311ca162013-02-28 15:56:43 -0800974 }
975 fprintf(file, " }\"];\n\n");
976
977 GetBlockName(bb, block_name1);
978 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
979 block_name1, bb->start_offset, bb->id);
980
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700981 // Link the successor pseudo-block with all of its potential targets.
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700982 succ_id = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100983 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700984 BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800985
Razvan A Lupusoru25bc2792014-03-19 14:27:59 -0700986 GetBlockName(dest_block, block_name2);
987 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
988 bb->id, succ_id++, block_name2);
buzbee311ca162013-02-28 15:56:43 -0800989 }
990 }
991 fprintf(file, "\n");
992
993 if (cu_->verbose) {
994 /* Display the dominator tree */
995 GetBlockName(bb, block_name1);
996 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
997 block_name1, block_name1);
998 if (bb->i_dom) {
buzbee0d829482013-10-11 15:24:55 -0700999 GetBlockName(GetBasicBlock(bb->i_dom), block_name2);
buzbee311ca162013-02-28 15:56:43 -08001000 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
1001 }
1002 }
1003 }
1004 fprintf(file, "}\n");
1005 fclose(file);
1006}
1007
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001008/* Insert an MIR instruction to the end of a basic block. */
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001009void BasicBlock::AppendMIR(MIR* mir) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001010 // Insert it after the last MIR.
1011 InsertMIRListAfter(last_mir_insn, mir, mir);
buzbee1fd33462013-03-25 13:40:45 -07001012}
1013
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001014void BasicBlock::AppendMIRList(MIR* first_list_mir, MIR* last_list_mir) {
1015 // Insert it after the last MIR.
1016 InsertMIRListAfter(last_mir_insn, first_list_mir, last_list_mir);
1017}
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001018
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001019void BasicBlock::AppendMIRList(const std::vector<MIR*>& insns) {
1020 for (std::vector<MIR*>::const_iterator it = insns.begin(); it != insns.end(); it++) {
1021 MIR* new_mir = *it;
1022
1023 // Add a copy of each MIR.
1024 InsertMIRListAfter(last_mir_insn, new_mir, new_mir);
1025 }
buzbee1fd33462013-03-25 13:40:45 -07001026}
1027
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001028/* Insert a MIR instruction after the specified MIR. */
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001029void BasicBlock::InsertMIRAfter(MIR* current_mir, MIR* new_mir) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001030 InsertMIRListAfter(current_mir, new_mir, new_mir);
1031}
buzbee1fd33462013-03-25 13:40:45 -07001032
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001033void BasicBlock::InsertMIRListAfter(MIR* insert_after, MIR* first_list_mir, MIR* last_list_mir) {
1034 // If no MIR, we are done.
1035 if (first_list_mir == nullptr || last_list_mir == nullptr) {
1036 return;
buzbee1fd33462013-03-25 13:40:45 -07001037 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001038
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001039 // If insert_after is null, assume BB is empty.
1040 if (insert_after == nullptr) {
1041 first_mir_insn = first_list_mir;
1042 last_mir_insn = last_list_mir;
1043 last_list_mir->next = nullptr;
1044 } else {
1045 MIR* after_list = insert_after->next;
1046 insert_after->next = first_list_mir;
1047 last_list_mir->next = after_list;
1048 if (after_list == nullptr) {
1049 last_mir_insn = last_list_mir;
1050 }
1051 }
1052
1053 // Set this BB to be the basic block of the MIRs.
1054 MIR* last = last_list_mir->next;
1055 for (MIR* mir = first_list_mir; mir != last; mir = mir->next) {
1056 mir->bb = id;
1057 }
1058}
1059
1060/* Insert an MIR instruction to the head of a basic block. */
1061void BasicBlock::PrependMIR(MIR* mir) {
1062 InsertMIRListBefore(first_mir_insn, mir, mir);
1063}
1064
1065void BasicBlock::PrependMIRList(MIR* first_list_mir, MIR* last_list_mir) {
1066 // Insert it before the first MIR.
1067 InsertMIRListBefore(first_mir_insn, first_list_mir, last_list_mir);
1068}
1069
1070void BasicBlock::PrependMIRList(const std::vector<MIR*>& to_add) {
1071 for (std::vector<MIR*>::const_iterator it = to_add.begin(); it != to_add.end(); it++) {
1072 MIR* mir = *it;
1073
1074 InsertMIRListBefore(first_mir_insn, mir, mir);
1075 }
1076}
1077
1078/* Insert a MIR instruction before the specified MIR. */
1079void BasicBlock::InsertMIRBefore(MIR* current_mir, MIR* new_mir) {
1080 // Insert as a single element list.
1081 return InsertMIRListBefore(current_mir, new_mir, new_mir);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001082}
1083
1084MIR* BasicBlock::FindPreviousMIR(MIR* mir) {
1085 MIR* current = first_mir_insn;
1086
1087 while (current != nullptr) {
1088 MIR* next = current->next;
1089
1090 if (next == mir) {
1091 return current;
1092 }
1093
1094 current = next;
1095 }
1096
1097 return nullptr;
1098}
1099
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001100void BasicBlock::InsertMIRListBefore(MIR* insert_before, MIR* first_list_mir, MIR* last_list_mir) {
1101 // If no MIR, we are done.
1102 if (first_list_mir == nullptr || last_list_mir == nullptr) {
1103 return;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001104 }
1105
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001106 // If insert_before is null, assume BB is empty.
1107 if (insert_before == nullptr) {
1108 first_mir_insn = first_list_mir;
1109 last_mir_insn = last_list_mir;
1110 last_list_mir->next = nullptr;
1111 } else {
1112 if (first_mir_insn == insert_before) {
1113 last_list_mir->next = first_mir_insn;
1114 first_mir_insn = first_list_mir;
1115 } else {
1116 // Find the preceding MIR.
1117 MIR* before_list = FindPreviousMIR(insert_before);
1118 DCHECK(before_list != nullptr);
1119 before_list->next = first_list_mir;
1120 last_list_mir->next = insert_before;
1121 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001122 }
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001123
1124 // Set this BB to be the basic block of the MIRs.
1125 for (MIR* mir = first_list_mir; mir != last_list_mir->next; mir = mir->next) {
1126 mir->bb = id;
1127 }
1128}
1129
1130bool BasicBlock::RemoveMIR(MIR* mir) {
1131 // Remove as a single element list.
1132 return RemoveMIRList(mir, mir);
1133}
1134
1135bool BasicBlock::RemoveMIRList(MIR* first_list_mir, MIR* last_list_mir) {
1136 if (first_list_mir == nullptr) {
1137 return false;
1138 }
1139
1140 // Try to find the MIR.
1141 MIR* before_list = nullptr;
1142 MIR* after_list = nullptr;
1143
1144 // If we are removing from the beginning of the MIR list.
1145 if (first_mir_insn == first_list_mir) {
1146 before_list = nullptr;
1147 } else {
1148 before_list = FindPreviousMIR(first_list_mir);
1149 if (before_list == nullptr) {
1150 // We did not find the mir.
1151 return false;
1152 }
1153 }
1154
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001155 // Remove the BB information and also find the after_list.
Chao-ying Fu590c6a42014-09-23 14:54:32 -07001156 for (MIR* mir = first_list_mir; mir != last_list_mir->next; mir = mir->next) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001157 mir->bb = NullBasicBlockId;
1158 }
1159
1160 after_list = last_list_mir->next;
1161
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001162 // If there is nothing before the list, after_list is the first_mir.
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001163 if (before_list == nullptr) {
1164 first_mir_insn = after_list;
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001165 } else {
1166 before_list->next = after_list;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001167 }
1168
Jean Christophe Beylera4307ac2014-06-02 09:04:32 -07001169 // If there is nothing after the list, before_list is last_mir.
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001170 if (after_list == nullptr) {
1171 last_mir_insn = before_list;
1172 }
1173
1174 return true;
buzbee1fd33462013-03-25 13:40:45 -07001175}
1176
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001177MIR* BasicBlock::GetNextUnconditionalMir(MIRGraph* mir_graph, MIR* current) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001178 MIR* next_mir = nullptr;
1179
1180 if (current != nullptr) {
1181 next_mir = current->next;
1182 }
1183
1184 if (next_mir == nullptr) {
1185 // Only look for next MIR that follows unconditionally.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -07001186 if ((taken == NullBasicBlockId) && (fall_through != NullBasicBlockId)) {
1187 next_mir = mir_graph->GetBasicBlock(fall_through)->first_mir_insn;
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -08001188 }
1189 }
1190
1191 return next_mir;
1192}
1193
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001194static void FillTypeSizeString(uint32_t type_size, std::string* decoded_mir) {
1195 DCHECK(decoded_mir != nullptr);
1196 OpSize type = static_cast<OpSize>(type_size >> 16);
1197 uint16_t vect_size = (type_size & 0xFFFF);
1198
1199 // Now print the type and vector size.
1200 std::stringstream ss;
1201 ss << " (type:";
1202 ss << type;
1203 ss << " vectsize:";
1204 ss << vect_size;
1205 ss << ")";
1206
1207 decoded_mir->append(ss.str());
1208}
1209
1210void MIRGraph::DisassembleExtendedInstr(const MIR* mir, std::string* decoded_mir) {
1211 DCHECK(decoded_mir != nullptr);
1212 int opcode = mir->dalvikInsn.opcode;
1213 SSARepresentation* ssa_rep = mir->ssa_rep;
1214 int defs = (ssa_rep != nullptr) ? ssa_rep->num_defs : 0;
1215 int uses = (ssa_rep != nullptr) ? ssa_rep->num_uses : 0;
1216
1217 decoded_mir->append(extended_mir_op_names_[opcode - kMirOpFirst]);
1218
1219 switch (opcode) {
1220 case kMirOpPhi: {
1221 if (defs > 0 && uses > 0) {
1222 BasicBlockId* incoming = mir->meta.phi_incoming;
1223 decoded_mir->append(StringPrintf(" %s = (%s",
1224 GetSSANameWithConst(ssa_rep->defs[0], true).c_str(),
1225 GetSSANameWithConst(ssa_rep->uses[0], true).c_str()));
1226 decoded_mir->append(StringPrintf(":%d", incoming[0]));
1227 for (int i = 1; i < uses; i++) {
1228 decoded_mir->append(StringPrintf(", %s:%d", GetSSANameWithConst(ssa_rep->uses[i], true).c_str(), incoming[i]));
1229 }
1230 decoded_mir->append(")");
1231 }
1232 break;
1233 }
1234 case kMirOpCopy:
1235 if (ssa_rep != nullptr) {
1236 decoded_mir->append(" ");
1237 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
1238 if (defs > 1) {
1239 decoded_mir->append(", ");
1240 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
1241 }
1242 decoded_mir->append(" = ");
1243 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[0], false));
1244 if (uses > 1) {
1245 decoded_mir->append(", ");
1246 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
1247 }
1248 } else {
1249 decoded_mir->append(StringPrintf(" v%d = v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1250 }
1251 break;
1252 case kMirOpFusedCmplFloat:
1253 case kMirOpFusedCmpgFloat:
1254 case kMirOpFusedCmplDouble:
1255 case kMirOpFusedCmpgDouble:
1256 case kMirOpFusedCmpLong:
1257 if (ssa_rep != nullptr) {
1258 decoded_mir->append(" ");
1259 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[0], false));
1260 for (int i = 1; i < uses; i++) {
1261 decoded_mir->append(", ");
1262 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[i], false));
1263 }
1264 } else {
1265 decoded_mir->append(StringPrintf(" v%d, v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1266 }
1267 break;
1268 case kMirOpMoveVector:
1269 decoded_mir->append(StringPrintf(" vect%d = vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1270 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1271 break;
1272 case kMirOpPackedAddition:
1273 decoded_mir->append(StringPrintf(" vect%d = vect%d + vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1274 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1275 break;
1276 case kMirOpPackedMultiply:
1277 decoded_mir->append(StringPrintf(" vect%d = vect%d * vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1278 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1279 break;
1280 case kMirOpPackedSubtract:
1281 decoded_mir->append(StringPrintf(" vect%d = vect%d - vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1282 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1283 break;
1284 case kMirOpPackedAnd:
1285 decoded_mir->append(StringPrintf(" vect%d = vect%d & vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1286 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1287 break;
1288 case kMirOpPackedOr:
1289 decoded_mir->append(StringPrintf(" vect%d = vect%d \\| vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1290 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1291 break;
1292 case kMirOpPackedXor:
1293 decoded_mir->append(StringPrintf(" vect%d = vect%d ^ vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1294 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1295 break;
1296 case kMirOpPackedShiftLeft:
1297 decoded_mir->append(StringPrintf(" vect%d = vect%d \\<\\< %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1298 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1299 break;
1300 case kMirOpPackedUnsignedShiftRight:
1301 decoded_mir->append(StringPrintf(" vect%d = vect%d \\>\\>\\> %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1302 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1303 break;
1304 case kMirOpPackedSignedShiftRight:
1305 decoded_mir->append(StringPrintf(" vect%d = vect%d \\>\\> %d", mir->dalvikInsn.vA, mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1306 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1307 break;
1308 case kMirOpConstVector:
1309 decoded_mir->append(StringPrintf(" vect%d = %x, %x, %x, %x", mir->dalvikInsn.vA, mir->dalvikInsn.arg[0],
1310 mir->dalvikInsn.arg[1], mir->dalvikInsn.arg[2], mir->dalvikInsn.arg[3]));
1311 break;
1312 case kMirOpPackedSet:
1313 if (ssa_rep != nullptr) {
1314 decoded_mir->append(StringPrintf(" vect%d = %s", mir->dalvikInsn.vA,
1315 GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
1316 if (uses > 1) {
1317 decoded_mir->append(", ");
1318 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
1319 }
1320 } else {
1321 decoded_mir->append(StringPrintf(" vect%d = v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1322 }
1323 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1324 break;
1325 case kMirOpPackedAddReduce:
1326 if (ssa_rep != nullptr) {
1327 decoded_mir->append(" ");
1328 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
1329 if (defs > 1) {
1330 decoded_mir->append(", ");
1331 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
1332 }
1333 decoded_mir->append(StringPrintf(" = vect%d + %s", mir->dalvikInsn.vB,
1334 GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
1335 if (uses > 1) {
1336 decoded_mir->append(", ");
1337 decoded_mir->append(GetSSANameWithConst(ssa_rep->uses[1], false));
1338 }
1339 } else {
1340 decoded_mir->append(StringPrintf("v%d = vect%d + v%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB, mir->dalvikInsn.vA));
1341 }
1342 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1343 break;
1344 case kMirOpPackedReduce:
1345 if (ssa_rep != nullptr) {
1346 decoded_mir->append(" ");
1347 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[0], false));
1348 if (defs > 1) {
1349 decoded_mir->append(", ");
1350 decoded_mir->append(GetSSANameWithConst(ssa_rep->defs[1], false));
1351 }
1352 decoded_mir->append(StringPrintf(" = vect%d", mir->dalvikInsn.vB));
1353 } else {
1354 decoded_mir->append(StringPrintf(" v%d = vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1355 }
1356 FillTypeSizeString(mir->dalvikInsn.vC, decoded_mir);
1357 break;
1358 case kMirOpReserveVectorRegisters:
1359 case kMirOpReturnVectorRegisters:
1360 decoded_mir->append(StringPrintf(" vect%d - vect%d", mir->dalvikInsn.vA, mir->dalvikInsn.vB));
1361 break;
1362 case kMirOpMemBarrier: {
1363 decoded_mir->append(" type:");
1364 std::stringstream ss;
1365 ss << static_cast<MemBarrierKind>(mir->dalvikInsn.vA);
1366 decoded_mir->append(ss.str());
1367 break;
1368 }
Lupusoru, Razvan Ab3a84e22014-07-28 14:11:01 -07001369 case kMirOpPackedArrayGet:
1370 case kMirOpPackedArrayPut:
1371 decoded_mir->append(StringPrintf(" vect%d", mir->dalvikInsn.vA));
1372 if (ssa_rep != nullptr) {
1373 decoded_mir->append(StringPrintf(", %s[%s]",
1374 GetSSANameWithConst(ssa_rep->uses[0], false).c_str(),
1375 GetSSANameWithConst(ssa_rep->uses[1], false).c_str()));
1376 } else {
1377 decoded_mir->append(StringPrintf(", v%d[v%d]", mir->dalvikInsn.vB, mir->dalvikInsn.vC));
1378 }
1379 FillTypeSizeString(mir->dalvikInsn.arg[0], decoded_mir);
1380 break;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001381 default:
1382 break;
1383 }
1384}
1385
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001386char* MIRGraph::GetDalvikDisassembly(const MIR* mir) {
Ian Rogers29a26482014-05-02 15:27:29 -07001387 MIR::DecodedInstruction insn = mir->dalvikInsn;
buzbee1fd33462013-03-25 13:40:45 -07001388 std::string str;
1389 int flags = 0;
1390 int opcode = insn.opcode;
1391 char* ret;
1392 bool nop = false;
1393 SSARepresentation* ssa_rep = mir->ssa_rep;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001394 Instruction::Format dalvik_format = Instruction::k10x; // Default to no-operand format.
buzbee1fd33462013-03-25 13:40:45 -07001395
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001396 // Handle special cases that recover the original dalvik instruction.
buzbee1fd33462013-03-25 13:40:45 -07001397 if ((opcode == kMirOpCheck) || (opcode == kMirOpCheckPart2)) {
1398 str.append(extended_mir_op_names_[opcode - kMirOpFirst]);
1399 str.append(": ");
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001400 // Recover the original Dex instruction.
buzbee1fd33462013-03-25 13:40:45 -07001401 insn = mir->meta.throw_insn->dalvikInsn;
1402 ssa_rep = mir->meta.throw_insn->ssa_rep;
buzbee1fd33462013-03-25 13:40:45 -07001403 opcode = insn.opcode;
1404 } else if (opcode == kMirOpNop) {
1405 str.append("[");
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001406 if (mir->offset < current_code_item_->insns_size_in_code_units_) {
1407 // Recover original opcode.
1408 insn.opcode = Instruction::At(current_code_item_->insns_ + mir->offset)->Opcode();
1409 opcode = insn.opcode;
1410 }
buzbee1fd33462013-03-25 13:40:45 -07001411 nop = true;
1412 }
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001413 int defs = (ssa_rep != NULL) ? ssa_rep->num_defs : 0;
1414 int uses = (ssa_rep != NULL) ? ssa_rep->num_uses : 0;
buzbee1fd33462013-03-25 13:40:45 -07001415
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -07001416 if (MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001417 // Note that this does not check the MIR's opcode in all cases. In cases where it
1418 // recovered dalvik instruction, it uses opcode of that instead of the extended one.
1419 DisassembleExtendedInstr(mir, &str);
buzbee1fd33462013-03-25 13:40:45 -07001420 } else {
1421 dalvik_format = Instruction::FormatOf(insn.opcode);
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07001422 flags = insn.FlagsOf();
buzbee1fd33462013-03-25 13:40:45 -07001423 str.append(Instruction::Name(insn.opcode));
buzbee1fd33462013-03-25 13:40:45 -07001424
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001425 // For invokes-style formats, treat wide regs as a pair of singles.
buzbee1fd33462013-03-25 13:40:45 -07001426 bool show_singles = ((dalvik_format == Instruction::k35c) ||
1427 (dalvik_format == Instruction::k3rc));
1428 if (defs != 0) {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001429 str.append(" ");
1430 str.append(GetSSANameWithConst(ssa_rep->defs[0], false));
1431 if (defs > 1) {
1432 str.append(", ");
1433 str.append(GetSSANameWithConst(ssa_rep->defs[1], false));
1434 }
buzbee1fd33462013-03-25 13:40:45 -07001435 if (uses != 0) {
1436 str.append(", ");
1437 }
1438 }
1439 for (int i = 0; i < uses; i++) {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001440 str.append(" ");
1441 str.append(GetSSANameWithConst(ssa_rep->uses[i], show_singles));
buzbee1fd33462013-03-25 13:40:45 -07001442 if (!show_singles && (reg_location_ != NULL) && reg_location_[i].wide) {
1443 // For the listing, skip the high sreg.
1444 i++;
1445 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001446 if (i != (uses - 1)) {
buzbee1fd33462013-03-25 13:40:45 -07001447 str.append(",");
1448 }
1449 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001450
buzbee1fd33462013-03-25 13:40:45 -07001451 switch (dalvik_format) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001452 case Instruction::k11n: // Add one immediate from vB.
buzbee1fd33462013-03-25 13:40:45 -07001453 case Instruction::k21s:
1454 case Instruction::k31i:
1455 case Instruction::k21h:
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001456 str.append(StringPrintf(", #0x%x", insn.vB));
buzbee1fd33462013-03-25 13:40:45 -07001457 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001458 case Instruction::k51l: // Add one wide immediate.
Ian Rogers23b03b52014-01-24 11:10:03 -08001459 str.append(StringPrintf(", #%" PRId64, insn.vB_wide));
buzbee1fd33462013-03-25 13:40:45 -07001460 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001461 case Instruction::k21c: // One register, one string/type/method index.
buzbee1fd33462013-03-25 13:40:45 -07001462 case Instruction::k31c:
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001463 str.append(StringPrintf(", index #0x%x", insn.vB));
buzbee1fd33462013-03-25 13:40:45 -07001464 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001465 case Instruction::k22c: // Two registers, one string/type/method index.
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001466 str.append(StringPrintf(", index #0x%x", insn.vC));
buzbee1fd33462013-03-25 13:40:45 -07001467 break;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001468 case Instruction::k22s: // Add one immediate from vC.
buzbee1fd33462013-03-25 13:40:45 -07001469 case Instruction::k22b:
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001470 str.append(StringPrintf(", #0x%x", insn.vC));
buzbee1fd33462013-03-25 13:40:45 -07001471 break;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001472 default:
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001473 // Nothing left to print.
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001474 break;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001475 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001476
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001477 if ((flags & Instruction::kBranch) != 0) {
1478 // For branches, decode the instructions to print out the branch targets.
1479 int offset = 0;
1480 switch (dalvik_format) {
1481 case Instruction::k21t:
1482 offset = insn.vB;
1483 break;
1484 case Instruction::k22t:
1485 offset = insn.vC;
1486 break;
1487 case Instruction::k10t:
1488 case Instruction::k20t:
1489 case Instruction::k30t:
1490 offset = insn.vA;
1491 break;
1492 default:
1493 LOG(FATAL) << "Unexpected branch format " << dalvik_format << " from " << insn.opcode;
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001494 break;
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001495 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001496 str.append(StringPrintf(", 0x%x (%c%x)", mir->offset + offset,
Serguei Katkov0f3e4982014-08-21 16:43:54 +07001497 offset > 0 ? '+' : '-', offset > 0 ? offset : -offset));
1498 }
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001499
1500 if (nop) {
1501 str.append("]--optimized away");
1502 }
buzbee1fd33462013-03-25 13:40:45 -07001503 }
1504 int length = str.length() + 1;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001505 ret = static_cast<char*>(arena_->Alloc(length, kArenaAllocDFInfo));
buzbee1fd33462013-03-25 13:40:45 -07001506 strncpy(ret, str.c_str(), length);
1507 return ret;
1508}
1509
1510/* Turn method name into a legal Linux file name */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001511void MIRGraph::ReplaceSpecialChars(std::string& str) {
Brian Carlstrom9b7085a2013-07-18 15:15:21 -07001512 static const struct { const char before; const char after; } match[] = {
1513 {'/', '-'}, {';', '#'}, {' ', '#'}, {'$', '+'},
1514 {'(', '@'}, {')', '@'}, {'<', '='}, {'>', '='}
1515 };
buzbee1fd33462013-03-25 13:40:45 -07001516 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
1517 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
1518 }
1519}
1520
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001521std::string MIRGraph::GetSSAName(int ssa_reg) {
Ian Rogers39ebcb82013-05-30 16:57:23 -07001522 // TODO: This value is needed for LLVM and debugging. Currently, we compute this and then copy to
1523 // the arena. We should be smarter and just place straight into the arena, or compute the
1524 // value more lazily.
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001525 int vreg = SRegToVReg(ssa_reg);
1526 if (vreg >= static_cast<int>(GetFirstTempVR())) {
1527 return StringPrintf("t%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1528 } else {
1529 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1530 }
buzbee1fd33462013-03-25 13:40:45 -07001531}
1532
1533// Similar to GetSSAName, but if ssa name represents an immediate show that as well.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001534std::string MIRGraph::GetSSANameWithConst(int ssa_reg, bool singles_only) {
buzbee1fd33462013-03-25 13:40:45 -07001535 if (reg_location_ == NULL) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001536 // Pre-SSA - just use the standard name.
buzbee1fd33462013-03-25 13:40:45 -07001537 return GetSSAName(ssa_reg);
1538 }
1539 if (IsConst(reg_location_[ssa_reg])) {
Mark Mendell9944b3b2014-10-06 10:58:54 -04001540 if (!singles_only && reg_location_[ssa_reg].wide &&
1541 !reg_location_[ssa_reg].high_word) {
Ian Rogers23b03b52014-01-24 11:10:03 -08001542 return StringPrintf("v%d_%d#0x%" PRIx64, SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001543 ConstantValueWide(reg_location_[ssa_reg]));
1544 } else {
Brian Carlstromb1eba212013-07-17 18:07:19 -07001545 return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001546 ConstantValue(reg_location_[ssa_reg]));
1547 }
1548 } else {
Razvan A Lupusoru1500e6f2014-08-22 15:39:50 -07001549 int vreg = SRegToVReg(ssa_reg);
1550 if (vreg >= static_cast<int>(GetFirstTempVR())) {
1551 return StringPrintf("t%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1552 } else {
1553 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1554 }
buzbee1fd33462013-03-25 13:40:45 -07001555 }
1556}
1557
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001558void MIRGraph::GetBlockName(BasicBlock* bb, char* name) {
buzbee1fd33462013-03-25 13:40:45 -07001559 switch (bb->block_type) {
1560 case kEntryBlock:
1561 snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id);
1562 break;
1563 case kExitBlock:
1564 snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id);
1565 break;
1566 case kDalvikByteCode:
1567 snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id);
1568 break;
1569 case kExceptionHandling:
1570 snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset,
1571 bb->id);
1572 break;
1573 default:
1574 snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id);
1575 break;
1576 }
1577}
1578
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001579const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) {
buzbee0d829482013-10-11 15:24:55 -07001580 // TODO: for inlining support, use current code unit.
buzbee1fd33462013-03-25 13:40:45 -07001581 const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx);
1582 return cu_->dex_file->GetShorty(method_id.proto_idx_);
1583}
1584
1585/* Debug Utility - dump a compilation unit */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001586void MIRGraph::DumpMIRGraph() {
buzbee1fd33462013-03-25 13:40:45 -07001587 const char* block_type_names[] = {
buzbee17189ac2013-11-08 11:07:02 -08001588 "Null Block",
buzbee1fd33462013-03-25 13:40:45 -07001589 "Entry Block",
1590 "Code Block",
1591 "Exit Block",
1592 "Exception Handling",
1593 "Catch Block"
1594 };
1595
1596 LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001597 LOG(INFO) << GetInsns(0) << " insns";
buzbee1fd33462013-03-25 13:40:45 -07001598 LOG(INFO) << GetNumBlocks() << " blocks in total";
buzbee1fd33462013-03-25 13:40:45 -07001599
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001600 for (BasicBlock* bb : block_list_) {
buzbee1fd33462013-03-25 13:40:45 -07001601 LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)",
1602 bb->id,
1603 block_type_names[bb->block_type],
1604 bb->start_offset,
1605 bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset,
1606 bb->last_mir_insn ? "" : " empty");
buzbee0d829482013-10-11 15:24:55 -07001607 if (bb->taken != NullBasicBlockId) {
1608 LOG(INFO) << " Taken branch: block " << bb->taken
1609 << "(0x" << std::hex << GetBasicBlock(bb->taken)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001610 }
buzbee0d829482013-10-11 15:24:55 -07001611 if (bb->fall_through != NullBasicBlockId) {
1612 LOG(INFO) << " Fallthrough : block " << bb->fall_through
1613 << " (0x" << std::hex << GetBasicBlock(bb->fall_through)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001614 }
1615 }
1616}
1617
1618/*
1619 * Build an array of location records for the incoming arguments.
1620 * Note: one location record per word of arguments, with dummy
1621 * high-word loc for wide arguments. Also pull up any following
1622 * MOVE_RESULT and incorporate it into the invoke.
1623 */
1624CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001625 bool is_range) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001626 CallInfo* info = static_cast<CallInfo*>(arena_->Alloc(sizeof(CallInfo),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001627 kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001628 MIR* move_result_mir = FindMoveResult(bb, mir);
1629 if (move_result_mir == NULL) {
1630 info->result.location = kLocInvalid;
1631 } else {
1632 info->result = GetRawDest(move_result_mir);
buzbee1fd33462013-03-25 13:40:45 -07001633 move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1634 }
1635 info->num_arg_words = mir->ssa_rep->num_uses;
1636 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001637 (arena_->Alloc(sizeof(RegLocation) * info->num_arg_words, kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001638 for (int i = 0; i < info->num_arg_words; i++) {
1639 info->args[i] = GetRawSrc(mir, i);
1640 }
1641 info->opt_flags = mir->optimization_flags;
1642 info->type = type;
1643 info->is_range = is_range;
1644 info->index = mir->dalvikInsn.vB;
1645 info->offset = mir->offset;
Vladimir Markof096aad2014-01-23 15:51:58 +00001646 info->mir = mir;
buzbee1fd33462013-03-25 13:40:45 -07001647 return info;
1648}
1649
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07001650// Allocate a new MIR.
1651MIR* MIRGraph::NewMIR() {
1652 MIR* mir = new (arena_) MIR();
1653 return mir;
1654}
1655
buzbee862a7602013-04-05 10:58:54 -07001656// Allocate a new basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001657BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001658 BasicBlock* bb = new (arena_) BasicBlock(block_id, block_type, arena_);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001659
buzbee862a7602013-04-05 10:58:54 -07001660 // TUNING: better estimate of the exit block predecessors?
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001661 bb->predecessors.reserve((block_type == kExitBlock) ? 2048 : 2);
buzbee862a7602013-04-05 10:58:54 -07001662 block_id_map_.Put(block_id, block_id);
1663 return bb;
1664}
buzbee1fd33462013-03-25 13:40:45 -07001665
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001666void MIRGraph::InitializeConstantPropagation() {
1667 is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001668 constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(), kArenaAllocDFInfo));
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001669}
1670
1671void MIRGraph::InitializeMethodUses() {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07001672 // The gate starts by initializing the use counts.
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001673 int num_ssa_regs = GetNumSSARegs();
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001674 use_counts_.clear();
1675 use_counts_.reserve(num_ssa_regs + 32);
1676 use_counts_.resize(num_ssa_regs, 0u);
1677 raw_use_counts_.clear();
1678 raw_use_counts_.reserve(num_ssa_regs + 32);
1679 raw_use_counts_.resize(num_ssa_regs, 0u);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001680}
1681
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001682void MIRGraph::SSATransformationStart() {
1683 DCHECK(temp_scoped_alloc_.get() == nullptr);
1684 temp_scoped_alloc_.reset(ScopedArenaAllocator::Create(&cu_->arena_stack));
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -07001685 temp_bit_vector_size_ = GetNumOfCodeAndTempVRs();
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001686 temp_bit_vector_ = new (temp_scoped_alloc_.get()) ArenaBitVector(
1687 temp_scoped_alloc_.get(), temp_bit_vector_size_, false, kBitMapRegisterV);
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001688}
1689
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001690void MIRGraph::SSATransformationEnd() {
1691 // Verify the dataflow information after the pass.
1692 if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
1693 VerifyDataflow();
1694 }
1695
1696 temp_bit_vector_size_ = 0u;
1697 temp_bit_vector_ = nullptr;
1698 DCHECK(temp_scoped_alloc_.get() != nullptr);
1699 temp_scoped_alloc_.reset();
Johnny Qiuc029c982014-06-04 07:23:49 +00001700
1701 // Update the maximum number of reachable blocks.
1702 max_num_reachable_blocks_ = num_reachable_blocks_;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +01001703}
1704
Razvan A Lupusoru75035972014-09-11 15:24:59 -07001705size_t MIRGraph::GetNumDalvikInsns() const {
1706 size_t cumulative_size = 0u;
1707 bool counted_current_item = false;
1708 const uint8_t size_for_null_code_item = 2u;
1709
1710 for (auto it : m_units_) {
1711 const DexFile::CodeItem* code_item = it->GetCodeItem();
1712 // Even if the code item is null, we still count non-zero value so that
1713 // each m_unit is counted as having impact.
1714 cumulative_size += (code_item == nullptr ?
1715 size_for_null_code_item : code_item->insns_size_in_code_units_);
1716 if (code_item == current_code_item_) {
1717 counted_current_item = true;
1718 }
1719 }
1720
1721 // If the current code item was not counted yet, count it now.
1722 // This can happen for example in unit tests where some fields like m_units_
1723 // are not initialized.
1724 if (counted_current_item == false) {
1725 cumulative_size += (current_code_item_ == nullptr ?
1726 size_for_null_code_item : current_code_item_->insns_size_in_code_units_);
1727 }
1728
1729 return cumulative_size;
1730}
1731
Vladimir Marko55fff042014-07-10 12:42:52 +01001732static BasicBlock* SelectTopologicalSortOrderFallBack(
1733 MIRGraph* mir_graph, const ArenaBitVector* current_loop,
1734 const ScopedArenaVector<size_t>* visited_cnt_values, ScopedArenaAllocator* allocator,
1735 ScopedArenaVector<BasicBlockId>* tmp_stack) {
1736 // No true loop head has been found but there may be true loop heads after the mess we need
1737 // to resolve. To avoid taking one of those, pick the candidate with the highest number of
1738 // reachable unvisited nodes. That candidate will surely be a part of a loop.
1739 BasicBlock* fall_back = nullptr;
1740 size_t fall_back_num_reachable = 0u;
1741 // Reuse the same bit vector for each candidate to mark reachable unvisited blocks.
1742 ArenaBitVector candidate_reachable(allocator, mir_graph->GetNumBlocks(), false, kBitMapMisc);
1743 AllNodesIterator iter(mir_graph);
1744 for (BasicBlock* candidate = iter.Next(); candidate != nullptr; candidate = iter.Next()) {
1745 if (candidate->hidden || // Hidden, or
1746 candidate->visited || // already processed, or
1747 (*visited_cnt_values)[candidate->id] == 0u || // no processed predecessors, or
1748 (current_loop != nullptr && // outside current loop.
1749 !current_loop->IsBitSet(candidate->id))) {
1750 continue;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001751 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001752 DCHECK(tmp_stack->empty());
1753 tmp_stack->push_back(candidate->id);
1754 candidate_reachable.ClearAllBits();
1755 size_t num_reachable = 0u;
1756 while (!tmp_stack->empty()) {
1757 BasicBlockId current_id = tmp_stack->back();
1758 tmp_stack->pop_back();
1759 BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
1760 DCHECK(current_bb != nullptr);
1761 ChildBlockIterator child_iter(current_bb, mir_graph);
1762 BasicBlock* child_bb = child_iter.Next();
1763 for ( ; child_bb != nullptr; child_bb = child_iter.Next()) {
1764 DCHECK(!child_bb->hidden);
1765 if (child_bb->visited || // Already processed, or
1766 (current_loop != nullptr && // outside current loop.
1767 !current_loop->IsBitSet(child_bb->id))) {
1768 continue;
1769 }
1770 if (!candidate_reachable.IsBitSet(child_bb->id)) {
1771 candidate_reachable.SetBit(child_bb->id);
1772 tmp_stack->push_back(child_bb->id);
1773 num_reachable += 1u;
1774 }
1775 }
1776 }
1777 if (fall_back_num_reachable < num_reachable) {
1778 fall_back_num_reachable = num_reachable;
1779 fall_back = candidate;
1780 }
1781 }
1782 return fall_back;
1783}
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001784
Vladimir Marko55fff042014-07-10 12:42:52 +01001785// Compute from which unvisited blocks is bb_id reachable through unvisited blocks.
1786static void ComputeUnvisitedReachableFrom(MIRGraph* mir_graph, BasicBlockId bb_id,
1787 ArenaBitVector* reachable,
1788 ScopedArenaVector<BasicBlockId>* tmp_stack) {
1789 // NOTE: Loop heads indicated by the "visited" flag.
1790 DCHECK(tmp_stack->empty());
1791 reachable->ClearAllBits();
1792 tmp_stack->push_back(bb_id);
1793 while (!tmp_stack->empty()) {
1794 BasicBlockId current_id = tmp_stack->back();
1795 tmp_stack->pop_back();
1796 BasicBlock* current_bb = mir_graph->GetBasicBlock(current_id);
1797 DCHECK(current_bb != nullptr);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001798 for (BasicBlockId pred_id : current_bb->predecessors) {
1799 BasicBlock* pred_bb = mir_graph->GetBasicBlock(pred_id);
1800 DCHECK(pred_bb != nullptr);
Vladimir Marko55fff042014-07-10 12:42:52 +01001801 if (!pred_bb->visited && !reachable->IsBitSet(pred_bb->id)) {
1802 reachable->SetBit(pred_bb->id);
1803 tmp_stack->push_back(pred_bb->id);
1804 }
1805 }
1806 }
1807}
1808
1809void MIRGraph::ComputeTopologicalSortOrder() {
1810 ScopedArenaAllocator allocator(&cu_->arena_stack);
1811 unsigned int num_blocks = GetNumBlocks();
1812
1813 ScopedArenaQueue<BasicBlock*> q(allocator.Adapter());
1814 ScopedArenaVector<size_t> visited_cnt_values(num_blocks, 0u, allocator.Adapter());
1815 ScopedArenaVector<BasicBlockId> loop_head_stack(allocator.Adapter());
1816 size_t max_nested_loops = 0u;
1817 ArenaBitVector loop_exit_blocks(&allocator, num_blocks, false, kBitMapMisc);
1818 loop_exit_blocks.ClearAllBits();
1819
1820 // Count the number of blocks to process and add the entry block(s).
Vladimir Marko55fff042014-07-10 12:42:52 +01001821 unsigned int num_blocks_to_process = 0u;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001822 for (BasicBlock* bb : block_list_) {
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001823 if (bb->hidden == true) {
1824 continue;
1825 }
1826
Vladimir Marko55fff042014-07-10 12:42:52 +01001827 num_blocks_to_process += 1u;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001828
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001829 if (bb->predecessors.size() == 0u) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001830 // Add entry block to the queue.
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001831 q.push(bb);
1832 }
1833 }
1834
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001835 // Clear the topological order arrays.
1836 topological_order_.clear();
1837 topological_order_.reserve(num_blocks);
1838 topological_order_loop_ends_.clear();
1839 topological_order_loop_ends_.resize(num_blocks, 0u);
1840 topological_order_indexes_.clear();
1841 topological_order_indexes_.resize(num_blocks, static_cast<uint16_t>(-1));
Vladimir Marko55fff042014-07-10 12:42:52 +01001842
1843 // Mark all blocks as unvisited.
1844 ClearAllVisitedFlags();
1845
1846 // For loop heads, keep track from which blocks they are reachable not going through other
1847 // loop heads. Other loop heads are excluded to detect the heads of nested loops. The children
1848 // in this set go into the loop body, the other children are jumping over the loop.
1849 ScopedArenaVector<ArenaBitVector*> loop_head_reachable_from(allocator.Adapter());
1850 loop_head_reachable_from.resize(num_blocks, nullptr);
1851 // Reuse the same temp stack whenever calculating a loop_head_reachable_from[loop_head_id].
1852 ScopedArenaVector<BasicBlockId> tmp_stack(allocator.Adapter());
1853
1854 while (num_blocks_to_process != 0u) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001855 BasicBlock* bb = nullptr;
1856 if (!q.empty()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001857 num_blocks_to_process -= 1u;
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001858 // Get top.
1859 bb = q.front();
1860 q.pop();
Vladimir Marko55fff042014-07-10 12:42:52 +01001861 if (bb->visited) {
1862 // Loop head: it was already processed, mark end and copy exit blocks to the queue.
1863 DCHECK(q.empty()) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001864 uint16_t idx = static_cast<uint16_t>(topological_order_.size());
1865 topological_order_loop_ends_[topological_order_indexes_[bb->id]] = idx;
Vladimir Marko55fff042014-07-10 12:42:52 +01001866 DCHECK_EQ(loop_head_stack.back(), bb->id);
1867 loop_head_stack.pop_back();
1868 ArenaBitVector* reachable =
1869 loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
1870 for (BasicBlockId candidate_id : loop_exit_blocks.Indexes()) {
1871 if (reachable == nullptr || reachable->IsBitSet(candidate_id)) {
1872 q.push(GetBasicBlock(candidate_id));
1873 // NOTE: The BitVectorSet::IndexIterator will not check the pointed-to bit again,
1874 // so clearing the bit has no effect on the iterator.
1875 loop_exit_blocks.ClearBit(candidate_id);
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001876 }
1877 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001878 continue;
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001879 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001880 } else {
1881 // Find the new loop head.
1882 AllNodesIterator iter(this);
1883 while (true) {
1884 BasicBlock* candidate = iter.Next();
1885 if (candidate == nullptr) {
1886 // We did not find a true loop head, fall back to a reachable block in any loop.
1887 ArenaBitVector* current_loop =
1888 loop_head_stack.empty() ? nullptr : loop_head_reachable_from[loop_head_stack.back()];
1889 bb = SelectTopologicalSortOrderFallBack(this, current_loop, &visited_cnt_values,
1890 &allocator, &tmp_stack);
1891 DCHECK(bb != nullptr) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
1892 if (kIsDebugBuild && cu_->dex_file != nullptr) {
1893 LOG(INFO) << "Topological sort order: Using fall-back in "
1894 << PrettyMethod(cu_->method_idx, *cu_->dex_file) << " BB #" << bb->id
1895 << " @0x" << std::hex << bb->start_offset
1896 << ", num_blocks = " << std::dec << num_blocks;
1897 }
1898 break;
1899 }
1900 if (candidate->hidden || // Hidden, or
1901 candidate->visited || // already processed, or
1902 visited_cnt_values[candidate->id] == 0u || // no processed predecessors, or
1903 (!loop_head_stack.empty() && // outside current loop.
1904 !loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(candidate->id))) {
1905 continue;
1906 }
1907
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001908 for (BasicBlockId pred_id : candidate->predecessors) {
1909 BasicBlock* pred_bb = GetBasicBlock(pred_id);
1910 DCHECK(pred_bb != nullptr);
Vladimir Marko55fff042014-07-10 12:42:52 +01001911 if (pred_bb != candidate && !pred_bb->visited &&
1912 !pred_bb->dominators->IsBitSet(candidate->id)) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001913 candidate = nullptr; // Set candidate to null to indicate failure.
1914 break;
Vladimir Marko55fff042014-07-10 12:42:52 +01001915 }
1916 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001917 if (candidate != nullptr) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001918 bb = candidate;
1919 break;
1920 }
1921 }
1922 // Compute blocks from which the loop head is reachable and process those blocks first.
1923 ArenaBitVector* reachable =
1924 new (&allocator) ArenaBitVector(&allocator, num_blocks, false, kBitMapMisc);
1925 loop_head_reachable_from[bb->id] = reachable;
1926 ComputeUnvisitedReachableFrom(this, bb->id, reachable, &tmp_stack);
1927 // Now mark as loop head. (Even if it's only a fall back when we don't find a true loop.)
1928 loop_head_stack.push_back(bb->id);
1929 max_nested_loops = std::max(max_nested_loops, loop_head_stack.size());
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001930 }
1931
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001932 DCHECK_EQ(bb->hidden, false);
1933 DCHECK_EQ(bb->visited, false);
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001934 bb->visited = true;
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001935
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001936 // Now add the basic block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001937 uint16_t idx = static_cast<uint16_t>(topological_order_.size());
1938 topological_order_indexes_[bb->id] = idx;
1939 topological_order_.push_back(bb->id);
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001940
Vladimir Marko55fff042014-07-10 12:42:52 +01001941 // Update visited_cnt_values for children.
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001942 ChildBlockIterator succIter(bb, this);
1943 BasicBlock* successor = succIter.Next();
1944 for ( ; successor != nullptr; successor = succIter.Next()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001945 if (successor->hidden) {
Vladimir Marko622bdbe2014-06-19 14:59:05 +01001946 continue;
1947 }
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001948
Vladimir Marko55fff042014-07-10 12:42:52 +01001949 // One more predecessor was visited.
1950 visited_cnt_values[successor->id] += 1u;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001951 if (visited_cnt_values[successor->id] == successor->predecessors.size()) {
Vladimir Marko55fff042014-07-10 12:42:52 +01001952 if (loop_head_stack.empty() ||
1953 loop_head_reachable_from[loop_head_stack.back()]->IsBitSet(successor->id)) {
1954 q.push(successor);
1955 } else {
1956 DCHECK(!loop_exit_blocks.IsBitSet(successor->id));
1957 loop_exit_blocks.SetBit(successor->id);
1958 }
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001959 }
1960 }
1961 }
Vladimir Marko55fff042014-07-10 12:42:52 +01001962
1963 // Prepare the loop head stack for iteration.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001964 topological_order_loop_head_stack_.clear();
1965 topological_order_loop_head_stack_.reserve(max_nested_loops);
Jean Christophe Beyler44e5bde2014-04-29 14:40:41 -07001966}
1967
1968bool BasicBlock::IsExceptionBlock() const {
1969 if (block_type == kExceptionHandling) {
1970 return true;
1971 }
1972 return false;
1973}
1974
Wei Jin04f4d8a2014-05-29 18:04:29 -07001975bool MIRGraph::HasSuspendTestBetween(BasicBlock* source, BasicBlockId target_id) {
1976 BasicBlock* target = GetBasicBlock(target_id);
1977
1978 if (source == nullptr || target == nullptr)
1979 return false;
1980
1981 int idx;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001982 for (idx = gen_suspend_test_list_.size() - 1; idx >= 0; idx--) {
1983 BasicBlock* bb = gen_suspend_test_list_[idx];
Wei Jin04f4d8a2014-05-29 18:04:29 -07001984 if (bb == source)
1985 return true; // The block has been inserted by a suspend check before.
1986 if (source->dominators->IsBitSet(bb->id) && bb->dominators->IsBitSet(target_id))
1987 return true;
1988 }
1989
1990 return false;
1991}
1992
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07001993ChildBlockIterator::ChildBlockIterator(BasicBlock* bb, MIRGraph* mir_graph)
1994 : basic_block_(bb), mir_graph_(mir_graph), visited_fallthrough_(false),
1995 visited_taken_(false), have_successors_(false) {
1996 // Check if we actually do have successors.
1997 if (basic_block_ != 0 && basic_block_->successor_block_list_type != kNotUsed) {
1998 have_successors_ = true;
Vladimir Markoe39c54e2014-09-22 14:50:02 +01001999 successor_iter_ = basic_block_->successor_blocks.cbegin();
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002000 }
2001}
2002
2003BasicBlock* ChildBlockIterator::Next() {
2004 // We check if we have a basic block. If we don't we cannot get next child.
2005 if (basic_block_ == nullptr) {
2006 return nullptr;
2007 }
2008
2009 // If we haven't visited fallthrough, return that.
2010 if (visited_fallthrough_ == false) {
2011 visited_fallthrough_ = true;
2012
2013 BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->fall_through);
2014 if (result != nullptr) {
2015 return result;
2016 }
2017 }
2018
2019 // If we haven't visited taken, return that.
2020 if (visited_taken_ == false) {
2021 visited_taken_ = true;
2022
2023 BasicBlock* result = mir_graph_->GetBasicBlock(basic_block_->taken);
2024 if (result != nullptr) {
2025 return result;
2026 }
2027 }
2028
2029 // We visited both taken and fallthrough. Now check if we have successors we need to visit.
2030 if (have_successors_ == true) {
2031 // Get information about next successor block.
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002032 auto end = basic_block_->successor_blocks.cend();
2033 while (successor_iter_ != end) {
2034 SuccessorBlockInfo* successor_block_info = *successor_iter_;
2035 ++successor_iter_;
Niranjan Kumar989367a2014-06-12 12:15:48 -07002036 // If block was replaced by zero block, take next one.
2037 if (successor_block_info->block != NullBasicBlockId) {
2038 return mir_graph_->GetBasicBlock(successor_block_info->block);
2039 }
Jean Christophe Beylerf8c762b2014-05-02 12:54:37 -07002040 }
2041 }
2042
2043 // We do not have anything.
2044 return nullptr;
2045}
2046
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002047BasicBlock* BasicBlock::Copy(CompilationUnit* c_unit) {
2048 MIRGraph* mir_graph = c_unit->mir_graph.get();
2049 return Copy(mir_graph);
2050}
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002051
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002052BasicBlock* BasicBlock::Copy(MIRGraph* mir_graph) {
2053 BasicBlock* result_bb = mir_graph->CreateNewBB(block_type);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002054
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002055 // We don't do a memcpy style copy here because it would lead to a lot of things
2056 // to clean up. Let us do it by hand instead.
2057 // Copy in taken and fallthrough.
2058 result_bb->fall_through = fall_through;
2059 result_bb->taken = taken;
2060
2061 // Copy successor links if needed.
2062 ArenaAllocator* arena = mir_graph->GetArena();
2063
2064 result_bb->successor_block_list_type = successor_block_list_type;
2065 if (result_bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002066 result_bb->successor_blocks.reserve(successor_blocks.size());
2067 for (SuccessorBlockInfo* sbi_old : successor_blocks) {
2068 SuccessorBlockInfo* sbi_new = static_cast<SuccessorBlockInfo*>(
2069 arena->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002070 memcpy(sbi_new, sbi_old, sizeof(SuccessorBlockInfo));
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002071 result_bb->successor_blocks.push_back(sbi_new);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002072 }
2073 }
2074
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002075 // Copy offset, method.
2076 result_bb->start_offset = start_offset;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002077
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002078 // Now copy instructions.
2079 for (MIR* mir = first_mir_insn; mir != 0; mir = mir->next) {
2080 // Get a copy first.
2081 MIR* copy = mir->Copy(mir_graph);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002082
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002083 // Append it.
2084 result_bb->AppendMIR(copy);
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002085 }
2086
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002087 return result_bb;
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002088}
2089
2090MIR* MIR::Copy(MIRGraph* mir_graph) {
2091 MIR* res = mir_graph->NewMIR();
2092 *res = *this;
2093
2094 // Remove links
2095 res->next = nullptr;
2096 res->bb = NullBasicBlockId;
2097 res->ssa_rep = nullptr;
2098
2099 return res;
2100}
2101
2102MIR* MIR::Copy(CompilationUnit* c_unit) {
2103 return Copy(c_unit->mir_graph.get());
2104}
2105
2106uint32_t SSARepresentation::GetStartUseIndex(Instruction::Code opcode) {
2107 // Default result.
2108 int res = 0;
2109
2110 // We are basically setting the iputs to their igets counterparts.
2111 switch (opcode) {
2112 case Instruction::IPUT:
2113 case Instruction::IPUT_OBJECT:
2114 case Instruction::IPUT_BOOLEAN:
2115 case Instruction::IPUT_BYTE:
2116 case Instruction::IPUT_CHAR:
2117 case Instruction::IPUT_SHORT:
2118 case Instruction::IPUT_QUICK:
2119 case Instruction::IPUT_OBJECT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -07002120 case Instruction::IPUT_BOOLEAN_QUICK:
2121 case Instruction::IPUT_BYTE_QUICK:
2122 case Instruction::IPUT_CHAR_QUICK:
2123 case Instruction::IPUT_SHORT_QUICK:
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -07002124 case Instruction::APUT:
2125 case Instruction::APUT_OBJECT:
2126 case Instruction::APUT_BOOLEAN:
2127 case Instruction::APUT_BYTE:
2128 case Instruction::APUT_CHAR:
2129 case Instruction::APUT_SHORT:
2130 case Instruction::SPUT:
2131 case Instruction::SPUT_OBJECT:
2132 case Instruction::SPUT_BOOLEAN:
2133 case Instruction::SPUT_BYTE:
2134 case Instruction::SPUT_CHAR:
2135 case Instruction::SPUT_SHORT:
2136 // Skip the VR containing what to store.
2137 res = 1;
2138 break;
2139 case Instruction::IPUT_WIDE:
2140 case Instruction::IPUT_WIDE_QUICK:
2141 case Instruction::APUT_WIDE:
2142 case Instruction::SPUT_WIDE:
2143 // Skip the two VRs containing what to store.
2144 res = 2;
2145 break;
2146 default:
2147 // Do nothing in the general case.
2148 break;
2149 }
2150
2151 return res;
2152}
2153
Jean Christophe Beylerc3db20b2014-05-05 21:09:40 -07002154/**
2155 * @brief Given a decoded instruction, it checks whether the instruction
2156 * sets a constant and if it does, more information is provided about the
2157 * constant being set.
2158 * @param ptr_value pointer to a 64-bit holder for the constant.
2159 * @param wide Updated by function whether a wide constant is being set by bytecode.
2160 * @return Returns false if the decoded instruction does not represent a constant bytecode.
2161 */
2162bool MIR::DecodedInstruction::GetConstant(int64_t* ptr_value, bool* wide) const {
2163 bool sets_const = true;
2164 int64_t value = vB;
2165
2166 DCHECK(ptr_value != nullptr);
2167 DCHECK(wide != nullptr);
2168
2169 switch (opcode) {
2170 case Instruction::CONST_4:
2171 case Instruction::CONST_16:
2172 case Instruction::CONST:
2173 *wide = false;
2174 value <<= 32; // In order to get the sign extend.
2175 value >>= 32;
2176 break;
2177 case Instruction::CONST_HIGH16:
2178 *wide = false;
2179 value <<= 48; // In order to get the sign extend.
2180 value >>= 32;
2181 break;
2182 case Instruction::CONST_WIDE_16:
2183 case Instruction::CONST_WIDE_32:
2184 *wide = true;
2185 value <<= 32; // In order to get the sign extend.
2186 value >>= 32;
2187 break;
2188 case Instruction::CONST_WIDE:
2189 *wide = true;
2190 value = vB_wide;
2191 break;
2192 case Instruction::CONST_WIDE_HIGH16:
2193 *wide = true;
2194 value <<= 48; // In order to get the sign extend.
2195 break;
2196 default:
2197 sets_const = false;
2198 break;
2199 }
2200
2201 if (sets_const) {
2202 *ptr_value = value;
2203 }
2204
2205 return sets_const;
2206}
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002207
2208void BasicBlock::ResetOptimizationFlags(uint16_t reset_flags) {
2209 // Reset flags for all MIRs in bb.
2210 for (MIR* mir = first_mir_insn; mir != NULL; mir = mir->next) {
2211 mir->optimization_flags &= (~reset_flags);
2212 }
2213}
2214
2215void BasicBlock::Hide(CompilationUnit* c_unit) {
2216 // First lets make it a dalvik bytecode block so it doesn't have any special meaning.
2217 block_type = kDalvikByteCode;
2218
2219 // Mark it as hidden.
2220 hidden = true;
2221
2222 // Detach it from its MIRs so we don't generate code for them. Also detached MIRs
2223 // are updated to know that they no longer have a parent.
2224 for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
2225 mir->bb = NullBasicBlockId;
2226 }
2227 first_mir_insn = nullptr;
2228 last_mir_insn = nullptr;
2229
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002230 MIRGraph* mir_graph = c_unit->mir_graph.get();
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002231 for (BasicBlockId pred_id : predecessors) {
2232 BasicBlock* pred_bb = mir_graph->GetBasicBlock(pred_id);
2233 DCHECK(pred_bb != nullptr);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002234
2235 // Sadly we have to go through the children by hand here.
2236 pred_bb->ReplaceChild(id, NullBasicBlockId);
2237 }
2238
2239 // Iterate through children of bb we are hiding.
2240 ChildBlockIterator successorChildIter(this, mir_graph);
2241
2242 for (BasicBlock* childPtr = successorChildIter.Next(); childPtr != 0; childPtr = successorChildIter.Next()) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002243 // Erase this predecessor from child.
2244 childPtr->ErasePredecessor(id);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002245 }
Jean Christophe Beylerf588b502014-06-18 14:14:15 -07002246
2247 // Remove link to children.
2248 taken = NullBasicBlockId;
2249 fall_through = NullBasicBlockId;
2250 successor_block_list_type = kNotUsed;
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002251}
2252
2253bool BasicBlock::IsSSALiveOut(const CompilationUnit* c_unit, int ssa_reg) {
2254 // In order to determine if the ssa reg is live out, we scan all the MIRs. We remember
2255 // the last SSA number of the same dalvik register. At the end, if it is different than ssa_reg,
2256 // then it is not live out of this BB.
2257 int dalvik_reg = c_unit->mir_graph->SRegToVReg(ssa_reg);
2258
2259 int last_ssa_reg = -1;
2260
2261 // Walk through the MIRs backwards.
2262 for (MIR* mir = first_mir_insn; mir != nullptr; mir = mir->next) {
2263 // Get ssa rep.
2264 SSARepresentation *ssa_rep = mir->ssa_rep;
2265
2266 // Go through the defines for this MIR.
2267 for (int i = 0; i < ssa_rep->num_defs; i++) {
2268 DCHECK(ssa_rep->defs != nullptr);
2269
2270 // Get the ssa reg.
2271 int def_ssa_reg = ssa_rep->defs[i];
2272
2273 // Get dalvik reg.
2274 int def_dalvik_reg = c_unit->mir_graph->SRegToVReg(def_ssa_reg);
2275
2276 // Compare dalvik regs.
2277 if (dalvik_reg == def_dalvik_reg) {
2278 // We found a def of the register that we are being asked about.
2279 // Remember it.
2280 last_ssa_reg = def_ssa_reg;
2281 }
2282 }
2283 }
2284
2285 if (last_ssa_reg == -1) {
2286 // If we get to this point we couldn't find a define of register user asked about.
2287 // Let's assume the user knows what he's doing so we can be safe and say that if we
2288 // couldn't find a def, it is live out.
2289 return true;
2290 }
2291
2292 // If it is not -1, we found a match, is it ssa_reg?
2293 return (ssa_reg == last_ssa_reg);
2294}
2295
2296bool BasicBlock::ReplaceChild(BasicBlockId old_bb, BasicBlockId new_bb) {
2297 // We need to check taken, fall_through, and successor_blocks to replace.
2298 bool found = false;
2299 if (taken == old_bb) {
2300 taken = new_bb;
2301 found = true;
2302 }
2303
2304 if (fall_through == old_bb) {
2305 fall_through = new_bb;
2306 found = true;
2307 }
2308
2309 if (successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002310 for (SuccessorBlockInfo* successor_block_info : successor_blocks) {
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002311 if (successor_block_info->block == old_bb) {
2312 successor_block_info->block = new_bb;
2313 found = true;
2314 }
2315 }
2316 }
2317
2318 return found;
2319}
2320
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002321void BasicBlock::ErasePredecessor(BasicBlockId old_pred) {
2322 auto pos = std::find(predecessors.begin(), predecessors.end(), old_pred);
2323 DCHECK(pos != predecessors.end());
2324 predecessors.erase(pos);
2325}
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002326
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002327void BasicBlock::UpdatePredecessor(BasicBlockId old_pred, BasicBlockId new_pred) {
2328 DCHECK_NE(new_pred, NullBasicBlockId);
2329 auto pos = std::find(predecessors.begin(), predecessors.end(), old_pred);
2330 if (pos != predecessors.end()) {
2331 *pos = new_pred;
2332 } else {
2333 // If not found, add it.
2334 predecessors.push_back(new_pred);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002335 }
2336}
2337
2338// Create a new basic block with block_id as num_blocks_ that is
2339// post-incremented.
2340BasicBlock* MIRGraph::CreateNewBB(BBType block_type) {
2341 BasicBlock* res = NewMemBB(block_type, num_blocks_++);
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002342 block_list_.push_back(res);
Jean Christophe Beyler85127582014-05-11 23:36:41 -07002343 return res;
2344}
2345
Jean Christophe Beyler2469e602014-05-06 20:36:55 -07002346void MIRGraph::CalculateBasicBlockInformation() {
2347 PassDriverMEPostOpt driver(cu_);
2348 driver.Launch();
2349}
2350
2351void MIRGraph::InitializeBasicBlockData() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +01002352 num_blocks_ = block_list_.size();
Jean Christophe Beyler2469e602014-05-06 20:36:55 -07002353}
2354
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002355int MIR::DecodedInstruction::FlagsOf() const {
2356 // Calculate new index.
2357 int idx = static_cast<int>(opcode) - kNumPackedOpcodes;
2358
2359 // Check if it is an extended or not.
2360 if (idx < 0) {
2361 return Instruction::FlagsOf(opcode);
2362 }
2363
2364 // For extended, we use a switch.
2365 switch (static_cast<int>(opcode)) {
2366 case kMirOpPhi:
2367 return Instruction::kContinue;
2368 case kMirOpCopy:
2369 return Instruction::kContinue;
2370 case kMirOpFusedCmplFloat:
2371 return Instruction::kContinue | Instruction::kBranch;
2372 case kMirOpFusedCmpgFloat:
2373 return Instruction::kContinue | Instruction::kBranch;
2374 case kMirOpFusedCmplDouble:
2375 return Instruction::kContinue | Instruction::kBranch;
2376 case kMirOpFusedCmpgDouble:
2377 return Instruction::kContinue | Instruction::kBranch;
2378 case kMirOpFusedCmpLong:
2379 return Instruction::kContinue | Instruction::kBranch;
2380 case kMirOpNop:
2381 return Instruction::kContinue;
2382 case kMirOpNullCheck:
2383 return Instruction::kContinue | Instruction::kThrow;
2384 case kMirOpRangeCheck:
2385 return Instruction::kContinue | Instruction::kThrow;
2386 case kMirOpDivZeroCheck:
2387 return Instruction::kContinue | Instruction::kThrow;
2388 case kMirOpCheck:
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002389 return Instruction::kContinue | Instruction::kThrow;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002390 case kMirOpCheckPart2:
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002391 return Instruction::kContinue;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002392 case kMirOpSelect:
2393 return Instruction::kContinue;
2394 case kMirOpConstVector:
2395 return Instruction::kContinue;
2396 case kMirOpMoveVector:
2397 return Instruction::kContinue;
2398 case kMirOpPackedMultiply:
2399 return Instruction::kContinue;
2400 case kMirOpPackedAddition:
2401 return Instruction::kContinue;
2402 case kMirOpPackedSubtract:
2403 return Instruction::kContinue;
2404 case kMirOpPackedShiftLeft:
2405 return Instruction::kContinue;
2406 case kMirOpPackedSignedShiftRight:
2407 return Instruction::kContinue;
2408 case kMirOpPackedUnsignedShiftRight:
2409 return Instruction::kContinue;
2410 case kMirOpPackedAnd:
2411 return Instruction::kContinue;
2412 case kMirOpPackedOr:
2413 return Instruction::kContinue;
2414 case kMirOpPackedXor:
2415 return Instruction::kContinue;
2416 case kMirOpPackedAddReduce:
2417 return Instruction::kContinue;
2418 case kMirOpPackedReduce:
2419 return Instruction::kContinue;
2420 case kMirOpPackedSet:
2421 return Instruction::kContinue;
2422 case kMirOpReserveVectorRegisters:
2423 return Instruction::kContinue;
2424 case kMirOpReturnVectorRegisters:
2425 return Instruction::kContinue;
Udayan Banerjib7fc6292014-09-09 16:49:34 -07002426 case kMirOpMemBarrier:
2427 return Instruction::kContinue;
2428 case kMirOpPackedArrayGet:
2429 return Instruction::kContinue | Instruction::kThrow;
2430 case kMirOpPackedArrayPut:
2431 return Instruction::kContinue | Instruction::kThrow;
Jean Christophe Beylerfb0ea2d2014-07-29 13:20:42 -07002432 default:
2433 LOG(WARNING) << "ExtendedFlagsOf: Unhandled case: " << static_cast<int> (opcode);
2434 return 0;
2435 }
2436}
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002437} // namespace art