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