blob: 609a25e2280f37aec7eac590b9b541ae5b002859 [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
buzbeeaad94382012-11-21 07:40:50 -080025static BasicBlock* NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070026 if (bb != NULL) {
27 if (bb->visited || bb->hidden) {
28 bb = NULL;
29 }
30 }
31 return bb;
32}
33
buzbee52a77fc2012-11-20 19:50:46 -080034static BasicBlock* NextUnvisitedSuccessor(BasicBlock* bb)
buzbeee5f01222012-06-14 15:19:35 -070035{
buzbee52a77fc2012-11-20 19:50:46 -080036 BasicBlock* res = NeedsVisit(bb->fallThrough);
buzbeee5f01222012-06-14 15:19:35 -070037 if (res == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -080038 res = NeedsVisit(bb->taken);
buzbeee5f01222012-06-14 15:19:35 -070039 if (res == NULL) {
40 if (bb->successorBlockList.blockListType != kNotUsed) {
41 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -080042 GrowableListIteratorInit(&bb->successorBlockList.blocks,
buzbeee5f01222012-06-14 15:19:35 -070043 &iterator);
44 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -080045 SuccessorBlockInfo *sbi = reinterpret_cast<SuccessorBlockInfo*>
buzbee52a77fc2012-11-20 19:50:46 -080046 (GrowableListIteratorNext(&iterator));
buzbeee5f01222012-06-14 15:19:35 -070047 if (sbi == NULL) break;
buzbee52a77fc2012-11-20 19:50:46 -080048 res = NeedsVisit(sbi->block);
buzbeee5f01222012-06-14 15:19:35 -070049 if (res != NULL) break;
50 }
51 }
52 }
53 }
54 return res;
55}
56
buzbee52a77fc2012-11-20 19:50:46 -080057static void MarkPreOrder(CompilationUnit* cUnit, BasicBlock* block)
buzbeee5f01222012-06-14 15:19:35 -070058{
59 block->visited = true;
buzbeee5f01222012-06-14 15:19:35 -070060 /* Enqueue the preOrder block id */
buzbee52a77fc2012-11-20 19:50:46 -080061 InsertGrowableList(cUnit, &cUnit->dfsOrder, block->id);
buzbeee5f01222012-06-14 15:19:35 -070062}
63
buzbee52a77fc2012-11-20 19:50:46 -080064static void RecordDFSOrders(CompilationUnit* cUnit, BasicBlock* block)
buzbee67bf8852011-08-17 17:51:35 -070065{
buzbeee5f01222012-06-14 15:19:35 -070066 std::vector<BasicBlock*> succ;
buzbee52a77fc2012-11-20 19:50:46 -080067 MarkPreOrder(cUnit, block);
buzbeee5f01222012-06-14 15:19:35 -070068 succ.push_back(block);
69 while (!succ.empty()) {
70 BasicBlock* curr = succ.back();
buzbee52a77fc2012-11-20 19:50:46 -080071 BasicBlock* nextSuccessor = NextUnvisitedSuccessor(curr);
buzbeee5f01222012-06-14 15:19:35 -070072 if (nextSuccessor != NULL) {
buzbee52a77fc2012-11-20 19:50:46 -080073 MarkPreOrder(cUnit, nextSuccessor);
buzbeee5f01222012-06-14 15:19:35 -070074 succ.push_back(nextSuccessor);
75 continue;
76 }
77 curr->dfsId = cUnit->dfsPostOrder.numUsed;
buzbee52a77fc2012-11-20 19:50:46 -080078 InsertGrowableList(cUnit, &cUnit->dfsPostOrder, curr->id);
buzbeee5f01222012-06-14 15:19:35 -070079 succ.pop_back();
80 }
81}
82
83#if defined(TEST_DFS)
84/* Enter the node to the dfsOrder list then visit its successors */
buzbee52a77fc2012-11-20 19:50:46 -080085static void RecursiveRecordDFSOrders(CompilationUnit* cUnit, BasicBlock* block)
buzbeee5f01222012-06-14 15:19:35 -070086{
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800101 InsertGrowableList(cUnit, &cUnit->dfsOrder, block->id);
buzbee67bf8852011-08-17 17:51:35 -0700102
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 if (block->fallThrough) {
buzbee52a77fc2012-11-20 19:50:46 -0800104 RecursiveRecordDFSOrders(cUnit, block->fallThrough);
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 }
buzbee52a77fc2012-11-20 19:50:46 -0800106 if (block->taken) RecursiveRecordDFSOrders(cUnit, block->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 if (block->successorBlockList.blockListType != kNotUsed) {
108 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800109 GrowableListIteratorInit(&block->successorBlockList.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 &iterator);
111 while (true) {
112 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800113 (SuccessorBlockInfo *) GrowableListIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 if (successorBlockInfo == NULL) break;
115 BasicBlock* succBB = successorBlockInfo->block;
buzbee52a77fc2012-11-20 19:50:46 -0800116 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;
buzbee52a77fc2012-11-20 19:50:46 -0800122 InsertGrowableList(cUnit, &cUnit->dfsPostOrder, block->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800128static void 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800132 CompilerInitGrowableList(cUnit, &cUnit->dfsOrder, cUnit->numBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700133 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800141 CompilerInitGrowableList(cUnit, &cUnit->dfsPostOrder, cUnit->numBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 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
buzbee52a77fc2012-11-20 19:50:46 -0800150 DataFlowAnalysisDispatcher(cUnit, ClearVisitedFlag,
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 kAllNodes, false /* isIterative */);
buzbeee5f01222012-06-14 15:19:35 -0700152 // Record pre and post order dfs
buzbee52a77fc2012-11-20 19:50:46 -0800153 RecursiveRecordDFSOrders(cUnit, cUnit->entryBlock);
buzbeee5f01222012-06-14 15:19:35 -0700154 // Copy the results for later comparison and reset the lists
155 GrowableList recursiveDfsOrder;
156 GrowableList recursiveDfsPostOrder;
buzbee52a77fc2012-11-20 19:50:46 -0800157 CompilerInitGrowableList(cUnit, &recursiveDfsOrder, cUnit->dfsOrder.numUsed,
buzbeee5f01222012-06-14 15:19:35 -0700158 kListDfsOrder);
159 for (unsigned int i = 0; i < cUnit->dfsOrder.numUsed; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800160 InsertGrowableList(cUnit, &recursiveDfsOrder,
buzbeee5f01222012-06-14 15:19:35 -0700161 cUnit->dfsOrder.elemList[i]);
162 }
163 cUnit->dfsOrder.numUsed = 0;
buzbee52a77fc2012-11-20 19:50:46 -0800164 CompilerInitGrowableList(cUnit, &recursiveDfsPostOrder,
buzbeee5f01222012-06-14 15:19:35 -0700165 cUnit->dfsPostOrder.numUsed, kListDfsOrder);
166 for (unsigned int i = 0; i < cUnit->dfsPostOrder.numUsed; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800167 InsertGrowableList(cUnit, &recursiveDfsPostOrder,
buzbeee5f01222012-06-14 15:19:35 -0700168 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
buzbee52a77fc2012-11-20 19:50:46 -0800174 DataFlowAnalysisDispatcher(cUnit, ClearVisitedFlag,
buzbeee5f01222012-06-14 15:19:35 -0700175 kAllNodes, false /* isIterative */);
176 // Record dfs orders
buzbee52a77fc2012-11-20 19:50:46 -0800177 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800229static bool 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
buzbee52a77fc2012-11-20 19:50:46 -0800235 BitVectorIteratorInit(bb->dataFlowInfo->defV, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800237 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700238 if (idx == -1) break;
239 /* Block bb defines register idx */
buzbee52a77fc2012-11-20 19:50:46 -0800240 SetBit(cUnit, cUnit->defBlockMatrix[idx], bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 }
242 return true;
buzbee67bf8852011-08-17 17:51:35 -0700243}
244
buzbee52a77fc2012-11-20 19:50:46 -0800245static void 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**>
buzbee52a77fc2012-11-20 19:50:46 -0800250 (NewMem(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++) {
buzbee52a77fc2012-11-20 19:50:46 -0800255 cUnit->defBlockMatrix[i] = AllocBitVector(cUnit, cUnit->numBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 false, kBitMapBMatrix);
257 }
buzbee52a77fc2012-11-20 19:50:46 -0800258 DataFlowAnalysisDispatcher(cUnit, FindLocalLiveIn,
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 kAllNodes, false /* isIterative */);
buzbee52a77fc2012-11-20 19:50:46 -0800260 DataFlowAnalysisDispatcher(cUnit, FillDefBlockMatrix,
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 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++) {
buzbee52a77fc2012-11-20 19:50:46 -0800270 SetBit(cUnit, cUnit->defBlockMatrix[inReg], cUnit->entryBlock->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 }
buzbee67bf8852011-08-17 17:51:35 -0700272}
273
274/* Compute the post-order traversal of the CFG */
buzbee52a77fc2012-11-20 19:50:46 -0800275static void ComputeDomPostOrderTraversal(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700276{
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 ArenaBitVectorIterator bvIterator;
buzbee52a77fc2012-11-20 19:50:46 -0800278 BitVectorIteratorInit(bb->iDominated, &bvIterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800283 //TUNING: hot call to BitVectorIteratorNext
284 int bbIdx = BitVectorIteratorNext(&bvIterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 if (bbIdx == -1) break;
286 BasicBlock* dominatedBB =
buzbee52a77fc2012-11-20 19:50:46 -0800287 reinterpret_cast<BasicBlock*>(GrowableListGetElement(blockList, bbIdx));
288 ComputeDomPostOrderTraversal(cUnit, dominatedBB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 }
buzbee67bf8852011-08-17 17:51:35 -0700290
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 /* Enter the current block id */
buzbee52a77fc2012-11-20 19:50:46 -0800292 InsertGrowableList(cUnit, &cUnit->domPostOrderTraversal, bb->id);
buzbee67bf8852011-08-17 17:51:35 -0700293
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 /* hacky loop detection */
buzbee52a77fc2012-11-20 19:50:46 -0800295 if (bb->taken && IsBitSet(bb->dominators, bb->taken->id)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 cUnit->hasLoop = true;
297 }
buzbee67bf8852011-08-17 17:51:35 -0700298}
299
buzbee52a77fc2012-11-20 19:50:46 -0800300static void CheckForDominanceFrontier(CompilationUnit* cUnit, BasicBlock* domBB,
301 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800310 SetBit(cUnit, domBB->domFrontier, succBB->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 }
buzbee67bf8852011-08-17 17:51:35 -0700312}
313
314/* Worker function to compute the dominance frontier */
buzbee52a77fc2012-11-20 19:50:46 -0800315static bool 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800321 CheckForDominanceFrontier(cUnit, bb, bb->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700322 }
323 if (bb->fallThrough) {
buzbee52a77fc2012-11-20 19:50:46 -0800324 CheckForDominanceFrontier(cUnit, bb, bb->fallThrough);
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 }
326 if (bb->successorBlockList.blockListType != kNotUsed) {
327 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800328 GrowableListIteratorInit(&bb->successorBlockList.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700329 &iterator);
330 while (true) {
331 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800332 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 if (successorBlockInfo == NULL) break;
334 BasicBlock* succBB = successorBlockInfo->block;
buzbee52a77fc2012-11-20 19:50:46 -0800335 CheckForDominanceFrontier(cUnit, bb, succBB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 }
337 }
buzbee67bf8852011-08-17 17:51:35 -0700338
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 /* Calculate DF_up */
340 ArenaBitVectorIterator bvIterator;
buzbee52a77fc2012-11-20 19:50:46 -0800341 BitVectorIteratorInit(bb->iDominated, &bvIterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800343 //TUNING: hot call to BitVectorIteratorNext
344 int dominatedIdx = BitVectorIteratorNext(&bvIterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 if (dominatedIdx == -1) break;
buzbeecbd6d442012-11-17 14:11:25 -0800346 BasicBlock* dominatedBB =
buzbee52a77fc2012-11-20 19:50:46 -0800347 reinterpret_cast<BasicBlock*>(GrowableListGetElement(blockList, dominatedIdx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 ArenaBitVectorIterator dfIterator;
buzbee52a77fc2012-11-20 19:50:46 -0800349 BitVectorIteratorInit(dominatedBB->domFrontier, &dfIterator);
buzbee67bf8852011-08-17 17:51:35 -0700350 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800351 //TUNING: hot call to BitVectorIteratorNext
352 int dfUpIdx = BitVectorIteratorNext(&dfIterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 if (dfUpIdx == -1) break;
buzbeecbd6d442012-11-17 14:11:25 -0800354 BasicBlock* dfUpBlock =
buzbee52a77fc2012-11-20 19:50:46 -0800355 reinterpret_cast<BasicBlock*>( GrowableListGetElement(blockList, dfUpIdx));
356 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800364static bool 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 ) {
buzbee52a77fc2012-11-20 19:50:46 -0800369 bb->dominators = AllocBitVector(cUnit, numTotalBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700370 false /* expandable */,
371 kBitMapDominators);
buzbee52a77fc2012-11-20 19:50:46 -0800372 bb->iDominated = AllocBitVector(cUnit, numTotalBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 false /* expandable */,
374 kBitMapIDominated);
buzbee52a77fc2012-11-20 19:50:46 -0800375 bb->domFrontier = AllocBitVector(cUnit, numTotalBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 false /* expandable */,
377 kBitMapDomFrontier);
378 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800379 ClearAllBits(bb->dominators);
380 ClearAllBits(bb->iDominated);
381 ClearAllBits(bb->domFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700382 }
383 /* Set all bits in the dominator vector */
buzbee52a77fc2012-11-20 19:50:46 -0800384 SetInitialBits(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
buzbee52a77fc2012-11-20 19:50:46 -0800392 * the same dominator sets as ComputeBlockDominiators.
buzbee5b537102012-01-17 17:33:47 -0800393 */
buzbee52a77fc2012-11-20 19:50:46 -0800394static bool 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
buzbee52a77fc2012-11-20 19:50:46 -0800407 SetInitialBits(tempBlockV, numTotalBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700408
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 /* Iterate through the predecessors */
buzbee52a77fc2012-11-20 19:50:46 -0800410 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800412 BasicBlock* predBB = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 if (!predBB) break;
414 /* tempBlockV = tempBlockV ^ dominators */
415 if (predBB->dominators != NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800416 IntersectBitVectors(tempBlockV, tempBlockV, predBB->dominators);
buzbee67bf8852011-08-17 17:51:35 -0700417 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 }
buzbee52a77fc2012-11-20 19:50:46 -0800419 SetBit(cUnit, tempBlockV, bb->id);
420 if (CompareBitVectors(tempBlockV, bb->dominators)) {
421 CopyBitVector(bb->dominators, tempBlockV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 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
buzbee52a77fc2012-11-20 19:50:46 -0800430 * same iDom as ComputeblockIDom.
buzbee5b537102012-01-17 17:33:47 -0800431 */
buzbee52a77fc2012-11-20 19:50:46 -0800432static bool 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
buzbee52a77fc2012-11-20 19:50:46 -0800441 CopyBitVector(tempBlockV, bb->dominators);
442 ClearBit(tempBlockV, bb->id);
443 BitVectorIteratorInit(tempBlockV, &bvIterator);
buzbee67bf8852011-08-17 17:51:35 -0700444
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 /* Should not see any dead block */
buzbee52a77fc2012-11-20 19:50:46 -0800446 DCHECK_NE(CountSetBits(tempBlockV), 0);
447 if (CountSetBits(tempBlockV) == 1) {
buzbeecbd6d442012-11-17 14:11:25 -0800448 iDom = reinterpret_cast<BasicBlock*>
buzbee52a77fc2012-11-20 19:50:46 -0800449 (GrowableListGetElement(blockList, BitVectorIteratorNext(&bvIterator)));
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 bb->iDom = iDom;
451 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800452 int iDomIdx = BitVectorIteratorNext(&bvIterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700453 DCHECK_NE(iDomIdx, -1);
454 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800455 int nextDom = BitVectorIteratorNext(&bvIterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 if (nextDom == -1) break;
buzbeecbd6d442012-11-17 14:11:25 -0800457 BasicBlock* nextDomBB =
buzbee52a77fc2012-11-20 19:50:46 -0800458 reinterpret_cast<BasicBlock*>(GrowableListGetElement(blockList, nextDom));
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 /* iDom dominates nextDom - set new iDom */
buzbee52a77fc2012-11-20 19:50:46 -0800460 if (IsBitSet(nextDomBB->dominators, iDomIdx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 iDomIdx = nextDom;
462 }
buzbee67bf8852011-08-17 17:51:35 -0700463
buzbee67bf8852011-08-17 17:51:35 -0700464 }
buzbee52a77fc2012-11-20 19:50:46 -0800465 iDom = reinterpret_cast<BasicBlock*>(GrowableListGetElement(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 */
buzbee52a77fc2012-11-20 19:50:46 -0800470 SetBit(cUnit, iDom->iDominated, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800479static int FindCommonParent(CompilationUnit *cUnit, int block1, int block2)
buzbee5b537102012-01-17 17:33:47 -0800480{
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800495static bool 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800506 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700507
508 /* Find the first processed predecessor */
509 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800510 BasicBlock* predBB = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&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) {
buzbee52a77fc2012-11-20 19:50:46 -0800520 BasicBlock* predBB = reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 if (!predBB) break;
522 if (cUnit->iDomList[predBB->dfsId] == NOTVISITED) {
523 continue;
524 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800525 idom = FindCommonParent(cUnit, predBB->dfsId, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 }
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800540static bool ComputeBlockDominiators(CompilationUnit* cUnit, BasicBlock* bb)
buzbee5b537102012-01-17 17:33:47 -0800541{
Bill Buzbeea114add2012-05-03 15:00:40 -0700542 if (bb == cUnit->entryBlock) {
buzbee52a77fc2012-11-20 19:50:46 -0800543 ClearAllBits(bb->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800545 CopyBitVector(bb->dominators, bb->iDom->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700546 }
buzbee52a77fc2012-11-20 19:50:46 -0800547 SetBit(cUnit, bb->dominators, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700548 return false;
buzbee5b537102012-01-17 17:33:47 -0800549}
550
buzbee52a77fc2012-11-20 19:50:46 -0800551static bool 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 =
buzbee52a77fc2012-11-20 19:50:46 -0800558 reinterpret_cast<BasicBlock*>(GrowableListGetElement(&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 */
buzbee52a77fc2012-11-20 19:50:46 -0800564 SetBit(cUnit, iDom->iDominated, bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 }
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 */
buzbee52a77fc2012-11-20 19:50:46 -0800570static void 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800576 DataFlowAnalysisDispatcher(cUnit, InitializeDominationInfo,
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800581 cUnit->iDomList = static_cast<int*>(NewMem(cUnit, sizeof(int) * numReachableBlocks,
buzbeecbd6d442012-11-17 14:11:25 -0800582 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800593 DataFlowAnalysisDispatcher(cUnit, ComputeblockIDom,
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 kReversePostOrderTraversal,
595 true /* isIterative */);
596
597 /* Set the dominator for the root node */
buzbee52a77fc2012-11-20 19:50:46 -0800598 ClearAllBits(cUnit->entryBlock->dominators);
599 SetBit(cUnit, cUnit->entryBlock->dominators, cUnit->entryBlock->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700600
601 if (cUnit->tempBlockV == NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800602 cUnit->tempBlockV = AllocBitVector(cUnit, numTotalBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700603 false /* expandable */,
604 kBitMapTmpBlockV);
605 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800606 ClearAllBits(cUnit->tempBlockV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 }
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
buzbee52a77fc2012-11-20 19:50:46 -0800613 DataFlowAnalysisDispatcher(cUnit, SlowComputeBlockDominators,
Bill Buzbeea114add2012-05-03 15:00:40 -0700614 kPreOrderDFSTraversal,
buzbee5b537102012-01-17 17:33:47 -0800615 true /* isIterative */);
616
buzbee52a77fc2012-11-20 19:50:46 -0800617 DataFlowAnalysisDispatcher(cUnit, SlowComputeBlockIDom,
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 kReachableNodes,
619 false /* isIterative */);
620 }
buzbee67bf8852011-08-17 17:51:35 -0700621
buzbee52a77fc2012-11-20 19:50:46 -0800622 DataFlowAnalysisDispatcher(cUnit, SetDominators,
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 kReachableNodes,
624 false /* isIterative */);
buzbee5b537102012-01-17 17:33:47 -0800625
buzbee52a77fc2012-11-20 19:50:46 -0800626 DataFlowAnalysisDispatcher(cUnit, ComputeBlockDominiators,
Bill Buzbeea114add2012-05-03 15:00:40 -0700627 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) {
buzbee52a77fc2012-11-20 19:50:46 -0800635 CompilerInitGrowableList(cUnit, &cUnit->domPostOrderTraversal,
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 numReachableBlocks, kListDomPostOrderTraversal);
637 } else {
638 cUnit->domPostOrderTraversal.numUsed = 0;
639 }
buzbee5b537102012-01-17 17:33:47 -0800640
buzbee52a77fc2012-11-20 19:50:46 -0800641 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800645 DataFlowAnalysisDispatcher(cUnit, ComputeDominanceFrontier,
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 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 */
buzbee52a77fc2012-11-20 19:50:46 -0800654static void ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
655 const ArenaBitVector* src2)
buzbee67bf8852011-08-17 17:51:35 -0700656{
Bill Buzbeea114add2012-05-03 15:00:40 -0700657 if (dest->storageSize != src1->storageSize ||
658 dest->storageSize != src2->storageSize ||
659 dest->expandable != src1->expandable ||
660 dest->expandable != src2->expandable) {
661 LOG(FATAL) << "Incompatible set properties";
662 }
buzbee67bf8852011-08-17 17:51:35 -0700663
Bill Buzbeea114add2012-05-03 15:00:40 -0700664 unsigned int idx;
665 for (idx = 0; idx < dest->storageSize; idx++) {
666 dest->storage[idx] |= src1->storage[idx] & ~src2->storage[idx];
667 }
buzbee67bf8852011-08-17 17:51:35 -0700668}
669
670/*
671 * Iterate through all successor blocks and propagate up the live-in sets.
672 * The calculated result is used for phi-node pruning - where we only need to
673 * insert a phi node if the variable is live-in to the block.
674 */
buzbee52a77fc2012-11-20 19:50:46 -0800675static bool ComputeBlockLiveIns(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700676{
Bill Buzbeea114add2012-05-03 15:00:40 -0700677 ArenaBitVector* tempDalvikRegisterV = cUnit->tempDalvikRegisterV;
buzbee67bf8852011-08-17 17:51:35 -0700678
Bill Buzbeea114add2012-05-03 15:00:40 -0700679 if (bb->dataFlowInfo == NULL) return false;
buzbee52a77fc2012-11-20 19:50:46 -0800680 CopyBitVector(tempDalvikRegisterV, bb->dataFlowInfo->liveInV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 if (bb->taken && bb->taken->dataFlowInfo)
buzbee52a77fc2012-11-20 19:50:46 -0800682 ComputeSuccLineIn(tempDalvikRegisterV, bb->taken->dataFlowInfo->liveInV,
Bill Buzbeea114add2012-05-03 15:00:40 -0700683 bb->dataFlowInfo->defV);
684 if (bb->fallThrough && bb->fallThrough->dataFlowInfo)
buzbee52a77fc2012-11-20 19:50:46 -0800685 ComputeSuccLineIn(tempDalvikRegisterV,
Bill Buzbeea114add2012-05-03 15:00:40 -0700686 bb->fallThrough->dataFlowInfo->liveInV,
687 bb->dataFlowInfo->defV);
688 if (bb->successorBlockList.blockListType != kNotUsed) {
689 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800690 GrowableListIteratorInit(&bb->successorBlockList.blocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700691 &iterator);
692 while (true) {
693 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800694 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 if (successorBlockInfo == NULL) break;
696 BasicBlock* succBB = successorBlockInfo->block;
697 if (succBB->dataFlowInfo) {
buzbee52a77fc2012-11-20 19:50:46 -0800698 ComputeSuccLineIn(tempDalvikRegisterV,
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 succBB->dataFlowInfo->liveInV,
buzbee67bf8852011-08-17 17:51:35 -0700700 bb->dataFlowInfo->defV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 }
buzbee67bf8852011-08-17 17:51:35 -0700702 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700703 }
buzbee52a77fc2012-11-20 19:50:46 -0800704 if (CompareBitVectors(tempDalvikRegisterV, bb->dataFlowInfo->liveInV)) {
705 CopyBitVector(bb->dataFlowInfo->liveInV, tempDalvikRegisterV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700706 return true;
707 }
708 return false;
buzbee67bf8852011-08-17 17:51:35 -0700709}
710
711/* Insert phi nodes to for each variable to the dominance frontiers */
buzbee52a77fc2012-11-20 19:50:46 -0800712static void InsertPhiNodes(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700713{
Bill Buzbeea114add2012-05-03 15:00:40 -0700714 int dalvikReg;
715 const GrowableList* blockList = &cUnit->blockList;
716 ArenaBitVector* phiBlocks =
buzbee52a77fc2012-11-20 19:50:46 -0800717 AllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapPhi);
Bill Buzbeea114add2012-05-03 15:00:40 -0700718 ArenaBitVector* tmpBlocks =
buzbee52a77fc2012-11-20 19:50:46 -0800719 AllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapTmpBlocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700720 ArenaBitVector* inputBlocks =
buzbee52a77fc2012-11-20 19:50:46 -0800721 AllocBitVector(cUnit, cUnit->numBlocks, false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700722
Bill Buzbeea114add2012-05-03 15:00:40 -0700723 cUnit->tempDalvikRegisterV =
buzbee52a77fc2012-11-20 19:50:46 -0800724 AllocBitVector(cUnit, cUnit->numDalvikRegisters, false,
Bill Buzbeea114add2012-05-03 15:00:40 -0700725 kBitMapRegisterV);
buzbee67bf8852011-08-17 17:51:35 -0700726
buzbee52a77fc2012-11-20 19:50:46 -0800727 DataFlowAnalysisDispatcher(cUnit, ComputeBlockLiveIns,
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 kPostOrderDFSTraversal, true /* isIterative */);
buzbee67bf8852011-08-17 17:51:35 -0700729
Bill Buzbeea114add2012-05-03 15:00:40 -0700730 /* Iterate through each Dalvik register */
buzbee2a83e8f2012-07-13 16:42:30 -0700731 for (dalvikReg = cUnit->numDalvikRegisters - 1; dalvikReg >= 0; dalvikReg--) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 bool change;
733 ArenaBitVectorIterator iterator;
buzbee67bf8852011-08-17 17:51:35 -0700734
buzbee52a77fc2012-11-20 19:50:46 -0800735 CopyBitVector(inputBlocks, cUnit->defBlockMatrix[dalvikReg]);
736 ClearAllBits(phiBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700737
Bill Buzbeea114add2012-05-03 15:00:40 -0700738 /* Calculate the phi blocks for each Dalvik register */
739 do {
740 change = false;
buzbee52a77fc2012-11-20 19:50:46 -0800741 ClearAllBits(tmpBlocks);
742 BitVectorIteratorInit(inputBlocks, &iterator);
buzbee67bf8852011-08-17 17:51:35 -0700743
Bill Buzbeea114add2012-05-03 15:00:40 -0700744 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800745 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700746 if (idx == -1) break;
747 BasicBlock* defBB =
buzbee52a77fc2012-11-20 19:50:46 -0800748 reinterpret_cast<BasicBlock*>(GrowableListGetElement(blockList, idx));
buzbee67bf8852011-08-17 17:51:35 -0700749
Bill Buzbeea114add2012-05-03 15:00:40 -0700750 /* Merge the dominance frontier to tmpBlocks */
buzbee52a77fc2012-11-20 19:50:46 -0800751 //TUNING: hot call to UnifyBitVetors
Bill Buzbeea114add2012-05-03 15:00:40 -0700752 if (defBB->domFrontier != NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800753 UnifyBitVetors(tmpBlocks, tmpBlocks, defBB->domFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700754 }
buzbee67bf8852011-08-17 17:51:35 -0700755 }
buzbee52a77fc2012-11-20 19:50:46 -0800756 if (CompareBitVectors(phiBlocks, tmpBlocks)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700757 change = true;
buzbee52a77fc2012-11-20 19:50:46 -0800758 CopyBitVector(phiBlocks, tmpBlocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700759
760 /*
761 * Iterate through the original blocks plus the new ones in
762 * the dominance frontier.
763 */
buzbee52a77fc2012-11-20 19:50:46 -0800764 CopyBitVector(inputBlocks, phiBlocks);
765 UnifyBitVetors(inputBlocks, inputBlocks,
Bill Buzbeea114add2012-05-03 15:00:40 -0700766 cUnit->defBlockMatrix[dalvikReg]);
767 }
768 } while (change);
769
770 /*
771 * Insert a phi node for dalvikReg in the phiBlocks if the Dalvik
772 * register is in the live-in set.
773 */
buzbee52a77fc2012-11-20 19:50:46 -0800774 BitVectorIteratorInit(phiBlocks, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700775 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800776 int idx = BitVectorIteratorNext(&iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700777 if (idx == -1) break;
778 BasicBlock* phiBB =
buzbee52a77fc2012-11-20 19:50:46 -0800779 reinterpret_cast<BasicBlock*>(GrowableListGetElement(blockList, idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700780 /* Variable will be clobbered before being used - no need for phi */
buzbee52a77fc2012-11-20 19:50:46 -0800781 if (!IsBitSet(phiBB->dataFlowInfo->liveInV, dalvikReg)) continue;
782 MIR *phi = static_cast<MIR*>(NewMem(cUnit, sizeof(MIR), true, kAllocDFInfo));
buzbeecbd6d442012-11-17 14:11:25 -0800783 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
Bill Buzbeea114add2012-05-03 15:00:40 -0700784 phi->dalvikInsn.vA = dalvikReg;
785 phi->offset = phiBB->startOffset;
786 phi->meta.phiNext = cUnit->phiList;
787 cUnit->phiList = phi;
buzbee52a77fc2012-11-20 19:50:46 -0800788 PrependMIR(phiBB, phi);
buzbee67bf8852011-08-17 17:51:35 -0700789 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700790 }
buzbee67bf8852011-08-17 17:51:35 -0700791}
792
793/*
794 * Worker function to insert phi-operands with latest SSA names from
795 * predecessor blocks
796 */
buzbee52a77fc2012-11-20 19:50:46 -0800797static bool InsertPhiNodeOperands(CompilationUnit* cUnit, BasicBlock* bb)
buzbee67bf8852011-08-17 17:51:35 -0700798{
Bill Buzbeea114add2012-05-03 15:00:40 -0700799 GrowableListIterator iter;
800 MIR *mir;
buzbee6969d502012-06-15 16:40:31 -0700801 std::vector<int> uses;
802 std::vector<int> incomingArc;
buzbee67bf8852011-08-17 17:51:35 -0700803
Bill Buzbeea114add2012-05-03 15:00:40 -0700804 /* Phi nodes are at the beginning of each block */
805 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800806 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 return true;
808 int ssaReg = mir->ssaRep->defs[0];
809 DCHECK_GE(ssaReg, 0); // Shouldn't see compiler temps here
810 int vReg = SRegToVReg(cUnit, ssaReg);
buzbee67bf8852011-08-17 17:51:35 -0700811
buzbee6969d502012-06-15 16:40:31 -0700812 uses.clear();
813 incomingArc.clear();
buzbee67bf8852011-08-17 17:51:35 -0700814
Bill Buzbeea114add2012-05-03 15:00:40 -0700815 /* Iterate through the predecessors */
buzbee52a77fc2012-11-20 19:50:46 -0800816 GrowableListIteratorInit(bb->predecessors, &iter);
Bill Buzbeea114add2012-05-03 15:00:40 -0700817 while (true) {
818 BasicBlock* predBB =
buzbee52a77fc2012-11-20 19:50:46 -0800819 reinterpret_cast<BasicBlock*>(GrowableListIteratorNext(&iter));
Bill Buzbeea114add2012-05-03 15:00:40 -0700820 if (!predBB) break;
821 int ssaReg = predBB->dataFlowInfo->vRegToSSAMap[vReg];
buzbee6969d502012-06-15 16:40:31 -0700822 uses.push_back(ssaReg);
823 incomingArc.push_back(predBB->id);
buzbee67bf8852011-08-17 17:51:35 -0700824 }
825
Bill Buzbeea114add2012-05-03 15:00:40 -0700826 /* Count the number of SSA registers for a Dalvik register */
buzbee6969d502012-06-15 16:40:31 -0700827 int numUses = uses.size();
Bill Buzbeea114add2012-05-03 15:00:40 -0700828 mir->ssaRep->numUses = numUses;
829 mir->ssaRep->uses =
buzbee52a77fc2012-11-20 19:50:46 -0800830 static_cast<int*>(NewMem(cUnit, sizeof(int) * numUses, false, kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700831 mir->ssaRep->fpUse =
buzbee52a77fc2012-11-20 19:50:46 -0800832 static_cast<bool*>(NewMem(cUnit, sizeof(bool) * numUses, true, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700833 int* incoming =
buzbee52a77fc2012-11-20 19:50:46 -0800834 static_cast<int*>(NewMem(cUnit, sizeof(int) * numUses, false, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700835 // TODO: Ugly, rework (but don't burden each MIR/LIR for Phi-only needs)
buzbeecbd6d442012-11-17 14:11:25 -0800836 mir->dalvikInsn.vB = reinterpret_cast<uintptr_t>(incoming);
Bill Buzbeea114add2012-05-03 15:00:40 -0700837
Bill Buzbeea114add2012-05-03 15:00:40 -0700838 /* Set the uses array for the phi node */
buzbee6969d502012-06-15 16:40:31 -0700839 int *usePtr = mir->ssaRep->uses;
840 for (int i = 0; i < numUses; i++) {
841 *usePtr++ = uses[i];
842 *incoming++ = incomingArc[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700843 }
844 }
845
846 return true;
buzbee67bf8852011-08-17 17:51:35 -0700847}
848
buzbee52a77fc2012-11-20 19:50:46 -0800849static void DoDFSPreOrderSSARename(CompilationUnit* cUnit, BasicBlock* block)
buzbeef0cde542011-09-13 14:55:02 -0700850{
851
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 if (block->visited || block->hidden) return;
853 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700854
Bill Buzbeea114add2012-05-03 15:00:40 -0700855 /* Process this block */
buzbee52a77fc2012-11-20 19:50:46 -0800856 DoSSAConversion(cUnit, block);
Bill Buzbeea114add2012-05-03 15:00:40 -0700857 int mapSize = sizeof(int) * cUnit->numDalvikRegisters;
buzbeef0cde542011-09-13 14:55:02 -0700858
Bill Buzbeea114add2012-05-03 15:00:40 -0700859 /* Save SSA map snapshot */
buzbee52a77fc2012-11-20 19:50:46 -0800860 int* savedSSAMap = static_cast<int*>(NewMem(cUnit, mapSize, false, kAllocDalvikToSSAMap));
Bill Buzbeea114add2012-05-03 15:00:40 -0700861 memcpy(savedSSAMap, cUnit->vRegToSSAMap, mapSize);
buzbeef0cde542011-09-13 14:55:02 -0700862
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 if (block->fallThrough) {
buzbee52a77fc2012-11-20 19:50:46 -0800864 DoDFSPreOrderSSARename(cUnit, block->fallThrough);
Bill Buzbeea114add2012-05-03 15:00:40 -0700865 /* Restore SSA map snapshot */
866 memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize);
867 }
868 if (block->taken) {
buzbee52a77fc2012-11-20 19:50:46 -0800869 DoDFSPreOrderSSARename(cUnit, block->taken);
Bill Buzbeea114add2012-05-03 15:00:40 -0700870 /* Restore SSA map snapshot */
871 memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize);
872 }
873 if (block->successorBlockList.blockListType != kNotUsed) {
874 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800875 GrowableListIteratorInit(&block->successorBlockList.blocks, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 while (true) {
877 SuccessorBlockInfo *successorBlockInfo =
buzbee52a77fc2012-11-20 19:50:46 -0800878 reinterpret_cast<SuccessorBlockInfo*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700879 if (successorBlockInfo == NULL) break;
880 BasicBlock* succBB = successorBlockInfo->block;
buzbee52a77fc2012-11-20 19:50:46 -0800881 DoDFSPreOrderSSARename(cUnit, succBB);
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 /* Restore SSA map snapshot */
883 memcpy(cUnit->vRegToSSAMap, savedSSAMap, mapSize);
buzbeef0cde542011-09-13 14:55:02 -0700884 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 }
886 cUnit->vRegToSSAMap = savedSSAMap;
887 return;
buzbeef0cde542011-09-13 14:55:02 -0700888}
889
buzbee67bf8852011-08-17 17:51:35 -0700890/* Perform SSA transformation for the whole method */
buzbee52a77fc2012-11-20 19:50:46 -0800891void SSATransformation(CompilationUnit* cUnit)
buzbee67bf8852011-08-17 17:51:35 -0700892{
Bill Buzbeea114add2012-05-03 15:00:40 -0700893 /* Compute the DFS order */
buzbee52a77fc2012-11-20 19:50:46 -0800894 ComputeDFSOrders(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700895
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 if (!cUnit->disableDataflow) {
897 /* Compute the dominator info */
buzbee52a77fc2012-11-20 19:50:46 -0800898 ComputeDominators(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700899 }
buzbee67bf8852011-08-17 17:51:35 -0700900
Bill Buzbeea114add2012-05-03 15:00:40 -0700901 /* Allocate data structures in preparation for SSA conversion */
buzbee52a77fc2012-11-20 19:50:46 -0800902 CompilerInitializeSSAConversion(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700903
Bill Buzbeea114add2012-05-03 15:00:40 -0700904 if (!cUnit->disableDataflow) {
905 /* Find out the "Dalvik reg def x block" relation */
buzbee52a77fc2012-11-20 19:50:46 -0800906 ComputeDefBlockMatrix(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700907
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 /* Insert phi nodes to dominance frontiers for all variables */
buzbee52a77fc2012-11-20 19:50:46 -0800909 InsertPhiNodes(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700910 }
buzbee67bf8852011-08-17 17:51:35 -0700911
Bill Buzbeea114add2012-05-03 15:00:40 -0700912 /* Rename register names by local defs and phi nodes */
buzbee52a77fc2012-11-20 19:50:46 -0800913 DataFlowAnalysisDispatcher(cUnit, ClearVisitedFlag,
Bill Buzbeea114add2012-05-03 15:00:40 -0700914 kAllNodes, false /* isIterative */);
buzbee52a77fc2012-11-20 19:50:46 -0800915 DoDFSPreOrderSSARename(cUnit, cUnit->entryBlock);
buzbee67bf8852011-08-17 17:51:35 -0700916
Bill Buzbeea114add2012-05-03 15:00:40 -0700917 if (!cUnit->disableDataflow) {
918 /*
919 * Shared temp bit vector used by each block to count the number of defs
920 * from all the predecessor blocks.
921 */
buzbee52a77fc2012-11-20 19:50:46 -0800922 cUnit->tempSSARegisterV = AllocBitVector(cUnit, cUnit->numSSARegs,
Bill Buzbeea114add2012-05-03 15:00:40 -0700923 false, kBitMapTempSSARegisterV);
buzbee67bf8852011-08-17 17:51:35 -0700924
buzbee2cfc6392012-05-07 14:51:40 -0700925 cUnit->tempSSABlockIdV =
buzbee52a77fc2012-11-20 19:50:46 -0800926 static_cast<int*>(NewMem(cUnit, sizeof(int) * cUnit->numSSARegs, false, kAllocDFInfo));
buzbee2cfc6392012-05-07 14:51:40 -0700927
Bill Buzbeea114add2012-05-03 15:00:40 -0700928 /* Insert phi-operands with latest SSA names from predecessor blocks */
buzbee52a77fc2012-11-20 19:50:46 -0800929 DataFlowAnalysisDispatcher(cUnit, InsertPhiNodeOperands,
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 kReachableNodes, false /* isIterative */);
931 }
buzbee67bf8852011-08-17 17:51:35 -0700932}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800933
934} // namespace art