buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_DEX_MIR_GRAPH_H_ |
| 18 | #define ART_COMPILER_DEX_MIR_GRAPH_H_ |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 19 | |
| 20 | #include "dex_file.h" |
| 21 | #include "dex_instruction.h" |
| 22 | #include "compiler_ir.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 23 | #include "arena_bit_vector.h" |
| 24 | #include "growable_array.h" |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | enum DataFlowAttributePos { |
| 29 | kUA = 0, |
| 30 | kUB, |
| 31 | kUC, |
| 32 | kAWide, |
| 33 | kBWide, |
| 34 | kCWide, |
| 35 | kDA, |
| 36 | kIsMove, |
| 37 | kSetsConst, |
| 38 | kFormat35c, |
| 39 | kFormat3rc, |
| 40 | kNullCheckSrc0, // Null check of uses[0]. |
| 41 | kNullCheckSrc1, // Null check of uses[1]. |
| 42 | kNullCheckSrc2, // Null check of uses[2]. |
| 43 | kNullCheckOut0, // Null check out outgoing arg0. |
| 44 | kDstNonNull, // May assume dst is non-null. |
| 45 | kRetNonNull, // May assume retval is non-null. |
| 46 | kNullTransferSrc0, // Object copy src[0] -> dst. |
| 47 | kNullTransferSrcN, // Phi null check state transfer. |
| 48 | kRangeCheckSrc1, // Range check of uses[1]. |
| 49 | kRangeCheckSrc2, // Range check of uses[2]. |
| 50 | kRangeCheckSrc3, // Range check of uses[3]. |
| 51 | kFPA, |
| 52 | kFPB, |
| 53 | kFPC, |
| 54 | kCoreA, |
| 55 | kCoreB, |
| 56 | kCoreC, |
| 57 | kRefA, |
| 58 | kRefB, |
| 59 | kRefC, |
| 60 | kUsesMethodStar, // Implicit use of Method*. |
| 61 | }; |
| 62 | |
| 63 | #define DF_NOP 0 |
| 64 | #define DF_UA (1 << kUA) |
| 65 | #define DF_UB (1 << kUB) |
| 66 | #define DF_UC (1 << kUC) |
| 67 | #define DF_A_WIDE (1 << kAWide) |
| 68 | #define DF_B_WIDE (1 << kBWide) |
| 69 | #define DF_C_WIDE (1 << kCWide) |
| 70 | #define DF_DA (1 << kDA) |
| 71 | #define DF_IS_MOVE (1 << kIsMove) |
| 72 | #define DF_SETS_CONST (1 << kSetsConst) |
| 73 | #define DF_FORMAT_35C (1 << kFormat35c) |
| 74 | #define DF_FORMAT_3RC (1 << kFormat3rc) |
| 75 | #define DF_NULL_CHK_0 (1 << kNullCheckSrc0) |
| 76 | #define DF_NULL_CHK_1 (1 << kNullCheckSrc1) |
| 77 | #define DF_NULL_CHK_2 (1 << kNullCheckSrc2) |
| 78 | #define DF_NULL_CHK_OUT0 (1 << kNullCheckOut0) |
| 79 | #define DF_NON_NULL_DST (1 << kDstNonNull) |
| 80 | #define DF_NON_NULL_RET (1 << kRetNonNull) |
| 81 | #define DF_NULL_TRANSFER_0 (1 << kNullTransferSrc0) |
| 82 | #define DF_NULL_TRANSFER_N (1 << kNullTransferSrcN) |
| 83 | #define DF_RANGE_CHK_1 (1 << kRangeCheckSrc1) |
| 84 | #define DF_RANGE_CHK_2 (1 << kRangeCheckSrc2) |
| 85 | #define DF_RANGE_CHK_3 (1 << kRangeCheckSrc3) |
| 86 | #define DF_FP_A (1 << kFPA) |
| 87 | #define DF_FP_B (1 << kFPB) |
| 88 | #define DF_FP_C (1 << kFPC) |
| 89 | #define DF_CORE_A (1 << kCoreA) |
| 90 | #define DF_CORE_B (1 << kCoreB) |
| 91 | #define DF_CORE_C (1 << kCoreC) |
| 92 | #define DF_REF_A (1 << kRefA) |
| 93 | #define DF_REF_B (1 << kRefB) |
| 94 | #define DF_REF_C (1 << kRefC) |
| 95 | #define DF_UMS (1 << kUsesMethodStar) |
| 96 | |
| 97 | #define DF_HAS_USES (DF_UA | DF_UB | DF_UC) |
| 98 | |
| 99 | #define DF_HAS_DEFS (DF_DA) |
| 100 | |
| 101 | #define DF_HAS_NULL_CHKS (DF_NULL_CHK_0 | \ |
| 102 | DF_NULL_CHK_1 | \ |
| 103 | DF_NULL_CHK_2 | \ |
| 104 | DF_NULL_CHK_OUT0) |
| 105 | |
| 106 | #define DF_HAS_RANGE_CHKS (DF_RANGE_CHK_1 | \ |
| 107 | DF_RANGE_CHK_2 | \ |
| 108 | DF_RANGE_CHK_3) |
| 109 | |
| 110 | #define DF_HAS_NR_CHKS (DF_HAS_NULL_CHKS | \ |
| 111 | DF_HAS_RANGE_CHKS) |
| 112 | |
| 113 | #define DF_A_IS_REG (DF_UA | DF_DA) |
| 114 | #define DF_B_IS_REG (DF_UB) |
| 115 | #define DF_C_IS_REG (DF_UC) |
| 116 | #define DF_IS_GETTER_OR_SETTER (DF_IS_GETTER | DF_IS_SETTER) |
| 117 | #define DF_USES_FP (DF_FP_A | DF_FP_B | DF_FP_C) |
| 118 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 119 | enum OatMethodAttributes { |
| 120 | kIsLeaf, // Method is leaf. |
| 121 | kHasLoop, // Method contains simple loop. |
| 122 | }; |
| 123 | |
| 124 | #define METHOD_IS_LEAF (1 << kIsLeaf) |
| 125 | #define METHOD_HAS_LOOP (1 << kHasLoop) |
| 126 | |
| 127 | // Minimum field size to contain Dalvik v_reg number. |
| 128 | #define VREG_NUM_WIDTH 16 |
| 129 | |
| 130 | #define INVALID_SREG (-1) |
| 131 | #define INVALID_VREG (0xFFFFU) |
| 132 | #define INVALID_REG (0xFF) |
| 133 | #define INVALID_OFFSET (0xDEADF00FU) |
| 134 | |
| 135 | /* SSA encodings for special registers */ |
| 136 | #define SSA_METHOD_BASEREG (-2) |
| 137 | /* First compiler temp basereg, grows smaller */ |
| 138 | #define SSA_CTEMP_BASEREG (SSA_METHOD_BASEREG - 1) |
| 139 | |
| 140 | #define MIR_IGNORE_NULL_CHECK (1 << kMIRIgnoreNullCheck) |
| 141 | #define MIR_NULL_CHECK_ONLY (1 << kMIRNullCheckOnly) |
| 142 | #define MIR_IGNORE_RANGE_CHECK (1 << kMIRIgnoreRangeCheck) |
| 143 | #define MIR_RANGE_CHECK_ONLY (1 << kMIRRangeCheckOnly) |
| 144 | #define MIR_INLINED (1 << kMIRInlined) |
| 145 | #define MIR_INLINED_PRED (1 << kMIRInlinedPred) |
| 146 | #define MIR_CALLEE (1 << kMIRCallee) |
| 147 | #define MIR_IGNORE_SUSPEND_CHECK (1 << kMIRIgnoreSuspendCheck) |
| 148 | #define MIR_DUP (1 << kMIRDup) |
| 149 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 150 | #define BLOCK_NAME_LEN 80 |
| 151 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 152 | /* |
| 153 | * In general, vreg/sreg describe Dalvik registers that originated with dx. However, |
| 154 | * it is useful to have compiler-generated temporary registers and have them treated |
| 155 | * in the same manner as dx-generated virtual registers. This struct records the SSA |
| 156 | * name of compiler-introduced temporaries. |
| 157 | */ |
| 158 | struct CompilerTemp { |
| 159 | int s_reg; |
| 160 | }; |
| 161 | |
| 162 | // When debug option enabled, records effectiveness of null and range check elimination. |
| 163 | struct Checkstats { |
| 164 | int null_checks; |
| 165 | int null_checks_eliminated; |
| 166 | int range_checks; |
| 167 | int range_checks_eliminated; |
| 168 | }; |
| 169 | |
| 170 | // Dataflow attributes of a basic block. |
| 171 | struct BasicBlockDataFlow { |
| 172 | ArenaBitVector* use_v; |
| 173 | ArenaBitVector* def_v; |
| 174 | ArenaBitVector* live_in_v; |
| 175 | ArenaBitVector* phi_v; |
| 176 | int* vreg_to_ssa_map; |
| 177 | ArenaBitVector* ending_null_check_v; |
| 178 | }; |
| 179 | |
| 180 | /* |
| 181 | * Normalized use/def for a MIR operation using SSA names rather than vregs. Note that |
| 182 | * uses/defs retain the Dalvik convention that long operations operate on a pair of 32-bit |
| 183 | * vregs. For example, "ADD_LONG v0, v2, v3" would have 2 defs (v0/v1) and 4 uses (v2/v3, v4/v5). |
| 184 | * Following SSA renaming, this is the primary struct used by code generators to locate |
| 185 | * operand and result registers. This is a somewhat confusing and unhelpful convention that |
| 186 | * we may want to revisit in the future. |
| 187 | */ |
| 188 | struct SSARepresentation { |
| 189 | int num_uses; |
| 190 | int* uses; |
| 191 | bool* fp_use; |
| 192 | int num_defs; |
| 193 | int* defs; |
| 194 | bool* fp_def; |
| 195 | }; |
| 196 | |
| 197 | /* |
| 198 | * The Midlevel Intermediate Representation node, which may be largely considered a |
| 199 | * wrapper around a Dalvik byte code. |
| 200 | */ |
| 201 | struct MIR { |
| 202 | DecodedInstruction dalvikInsn; |
| 203 | unsigned int width; |
| 204 | unsigned int offset; |
| 205 | int m_unit_index; // From which method was this MIR included |
| 206 | MIR* prev; |
| 207 | MIR* next; |
| 208 | SSARepresentation* ssa_rep; |
| 209 | int optimization_flags; |
| 210 | union { |
| 211 | // Establish link between two halves of throwing instructions. |
| 212 | MIR* throw_insn; |
| 213 | // Saved opcode for NOP'd MIRs |
| 214 | Instruction::Code original_opcode; |
| 215 | } meta; |
| 216 | }; |
| 217 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 218 | struct SuccessorBlockInfo; |
| 219 | |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 220 | struct BasicBlock { |
| 221 | int id; |
| 222 | int dfs_id; |
| 223 | bool visited; |
| 224 | bool hidden; |
| 225 | bool catch_entry; |
| 226 | bool explicit_throw; |
| 227 | bool conditional_branch; |
| 228 | bool terminated_by_return; // Block ends with a Dalvik return opcode. |
| 229 | bool dominates_return; // Is a member of return extended basic block. |
| 230 | uint16_t start_offset; |
| 231 | uint16_t nesting_depth; |
| 232 | BBType block_type; |
| 233 | MIR* first_mir_insn; |
| 234 | MIR* last_mir_insn; |
| 235 | BasicBlock* fall_through; |
| 236 | BasicBlock* taken; |
| 237 | BasicBlock* i_dom; // Immediate dominator. |
| 238 | BasicBlockDataFlow* data_flow_info; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 239 | GrowableArray<BasicBlock*>* predecessors; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 240 | ArenaBitVector* dominators; |
| 241 | ArenaBitVector* i_dominated; // Set nodes being immediately dominated. |
| 242 | ArenaBitVector* dom_frontier; // Dominance frontier. |
| 243 | struct { // For one-to-many successors like. |
| 244 | BlockListType block_list_type; // switch and exception handling. |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 245 | GrowableArray<SuccessorBlockInfo*>* blocks; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 246 | } successor_block_list; |
| 247 | }; |
| 248 | |
| 249 | /* |
| 250 | * The "blocks" field in "successor_block_list" points to an array of elements with the type |
| 251 | * "SuccessorBlockInfo". For catch blocks, key is type index for the exception. For swtich |
| 252 | * blocks, key is the case value. |
| 253 | */ |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 254 | // TODO: make class with placement new. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 255 | struct SuccessorBlockInfo { |
| 256 | BasicBlock* block; |
| 257 | int key; |
| 258 | }; |
| 259 | |
| 260 | /* |
| 261 | * Whereas a SSA name describes a definition of a Dalvik vreg, the RegLocation describes |
| 262 | * the type of an SSA name (and, can also be used by code generators to record where the |
| 263 | * value is located (i.e. - physical register, frame, spill, etc.). For each SSA name (SReg) |
| 264 | * there is a RegLocation. |
| 265 | * FIXME: The orig_sreg field was added as a workaround for llvm bitcode generation. With |
| 266 | * the latest restructuring, we should be able to remove it and rely on s_reg_low throughout. |
| 267 | */ |
| 268 | struct RegLocation { |
| 269 | RegLocationType location:3; |
| 270 | unsigned wide:1; |
| 271 | unsigned defined:1; // Do we know the type? |
| 272 | unsigned is_const:1; // Constant, value in mir_graph->constant_values[]. |
| 273 | unsigned fp:1; // Floating point? |
| 274 | unsigned core:1; // Non-floating point? |
| 275 | unsigned ref:1; // Something GC cares about. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame^] | 276 | unsigned high_word:1; // High word of pair? |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 277 | unsigned home:1; // Does this represent the home location? |
| 278 | uint8_t low_reg; // First physical register. |
| 279 | uint8_t high_reg; // 2nd physical register (if wide). |
| 280 | int32_t s_reg_low; // SSA name for low Dalvik word. |
| 281 | int32_t orig_sreg; // TODO: remove after Bitcode gen complete |
| 282 | // and consolodate usage w/ s_reg_low. |
| 283 | }; |
| 284 | |
| 285 | /* |
| 286 | * Collection of information describing an invoke, and the destination of |
| 287 | * the subsequent MOVE_RESULT (if applicable). Collected as a unit to enable |
| 288 | * more efficient invoke code generation. |
| 289 | */ |
| 290 | struct CallInfo { |
| 291 | int num_arg_words; // Note: word count, not arg count. |
| 292 | RegLocation* args; // One for each word of arguments. |
| 293 | RegLocation result; // Eventual target of MOVE_RESULT. |
| 294 | int opt_flags; |
| 295 | InvokeType type; |
| 296 | uint32_t dex_idx; |
| 297 | uint32_t index; // Method idx for invokes, type idx for FilledNewArray. |
| 298 | uintptr_t direct_code; |
| 299 | uintptr_t direct_method; |
| 300 | RegLocation target; // Target of following move_result. |
| 301 | bool skip_this; |
| 302 | bool is_range; |
| 303 | int offset; // Dalvik offset. |
| 304 | }; |
| 305 | |
| 306 | |
| 307 | const RegLocation bad_loc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0, 0, 0, |
| 308 | INVALID_REG, INVALID_REG, INVALID_SREG, INVALID_SREG}; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 309 | |
| 310 | class MIRGraph { |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 311 | public: |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 312 | MIRGraph(CompilationUnit* cu, ArenaAllocator* arena); |
Ian Rogers | 6282dc1 | 2013-04-18 15:54:02 -0700 | [diff] [blame] | 313 | ~MIRGraph(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 314 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 315 | /* |
| 316 | * Parse dex method and add MIR at current insert point. Returns id (which is |
| 317 | * actually the index of the method in the m_units_ array). |
| 318 | */ |
| 319 | void InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, |
| 320 | InvokeType invoke_type, uint32_t class_def_idx, |
| 321 | uint32_t method_idx, jobject class_loader, const DexFile& dex_file); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 322 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 323 | /* Find existing block */ |
| 324 | BasicBlock* FindBlock(unsigned int code_offset) { |
| 325 | return FindBlock(code_offset, false, false, NULL); |
| 326 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 327 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 328 | const uint16_t* GetCurrentInsns() const { |
| 329 | return current_code_item_->insns_; |
| 330 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 331 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 332 | const uint16_t* GetInsns(int m_unit_index) const { |
| 333 | return m_units_[m_unit_index]->GetCodeItem()->insns_; |
| 334 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 335 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 336 | int GetNumBlocks() const { |
| 337 | return num_blocks_; |
| 338 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 339 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 340 | ArenaBitVector* GetTryBlockAddr() const { |
| 341 | return try_block_addr_; |
| 342 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 343 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 344 | BasicBlock* GetEntryBlock() const { |
| 345 | return entry_block_; |
| 346 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 347 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 348 | BasicBlock* GetExitBlock() const { |
| 349 | return exit_block_; |
| 350 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 351 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 352 | BasicBlock* GetBasicBlock(int block_id) const { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 353 | return block_list_.Get(block_id); |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 354 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 355 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 356 | size_t GetBasicBlockListCount() const { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 357 | return block_list_.Size(); |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 358 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 359 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 360 | GrowableArray<BasicBlock*>* GetBlockList() { |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 361 | return &block_list_; |
| 362 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 363 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 364 | GrowableArray<int>* GetDfsOrder() { |
| 365 | return dfs_order_; |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 366 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 367 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 368 | GrowableArray<int>* GetDfsPostOrder() { |
| 369 | return dfs_post_order_; |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 370 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 371 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 372 | GrowableArray<int>* GetDomPostOrder() { |
| 373 | return dom_post_order_traversal_; |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 374 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 375 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 376 | int GetDefCount() const { |
| 377 | return def_count_; |
| 378 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 379 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 380 | ArenaAllocator* GetArena() { |
| 381 | return arena_; |
| 382 | } |
| 383 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 384 | void EnableOpcodeCounting() { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 385 | opcode_count_ = static_cast<int*>(arena_->NewMem(kNumPackedOpcodes * sizeof(int), true, |
| 386 | ArenaAllocator::kAllocMisc)); |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 387 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 388 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 389 | void ShowOpcodeStats(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 390 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 391 | DexCompilationUnit* GetCurrentDexCompilationUnit() const { |
| 392 | return m_units_[current_method_]; |
| 393 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 394 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 395 | void DumpCFG(const char* dir_prefix, bool all_blocks); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 396 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 397 | void BuildRegLocations(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 398 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 399 | void DumpRegLocTable(RegLocation* table, int count); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 400 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 401 | void BasicBlockOptimization(); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 402 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 403 | bool IsConst(int32_t s_reg) const { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 404 | return is_constant_v_->IsBitSet(s_reg); |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 405 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 406 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 407 | bool IsConst(RegLocation loc) const { |
| 408 | return (IsConst(loc.orig_sreg)); |
| 409 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 410 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 411 | int32_t ConstantValue(RegLocation loc) const { |
| 412 | DCHECK(IsConst(loc)); |
| 413 | return constant_values_[loc.orig_sreg]; |
| 414 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 415 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 416 | int32_t ConstantValue(int32_t s_reg) const { |
| 417 | DCHECK(IsConst(s_reg)); |
| 418 | return constant_values_[s_reg]; |
| 419 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 420 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 421 | int64_t ConstantValueWide(RegLocation loc) const { |
| 422 | DCHECK(IsConst(loc)); |
| 423 | return (static_cast<int64_t>(constant_values_[loc.orig_sreg + 1]) << 32) | |
| 424 | Low32Bits(static_cast<int64_t>(constant_values_[loc.orig_sreg])); |
| 425 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 426 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 427 | bool IsConstantNullRef(RegLocation loc) const { |
| 428 | return loc.ref && loc.is_const && (ConstantValue(loc) == 0); |
| 429 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 430 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 431 | int GetNumSSARegs() const { |
| 432 | return num_ssa_regs_; |
| 433 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 434 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 435 | void SetNumSSARegs(int new_num) { |
| 436 | num_ssa_regs_ = new_num; |
| 437 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 438 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 439 | unsigned int GetNumReachableBlocks() const { |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 440 | return num_reachable_blocks_; |
| 441 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 442 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 443 | int GetUseCount(int vreg) const { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 444 | return use_counts_.Get(vreg); |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 445 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 446 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 447 | int GetRawUseCount(int vreg) const { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 448 | return raw_use_counts_.Get(vreg); |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 449 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 450 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 451 | int GetSSASubscript(int ssa_reg) const { |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 452 | return ssa_subscripts_->Get(ssa_reg); |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 453 | } |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 454 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 455 | RegLocation GetRawSrc(MIR* mir, int num) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 456 | DCHECK(num < mir->ssa_rep->num_uses); |
| 457 | RegLocation res = reg_location_[mir->ssa_rep->uses[num]]; |
| 458 | return res; |
| 459 | } |
| 460 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 461 | RegLocation GetRawDest(MIR* mir) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 462 | DCHECK_GT(mir->ssa_rep->num_defs, 0); |
| 463 | RegLocation res = reg_location_[mir->ssa_rep->defs[0]]; |
| 464 | return res; |
| 465 | } |
| 466 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 467 | RegLocation GetDest(MIR* mir) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 468 | RegLocation res = GetRawDest(mir); |
| 469 | DCHECK(!res.wide); |
| 470 | return res; |
| 471 | } |
| 472 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 473 | RegLocation GetSrc(MIR* mir, int num) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 474 | RegLocation res = GetRawSrc(mir, num); |
| 475 | DCHECK(!res.wide); |
| 476 | return res; |
| 477 | } |
| 478 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 479 | RegLocation GetDestWide(MIR* mir) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 480 | RegLocation res = GetRawDest(mir); |
| 481 | DCHECK(res.wide); |
| 482 | return res; |
| 483 | } |
| 484 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 485 | RegLocation GetSrcWide(MIR* mir, int low) { |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 486 | RegLocation res = GetRawSrc(mir, low); |
| 487 | DCHECK(res.wide); |
| 488 | return res; |
| 489 | } |
| 490 | |
| 491 | RegLocation GetBadLoc() { |
| 492 | return bad_loc; |
| 493 | } |
| 494 | |
| 495 | int GetMethodSReg() { |
| 496 | return method_sreg_; |
| 497 | } |
| 498 | |
| 499 | bool MethodIsLeaf() { |
| 500 | return attributes_ & METHOD_IS_LEAF; |
| 501 | } |
| 502 | |
| 503 | RegLocation GetRegLocation(int index) { |
| 504 | DCHECK((index >= 0) && (index > num_ssa_regs_)); |
| 505 | return reg_location_[index]; |
| 506 | } |
| 507 | |
| 508 | RegLocation GetMethodLoc() { |
| 509 | return reg_location_[method_sreg_]; |
| 510 | } |
| 511 | |
buzbee | 479f83c | 2013-07-19 10:58:21 -0700 | [diff] [blame] | 512 | bool IsSpecialCase() { |
| 513 | return special_case_ != kNoHandler; |
| 514 | } |
| 515 | |
| 516 | SpecialCaseHandler GetSpecialCase() { |
| 517 | return special_case_; |
| 518 | } |
| 519 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 520 | void BasicBlockCombine(); |
| 521 | void CodeLayout(); |
| 522 | void DumpCheckStats(); |
| 523 | void PropagateConstants(); |
| 524 | MIR* FindMoveResult(BasicBlock* bb, MIR* mir); |
| 525 | int SRegToVReg(int ssa_reg) const; |
| 526 | void VerifyDataflow(); |
| 527 | void MethodUseCount(); |
| 528 | void SSATransformation(); |
| 529 | void CheckForDominanceFrontier(BasicBlock* dom_bb, const BasicBlock* succ_bb); |
| 530 | void NullCheckElimination(); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 531 | bool SetFp(int index, bool is_fp); |
| 532 | bool SetCore(int index, bool is_core); |
| 533 | bool SetRef(int index, bool is_ref); |
| 534 | bool SetWide(int index, bool is_wide); |
| 535 | bool SetHigh(int index, bool is_high); |
| 536 | void AppendMIR(BasicBlock* bb, MIR* mir); |
| 537 | void PrependMIR(BasicBlock* bb, MIR* mir); |
| 538 | void InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir); |
| 539 | char* GetDalvikDisassembly(const MIR* mir); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 540 | void ReplaceSpecialChars(std::string& str); |
| 541 | std::string GetSSAName(int ssa_reg); |
| 542 | std::string GetSSANameWithConst(int ssa_reg, bool singles_only); |
| 543 | void GetBlockName(BasicBlock* bb, char* name); |
| 544 | const char* GetShortyFromTargetIdx(int); |
| 545 | void DumpMIRGraph(); |
| 546 | CallInfo* NewMemCallInfo(BasicBlock* bb, MIR* mir, InvokeType type, bool is_range); |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 547 | BasicBlock* NewMemBB(BBType block_type, int block_id); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 548 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 549 | /* |
| 550 | * IsDebugBuild sanity check: keep track of the Dex PCs for catch entries so that later on |
| 551 | * we can verify that all catch entries have native PC entries. |
| 552 | */ |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 553 | std::set<uint32_t> catches_; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 554 | |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 555 | // TODO: make these private. |
| 556 | RegLocation* reg_location_; // Map SSA names to location. |
| 557 | GrowableArray<CompilerTemp*> compiler_temps_; |
| 558 | SafeMap<unsigned int, unsigned int> block_id_map_; // Block collapse lookup cache. |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 559 | |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 560 | static const int oat_data_flow_attributes_[kMirOpLast]; |
| 561 | static const char* extended_mir_op_names_[kMirOpLast - kMirOpFirst]; |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame] | 562 | |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 563 | private: |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 564 | int FindCommonParent(int block1, int block2); |
| 565 | void ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1, |
| 566 | const ArenaBitVector* src2); |
| 567 | void HandleLiveInUse(ArenaBitVector* use_v, ArenaBitVector* def_v, |
| 568 | ArenaBitVector* live_in_v, int dalvik_reg_id); |
| 569 | void HandleDef(ArenaBitVector* def_v, int dalvik_reg_id); |
| 570 | void CompilerInitializeSSAConversion(); |
| 571 | bool DoSSAConversion(BasicBlock* bb); |
| 572 | bool InvokeUsesMethodStar(MIR* mir); |
| 573 | int ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction); |
| 574 | bool ContentIsInsn(const uint16_t* code_ptr); |
| 575 | BasicBlock* SplitBlock(unsigned int code_offset, BasicBlock* orig_block, |
Ian Rogers | 71fe267 | 2013-03-19 20:45:02 -0700 | [diff] [blame] | 576 | BasicBlock** immed_pred_block_p); |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 577 | BasicBlock* FindBlock(unsigned int code_offset, bool split, bool create, |
| 578 | BasicBlock** immed_pred_block_p); |
| 579 | void ProcessTryCatchBlocks(); |
| 580 | BasicBlock* ProcessCanBranch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, |
| 581 | int flags, const uint16_t* code_ptr, const uint16_t* code_end); |
| 582 | void ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, int flags); |
| 583 | BasicBlock* ProcessCanThrow(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, |
| 584 | int flags, ArenaBitVector* try_block_addr, const uint16_t* code_ptr, |
| 585 | const uint16_t* code_end); |
| 586 | int AddNewSReg(int v_reg); |
| 587 | void HandleSSAUse(int* uses, int dalvik_reg, int reg_index); |
| 588 | void HandleSSADef(int* defs, int dalvik_reg, int reg_index); |
| 589 | void DataFlowSSAFormat35C(MIR* mir); |
| 590 | void DataFlowSSAFormat3RC(MIR* mir); |
| 591 | bool FindLocalLiveIn(BasicBlock* bb); |
| 592 | void ClearAllVisitedFlags(); |
| 593 | bool CountUses(struct BasicBlock* bb); |
| 594 | bool InferTypeAndSize(BasicBlock* bb); |
| 595 | bool VerifyPredInfo(BasicBlock* bb); |
| 596 | BasicBlock* NeedsVisit(BasicBlock* bb); |
| 597 | BasicBlock* NextUnvisitedSuccessor(BasicBlock* bb); |
| 598 | void MarkPreOrder(BasicBlock* bb); |
| 599 | void RecordDFSOrders(BasicBlock* bb); |
| 600 | void ComputeDFSOrders(); |
| 601 | void ComputeDefBlockMatrix(); |
| 602 | void ComputeDomPostOrderTraversal(BasicBlock* bb); |
| 603 | void ComputeDominators(); |
| 604 | void InsertPhiNodes(); |
| 605 | void DoDFSPreOrderSSARename(BasicBlock* block); |
| 606 | void SetConstant(int32_t ssa_reg, int value); |
| 607 | void SetConstantWide(int ssa_reg, int64_t value); |
| 608 | int GetSSAUseCount(int s_reg); |
| 609 | bool BasicBlockOpt(BasicBlock* bb); |
| 610 | bool EliminateNullChecks(BasicBlock* bb); |
| 611 | void NullCheckEliminationInit(BasicBlock* bb); |
| 612 | bool BuildExtendedBBList(struct BasicBlock* bb); |
| 613 | bool FillDefBlockMatrix(BasicBlock* bb); |
| 614 | void InitializeDominationInfo(BasicBlock* bb); |
| 615 | bool ComputeblockIDom(BasicBlock* bb); |
| 616 | bool ComputeBlockDominators(BasicBlock* bb); |
| 617 | bool SetDominators(BasicBlock* bb); |
| 618 | bool ComputeBlockLiveIns(BasicBlock* bb); |
| 619 | bool InsertPhiNodeOperands(BasicBlock* bb); |
| 620 | bool ComputeDominanceFrontier(BasicBlock* bb); |
| 621 | void DoConstantPropogation(BasicBlock* bb); |
| 622 | void CountChecks(BasicBlock* bb); |
| 623 | bool CombineBlocks(BasicBlock* bb); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 624 | |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 625 | CompilationUnit* const cu_; |
| 626 | GrowableArray<int>* ssa_base_vregs_; |
| 627 | GrowableArray<int>* ssa_subscripts_; |
| 628 | // Map original Dalvik virtual reg i to the current SSA name. |
| 629 | int* vreg_to_ssa_map_; // length == method->registers_size |
| 630 | int* ssa_last_defs_; // length == method->registers_size |
| 631 | ArenaBitVector* is_constant_v_; // length == num_ssa_reg |
| 632 | int* constant_values_; // length == num_ssa_reg |
| 633 | // Use counts of ssa names. |
| 634 | GrowableArray<uint32_t> use_counts_; // Weighted by nesting depth |
| 635 | GrowableArray<uint32_t> raw_use_counts_; // Not weighted |
| 636 | unsigned int num_reachable_blocks_; |
| 637 | GrowableArray<int>* dfs_order_; |
| 638 | GrowableArray<int>* dfs_post_order_; |
| 639 | GrowableArray<int>* dom_post_order_traversal_; |
| 640 | int* i_dom_list_; |
| 641 | ArenaBitVector** def_block_matrix_; // num_dalvik_register x num_blocks. |
| 642 | ArenaBitVector* temp_block_v_; |
| 643 | ArenaBitVector* temp_dalvik_register_v_; |
| 644 | ArenaBitVector* temp_ssa_register_v_; // num_ssa_regs. |
| 645 | static const int kInvalidEntry = -1; |
| 646 | GrowableArray<BasicBlock*> block_list_; |
| 647 | ArenaBitVector* try_block_addr_; |
| 648 | BasicBlock* entry_block_; |
| 649 | BasicBlock* exit_block_; |
| 650 | BasicBlock* cur_block_; |
| 651 | int num_blocks_; |
| 652 | const DexFile::CodeItem* current_code_item_; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame^] | 653 | SafeMap<unsigned int, BasicBlock*> block_map_; // FindBlock lookup cache. |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 654 | std::vector<DexCompilationUnit*> m_units_; // List of methods included in this graph |
| 655 | typedef std::pair<int, int> MIRLocation; // Insert point, (m_unit_ index, offset) |
| 656 | std::vector<MIRLocation> method_stack_; // Include stack |
| 657 | int current_method_; |
| 658 | int current_offset_; |
| 659 | int def_count_; // Used to estimate size of ssa name storage. |
| 660 | int* opcode_count_; // Dex opcode coverage stats. |
| 661 | int num_ssa_regs_; // Number of names following SSA transformation. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame^] | 662 | std::vector<BasicBlock*> extended_basic_blocks_; // Heads of block "traces". |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 663 | int method_sreg_; |
| 664 | unsigned int attributes_; |
| 665 | Checkstats* checkstats_; |
buzbee | 479f83c | 2013-07-19 10:58:21 -0700 | [diff] [blame] | 666 | SpecialCaseHandler special_case_; |
Brian Carlstrom | 6f485c6 | 2013-07-18 15:35:35 -0700 | [diff] [blame] | 667 | ArenaAllocator* arena_; |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 668 | }; |
| 669 | |
| 670 | } // namespace art |
| 671 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 672 | #endif // ART_COMPILER_DEX_MIR_GRAPH_H_ |