blob: 2bfc15459a8d455ca6febf00a74eca54b9403330 [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),
90 punt_to_interpreter_(false) {
buzbee862a7602013-04-05 10:58:54 -070091 try_block_addr_ = new (arena_) ArenaBitVector(arena_, 0, true /* expandable */);
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -080092 max_available_special_compiler_temps_ = std::abs(static_cast<int>(kVRegNonSpecialTempBaseReg))
93 - std::abs(static_cast<int>(kVRegTempBaseReg));
buzbee311ca162013-02-28 15:56:43 -080094}
95
Ian Rogers6282dc12013-04-18 15:54:02 -070096MIRGraph::~MIRGraph() {
97 STLDeleteElements(&m_units_);
98}
99
buzbee311ca162013-02-28 15:56:43 -0800100/*
101 * Parse an instruction, return the length of the instruction
102 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700103int MIRGraph::ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction) {
buzbee311ca162013-02-28 15:56:43 -0800104 const Instruction* instruction = Instruction::At(code_ptr);
105 *decoded_instruction = DecodedInstruction(instruction);
106
107 return instruction->SizeInCodeUnits();
108}
109
110
111/* Split an existing block from the specified code offset into two */
buzbee0d829482013-10-11 15:24:55 -0700112BasicBlock* MIRGraph::SplitBlock(DexOffset code_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700113 BasicBlock* orig_block, BasicBlock** immed_pred_block_p) {
buzbee0d829482013-10-11 15:24:55 -0700114 DCHECK_GT(code_offset, orig_block->start_offset);
buzbee311ca162013-02-28 15:56:43 -0800115 MIR* insn = orig_block->first_mir_insn;
buzbee0d829482013-10-11 15:24:55 -0700116 MIR* prev = NULL;
buzbee311ca162013-02-28 15:56:43 -0800117 while (insn) {
118 if (insn->offset == code_offset) break;
buzbee0d829482013-10-11 15:24:55 -0700119 prev = insn;
buzbee311ca162013-02-28 15:56:43 -0800120 insn = insn->next;
121 }
122 if (insn == NULL) {
123 LOG(FATAL) << "Break split failed";
124 }
buzbee862a7602013-04-05 10:58:54 -0700125 BasicBlock *bottom_block = NewMemBB(kDalvikByteCode, num_blocks_++);
126 block_list_.Insert(bottom_block);
buzbee311ca162013-02-28 15:56:43 -0800127
128 bottom_block->start_offset = code_offset;
129 bottom_block->first_mir_insn = insn;
130 bottom_block->last_mir_insn = orig_block->last_mir_insn;
131
132 /* If this block was terminated by a return, the flag needs to go with the bottom block */
133 bottom_block->terminated_by_return = orig_block->terminated_by_return;
134 orig_block->terminated_by_return = false;
135
buzbee311ca162013-02-28 15:56:43 -0800136 /* Handle the taken path */
137 bottom_block->taken = orig_block->taken;
buzbee0d829482013-10-11 15:24:55 -0700138 if (bottom_block->taken != NullBasicBlockId) {
139 orig_block->taken = NullBasicBlockId;
140 BasicBlock* bb_taken = GetBasicBlock(bottom_block->taken);
141 bb_taken->predecessors->Delete(orig_block->id);
142 bb_taken->predecessors->Insert(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800143 }
144
145 /* Handle the fallthrough path */
146 bottom_block->fall_through = orig_block->fall_through;
buzbee0d829482013-10-11 15:24:55 -0700147 orig_block->fall_through = bottom_block->id;
148 bottom_block->predecessors->Insert(orig_block->id);
149 if (bottom_block->fall_through != NullBasicBlockId) {
150 BasicBlock* bb_fall_through = GetBasicBlock(bottom_block->fall_through);
151 bb_fall_through->predecessors->Delete(orig_block->id);
152 bb_fall_through->predecessors->Insert(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800153 }
154
155 /* Handle the successor list */
buzbee0d829482013-10-11 15:24:55 -0700156 if (orig_block->successor_block_list_type != kNotUsed) {
157 bottom_block->successor_block_list_type = orig_block->successor_block_list_type;
158 bottom_block->successor_blocks = orig_block->successor_blocks;
159 orig_block->successor_block_list_type = kNotUsed;
160 orig_block->successor_blocks = NULL;
161 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bottom_block->successor_blocks);
buzbee311ca162013-02-28 15:56:43 -0800162 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700163 SuccessorBlockInfo *successor_block_info = iterator.Next();
buzbee311ca162013-02-28 15:56:43 -0800164 if (successor_block_info == NULL) break;
buzbee0d829482013-10-11 15:24:55 -0700165 BasicBlock *bb = GetBasicBlock(successor_block_info->block);
166 bb->predecessors->Delete(orig_block->id);
167 bb->predecessors->Insert(bottom_block->id);
buzbee311ca162013-02-28 15:56:43 -0800168 }
169 }
170
buzbee0d829482013-10-11 15:24:55 -0700171 orig_block->last_mir_insn = prev;
172 prev->next = NULL;
buzbee311ca162013-02-28 15:56:43 -0800173
buzbee311ca162013-02-28 15:56:43 -0800174 /*
175 * Update the immediate predecessor block pointer so that outgoing edges
176 * can be applied to the proper block.
177 */
178 if (immed_pred_block_p) {
179 DCHECK_EQ(*immed_pred_block_p, orig_block);
180 *immed_pred_block_p = bottom_block;
181 }
buzbeeb48819d2013-09-14 16:15:25 -0700182
183 // Associate dex instructions in the bottom block with the new container.
Vladimir Marko4376c872014-01-23 12:39:29 +0000184 DCHECK(insn != nullptr);
185 DCHECK(insn != orig_block->first_mir_insn);
186 DCHECK(insn == bottom_block->first_mir_insn);
187 DCHECK_EQ(insn->offset, bottom_block->start_offset);
188 DCHECK(static_cast<int>(insn->dalvikInsn.opcode) == kMirOpCheck ||
189 !IsPseudoMirOp(insn->dalvikInsn.opcode));
190 DCHECK_EQ(dex_pc_to_block_map_.Get(insn->offset), orig_block->id);
191 MIR* p = insn;
192 dex_pc_to_block_map_.Put(p->offset, bottom_block->id);
193 while (p != bottom_block->last_mir_insn) {
194 p = p->next;
195 DCHECK(p != nullptr);
buzbeeb48819d2013-09-14 16:15:25 -0700196 int opcode = p->dalvikInsn.opcode;
197 /*
198 * Some messiness here to ensure that we only enter real opcodes and only the
199 * first half of a potentially throwing instruction that has been split into
Vladimir Marko4376c872014-01-23 12:39:29 +0000200 * CHECK and work portions. Since the 2nd half of a split operation is always
201 * the first in a BasicBlock, we can't hit it here.
buzbeeb48819d2013-09-14 16:15:25 -0700202 */
Vladimir Marko4376c872014-01-23 12:39:29 +0000203 if ((opcode == kMirOpCheck) || !IsPseudoMirOp(opcode)) {
204 DCHECK_EQ(dex_pc_to_block_map_.Get(p->offset), orig_block->id);
buzbeeb48819d2013-09-14 16:15:25 -0700205 dex_pc_to_block_map_.Put(p->offset, bottom_block->id);
206 }
buzbeeb48819d2013-09-14 16:15:25 -0700207 }
208
buzbee311ca162013-02-28 15:56:43 -0800209 return bottom_block;
210}
211
212/*
213 * Given a code offset, find out the block that starts with it. If the offset
214 * is in the middle of an existing block, split it into two. If immed_pred_block_p
215 * is not non-null and is the block being split, update *immed_pred_block_p to
216 * point to the bottom block so that outgoing edges can be set up properly
217 * (by the caller)
218 * Utilizes a map for fast lookup of the typical cases.
219 */
buzbee0d829482013-10-11 15:24:55 -0700220BasicBlock* MIRGraph::FindBlock(DexOffset code_offset, bool split, bool create,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700221 BasicBlock** immed_pred_block_p) {
buzbeebd663de2013-09-10 15:41:31 -0700222 if (code_offset >= cu_->code_item->insns_size_in_code_units_) {
buzbee311ca162013-02-28 15:56:43 -0800223 return NULL;
224 }
buzbeeb48819d2013-09-14 16:15:25 -0700225
226 int block_id = dex_pc_to_block_map_.Get(code_offset);
227 BasicBlock* bb = (block_id == 0) ? NULL : block_list_.Get(block_id);
228
229 if ((bb != NULL) && (bb->start_offset == code_offset)) {
230 // Does this containing block start with the desired instruction?
buzbeebd663de2013-09-10 15:41:31 -0700231 return bb;
232 }
buzbee311ca162013-02-28 15:56:43 -0800233
buzbeeb48819d2013-09-14 16:15:25 -0700234 // No direct hit.
235 if (!create) {
236 return NULL;
buzbee311ca162013-02-28 15:56:43 -0800237 }
238
buzbeeb48819d2013-09-14 16:15:25 -0700239 if (bb != NULL) {
240 // The target exists somewhere in an existing block.
241 return SplitBlock(code_offset, bb, bb == *immed_pred_block_p ? immed_pred_block_p : NULL);
242 }
243
244 // Create a new block.
buzbee862a7602013-04-05 10:58:54 -0700245 bb = NewMemBB(kDalvikByteCode, num_blocks_++);
246 block_list_.Insert(bb);
buzbee311ca162013-02-28 15:56:43 -0800247 bb->start_offset = code_offset;
buzbeeb48819d2013-09-14 16:15:25 -0700248 dex_pc_to_block_map_.Put(bb->start_offset, bb->id);
buzbee311ca162013-02-28 15:56:43 -0800249 return bb;
250}
251
buzbeeb48819d2013-09-14 16:15:25 -0700252
buzbee311ca162013-02-28 15:56:43 -0800253/* Identify code range in try blocks and set up the empty catch blocks */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700254void MIRGraph::ProcessTryCatchBlocks() {
buzbee311ca162013-02-28 15:56:43 -0800255 int tries_size = current_code_item_->tries_size_;
buzbee0d829482013-10-11 15:24:55 -0700256 DexOffset offset;
buzbee311ca162013-02-28 15:56:43 -0800257
258 if (tries_size == 0) {
259 return;
260 }
261
262 for (int i = 0; i < tries_size; i++) {
263 const DexFile::TryItem* pTry =
264 DexFile::GetTryItems(*current_code_item_, i);
buzbee0d829482013-10-11 15:24:55 -0700265 DexOffset start_offset = pTry->start_addr_;
266 DexOffset end_offset = start_offset + pTry->insn_count_;
buzbee311ca162013-02-28 15:56:43 -0800267 for (offset = start_offset; offset < end_offset; offset++) {
buzbee862a7602013-04-05 10:58:54 -0700268 try_block_addr_->SetBit(offset);
buzbee311ca162013-02-28 15:56:43 -0800269 }
270 }
271
272 // Iterate over each of the handlers to enqueue the empty Catch blocks
273 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*current_code_item_, 0);
274 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
275 for (uint32_t idx = 0; idx < handlers_size; idx++) {
276 CatchHandlerIterator iterator(handlers_ptr);
277 for (; iterator.HasNext(); iterator.Next()) {
278 uint32_t address = iterator.GetHandlerAddress();
279 FindBlock(address, false /* split */, true /*create*/,
280 /* immed_pred_block_p */ NULL);
281 }
282 handlers_ptr = iterator.EndDataPointer();
283 }
284}
285
286/* Process instructions with the kBranch flag */
buzbee0d829482013-10-11 15:24:55 -0700287BasicBlock* MIRGraph::ProcessCanBranch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
288 int width, int flags, const uint16_t* code_ptr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700289 const uint16_t* code_end) {
buzbee0d829482013-10-11 15:24:55 -0700290 DexOffset target = cur_offset;
buzbee311ca162013-02-28 15:56:43 -0800291 switch (insn->dalvikInsn.opcode) {
292 case Instruction::GOTO:
293 case Instruction::GOTO_16:
294 case Instruction::GOTO_32:
295 target += insn->dalvikInsn.vA;
296 break;
297 case Instruction::IF_EQ:
298 case Instruction::IF_NE:
299 case Instruction::IF_LT:
300 case Instruction::IF_GE:
301 case Instruction::IF_GT:
302 case Instruction::IF_LE:
303 cur_block->conditional_branch = true;
304 target += insn->dalvikInsn.vC;
305 break;
306 case Instruction::IF_EQZ:
307 case Instruction::IF_NEZ:
308 case Instruction::IF_LTZ:
309 case Instruction::IF_GEZ:
310 case Instruction::IF_GTZ:
311 case Instruction::IF_LEZ:
312 cur_block->conditional_branch = true;
313 target += insn->dalvikInsn.vB;
314 break;
315 default:
316 LOG(FATAL) << "Unexpected opcode(" << insn->dalvikInsn.opcode << ") with kBranch set";
317 }
buzbeeb48819d2013-09-14 16:15:25 -0700318 CountBranch(target);
buzbee311ca162013-02-28 15:56:43 -0800319 BasicBlock *taken_block = FindBlock(target, /* split */ true, /* create */ true,
320 /* immed_pred_block_p */ &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700321 cur_block->taken = taken_block->id;
322 taken_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800323
324 /* Always terminate the current block for conditional branches */
325 if (flags & Instruction::kContinue) {
326 BasicBlock *fallthrough_block = FindBlock(cur_offset + width,
327 /*
328 * If the method is processed
329 * in sequential order from the
330 * beginning, we don't need to
331 * specify split for continue
332 * blocks. However, this
333 * routine can be called by
334 * compileLoop, which starts
335 * parsing the method from an
336 * arbitrary address in the
337 * method body.
338 */
339 true,
340 /* create */
341 true,
342 /* immed_pred_block_p */
343 &cur_block);
buzbee0d829482013-10-11 15:24:55 -0700344 cur_block->fall_through = fallthrough_block->id;
345 fallthrough_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800346 } else if (code_ptr < code_end) {
buzbee27247762013-07-28 12:03:58 -0700347 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
buzbee311ca162013-02-28 15:56:43 -0800348 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800349 }
350 return cur_block;
351}
352
353/* Process instructions with the kSwitch flag */
buzbee17189ac2013-11-08 11:07:02 -0800354BasicBlock* MIRGraph::ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
355 int width, int flags) {
buzbee311ca162013-02-28 15:56:43 -0800356 const uint16_t* switch_data =
357 reinterpret_cast<const uint16_t*>(GetCurrentInsns() + cur_offset + insn->dalvikInsn.vB);
358 int size;
359 const int* keyTable;
360 const int* target_table;
361 int i;
362 int first_key;
363
364 /*
365 * Packed switch data format:
366 * ushort ident = 0x0100 magic value
367 * ushort size number of entries in the table
368 * int first_key first (and lowest) switch case value
369 * int targets[size] branch targets, relative to switch opcode
370 *
371 * Total size is (4+size*2) 16-bit code units.
372 */
373 if (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) {
374 DCHECK_EQ(static_cast<int>(switch_data[0]),
375 static_cast<int>(Instruction::kPackedSwitchSignature));
376 size = switch_data[1];
377 first_key = switch_data[2] | (switch_data[3] << 16);
378 target_table = reinterpret_cast<const int*>(&switch_data[4]);
379 keyTable = NULL; // Make the compiler happy
380 /*
381 * Sparse switch data format:
382 * ushort ident = 0x0200 magic value
383 * ushort size number of entries in the table; > 0
384 * int keys[size] keys, sorted low-to-high; 32-bit aligned
385 * int targets[size] branch targets, relative to switch opcode
386 *
387 * Total size is (2+size*4) 16-bit code units.
388 */
389 } else {
390 DCHECK_EQ(static_cast<int>(switch_data[0]),
391 static_cast<int>(Instruction::kSparseSwitchSignature));
392 size = switch_data[1];
393 keyTable = reinterpret_cast<const int*>(&switch_data[2]);
394 target_table = reinterpret_cast<const int*>(&switch_data[2 + size*2]);
395 first_key = 0; // To make the compiler happy
396 }
397
buzbee0d829482013-10-11 15:24:55 -0700398 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800399 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700400 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800401 }
buzbee0d829482013-10-11 15:24:55 -0700402 cur_block->successor_block_list_type =
403 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ? kPackedSwitch : kSparseSwitch;
404 cur_block->successor_blocks =
Brian Carlstromdf629502013-07-17 22:39:56 -0700405 new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, size, kGrowableArraySuccessorBlocks);
buzbee311ca162013-02-28 15:56:43 -0800406
407 for (i = 0; i < size; i++) {
408 BasicBlock *case_block = FindBlock(cur_offset + target_table[i], /* split */ true,
409 /* create */ true, /* immed_pred_block_p */ &cur_block);
410 SuccessorBlockInfo *successor_block_info =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700411 static_cast<SuccessorBlockInfo*>(arena_->Alloc(sizeof(SuccessorBlockInfo),
412 ArenaAllocator::kAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700413 successor_block_info->block = case_block->id;
buzbee311ca162013-02-28 15:56:43 -0800414 successor_block_info->key =
415 (insn->dalvikInsn.opcode == Instruction::PACKED_SWITCH) ?
416 first_key + i : keyTable[i];
buzbee0d829482013-10-11 15:24:55 -0700417 cur_block->successor_blocks->Insert(successor_block_info);
418 case_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800419 }
420
421 /* Fall-through case */
Brian Carlstromdf629502013-07-17 22:39:56 -0700422 BasicBlock* fallthrough_block = FindBlock(cur_offset + width, /* split */ false,
423 /* create */ true, /* immed_pred_block_p */ NULL);
buzbee0d829482013-10-11 15:24:55 -0700424 cur_block->fall_through = fallthrough_block->id;
425 fallthrough_block->predecessors->Insert(cur_block->id);
buzbee17189ac2013-11-08 11:07:02 -0800426 return cur_block;
buzbee311ca162013-02-28 15:56:43 -0800427}
428
429/* Process instructions with the kThrow flag */
buzbee0d829482013-10-11 15:24:55 -0700430BasicBlock* MIRGraph::ProcessCanThrow(BasicBlock* cur_block, MIR* insn, DexOffset cur_offset,
431 int width, int flags, ArenaBitVector* try_block_addr,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700432 const uint16_t* code_ptr, const uint16_t* code_end) {
buzbee862a7602013-04-05 10:58:54 -0700433 bool in_try_block = try_block_addr->IsBitSet(cur_offset);
buzbee17189ac2013-11-08 11:07:02 -0800434 bool is_throw = (insn->dalvikInsn.opcode == Instruction::THROW);
435 bool build_all_edges =
436 (cu_->disable_opt & (1 << kSuppressExceptionEdges)) || is_throw || in_try_block;
buzbee311ca162013-02-28 15:56:43 -0800437
438 /* In try block */
439 if (in_try_block) {
440 CatchHandlerIterator iterator(*current_code_item_, cur_offset);
441
buzbee0d829482013-10-11 15:24:55 -0700442 if (cur_block->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800443 LOG(INFO) << PrettyMethod(cu_->method_idx, *cu_->dex_file);
444 LOG(FATAL) << "Successor block list already in use: "
buzbee0d829482013-10-11 15:24:55 -0700445 << static_cast<int>(cur_block->successor_block_list_type);
buzbee311ca162013-02-28 15:56:43 -0800446 }
447
buzbee0d829482013-10-11 15:24:55 -0700448 cur_block->successor_block_list_type = kCatch;
449 cur_block->successor_blocks =
buzbee862a7602013-04-05 10:58:54 -0700450 new (arena_) GrowableArray<SuccessorBlockInfo*>(arena_, 2, kGrowableArraySuccessorBlocks);
buzbee311ca162013-02-28 15:56:43 -0800451
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700452 for (; iterator.HasNext(); iterator.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800453 BasicBlock *catch_block = FindBlock(iterator.GetHandlerAddress(), false /* split*/,
454 false /* creat */, NULL /* immed_pred_block_p */);
455 catch_block->catch_entry = true;
456 if (kIsDebugBuild) {
457 catches_.insert(catch_block->start_offset);
458 }
459 SuccessorBlockInfo *successor_block_info = reinterpret_cast<SuccessorBlockInfo*>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700460 (arena_->Alloc(sizeof(SuccessorBlockInfo), ArenaAllocator::kAllocSuccessor));
buzbee0d829482013-10-11 15:24:55 -0700461 successor_block_info->block = catch_block->id;
buzbee311ca162013-02-28 15:56:43 -0800462 successor_block_info->key = iterator.GetHandlerTypeIndex();
buzbee0d829482013-10-11 15:24:55 -0700463 cur_block->successor_blocks->Insert(successor_block_info);
464 catch_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800465 }
buzbee17189ac2013-11-08 11:07:02 -0800466 } else if (build_all_edges) {
buzbee862a7602013-04-05 10:58:54 -0700467 BasicBlock *eh_block = NewMemBB(kExceptionHandling, num_blocks_++);
buzbee0d829482013-10-11 15:24:55 -0700468 cur_block->taken = eh_block->id;
buzbee862a7602013-04-05 10:58:54 -0700469 block_list_.Insert(eh_block);
buzbee311ca162013-02-28 15:56:43 -0800470 eh_block->start_offset = cur_offset;
buzbee0d829482013-10-11 15:24:55 -0700471 eh_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800472 }
473
buzbee17189ac2013-11-08 11:07:02 -0800474 if (is_throw) {
buzbee311ca162013-02-28 15:56:43 -0800475 cur_block->explicit_throw = true;
buzbee27247762013-07-28 12:03:58 -0700476 if (code_ptr < code_end) {
buzbee311ca162013-02-28 15:56:43 -0800477 // Force creation of new block following THROW via side-effect
478 FindBlock(cur_offset + width, /* split */ false, /* create */ true,
479 /* immed_pred_block_p */ NULL);
480 }
481 if (!in_try_block) {
482 // Don't split a THROW that can't rethrow - we're done.
483 return cur_block;
484 }
485 }
486
buzbee17189ac2013-11-08 11:07:02 -0800487 if (!build_all_edges) {
488 /*
489 * Even though there is an exception edge here, control cannot return to this
490 * method. Thus, for the purposes of dataflow analysis and optimization, we can
491 * ignore the edge. Doing this reduces compile time, and increases the scope
492 * of the basic-block level optimization pass.
493 */
494 return cur_block;
495 }
496
buzbee311ca162013-02-28 15:56:43 -0800497 /*
498 * Split the potentially-throwing instruction into two parts.
499 * The first half will be a pseudo-op that captures the exception
500 * edges and terminates the basic block. It always falls through.
501 * Then, create a new basic block that begins with the throwing instruction
502 * (minus exceptions). Note: this new basic block must NOT be entered into
503 * the block_map. If the potentially-throwing instruction is the target of a
504 * future branch, we need to find the check psuedo half. The new
505 * basic block containing the work portion of the instruction should
506 * only be entered via fallthrough from the block containing the
507 * pseudo exception edge MIR. Note also that this new block is
508 * not automatically terminated after the work portion, and may
509 * contain following instructions.
buzbeeb48819d2013-09-14 16:15:25 -0700510 *
511 * Note also that the dex_pc_to_block_map_ entry for the potentially
512 * throwing instruction will refer to the original basic block.
buzbee311ca162013-02-28 15:56:43 -0800513 */
buzbee862a7602013-04-05 10:58:54 -0700514 BasicBlock *new_block = NewMemBB(kDalvikByteCode, num_blocks_++);
515 block_list_.Insert(new_block);
buzbee311ca162013-02-28 15:56:43 -0800516 new_block->start_offset = insn->offset;
buzbee0d829482013-10-11 15:24:55 -0700517 cur_block->fall_through = new_block->id;
518 new_block->predecessors->Insert(cur_block->id);
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700519 MIR* new_insn = static_cast<MIR*>(arena_->Alloc(sizeof(MIR), ArenaAllocator::kAllocMIR));
buzbee311ca162013-02-28 15:56:43 -0800520 *new_insn = *insn;
521 insn->dalvikInsn.opcode =
522 static_cast<Instruction::Code>(kMirOpCheck);
523 // Associate the two halves
524 insn->meta.throw_insn = new_insn;
buzbee311ca162013-02-28 15:56:43 -0800525 AppendMIR(new_block, new_insn);
526 return new_block;
527}
528
529/* Parse a Dex method and insert it into the MIRGraph at the current insert point. */
530void MIRGraph::InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700531 InvokeType invoke_type, uint16_t class_def_idx,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700532 uint32_t method_idx, jobject class_loader, const DexFile& dex_file) {
buzbee311ca162013-02-28 15:56:43 -0800533 current_code_item_ = code_item;
534 method_stack_.push_back(std::make_pair(current_method_, current_offset_));
535 current_method_ = m_units_.size();
536 current_offset_ = 0;
537 // TODO: will need to snapshot stack image and use that as the mir context identification.
538 m_units_.push_back(new DexCompilationUnit(cu_, class_loader, Runtime::Current()->GetClassLinker(),
Vladimir Marko2730db02014-01-27 11:15:17 +0000539 dex_file, current_code_item_, class_def_idx, method_idx, access_flags,
540 cu_->compiler_driver->GetVerifiedMethod(&dex_file, method_idx)));
buzbee311ca162013-02-28 15:56:43 -0800541 const uint16_t* code_ptr = current_code_item_->insns_;
542 const uint16_t* code_end =
543 current_code_item_->insns_ + current_code_item_->insns_size_in_code_units_;
544
545 // TODO: need to rework expansion of block list & try_block_addr when inlining activated.
buzbeeb48819d2013-09-14 16:15:25 -0700546 // TUNING: use better estimate of basic blocks for following resize.
buzbee862a7602013-04-05 10:58:54 -0700547 block_list_.Resize(block_list_.Size() + current_code_item_->insns_size_in_code_units_);
buzbeeb48819d2013-09-14 16:15:25 -0700548 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 -0700549
buzbee311ca162013-02-28 15:56:43 -0800550 // TODO: replace with explicit resize routine. Using automatic extension side effect for now.
buzbee862a7602013-04-05 10:58:54 -0700551 try_block_addr_->SetBit(current_code_item_->insns_size_in_code_units_);
552 try_block_addr_->ClearBit(current_code_item_->insns_size_in_code_units_);
buzbee311ca162013-02-28 15:56:43 -0800553
554 // If this is the first method, set up default entry and exit blocks.
555 if (current_method_ == 0) {
556 DCHECK(entry_block_ == NULL);
557 DCHECK(exit_block_ == NULL);
Brian Carlstrom42748892013-07-18 18:04:08 -0700558 DCHECK_EQ(num_blocks_, 0);
buzbee0d829482013-10-11 15:24:55 -0700559 // Use id 0 to represent a null block.
560 BasicBlock* null_block = NewMemBB(kNullBlock, num_blocks_++);
561 DCHECK_EQ(null_block->id, NullBasicBlockId);
562 null_block->hidden = true;
563 block_list_.Insert(null_block);
buzbee862a7602013-04-05 10:58:54 -0700564 entry_block_ = NewMemBB(kEntryBlock, num_blocks_++);
buzbee862a7602013-04-05 10:58:54 -0700565 block_list_.Insert(entry_block_);
buzbee0d829482013-10-11 15:24:55 -0700566 exit_block_ = NewMemBB(kExitBlock, num_blocks_++);
buzbee862a7602013-04-05 10:58:54 -0700567 block_list_.Insert(exit_block_);
buzbee311ca162013-02-28 15:56:43 -0800568 // TODO: deprecate all "cu->" fields; move what's left to wherever CompilationUnit is allocated.
569 cu_->dex_file = &dex_file;
570 cu_->class_def_idx = class_def_idx;
571 cu_->method_idx = method_idx;
572 cu_->access_flags = access_flags;
573 cu_->invoke_type = invoke_type;
574 cu_->shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
575 cu_->num_ins = current_code_item_->ins_size_;
576 cu_->num_regs = current_code_item_->registers_size_ - cu_->num_ins;
577 cu_->num_outs = current_code_item_->outs_size_;
578 cu_->num_dalvik_registers = current_code_item_->registers_size_;
579 cu_->insns = current_code_item_->insns_;
580 cu_->code_item = current_code_item_;
581 } else {
582 UNIMPLEMENTED(FATAL) << "Nested inlining not implemented.";
583 /*
584 * Will need to manage storage for ins & outs, push prevous state and update
585 * insert point.
586 */
587 }
588
589 /* Current block to record parsed instructions */
buzbee862a7602013-04-05 10:58:54 -0700590 BasicBlock *cur_block = NewMemBB(kDalvikByteCode, num_blocks_++);
buzbee0d829482013-10-11 15:24:55 -0700591 DCHECK_EQ(current_offset_, 0U);
buzbee311ca162013-02-28 15:56:43 -0800592 cur_block->start_offset = current_offset_;
buzbee862a7602013-04-05 10:58:54 -0700593 block_list_.Insert(cur_block);
buzbee0d829482013-10-11 15:24:55 -0700594 // TODO: for inlining support, insert at the insert point rather than entry block.
595 entry_block_->fall_through = cur_block->id;
596 cur_block->predecessors->Insert(entry_block_->id);
buzbee311ca162013-02-28 15:56:43 -0800597
598 /* Identify code range in try blocks and set up the empty catch blocks */
599 ProcessTryCatchBlocks();
600
buzbee311ca162013-02-28 15:56:43 -0800601 /* Parse all instructions and put them into containing basic blocks */
602 while (code_ptr < code_end) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700603 MIR *insn = static_cast<MIR *>(arena_->Alloc(sizeof(MIR), ArenaAllocator::kAllocMIR));
buzbee311ca162013-02-28 15:56:43 -0800604 insn->offset = current_offset_;
605 insn->m_unit_index = current_method_;
606 int width = ParseInsn(code_ptr, &insn->dalvikInsn);
607 insn->width = width;
608 Instruction::Code opcode = insn->dalvikInsn.opcode;
609 if (opcode_count_ != NULL) {
610 opcode_count_[static_cast<int>(opcode)]++;
611 }
612
buzbee311ca162013-02-28 15:56:43 -0800613 int flags = Instruction::FlagsOf(insn->dalvikInsn.opcode);
buzbeeb1f1d642014-02-27 12:55:32 -0800614 int verify_flags = Instruction::VerifyFlagsOf(insn->dalvikInsn.opcode);
buzbee311ca162013-02-28 15:56:43 -0800615
buzbee1da1e2f2013-11-15 13:37:01 -0800616 uint64_t df_flags = oat_data_flow_attributes_[insn->dalvikInsn.opcode];
buzbee311ca162013-02-28 15:56:43 -0800617
618 if (df_flags & DF_HAS_DEFS) {
619 def_count_ += (df_flags & DF_A_WIDE) ? 2 : 1;
620 }
621
buzbee1da1e2f2013-11-15 13:37:01 -0800622 if (df_flags & DF_LVN) {
623 cur_block->use_lvn = true; // Run local value numbering on this basic block.
624 }
625
buzbee27247762013-07-28 12:03:58 -0700626 // Check for inline data block signatures
627 if (opcode == Instruction::NOP) {
628 // A simple NOP will have a width of 1 at this point, embedded data NOP > 1.
629 if ((width == 1) && ((current_offset_ & 0x1) == 0x1) && ((code_end - code_ptr) > 1)) {
630 // Could be an aligning nop. If an embedded data NOP follows, treat pair as single unit.
631 uint16_t following_raw_instruction = code_ptr[1];
632 if ((following_raw_instruction == Instruction::kSparseSwitchSignature) ||
633 (following_raw_instruction == Instruction::kPackedSwitchSignature) ||
634 (following_raw_instruction == Instruction::kArrayDataSignature)) {
635 width += Instruction::At(code_ptr + 1)->SizeInCodeUnits();
636 }
637 }
638 if (width == 1) {
639 // It is a simple nop - treat normally.
640 AppendMIR(cur_block, insn);
641 } else {
buzbee0d829482013-10-11 15:24:55 -0700642 DCHECK(cur_block->fall_through == NullBasicBlockId);
643 DCHECK(cur_block->taken == NullBasicBlockId);
buzbee27247762013-07-28 12:03:58 -0700644 // Unreachable instruction, mark for no continuation.
645 flags &= ~Instruction::kContinue;
646 }
647 } else {
648 AppendMIR(cur_block, insn);
649 }
650
buzbeeb48819d2013-09-14 16:15:25 -0700651 // Associate the starting dex_pc for this opcode with its containing basic block.
652 dex_pc_to_block_map_.Put(insn->offset, cur_block->id);
653
buzbee27247762013-07-28 12:03:58 -0700654 code_ptr += width;
655
buzbee311ca162013-02-28 15:56:43 -0800656 if (flags & Instruction::kBranch) {
657 cur_block = ProcessCanBranch(cur_block, insn, current_offset_,
658 width, flags, code_ptr, code_end);
659 } else if (flags & Instruction::kReturn) {
660 cur_block->terminated_by_return = true;
buzbee0d829482013-10-11 15:24:55 -0700661 cur_block->fall_through = exit_block_->id;
662 exit_block_->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800663 /*
664 * Terminate the current block if there are instructions
665 * afterwards.
666 */
667 if (code_ptr < code_end) {
668 /*
669 * Create a fallthrough block for real instructions
670 * (incl. NOP).
671 */
buzbee27247762013-07-28 12:03:58 -0700672 FindBlock(current_offset_ + width, /* split */ false, /* create */ true,
673 /* immed_pred_block_p */ NULL);
buzbee311ca162013-02-28 15:56:43 -0800674 }
675 } else if (flags & Instruction::kThrow) {
676 cur_block = ProcessCanThrow(cur_block, insn, current_offset_, width, flags, try_block_addr_,
677 code_ptr, code_end);
678 } else if (flags & Instruction::kSwitch) {
buzbee17189ac2013-11-08 11:07:02 -0800679 cur_block = ProcessCanSwitch(cur_block, insn, current_offset_, width, flags);
buzbee311ca162013-02-28 15:56:43 -0800680 }
buzbeeb1f1d642014-02-27 12:55:32 -0800681 if (verify_flags & Instruction::kVerifyVarArgRange) {
682 /*
683 * The Quick backend's runtime model includes a gap between a method's
684 * argument ("in") vregs and the rest of its vregs. Handling a range instruction
685 * which spans the gap is somewhat complicated, and should not happen
686 * in normal usage of dx. Punt to the interpreter.
687 */
688 int first_reg_in_range = insn->dalvikInsn.vC;
689 int last_reg_in_range = first_reg_in_range + insn->dalvikInsn.vA - 1;
690 if (IsInVReg(first_reg_in_range) != IsInVReg(last_reg_in_range)) {
691 punt_to_interpreter_ = true;
692 }
693 }
buzbee311ca162013-02-28 15:56:43 -0800694 current_offset_ += width;
695 BasicBlock *next_block = FindBlock(current_offset_, /* split */ false, /* create */
696 false, /* immed_pred_block_p */ NULL);
697 if (next_block) {
698 /*
699 * The next instruction could be the target of a previously parsed
700 * forward branch so a block is already created. If the current
701 * instruction is not an unconditional branch, connect them through
702 * the fall-through link.
703 */
buzbee0d829482013-10-11 15:24:55 -0700704 DCHECK(cur_block->fall_through == NullBasicBlockId ||
705 GetBasicBlock(cur_block->fall_through) == next_block ||
706 GetBasicBlock(cur_block->fall_through) == exit_block_);
buzbee311ca162013-02-28 15:56:43 -0800707
buzbee0d829482013-10-11 15:24:55 -0700708 if ((cur_block->fall_through == NullBasicBlockId) && (flags & Instruction::kContinue)) {
709 cur_block->fall_through = next_block->id;
710 next_block->predecessors->Insert(cur_block->id);
buzbee311ca162013-02-28 15:56:43 -0800711 }
712 cur_block = next_block;
713 }
714 }
Vladimir Marko5816ed42013-11-27 17:04:20 +0000715
buzbee311ca162013-02-28 15:56:43 -0800716 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
717 DumpCFG("/sdcard/1_post_parse_cfg/", true);
718 }
719
720 if (cu_->verbose) {
buzbee1fd33462013-03-25 13:40:45 -0700721 DumpMIRGraph();
buzbee311ca162013-02-28 15:56:43 -0800722 }
723}
724
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700725void MIRGraph::ShowOpcodeStats() {
buzbee311ca162013-02-28 15:56:43 -0800726 DCHECK(opcode_count_ != NULL);
727 LOG(INFO) << "Opcode Count";
728 for (int i = 0; i < kNumPackedOpcodes; i++) {
729 if (opcode_count_[i] != 0) {
730 LOG(INFO) << "-C- " << Instruction::Name(static_cast<Instruction::Code>(i))
731 << " " << opcode_count_[i];
732 }
733 }
734}
735
736// TODO: use a configurable base prefix, and adjust callers to supply pass name.
737/* Dump the CFG into a DOT graph */
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800738void MIRGraph::DumpCFG(const char* dir_prefix, bool all_blocks, const char *suffix) {
buzbee311ca162013-02-28 15:56:43 -0800739 FILE* file;
740 std::string fname(PrettyMethod(cu_->method_idx, *cu_->dex_file));
741 ReplaceSpecialChars(fname);
Jean Christophe Beylerd0a51552014-01-10 14:18:31 -0800742 fname = StringPrintf("%s%s%x%s.dot", dir_prefix, fname.c_str(),
743 GetBasicBlock(GetEntryBlock()->fall_through)->start_offset,
744 suffix == nullptr ? "" : suffix);
buzbee311ca162013-02-28 15:56:43 -0800745 file = fopen(fname.c_str(), "w");
746 if (file == NULL) {
747 return;
748 }
749 fprintf(file, "digraph G {\n");
750
751 fprintf(file, " rankdir=TB\n");
752
753 int num_blocks = all_blocks ? GetNumBlocks() : num_reachable_blocks_;
754 int idx;
755
756 for (idx = 0; idx < num_blocks; idx++) {
buzbee862a7602013-04-05 10:58:54 -0700757 int block_idx = all_blocks ? idx : dfs_order_->Get(idx);
buzbee311ca162013-02-28 15:56:43 -0800758 BasicBlock *bb = GetBasicBlock(block_idx);
759 if (bb == NULL) break;
760 if (bb->block_type == kDead) continue;
761 if (bb->block_type == kEntryBlock) {
762 fprintf(file, " entry_%d [shape=Mdiamond];\n", bb->id);
763 } else if (bb->block_type == kExitBlock) {
764 fprintf(file, " exit_%d [shape=Mdiamond];\n", bb->id);
765 } else if (bb->block_type == kDalvikByteCode) {
766 fprintf(file, " block%04x_%d [shape=record,label = \"{ \\\n",
767 bb->start_offset, bb->id);
768 const MIR *mir;
769 fprintf(file, " {block id %d\\l}%s\\\n", bb->id,
770 bb->first_mir_insn ? " | " : " ");
771 for (mir = bb->first_mir_insn; mir; mir = mir->next) {
772 int opcode = mir->dalvikInsn.opcode;
773 fprintf(file, " {%04x %s %s %s\\l}%s\\\n", mir->offset,
buzbee1fd33462013-03-25 13:40:45 -0700774 mir->ssa_rep ? GetDalvikDisassembly(mir) :
buzbee311ca162013-02-28 15:56:43 -0800775 (opcode < kMirOpFirst) ? Instruction::Name(mir->dalvikInsn.opcode) :
buzbee1fd33462013-03-25 13:40:45 -0700776 extended_mir_op_names_[opcode - kMirOpFirst],
buzbee311ca162013-02-28 15:56:43 -0800777 (mir->optimization_flags & MIR_IGNORE_RANGE_CHECK) != 0 ? " no_rangecheck" : " ",
778 (mir->optimization_flags & MIR_IGNORE_NULL_CHECK) != 0 ? " no_nullcheck" : " ",
779 mir->next ? " | " : " ");
780 }
781 fprintf(file, " }\"];\n\n");
782 } else if (bb->block_type == kExceptionHandling) {
783 char block_name[BLOCK_NAME_LEN];
784
785 GetBlockName(bb, block_name);
786 fprintf(file, " %s [shape=invhouse];\n", block_name);
787 }
788
789 char block_name1[BLOCK_NAME_LEN], block_name2[BLOCK_NAME_LEN];
790
buzbee0d829482013-10-11 15:24:55 -0700791 if (bb->taken != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800792 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700793 GetBlockName(GetBasicBlock(bb->taken), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800794 fprintf(file, " %s:s -> %s:n [style=dotted]\n",
795 block_name1, block_name2);
796 }
buzbee0d829482013-10-11 15:24:55 -0700797 if (bb->fall_through != NullBasicBlockId) {
buzbee311ca162013-02-28 15:56:43 -0800798 GetBlockName(bb, block_name1);
buzbee0d829482013-10-11 15:24:55 -0700799 GetBlockName(GetBasicBlock(bb->fall_through), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800800 fprintf(file, " %s:s -> %s:n\n", block_name1, block_name2);
801 }
802
buzbee0d829482013-10-11 15:24:55 -0700803 if (bb->successor_block_list_type != kNotUsed) {
buzbee311ca162013-02-28 15:56:43 -0800804 fprintf(file, " succ%04x_%d [shape=%s,label = \"{ \\\n",
805 bb->start_offset, bb->id,
buzbee0d829482013-10-11 15:24:55 -0700806 (bb->successor_block_list_type == kCatch) ? "Mrecord" : "record");
807 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
buzbee862a7602013-04-05 10:58:54 -0700808 SuccessorBlockInfo *successor_block_info = iterator.Next();
buzbee311ca162013-02-28 15:56:43 -0800809
810 int succ_id = 0;
811 while (true) {
812 if (successor_block_info == NULL) break;
813
buzbee0d829482013-10-11 15:24:55 -0700814 BasicBlock *dest_block = GetBasicBlock(successor_block_info->block);
buzbee862a7602013-04-05 10:58:54 -0700815 SuccessorBlockInfo *next_successor_block_info = iterator.Next();
buzbee311ca162013-02-28 15:56:43 -0800816
817 fprintf(file, " {<f%d> %04x: %04x\\l}%s\\\n",
818 succ_id++,
819 successor_block_info->key,
820 dest_block->start_offset,
821 (next_successor_block_info != NULL) ? " | " : " ");
822
823 successor_block_info = next_successor_block_info;
824 }
825 fprintf(file, " }\"];\n\n");
826
827 GetBlockName(bb, block_name1);
828 fprintf(file, " %s:s -> succ%04x_%d:n [style=dashed]\n",
829 block_name1, bb->start_offset, bb->id);
830
buzbee0d829482013-10-11 15:24:55 -0700831 if (bb->successor_block_list_type == kPackedSwitch ||
832 bb->successor_block_list_type == kSparseSwitch) {
833 GrowableArray<SuccessorBlockInfo*>::Iterator iter(bb->successor_blocks);
buzbee311ca162013-02-28 15:56:43 -0800834
835 succ_id = 0;
836 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700837 SuccessorBlockInfo *successor_block_info = iter.Next();
buzbee311ca162013-02-28 15:56:43 -0800838 if (successor_block_info == NULL) break;
839
buzbee0d829482013-10-11 15:24:55 -0700840 BasicBlock* dest_block = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800841
842 GetBlockName(dest_block, block_name2);
843 fprintf(file, " succ%04x_%d:f%d:e -> %s:n\n", bb->start_offset,
844 bb->id, succ_id++, block_name2);
845 }
846 }
847 }
848 fprintf(file, "\n");
849
850 if (cu_->verbose) {
851 /* Display the dominator tree */
852 GetBlockName(bb, block_name1);
853 fprintf(file, " cfg%s [label=\"%s\", shape=none];\n",
854 block_name1, block_name1);
855 if (bb->i_dom) {
buzbee0d829482013-10-11 15:24:55 -0700856 GetBlockName(GetBasicBlock(bb->i_dom), block_name2);
buzbee311ca162013-02-28 15:56:43 -0800857 fprintf(file, " cfg%s:s -> cfg%s:n\n\n", block_name2, block_name1);
858 }
859 }
860 }
861 fprintf(file, "}\n");
862 fclose(file);
863}
864
buzbee1fd33462013-03-25 13:40:45 -0700865/* Insert an MIR instruction to the end of a basic block */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700866void MIRGraph::AppendMIR(BasicBlock* bb, MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700867 if (bb->first_mir_insn == NULL) {
868 DCHECK(bb->last_mir_insn == NULL);
869 bb->last_mir_insn = bb->first_mir_insn = mir;
buzbee0d829482013-10-11 15:24:55 -0700870 mir->next = NULL;
buzbee1fd33462013-03-25 13:40:45 -0700871 } else {
872 bb->last_mir_insn->next = mir;
buzbee1fd33462013-03-25 13:40:45 -0700873 mir->next = NULL;
874 bb->last_mir_insn = mir;
875 }
876}
877
878/* Insert an MIR instruction to the head of a basic block */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700879void MIRGraph::PrependMIR(BasicBlock* bb, MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700880 if (bb->first_mir_insn == NULL) {
881 DCHECK(bb->last_mir_insn == NULL);
882 bb->last_mir_insn = bb->first_mir_insn = mir;
buzbee0d829482013-10-11 15:24:55 -0700883 mir->next = NULL;
buzbee1fd33462013-03-25 13:40:45 -0700884 } else {
buzbee1fd33462013-03-25 13:40:45 -0700885 mir->next = bb->first_mir_insn;
buzbee1fd33462013-03-25 13:40:45 -0700886 bb->first_mir_insn = mir;
887 }
888}
889
890/* Insert a MIR instruction after the specified MIR */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700891void MIRGraph::InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir) {
buzbee1fd33462013-03-25 13:40:45 -0700892 new_mir->next = current_mir->next;
893 current_mir->next = new_mir;
894
buzbee0d829482013-10-11 15:24:55 -0700895 if (bb->last_mir_insn == current_mir) {
buzbee1fd33462013-03-25 13:40:45 -0700896 /* Is the last MIR in the block */
897 bb->last_mir_insn = new_mir;
898 }
899}
900
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800901MIR* MIRGraph::GetNextUnconditionalMir(BasicBlock* bb, MIR* current) {
902 MIR* next_mir = nullptr;
903
904 if (current != nullptr) {
905 next_mir = current->next;
906 }
907
908 if (next_mir == nullptr) {
909 // Only look for next MIR that follows unconditionally.
910 if ((bb->taken == NullBasicBlockId) && (bb->fall_through != NullBasicBlockId)) {
911 next_mir = GetBasicBlock(bb->fall_through)->first_mir_insn;
912 }
913 }
914
915 return next_mir;
916}
917
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700918char* MIRGraph::GetDalvikDisassembly(const MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700919 DecodedInstruction insn = mir->dalvikInsn;
920 std::string str;
921 int flags = 0;
922 int opcode = insn.opcode;
923 char* ret;
924 bool nop = false;
925 SSARepresentation* ssa_rep = mir->ssa_rep;
926 Instruction::Format dalvik_format = Instruction::k10x; // Default to no-operand format
927 int defs = (ssa_rep != NULL) ? ssa_rep->num_defs : 0;
928 int uses = (ssa_rep != NULL) ? ssa_rep->num_uses : 0;
929
930 // Handle special cases.
931 if ((opcode == kMirOpCheck) || (opcode == kMirOpCheckPart2)) {
932 str.append(extended_mir_op_names_[opcode - kMirOpFirst]);
933 str.append(": ");
934 // Recover the original Dex instruction
935 insn = mir->meta.throw_insn->dalvikInsn;
936 ssa_rep = mir->meta.throw_insn->ssa_rep;
937 defs = ssa_rep->num_defs;
938 uses = ssa_rep->num_uses;
939 opcode = insn.opcode;
940 } else if (opcode == kMirOpNop) {
941 str.append("[");
buzbee0d829482013-10-11 15:24:55 -0700942 // Recover original opcode.
943 insn.opcode = Instruction::At(current_code_item_->insns_ + mir->offset)->Opcode();
944 opcode = insn.opcode;
buzbee1fd33462013-03-25 13:40:45 -0700945 nop = true;
946 }
947
948 if (opcode >= kMirOpFirst) {
949 str.append(extended_mir_op_names_[opcode - kMirOpFirst]);
950 } else {
951 dalvik_format = Instruction::FormatOf(insn.opcode);
952 flags = Instruction::FlagsOf(insn.opcode);
953 str.append(Instruction::Name(insn.opcode));
954 }
955
956 if (opcode == kMirOpPhi) {
buzbee0d829482013-10-11 15:24:55 -0700957 BasicBlockId* incoming = mir->meta.phi_incoming;
buzbee1fd33462013-03-25 13:40:45 -0700958 str.append(StringPrintf(" %s = (%s",
959 GetSSANameWithConst(ssa_rep->defs[0], true).c_str(),
960 GetSSANameWithConst(ssa_rep->uses[0], true).c_str()));
Brian Carlstromb1eba212013-07-17 18:07:19 -0700961 str.append(StringPrintf(":%d", incoming[0]));
buzbee1fd33462013-03-25 13:40:45 -0700962 int i;
963 for (i = 1; i < uses; i++) {
964 str.append(StringPrintf(", %s:%d",
965 GetSSANameWithConst(ssa_rep->uses[i], true).c_str(),
966 incoming[i]));
967 }
968 str.append(")");
969 } else if ((flags & Instruction::kBranch) != 0) {
970 // For branches, decode the instructions to print out the branch targets.
971 int offset = 0;
972 switch (dalvik_format) {
973 case Instruction::k21t:
974 str.append(StringPrintf(" %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str()));
975 offset = insn.vB;
976 break;
977 case Instruction::k22t:
978 str.append(StringPrintf(" %s, %s,", GetSSANameWithConst(ssa_rep->uses[0], false).c_str(),
979 GetSSANameWithConst(ssa_rep->uses[1], false).c_str()));
980 offset = insn.vC;
981 break;
982 case Instruction::k10t:
983 case Instruction::k20t:
984 case Instruction::k30t:
985 offset = insn.vA;
986 break;
987 default:
988 LOG(FATAL) << "Unexpected branch format " << dalvik_format << " from " << insn.opcode;
989 }
990 str.append(StringPrintf(" 0x%x (%c%x)", mir->offset + offset,
991 offset > 0 ? '+' : '-', offset > 0 ? offset : -offset));
992 } else {
993 // For invokes-style formats, treat wide regs as a pair of singles
994 bool show_singles = ((dalvik_format == Instruction::k35c) ||
995 (dalvik_format == Instruction::k3rc));
996 if (defs != 0) {
997 str.append(StringPrintf(" %s", GetSSANameWithConst(ssa_rep->defs[0], false).c_str()));
998 if (uses != 0) {
999 str.append(", ");
1000 }
1001 }
1002 for (int i = 0; i < uses; i++) {
1003 str.append(
1004 StringPrintf(" %s", GetSSANameWithConst(ssa_rep->uses[i], show_singles).c_str()));
1005 if (!show_singles && (reg_location_ != NULL) && reg_location_[i].wide) {
1006 // For the listing, skip the high sreg.
1007 i++;
1008 }
1009 if (i != (uses -1)) {
1010 str.append(",");
1011 }
1012 }
1013 switch (dalvik_format) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001014 case Instruction::k11n: // Add one immediate from vB
buzbee1fd33462013-03-25 13:40:45 -07001015 case Instruction::k21s:
1016 case Instruction::k31i:
1017 case Instruction::k21h:
1018 str.append(StringPrintf(", #%d", insn.vB));
1019 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001020 case Instruction::k51l: // Add one wide immediate
Ian Rogers23b03b52014-01-24 11:10:03 -08001021 str.append(StringPrintf(", #%" PRId64, insn.vB_wide));
buzbee1fd33462013-03-25 13:40:45 -07001022 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001023 case Instruction::k21c: // One register, one string/type/method index
buzbee1fd33462013-03-25 13:40:45 -07001024 case Instruction::k31c:
1025 str.append(StringPrintf(", index #%d", insn.vB));
1026 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001027 case Instruction::k22c: // Two registers, one string/type/method index
buzbee1fd33462013-03-25 13:40:45 -07001028 str.append(StringPrintf(", index #%d", insn.vC));
1029 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001030 case Instruction::k22s: // Add one immediate from vC
buzbee1fd33462013-03-25 13:40:45 -07001031 case Instruction::k22b:
1032 str.append(StringPrintf(", #%d", insn.vC));
1033 break;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001034 default: {
1035 // Nothing left to print
buzbee1fd33462013-03-25 13:40:45 -07001036 }
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07001037 }
buzbee1fd33462013-03-25 13:40:45 -07001038 }
1039 if (nop) {
1040 str.append("]--optimized away");
1041 }
1042 int length = str.length() + 1;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001043 ret = static_cast<char*>(arena_->Alloc(length, ArenaAllocator::kAllocDFInfo));
buzbee1fd33462013-03-25 13:40:45 -07001044 strncpy(ret, str.c_str(), length);
1045 return ret;
1046}
1047
1048/* Turn method name into a legal Linux file name */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001049void MIRGraph::ReplaceSpecialChars(std::string& str) {
Brian Carlstrom9b7085a2013-07-18 15:15:21 -07001050 static const struct { const char before; const char after; } match[] = {
1051 {'/', '-'}, {';', '#'}, {' ', '#'}, {'$', '+'},
1052 {'(', '@'}, {')', '@'}, {'<', '='}, {'>', '='}
1053 };
buzbee1fd33462013-03-25 13:40:45 -07001054 for (unsigned int i = 0; i < sizeof(match)/sizeof(match[0]); i++) {
1055 std::replace(str.begin(), str.end(), match[i].before, match[i].after);
1056 }
1057}
1058
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001059std::string MIRGraph::GetSSAName(int ssa_reg) {
Ian Rogers39ebcb82013-05-30 16:57:23 -07001060 // TODO: This value is needed for LLVM and debugging. Currently, we compute this and then copy to
1061 // the arena. We should be smarter and just place straight into the arena, or compute the
1062 // value more lazily.
buzbee1fd33462013-03-25 13:40:45 -07001063 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1064}
1065
1066// Similar to GetSSAName, but if ssa name represents an immediate show that as well.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001067std::string MIRGraph::GetSSANameWithConst(int ssa_reg, bool singles_only) {
buzbee1fd33462013-03-25 13:40:45 -07001068 if (reg_location_ == NULL) {
1069 // Pre-SSA - just use the standard name
1070 return GetSSAName(ssa_reg);
1071 }
1072 if (IsConst(reg_location_[ssa_reg])) {
1073 if (!singles_only && reg_location_[ssa_reg].wide) {
Ian Rogers23b03b52014-01-24 11:10:03 -08001074 return StringPrintf("v%d_%d#0x%" PRIx64, SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001075 ConstantValueWide(reg_location_[ssa_reg]));
1076 } else {
Brian Carlstromb1eba212013-07-17 18:07:19 -07001077 return StringPrintf("v%d_%d#0x%x", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg),
buzbee1fd33462013-03-25 13:40:45 -07001078 ConstantValue(reg_location_[ssa_reg]));
1079 }
1080 } else {
1081 return StringPrintf("v%d_%d", SRegToVReg(ssa_reg), GetSSASubscript(ssa_reg));
1082 }
1083}
1084
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001085void MIRGraph::GetBlockName(BasicBlock* bb, char* name) {
buzbee1fd33462013-03-25 13:40:45 -07001086 switch (bb->block_type) {
1087 case kEntryBlock:
1088 snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id);
1089 break;
1090 case kExitBlock:
1091 snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id);
1092 break;
1093 case kDalvikByteCode:
1094 snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id);
1095 break;
1096 case kExceptionHandling:
1097 snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset,
1098 bb->id);
1099 break;
1100 default:
1101 snprintf(name, BLOCK_NAME_LEN, "_%d", bb->id);
1102 break;
1103 }
1104}
1105
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001106const char* MIRGraph::GetShortyFromTargetIdx(int target_idx) {
buzbee0d829482013-10-11 15:24:55 -07001107 // TODO: for inlining support, use current code unit.
buzbee1fd33462013-03-25 13:40:45 -07001108 const DexFile::MethodId& method_id = cu_->dex_file->GetMethodId(target_idx);
1109 return cu_->dex_file->GetShorty(method_id.proto_idx_);
1110}
1111
1112/* Debug Utility - dump a compilation unit */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001113void MIRGraph::DumpMIRGraph() {
buzbee1fd33462013-03-25 13:40:45 -07001114 BasicBlock* bb;
1115 const char* block_type_names[] = {
buzbee17189ac2013-11-08 11:07:02 -08001116 "Null Block",
buzbee1fd33462013-03-25 13:40:45 -07001117 "Entry Block",
1118 "Code Block",
1119 "Exit Block",
1120 "Exception Handling",
1121 "Catch Block"
1122 };
1123
1124 LOG(INFO) << "Compiling " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
1125 LOG(INFO) << cu_->insns << " insns";
1126 LOG(INFO) << GetNumBlocks() << " blocks in total";
buzbee862a7602013-04-05 10:58:54 -07001127 GrowableArray<BasicBlock*>::Iterator iterator(&block_list_);
buzbee1fd33462013-03-25 13:40:45 -07001128
1129 while (true) {
buzbee862a7602013-04-05 10:58:54 -07001130 bb = iterator.Next();
buzbee1fd33462013-03-25 13:40:45 -07001131 if (bb == NULL) break;
1132 LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)",
1133 bb->id,
1134 block_type_names[bb->block_type],
1135 bb->start_offset,
1136 bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset,
1137 bb->last_mir_insn ? "" : " empty");
buzbee0d829482013-10-11 15:24:55 -07001138 if (bb->taken != NullBasicBlockId) {
1139 LOG(INFO) << " Taken branch: block " << bb->taken
1140 << "(0x" << std::hex << GetBasicBlock(bb->taken)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001141 }
buzbee0d829482013-10-11 15:24:55 -07001142 if (bb->fall_through != NullBasicBlockId) {
1143 LOG(INFO) << " Fallthrough : block " << bb->fall_through
1144 << " (0x" << std::hex << GetBasicBlock(bb->fall_through)->start_offset << ")";
buzbee1fd33462013-03-25 13:40:45 -07001145 }
1146 }
1147}
1148
1149/*
1150 * Build an array of location records for the incoming arguments.
1151 * Note: one location record per word of arguments, with dummy
1152 * high-word loc for wide arguments. Also pull up any following
1153 * MOVE_RESULT and incorporate it into the invoke.
1154 */
1155CallInfo* MIRGraph::NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001156 bool is_range) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001157 CallInfo* info = static_cast<CallInfo*>(arena_->Alloc(sizeof(CallInfo),
1158 ArenaAllocator::kAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001159 MIR* move_result_mir = FindMoveResult(bb, mir);
1160 if (move_result_mir == NULL) {
1161 info->result.location = kLocInvalid;
1162 } else {
1163 info->result = GetRawDest(move_result_mir);
buzbee1fd33462013-03-25 13:40:45 -07001164 move_result_mir->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpNop);
1165 }
1166 info->num_arg_words = mir->ssa_rep->num_uses;
1167 info->args = (info->num_arg_words == 0) ? NULL : static_cast<RegLocation*>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001168 (arena_->Alloc(sizeof(RegLocation) * info->num_arg_words, ArenaAllocator::kAllocMisc));
buzbee1fd33462013-03-25 13:40:45 -07001169 for (int i = 0; i < info->num_arg_words; i++) {
1170 info->args[i] = GetRawSrc(mir, i);
1171 }
1172 info->opt_flags = mir->optimization_flags;
1173 info->type = type;
1174 info->is_range = is_range;
1175 info->index = mir->dalvikInsn.vB;
1176 info->offset = mir->offset;
1177 return info;
1178}
1179
buzbee862a7602013-04-05 10:58:54 -07001180// Allocate a new basic block.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001181BasicBlock* MIRGraph::NewMemBB(BBType block_type, int block_id) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -07001182 BasicBlock* bb = static_cast<BasicBlock*>(arena_->Alloc(sizeof(BasicBlock),
1183 ArenaAllocator::kAllocBB));
buzbee862a7602013-04-05 10:58:54 -07001184 bb->block_type = block_type;
1185 bb->id = block_id;
1186 // TUNING: better estimate of the exit block predecessors?
buzbee0d829482013-10-11 15:24:55 -07001187 bb->predecessors = new (arena_) GrowableArray<BasicBlockId>(arena_,
Brian Carlstromdf629502013-07-17 22:39:56 -07001188 (block_type == kExitBlock) ? 2048 : 2,
1189 kGrowableArrayPredecessors);
buzbee0d829482013-10-11 15:24:55 -07001190 bb->successor_block_list_type = kNotUsed;
buzbee862a7602013-04-05 10:58:54 -07001191 block_id_map_.Put(block_id, block_id);
1192 return bb;
1193}
buzbee1fd33462013-03-25 13:40:45 -07001194
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -08001195void MIRGraph::InitializeConstantPropagation() {
1196 is_constant_v_ = new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false);
1197 constant_values_ = static_cast<int*>(arena_->Alloc(sizeof(int) * GetNumSSARegs(), ArenaAllocator::kAllocDFInfo));
1198}
1199
1200void MIRGraph::InitializeMethodUses() {
1201 // The gate starts by initializing the use counts
1202 int num_ssa_regs = GetNumSSARegs();
1203 use_counts_.Resize(num_ssa_regs + 32);
1204 raw_use_counts_.Resize(num_ssa_regs + 32);
1205 // Initialize list
1206 for (int i = 0; i < num_ssa_regs; i++) {
1207 use_counts_.Insert(0);
1208 raw_use_counts_.Insert(0);
1209 }
1210}
1211
1212void MIRGraph::InitializeSSATransformation() {
1213 /* Compute the DFS order */
1214 ComputeDFSOrders();
1215
1216 /* Compute the dominator info */
1217 ComputeDominators();
1218
1219 /* Allocate data structures in preparation for SSA conversion */
1220 CompilerInitializeSSAConversion();
1221
1222 /* Find out the "Dalvik reg def x block" relation */
1223 ComputeDefBlockMatrix();
1224
1225 /* Insert phi nodes to dominance frontiers for all variables */
1226 InsertPhiNodes();
1227
1228 /* Rename register names by local defs and phi nodes */
1229 ClearAllVisitedFlags();
1230 DoDFSPreOrderSSARename(GetEntryBlock());
1231
1232 /*
1233 * Shared temp bit vector used by each block to count the number of defs
1234 * from all the predecessor blocks.
1235 */
1236 temp_ssa_register_v_ =
1237 new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapTempSSARegisterV);
1238}
1239
1240void MIRGraph::CheckSSARegisterVector() {
1241 DCHECK(temp_ssa_register_v_ != nullptr);
1242}
1243
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001244} // namespace art