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