blob: 8bb5615bd02da174a5822e5d4d0a644339f71d44 [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>
20
Ian Rogers6282dc12013-04-18 15:54:02 -070021#include "base/stl_util.h"
buzbee311ca162013-02-28 15:56:43 -080022#include "compiler_internals.h"
buzbee311ca162013-02-28 15:56:43 -080023#include "dex_file-inl.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000024#include "dex/quick/dex_file_to_method_inliner_map.h"
25#include "dex/quick/dex_file_method_inliner.h"
Nicolas Geoffrayf3e2cc42014-02-18 18:37:26 +000026#include "leb128.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000027
buzbee311ca162013-02-28 15:56:43 -080028namespace art {
29
30#define MAX_PATTERN_LEN 5
31
buzbee1fd33462013-03-25 13:40:45 -070032const char* MIRGraph::extended_mir_op_names_[kMirOpLast - kMirOpFirst] = {
33 "Phi",
34 "Copy",
35 "FusedCmplFloat",
36 "FusedCmpgFloat",
37 "FusedCmplDouble",
38 "FusedCmpgDouble",
39 "FusedCmpLong",
40 "Nop",
41 "OpNullCheck",
42 "OpRangeCheck",
43 "OpDivZeroCheck",
44 "Check1",
45 "Check2",
46 "Select",
47};
48
buzbee862a7602013-04-05 10:58:54 -070049MIRGraph::MIRGraph(CompilationUnit* cu, ArenaAllocator* arena)
buzbee1fd33462013-03-25 13:40:45 -070050 : reg_location_(NULL),
51 cu_(cu),
buzbee311ca162013-02-28 15:56:43 -080052 ssa_base_vregs_(NULL),
53 ssa_subscripts_(NULL),
buzbee311ca162013-02-28 15:56:43 -080054 vreg_to_ssa_map_(NULL),
55 ssa_last_defs_(NULL),
56 is_constant_v_(NULL),
57 constant_values_(NULL),
buzbee862a7602013-04-05 10:58:54 -070058 use_counts_(arena, 256, kGrowableArrayMisc),
59 raw_use_counts_(arena, 256, kGrowableArrayMisc),
buzbee311ca162013-02-28 15:56:43 -080060 num_reachable_blocks_(0),
buzbee862a7602013-04-05 10:58:54 -070061 dfs_order_(NULL),
62 dfs_post_order_(NULL),
63 dom_post_order_traversal_(NULL),
buzbee311ca162013-02-28 15:56:43 -080064 i_dom_list_(NULL),
65 def_block_matrix_(NULL),
66 temp_block_v_(NULL),
67 temp_dalvik_register_v_(NULL),
68 temp_ssa_register_v_(NULL),
buzbee862a7602013-04-05 10:58:54 -070069 block_list_(arena, 100, kGrowableArrayBlockList),
buzbee311ca162013-02-28 15:56:43 -080070 try_block_addr_(NULL),
71 entry_block_(NULL),
72 exit_block_(NULL),
buzbee311ca162013-02-28 15:56:43 -080073 num_blocks_(0),
74 current_code_item_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -070075 dex_pc_to_block_map_(arena, 0, kGrowableArrayMisc),
buzbee311ca162013-02-28 15:56:43 -080076 current_method_(kInvalidEntry),
77 current_offset_(kInvalidEntry),
78 def_count_(0),
79 opcode_count_(NULL),
buzbee1fd33462013-03-25 13:40:45 -070080 num_ssa_regs_(0),
81 method_sreg_(0),
buzbee862a7602013-04-05 10:58:54 -070082 attributes_(METHOD_IS_LEAF), // Start with leaf assumption, change on encountering invoke.
83 checkstats_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -070084 arena_(arena),
85 backward_branches_(0),
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -080086 forward_branches_(0),
87 compiler_temps_(arena, 6, kGrowableArrayMisc),
88 num_non_special_compiler_temps_(0),
buzbeeb1f1d642014-02-27 12:55:32 -080089 max_available_non_special_compiler_temps_(0),
Vladimir Markobe0e5462014-02-26 11:24:15 +000090 punt_to_interpreter_(false),
Vladimir Marko3d73ba22014-03-06 15:18:04 +000091 merged_df_flags_(0u),
Vladimir Markobe0e5462014-02-26 11:24:15 +000092 ifield_lowering_infos_(arena, 0u),
Vladimir Markof096aad2014-01-23 15:51:58 +000093 sfield_lowering_infos_(arena, 0u),
94 method_lowering_infos_(arena, 0u) {
buzbee862a7602013-04-05 10:58:54 -070095 try_block_addr_ = new (arena_) ArenaBitVector(arena_, 0, true /* expandable */);
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -080096 max_available_special_compiler_temps_ = std::abs(static_cast<int>(kVRegNonSpecialTempBaseReg))
97 - std::abs(static_cast<int>(kVRegTempBaseReg));
buzbee311ca162013-02-28 15:56:43 -080098}
99
Ian Rogers6282dc12013-04-18 15:54:02 -0700100MIRGraph::~MIRGraph() {
101 STLDeleteElements(&m_units_);
102}
103
buzbee311ca162013-02-28 15:56:43 -0800104/*
105 * Parse an instruction, return the length of the instruction
106 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700107int MIRGraph::ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction) {
buzbee311ca162013-02-28 15:56:43 -0800108 const Instruction* instruction = Instruction::At(code_ptr);
109 *decoded_instruction = DecodedInstruction(instruction);
110
111 return instruction->SizeInCodeUnits();
112}
113
114
115/* Split an existing block from the specified code offset into two */
buzbee0d829482013-10-11 15:24:55 -0700116BasicBlock* MIRGraph::SplitBlock(DexOffset code_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700117 BasicBlock* orig_block, BasicBlock** immed_pred_block_p) {
buzbee0d829482013-10-11 15:24:55 -0700118 DCHECK_GT(code_offset, orig_block->start_offset);
buzbee311ca162013-02-28 15:56:43 -0800119 MIR* insn = orig_block->first_mir_insn;
buzbee0d829482013-10-11 15:24:55 -0700120 MIR* prev = NULL;
buzbee311ca162013-02-28 15:56:43 -0800121 while (insn) {
122 if (insn->offset == code_offset) break;
buzbee0d829482013-10-11 15:24:55 -0700123 prev = insn;
buzbee311ca162013-02-28 15:56:43 -0800124 insn = insn->next;
125 }
126 if (insn == NULL) {
127 LOG(FATAL) << "Break split failed";
128 }
buzbee862a7602013-04-05 10:58:54 -0700129 BasicBlock *bottom_block = NewMemBB(kDalvikByteCode, num_blocks_++);
130 block_list_.Insert(bottom_block);
buzbee311ca162013-02-28 15:56:43 -0800131
132 bottom_block->start_offset = code_offset;
133 bottom_block->first_mir_insn = insn;
134 bottom_block->last_mir_insn = orig_block->last_mir_insn;
135
136 /* If this block was terminated by a return, the flag needs to go with the bottom block */
137 bottom_block->terminated_by_return = orig_block->terminated_by_return;
138 orig_block->terminated_by_return = false;
139
buzbee311ca162013-02-28 15:56:43 -0800140 /* Handle the taken path */
141 bottom_block->taken = orig_block->taken;
buzbee0d829482013-10-11 15:24:55 -0700142 if (bottom_block->taken != NullBasicBlockId) {
143 orig_block->taken = NullBasicBlockId;
144 BasicBlock* bb_taken = GetBasicBlock(bottom_block->taken);
145 bb_taken->predecessors->Delete(orig_block->id);
146 bb_taken->predecessors->Insert(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800147 }
148
149 /* Handle the fallthrough path */
150 bottom_block->fall_through = orig_block->fall_through;
buzbee0d829482013-10-11 15:24:55 -0700151 orig_block->fall_through = bottom_block->id;
152 bottom_block->predecessors->Insert(orig_block->id);
153 if (bottom_block->fall_through != NullBasicBlockId) {
154 BasicBlock* bb_fall_through = GetBasicBlock(bottom_block->fall_through);
155 bb_fall_through->predecessors->Delete(orig_block->id);
156 bb_fall_through->predecessors->Insert(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800157 }
158
159 /* Handle the successor list */
buzbee0d829482013-10-11 15:24:55 -0700160 if (orig_block->successor_block_list_type != kNotUsed) {
161 bottom_block->successor_block_list_type = orig_block->successor_block_list_type;
162 bottom_block->successor_blocks = orig_block->successor_blocks;
163 orig_block->successor_block_list_type = kNotUsed;
164 orig_block->successor_blocks = NULL;
165 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bottom_block->successor_blocks);
buzbee311ca162013-02-28 15:56:43 -0800166 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700167 SuccessorBlockInfo *successor_block_info = iterator.Next();
buzbee311ca162013-02-28 15:56:43 -0800168 if (successor_block_info == NULL) break;
buzbee0d829482013-10-11 15:24:55 -0700169 BasicBlock *bb = GetBasicBlock(successor_block_info->block);
170 bb->predecessors->Delete(orig_block->id);
171 bb->predecessors->Insert(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800172 }
173 }
174
buzbee0d829482013-10-11 15:24:55 -0700175 orig_block->last_mir_insn = prev;
176 prev->next = NULL;
buzbee311ca162013-02-28 15:56:43 -0800177
buzbee311ca162013-02-28 15:56:43 -0800178 /*
179 * Update the immediate predecessor block pointer so that outgoing edges
180 * can be applied to the proper block.
181 */
182 if (immed_pred_block_p) {
183 DCHECK_EQ(*immed_pred_block_p, orig_block);
184 *immed_pred_block_p = bottom_block;
185 }
buzbeeb48819d2013-09-14 16:15:25 -0700186
187 // Associate dex instructions in the bottom block with the new container.
Vladimir Marko4376c872014-01-23 12:39:29 +0000188 DCHECK(insn != nullptr);
189 DCHECK(insn != orig_block->first_mir_insn);
190 DCHECK(insn == bottom_block->first_mir_insn);
191 DCHECK_EQ(insn->offset, bottom_block->start_offset);
192 DCHECK(static_cast<int>(insn->dalvikInsn.opcode) == kMirOpCheck ||
193 !IsPseudoMirOp(insn->dalvikInsn.opcode));
194 DCHECK_EQ(dex_pc_to_block_map_.Get(insn->offset), orig_block->id);
195 MIR* p = insn;
196 dex_pc_to_block_map_.Put(p->offset, bottom_block->id);
197 while (p != bottom_block->last_mir_insn) {
198 p = p->next;
199 DCHECK(p != nullptr);
buzbeeb48819d2013-09-14 16:15:25 -0700200 int opcode = p->dalvikInsn.opcode;
201 /*
202 * Some messiness here to ensure that we only enter real opcodes and only the
203 * first half of a potentially throwing instruction that has been split into
Vladimir Marko4376c872014-01-23 12:39:29 +0000204 * CHECK and work portions. Since the 2nd half of a split operation is always
205 * the first in a BasicBlock, we can't hit it here.
buzbeeb48819d2013-09-14 16:15:25 -0700206 */
Vladimir Marko4376c872014-01-23 12:39:29 +0000207 if ((opcode == kMirOpCheck) || !IsPseudoMirOp(opcode)) {
208 DCHECK_EQ(dex_pc_to_block_map_.Get(p->offset), orig_block->id);
buzbeeb48819d2013-09-14 16:15:25 -0700209 dex_pc_to_block_map_.Put(p->offset, bottom_block->id);
210 }
buzbeeb48819d2013-09-14 16:15:25 -0700211 }
212
buzbee311ca162013-02-28 15:56:43 -0800213 return bottom_block;
214}
215
216/*
217 * Given a code offset, find out the block that starts with it. If the offset
218 * is in the middle of an existing block, split it into two. If immed_pred_block_p
219 * is not non-null and is the block being split, update *immed_pred_block_p to
220 * point to the bottom block so that outgoing edges can be set up properly
221 * (by the caller)
222 * Utilizes a map for fast lookup of the typical cases.
223 */
buzbee0d829482013-10-11 15:24:55 -0700224BasicBlock* MIRGraph::FindBlock(DexOffset code_offset, bool split, bool create,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700225 BasicBlock** immed_pred_block_p) {
buzbeebd663de2013-09-10 15:41:31 -0700226 if (code_offset >= cu_->code_item->insns_size_in_code_units_) {
buzbee311ca162013-02-28 15:56:43 -0800227 return NULL;
228 }
buzbeeb48819d2013-09-14 16:15:25 -0700229
230 int block_id = dex_pc_to_block_map_.Get(code_offset);
231 BasicBlock* bb = (block_id == 0) ? NULL : block_list_.Get(block_id);
232
233 if ((bb != NULL) && (bb->start_offset == code_offset)) {
234 // Does this containing block start with the desired instruction?
buzbeebd663de2013-09-10 15:41:31 -0700235 return bb;
236 }
buzbee311ca162013-02-28 15:56:43 -0800237
buzbeeb48819d2013-09-14 16:15:25 -0700238 // No direct hit.
239 if (!create) {
240 return NULL;
buzbee311ca162013-02-28 15:56:43 -0800241 }
242
buzbeeb48819d2013-09-14 16:15:25 -0700243 if (bb != NULL) {
244 // The target exists somewhere in an existing block.
245 return SplitBlock(code_offset, bb, bb == *immed_pred_block_p ? immed_pred_block_p : NULL);
246 }
247
248 // Create a new block.
buzbee862a7602013-04-05 10:58:54 -0700249 bb = NewMemBB(kDalvikByteCode, num_blocks_++);
250 block_list_.Insert(bb);
buzbee311ca162013-02-28 15:56:43 -0800251 bb->start_offset = code_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700252 dex_pc_to_block_map_.Put(bb->start_offset, bb->id);
buzbee311ca162013-02-28 15:56:43 -0800253 return bb;
254}
255
buzbeeb48819d2013-09-14 16:15:25 -0700256
buzbee311ca162013-02-28 15:56:43 -0800257/* Identify code range in try blocks and set up the empty catch blocks */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700258void MIRGraph::ProcessTryCatchBlocks() {
buzbee311ca162013-02-28 15:56:43 -0800259 int tries_size = current_code_item_->tries_size_;
buzbee0d829482013-10-11 15:24:55 -0700260 DexOffset offset;
buzbee311ca162013-02-28 15:56:43 -0800261
262 if (tries_size == 0) {
263 return;
264 }
265
266 for (int i = 0; i < tries_size; i++) {
267 const DexFile::TryItem* pTry =
268 DexFile::GetTryItems(*current_code_item_, i);
buzbee0d829482013-10-11 15:24:55 -0700269 DexOffset start_offset = pTry->start_addr_;
270 DexOffset end_offset = start_offset + pTry->insn_count_;
buzbee311ca162013-02-28 15:56:43 -0800271 for (offset = start_offset; offset < end_offset; offset++) {
buzbee862a7602013-04-05 10:58:54 -0700272 try_block_addr_->SetBit(offset);
buzbee311ca162013-02-28 15:56:43 -0800273 }
274 }
275
276 // Iterate over each of the handlers to enqueue the empty Catch blocks
277 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*current_code_item_, 0);
278 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
279 for (uint32_t idx = 0; idx < handlers_size; idx++) {
280 CatchHandlerIterator iterator(handlers_ptr);
281 for (; iterator.HasNext(); iterator.Next()) {
282 uint32_t address = iterator.GetHandlerAddress();
283 FindBlock(address, false /* split */, true /*create*/,
284 /* immed_pred_block_p */ NULL);
285 }
286 handlers_ptr = iterator.EndDataPointer();
287 }
288}
289
290/* Process instructions with the kBranch flag */
buzbee0d829482013-10-11 15:24:55 -0700291BasicBlock* MIRGraph::ProcessCanBranch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
292 int width, int flags, const uint16_t* code_ptr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700293 const uint16_t* code_end) {
buzbee0d829482013-10-11 15:24:55 -0700294 DexOffset target = cur_offset;
buzbee311ca162013-02-28 15:56:43 -0800295 switch (insn->dalvikInsn.opcode) {
296 case Instruction::GOTO:
297 case Instruction::GOTO_16:
298 case Instruction::GOTO_32:
299 target += insn->dalvikInsn.vA;
300 break;
301 case Instruction::IF_EQ:
302 case Instruction::IF_NE:
303 case Instruction::IF_LT:
304 case Instruction::IF_GE:
305 case Instruction::IF_GT:
306 case Instruction::IF_LE:
307 cur_block->conditional_branch = true;
308 target += insn->dalvikInsn.vC;
309 break;
310 case Instruction::IF_EQZ:
311 case Instruction::IF_NEZ:
312 case Instruction::IF_LTZ:
313 case Instruction::IF_GEZ:
314 case Instruction::IF_GTZ:
315 case Instruction::IF_LEZ:
316 cur_block->conditional_branch = true;
317 target += insn->dalvikInsn.vB;
318 break;
319 default:
320 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
321 }
buzbeeb48819d2013-09-14 16:15:25 -0700322 CountBranch(target);
buzbee311ca162013-02-28 15:56:43 -0800323 BasicBlock *taken_block = FindBlock(target, /* split */ true, /* create */ true,
324 /* immed_pred_block_p */ &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700325 cur_block->taken = taken_block->id;
326 taken_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800327
328 /* Always terminate the current block for conditional branches */
329 if (flags & Instruction::kContinue) {
330 BasicBlock *fallthrough_block = FindBlock(cur_offset + width,
331 /*
332 * If the method is processed
333 * in sequential order from the
334 * beginning, we don't need to
335 * specify split for continue
336 * blocks. However, this
337 * routine can be called by
338 * compileLoop, which starts
339 * parsing the method from an
340 * arbitrary address in the
341 * method body.
342 */
343 true,
344 /* create */
345 true,
346 /* immed_pred_block_p */
347 &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700348 cur_block->fall_through = fallthrough_block->id;
349 fallthrough_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800350 } else if (code_ptr < code_end) {
buzbee27247762013-07-28 12:03:58 -0700351 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
buzbee311ca162013-02-28 15:56:43 -0800352 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800353 }
354 return cur_block;
355}
356
357/* Process instructions with the kSwitch flag */
buzbee17189ac2013-11-08 11:07:02 -0800358BasicBlock* MIRGraph::ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
359 int width, int flags) {
buzbee311ca162013-02-28 15:56:43 -0800360 const uint16_t* switch_data =
361 reinterpret_cast<const uint16_t*>(GetCurrentInsns() + cur_offset + insn->dalvikInsn.vB);
362 int size;
363 const int* keyTable;
364 const int* target_table;
365 int i;
366 int first_key;
367
368 /*
369 * Packed switch data format:
370 * ushort ident = 0x0100 magic value
371 * ushort size number of entries in the table
372 * int first_key first (and lowest) switch case value
373 * int targets[size] branch targets, relative to switch opcode
374 *
375 * Total size is (4+size*2) 16-bit code units.
376 */
377 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
378 DCHECK_EQ(static_cast<int>(switch_data[0]),
379 static_cast<int>(Instruction::kPackedSwitchSignature));
380 size = switch_data[1];
381 first_key = switch_data[2] | (switch_data[3] << 16);
382 target_table = reinterpret_cast<const int*>(&switch_data[4]);
383 keyTable = NULL; // Make the compiler happy
384 /*
385 * Sparse switch data format:
386 * ushort ident = 0x0200 magic value
387 * ushort size number of entries in the table; > 0
388 * int keys[size] keys, sorted low-to-high; 32-bit aligned
389 * int targets[size] branch targets, relative to switch opcode
390 *
391 * Total size is (2+size*4) 16-bit code units.
392 */
393 } else {
394 DCHECK_EQ(static_cast<int>(switch_data[0]),
395 static_cast<int>(Instruction::kSparseSwitchSignature));
396 size = switch_data[1];
397 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
398 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
399 first_key = 0; // To make the compiler happy
400 }
401
buzbee0d829482013-10-11 15:24:55 -0700402 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800403 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700404 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800405 }
buzbee0d829482013-10-11 15:24:55 -0700406 cur_block->successor_block_list_type =
407 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? kPackedSwitch : kSparseSwitch;
408 cur_block->successor_blocks =
Brian Carlstromdf629502013-07-17 22:39:56 -0700409 new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, size, kGrowableArraySuccessorBlocks);
buzbee311ca162013-02-28 15:56:43 -0800410
411 for (i = 0; i < size; i++) {
412 BasicBlock *case_block = FindBlock(cur_offset + target_table[i], /* split */ true,
413 /* create */ true, /* immed_pred_block_p */ &cur_block);
414 SuccessorBlockInfo *successor_block_info =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700415 static_cast<SuccessorBlockInfo*>(arena_->Alloc(sizeof(SuccessorBlockInfo),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000416 kArenaAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700417 successor_block_info->block = case_block->id;
buzbee311ca162013-02-28 15:56:43 -0800418 successor_block_info->key =
419 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
420 first_key + i : keyTable[i];
buzbee0d829482013-10-11 15:24:55 -0700421 cur_block->successor_blocks->Insert(successor_block_info);
422 case_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800423 }
424
425 /* Fall-through case */
Brian Carlstromdf629502013-07-17 22:39:56 -0700426 BasicBlock* fallthrough_block = FindBlock(cur_offset + width, /* split */ false,
427 /* create */ true, /* immed_pred_block_p */ NULL);
buzbee0d829482013-10-11 15:24:55 -0700428 cur_block->fall_through = fallthrough_block->id;
429 fallthrough_block->predecessors->Insert(cur_block->id);
buzbee17189ac2013-11-08 11:07:02 -0800430 return cur_block;
buzbee311ca162013-02-28 15:56:43 -0800431}
432
433/* Process instructions with the kThrow flag */
buzbee0d829482013-10-11 15:24:55 -0700434BasicBlock* MIRGraph::ProcessCanThrow(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
435 int width, int flags, ArenaBitVector* try_block_addr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700436 const uint16_t* code_ptr, const uint16_t* code_end) {
buzbee862a7602013-04-05 10:58:54 -0700437 bool in_try_block = try_block_addr->IsBitSet(cur_offset);
buzbee17189ac2013-11-08 11:07:02 -0800438 bool is_throw = (insn->dalvikInsn.opcode == Instruction::THROW);
439 bool build_all_edges =
440 (cu_->disable_opt & (1 << kSuppressExceptionEdges)) || is_throw || in_try_block;
buzbee311ca162013-02-28 15:56:43 -0800441
442 /* In try block */
443 if (in_try_block) {
444 CatchHandlerIterator iterator(*current_code_item_, cur_offset);
445
buzbee0d829482013-10-11 15:24:55 -0700446 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800447 LOG(INFO) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
448 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700449 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800450 }
451
buzbee0d829482013-10-11 15:24:55 -0700452 cur_block->successor_block_list_type = kCatch;
453 cur_block->successor_blocks =
buzbee862a7602013-04-05 10:58:54 -0700454 new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, 2, kGrowableArraySuccessorBlocks);
buzbee311ca162013-02-28 15:56:43 -0800455
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700456 for (; iterator.HasNext(); iterator.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800457 BasicBlock *catch_block = FindBlock(iterator.GetHandlerAddress(), false /* split*/,
458 false /* creat */, NULL /* immed_pred_block_p */);
459 catch_block->catch_entry = true;
460 if (kIsDebugBuild) {
461 catches_.insert(catch_block->start_offset);
462 }
463 SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000464 (arena_->Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700465 successor_block_info->block = catch_block->id;
buzbee311ca162013-02-28 15:56:43 -0800466 successor_block_info->key = iterator.GetHandlerTypeIndex();
buzbee0d829482013-10-11 15:24:55 -0700467 cur_block->successor_blocks->Insert(successor_block_info);
468 catch_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800469 }
buzbee17189ac2013-11-08 11:07:02 -0800470 } else if (build_all_edges) {
buzbee862a7602013-04-05 10:58:54 -0700471 BasicBlock *eh_block = NewMemBB(kExceptionHandling, num_blocks_++);
buzbee0d829482013-10-11 15:24:55 -0700472 cur_block->taken = eh_block->id;
buzbee862a7602013-04-05 10:58:54 -0700473 block_list_.Insert(eh_block);
buzbee311ca162013-02-28 15:56:43 -0800474 eh_block->start_offset = cur_offset;
buzbee0d829482013-10-11 15:24:55 -0700475 eh_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800476 }
477
buzbee17189ac2013-11-08 11:07:02 -0800478 if (is_throw) {
buzbee311ca162013-02-28 15:56:43 -0800479 cur_block->explicit_throw = true;
buzbee27247762013-07-28 12:03:58 -0700480 if (code_ptr < code_end) {
buzbee311ca162013-02-28 15:56:43 -0800481 // Force creation of new block following THROW via side-effect
482 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
483 /* immed_pred_block_p */ NULL);
484 }
485 if (!in_try_block) {
486 // Don't split a THROW that can't rethrow - we're done.
487 return cur_block;
488 }
489 }
490
buzbee17189ac2013-11-08 11:07:02 -0800491 if (!build_all_edges) {
492 /*
493 * Even though there is an exception edge here, control cannot return to this
494 * method. Thus, for the purposes of dataflow analysis and optimization, we can
495 * ignore the edge. Doing this reduces compile time, and increases the scope
496 * of the basic-block level optimization pass.
497 */
498 return cur_block;
499 }
500
buzbee311ca162013-02-28 15:56:43 -0800501 /*
502 * Split the potentially-throwing instruction into two parts.
503 * The first half will be a pseudo-op that captures the exception
504 * edges and terminates the basic block. It always falls through.
505 * Then, create a new basic block that begins with the throwing instruction
506 * (minus exceptions). Note: this new basic block must NOT be entered into
507 * the block_map. If the potentially-throwing instruction is the target of a
508 * future branch, we need to find the check psuedo half. The new
509 * basic block containing the work portion of the instruction should
510 * only be entered via fallthrough from the block containing the
511 * pseudo exception edge MIR. Note also that this new block is
512 * not automatically terminated after the work portion, and may
513 * contain following instructions.
buzbeeb48819d2013-09-14 16:15:25 -0700514 *
515 * Note also that the dex_pc_to_block_map_ entry for the potentially
516 * throwing instruction will refer to the original basic block.
buzbee311ca162013-02-28 15:56:43 -0800517 */
buzbee862a7602013-04-05 10:58:54 -0700518 BasicBlock *new_block = NewMemBB(kDalvikByteCode, num_blocks_++);
519 block_list_.Insert(new_block);
buzbee311ca162013-02-28 15:56:43 -0800520 new_block->start_offset = insn->offset;
buzbee0d829482013-10-11 15:24:55 -0700521 cur_block->fall_through = new_block->id;
522 new_block->predecessors->Insert(cur_block->id);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000523 MIR* new_insn = static_cast<MIR*>(arena_->Alloc(sizeof(MIR), kArenaAllocMIR));
buzbee311ca162013-02-28 15:56:43 -0800524 *new_insn = *insn;
525 insn->dalvikInsn.opcode =
526 static_cast<Instruction::Code>(kMirOpCheck);
527 // Associate the two halves
528 insn->meta.throw_insn = new_insn;
buzbee311ca162013-02-28 15:56:43 -0800529 AppendMIR(new_block, new_insn);
530 return new_block;
531}
532
533/* Parse a Dex method and insert it into the MIRGraph at the current insert point. */
534void MIRGraph::InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700535 InvokeType invoke_type, uint16_t class_def_idx,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700536 uint32_t method_idx, jobject class_loader, const DexFile& dex_file) {
buzbee311ca162013-02-28 15:56:43 -0800537 current_code_item_ = code_item;
538 method_stack_.push_back(std::make_pair(current_method_, current_offset_));
539 current_method_ = m_units_.size();
540 current_offset_ = 0;
541 // TODO: will need to snapshot stack image and use that as the mir context identification.
542 m_units_.push_back(new DexCompilationUnit(cu_, class_loader, Runtime::Current()->GetClassLinker(),
Vladimir Marko2730db02014-01-27 11:15:17 +0000543 dex_file, current_code_item_, class_def_idx, method_idx, access_flags,
544 cu_->compiler_driver->GetVerifiedMethod(&dex_file, method_idx)));
buzbee311ca162013-02-28 15:56:43 -0800545 const uint16_t* code_ptr = current_code_item_->insns_;
546 const uint16_t* code_end =
547 current_code_item_->insns_ + current_code_item_->insns_size_in_code_units_;
548
549 // TODO: need to rework expansion of block list & try_block_addr when inlining activated.
buzbeeb48819d2013-09-14 16:15:25 -0700550 // TUNING: use better estimate of basic blocks for following resize.
buzbee862a7602013-04-05 10:58:54 -0700551 block_list_.Resize(block_list_.Size() + current_code_item_->insns_size_in_code_units_);
buzbeeb48819d2013-09-14 16:15:25 -0700552 dex_pc_to_block_map_.SetSize(dex_pc_to_block_map_.Size() + current_code_item_->insns_size_in_code_units_);
buzbeebd663de2013-09-10 15:41:31 -0700553
buzbee311ca162013-02-28 15:56:43 -0800554 // TODO: replace with explicit resize routine. Using automatic extension side effect for now.
buzbee862a7602013-04-05 10:58:54 -0700555 try_block_addr_->SetBit(current_code_item_->insns_size_in_code_units_);
556 try_block_addr_->ClearBit(current_code_item_->insns_size_in_code_units_);
buzbee311ca162013-02-28 15:56:43 -0800557
558 // If this is the first method, set up default entry and exit blocks.
559 if (current_method_ == 0) {
560 DCHECK(entry_block_ == NULL);
561 DCHECK(exit_block_ == NULL);
Brian Carlstrom42748892013-07-18 18:04:08 -0700562 DCHECK_EQ(num_blocks_, 0);
buzbee0d829482013-10-11 15:24:55 -0700563 // Use id 0 to represent a null block.
564 BasicBlock* null_block = NewMemBB(kNullBlock, num_blocks_++);
565 DCHECK_EQ(null_block->id, NullBasicBlockId);
566 null_block->hidden = true;
567 block_list_.Insert(null_block);
buzbee862a7602013-04-05 10:58:54 -0700568 entry_block_ = NewMemBB(kEntryBlock, num_blocks_++);
buzbee862a7602013-04-05 10:58:54 -0700569 block_list_.Insert(entry_block_);
buzbee0d829482013-10-11 15:24:55 -0700570 exit_block_ = NewMemBB(kExitBlock, num_blocks_++);
buzbee862a7602013-04-05 10:58:54 -0700571 block_list_.Insert(exit_block_);
buzbee311ca162013-02-28 15:56:43 -0800572 // TODO: deprecate all "cu->" fields; move what's left to wherever CompilationUnit is allocated.
573 cu_->dex_file = &dex_file;
574 cu_->class_def_idx = class_def_idx;
575 cu_->method_idx = method_idx;
576 cu_->access_flags = access_flags;
577 cu_->invoke_type = invoke_type;
578 cu_->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
579 cu_->num_ins = current_code_item_->ins_size_;
580 cu_->num_regs = current_code_item_->registers_size_ - cu_->num_ins;
581 cu_->num_outs = current_code_item_->outs_size_;
582 cu_->num_dalvik_registers = current_code_item_->registers_size_;
583 cu_->insns = current_code_item_->insns_;
584 cu_->code_item = current_code_item_;
585 } else {
586 UNIMPLEMENTED(FATAL) << "Nested inlining not implemented.";
587 /*
588 * Will need to manage storage for ins & outs, push prevous state and update
589 * insert point.
590 */
591 }
592
593 /* Current block to record parsed instructions */
buzbee862a7602013-04-05 10:58:54 -0700594 BasicBlock *cur_block = NewMemBB(kDalvikByteCode, num_blocks_++);
buzbee0d829482013-10-11 15:24:55 -0700595 DCHECK_EQ(current_offset_, 0U);
buzbee311ca162013-02-28 15:56:43 -0800596 cur_block->start_offset = current_offset_;
buzbee862a7602013-04-05 10:58:54 -0700597 block_list_.Insert(cur_block);
buzbee0d829482013-10-11 15:24:55 -0700598 // TODO: for inlining support, insert at the insert point rather than entry block.
599 entry_block_->fall_through = cur_block->id;
600 cur_block->predecessors->Insert(entry_block_->id);
buzbee311ca162013-02-28 15:56:43 -0800601
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000602 /* Identify code range in try blocks and set up the empty catch blocks */
buzbee311ca162013-02-28 15:56:43 -0800603 ProcessTryCatchBlocks();
604
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000605 uint64_t merged_df_flags = 0u;
606
buzbee311ca162013-02-28 15:56:43 -0800607 /* Parse all instructions and put them into containing basic blocks */
608 while (code_ptr < code_end) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000609 MIR *insn = static_cast<MIR *>(arena_->Alloc(sizeof(MIR), kArenaAllocMIR));
buzbee311ca162013-02-28 15:56:43 -0800610 insn->offset = current_offset_;
611 insn->m_unit_index = current_method_;
612 int width = ParseInsn(code_ptr, &insn->dalvikInsn);
613 insn->width = width;
614 Instruction::Code opcode = insn->dalvikInsn.opcode;
615 if (opcode_count_ != NULL) {
616 opcode_count_[static_cast<int>(opcode)]++;
617 }
618
buzbee311ca162013-02-28 15:56:43 -0800619 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbeeb1f1d642014-02-27 12:55:32 -0800620 int verify_flags = Instruction::VerifyFlagsOf(insn->dalvikInsn.opcode);
buzbee311ca162013-02-28 15:56:43 -0800621
buzbee1da1e2f2013-11-15 13:37:01 -0800622 uint64_t df_flags = oat_data_flow_attributes_[insn->dalvikInsn.opcode];
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000623 merged_df_flags |= df_flags;
buzbee311ca162013-02-28 15:56:43 -0800624
625 if (df_flags & DF_HAS_DEFS) {
626 def_count_ += (df_flags & DF_A_WIDE) ? 2 : 1;
627 }
628
buzbee1da1e2f2013-11-15 13:37:01 -0800629 if (df_flags & DF_LVN) {
630 cur_block->use_lvn = true; // Run local value numbering on this basic block.
631 }
632
buzbee27247762013-07-28 12:03:58 -0700633 // Check for inline data block signatures
634 if (opcode == Instruction::NOP) {
635 // A simple NOP will have a width of 1 at this point, embedded data NOP > 1.
636 if ((width == 1) && ((current_offset_ & 0x1) == 0x1) && ((code_end - code_ptr) > 1)) {
637 // Could be an aligning nop. If an embedded data NOP follows, treat pair as single unit.
638 uint16_t following_raw_instruction = code_ptr[1];
639 if ((following_raw_instruction == Instruction::kSparseSwitchSignature) ||
640 (following_raw_instruction == Instruction::kPackedSwitchSignature) ||
641 (following_raw_instruction == Instruction::kArrayDataSignature)) {
642 width += Instruction::At(code_ptr + 1)->SizeInCodeUnits();
643 }
644 }
645 if (width == 1) {
646 // It is a simple nop - treat normally.
647 AppendMIR(cur_block, insn);
648 } else {
buzbee0d829482013-10-11 15:24:55 -0700649 DCHECK(cur_block->fall_through == NullBasicBlockId);
650 DCHECK(cur_block->taken == NullBasicBlockId);
buzbee27247762013-07-28 12:03:58 -0700651 // Unreachable instruction, mark for no continuation.
652 flags &= ~Instruction::kContinue;
653 }
654 } else {
655 AppendMIR(cur_block, insn);
656 }
657
buzbeeb48819d2013-09-14 16:15:25 -0700658 // Associate the starting dex_pc for this opcode with its containing basic block.
659 dex_pc_to_block_map_.Put(insn->offset, cur_block->id);
660
buzbee27247762013-07-28 12:03:58 -0700661 code_ptr += width;
662
buzbee311ca162013-02-28 15:56:43 -0800663 if (flags & Instruction::kBranch) {
664 cur_block = ProcessCanBranch(cur_block, insn, current_offset_,
665 width, flags, code_ptr, code_end);
666 } else if (flags & Instruction::kReturn) {
667 cur_block->terminated_by_return = true;
buzbee0d829482013-10-11 15:24:55 -0700668 cur_block->fall_through = exit_block_->id;
669 exit_block_->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800670 /*
671 * Terminate the current block if there are instructions
672 * afterwards.
673 */
674 if (code_ptr < code_end) {
675 /*
676 * Create a fallthrough block for real instructions
677 * (incl. NOP).
678 */
buzbee27247762013-07-28 12:03:58 -0700679 FindBlock(current_offset_ + width, /* split */ false, /* create */ true,
680 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800681 }
682 } else if (flags & Instruction::kThrow) {
683 cur_block = ProcessCanThrow(cur_block, insn, current_offset_, width, flags, try_block_addr_,
684 code_ptr, code_end);
685 } else if (flags & Instruction::kSwitch) {
buzbee17189ac2013-11-08 11:07:02 -0800686 cur_block = ProcessCanSwitch(cur_block, insn, current_offset_, width, flags);
buzbee311ca162013-02-28 15:56:43 -0800687 }
buzbeeb1f1d642014-02-27 12:55:32 -0800688 if (verify_flags & Instruction::kVerifyVarArgRange) {
689 /*
690 * The Quick backend's runtime model includes a gap between a method's
691 * argument ("in") vregs and the rest of its vregs. Handling a range instruction
692 * which spans the gap is somewhat complicated, and should not happen
693 * in normal usage of dx. Punt to the interpreter.
694 */
695 int first_reg_in_range = insn->dalvikInsn.vC;
696 int last_reg_in_range = first_reg_in_range + insn->dalvikInsn.vA - 1;
697 if (IsInVReg(first_reg_in_range) != IsInVReg(last_reg_in_range)) {
698 punt_to_interpreter_ = true;
699 }
700 }
buzbee311ca162013-02-28 15:56:43 -0800701 current_offset_ += width;
702 BasicBlock *next_block = FindBlock(current_offset_, /* split */ false, /* create */
703 false, /* immed_pred_block_p */ NULL);
704 if (next_block) {
705 /*
706 * The next instruction could be the target of a previously parsed
707 * forward branch so a block is already created. If the current
708 * instruction is not an unconditional branch, connect them through
709 * the fall-through link.
710 */
buzbee0d829482013-10-11 15:24:55 -0700711 DCHECK(cur_block->fall_through == NullBasicBlockId ||
712 GetBasicBlock(cur_block->fall_through) == next_block ||
713 GetBasicBlock(cur_block->fall_through) == exit_block_);
buzbee311ca162013-02-28 15:56:43 -0800714
buzbee0d829482013-10-11 15:24:55 -0700715 if ((cur_block->fall_through == NullBasicBlockId) && (flags & Instruction::kContinue)) {
716 cur_block->fall_through = next_block->id;
717 next_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800718 }
719 cur_block = next_block;
720 }
721 }
Vladimir Marko3d73ba22014-03-06 15:18:04 +0000722 merged_df_flags_ = merged_df_flags;
Vladimir Marko5816ed42013-11-27 17:04:20 +0000723
buzbee311ca162013-02-28 15:56:43 -0800724 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
725 DumpCFG("/sdcard/1_post_parse_cfg/", true);
726 }
727
728 if (cu_->verbose) {
buzbee1fd33462013-03-25 13:40:45 -0700729 DumpMIRGraph();
buzbee311ca162013-02-28 15:56:43 -0800730 }
731}
732
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700733void MIRGraph::ShowOpcodeStats() {
buzbee311ca162013-02-28 15:56:43 -0800734 DCHECK(opcode_count_ != NULL);
735 LOG(INFO) << "Opcode Count";
736 for (int i = 0; i < kNumPackedOpcodes; i++) {
737 if (opcode_count_[i] != 0) {
738 LOG(INFO) << "-C- " << Instruction::Name(static_cast<Instruction::Code>(i))
739 << " " << opcode_count_[i];
740 }
741 }
742}
743
744// TODO: use a configurable base prefix, and adjust callers to supply pass name.
745/* Dump the CFG into a DOT graph */
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800746void MIRGraph::DumpCFG(const char* dir_prefix, bool all_blocks, const char *suffix) {
buzbee311ca162013-02-28 15:56:43 -0800747 FILE* file;
748 std::string fname(PrettyMethod(cu_->method_idx, *cu_->dex_file));
749 ReplaceSpecialChars(fname);
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800750 fname = StringPrintf("%s%s%x%s.dot", dir_prefix, fname.c_str(),
751 GetBasicBlock(GetEntryBlock()->fall_through)->start_offset,
752 suffix == nullptr ? "" : suffix);
buzbee311ca162013-02-28 15:56:43 -0800753 file = fopen(fname.c_str(), "w");
754 if (file == NULL) {
755 return;
756 }
757 fprintf(file, "digraph G {\n");
758
759 fprintf(file, " rankdir=TB\n");
760
761 int num_blocks = all_blocks ? GetNumBlocks() : num_reachable_blocks_;
762 int idx;
763
764 for (idx = 0; idx < num_blocks; idx++) {
buzbee862a7602013-04-05 10:58:54 -0700765 int block_idx = all_blocks ? idx : dfs_order_->Get(idx);
buzbee311ca162013-02-28 15:56:43 -0800766 BasicBlock *bb = GetBasicBlock(block_idx);
767 if (bb == NULL) break;
768 if (bb->block_type == kDead) continue;
769 if (bb->block_type == kEntryBlock) {
770 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
771 } else if (bb->block_type == kExitBlock) {
772 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
773 } else if (bb->block_type == kDalvikByteCode) {
774 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
775 bb->start_offset, bb->id);
776 const MIR *mir;
777 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
778 bb->first_mir_insn ? " | " : " ");
779 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
780 int opcode = mir->dalvikInsn.opcode;
781 fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset,
buzbee1fd33462013-03-25 13:40:45 -0700782 mir->ssa_rep ? GetDalvikDisassembly(mir) :
buzbee311ca162013-02-28 15:56:43 -0800783 (opcode < kMirOpFirst) ? Instruction::Name(mir->dalvikInsn.opcode) :
buzbee1fd33462013-03-25 13:40:45 -0700784 extended_mir_op_names_[opcode - kMirOpFirst],
buzbee311ca162013-02-28 15:56:43 -0800785 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
786 (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
787 mir->next ? " | " : " ");
788 }
789 fprintf(file, " }\"];\n\n");
790 } else if (bb->block_type == kExceptionHandling) {
791 char block_name[BLOCK_NAME_LEN];
792
793 GetBlockName(bb, block_name);
794 fprintf(file, " %s [shape=invhouse];\n", block_name);
795 }
796
797 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
798
buzbee0d829482013-10-11 15:24:55 -0700799 if (bb->taken != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800800 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700801 GetBlockName(GetBasicBlock(bb->taken), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800802 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
803 block_name1, block_name2);
804 }
buzbee0d829482013-10-11 15:24:55 -0700805 if (bb->fall_through != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800806 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700807 GetBlockName(GetBasicBlock(bb->fall_through), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800808 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
809 }
810
buzbee0d829482013-10-11 15:24:55 -0700811 if (bb->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800812 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
813 bb->start_offset, bb->id,
buzbee0d829482013-10-11 15:24:55 -0700814 (bb->successor_block_list_type == kCatch) ? "Mrecord" : "record");
815 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
buzbee862a7602013-04-05 10:58:54 -0700816 SuccessorBlockInfo *successor_block_info = iterator.Next();
buzbee311ca162013-02-28 15:56:43 -0800817
818 int succ_id = 0;
819 while (true) {
820 if (successor_block_info == NULL) break;
821
buzbee0d829482013-10-11 15:24:55 -0700822 BasicBlock *dest_block = GetBasicBlock(successor_block_info->block);
buzbee862a7602013-04-05 10:58:54 -0700823 SuccessorBlockInfo *next_successor_block_info = iterator.Next();
buzbee311ca162013-02-28 15:56:43 -0800824
825 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
826 succ_id++,
827 successor_block_info->key,
828 dest_block->start_offset,
829 (next_successor_block_info != NULL) ? " | " : " ");
830
831 successor_block_info = next_successor_block_info;
832 }
833 fprintf(file, " }\"];\n\n");
834
835 GetBlockName(bb, block_name1);
836 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
837 block_name1, bb->start_offset, bb->id);
838
buzbee0d829482013-10-11 15:24:55 -0700839 if (bb->successor_block_list_type == kPackedSwitch ||
840 bb->successor_block_list_type == kSparseSwitch) {
841 GrowableArray<SuccessorBlockInfo*>::Iterator iter(bb->successor_blocks);
buzbee311ca162013-02-28 15:56:43 -0800842
843 succ_id = 0;
844 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700845 SuccessorBlockInfo *successor_block_info = iter.Next();
buzbee311ca162013-02-28 15:56:43 -0800846 if (successor_block_info == NULL) break;
847
buzbee0d829482013-10-11 15:24:55 -0700848 BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800849
850 GetBlockName(dest_block, block_name2);
851 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
852 bb->id, succ_id++, block_name2);
853 }
854 }
855 }
856 fprintf(file, "\n");
857
858 if (cu_->verbose) {
859 /* Display the dominator tree */
860 GetBlockName(bb, block_name1);
861 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
862 block_name1, block_name1);
863 if (bb->i_dom) {
buzbee0d829482013-10-11 15:24:55 -0700864 GetBlockName(GetBasicBlock(bb->i_dom), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800865 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
866 }
867 }
868 }
869 fprintf(file, "}\n");
870 fclose(file);
871}
872
buzbee1fd33462013-03-25 13:40:45 -0700873/* Insert an MIR instruction to the end of a basic block */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700874void MIRGraph::AppendMIR(BasicBlock* bb, MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700875 if (bb->first_mir_insn == NULL) {
876 DCHECK(bb->last_mir_insn == NULL);
877 bb->last_mir_insn = bb->first_mir_insn = mir;
buzbee0d829482013-10-11 15:24:55 -0700878 mir->next = NULL;
buzbee1fd33462013-03-25 13:40:45 -0700879 } else {
880 bb->last_mir_insn->next = mir;
buzbee1fd33462013-03-25 13:40:45 -0700881 mir->next = NULL;
882 bb->last_mir_insn = mir;
883 }
884}
885
886/* Insert an MIR instruction to the head of a basic block */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700887void MIRGraph::PrependMIR(BasicBlock* bb, MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700888 if (bb->first_mir_insn == NULL) {
889 DCHECK(bb->last_mir_insn == NULL);
890 bb->last_mir_insn = bb->first_mir_insn = mir;
buzbee0d829482013-10-11 15:24:55 -0700891 mir->next = NULL;
buzbee1fd33462013-03-25 13:40:45 -0700892 } else {
buzbee1fd33462013-03-25 13:40:45 -0700893 mir->next = bb->first_mir_insn;
buzbee1fd33462013-03-25 13:40:45 -0700894 bb->first_mir_insn = mir;
895 }
896}
897
898/* Insert a MIR instruction after the specified MIR */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700899void MIRGraph::InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir) {
buzbee1fd33462013-03-25 13:40:45 -0700900 new_mir->next = current_mir->next;
901 current_mir->next = new_mir;
902
buzbee0d829482013-10-11 15:24:55 -0700903 if (bb->last_mir_insn == current_mir) {
buzbee1fd33462013-03-25 13:40:45 -0700904 /* Is the last MIR in the block */
905 bb->last_mir_insn = new_mir;
906 }
907}
908
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800909MIR* MIRGraph::GetNextUnconditionalMir(BasicBlock* bb, MIR* current) {
910 MIR* next_mir = nullptr;
911
912 if (current != nullptr) {
913 next_mir = current->next;
914 }
915
916 if (next_mir == nullptr) {
917 // Only look for next MIR that follows unconditionally.
918 if ((bb->taken == NullBasicBlockId) && (bb->fall_through != NullBasicBlockId)) {
919 next_mir = GetBasicBlock(bb->fall_through)->first_mir_insn;
920 }
921 }
922
923 return next_mir;
924}
925
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700926char* MIRGraph::GetDalvikDisassembly(const MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700927 DecodedInstruction insn = mir->dalvikInsn;
928 std::string str;
929 int flags = 0;
930 int opcode = insn.opcode;
931 char* ret;
932 bool nop = false;
933 SSARepresentation* ssa_rep = mir->ssa_rep;
934 Instruction::Format dalvik_format = Instruction::k10x; // Default to no-operand format
935 int defs = (ssa_rep != NULL) ? ssa_rep->num_defs : 0;
936 int uses = (ssa_rep != NULL) ? ssa_rep->num_uses : 0;
937
938 // Handle special cases.
939 if ((opcode == kMirOpCheck) || (opcode == kMirOpCheckPart2)) {
940 str.append(extended_mir_op_names_[opcode - kMirOpFirst]);
941 str.append(": ");
942 // Recover the original Dex instruction
943 insn = mir->meta.throw_insn->dalvikInsn;
944 ssa_rep = mir->meta.throw_insn->ssa_rep;
945 defs = ssa_rep->num_defs;
946 uses = ssa_rep->num_uses;
947 opcode = insn.opcode;
948 } else if (opcode == kMirOpNop) {
949 str.append("[");
buzbee0d829482013-10-11 15:24:55 -0700950 // Recover original opcode.
951 insn.opcode = Instruction::At(current_code_item_->insns_ + mir->offset)->Opcode();
952 opcode = insn.opcode;
buzbee1fd33462013-03-25 13:40:45 -0700953 nop = true;
954 }
955
956 if (opcode >= kMirOpFirst) {
957 str.append(extended_mir_op_names_[opcode - kMirOpFirst]);
958 } else {
959 dalvik_format = Instruction::FormatOf(insn.opcode);
960 flags = Instruction::FlagsOf(insn.opcode);
961 str.append(Instruction::Name(insn.opcode));
962 }
963
964 if (opcode == kMirOpPhi) {
buzbee0d829482013-10-11 15:24:55 -0700965 BasicBlockId* incoming = mir->meta.phi_incoming;
buzbee1fd33462013-03-25 13:40:45 -0700966 str.append(StringPrintf(" %s = (%s",
967 GetSSANameWithConst(ssa_rep->defs[0], true).c_str(),
968 GetSSANameWithConst(ssa_rep->uses[0], true).c_str()));
Brian Carlstromb1eba212013-07-17 18:07:19 -0700969 str.append(StringPrintf(":%d", incoming[0]));
buzbee1fd33462013-03-25 13:40:45 -0700970 int i;
971 for (i = 1; i < uses; i++) {
972 str.append(StringPrintf(", %s:%d",
973 GetSSANameWithConst(ssa_rep->uses[i], true).c_str(),
974 incoming[i]));
975 }
976 str.append(")");
977 } else if ((flags & Instruction::kBranch) != 0) {
978 // For branches, decode the instructions to print out the branch targets.
979 int offset = 0;
980 switch (dalvik_format) {
981 case Instruction::k21t:
982 str.append(StringPrintf(" %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
983 offset = insn.vB;
984 break;
985 case Instruction::k22t:
986 str.append(StringPrintf(" %s, %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str(),
987 GetSSANameWithConst(ssa_rep->uses[1], false).c_str()));
988 offset = insn.vC;
989 break;
990 case Instruction::k10t:
991 case Instruction::k20t:
992 case Instruction::k30t:
993 offset = insn.vA;
994 break;
995 default:
996 LOG(FATAL) << "Unexpected branch format " << dalvik_format << " from " << insn.opcode;
997 }
998 str.append(StringPrintf(" 0x%x (%c%x)", mir->offset + offset,
999 offset > 0 ? '+' : '-', offset > 0 ? offset : -offset));
1000 } else {
1001 // For invokes-style formats, treat wide regs as a pair of singles
1002 bool show_singles = ((dalvik_format == Instruction::k35c) ||
1003 (dalvik_format == Instruction::k3rc));
1004 if (defs != 0) {
1005 str.append(StringPrintf(" %s", GetSSANameWithConst(ssa_rep->defs[0], false).c_str()));
1006 if (uses != 0) {
1007 str.append(", ");
1008 }
1009 }
1010 for (int i = 0; i < uses; i++) {
1011 str.append(
1012 StringPrintf(" %s", GetSSANameWithConst(ssa_rep->uses[i], show_singles).c_str()));
1013 if (!show_singles && (reg_location_ != NULL) && reg_location_[i].wide) {
1014 // For the listing, skip the high sreg.
1015 i++;
1016 }
1017 if (i != (uses -1)) {
1018 str.append(",");
1019 }
1020 }
1021 switch (dalvik_format) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001022 case Instruction::k11n: // Add one immediate from vB
buzbee1fd33462013-03-25 13:40:45 -07001023 case Instruction::k21s:
1024 case Instruction::k31i:
1025 case Instruction::k21h:
1026 str.append(StringPrintf(", #%d", insn.vB));
1027 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001028 case Instruction::k51l: // Add one wide immediate
Ian Rogers23b03b52014-01-24 11:10:03 -08001029 str.append(StringPrintf(", #%" PRId64, insn.vB_wide));
buzbee1fd33462013-03-25 13:40:45 -07001030 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001031 case Instruction::k21c: // One register, one string/type/method index
buzbee1fd33462013-03-25 13:40:45 -07001032 case Instruction::k31c:
1033 str.append(StringPrintf(", index #%d", insn.vB));
1034 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001035 case Instruction::k22c: // Two registers, one string/type/method index
buzbee1fd33462013-03-25 13:40:45 -07001036 str.append(StringPrintf(", index #%d", insn.vC));
1037 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001038 case Instruction::k22s: // Add one immediate from vC
buzbee1fd33462013-03-25 13:40:45 -07001039 case Instruction::k22b:
1040 str.append(StringPrintf(", #%d", insn.vC));
1041 break;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001042 default: {
1043 // Nothing left to print
buzbee1fd33462013-03-25 13:40:45 -07001044 }
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001045 }
buzbee1fd33462013-03-25 13:40:45 -07001046 }
1047 if (nop) {
1048 str.append("]--optimized away");
1049 }
1050 int length = str.length() + 1;
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001051 ret = static_cast<char*>(arena_->Alloc(length, kArenaAllocDFInfo));
buzbee1fd33462013-03-25 13:40:45 -07001052 strncpy(ret, str.c_str(), length);
1053 return ret;
1054}
1055
1056/* Turn method name into a legal Linux file name */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001057void MIRGraph::ReplaceSpecialChars(std::string& str) {
Brian Carlstrom9b7085a2013-07-18 15:15:21 -07001058 static const struct { const char before; const char after; } match[] = {
1059 {'/', '-'}, {';', '#'}, {' ', '#'}, {'$', '+'},
1060 {'(', '@'}, {')', '@'}, {'<', '='}, {'>', '='}
1061 };
buzbee1fd33462013-03-25 13:40:45 -07001062 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
1063 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
1064 }
1065}
1066
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001067std::string MIRGraph::GetSSAName(int ssa_reg) {
Ian Rogers39ebcb82013-05-30 16:57:23 -07001068 // TODO: This value is needed for LLVM and debugging. Currently, we compute this and then copy to
1069 // the arena. We should be smarter and just place straight into the arena, or compute the
1070 // value more lazily.
buzbee1fd33462013-03-25 13:40:45 -07001071 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1072}
1073
1074// Similar to GetSSAName, but if ssa name represents an immediate show that as well.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001075std::string MIRGraph::GetSSANameWithConst(int ssa_reg, bool singles_only) {
buzbee1fd33462013-03-25 13:40:45 -07001076 if (reg_location_ == NULL) {
1077 // Pre-SSA - just use the standard name
1078 return GetSSAName(ssa_reg);
1079 }
1080 if (IsConst(reg_location_[ssa_reg])) {
1081 if (!singles_only && reg_location_[ssa_reg].wide) {
Ian Rogers23b03b52014-01-24 11:10:03 -08001082 return StringPrintf("v%d_%d#0x%" PRIx64, SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001083 ConstantValueWide(reg_location_[ssa_reg]));
1084 } else {
Brian Carlstromb1eba212013-07-17 18:07:19 -07001085 return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001086 ConstantValue(reg_location_[ssa_reg]));
1087 }
1088 } else {
1089 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1090 }
1091}
1092
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001093void MIRGraph::GetBlockName(BasicBlock* bb, char* name) {
buzbee1fd33462013-03-25 13:40:45 -07001094 switch (bb->block_type) {
1095 case kEntryBlock:
1096 snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id);
1097 break;
1098 case kExitBlock:
1099 snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id);
1100 break;
1101 case kDalvikByteCode:
1102 snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id);
1103 break;
1104 case kExceptionHandling:
1105 snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset,
1106 bb->id);
1107 break;
1108 default:
1109 snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id);
1110 break;
1111 }
1112}
1113
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001114const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) {
buzbee0d829482013-10-11 15:24:55 -07001115 // TODO: for inlining support, use current code unit.
buzbee1fd33462013-03-25 13:40:45 -07001116 const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx);
1117 return cu_->dex_file->GetShorty(method_id.proto_idx_);
1118}
1119
1120/* Debug Utility - dump a compilation unit */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001121void MIRGraph::DumpMIRGraph() {
buzbee1fd33462013-03-25 13:40:45 -07001122 BasicBlock* bb;
1123 const char* block_type_names[] = {
buzbee17189ac2013-11-08 11:07:02 -08001124 "Null Block",
buzbee1fd33462013-03-25 13:40:45 -07001125 "Entry Block",
1126 "Code Block",
1127 "Exit Block",
1128 "Exception Handling",
1129 "Catch Block"
1130 };
1131
1132 LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
1133 LOG(INFO) << cu_->insns << " insns";
1134 LOG(INFO) << GetNumBlocks() << " blocks in total";
buzbee862a7602013-04-05 10:58:54 -07001135 GrowableArray<BasicBlock*>::Iterator iterator(&block_list_);
buzbee1fd33462013-03-25 13:40:45 -07001136
1137 while (true) {
buzbee862a7602013-04-05 10:58:54 -07001138 bb = iterator.Next();
buzbee1fd33462013-03-25 13:40:45 -07001139 if (bb == NULL) break;
1140 LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)",
1141 bb->id,
1142 block_type_names[bb->block_type],
1143 bb->start_offset,
1144 bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset,
1145 bb->last_mir_insn ? "" : " empty");
buzbee0d829482013-10-11 15:24:55 -07001146 if (bb->taken != NullBasicBlockId) {
1147 LOG(INFO) << " Taken branch: block " << bb->taken
1148 << "(0x" << std::hex << GetBasicBlock(bb->taken)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001149 }
buzbee0d829482013-10-11 15:24:55 -07001150 if (bb->fall_through != NullBasicBlockId) {
1151 LOG(INFO) << " Fallthrough : block " << bb->fall_through
1152 << " (0x" << std::hex << GetBasicBlock(bb->fall_through)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001153 }
1154 }
1155}
1156
1157/*
1158 * Build an array of location records for the incoming arguments.
1159 * Note: one location record per word of arguments, with dummy
1160 * high-word loc for wide arguments. Also pull up any following
1161 * MOVE_RESULT and incorporate it into the invoke.
1162 */
1163CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001164 bool is_range) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001165 CallInfo* info = static_cast<CallInfo*>(arena_->Alloc(sizeof(CallInfo),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001166 kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001167 MIR* move_result_mir = FindMoveResult(bb, mir);
1168 if (move_result_mir == NULL) {
1169 info->result.location = kLocInvalid;
1170 } else {
1171 info->result = GetRawDest(move_result_mir);
buzbee1fd33462013-03-25 13:40:45 -07001172 move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1173 }
1174 info->num_arg_words = mir->ssa_rep->num_uses;
1175 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001176 (arena_->Alloc(sizeof(RegLocation) * info->num_arg_words, kArenaAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001177 for (int i = 0; i < info->num_arg_words; i++) {
1178 info->args[i] = GetRawSrc(mir, i);
1179 }
1180 info->opt_flags = mir->optimization_flags;
1181 info->type = type;
1182 info->is_range = is_range;
1183 info->index = mir->dalvikInsn.vB;
1184 info->offset = mir->offset;
Vladimir Markof096aad2014-01-23 15:51:58 +00001185 info->mir = mir;
buzbee1fd33462013-03-25 13:40:45 -07001186 return info;
1187}
1188
buzbee862a7602013-04-05 10:58:54 -07001189// Allocate a new basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001190BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001191 BasicBlock* bb = static_cast<BasicBlock*>(arena_->Alloc(sizeof(BasicBlock),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001192 kArenaAllocBB));
buzbee862a7602013-04-05 10:58:54 -07001193 bb->block_type = block_type;
1194 bb->id = block_id;
1195 // TUNING: better estimate of the exit block predecessors?
buzbee0d829482013-10-11 15:24:55 -07001196 bb->predecessors = new (arena_) GrowableArray<BasicBlockId>(arena_,
Brian Carlstromdf629502013-07-17 22:39:56 -07001197 (block_type == kExitBlock) ? 2048 : 2,
1198 kGrowableArrayPredecessors);
buzbee0d829482013-10-11 15:24:55 -07001199 bb->successor_block_list_type = kNotUsed;
buzbee862a7602013-04-05 10:58:54 -07001200 block_id_map_.Put(block_id, block_id);
1201 return bb;
1202}
buzbee1fd33462013-03-25 13:40:45 -07001203
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001204void MIRGraph::InitializeConstantPropagation() {
1205 is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001206 constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(), kArenaAllocDFInfo));
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001207}
1208
1209void MIRGraph::InitializeMethodUses() {
1210 // The gate starts by initializing the use counts
1211 int num_ssa_regs = GetNumSSARegs();
1212 use_counts_.Resize(num_ssa_regs + 32);
1213 raw_use_counts_.Resize(num_ssa_regs + 32);
1214 // Initialize list
1215 for (int i = 0; i < num_ssa_regs; i++) {
1216 use_counts_.Insert(0);
1217 raw_use_counts_.Insert(0);
1218 }
1219}
1220
1221void MIRGraph::InitializeSSATransformation() {
1222 /* Compute the DFS order */
1223 ComputeDFSOrders();
1224
1225 /* Compute the dominator info */
1226 ComputeDominators();
1227
1228 /* Allocate data structures in preparation for SSA conversion */
1229 CompilerInitializeSSAConversion();
1230
1231 /* Find out the "Dalvik reg def x block" relation */
1232 ComputeDefBlockMatrix();
1233
1234 /* Insert phi nodes to dominance frontiers for all variables */
1235 InsertPhiNodes();
1236
1237 /* Rename register names by local defs and phi nodes */
1238 ClearAllVisitedFlags();
1239 DoDFSPreOrderSSARename(GetEntryBlock());
1240
1241 /*
1242 * Shared temp bit vector used by each block to count the number of defs
1243 * from all the predecessor blocks.
1244 */
1245 temp_ssa_register_v_ =
1246 new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapTempSSARegisterV);
1247}
1248
1249void MIRGraph::CheckSSARegisterVector() {
1250 DCHECK(temp_ssa_register_v_ != nullptr);
1251}
1252
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001253} // namespace art