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