blob: 2f91787b748bd9c396bbee1ff65e0f1dd420e8d9 [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 {
120 public:
121 MIRGraph(CompilationUnit* cu);
122 ~MIRGraph() {}
123
124 /*
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);
131
132 /* Find existing block */
133 BasicBlock* FindBlock(unsigned int code_offset)
134 {
135 return FindBlock(code_offset, false, false, NULL);
136 }
137
138 const uint16_t* GetCurrentInsns()
139 {
140 return current_code_item_->insns_;
141 }
142
143 const uint16_t* GetInsns(int m_unit_index)
144 {
145 return m_units_[m_unit_index]->GetCodeItem()->insns_;
146 }
147
148 int GetNumBlocks()
149 {
150 return num_blocks_;
151 }
152
153 ArenaBitVector* GetTryBlockAddr()
154 {
155 return try_block_addr_;
156 }
157
158 BasicBlock* GetEntryBlock()
159 {
160 return entry_block_;
161 }
162
163 BasicBlock* GetExitBlock()
164 {
165 return exit_block_;
166 }
167
168 GrowableListIterator GetBasicBlockIterator()
169 {
170 GrowableListIterator iterator;
171 GrowableListIteratorInit(&block_list_, &iterator);
172 return iterator;
173 }
174
175 BasicBlock* GetBasicBlock(int block_id)
176 {
177 return reinterpret_cast<BasicBlock*>(GrowableListGetElement(&block_list_, block_id));
178 }
179
180 size_t GetBasicBlockListCount()
181 {
182 return block_list_.num_used;
183 }
184
185 GrowableList* GetBlockList()
186 {
187 return &block_list_;
188 }
189
190 GrowableList* GetDfsOrder()
191 {
192 return &dfs_order_;
193 }
194
195 GrowableList* GetDfsPostOrder()
196 {
197 return &dfs_post_order_;
198 }
199
200 GrowableList* GetDomPostOrder()
201 {
202 return &dom_post_order_traversal_;
203 }
204
205 GrowableList* GetSSASubscripts()
206 {
207 return ssa_subscripts_;
208 }
209
210 int GetDefCount()
211 {
212 return def_count_;
213 }
214
215 void EnableOpcodeCounting()
216 {
217 opcode_count_ = static_cast<int*>(NewMem(cu_, kNumPackedOpcodes * sizeof(int), true,
218 kAllocMisc));
219 }
220
221 void ShowOpcodeStats();
222
223 DexCompilationUnit* GetCurrentDexCompilationUnit()
224 {
225 return m_units_[current_method_];
226 }
227
228 void DumpCFG(const char* dir_prefix, bool all_blocks);
229
230 void BuildRegLocations();
231
232 void DumpRegLocTable(RegLocation* table, int count);
233
234 int ComputeFrameSize();
235
236 void BasicBlockOptimization();
237
238 bool IsConst(int32_t s_reg)
239 {
240 return (IsBitSet(is_constant_v_, s_reg));
241 }
242
243 bool IsConst(RegLocation loc)
244 {
245 return (IsConst(loc.orig_sreg));
246 }
247
248 int32_t ConstantValue(RegLocation loc)
249 {
250 DCHECK(IsConst(loc));
251 return constant_values_[loc.orig_sreg];
252 }
253
254 int32_t ConstantValue(int32_t s_reg)
255 {
256 DCHECK(IsConst(s_reg));
257 return constant_values_[s_reg];
258 }
259
260 int64_t ConstantValueWide(RegLocation loc)
261 {
262 DCHECK(IsConst(loc));
263 return (static_cast<int64_t>(constant_values_[loc.orig_sreg + 1]) << 32) |
264 Low32Bits(static_cast<int64_t>(constant_values_[loc.orig_sreg]));
265 }
266
267 bool IsConstantNullRef(RegLocation loc)
268 {
269 return loc.ref && loc.is_const && (ConstantValue(loc) == 0);
270 }
271
272 int GetNumSSARegs()
273 {
274 return num_ssa_regs_;
275 }
276
277 void SetNumSSARegs(int new_num)
278 {
279 num_ssa_regs_ = new_num;
280 }
281
282 int GetNumReachableBlocks()
283 {
284 return num_reachable_blocks_;
285 }
286
287 int GetUseCount(int vreg)
288 {
289 return GrowableListGetElement(&use_counts_, vreg);
290 }
291
292 int GetRawUseCount(int vreg)
293 {
294 return GrowableListGetElement(&raw_use_counts_, vreg);
295 }
296
297 int GetSSASubscript(int ssa_reg)
298 {
299 return GrowableListGetElement(ssa_subscripts_, ssa_reg);
300 }
301
302 const char* GetSSAString(int ssa_reg)
303 {
304 return GET_ELEM_N(ssa_strings_, char*, ssa_reg);
305 }
306
307 void BasicBlockCombine();
308 void CodeLayout();
309 void DumpCheckStats();
310 void PropagateConstants();
311 MIR* FindMoveResult(BasicBlock* bb, MIR* mir);
312 int SRegToVReg(int ssa_reg);
313 void VerifyDataflow();
314 void MethodUseCount();
315 void SSATransformation();
316 void CheckForDominanceFrontier(BasicBlock* dom_bb, const BasicBlock* succ_bb);
317 void NullCheckElimination();
318
319 /*
320 * IsDebugBuild sanity check: keep track of the Dex PCs for catch entries so that later on
321 * we can verify that all catch entries have native PC entries.
322 */
323 std::set<uint32_t> catches_;
324
325 private:
326
327 int FindCommonParent(int block1, int block2);
328 void ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
329 const ArenaBitVector* src2);
330 void HandleLiveInUse(ArenaBitVector* use_v, ArenaBitVector* def_v,
331 ArenaBitVector* live_in_v, int dalvik_reg_id);
332 void HandleDef(ArenaBitVector* def_v, int dalvik_reg_id);
333 void CompilerInitializeSSAConversion();
334 bool DoSSAConversion(BasicBlock* bb);
335 bool InvokeUsesMethodStar(MIR* mir);
336 int ParseInsn(const uint16_t* code_ptr, DecodedInstruction* decoded_instruction);
337 bool ContentIsInsn(const uint16_t* code_ptr);
338 BasicBlock* SplitBlock(unsigned int code_offset, BasicBlock* orig_block,
339 BasicBlock** immed_pred_block_p);
340 BasicBlock* FindBlock(unsigned int code_offset, bool split, bool create,
341 BasicBlock** immed_pred_block_p);
342 void ProcessTryCatchBlocks();
343 BasicBlock* ProcessCanBranch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
344 int flags, const uint16_t* code_ptr, const uint16_t* code_end);
345 void ProcessCanSwitch(BasicBlock* cur_block, MIR* insn, int cur_offset, int width, int flags);
346 BasicBlock* ProcessCanThrow(BasicBlock* cur_block, MIR* insn, int cur_offset, int width,
347 int flags, ArenaBitVector* try_block_addr, const uint16_t* code_ptr,
348 const uint16_t* code_end);
349 int AddNewSReg(int v_reg);
350 void HandleSSAUse(int* uses, int dalvik_reg, int reg_index);
351 void HandleSSADef(int* defs, int dalvik_reg, int reg_index);
352 void DataFlowSSAFormat35C(MIR* mir);
353 void DataFlowSSAFormat3RC(MIR* mir);
354 bool FindLocalLiveIn(BasicBlock* bb);
355 bool ClearVisitedFlag(struct BasicBlock* bb);
356 bool CountUses(struct BasicBlock* bb);
357 bool InferTypeAndSize(BasicBlock* bb);
358 bool VerifyPredInfo(BasicBlock* bb);
359 BasicBlock* NeedsVisit(BasicBlock* bb);
360 BasicBlock* NextUnvisitedSuccessor(BasicBlock* bb);
361 void MarkPreOrder(BasicBlock* bb);
362 void RecordDFSOrders(BasicBlock* bb);
363 void ComputeDFSOrders();
364 void ComputeDefBlockMatrix();
365 void ComputeDomPostOrderTraversal(BasicBlock* bb);
366 void ComputeDominators();
367 void InsertPhiNodes();
368 void DoDFSPreOrderSSARename(BasicBlock* block);
369 void SetConstant(int32_t ssa_reg, int value);
370 void SetConstantWide(int ssa_reg, int64_t value);
371 int GetSSAUseCount(int s_reg);
372 bool BasicBlockOpt(BasicBlock* bb);
373 bool EliminateNullChecks(BasicBlock* bb);
374 bool NullCheckEliminationInit(BasicBlock* bb);
375 bool BuildExtendedBBList(struct BasicBlock* bb);
376 bool FillDefBlockMatrix(BasicBlock* bb);
377 bool InitializeDominationInfo(BasicBlock* bb);
378 bool ComputeblockIDom(BasicBlock* bb);
379 bool ComputeBlockDominators(BasicBlock* bb);
380 bool SetDominators(BasicBlock* bb);
381 bool ComputeBlockLiveIns(BasicBlock* bb);
382 bool InsertPhiNodeOperands(BasicBlock* bb);
383 bool ComputeDominanceFrontier(BasicBlock* bb);
384 bool DoConstantPropogation(BasicBlock* bb);
385 bool CountChecks(BasicBlock* bb);
386 bool CombineBlocks(BasicBlock* bb);
387
388 CompilationUnit* cu_;
389 GrowableList* ssa_base_vregs_;
390 GrowableList* ssa_subscripts_;
391 GrowableList* ssa_strings_;
392 // Map original Dalvik virtual reg i to the current SSA name.
393 int* vreg_to_ssa_map_; // length == method->registers_size
394 int* ssa_last_defs_; // length == method->registers_size
395 ArenaBitVector* is_constant_v_; // length == num_ssa_reg
396 int* constant_values_; // length == num_ssa_reg
397 // Use counts of ssa names.
398 GrowableList use_counts_; // Weighted by nesting depth
399 GrowableList raw_use_counts_; // Not weighted
400 int num_reachable_blocks_;
401 GrowableList dfs_order_;
402 GrowableList dfs_post_order_;
403 GrowableList dom_post_order_traversal_;
404 int* i_dom_list_;
405 ArenaBitVector** def_block_matrix_; // num_dalvik_register x num_blocks.
406 ArenaBitVector* temp_block_v_;
407 ArenaBitVector* temp_dalvik_register_v_;
408 ArenaBitVector* temp_ssa_register_v_; // num_ssa_regs.
409 static const int kInvalidEntry = -1;
410 GrowableList block_list_;
411 ArenaBitVector* try_block_addr_;
412 BasicBlock* entry_block_;
413 BasicBlock* exit_block_;
414 BasicBlock* cur_block_;
415 int num_blocks_;
416 const DexFile::CodeItem* current_code_item_;
417 SafeMap<unsigned int, BasicBlock*> block_map_; // FindBlock lookup cache.
418 std::vector<DexCompilationUnit*> m_units_; // List of methods included in this graph
419 typedef std::pair<int, int> MIRLocation; // Insert point, (m_unit_ index, offset)
420 std::vector<MIRLocation> method_stack_; // Include stack
421 int current_method_;
422 int current_offset_;
423 int def_count_; // Used to estimate size of ssa name storage.
424 int* opcode_count_; // Dex opcode coverage stats.
425 int num_ssa_regs_; // Number of names following SSA transformation.
426 std::vector<BasicBlock*> extended_basic_blocks_; // Heads of block "traces".
427};
428
429} // namespace art
430
431#endif // ART_SRC_COMPILER_DEX_MIRGRAPH_H_