blob: 4388041face0e3ea62cc79aa3034c36205c8aed3 [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
Ian Rogerse77493c2014-08-20 15:08:45 -070017#include "base/bit_vector-inl.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080018#include "compiler_internals.h"
Ian Rogers8d3a1172013-06-04 01:13:28 -070019#include "dataflow_iterator-inl.h"
Vladimir Markoc9360ce2014-06-05 20:09:47 +010020#include "utils/scoped_arena_containers.h"
buzbee67bf8852011-08-17 17:51:35 -070021
buzbee1fd33462013-03-25 13:40:45 -070022#define NOTVISITED (-1)
23
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
buzbeea5abf702013-04-12 14:39:29 -070026void MIRGraph::ClearAllVisitedFlags() {
buzbee56c71782013-09-05 17:13:19 -070027 AllNodesIterator iter(this);
buzbeea5abf702013-04-12 14:39:29 -070028 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
29 bb->visited = false;
30 }
31}
32
buzbee311ca162013-02-28 15:56:43 -080033BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
buzbeee5f01222012-06-14 15:19:35 -070034 if (bb != NULL) {
35 if (bb->visited || bb->hidden) {
36 bb = NULL;
37 }
38 }
39 return bb;
40}
41
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070042BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) {
buzbee0d829482013-10-11 15:24:55 -070043 BasicBlock* res = NeedsVisit(GetBasicBlock(bb->fall_through));
buzbeee5f01222012-06-14 15:19:35 -070044 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070045 res = NeedsVisit(GetBasicBlock(bb->taken));
buzbeee5f01222012-06-14 15:19:35 -070046 if (res == NULL) {
buzbee0d829482013-10-11 15:24:55 -070047 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010048 for (SuccessorBlockInfo* sbi : bb->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -070049 res = NeedsVisit(GetBasicBlock(sbi->block));
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070050 if (res != NULL) {
51 break;
52 }
buzbeee5f01222012-06-14 15:19:35 -070053 }
54 }
55 }
56 }
57 return res;
58}
59
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070060void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070061 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080062 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070063 if (block->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010064 dfs_order_.push_back(block->id);
buzbee0d829482013-10-11 15:24:55 -070065 }
buzbeee5f01222012-06-14 15:19:35 -070066}
67
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070068void MIRGraph::RecordDFSOrders(BasicBlock* block) {
Vladimir Markoc9360ce2014-06-05 20:09:47 +010069 DCHECK(temp_scoped_alloc_.get() != nullptr);
70 ScopedArenaVector<BasicBlock*> succ(temp_scoped_alloc_->Adapter());
buzbee311ca162013-02-28 15:56:43 -080071 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070072 succ.push_back(block);
73 while (!succ.empty()) {
74 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080075 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
76 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080077 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080078 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070079 continue;
80 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010081 curr->dfs_id = dfs_post_order_.size();
buzbee0d829482013-10-11 15:24:55 -070082 if (curr->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010083 dfs_post_order_.push_back(curr->id);
buzbee0d829482013-10-11 15:24:55 -070084 }
buzbeee5f01222012-06-14 15:19:35 -070085 succ.pop_back();
86 }
87}
88
buzbee5b537102012-01-17 17:33:47 -080089/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070090void MIRGraph::ComputeDFSOrders() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010091 /* Clear the DFS pre-order and post-order lists. */
92 dfs_order_.clear();
93 dfs_order_.reserve(GetNumBlocks());
94 dfs_post_order_.clear();
95 dfs_post_order_.reserve(GetNumBlocks());
buzbee5b537102012-01-17 17:33:47 -080096
buzbeee5f01222012-06-14 15:19:35 -070097 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -070098 ClearAllVisitedFlags();
99
buzbeee5f01222012-06-14 15:19:35 -0700100 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800101 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700102
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100103 num_reachable_blocks_ = dfs_order_.size();
Andreas Gampe44395962014-06-13 13:44:40 -0700104
105 if (num_reachable_blocks_ != num_blocks_) {
106 // Hide all unreachable blocks.
107 AllNodesIterator iter(this);
108 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
109 if (!bb->visited) {
110 bb->Hide(cu_);
111 }
112 }
113 }
buzbee67bf8852011-08-17 17:51:35 -0700114}
115
116/*
117 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
118 * register idx is defined in BasicBlock bb.
119 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700120bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700121 if (bb->data_flow_info == NULL) {
122 return false;
123 }
buzbee67bf8852011-08-17 17:51:35 -0700124
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100125 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 /* Block bb defines register idx */
Vladimir Marko5229cf12014-10-09 14:57:59 +0100127 temp_bit_matrix_[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 }
129 return true;
buzbee67bf8852011-08-17 17:51:35 -0700130}
131
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700132void MIRGraph::ComputeDefBlockMatrix() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700133 int num_registers = GetNumOfCodeAndTempVRs();
134 /* Allocate num_registers bit vector pointers */
Vladimir Marko5229cf12014-10-09 14:57:59 +0100135 DCHECK(temp_scoped_alloc_ != nullptr);
136 DCHECK(temp_bit_matrix_ == nullptr);
137 temp_bit_matrix_ = static_cast<ArenaBitVector**>(
138 temp_scoped_alloc_->Alloc(sizeof(ArenaBitVector*) * num_registers, kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700139 int i;
buzbee67bf8852011-08-17 17:51:35 -0700140
buzbeefa57c472012-11-21 12:06:18 -0800141 /* Initialize num_register vectors with num_blocks bits each */
142 for (i = 0; i < num_registers; i++) {
Vladimir Marko5229cf12014-10-09 14:57:59 +0100143 temp_bit_matrix_[i] = new (temp_scoped_alloc_.get()) ArenaBitVector(arena_, GetNumBlocks(),
144 false, kBitMapBMatrix);
145 temp_bit_matrix_[i]->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700146 }
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700147
buzbee56c71782013-09-05 17:13:19 -0700148 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800149 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
150 FindLocalLiveIn(bb);
151 }
buzbee56c71782013-09-05 17:13:19 -0700152 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800153 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
154 FillDefBlockMatrix(bb);
155 }
buzbee67bf8852011-08-17 17:51:35 -0700156
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 /*
158 * Also set the incoming parameters as defs in the entry block.
159 * Only need to handle the parameters for the outer method.
160 */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700161 int num_regs = GetNumOfCodeVRs();
162 int in_reg = GetFirstInVR();
buzbeefa57c472012-11-21 12:06:18 -0800163 for (; in_reg < num_regs; in_reg++) {
Vladimir Marko5229cf12014-10-09 14:57:59 +0100164 temp_bit_matrix_[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 }
buzbee67bf8852011-08-17 17:51:35 -0700166}
167
buzbeea5abf702013-04-12 14:39:29 -0700168void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100169 // Clear the dominator post-order list.
170 dom_post_order_traversal_.clear();
171 dom_post_order_traversal_.reserve(num_reachable_blocks_);
172
buzbeea5abf702013-04-12 14:39:29 -0700173 ClearAllVisitedFlags();
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100174 DCHECK(temp_scoped_alloc_.get() != nullptr);
175 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
176 temp_scoped_alloc_->Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700177 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100178 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700179 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100180 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
181 BasicBlock* curr_bb = curr->first;
182 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
183 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
184 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700185 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100186 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
187 if (!curr_idom_iter->Done()) {
188 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
189 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700190 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100191 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700192 } else {
193 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700194 if (curr_bb->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100195 dom_post_order_traversal_.push_back(curr_bb->id);
buzbee0d829482013-10-11 15:24:55 -0700196 }
buzbeea5abf702013-04-12 14:39:29 -0700197 work_stack.pop_back();
buzbee67bf8852011-08-17 17:51:35 -0700198
buzbeea5abf702013-04-12 14:39:29 -0700199 /* hacky loop detection */
buzbee0d829482013-10-11 15:24:55 -0700200 if ((curr_bb->taken != NullBasicBlockId) && curr_bb->dominators->IsBitSet(curr_bb->taken)) {
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000201 curr_bb->nesting_depth++;
buzbeea5abf702013-04-12 14:39:29 -0700202 attributes_ |= METHOD_HAS_LOOP;
203 }
204 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 }
buzbee67bf8852011-08-17 17:51:35 -0700206}
207
buzbee311ca162013-02-28 15:56:43 -0800208void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700209 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 /*
211 * TODO - evaluate whether phi will ever need to be inserted into exit
212 * blocks.
213 */
buzbee0d829482013-10-11 15:24:55 -0700214 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800215 succ_bb->block_type == kDalvikByteCode &&
216 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700217 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700218 }
buzbee67bf8852011-08-17 17:51:35 -0700219}
220
221/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700222bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700223 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700224 if (bb->taken != NullBasicBlockId) {
225 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 }
buzbee0d829482013-10-11 15:24:55 -0700227 if (bb->fall_through != NullBasicBlockId) {
228 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 }
buzbee0d829482013-10-11 15:24:55 -0700230 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100231 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
232 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
233 CheckForDominanceFrontier(bb, succ_bb);
234 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 }
buzbee67bf8852011-08-17 17:51:35 -0700236
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100238 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700239 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100240 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700241 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800242 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700243 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 }
buzbee67bf8852011-08-17 17:51:35 -0700245
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 return true;
buzbee67bf8852011-08-17 17:51:35 -0700247}
248
249/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700250void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800251 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700252
Brian Carlstromdf629502013-07-17 22:39:56 -0700253 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700254 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700255 true /* expandable */, kBitMapDominators);
buzbee862a7602013-04-05 10:58:54 -0700256 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700257 true /* expandable */, kBitMapIDominated);
buzbee862a7602013-04-05 10:58:54 -0700258 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700259 true /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 } else {
buzbee862a7602013-04-05 10:58:54 -0700261 bb->dominators->ClearAllBits();
262 bb->i_dominated->ClearAllBits();
263 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 }
265 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700266 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700267
buzbee862a7602013-04-05 10:58:54 -0700268 return;
buzbee67bf8852011-08-17 17:51:35 -0700269}
270
buzbee5b537102012-01-17 17:33:47 -0800271/*
buzbeefa57c472012-11-21 12:06:18 -0800272 * Walk through the ordered i_dom list until we reach common parent.
273 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800274 * last element of the intersection of block1 and block2 dominators.
275 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700276int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 while (block1 != block2) {
278 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800279 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800281 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800283 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 DCHECK_NE(block2, NOTVISITED);
285 }
286 }
287 return block1;
buzbee5b537102012-01-17 17:33:47 -0800288}
289
290/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700291bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700293 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800294 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700295 }
296
297 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100298 auto it = bb->predecessors.begin(), end = bb->predecessors.end();
Bill Buzbeea114add2012-05-03 15:00:40 -0700299
300 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700301 int idom = -1;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100302 for ( ; ; ++it) {
303 CHECK(it != end);
304 BasicBlock* pred_bb = GetBasicBlock(*it);
305 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800306 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800307 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700308 break;
309 }
310 }
311
312 /* Scan the rest of the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100313 for ( ; it != end; ++it) {
314 BasicBlock* pred_bb = GetBasicBlock(*it);
315 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800316 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 continue;
318 } else {
buzbee311ca162013-02-28 15:56:43 -0800319 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700320 }
321 }
322
323 DCHECK_NE(idom, NOTVISITED);
324
325 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800326 if (i_dom_list_[bb->dfs_id] != idom) {
327 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700328 return true;
329 }
330 return false;
buzbee5b537102012-01-17 17:33:47 -0800331}
332
333/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700334bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800335 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700336 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 } else {
buzbee0d829482013-10-11 15:24:55 -0700338 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 }
buzbee862a7602013-04-05 10:58:54 -0700340 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 return false;
buzbee5b537102012-01-17 17:33:47 -0800342}
343
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700344bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800345 if (bb != GetEntryBlock()) {
346 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800347 DCHECK_NE(idom_dfs_idx, NOTVISITED);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100348 int i_dom_idx = dfs_post_order_[idom_dfs_idx];
buzbee311ca162013-02-28 15:56:43 -0800349 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700350 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800351 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700352 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700353 }
354 return false;
buzbee5b537102012-01-17 17:33:47 -0800355}
356
buzbee67bf8852011-08-17 17:51:35 -0700357/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700358void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800359 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700360
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700362 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800363 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
364 InitializeDominationInfo(bb);
365 }
buzbee67bf8852011-08-17 17:51:35 -0700366
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700367 /* Initialize & Clear i_dom_list */
368 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700369 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000370 kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700371 }
buzbeefa57c472012-11-21 12:06:18 -0800372 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800373 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 }
buzbee5b537102012-01-17 17:33:47 -0800375
buzbeefa57c472012-11-21 12:06:18 -0800376 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800377 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
378 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800379
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700381 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800382 bool change = false;
383 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
384 change = ComputeblockIDom(bb);
385 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700386
387 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700388 GetEntryBlock()->dominators->ClearAllBits();
389 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700390
buzbee0d829482013-10-11 15:24:55 -0700391 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700392
buzbee56c71782013-09-05 17:13:19 -0700393 PreOrderDfsIterator iter3(this);
buzbee311ca162013-02-28 15:56:43 -0800394 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
395 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 }
buzbee67bf8852011-08-17 17:51:35 -0700397
buzbee56c71782013-09-05 17:13:19 -0700398 ReversePostOrderDfsIterator iter4(this);
buzbee311ca162013-02-28 15:56:43 -0800399 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
400 ComputeBlockDominators(bb);
401 }
buzbee5b537102012-01-17 17:33:47 -0800402
buzbeea5abf702013-04-12 14:39:29 -0700403 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800404 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700405 PostOrderDOMIterator iter5(this);
buzbee311ca162013-02-28 15:56:43 -0800406 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
407 ComputeDominanceFrontier(bb);
408 }
buzbee67bf8852011-08-17 17:51:35 -0700409}
410
411/*
412 * Perform dest U= src1 ^ ~src2
413 * This is probably not general enough to be placed in BitVector.[ch].
414 */
buzbee311ca162013-02-28 15:56:43 -0800415void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700416 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700417 if (dest->GetStorageSize() != src1->GetStorageSize() ||
418 dest->GetStorageSize() != src2->GetStorageSize() ||
419 dest->IsExpandable() != src1->IsExpandable() ||
420 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 LOG(FATAL) << "Incompatible set properties";
422 }
buzbee67bf8852011-08-17 17:51:35 -0700423
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700425 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
426 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 }
buzbee67bf8852011-08-17 17:51:35 -0700428}
429
430/*
431 * Iterate through all successor blocks and propagate up the live-in sets.
432 * The calculated result is used for phi-node pruning - where we only need to
433 * insert a phi node if the variable is live-in to the block.
434 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700435bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700436 DCHECK_EQ(temp_bit_vector_size_, cu_->mir_graph.get()->GetNumOfCodeAndTempVRs());
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100437 ArenaBitVector* temp_dalvik_register_v = temp_bit_vector_;
buzbee67bf8852011-08-17 17:51:35 -0700438
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700439 if (bb->data_flow_info == NULL) {
440 return false;
441 }
buzbee862a7602013-04-05 10:58:54 -0700442 temp_dalvik_register_v->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700443 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
444 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
445 if (bb_taken && bb_taken->data_flow_info)
446 ComputeSuccLineIn(temp_dalvik_register_v, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800447 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700448 if (bb_fall_through && bb_fall_through->data_flow_info)
449 ComputeSuccLineIn(temp_dalvik_register_v, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800450 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700451 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100452 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -0700453 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800454 if (succ_bb->data_flow_info) {
buzbee311ca162013-02-28 15:56:43 -0800455 ComputeSuccLineIn(temp_dalvik_register_v, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800456 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 }
buzbee67bf8852011-08-17 17:51:35 -0700458 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 }
buzbee862a7602013-04-05 10:58:54 -0700460 if (!temp_dalvik_register_v->Equal(bb->data_flow_info->live_in_v)) {
461 bb->data_flow_info->live_in_v->Copy(temp_dalvik_register_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 return true;
463 }
464 return false;
buzbee67bf8852011-08-17 17:51:35 -0700465}
466
467/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700468void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800469 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100470 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
471 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
472 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
473 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700474
buzbee56c71782013-09-05 17:13:19 -0700475 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800476 bool change = false;
477 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
478 change = ComputeBlockLiveIns(bb);
479 }
buzbee67bf8852011-08-17 17:51:35 -0700480
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 /* Iterate through each Dalvik register */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700482 for (dalvik_reg = GetNumOfCodeAndTempVRs() - 1; dalvik_reg >= 0; dalvik_reg--) {
Vladimir Marko5229cf12014-10-09 14:57:59 +0100483 input_blocks->Copy(temp_bit_matrix_[dalvik_reg]);
buzbee862a7602013-04-05 10:58:54 -0700484 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100486 // TUNING: When we repeat this, we could skip indexes from the previous pass.
487 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700488 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100489 if (def_bb->dom_frontier != nullptr) {
490 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700491 }
492 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100493 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700494
495 /*
buzbeefa57c472012-11-21 12:06:18 -0800496 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700497 * register is in the live-in set.
498 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100499 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800500 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700502 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
503 continue;
504 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700505 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800506 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800507 phi->dalvikInsn.vA = dalvik_reg;
508 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700509 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700510 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700511 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700512 }
buzbee67bf8852011-08-17 17:51:35 -0700513}
514
515/*
516 * Worker function to insert phi-operands with latest SSA names from
517 * predecessor blocks
518 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700519bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700520 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700521 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800522 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700523 return true;
buzbeefa57c472012-11-21 12:06:18 -0800524 int ssa_reg = mir->ssa_rep->defs[0];
525 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800526 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700527
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100529 size_t num_uses = bb->predecessors.size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700530 AllocateSSAUseData(mir, num_uses);
531 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700532 BasicBlockId* incoming =
533 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000534 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700535 mir->meta.phi_incoming = incoming;
536 int idx = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100537 for (BasicBlockId pred_id : bb->predecessors) {
538 BasicBlock* pred_bb = GetBasicBlock(pred_id);
539 DCHECK(pred_bb != nullptr);
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700540 int ssa_reg = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
buzbee0d829482013-10-11 15:24:55 -0700541 uses[idx] = ssa_reg;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100542 incoming[idx] = pred_id;
buzbee0d829482013-10-11 15:24:55 -0700543 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 }
545 }
546
547 return true;
buzbee67bf8852011-08-17 17:51:35 -0700548}
549
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700550void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700551 if (block->visited || block->hidden) {
552 return;
553 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700554 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700555
Bill Buzbeea114add2012-05-03 15:00:40 -0700556 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800557 DoSSAConversion(block);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700558 int map_size = sizeof(int) * GetNumOfCodeAndTempVRs();
buzbeef0cde542011-09-13 14:55:02 -0700559
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 /* Save SSA map snapshot */
Vladimir Marko5d474472014-03-21 17:10:58 +0000561 ScopedArenaAllocator allocator(&cu_->arena_stack);
buzbee862a7602013-04-05 10:58:54 -0700562 int* saved_ssa_map =
Vladimir Marko5d474472014-03-21 17:10:58 +0000563 static_cast<int*>(allocator.Alloc(map_size, kArenaAllocDalvikToSSAMap));
buzbee311ca162013-02-28 15:56:43 -0800564 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700565
buzbee0d829482013-10-11 15:24:55 -0700566 if (block->fall_through != NullBasicBlockId) {
567 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700568 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800569 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 }
buzbee0d829482013-10-11 15:24:55 -0700571 if (block->taken != NullBasicBlockId) {
572 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800574 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 }
buzbee0d829482013-10-11 15:24:55 -0700576 if (block->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100577 for (SuccessorBlockInfo* successor_block_info : block->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -0700578 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800579 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 /* Restore SSA map snapshot */
buzbee311ca162013-02-28 15:56:43 -0800581 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700582 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700583 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 return;
buzbeef0cde542011-09-13 14:55:02 -0700585}
586
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800587} // namespace art