blob: 4201f5d54a41626555c8c221833ef3c97472746f [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
buzbee02031b12012-11-23 09:41:35 -080087// Allocate the initial memory block for arena-based allocation.
buzbeefa57c472012-11-21 12:06:18 -080088bool HeapInit(CompilationUnit* cu);
buzbee67bf8852011-08-17 17:51:35 -070089
buzbee02031b12012-11-23 09:41:35 -080090// Uncomment to collect memory usage statistics.
buzbee5abfa3e2012-01-31 17:01:43 -080091//#define WITH_MEMSTATS
92
Elliott Hughes719ace42012-03-09 18:06:03 -080093struct ArenaMemBlock {
buzbeefa57c472012-11-21 12:06:18 -080094 size_t block_size;
95 size_t bytes_allocated;
Bill Buzbeea114add2012-05-03 15:00:40 -070096 ArenaMemBlock *next;
97 char ptr[0];
Elliott Hughes719ace42012-03-09 18:06:03 -080098};
buzbee67bf8852011-08-17 17:51:35 -070099
buzbeefa57c472012-11-21 12:06:18 -0800100void* NewMem(CompilationUnit* cu, size_t size, bool zero, oat_alloc_kind kind);
buzbee67bf8852011-08-17 17:51:35 -0700101
buzbeefa57c472012-11-21 12:06:18 -0800102void ArenaReset(CompilationUnit *cu);
buzbee67bf8852011-08-17 17:51:35 -0700103
Elliott Hughes719ace42012-03-09 18:06:03 -0800104struct GrowableList {
buzbeefa57c472012-11-21 12:06:18 -0800105 GrowableList() : num_allocated(0), num_used(0), elem_list(NULL) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700106 }
107
buzbeefa57c472012-11-21 12:06:18 -0800108 size_t num_allocated;
109 size_t num_used;
110 uintptr_t* elem_list;
buzbee5abfa3e2012-01-31 17:01:43 -0800111#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -0800112 oat_list_kind kind;
buzbee5abfa3e2012-01-31 17:01:43 -0800113#endif
Elliott Hughes719ace42012-03-09 18:06:03 -0800114};
buzbee67bf8852011-08-17 17:51:35 -0700115
Elliott Hughes719ace42012-03-09 18:06:03 -0800116struct GrowableListIterator {
Bill Buzbeea114add2012-05-03 15:00:40 -0700117 GrowableList* list;
118 size_t idx;
119 size_t size;
Elliott Hughes719ace42012-03-09 18:06:03 -0800120};
buzbee67bf8852011-08-17 17:51:35 -0700121
122/*
123 * Expanding bitmap, used for tracking resources. Bits are numbered starting
buzbee02031b12012-11-23 09:41:35 -0800124 * from zero. All operations on a BitVector are unsynchronized.
buzbee67bf8852011-08-17 17:51:35 -0700125 */
126struct ArenaBitVector {
buzbee02031b12012-11-23 09:41:35 -0800127 bool expandable; // expand bitmap if we run out?
128 uint32_t storage_size; // current size, in 32-bit words.
buzbeeeaf09bc2012-11-15 14:51:41 -0800129 uint32_t* storage;
buzbee5abfa3e2012-01-31 17:01:43 -0800130#ifdef WITH_MEMSTATS
buzbee02031b12012-11-23 09:41:35 -0800131 oat_bit_map_kind kind; // for memory use tuning.
buzbee5abfa3e2012-01-31 17:01:43 -0800132#endif
buzbee67bf8852011-08-17 17:51:35 -0700133};
134
buzbee02031b12012-11-23 09:41:35 -0800135// Handy iterator to walk through the bit positions set to 1.
buzbee67bf8852011-08-17 17:51:35 -0700136struct ArenaBitVectorIterator {
buzbeefa57c472012-11-21 12:06:18 -0800137 ArenaBitVector* p_bits;
buzbeeeaf09bc2012-11-15 14:51:41 -0800138 uint32_t idx;
buzbeefa57c472012-11-21 12:06:18 -0800139 uint32_t bit_size;
buzbee67bf8852011-08-17 17:51:35 -0700140};
141
buzbeefa57c472012-11-21 12:06:18 -0800142#define GET_ELEM_N(LIST, TYPE, N) ((reinterpret_cast<TYPE*>(LIST->elem_list)[N]))
buzbee67bf8852011-08-17 17:51:35 -0700143
144#define BLOCK_NAME_LEN 80
145
buzbee02031b12012-11-23 09:41:35 -0800146// Forward declarations
buzbee67bf8852011-08-17 17:51:35 -0700147struct BasicBlock;
148struct CompilationUnit;
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700149struct LIR;
150struct RegLocation;
buzbee02031b12012-11-23 09:41:35 -0800151struct MIR;
152enum BBType;
buzbee67bf8852011-08-17 17:51:35 -0700153
buzbeefa57c472012-11-21 12:06:18 -0800154void CompilerInitGrowableList(CompilationUnit* cu, GrowableList* g_list,
buzbee02031b12012-11-23 09:41:35 -0800155 size_t init_length, oat_list_kind kind = kListMisc);
buzbee311ca162013-02-28 15:56:43 -0800156void ReallocGrowableList(CompilationUnit* cu, GrowableList* g_list, size_t new_length);
buzbee02031b12012-11-23 09:41:35 -0800157void InsertGrowableList(CompilationUnit* cu, GrowableList* g_list, uintptr_t elem);
buzbeefa57c472012-11-21 12:06:18 -0800158void DeleteGrowableList(GrowableList* g_list, uintptr_t elem);
buzbee02031b12012-11-23 09:41:35 -0800159void GrowableListIteratorInit(GrowableList* g_list, 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);
buzbee02031b12012-11-23 09:41:35 -0800162ArenaBitVector* AllocBitVector(CompilationUnit* cu, unsigned int start_bits, bool expandable,
163 oat_bit_map_kind = kBitMapMisc);
164void BitVectorIteratorInit(ArenaBitVector* p_bits, ArenaBitVectorIterator* iterator);
buzbee52a77fc2012-11-20 19:50:46 -0800165int BitVectorIteratorNext(ArenaBitVectorIterator* iterator);
buzbeefa57c472012-11-21 12:06:18 -0800166bool SetBit(CompilationUnit *cu, ArenaBitVector* p_bits, unsigned int num);
167bool ClearBit(ArenaBitVector* p_bits, unsigned int num);
168void MarkAllBits(ArenaBitVector* p_bits, bool set);
buzbee52a77fc2012-11-20 19:50:46 -0800169void DebugBitVector(char* msg, const ArenaBitVector* bv, int length);
buzbeefa57c472012-11-21 12:06:18 -0800170bool IsBitSet(const ArenaBitVector* p_bits, unsigned int num);
171void ClearAllBits(ArenaBitVector* p_bits);
172void SetInitialBits(ArenaBitVector* p_bits, unsigned int num_bits);
buzbee52a77fc2012-11-20 19:50:46 -0800173void CopyBitVector(ArenaBitVector* dest, const ArenaBitVector* src);
174bool IntersectBitVectors(ArenaBitVector* dest, const ArenaBitVector* src1,
buzbee02031b12012-11-23 09:41:35 -0800175 const ArenaBitVector* src2);
176bool UnifyBitVetors(ArenaBitVector* dest, const ArenaBitVector* src1, const ArenaBitVector* src2);
177bool CompareBitVectors(const ArenaBitVector* src1, const ArenaBitVector* src2);
buzbee52a77fc2012-11-20 19:50:46 -0800178bool TestBitVectors(const ArenaBitVector* src1, const ArenaBitVector* src2);
buzbeefa57c472012-11-21 12:06:18 -0800179int CountSetBits(const ArenaBitVector* p_bits);
buzbeefa57c472012-11-21 12:06:18 -0800180void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr);
buzbee52a77fc2012-11-20 19:50:46 -0800181void DumpResourceMask(LIR* lir, uint64_t mask, const char* prefix);
buzbee02031b12012-11-23 09:41:35 -0800182void DumpBlockBitVector(const GrowableList* blocks, char* msg, const ArenaBitVector* bv,
183 int length);
buzbee52a77fc2012-11-20 19:50:46 -0800184void GetBlockName(BasicBlock* bb, char* name);
185const char* GetShortyFromTargetIdx(CompilationUnit*, int);
buzbeefa57c472012-11-21 12:06:18 -0800186void DumpMemStats(CompilationUnit* cu);
buzbee02031b12012-11-23 09:41:35 -0800187void DumpCompilationUnit(CompilationUnit* cu);
188BasicBlock* NewMemBB(CompilationUnit* cu, BBType block_type, int block_id);
189void AppendMIR(BasicBlock* bb, MIR* mir);
190void PrependMIR(BasicBlock* bb, MIR* mir);
191void InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir);
192void AppendLIR(CompilationUnit *cu, LIR* lir);
193void InsertLIRBefore(LIR* current_lir, LIR* new_lir);
194void InsertLIRAfter(LIR* current_lir, LIR* new_lir);
buzbee311ca162013-02-28 15:56:43 -0800195void ReplaceSpecialChars(std::string& str);
196char* GetDalvikDisassembly(CompilationUnit* cu, const MIR* mir);
197std::string GetSSAName(const CompilationUnit* cu, int ssa_reg);
198std::string GetSSANameWithConst(const CompilationUnit* cu, int ssa_reg, bool singles_only);
buzbee67bf8852011-08-17 17:51:35 -0700199
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800200} // namespace art
201
buzbee395116c2013-02-27 14:30:25 -0800202#endif // ART_SRC_COMPILER_DEX_COMPILER_UTILITY_H_