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