blob: 027204b4b9dca7d09fd1018b746232bff77cdfac [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
17#ifndef ART_SRC_COMPILER_COMPILER_UTILITY_H_
18#define ART_SRC_COMPILER_COMPILER_UTILITY_H_
19
buzbeeeaf09bc2012-11-15 14:51:41 -080020#include <stdint.h>
21#include <stddef.h>
buzbee67bf8852011-08-17 17:51:35 -070022
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080023namespace art {
24
buzbeeeaf09bc2012-11-15 14:51:41 -080025struct CompilationUnit;
26
buzbee67bc2362011-10-11 18:08:40 -070027/* Each arena page has some overhead, so take a few bytes off */
buzbeeba938cb2012-02-03 14:47:55 -080028#define ARENA_DEFAULT_SIZE ((2 * 1024 * 1024) - 256)
buzbee67bf8852011-08-17 17:51:35 -070029
buzbeeeaf09bc2012-11-15 14:51:41 -080030/* Type of allocation for memory tuning */
buzbeefa57c472012-11-21 12:06:18 -080031enum oat_alloc_kind {
buzbeeeaf09bc2012-11-15 14:51:41 -080032 kAllocMisc,
33 kAllocBB,
34 kAllocLIR,
35 kAllocMIR,
36 kAllocDFInfo,
37 kAllocGrowableList,
38 kAllocGrowableBitMap,
39 kAllocDalvikToSSAMap,
40 kAllocDebugInfo,
41 kAllocSuccessor,
42 kAllocRegAlloc,
43 kAllocData,
44 kAllocPredecessors,
45 kNumAllocKinds
46};
47
48/* Type of growable list for memory tuning */
buzbeefa57c472012-11-21 12:06:18 -080049enum oat_list_kind {
buzbeeeaf09bc2012-11-15 14:51:41 -080050 kListMisc = 0,
51 kListBlockList,
52 kListSSAtoDalvikMap,
53 kListDfsOrder,
54 kListDfsPostOrder,
55 kListDomPostOrderTraversal,
56 kListThrowLaunchPads,
57 kListSuspendLaunchPads,
58 kListSwitchTables,
59 kListFillArrayData,
60 kListSuccessorBlocks,
61 kListPredecessors,
62 kNumListKinds
63};
64
65/* Type of growable bitmap for memory tuning */
buzbeefa57c472012-11-21 12:06:18 -080066enum oat_bit_map_kind {
buzbeeeaf09bc2012-11-15 14:51:41 -080067 kBitMapMisc = 0,
68 kBitMapUse,
69 kBitMapDef,
70 kBitMapLiveIn,
71 kBitMapBMatrix,
72 kBitMapDominators,
73 kBitMapIDominated,
74 kBitMapDomFrontier,
75 kBitMapPhi,
76 kBitMapTmpBlocks,
77 kBitMapInputBlocks,
78 kBitMapRegisterV,
79 kBitMapTempSSARegisterV,
80 kBitMapNullCheck,
81 kBitMapTmpBlockV,
82 kBitMapPredecessors,
83 kNumBitMapKinds
84};
85
buzbee67bf8852011-08-17 17:51:35 -070086/* Allocate the initial memory block for arena-based allocation */
buzbeefa57c472012-11-21 12:06:18 -080087bool HeapInit(CompilationUnit* cu);
buzbee67bf8852011-08-17 17:51:35 -070088
Elliott Hughesabe64aa2012-05-30 17:34:45 -070089/* Collect memory usage statistics */
buzbee5abfa3e2012-01-31 17:01:43 -080090//#define WITH_MEMSTATS
91
Elliott Hughes719ace42012-03-09 18:06:03 -080092struct ArenaMemBlock {
buzbeefa57c472012-11-21 12:06:18 -080093 size_t block_size;
94 size_t bytes_allocated;
Bill Buzbeea114add2012-05-03 15:00:40 -070095 ArenaMemBlock *next;
96 char ptr[0];
Elliott Hughes719ace42012-03-09 18:06:03 -080097};
buzbee67bf8852011-08-17 17:51:35 -070098
buzbeefa57c472012-11-21 12:06:18 -080099void* NewMem(CompilationUnit* cu, size_t size, bool zero, oat_alloc_kind kind);
buzbee67bf8852011-08-17 17:51:35 -0700100
buzbeefa57c472012-11-21 12:06:18 -0800101void ArenaReset(CompilationUnit *cu);
buzbee67bf8852011-08-17 17:51:35 -0700102
Elliott Hughes719ace42012-03-09 18:06:03 -0800103struct GrowableList {
buzbeefa57c472012-11-21 12:06:18 -0800104 GrowableList() : num_allocated(0), num_used(0), elem_list(NULL) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700105 }
106
buzbeefa57c472012-11-21 12:06:18 -0800107 size_t num_allocated;
108 size_t num_used;
109 uintptr_t* elem_list;
buzbee5abfa3e2012-01-31 17:01:43 -0800110#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -0800111 oat_list_kind kind;
buzbee5abfa3e2012-01-31 17:01:43 -0800112#endif
Elliott Hughes719ace42012-03-09 18:06:03 -0800113};
buzbee67bf8852011-08-17 17:51:35 -0700114
Elliott Hughes719ace42012-03-09 18:06:03 -0800115struct GrowableListIterator {
Bill Buzbeea114add2012-05-03 15:00:40 -0700116 GrowableList* list;
117 size_t idx;
118 size_t size;
Elliott Hughes719ace42012-03-09 18:06:03 -0800119};
buzbee67bf8852011-08-17 17:51:35 -0700120
121/*
122 * Expanding bitmap, used for tracking resources. Bits are numbered starting
123 * from zero.
124 *
125 * All operations on a BitVector are unsynchronized.
126 */
127struct ArenaBitVector {
buzbeeeaf09bc2012-11-15 14:51:41 -0800128 bool expandable; /* expand bitmap if we run out? */
buzbeefa57c472012-11-21 12:06:18 -0800129 uint32_t storage_size; /* current size, in 32-bit words */
buzbeeeaf09bc2012-11-15 14:51:41 -0800130 uint32_t* storage;
buzbee5abfa3e2012-01-31 17:01:43 -0800131#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -0800132 oat_bit_map_kind kind; /* for memory use tuning */
buzbee5abfa3e2012-01-31 17:01:43 -0800133#endif
buzbee67bf8852011-08-17 17:51:35 -0700134};
135
136/* Handy iterator to walk through the bit positions set to 1 */
137struct ArenaBitVectorIterator {
buzbeefa57c472012-11-21 12:06:18 -0800138 ArenaBitVector* p_bits;
buzbeeeaf09bc2012-11-15 14:51:41 -0800139 uint32_t idx;
buzbeefa57c472012-11-21 12:06:18 -0800140 uint32_t bit_size;
buzbee67bf8852011-08-17 17:51:35 -0700141};
142
buzbeefa57c472012-11-21 12:06:18 -0800143#define GET_ELEM_N(LIST, TYPE, N) ((reinterpret_cast<TYPE*>(LIST->elem_list)[N]))
buzbee67bf8852011-08-17 17:51:35 -0700144
145#define BLOCK_NAME_LEN 80
146
147/* Forward declarations */
buzbee67bf8852011-08-17 17:51:35 -0700148struct BasicBlock;
149struct CompilationUnit;
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700150struct LIR;
151struct RegLocation;
buzbee67bf8852011-08-17 17:51:35 -0700152
buzbeefa57c472012-11-21 12:06:18 -0800153void CompilerInitGrowableList(CompilationUnit* cu, GrowableList* g_list,
154 size_t init_length, oat_list_kind kind = kListMisc);
155void InsertGrowableList(CompilationUnit* cu, GrowableList* g_list,
buzbeecbd6d442012-11-17 14:11:25 -0800156 uintptr_t elem);
buzbeefa57c472012-11-21 12:06:18 -0800157void DeleteGrowableList(GrowableList* g_list, uintptr_t elem);
158void GrowableListIteratorInit(GrowableList* g_list,
buzbee67bf8852011-08-17 17:51:35 -0700159 GrowableListIterator* iterator);
buzbee52a77fc2012-11-20 19:50:46 -0800160uintptr_t GrowableListIteratorNext(GrowableListIterator* iterator);
buzbeefa57c472012-11-21 12:06:18 -0800161uintptr_t GrowableListGetElement(const GrowableList* g_list, size_t idx);
buzbee67bf8852011-08-17 17:51:35 -0700162
buzbeefa57c472012-11-21 12:06:18 -0800163ArenaBitVector* AllocBitVector(CompilationUnit* cu,
164 unsigned int start_bits, bool expandable,
165 oat_bit_map_kind = kBitMapMisc);
166void BitVectorIteratorInit(ArenaBitVector* p_bits,
buzbee67bf8852011-08-17 17:51:35 -0700167 ArenaBitVectorIterator* iterator);
buzbee52a77fc2012-11-20 19:50:46 -0800168int BitVectorIteratorNext(ArenaBitVectorIterator* iterator);
buzbeefa57c472012-11-21 12:06:18 -0800169bool SetBit(CompilationUnit *cu, ArenaBitVector* p_bits, unsigned int num);
170bool ClearBit(ArenaBitVector* p_bits, unsigned int num);
171void MarkAllBits(ArenaBitVector* p_bits, bool set);
buzbee52a77fc2012-11-20 19:50:46 -0800172void DebugBitVector(char* msg, const ArenaBitVector* bv, int length);
buzbeefa57c472012-11-21 12:06:18 -0800173bool IsBitSet(const ArenaBitVector* p_bits, unsigned int num);
174void ClearAllBits(ArenaBitVector* p_bits);
175void SetInitialBits(ArenaBitVector* p_bits, unsigned int num_bits);
buzbee52a77fc2012-11-20 19:50:46 -0800176void CopyBitVector(ArenaBitVector* dest, const ArenaBitVector* src);
177bool IntersectBitVectors(ArenaBitVector* dest, const ArenaBitVector* src1,
buzbee67bf8852011-08-17 17:51:35 -0700178 const ArenaBitVector* src2);
buzbee52a77fc2012-11-20 19:50:46 -0800179bool UnifyBitVetors(ArenaBitVector* dest, const ArenaBitVector* src1,
buzbee67bf8852011-08-17 17:51:35 -0700180 const ArenaBitVector* src2);
buzbee52a77fc2012-11-20 19:50:46 -0800181bool CompareBitVectors(const ArenaBitVector* src1,
buzbee67bf8852011-08-17 17:51:35 -0700182 const ArenaBitVector* src2);
buzbee52a77fc2012-11-20 19:50:46 -0800183bool TestBitVectors(const ArenaBitVector* src1, const ArenaBitVector* src2);
buzbeefa57c472012-11-21 12:06:18 -0800184int CountSetBits(const ArenaBitVector* p_bits);
buzbee67bf8852011-08-17 17:51:35 -0700185
buzbeefa57c472012-11-21 12:06:18 -0800186void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr);
buzbee52a77fc2012-11-20 19:50:46 -0800187void DumpResourceMask(LIR* lir, uint64_t mask, const char* prefix);
188void DumpBlockBitVector(const GrowableList* blocks, char* msg,
buzbee67bf8852011-08-17 17:51:35 -0700189 const ArenaBitVector* bv, int length);
buzbee52a77fc2012-11-20 19:50:46 -0800190void GetBlockName(BasicBlock* bb, char* name);
191const char* GetShortyFromTargetIdx(CompilationUnit*, int);
buzbeefa57c472012-11-21 12:06:18 -0800192void DumpMemStats(CompilationUnit* cu);
buzbee67bf8852011-08-17 17:51:35 -0700193
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800194} // namespace art
195
buzbee67bf8852011-08-17 17:51:35 -0700196#endif // ART_SRC_COMPILER_COMPILER_UTILITY_H_