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 | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 23 | void recordDFSOrders(CompilationUnit* cUnit, BasicBlock* block) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 24 | { |
| 25 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 26 | if (block->visited || block->hidden) return; |
| 27 | block->visited = true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 28 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 29 | // Can this block be reached only via previous block fallthrough? |
| 30 | if ((block->blockType == kDalvikByteCode) && |
| 31 | (block->predecessors->numUsed == 1)) { |
| 32 | DCHECK_GE(cUnit->dfsOrder.numUsed, 1U); |
| 33 | int prevIdx = cUnit->dfsOrder.numUsed - 1; |
| 34 | int prevId = cUnit->dfsOrder.elemList[prevIdx]; |
| 35 | BasicBlock* predBB = (BasicBlock*)block->predecessors->elemList[0]; |
| 36 | if (predBB->id == prevId) { |
| 37 | block->fallThroughTarget = true; |
buzbee | 3d66194 | 2012-03-14 17:37:27 -0700 | [diff] [blame] | 38 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 39 | } |
buzbee | 3d66194 | 2012-03-14 17:37:27 -0700 | [diff] [blame] | 40 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 41 | /* Enqueue the preOrder block id */ |
| 42 | oatInsertGrowableList(cUnit, &cUnit->dfsOrder, block->id); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 43 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 44 | if (block->fallThrough) { |
| 45 | recordDFSOrders(cUnit, block->fallThrough); |
| 46 | } |
| 47 | if (block->taken) recordDFSOrders(cUnit, block->taken); |
| 48 | if (block->successorBlockList.blockListType != kNotUsed) { |
| 49 | GrowableListIterator iterator; |
| 50 | oatGrowableListIteratorInit(&block->successorBlockList.blocks, |
| 51 | &iterator); |
| 52 | while (true) { |
| 53 | SuccessorBlockInfo *successorBlockInfo = |
| 54 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 55 | if (successorBlockInfo == NULL) break; |
| 56 | BasicBlock* succBB = successorBlockInfo->block; |
| 57 | recordDFSOrders(cUnit, succBB); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame] | 58 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 59 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 60 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 61 | /* Record postorder in basic block and enqueue normal id in dfsPostOrder */ |
| 62 | block->dfsId = cUnit->dfsPostOrder.numUsed; |
| 63 | oatInsertGrowableList(cUnit, &cUnit->dfsPostOrder, block->id); |
| 64 | return; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 65 | } |
| 66 | |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 67 | /* Sort the blocks by the Depth-First-Search */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 68 | void computeDFSOrders(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 69 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 70 | /* Initialize or reset the DFS preOrder list */ |
| 71 | if (cUnit->dfsOrder.elemList == NULL) { |
| 72 | oatInitGrowableList(cUnit, &cUnit->dfsOrder, cUnit->numBlocks, |
| 73 | kListDfsOrder); |
| 74 | } else { |
| 75 | /* Just reset the used length on the counter */ |
| 76 | cUnit->dfsOrder.numUsed = 0; |
| 77 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 78 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 79 | /* Initialize or reset the DFS postOrder list */ |
| 80 | if (cUnit->dfsPostOrder.elemList == NULL) { |
| 81 | oatInitGrowableList(cUnit, &cUnit->dfsPostOrder, cUnit->numBlocks, |
| 82 | kListDfsPostOrder); |
| 83 | } else { |
| 84 | /* Just reset the used length on the counter */ |
| 85 | cUnit->dfsPostOrder.numUsed = 0; |
| 86 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 87 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 88 | oatDataFlowAnalysisDispatcher(cUnit, oatClearVisitedFlag, |
| 89 | kAllNodes, false /* isIterative */); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 90 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 91 | recordDFSOrders(cUnit, cUnit->entryBlock); |
| 92 | cUnit->numReachableBlocks = cUnit->dfsOrder.numUsed; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Mark block bit on the per-Dalvik register vector to denote that Dalvik |
| 97 | * register idx is defined in BasicBlock bb. |
| 98 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 99 | bool fillDefBlockMatrix(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 100 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 101 | if (bb->dataFlowInfo == NULL) return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 102 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 103 | ArenaBitVectorIterator iterator; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 104 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 105 | oatBitVectorIteratorInit(bb->dataFlowInfo->defV, &iterator); |
| 106 | while (true) { |
| 107 | int idx = oatBitVectorIteratorNext(&iterator); |
| 108 | if (idx == -1) break; |
| 109 | /* Block bb defines register idx */ |
| 110 | oatSetBit(cUnit, cUnit->defBlockMatrix[idx], bb->id); |
| 111 | } |
| 112 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 113 | } |
| 114 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 115 | void computeDefBlockMatrix(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 116 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 117 | int numRegisters = cUnit->numDalvikRegisters; |
| 118 | /* Allocate numDalvikRegisters bit vector pointers */ |
| 119 | cUnit->defBlockMatrix = (ArenaBitVector **) |
| 120 | oatNew(cUnit, sizeof(ArenaBitVector *) * numRegisters, true, |
| 121 | kAllocDFInfo); |
| 122 | int i; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 123 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 124 | /* Initialize numRegister vectors with numBlocks bits each */ |
| 125 | for (i = 0; i < numRegisters; i++) { |
| 126 | cUnit->defBlockMatrix[i] = oatAllocBitVector(cUnit, cUnit->numBlocks, |
| 127 | false, kBitMapBMatrix); |
| 128 | } |
| 129 | oatDataFlowAnalysisDispatcher(cUnit, oatFindLocalLiveIn, |
| 130 | kAllNodes, false /* isIterative */); |
| 131 | oatDataFlowAnalysisDispatcher(cUnit, fillDefBlockMatrix, |
| 132 | kAllNodes, false /* isIterative */); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 133 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Also set the incoming parameters as defs in the entry block. |
| 136 | * Only need to handle the parameters for the outer method. |
| 137 | */ |
| 138 | int numRegs = cUnit->numDalvikRegisters; |
| 139 | int inReg = numRegs - cUnit->numIns; |
| 140 | for (; inReg < numRegs; inReg++) { |
| 141 | oatSetBit(cUnit, cUnit->defBlockMatrix[inReg], cUnit->entryBlock->id); |
| 142 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Compute the post-order traversal of the CFG */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 146 | void computeDomPostOrderTraversal(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 147 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 148 | ArenaBitVectorIterator bvIterator; |
| 149 | oatBitVectorIteratorInit(bb->iDominated, &bvIterator); |
| 150 | GrowableList* blockList = &cUnit->blockList; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 151 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 152 | /* Iterate through the dominated blocks first */ |
| 153 | while (true) { |
| 154 | //TUNING: hot call to oatBitVectorIteratorNext |
| 155 | int bbIdx = oatBitVectorIteratorNext(&bvIterator); |
| 156 | if (bbIdx == -1) break; |
| 157 | BasicBlock* dominatedBB = |
| 158 | (BasicBlock* ) oatGrowableListGetElement(blockList, bbIdx); |
| 159 | computeDomPostOrderTraversal(cUnit, dominatedBB); |
| 160 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 161 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 162 | /* Enter the current block id */ |
| 163 | oatInsertGrowableList(cUnit, &cUnit->domPostOrderTraversal, bb->id); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 164 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 165 | /* hacky loop detection */ |
| 166 | if (bb->taken && oatIsBitSet(bb->dominators, bb->taken->id)) { |
| 167 | cUnit->hasLoop = true; |
| 168 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 169 | } |
| 170 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 171 | void checkForDominanceFrontier(CompilationUnit* cUnit, BasicBlock* domBB, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 172 | const BasicBlock* succBB) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 173 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 174 | /* |
| 175 | * TODO - evaluate whether phi will ever need to be inserted into exit |
| 176 | * blocks. |
| 177 | */ |
| 178 | if (succBB->iDom != domBB && |
| 179 | succBB->blockType == kDalvikByteCode && |
| 180 | succBB->hidden == false) { |
| 181 | oatSetBit(cUnit, domBB->domFrontier, succBB->id); |
| 182 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* Worker function to compute the dominance frontier */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 186 | bool computeDominanceFrontier(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 187 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 188 | GrowableList* blockList = &cUnit->blockList; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 189 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 190 | /* Calculate DF_local */ |
| 191 | if (bb->taken) { |
| 192 | checkForDominanceFrontier(cUnit, bb, bb->taken); |
| 193 | } |
| 194 | if (bb->fallThrough) { |
| 195 | checkForDominanceFrontier(cUnit, bb, bb->fallThrough); |
| 196 | } |
| 197 | if (bb->successorBlockList.blockListType != kNotUsed) { |
| 198 | GrowableListIterator iterator; |
| 199 | oatGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 200 | &iterator); |
| 201 | while (true) { |
| 202 | SuccessorBlockInfo *successorBlockInfo = |
| 203 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 204 | if (successorBlockInfo == NULL) break; |
| 205 | BasicBlock* succBB = successorBlockInfo->block; |
| 206 | checkForDominanceFrontier(cUnit, bb, succBB); |
| 207 | } |
| 208 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 209 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 210 | /* Calculate DF_up */ |
| 211 | ArenaBitVectorIterator bvIterator; |
| 212 | oatBitVectorIteratorInit(bb->iDominated, &bvIterator); |
| 213 | while (true) { |
| 214 | //TUNING: hot call to oatBitVectorIteratorNext |
| 215 | int dominatedIdx = oatBitVectorIteratorNext(&bvIterator); |
| 216 | if (dominatedIdx == -1) break; |
| 217 | BasicBlock* dominatedBB = (BasicBlock* ) |
| 218 | oatGrowableListGetElement(blockList, dominatedIdx); |
| 219 | ArenaBitVectorIterator dfIterator; |
| 220 | oatBitVectorIteratorInit(dominatedBB->domFrontier, &dfIterator); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 221 | while (true) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 222 | //TUNING: hot call to oatBitVectorIteratorNext |
| 223 | int dfUpIdx = oatBitVectorIteratorNext(&dfIterator); |
| 224 | if (dfUpIdx == -1) break; |
| 225 | BasicBlock* dfUpBlock = (BasicBlock* ) |
| 226 | oatGrowableListGetElement(blockList, dfUpIdx); |
| 227 | checkForDominanceFrontier(cUnit, bb, dfUpBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 228 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 229 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 230 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 231 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Worker function for initializing domination-related data structures */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 235 | bool initializeDominationInfo(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 236 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 237 | int numTotalBlocks = cUnit->blockList.numUsed; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 238 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 239 | if (bb->dominators == NULL ) { |
| 240 | bb->dominators = oatAllocBitVector(cUnit, numTotalBlocks, |
| 241 | false /* expandable */, |
| 242 | kBitMapDominators); |
| 243 | bb->iDominated = oatAllocBitVector(cUnit, numTotalBlocks, |
| 244 | false /* expandable */, |
| 245 | kBitMapIDominated); |
| 246 | bb->domFrontier = oatAllocBitVector(cUnit, numTotalBlocks, |
| 247 | false /* expandable */, |
| 248 | kBitMapDomFrontier); |
| 249 | } else { |
| 250 | oatClearAllBits(bb->dominators); |
| 251 | oatClearAllBits(bb->iDominated); |
| 252 | oatClearAllBits(bb->domFrontier); |
| 253 | } |
| 254 | /* Set all bits in the dominator vector */ |
| 255 | oatSetInitialBits(bb->dominators, numTotalBlocks); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 256 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 257 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 258 | } |
| 259 | |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 260 | /* |
| 261 | * Worker function to compute each block's dominators. This implementation |
| 262 | * is only used when kDebugVerifyDataflow is active and should compute |
| 263 | * the same dominator sets as computeBlockDominators. |
| 264 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 265 | bool slowComputeBlockDominators(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 266 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 267 | GrowableList* blockList = &cUnit->blockList; |
| 268 | int numTotalBlocks = blockList->numUsed; |
| 269 | ArenaBitVector* tempBlockV = cUnit->tempBlockV; |
| 270 | GrowableListIterator iter; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 271 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 272 | /* |
| 273 | * The dominator of the entry block has been preset to itself and we need |
| 274 | * to skip the calculation here. |
| 275 | */ |
| 276 | if (bb == cUnit->entryBlock) return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 277 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 278 | oatSetInitialBits(tempBlockV, numTotalBlocks); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 279 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 280 | /* Iterate through the predecessors */ |
| 281 | oatGrowableListIteratorInit(bb->predecessors, &iter); |
| 282 | while (true) { |
| 283 | BasicBlock* predBB = (BasicBlock*)oatGrowableListIteratorNext(&iter); |
| 284 | if (!predBB) break; |
| 285 | /* tempBlockV = tempBlockV ^ dominators */ |
| 286 | if (predBB->dominators != NULL) { |
| 287 | oatIntersectBitVectors(tempBlockV, tempBlockV, predBB->dominators); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 288 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 289 | } |
| 290 | oatSetBit(cUnit, tempBlockV, bb->id); |
| 291 | if (oatCompareBitVectors(tempBlockV, bb->dominators)) { |
| 292 | oatCopyBitVector(bb->dominators, tempBlockV); |
| 293 | return true; |
| 294 | } |
| 295 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 296 | } |
| 297 | |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 298 | /* |
| 299 | * Worker function to compute the idom. This implementation is only |
| 300 | * used when kDebugVerifyDataflow is active and should compute the |
| 301 | * same iDom as computeBlockIDom. |
| 302 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 303 | bool slowComputeBlockIDom(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 304 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 305 | GrowableList* blockList = &cUnit->blockList; |
| 306 | ArenaBitVector* tempBlockV = cUnit->tempBlockV; |
| 307 | ArenaBitVectorIterator bvIterator; |
| 308 | BasicBlock* iDom; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 309 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 310 | if (bb == cUnit->entryBlock) return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 311 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 312 | oatCopyBitVector(tempBlockV, bb->dominators); |
| 313 | oatClearBit(tempBlockV, bb->id); |
| 314 | oatBitVectorIteratorInit(tempBlockV, &bvIterator); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 315 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 316 | /* Should not see any dead block */ |
| 317 | DCHECK_NE(oatCountSetBits(tempBlockV), 0); |
| 318 | if (oatCountSetBits(tempBlockV) == 1) { |
| 319 | iDom = (BasicBlock* ) |
| 320 | oatGrowableListGetElement(blockList, |
| 321 | oatBitVectorIteratorNext(&bvIterator)); |
| 322 | bb->iDom = iDom; |
| 323 | } else { |
| 324 | int iDomIdx = oatBitVectorIteratorNext(&bvIterator); |
| 325 | DCHECK_NE(iDomIdx, -1); |
| 326 | while (true) { |
| 327 | int nextDom = oatBitVectorIteratorNext(&bvIterator); |
| 328 | if (nextDom == -1) break; |
| 329 | BasicBlock* nextDomBB = (BasicBlock* ) |
| 330 | oatGrowableListGetElement(blockList, nextDom); |
| 331 | /* iDom dominates nextDom - set new iDom */ |
| 332 | if (oatIsBitSet(nextDomBB->dominators, iDomIdx)) { |
| 333 | iDomIdx = nextDom; |
| 334 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 335 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 336 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 337 | iDom = (BasicBlock* ) oatGrowableListGetElement(blockList, iDomIdx); |
| 338 | /* Set the immediate dominator block for bb */ |
| 339 | bb->iDom = iDom; |
| 340 | } |
| 341 | /* Add bb to the iDominated set of the immediate dominator block */ |
| 342 | oatSetBit(cUnit, iDom->iDominated, bb->id); |
| 343 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 344 | } |
| 345 | |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 346 | /* |
| 347 | * Walk through the ordered iDom list until we reach common parent. |
| 348 | * Given the ordering of iDomList, this common parent represents the |
| 349 | * last element of the intersection of block1 and block2 dominators. |
| 350 | */ |
| 351 | int findCommonParent(CompilationUnit *cUnit, int block1, int block2) |
| 352 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 353 | while (block1 != block2) { |
| 354 | while (block1 < block2) { |
| 355 | block1 = cUnit->iDomList[block1]; |
| 356 | DCHECK_NE(block1, NOTVISITED); |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 357 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 358 | while (block2 < block1) { |
| 359 | block2 = cUnit->iDomList[block2]; |
| 360 | DCHECK_NE(block2, NOTVISITED); |
| 361 | } |
| 362 | } |
| 363 | return block1; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* Worker function to compute each block's immediate dominator */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 367 | bool computeBlockIDom(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 368 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 369 | GrowableListIterator iter; |
| 370 | int idom = -1; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 371 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 372 | /* Special-case entry block */ |
| 373 | if (bb == cUnit->entryBlock) { |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 374 | return false; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* Iterate through the predecessors */ |
| 378 | oatGrowableListIteratorInit(bb->predecessors, &iter); |
| 379 | |
| 380 | /* Find the first processed predecessor */ |
| 381 | while (true) { |
| 382 | BasicBlock* predBB = (BasicBlock*)oatGrowableListIteratorNext(&iter); |
| 383 | CHECK(predBB != NULL); |
| 384 | if (cUnit->iDomList[predBB->dfsId] != NOTVISITED) { |
| 385 | idom = predBB->dfsId; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* Scan the rest of the predecessors */ |
| 391 | while (true) { |
| 392 | BasicBlock* predBB = (BasicBlock*)oatGrowableListIteratorNext(&iter); |
| 393 | if (!predBB) break; |
| 394 | if (cUnit->iDomList[predBB->dfsId] == NOTVISITED) { |
| 395 | continue; |
| 396 | } else { |
| 397 | idom = findCommonParent(cUnit, predBB->dfsId, idom); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | DCHECK_NE(idom, NOTVISITED); |
| 402 | |
| 403 | /* Did something change? */ |
| 404 | if (cUnit->iDomList[bb->dfsId] != idom) { |
| 405 | cUnit->iDomList[bb->dfsId] = idom; |
| 406 | return true; |
| 407 | } |
| 408 | return false; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* Worker function to compute each block's domintors */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 412 | bool computeBlockDominators(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 413 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 414 | if (bb == cUnit->entryBlock) { |
| 415 | oatClearAllBits(bb->dominators); |
| 416 | } else { |
| 417 | oatCopyBitVector(bb->dominators, bb->iDom->dominators); |
| 418 | } |
| 419 | oatSetBit(cUnit, bb->dominators, bb->id); |
| 420 | return false; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 421 | } |
| 422 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 423 | bool setDominators(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 424 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 425 | if (bb != cUnit->entryBlock) { |
| 426 | int iDomDFSIdx = cUnit->iDomList[bb->dfsId]; |
| 427 | DCHECK_NE(iDomDFSIdx, NOTVISITED); |
| 428 | int iDomIdx = cUnit->dfsPostOrder.elemList[iDomDFSIdx]; |
| 429 | BasicBlock* iDom = (BasicBlock*) |
| 430 | oatGrowableListGetElement(&cUnit->blockList, iDomIdx); |
| 431 | if (cUnit->enableDebug & (1 << kDebugVerifyDataflow)) { |
| 432 | DCHECK_EQ(bb->iDom->id, iDom->id); |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 433 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 434 | bb->iDom = iDom; |
| 435 | /* Add bb to the iDominated set of the immediate dominator block */ |
| 436 | oatSetBit(cUnit, iDom->iDominated, bb->id); |
| 437 | } |
| 438 | return false; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 439 | } |
| 440 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 441 | /* Compute dominators, immediate dominator, and dominance fronter */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 442 | void computeDominators(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 443 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 444 | int numReachableBlocks = cUnit->numReachableBlocks; |
| 445 | int numTotalBlocks = cUnit->blockList.numUsed; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 446 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 447 | /* Initialize domination-related data structures */ |
| 448 | oatDataFlowAnalysisDispatcher(cUnit, initializeDominationInfo, |
| 449 | kReachableNodes, false /* isIterative */); |
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 | /* Initalize & Clear iDomList */ |
| 452 | if (cUnit->iDomList == NULL) { |
| 453 | cUnit->iDomList = (int*)oatNew(cUnit, sizeof(int) * numReachableBlocks, |
| 454 | false, kAllocDFInfo); |
| 455 | } |
| 456 | for (int i = 0; i < numReachableBlocks; i++) { |
| 457 | cUnit->iDomList[i] = NOTVISITED; |
| 458 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 459 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 460 | /* For post-order, last block is entry block. Set its iDom to istelf */ |
| 461 | DCHECK_EQ(cUnit->entryBlock->dfsId, numReachableBlocks-1); |
| 462 | cUnit->iDomList[cUnit->entryBlock->dfsId] = cUnit->entryBlock->dfsId; |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 463 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 464 | /* Compute the immediate dominators */ |
| 465 | oatDataFlowAnalysisDispatcher(cUnit, computeBlockIDom, |
| 466 | kReversePostOrderTraversal, |
| 467 | true /* isIterative */); |
| 468 | |
| 469 | /* Set the dominator for the root node */ |
| 470 | oatClearAllBits(cUnit->entryBlock->dominators); |
| 471 | oatSetBit(cUnit, cUnit->entryBlock->dominators, cUnit->entryBlock->id); |
| 472 | |
| 473 | if (cUnit->tempBlockV == NULL) { |
| 474 | cUnit->tempBlockV = oatAllocBitVector(cUnit, numTotalBlocks, |
| 475 | false /* expandable */, |
| 476 | kBitMapTmpBlockV); |
| 477 | } else { |
| 478 | oatClearAllBits(cUnit->tempBlockV); |
| 479 | } |
| 480 | cUnit->entryBlock->iDom = NULL; |
| 481 | |
| 482 | /* For testing, compute sets using alternate mechanism */ |
| 483 | if (cUnit->enableDebug & (1 << kDebugVerifyDataflow)) { |
| 484 | // Use alternate mechanism to compute dominators for comparison |
| 485 | oatDataFlowAnalysisDispatcher(cUnit, slowComputeBlockDominators, |
| 486 | kPreOrderDFSTraversal, |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 487 | true /* isIterative */); |
| 488 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 489 | oatDataFlowAnalysisDispatcher(cUnit, slowComputeBlockIDom, |
| 490 | kReachableNodes, |
| 491 | false /* isIterative */); |
| 492 | } |
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 | oatDataFlowAnalysisDispatcher(cUnit, setDominators, |
| 495 | kReachableNodes, |
| 496 | false /* isIterative */); |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 497 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 498 | oatDataFlowAnalysisDispatcher(cUnit, computeBlockDominators, |
| 499 | kReversePostOrderTraversal, |
| 500 | false /* isIterative */); |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 501 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 502 | /* |
| 503 | * Now go ahead and compute the post order traversal based on the |
| 504 | * iDominated sets. |
| 505 | */ |
| 506 | if (cUnit->domPostOrderTraversal.elemList == NULL) { |
| 507 | oatInitGrowableList(cUnit, &cUnit->domPostOrderTraversal, |
| 508 | numReachableBlocks, kListDomPostOrderTraversal); |
| 509 | } else { |
| 510 | cUnit->domPostOrderTraversal.numUsed = 0; |
| 511 | } |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 512 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 513 | computeDomPostOrderTraversal(cUnit, cUnit->entryBlock); |
| 514 | DCHECK_EQ(cUnit->domPostOrderTraversal.numUsed, |
| 515 | (unsigned) cUnit->numReachableBlocks); |
buzbee | 5b53710 | 2012-01-17 17:33:47 -0800 | [diff] [blame] | 516 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 517 | /* Now compute the dominance frontier for each block */ |
| 518 | oatDataFlowAnalysisDispatcher(cUnit, computeDominanceFrontier, |
| 519 | kPostOrderDOMTraversal, |
| 520 | false /* isIterative */); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Perform dest U= src1 ^ ~src2 |
| 525 | * This is probably not general enough to be placed in BitVector.[ch]. |
| 526 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 527 | void computeSuccLiveIn(ArenaBitVector* dest, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 528 | const ArenaBitVector* src1, |
| 529 | const ArenaBitVector* src2) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 530 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 531 | if (dest->storageSize != src1->storageSize || |
| 532 | dest->storageSize != src2->storageSize || |
| 533 | dest->expandable != src1->expandable || |
| 534 | dest->expandable != src2->expandable) { |
| 535 | LOG(FATAL) << "Incompatible set properties"; |
| 536 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 537 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 538 | unsigned int idx; |
| 539 | for (idx = 0; idx < dest->storageSize; idx++) { |
| 540 | dest->storage[idx] |= src1->storage[idx] & ~src2->storage[idx]; |
| 541 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Iterate through all successor blocks and propagate up the live-in sets. |
| 546 | * The calculated result is used for phi-node pruning - where we only need to |
| 547 | * insert a phi node if the variable is live-in to the block. |
| 548 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 549 | bool computeBlockLiveIns(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 550 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 551 | ArenaBitVector* tempDalvikRegisterV = cUnit->tempDalvikRegisterV; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 552 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 553 | if (bb->dataFlowInfo == NULL) return false; |
| 554 | oatCopyBitVector(tempDalvikRegisterV, bb->dataFlowInfo->liveInV); |
| 555 | if (bb->taken && bb->taken->dataFlowInfo) |
| 556 | computeSuccLiveIn(tempDalvikRegisterV, bb->taken->dataFlowInfo->liveInV, |
| 557 | bb->dataFlowInfo->defV); |
| 558 | if (bb->fallThrough && bb->fallThrough->dataFlowInfo) |
| 559 | computeSuccLiveIn(tempDalvikRegisterV, |
| 560 | bb->fallThrough->dataFlowInfo->liveInV, |
| 561 | bb->dataFlowInfo->defV); |
| 562 | if (bb->successorBlockList.blockListType != kNotUsed) { |
| 563 | GrowableListIterator iterator; |
| 564 | oatGrowableListIteratorInit(&bb->successorBlockList.blocks, |
| 565 | &iterator); |
| 566 | while (true) { |
| 567 | SuccessorBlockInfo *successorBlockInfo = |
| 568 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 569 | if (successorBlockInfo == NULL) break; |
| 570 | BasicBlock* succBB = successorBlockInfo->block; |
| 571 | if (succBB->dataFlowInfo) { |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 572 | computeSuccLiveIn(tempDalvikRegisterV, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 573 | succBB->dataFlowInfo->liveInV, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 574 | bb->dataFlowInfo->defV); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 575 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 576 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 577 | } |
| 578 | if (oatCompareBitVectors(tempDalvikRegisterV, bb->dataFlowInfo->liveInV)) { |
| 579 | oatCopyBitVector(bb->dataFlowInfo->liveInV, tempDalvikRegisterV); |
| 580 | return true; |
| 581 | } |
| 582 | return false; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* Insert phi nodes to for each variable to the dominance frontiers */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 586 | void insertPhiNodes(CompilationUnit* cUnit) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 587 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 588 | int dalvikReg; |
| 589 | const GrowableList* blockList = &cUnit->blockList; |
| 590 | ArenaBitVector* phiBlocks = |
| 591 | oatAllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapPhi); |
| 592 | ArenaBitVector* tmpBlocks = |
| 593 | oatAllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapTmpBlocks); |
| 594 | ArenaBitVector* inputBlocks = |
| 595 | oatAllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapInputBlocks); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 596 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 597 | cUnit->tempDalvikRegisterV = |
| 598 | oatAllocBitVector(cUnit, cUnit->numDalvikRegisters, false, |
| 599 | kBitMapRegisterV); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 600 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 601 | oatDataFlowAnalysisDispatcher(cUnit, computeBlockLiveIns, |
| 602 | kPostOrderDFSTraversal, true /* isIterative */); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 603 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 604 | /* Iterate through each Dalvik register */ |
| 605 | for (dalvikReg = 0; dalvikReg < cUnit->numDalvikRegisters; dalvikReg++) { |
| 606 | bool change; |
| 607 | ArenaBitVectorIterator iterator; |
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 | oatCopyBitVector(inputBlocks, cUnit->defBlockMatrix[dalvikReg]); |
| 610 | oatClearAllBits(phiBlocks); |
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 | /* Calculate the phi blocks for each Dalvik register */ |
| 613 | do { |
| 614 | change = false; |
| 615 | oatClearAllBits(tmpBlocks); |
| 616 | oatBitVectorIteratorInit(inputBlocks, &iterator); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 617 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 618 | while (true) { |
| 619 | int idx = oatBitVectorIteratorNext(&iterator); |
| 620 | if (idx == -1) break; |
| 621 | BasicBlock* defBB = |
| 622 | (BasicBlock* ) oatGrowableListGetElement(blockList, idx); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 623 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 624 | /* Merge the dominance frontier to tmpBlocks */ |
| 625 | //TUNING: hot call to oatUnifyBitVectors |
| 626 | if (defBB->domFrontier != NULL) { |
| 627 | oatUnifyBitVectors(tmpBlocks, tmpBlocks, defBB->domFrontier); |
| 628 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 629 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 630 | if (oatCompareBitVectors(phiBlocks, tmpBlocks)) { |
| 631 | change = true; |
| 632 | oatCopyBitVector(phiBlocks, tmpBlocks); |
| 633 | |
| 634 | /* |
| 635 | * Iterate through the original blocks plus the new ones in |
| 636 | * the dominance frontier. |
| 637 | */ |
| 638 | oatCopyBitVector(inputBlocks, phiBlocks); |
| 639 | oatUnifyBitVectors(inputBlocks, inputBlocks, |
| 640 | cUnit->defBlockMatrix[dalvikReg]); |
| 641 | } |
| 642 | } while (change); |
| 643 | |
| 644 | /* |
| 645 | * Insert a phi node for dalvikReg in the phiBlocks if the Dalvik |
| 646 | * register is in the live-in set. |
| 647 | */ |
| 648 | oatBitVectorIteratorInit(phiBlocks, &iterator); |
| 649 | while (true) { |
| 650 | int idx = oatBitVectorIteratorNext(&iterator); |
| 651 | if (idx == -1) break; |
| 652 | BasicBlock* phiBB = |
| 653 | (BasicBlock* ) oatGrowableListGetElement(blockList, idx); |
| 654 | /* Variable will be clobbered before being used - no need for phi */ |
| 655 | if (!oatIsBitSet(phiBB->dataFlowInfo->liveInV, dalvikReg)) continue; |
| 656 | MIR *phi = (MIR *) oatNew(cUnit, sizeof(MIR), true, kAllocDFInfo); |
| 657 | phi->dalvikInsn.opcode = (Instruction::Code)kMirOpPhi; |
| 658 | phi->dalvikInsn.vA = dalvikReg; |
| 659 | phi->offset = phiBB->startOffset; |
| 660 | phi->meta.phiNext = cUnit->phiList; |
| 661 | cUnit->phiList = phi; |
| 662 | oatPrependMIR(phiBB, phi); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 663 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 664 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* |
| 668 | * Worker function to insert phi-operands with latest SSA names from |
| 669 | * predecessor blocks |
| 670 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 671 | bool insertPhiNodeOperands(CompilationUnit* cUnit, BasicBlock* bb) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 672 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 673 | ArenaBitVector* ssaRegV = cUnit->tempSSARegisterV; |
| 674 | GrowableListIterator iter; |
| 675 | MIR *mir; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 676 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 677 | /* Phi nodes are at the beginning of each block */ |
| 678 | for (mir = bb->firstMIRInsn; mir; mir = mir->next) { |
| 679 | if (mir->dalvikInsn.opcode != (Instruction::Code)kMirOpPhi) |
| 680 | return true; |
| 681 | int ssaReg = mir->ssaRep->defs[0]; |
| 682 | DCHECK_GE(ssaReg, 0); // Shouldn't see compiler temps here |
| 683 | int vReg = SRegToVReg(cUnit, ssaReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 684 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 685 | oatClearAllBits(ssaRegV); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 686 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 687 | /* Iterate through the predecessors */ |
| 688 | oatGrowableListIteratorInit(bb->predecessors, &iter); |
| 689 | while (true) { |
| 690 | BasicBlock* predBB = |
| 691 | (BasicBlock*)oatGrowableListIteratorNext(&iter); |
| 692 | if (!predBB) break; |
| 693 | int ssaReg = predBB->dataFlowInfo->vRegToSSAMap[vReg]; |
| 694 | oatSetBit(cUnit, ssaRegV, ssaReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 697 | /* Count the number of SSA registers for a Dalvik register */ |
| 698 | int numUses = oatCountSetBits(ssaRegV); |
| 699 | mir->ssaRep->numUses = numUses; |
| 700 | mir->ssaRep->uses = |
| 701 | (int *) oatNew(cUnit, sizeof(int) * numUses, false, kAllocDFInfo); |
| 702 | mir->ssaRep->fpUse = |
| 703 | (bool *) oatNew(cUnit, sizeof(bool) * numUses, true, kAllocDFInfo); |
| 704 | |
| 705 | ArenaBitVectorIterator phiIterator; |
| 706 | |
| 707 | oatBitVectorIteratorInit(ssaRegV, &phiIterator); |
| 708 | int *usePtr = mir->ssaRep->uses; |
| 709 | |
| 710 | /* Set the uses array for the phi node */ |
| 711 | while (true) { |
| 712 | int ssaRegIdx = oatBitVectorIteratorNext(&phiIterator); |
| 713 | if (ssaRegIdx == -1) break; |
| 714 | *usePtr++ = ssaRegIdx; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | return true; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 719 | } |
| 720 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 721 | void doDFSPreOrderSSARename(CompilationUnit* cUnit, BasicBlock* block) |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 722 | { |
| 723 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 724 | if (block->visited || block->hidden) return; |
| 725 | block->visited = true; |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 726 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 727 | /* Process this block */ |
| 728 | oatDoSSAConversion(cUnit, block); |
| 729 | int mapSize = sizeof(int) * cUnit->numDalvikRegisters; |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 730 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 731 | /* Save SSA map snapshot */ |
| 732 | int* savedSSAMap = (int*)oatNew(cUnit, mapSize, false, |
| 733 | kAllocDalvikToSSAMap); |
| 734 | memcpy(savedSSAMap, cUnit->vRegToSSAMap, mapSize); |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 735 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 736 | if (block->fallThrough) { |
| 737 | doDFSPreOrderSSARename(cUnit, block->fallThrough); |
| 738 | /* Restore SSA map snapshot */ |
| 739 | memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize); |
| 740 | } |
| 741 | if (block->taken) { |
| 742 | doDFSPreOrderSSARename(cUnit, block->taken); |
| 743 | /* Restore SSA map snapshot */ |
| 744 | memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize); |
| 745 | } |
| 746 | if (block->successorBlockList.blockListType != kNotUsed) { |
| 747 | GrowableListIterator iterator; |
| 748 | oatGrowableListIteratorInit(&block->successorBlockList.blocks, |
| 749 | &iterator); |
| 750 | while (true) { |
| 751 | SuccessorBlockInfo *successorBlockInfo = |
| 752 | (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator); |
| 753 | if (successorBlockInfo == NULL) break; |
| 754 | BasicBlock* succBB = successorBlockInfo->block; |
| 755 | doDFSPreOrderSSARename(cUnit, succBB); |
| 756 | /* Restore SSA map snapshot */ |
| 757 | memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize); |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 758 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 759 | } |
| 760 | cUnit->vRegToSSAMap = savedSSAMap; |
| 761 | return; |
buzbee | f0cde54 | 2011-09-13 14:55:02 -0700 | [diff] [blame] | 762 | } |
| 763 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 764 | /* Perform SSA transformation for the whole method */ |
| 765 | void oatMethodSSATransformation(CompilationUnit* cUnit) |
| 766 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 767 | /* Compute the DFS order */ |
| 768 | computeDFSOrders(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 769 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 770 | if (!cUnit->disableDataflow) { |
| 771 | /* Compute the dominator info */ |
| 772 | computeDominators(cUnit); |
| 773 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 774 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 775 | /* Allocate data structures in preparation for SSA conversion */ |
| 776 | oatInitializeSSAConversion(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 777 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 778 | if (!cUnit->disableDataflow) { |
| 779 | /* Find out the "Dalvik reg def x block" relation */ |
| 780 | computeDefBlockMatrix(cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 781 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 782 | /* Insert phi nodes to dominance frontiers for all variables */ |
| 783 | insertPhiNodes(cUnit); |
| 784 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 785 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 786 | /* Rename register names by local defs and phi nodes */ |
| 787 | oatDataFlowAnalysisDispatcher(cUnit, oatClearVisitedFlag, |
| 788 | kAllNodes, false /* isIterative */); |
| 789 | doDFSPreOrderSSARename(cUnit, cUnit->entryBlock); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 790 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 791 | if (!cUnit->disableDataflow) { |
| 792 | /* |
| 793 | * Shared temp bit vector used by each block to count the number of defs |
| 794 | * from all the predecessor blocks. |
| 795 | */ |
| 796 | cUnit->tempSSARegisterV = oatAllocBitVector(cUnit, cUnit->numSSARegs, |
| 797 | false, kBitMapTempSSARegisterV); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 798 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 799 | /* Insert phi-operands with latest SSA names from predecessor blocks */ |
| 800 | oatDataFlowAnalysisDispatcher(cUnit, insertPhiNodeOperands, |
| 801 | kReachableNodes, false /* isIterative */); |
| 802 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 803 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 804 | |
| 805 | } // namespace art |