blob: b2dcd44ededff49e1bafc2fcf210f3959dd0aa7f [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 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
buzbee395116c2013-02-27 14:30:25 -080017#ifndef ART_SRC_COMPILER_DEX_COMPILER_UTILITY_H_
18#define ART_SRC_COMPILER_DEX_COMPILER_UTILITY_H_
buzbee67bf8852011-08-17 17:51:35 -070019
buzbeeeaf09bc2012-11-15 14:51:41 -080020#include <stdint.h>
21#include <stddef.h>
buzbee02031b12012-11-23 09:41:35 -080022#include "compiler_enums.h"
buzbee67bf8852011-08-17 17:51:35 -070023
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
buzbeeeaf09bc2012-11-15 14:51:41 -080026struct CompilationUnit;
27
buzbee02031b12012-11-23 09:41:35 -080028// Each arena page has some overhead, so take a few bytes off.
buzbeeba938cb2012-02-03 14:47:55 -080029#define ARENA_DEFAULT_SIZE ((2 * 1024 * 1024) - 256)
buzbee67bf8852011-08-17 17:51:35 -070030
buzbee02031b12012-11-23 09:41:35 -080031// Type of allocation for memory tuning.
buzbeefa57c472012-11-21 12:06:18 -080032enum oat_alloc_kind {
buzbeeeaf09bc2012-11-15 14:51:41 -080033 kAllocMisc,
34 kAllocBB,
35 kAllocLIR,
36 kAllocMIR,
37 kAllocDFInfo,
38 kAllocGrowableList,
39 kAllocGrowableBitMap,
40 kAllocDalvikToSSAMap,
41 kAllocDebugInfo,
42 kAllocSuccessor,
43 kAllocRegAlloc,
44 kAllocData,
45 kAllocPredecessors,
46 kNumAllocKinds
47};
48
buzbee02031b12012-11-23 09:41:35 -080049// Type of growable list for memory tuning.
buzbeefa57c472012-11-21 12:06:18 -080050enum oat_list_kind {
buzbeeeaf09bc2012-11-15 14:51:41 -080051 kListMisc = 0,
52 kListBlockList,
53 kListSSAtoDalvikMap,
54 kListDfsOrder,
55 kListDfsPostOrder,
56 kListDomPostOrderTraversal,
57 kListThrowLaunchPads,
58 kListSuspendLaunchPads,
59 kListSwitchTables,
60 kListFillArrayData,
61 kListSuccessorBlocks,
62 kListPredecessors,
63 kNumListKinds
64};
65
buzbee02031b12012-11-23 09:41:35 -080066// Type of growable bitmap for memory tuning.
buzbeefa57c472012-11-21 12:06:18 -080067enum oat_bit_map_kind {
buzbeeeaf09bc2012-11-15 14:51:41 -080068 kBitMapMisc = 0,
69 kBitMapUse,
70 kBitMapDef,
71 kBitMapLiveIn,
72 kBitMapBMatrix,
73 kBitMapDominators,
74 kBitMapIDominated,
75 kBitMapDomFrontier,
76 kBitMapPhi,
77 kBitMapTmpBlocks,
78 kBitMapInputBlocks,
79 kBitMapRegisterV,
80 kBitMapTempSSARegisterV,
81 kBitMapNullCheck,
82 kBitMapTmpBlockV,
83 kBitMapPredecessors,
84 kNumBitMapKinds
85};
86
buzbee67bf8852011-08-17 17:51:35 -070087
buzbee02031b12012-11-23 09:41:35 -080088// Uncomment to collect memory usage statistics.
buzbee5abfa3e2012-01-31 17:01:43 -080089//#define WITH_MEMSTATS
90
Elliott Hughes719ace42012-03-09 18:06:03 -080091struct ArenaMemBlock {
buzbeefa57c472012-11-21 12:06:18 -080092 size_t block_size;
93 size_t bytes_allocated;
Bill Buzbeea114add2012-05-03 15:00:40 -070094 ArenaMemBlock *next;
95 char ptr[0];
Elliott Hughes719ace42012-03-09 18:06:03 -080096};
buzbee67bf8852011-08-17 17:51:35 -070097
Elliott Hughes719ace42012-03-09 18:06:03 -080098struct GrowableList {
buzbeefa57c472012-11-21 12:06:18 -080099 GrowableList() : num_allocated(0), num_used(0), elem_list(NULL) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700100 }
101
buzbeefa57c472012-11-21 12:06:18 -0800102 size_t num_allocated;
103 size_t num_used;
104 uintptr_t* elem_list;
buzbee5abfa3e2012-01-31 17:01:43 -0800105#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -0800106 oat_list_kind kind;
buzbee5abfa3e2012-01-31 17:01:43 -0800107#endif
Elliott Hughes719ace42012-03-09 18:06:03 -0800108};
buzbee67bf8852011-08-17 17:51:35 -0700109
Elliott Hughes719ace42012-03-09 18:06:03 -0800110struct GrowableListIterator {
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 GrowableList* list;
112 size_t idx;
113 size_t size;
Elliott Hughes719ace42012-03-09 18:06:03 -0800114};
buzbee67bf8852011-08-17 17:51:35 -0700115
116/*
117 * Expanding bitmap, used for tracking resources. Bits are numbered starting
buzbee02031b12012-11-23 09:41:35 -0800118 * from zero. All operations on a BitVector are unsynchronized.
buzbee67bf8852011-08-17 17:51:35 -0700119 */
120struct ArenaBitVector {
buzbee02031b12012-11-23 09:41:35 -0800121 bool expandable; // expand bitmap if we run out?
122 uint32_t storage_size; // current size, in 32-bit words.
buzbeeeaf09bc2012-11-15 14:51:41 -0800123 uint32_t* storage;
buzbee5abfa3e2012-01-31 17:01:43 -0800124#ifdef WITH_MEMSTATS
buzbee02031b12012-11-23 09:41:35 -0800125 oat_bit_map_kind kind; // for memory use tuning.
buzbee5abfa3e2012-01-31 17:01:43 -0800126#endif
buzbee67bf8852011-08-17 17:51:35 -0700127};
128
buzbee02031b12012-11-23 09:41:35 -0800129// Handy iterator to walk through the bit positions set to 1.
buzbee67bf8852011-08-17 17:51:35 -0700130struct ArenaBitVectorIterator {
buzbeefa57c472012-11-21 12:06:18 -0800131 ArenaBitVector* p_bits;
buzbeeeaf09bc2012-11-15 14:51:41 -0800132 uint32_t idx;
buzbeefa57c472012-11-21 12:06:18 -0800133 uint32_t bit_size;
buzbee67bf8852011-08-17 17:51:35 -0700134};
135
buzbeefa57c472012-11-21 12:06:18 -0800136#define GET_ELEM_N(LIST, TYPE, N) ((reinterpret_cast<TYPE*>(LIST->elem_list)[N]))
buzbee67bf8852011-08-17 17:51:35 -0700137
138#define BLOCK_NAME_LEN 80
139
buzbee02031b12012-11-23 09:41:35 -0800140// Forward declarations
buzbee67bf8852011-08-17 17:51:35 -0700141struct BasicBlock;
142struct CompilationUnit;
buzbee02031b12012-11-23 09:41:35 -0800143enum BBType;
buzbee67bf8852011-08-17 17:51:35 -0700144
buzbee1fd33462013-03-25 13:40:45 -0700145// Allocate the initial memory block for arena-based allocation.
146bool HeapInit(CompilationUnit* cu);
147void* NewMem(CompilationUnit* cu, size_t size, bool zero, oat_alloc_kind kind);
148BasicBlock* NewMemBB(CompilationUnit* cu, BBType block_type, int block_id);
149void ArenaReset(CompilationUnit *cu);
buzbeefa57c472012-11-21 12:06:18 -0800150void CompilerInitGrowableList(CompilationUnit* cu, GrowableList* g_list,
buzbee02031b12012-11-23 09:41:35 -0800151 size_t init_length, oat_list_kind kind = kListMisc);
buzbee311ca162013-02-28 15:56:43 -0800152void ReallocGrowableList(CompilationUnit* cu, GrowableList* g_list, size_t new_length);
buzbee02031b12012-11-23 09:41:35 -0800153void InsertGrowableList(CompilationUnit* cu, GrowableList* g_list, uintptr_t elem);
buzbeefa57c472012-11-21 12:06:18 -0800154void DeleteGrowableList(GrowableList* g_list, uintptr_t elem);
buzbee02031b12012-11-23 09:41:35 -0800155void GrowableListIteratorInit(GrowableList* g_list, GrowableListIterator* iterator);
buzbee52a77fc2012-11-20 19:50:46 -0800156uintptr_t GrowableListIteratorNext(GrowableListIterator* iterator);
buzbeefa57c472012-11-21 12:06:18 -0800157uintptr_t GrowableListGetElement(const GrowableList* g_list, size_t idx);
buzbee02031b12012-11-23 09:41:35 -0800158ArenaBitVector* AllocBitVector(CompilationUnit* cu, unsigned int start_bits, bool expandable,
159 oat_bit_map_kind = kBitMapMisc);
160void BitVectorIteratorInit(ArenaBitVector* p_bits, ArenaBitVectorIterator* iterator);
buzbee52a77fc2012-11-20 19:50:46 -0800161int BitVectorIteratorNext(ArenaBitVectorIterator* iterator);
buzbeefa57c472012-11-21 12:06:18 -0800162bool SetBit(CompilationUnit *cu, ArenaBitVector* p_bits, unsigned int num);
163bool ClearBit(ArenaBitVector* p_bits, unsigned int num);
164void MarkAllBits(ArenaBitVector* p_bits, bool set);
buzbee52a77fc2012-11-20 19:50:46 -0800165void DebugBitVector(char* msg, const ArenaBitVector* bv, int length);
buzbeefa57c472012-11-21 12:06:18 -0800166bool IsBitSet(const ArenaBitVector* p_bits, unsigned int num);
167void ClearAllBits(ArenaBitVector* p_bits);
168void SetInitialBits(ArenaBitVector* p_bits, unsigned int num_bits);
buzbee52a77fc2012-11-20 19:50:46 -0800169void CopyBitVector(ArenaBitVector* dest, const ArenaBitVector* src);
170bool IntersectBitVectors(ArenaBitVector* dest, const ArenaBitVector* src1,
buzbee02031b12012-11-23 09:41:35 -0800171 const ArenaBitVector* src2);
172bool UnifyBitVetors(ArenaBitVector* dest, const ArenaBitVector* src1, const ArenaBitVector* src2);
173bool CompareBitVectors(const ArenaBitVector* src1, const ArenaBitVector* src2);
buzbee52a77fc2012-11-20 19:50:46 -0800174bool TestBitVectors(const ArenaBitVector* src1, const ArenaBitVector* src2);
buzbeefa57c472012-11-21 12:06:18 -0800175int CountSetBits(const ArenaBitVector* p_bits);
buzbee02031b12012-11-23 09:41:35 -0800176void DumpBlockBitVector(const GrowableList* blocks, char* msg, const ArenaBitVector* bv,
177 int length);
buzbeefa57c472012-11-21 12:06:18 -0800178void DumpMemStats(CompilationUnit* cu);
buzbee1fd33462013-03-25 13:40:45 -0700179
buzbee311ca162013-02-28 15:56:43 -0800180void ReplaceSpecialChars(std::string& str);
buzbee311ca162013-02-28 15:56:43 -0800181std::string GetSSAName(const CompilationUnit* cu, int ssa_reg);
182std::string GetSSANameWithConst(const CompilationUnit* cu, int ssa_reg, bool singles_only);
buzbee1fd33462013-03-25 13:40:45 -0700183void GetBlockName(BasicBlock* bb, char* name);
184const char* GetShortyFromTargetIdx(CompilationUnit*, int);
buzbee67bf8852011-08-17 17:51:35 -0700185
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800186} // namespace art
187
buzbee395116c2013-02-27 14:30:25 -0800188#endif // ART_SRC_COMPILER_DEX_COMPILER_UTILITY_H_