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 | |
| 17 | #include "Dalvik.h" |
| 18 | #include "Dataflow.h" |
| 19 | |
| 20 | /* Enter the node to the dfsOrder list then visit its successors */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 21 | STATIC void recordDFSPreOrder(CompilationUnit* cUnit, BasicBlock* block) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 22 | { |
| 23 | |
| 24 | if (block->visited || block->hidden) return; |
| 25 | block->visited = true; |
| 26 | |
| 27 | /* Enqueue the block id */ |
| 28 | oatInsertGrowableList(&cUnit->dfsOrder, block->id); |
| 29 | |
| 30 | if (block->fallThrough) recordDFSPreOrder(cUnit, block->fallThrough); |
| 31 | if (block->taken) recordDFSPreOrder(cUnit, block->taken); |
| 32 | if (block->successorBlockList.blockListType != kNotUsed) { |
| 33 | GrowableListIterator iterator; |
| 34 | oatGrowableListIteratorInit(&block->successorBlockList.blocks, |
| 35 | &iterator); |
| 36 | while (true) { |
| 37 | SuccessorBlockInfo *successorBlockInfo = |
| 38 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 39 | if (successorBlockInfo == NULL) break; |
| 40 | BasicBlock* succBB = successorBlockInfo->block; |
| 41 | recordDFSPreOrder(cUnit, succBB); |
| 42 | } |
| 43 | } |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | /* Sort the blocks by the Depth-First-Search pre-order */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 48 | STATIC void computeDFSOrder(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 49 | { |
| 50 | /* Initialize or reset the DFS order list */ |
| 51 | if (cUnit->dfsOrder.elemList == NULL) { |
| 52 | oatInitGrowableList(&cUnit->dfsOrder, cUnit->numBlocks); |
| 53 | } else { |
| 54 | /* Just reset the used length on the counter */ |
| 55 | cUnit->dfsOrder.numUsed = 0; |
| 56 | } |
| 57 | |
| 58 | oatDataFlowAnalysisDispatcher(cUnit, oatClearVisitedFlag, |
| 59 | kAllNodes, |
| 60 | false /* isIterative */); |
| 61 | |
| 62 | recordDFSPreOrder(cUnit, cUnit->entryBlock); |
| 63 | cUnit->numReachableBlocks = cUnit->dfsOrder.numUsed; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Mark block bit on the per-Dalvik register vector to denote that Dalvik |
| 68 | * register idx is defined in BasicBlock bb. |
| 69 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 70 | STATIC bool fillDefBlockMatrix(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 71 | { |
| 72 | if (bb->dataFlowInfo == NULL) return false; |
| 73 | |
| 74 | ArenaBitVectorIterator iterator; |
| 75 | |
| 76 | oatBitVectorIteratorInit(bb->dataFlowInfo->defV, &iterator); |
| 77 | while (true) { |
| 78 | int idx = oatBitVectorIteratorNext(&iterator); |
| 79 | if (idx == -1) break; |
| 80 | /* Block bb defines register idx */ |
| 81 | oatSetBit(cUnit->defBlockMatrix[idx], bb->id); |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 86 | STATIC void computeDefBlockMatrix(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 87 | { |
| 88 | int numRegisters = cUnit->numDalvikRegisters; |
| 89 | /* Allocate numDalvikRegisters bit vector pointers */ |
| 90 | cUnit->defBlockMatrix = (ArenaBitVector **) |
| 91 | oatNew(sizeof(ArenaBitVector *) * numRegisters, true); |
| 92 | int i; |
| 93 | |
| 94 | /* Initialize numRegister vectors with numBlocks bits each */ |
| 95 | for (i = 0; i < numRegisters; i++) { |
| 96 | cUnit->defBlockMatrix[i] = oatAllocBitVector(cUnit->numBlocks, |
| 97 | false); |
| 98 | } |
| 99 | oatDataFlowAnalysisDispatcher(cUnit, oatFindLocalLiveIn, |
| 100 | kAllNodes, |
| 101 | false /* isIterative */); |
| 102 | oatDataFlowAnalysisDispatcher(cUnit, fillDefBlockMatrix, |
| 103 | kAllNodes, |
| 104 | false /* isIterative */); |
| 105 | |
| 106 | /* |
| 107 | * Also set the incoming parameters as defs in the entry block. |
| 108 | * Only need to handle the parameters for the outer method. |
| 109 | */ |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 110 | int inReg = cUnit->method->NumRegisters() - cUnit->method->NumIns(); |
| 111 | for (; inReg < cUnit->method->NumRegisters(); inReg++) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 112 | oatSetBit(cUnit->defBlockMatrix[inReg], |
| 113 | cUnit->entryBlock->id); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* Compute the post-order traversal of the CFG */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 118 | STATIC void computeDomPostOrderTraversal(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 119 | { |
| 120 | ArenaBitVectorIterator bvIterator; |
| 121 | oatBitVectorIteratorInit(bb->iDominated, &bvIterator); |
| 122 | GrowableList* blockList = &cUnit->blockList; |
| 123 | |
| 124 | /* Iterate through the dominated blocks first */ |
| 125 | while (true) { |
| 126 | int bbIdx = oatBitVectorIteratorNext(&bvIterator); |
| 127 | if (bbIdx == -1) break; |
| 128 | BasicBlock* dominatedBB = |
| 129 | (BasicBlock* ) oatGrowableListGetElement(blockList, bbIdx); |
| 130 | computeDomPostOrderTraversal(cUnit, dominatedBB); |
| 131 | } |
| 132 | |
| 133 | /* Enter the current block id */ |
| 134 | oatInsertGrowableList(&cUnit->domPostOrderTraversal, bb->id); |
| 135 | |
| 136 | /* hacky loop detection */ |
| 137 | if (bb->taken && oatIsBitSet(bb->dominators, bb->taken->id)) { |
| 138 | cUnit->hasLoop = true; |
| 139 | } |
| 140 | } |
| 141 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 142 | STATIC void checkForDominanceFrontier(BasicBlock* domBB, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 143 | const BasicBlock* succBB) |
| 144 | { |
| 145 | /* |
| 146 | * TODO - evaluate whether phi will ever need to be inserted into exit |
| 147 | * blocks. |
| 148 | */ |
| 149 | if (succBB->iDom != domBB && |
| 150 | succBB->blockType == kDalvikByteCode && |
| 151 | succBB->hidden == false) { |
| 152 | oatSetBit(domBB->domFrontier, succBB->id); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* Worker function to compute the dominance frontier */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 157 | STATIC bool computeDominanceFrontier(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 158 | { |
| 159 | GrowableList* blockList = &cUnit->blockList; |
| 160 | |
| 161 | /* Calculate DF_local */ |
| 162 | if (bb->taken) { |
| 163 | checkForDominanceFrontier(bb, bb->taken); |
| 164 | } |
| 165 | if (bb->fallThrough) { |
| 166 | checkForDominanceFrontier(bb, bb->fallThrough); |
| 167 | } |
| 168 | if (bb->successorBlockList.blockListType != kNotUsed) { |
| 169 | GrowableListIterator iterator; |
| 170 | oatGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 171 | &iterator); |
| 172 | while (true) { |
| 173 | SuccessorBlockInfo *successorBlockInfo = |
| 174 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 175 | if (successorBlockInfo == NULL) break; |
| 176 | BasicBlock* succBB = successorBlockInfo->block; |
| 177 | checkForDominanceFrontier(bb, succBB); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* Calculate DF_up */ |
| 182 | ArenaBitVectorIterator bvIterator; |
| 183 | oatBitVectorIteratorInit(bb->iDominated, &bvIterator); |
| 184 | while (true) { |
| 185 | int dominatedIdx = oatBitVectorIteratorNext(&bvIterator); |
| 186 | if (dominatedIdx == -1) break; |
| 187 | BasicBlock* dominatedBB = (BasicBlock* ) |
| 188 | oatGrowableListGetElement(blockList, dominatedIdx); |
| 189 | ArenaBitVectorIterator dfIterator; |
| 190 | oatBitVectorIteratorInit(dominatedBB->domFrontier, &dfIterator); |
| 191 | while (true) { |
| 192 | int dfUpIdx = oatBitVectorIteratorNext(&dfIterator); |
| 193 | if (dfUpIdx == -1) break; |
| 194 | BasicBlock* dfUpBlock = (BasicBlock* ) |
| 195 | oatGrowableListGetElement(blockList, dfUpIdx); |
| 196 | checkForDominanceFrontier(bb, dfUpBlock); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | /* Worker function for initializing domination-related data structures */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 204 | STATIC bool initializeDominationInfo(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 205 | { |
| 206 | int numTotalBlocks = cUnit->blockList.numUsed; |
| 207 | |
| 208 | if (bb->dominators == NULL ) { |
| 209 | bb->dominators = oatAllocBitVector(numTotalBlocks, |
| 210 | false /* expandable */); |
| 211 | bb->iDominated = oatAllocBitVector(numTotalBlocks, |
| 212 | false /* expandable */); |
| 213 | bb->domFrontier = oatAllocBitVector(numTotalBlocks, |
| 214 | false /* expandable */); |
| 215 | } else { |
| 216 | oatClearAllBits(bb->dominators); |
| 217 | oatClearAllBits(bb->iDominated); |
| 218 | oatClearAllBits(bb->domFrontier); |
| 219 | } |
| 220 | /* Set all bits in the dominator vector */ |
| 221 | oatSetInitialBits(bb->dominators, numTotalBlocks); |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | /* Worker function to compute each block's dominators */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 227 | STATIC bool computeBlockDominators(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 228 | { |
| 229 | GrowableList* blockList = &cUnit->blockList; |
| 230 | int numTotalBlocks = blockList->numUsed; |
| 231 | ArenaBitVector* tempBlockV = cUnit->tempBlockV; |
| 232 | ArenaBitVectorIterator bvIterator; |
| 233 | |
| 234 | /* |
| 235 | * The dominator of the entry block has been preset to itself and we need |
| 236 | * to skip the calculation here. |
| 237 | */ |
| 238 | if (bb == cUnit->entryBlock) return false; |
| 239 | |
| 240 | oatSetInitialBits(tempBlockV, numTotalBlocks); |
| 241 | |
| 242 | /* Iterate through the predecessors */ |
| 243 | oatBitVectorIteratorInit(bb->predecessors, &bvIterator); |
| 244 | while (true) { |
| 245 | int predIdx = oatBitVectorIteratorNext(&bvIterator); |
| 246 | if (predIdx == -1) break; |
| 247 | BasicBlock* predBB = (BasicBlock* ) oatGrowableListGetElement( |
| 248 | blockList, predIdx); |
| 249 | /* tempBlockV = tempBlockV ^ dominators */ |
buzbee | aad7201 | 2011-09-21 21:52:09 -0700 | [diff] [blame] | 250 | if (predBB->dominators != NULL) { |
| 251 | oatIntersectBitVectors(tempBlockV, tempBlockV, predBB->dominators); |
| 252 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 253 | } |
| 254 | oatSetBit(tempBlockV, bb->id); |
| 255 | if (oatCompareBitVectors(tempBlockV, bb->dominators)) { |
| 256 | oatCopyBitVector(bb->dominators, tempBlockV); |
| 257 | return true; |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /* Worker function to compute the idom */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 263 | STATIC bool computeImmediateDominator(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 264 | { |
| 265 | GrowableList* blockList = &cUnit->blockList; |
| 266 | ArenaBitVector* tempBlockV = cUnit->tempBlockV; |
| 267 | ArenaBitVectorIterator bvIterator; |
| 268 | BasicBlock* iDom; |
| 269 | |
| 270 | if (bb == cUnit->entryBlock) return false; |
| 271 | |
| 272 | oatCopyBitVector(tempBlockV, bb->dominators); |
| 273 | oatClearBit(tempBlockV, bb->id); |
| 274 | oatBitVectorIteratorInit(tempBlockV, &bvIterator); |
| 275 | |
| 276 | /* Should not see any dead block */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 277 | DCHECK_NE(oatCountSetBits(tempBlockV), 0); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 278 | if (oatCountSetBits(tempBlockV) == 1) { |
| 279 | iDom = (BasicBlock* ) oatGrowableListGetElement( |
| 280 | blockList, oatBitVectorIteratorNext(&bvIterator)); |
| 281 | bb->iDom = iDom; |
| 282 | } else { |
| 283 | int iDomIdx = oatBitVectorIteratorNext(&bvIterator); |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 284 | DCHECK_NE(iDomIdx, -1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 285 | while (true) { |
| 286 | int nextDom = oatBitVectorIteratorNext(&bvIterator); |
| 287 | if (nextDom == -1) break; |
| 288 | BasicBlock* nextDomBB = (BasicBlock* ) |
| 289 | oatGrowableListGetElement(blockList, nextDom); |
| 290 | /* iDom dominates nextDom - set new iDom */ |
| 291 | if (oatIsBitSet(nextDomBB->dominators, iDomIdx)) { |
| 292 | iDomIdx = nextDom; |
| 293 | } |
| 294 | |
| 295 | } |
| 296 | iDom = (BasicBlock* ) oatGrowableListGetElement(blockList, iDomIdx); |
| 297 | /* Set the immediate dominator block for bb */ |
| 298 | bb->iDom = iDom; |
| 299 | } |
| 300 | /* Add bb to the iDominated set of the immediate dominator block */ |
| 301 | oatSetBit(iDom->iDominated, bb->id); |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | /* Compute dominators, immediate dominator, and dominance fronter */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 306 | STATIC void computeDominators(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 307 | { |
| 308 | int numReachableBlocks = cUnit->numReachableBlocks; |
| 309 | int numTotalBlocks = cUnit->blockList.numUsed; |
| 310 | |
| 311 | /* Initialize domination-related data structures */ |
| 312 | oatDataFlowAnalysisDispatcher(cUnit, initializeDominationInfo, |
| 313 | kReachableNodes, |
| 314 | false /* isIterative */); |
| 315 | |
| 316 | /* Set the dominator for the root node */ |
| 317 | oatClearAllBits(cUnit->entryBlock->dominators); |
| 318 | oatSetBit(cUnit->entryBlock->dominators, cUnit->entryBlock->id); |
| 319 | |
| 320 | if (cUnit->tempBlockV == NULL) { |
| 321 | cUnit->tempBlockV = oatAllocBitVector(numTotalBlocks, |
| 322 | false /* expandable */); |
| 323 | } else { |
| 324 | oatClearAllBits(cUnit->tempBlockV); |
| 325 | } |
| 326 | oatDataFlowAnalysisDispatcher(cUnit, computeBlockDominators, |
| 327 | kPreOrderDFSTraversal, |
| 328 | true /* isIterative */); |
| 329 | |
| 330 | cUnit->entryBlock->iDom = NULL; |
| 331 | oatDataFlowAnalysisDispatcher(cUnit, computeImmediateDominator, |
| 332 | kReachableNodes, |
| 333 | false /* isIterative */); |
| 334 | |
| 335 | /* |
| 336 | * Now go ahead and compute the post order traversal based on the |
| 337 | * iDominated sets. |
| 338 | */ |
| 339 | if (cUnit->domPostOrderTraversal.elemList == NULL) { |
| 340 | oatInitGrowableList(&cUnit->domPostOrderTraversal, numReachableBlocks); |
| 341 | } else { |
| 342 | cUnit->domPostOrderTraversal.numUsed = 0; |
| 343 | } |
| 344 | |
| 345 | computeDomPostOrderTraversal(cUnit, cUnit->entryBlock); |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 346 | DCHECK_EQ(cUnit->domPostOrderTraversal.numUsed, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 347 | (unsigned) cUnit->numReachableBlocks); |
| 348 | |
| 349 | /* Now compute the dominance frontier for each block */ |
| 350 | oatDataFlowAnalysisDispatcher(cUnit, computeDominanceFrontier, |
| 351 | kPostOrderDOMTraversal, |
| 352 | false /* isIterative */); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Perform dest U= src1 ^ ~src2 |
| 357 | * This is probably not general enough to be placed in BitVector.[ch]. |
| 358 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 359 | STATIC void computeSuccLiveIn(ArenaBitVector* dest, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 360 | const ArenaBitVector* src1, |
| 361 | const ArenaBitVector* src2) |
| 362 | { |
| 363 | if (dest->storageSize != src1->storageSize || |
| 364 | dest->storageSize != src2->storageSize || |
| 365 | dest->expandable != src1->expandable || |
| 366 | dest->expandable != src2->expandable) { |
| 367 | LOG(FATAL) << "Incompatible set properties"; |
| 368 | } |
| 369 | |
| 370 | unsigned int idx; |
| 371 | for (idx = 0; idx < dest->storageSize; idx++) { |
| 372 | dest->storage[idx] |= src1->storage[idx] & ~src2->storage[idx]; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Iterate through all successor blocks and propagate up the live-in sets. |
| 378 | * The calculated result is used for phi-node pruning - where we only need to |
| 379 | * insert a phi node if the variable is live-in to the block. |
| 380 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 381 | STATIC bool computeBlockLiveIns(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 382 | { |
| 383 | ArenaBitVector* tempDalvikRegisterV = cUnit->tempDalvikRegisterV; |
| 384 | |
| 385 | if (bb->dataFlowInfo == NULL) return false; |
| 386 | oatCopyBitVector(tempDalvikRegisterV, bb->dataFlowInfo->liveInV); |
| 387 | if (bb->taken && bb->taken->dataFlowInfo) |
| 388 | computeSuccLiveIn(tempDalvikRegisterV, bb->taken->dataFlowInfo->liveInV, |
| 389 | bb->dataFlowInfo->defV); |
| 390 | if (bb->fallThrough && bb->fallThrough->dataFlowInfo) |
| 391 | computeSuccLiveIn(tempDalvikRegisterV, |
| 392 | bb->fallThrough->dataFlowInfo->liveInV, |
| 393 | bb->dataFlowInfo->defV); |
| 394 | if (bb->successorBlockList.blockListType != kNotUsed) { |
| 395 | GrowableListIterator iterator; |
| 396 | oatGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 397 | &iterator); |
| 398 | while (true) { |
| 399 | SuccessorBlockInfo *successorBlockInfo = |
| 400 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 401 | if (successorBlockInfo == NULL) break; |
| 402 | BasicBlock* succBB = successorBlockInfo->block; |
| 403 | if (succBB->dataFlowInfo) { |
| 404 | computeSuccLiveIn(tempDalvikRegisterV, |
| 405 | succBB->dataFlowInfo->liveInV, |
| 406 | bb->dataFlowInfo->defV); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | if (oatCompareBitVectors(tempDalvikRegisterV, bb->dataFlowInfo->liveInV)) { |
| 411 | oatCopyBitVector(bb->dataFlowInfo->liveInV, tempDalvikRegisterV); |
| 412 | return true; |
| 413 | } |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | /* Insert phi nodes to for each variable to the dominance frontiers */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 418 | STATIC void insertPhiNodes(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 419 | { |
| 420 | int dalvikReg; |
| 421 | const GrowableList* blockList = &cUnit->blockList; |
| 422 | ArenaBitVector* phiBlocks = |
| 423 | oatAllocBitVector(cUnit->numBlocks, false); |
| 424 | ArenaBitVector* tmpBlocks = |
| 425 | oatAllocBitVector(cUnit->numBlocks, false); |
| 426 | ArenaBitVector* inputBlocks = |
| 427 | oatAllocBitVector(cUnit->numBlocks, false); |
| 428 | |
| 429 | cUnit->tempDalvikRegisterV = |
| 430 | oatAllocBitVector(cUnit->numDalvikRegisters, false); |
| 431 | |
| 432 | oatDataFlowAnalysisDispatcher(cUnit, computeBlockLiveIns, |
| 433 | kPostOrderDFSTraversal, |
| 434 | true /* isIterative */); |
| 435 | |
| 436 | /* Iterate through each Dalvik register */ |
| 437 | for (dalvikReg = 0; dalvikReg < cUnit->numDalvikRegisters; dalvikReg++) { |
| 438 | bool change; |
| 439 | ArenaBitVectorIterator iterator; |
| 440 | |
| 441 | oatCopyBitVector(inputBlocks, cUnit->defBlockMatrix[dalvikReg]); |
| 442 | oatClearAllBits(phiBlocks); |
| 443 | |
| 444 | /* Calculate the phi blocks for each Dalvik register */ |
| 445 | do { |
| 446 | change = false; |
| 447 | oatClearAllBits(tmpBlocks); |
| 448 | oatBitVectorIteratorInit(inputBlocks, &iterator); |
| 449 | |
| 450 | while (true) { |
| 451 | int idx = oatBitVectorIteratorNext(&iterator); |
| 452 | if (idx == -1) break; |
| 453 | BasicBlock* defBB = |
| 454 | (BasicBlock* ) oatGrowableListGetElement(blockList, idx); |
| 455 | |
| 456 | /* Merge the dominance frontier to tmpBlocks */ |
buzbee | aad7201 | 2011-09-21 21:52:09 -0700 | [diff] [blame] | 457 | if (defBB->domFrontier != NULL) { |
| 458 | oatUnifyBitVectors(tmpBlocks, tmpBlocks, defBB->domFrontier); |
| 459 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 460 | } |
| 461 | if (oatCompareBitVectors(phiBlocks, tmpBlocks)) { |
| 462 | change = true; |
| 463 | oatCopyBitVector(phiBlocks, tmpBlocks); |
| 464 | |
| 465 | /* |
| 466 | * Iterate through the original blocks plus the new ones in |
| 467 | * the dominance frontier. |
| 468 | */ |
| 469 | oatCopyBitVector(inputBlocks, phiBlocks); |
| 470 | oatUnifyBitVectors(inputBlocks, inputBlocks, |
| 471 | cUnit->defBlockMatrix[dalvikReg]); |
| 472 | } |
| 473 | } while (change); |
| 474 | |
| 475 | /* |
| 476 | * Insert a phi node for dalvikReg in the phiBlocks if the Dalvik |
| 477 | * register is in the live-in set. |
| 478 | */ |
| 479 | oatBitVectorIteratorInit(phiBlocks, &iterator); |
| 480 | while (true) { |
| 481 | int idx = oatBitVectorIteratorNext(&iterator); |
| 482 | if (idx == -1) break; |
| 483 | BasicBlock* phiBB = |
| 484 | (BasicBlock* ) oatGrowableListGetElement(blockList, idx); |
| 485 | /* Variable will be clobbered before being used - no need for phi */ |
| 486 | if (!oatIsBitSet(phiBB->dataFlowInfo->liveInV, dalvikReg)) continue; |
| 487 | MIR *phi = (MIR *) oatNew(sizeof(MIR), true); |
| 488 | phi->dalvikInsn.opcode = (Opcode)kMirOpPhi; |
| 489 | phi->dalvikInsn.vA = dalvikReg; |
| 490 | phi->offset = phiBB->startOffset; |
buzbee | c0ecd65 | 2011-09-25 18:11:54 -0700 | [diff] [blame^] | 491 | phi->meta.phiNext = cUnit->phiList; |
| 492 | cUnit->phiList = phi; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 493 | oatPrependMIR(phiBB, phi); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Worker function to insert phi-operands with latest SSA names from |
| 500 | * predecessor blocks |
| 501 | */ |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 502 | STATIC bool insertPhiNodeOperands(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 503 | { |
| 504 | ArenaBitVector* ssaRegV = cUnit->tempSSARegisterV; |
| 505 | ArenaBitVectorIterator bvIterator; |
| 506 | GrowableList* blockList = &cUnit->blockList; |
| 507 | MIR *mir; |
| 508 | |
| 509 | /* Phi nodes are at the beginning of each block */ |
| 510 | for (mir = bb->firstMIRInsn; mir; mir = mir->next) { |
| 511 | if (mir->dalvikInsn.opcode != (Opcode)kMirOpPhi) |
| 512 | return true; |
| 513 | int ssaReg = mir->ssaRep->defs[0]; |
| 514 | int encodedDalvikValue = |
| 515 | (int) oatGrowableListGetElement(cUnit->ssaToDalvikMap, ssaReg); |
| 516 | int dalvikReg = DECODE_REG(encodedDalvikValue); |
| 517 | |
| 518 | oatClearAllBits(ssaRegV); |
| 519 | |
| 520 | /* Iterate through the predecessors */ |
| 521 | oatBitVectorIteratorInit(bb->predecessors, &bvIterator); |
| 522 | while (true) { |
| 523 | int predIdx = oatBitVectorIteratorNext(&bvIterator); |
| 524 | if (predIdx == -1) break; |
| 525 | BasicBlock* predBB = (BasicBlock* ) oatGrowableListGetElement( |
| 526 | blockList, predIdx); |
| 527 | int encodedSSAValue = |
| 528 | predBB->dataFlowInfo->dalvikToSSAMap[dalvikReg]; |
| 529 | int ssaReg = DECODE_REG(encodedSSAValue); |
| 530 | oatSetBit(ssaRegV, ssaReg); |
| 531 | } |
| 532 | |
| 533 | /* Count the number of SSA registers for a Dalvik register */ |
| 534 | int numUses = oatCountSetBits(ssaRegV); |
| 535 | mir->ssaRep->numUses = numUses; |
| 536 | mir->ssaRep->uses = |
| 537 | (int *) oatNew(sizeof(int) * numUses, false); |
| 538 | mir->ssaRep->fpUse = |
| 539 | (bool *) oatNew(sizeof(bool) * numUses, true); |
| 540 | |
| 541 | ArenaBitVectorIterator phiIterator; |
| 542 | |
| 543 | oatBitVectorIteratorInit(ssaRegV, &phiIterator); |
| 544 | int *usePtr = mir->ssaRep->uses; |
| 545 | |
| 546 | /* Set the uses array for the phi node */ |
| 547 | while (true) { |
| 548 | int ssaRegIdx = oatBitVectorIteratorNext(&phiIterator); |
| 549 | if (ssaRegIdx == -1) break; |
| 550 | *usePtr++ = ssaRegIdx; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | return true; |
| 555 | } |
| 556 | |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 557 | STATIC void doDFSPreOrderSSARename(CompilationUnit* cUnit, BasicBlock* block) |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 558 | { |
| 559 | |
| 560 | if (block->visited || block->hidden) return; |
| 561 | block->visited = true; |
| 562 | |
| 563 | /* Process this block */ |
| 564 | oatDoSSAConversion(cUnit, block); |
| 565 | int mapSize = sizeof(int) * cUnit->method->NumRegisters(); |
| 566 | |
| 567 | /* Save SSA map snapshot */ |
| 568 | int* savedSSAMap = (int*)oatNew(mapSize, false); |
| 569 | memcpy(savedSSAMap, cUnit->dalvikToSSAMap, mapSize); |
| 570 | |
| 571 | if (block->fallThrough) { |
| 572 | doDFSPreOrderSSARename(cUnit, block->fallThrough); |
| 573 | /* Restore SSA map snapshot */ |
| 574 | memcpy(cUnit->dalvikToSSAMap, savedSSAMap, mapSize); |
| 575 | } |
| 576 | if (block->taken) { |
| 577 | doDFSPreOrderSSARename(cUnit, block->taken); |
| 578 | /* Restore SSA map snapshot */ |
| 579 | memcpy(cUnit->dalvikToSSAMap, savedSSAMap, mapSize); |
| 580 | } |
| 581 | if (block->successorBlockList.blockListType != kNotUsed) { |
| 582 | GrowableListIterator iterator; |
| 583 | oatGrowableListIteratorInit(&block->successorBlockList.blocks, |
| 584 | &iterator); |
| 585 | while (true) { |
| 586 | SuccessorBlockInfo *successorBlockInfo = |
| 587 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 588 | if (successorBlockInfo == NULL) break; |
| 589 | BasicBlock* succBB = successorBlockInfo->block; |
| 590 | doDFSPreOrderSSARename(cUnit, succBB); |
| 591 | /* Restore SSA map snapshot */ |
| 592 | memcpy(cUnit->dalvikToSSAMap, savedSSAMap, mapSize); |
| 593 | } |
| 594 | } |
| 595 | cUnit->dalvikToSSAMap = savedSSAMap; |
| 596 | return; |
| 597 | } |
| 598 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 599 | /* Perform SSA transformation for the whole method */ |
| 600 | void oatMethodSSATransformation(CompilationUnit* cUnit) |
| 601 | { |
| 602 | /* Compute the DFS order */ |
| 603 | computeDFSOrder(cUnit); |
| 604 | |
| 605 | /* Compute the dominator info */ |
| 606 | computeDominators(cUnit); |
| 607 | |
| 608 | /* Allocate data structures in preparation for SSA conversion */ |
| 609 | oatInitializeSSAConversion(cUnit); |
| 610 | |
| 611 | /* Find out the "Dalvik reg def x block" relation */ |
| 612 | computeDefBlockMatrix(cUnit); |
| 613 | |
| 614 | /* Insert phi nodes to dominance frontiers for all variables */ |
| 615 | insertPhiNodes(cUnit); |
| 616 | |
| 617 | /* Rename register names by local defs and phi nodes */ |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 618 | oatDataFlowAnalysisDispatcher(cUnit, oatClearVisitedFlag, |
| 619 | kAllNodes, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 620 | false /* isIterative */); |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 621 | doDFSPreOrderSSARename(cUnit, cUnit->entryBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * Shared temp bit vector used by each block to count the number of defs |
| 625 | * from all the predecessor blocks. |
| 626 | */ |
| 627 | cUnit->tempSSARegisterV = oatAllocBitVector(cUnit->numSSARegs, |
| 628 | false); |
| 629 | |
| 630 | /* Insert phi-operands with latest SSA names from predecessor blocks */ |
| 631 | oatDataFlowAnalysisDispatcher(cUnit, insertPhiNodeOperands, |
| 632 | kReachableNodes, |
| 633 | false /* isIterative */); |
| 634 | } |