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