blob: bbe08f50a6d547361b9906597b53cb1e5fec76c5 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
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
buzbeeeaf09bc2012-11-15 14:51:41 -080017#include "compiler_internals.h"
buzbeeefc63692012-11-14 16:31:52 -080018#include "dataflow.h"
buzbee67bf8852011-08-17 17:51:35 -070019
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080020namespace art {
21
buzbeee5f01222012-06-14 15:19:35 -070022// Make sure iterative dfs recording matches old recursive version
23//#define TEST_DFS
24
25inline BasicBlock* needsVisit(BasicBlock* bb) {
26 if (bb != NULL) {
27 if (bb->visited || bb->hidden) {
28 bb = NULL;
29 }
30 }
31 return bb;
32}
33
34BasicBlock* nextUnvisitedSuccessor(BasicBlock* bb)
35{
36 BasicBlock* res = needsVisit(bb->fallThrough);
37 if (res == NULL) {
38 res = needsVisit(bb->taken);
39 if (res == NULL) {
40 if (bb->successorBlockList.blockListType != kNotUsed) {
41 GrowableListIterator iterator;
42 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
43 &iterator);
44 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -080045 SuccessorBlockInfo *sbi = reinterpret_cast<SuccessorBlockInfo*>
46 (oatGrowableListIteratorNext(&iterator));
buzbeee5f01222012-06-14 15:19:35 -070047 if (sbi == NULL) break;
48 res = needsVisit(sbi->block);
49 if (res != NULL) break;
50 }
51 }
52 }
53 }
54 return res;
55}
56
57void markPreOrder(CompilationUnit* cUnit, BasicBlock* block)
58{
59 block->visited = true;
buzbeee5f01222012-06-14 15:19:35 -070060 /* Enqueue the preOrder block id */
61 oatInsertGrowableList(cUnit, &cUnit->dfsOrder, block->id);
62}
63
buzbee31a4a6f2012-02-28 15:36:15 -080064void recordDFSOrders(CompilationUnit* cUnit, BasicBlock* block)
buzbee67bf8852011-08-17 17:51:35 -070065{
buzbeee5f01222012-06-14 15:19:35 -070066 std::vector<BasicBlock*> succ;
67 markPreOrder(cUnit, block);
68 succ.push_back(block);
69 while (!succ.empty()) {
70 BasicBlock* curr = succ.back();
71 BasicBlock* nextSuccessor = nextUnvisitedSuccessor(curr);
72 if (nextSuccessor != NULL) {
73 markPreOrder(cUnit, nextSuccessor);
74 succ.push_back(nextSuccessor);
75 continue;
76 }
77 curr->dfsId = cUnit->dfsPostOrder.numUsed;
78 oatInsertGrowableList(cUnit, &cUnit->dfsPostOrder, curr->id);
79 succ.pop_back();
80 }
81}
82
83#if defined(TEST_DFS)
84/* Enter the node to the dfsOrder list then visit its successors */
85void recursiveRecordDFSOrders(CompilationUnit* cUnit, BasicBlock* block)
86{
buzbee67bf8852011-08-17 17:51:35 -070087
Bill Buzbeea114add2012-05-03 15:00:40 -070088 if (block->visited || block->hidden) return;
89 block->visited = true;
buzbee67bf8852011-08-17 17:51:35 -070090
Bill Buzbeea114add2012-05-03 15:00:40 -070091 // Can this block be reached only via previous block fallthrough?
92 if ((block->blockType == kDalvikByteCode) &&
93 (block->predecessors->numUsed == 1)) {
94 DCHECK_GE(cUnit->dfsOrder.numUsed, 1U);
95 int prevIdx = cUnit->dfsOrder.numUsed - 1;
96 int prevId = cUnit->dfsOrder.elemList[prevIdx];
97 BasicBlock* predBB = (BasicBlock*)block->predecessors->elemList[0];
Bill Buzbeea114add2012-05-03 15:00:40 -070098 }
buzbee3d661942012-03-14 17:37:27 -070099
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 /* Enqueue the preOrder block id */
101 oatInsertGrowableList(cUnit, &cUnit->dfsOrder, block->id);
buzbee67bf8852011-08-17 17:51:35 -0700102
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 if (block->fallThrough) {
buzbeee5f01222012-06-14 15:19:35 -0700104 recursiveRecordDFSOrders(cUnit, block->fallThrough);
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 }
buzbeee5f01222012-06-14 15:19:35 -0700106 if (block->taken) recursiveRecordDFSOrders(cUnit, block->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 if (block->successorBlockList.blockListType != kNotUsed) {
108 GrowableListIterator iterator;
109 oatGrowableListIteratorInit(&block->successorBlockList.blocks,
110 &iterator);
111 while (true) {
112 SuccessorBlockInfo *successorBlockInfo =
113 (SuccessorBlockInfo *) oatGrowableListIteratorNext(&iterator);
114 if (successorBlockInfo == NULL) break;
115 BasicBlock* succBB = successorBlockInfo->block;
buzbeee5f01222012-06-14 15:19:35 -0700116 recursiveRecordDFSOrders(cUnit, succBB);
buzbeee1965672012-03-11 18:39:19 -0700117 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 }
buzbee5b537102012-01-17 17:33:47 -0800119
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 /* Record postorder in basic block and enqueue normal id in dfsPostOrder */
121 block->dfsId = cUnit->dfsPostOrder.numUsed;
122 oatInsertGrowableList(cUnit, &cUnit->dfsPostOrder, block->id);
123 return;
buzbee67bf8852011-08-17 17:51:35 -0700124}
buzbeee5f01222012-06-14 15:19:35 -0700125#endif
buzbee67bf8852011-08-17 17:51:35 -0700126
buzbee5b537102012-01-17 17:33:47 -0800127/* Sort the blocks by the Depth-First-Search */
buzbee31a4a6f2012-02-28 15:36:15 -0800128void computeDFSOrders(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700129{
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 /* Initialize or reset the DFS preOrder list */
131 if (cUnit->dfsOrder.elemList == NULL) {
132 oatInitGrowableList(cUnit, &cUnit->dfsOrder, cUnit->numBlocks,
133 kListDfsOrder);
134 } else {
135 /* Just reset the used length on the counter */
136 cUnit->dfsOrder.numUsed = 0;
137 }
buzbee67bf8852011-08-17 17:51:35 -0700138
Bill Buzbeea114add2012-05-03 15:00:40 -0700139 /* Initialize or reset the DFS postOrder list */
140 if (cUnit->dfsPostOrder.elemList == NULL) {
141 oatInitGrowableList(cUnit, &cUnit->dfsPostOrder, cUnit->numBlocks,
142 kListDfsPostOrder);
143 } else {
144 /* Just reset the used length on the counter */
145 cUnit->dfsPostOrder.numUsed = 0;
146 }
buzbee5b537102012-01-17 17:33:47 -0800147
buzbeee5f01222012-06-14 15:19:35 -0700148#if defined(TEST_DFS)
149 // Reset visited flags
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 oatDataFlowAnalysisDispatcher(cUnit, oatClearVisitedFlag,
151 kAllNodes, false /* isIterative */);
buzbeee5f01222012-06-14 15:19:35 -0700152 // Record pre and post order dfs
153 recursiveRecordDFSOrders(cUnit, cUnit->entryBlock);
154 // Copy the results for later comparison and reset the lists
155 GrowableList recursiveDfsOrder;
156 GrowableList recursiveDfsPostOrder;
157 oatInitGrowableList(cUnit, &recursiveDfsOrder, cUnit->dfsOrder.numUsed,
158 kListDfsOrder);
159 for (unsigned int i = 0; i < cUnit->dfsOrder.numUsed; i++) {
160 oatInsertGrowableList(cUnit, &recursiveDfsOrder,
161 cUnit->dfsOrder.elemList[i]);
162 }
163 cUnit->dfsOrder.numUsed = 0;
164 oatInitGrowableList(cUnit, &recursiveDfsPostOrder,
165 cUnit->dfsPostOrder.numUsed, kListDfsOrder);
166 for (unsigned int i = 0; i < cUnit->dfsPostOrder.numUsed; i++) {
167 oatInsertGrowableList(cUnit, &recursiveDfsPostOrder,
168 cUnit->dfsPostOrder.elemList[i]);
169 }
170 cUnit->dfsPostOrder.numUsed = 0;
171#endif
buzbee67bf8852011-08-17 17:51:35 -0700172
buzbeee5f01222012-06-14 15:19:35 -0700173 // Reset visited flags from all nodes
174 oatDataFlowAnalysisDispatcher(cUnit, oatClearVisitedFlag,
175 kAllNodes, false /* isIterative */);
176 // Record dfs orders
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 recordDFSOrders(cUnit, cUnit->entryBlock);
buzbeee5f01222012-06-14 15:19:35 -0700178
179#if defined(TEST_DFS)
180 bool mismatch = false;
181 mismatch |= (cUnit->dfsOrder.numUsed != recursiveDfsOrder.numUsed);
182 for (unsigned int i = 0; i < cUnit->dfsOrder.numUsed; i++) {
183 mismatch |= (cUnit->dfsOrder.elemList[i] !=
184 recursiveDfsOrder.elemList[i]);
185 }
186 mismatch |= (cUnit->dfsPostOrder.numUsed != recursiveDfsPostOrder.numUsed);
187 for (unsigned int i = 0; i < cUnit->dfsPostOrder.numUsed; i++) {
188 mismatch |= (cUnit->dfsPostOrder.elemList[i] !=
189 recursiveDfsPostOrder.elemList[i]);
190 }
191 if (mismatch) {
192 LOG(INFO) << "Mismatch for "
193 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
buzbeee5f01222012-06-14 15:19:35 -0700194 LOG(INFO) << "New dfs";
195 for (unsigned int i = 0; i < cUnit->dfsOrder.numUsed; i++) {
196 LOG(INFO) << i << " - " << cUnit->dfsOrder.elemList[i];
197 }
198 LOG(INFO) << "Recursive dfs";
199 for (unsigned int i = 0; i < recursiveDfsOrder.numUsed; i++) {
200 LOG(INFO) << i << " - " << recursiveDfsOrder.elemList[i];
201 }
202 LOG(INFO) << "New post dfs";
203 for (unsigned int i = 0; i < cUnit->dfsPostOrder.numUsed; i++) {
204 LOG(INFO) << i << " - " << cUnit->dfsPostOrder.elemList[i];
205 }
206 LOG(INFO) << "Recursive post dfs";
207 for (unsigned int i = 0; i < recursiveDfsPostOrder.numUsed; i++) {
208 LOG(INFO) << i << " - " << recursiveDfsPostOrder.elemList[i];
209 }
210 }
211 CHECK_EQ(cUnit->dfsOrder.numUsed, recursiveDfsOrder.numUsed);
212 for (unsigned int i = 0; i < cUnit->dfsOrder.numUsed; i++) {
213 CHECK_EQ(cUnit->dfsOrder.elemList[i], recursiveDfsOrder.elemList[i]);
214 }
215 CHECK_EQ(cUnit->dfsPostOrder.numUsed, recursiveDfsPostOrder.numUsed);
216 for (unsigned int i = 0; i < cUnit->dfsPostOrder.numUsed; i++) {
217 CHECK_EQ(cUnit->dfsPostOrder.elemList[i],
218 recursiveDfsPostOrder.elemList[i]);
219 }
220#endif
221
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 cUnit->numReachableBlocks = cUnit->dfsOrder.numUsed;
buzbee67bf8852011-08-17 17:51:35 -0700223}
224
225/*
226 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
227 * register idx is defined in BasicBlock bb.
228 */
buzbee31a4a6f2012-02-28 15:36:15 -0800229bool fillDefBlockMatrix(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700230{
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 if (bb->dataFlowInfo == NULL) return false;
buzbee67bf8852011-08-17 17:51:35 -0700232
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 ArenaBitVectorIterator iterator;
buzbee67bf8852011-08-17 17:51:35 -0700234
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 oatBitVectorIteratorInit(bb->dataFlowInfo->defV, &iterator);
236 while (true) {
237 int idx = oatBitVectorIteratorNext(&iterator);
238 if (idx == -1) break;
239 /* Block bb defines register idx */
240 oatSetBit(cUnit, cUnit->defBlockMatrix[idx], bb->id);
241 }
242 return true;
buzbee67bf8852011-08-17 17:51:35 -0700243}
244
buzbee31a4a6f2012-02-28 15:36:15 -0800245void computeDefBlockMatrix(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700246{
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 int numRegisters = cUnit->numDalvikRegisters;
248 /* Allocate numDalvikRegisters bit vector pointers */
buzbeecbd6d442012-11-17 14:11:25 -0800249 cUnit->defBlockMatrix = static_cast<ArenaBitVector**>
250 (oatNew(cUnit, sizeof(ArenaBitVector *) * numRegisters, true, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 int i;
buzbee67bf8852011-08-17 17:51:35 -0700252
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 /* Initialize numRegister vectors with numBlocks bits each */
254 for (i = 0; i < numRegisters; i++) {
255 cUnit->defBlockMatrix[i] = oatAllocBitVector(cUnit, cUnit->numBlocks,
256 false, kBitMapBMatrix);
257 }
258 oatDataFlowAnalysisDispatcher(cUnit, oatFindLocalLiveIn,
259 kAllNodes, false /* isIterative */);
260 oatDataFlowAnalysisDispatcher(cUnit, fillDefBlockMatrix,
261 kAllNodes, false /* isIterative */);
buzbee67bf8852011-08-17 17:51:35 -0700262
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 /*
264 * Also set the incoming parameters as defs in the entry block.
265 * Only need to handle the parameters for the outer method.
266 */
267 int numRegs = cUnit->numDalvikRegisters;
268 int inReg = numRegs - cUnit->numIns;
269 for (; inReg < numRegs; inReg++) {
270 oatSetBit(cUnit, cUnit->defBlockMatrix[inReg], cUnit->entryBlock->id);
271 }
buzbee67bf8852011-08-17 17:51:35 -0700272}
273
274/* Compute the post-order traversal of the CFG */
buzbee31a4a6f2012-02-28 15:36:15 -0800275void computeDomPostOrderTraversal(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700276{
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 ArenaBitVectorIterator bvIterator;
278 oatBitVectorIteratorInit(bb->iDominated, &bvIterator);
279 GrowableList* blockList = &cUnit->blockList;
buzbee67bf8852011-08-17 17:51:35 -0700280
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 /* Iterate through the dominated blocks first */
282 while (true) {
283 //TUNING: hot call to oatBitVectorIteratorNext
284 int bbIdx = oatBitVectorIteratorNext(&bvIterator);
285 if (bbIdx == -1) break;
286 BasicBlock* dominatedBB =
buzbeecbd6d442012-11-17 14:11:25 -0800287 reinterpret_cast<BasicBlock*>(oatGrowableListGetElement(blockList, bbIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 computeDomPostOrderTraversal(cUnit, dominatedBB);
289 }
buzbee67bf8852011-08-17 17:51:35 -0700290
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 /* Enter the current block id */
292 oatInsertGrowableList(cUnit, &cUnit->domPostOrderTraversal, bb->id);
buzbee67bf8852011-08-17 17:51:35 -0700293
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 /* hacky loop detection */
295 if (bb->taken && oatIsBitSet(bb->dominators, bb->taken->id)) {
296 cUnit->hasLoop = true;
297 }
buzbee67bf8852011-08-17 17:51:35 -0700298}
299
buzbee31a4a6f2012-02-28 15:36:15 -0800300void checkForDominanceFrontier(CompilationUnit* cUnit, BasicBlock* domBB,
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 const BasicBlock* succBB)
buzbee67bf8852011-08-17 17:51:35 -0700302{
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 /*
304 * TODO - evaluate whether phi will ever need to be inserted into exit
305 * blocks.
306 */
307 if (succBB->iDom != domBB &&
308 succBB->blockType == kDalvikByteCode &&
309 succBB->hidden == false) {
310 oatSetBit(cUnit, domBB->domFrontier, succBB->id);
311 }
buzbee67bf8852011-08-17 17:51:35 -0700312}
313
314/* Worker function to compute the dominance frontier */
buzbee31a4a6f2012-02-28 15:36:15 -0800315bool computeDominanceFrontier(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700316{
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 GrowableList* blockList = &cUnit->blockList;
buzbee67bf8852011-08-17 17:51:35 -0700318
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 /* Calculate DF_local */
320 if (bb->taken) {
321 checkForDominanceFrontier(cUnit, bb, bb->taken);
322 }
323 if (bb->fallThrough) {
324 checkForDominanceFrontier(cUnit, bb, bb->fallThrough);
325 }
326 if (bb->successorBlockList.blockListType != kNotUsed) {
327 GrowableListIterator iterator;
328 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
329 &iterator);
330 while (true) {
331 SuccessorBlockInfo *successorBlockInfo =
buzbeecbd6d442012-11-17 14:11:25 -0800332 reinterpret_cast<SuccessorBlockInfo*>(oatGrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 if (successorBlockInfo == NULL) break;
334 BasicBlock* succBB = successorBlockInfo->block;
335 checkForDominanceFrontier(cUnit, bb, succBB);
336 }
337 }
buzbee67bf8852011-08-17 17:51:35 -0700338
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 /* Calculate DF_up */
340 ArenaBitVectorIterator bvIterator;
341 oatBitVectorIteratorInit(bb->iDominated, &bvIterator);
342 while (true) {
343 //TUNING: hot call to oatBitVectorIteratorNext
344 int dominatedIdx = oatBitVectorIteratorNext(&bvIterator);
345 if (dominatedIdx == -1) break;
buzbeecbd6d442012-11-17 14:11:25 -0800346 BasicBlock* dominatedBB =
347 reinterpret_cast<BasicBlock*>(oatGrowableListGetElement(blockList, dominatedIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 ArenaBitVectorIterator dfIterator;
349 oatBitVectorIteratorInit(dominatedBB->domFrontier, &dfIterator);
buzbee67bf8852011-08-17 17:51:35 -0700350 while (true) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 //TUNING: hot call to oatBitVectorIteratorNext
352 int dfUpIdx = oatBitVectorIteratorNext(&dfIterator);
353 if (dfUpIdx == -1) break;
buzbeecbd6d442012-11-17 14:11:25 -0800354 BasicBlock* dfUpBlock =
355 reinterpret_cast<BasicBlock*>( oatGrowableListGetElement(blockList, dfUpIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700356 checkForDominanceFrontier(cUnit, bb, dfUpBlock);
buzbee67bf8852011-08-17 17:51:35 -0700357 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 }
buzbee67bf8852011-08-17 17:51:35 -0700359
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 return true;
buzbee67bf8852011-08-17 17:51:35 -0700361}
362
363/* Worker function for initializing domination-related data structures */
buzbee31a4a6f2012-02-28 15:36:15 -0800364bool initializeDominationInfo(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700365{
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 int numTotalBlocks = cUnit->blockList.numUsed;
buzbee67bf8852011-08-17 17:51:35 -0700367
Bill Buzbeea114add2012-05-03 15:00:40 -0700368 if (bb->dominators == NULL ) {
369 bb->dominators = oatAllocBitVector(cUnit, numTotalBlocks,
370 false /* expandable */,
371 kBitMapDominators);
372 bb->iDominated = oatAllocBitVector(cUnit, numTotalBlocks,
373 false /* expandable */,
374 kBitMapIDominated);
375 bb->domFrontier = oatAllocBitVector(cUnit, numTotalBlocks,
376 false /* expandable */,
377 kBitMapDomFrontier);
378 } else {
379 oatClearAllBits(bb->dominators);
380 oatClearAllBits(bb->iDominated);
381 oatClearAllBits(bb->domFrontier);
382 }
383 /* Set all bits in the dominator vector */
384 oatSetInitialBits(bb->dominators, numTotalBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700385
Bill Buzbeea114add2012-05-03 15:00:40 -0700386 return true;
buzbee67bf8852011-08-17 17:51:35 -0700387}
388
buzbee5b537102012-01-17 17:33:47 -0800389/*
390 * Worker function to compute each block's dominators. This implementation
391 * is only used when kDebugVerifyDataflow is active and should compute
392 * the same dominator sets as computeBlockDominators.
393 */
buzbee31a4a6f2012-02-28 15:36:15 -0800394bool slowComputeBlockDominators(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700395{
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 GrowableList* blockList = &cUnit->blockList;
397 int numTotalBlocks = blockList->numUsed;
398 ArenaBitVector* tempBlockV = cUnit->tempBlockV;
399 GrowableListIterator iter;
buzbee67bf8852011-08-17 17:51:35 -0700400
Bill Buzbeea114add2012-05-03 15:00:40 -0700401 /*
402 * The dominator of the entry block has been preset to itself and we need
403 * to skip the calculation here.
404 */
405 if (bb == cUnit->entryBlock) return false;
buzbee67bf8852011-08-17 17:51:35 -0700406
Bill Buzbeea114add2012-05-03 15:00:40 -0700407 oatSetInitialBits(tempBlockV, numTotalBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700408
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 /* Iterate through the predecessors */
410 oatGrowableListIteratorInit(bb->predecessors, &iter);
411 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -0800412 BasicBlock* predBB = reinterpret_cast<BasicBlock*>(oatGrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 if (!predBB) break;
414 /* tempBlockV = tempBlockV ^ dominators */
415 if (predBB->dominators != NULL) {
416 oatIntersectBitVectors(tempBlockV, tempBlockV, predBB->dominators);
buzbee67bf8852011-08-17 17:51:35 -0700417 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 }
419 oatSetBit(cUnit, tempBlockV, bb->id);
420 if (oatCompareBitVectors(tempBlockV, bb->dominators)) {
421 oatCopyBitVector(bb->dominators, tempBlockV);
422 return true;
423 }
424 return false;
buzbee67bf8852011-08-17 17:51:35 -0700425}
426
buzbee5b537102012-01-17 17:33:47 -0800427/*
428 * Worker function to compute the idom. This implementation is only
429 * used when kDebugVerifyDataflow is active and should compute the
430 * same iDom as computeBlockIDom.
431 */
buzbee31a4a6f2012-02-28 15:36:15 -0800432bool slowComputeBlockIDom(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700433{
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 GrowableList* blockList = &cUnit->blockList;
435 ArenaBitVector* tempBlockV = cUnit->tempBlockV;
436 ArenaBitVectorIterator bvIterator;
437 BasicBlock* iDom;
buzbee67bf8852011-08-17 17:51:35 -0700438
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 if (bb == cUnit->entryBlock) return false;
buzbee67bf8852011-08-17 17:51:35 -0700440
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 oatCopyBitVector(tempBlockV, bb->dominators);
442 oatClearBit(tempBlockV, bb->id);
443 oatBitVectorIteratorInit(tempBlockV, &bvIterator);
buzbee67bf8852011-08-17 17:51:35 -0700444
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 /* Should not see any dead block */
446 DCHECK_NE(oatCountSetBits(tempBlockV), 0);
447 if (oatCountSetBits(tempBlockV) == 1) {
buzbeecbd6d442012-11-17 14:11:25 -0800448 iDom = reinterpret_cast<BasicBlock*>
449 (oatGrowableListGetElement(blockList, oatBitVectorIteratorNext(&bvIterator)));
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 bb->iDom = iDom;
451 } else {
452 int iDomIdx = oatBitVectorIteratorNext(&bvIterator);
453 DCHECK_NE(iDomIdx, -1);
454 while (true) {
455 int nextDom = oatBitVectorIteratorNext(&bvIterator);
456 if (nextDom == -1) break;
buzbeecbd6d442012-11-17 14:11:25 -0800457 BasicBlock* nextDomBB =
458 reinterpret_cast<BasicBlock*>(oatGrowableListGetElement(blockList, nextDom));
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 /* iDom dominates nextDom - set new iDom */
460 if (oatIsBitSet(nextDomBB->dominators, iDomIdx)) {
461 iDomIdx = nextDom;
462 }
buzbee67bf8852011-08-17 17:51:35 -0700463
buzbee67bf8852011-08-17 17:51:35 -0700464 }
buzbeecbd6d442012-11-17 14:11:25 -0800465 iDom = reinterpret_cast<BasicBlock*>(oatGrowableListGetElement(blockList, iDomIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700466 /* Set the immediate dominator block for bb */
467 bb->iDom = iDom;
468 }
469 /* Add bb to the iDominated set of the immediate dominator block */
470 oatSetBit(cUnit, iDom->iDominated, bb->id);
471 return true;
buzbee67bf8852011-08-17 17:51:35 -0700472}
473
buzbee5b537102012-01-17 17:33:47 -0800474/*
475 * Walk through the ordered iDom list until we reach common parent.
476 * Given the ordering of iDomList, this common parent represents the
477 * last element of the intersection of block1 and block2 dominators.
478 */
479int findCommonParent(CompilationUnit *cUnit, int block1, int block2)
480{
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 while (block1 != block2) {
482 while (block1 < block2) {
483 block1 = cUnit->iDomList[block1];
484 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800485 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700486 while (block2 < block1) {
487 block2 = cUnit->iDomList[block2];
488 DCHECK_NE(block2, NOTVISITED);
489 }
490 }
491 return block1;
buzbee5b537102012-01-17 17:33:47 -0800492}
493
494/* Worker function to compute each block's immediate dominator */
buzbee31a4a6f2012-02-28 15:36:15 -0800495bool computeBlockIDom(CompilationUnit* cUnit, BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800496{
Bill Buzbeea114add2012-05-03 15:00:40 -0700497 GrowableListIterator iter;
498 int idom = -1;
buzbee5b537102012-01-17 17:33:47 -0800499
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 /* Special-case entry block */
501 if (bb == cUnit->entryBlock) {
buzbee5b537102012-01-17 17:33:47 -0800502 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 }
504
505 /* Iterate through the predecessors */
506 oatGrowableListIteratorInit(bb->predecessors, &iter);
507
508 /* Find the first processed predecessor */
509 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -0800510 BasicBlock* predBB = reinterpret_cast<BasicBlock*>(oatGrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700511 CHECK(predBB != NULL);
512 if (cUnit->iDomList[predBB->dfsId] != NOTVISITED) {
513 idom = predBB->dfsId;
514 break;
515 }
516 }
517
518 /* Scan the rest of the predecessors */
519 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -0800520 BasicBlock* predBB = reinterpret_cast<BasicBlock*>(oatGrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 if (!predBB) break;
522 if (cUnit->iDomList[predBB->dfsId] == NOTVISITED) {
523 continue;
524 } else {
525 idom = findCommonParent(cUnit, predBB->dfsId, idom);
526 }
527 }
528
529 DCHECK_NE(idom, NOTVISITED);
530
531 /* Did something change? */
532 if (cUnit->iDomList[bb->dfsId] != idom) {
533 cUnit->iDomList[bb->dfsId] = idom;
534 return true;
535 }
536 return false;
buzbee5b537102012-01-17 17:33:47 -0800537}
538
539/* Worker function to compute each block's domintors */
buzbee31a4a6f2012-02-28 15:36:15 -0800540bool computeBlockDominators(CompilationUnit* cUnit, BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800541{
Bill Buzbeea114add2012-05-03 15:00:40 -0700542 if (bb == cUnit->entryBlock) {
543 oatClearAllBits(bb->dominators);
544 } else {
545 oatCopyBitVector(bb->dominators, bb->iDom->dominators);
546 }
547 oatSetBit(cUnit, bb->dominators, bb->id);
548 return false;
buzbee5b537102012-01-17 17:33:47 -0800549}
550
buzbee31a4a6f2012-02-28 15:36:15 -0800551bool setDominators(CompilationUnit* cUnit, BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800552{
Bill Buzbeea114add2012-05-03 15:00:40 -0700553 if (bb != cUnit->entryBlock) {
554 int iDomDFSIdx = cUnit->iDomList[bb->dfsId];
555 DCHECK_NE(iDomDFSIdx, NOTVISITED);
556 int iDomIdx = cUnit->dfsPostOrder.elemList[iDomDFSIdx];
buzbeecbd6d442012-11-17 14:11:25 -0800557 BasicBlock* iDom =
558 reinterpret_cast<BasicBlock*>(oatGrowableListGetElement(&cUnit->blockList, iDomIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 if (cUnit->enableDebug & (1 << kDebugVerifyDataflow)) {
560 DCHECK_EQ(bb->iDom->id, iDom->id);
buzbee5b537102012-01-17 17:33:47 -0800561 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700562 bb->iDom = iDom;
563 /* Add bb to the iDominated set of the immediate dominator block */
564 oatSetBit(cUnit, iDom->iDominated, bb->id);
565 }
566 return false;
buzbee5b537102012-01-17 17:33:47 -0800567}
568
buzbee67bf8852011-08-17 17:51:35 -0700569/* Compute dominators, immediate dominator, and dominance fronter */
buzbee31a4a6f2012-02-28 15:36:15 -0800570void computeDominators(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700571{
Bill Buzbeea114add2012-05-03 15:00:40 -0700572 int numReachableBlocks = cUnit->numReachableBlocks;
573 int numTotalBlocks = cUnit->blockList.numUsed;
buzbee67bf8852011-08-17 17:51:35 -0700574
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 /* Initialize domination-related data structures */
576 oatDataFlowAnalysisDispatcher(cUnit, initializeDominationInfo,
577 kReachableNodes, false /* isIterative */);
buzbee67bf8852011-08-17 17:51:35 -0700578
Bill Buzbeea114add2012-05-03 15:00:40 -0700579 /* Initalize & Clear iDomList */
580 if (cUnit->iDomList == NULL) {
buzbeecbd6d442012-11-17 14:11:25 -0800581 cUnit->iDomList = static_cast<int*>(oatNew(cUnit, sizeof(int) * numReachableBlocks,
582 false, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700583 }
584 for (int i = 0; i < numReachableBlocks; i++) {
585 cUnit->iDomList[i] = NOTVISITED;
586 }
buzbee5b537102012-01-17 17:33:47 -0800587
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 /* For post-order, last block is entry block. Set its iDom to istelf */
589 DCHECK_EQ(cUnit->entryBlock->dfsId, numReachableBlocks-1);
590 cUnit->iDomList[cUnit->entryBlock->dfsId] = cUnit->entryBlock->dfsId;
buzbee5b537102012-01-17 17:33:47 -0800591
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 /* Compute the immediate dominators */
593 oatDataFlowAnalysisDispatcher(cUnit, computeBlockIDom,
594 kReversePostOrderTraversal,
595 true /* isIterative */);
596
597 /* Set the dominator for the root node */
598 oatClearAllBits(cUnit->entryBlock->dominators);
599 oatSetBit(cUnit, cUnit->entryBlock->dominators, cUnit->entryBlock->id);
600
601 if (cUnit->tempBlockV == NULL) {
602 cUnit->tempBlockV = oatAllocBitVector(cUnit, numTotalBlocks,
603 false /* expandable */,
604 kBitMapTmpBlockV);
605 } else {
606 oatClearAllBits(cUnit->tempBlockV);
607 }
608 cUnit->entryBlock->iDom = NULL;
609
610 /* For testing, compute sets using alternate mechanism */
611 if (cUnit->enableDebug & (1 << kDebugVerifyDataflow)) {
612 // Use alternate mechanism to compute dominators for comparison
613 oatDataFlowAnalysisDispatcher(cUnit, slowComputeBlockDominators,
614 kPreOrderDFSTraversal,
buzbee5b537102012-01-17 17:33:47 -0800615 true /* isIterative */);
616
Bill Buzbeea114add2012-05-03 15:00:40 -0700617 oatDataFlowAnalysisDispatcher(cUnit, slowComputeBlockIDom,
618 kReachableNodes,
619 false /* isIterative */);
620 }
buzbee67bf8852011-08-17 17:51:35 -0700621
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 oatDataFlowAnalysisDispatcher(cUnit, setDominators,
623 kReachableNodes,
624 false /* isIterative */);
buzbee5b537102012-01-17 17:33:47 -0800625
Bill Buzbeea114add2012-05-03 15:00:40 -0700626 oatDataFlowAnalysisDispatcher(cUnit, computeBlockDominators,
627 kReversePostOrderTraversal,
628 false /* isIterative */);
buzbee5b537102012-01-17 17:33:47 -0800629
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 /*
631 * Now go ahead and compute the post order traversal based on the
632 * iDominated sets.
633 */
634 if (cUnit->domPostOrderTraversal.elemList == NULL) {
635 oatInitGrowableList(cUnit, &cUnit->domPostOrderTraversal,
636 numReachableBlocks, kListDomPostOrderTraversal);
637 } else {
638 cUnit->domPostOrderTraversal.numUsed = 0;
639 }
buzbee5b537102012-01-17 17:33:47 -0800640
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 computeDomPostOrderTraversal(cUnit, cUnit->entryBlock);
buzbeecbd6d442012-11-17 14:11:25 -0800642 DCHECK_EQ(cUnit->domPostOrderTraversal.numUsed, static_cast<unsigned>(cUnit->numReachableBlocks));
buzbee5b537102012-01-17 17:33:47 -0800643
Bill Buzbeea114add2012-05-03 15:00:40 -0700644 /* Now compute the dominance frontier for each block */
645 oatDataFlowAnalysisDispatcher(cUnit, computeDominanceFrontier,
646 kPostOrderDOMTraversal,
647 false /* isIterative */);
buzbee67bf8852011-08-17 17:51:35 -0700648}
649
650/*
651 * Perform dest U= src1 ^ ~src2
652 * This is probably not general enough to be placed in BitVector.[ch].
653 */
buzbee31a4a6f2012-02-28 15:36:15 -0800654void computeSuccLiveIn(ArenaBitVector* dest,
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 const ArenaBitVector* src1,
656 const ArenaBitVector* src2)
buzbee67bf8852011-08-17 17:51:35 -0700657{
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 if (dest->storageSize != src1->storageSize ||
659 dest->storageSize != src2->storageSize ||
660 dest->expandable != src1->expandable ||
661 dest->expandable != src2->expandable) {
662 LOG(FATAL) << "Incompatible set properties";
663 }
buzbee67bf8852011-08-17 17:51:35 -0700664
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 unsigned int idx;
666 for (idx = 0; idx < dest->storageSize; idx++) {
667 dest->storage[idx] |= src1->storage[idx] & ~src2->storage[idx];
668 }
buzbee67bf8852011-08-17 17:51:35 -0700669}
670
671/*
672 * Iterate through all successor blocks and propagate up the live-in sets.
673 * The calculated result is used for phi-node pruning - where we only need to
674 * insert a phi node if the variable is live-in to the block.
675 */
buzbee31a4a6f2012-02-28 15:36:15 -0800676bool computeBlockLiveIns(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700677{
Bill Buzbeea114add2012-05-03 15:00:40 -0700678 ArenaBitVector* tempDalvikRegisterV = cUnit->tempDalvikRegisterV;
buzbee67bf8852011-08-17 17:51:35 -0700679
Bill Buzbeea114add2012-05-03 15:00:40 -0700680 if (bb->dataFlowInfo == NULL) return false;
681 oatCopyBitVector(tempDalvikRegisterV, bb->dataFlowInfo->liveInV);
682 if (bb->taken && bb->taken->dataFlowInfo)
683 computeSuccLiveIn(tempDalvikRegisterV, bb->taken->dataFlowInfo->liveInV,
684 bb->dataFlowInfo->defV);
685 if (bb->fallThrough && bb->fallThrough->dataFlowInfo)
686 computeSuccLiveIn(tempDalvikRegisterV,
687 bb->fallThrough->dataFlowInfo->liveInV,
688 bb->dataFlowInfo->defV);
689 if (bb->successorBlockList.blockListType != kNotUsed) {
690 GrowableListIterator iterator;
691 oatGrowableListIteratorInit(&bb->successorBlockList.blocks,
692 &iterator);
693 while (true) {
694 SuccessorBlockInfo *successorBlockInfo =
buzbeecbd6d442012-11-17 14:11:25 -0800695 reinterpret_cast<SuccessorBlockInfo*>(oatGrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700696 if (successorBlockInfo == NULL) break;
697 BasicBlock* succBB = successorBlockInfo->block;
698 if (succBB->dataFlowInfo) {
buzbee67bf8852011-08-17 17:51:35 -0700699 computeSuccLiveIn(tempDalvikRegisterV,
Bill Buzbeea114add2012-05-03 15:00:40 -0700700 succBB->dataFlowInfo->liveInV,
buzbee67bf8852011-08-17 17:51:35 -0700701 bb->dataFlowInfo->defV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700702 }
buzbee67bf8852011-08-17 17:51:35 -0700703 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700704 }
705 if (oatCompareBitVectors(tempDalvikRegisterV, bb->dataFlowInfo->liveInV)) {
706 oatCopyBitVector(bb->dataFlowInfo->liveInV, tempDalvikRegisterV);
707 return true;
708 }
709 return false;
buzbee67bf8852011-08-17 17:51:35 -0700710}
711
712/* Insert phi nodes to for each variable to the dominance frontiers */
buzbee31a4a6f2012-02-28 15:36:15 -0800713void insertPhiNodes(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700714{
Bill Buzbeea114add2012-05-03 15:00:40 -0700715 int dalvikReg;
716 const GrowableList* blockList = &cUnit->blockList;
717 ArenaBitVector* phiBlocks =
718 oatAllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapPhi);
719 ArenaBitVector* tmpBlocks =
720 oatAllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapTmpBlocks);
721 ArenaBitVector* inputBlocks =
722 oatAllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700723
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 cUnit->tempDalvikRegisterV =
725 oatAllocBitVector(cUnit, cUnit->numDalvikRegisters, false,
726 kBitMapRegisterV);
buzbee67bf8852011-08-17 17:51:35 -0700727
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 oatDataFlowAnalysisDispatcher(cUnit, computeBlockLiveIns,
729 kPostOrderDFSTraversal, true /* isIterative */);
buzbee67bf8852011-08-17 17:51:35 -0700730
Bill Buzbeea114add2012-05-03 15:00:40 -0700731 /* Iterate through each Dalvik register */
buzbee2a83e8f2012-07-13 16:42:30 -0700732 for (dalvikReg = cUnit->numDalvikRegisters - 1; dalvikReg >= 0; dalvikReg--) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700733 bool change;
734 ArenaBitVectorIterator iterator;
buzbee67bf8852011-08-17 17:51:35 -0700735
Bill Buzbeea114add2012-05-03 15:00:40 -0700736 oatCopyBitVector(inputBlocks, cUnit->defBlockMatrix[dalvikReg]);
737 oatClearAllBits(phiBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700738
Bill Buzbeea114add2012-05-03 15:00:40 -0700739 /* Calculate the phi blocks for each Dalvik register */
740 do {
741 change = false;
742 oatClearAllBits(tmpBlocks);
743 oatBitVectorIteratorInit(inputBlocks, &iterator);
buzbee67bf8852011-08-17 17:51:35 -0700744
Bill Buzbeea114add2012-05-03 15:00:40 -0700745 while (true) {
746 int idx = oatBitVectorIteratorNext(&iterator);
747 if (idx == -1) break;
748 BasicBlock* defBB =
buzbeecbd6d442012-11-17 14:11:25 -0800749 reinterpret_cast<BasicBlock*>(oatGrowableListGetElement(blockList, idx));
buzbee67bf8852011-08-17 17:51:35 -0700750
Bill Buzbeea114add2012-05-03 15:00:40 -0700751 /* Merge the dominance frontier to tmpBlocks */
752 //TUNING: hot call to oatUnifyBitVectors
753 if (defBB->domFrontier != NULL) {
754 oatUnifyBitVectors(tmpBlocks, tmpBlocks, defBB->domFrontier);
755 }
buzbee67bf8852011-08-17 17:51:35 -0700756 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700757 if (oatCompareBitVectors(phiBlocks, tmpBlocks)) {
758 change = true;
759 oatCopyBitVector(phiBlocks, tmpBlocks);
760
761 /*
762 * Iterate through the original blocks plus the new ones in
763 * the dominance frontier.
764 */
765 oatCopyBitVector(inputBlocks, phiBlocks);
766 oatUnifyBitVectors(inputBlocks, inputBlocks,
767 cUnit->defBlockMatrix[dalvikReg]);
768 }
769 } while (change);
770
771 /*
772 * Insert a phi node for dalvikReg in the phiBlocks if the Dalvik
773 * register is in the live-in set.
774 */
775 oatBitVectorIteratorInit(phiBlocks, &iterator);
776 while (true) {
777 int idx = oatBitVectorIteratorNext(&iterator);
778 if (idx == -1) break;
779 BasicBlock* phiBB =
buzbeecbd6d442012-11-17 14:11:25 -0800780 reinterpret_cast<BasicBlock*>(oatGrowableListGetElement(blockList, idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700781 /* Variable will be clobbered before being used - no need for phi */
782 if (!oatIsBitSet(phiBB->dataFlowInfo->liveInV, dalvikReg)) continue;
buzbeecbd6d442012-11-17 14:11:25 -0800783 MIR *phi = static_cast<MIR*>(oatNew(cUnit, sizeof(MIR), true, kAllocDFInfo));
784 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
Bill Buzbeea114add2012-05-03 15:00:40 -0700785 phi->dalvikInsn.vA = dalvikReg;
786 phi->offset = phiBB->startOffset;
787 phi->meta.phiNext = cUnit->phiList;
788 cUnit->phiList = phi;
789 oatPrependMIR(phiBB, phi);
buzbee67bf8852011-08-17 17:51:35 -0700790 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700791 }
buzbee67bf8852011-08-17 17:51:35 -0700792}
793
794/*
795 * Worker function to insert phi-operands with latest SSA names from
796 * predecessor blocks
797 */
buzbee31a4a6f2012-02-28 15:36:15 -0800798bool insertPhiNodeOperands(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700799{
Bill Buzbeea114add2012-05-03 15:00:40 -0700800 GrowableListIterator iter;
801 MIR *mir;
buzbee6969d502012-06-15 16:40:31 -0700802 std::vector<int> uses;
803 std::vector<int> incomingArc;
buzbee67bf8852011-08-17 17:51:35 -0700804
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 /* Phi nodes are at the beginning of each block */
806 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800807 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700808 return true;
809 int ssaReg = mir->ssaRep->defs[0];
810 DCHECK_GE(ssaReg, 0); // Shouldn't see compiler temps here
811 int vReg = SRegToVReg(cUnit, ssaReg);
buzbee67bf8852011-08-17 17:51:35 -0700812
buzbee6969d502012-06-15 16:40:31 -0700813 uses.clear();
814 incomingArc.clear();
buzbee67bf8852011-08-17 17:51:35 -0700815
Bill Buzbeea114add2012-05-03 15:00:40 -0700816 /* Iterate through the predecessors */
817 oatGrowableListIteratorInit(bb->predecessors, &iter);
818 while (true) {
819 BasicBlock* predBB =
buzbeecbd6d442012-11-17 14:11:25 -0800820 reinterpret_cast<BasicBlock*>(oatGrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700821 if (!predBB) break;
822 int ssaReg = predBB->dataFlowInfo->vRegToSSAMap[vReg];
buzbee6969d502012-06-15 16:40:31 -0700823 uses.push_back(ssaReg);
824 incomingArc.push_back(predBB->id);
buzbee67bf8852011-08-17 17:51:35 -0700825 }
826
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 /* Count the number of SSA registers for a Dalvik register */
buzbee6969d502012-06-15 16:40:31 -0700828 int numUses = uses.size();
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 mir->ssaRep->numUses = numUses;
830 mir->ssaRep->uses =
buzbeecbd6d442012-11-17 14:11:25 -0800831 static_cast<int*>(oatNew(cUnit, sizeof(int) * numUses, false, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700832 mir->ssaRep->fpUse =
buzbeecbd6d442012-11-17 14:11:25 -0800833 static_cast<bool*>(oatNew(cUnit, sizeof(bool) * numUses, true, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700834 int* incoming =
buzbeecbd6d442012-11-17 14:11:25 -0800835 static_cast<int*>(oatNew(cUnit, sizeof(int) * numUses, false, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700836 // TODO: Ugly, rework (but don't burden each MIR/LIR for Phi-only needs)
buzbeecbd6d442012-11-17 14:11:25 -0800837 mir->dalvikInsn.vB = reinterpret_cast<uintptr_t>(incoming);
Bill Buzbeea114add2012-05-03 15:00:40 -0700838
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 /* Set the uses array for the phi node */
buzbee6969d502012-06-15 16:40:31 -0700840 int *usePtr = mir->ssaRep->uses;
841 for (int i = 0; i < numUses; i++) {
842 *usePtr++ = uses[i];
843 *incoming++ = incomingArc[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700844 }
845 }
846
847 return true;
buzbee67bf8852011-08-17 17:51:35 -0700848}
849
buzbee31a4a6f2012-02-28 15:36:15 -0800850void doDFSPreOrderSSARename(CompilationUnit* cUnit, BasicBlock* block)
buzbeef0cde542011-09-13 14:55:02 -0700851{
852
Bill Buzbeea114add2012-05-03 15:00:40 -0700853 if (block->visited || block->hidden) return;
854 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700855
Bill Buzbeea114add2012-05-03 15:00:40 -0700856 /* Process this block */
857 oatDoSSAConversion(cUnit, block);
858 int mapSize = sizeof(int) * cUnit->numDalvikRegisters;
buzbeef0cde542011-09-13 14:55:02 -0700859
Bill Buzbeea114add2012-05-03 15:00:40 -0700860 /* Save SSA map snapshot */
buzbeecbd6d442012-11-17 14:11:25 -0800861 int* savedSSAMap = static_cast<int*>(oatNew(cUnit, mapSize, false, kAllocDalvikToSSAMap));
Bill Buzbeea114add2012-05-03 15:00:40 -0700862 memcpy(savedSSAMap, cUnit->vRegToSSAMap, mapSize);
buzbeef0cde542011-09-13 14:55:02 -0700863
Bill Buzbeea114add2012-05-03 15:00:40 -0700864 if (block->fallThrough) {
865 doDFSPreOrderSSARename(cUnit, block->fallThrough);
866 /* Restore SSA map snapshot */
867 memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize);
868 }
869 if (block->taken) {
870 doDFSPreOrderSSARename(cUnit, block->taken);
871 /* Restore SSA map snapshot */
872 memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize);
873 }
874 if (block->successorBlockList.blockListType != kNotUsed) {
875 GrowableListIterator iterator;
buzbeecbd6d442012-11-17 14:11:25 -0800876 oatGrowableListIteratorInit(&block->successorBlockList.blocks, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700877 while (true) {
878 SuccessorBlockInfo *successorBlockInfo =
buzbeecbd6d442012-11-17 14:11:25 -0800879 reinterpret_cast<SuccessorBlockInfo*>(oatGrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 if (successorBlockInfo == NULL) break;
881 BasicBlock* succBB = successorBlockInfo->block;
882 doDFSPreOrderSSARename(cUnit, succBB);
883 /* Restore SSA map snapshot */
884 memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize);
buzbeef0cde542011-09-13 14:55:02 -0700885 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700886 }
887 cUnit->vRegToSSAMap = savedSSAMap;
888 return;
buzbeef0cde542011-09-13 14:55:02 -0700889}
890
buzbee67bf8852011-08-17 17:51:35 -0700891/* Perform SSA transformation for the whole method */
892void oatMethodSSATransformation(CompilationUnit* cUnit)
893{
Bill Buzbeea114add2012-05-03 15:00:40 -0700894 /* Compute the DFS order */
895 computeDFSOrders(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700896
Bill Buzbeea114add2012-05-03 15:00:40 -0700897 if (!cUnit->disableDataflow) {
898 /* Compute the dominator info */
899 computeDominators(cUnit);
900 }
buzbee67bf8852011-08-17 17:51:35 -0700901
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 /* Allocate data structures in preparation for SSA conversion */
903 oatInitializeSSAConversion(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700904
Bill Buzbeea114add2012-05-03 15:00:40 -0700905 if (!cUnit->disableDataflow) {
906 /* Find out the "Dalvik reg def x block" relation */
907 computeDefBlockMatrix(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700908
Bill Buzbeea114add2012-05-03 15:00:40 -0700909 /* Insert phi nodes to dominance frontiers for all variables */
910 insertPhiNodes(cUnit);
911 }
buzbee67bf8852011-08-17 17:51:35 -0700912
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 /* Rename register names by local defs and phi nodes */
914 oatDataFlowAnalysisDispatcher(cUnit, oatClearVisitedFlag,
915 kAllNodes, false /* isIterative */);
916 doDFSPreOrderSSARename(cUnit, cUnit->entryBlock);
buzbee67bf8852011-08-17 17:51:35 -0700917
Bill Buzbeea114add2012-05-03 15:00:40 -0700918 if (!cUnit->disableDataflow) {
919 /*
920 * Shared temp bit vector used by each block to count the number of defs
921 * from all the predecessor blocks.
922 */
923 cUnit->tempSSARegisterV = oatAllocBitVector(cUnit, cUnit->numSSARegs,
924 false, kBitMapTempSSARegisterV);
buzbee67bf8852011-08-17 17:51:35 -0700925
buzbee2cfc6392012-05-07 14:51:40 -0700926 cUnit->tempSSABlockIdV =
buzbeecbd6d442012-11-17 14:11:25 -0800927 static_cast<int*>(oatNew(cUnit, sizeof(int) * cUnit->numSSARegs, false, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700928
Bill Buzbeea114add2012-05-03 15:00:40 -0700929 /* Insert phi-operands with latest SSA names from predecessor blocks */
930 oatDataFlowAnalysisDispatcher(cUnit, insertPhiNodeOperands,
931 kReachableNodes, false /* isIterative */);
932 }
buzbee67bf8852011-08-17 17:51:35 -0700933}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800934
935} // namespace art