buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame] | 17 | #include "compiler_internals.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 18 | #include "dex_file-inl.h" |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 20 | namespace art { |
| 21 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 22 | #ifdef WITH_MEMSTATS |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 23 | struct Memstats { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 24 | uint32_t alloc_stats[kNumAllocKinds]; |
| 25 | int list_sizes[kNumListKinds]; |
| 26 | int list_wasted[kNumListKinds]; |
| 27 | int list_grows[kNumListKinds]; |
| 28 | int list_max_elems[kNumListKinds]; |
| 29 | int bit_map_sizes[kNumBitMapKinds]; |
| 30 | int bit_map_wasted[kNumBitMapKinds]; |
| 31 | int bit_map_grows[kNumBitMapKinds]; |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 32 | }; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 33 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 34 | const char* alloc_names[kNumAllocKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 35 | "Misc ", |
| 36 | "BasicBlock ", |
| 37 | "LIR ", |
| 38 | "MIR ", |
| 39 | "DataFlow ", |
| 40 | "GrowList ", |
| 41 | "GrowBitMap ", |
| 42 | "Dalvik2SSA ", |
| 43 | "DebugInfo ", |
| 44 | "Successor ", |
| 45 | "RegAlloc ", |
| 46 | "Data ", |
| 47 | "Preds ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 50 | const char* list_names[kNumListKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 51 | "Misc ", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 52 | "block_list ", |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 53 | "SSAtoDalvik ", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 54 | "dfs_order ", |
| 55 | "dfs_post_order ", |
| 56 | "dom_post_order_traversal ", |
| 57 | "throw_launch_pads ", |
| 58 | "suspend_launch_pads ", |
| 59 | "switch_tables ", |
| 60 | "fill_array_data ", |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 61 | "SuccessorBlocks ", |
| 62 | "Predecessors ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 65 | const char* bit_map_names[kNumBitMapKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 66 | "Misc ", |
| 67 | "Use ", |
| 68 | "Def ", |
| 69 | "LiveIn ", |
| 70 | "BlockMatrix ", |
| 71 | "Dominators ", |
| 72 | "IDominated ", |
| 73 | "DomFrontier ", |
| 74 | "Phi ", |
| 75 | "TmpBlocks ", |
| 76 | "InputBlocks ", |
| 77 | "RegisterV ", |
| 78 | "TempSSARegisterV ", |
| 79 | "Null Check ", |
| 80 | "TmpBlockV ", |
| 81 | "Predecessors ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 82 | }; |
| 83 | #endif |
| 84 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 85 | #define kArenaBitVectorGrowth 4 /* increase by 4 uint32_ts when limit hit */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 86 | |
| 87 | /* Allocate the initial memory block for arena-based allocation */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 88 | bool HeapInit(CompilationUnit* cu) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 89 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 90 | DCHECK(cu->arena_head == NULL); |
| 91 | cu->arena_head = |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 92 | static_cast<ArenaMemBlock*>(malloc(sizeof(ArenaMemBlock) + ARENA_DEFAULT_SIZE)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 93 | if (cu->arena_head == NULL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 94 | LOG(FATAL) << "No memory left to create compiler heap memory"; |
| 95 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 96 | cu->arena_head->block_size = ARENA_DEFAULT_SIZE; |
| 97 | cu->current_arena = cu->arena_head; |
| 98 | cu->current_arena->bytes_allocated = 0; |
| 99 | cu->current_arena->next = NULL; |
| 100 | cu->num_arena_blocks = 1; |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 101 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 102 | cu->mstats = (Memstats*) NewMem(cu, sizeof(Memstats), true, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 103 | kAllocDebugInfo); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 104 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 105 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* Arena-based malloc for compilation tasks */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 109 | void* NewMem(CompilationUnit* cu, size_t size, bool zero, oat_alloc_kind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 110 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 111 | size = (size + 3) & ~3; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 112 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 113 | if (cu->mstats != NULL) { |
| 114 | cu->mstats->alloc_stats[kind] += size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 115 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 116 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 117 | retry: |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 118 | /* Normal case - space is available in the current page */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 119 | if (size + cu->current_arena->bytes_allocated <= |
| 120 | cu->current_arena->block_size) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 121 | void *ptr; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 122 | ptr = &cu->current_arena->ptr[cu->current_arena->bytes_allocated]; |
| 123 | cu->current_arena->bytes_allocated += size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 124 | if (zero) { |
| 125 | memset(ptr, 0, size); |
| 126 | } |
| 127 | return ptr; |
| 128 | } else { |
| 129 | /* |
| 130 | * See if there are previously allocated arena blocks before the last |
| 131 | * reset |
| 132 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 133 | if (cu->current_arena->next) { |
| 134 | cu->current_arena = cu->current_arena->next; |
| 135 | cu->current_arena->bytes_allocated = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 136 | goto retry; |
| 137 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 138 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 139 | size_t block_size = (size < ARENA_DEFAULT_SIZE) ? ARENA_DEFAULT_SIZE : size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 140 | /* Time to allocate a new arena */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 141 | ArenaMemBlock *new_arena = |
| 142 | static_cast<ArenaMemBlock*>(malloc(sizeof(ArenaMemBlock) + block_size)); |
| 143 | if (new_arena == NULL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 144 | LOG(FATAL) << "Arena allocation failure"; |
| 145 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 146 | new_arena->block_size = block_size; |
| 147 | new_arena->bytes_allocated = 0; |
| 148 | new_arena->next = NULL; |
| 149 | cu->current_arena->next = new_arena; |
| 150 | cu->current_arena = new_arena; |
| 151 | cu->num_arena_blocks++; |
| 152 | if (cu->num_arena_blocks > 20000) { |
| 153 | LOG(INFO) << "Total arena pages: " << cu->num_arena_blocks; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 154 | } |
| 155 | goto retry; |
| 156 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* Reclaim all the arena blocks allocated so far */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 160 | void ArenaReset(CompilationUnit* cu) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 161 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 162 | ArenaMemBlock* head = cu->arena_head; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 163 | while (head != NULL) { |
| 164 | ArenaMemBlock* p = head; |
| 165 | head = head->next; |
| 166 | free(p); |
| 167 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 168 | cu->arena_head = NULL; |
| 169 | cu->current_arena = NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Growable List initialization */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 173 | void CompilerInitGrowableList(CompilationUnit* cu, GrowableList* g_list, |
| 174 | size_t init_length, oat_list_kind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 175 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 176 | g_list->num_allocated = init_length; |
| 177 | g_list->num_used = 0; |
| 178 | g_list->elem_list = static_cast<uintptr_t *>(NewMem(cu, sizeof(intptr_t) * init_length, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 179 | true, kAllocGrowableList)); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 180 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 181 | cu->mstats->list_sizes[kind] += sizeof(uintptr_t) * init_length; |
| 182 | g_list->kind = kind; |
| 183 | if (static_cast<int>(init_length) > cu->mstats->list_max_elems[kind]) { |
| 184 | cu->mstats->list_max_elems[kind] = init_length; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 185 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 186 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 187 | } |
| 188 | |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 189 | void ReallocGrowableList(CompilationUnit* cu, GrowableList* g_list, size_t new_length) |
| 190 | { |
| 191 | if (new_length > g_list->num_allocated) { |
| 192 | uintptr_t *new_array = |
| 193 | static_cast<uintptr_t*>(NewMem(cu, sizeof(uintptr_t) * new_length, true, |
| 194 | kAllocGrowableList)); |
| 195 | memcpy(new_array, g_list->elem_list, sizeof(uintptr_t) * g_list->num_allocated); |
| 196 | g_list->num_allocated = new_length; |
| 197 | g_list->elem_list = new_array; |
| 198 | } |
| 199 | } |
| 200 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 201 | /* Expand the capacity of a growable list */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 202 | static void ExpandGrowableList(CompilationUnit* cu, GrowableList* g_list) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 203 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 204 | int new_length = g_list->num_allocated; |
| 205 | if (new_length < 128) { |
| 206 | new_length <<= 1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 207 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 208 | new_length += 128; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 209 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 210 | uintptr_t *new_array = |
| 211 | static_cast<uintptr_t*>(NewMem(cu, sizeof(uintptr_t) * new_length, true, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 212 | kAllocGrowableList)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 213 | memcpy(new_array, g_list->elem_list, sizeof(uintptr_t) * g_list->num_allocated); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 214 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 215 | cu->mstats->list_sizes[g_list->kind] += sizeof(uintptr_t) * new_length; |
| 216 | cu->mstats->list_wasted[g_list->kind] += |
| 217 | sizeof(uintptr_t) * g_list->num_allocated; |
| 218 | cu->mstats->list_grows[g_list->kind]++; |
| 219 | if (new_length > cu->mstats->list_max_elems[g_list->kind]) { |
| 220 | cu->mstats->list_max_elems[g_list->kind] = new_length; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 221 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 222 | #endif |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 223 | g_list->num_allocated = new_length; |
| 224 | g_list->elem_list = new_array; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* Insert a new element into the growable list */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 228 | void InsertGrowableList(CompilationUnit* cu, GrowableList* g_list, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 229 | uintptr_t elem) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 230 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 231 | DCHECK_NE(g_list->num_allocated, 0U); |
| 232 | if (g_list->num_used == g_list->num_allocated) { |
| 233 | ExpandGrowableList(cu, g_list); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 234 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 235 | g_list->elem_list[g_list->num_used++] = elem; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 236 | } |
| 237 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 238 | /* Delete an element from a growable list. Element must be present */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 239 | void DeleteGrowableList(GrowableList* g_list, uintptr_t elem) |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 240 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 241 | bool found = false; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 242 | for (unsigned int i = 0; i < g_list->num_used; i++) { |
| 243 | if (!found && g_list->elem_list[i] == elem) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 244 | found = true; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 245 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 246 | if (found) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 247 | g_list->elem_list[i] = g_list->elem_list[i+1]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | DCHECK_EQ(found, true); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 251 | g_list->num_used--; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 252 | } |
| 253 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 254 | void GrowableListIteratorInit(GrowableList* g_list, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 255 | GrowableListIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 256 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 257 | iterator->list = g_list; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 258 | iterator->idx = 0; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 259 | iterator->size = g_list->num_used; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 260 | } |
| 261 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 262 | uintptr_t GrowableListIteratorNext(GrowableListIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 263 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 264 | DCHECK_EQ(iterator->size, iterator->list->num_used); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 265 | if (iterator->idx == iterator->size) return 0; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 266 | return iterator->list->elem_list[iterator->idx++]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 267 | } |
| 268 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 269 | uintptr_t GrowableListGetElement(const GrowableList* g_list, size_t idx) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 270 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 271 | DCHECK_LT(idx, g_list->num_used); |
| 272 | return g_list->elem_list[idx]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 273 | } |
| 274 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 275 | #ifdef WITH_MEMSTATS |
| 276 | /* Dump memory usage stats */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 277 | void DumpMemStats(CompilationUnit* cu) |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 278 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 279 | uint32_t total = 0; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 280 | for (int i = 0; i < kNumAllocKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 281 | total += cu->mstats->alloc_stats[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 282 | } |
| 283 | if (total > (10 * 1024 * 1024)) { |
| 284 | LOG(INFO) << "MEMUSAGE: " << total << " : " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 285 | << PrettyMethod(cu->method_idx, *cu->dex_file); |
buzbee | 311ca16 | 2013-02-28 15:56:43 -0800 | [diff] [blame] | 286 | LOG(INFO) << "insns_size: " << cu->code_item->insns_size_in_code_units_; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 287 | LOG(INFO) << "===== Overall allocations"; |
| 288 | for (int i = 0; i < kNumAllocKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 289 | LOG(INFO) << alloc_names[i] << std::setw(10) << |
| 290 | cu->mstats->alloc_stats[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 291 | } |
| 292 | LOG(INFO) << "===== GrowableList allocations"; |
| 293 | for (int i = 0; i < kNumListKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 294 | LOG(INFO) << list_names[i] |
| 295 | << " S:" << cu->mstats->list_sizes[i] |
| 296 | << ", W:" << cu->mstats->list_wasted[i] |
| 297 | << ", G:" << cu->mstats->list_grows[i] |
| 298 | << ", E:" << cu->mstats->list_max_elems[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 299 | } |
| 300 | LOG(INFO) << "===== GrowableBitMap allocations"; |
| 301 | for (int i = 0; i < kNumBitMapKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 302 | LOG(INFO) << bit_map_names[i] |
| 303 | << " S:" << cu->mstats->bit_map_sizes[i] |
| 304 | << ", W:" << cu->mstats->bit_map_wasted[i] |
| 305 | << ", G:" << cu->mstats->bit_map_grows[i]; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 306 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 307 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 308 | } |
| 309 | #endif |
| 310 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 311 | static uint32_t check_masks[32] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 312 | 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, |
| 313 | 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200, |
| 314 | 0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000, |
| 315 | 0x00008000, 0x00010000, 0x00020000, 0x00040000, 0x00080000, |
| 316 | 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000, |
| 317 | 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, |
| 318 | 0x40000000, 0x80000000 }; |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 319 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 320 | /* |
| 321 | * Allocate a bit vector with enough space to hold at least the specified |
| 322 | * number of bits. |
| 323 | * |
| 324 | * NOTE: memory is allocated from the compiler arena. |
| 325 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 326 | ArenaBitVector* AllocBitVector(CompilationUnit* cu, |
| 327 | unsigned int start_bits, bool expandable, |
| 328 | oat_bit_map_kind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 329 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 330 | ArenaBitVector* bv; |
| 331 | unsigned int count; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 332 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 333 | DCHECK_EQ(sizeof(bv->storage[0]), 4U); /* assuming 32-bit units */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 334 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 335 | bv = static_cast<ArenaBitVector*>(NewMem(cu, sizeof(ArenaBitVector), false, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 336 | kAllocGrowableBitMap)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 337 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 338 | count = (start_bits + 31) >> 5; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 339 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 340 | bv->storage_size = count; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 341 | bv->expandable = expandable; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 342 | bv->storage = static_cast<uint32_t*>(NewMem(cu, count * sizeof(uint32_t), true, |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 343 | kAllocGrowableBitMap)); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 344 | #ifdef WITH_MEMSTATS |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 345 | bv->kind = kind; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 346 | cu->mstats->bit_map_sizes[kind] += count * sizeof(uint32_t); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 347 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 348 | return bv; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Determine whether or not the specified bit is set. |
| 353 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 354 | bool IsBitSet(const ArenaBitVector* p_bits, unsigned int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 355 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 356 | DCHECK_LT(num, p_bits->storage_size * sizeof(uint32_t) * 8); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 357 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 358 | unsigned int val = p_bits->storage[num >> 5] & check_masks[num & 0x1f]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 359 | return (val != 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Mark all bits bit as "clear". |
| 364 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 365 | void ClearAllBits(ArenaBitVector* p_bits) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 366 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 367 | unsigned int count = p_bits->storage_size; |
| 368 | memset(p_bits->storage, 0, count * sizeof(uint32_t)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Mark the specified bit as "set". |
| 373 | * |
| 374 | * Returns "false" if the bit is outside the range of the vector and we're |
| 375 | * not allowed to expand. |
| 376 | * |
| 377 | * NOTE: memory is allocated from the compiler arena. |
| 378 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 379 | bool SetBit(CompilationUnit* cu, ArenaBitVector* p_bits, unsigned int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 380 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 381 | if (num >= p_bits->storage_size * sizeof(uint32_t) * 8) { |
| 382 | if (!p_bits->expandable) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 383 | LOG(FATAL) << "Can't expand"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 386 | /* Round up to word boundaries for "num+1" bits */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 387 | unsigned int new_size = (num + 1 + 31) >> 5; |
| 388 | DCHECK_GT(new_size, p_bits->storage_size); |
| 389 | uint32_t *new_storage = static_cast<uint32_t*>(NewMem(cu, new_size * sizeof(uint32_t), false, |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 390 | kAllocGrowableBitMap)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 391 | memcpy(new_storage, p_bits->storage, p_bits->storage_size * sizeof(uint32_t)); |
| 392 | memset(&new_storage[p_bits->storage_size], 0, |
| 393 | (new_size - p_bits->storage_size) * sizeof(uint32_t)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 394 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 395 | cu->mstats->bit_map_wasted[p_bits->kind] += |
| 396 | p_bits->storage_size * sizeof(uint32_t); |
| 397 | cu->mstats->bit_map_sizes[p_bits->kind] += new_size * sizeof(uint32_t); |
| 398 | cu->mstats->bit_map_grows[p_bits->kind]++; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 399 | #endif |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 400 | p_bits->storage = new_storage; |
| 401 | p_bits->storage_size = new_size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 402 | } |
| 403 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 404 | p_bits->storage[num >> 5] |= check_masks[num & 0x1f]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 405 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Mark the specified bit as "unset". |
| 410 | * |
| 411 | * Returns "false" if the bit is outside the range of the vector and we're |
| 412 | * not allowed to expand. |
| 413 | * |
| 414 | * NOTE: memory is allocated from the compiler arena. |
| 415 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 416 | bool ClearBit(ArenaBitVector* p_bits, unsigned int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 417 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 418 | if (num >= p_bits->storage_size * sizeof(uint32_t) * 8) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 419 | LOG(FATAL) << "Attempt to clear a bit not set in the vector yet";; |
| 420 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 421 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 422 | p_bits->storage[num >> 5] &= ~check_masks[num & 0x1f]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 423 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 424 | } |
| 425 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 426 | /* Initialize the iterator structure */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 427 | void BitVectorIteratorInit(ArenaBitVector* p_bits, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 428 | ArenaBitVectorIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 429 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 430 | iterator->p_bits = p_bits; |
| 431 | iterator->bit_size = p_bits->storage_size * sizeof(uint32_t) * 8; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 432 | iterator->idx = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* |
| 436 | * If the vector sizes don't match, log an error and abort. |
| 437 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 438 | static void CheckSizes(const ArenaBitVector* bv1, const ArenaBitVector* bv2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 439 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 440 | if (bv1->storage_size != bv2->storage_size) { |
| 441 | LOG(FATAL) << "Mismatched vector sizes (" << bv1->storage_size |
| 442 | << ", " << bv2->storage_size << ")"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 443 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Copy a whole vector to the other. Only do that when the both vectors have |
| 448 | * the same size. |
| 449 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 450 | void CopyBitVector(ArenaBitVector* dest, const ArenaBitVector* src) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 451 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 452 | /* if dest is expandable and < src, we could expand dest to match */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 453 | CheckSizes(dest, src); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 454 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 455 | memcpy(dest->storage, src->storage, sizeof(uint32_t) * dest->storage_size); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Intersect two bit vectors and store the result to the dest vector. |
| 460 | */ |
| 461 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 462 | bool IntersectBitVectors(ArenaBitVector* dest, const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 463 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 464 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 465 | DCHECK(src1 != NULL); |
| 466 | DCHECK(src2 != NULL); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 467 | if (dest->storage_size != src1->storage_size || |
| 468 | dest->storage_size != src2->storage_size || |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 469 | dest->expandable != src1->expandable || |
| 470 | dest->expandable != src2->expandable) |
| 471 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 472 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 473 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 474 | for (idx = 0; idx < dest->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 475 | dest->storage[idx] = src1->storage[idx] & src2->storage[idx]; |
| 476 | } |
| 477 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Unify two bit vectors and store the result to the dest vector. |
| 482 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 483 | bool UnifyBitVetors(ArenaBitVector* dest, const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 484 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 485 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 486 | DCHECK(src1 != NULL); |
| 487 | DCHECK(src2 != NULL); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 488 | if (dest->storage_size != src1->storage_size || |
| 489 | dest->storage_size != src2->storage_size || |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 490 | dest->expandable != src1->expandable || |
| 491 | dest->expandable != src2->expandable) |
| 492 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 493 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 494 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 495 | for (idx = 0; idx < dest->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 496 | dest->storage[idx] = src1->storage[idx] | src2->storage[idx]; |
| 497 | } |
| 498 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 502 | * Return true if any bits collide. Vectors must be same size. |
| 503 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 504 | bool TestBitVectors(const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 505 | const ArenaBitVector* src2) |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 506 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 507 | DCHECK_EQ(src1->storage_size, src2->storage_size); |
| 508 | for (uint32_t idx = 0; idx < src1->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 509 | if (src1->storage[idx] & src2->storage[idx]) return true; |
| 510 | } |
| 511 | return false; |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 515 | * Compare two bit vectors and return true if difference is seen. |
| 516 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 517 | bool CompareBitVectors(const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 518 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 519 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 520 | if (src1->storage_size != src2->storage_size || |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 521 | src1->expandable != src2->expandable) |
| 522 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 523 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 524 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 525 | for (idx = 0; idx < src1->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 526 | if (src1->storage[idx] != src2->storage[idx]) return true; |
| 527 | } |
| 528 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Count the number of bits that are set. |
| 533 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 534 | int CountSetBits(const ArenaBitVector* p_bits) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 535 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 536 | unsigned int word; |
| 537 | unsigned int count = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 538 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 539 | for (word = 0; word < p_bits->storage_size; word++) { |
| 540 | uint32_t val = p_bits->storage[word]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 541 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 542 | if (val != 0) { |
| 543 | if (val == 0xffffffff) { |
| 544 | count += 32; |
| 545 | } else { |
| 546 | /* count the number of '1' bits */ |
| 547 | while (val != 0) { |
| 548 | val &= val - 1; |
| 549 | count++; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 550 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 551 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 552 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 553 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 554 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 555 | return count; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* Return the next position set to 1. -1 means end-of-element reached */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 559 | int BitVectorIteratorNext(ArenaBitVectorIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 560 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 561 | ArenaBitVector* p_bits = iterator->p_bits; |
| 562 | uint32_t bit_index = iterator->idx; |
| 563 | uint32_t bit_size = iterator->bit_size; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 564 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 565 | DCHECK_EQ(bit_size, p_bits->storage_size * sizeof(uint32_t) * 8); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 566 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 567 | if (bit_index >= bit_size) return -1; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 568 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 569 | uint32_t word_index = bit_index >> 5; |
| 570 | uint32_t end_word_index = bit_size >> 5; |
| 571 | uint32_t* storage = p_bits->storage; |
| 572 | uint32_t word = storage[word_index++]; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 573 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 574 | // Mask out any bits in the first word we've already considered |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 575 | word &= ~((1 << (bit_index & 0x1f))-1); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 576 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 577 | for (; word_index <= end_word_index;) { |
| 578 | uint32_t bit_pos = bit_index & 0x1f; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 579 | if (word == 0) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 580 | bit_index += (32 - bit_pos); |
| 581 | word = storage[word_index++]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 582 | continue; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 583 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 584 | for (; bit_pos < 32; bit_pos++) { |
| 585 | if (word & (1 << bit_pos)) { |
| 586 | iterator->idx = bit_index + 1; |
| 587 | return bit_index; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 588 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 589 | bit_index++; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 590 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 591 | word = storage[word_index++]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 592 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 593 | iterator->idx = iterator->bit_size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 594 | return -1; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Mark specified number of bits as "set". Cannot set all bits like ClearAll |
| 599 | * since there might be unused bits - setting those to one will confuse the |
| 600 | * iterator. |
| 601 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 602 | void SetInitialBits(ArenaBitVector* p_bits, unsigned int num_bits) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 603 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 604 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 605 | DCHECK_LE(((num_bits + 31) >> 5), p_bits->storage_size); |
| 606 | for (idx = 0; idx < (num_bits >> 5); idx++) { |
| 607 | p_bits->storage[idx] = -1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 608 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 609 | unsigned int rem_num_bits = num_bits & 0x1f; |
| 610 | if (rem_num_bits) { |
| 611 | p_bits->storage[idx] = (1 << rem_num_bits) - 1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 612 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 613 | } |
| 614 | |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 615 | /* Allocate a new basic block */ |
| 616 | BasicBlock* NewMemBB(CompilationUnit* cu, BBType block_type, int block_id) |
| 617 | { |
| 618 | BasicBlock* bb = static_cast<BasicBlock*>(NewMem(cu, sizeof(BasicBlock), true, kAllocBB)); |
| 619 | bb->block_type = block_type; |
| 620 | bb->id = block_id; |
| 621 | bb->predecessors = static_cast<GrowableList*> |
| 622 | (NewMem(cu, sizeof(GrowableList), false, kAllocPredecessors)); |
| 623 | CompilerInitGrowableList(cu, bb->predecessors, |
| 624 | (block_type == kExitBlock) ? 2048 : 2, |
| 625 | kListPredecessors); |
buzbee | 1fd3346 | 2013-03-25 13:40:45 -0700 | [diff] [blame^] | 626 | cu->mir_graph->block_id_map_.Put(block_id, block_id); |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 627 | return bb; |
| 628 | } |
| 629 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 630 | } // namespace art |