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" |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 19 | namespace art { |
| 20 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 21 | const char* extended_mir_op_names[kMirOpLast - kMirOpFirst] = { |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 22 | "kMirOpPhi", |
| 23 | "kMirOpCopy", |
| 24 | "kMirFusedCmplFloat", |
| 25 | "kMirFusedCmpgFloat", |
| 26 | "kMirFusedCmplDouble", |
| 27 | "kMirFusedCmpgDouble", |
| 28 | "kMirFusedCmpLong", |
| 29 | "kMirNop", |
| 30 | "kMirOpNullCheck", |
| 31 | "kMirOpRangeCheck", |
| 32 | "kMirOpDivZeroCheck", |
| 33 | "kMirOpCheck", |
| 34 | }; |
| 35 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 36 | #ifdef WITH_MEMSTATS |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 37 | struct Memstats { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 38 | uint32_t alloc_stats[kNumAllocKinds]; |
| 39 | int list_sizes[kNumListKinds]; |
| 40 | int list_wasted[kNumListKinds]; |
| 41 | int list_grows[kNumListKinds]; |
| 42 | int list_max_elems[kNumListKinds]; |
| 43 | int bit_map_sizes[kNumBitMapKinds]; |
| 44 | int bit_map_wasted[kNumBitMapKinds]; |
| 45 | int bit_map_grows[kNumBitMapKinds]; |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 46 | }; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 47 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 48 | const char* alloc_names[kNumAllocKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 49 | "Misc ", |
| 50 | "BasicBlock ", |
| 51 | "LIR ", |
| 52 | "MIR ", |
| 53 | "DataFlow ", |
| 54 | "GrowList ", |
| 55 | "GrowBitMap ", |
| 56 | "Dalvik2SSA ", |
| 57 | "DebugInfo ", |
| 58 | "Successor ", |
| 59 | "RegAlloc ", |
| 60 | "Data ", |
| 61 | "Preds ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 64 | const char* list_names[kNumListKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 65 | "Misc ", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 66 | "block_list ", |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 67 | "SSAtoDalvik ", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 68 | "dfs_order ", |
| 69 | "dfs_post_order ", |
| 70 | "dom_post_order_traversal ", |
| 71 | "throw_launch_pads ", |
| 72 | "suspend_launch_pads ", |
| 73 | "switch_tables ", |
| 74 | "fill_array_data ", |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 75 | "SuccessorBlocks ", |
| 76 | "Predecessors ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 79 | const char* bit_map_names[kNumBitMapKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 80 | "Misc ", |
| 81 | "Use ", |
| 82 | "Def ", |
| 83 | "LiveIn ", |
| 84 | "BlockMatrix ", |
| 85 | "Dominators ", |
| 86 | "IDominated ", |
| 87 | "DomFrontier ", |
| 88 | "Phi ", |
| 89 | "TmpBlocks ", |
| 90 | "InputBlocks ", |
| 91 | "RegisterV ", |
| 92 | "TempSSARegisterV ", |
| 93 | "Null Check ", |
| 94 | "TmpBlockV ", |
| 95 | "Predecessors ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 96 | }; |
| 97 | #endif |
| 98 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 99 | #define kArenaBitVectorGrowth 4 /* increase by 4 uint32_ts when limit hit */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 100 | |
| 101 | /* Allocate the initial memory block for arena-based allocation */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 102 | bool HeapInit(CompilationUnit* cu) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 103 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 104 | DCHECK(cu->arena_head == NULL); |
| 105 | cu->arena_head = |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 106 | static_cast<ArenaMemBlock*>(malloc(sizeof(ArenaMemBlock) + ARENA_DEFAULT_SIZE)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 107 | if (cu->arena_head == NULL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 108 | LOG(FATAL) << "No memory left to create compiler heap memory"; |
| 109 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 110 | cu->arena_head->block_size = ARENA_DEFAULT_SIZE; |
| 111 | cu->current_arena = cu->arena_head; |
| 112 | cu->current_arena->bytes_allocated = 0; |
| 113 | cu->current_arena->next = NULL; |
| 114 | cu->num_arena_blocks = 1; |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 115 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 116 | cu->mstats = (Memstats*) NewMem(cu, sizeof(Memstats), true, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 117 | kAllocDebugInfo); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 118 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 119 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Arena-based malloc for compilation tasks */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 123 | void* NewMem(CompilationUnit* cu, size_t size, bool zero, oat_alloc_kind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 124 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 125 | size = (size + 3) & ~3; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 126 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 127 | if (cu->mstats != NULL) { |
| 128 | cu->mstats->alloc_stats[kind] += size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 129 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 130 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 131 | retry: |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 132 | /* Normal case - space is available in the current page */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 133 | if (size + cu->current_arena->bytes_allocated <= |
| 134 | cu->current_arena->block_size) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 135 | void *ptr; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 136 | ptr = &cu->current_arena->ptr[cu->current_arena->bytes_allocated]; |
| 137 | cu->current_arena->bytes_allocated += size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 138 | if (zero) { |
| 139 | memset(ptr, 0, size); |
| 140 | } |
| 141 | return ptr; |
| 142 | } else { |
| 143 | /* |
| 144 | * See if there are previously allocated arena blocks before the last |
| 145 | * reset |
| 146 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 147 | if (cu->current_arena->next) { |
| 148 | cu->current_arena = cu->current_arena->next; |
| 149 | cu->current_arena->bytes_allocated = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 150 | goto retry; |
| 151 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 152 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 153 | size_t block_size = (size < ARENA_DEFAULT_SIZE) ? ARENA_DEFAULT_SIZE : size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 154 | /* Time to allocate a new arena */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 155 | ArenaMemBlock *new_arena = |
| 156 | static_cast<ArenaMemBlock*>(malloc(sizeof(ArenaMemBlock) + block_size)); |
| 157 | if (new_arena == NULL) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 158 | LOG(FATAL) << "Arena allocation failure"; |
| 159 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 160 | new_arena->block_size = block_size; |
| 161 | new_arena->bytes_allocated = 0; |
| 162 | new_arena->next = NULL; |
| 163 | cu->current_arena->next = new_arena; |
| 164 | cu->current_arena = new_arena; |
| 165 | cu->num_arena_blocks++; |
| 166 | if (cu->num_arena_blocks > 20000) { |
| 167 | LOG(INFO) << "Total arena pages: " << cu->num_arena_blocks; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 168 | } |
| 169 | goto retry; |
| 170 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* Reclaim all the arena blocks allocated so far */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 174 | void ArenaReset(CompilationUnit* cu) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 175 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 176 | ArenaMemBlock* head = cu->arena_head; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 177 | while (head != NULL) { |
| 178 | ArenaMemBlock* p = head; |
| 179 | head = head->next; |
| 180 | free(p); |
| 181 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 182 | cu->arena_head = NULL; |
| 183 | cu->current_arena = NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* Growable List initialization */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 187 | void CompilerInitGrowableList(CompilationUnit* cu, GrowableList* g_list, |
| 188 | size_t init_length, oat_list_kind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 189 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 190 | g_list->num_allocated = init_length; |
| 191 | g_list->num_used = 0; |
| 192 | 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] | 193 | true, kAllocGrowableList)); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 194 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 195 | cu->mstats->list_sizes[kind] += sizeof(uintptr_t) * init_length; |
| 196 | g_list->kind = kind; |
| 197 | if (static_cast<int>(init_length) > cu->mstats->list_max_elems[kind]) { |
| 198 | cu->mstats->list_max_elems[kind] = init_length; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 199 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 200 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* Expand the capacity of a growable list */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 204 | static void ExpandGrowableList(CompilationUnit* cu, GrowableList* g_list) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 205 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 206 | int new_length = g_list->num_allocated; |
| 207 | if (new_length < 128) { |
| 208 | new_length <<= 1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 209 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 210 | new_length += 128; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 211 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 212 | uintptr_t *new_array = |
| 213 | static_cast<uintptr_t*>(NewMem(cu, sizeof(uintptr_t) * new_length, true, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 214 | kAllocGrowableList)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 215 | 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] | 216 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 217 | cu->mstats->list_sizes[g_list->kind] += sizeof(uintptr_t) * new_length; |
| 218 | cu->mstats->list_wasted[g_list->kind] += |
| 219 | sizeof(uintptr_t) * g_list->num_allocated; |
| 220 | cu->mstats->list_grows[g_list->kind]++; |
| 221 | if (new_length > cu->mstats->list_max_elems[g_list->kind]) { |
| 222 | cu->mstats->list_max_elems[g_list->kind] = new_length; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 223 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 224 | #endif |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 225 | g_list->num_allocated = new_length; |
| 226 | g_list->elem_list = new_array; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Insert a new element into the growable list */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 230 | void InsertGrowableList(CompilationUnit* cu, GrowableList* g_list, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 231 | uintptr_t elem) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 232 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 233 | DCHECK_NE(g_list->num_allocated, 0U); |
| 234 | if (g_list->num_used == g_list->num_allocated) { |
| 235 | ExpandGrowableList(cu, g_list); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 236 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 237 | g_list->elem_list[g_list->num_used++] = elem; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 238 | } |
| 239 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 240 | /* Delete an element from a growable list. Element must be present */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 241 | void DeleteGrowableList(GrowableList* g_list, uintptr_t elem) |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 242 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 243 | bool found = false; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 244 | for (unsigned int i = 0; i < g_list->num_used; i++) { |
| 245 | if (!found && g_list->elem_list[i] == elem) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 246 | found = true; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 247 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 248 | if (found) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 249 | g_list->elem_list[i] = g_list->elem_list[i+1]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | DCHECK_EQ(found, true); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 253 | g_list->num_used--; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 254 | } |
| 255 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 256 | void GrowableListIteratorInit(GrowableList* g_list, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 257 | GrowableListIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 258 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 259 | iterator->list = g_list; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 260 | iterator->idx = 0; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 261 | iterator->size = g_list->num_used; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 262 | } |
| 263 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 264 | uintptr_t GrowableListIteratorNext(GrowableListIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 265 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 266 | DCHECK_EQ(iterator->size, iterator->list->num_used); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 267 | if (iterator->idx == iterator->size) return 0; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 268 | return iterator->list->elem_list[iterator->idx++]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 269 | } |
| 270 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 271 | uintptr_t GrowableListGetElement(const GrowableList* g_list, size_t idx) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 272 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 273 | DCHECK_LT(idx, g_list->num_used); |
| 274 | return g_list->elem_list[idx]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 275 | } |
| 276 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 277 | #ifdef WITH_MEMSTATS |
| 278 | /* Dump memory usage stats */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 279 | void DumpMemStats(CompilationUnit* cu) |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 280 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 281 | uint32_t total = 0; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 282 | for (int i = 0; i < kNumAllocKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 283 | total += cu->mstats->alloc_stats[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 284 | } |
| 285 | if (total > (10 * 1024 * 1024)) { |
| 286 | LOG(INFO) << "MEMUSAGE: " << total << " : " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 287 | << PrettyMethod(cu->method_idx, *cu->dex_file); |
| 288 | LOG(INFO) << "insns_size: " << cu->insns_size; |
| 289 | if (cu->disable_dataflow) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 290 | LOG(INFO) << " ** Dataflow disabled ** "; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 291 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 292 | LOG(INFO) << "===== Overall allocations"; |
| 293 | for (int i = 0; i < kNumAllocKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 294 | LOG(INFO) << alloc_names[i] << std::setw(10) << |
| 295 | cu->mstats->alloc_stats[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 296 | } |
| 297 | LOG(INFO) << "===== GrowableList allocations"; |
| 298 | for (int i = 0; i < kNumListKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 299 | LOG(INFO) << list_names[i] |
| 300 | << " S:" << cu->mstats->list_sizes[i] |
| 301 | << ", W:" << cu->mstats->list_wasted[i] |
| 302 | << ", G:" << cu->mstats->list_grows[i] |
| 303 | << ", E:" << cu->mstats->list_max_elems[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 304 | } |
| 305 | LOG(INFO) << "===== GrowableBitMap allocations"; |
| 306 | for (int i = 0; i < kNumBitMapKinds; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 307 | LOG(INFO) << bit_map_names[i] |
| 308 | << " S:" << cu->mstats->bit_map_sizes[i] |
| 309 | << ", W:" << cu->mstats->bit_map_wasted[i] |
| 310 | << ", G:" << cu->mstats->bit_map_grows[i]; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 311 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 312 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 313 | } |
| 314 | #endif |
| 315 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 316 | /* Debug Utility - dump a compilation unit */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 317 | void DumpCompilationUnit(CompilationUnit* cu) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 318 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 319 | BasicBlock* bb; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 320 | const char* block_type_names[] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 321 | "Entry Block", |
| 322 | "Code Block", |
| 323 | "Exit Block", |
| 324 | "Exception Handling", |
| 325 | "Catch Block" |
| 326 | }; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 327 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 328 | LOG(INFO) << "Compiling " << PrettyMethod(cu->method_idx, *cu->dex_file); |
| 329 | LOG(INFO) << cu->insns << " insns"; |
| 330 | LOG(INFO) << cu->num_blocks << " blocks in total"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 331 | GrowableListIterator iterator; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 332 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 333 | GrowableListIteratorInit(&cu->block_list, &iterator); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 334 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 335 | while (true) { |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 336 | bb = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iterator)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 337 | if (bb == NULL) break; |
| 338 | LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)", |
| 339 | bb->id, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 340 | block_type_names[bb->block_type], |
| 341 | bb->start_offset, |
| 342 | bb->last_mir_insn ? bb->last_mir_insn->offset : bb->start_offset, |
| 343 | bb->last_mir_insn ? "" : " empty"); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 344 | if (bb->taken) { |
| 345 | LOG(INFO) << " Taken branch: block " << bb->taken->id |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 346 | << "(0x" << std::hex << bb->taken->start_offset << ")"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 347 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 348 | if (bb->fall_through) { |
| 349 | LOG(INFO) << " Fallthrough : block " << bb->fall_through->id |
| 350 | << " (0x" << std::hex << bb->fall_through->start_offset << ")"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 353 | } |
| 354 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 355 | static uint32_t check_masks[32] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 356 | 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, |
| 357 | 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200, |
| 358 | 0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000, |
| 359 | 0x00008000, 0x00010000, 0x00020000, 0x00040000, 0x00080000, |
| 360 | 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000, |
| 361 | 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, |
| 362 | 0x40000000, 0x80000000 }; |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 363 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 364 | /* |
| 365 | * Allocate a bit vector with enough space to hold at least the specified |
| 366 | * number of bits. |
| 367 | * |
| 368 | * NOTE: memory is allocated from the compiler arena. |
| 369 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 370 | ArenaBitVector* AllocBitVector(CompilationUnit* cu, |
| 371 | unsigned int start_bits, bool expandable, |
| 372 | oat_bit_map_kind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 373 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 374 | ArenaBitVector* bv; |
| 375 | unsigned int count; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 376 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 377 | DCHECK_EQ(sizeof(bv->storage[0]), 4U); /* assuming 32-bit units */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 378 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 379 | bv = static_cast<ArenaBitVector*>(NewMem(cu, sizeof(ArenaBitVector), false, |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 380 | kAllocGrowableBitMap)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 381 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 382 | count = (start_bits + 31) >> 5; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 383 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 384 | bv->storage_size = count; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 385 | bv->expandable = expandable; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 386 | bv->storage = static_cast<uint32_t*>(NewMem(cu, count * sizeof(uint32_t), true, |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 387 | kAllocGrowableBitMap)); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 388 | #ifdef WITH_MEMSTATS |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 389 | bv->kind = kind; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 390 | cu->mstats->bit_map_sizes[kind] += count * sizeof(uint32_t); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 391 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 392 | return bv; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Determine whether or not the specified bit is set. |
| 397 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 398 | bool IsBitSet(const ArenaBitVector* p_bits, unsigned int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 399 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 400 | DCHECK_LT(num, p_bits->storage_size * sizeof(uint32_t) * 8); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 401 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 402 | unsigned int val = p_bits->storage[num >> 5] & check_masks[num & 0x1f]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 403 | return (val != 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Mark all bits bit as "clear". |
| 408 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 409 | void ClearAllBits(ArenaBitVector* p_bits) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 410 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 411 | unsigned int count = p_bits->storage_size; |
| 412 | memset(p_bits->storage, 0, count * sizeof(uint32_t)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Mark the specified bit as "set". |
| 417 | * |
| 418 | * Returns "false" if the bit is outside the range of the vector and we're |
| 419 | * not allowed to expand. |
| 420 | * |
| 421 | * NOTE: memory is allocated from the compiler arena. |
| 422 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 423 | bool SetBit(CompilationUnit* cu, ArenaBitVector* p_bits, unsigned int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 424 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 425 | if (num >= p_bits->storage_size * sizeof(uint32_t) * 8) { |
| 426 | if (!p_bits->expandable) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 427 | LOG(FATAL) << "Can't expand"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 430 | /* Round up to word boundaries for "num+1" bits */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 431 | unsigned int new_size = (num + 1 + 31) >> 5; |
| 432 | DCHECK_GT(new_size, p_bits->storage_size); |
| 433 | 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] | 434 | kAllocGrowableBitMap)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 435 | memcpy(new_storage, p_bits->storage, p_bits->storage_size * sizeof(uint32_t)); |
| 436 | memset(&new_storage[p_bits->storage_size], 0, |
| 437 | (new_size - p_bits->storage_size) * sizeof(uint32_t)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 438 | #ifdef WITH_MEMSTATS |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 439 | cu->mstats->bit_map_wasted[p_bits->kind] += |
| 440 | p_bits->storage_size * sizeof(uint32_t); |
| 441 | cu->mstats->bit_map_sizes[p_bits->kind] += new_size * sizeof(uint32_t); |
| 442 | cu->mstats->bit_map_grows[p_bits->kind]++; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 443 | #endif |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 444 | p_bits->storage = new_storage; |
| 445 | p_bits->storage_size = new_size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 446 | } |
| 447 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 448 | p_bits->storage[num >> 5] |= check_masks[num & 0x1f]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 449 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Mark the specified bit as "unset". |
| 454 | * |
| 455 | * Returns "false" if the bit is outside the range of the vector and we're |
| 456 | * not allowed to expand. |
| 457 | * |
| 458 | * NOTE: memory is allocated from the compiler arena. |
| 459 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 460 | bool ClearBit(ArenaBitVector* p_bits, unsigned int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 461 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 462 | if (num >= p_bits->storage_size * sizeof(uint32_t) * 8) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 463 | LOG(FATAL) << "Attempt to clear a bit not set in the vector yet";; |
| 464 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 465 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 466 | p_bits->storage[num >> 5] &= ~check_masks[num & 0x1f]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 467 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 468 | } |
| 469 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 470 | /* Initialize the iterator structure */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 471 | void BitVectorIteratorInit(ArenaBitVector* p_bits, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 472 | ArenaBitVectorIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 473 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 474 | iterator->p_bits = p_bits; |
| 475 | iterator->bit_size = p_bits->storage_size * sizeof(uint32_t) * 8; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 476 | iterator->idx = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* |
| 480 | * If the vector sizes don't match, log an error and abort. |
| 481 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 482 | static void CheckSizes(const ArenaBitVector* bv1, const ArenaBitVector* bv2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 483 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 484 | if (bv1->storage_size != bv2->storage_size) { |
| 485 | LOG(FATAL) << "Mismatched vector sizes (" << bv1->storage_size |
| 486 | << ", " << bv2->storage_size << ")"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 487 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Copy a whole vector to the other. Only do that when the both vectors have |
| 492 | * the same size. |
| 493 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 494 | void CopyBitVector(ArenaBitVector* dest, const ArenaBitVector* src) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 495 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 496 | /* if dest is expandable and < src, we could expand dest to match */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 497 | CheckSizes(dest, src); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 498 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 499 | memcpy(dest->storage, src->storage, sizeof(uint32_t) * dest->storage_size); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Intersect two bit vectors and store the result to the dest vector. |
| 504 | */ |
| 505 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 506 | bool IntersectBitVectors(ArenaBitVector* dest, const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 507 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 508 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 509 | DCHECK(src1 != NULL); |
| 510 | DCHECK(src2 != NULL); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 511 | if (dest->storage_size != src1->storage_size || |
| 512 | dest->storage_size != src2->storage_size || |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 513 | dest->expandable != src1->expandable || |
| 514 | dest->expandable != src2->expandable) |
| 515 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 516 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 517 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 518 | for (idx = 0; idx < dest->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 519 | dest->storage[idx] = src1->storage[idx] & src2->storage[idx]; |
| 520 | } |
| 521 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Unify two bit vectors and store the result to the dest vector. |
| 526 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 527 | bool UnifyBitVetors(ArenaBitVector* dest, const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 528 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 529 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 530 | DCHECK(src1 != NULL); |
| 531 | DCHECK(src2 != NULL); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 532 | if (dest->storage_size != src1->storage_size || |
| 533 | dest->storage_size != src2->storage_size || |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 534 | dest->expandable != src1->expandable || |
| 535 | dest->expandable != src2->expandable) |
| 536 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 537 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 538 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 539 | for (idx = 0; idx < dest->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 540 | dest->storage[idx] = src1->storage[idx] | src2->storage[idx]; |
| 541 | } |
| 542 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 546 | * Return true if any bits collide. Vectors must be same size. |
| 547 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 548 | bool TestBitVectors(const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 549 | const ArenaBitVector* src2) |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 550 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 551 | DCHECK_EQ(src1->storage_size, src2->storage_size); |
| 552 | for (uint32_t idx = 0; idx < src1->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 553 | if (src1->storage[idx] & src2->storage[idx]) return true; |
| 554 | } |
| 555 | return false; |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 559 | * Compare two bit vectors and return true if difference is seen. |
| 560 | */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 561 | bool CompareBitVectors(const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 562 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 563 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 564 | if (src1->storage_size != src2->storage_size || |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 565 | src1->expandable != src2->expandable) |
| 566 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 567 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 568 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 569 | for (idx = 0; idx < src1->storage_size; idx++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 570 | if (src1->storage[idx] != src2->storage[idx]) return true; |
| 571 | } |
| 572 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Count the number of bits that are set. |
| 577 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 578 | int CountSetBits(const ArenaBitVector* p_bits) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 579 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 580 | unsigned int word; |
| 581 | unsigned int count = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 582 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 583 | for (word = 0; word < p_bits->storage_size; word++) { |
| 584 | uint32_t val = p_bits->storage[word]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 585 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 586 | if (val != 0) { |
| 587 | if (val == 0xffffffff) { |
| 588 | count += 32; |
| 589 | } else { |
| 590 | /* count the number of '1' bits */ |
| 591 | while (val != 0) { |
| 592 | val &= val - 1; |
| 593 | count++; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 594 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 595 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 596 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 597 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 598 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 599 | return count; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* Return the next position set to 1. -1 means end-of-element reached */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 603 | int BitVectorIteratorNext(ArenaBitVectorIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 604 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 605 | ArenaBitVector* p_bits = iterator->p_bits; |
| 606 | uint32_t bit_index = iterator->idx; |
| 607 | uint32_t bit_size = iterator->bit_size; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 608 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 609 | DCHECK_EQ(bit_size, p_bits->storage_size * sizeof(uint32_t) * 8); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 610 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 611 | if (bit_index >= bit_size) return -1; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 612 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 613 | uint32_t word_index = bit_index >> 5; |
| 614 | uint32_t end_word_index = bit_size >> 5; |
| 615 | uint32_t* storage = p_bits->storage; |
| 616 | uint32_t word = storage[word_index++]; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 617 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 618 | // Mask out any bits in the first word we've already considered |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 619 | word &= ~((1 << (bit_index & 0x1f))-1); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 620 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 621 | for (; word_index <= end_word_index;) { |
| 622 | uint32_t bit_pos = bit_index & 0x1f; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 623 | if (word == 0) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 624 | bit_index += (32 - bit_pos); |
| 625 | word = storage[word_index++]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 626 | continue; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 627 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 628 | for (; bit_pos < 32; bit_pos++) { |
| 629 | if (word & (1 << bit_pos)) { |
| 630 | iterator->idx = bit_index + 1; |
| 631 | return bit_index; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 632 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 633 | bit_index++; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 634 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 635 | word = storage[word_index++]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 636 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 637 | iterator->idx = iterator->bit_size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 638 | return -1; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Mark specified number of bits as "set". Cannot set all bits like ClearAll |
| 643 | * since there might be unused bits - setting those to one will confuse the |
| 644 | * iterator. |
| 645 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 646 | void SetInitialBits(ArenaBitVector* p_bits, unsigned int num_bits) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 647 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 648 | unsigned int idx; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 649 | DCHECK_LE(((num_bits + 31) >> 5), p_bits->storage_size); |
| 650 | for (idx = 0; idx < (num_bits >> 5); idx++) { |
| 651 | p_bits->storage[idx] = -1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 652 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 653 | unsigned int rem_num_bits = num_bits & 0x1f; |
| 654 | if (rem_num_bits) { |
| 655 | p_bits->storage[idx] = (1 << rem_num_bits) - 1; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 656 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 657 | } |
| 658 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 659 | void GetBlockName(BasicBlock* bb, char* name) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 660 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 661 | switch (bb->block_type) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 662 | case kEntryBlock: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 663 | snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 664 | break; |
| 665 | case kExitBlock: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 666 | snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 667 | break; |
| 668 | case kDalvikByteCode: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 669 | snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->start_offset, bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 670 | break; |
| 671 | case kExceptionHandling: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 672 | snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->start_offset, |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 673 | bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 674 | break; |
| 675 | default: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 676 | snprintf(name, BLOCK_NAME_LEN, "??_%d", bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 677 | break; |
| 678 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 679 | } |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 680 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 681 | const char* GetShortyFromTargetIdx(CompilationUnit *cu, int target_idx) |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 682 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 683 | const DexFile::MethodId& method_id = cu->dex_file->GetMethodId(target_idx); |
| 684 | return cu->dex_file->GetShorty(method_id.proto_idx_); |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 685 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 686 | |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 687 | /* Allocate a new basic block */ |
| 688 | BasicBlock* NewMemBB(CompilationUnit* cu, BBType block_type, int block_id) |
| 689 | { |
| 690 | BasicBlock* bb = static_cast<BasicBlock*>(NewMem(cu, sizeof(BasicBlock), true, kAllocBB)); |
| 691 | bb->block_type = block_type; |
| 692 | bb->id = block_id; |
| 693 | bb->predecessors = static_cast<GrowableList*> |
| 694 | (NewMem(cu, sizeof(GrowableList), false, kAllocPredecessors)); |
| 695 | CompilerInitGrowableList(cu, bb->predecessors, |
| 696 | (block_type == kExitBlock) ? 2048 : 2, |
| 697 | kListPredecessors); |
| 698 | cu->block_id_map.Put(block_id, block_id); |
| 699 | return bb; |
| 700 | } |
| 701 | |
| 702 | /* Insert an MIR instruction to the end of a basic block */ |
| 703 | void AppendMIR(BasicBlock* bb, MIR* mir) |
| 704 | { |
| 705 | if (bb->first_mir_insn == NULL) { |
| 706 | DCHECK(bb->last_mir_insn == NULL); |
| 707 | bb->last_mir_insn = bb->first_mir_insn = mir; |
| 708 | mir->prev = mir->next = NULL; |
| 709 | } else { |
| 710 | bb->last_mir_insn->next = mir; |
| 711 | mir->prev = bb->last_mir_insn; |
| 712 | mir->next = NULL; |
| 713 | bb->last_mir_insn = mir; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /* Insert an MIR instruction to the head of a basic block */ |
| 718 | void PrependMIR(BasicBlock* bb, MIR* mir) |
| 719 | { |
| 720 | if (bb->first_mir_insn == NULL) { |
| 721 | DCHECK(bb->last_mir_insn == NULL); |
| 722 | bb->last_mir_insn = bb->first_mir_insn = mir; |
| 723 | mir->prev = mir->next = NULL; |
| 724 | } else { |
| 725 | bb->first_mir_insn->prev = mir; |
| 726 | mir->next = bb->first_mir_insn; |
| 727 | mir->prev = NULL; |
| 728 | bb->first_mir_insn = mir; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* Insert a MIR instruction after the specified MIR */ |
| 733 | void InsertMIRAfter(BasicBlock* bb, MIR* current_mir, MIR* new_mir) |
| 734 | { |
| 735 | new_mir->prev = current_mir; |
| 736 | new_mir->next = current_mir->next; |
| 737 | current_mir->next = new_mir; |
| 738 | |
| 739 | if (new_mir->next) { |
| 740 | /* Is not the last MIR in the block */ |
| 741 | new_mir->next->prev = new_mir; |
| 742 | } else { |
| 743 | /* Is the last MIR in the block */ |
| 744 | bb->last_mir_insn = new_mir; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Append an LIR instruction to the LIR list maintained by a compilation |
| 750 | * unit |
| 751 | */ |
| 752 | void AppendLIR(CompilationUnit *cu, LIR* lir) |
| 753 | { |
| 754 | if (cu->first_lir_insn == NULL) { |
| 755 | DCHECK(cu->last_lir_insn == NULL); |
| 756 | cu->last_lir_insn = cu->first_lir_insn = lir; |
| 757 | lir->prev = lir->next = NULL; |
| 758 | } else { |
| 759 | cu->last_lir_insn->next = lir; |
| 760 | lir->prev = cu->last_lir_insn; |
| 761 | lir->next = NULL; |
| 762 | cu->last_lir_insn = lir; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * Insert an LIR instruction before the current instruction, which cannot be the |
| 768 | * first instruction. |
| 769 | * |
| 770 | * prev_lir <-> new_lir <-> current_lir |
| 771 | */ |
| 772 | void InsertLIRBefore(LIR* current_lir, LIR* new_lir) |
| 773 | { |
| 774 | DCHECK(current_lir->prev != NULL); |
| 775 | LIR *prev_lir = current_lir->prev; |
| 776 | |
| 777 | prev_lir->next = new_lir; |
| 778 | new_lir->prev = prev_lir; |
| 779 | new_lir->next = current_lir; |
| 780 | current_lir->prev = new_lir; |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Insert an LIR instruction after the current instruction, which cannot be the |
| 785 | * first instruction. |
| 786 | * |
| 787 | * current_lir -> new_lir -> old_next |
| 788 | */ |
| 789 | void InsertLIRAfter(LIR* current_lir, LIR* new_lir) |
| 790 | { |
| 791 | new_lir->prev = current_lir; |
| 792 | new_lir->next = current_lir->next; |
| 793 | current_lir->next = new_lir; |
| 794 | new_lir->next->prev = new_lir; |
| 795 | } |
| 796 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 797 | } // namespace art |