blob: 832901084ae7bebd0614f310fffef837eb9649f6 [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
17#ifndef ART_SRC_COMPILER_DEX_MIRGRAPH_H_
18#define ART_SRC_COMPILER_DEX_MIRGRAPH_H_
19
20#include "dex_file.h"
21#include "dex_instruction.h"
22#include "compiler_ir.h"
23
24namespace art {
25
26enum DataFlowAttributePos {
27 kUA = 0,
28 kUB,
29 kUC,
30 kAWide,
31 kBWide,
32 kCWide,
33 kDA,
34 kIsMove,
35 kSetsConst,
36 kFormat35c,
37 kFormat3rc,
38 kNullCheckSrc0, // Null check of uses[0].
39 kNullCheckSrc1, // Null check of uses[1].
40 kNullCheckSrc2, // Null check of uses[2].
41 kNullCheckOut0, // Null check out outgoing arg0.
42 kDstNonNull, // May assume dst is non-null.
43 kRetNonNull, // May assume retval is non-null.
44 kNullTransferSrc0, // Object copy src[0] -> dst.
45 kNullTransferSrcN, // Phi null check state transfer.
46 kRangeCheckSrc1, // Range check of uses[1].
47 kRangeCheckSrc2, // Range check of uses[2].
48 kRangeCheckSrc3, // Range check of uses[3].
49 kFPA,
50 kFPB,
51 kFPC,
52 kCoreA,
53 kCoreB,
54 kCoreC,
55 kRefA,
56 kRefB,
57 kRefC,
58 kUsesMethodStar, // Implicit use of Method*.
59};
60
61#define DF_NOP 0
62#define DF_UA (1 << kUA)
63#define DF_UB (1 << kUB)
64#define DF_UC (1 << kUC)
65#define DF_A_WIDE (1 << kAWide)
66#define DF_B_WIDE (1 << kBWide)
67#define DF_C_WIDE (1 << kCWide)
68#define DF_DA (1 << kDA)
69#define DF_IS_MOVE (1 << kIsMove)
70#define DF_SETS_CONST (1 << kSetsConst)
71#define DF_FORMAT_35C (1 << kFormat35c)
72#define DF_FORMAT_3RC (1 << kFormat3rc)
73#define DF_NULL_CHK_0 (1 << kNullCheckSrc0)
74#define DF_NULL_CHK_1 (1 << kNullCheckSrc1)
75#define DF_NULL_CHK_2 (1 << kNullCheckSrc2)
76#define DF_NULL_CHK_OUT0 (1 << kNullCheckOut0)
77#define DF_NON_NULL_DST (1 << kDstNonNull)
78#define DF_NON_NULL_RET (1 << kRetNonNull)
79#define DF_NULL_TRANSFER_0 (1 << kNullTransferSrc0)
80#define DF_NULL_TRANSFER_N (1 << kNullTransferSrcN)
81#define DF_RANGE_CHK_1 (1 << kRangeCheckSrc1)
82#define DF_RANGE_CHK_2 (1 << kRangeCheckSrc2)
83#define DF_RANGE_CHK_3 (1 << kRangeCheckSrc3)
84#define DF_FP_A (1 << kFPA)
85#define DF_FP_B (1 << kFPB)
86#define DF_FP_C (1 << kFPC)
87#define DF_CORE_A (1 << kCoreA)
88#define DF_CORE_B (1 << kCoreB)
89#define DF_CORE_C (1 << kCoreC)
90#define DF_REF_A (1 << kRefA)
91#define DF_REF_B (1 << kRefB)
92#define DF_REF_C (1 << kRefC)
93#define DF_UMS (1 << kUsesMethodStar)
94
95#define DF_HAS_USES (DF_UA | DF_UB | DF_UC)
96
97#define DF_HAS_DEFS (DF_DA)
98
99#define DF_HAS_NULL_CHKS (DF_NULL_CHK_0 | \
100 DF_NULL_CHK_1 | \
101 DF_NULL_CHK_2 | \
102 DF_NULL_CHK_OUT0)
103
104#define DF_HAS_RANGE_CHKS (DF_RANGE_CHK_1 | \
105 DF_RANGE_CHK_2 | \
106 DF_RANGE_CHK_3)
107
108#define DF_HAS_NR_CHKS (DF_HAS_NULL_CHKS | \
109 DF_HAS_RANGE_CHKS)
110
111#define DF_A_IS_REG (DF_UA | DF_DA)
112#define DF_B_IS_REG (DF_UB)
113#define DF_C_IS_REG (DF_UC)
114#define DF_IS_GETTER_OR_SETTER (DF_IS_GETTER | DF_IS_SETTER)
115#define DF_USES_FP (DF_FP_A | DF_FP_B | DF_FP_C)
116
117extern const int oat_data_flow_attributes[kMirOpLast];
118
119class MIRGraph {
Ian Rogers71fe2672013-03-19 20:45:02 -0700120 public:
121 MIRGraph(CompilationUnit* cu);
122 ~MIRGraph() {}
buzbee311ca162013-02-28 15:56:43 -0800123
Ian Rogers71fe2672013-03-19 20:45:02 -0700124 /*
125 * Parse dex method and add MIR at current insert point. Returns id (which is
126 * actually the index of the method in the m_units_ array).
127 */
128 void InlineMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
129 InvokeType invoke_type, uint32_t class_def_idx,
130 uint32_t method_idx, jobject class_loader, const DexFile& dex_file);
buzbee311ca162013-02-28 15:56:43 -0800131
Ian Rogers71fe2672013-03-19 20:45:02 -0700132 /* Find existing block */
133 BasicBlock* FindBlock(unsigned int code_offset) {
134 return FindBlock(code_offset, false, false, NULL);
135 }
buzbee311ca162013-02-28 15:56:43 -0800136
Ian Rogers71fe2672013-03-19 20:45:02 -0700137 const uint16_t* GetCurrentInsns() const {
138 return current_code_item_->insns_;
139 }
buzbee311ca162013-02-28 15:56:43 -0800140
Ian Rogers71fe2672013-03-19 20:45:02 -0700141 const uint16_t* GetInsns(int m_unit_index) const {
142 return m_units_[m_unit_index]->GetCodeItem()->insns_;
143 }
buzbee311ca162013-02-28 15:56:43 -0800144
Ian Rogers71fe2672013-03-19 20:45:02 -0700145 int GetNumBlocks() const {
146 return num_blocks_;
147 }
buzbee311ca162013-02-28 15:56:43 -0800148
Ian Rogers71fe2672013-03-19 20:45:02 -0700149 ArenaBitVector* GetTryBlockAddr() const {
150 return try_block_addr_;
151 }
buzbee311ca162013-02-28 15:56:43 -0800152
Ian Rogers71fe2672013-03-19 20:45:02 -0700153 BasicBlock* GetEntryBlock() const {
154 return entry_block_;
155 }
buzbee311ca162013-02-28 15:56:43 -0800156
Ian Rogers71fe2672013-03-19 20:45:02 -0700157 BasicBlock* GetExitBlock() const {
158 return exit_block_;
159 }
buzbee311ca162013-02-28 15:56:43 -0800160
Ian Rogers71fe2672013-03-19 20:45:02 -0700161 GrowableListIterator GetBasicBlockIterator() {
162 GrowableListIterator iterator;
163 GrowableListIteratorInit(&block_list_, &iterator);
164 return iterator;
165 }
buzbee311ca162013-02-28 15:56:43 -0800166
Ian Rogers71fe2672013-03-19 20:45:02 -0700167 BasicBlock* GetBasicBlock(int block_id) const {
168 return reinterpret_cast<BasicBlock*>(GrowableListGetElement(&block_list_, block_id));
169 }
buzbee311ca162013-02-28 15:56:43 -0800170
Ian Rogers71fe2672013-03-19 20:45:02 -0700171 size_t GetBasicBlockListCount() const {
172 return block_list_.num_used;
173 }
buzbee311ca162013-02-28 15:56:43 -0800174
Ian Rogers71fe2672013-03-19 20:45:02 -0700175 GrowableList* GetBlockList() {
176 return &block_list_;
177 }
buzbee311ca162013-02-28 15:56:43 -0800178
Ian Rogers71fe2672013-03-19 20:45:02 -0700179 GrowableList* GetDfsOrder() {
180 return &dfs_order_;
181 }
buzbee311ca162013-02-28 15:56:43 -0800182
Ian Rogers71fe2672013-03-19 20:45:02 -0700183 GrowableList* GetDfsPostOrder() {
184 return &dfs_post_order_;
185 }
buzbee311ca162013-02-28 15:56:43 -0800186
Ian Rogers71fe2672013-03-19 20:45:02 -0700187 GrowableList* GetDomPostOrder() {
188 return &dom_post_order_traversal_;
189 }
buzbee311ca162013-02-28 15:56:43 -0800190
Ian Rogers71fe2672013-03-19 20:45:02 -0700191 GrowableList* GetSSASubscripts() {
192 return ssa_subscripts_;
193 }
buzbee311ca162013-02-28 15:56:43 -0800194
Ian Rogers71fe2672013-03-19 20:45:02 -0700195 int GetDefCount() const {
196 return def_count_;
197 }
buzbee311ca162013-02-28 15:56:43 -0800198
Ian Rogers71fe2672013-03-19 20:45:02 -0700199 void EnableOpcodeCounting() {
200 opcode_count_ = static_cast<int*>(NewMem(cu_, kNumPackedOpcodes * sizeof(int), true,
201 kAllocMisc));
202 }
buzbee311ca162013-02-28 15:56:43 -0800203
Ian Rogers71fe2672013-03-19 20:45:02 -0700204 void ShowOpcodeStats();
buzbee311ca162013-02-28 15:56:43 -0800205
Ian Rogers71fe2672013-03-19 20:45:02 -0700206 DexCompilationUnit* GetCurrentDexCompilationUnit() const {
207 return m_units_[current_method_];
208 }
buzbee311ca162013-02-28 15:56:43 -0800209
Ian Rogers71fe2672013-03-19 20:45:02 -0700210 void DumpCFG(const char* dir_prefix, bool all_blocks);
buzbee311ca162013-02-28 15:56:43 -0800211
Ian Rogers71fe2672013-03-19 20:45:02 -0700212 void BuildRegLocations();
buzbee311ca162013-02-28 15:56:43 -0800213
Ian Rogers71fe2672013-03-19 20:45:02 -0700214 void DumpRegLocTable(RegLocation* table, int count);
buzbee311ca162013-02-28 15:56:43 -0800215
Ian Rogers71fe2672013-03-19 20:45:02 -0700216 int ComputeFrameSize();
buzbee311ca162013-02-28 15:56:43 -0800217
Ian Rogers71fe2672013-03-19 20:45:02 -0700218 void BasicBlockOptimization();
buzbee311ca162013-02-28 15:56:43 -0800219
Ian Rogers71fe2672013-03-19 20:45:02 -0700220 bool IsConst(int32_t s_reg) const {
221 return (IsBitSet(is_constant_v_, s_reg));
222 }
buzbee311ca162013-02-28 15:56:43 -0800223
Ian Rogers71fe2672013-03-19 20:45:02 -0700224 bool IsConst(RegLocation loc) const {
225 return (IsConst(loc.orig_sreg));
226 }
buzbee311ca162013-02-28 15:56:43 -0800227
Ian Rogers71fe2672013-03-19 20:45:02 -0700228 int32_t ConstantValue(RegLocation loc) const {
229 DCHECK(IsConst(loc));
230 return constant_values_[loc.orig_sreg];
231 }
buzbee311ca162013-02-28 15:56:43 -0800232
Ian Rogers71fe2672013-03-19 20:45:02 -0700233 int32_t ConstantValue(int32_t s_reg) const {
234 DCHECK(IsConst(s_reg));
235 return constant_values_[s_reg];
236 }
buzbee311ca162013-02-28 15:56:43 -0800237
Ian Rogers71fe2672013-03-19 20:45:02 -0700238 int64_t ConstantValueWide(RegLocation loc) const {
239 DCHECK(IsConst(loc));
240 return (static_cast<int64_t>(constant_values_[loc.orig_sreg + 1]) << 32) |
241 Low32Bits(static_cast<int64_t>(constant_values_[loc.orig_sreg]));
242 }
buzbee311ca162013-02-28 15:56:43 -0800243
Ian Rogers71fe2672013-03-19 20:45:02 -0700244 bool IsConstantNullRef(RegLocation loc) const {
245 return loc.ref && loc.is_const && (ConstantValue(loc) == 0);
246 }
buzbee311ca162013-02-28 15:56:43 -0800247
Ian Rogers71fe2672013-03-19 20:45:02 -0700248 int GetNumSSARegs() const {
249 return num_ssa_regs_;
250 }
buzbee311ca162013-02-28 15:56:43 -0800251
Ian Rogers71fe2672013-03-19 20:45:02 -0700252 void SetNumSSARegs(int new_num) {
253 num_ssa_regs_ = new_num;
254 }
buzbee311ca162013-02-28 15:56:43 -0800255
Ian Rogers71fe2672013-03-19 20:45:02 -0700256 int GetNumReachableBlocks() const {
257 return num_reachable_blocks_;
258 }
buzbee311ca162013-02-28 15:56:43 -0800259
Ian Rogers71fe2672013-03-19 20:45:02 -0700260 int GetUseCount(int vreg) const {
261 return GrowableListGetElement(&use_counts_, vreg);
262 }
buzbee311ca162013-02-28 15:56:43 -0800263
Ian Rogers71fe2672013-03-19 20:45:02 -0700264 int GetRawUseCount(int vreg) const {
265 return GrowableListGetElement(&raw_use_counts_, vreg);
266 }
buzbee311ca162013-02-28 15:56:43 -0800267
Ian Rogers71fe2672013-03-19 20:45:02 -0700268 int GetSSASubscript(int ssa_reg) const {
269 return GrowableListGetElement(ssa_subscripts_, ssa_reg);
270 }
buzbee311ca162013-02-28 15:56:43 -0800271
Ian Rogers71fe2672013-03-19 20:45:02 -0700272 const char* GetSSAString(int ssa_reg) const {
273 return GET_ELEM_N(ssa_strings_, char*, ssa_reg);
274 }
buzbee311ca162013-02-28 15:56:43 -0800275
Ian Rogers71fe2672013-03-19 20:45:02 -0700276 void BasicBlockCombine();
277 void CodeLayout();
278 void DumpCheckStats();
279 void PropagateConstants();
280 MIR* FindMoveResult(BasicBlock* bb, MIR* mir);
281 int SRegToVReg(int ssa_reg) const;
282 void VerifyDataflow();
283 void MethodUseCount();
284 void SSATransformation();
285 void CheckForDominanceFrontier(BasicBlock* dom_bb, const BasicBlock* succ_bb);
286 void NullCheckElimination();
buzbee311ca162013-02-28 15:56:43 -0800287
Ian Rogers71fe2672013-03-19 20:45:02 -0700288 /*
289 * IsDebugBuild sanity check: keep track of the Dex PCs for catch entries so that later on
290 * we can verify that all catch entries have native PC entries.
291 */
292 std::set<uint32_t> catches_;
buzbee311ca162013-02-28 15:56:43 -0800293
Ian Rogers71fe2672013-03-19 20:45:02 -0700294 private:
buzbee311ca162013-02-28 15:56:43 -0800295
Ian Rogers71fe2672013-03-19 20:45:02 -0700296 int FindCommonParent(int block1, int block2);
297 void ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
298 const ArenaBitVector* src2);
299 void HandleLiveInUse(ArenaBitVector* use_v, ArenaBitVector* def_v,
300 ArenaBitVector* live_in_v, int dalvik_reg_id);
301 void HandleDef(ArenaBitVector* def_v, int dalvik_reg_id);
302 void CompilerInitializeSSAConversion();
303 bool DoSSAConversion(BasicBlock* bb);
304 bool InvokeUsesMethodStar(MIR* mir);
305 int ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction);
306 bool ContentIsInsn(const uint16_t* code_ptr);
307 BasicBlock* SplitBlock(unsigned int code_offset, BasicBlock* orig_block,
buzbee311ca162013-02-28 15:56:43 -0800308 BasicBlock** immed_pred_block_p);
Ian Rogers71fe2672013-03-19 20:45:02 -0700309 BasicBlock* FindBlock(unsigned int code_offset, bool split, bool create,
310 BasicBlock** immed_pred_block_p);
311 void ProcessTryCatchBlocks();
312 BasicBlock* ProcessCanBranch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
313 int flags, const uint16_t* code_ptr, const uint16_t* code_end);
314 void ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, int flags);
315 BasicBlock* ProcessCanThrow(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
316 int flags, ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
317 const uint16_t* code_end);
318 int AddNewSReg(int v_reg);
319 void HandleSSAUse(int* uses, int dalvik_reg, int reg_index);
320 void HandleSSADef(int* defs, int dalvik_reg, int reg_index);
321 void DataFlowSSAFormat35C(MIR* mir);
322 void DataFlowSSAFormat3RC(MIR* mir);
323 bool FindLocalLiveIn(BasicBlock* bb);
324 bool ClearVisitedFlag(struct BasicBlock* bb);
325 bool CountUses(struct BasicBlock* bb);
326 bool InferTypeAndSize(BasicBlock* bb);
327 bool VerifyPredInfo(BasicBlock* bb);
328 BasicBlock* NeedsVisit(BasicBlock* bb);
329 BasicBlock* NextUnvisitedSuccessor(BasicBlock* bb);
330 void MarkPreOrder(BasicBlock* bb);
331 void RecordDFSOrders(BasicBlock* bb);
332 void ComputeDFSOrders();
333 void ComputeDefBlockMatrix();
334 void ComputeDomPostOrderTraversal(BasicBlock* bb);
335 void ComputeDominators();
336 void InsertPhiNodes();
337 void DoDFSPreOrderSSARename(BasicBlock* block);
338 void SetConstant(int32_t ssa_reg, int value);
339 void SetConstantWide(int ssa_reg, int64_t value);
340 int GetSSAUseCount(int s_reg);
341 bool BasicBlockOpt(BasicBlock* bb);
342 bool EliminateNullChecks(BasicBlock* bb);
343 bool NullCheckEliminationInit(BasicBlock* bb);
344 bool BuildExtendedBBList(struct BasicBlock* bb);
345 bool FillDefBlockMatrix(BasicBlock* bb);
346 bool InitializeDominationInfo(BasicBlock* bb);
347 bool ComputeblockIDom(BasicBlock* bb);
348 bool ComputeBlockDominators(BasicBlock* bb);
349 bool SetDominators(BasicBlock* bb);
350 bool ComputeBlockLiveIns(BasicBlock* bb);
351 bool InsertPhiNodeOperands(BasicBlock* bb);
352 bool ComputeDominanceFrontier(BasicBlock* bb);
353 bool DoConstantPropogation(BasicBlock* bb);
354 bool CountChecks(BasicBlock* bb);
355 bool CombineBlocks(BasicBlock* bb);
buzbee311ca162013-02-28 15:56:43 -0800356
Ian Rogers71fe2672013-03-19 20:45:02 -0700357 CompilationUnit* const cu_;
358 GrowableList* ssa_base_vregs_;
359 GrowableList* ssa_subscripts_;
360 GrowableList* ssa_strings_;
361 // Map original Dalvik virtual reg i to the current SSA name.
362 int* vreg_to_ssa_map_; // length == method->registers_size
363 int* ssa_last_defs_; // length == method->registers_size
364 ArenaBitVector* is_constant_v_; // length == num_ssa_reg
365 int* constant_values_; // length == num_ssa_reg
366 // Use counts of ssa names.
367 GrowableList use_counts_; // Weighted by nesting depth
368 GrowableList raw_use_counts_; // Not weighted
369 int num_reachable_blocks_;
370 GrowableList dfs_order_;
371 GrowableList dfs_post_order_;
372 GrowableList dom_post_order_traversal_;
373 int* i_dom_list_;
374 ArenaBitVector** def_block_matrix_; // num_dalvik_register x num_blocks.
375 ArenaBitVector* temp_block_v_;
376 ArenaBitVector* temp_dalvik_register_v_;
377 ArenaBitVector* temp_ssa_register_v_; // num_ssa_regs.
378 static const int kInvalidEntry = -1;
379 GrowableList block_list_;
380 ArenaBitVector* try_block_addr_;
381 BasicBlock* entry_block_;
382 BasicBlock* exit_block_;
383 BasicBlock* cur_block_;
384 int num_blocks_;
385 const DexFile::CodeItem* current_code_item_;
386 SafeMap<unsigned int, BasicBlock*> block_map_; // FindBlock lookup cache.
387 std::vector<DexCompilationUnit*> m_units_; // List of methods included in this graph
388 typedef std::pair<int, int> MIRLocation; // Insert point, (m_unit_ index, offset)
389 std::vector<MIRLocation> method_stack_; // Include stack
390 int current_method_;
391 int current_offset_;
392 int def_count_; // Used to estimate size of ssa name storage.
393 int* opcode_count_; // Dex opcode coverage stats.
394 int num_ssa_regs_; // Number of names following SSA transformation.
395 std::vector<BasicBlock*> extended_basic_blocks_; // Heads of block "traces".
buzbee311ca162013-02-28 15:56:43 -0800396};
397
398} // namespace art
399
400#endif // ART_SRC_COMPILER_DEX_MIRGRAPH_H_