blob: 6ed666b9f7951c679636e816ceec299cec8d6fc7 [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"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080018#include "base/logging.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080019#include "base/scoped_arena_containers.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020#include "compiler_ir.h"
Ian Rogers8d3a1172013-06-04 01:13:28 -070021#include "dataflow_iterator-inl.h"
buzbee67bf8852011-08-17 17:51:35 -070022
buzbee1fd33462013-03-25 13:40:45 -070023#define NOTVISITED (-1)
24
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080025namespace art {
26
buzbeea5abf702013-04-12 14:39:29 -070027void MIRGraph::ClearAllVisitedFlags() {
buzbee56c71782013-09-05 17:13:19 -070028 AllNodesIterator iter(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070029 for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) {
buzbeea5abf702013-04-12 14:39:29 -070030 bb->visited = false;
31 }
32}
33
buzbee311ca162013-02-28 15:56:43 -080034BasicBlock* MIRGraph::NeedsVisit(BasicBlock* bb) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070035 if (bb != nullptr) {
buzbeee5f01222012-06-14 15:19:35 -070036 if (bb->visited || bb->hidden) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070037 bb = nullptr;
buzbeee5f01222012-06-14 15:19:35 -070038 }
39 }
40 return bb;
41}
42
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070043BasicBlock* MIRGraph::NextUnvisitedSuccessor(BasicBlock* bb) {
buzbee0d829482013-10-11 15:24:55 -070044 BasicBlock* res = NeedsVisit(GetBasicBlock(bb->fall_through));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 if (res == nullptr) {
buzbee0d829482013-10-11 15:24:55 -070046 res = NeedsVisit(GetBasicBlock(bb->taken));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 if (res == nullptr) {
buzbee0d829482013-10-11 15:24:55 -070048 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010049 for (SuccessorBlockInfo* sbi : bb->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -070050 res = NeedsVisit(GetBasicBlock(sbi->block));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 if (res != nullptr) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070052 break;
53 }
buzbeee5f01222012-06-14 15:19:35 -070054 }
55 }
56 }
57 }
58 return res;
59}
60
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070061void MIRGraph::MarkPreOrder(BasicBlock* block) {
buzbeee5f01222012-06-14 15:19:35 -070062 block->visited = true;
buzbeefa57c472012-11-21 12:06:18 -080063 /* Enqueue the pre_order block id */
buzbee0d829482013-10-11 15:24:55 -070064 if (block->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010065 dfs_order_.push_back(block->id);
buzbee0d829482013-10-11 15:24:55 -070066 }
buzbeee5f01222012-06-14 15:19:35 -070067}
68
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070069void MIRGraph::RecordDFSOrders(BasicBlock* block) {
Vladimir Marko7baa6f82014-10-09 18:01:24 +010070 ScopedArenaAllocator allocator(&cu_->arena_stack);
71 ScopedArenaVector<BasicBlock*> succ(allocator.Adapter());
72 succ.reserve(GetNumBlocks());
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);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070078 if (next_successor != nullptr) {
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 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010083 curr->dfs_id = dfs_post_order_.size();
buzbee0d829482013-10-11 15:24:55 -070084 if (curr->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010085 dfs_post_order_.push_back(curr->id);
buzbee0d829482013-10-11 15:24:55 -070086 }
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() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010093 /* Clear the DFS pre-order and post-order lists. */
94 dfs_order_.clear();
95 dfs_order_.reserve(GetNumBlocks());
96 dfs_post_order_.clear();
97 dfs_post_order_.reserve(GetNumBlocks());
buzbee5b537102012-01-17 17:33:47 -080098
buzbeee5f01222012-06-14 15:19:35 -070099 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -0700100 ClearAllVisitedFlags();
101
buzbeee5f01222012-06-14 15:19:35 -0700102 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800103 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700104
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100105 num_reachable_blocks_ = dfs_order_.size();
Andreas Gampe44395962014-06-13 13:44:40 -0700106
Vladimir Markoffda4992014-12-18 17:05:58 +0000107 if (num_reachable_blocks_ != GetNumBlocks()) {
Vladimir Markocb873d82014-12-08 15:16:54 +0000108 // Kill all unreachable blocks.
Andreas Gampe44395962014-06-13 13:44:40 -0700109 AllNodesIterator iter(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700110 for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) {
Andreas Gampe44395962014-06-13 13:44:40 -0700111 if (!bb->visited) {
Vladimir Markocb873d82014-12-08 15:16:54 +0000112 bb->Kill(this);
Andreas Gampe44395962014-06-13 13:44:40 -0700113 }
114 }
115 }
Vladimir Marko312eb252014-10-07 15:01:57 +0100116 dfs_orders_up_to_date_ = true;
buzbee67bf8852011-08-17 17:51:35 -0700117}
118
119/*
120 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
121 * register idx is defined in BasicBlock bb.
122 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700123bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 if (bb->data_flow_info == nullptr) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700125 return false;
126 }
buzbee67bf8852011-08-17 17:51:35 -0700127
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100128 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 /* Block bb defines register idx */
Vladimir Markof585e542014-11-21 13:41:32 +0000130 temp_.ssa.def_block_matrix[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700131 }
132 return true;
buzbee67bf8852011-08-17 17:51:35 -0700133}
134
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700135void MIRGraph::ComputeDefBlockMatrix() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700136 int num_registers = GetNumOfCodeAndTempVRs();
137 /* Allocate num_registers bit vector pointers */
Vladimir Marko5229cf12014-10-09 14:57:59 +0100138 DCHECK(temp_scoped_alloc_ != nullptr);
Vladimir Markof585e542014-11-21 13:41:32 +0000139 DCHECK(temp_.ssa.def_block_matrix == nullptr);
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000140 temp_.ssa.def_block_matrix =
141 temp_scoped_alloc_->AllocArray<ArenaBitVector*>(num_registers, kArenaAllocDFInfo);
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 int i;
buzbee67bf8852011-08-17 17:51:35 -0700143
buzbeefa57c472012-11-21 12:06:18 -0800144 /* Initialize num_register vectors with num_blocks bits each */
145 for (i = 0; i < num_registers; i++) {
Vladimir Markof585e542014-11-21 13:41:32 +0000146 temp_.ssa.def_block_matrix[i] = new (temp_scoped_alloc_.get()) ArenaBitVector(
147 arena_, GetNumBlocks(), false, kBitMapBMatrix);
148 temp_.ssa.def_block_matrix[i]->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700149 }
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700150
buzbee56c71782013-09-05 17:13:19 -0700151 AllNodesIterator iter(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700152 for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800153 FindLocalLiveIn(bb);
154 }
buzbee56c71782013-09-05 17:13:19 -0700155 AllNodesIterator iter2(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 for (BasicBlock* bb = iter2.Next(); bb != nullptr; bb = iter2.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800157 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 */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700164 int num_regs = GetNumOfCodeVRs();
165 int in_reg = GetFirstInVR();
buzbeefa57c472012-11-21 12:06:18 -0800166 for (; in_reg < num_regs; in_reg++) {
Vladimir Markof585e542014-11-21 13:41:32 +0000167 temp_.ssa.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) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100172 // Clear the dominator post-order list.
173 dom_post_order_traversal_.clear();
174 dom_post_order_traversal_.reserve(num_reachable_blocks_);
175
buzbeea5abf702013-04-12 14:39:29 -0700176 ClearAllVisitedFlags();
Vladimir Markoffda4992014-12-18 17:05:58 +0000177 ScopedArenaAllocator allocator(&cu_->arena_stack);
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100178 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
Vladimir Markoffda4992014-12-18 17:05:58 +0000179 allocator.Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700180 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100181 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700182 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100183 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
184 BasicBlock* curr_bb = curr->first;
185 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
186 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
187 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700188 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100189 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
190 if (!curr_idom_iter->Done()) {
191 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
192 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700193 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100194 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700195 } else {
196 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700197 if (curr_bb->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100198 dom_post_order_traversal_.push_back(curr_bb->id);
buzbee0d829482013-10-11 15:24:55 -0700199 }
buzbeea5abf702013-04-12 14:39:29 -0700200 work_stack.pop_back();
buzbeea5abf702013-04-12 14:39:29 -0700201 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 }
buzbee67bf8852011-08-17 17:51:35 -0700203}
204
buzbee311ca162013-02-28 15:56:43 -0800205void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700206 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 /*
208 * TODO - evaluate whether phi will ever need to be inserted into exit
209 * blocks.
210 */
buzbee0d829482013-10-11 15:24:55 -0700211 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800212 succ_bb->block_type == kDalvikByteCode &&
213 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700214 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 }
buzbee67bf8852011-08-17 17:51:35 -0700216}
217
218/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700219bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700221 if (bb->taken != NullBasicBlockId) {
222 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700223 }
buzbee0d829482013-10-11 15:24:55 -0700224 if (bb->fall_through != NullBasicBlockId) {
225 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 }
buzbee0d829482013-10-11 15:24:55 -0700227 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100228 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
229 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
230 CheckForDominanceFrontier(bb, succ_bb);
231 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 }
buzbee67bf8852011-08-17 17:51:35 -0700233
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100235 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700236 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100237 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700238 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800239 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700240 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 }
buzbee67bf8852011-08-17 17:51:35 -0700242
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 return true;
buzbee67bf8852011-08-17 17:51:35 -0700244}
245
246/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700247void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800248 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700249
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700250 if (bb->dominators == nullptr) {
buzbee862a7602013-04-05 10:58:54 -0700251 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700252 true /* expandable */, kBitMapDominators);
buzbee862a7602013-04-05 10:58:54 -0700253 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700254 true /* expandable */, kBitMapIDominated);
buzbee862a7602013-04-05 10:58:54 -0700255 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700256 true /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 } else {
buzbee862a7602013-04-05 10:58:54 -0700258 bb->dominators->ClearAllBits();
259 bb->i_dominated->ClearAllBits();
260 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 }
262 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700263 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700264
buzbee862a7602013-04-05 10:58:54 -0700265 return;
buzbee67bf8852011-08-17 17:51:35 -0700266}
267
buzbee5b537102012-01-17 17:33:47 -0800268/*
buzbeefa57c472012-11-21 12:06:18 -0800269 * Walk through the ordered i_dom list until we reach common parent.
270 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800271 * last element of the intersection of block1 and block2 dominators.
272 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700273int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 while (block1 != block2) {
275 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800276 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800278 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800280 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 DCHECK_NE(block2, NOTVISITED);
282 }
283 }
284 return block1;
buzbee5b537102012-01-17 17:33:47 -0800285}
286
287/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700288bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700290 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800291 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700292 }
293
294 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100295 auto it = bb->predecessors.begin(), end = bb->predecessors.end();
Bill Buzbeea114add2012-05-03 15:00:40 -0700296
297 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700298 int idom = -1;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100299 for ( ; ; ++it) {
300 CHECK(it != end);
301 BasicBlock* pred_bb = GetBasicBlock(*it);
302 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800303 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800304 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 break;
306 }
307 }
308
309 /* Scan the rest of the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100310 for ( ; it != end; ++it) {
311 BasicBlock* pred_bb = GetBasicBlock(*it);
312 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800313 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 continue;
315 } else {
buzbee311ca162013-02-28 15:56:43 -0800316 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 }
318 }
319
320 DCHECK_NE(idom, NOTVISITED);
321
322 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800323 if (i_dom_list_[bb->dfs_id] != idom) {
324 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700325 return true;
326 }
327 return false;
buzbee5b537102012-01-17 17:33:47 -0800328}
329
330/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700331bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800332 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700333 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700334 } else {
buzbee0d829482013-10-11 15:24:55 -0700335 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 }
buzbee862a7602013-04-05 10:58:54 -0700337 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 return false;
buzbee5b537102012-01-17 17:33:47 -0800339}
340
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700341bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800342 if (bb != GetEntryBlock()) {
343 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800344 DCHECK_NE(idom_dfs_idx, NOTVISITED);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100345 int i_dom_idx = dfs_post_order_[idom_dfs_idx];
buzbee311ca162013-02-28 15:56:43 -0800346 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700347 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800348 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700349 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 }
351 return false;
buzbee5b537102012-01-17 17:33:47 -0800352}
353
buzbee67bf8852011-08-17 17:51:35 -0700354/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700355void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800356 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700357
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700359 PreOrderDfsIterator iter(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700360 for (BasicBlock* bb = iter.Next(); bb != nullptr; bb = iter.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800361 InitializeDominationInfo(bb);
362 }
buzbee67bf8852011-08-17 17:51:35 -0700363
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700364 /* Initialize & Clear i_dom_list */
365 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000366 i_dom_list_ = arena_->AllocArray<int>(num_reachable_blocks, kArenaAllocDFInfo);
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 }
buzbeefa57c472012-11-21 12:06:18 -0800368 for (int i = 0; i < num_reachable_blocks; i++) {
buzbee311ca162013-02-28 15:56:43 -0800369 i_dom_list_[i] = NOTVISITED;
Bill Buzbeea114add2012-05-03 15:00:40 -0700370 }
buzbee5b537102012-01-17 17:33:47 -0800371
buzbeefa57c472012-11-21 12:06:18 -0800372 /* For post-order, last block is entry block. Set its i_dom to istelf */
buzbee311ca162013-02-28 15:56:43 -0800373 DCHECK_EQ(GetEntryBlock()->dfs_id, num_reachable_blocks-1);
374 i_dom_list_[GetEntryBlock()->dfs_id] = GetEntryBlock()->dfs_id;
buzbee5b537102012-01-17 17:33:47 -0800375
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 /* Compute the immediate dominators */
buzbee56c71782013-09-05 17:13:19 -0700377 RepeatingReversePostOrderDfsIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800378 bool change = false;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 for (BasicBlock* bb = iter2.Next(false); bb != nullptr; bb = iter2.Next(change)) {
buzbee311ca162013-02-28 15:56:43 -0800380 change = ComputeblockIDom(bb);
381 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700382
383 /* Set the dominator for the root node */
buzbee862a7602013-04-05 10:58:54 -0700384 GetEntryBlock()->dominators->ClearAllBits();
385 GetEntryBlock()->dominators->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700386
buzbee0d829482013-10-11 15:24:55 -0700387 GetEntryBlock()->i_dom = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700388
buzbee56c71782013-09-05 17:13:19 -0700389 PreOrderDfsIterator iter3(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700390 for (BasicBlock* bb = iter3.Next(); bb != nullptr; bb = iter3.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800391 SetDominators(bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 }
buzbee67bf8852011-08-17 17:51:35 -0700393
buzbee56c71782013-09-05 17:13:19 -0700394 ReversePostOrderDfsIterator iter4(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700395 for (BasicBlock* bb = iter4.Next(); bb != nullptr; bb = iter4.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800396 ComputeBlockDominators(bb);
397 }
buzbee5b537102012-01-17 17:33:47 -0800398
buzbeea5abf702013-04-12 14:39:29 -0700399 // Compute the dominance frontier for each block.
buzbee311ca162013-02-28 15:56:43 -0800400 ComputeDomPostOrderTraversal(GetEntryBlock());
buzbee56c71782013-09-05 17:13:19 -0700401 PostOrderDOMIterator iter5(this);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700402 for (BasicBlock* bb = iter5.Next(); bb != nullptr; bb = iter5.Next()) {
buzbee311ca162013-02-28 15:56:43 -0800403 ComputeDominanceFrontier(bb);
404 }
Vladimir Markoffda4992014-12-18 17:05:58 +0000405
406 domination_up_to_date_ = true;
buzbee67bf8852011-08-17 17:51:35 -0700407}
408
409/*
410 * Perform dest U= src1 ^ ~src2
411 * This is probably not general enough to be placed in BitVector.[ch].
412 */
buzbee311ca162013-02-28 15:56:43 -0800413void MIRGraph::ComputeSuccLineIn(ArenaBitVector* dest, const ArenaBitVector* src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700414 const ArenaBitVector* src2) {
buzbee862a7602013-04-05 10:58:54 -0700415 if (dest->GetStorageSize() != src1->GetStorageSize() ||
416 dest->GetStorageSize() != src2->GetStorageSize() ||
417 dest->IsExpandable() != src1->IsExpandable() ||
418 dest->IsExpandable() != src2->IsExpandable()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 LOG(FATAL) << "Incompatible set properties";
420 }
buzbee67bf8852011-08-17 17:51:35 -0700421
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 unsigned int idx;
buzbee862a7602013-04-05 10:58:54 -0700423 for (idx = 0; idx < dest->GetStorageSize(); idx++) {
424 dest->GetRawStorage()[idx] |= src1->GetRawStorageWord(idx) & ~(src2->GetRawStorageWord(idx));
Bill Buzbeea114add2012-05-03 15:00:40 -0700425 }
buzbee67bf8852011-08-17 17:51:35 -0700426}
427
428/*
429 * Iterate through all successor blocks and propagate up the live-in sets.
430 * The calculated result is used for phi-node pruning - where we only need to
431 * insert a phi node if the variable is live-in to the block.
432 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700433bool MIRGraph::ComputeBlockLiveIns(BasicBlock* bb) {
Vladimir Markof585e542014-11-21 13:41:32 +0000434 DCHECK_EQ(temp_.ssa.num_vregs, cu_->mir_graph.get()->GetNumOfCodeAndTempVRs());
435 ArenaBitVector* temp_live_vregs = temp_.ssa.work_live_vregs;
buzbee67bf8852011-08-17 17:51:35 -0700436
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700437 if (bb->data_flow_info == nullptr) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700438 return false;
439 }
Vladimir Markof585e542014-11-21 13:41:32 +0000440 temp_live_vregs->Copy(bb->data_flow_info->live_in_v);
buzbee0d829482013-10-11 15:24:55 -0700441 BasicBlock* bb_taken = GetBasicBlock(bb->taken);
442 BasicBlock* bb_fall_through = GetBasicBlock(bb->fall_through);
443 if (bb_taken && bb_taken->data_flow_info)
Vladimir Markof585e542014-11-21 13:41:32 +0000444 ComputeSuccLineIn(temp_live_vregs, bb_taken->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800445 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700446 if (bb_fall_through && bb_fall_through->data_flow_info)
Vladimir Markof585e542014-11-21 13:41:32 +0000447 ComputeSuccLineIn(temp_live_vregs, bb_fall_through->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800448 bb->data_flow_info->def_v);
buzbee0d829482013-10-11 15:24:55 -0700449 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100450 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -0700451 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbeefa57c472012-11-21 12:06:18 -0800452 if (succ_bb->data_flow_info) {
Vladimir Markof585e542014-11-21 13:41:32 +0000453 ComputeSuccLineIn(temp_live_vregs, succ_bb->data_flow_info->live_in_v,
buzbeefa57c472012-11-21 12:06:18 -0800454 bb->data_flow_info->def_v);
Bill Buzbeea114add2012-05-03 15:00:40 -0700455 }
buzbee67bf8852011-08-17 17:51:35 -0700456 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 }
Vladimir Markof585e542014-11-21 13:41:32 +0000458 if (!temp_live_vregs->Equal(bb->data_flow_info->live_in_v)) {
459 bb->data_flow_info->live_in_v->Copy(temp_live_vregs);
Bill Buzbeea114add2012-05-03 15:00:40 -0700460 return true;
461 }
462 return false;
buzbee67bf8852011-08-17 17:51:35 -0700463}
464
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000465/* For each dalvik reg, find blocks that need phi nodes according to the dominance frontiers. */
466void MIRGraph::FindPhiNodeBlocks() {
buzbee56c71782013-09-05 17:13:19 -0700467 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800468 bool change = false;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700469 for (BasicBlock* bb = iter.Next(false); bb != nullptr; bb = iter.Next(change)) {
buzbee311ca162013-02-28 15:56:43 -0800470 change = ComputeBlockLiveIns(bb);
471 }
buzbee67bf8852011-08-17 17:51:35 -0700472
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000473 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
474 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapBMatrix);
475
476 // Reuse the def_block_matrix storage for phi_node_blocks.
477 ArenaBitVector** def_block_matrix = temp_.ssa.def_block_matrix;
478 ArenaBitVector** phi_node_blocks = def_block_matrix;
479 DCHECK(temp_.ssa.phi_node_blocks == nullptr);
480 temp_.ssa.phi_node_blocks = phi_node_blocks;
481 temp_.ssa.def_block_matrix = nullptr;
482
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 /* Iterate through each Dalvik register */
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000484 for (int dalvik_reg = GetNumOfCodeAndTempVRs() - 1; dalvik_reg >= 0; dalvik_reg--) {
buzbee862a7602013-04-05 10:58:54 -0700485 phi_blocks->ClearAllBits();
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000486 ArenaBitVector* input_blocks = def_block_matrix[dalvik_reg];
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100488 // TUNING: When we repeat this, we could skip indexes from the previous pass.
489 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700490 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100491 if (def_bb->dom_frontier != nullptr) {
492 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700493 }
494 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100495 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700496
Vladimir Marko6a8946b2015-02-09 12:35:05 +0000497 def_block_matrix[dalvik_reg] = phi_blocks;
498 phi_blocks = input_blocks; // Reuse the bit vector in next iteration.
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 }
buzbee67bf8852011-08-17 17:51:35 -0700500}
501
502/*
503 * Worker function to insert phi-operands with latest SSA names from
504 * predecessor blocks
505 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700506bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700507 /* Phi nodes are at the beginning of each block */
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700508 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800509 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 return true;
buzbeefa57c472012-11-21 12:06:18 -0800511 int ssa_reg = mir->ssa_rep->defs[0];
512 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800513 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700514
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100516 size_t num_uses = bb->predecessors.size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700517 AllocateSSAUseData(mir, num_uses);
518 int* uses = mir->ssa_rep->uses;
Vladimir Markoe4fcc5b2015-02-13 10:28:29 +0000519 BasicBlockId* incoming = arena_->AllocArray<BasicBlockId>(num_uses, kArenaAllocDFInfo);
buzbee0d829482013-10-11 15:24:55 -0700520 mir->meta.phi_incoming = incoming;
521 int idx = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100522 for (BasicBlockId pred_id : bb->predecessors) {
523 BasicBlock* pred_bb = GetBasicBlock(pred_id);
524 DCHECK(pred_bb != nullptr);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800525 uses[idx] = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100526 incoming[idx] = pred_id;
buzbee0d829482013-10-11 15:24:55 -0700527 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 }
529 }
530
531 return true;
buzbee67bf8852011-08-17 17:51:35 -0700532}
533
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700534void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700535 if (block->visited || block->hidden) {
536 return;
537 }
buzbeef0cde542011-09-13 14:55:02 -0700538
Chao-ying Fua661d7d2015-08-12 17:52:35 -0700539 typedef struct {
540 BasicBlock* bb;
541 int32_t* ssa_map;
542 } BasicBlockInfo;
543 BasicBlockInfo temp;
buzbeef0cde542011-09-13 14:55:02 -0700544
Vladimir Marko5d474472014-03-21 17:10:58 +0000545 ScopedArenaAllocator allocator(&cu_->arena_stack);
Chao-ying Fua661d7d2015-08-12 17:52:35 -0700546 ScopedArenaVector<BasicBlockInfo> bi_stack(allocator.Adapter());
547 ScopedArenaVector<BasicBlock*> succ_stack(allocator.Adapter());
buzbeef0cde542011-09-13 14:55:02 -0700548
Chao-ying Fua661d7d2015-08-12 17:52:35 -0700549 uint32_t num_vregs = GetNumOfCodeAndTempVRs();
550 size_t map_size = sizeof(int32_t) * num_vregs;
551 temp.bb = block;
552 temp.ssa_map = vreg_to_ssa_map_;
553 bi_stack.push_back(temp);
554
555 while (!bi_stack.empty()) {
556 temp = bi_stack.back();
557 bi_stack.pop_back();
558 BasicBlock* b = temp.bb;
559
560 if (b->visited || b->hidden) {
561 continue;
562 }
563 b->visited = true;
564
565 /* Restore SSA map snapshot, except for the first block */
566 if (b != block) {
567 memcpy(vreg_to_ssa_map_, temp.ssa_map, map_size);
568 }
569
570 /* Process this block */
571 DoSSAConversion(b);
572
573 /* If there are no successor, taken, and fall through blocks, continue */
574 if (b->successor_block_list_type == kNotUsed &&
575 b->taken == NullBasicBlockId &&
576 b->fall_through == NullBasicBlockId) {
577 continue;
578 }
579
580 /* Save SSA map snapshot */
581 int32_t* saved_ssa_map =
582 allocator.AllocArray<int32_t>(num_vregs, kArenaAllocDalvikToSSAMap);
583 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
584
585 if (b->successor_block_list_type != kNotUsed) {
586 for (SuccessorBlockInfo* successor_block_info : b->successor_blocks) {
587 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
588 succ_stack.push_back(succ_bb);
589 }
590 while (!succ_stack.empty()) {
591 temp.bb = succ_stack.back();
592 succ_stack.pop_back();
593 temp.ssa_map = saved_ssa_map;
594 bi_stack.push_back(temp);
595 }
596 }
597 if (b->taken != NullBasicBlockId) {
598 temp.bb = GetBasicBlock(b->taken);
599 temp.ssa_map = saved_ssa_map;
600 bi_stack.push_back(temp);
601 }
602 if (b->fall_through != NullBasicBlockId) {
603 temp.bb = GetBasicBlock(b->fall_through);
604 temp.ssa_map = saved_ssa_map;
605 bi_stack.push_back(temp);
buzbeef0cde542011-09-13 14:55:02 -0700606 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 }
buzbeef0cde542011-09-13 14:55:02 -0700608}
609
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800610} // namespace art