blob: 3905649ac6dc45338b7b7aad9f6b7270a8306281 [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 Marko7baa6f82014-10-09 18:01:24 +010069 ScopedArenaAllocator allocator(&cu_->arena_stack);
70 ScopedArenaVector<BasicBlock*> succ(allocator.Adapter());
71 succ.reserve(GetNumBlocks());
buzbee311ca162013-02-28 15:56:43 -080072 MarkPreOrder(block);
buzbeee5f01222012-06-14 15:19:35 -070073 succ.push_back(block);
74 while (!succ.empty()) {
75 BasicBlock* curr = succ.back();
buzbeefa57c472012-11-21 12:06:18 -080076 BasicBlock* next_successor = NextUnvisitedSuccessor(curr);
77 if (next_successor != NULL) {
buzbee311ca162013-02-28 15:56:43 -080078 MarkPreOrder(next_successor);
buzbeefa57c472012-11-21 12:06:18 -080079 succ.push_back(next_successor);
buzbeee5f01222012-06-14 15:19:35 -070080 continue;
81 }
Vladimir Markoe39c54e2014-09-22 14:50:02 +010082 curr->dfs_id = dfs_post_order_.size();
buzbee0d829482013-10-11 15:24:55 -070083 if (curr->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010084 dfs_post_order_.push_back(curr->id);
buzbee0d829482013-10-11 15:24:55 -070085 }
buzbeee5f01222012-06-14 15:19:35 -070086 succ.pop_back();
87 }
88}
89
buzbee5b537102012-01-17 17:33:47 -080090/* Sort the blocks by the Depth-First-Search */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070091void MIRGraph::ComputeDFSOrders() {
Vladimir Markoe39c54e2014-09-22 14:50:02 +010092 /* Clear the DFS pre-order and post-order lists. */
93 dfs_order_.clear();
94 dfs_order_.reserve(GetNumBlocks());
95 dfs_post_order_.clear();
96 dfs_post_order_.reserve(GetNumBlocks());
buzbee5b537102012-01-17 17:33:47 -080097
buzbeee5f01222012-06-14 15:19:35 -070098 // Reset visited flags from all nodes
buzbeea5abf702013-04-12 14:39:29 -070099 ClearAllVisitedFlags();
100
buzbeee5f01222012-06-14 15:19:35 -0700101 // Record dfs orders
buzbee311ca162013-02-28 15:56:43 -0800102 RecordDFSOrders(GetEntryBlock());
buzbeee5f01222012-06-14 15:19:35 -0700103
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100104 num_reachable_blocks_ = dfs_order_.size();
Andreas Gampe44395962014-06-13 13:44:40 -0700105
Vladimir Markoffda4992014-12-18 17:05:58 +0000106 if (num_reachable_blocks_ != GetNumBlocks()) {
Vladimir Markocb873d82014-12-08 15:16:54 +0000107 // Kill all unreachable blocks.
Andreas Gampe44395962014-06-13 13:44:40 -0700108 AllNodesIterator iter(this);
109 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
110 if (!bb->visited) {
Vladimir Markocb873d82014-12-08 15:16:54 +0000111 bb->Kill(this);
Andreas Gampe44395962014-06-13 13:44:40 -0700112 }
113 }
114 }
Vladimir Marko312eb252014-10-07 15:01:57 +0100115 dfs_orders_up_to_date_ = true;
buzbee67bf8852011-08-17 17:51:35 -0700116}
117
118/*
119 * Mark block bit on the per-Dalvik register vector to denote that Dalvik
120 * register idx is defined in BasicBlock bb.
121 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700122bool MIRGraph::FillDefBlockMatrix(BasicBlock* bb) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700123 if (bb->data_flow_info == NULL) {
124 return false;
125 }
buzbee67bf8852011-08-17 17:51:35 -0700126
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100127 for (uint32_t idx : bb->data_flow_info->def_v->Indexes()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 /* Block bb defines register idx */
Vladimir Markof585e542014-11-21 13:41:32 +0000129 temp_.ssa.def_block_matrix[idx]->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 }
131 return true;
buzbee67bf8852011-08-17 17:51:35 -0700132}
133
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700134void MIRGraph::ComputeDefBlockMatrix() {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700135 int num_registers = GetNumOfCodeAndTempVRs();
136 /* Allocate num_registers bit vector pointers */
Vladimir Marko5229cf12014-10-09 14:57:59 +0100137 DCHECK(temp_scoped_alloc_ != nullptr);
Vladimir Markof585e542014-11-21 13:41:32 +0000138 DCHECK(temp_.ssa.def_block_matrix == nullptr);
139 temp_.ssa.def_block_matrix = static_cast<ArenaBitVector**>(
Vladimir Marko5229cf12014-10-09 14:57:59 +0100140 temp_scoped_alloc_->Alloc(sizeof(ArenaBitVector*) * num_registers, kArenaAllocDFInfo));
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 int i;
buzbee67bf8852011-08-17 17:51:35 -0700142
buzbeefa57c472012-11-21 12:06:18 -0800143 /* Initialize num_register vectors with num_blocks bits each */
144 for (i = 0; i < num_registers; i++) {
Vladimir Markof585e542014-11-21 13:41:32 +0000145 temp_.ssa.def_block_matrix[i] = new (temp_scoped_alloc_.get()) ArenaBitVector(
146 arena_, GetNumBlocks(), false, kBitMapBMatrix);
147 temp_.ssa.def_block_matrix[i]->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 }
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700149
buzbee56c71782013-09-05 17:13:19 -0700150 AllNodesIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800151 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
152 FindLocalLiveIn(bb);
153 }
buzbee56c71782013-09-05 17:13:19 -0700154 AllNodesIterator iter2(this);
buzbee311ca162013-02-28 15:56:43 -0800155 for (BasicBlock* bb = iter2.Next(); bb != NULL; bb = iter2.Next()) {
156 FillDefBlockMatrix(bb);
157 }
buzbee67bf8852011-08-17 17:51:35 -0700158
Bill Buzbeea114add2012-05-03 15:00:40 -0700159 /*
160 * Also set the incoming parameters as defs in the entry block.
161 * Only need to handle the parameters for the outer method.
162 */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700163 int num_regs = GetNumOfCodeVRs();
164 int in_reg = GetFirstInVR();
buzbeefa57c472012-11-21 12:06:18 -0800165 for (; in_reg < num_regs; in_reg++) {
Vladimir Markof585e542014-11-21 13:41:32 +0000166 temp_.ssa.def_block_matrix[in_reg]->SetBit(GetEntryBlock()->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 }
buzbee67bf8852011-08-17 17:51:35 -0700168}
169
buzbeea5abf702013-04-12 14:39:29 -0700170void MIRGraph::ComputeDomPostOrderTraversal(BasicBlock* bb) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100171 // Clear the dominator post-order list.
172 dom_post_order_traversal_.clear();
173 dom_post_order_traversal_.reserve(num_reachable_blocks_);
174
buzbeea5abf702013-04-12 14:39:29 -0700175 ClearAllVisitedFlags();
Vladimir Markoffda4992014-12-18 17:05:58 +0000176 ScopedArenaAllocator allocator(&cu_->arena_stack);
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100177 ScopedArenaVector<std::pair<BasicBlock*, ArenaBitVector::IndexIterator>> work_stack(
Vladimir Markoffda4992014-12-18 17:05:58 +0000178 allocator.Adapter());
buzbeea5abf702013-04-12 14:39:29 -0700179 bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100180 work_stack.push_back(std::make_pair(bb, bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700181 while (!work_stack.empty()) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100182 std::pair<BasicBlock*, ArenaBitVector::IndexIterator>* curr = &work_stack.back();
183 BasicBlock* curr_bb = curr->first;
184 ArenaBitVector::IndexIterator* curr_idom_iter = &curr->second;
185 while (!curr_idom_iter->Done() && (NeedsVisit(GetBasicBlock(**curr_idom_iter)) == nullptr)) {
186 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700187 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100188 // NOTE: work_stack.push_back()/pop_back() invalidate curr and curr_idom_iter.
189 if (!curr_idom_iter->Done()) {
190 BasicBlock* new_bb = GetBasicBlock(**curr_idom_iter);
191 ++*curr_idom_iter;
buzbeea5abf702013-04-12 14:39:29 -0700192 new_bb->visited = true;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100193 work_stack.push_back(std::make_pair(new_bb, new_bb->i_dominated->Indexes().begin()));
buzbeea5abf702013-04-12 14:39:29 -0700194 } else {
195 // no successor/next
buzbee0d829482013-10-11 15:24:55 -0700196 if (curr_bb->id != NullBasicBlockId) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100197 dom_post_order_traversal_.push_back(curr_bb->id);
buzbee0d829482013-10-11 15:24:55 -0700198 }
buzbeea5abf702013-04-12 14:39:29 -0700199 work_stack.pop_back();
buzbeea5abf702013-04-12 14:39:29 -0700200 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 }
buzbee67bf8852011-08-17 17:51:35 -0700202}
203
buzbee311ca162013-02-28 15:56:43 -0800204void MIRGraph::CheckForDominanceFrontier(BasicBlock* dom_bb,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700205 const BasicBlock* succ_bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 /*
207 * TODO - evaluate whether phi will ever need to be inserted into exit
208 * blocks.
209 */
buzbee0d829482013-10-11 15:24:55 -0700210 if (succ_bb->i_dom != dom_bb->id &&
buzbeefa57c472012-11-21 12:06:18 -0800211 succ_bb->block_type == kDalvikByteCode &&
212 succ_bb->hidden == false) {
buzbee862a7602013-04-05 10:58:54 -0700213 dom_bb->dom_frontier->SetBit(succ_bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 }
buzbee67bf8852011-08-17 17:51:35 -0700215}
216
217/* Worker function to compute the dominance frontier */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700218bool MIRGraph::ComputeDominanceFrontier(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700219 /* Calculate DF_local */
buzbee0d829482013-10-11 15:24:55 -0700220 if (bb->taken != NullBasicBlockId) {
221 CheckForDominanceFrontier(bb, GetBasicBlock(bb->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 }
buzbee0d829482013-10-11 15:24:55 -0700223 if (bb->fall_through != NullBasicBlockId) {
224 CheckForDominanceFrontier(bb, GetBasicBlock(bb->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700225 }
buzbee0d829482013-10-11 15:24:55 -0700226 if (bb->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100227 for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) {
228 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
229 CheckForDominanceFrontier(bb, succ_bb);
230 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 }
buzbee67bf8852011-08-17 17:51:35 -0700232
Bill Buzbeea114add2012-05-03 15:00:40 -0700233 /* Calculate DF_up */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100234 for (uint32_t dominated_idx : bb->i_dominated->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700235 BasicBlock* dominated_bb = GetBasicBlock(dominated_idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100236 for (uint32_t df_up_block_idx : dominated_bb->dom_frontier->Indexes()) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700237 BasicBlock* df_up_block = GetBasicBlock(df_up_block_idx);
buzbee311ca162013-02-28 15:56:43 -0800238 CheckForDominanceFrontier(bb, df_up_block);
buzbee67bf8852011-08-17 17:51:35 -0700239 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700240 }
buzbee67bf8852011-08-17 17:51:35 -0700241
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 return true;
buzbee67bf8852011-08-17 17:51:35 -0700243}
244
245/* Worker function for initializing domination-related data structures */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700246void MIRGraph::InitializeDominationInfo(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800247 int num_total_blocks = GetBasicBlockListCount();
buzbee67bf8852011-08-17 17:51:35 -0700248
Brian Carlstromdf629502013-07-17 22:39:56 -0700249 if (bb->dominators == NULL) {
buzbee862a7602013-04-05 10:58:54 -0700250 bb->dominators = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700251 true /* expandable */, kBitMapDominators);
buzbee862a7602013-04-05 10:58:54 -0700252 bb->i_dominated = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700253 true /* expandable */, kBitMapIDominated);
buzbee862a7602013-04-05 10:58:54 -0700254 bb->dom_frontier = new (arena_) ArenaBitVector(arena_, num_total_blocks,
Jean Christophe Beyler007a0652014-09-05 16:06:42 -0700255 true /* expandable */, kBitMapDomFrontier);
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 } else {
buzbee862a7602013-04-05 10:58:54 -0700257 bb->dominators->ClearAllBits();
258 bb->i_dominated->ClearAllBits();
259 bb->dom_frontier->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 }
261 /* Set all bits in the dominator vector */
buzbee862a7602013-04-05 10:58:54 -0700262 bb->dominators->SetInitialBits(num_total_blocks);
buzbee67bf8852011-08-17 17:51:35 -0700263
buzbee862a7602013-04-05 10:58:54 -0700264 return;
buzbee67bf8852011-08-17 17:51:35 -0700265}
266
buzbee5b537102012-01-17 17:33:47 -0800267/*
buzbeefa57c472012-11-21 12:06:18 -0800268 * Walk through the ordered i_dom list until we reach common parent.
269 * Given the ordering of i_dom_list, this common parent represents the
buzbee5b537102012-01-17 17:33:47 -0800270 * last element of the intersection of block1 and block2 dominators.
271 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700272int MIRGraph::FindCommonParent(int block1, int block2) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 while (block1 != block2) {
274 while (block1 < block2) {
buzbee311ca162013-02-28 15:56:43 -0800275 block1 = i_dom_list_[block1];
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 DCHECK_NE(block1, NOTVISITED);
buzbee5b537102012-01-17 17:33:47 -0800277 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 while (block2 < block1) {
buzbee311ca162013-02-28 15:56:43 -0800279 block2 = i_dom_list_[block2];
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 DCHECK_NE(block2, NOTVISITED);
281 }
282 }
283 return block1;
buzbee5b537102012-01-17 17:33:47 -0800284}
285
286/* Worker function to compute each block's immediate dominator */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700287bool MIRGraph::ComputeblockIDom(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 /* Special-case entry block */
buzbee0d829482013-10-11 15:24:55 -0700289 if ((bb->id == NullBasicBlockId) || (bb == GetEntryBlock())) {
buzbee5b537102012-01-17 17:33:47 -0800290 return false;
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 }
292
293 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100294 auto it = bb->predecessors.begin(), end = bb->predecessors.end();
Bill Buzbeea114add2012-05-03 15:00:40 -0700295
296 /* Find the first processed predecessor */
buzbee862a7602013-04-05 10:58:54 -0700297 int idom = -1;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100298 for ( ; ; ++it) {
299 CHECK(it != end);
300 BasicBlock* pred_bb = GetBasicBlock(*it);
301 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800302 if (i_dom_list_[pred_bb->dfs_id] != NOTVISITED) {
buzbeefa57c472012-11-21 12:06:18 -0800303 idom = pred_bb->dfs_id;
Bill Buzbeea114add2012-05-03 15:00:40 -0700304 break;
305 }
306 }
307
308 /* Scan the rest of the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100309 for ( ; it != end; ++it) {
310 BasicBlock* pred_bb = GetBasicBlock(*it);
311 DCHECK(pred_bb != nullptr);
buzbee311ca162013-02-28 15:56:43 -0800312 if (i_dom_list_[pred_bb->dfs_id] == NOTVISITED) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700313 continue;
314 } else {
buzbee311ca162013-02-28 15:56:43 -0800315 idom = FindCommonParent(pred_bb->dfs_id, idom);
Bill Buzbeea114add2012-05-03 15:00:40 -0700316 }
317 }
318
319 DCHECK_NE(idom, NOTVISITED);
320
321 /* Did something change? */
buzbee311ca162013-02-28 15:56:43 -0800322 if (i_dom_list_[bb->dfs_id] != idom) {
323 i_dom_list_[bb->dfs_id] = idom;
Bill Buzbeea114add2012-05-03 15:00:40 -0700324 return true;
325 }
326 return false;
buzbee5b537102012-01-17 17:33:47 -0800327}
328
329/* Worker function to compute each block's domintors */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700330bool MIRGraph::ComputeBlockDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800331 if (bb == GetEntryBlock()) {
buzbee862a7602013-04-05 10:58:54 -0700332 bb->dominators->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 } else {
buzbee0d829482013-10-11 15:24:55 -0700334 bb->dominators->Copy(GetBasicBlock(bb->i_dom)->dominators);
Bill Buzbeea114add2012-05-03 15:00:40 -0700335 }
buzbee862a7602013-04-05 10:58:54 -0700336 bb->dominators->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 return false;
buzbee5b537102012-01-17 17:33:47 -0800338}
339
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700340bool MIRGraph::SetDominators(BasicBlock* bb) {
buzbee311ca162013-02-28 15:56:43 -0800341 if (bb != GetEntryBlock()) {
342 int idom_dfs_idx = i_dom_list_[bb->dfs_id];
buzbeefa57c472012-11-21 12:06:18 -0800343 DCHECK_NE(idom_dfs_idx, NOTVISITED);
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100344 int i_dom_idx = dfs_post_order_[idom_dfs_idx];
buzbee311ca162013-02-28 15:56:43 -0800345 BasicBlock* i_dom = GetBasicBlock(i_dom_idx);
buzbee0d829482013-10-11 15:24:55 -0700346 bb->i_dom = i_dom->id;
buzbeefa57c472012-11-21 12:06:18 -0800347 /* Add bb to the i_dominated set of the immediate dominator block */
buzbee862a7602013-04-05 10:58:54 -0700348 i_dom->i_dominated->SetBit(bb->id);
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 }
350 return false;
buzbee5b537102012-01-17 17:33:47 -0800351}
352
buzbee67bf8852011-08-17 17:51:35 -0700353/* Compute dominators, immediate dominator, and dominance fronter */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700354void MIRGraph::ComputeDominators() {
buzbee311ca162013-02-28 15:56:43 -0800355 int num_reachable_blocks = num_reachable_blocks_;
buzbee67bf8852011-08-17 17:51:35 -0700356
Bill Buzbeea114add2012-05-03 15:00:40 -0700357 /* Initialize domination-related data structures */
buzbee56c71782013-09-05 17:13:19 -0700358 PreOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800359 for (BasicBlock* bb = iter.Next(); bb != NULL; bb = iter.Next()) {
360 InitializeDominationInfo(bb);
361 }
buzbee67bf8852011-08-17 17:51:35 -0700362
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700363 /* Initialize & Clear i_dom_list */
364 if (max_num_reachable_blocks_ < num_reachable_blocks_) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700365 i_dom_list_ = static_cast<int*>(arena_->Alloc(sizeof(int) * num_reachable_blocks,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000366 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;
379 for (BasicBlock* bb = iter2.Next(false); bb != NULL; bb = iter2.Next(change)) {
380 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);
buzbee311ca162013-02-28 15:56:43 -0800390 for (BasicBlock* bb = iter3.Next(); bb != NULL; bb = iter3.Next()) {
391 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);
buzbee311ca162013-02-28 15:56:43 -0800395 for (BasicBlock* bb = iter4.Next(); bb != NULL; bb = iter4.Next()) {
396 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);
buzbee311ca162013-02-28 15:56:43 -0800402 for (BasicBlock* bb = iter5.Next(); bb != NULL; bb = iter5.Next()) {
403 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
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700437 if (bb->data_flow_info == NULL) {
438 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
465/* Insert phi nodes to for each variable to the dominance frontiers */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700466void MIRGraph::InsertPhiNodes() {
buzbeefa57c472012-11-21 12:06:18 -0800467 int dalvik_reg;
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100468 ArenaBitVector* phi_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
469 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapPhi);
470 ArenaBitVector* input_blocks = new (temp_scoped_alloc_.get()) ArenaBitVector(
471 temp_scoped_alloc_.get(), GetNumBlocks(), false, kBitMapInputBlocks);
buzbee67bf8852011-08-17 17:51:35 -0700472
buzbee56c71782013-09-05 17:13:19 -0700473 RepeatingPostOrderDfsIterator iter(this);
buzbee311ca162013-02-28 15:56:43 -0800474 bool change = false;
475 for (BasicBlock* bb = iter.Next(false); bb != NULL; bb = iter.Next(change)) {
476 change = ComputeBlockLiveIns(bb);
477 }
buzbee67bf8852011-08-17 17:51:35 -0700478
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 /* Iterate through each Dalvik register */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700480 for (dalvik_reg = GetNumOfCodeAndTempVRs() - 1; dalvik_reg >= 0; dalvik_reg--) {
Vladimir Markof585e542014-11-21 13:41:32 +0000481 input_blocks->Copy(temp_.ssa.def_block_matrix[dalvik_reg]);
buzbee862a7602013-04-05 10:58:54 -0700482 phi_blocks->ClearAllBits();
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 do {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100484 // TUNING: When we repeat this, we could skip indexes from the previous pass.
485 for (uint32_t idx : input_blocks->Indexes()) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700486 BasicBlock* def_bb = GetBasicBlock(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100487 if (def_bb->dom_frontier != nullptr) {
488 phi_blocks->Union(def_bb->dom_frontier);
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700489 }
490 }
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100491 } while (input_blocks->Union(phi_blocks));
Bill Buzbeea114add2012-05-03 15:00:40 -0700492
493 /*
buzbeefa57c472012-11-21 12:06:18 -0800494 * Insert a phi node for dalvik_reg in the phi_blocks if the Dalvik
Bill Buzbeea114add2012-05-03 15:00:40 -0700495 * register is in the live-in set.
496 */
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100497 for (uint32_t idx : phi_blocks->Indexes()) {
buzbee311ca162013-02-28 15:56:43 -0800498 BasicBlock* phi_bb = GetBasicBlock(idx);
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 /* Variable will be clobbered before being used - no need for phi */
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700500 if (!phi_bb->data_flow_info->live_in_v->IsBitSet(dalvik_reg)) {
501 continue;
502 }
Jean Christophe Beyler3aa57732014-04-17 12:47:24 -0700503 MIR *phi = NewMIR();
buzbeecbd6d442012-11-17 14:11:25 -0800504 phi->dalvikInsn.opcode = static_cast<Instruction::Code>(kMirOpPhi);
buzbeefa57c472012-11-21 12:06:18 -0800505 phi->dalvikInsn.vA = dalvik_reg;
506 phi->offset = phi_bb->start_offset;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700507 phi->m_unit_index = 0; // Arbitrarily assign all Phi nodes to outermost method.
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700508 phi_bb->PrependMIR(phi);
buzbee67bf8852011-08-17 17:51:35 -0700509 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 }
buzbee67bf8852011-08-17 17:51:35 -0700511}
512
513/*
514 * Worker function to insert phi-operands with latest SSA names from
515 * predecessor blocks
516 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700517bool MIRGraph::InsertPhiNodeOperands(BasicBlock* bb) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700518 /* Phi nodes are at the beginning of each block */
buzbee0d829482013-10-11 15:24:55 -0700519 for (MIR* mir = bb->first_mir_insn; mir != NULL; mir = mir->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800520 if (mir->dalvikInsn.opcode != static_cast<Instruction::Code>(kMirOpPhi))
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 return true;
buzbeefa57c472012-11-21 12:06:18 -0800522 int ssa_reg = mir->ssa_rep->defs[0];
523 DCHECK_GE(ssa_reg, 0); // Shouldn't see compiler temps here
buzbee311ca162013-02-28 15:56:43 -0800524 int v_reg = SRegToVReg(ssa_reg);
buzbee67bf8852011-08-17 17:51:35 -0700525
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 /* Iterate through the predecessors */
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100527 size_t num_uses = bb->predecessors.size();
Jean Christophe Beyler4896d7b2014-05-01 15:36:22 -0700528 AllocateSSAUseData(mir, num_uses);
529 int* uses = mir->ssa_rep->uses;
buzbee0d829482013-10-11 15:24:55 -0700530 BasicBlockId* incoming =
531 static_cast<BasicBlockId*>(arena_->Alloc(sizeof(BasicBlockId) * num_uses,
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000532 kArenaAllocDFInfo));
buzbee0d829482013-10-11 15:24:55 -0700533 mir->meta.phi_incoming = incoming;
534 int idx = 0;
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100535 for (BasicBlockId pred_id : bb->predecessors) {
536 BasicBlock* pred_bb = GetBasicBlock(pred_id);
537 DCHECK(pred_bb != nullptr);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800538 uses[idx] = pred_bb->data_flow_info->vreg_to_ssa_map_exit[v_reg];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100539 incoming[idx] = pred_id;
buzbee0d829482013-10-11 15:24:55 -0700540 idx++;
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 }
542 }
543
544 return true;
buzbee67bf8852011-08-17 17:51:35 -0700545}
546
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700547void MIRGraph::DoDFSPreOrderSSARename(BasicBlock* block) {
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700548 if (block->visited || block->hidden) {
549 return;
550 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 block->visited = true;
buzbeef0cde542011-09-13 14:55:02 -0700552
Bill Buzbeea114add2012-05-03 15:00:40 -0700553 /* Process this block */
buzbee311ca162013-02-28 15:56:43 -0800554 DoSSAConversion(block);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700555 int map_size = sizeof(int) * GetNumOfCodeAndTempVRs();
buzbeef0cde542011-09-13 14:55:02 -0700556
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 /* Save SSA map snapshot */
Vladimir Marko5d474472014-03-21 17:10:58 +0000558 ScopedArenaAllocator allocator(&cu_->arena_stack);
buzbee862a7602013-04-05 10:58:54 -0700559 int* saved_ssa_map =
Vladimir Marko5d474472014-03-21 17:10:58 +0000560 static_cast<int*>(allocator.Alloc(map_size, kArenaAllocDalvikToSSAMap));
Vladimir Marko1c6ea442014-12-19 18:11:35 +0000561 memcpy(saved_ssa_map, vreg_to_ssa_map_, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700562
buzbee0d829482013-10-11 15:24:55 -0700563 if (block->fall_through != NullBasicBlockId) {
564 DoDFSPreOrderSSARename(GetBasicBlock(block->fall_through));
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 /* Restore SSA map snapshot */
Vladimir Marko1c6ea442014-12-19 18:11:35 +0000566 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700567 }
buzbee0d829482013-10-11 15:24:55 -0700568 if (block->taken != NullBasicBlockId) {
569 DoDFSPreOrderSSARename(GetBasicBlock(block->taken));
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 /* Restore SSA map snapshot */
Vladimir Marko1c6ea442014-12-19 18:11:35 +0000571 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
Bill Buzbeea114add2012-05-03 15:00:40 -0700572 }
buzbee0d829482013-10-11 15:24:55 -0700573 if (block->successor_block_list_type != kNotUsed) {
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100574 for (SuccessorBlockInfo* successor_block_info : block->successor_blocks) {
buzbee0d829482013-10-11 15:24:55 -0700575 BasicBlock* succ_bb = GetBasicBlock(successor_block_info->block);
buzbee311ca162013-02-28 15:56:43 -0800576 DoDFSPreOrderSSARename(succ_bb);
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 /* Restore SSA map snapshot */
Vladimir Marko1c6ea442014-12-19 18:11:35 +0000578 memcpy(vreg_to_ssa_map_, saved_ssa_map, map_size);
buzbeef0cde542011-09-13 14:55:02 -0700579 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700581 return;
buzbeef0cde542011-09-13 14:55:02 -0700582}
583
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800584} // namespace art