blob: b6c892212cddf0a693d15c0c9e79b38b9bcd16ef [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"
Ian Rogers8d3a1172013-06-04 01:13:28 -070018#include "dataflow_iterator-inl.h"
buzbee67bf8852011-08-17 17:51:35 -070019
buzbee1fd33462013-03-25 13:40:45 -070020#define NOTVISITED (-1)
21
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080022namespace art {
23
buzbeea5abf702013-04-12 14:39:29 -070024void MIRGraph::ClearAllVisitedFlags() {
buzbee56c71782013-09-05 17:13:19 -070025 AllNodesIterator iter(this);
buzbeea5abf702013-04-12 14:39:29 -070026 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
27 bb->visited = false;
28 }
29}
30
buzbee311ca162013-02-28 15:56:43 -080031BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070032 if (bb != NULL) {
33 if (bb->visited || bb->hidden) {
34 bb = NULL;
35 }
36 }
37 return bb;
38}
39
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070040BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) {
buzbee0d829482013-10-11 15:24:55 -070041 BasicBlock* res = NeedsVisit(GetBasicBlock(bb->fall_through));
buzbeee5f01222012-06-14 15:19:35 -070042 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070043 res = NeedsVisit(GetBasicBlock(bb->taken));
buzbeee5f01222012-06-14 15:19:35 -070044 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070045 if (bb->successor_block_list_type != kNotUsed) {
46 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
buzbeee5f01222012-06-14 15:19:35 -070047 while (true) {
buzbee862a7602013-04-05 10:58:54 -070048 SuccessorBlockInfo *sbi = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070049 if (sbi == NULL) {
50 break;
51 }
buzbee0d829482013-10-11 15:24:55 -070052 res = NeedsVisit(GetBasicBlock(sbi->block));
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070053 if (res != NULL) {
54 break;
55 }
buzbeee5f01222012-06-14 15:19:35 -070056 }
57 }
58 }
59 }
60 return res;
61}
62
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070063void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070064 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080065 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070066 if (block->id != NullBasicBlockId) {
67 dfs_order_->Insert(block->id);
68 }
buzbeee5f01222012-06-14 15:19:35 -070069}
70
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070071void MIRGraph::RecordDFSOrders(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070072 std::vector<BasicBlock*> succ;
buzbee311ca162013-02-28 15:56:43 -080073 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070074 succ.push_back(block);
75 while (!succ.empty()) {
76 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080077 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
78 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080079 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080080 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070081 continue;
82 }
buzbee862a7602013-04-05 10:58:54 -070083 curr->dfs_id = dfs_post_order_->Size();
buzbee0d829482013-10-11 15:24:55 -070084 if (curr->id != NullBasicBlockId) {
85 dfs_post_order_->Insert(curr->id);
86 }
buzbeee5f01222012-06-14 15:19:35 -070087 succ.pop_back();
88 }
89}
90
buzbee5b537102012-01-17 17:33:47 -080091/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070092void MIRGraph::ComputeDFSOrders() {
buzbeefa57c472012-11-21 12:06:18 -080093 /* Initialize or reset the DFS pre_order list */
buzbee862a7602013-04-05 10:58:54 -070094 if (dfs_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -070095 dfs_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
96 kGrowableArrayDfsOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070097 } else {
98 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -070099 dfs_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 }
buzbee67bf8852011-08-17 17:51:35 -0700101
buzbeefa57c472012-11-21 12:06:18 -0800102 /* Initialize or reset the DFS post_order list */
buzbee862a7602013-04-05 10:58:54 -0700103 if (dfs_post_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -0700104 dfs_post_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
105 kGrowableArrayDfsPostOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 } else {
107 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700108 dfs_post_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 }
buzbee5b537102012-01-17 17:33:47 -0800110
buzbeee5f01222012-06-14 15:19:35 -0700111 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -0700112 ClearAllVisitedFlags();
113
buzbeee5f01222012-06-14 15:19:35 -0700114 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800115 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700116
buzbee862a7602013-04-05 10:58:54 -0700117 num_reachable_blocks_ = dfs_order_->Size();
buzbee67bf8852011-08-17 17:51:35 -0700118}
119
120/*
121 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
122 * register idx is defined in BasicBlock bb.
123 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700124bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700125 if (bb->data_flow_info == NULL) {
126 return false;
127 }
buzbee67bf8852011-08-17 17:51:35 -0700128
buzbee862a7602013-04-05 10:58:54 -0700129 ArenaBitVector::Iterator iterator(bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700131 int idx = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700132 if (idx == -1) {
133 break;
134 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700135 /* Block bb defines register idx */
buzbee862a7602013-04-05 10:58:54 -0700136 def_block_matrix_[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 }
138 return true;
buzbee67bf8852011-08-17 17:51:35 -0700139}
140
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700141void MIRGraph::ComputeDefBlockMatrix() {
buzbee311ca162013-02-28 15:56:43 -0800142 int num_registers = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -0800143 /* Allocate num_dalvik_registers bit vector pointers */
buzbee311ca162013-02-28 15:56:43 -0800144 def_block_matrix_ = static_cast<ArenaBitVector**>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700145 (arena_->Alloc(sizeof(ArenaBitVector *) * num_registers,
146 ArenaAllocator::kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 int i;
buzbee67bf8852011-08-17 17:51:35 -0700148
buzbeefa57c472012-11-21 12:06:18 -0800149 /* Initialize num_register vectors with num_blocks bits each */
150 for (i = 0; i < num_registers; i++) {
buzbee862a7602013-04-05 10:58:54 -0700151 def_block_matrix_[i] =
152 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapBMatrix);
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 }
buzbee56c71782013-09-05 17:13:19 -0700154 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800155 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
156 FindLocalLiveIn(bb);
157 }
buzbee56c71782013-09-05 17:13:19 -0700158 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800159 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
160 FillDefBlockMatrix(bb);
161 }
buzbee67bf8852011-08-17 17:51:35 -0700162
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 /*
164 * Also set the incoming parameters as defs in the entry block.
165 * Only need to handle the parameters for the outer method.
166 */
buzbee311ca162013-02-28 15:56:43 -0800167 int num_regs = cu_->num_dalvik_registers;
168 int in_reg = num_regs - cu_->num_ins;
buzbeefa57c472012-11-21 12:06:18 -0800169 for (; in_reg < num_regs; in_reg++) {
buzbee862a7602013-04-05 10:58:54 -0700170 def_block_matrix_[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700171 }
buzbee67bf8852011-08-17 17:51:35 -0700172}
173
buzbeea5abf702013-04-12 14:39:29 -0700174void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
175 if (dom_post_order_traversal_ == NULL) {
176 // First time - create the array.
177 dom_post_order_traversal_ =
buzbee0d829482013-10-11 15:24:55 -0700178 new (arena_) GrowableArray<BasicBlockId>(arena_, num_reachable_blocks_,
buzbeea5abf702013-04-12 14:39:29 -0700179 kGrowableArrayDomPostOrderTraversal);
180 } else {
181 dom_post_order_traversal_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 }
buzbeea5abf702013-04-12 14:39:29 -0700183 ClearAllVisitedFlags();
184 std::vector<std::pair<BasicBlock*, ArenaBitVector::Iterator*> > work_stack;
185 bb->visited = true;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700186 work_stack.push_back(std::make_pair(bb, bb->i_dominated->GetIterator()));
buzbeea5abf702013-04-12 14:39:29 -0700187 while (!work_stack.empty()) {
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200188 const std::pair<BasicBlock*, ArenaBitVector::Iterator*>& curr = work_stack.back();
buzbeea5abf702013-04-12 14:39:29 -0700189 BasicBlock* curr_bb = curr.first;
190 ArenaBitVector::Iterator* curr_idom_iter = curr.second;
191 int bb_idx = curr_idom_iter->Next();
192 while ((bb_idx != -1) && (NeedsVisit(GetBasicBlock(bb_idx)) == NULL)) {
193 bb_idx = curr_idom_iter->Next();
194 }
195 if (bb_idx != -1) {
196 BasicBlock* new_bb = GetBasicBlock(bb_idx);
197 new_bb->visited = true;
198 work_stack.push_back(
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700199 std::make_pair(new_bb, new_bb->i_dominated->GetIterator()));
buzbeea5abf702013-04-12 14:39:29 -0700200 } else {
201 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700202 if (curr_bb->id != NullBasicBlockId) {
203 dom_post_order_traversal_->Insert(curr_bb->id);
204 }
buzbeea5abf702013-04-12 14:39:29 -0700205 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700206
buzbeea5abf702013-04-12 14:39:29 -0700207 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700208 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
buzbeea5abf702013-04-12 14:39:29 -0700209 attributes_ |= METHOD_HAS_LOOP;
210 }
211 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 }
buzbee67bf8852011-08-17 17:51:35 -0700213}
214
buzbee311ca162013-02-28 15:56:43 -0800215void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700216 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700217 /*
218 * TODO - evaluate whether phi will ever need to be inserted into exit
219 * blocks.
220 */
buzbee0d829482013-10-11 15:24:55 -0700221 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800222 succ_bb->block_type == kDalvikByteCode &&
223 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700224 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 }
buzbee67bf8852011-08-17 17:51:35 -0700226}
227
228/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700229bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700231 if (bb->taken != NullBasicBlockId) {
232 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 }
buzbee0d829482013-10-11 15:24:55 -0700234 if (bb->fall_through != NullBasicBlockId) {
235 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 }
buzbee0d829482013-10-11 15:24:55 -0700237 if (bb->successor_block_list_type != kNotUsed) {
238 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700240 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700241 if (successor_block_info == NULL) {
242 break;
243 }
buzbee0d829482013-10-11 15:24:55 -0700244 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800245 CheckForDominanceFrontier(bb, succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 }
247 }
buzbee67bf8852011-08-17 17:51:35 -0700248
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 /* Calculate DF_up */
buzbee862a7602013-04-05 10:58:54 -0700250 ArenaBitVector::Iterator bv_iterator(bb->i_dominated);
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 while (true) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700252 // TUNING: hot call to BitVectorIteratorNext
buzbee862a7602013-04-05 10:58:54 -0700253 int dominated_idx = bv_iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700254 if (dominated_idx == -1) {
255 break;
256 }
buzbee311ca162013-02-28 15:56:43 -0800257 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
buzbee862a7602013-04-05 10:58:54 -0700258 ArenaBitVector::Iterator df_iterator(dominated_bb->dom_frontier);
buzbee67bf8852011-08-17 17:51:35 -0700259 while (true) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700260 // TUNING: hot call to BitVectorIteratorNext
buzbee862a7602013-04-05 10:58:54 -0700261 int df_up_idx = df_iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700262 if (df_up_idx == -1) {
263 break;
264 }
buzbee311ca162013-02-28 15:56:43 -0800265 BasicBlock* df_up_block = GetBasicBlock(df_up_idx);
266 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700267 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 }
buzbee67bf8852011-08-17 17:51:35 -0700269
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 return true;
buzbee67bf8852011-08-17 17:51:35 -0700271}
272
273/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700274void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800275 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700276
Brian Carlstromdf629502013-07-17 22:39:56 -0700277 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700278 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
279 false /* expandable */, kBitMapDominators);
280 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
281 false /* expandable */, kBitMapIDominated);
282 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
283 false /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 } else {
buzbee862a7602013-04-05 10:58:54 -0700285 bb->dominators->ClearAllBits();
286 bb->i_dominated->ClearAllBits();
287 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 }
289 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700290 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700291
buzbee862a7602013-04-05 10:58:54 -0700292 return;
buzbee67bf8852011-08-17 17:51:35 -0700293}
294
buzbee5b537102012-01-17 17:33:47 -0800295/*
buzbeefa57c472012-11-21 12:06:18 -0800296 * Walk through the ordered i_dom list until we reach common parent.
297 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800298 * last element of the intersection of block1 and block2 dominators.
299 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700300int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 while (block1 != block2) {
302 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800303 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700304 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800305 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800307 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700308 DCHECK_NE(block2, NOTVISITED);
309 }
310 }
311 return block1;
buzbee5b537102012-01-17 17:33:47 -0800312}
313
314/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700315bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700316 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700317 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800318 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 }
320
321 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700322 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
Bill Buzbeea114add2012-05-03 15:00:40 -0700323
324 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700325 int idom = -1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700326 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700327 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
buzbeefa57c472012-11-21 12:06:18 -0800328 CHECK(pred_bb != NULL);
buzbee311ca162013-02-28 15:56:43 -0800329 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800330 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 break;
332 }
333 }
334
335 /* Scan the rest of the predecessors */
336 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700337 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700338 if (!pred_bb) {
339 break;
340 }
buzbee311ca162013-02-28 15:56:43 -0800341 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700342 continue;
343 } else {
buzbee311ca162013-02-28 15:56:43 -0800344 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700345 }
346 }
347
348 DCHECK_NE(idom, NOTVISITED);
349
350 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800351 if (i_dom_list_[bb->dfs_id] != idom) {
352 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 return true;
354 }
355 return false;
buzbee5b537102012-01-17 17:33:47 -0800356}
357
358/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700359bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800360 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700361 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700362 } else {
buzbee0d829482013-10-11 15:24:55 -0700363 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 }
buzbee862a7602013-04-05 10:58:54 -0700365 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 return false;
buzbee5b537102012-01-17 17:33:47 -0800367}
368
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700369bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800370 if (bb != GetEntryBlock()) {
371 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800372 DCHECK_NE(idom_dfs_idx, NOTVISITED);
buzbee862a7602013-04-05 10:58:54 -0700373 int i_dom_idx = dfs_post_order_->Get(idom_dfs_idx);
buzbee311ca162013-02-28 15:56:43 -0800374 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700375 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800376 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700377 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 }
379 return false;
buzbee5b537102012-01-17 17:33:47 -0800380}
381
buzbee67bf8852011-08-17 17:51:35 -0700382/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700383void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800384 int num_reachable_blocks = num_reachable_blocks_;
385 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700386
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700388 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800389 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
390 InitializeDominationInfo(bb);
391 }
buzbee67bf8852011-08-17 17:51:35 -0700392
buzbeefa57c472012-11-21 12:06:18 -0800393 /* Initalize & Clear i_dom_list */
buzbee311ca162013-02-28 15:56:43 -0800394 if (i_dom_list_ == NULL) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700395 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
396 ArenaAllocator::kAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700397 }
buzbeefa57c472012-11-21 12:06:18 -0800398 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800399 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 }
buzbee5b537102012-01-17 17:33:47 -0800401
buzbeefa57c472012-11-21 12:06:18 -0800402 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800403 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
404 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800405
Bill Buzbeea114add2012-05-03 15:00:40 -0700406 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700407 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800408 bool change = false;
409 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
410 change = ComputeblockIDom(bb);
411 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700412
413 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700414 GetEntryBlock()->dominators->ClearAllBits();
415 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700416
buzbee311ca162013-02-28 15:56:43 -0800417 if (temp_block_v_ == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700418 temp_block_v_ = new (arena_) ArenaBitVector(arena_, num_total_blocks,
419 false /* expandable */, kBitMapTmpBlockV);
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 } else {
buzbee862a7602013-04-05 10:58:54 -0700421 temp_block_v_->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 }
buzbee0d829482013-10-11 15:24:55 -0700423 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700424
buzbee56c71782013-09-05 17:13:19 -0700425 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800426 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
427 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 }
buzbee67bf8852011-08-17 17:51:35 -0700429
buzbee56c71782013-09-05 17:13:19 -0700430 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800431 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
432 ComputeBlockDominators(bb);
433 }
buzbee5b537102012-01-17 17:33:47 -0800434
buzbeea5abf702013-04-12 14:39:29 -0700435 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800436 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700437 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800438 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
439 ComputeDominanceFrontier(bb);
440 }
buzbee67bf8852011-08-17 17:51:35 -0700441}
442
443/*
444 * Perform dest U= src1 ^ ~src2
445 * This is probably not general enough to be placed in BitVector.[ch].
446 */
buzbee311ca162013-02-28 15:56:43 -0800447void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700448 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700449 if (dest->GetStorageSize() != src1->GetStorageSize() ||
450 dest->GetStorageSize() != src2->GetStorageSize() ||
451 dest->IsExpandable() != src1->IsExpandable() ||
452 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700453 LOG(FATAL) << "Incompatible set properties";
454 }
buzbee67bf8852011-08-17 17:51:35 -0700455
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700457 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
458 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 }
buzbee67bf8852011-08-17 17:51:35 -0700460}
461
462/*
463 * Iterate through all successor blocks and propagate up the live-in sets.
464 * The calculated result is used for phi-node pruning - where we only need to
465 * insert a phi node if the variable is live-in to the block.
466 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700467bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800468 ArenaBitVector* temp_dalvik_register_v = temp_dalvik_register_v_;
buzbee67bf8852011-08-17 17:51:35 -0700469
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700470 if (bb->data_flow_info == NULL) {
471 return false;
472 }
buzbee862a7602013-04-05 10:58:54 -0700473 temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700474 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
475 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
476 if (bb_taken && bb_taken->data_flow_info)
477 ComputeSuccLineIn(temp_dalvik_register_v, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800478 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700479 if (bb_fall_through && bb_fall_through->data_flow_info)
480 ComputeSuccLineIn(temp_dalvik_register_v, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800481 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700482 if (bb->successor_block_list_type != kNotUsed) {
483 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700484 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700485 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700486 if (successor_block_info == NULL) {
487 break;
488 }
buzbee0d829482013-10-11 15:24:55 -0700489 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800490 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800491 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800492 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 }
buzbee67bf8852011-08-17 17:51:35 -0700494 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 }
buzbee862a7602013-04-05 10:58:54 -0700496 if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) {
497 bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700498 return true;
499 }
500 return false;
buzbee67bf8852011-08-17 17:51:35 -0700501}
502
503/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700504void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800505 int dalvik_reg;
buzbee862a7602013-04-05 10:58:54 -0700506 ArenaBitVector* phi_blocks =
507 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapPhi);
508 ArenaBitVector* tmp_blocks =
509 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapTmpBlocks);
510 ArenaBitVector* input_blocks =
511 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700512
buzbee311ca162013-02-28 15:56:43 -0800513 temp_dalvik_register_v_ =
buzbee862a7602013-04-05 10:58:54 -0700514 new (arena_) ArenaBitVector(arena_, cu_->num_dalvik_registers, false, kBitMapRegisterV);
buzbee67bf8852011-08-17 17:51:35 -0700515
buzbee56c71782013-09-05 17:13:19 -0700516 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800517 bool change = false;
518 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
519 change = ComputeBlockLiveIns(bb);
520 }
buzbee67bf8852011-08-17 17:51:35 -0700521
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 /* Iterate through each Dalvik register */
buzbee311ca162013-02-28 15:56:43 -0800523 for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 bool change;
buzbee67bf8852011-08-17 17:51:35 -0700525
buzbee862a7602013-04-05 10:58:54 -0700526 input_blocks->Copy(def_block_matrix_[dalvik_reg]);
527 phi_blocks->ClearAllBits();
buzbee67bf8852011-08-17 17:51:35 -0700528
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 /* Calculate the phi blocks for each Dalvik register */
530 do {
531 change = false;
buzbee862a7602013-04-05 10:58:54 -0700532 tmp_blocks->ClearAllBits();
533 ArenaBitVector::Iterator iterator(input_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700534
Bill Buzbeea114add2012-05-03 15:00:40 -0700535 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700536 int idx = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700537 if (idx == -1) {
538 break;
buzbee67bf8852011-08-17 17:51:35 -0700539 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700540 BasicBlock* def_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700541
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700542 /* Merge the dominance frontier to tmp_blocks */
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700543 // TUNING: hot call to Union().
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700544 if (def_bb->dom_frontier != NULL) {
545 tmp_blocks->Union(def_bb->dom_frontier);
546 }
547 }
548 if (!phi_blocks->Equal(tmp_blocks)) {
549 change = true;
550 phi_blocks->Copy(tmp_blocks);
551
552 /*
553 * Iterate through the original blocks plus the new ones in
554 * the dominance frontier.
555 */
556 input_blocks->Copy(phi_blocks);
557 input_blocks->Union(def_block_matrix_[dalvik_reg]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700558 }
559 } while (change);
560
561 /*
buzbeefa57c472012-11-21 12:06:18 -0800562 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700563 * register is in the live-in set.
564 */
buzbee862a7602013-04-05 10:58:54 -0700565 ArenaBitVector::Iterator iterator(phi_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700567 int idx = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700568 if (idx == -1) {
569 break;
570 }
buzbee311ca162013-02-28 15:56:43 -0800571 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700572 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700573 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
574 continue;
575 }
buzbee862a7602013-04-05 10:58:54 -0700576 MIR *phi =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700577 static_cast<MIR*>(arena_->Alloc(sizeof(MIR), ArenaAllocator::kAllocDFInfo));
buzbeecbd6d442012-11-17 14:11:25 -0800578 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800579 phi->dalvikInsn.vA = dalvik_reg;
580 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700581 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
buzbeefa57c472012-11-21 12:06:18 -0800582 PrependMIR(phi_bb, phi);
buzbee67bf8852011-08-17 17:51:35 -0700583 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 }
buzbee67bf8852011-08-17 17:51:35 -0700585}
586
587/*
588 * Worker function to insert phi-operands with latest SSA names from
589 * predecessor blocks
590 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700591bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700593 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800594 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 return true;
buzbeefa57c472012-11-21 12:06:18 -0800596 int ssa_reg = mir->ssa_rep->defs[0];
597 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800598 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700599
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700601 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
602 size_t num_uses = bb->predecessors->Size();
603 mir->ssa_rep->num_uses = num_uses;
604 int* uses = static_cast<int*>(arena_->Alloc(sizeof(int) * num_uses,
605 ArenaAllocator::kAllocDFInfo));
606 mir->ssa_rep->uses = uses;
607 mir->ssa_rep->fp_use =
608 static_cast<bool*>(arena_->Alloc(sizeof(bool) * num_uses, ArenaAllocator::kAllocDFInfo));
609 BasicBlockId* incoming =
610 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
611 ArenaAllocator::kAllocDFInfo));
612 mir->meta.phi_incoming = incoming;
613 int idx = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700614 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700615 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700616 if (!pred_bb) {
617 break;
618 }
buzbeefa57c472012-11-21 12:06:18 -0800619 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map[v_reg];
buzbee0d829482013-10-11 15:24:55 -0700620 uses[idx] = ssa_reg;
621 incoming[idx] = pred_bb->id;
622 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 }
624 }
625
626 return true;
buzbee67bf8852011-08-17 17:51:35 -0700627}
628
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700629void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700630 if (block->visited || block->hidden) {
631 return;
632 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700634
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800636 DoSSAConversion(block);
637 int map_size = sizeof(int) * cu_->num_dalvik_registers;
buzbeef0cde542011-09-13 14:55:02 -0700638
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 /* Save SSA map snapshot */
buzbee862a7602013-04-05 10:58:54 -0700640 int* saved_ssa_map =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700641 static_cast<int*>(arena_->Alloc(map_size, ArenaAllocator::kAllocDalvikToSSAMap));
buzbee311ca162013-02-28 15:56:43 -0800642 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700643
buzbee0d829482013-10-11 15:24:55 -0700644 if (block->fall_through != NullBasicBlockId) {
645 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800647 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 }
buzbee0d829482013-10-11 15:24:55 -0700649 if (block->taken != NullBasicBlockId) {
650 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800652 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 }
buzbee0d829482013-10-11 15:24:55 -0700654 if (block->successor_block_list_type != kNotUsed) {
655 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(block->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700657 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700658 if (successor_block_info == NULL) {
659 break;
660 }
buzbee0d829482013-10-11 15:24:55 -0700661 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800662 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800664 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700665 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 }
buzbee311ca162013-02-28 15:56:43 -0800667 vreg_to_ssa_map_ = saved_ssa_map;
Bill Buzbeea114add2012-05-03 15:00:40 -0700668 return;
buzbeef0cde542011-09-13 14:55:02 -0700669}
670
buzbee67bf8852011-08-17 17:51:35 -0700671/* Perform SSA transformation for the whole method */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700672void MIRGraph::SSATransformation() {
Bill Buzbeea114add2012-05-03 15:00:40 -0700673 /* Compute the DFS order */
buzbee311ca162013-02-28 15:56:43 -0800674 ComputeDFSOrders();
buzbee67bf8852011-08-17 17:51:35 -0700675
buzbee1fd33462013-03-25 13:40:45 -0700676 /* Compute the dominator info */
677 ComputeDominators();
buzbee67bf8852011-08-17 17:51:35 -0700678
Bill Buzbeea114add2012-05-03 15:00:40 -0700679 /* Allocate data structures in preparation for SSA conversion */
buzbee311ca162013-02-28 15:56:43 -0800680 CompilerInitializeSSAConversion();
buzbee67bf8852011-08-17 17:51:35 -0700681
buzbee1fd33462013-03-25 13:40:45 -0700682 /* Find out the "Dalvik reg def x block" relation */
683 ComputeDefBlockMatrix();
buzbee67bf8852011-08-17 17:51:35 -0700684
buzbee1fd33462013-03-25 13:40:45 -0700685 /* Insert phi nodes to dominance frontiers for all variables */
686 InsertPhiNodes();
buzbee67bf8852011-08-17 17:51:35 -0700687
Bill Buzbeea114add2012-05-03 15:00:40 -0700688 /* Rename register names by local defs and phi nodes */
buzbeea5abf702013-04-12 14:39:29 -0700689 ClearAllVisitedFlags();
buzbee311ca162013-02-28 15:56:43 -0800690 DoDFSPreOrderSSARename(GetEntryBlock());
buzbee67bf8852011-08-17 17:51:35 -0700691
buzbee1fd33462013-03-25 13:40:45 -0700692 /*
693 * Shared temp bit vector used by each block to count the number of defs
694 * from all the predecessor blocks.
695 */
buzbee862a7602013-04-05 10:58:54 -0700696 temp_ssa_register_v_ =
697 new (arena_) ArenaBitVector(arena_, GetNumSSARegs(), false, kBitMapTempSSARegisterV);
buzbee2cfc6392012-05-07 14:51:40 -0700698
buzbee1fd33462013-03-25 13:40:45 -0700699 /* Insert phi-operands with latest SSA names from predecessor blocks */
buzbee56c71782013-09-05 17:13:19 -0700700 PreOrderDfsIterator iter2(this);
buzbee1fd33462013-03-25 13:40:45 -0700701 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
702 InsertPhiNodeOperands(bb);
buzbee311ca162013-02-28 15:56:43 -0800703 }
buzbee1fd33462013-03-25 13:40:45 -0700704
buzbee311ca162013-02-28 15:56:43 -0800705 if (cu_->enable_debug & (1 << kDebugDumpCFG)) {
706 DumpCFG("/sdcard/3_post_ssa_cfg/", false);
Bill Buzbeea114add2012-05-03 15:00:40 -0700707 }
buzbee1fd33462013-03-25 13:40:45 -0700708 if (cu_->enable_debug & (1 << kDebugVerifyDataflow)) {
709 VerifyDataflow();
710 }
buzbee67bf8852011-08-17 17:51:35 -0700711}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800712
713} // namespace art