blob: 45e425b32d53a0b14ce0399c3750cda14c776efa [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;
buzbeecbcfaf32013-08-19 07:37:40 -0700235 uint16_t width;
236 bool backwards_branch; // TODO: may be useful to make this an attribute flag word.
buzbee1fd33462013-03-25 13:40:45 -0700237 unsigned int offset;
238 int m_unit_index; // From which method was this MIR included
239 MIR* prev;
240 MIR* next;
241 SSARepresentation* ssa_rep;
242 int optimization_flags;
243 union {
244 // Establish link between two halves of throwing instructions.
245 MIR* throw_insn;
246 // Saved opcode for NOP'd MIRs
247 Instruction::Code original_opcode;
248 } meta;
249};
250
buzbee862a7602013-04-05 10:58:54 -0700251struct SuccessorBlockInfo;
252
buzbee1fd33462013-03-25 13:40:45 -0700253struct BasicBlock {
254 int id;
255 int dfs_id;
256 bool visited;
257 bool hidden;
258 bool catch_entry;
259 bool explicit_throw;
260 bool conditional_branch;
261 bool terminated_by_return; // Block ends with a Dalvik return opcode.
262 bool dominates_return; // Is a member of return extended basic block.
263 uint16_t start_offset;
264 uint16_t nesting_depth;
265 BBType block_type;
266 MIR* first_mir_insn;
267 MIR* last_mir_insn;
268 BasicBlock* fall_through;
269 BasicBlock* taken;
270 BasicBlock* i_dom; // Immediate dominator.
271 BasicBlockDataFlow* data_flow_info;
buzbee862a7602013-04-05 10:58:54 -0700272 GrowableArray<BasicBlock*>* predecessors;
buzbee1fd33462013-03-25 13:40:45 -0700273 ArenaBitVector* dominators;
274 ArenaBitVector* i_dominated; // Set nodes being immediately dominated.
275 ArenaBitVector* dom_frontier; // Dominance frontier.
276 struct { // For one-to-many successors like.
277 BlockListType block_list_type; // switch and exception handling.
buzbee862a7602013-04-05 10:58:54 -0700278 GrowableArray<SuccessorBlockInfo*>* blocks;
buzbee1fd33462013-03-25 13:40:45 -0700279 } successor_block_list;
280};
281
282/*
283 * The "blocks" field in "successor_block_list" points to an array of elements with the type
284 * "SuccessorBlockInfo". For catch blocks, key is type index for the exception. For swtich
285 * blocks, key is the case value.
286 */
buzbee862a7602013-04-05 10:58:54 -0700287// TODO: make class with placement new.
buzbee1fd33462013-03-25 13:40:45 -0700288struct SuccessorBlockInfo {
289 BasicBlock* block;
290 int key;
291};
292
293/*
294 * Whereas a SSA name describes a definition of a Dalvik vreg, the RegLocation describes
295 * the type of an SSA name (and, can also be used by code generators to record where the
296 * value is located (i.e. - physical register, frame, spill, etc.). For each SSA name (SReg)
297 * there is a RegLocation.
298 * FIXME: The orig_sreg field was added as a workaround for llvm bitcode generation. With
299 * the latest restructuring, we should be able to remove it and rely on s_reg_low throughout.
300 */
301struct RegLocation {
302 RegLocationType location:3;
303 unsigned wide:1;
304 unsigned defined:1; // Do we know the type?
305 unsigned is_const:1; // Constant, value in mir_graph->constant_values[].
306 unsigned fp:1; // Floating point?
307 unsigned core:1; // Non-floating point?
308 unsigned ref:1; // Something GC cares about.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700309 unsigned high_word:1; // High word of pair?
buzbee1fd33462013-03-25 13:40:45 -0700310 unsigned home:1; // Does this represent the home location?
311 uint8_t low_reg; // First physical register.
312 uint8_t high_reg; // 2nd physical register (if wide).
313 int32_t s_reg_low; // SSA name for low Dalvik word.
314 int32_t orig_sreg; // TODO: remove after Bitcode gen complete
315 // and consolodate usage w/ s_reg_low.
316};
317
318/*
319 * Collection of information describing an invoke, and the destination of
320 * the subsequent MOVE_RESULT (if applicable). Collected as a unit to enable
321 * more efficient invoke code generation.
322 */
323struct CallInfo {
324 int num_arg_words; // Note: word count, not arg count.
325 RegLocation* args; // One for each word of arguments.
326 RegLocation result; // Eventual target of MOVE_RESULT.
327 int opt_flags;
328 InvokeType type;
329 uint32_t dex_idx;
330 uint32_t index; // Method idx for invokes, type idx for FilledNewArray.
331 uintptr_t direct_code;
332 uintptr_t direct_method;
333 RegLocation target; // Target of following move_result.
334 bool skip_this;
335 bool is_range;
336 int offset; // Dalvik offset.
337};
338
339
340const RegLocation bad_loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0,
341 INVALID_REG, INVALID_REG, INVALID_SREG, INVALID_SREG};
buzbee311ca162013-02-28 15:56:43 -0800342
343class MIRGraph {
Ian Rogers71fe2672013-03-19 20:45:02 -0700344 public:
buzbee862a7602013-04-05 10:58:54 -0700345 MIRGraph(CompilationUnit* cu, ArenaAllocator* arena);
Ian Rogers6282dc12013-04-18 15:54:02 -0700346 ~MIRGraph();
buzbee311ca162013-02-28 15:56:43 -0800347
Ian Rogers71fe2672013-03-19 20:45:02 -0700348 /*
buzbeeee17e0a2013-07-31 10:47:37 -0700349 * Examine the graph to determine whether it's worthwile to spend the time compiling
350 * this method.
351 */
352 bool SkipCompilation(Runtime::CompilerFilter compiler_filter);
353
354 /*
Ian Rogers71fe2672013-03-19 20:45:02 -0700355 * Parse dex method and add MIR at current insert point. Returns id (which is
356 * actually the index of the method in the m_units_ array).
357 */
358 void InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
359 InvokeType invoke_type, uint32_t class_def_idx,
360 uint32_t method_idx, jobject class_loader, const DexFile& dex_file);
buzbee311ca162013-02-28 15:56:43 -0800361
Ian Rogers71fe2672013-03-19 20:45:02 -0700362 /* Find existing block */
363 BasicBlock* FindBlock(unsigned int code_offset) {
364 return FindBlock(code_offset, false, false, NULL);
365 }
buzbee311ca162013-02-28 15:56:43 -0800366
Ian Rogers71fe2672013-03-19 20:45:02 -0700367 const uint16_t* GetCurrentInsns() const {
368 return current_code_item_->insns_;
369 }
buzbee311ca162013-02-28 15:56:43 -0800370
Ian Rogers71fe2672013-03-19 20:45:02 -0700371 const uint16_t* GetInsns(int m_unit_index) const {
372 return m_units_[m_unit_index]->GetCodeItem()->insns_;
373 }
buzbee311ca162013-02-28 15:56:43 -0800374
Ian Rogers71fe2672013-03-19 20:45:02 -0700375 int GetNumBlocks() const {
376 return num_blocks_;
377 }
buzbee311ca162013-02-28 15:56:43 -0800378
buzbeeee17e0a2013-07-31 10:47:37 -0700379 size_t GetNumDalvikInsns() const {
380 return cu_->code_item->insns_size_in_code_units_;
381 }
382
Ian Rogers71fe2672013-03-19 20:45:02 -0700383 ArenaBitVector* GetTryBlockAddr() const {
384 return try_block_addr_;
385 }
buzbee311ca162013-02-28 15:56:43 -0800386
Ian Rogers71fe2672013-03-19 20:45:02 -0700387 BasicBlock* GetEntryBlock() const {
388 return entry_block_;
389 }
buzbee311ca162013-02-28 15:56:43 -0800390
Ian Rogers71fe2672013-03-19 20:45:02 -0700391 BasicBlock* GetExitBlock() const {
392 return exit_block_;
393 }
buzbee311ca162013-02-28 15:56:43 -0800394
Ian Rogers71fe2672013-03-19 20:45:02 -0700395 BasicBlock* GetBasicBlock(int block_id) const {
buzbee862a7602013-04-05 10:58:54 -0700396 return block_list_.Get(block_id);
Ian Rogers71fe2672013-03-19 20:45:02 -0700397 }
buzbee311ca162013-02-28 15:56:43 -0800398
Ian Rogers71fe2672013-03-19 20:45:02 -0700399 size_t GetBasicBlockListCount() const {
buzbee862a7602013-04-05 10:58:54 -0700400 return block_list_.Size();
Ian Rogers71fe2672013-03-19 20:45:02 -0700401 }
buzbee311ca162013-02-28 15:56:43 -0800402
buzbee862a7602013-04-05 10:58:54 -0700403 GrowableArray<BasicBlock*>* GetBlockList() {
Ian Rogers71fe2672013-03-19 20:45:02 -0700404 return &block_list_;
405 }
buzbee311ca162013-02-28 15:56:43 -0800406
buzbee862a7602013-04-05 10:58:54 -0700407 GrowableArray<int>* GetDfsOrder() {
408 return dfs_order_;
Ian Rogers71fe2672013-03-19 20:45:02 -0700409 }
buzbee311ca162013-02-28 15:56:43 -0800410
buzbee862a7602013-04-05 10:58:54 -0700411 GrowableArray<int>* GetDfsPostOrder() {
412 return dfs_post_order_;
Ian Rogers71fe2672013-03-19 20:45:02 -0700413 }
buzbee311ca162013-02-28 15:56:43 -0800414
buzbee862a7602013-04-05 10:58:54 -0700415 GrowableArray<int>* GetDomPostOrder() {
416 return dom_post_order_traversal_;
Ian Rogers71fe2672013-03-19 20:45:02 -0700417 }
buzbee311ca162013-02-28 15:56:43 -0800418
Ian Rogers71fe2672013-03-19 20:45:02 -0700419 int GetDefCount() const {
420 return def_count_;
421 }
buzbee311ca162013-02-28 15:56:43 -0800422
buzbee862a7602013-04-05 10:58:54 -0700423 ArenaAllocator* GetArena() {
424 return arena_;
425 }
426
Ian Rogers71fe2672013-03-19 20:45:02 -0700427 void EnableOpcodeCounting() {
buzbee862a7602013-04-05 10:58:54 -0700428 opcode_count_ = static_cast<int*>(arena_->NewMem(kNumPackedOpcodes * sizeof(int), true,
429 ArenaAllocator::kAllocMisc));
Ian Rogers71fe2672013-03-19 20:45:02 -0700430 }
buzbee311ca162013-02-28 15:56:43 -0800431
Ian Rogers71fe2672013-03-19 20:45:02 -0700432 void ShowOpcodeStats();
buzbee311ca162013-02-28 15:56:43 -0800433
Ian Rogers71fe2672013-03-19 20:45:02 -0700434 DexCompilationUnit* GetCurrentDexCompilationUnit() const {
435 return m_units_[current_method_];
436 }
buzbee311ca162013-02-28 15:56:43 -0800437
Ian Rogers71fe2672013-03-19 20:45:02 -0700438 void DumpCFG(const char* dir_prefix, bool all_blocks);
buzbee311ca162013-02-28 15:56:43 -0800439
Ian Rogers71fe2672013-03-19 20:45:02 -0700440 void BuildRegLocations();
buzbee311ca162013-02-28 15:56:43 -0800441
Ian Rogers71fe2672013-03-19 20:45:02 -0700442 void DumpRegLocTable(RegLocation* table, int count);
buzbee311ca162013-02-28 15:56:43 -0800443
Ian Rogers71fe2672013-03-19 20:45:02 -0700444 void BasicBlockOptimization();
buzbee311ca162013-02-28 15:56:43 -0800445
Ian Rogers71fe2672013-03-19 20:45:02 -0700446 bool IsConst(int32_t s_reg) const {
buzbee862a7602013-04-05 10:58:54 -0700447 return is_constant_v_->IsBitSet(s_reg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700448 }
buzbee311ca162013-02-28 15:56:43 -0800449
Ian Rogers71fe2672013-03-19 20:45:02 -0700450 bool IsConst(RegLocation loc) const {
451 return (IsConst(loc.orig_sreg));
452 }
buzbee311ca162013-02-28 15:56:43 -0800453
Ian Rogers71fe2672013-03-19 20:45:02 -0700454 int32_t ConstantValue(RegLocation loc) const {
455 DCHECK(IsConst(loc));
456 return constant_values_[loc.orig_sreg];
457 }
buzbee311ca162013-02-28 15:56:43 -0800458
Ian Rogers71fe2672013-03-19 20:45:02 -0700459 int32_t ConstantValue(int32_t s_reg) const {
460 DCHECK(IsConst(s_reg));
461 return constant_values_[s_reg];
462 }
buzbee311ca162013-02-28 15:56:43 -0800463
Ian Rogers71fe2672013-03-19 20:45:02 -0700464 int64_t ConstantValueWide(RegLocation loc) const {
465 DCHECK(IsConst(loc));
466 return (static_cast<int64_t>(constant_values_[loc.orig_sreg + 1]) << 32) |
467 Low32Bits(static_cast<int64_t>(constant_values_[loc.orig_sreg]));
468 }
buzbee311ca162013-02-28 15:56:43 -0800469
Ian Rogers71fe2672013-03-19 20:45:02 -0700470 bool IsConstantNullRef(RegLocation loc) const {
471 return loc.ref && loc.is_const && (ConstantValue(loc) == 0);
472 }
buzbee311ca162013-02-28 15:56:43 -0800473
Ian Rogers71fe2672013-03-19 20:45:02 -0700474 int GetNumSSARegs() const {
475 return num_ssa_regs_;
476 }
buzbee311ca162013-02-28 15:56:43 -0800477
Ian Rogers71fe2672013-03-19 20:45:02 -0700478 void SetNumSSARegs(int new_num) {
479 num_ssa_regs_ = new_num;
480 }
buzbee311ca162013-02-28 15:56:43 -0800481
buzbee862a7602013-04-05 10:58:54 -0700482 unsigned int GetNumReachableBlocks() const {
Ian Rogers71fe2672013-03-19 20:45:02 -0700483 return num_reachable_blocks_;
484 }
buzbee311ca162013-02-28 15:56:43 -0800485
Ian Rogers71fe2672013-03-19 20:45:02 -0700486 int GetUseCount(int vreg) const {
buzbee862a7602013-04-05 10:58:54 -0700487 return use_counts_.Get(vreg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700488 }
buzbee311ca162013-02-28 15:56:43 -0800489
Ian Rogers71fe2672013-03-19 20:45:02 -0700490 int GetRawUseCount(int vreg) const {
buzbee862a7602013-04-05 10:58:54 -0700491 return raw_use_counts_.Get(vreg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700492 }
buzbee311ca162013-02-28 15:56:43 -0800493
Ian Rogers71fe2672013-03-19 20:45:02 -0700494 int GetSSASubscript(int ssa_reg) const {
buzbee862a7602013-04-05 10:58:54 -0700495 return ssa_subscripts_->Get(ssa_reg);
Ian Rogers71fe2672013-03-19 20:45:02 -0700496 }
buzbee311ca162013-02-28 15:56:43 -0800497
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700498 RegLocation GetRawSrc(MIR* mir, int num) {
buzbee1fd33462013-03-25 13:40:45 -0700499 DCHECK(num < mir->ssa_rep->num_uses);
500 RegLocation res = reg_location_[mir->ssa_rep->uses[num]];
501 return res;
502 }
503
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700504 RegLocation GetRawDest(MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700505 DCHECK_GT(mir->ssa_rep->num_defs, 0);
506 RegLocation res = reg_location_[mir->ssa_rep->defs[0]];
507 return res;
508 }
509
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700510 RegLocation GetDest(MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700511 RegLocation res = GetRawDest(mir);
512 DCHECK(!res.wide);
513 return res;
514 }
515
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700516 RegLocation GetSrc(MIR* mir, int num) {
buzbee1fd33462013-03-25 13:40:45 -0700517 RegLocation res = GetRawSrc(mir, num);
518 DCHECK(!res.wide);
519 return res;
520 }
521
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700522 RegLocation GetDestWide(MIR* mir) {
buzbee1fd33462013-03-25 13:40:45 -0700523 RegLocation res = GetRawDest(mir);
524 DCHECK(res.wide);
525 return res;
526 }
527
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700528 RegLocation GetSrcWide(MIR* mir, int low) {
buzbee1fd33462013-03-25 13:40:45 -0700529 RegLocation res = GetRawSrc(mir, low);
530 DCHECK(res.wide);
531 return res;
532 }
533
534 RegLocation GetBadLoc() {
535 return bad_loc;
536 }
537
538 int GetMethodSReg() {
539 return method_sreg_;
540 }
541
542 bool MethodIsLeaf() {
543 return attributes_ & METHOD_IS_LEAF;
544 }
545
546 RegLocation GetRegLocation(int index) {
547 DCHECK((index >= 0) && (index > num_ssa_regs_));
548 return reg_location_[index];
549 }
550
551 RegLocation GetMethodLoc() {
552 return reg_location_[method_sreg_];
553 }
554
buzbee479f83c2013-07-19 10:58:21 -0700555 bool IsSpecialCase() {
556 return special_case_ != kNoHandler;
557 }
558
559 SpecialCaseHandler GetSpecialCase() {
560 return special_case_;
561 }
562
Ian Rogers71fe2672013-03-19 20:45:02 -0700563 void BasicBlockCombine();
564 void CodeLayout();
565 void DumpCheckStats();
566 void PropagateConstants();
567 MIR* FindMoveResult(BasicBlock* bb, MIR* mir);
568 int SRegToVReg(int ssa_reg) const;
569 void VerifyDataflow();
570 void MethodUseCount();
571 void SSATransformation();
572 void CheckForDominanceFrontier(BasicBlock* dom_bb, const BasicBlock* succ_bb);
573 void NullCheckElimination();
buzbee1fd33462013-03-25 13:40:45 -0700574 bool SetFp(int index, bool is_fp);
575 bool SetCore(int index, bool is_core);
576 bool SetRef(int index, bool is_ref);
577 bool SetWide(int index, bool is_wide);
578 bool SetHigh(int index, bool is_high);
579 void AppendMIR(BasicBlock* bb, MIR* mir);
580 void PrependMIR(BasicBlock* bb, MIR* mir);
581 void InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir);
582 char* GetDalvikDisassembly(const MIR* mir);
buzbee1fd33462013-03-25 13:40:45 -0700583 void ReplaceSpecialChars(std::string& str);
584 std::string GetSSAName(int ssa_reg);
585 std::string GetSSANameWithConst(int ssa_reg, bool singles_only);
586 void GetBlockName(BasicBlock* bb, char* name);
587 const char* GetShortyFromTargetIdx(int);
588 void DumpMIRGraph();
589 CallInfo* NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type, bool is_range);
buzbee862a7602013-04-05 10:58:54 -0700590 BasicBlock* NewMemBB(BBType block_type, int block_id);
buzbee311ca162013-02-28 15:56:43 -0800591
Ian Rogers71fe2672013-03-19 20:45:02 -0700592 /*
593 * IsDebugBuild sanity check: keep track of the Dex PCs for catch entries so that later on
594 * we can verify that all catch entries have native PC entries.
595 */
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700596 std::set<uint32_t> catches_;
buzbee311ca162013-02-28 15:56:43 -0800597
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700598 // TODO: make these private.
599 RegLocation* reg_location_; // Map SSA names to location.
600 GrowableArray<CompilerTemp*> compiler_temps_;
601 SafeMap<unsigned int, unsigned int> block_id_map_; // Block collapse lookup cache.
buzbee1fd33462013-03-25 13:40:45 -0700602
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700603 static const int oat_data_flow_attributes_[kMirOpLast];
604 static const char* extended_mir_op_names_[kMirOpLast - kMirOpFirst];
buzbeeee17e0a2013-07-31 10:47:37 -0700605 static const uint32_t analysis_attributes_[kMirOpLast];
buzbee1fd33462013-03-25 13:40:45 -0700606
Ian Rogers71fe2672013-03-19 20:45:02 -0700607 private:
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700608 int FindCommonParent(int block1, int block2);
609 void ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
610 const ArenaBitVector* src2);
611 void HandleLiveInUse(ArenaBitVector* use_v, ArenaBitVector* def_v,
612 ArenaBitVector* live_in_v, int dalvik_reg_id);
613 void HandleDef(ArenaBitVector* def_v, int dalvik_reg_id);
614 void CompilerInitializeSSAConversion();
615 bool DoSSAConversion(BasicBlock* bb);
616 bool InvokeUsesMethodStar(MIR* mir);
617 int ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction);
618 bool ContentIsInsn(const uint16_t* code_ptr);
619 BasicBlock* SplitBlock(unsigned int code_offset, BasicBlock* orig_block,
Ian Rogers71fe2672013-03-19 20:45:02 -0700620 BasicBlock** immed_pred_block_p);
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700621 BasicBlock* FindBlock(unsigned int code_offset, bool split, bool create,
622 BasicBlock** immed_pred_block_p);
623 void ProcessTryCatchBlocks();
624 BasicBlock* ProcessCanBranch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
625 int flags, const uint16_t* code_ptr, const uint16_t* code_end);
626 void ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, int flags);
627 BasicBlock* ProcessCanThrow(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
628 int flags, ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
629 const uint16_t* code_end);
630 int AddNewSReg(int v_reg);
631 void HandleSSAUse(int* uses, int dalvik_reg, int reg_index);
632 void HandleSSADef(int* defs, int dalvik_reg, int reg_index);
633 void DataFlowSSAFormat35C(MIR* mir);
634 void DataFlowSSAFormat3RC(MIR* mir);
635 bool FindLocalLiveIn(BasicBlock* bb);
636 void ClearAllVisitedFlags();
637 bool CountUses(struct BasicBlock* bb);
638 bool InferTypeAndSize(BasicBlock* bb);
639 bool VerifyPredInfo(BasicBlock* bb);
640 BasicBlock* NeedsVisit(BasicBlock* bb);
641 BasicBlock* NextUnvisitedSuccessor(BasicBlock* bb);
642 void MarkPreOrder(BasicBlock* bb);
643 void RecordDFSOrders(BasicBlock* bb);
644 void ComputeDFSOrders();
645 void ComputeDefBlockMatrix();
646 void ComputeDomPostOrderTraversal(BasicBlock* bb);
647 void ComputeDominators();
648 void InsertPhiNodes();
649 void DoDFSPreOrderSSARename(BasicBlock* block);
650 void SetConstant(int32_t ssa_reg, int value);
651 void SetConstantWide(int ssa_reg, int64_t value);
652 int GetSSAUseCount(int s_reg);
653 bool BasicBlockOpt(BasicBlock* bb);
654 bool EliminateNullChecks(BasicBlock* bb);
655 void NullCheckEliminationInit(BasicBlock* bb);
656 bool BuildExtendedBBList(struct BasicBlock* bb);
657 bool FillDefBlockMatrix(BasicBlock* bb);
658 void InitializeDominationInfo(BasicBlock* bb);
659 bool ComputeblockIDom(BasicBlock* bb);
660 bool ComputeBlockDominators(BasicBlock* bb);
661 bool SetDominators(BasicBlock* bb);
662 bool ComputeBlockLiveIns(BasicBlock* bb);
663 bool InsertPhiNodeOperands(BasicBlock* bb);
664 bool ComputeDominanceFrontier(BasicBlock* bb);
665 void DoConstantPropogation(BasicBlock* bb);
666 void CountChecks(BasicBlock* bb);
667 bool CombineBlocks(BasicBlock* bb);
buzbeeee17e0a2013-07-31 10:47:37 -0700668 void AnalyzeBlock(BasicBlock* bb, struct MethodStats* stats);
669 bool ComputeSkipCompilation(struct MethodStats* stats, bool skip_default);
buzbee311ca162013-02-28 15:56:43 -0800670
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700671 CompilationUnit* const cu_;
672 GrowableArray<int>* ssa_base_vregs_;
673 GrowableArray<int>* ssa_subscripts_;
674 // Map original Dalvik virtual reg i to the current SSA name.
675 int* vreg_to_ssa_map_; // length == method->registers_size
676 int* ssa_last_defs_; // length == method->registers_size
677 ArenaBitVector* is_constant_v_; // length == num_ssa_reg
678 int* constant_values_; // length == num_ssa_reg
679 // Use counts of ssa names.
680 GrowableArray<uint32_t> use_counts_; // Weighted by nesting depth
681 GrowableArray<uint32_t> raw_use_counts_; // Not weighted
682 unsigned int num_reachable_blocks_;
683 GrowableArray<int>* dfs_order_;
684 GrowableArray<int>* dfs_post_order_;
685 GrowableArray<int>* dom_post_order_traversal_;
686 int* i_dom_list_;
687 ArenaBitVector** def_block_matrix_; // num_dalvik_register x num_blocks.
688 ArenaBitVector* temp_block_v_;
689 ArenaBitVector* temp_dalvik_register_v_;
690 ArenaBitVector* temp_ssa_register_v_; // num_ssa_regs.
691 static const int kInvalidEntry = -1;
692 GrowableArray<BasicBlock*> block_list_;
693 ArenaBitVector* try_block_addr_;
694 BasicBlock* entry_block_;
695 BasicBlock* exit_block_;
696 BasicBlock* cur_block_;
697 int num_blocks_;
698 const DexFile::CodeItem* current_code_item_;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700699 SafeMap<unsigned int, BasicBlock*> block_map_; // FindBlock lookup cache.
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700700 std::vector<DexCompilationUnit*> m_units_; // List of methods included in this graph
701 typedef std::pair<int, int> MIRLocation; // Insert point, (m_unit_ index, offset)
702 std::vector<MIRLocation> method_stack_; // Include stack
703 int current_method_;
704 int current_offset_;
705 int def_count_; // Used to estimate size of ssa name storage.
706 int* opcode_count_; // Dex opcode coverage stats.
707 int num_ssa_regs_; // Number of names following SSA transformation.
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700708 std::vector<BasicBlock*> extended_basic_blocks_; // Heads of block "traces".
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700709 int method_sreg_;
710 unsigned int attributes_;
711 Checkstats* checkstats_;
buzbee479f83c2013-07-19 10:58:21 -0700712 SpecialCaseHandler special_case_;
Brian Carlstrom6f485c62013-07-18 15:35:35 -0700713 ArenaAllocator* arena_;
buzbee311ca162013-02-28 15:56:43 -0800714};
715
716} // namespace art
717
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700718#endif // ART_COMPILER_DEX_MIR_GRAPH_H_