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 | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 21 | #ifdef WITH_MEMSTATS |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 22 | struct Memstats { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 23 | uint32_t allocStats[kNumAllocKinds]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 24 | int listSizes[kNumListKinds]; |
| 25 | int listWasted[kNumListKinds]; |
| 26 | int listGrows[kNumListKinds]; |
| 27 | int listMaxElems[kNumListKinds]; |
| 28 | int bitMapSizes[kNumBitMapKinds]; |
| 29 | int bitMapWasted[kNumBitMapKinds]; |
| 30 | int bitMapGrows[kNumBitMapKinds]; |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 31 | }; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 32 | |
| 33 | const char* allocNames[kNumAllocKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 34 | "Misc ", |
| 35 | "BasicBlock ", |
| 36 | "LIR ", |
| 37 | "MIR ", |
| 38 | "DataFlow ", |
| 39 | "GrowList ", |
| 40 | "GrowBitMap ", |
| 41 | "Dalvik2SSA ", |
| 42 | "DebugInfo ", |
| 43 | "Successor ", |
| 44 | "RegAlloc ", |
| 45 | "Data ", |
| 46 | "Preds ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | const char* listNames[kNumListKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 50 | "Misc ", |
| 51 | "blockList ", |
| 52 | "SSAtoDalvik ", |
| 53 | "dfsOrder ", |
| 54 | "dfsPostOrder ", |
| 55 | "domPostOrderTraversal ", |
| 56 | "throwLaunchPads ", |
| 57 | "suspendLaunchPads ", |
| 58 | "switchTables ", |
| 59 | "fillArrayData ", |
| 60 | "SuccessorBlocks ", |
| 61 | "Predecessors ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | const char* bitMapNames[kNumBitMapKinds] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 65 | "Misc ", |
| 66 | "Use ", |
| 67 | "Def ", |
| 68 | "LiveIn ", |
| 69 | "BlockMatrix ", |
| 70 | "Dominators ", |
| 71 | "IDominated ", |
| 72 | "DomFrontier ", |
| 73 | "Phi ", |
| 74 | "TmpBlocks ", |
| 75 | "InputBlocks ", |
| 76 | "RegisterV ", |
| 77 | "TempSSARegisterV ", |
| 78 | "Null Check ", |
| 79 | "TmpBlockV ", |
| 80 | "Predecessors ", |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 81 | }; |
| 82 | #endif |
| 83 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 84 | #define kArenaBitVectorGrowth 4 /* increase by 4 uint32_ts when limit hit */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 85 | |
| 86 | /* Allocate the initial memory block for arena-based allocation */ |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 87 | bool oatHeapInit(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 88 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 89 | DCHECK(cUnit->arenaHead == NULL); |
| 90 | cUnit->arenaHead = |
| 91 | (ArenaMemBlock *) malloc(sizeof(ArenaMemBlock) + ARENA_DEFAULT_SIZE); |
| 92 | if (cUnit->arenaHead == NULL) { |
| 93 | LOG(FATAL) << "No memory left to create compiler heap memory"; |
| 94 | } |
| 95 | cUnit->arenaHead->blockSize = ARENA_DEFAULT_SIZE; |
| 96 | cUnit->currentArena = cUnit->arenaHead; |
| 97 | cUnit->currentArena->bytesAllocated = 0; |
| 98 | cUnit->currentArena->next = NULL; |
| 99 | cUnit->numArenaBlocks = 1; |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 100 | #ifdef WITH_MEMSTATS |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 101 | cUnit->mstats = (Memstats*) oatNew(cUnit, sizeof(Memstats), true, |
| 102 | kAllocDebugInfo); |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 103 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 104 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Arena-based malloc for compilation tasks */ |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 108 | void* oatNew(CompilationUnit* cUnit, size_t size, bool zero, oatAllocKind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 109 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 110 | size = (size + 3) & ~3; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 111 | #ifdef WITH_MEMSTATS |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 112 | if (cUnit->mstats != NULL) { |
| 113 | cUnit->mstats->allocStats[kind] += size; |
| 114 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 115 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 116 | retry: |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 117 | /* Normal case - space is available in the current page */ |
| 118 | if (size + cUnit->currentArena->bytesAllocated <= |
| 119 | cUnit->currentArena->blockSize) { |
| 120 | void *ptr; |
| 121 | ptr = &cUnit->currentArena->ptr[cUnit->currentArena->bytesAllocated]; |
| 122 | cUnit->currentArena->bytesAllocated += size; |
| 123 | if (zero) { |
| 124 | memset(ptr, 0, size); |
| 125 | } |
| 126 | return ptr; |
| 127 | } else { |
| 128 | /* |
| 129 | * See if there are previously allocated arena blocks before the last |
| 130 | * reset |
| 131 | */ |
| 132 | if (cUnit->currentArena->next) { |
| 133 | cUnit->currentArena = cUnit->currentArena->next; |
| 134 | cUnit->currentArena->bytesAllocated = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 135 | goto retry; |
| 136 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 137 | |
| 138 | size_t blockSize = (size < ARENA_DEFAULT_SIZE) ? ARENA_DEFAULT_SIZE : size; |
| 139 | /* Time to allocate a new arena */ |
| 140 | ArenaMemBlock *newArena = (ArenaMemBlock *) |
| 141 | malloc(sizeof(ArenaMemBlock) + blockSize); |
| 142 | if (newArena == NULL) { |
| 143 | LOG(FATAL) << "Arena allocation failure"; |
| 144 | } |
| 145 | newArena->blockSize = blockSize; |
| 146 | newArena->bytesAllocated = 0; |
| 147 | newArena->next = NULL; |
| 148 | cUnit->currentArena->next = newArena; |
| 149 | cUnit->currentArena = newArena; |
| 150 | cUnit->numArenaBlocks++; |
| 151 | if (cUnit->numArenaBlocks > 20000) { |
| 152 | LOG(INFO) << "Total arena pages: " << cUnit->numArenaBlocks; |
| 153 | } |
| 154 | goto retry; |
| 155 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* Reclaim all the arena blocks allocated so far */ |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 159 | void oatArenaReset(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 160 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 161 | ArenaMemBlock* head = cUnit->arenaHead; |
| 162 | while (head != NULL) { |
| 163 | ArenaMemBlock* p = head; |
| 164 | head = head->next; |
| 165 | free(p); |
| 166 | } |
| 167 | cUnit->arenaHead = NULL; |
| 168 | cUnit->currentArena = NULL; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* Growable List initialization */ |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 172 | void oatInitGrowableList(CompilationUnit* cUnit, GrowableList* gList, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 173 | size_t initLength, oatListKind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 174 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 175 | gList->numAllocated = initLength; |
| 176 | gList->numUsed = 0; |
| 177 | gList->elemList = (intptr_t *) oatNew(cUnit, sizeof(intptr_t) * initLength, |
| 178 | true, kAllocGrowableList); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 179 | #ifdef WITH_MEMSTATS |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 180 | cUnit->mstats->listSizes[kind] += sizeof(intptr_t) * initLength; |
| 181 | gList->kind = kind; |
| 182 | if ((int)initLength > cUnit->mstats->listMaxElems[kind]) { |
| 183 | cUnit->mstats->listMaxElems[kind] = initLength; |
| 184 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 185 | #endif |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* Expand the capacity of a growable list */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 189 | void expandGrowableList(CompilationUnit* cUnit, GrowableList* gList) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 190 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 191 | int newLength = gList->numAllocated; |
| 192 | if (newLength < 128) { |
| 193 | newLength <<= 1; |
| 194 | } else { |
| 195 | newLength += 128; |
| 196 | } |
| 197 | intptr_t *newArray = |
| 198 | (intptr_t *) oatNew(cUnit, sizeof(intptr_t) * newLength, true, |
| 199 | kAllocGrowableList); |
| 200 | memcpy(newArray, gList->elemList, sizeof(intptr_t) * gList->numAllocated); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 201 | #ifdef WITH_MEMSTATS |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 202 | cUnit->mstats->listSizes[gList->kind] += sizeof(intptr_t) * newLength; |
| 203 | cUnit->mstats->listWasted[gList->kind] += |
| 204 | sizeof(intptr_t) * gList->numAllocated; |
| 205 | cUnit->mstats->listGrows[gList->kind]++; |
| 206 | if (newLength > cUnit->mstats->listMaxElems[gList->kind]) { |
| 207 | cUnit->mstats->listMaxElems[gList->kind] = newLength; |
| 208 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 209 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 210 | gList->numAllocated = newLength; |
| 211 | gList->elemList = newArray; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* Insert a new element into the growable list */ |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 215 | void oatInsertGrowableList(CompilationUnit* cUnit, GrowableList* gList, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 216 | intptr_t elem) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 217 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 218 | DCHECK_NE(gList->numAllocated, 0U); |
| 219 | if (gList->numUsed == gList->numAllocated) { |
| 220 | expandGrowableList(cUnit, gList); |
| 221 | } |
| 222 | gList->elemList[gList->numUsed++] = elem; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 223 | } |
| 224 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 225 | /* Delete an element from a growable list. Element must be present */ |
| 226 | void oatDeleteGrowableList(GrowableList* gList, intptr_t elem) |
| 227 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 228 | bool found = false; |
| 229 | for (unsigned int i = 0; i < gList->numUsed; i++) { |
| 230 | if (!found && gList->elemList[i] == elem) { |
| 231 | found = true; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 232 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 233 | if (found) { |
| 234 | gList->elemList[i] = gList->elemList[i+1]; |
| 235 | } |
| 236 | } |
| 237 | DCHECK_EQ(found, true); |
| 238 | gList->numUsed--; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 239 | } |
| 240 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 241 | void oatGrowableListIteratorInit(GrowableList* gList, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 242 | GrowableListIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 243 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 244 | iterator->list = gList; |
| 245 | iterator->idx = 0; |
| 246 | iterator->size = gList->numUsed; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | intptr_t oatGrowableListIteratorNext(GrowableListIterator* iterator) |
| 250 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 251 | DCHECK_EQ(iterator->size, iterator->list->numUsed); |
| 252 | if (iterator->idx == iterator->size) return 0; |
| 253 | return iterator->list->elemList[iterator->idx++]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | intptr_t oatGrowableListGetElement(const GrowableList* gList, size_t idx) |
| 257 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 258 | DCHECK_LT(idx, gList->numUsed); |
| 259 | return gList->elemList[idx]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 260 | } |
| 261 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 262 | #ifdef WITH_MEMSTATS |
| 263 | /* Dump memory usage stats */ |
| 264 | void oatDumpMemStats(CompilationUnit* cUnit) |
| 265 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 266 | uint32_t total = 0; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 267 | for (int i = 0; i < kNumAllocKinds; i++) { |
| 268 | total += cUnit->mstats->allocStats[i]; |
| 269 | } |
| 270 | if (total > (10 * 1024 * 1024)) { |
| 271 | LOG(INFO) << "MEMUSAGE: " << total << " : " |
| 272 | << PrettyMethod(cUnit->method_idx, *cUnit->dex_file); |
| 273 | LOG(INFO) << "insnsSize: " << cUnit->insnsSize; |
| 274 | if (cUnit->disableDataflow) { |
| 275 | LOG(INFO) << " ** Dataflow disabled ** "; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 276 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 277 | LOG(INFO) << "===== Overall allocations"; |
| 278 | for (int i = 0; i < kNumAllocKinds; i++) { |
| 279 | LOG(INFO) << allocNames[i] << std::setw(10) << |
| 280 | cUnit->mstats->allocStats[i]; |
| 281 | } |
| 282 | LOG(INFO) << "===== GrowableList allocations"; |
| 283 | for (int i = 0; i < kNumListKinds; i++) { |
| 284 | LOG(INFO) << listNames[i] |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 285 | << " S:" << cUnit->mstats->listSizes[i] |
| 286 | << ", W:" << cUnit->mstats->listWasted[i] |
| 287 | << ", G:" << cUnit->mstats->listGrows[i] |
| 288 | << ", E:" << cUnit->mstats->listMaxElems[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 289 | } |
| 290 | LOG(INFO) << "===== GrowableBitMap allocations"; |
| 291 | for (int i = 0; i < kNumBitMapKinds; i++) { |
| 292 | LOG(INFO) << bitMapNames[i] |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 293 | << " S:" << cUnit->mstats->bitMapSizes[i] |
| 294 | << ", W:" << cUnit->mstats->bitMapWasted[i] |
| 295 | << ", G:" << cUnit->mstats->bitMapGrows[i]; |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 296 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 297 | } |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 298 | } |
| 299 | #endif |
| 300 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 301 | /* Debug Utility - dump a compilation unit */ |
| 302 | void oatDumpCompilationUnit(CompilationUnit* cUnit) |
| 303 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 304 | BasicBlock* bb; |
| 305 | const char* blockTypeNames[] = { |
| 306 | "Entry Block", |
| 307 | "Code Block", |
| 308 | "Exit Block", |
| 309 | "Exception Handling", |
| 310 | "Catch Block" |
| 311 | }; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 312 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 313 | LOG(INFO) << "Compiling " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file); |
| 314 | LOG(INFO) << cUnit->insns << " insns"; |
| 315 | LOG(INFO) << cUnit->numBlocks << " blocks in total"; |
| 316 | GrowableListIterator iterator; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 317 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 318 | oatGrowableListIteratorInit(&cUnit->blockList, &iterator); |
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 | while (true) { |
| 321 | bb = (BasicBlock *) oatGrowableListIteratorNext(&iterator); |
| 322 | if (bb == NULL) break; |
| 323 | LOG(INFO) << StringPrintf("Block %d (%s) (insn %04x - %04x%s)", |
| 324 | bb->id, |
| 325 | blockTypeNames[bb->blockType], |
| 326 | bb->startOffset, |
| 327 | bb->lastMIRInsn ? bb->lastMIRInsn->offset : bb->startOffset, |
| 328 | bb->lastMIRInsn ? "" : " empty"); |
| 329 | if (bb->taken) { |
| 330 | LOG(INFO) << " Taken branch: block " << bb->taken->id |
| 331 | << "(0x" << std::hex << bb->taken->startOffset << ")"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 332 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 333 | if (bb->fallThrough) { |
| 334 | LOG(INFO) << " Fallthrough : block " << bb->fallThrough->id |
| 335 | << " (0x" << std::hex << bb->fallThrough->startOffset << ")"; |
| 336 | } |
| 337 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 338 | } |
| 339 | |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 340 | static uint32_t checkMasks[32] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 341 | 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, |
| 342 | 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200, |
| 343 | 0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000, |
| 344 | 0x00008000, 0x00010000, 0x00020000, 0x00040000, 0x00080000, |
| 345 | 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000, |
| 346 | 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, |
| 347 | 0x40000000, 0x80000000 }; |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 348 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 349 | /* |
| 350 | * Allocate a bit vector with enough space to hold at least the specified |
| 351 | * number of bits. |
| 352 | * |
| 353 | * NOTE: memory is allocated from the compiler arena. |
| 354 | */ |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 355 | ArenaBitVector* oatAllocBitVector(CompilationUnit* cUnit, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 356 | unsigned int startBits, bool expandable, |
| 357 | oatBitMapKind kind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 358 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 359 | ArenaBitVector* bv; |
| 360 | unsigned int count; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 361 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 362 | DCHECK_EQ(sizeof(bv->storage[0]), 4U); /* assuming 32-bit units */ |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 363 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 364 | bv = (ArenaBitVector*) oatNew(cUnit, sizeof(ArenaBitVector), false, |
| 365 | kAllocGrowableBitMap); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 366 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 367 | count = (startBits + 31) >> 5; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 368 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 369 | bv->storageSize = count; |
| 370 | bv->expandable = expandable; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 371 | bv->storage = static_cast<uint32_t*>(oatNew(cUnit, count * sizeof(uint32_t), true, |
| 372 | kAllocGrowableBitMap)); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 373 | #ifdef WITH_MEMSTATS |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 374 | bv->kind = kind; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 375 | cUnit->mstats->bitMapSizes[kind] += count * sizeof(uint32_t); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 376 | #endif |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 377 | return bv; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Determine whether or not the specified bit is set. |
| 382 | */ |
| 383 | bool oatIsBitSet(const ArenaBitVector* pBits, unsigned int num) |
| 384 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 385 | DCHECK_LT(num, pBits->storageSize * sizeof(uint32_t) * 8); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 386 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 387 | unsigned int val = pBits->storage[num >> 5] & checkMasks[num & 0x1f]; |
| 388 | return (val != 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Mark all bits bit as "clear". |
| 393 | */ |
| 394 | void oatClearAllBits(ArenaBitVector* pBits) |
| 395 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 396 | unsigned int count = pBits->storageSize; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 397 | memset(pBits->storage, 0, count * sizeof(uint32_t)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Mark the specified bit as "set". |
| 402 | * |
| 403 | * Returns "false" if the bit is outside the range of the vector and we're |
| 404 | * not allowed to expand. |
| 405 | * |
| 406 | * NOTE: memory is allocated from the compiler arena. |
| 407 | */ |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 408 | bool oatSetBit(CompilationUnit* cUnit, ArenaBitVector* pBits, unsigned int num) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 409 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 410 | if (num >= pBits->storageSize * sizeof(uint32_t) * 8) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 411 | if (!pBits->expandable) { |
| 412 | LOG(FATAL) << "Can't expand"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 415 | /* Round up to word boundaries for "num+1" bits */ |
| 416 | unsigned int newSize = (num + 1 + 31) >> 5; |
| 417 | DCHECK_GT(newSize, pBits->storageSize); |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 418 | uint32_t *newStorage = static_cast<uint32_t*>(oatNew(cUnit, newSize * sizeof(uint32_t), false, |
| 419 | kAllocGrowableBitMap)); |
| 420 | memcpy(newStorage, pBits->storage, pBits->storageSize * sizeof(uint32_t)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 421 | memset(&newStorage[pBits->storageSize], 0, |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 422 | (newSize - pBits->storageSize) * sizeof(uint32_t)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 423 | #ifdef WITH_MEMSTATS |
| 424 | cUnit->mstats->bitMapWasted[pBits->kind] += |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 425 | pBits->storageSize * sizeof(uint32_t); |
| 426 | cUnit->mstats->bitMapSizes[pBits->kind] += newSize * sizeof(uint32_t); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 427 | cUnit->mstats->bitMapGrows[pBits->kind]++; |
| 428 | #endif |
| 429 | pBits->storage = newStorage; |
| 430 | pBits->storageSize = newSize; |
| 431 | } |
| 432 | |
| 433 | pBits->storage[num >> 5] |= checkMasks[num & 0x1f]; |
| 434 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Mark the specified bit as "unset". |
| 439 | * |
| 440 | * Returns "false" if the bit is outside the range of the vector and we're |
| 441 | * not allowed to expand. |
| 442 | * |
| 443 | * NOTE: memory is allocated from the compiler arena. |
| 444 | */ |
| 445 | bool oatClearBit(ArenaBitVector* pBits, unsigned int num) |
| 446 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 447 | if (num >= pBits->storageSize * sizeof(uint32_t) * 8) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 448 | LOG(FATAL) << "Attempt to clear a bit not set in the vector yet";; |
| 449 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 450 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 451 | pBits->storage[num >> 5] &= ~checkMasks[num & 0x1f]; |
| 452 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* |
| 456 | * If set is true, mark all bits as 1. Otherwise mark all bits as 0. |
| 457 | */ |
| 458 | void oatMarkAllBits(ArenaBitVector* pBits, bool set) |
| 459 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 460 | int value = set ? -1 : 0; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 461 | memset(pBits->storage, value, pBits->storageSize * static_cast<int>(sizeof(uint32_t))); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void oatDebugBitVector(char* msg, const ArenaBitVector* bv, int length) |
| 465 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 466 | int i; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 467 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 468 | LOG(INFO) << msg; |
| 469 | for (i = 0; i < length; i++) { |
| 470 | if (oatIsBitSet(bv, i)) { |
| 471 | LOG(INFO) << " Bit " << i << " is set"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 472 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 473 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | void oatAbort(CompilationUnit* cUnit) |
| 477 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 478 | LOG(FATAL) << "Compiler aborting"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void oatDumpBlockBitVector(const GrowableList* blocks, char* msg, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 482 | const ArenaBitVector* bv, int length) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 483 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 484 | int i; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 485 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 486 | LOG(INFO) << msg; |
| 487 | for (i = 0; i < length; i++) { |
| 488 | if (oatIsBitSet(bv, i)) { |
| 489 | BasicBlock *bb = (BasicBlock *) oatGrowableListGetElement(blocks, i); |
| 490 | char blockName[BLOCK_NAME_LEN]; |
| 491 | oatGetBlockName(bb, blockName); |
| 492 | LOG(INFO) << "Bit " << i << " / " << blockName << " is set"; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 493 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 494 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 495 | } |
| 496 | /* Initialize the iterator structure */ |
| 497 | void oatBitVectorIteratorInit(ArenaBitVector* pBits, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 498 | ArenaBitVectorIterator* iterator) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 499 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 500 | iterator->pBits = pBits; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 501 | iterator->bitSize = pBits->storageSize * sizeof(uint32_t) * 8; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 502 | iterator->idx = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
| 506 | * If the vector sizes don't match, log an error and abort. |
| 507 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 508 | void checkSizes(const ArenaBitVector* bv1, const ArenaBitVector* bv2) |
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 | if (bv1->storageSize != bv2->storageSize) { |
| 511 | LOG(FATAL) << "Mismatched vector sizes (" << bv1->storageSize |
| 512 | << ", " << bv2->storageSize << ")"; |
| 513 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Copy a whole vector to the other. Only do that when the both vectors have |
| 518 | * the same size. |
| 519 | */ |
| 520 | void oatCopyBitVector(ArenaBitVector* dest, const ArenaBitVector* src) |
| 521 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 522 | /* if dest is expandable and < src, we could expand dest to match */ |
| 523 | checkSizes(dest, src); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 524 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 525 | memcpy(dest->storage, src->storage, sizeof(uint32_t) * dest->storageSize); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Intersect two bit vectors and store the result to the dest vector. |
| 530 | */ |
| 531 | |
| 532 | bool oatIntersectBitVectors(ArenaBitVector* dest, const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 533 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 534 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 535 | DCHECK(src1 != NULL); |
| 536 | DCHECK(src2 != NULL); |
| 537 | if (dest->storageSize != src1->storageSize || |
| 538 | dest->storageSize != src2->storageSize || |
| 539 | dest->expandable != src1->expandable || |
| 540 | dest->expandable != src2->expandable) |
| 541 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 542 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 543 | unsigned int idx; |
| 544 | for (idx = 0; idx < dest->storageSize; idx++) { |
| 545 | dest->storage[idx] = src1->storage[idx] & src2->storage[idx]; |
| 546 | } |
| 547 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Unify two bit vectors and store the result to the dest vector. |
| 552 | */ |
| 553 | bool oatUnifyBitVectors(ArenaBitVector* dest, const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 554 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 555 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 556 | DCHECK(src1 != NULL); |
| 557 | DCHECK(src2 != NULL); |
| 558 | if (dest->storageSize != src1->storageSize || |
| 559 | dest->storageSize != src2->storageSize || |
| 560 | dest->expandable != src1->expandable || |
| 561 | dest->expandable != src2->expandable) |
| 562 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 563 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 564 | unsigned int idx; |
| 565 | for (idx = 0; idx < dest->storageSize; idx++) { |
| 566 | dest->storage[idx] = src1->storage[idx] | src2->storage[idx]; |
| 567 | } |
| 568 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 572 | * Return true if any bits collide. Vectors must be same size. |
| 573 | */ |
| 574 | bool oatTestBitVectors(const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 575 | const ArenaBitVector* src2) |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 576 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 577 | DCHECK_EQ(src1->storageSize, src2->storageSize); |
| 578 | for (uint32_t idx = 0; idx < src1->storageSize; idx++) { |
| 579 | if (src1->storage[idx] & src2->storage[idx]) return true; |
| 580 | } |
| 581 | return false; |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 585 | * Compare two bit vectors and return true if difference is seen. |
| 586 | */ |
| 587 | bool oatCompareBitVectors(const ArenaBitVector* src1, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 588 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 589 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 590 | if (src1->storageSize != src2->storageSize || |
| 591 | src1->expandable != src2->expandable) |
| 592 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 593 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 594 | unsigned int idx; |
| 595 | for (idx = 0; idx < src1->storageSize; idx++) { |
| 596 | if (src1->storage[idx] != src2->storage[idx]) return true; |
| 597 | } |
| 598 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Count the number of bits that are set. |
| 603 | */ |
| 604 | int oatCountSetBits(const ArenaBitVector* pBits) |
| 605 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 606 | unsigned int word; |
| 607 | unsigned int count = 0; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 608 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 609 | for (word = 0; word < pBits->storageSize; word++) { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 610 | uint32_t val = pBits->storage[word]; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 611 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 612 | if (val != 0) { |
| 613 | if (val == 0xffffffff) { |
| 614 | count += 32; |
| 615 | } else { |
| 616 | /* count the number of '1' bits */ |
| 617 | while (val != 0) { |
| 618 | val &= val - 1; |
| 619 | count++; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 620 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 621 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 622 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 623 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 624 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 625 | return count; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* Return the next position set to 1. -1 means end-of-element reached */ |
| 629 | int oatBitVectorIteratorNext(ArenaBitVectorIterator* iterator) |
| 630 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 631 | ArenaBitVector* pBits = iterator->pBits; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 632 | uint32_t bitIndex = iterator->idx; |
| 633 | uint32_t bitSize = iterator->bitSize; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 634 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 635 | DCHECK_EQ(bitSize, pBits->storageSize * sizeof(uint32_t) * 8); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 636 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 637 | if (bitIndex >= bitSize) return -1; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 638 | |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 639 | uint32_t wordIndex = bitIndex >> 5; |
| 640 | uint32_t endWordIndex = bitSize >> 5; |
| 641 | uint32_t* storage = pBits->storage; |
| 642 | uint32_t word = storage[wordIndex++]; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 643 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 644 | // Mask out any bits in the first word we've already considered |
| 645 | word &= ~((1 << (bitIndex & 0x1f))-1); |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 646 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 647 | for (; wordIndex <= endWordIndex;) { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame^] | 648 | uint32_t bitPos = bitIndex & 0x1f; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 649 | if (word == 0) { |
| 650 | bitIndex += (32 - bitPos); |
| 651 | word = storage[wordIndex++]; |
| 652 | continue; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 653 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 654 | for (; bitPos < 32; bitPos++) { |
| 655 | if (word & (1 << bitPos)) { |
| 656 | iterator->idx = bitIndex + 1; |
| 657 | return bitIndex; |
| 658 | } |
| 659 | bitIndex++; |
| 660 | } |
| 661 | word = storage[wordIndex++]; |
| 662 | } |
| 663 | iterator->idx = iterator->bitSize; |
| 664 | return -1; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* |
| 668 | * Mark specified number of bits as "set". Cannot set all bits like ClearAll |
| 669 | * since there might be unused bits - setting those to one will confuse the |
| 670 | * iterator. |
| 671 | */ |
| 672 | void oatSetInitialBits(ArenaBitVector* pBits, unsigned int numBits) |
| 673 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 674 | unsigned int idx; |
| 675 | DCHECK_LE(((numBits + 31) >> 5), pBits->storageSize); |
| 676 | for (idx = 0; idx < (numBits >> 5); idx++) { |
| 677 | pBits->storage[idx] = -1; |
| 678 | } |
| 679 | unsigned int remNumBits = numBits & 0x1f; |
| 680 | if (remNumBits) { |
| 681 | pBits->storage[idx] = (1 << remNumBits) - 1; |
| 682 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | void oatGetBlockName(BasicBlock* bb, char* name) |
| 686 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 687 | switch (bb->blockType) { |
| 688 | case kEntryBlock: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 689 | snprintf(name, BLOCK_NAME_LEN, "entry_%d", bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 690 | break; |
| 691 | case kExitBlock: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 692 | snprintf(name, BLOCK_NAME_LEN, "exit_%d", bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 693 | break; |
| 694 | case kDalvikByteCode: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 695 | snprintf(name, BLOCK_NAME_LEN, "block%04x_%d", bb->startOffset, bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 696 | break; |
| 697 | case kExceptionHandling: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 698 | snprintf(name, BLOCK_NAME_LEN, "exception%04x_%d", bb->startOffset, |
| 699 | bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 700 | break; |
| 701 | default: |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 702 | snprintf(name, BLOCK_NAME_LEN, "??_%d", bb->id); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 703 | break; |
| 704 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 705 | } |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 706 | |
| 707 | const char* oatGetShortyFromTargetIdx(CompilationUnit *cUnit, int targetIdx) |
| 708 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 709 | const DexFile::MethodId& methodId = cUnit->dex_file->GetMethodId(targetIdx); |
| 710 | return cUnit->dex_file->GetShorty(methodId.proto_idx_); |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 711 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 712 | |
| 713 | } // namespace art |