blob: 84ab2590b05c50903c07d148d596ae83719b223c [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_MIR_GRAPH_H_
18#define ART_COMPILER_DEX_MIR_GRAPH_H_
buzbee311ca162013-02-28 15:56:43 -080019
20#include "dex_file.h"
21#include "dex_instruction.h"
22#include "compiler_ir.h"
buzbee862a7602013-04-05 10:58:54 -070023#include "arena_bit_vector.h"
24#include "growable_array.h"
buzbee311ca162013-02-28 15:56:43 -080025
26namespace art {
27
buzbeeee17e0a2013-07-31 10:47:37 -070028enum InstructionAnalysisAttributePos {
29 kUninterestingOp = 0,
30 kArithmeticOp,
31 kFPOp,
32 kSingleOp,
33 kDoubleOp,
34 kIntOp,
35 kLongOp,
36 kBranchOp,
37 kInvokeOp,
38 kArrayOp,
39 kHeavyweightOp,
40 kSimpleConstOp,
41 kMoveOp
42};
43
44#define AN_NONE (1 << kUninterestingOp)
45#define AN_MATH (1 << kArithmeticOp)
46#define AN_FP (1 << kFPOp)
47#define AN_LONG (1 << kLongOp)
48#define AN_INT (1 << kIntOp)
49#define AN_SINGLE (1 << kSingleOp)
50#define AN_DOUBLE (1 << kDoubleOp)
51#define AN_FLOATMATH (1 << kFPOp)
52#define AN_BRANCH (1 << kBranchOp)
53#define AN_INVOKE (1 << kInvokeOp)
54#define AN_ARRAYOP (1 << kArrayOp)
55#define AN_HEAVYWEIGHT (1 << kHeavyweightOp)
56#define AN_SIMPLECONST (1 << kSimpleConstOp)
57#define AN_MOVE (1 << kMoveOp)
58#define AN_COMPUTATIONAL (AN_MATH | AN_ARRAYOP | AN_MOVE | AN_SIMPLECONST)
59
buzbee311ca162013-02-28 15:56:43 -080060enum DataFlowAttributePos {
61 kUA = 0,
62 kUB,
63 kUC,
64 kAWide,
65 kBWide,
66 kCWide,
67 kDA,
68 kIsMove,
69 kSetsConst,
70 kFormat35c,
71 kFormat3rc,
72 kNullCheckSrc0, // Null check of uses[0].
73 kNullCheckSrc1, // Null check of uses[1].
74 kNullCheckSrc2, // Null check of uses[2].
75 kNullCheckOut0, // Null check out outgoing arg0.
76 kDstNonNull, // May assume dst is non-null.
77 kRetNonNull, // May assume retval is non-null.
78 kNullTransferSrc0, // Object copy src[0] -> dst.
79 kNullTransferSrcN, // Phi null check state transfer.
80 kRangeCheckSrc1, // Range check of uses[1].
81 kRangeCheckSrc2, // Range check of uses[2].
82 kRangeCheckSrc3, // Range check of uses[3].
83 kFPA,
84 kFPB,
85 kFPC,
86 kCoreA,
87 kCoreB,
88 kCoreC,
89 kRefA,
90 kRefB,
91 kRefC,
92 kUsesMethodStar, // Implicit use of Method*.
93};
94
95#define DF_NOP 0
96#define DF_UA (1 << kUA)
97#define DF_UB (1 << kUB)
98#define DF_UC (1 << kUC)
99#define DF_A_WIDE (1 << kAWide)
100#define DF_B_WIDE (1 << kBWide)
101#define DF_C_WIDE (1 << kCWide)
102#define DF_DA (1 << kDA)
103#define DF_IS_MOVE (1 << kIsMove)
104#define DF_SETS_CONST (1 << kSetsConst)
105#define DF_FORMAT_35C (1 << kFormat35c)
106#define DF_FORMAT_3RC (1 << kFormat3rc)
107#define DF_NULL_CHK_0 (1 << kNullCheckSrc0)
108#define DF_NULL_CHK_1 (1 << kNullCheckSrc1)
109#define DF_NULL_CHK_2 (1 << kNullCheckSrc2)
110#define DF_NULL_CHK_OUT0 (1 << kNullCheckOut0)
111#define DF_NON_NULL_DST (1 << kDstNonNull)
112#define DF_NON_NULL_RET (1 << kRetNonNull)
113#define DF_NULL_TRANSFER_0 (1 << kNullTransferSrc0)
114#define DF_NULL_TRANSFER_N (1 << kNullTransferSrcN)
115#define DF_RANGE_CHK_1 (1 << kRangeCheckSrc1)
116#define DF_RANGE_CHK_2 (1 << kRangeCheckSrc2)
117#define DF_RANGE_CHK_3 (1 << kRangeCheckSrc3)
118#define DF_FP_A (1 << kFPA)
119#define DF_FP_B (1 << kFPB)
120#define DF_FP_C (1 << kFPC)
121#define DF_CORE_A (1 << kCoreA)
122#define DF_CORE_B (1 << kCoreB)
123#define DF_CORE_C (1 << kCoreC)
124#define DF_REF_A (1 << kRefA)
125#define DF_REF_B (1 << kRefB)
126#define DF_REF_C (1 << kRefC)
127#define DF_UMS (1 << kUsesMethodStar)
128
129#define DF_HAS_USES (DF_UA | DF_UB | DF_UC)
130
131#define DF_HAS_DEFS (DF_DA)
132
133#define DF_HAS_NULL_CHKS (DF_NULL_CHK_0 | \
134 DF_NULL_CHK_1 | \
135 DF_NULL_CHK_2 | \
136 DF_NULL_CHK_OUT0)
137
138#define DF_HAS_RANGE_CHKS (DF_RANGE_CHK_1 | \
139 DF_RANGE_CHK_2 | \
140 DF_RANGE_CHK_3)
141
142#define DF_HAS_NR_CHKS (DF_HAS_NULL_CHKS | \
143 DF_HAS_RANGE_CHKS)
144
145#define DF_A_IS_REG (DF_UA | DF_DA)
146#define DF_B_IS_REG (DF_UB)
147#define DF_C_IS_REG (DF_UC)
148#define DF_IS_GETTER_OR_SETTER (DF_IS_GETTER | DF_IS_SETTER)
149#define DF_USES_FP (DF_FP_A | DF_FP_B | DF_FP_C)
150
buzbee1fd33462013-03-25 13:40:45 -0700151enum OatMethodAttributes {
152 kIsLeaf, // Method is leaf.
153 kHasLoop, // Method contains simple loop.
154};
155
156#define METHOD_IS_LEAF (1 << kIsLeaf)
157#define METHOD_HAS_LOOP (1 << kHasLoop)
158
159// Minimum field size to contain Dalvik v_reg number.
160#define VREG_NUM_WIDTH 16
161
162#define INVALID_SREG (-1)
163#define INVALID_VREG (0xFFFFU)
164#define INVALID_REG (0xFF)
165#define INVALID_OFFSET (0xDEADF00FU)
166
167/* SSA encodings for special registers */
168#define SSA_METHOD_BASEREG (-2)
169/* First compiler temp basereg, grows smaller */
170#define SSA_CTEMP_BASEREG (SSA_METHOD_BASEREG - 1)
171
172#define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck)
173#define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly)
174#define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck)
175#define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly)
176#define MIR_INLINED (1 << kMIRInlined)
177#define MIR_INLINED_PRED (1 << kMIRInlinedPred)
178#define MIR_CALLEE (1 << kMIRCallee)
179#define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck)
180#define MIR_DUP (1 << kMIRDup)
181
buzbee862a7602013-04-05 10:58:54 -0700182#define BLOCK_NAME_LEN 80
183
buzbee1fd33462013-03-25 13:40:45 -0700184/*
185 * In general, vreg/sreg describe Dalvik registers that originated with dx. However,
186 * it is useful to have compiler-generated temporary registers and have them treated
187 * in the same manner as dx-generated virtual registers. This struct records the SSA
188 * name of compiler-introduced temporaries.
189 */
190struct CompilerTemp {
191 int s_reg;
192};
193
194// When debug option enabled, records effectiveness of null and range check elimination.
195struct Checkstats {
196 int null_checks;
197 int null_checks_eliminated;
198 int range_checks;
199 int range_checks_eliminated;
200};
201
202// Dataflow attributes of a basic block.
203struct BasicBlockDataFlow {
204 ArenaBitVector* use_v;
205 ArenaBitVector* def_v;
206 ArenaBitVector* live_in_v;
207 ArenaBitVector* phi_v;
208 int* vreg_to_ssa_map;
209 ArenaBitVector* ending_null_check_v;
210};
211
212/*
213 * Normalized use/def for a MIR operation using SSA names rather than vregs. Note that
214 * uses/defs retain the Dalvik convention that long operations operate on a pair of 32-bit
215 * vregs. For example, "ADD_LONG v0, v2, v3" would have 2 defs (v0/v1) and 4 uses (v2/v3, v4/v5).
216 * Following SSA renaming, this is the primary struct used by code generators to locate
217 * operand and result registers. This is a somewhat confusing and unhelpful convention that
218 * we may want to revisit in the future.
219 */
220struct SSARepresentation {
221 int num_uses;
222 int* uses;
223 bool* fp_use;
224 int num_defs;
225 int* defs;
226 bool* fp_def;
227};
228
229/*
230 * The Midlevel Intermediate Representation node, which may be largely considered a
231 * wrapper around a Dalvik byte code.
232 */
233struct MIR {
234 DecodedInstruction dalvikInsn;
235 unsigned int width;
236 unsigned int offset;
237 int m_unit_index; // From which method was this MIR included
238 MIR* prev;
239 MIR* next;
240 SSARepresentation* ssa_rep;
241 int optimization_flags;
242 union {
243 // Establish link between two halves of throwing instructions.
244 MIR* throw_insn;
245 // Saved opcode for NOP'd MIRs
246 Instruction::Code original_opcode;
247 } meta;
248};
249
buzbee862a7602013-04-05 10:58:54 -0700250struct SuccessorBlockInfo;
251
buzbee1fd33462013-03-25 13:40:45 -0700252struct BasicBlock {
253 int id;
254 int dfs_id;
255 bool visited;
256 bool hidden;
257 bool catch_entry;
258 bool explicit_throw;
259 bool conditional_branch;
260 bool terminated_by_return; // Block ends with a Dalvik return opcode.
261 bool dominates_return; // Is a member of return extended basic block.
262 uint16_t start_offset;
263 uint16_t nesting_depth;
264 BBType block_type;
265 MIR* first_mir_insn;
266 MIR* last_mir_insn;
267 BasicBlock* fall_through;
268 BasicBlock* taken;
269 BasicBlock* i_dom; // Immediate dominator.
270 BasicBlockDataFlow* data_flow_info;
buzbee862a7602013-04-05 10:58:54 -0700271 GrowableArray<BasicBlock*>* predecessors;
buzbee1fd33462013-03-25 13:40:45 -0700272 ArenaBitVector* dominators;
273 ArenaBitVector* i_dominated; // Set nodes being immediately dominated.
274 ArenaBitVector* dom_frontier; // Dominance frontier.
275 struct { // For one-to-many successors like.
276 BlockListType block_list_type; // switch and exception handling.
buzbee862a7602013-04-05 10:58:54 -0700277 GrowableArray<SuccessorBlockInfo*>* blocks;
buzbee1fd33462013-03-25 13:40:45 -0700278 } successor_block_list;
279};
280
281/*
282 * The "blocks" field in "successor_block_list" points to an array of elements with the type
283 * "SuccessorBlockInfo". For catch blocks, key is type index for the exception. For swtich
284 * blocks, key is the case value.
285 */
buzbee862a7602013-04-05 10:58:54 -0700286// TODO: make class with placement new.
buzbee1fd33462013-03-25 13:40:45 -0700287struct SuccessorBlockInfo {
288 BasicBlock* block;
289 int key;
290};
291
292/*
293 * Whereas a SSA name describes a definition of a Dalvik vreg, the RegLocation describes
294 * the type of an SSA name (and, can also be used by code generators to record where the
295 * value is located (i.e. - physical register, frame, spill, etc.). For each SSA name (SReg)
296 * there is a RegLocation.
297 * FIXME: The orig_sreg field was added as a workaround for llvm bitcode generation. With
298 * the latest restructuring, we should be able to remove it and rely on s_reg_low throughout.
299 */
300struct RegLocation {
301 RegLocationType location:3;
302 unsigned wide:1;
303 unsigned defined:1; // Do we know the type?
304 unsigned is_const:1; // Constant, value in mir_graph->constant_values[].
305 unsigned fp:1; // Floating point?
306 unsigned core:1; // Non-floating point?
307 unsigned ref:1; // Something GC cares about.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700308 unsigned high_word:1; // High word of pair?
buzbee1fd33462013-03-25 13:40:45 -0700309 unsigned home:1; // Does this represent the home location?
310 uint8_t low_reg; // First physical register.
311 uint8_t high_reg; // 2nd physical register (if wide).
312 int32_t s_reg_low; // SSA name for low Dalvik word.
313 int32_t orig_sreg; // TODO: remove after Bitcode gen complete
314 // and consolodate usage w/ s_reg_low.
315};
316
317/*
318 * Collection of information describing an invoke, and the destination of
319 * the subsequent MOVE_RESULT (if applicable). Collected as a unit to enable
320 * more efficient invoke code generation.
321 */
322struct CallInfo {
323 int num_arg_words; // Note: word count, not arg count.
324 RegLocation* args; // One for each word of arguments.
325 RegLocation result; // Eventual target of MOVE_RESULT.
326 int opt_flags;
327 InvokeType type;
328 uint32_t dex_idx;
329 uint32_t index; // Method idx for invokes, type idx for FilledNewArray.
330 uintptr_t direct_code;
331 uintptr_t direct_method;
332 RegLocation target; // Target of following move_result.
333 bool skip_this;
334 bool is_range;
335 int offset; // Dalvik offset.
336};
337
338
339const RegLocation bad_loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
340 INVALID_REG, INVALID_REG, INVALID_SREG, INVALID_SREG};
buzbee311ca162013-02-28 15:56:43 -0800341
342class MIRGraph {
Ian Rogers71fe2672013-03-19 20:45:02 -0700343 public:
buzbee862a7602013-04-05 10:58:54 -0700344 MIRGraph(CompilationUnit* cu, ArenaAllocator* arena);
Ian Rogers6282dc12013-04-18 15:54:02 -0700345 ~MIRGraph();
buzbee311ca162013-02-28 15:56:43 -0800346
Ian Rogers71fe2672013-03-19 20:45:02 -0700347 /*
buzbeeee17e0a2013-07-31 10:47:37 -0700348 * Examine the graph to determine whether it's worthwile to spend the time compiling
349 * this method.
350 */
351 bool SkipCompilation(Runtime::CompilerFilter compiler_filter);
352
353 /*
Ian Rogers71fe2672013-03-19 20:45:02 -0700354 * Parse dex method and add MIR at current insert point. Returns id (which is
355 * actually the index of the method in the m_units_ array).
356 */
357 void InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
358 InvokeType invoke_type, uint32_t class_def_idx,
359 uint32_t method_idx, jobject class_loader, const DexFile& dex_file);
buzbee311ca162013-02-28 15:56:43 -0800360
Ian Rogers71fe2672013-03-19 20:45:02 -0700361 /* Find existing block */
362 BasicBlock* FindBlock(unsigned int code_offset) {
363 return FindBlock(code_offset, false, false, NULL);
364 }
buzbee311ca162013-02-28 15:56:43 -0800365
Ian Rogers71fe2672013-03-19 20:45:02 -0700366 const uint16_t* GetCurrentInsns() const {
367 return current_code_item_->insns_;
368 }
buzbee311ca162013-02-28 15:56:43 -0800369
Ian Rogers71fe2672013-03-19 20:45:02 -0700370 const uint16_t* GetInsns(int m_unit_index) const {
371 return m_units_[m_unit_index]->GetCodeItem()->insns_;
372 }
buzbee311ca162013-02-28 15:56:43 -0800373
Ian Rogers71fe2672013-03-19 20:45:02 -0700374 int GetNumBlocks() const {
375 return num_blocks_;
376 }
buzbee311ca162013-02-28 15:56:43 -0800377
buzbeeee17e0a2013-07-31 10:47:37 -0700378 size_t GetNumDalvikInsns() const {
379 return cu_->code_item->insns_size_in_code_units_;
380 }
381
Ian Rogers71fe2672013-03-19 20:45:02 -0700382 ArenaBitVector* GetTryBlockAddr() const {
383 return try_block_addr_;
384 }
buzbee311ca162013-02-28 15:56:43 -0800385
Ian Rogers71fe2672013-03-19 20:45:02 -0700386 BasicBlock* GetEntryBlock() const {
387 return entry_block_;
388 }
buzbee311ca162013-02-28 15:56:43 -0800389
Ian Rogers71fe2672013-03-19 20:45:02 -0700390 BasicBlock* GetExitBlock() const {
391 return exit_block_;
392 }
buzbee311ca162013-02-28 15:56:43 -0800393
Ian Rogers71fe2672013-03-19 20:45:02 -0700394 BasicBlock* GetBasicBlock(int block_id) const {
buzbee862a7602013-04-05 10:58:54 -0700395 return block_list_.Get(block_id);
Ian Rogers71fe2672013-03-19 20:45:02 -0700396 }
buzbee311ca162013-02-28 15:56:43 -0800397
Ian Rogers71fe2672013-03-19 20:45:02 -0700398 size_t GetBasicBlockListCount() const {
buzbee862a7602013-04-05 10:58:54 -0700399 return block_list_.Size();
Ian Rogers71fe2672013-03-19 20:45:02 -0700400 }
buzbee311ca162013-02-28 15:56:43 -0800401
buzbee862a7602013-04-05 10:58:54 -0700402 GrowableArray<BasicBlock*>* GetBlockList() {
Ian Rogers71fe2672013-03-19 20:45:02 -0700403 return &block_list_;
404 }
buzbee311ca162013-02-28 15:56:43 -0800405
buzbee862a7602013-04-05 10:58:54 -0700406 GrowableArray<int>* GetDfsOrder() {
407 return dfs_order_;
Ian Rogers71fe2672013-03-19 20:45:02 -0700408 }
buzbee311ca162013-02-28 15:56:43 -0800409
buzbee862a7602013-04-05 10:58:54 -0700410 GrowableArray<int>* GetDfsPostOrder() {
411 return dfs_post_order_;
Ian Rogers71fe2672013-03-19 20:45:02 -0700412 }
buzbee311ca162013-02-28 15:56:43 -0800413
buzbee862a7602013-04-05 10:58:54 -0700414 GrowableArray<int>* GetDomPostOrder() {
415 return dom_post_order_traversal_;
Ian Rogers71fe2672013-03-19 20:45:02 -0700416 }
buzbee311ca162013-02-28 15:56:43 -0800417
Ian Rogers71fe2672013-03-19 20:45:02 -0700418 int GetDefCount() const {
419 return def_count_;
420 }
buzbee311ca162013-02-28 15:56:43 -0800421
buzbee862a7602013-04-05 10:58:54 -0700422 ArenaAllocator* GetArena() {
423 return arena_;
424 }
425
Ian Rogers71fe2672013-03-19 20:45:02 -0700426 void EnableOpcodeCounting() {
buzbee862a7602013-04-05 10:58:54 -0700427 opcode_count_ = static_cast<int*>(arena_->NewMem(kNumPackedOpcodes * sizeof(int), true,
428 ArenaAllocator::kAllocMisc));
Ian Rogers71fe2672013-03-19 20:45:02 -0700429 }
buzbee311ca162013-02-28 15:56:43 -0800430
Ian Rogers71fe2672013-03-19 20:45:02 -0700431 void ShowOpcodeStats();
buzbee311ca162013-02-28 15:56:43 -0800432
Ian Rogers71fe2672013-03-19 20:45:02 -0700433 DexCompilationUnit* GetCurrentDexCompilationUnit() const {
434 return m_units_[current_method_];
435 }
buzbee311ca162013-02-28 15:56:43 -0800436
Ian Rogers71fe2672013-03-19 20:45:02 -0700437 void DumpCFG(const char* dir_prefix, bool all_blocks);
buzbee311ca162013-02-28 15:56:43 -0800438
Ian Rogers71fe2672013-03-19 20:45:02 -0700439 void BuildRegLocations();
buzbee311ca162013-02-28 15:56:43 -0800440
Ian Rogers71fe2672013-03-19 20:45:02 -0700441 void DumpRegLocTable(RegLocation* table, int count);
buzbee311ca162013-02-28 15:56:43 -0800442
Ian Rogers71fe2672013-03-19 20:45:02 -0700443 void BasicBlockOptimization();
buzbee311ca162013-02-28 15:56:43 -0800444
Ian Rogers71fe2672013-03-19 20:45:02 -0700445 bool IsConst(int32_t s_reg) const {
buzbee862a7602013-04-05 10:58:54 -0700446 return is_constant_v_->IsBitSet(s_reg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700447 }
buzbee311ca162013-02-28 15:56:43 -0800448
Ian Rogers71fe2672013-03-19 20:45:02 -0700449 bool IsConst(RegLocation loc) const {
450 return (IsConst(loc.orig_sreg));
451 }
buzbee311ca162013-02-28 15:56:43 -0800452
Ian Rogers71fe2672013-03-19 20:45:02 -0700453 int32_t ConstantValue(RegLocation loc) const {
454 DCHECK(IsConst(loc));
455 return constant_values_[loc.orig_sreg];
456 }
buzbee311ca162013-02-28 15:56:43 -0800457
Ian Rogers71fe2672013-03-19 20:45:02 -0700458 int32_t ConstantValue(int32_t s_reg) const {
459 DCHECK(IsConst(s_reg));
460 return constant_values_[s_reg];
461 }
buzbee311ca162013-02-28 15:56:43 -0800462
Ian Rogers71fe2672013-03-19 20:45:02 -0700463 int64_t ConstantValueWide(RegLocation loc) const {
464 DCHECK(IsConst(loc));
465 return (static_cast<int64_t>(constant_values_[loc.orig_sreg + 1]) << 32) |
466 Low32Bits(static_cast<int64_t>(constant_values_[loc.orig_sreg]));
467 }
buzbee311ca162013-02-28 15:56:43 -0800468
Ian Rogers71fe2672013-03-19 20:45:02 -0700469 bool IsConstantNullRef(RegLocation loc) const {
470 return loc.ref && loc.is_const && (ConstantValue(loc) == 0);
471 }
buzbee311ca162013-02-28 15:56:43 -0800472
Ian Rogers71fe2672013-03-19 20:45:02 -0700473 int GetNumSSARegs() const {
474 return num_ssa_regs_;
475 }
buzbee311ca162013-02-28 15:56:43 -0800476
Ian Rogers71fe2672013-03-19 20:45:02 -0700477 void SetNumSSARegs(int new_num) {
478 num_ssa_regs_ = new_num;
479 }
buzbee311ca162013-02-28 15:56:43 -0800480
buzbee862a7602013-04-05 10:58:54 -0700481 unsigned int GetNumReachableBlocks() const {
Ian Rogers71fe2672013-03-19 20:45:02 -0700482 return num_reachable_blocks_;
483 }
buzbee311ca162013-02-28 15:56:43 -0800484
Ian Rogers71fe2672013-03-19 20:45:02 -0700485 int GetUseCount(int vreg) const {
buzbee862a7602013-04-05 10:58:54 -0700486 return use_counts_.Get(vreg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700487 }
buzbee311ca162013-02-28 15:56:43 -0800488
Ian Rogers71fe2672013-03-19 20:45:02 -0700489 int GetRawUseCount(int vreg) const {
buzbee862a7602013-04-05 10:58:54 -0700490 return raw_use_counts_.Get(vreg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700491 }
buzbee311ca162013-02-28 15:56:43 -0800492
Ian Rogers71fe2672013-03-19 20:45:02 -0700493 int GetSSASubscript(int ssa_reg) const {
buzbee862a7602013-04-05 10:58:54 -0700494 return ssa_subscripts_->Get(ssa_reg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700495 }
buzbee311ca162013-02-28 15:56:43 -0800496
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700497 RegLocation GetRawSrc(MIR* mir, int num) {
buzbee1fd33462013-03-25 13:40:45 -0700498 DCHECK(num < mir->ssa_rep->num_uses);
499 RegLocation res = reg_location_[mir->ssa_rep->uses[num]];
500 return res;
501 }
502
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700503 RegLocation GetRawDest(MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700504 DCHECK_GT(mir->ssa_rep->num_defs, 0);
505 RegLocation res = reg_location_[mir->ssa_rep->defs[0]];
506 return res;
507 }
508
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700509 RegLocation GetDest(MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700510 RegLocation res = GetRawDest(mir);
511 DCHECK(!res.wide);
512 return res;
513 }
514
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700515 RegLocation GetSrc(MIR* mir, int num) {
buzbee1fd33462013-03-25 13:40:45 -0700516 RegLocation res = GetRawSrc(mir, num);
517 DCHECK(!res.wide);
518 return res;
519 }
520
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700521 RegLocation GetDestWide(MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700522 RegLocation res = GetRawDest(mir);
523 DCHECK(res.wide);
524 return res;
525 }
526
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700527 RegLocation GetSrcWide(MIR* mir, int low) {
buzbee1fd33462013-03-25 13:40:45 -0700528 RegLocation res = GetRawSrc(mir, low);
529 DCHECK(res.wide);
530 return res;
531 }
532
533 RegLocation GetBadLoc() {
534 return bad_loc;
535 }
536
537 int GetMethodSReg() {
538 return method_sreg_;
539 }
540
541 bool MethodIsLeaf() {
542 return attributes_ & METHOD_IS_LEAF;
543 }
544
545 RegLocation GetRegLocation(int index) {
546 DCHECK((index >= 0) && (index > num_ssa_regs_));
547 return reg_location_[index];
548 }
549
550 RegLocation GetMethodLoc() {
551 return reg_location_[method_sreg_];
552 }
553
buzbee479f83c2013-07-19 10:58:21 -0700554 bool IsSpecialCase() {
555 return special_case_ != kNoHandler;
556 }
557
558 SpecialCaseHandler GetSpecialCase() {
559 return special_case_;
560 }
561
Ian Rogers71fe2672013-03-19 20:45:02 -0700562 void BasicBlockCombine();
563 void CodeLayout();
564 void DumpCheckStats();
565 void PropagateConstants();
566 MIR* FindMoveResult(BasicBlock* bb, MIR* mir);
567 int SRegToVReg(int ssa_reg) const;
568 void VerifyDataflow();
569 void MethodUseCount();
570 void SSATransformation();
571 void CheckForDominanceFrontier(BasicBlock* dom_bb, const BasicBlock* succ_bb);
572 void NullCheckElimination();
buzbee1fd33462013-03-25 13:40:45 -0700573 bool SetFp(int index, bool is_fp);
574 bool SetCore(int index, bool is_core);
575 bool SetRef(int index, bool is_ref);
576 bool SetWide(int index, bool is_wide);
577 bool SetHigh(int index, bool is_high);
578 void AppendMIR(BasicBlock* bb, MIR* mir);
579 void PrependMIR(BasicBlock* bb, MIR* mir);
580 void InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir);
581 char* GetDalvikDisassembly(const MIR* mir);
buzbee1fd33462013-03-25 13:40:45 -0700582 void ReplaceSpecialChars(std::string& str);
583 std::string GetSSAName(int ssa_reg);
584 std::string GetSSANameWithConst(int ssa_reg, bool singles_only);
585 void GetBlockName(BasicBlock* bb, char* name);
586 const char* GetShortyFromTargetIdx(int);
587 void DumpMIRGraph();
588 CallInfo* NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type, bool is_range);
buzbee862a7602013-04-05 10:58:54 -0700589 BasicBlock* NewMemBB(BBType block_type, int block_id);
buzbee311ca162013-02-28 15:56:43 -0800590
Ian Rogers71fe2672013-03-19 20:45:02 -0700591 /*
592 * IsDebugBuild sanity check: keep track of the Dex PCs for catch entries so that later on
593 * we can verify that all catch entries have native PC entries.
594 */
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700595 std::set<uint32_t> catches_;
buzbee311ca162013-02-28 15:56:43 -0800596
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700597 // TODO: make these private.
598 RegLocation* reg_location_; // Map SSA names to location.
599 GrowableArray<CompilerTemp*> compiler_temps_;
600 SafeMap<unsigned int, unsigned int> block_id_map_; // Block collapse lookup cache.
buzbee1fd33462013-03-25 13:40:45 -0700601
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700602 static const int oat_data_flow_attributes_[kMirOpLast];
603 static const char* extended_mir_op_names_[kMirOpLast - kMirOpFirst];
buzbeeee17e0a2013-07-31 10:47:37 -0700604 static const uint32_t analysis_attributes_[kMirOpLast];
buzbee1fd33462013-03-25 13:40:45 -0700605
Ian Rogers71fe2672013-03-19 20:45:02 -0700606 private:
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700607 int FindCommonParent(int block1, int block2);
608 void ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
609 const ArenaBitVector* src2);
610 void HandleLiveInUse(ArenaBitVector* use_v, ArenaBitVector* def_v,
611 ArenaBitVector* live_in_v, int dalvik_reg_id);
612 void HandleDef(ArenaBitVector* def_v, int dalvik_reg_id);
613 void CompilerInitializeSSAConversion();
614 bool DoSSAConversion(BasicBlock* bb);
615 bool InvokeUsesMethodStar(MIR* mir);
616 int ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction);
617 bool ContentIsInsn(const uint16_t* code_ptr);
618 BasicBlock* SplitBlock(unsigned int code_offset, BasicBlock* orig_block,
Ian Rogers71fe2672013-03-19 20:45:02 -0700619 BasicBlock** immed_pred_block_p);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700620 BasicBlock* FindBlock(unsigned int code_offset, bool split, bool create,
621 BasicBlock** immed_pred_block_p);
622 void ProcessTryCatchBlocks();
623 BasicBlock* ProcessCanBranch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
624 int flags, const uint16_t* code_ptr, const uint16_t* code_end);
625 void ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, int flags);
626 BasicBlock* ProcessCanThrow(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
627 int flags, ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
628 const uint16_t* code_end);
629 int AddNewSReg(int v_reg);
630 void HandleSSAUse(int* uses, int dalvik_reg, int reg_index);
631 void HandleSSADef(int* defs, int dalvik_reg, int reg_index);
632 void DataFlowSSAFormat35C(MIR* mir);
633 void DataFlowSSAFormat3RC(MIR* mir);
634 bool FindLocalLiveIn(BasicBlock* bb);
635 void ClearAllVisitedFlags();
636 bool CountUses(struct BasicBlock* bb);
637 bool InferTypeAndSize(BasicBlock* bb);
638 bool VerifyPredInfo(BasicBlock* bb);
639 BasicBlock* NeedsVisit(BasicBlock* bb);
640 BasicBlock* NextUnvisitedSuccessor(BasicBlock* bb);
641 void MarkPreOrder(BasicBlock* bb);
642 void RecordDFSOrders(BasicBlock* bb);
643 void ComputeDFSOrders();
644 void ComputeDefBlockMatrix();
645 void ComputeDomPostOrderTraversal(BasicBlock* bb);
646 void ComputeDominators();
647 void InsertPhiNodes();
648 void DoDFSPreOrderSSARename(BasicBlock* block);
649 void SetConstant(int32_t ssa_reg, int value);
650 void SetConstantWide(int ssa_reg, int64_t value);
651 int GetSSAUseCount(int s_reg);
652 bool BasicBlockOpt(BasicBlock* bb);
653 bool EliminateNullChecks(BasicBlock* bb);
654 void NullCheckEliminationInit(BasicBlock* bb);
655 bool BuildExtendedBBList(struct BasicBlock* bb);
656 bool FillDefBlockMatrix(BasicBlock* bb);
657 void InitializeDominationInfo(BasicBlock* bb);
658 bool ComputeblockIDom(BasicBlock* bb);
659 bool ComputeBlockDominators(BasicBlock* bb);
660 bool SetDominators(BasicBlock* bb);
661 bool ComputeBlockLiveIns(BasicBlock* bb);
662 bool InsertPhiNodeOperands(BasicBlock* bb);
663 bool ComputeDominanceFrontier(BasicBlock* bb);
664 void DoConstantPropogation(BasicBlock* bb);
665 void CountChecks(BasicBlock* bb);
666 bool CombineBlocks(BasicBlock* bb);
buzbeeee17e0a2013-07-31 10:47:37 -0700667 void AnalyzeBlock(BasicBlock* bb, struct MethodStats* stats);
668 bool ComputeSkipCompilation(struct MethodStats* stats, bool skip_default);
buzbee311ca162013-02-28 15:56:43 -0800669
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700670 CompilationUnit* const cu_;
671 GrowableArray<int>* ssa_base_vregs_;
672 GrowableArray<int>* ssa_subscripts_;
673 // Map original Dalvik virtual reg i to the current SSA name.
674 int* vreg_to_ssa_map_; // length == method->registers_size
675 int* ssa_last_defs_; // length == method->registers_size
676 ArenaBitVector* is_constant_v_; // length == num_ssa_reg
677 int* constant_values_; // length == num_ssa_reg
678 // Use counts of ssa names.
679 GrowableArray<uint32_t> use_counts_; // Weighted by nesting depth
680 GrowableArray<uint32_t> raw_use_counts_; // Not weighted
681 unsigned int num_reachable_blocks_;
682 GrowableArray<int>* dfs_order_;
683 GrowableArray<int>* dfs_post_order_;
684 GrowableArray<int>* dom_post_order_traversal_;
685 int* i_dom_list_;
686 ArenaBitVector** def_block_matrix_; // num_dalvik_register x num_blocks.
687 ArenaBitVector* temp_block_v_;
688 ArenaBitVector* temp_dalvik_register_v_;
689 ArenaBitVector* temp_ssa_register_v_; // num_ssa_regs.
690 static const int kInvalidEntry = -1;
691 GrowableArray<BasicBlock*> block_list_;
692 ArenaBitVector* try_block_addr_;
693 BasicBlock* entry_block_;
694 BasicBlock* exit_block_;
695 BasicBlock* cur_block_;
696 int num_blocks_;
697 const DexFile::CodeItem* current_code_item_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700698 SafeMap<unsigned int, BasicBlock*> block_map_; // FindBlock lookup cache.
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700699 std::vector<DexCompilationUnit*> m_units_; // List of methods included in this graph
700 typedef std::pair<int, int> MIRLocation; // Insert point, (m_unit_ index, offset)
701 std::vector<MIRLocation> method_stack_; // Include stack
702 int current_method_;
703 int current_offset_;
704 int def_count_; // Used to estimate size of ssa name storage.
705 int* opcode_count_; // Dex opcode coverage stats.
706 int num_ssa_regs_; // Number of names following SSA transformation.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700707 std::vector<BasicBlock*> extended_basic_blocks_; // Heads of block "traces".
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700708 int method_sreg_;
709 unsigned int attributes_;
710 Checkstats* checkstats_;
buzbee479f83c2013-07-19 10:58:21 -0700711 SpecialCaseHandler special_case_;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700712 ArenaAllocator* arena_;
buzbee311ca162013-02-28 15:56:43 -0800713};
714
715} // namespace art
716
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700717#endif // ART_COMPILER_DEX_MIR_GRAPH_H_