blob: d153bf76b8538461d804ca0f16d986c998de0d9d [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 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
17#include "nodes.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010018#include "ssa_builder.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000019#include "utils/growable_array.h"
20
21namespace art {
22
23void HGraph::AddBlock(HBasicBlock* block) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000024 block->SetBlockId(blocks_.Size());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025 blocks_.Add(block);
26}
27
Nicolas Geoffray804d0932014-05-02 08:46:00 +010028void HGraph::FindBackEdges(ArenaBitVector* visited) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000029 ArenaBitVector visiting(arena_, blocks_.Size(), false);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030 VisitBlockForBackEdges(entry_block_, visited, &visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000031}
32
33void HGraph::RemoveDeadBlocks(const ArenaBitVector& visited) const {
34 for (size_t i = 0; i < blocks_.Size(); i++) {
35 if (!visited.IsBitSet(i)) {
36 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000037 for (size_t j = 0; j < block->GetSuccessors()->Size(); j++) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010038 block->GetSuccessors()->Get(j)->RemovePredecessor(block, false);
39 }
40 for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) {
41 block->RemovePhi(it.Current()->AsPhi());
42 }
43 for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
44 block->RemoveInstruction(it.Current());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045 }
46 }
47 }
48}
49
50void HGraph::VisitBlockForBackEdges(HBasicBlock* block,
51 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +010052 ArenaBitVector* visiting) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000053 int id = block->GetBlockId();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000054 if (visited->IsBitSet(id)) return;
55
56 visited->SetBit(id);
57 visiting->SetBit(id);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000058 for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) {
59 HBasicBlock* successor = block->GetSuccessors()->Get(i);
60 if (visiting->IsBitSet(successor->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000061 successor->AddBackEdge(block);
62 } else {
63 VisitBlockForBackEdges(successor, visited, visiting);
64 }
65 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010066 post_order_.Add(block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000067 visiting->ClearBit(id);
68}
69
70void HGraph::BuildDominatorTree() {
71 ArenaBitVector visited(arena_, blocks_.Size(), false);
72
73 // (1) Find the back edges in the graph doing a DFS traversal.
74 FindBackEdges(&visited);
75
76 // (2) Remove blocks not visited during the initial DFS.
77 // Step (3) requires dead blocks to be removed from the
78 // predecessors list of live blocks.
79 RemoveDeadBlocks(visited);
80
81 // (3) Compute the immediate dominator of each block. We visit
82 // the successors of a block only when all its forward branches
83 // have been processed.
84 GrowableArray<size_t> visits(arena_, blocks_.Size());
85 visits.SetSize(blocks_.Size());
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 for (size_t i = 0; i < entry_block_->GetSuccessors()->Size(); i++) {
87 VisitBlockForDominatorTree(entry_block_->GetSuccessors()->Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088 }
89}
90
91HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
92 ArenaBitVector visited(arena_, blocks_.Size(), false);
93 // Walk the dominator tree of the first block and mark the visited blocks.
94 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095 visited.SetBit(first->GetBlockId());
96 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000097 }
98 // Walk the dominator tree of the second block until a marked block is found.
99 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000101 return second;
102 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000104 }
105 LOG(ERROR) << "Could not find common dominator";
106 return nullptr;
107}
108
109void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
110 HBasicBlock* predecessor,
111 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000112 if (block->GetDominator() == nullptr) {
113 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000114 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000115 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000116 }
117
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000118 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000119 // Once all the forward edges have been visited, we know the immediate
120 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000121 if (visits->Get(block->GetBlockId()) ==
122 block->GetPredecessors()->Size() - block->NumberOfBackEdges()) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000123 for (size_t i = 0; i < block->GetSuccessors()->Size(); i++) {
124 VisitBlockForDominatorTree(block->GetSuccessors()->Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000125 }
126 }
127}
128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100129void HGraph::TransformToSSA() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100130 DCHECK(!post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131 SimplifyCFG();
132 SsaBuilder ssa_builder(this);
133 ssa_builder.BuildSsa();
134}
135
136void HGraph::SimplifyCFG() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100137 for (size_t i = post_order_.Size(); i > 0; --i) {
138 HBasicBlock* current = post_order_.Get(i - 1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100139 if (current->IsLoopHeader()) {
140 // Make sure the loop has only one pre header. This simplifies SSA building by having
141 // to just look at the pre header to know which locals are initialized at entry of the
142 // loop.
143 HLoopInformation* info = current->GetLoopInformation();
144 size_t number_of_incomings = current->GetPredecessors()->Size() - info->NumberOfBackEdges();
145 if (number_of_incomings != 1) {
146 HBasicBlock* pre_header = new (arena_) HBasicBlock(this);
147 AddBlock(pre_header);
148 pre_header->AddInstruction(new (arena_) HGoto());
149 pre_header->SetDominator(current->GetDominator());
150 current->SetDominator(pre_header);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100151 post_order_.InsertAt(i, pre_header);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100152
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100153 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100154 for (size_t pred = 0; pred < info->GetBackEdges()->Size(); pred++) {
155 back_edges.SetBit(info->GetBackEdges()->Get(pred)->GetBlockId());
156 }
157 for (size_t pred = 0; pred < current->GetPredecessors()->Size(); pred++) {
158 HBasicBlock* predecessor = current->GetPredecessors()->Get(pred);
159 if (!back_edges.IsBitSet(predecessor->GetBlockId())) {
160 current->RemovePredecessor(predecessor);
161 pred--;
162 predecessor->AddSuccessor(pre_header);
163 }
164 }
165 pre_header->AddSuccessor(current);
166 }
167 info->SetPreHeader(current->GetDominator());
168 }
169 }
170}
171
172void HLoopInformation::SetPreHeader(HBasicBlock* block) {
173 DCHECK_EQ(header_->GetDominator(), block);
174 pre_header_ = block;
175}
176
177static void Add(HInstructionList* instruction_list,
178 HBasicBlock* block,
179 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000180 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000181 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100182 instruction->SetBlock(block);
183 instruction->SetId(block->GetGraph()->GetNextInstructionId());
184 instruction_list->AddInstruction(instruction);
185}
186
187void HBasicBlock::AddInstruction(HInstruction* instruction) {
188 Add(&instructions_, this, instruction);
189}
190
191void HBasicBlock::AddPhi(HPhi* phi) {
192 Add(&phis_, this, phi);
193}
194
195static void Remove(HInstructionList* instruction_list,
196 HBasicBlock* block,
197 HInstruction* instruction) {
198 DCHECK_EQ(block, instruction->GetBlock());
199 DCHECK(instruction->GetUses() == nullptr);
200 DCHECK(instruction->GetEnvUses() == nullptr);
201 instruction->SetBlock(nullptr);
202 instruction_list->RemoveInstruction(instruction);
203
204 for (size_t i = 0; i < instruction->InputCount(); i++) {
205 instruction->InputAt(i)->RemoveUser(instruction, i);
206 }
207}
208
209void HBasicBlock::RemoveInstruction(HInstruction* instruction) {
210 Remove(&instructions_, this, instruction);
211}
212
213void HBasicBlock::RemovePhi(HPhi* phi) {
214 Remove(&phis_, this, phi);
215}
216
217void HInstruction::RemoveUser(HInstruction* user, size_t input_index) {
218 HUseListNode<HInstruction>* previous = nullptr;
219 HUseListNode<HInstruction>* current = uses_;
220 while (current != nullptr) {
221 if (current->GetUser() == user && current->GetIndex() == input_index) {
222 if (previous == NULL) {
223 uses_ = current->GetTail();
224 } else {
225 previous->SetTail(current->GetTail());
226 }
227 }
228 previous = current;
229 current = current->GetTail();
230 }
231}
232
233void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000234 if (first_instruction_ == nullptr) {
235 DCHECK(last_instruction_ == nullptr);
236 first_instruction_ = last_instruction_ = instruction;
237 } else {
238 last_instruction_->next_ = instruction;
239 instruction->previous_ = last_instruction_;
240 last_instruction_ = instruction;
241 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 for (size_t i = 0; i < instruction->InputCount(); i++) {
243 instruction->InputAt(i)->AddUseAt(instruction, i);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000244 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000245}
246
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100247void HInstructionList::RemoveInstruction(HInstruction* instruction) {
248 if (instruction->previous_ != nullptr) {
249 instruction->previous_->next_ = instruction->next_;
250 }
251 if (instruction->next_ != nullptr) {
252 instruction->next_->previous_ = instruction->previous_;
253 }
254 if (instruction == first_instruction_) {
255 first_instruction_ = instruction->next_;
256 }
257 if (instruction == last_instruction_) {
258 last_instruction_ = instruction->previous_;
259 }
260}
261
262void HInstruction::ReplaceWith(HInstruction* other) {
263 for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) {
264 HUseListNode<HInstruction>* current = it.Current();
265 HInstruction* user = current->GetUser();
266 size_t input_index = current->GetIndex();
267 user->SetRawInputAt(input_index, other);
268 other->AddUseAt(user, input_index);
269 }
270
271 for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) {
272 HUseListNode<HEnvironment>* current = it.Current();
273 HEnvironment* user = current->GetUser();
274 size_t input_index = current->GetIndex();
275 user->SetRawEnvAt(input_index, other);
276 other->AddEnvUseAt(user, input_index);
277 }
278
279 uses_ = nullptr;
280 env_uses_ = nullptr;
281}
282
283void HPhi::AddInput(HInstruction* input) {
284 DCHECK(input->GetBlock() != nullptr);
285 inputs_.Add(input);
286 input->AddUseAt(this, inputs_.Size() - 1);
287}
288
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289#define DEFINE_ACCEPT(name) \
290void H##name::Accept(HGraphVisitor* visitor) { \
291 visitor->Visit##name(this); \
292}
293
294FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
295
296#undef DEFINE_ACCEPT
297
298void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100299 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
300 for (size_t i = 0 ; i < blocks.Size(); i++) {
301 VisitBasicBlock(blocks.Get(i));
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302 }
303}
304
305void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100306 for (HInstructionIterator it(*block->GetPhis()); !it.Done(); it.Advance()) {
307 it.Current()->Accept(this);
308 }
309 for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 it.Current()->Accept(this);
311 }
312}
313
314} // namespace art