blob: 43243254f166bec1fe1e5e8705ed299bbea4228d [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"
Vladimir Markoc9360ce2014-06-05 20:09:47 +010019#include "utils/scoped_arena_containers.h"
buzbee67bf8852011-08-17 17:51:35 -070020
buzbee1fd33462013-03-25 13:40:45 -070021#define NOTVISITED (-1)
22
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080023namespace art {
24
buzbeea5abf702013-04-12 14:39:29 -070025void MIRGraph::ClearAllVisitedFlags() {
buzbee56c71782013-09-05 17:13:19 -070026 AllNodesIterator iter(this);
buzbeea5abf702013-04-12 14:39:29 -070027 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
28 bb->visited = false;
29 }
30}
31
buzbee311ca162013-02-28 15:56:43 -080032BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070033 if (bb != NULL) {
34 if (bb->visited || bb->hidden) {
35 bb = NULL;
36 }
37 }
38 return bb;
39}
40
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070041BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) {
buzbee0d829482013-10-11 15:24:55 -070042 BasicBlock* res = NeedsVisit(GetBasicBlock(bb->fall_through));
buzbeee5f01222012-06-14 15:19:35 -070043 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070044 res = NeedsVisit(GetBasicBlock(bb->taken));
buzbeee5f01222012-06-14 15:19:35 -070045 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070046 if (bb->successor_block_list_type != kNotUsed) {
47 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
buzbeee5f01222012-06-14 15:19:35 -070048 while (true) {
buzbee862a7602013-04-05 10:58:54 -070049 SuccessorBlockInfo *sbi = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070050 if (sbi == NULL) {
51 break;
52 }
buzbee0d829482013-10-11 15:24:55 -070053 res = NeedsVisit(GetBasicBlock(sbi->block));
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070054 if (res != NULL) {
55 break;
56 }
buzbeee5f01222012-06-14 15:19:35 -070057 }
58 }
59 }
60 }
61 return res;
62}
63
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070064void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070065 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080066 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070067 if (block->id != NullBasicBlockId) {
68 dfs_order_->Insert(block->id);
69 }
buzbeee5f01222012-06-14 15:19:35 -070070}
71
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070072void MIRGraph::RecordDFSOrders(BasicBlock* block) {
Vladimir Markoc9360ce2014-06-05 20:09:47 +010073 DCHECK(temp_scoped_alloc_.get() != nullptr);
74 ScopedArenaVector<BasicBlock*> succ(temp_scoped_alloc_->Adapter());
buzbee311ca162013-02-28 15:56:43 -080075 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070076 succ.push_back(block);
77 while (!succ.empty()) {
78 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080079 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
80 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080081 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080082 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070083 continue;
84 }
buzbee862a7602013-04-05 10:58:54 -070085 curr->dfs_id = dfs_post_order_->Size();
buzbee0d829482013-10-11 15:24:55 -070086 if (curr->id != NullBasicBlockId) {
87 dfs_post_order_->Insert(curr->id);
88 }
buzbeee5f01222012-06-14 15:19:35 -070089 succ.pop_back();
90 }
91}
92
buzbee5b537102012-01-17 17:33:47 -080093/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070094void MIRGraph::ComputeDFSOrders() {
buzbeefa57c472012-11-21 12:06:18 -080095 /* Initialize or reset the DFS pre_order list */
buzbee862a7602013-04-05 10:58:54 -070096 if (dfs_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -070097 dfs_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
98 kGrowableArrayDfsOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -070099 } else {
100 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700101 dfs_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 }
buzbee67bf8852011-08-17 17:51:35 -0700103
buzbeefa57c472012-11-21 12:06:18 -0800104 /* Initialize or reset the DFS post_order list */
buzbee862a7602013-04-05 10:58:54 -0700105 if (dfs_post_order_ == NULL) {
buzbee0d829482013-10-11 15:24:55 -0700106 dfs_post_order_ = new (arena_) GrowableArray<BasicBlockId>(arena_, GetNumBlocks(),
107 kGrowableArrayDfsPostOrder);
Bill Buzbeea114add2012-05-03 15:00:40 -0700108 } else {
109 /* Just reset the used length on the counter */
buzbee862a7602013-04-05 10:58:54 -0700110 dfs_post_order_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 }
buzbee5b537102012-01-17 17:33:47 -0800112
buzbeee5f01222012-06-14 15:19:35 -0700113 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -0700114 ClearAllVisitedFlags();
115
buzbeee5f01222012-06-14 15:19:35 -0700116 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800117 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700118
buzbee862a7602013-04-05 10:58:54 -0700119 num_reachable_blocks_ = dfs_order_->Size();
buzbee67bf8852011-08-17 17:51:35 -0700120}
121
122/*
123 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
124 * register idx is defined in BasicBlock bb.
125 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700126bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700127 if (bb->data_flow_info == NULL) {
128 return false;
129 }
buzbee67bf8852011-08-17 17:51:35 -0700130
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100131 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 /* Block bb defines register idx */
buzbee862a7602013-04-05 10:58:54 -0700133 def_block_matrix_[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 }
135 return true;
buzbee67bf8852011-08-17 17:51:35 -0700136}
137
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700138void MIRGraph::ComputeDefBlockMatrix() {
buzbee311ca162013-02-28 15:56:43 -0800139 int num_registers = cu_->num_dalvik_registers;
buzbeefa57c472012-11-21 12:06:18 -0800140 /* Allocate num_dalvik_registers bit vector pointers */
buzbee311ca162013-02-28 15:56:43 -0800141 def_block_matrix_ = static_cast<ArenaBitVector**>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700142 (arena_->Alloc(sizeof(ArenaBitVector *) * num_registers,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000143 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 int i;
buzbee67bf8852011-08-17 17:51:35 -0700145
buzbeefa57c472012-11-21 12:06:18 -0800146 /* Initialize num_register vectors with num_blocks bits each */
147 for (i = 0; i < num_registers; i++) {
buzbee862a7602013-04-05 10:58:54 -0700148 def_block_matrix_[i] =
149 new (arena_) ArenaBitVector(arena_, GetNumBlocks(), false, kBitMapBMatrix);
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 }
buzbee56c71782013-09-05 17:13:19 -0700151 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800152 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
153 FindLocalLiveIn(bb);
154 }
buzbee56c71782013-09-05 17:13:19 -0700155 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800156 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
157 FillDefBlockMatrix(bb);
158 }
buzbee67bf8852011-08-17 17:51:35 -0700159
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 /*
161 * Also set the incoming parameters as defs in the entry block.
162 * Only need to handle the parameters for the outer method.
163 */
buzbee311ca162013-02-28 15:56:43 -0800164 int num_regs = cu_->num_dalvik_registers;
165 int in_reg = num_regs - cu_->num_ins;
buzbeefa57c472012-11-21 12:06:18 -0800166 for (; in_reg < num_regs; in_reg++) {
buzbee862a7602013-04-05 10:58:54 -0700167 def_block_matrix_[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 }
buzbee67bf8852011-08-17 17:51:35 -0700169}
170
buzbeea5abf702013-04-12 14:39:29 -0700171void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700172 if (dom_post_order_traversal_ == NULL || max_num_reachable_blocks_ < num_reachable_blocks_) {
173 // First time or too small - create the array.
buzbeea5abf702013-04-12 14:39:29 -0700174 dom_post_order_traversal_ =
buzbee0d829482013-10-11 15:24:55 -0700175 new (arena_) GrowableArray<BasicBlockId>(arena_, num_reachable_blocks_,
buzbeea5abf702013-04-12 14:39:29 -0700176 kGrowableArrayDomPostOrderTraversal);
177 } else {
178 dom_post_order_traversal_->Reset();
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 }
buzbeea5abf702013-04-12 14:39:29 -0700180 ClearAllVisitedFlags();
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100181 DCHECK(temp_scoped_alloc_.get() != nullptr);
182 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
183 temp_scoped_alloc_->Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700184 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100185 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700186 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100187 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
188 BasicBlock* curr_bb = curr->first;
189 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
190 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
191 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700192 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100193 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
194 if (!curr_idom_iter->Done()) {
195 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
196 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700197 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100198 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700199 } else {
200 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700201 if (curr_bb->id != NullBasicBlockId) {
202 dom_post_order_traversal_->Insert(curr_bb->id);
203 }
buzbeea5abf702013-04-12 14:39:29 -0700204 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700205
buzbeea5abf702013-04-12 14:39:29 -0700206 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700207 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000208 curr_bb->nesting_depth++;
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 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100250 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700251 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100252 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700253 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800254 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700255 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 }
buzbee67bf8852011-08-17 17:51:35 -0700257
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 return true;
buzbee67bf8852011-08-17 17:51:35 -0700259}
260
261/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700262void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800263 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700264
Brian Carlstromdf629502013-07-17 22:39:56 -0700265 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700266 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
267 false /* expandable */, kBitMapDominators);
268 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
269 false /* expandable */, kBitMapIDominated);
270 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
271 false /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 } else {
buzbee862a7602013-04-05 10:58:54 -0700273 bb->dominators->ClearAllBits();
274 bb->i_dominated->ClearAllBits();
275 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 }
277 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700278 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700279
buzbee862a7602013-04-05 10:58:54 -0700280 return;
buzbee67bf8852011-08-17 17:51:35 -0700281}
282
buzbee5b537102012-01-17 17:33:47 -0800283/*
buzbeefa57c472012-11-21 12:06:18 -0800284 * Walk through the ordered i_dom list until we reach common parent.
285 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800286 * last element of the intersection of block1 and block2 dominators.
287 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700288int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 while (block1 != block2) {
290 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800291 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800293 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800295 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 DCHECK_NE(block2, NOTVISITED);
297 }
298 }
299 return block1;
buzbee5b537102012-01-17 17:33:47 -0800300}
301
302/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700303bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700304 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700305 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800306 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700307 }
308
309 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700310 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
Bill Buzbeea114add2012-05-03 15:00:40 -0700311
312 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700313 int idom = -1;
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700315 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
buzbeefa57c472012-11-21 12:06:18 -0800316 CHECK(pred_bb != NULL);
buzbee311ca162013-02-28 15:56:43 -0800317 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800318 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 break;
320 }
321 }
322
323 /* Scan the rest of the predecessors */
324 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700325 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700326 if (!pred_bb) {
327 break;
328 }
buzbee311ca162013-02-28 15:56:43 -0800329 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700330 continue;
331 } else {
buzbee311ca162013-02-28 15:56:43 -0800332 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 }
334 }
335
336 DCHECK_NE(idom, NOTVISITED);
337
338 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800339 if (i_dom_list_[bb->dfs_id] != idom) {
340 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 return true;
342 }
343 return false;
buzbee5b537102012-01-17 17:33:47 -0800344}
345
346/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700347bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800348 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700349 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 } else {
buzbee0d829482013-10-11 15:24:55 -0700351 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 }
buzbee862a7602013-04-05 10:58:54 -0700353 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 return false;
buzbee5b537102012-01-17 17:33:47 -0800355}
356
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700357bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800358 if (bb != GetEntryBlock()) {
359 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800360 DCHECK_NE(idom_dfs_idx, NOTVISITED);
buzbee862a7602013-04-05 10:58:54 -0700361 int i_dom_idx = dfs_post_order_->Get(idom_dfs_idx);
buzbee311ca162013-02-28 15:56:43 -0800362 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700363 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800364 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700365 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 }
367 return false;
buzbee5b537102012-01-17 17:33:47 -0800368}
369
buzbee67bf8852011-08-17 17:51:35 -0700370/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700371void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800372 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700373
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700375 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800376 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
377 InitializeDominationInfo(bb);
378 }
buzbee67bf8852011-08-17 17:51:35 -0700379
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700380 /* Initialize & Clear i_dom_list */
381 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700382 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000383 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 }
buzbeefa57c472012-11-21 12:06:18 -0800385 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800386 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 }
buzbee5b537102012-01-17 17:33:47 -0800388
buzbeefa57c472012-11-21 12:06:18 -0800389 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800390 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
391 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800392
Bill Buzbeea114add2012-05-03 15:00:40 -0700393 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700394 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800395 bool change = false;
396 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
397 change = ComputeblockIDom(bb);
398 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700399
400 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700401 GetEntryBlock()->dominators->ClearAllBits();
402 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700403
buzbee0d829482013-10-11 15:24:55 -0700404 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700405
buzbee56c71782013-09-05 17:13:19 -0700406 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800407 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
408 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 }
buzbee67bf8852011-08-17 17:51:35 -0700410
buzbee56c71782013-09-05 17:13:19 -0700411 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800412 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
413 ComputeBlockDominators(bb);
414 }
buzbee5b537102012-01-17 17:33:47 -0800415
buzbeea5abf702013-04-12 14:39:29 -0700416 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800417 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700418 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800419 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
420 ComputeDominanceFrontier(bb);
421 }
buzbee67bf8852011-08-17 17:51:35 -0700422}
423
424/*
425 * Perform dest U= src1 ^ ~src2
426 * This is probably not general enough to be placed in BitVector.[ch].
427 */
buzbee311ca162013-02-28 15:56:43 -0800428void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700429 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700430 if (dest->GetStorageSize() != src1->GetStorageSize() ||
431 dest->GetStorageSize() != src2->GetStorageSize() ||
432 dest->IsExpandable() != src1->IsExpandable() ||
433 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 LOG(FATAL) << "Incompatible set properties";
435 }
buzbee67bf8852011-08-17 17:51:35 -0700436
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700438 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
439 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700440 }
buzbee67bf8852011-08-17 17:51:35 -0700441}
442
443/*
444 * Iterate through all successor blocks and propagate up the live-in sets.
445 * The calculated result is used for phi-node pruning - where we only need to
446 * insert a phi node if the variable is live-in to the block.
447 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700448bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100449 DCHECK_EQ(temp_bit_vector_size_, cu_->num_dalvik_registers);
450 ArenaBitVector* temp_dalvik_register_v = temp_bit_vector_;
buzbee67bf8852011-08-17 17:51:35 -0700451
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700452 if (bb->data_flow_info == NULL) {
453 return false;
454 }
buzbee862a7602013-04-05 10:58:54 -0700455 temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700456 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
457 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
458 if (bb_taken && bb_taken->data_flow_info)
459 ComputeSuccLineIn(temp_dalvik_register_v, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800460 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700461 if (bb_fall_through && bb_fall_through->data_flow_info)
462 ComputeSuccLineIn(temp_dalvik_register_v, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800463 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700464 if (bb->successor_block_list_type != kNotUsed) {
465 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(bb->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700466 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700467 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700468 if (successor_block_info == NULL) {
469 break;
470 }
buzbee0d829482013-10-11 15:24:55 -0700471 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800472 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800473 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800474 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 }
buzbee67bf8852011-08-17 17:51:35 -0700476 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700477 }
buzbee862a7602013-04-05 10:58:54 -0700478 if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) {
479 bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 return true;
481 }
482 return false;
buzbee67bf8852011-08-17 17:51:35 -0700483}
484
485/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700486void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800487 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100488 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
489 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
490 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
491 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700492
buzbee56c71782013-09-05 17:13:19 -0700493 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800494 bool change = false;
495 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
496 change = ComputeBlockLiveIns(bb);
497 }
buzbee67bf8852011-08-17 17:51:35 -0700498
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 /* Iterate through each Dalvik register */
buzbee311ca162013-02-28 15:56:43 -0800500 for (dalvik_reg = cu_->num_dalvik_registers - 1; dalvik_reg >= 0; dalvik_reg--) {
buzbee862a7602013-04-05 10:58:54 -0700501 input_blocks->Copy(def_block_matrix_[dalvik_reg]);
502 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100504 // TUNING: When we repeat this, we could skip indexes from the previous pass.
505 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700506 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100507 if (def_bb->dom_frontier != nullptr) {
508 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700509 }
510 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100511 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700512
513 /*
buzbeefa57c472012-11-21 12:06:18 -0800514 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 * register is in the live-in set.
516 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100517 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800518 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700519 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700520 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
521 continue;
522 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700523 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800524 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800525 phi->dalvikInsn.vA = dalvik_reg;
526 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700527 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700528 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700529 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 }
buzbee67bf8852011-08-17 17:51:35 -0700531}
532
533/*
534 * Worker function to insert phi-operands with latest SSA names from
535 * predecessor blocks
536 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700537bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700539 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800540 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 return true;
buzbeefa57c472012-11-21 12:06:18 -0800542 int ssa_reg = mir->ssa_rep->defs[0];
543 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800544 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700545
Bill Buzbeea114add2012-05-03 15:00:40 -0700546 /* Iterate through the predecessors */
buzbee0d829482013-10-11 15:24:55 -0700547 GrowableArray<BasicBlockId>::Iterator iter(bb->predecessors);
548 size_t num_uses = bb->predecessors->Size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700549 AllocateSSAUseData(mir, num_uses);
550 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700551 BasicBlockId* incoming =
552 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000553 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700554 mir->meta.phi_incoming = incoming;
555 int idx = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700557 BasicBlock* pred_bb = GetBasicBlock(iter.Next());
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700558 if (!pred_bb) {
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700559 break;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700560 }
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700561 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
buzbee0d829482013-10-11 15:24:55 -0700562 uses[idx] = ssa_reg;
563 incoming[idx] = pred_bb->id;
564 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 }
566 }
567
568 return true;
buzbee67bf8852011-08-17 17:51:35 -0700569}
570
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700571void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700572 if (block->visited || block->hidden) {
573 return;
574 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700576
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800578 DoSSAConversion(block);
579 int map_size = sizeof(int) * cu_->num_dalvik_registers;
buzbeef0cde542011-09-13 14:55:02 -0700580
Bill Buzbeea114add2012-05-03 15:00:40 -0700581 /* Save SSA map snapshot */
Vladimir Marko5d474472014-03-21 17:10:58 +0000582 ScopedArenaAllocator allocator(&cu_->arena_stack);
buzbee862a7602013-04-05 10:58:54 -0700583 int* saved_ssa_map =
Vladimir Marko5d474472014-03-21 17:10:58 +0000584 static_cast<int*>(allocator.Alloc(map_size, kArenaAllocDalvikToSSAMap));
buzbee311ca162013-02-28 15:56:43 -0800585 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700586
buzbee0d829482013-10-11 15:24:55 -0700587 if (block->fall_through != NullBasicBlockId) {
588 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700589 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800590 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 }
buzbee0d829482013-10-11 15:24:55 -0700592 if (block->taken != NullBasicBlockId) {
593 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800595 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 }
buzbee0d829482013-10-11 15:24:55 -0700597 if (block->successor_block_list_type != kNotUsed) {
598 GrowableArray<SuccessorBlockInfo*>::Iterator iterator(block->successor_blocks);
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 while (true) {
buzbee862a7602013-04-05 10:58:54 -0700600 SuccessorBlockInfo *successor_block_info = iterator.Next();
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700601 if (successor_block_info == NULL) {
602 break;
603 }
buzbee0d829482013-10-11 15:24:55 -0700604 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800605 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700606 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800607 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700608 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700610 return;
buzbeef0cde542011-09-13 14:55:02 -0700611}
612
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800613} // namespace art