blob: 376d1af3efbf6f546580b30b41a1e5dec70c9920 [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 {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010034 for (size_t i = 0; i < blocks_.Size(); ++i) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000035 if (!visited.IsBitSet(i)) {
36 HBasicBlock* block = blocks_.Get(i);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010037 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +010038 block->GetSuccessors().Get(j)->RemovePredecessor(block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010040 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010041 block->RemovePhi(it.Current()->AsPhi());
42 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010043 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010044 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 Geoffray622d9c32014-05-12 16:11:02 +010058 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
59 HBasicBlock* successor = block->GetSuccessors().Get(i);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060 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 }
66 visiting->ClearBit(id);
67}
68
69void HGraph::BuildDominatorTree() {
70 ArenaBitVector visited(arena_, blocks_.Size(), false);
71
72 // (1) Find the back edges in the graph doing a DFS traversal.
73 FindBackEdges(&visited);
74
75 // (2) Remove blocks not visited during the initial DFS.
76 // Step (3) requires dead blocks to be removed from the
77 // predecessors list of live blocks.
78 RemoveDeadBlocks(visited);
79
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010080 // (3) Simplify the CFG now, so that we don't need to recompute
81 // dominators and the reverse post order.
82 SimplifyCFG();
83
84 // (4) Compute the immediate dominator of each block. We visit
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085 // the successors of a block only when all its forward branches
86 // have been processed.
87 GrowableArray<size_t> visits(arena_, blocks_.Size());
88 visits.SetSize(blocks_.Size());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010089 reverse_post_order_.Add(entry_block_);
90 for (size_t i = 0; i < entry_block_->GetSuccessors().Size(); i++) {
91 VisitBlockForDominatorTree(entry_block_->GetSuccessors().Get(i), entry_block_, &visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000092 }
93}
94
95HBasicBlock* HGraph::FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const {
96 ArenaBitVector visited(arena_, blocks_.Size(), false);
97 // Walk the dominator tree of the first block and mark the visited blocks.
98 while (first != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000099 visited.SetBit(first->GetBlockId());
100 first = first->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000101 }
102 // Walk the dominator tree of the second block until a marked block is found.
103 while (second != nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104 if (visited.IsBitSet(second->GetBlockId())) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000105 return second;
106 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 second = second->GetDominator();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 }
109 LOG(ERROR) << "Could not find common dominator";
110 return nullptr;
111}
112
113void HGraph::VisitBlockForDominatorTree(HBasicBlock* block,
114 HBasicBlock* predecessor,
115 GrowableArray<size_t>* visits) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 if (block->GetDominator() == nullptr) {
117 block->SetDominator(predecessor);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118 } else {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000119 block->SetDominator(FindCommonDominator(block->GetDominator(), predecessor));
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000120 }
121
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000122 visits->Increment(block->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 // Once all the forward edges have been visited, we know the immediate
124 // dominator of the block. We can then start visiting its successors.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000125 if (visits->Get(block->GetBlockId()) ==
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100126 block->GetPredecessors().Size() - block->NumberOfBackEdges()) {
127 reverse_post_order_.Add(block);
128 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
129 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000130 }
131 }
132}
133
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134void HGraph::TransformToSSA() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100136 SsaBuilder ssa_builder(this);
137 ssa_builder.BuildSsa();
138}
139
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100140void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
141 // Insert a new node between `block` and `successor` to split the
142 // critical edge.
143 HBasicBlock* new_block = new (arena_) HBasicBlock(this);
144 AddBlock(new_block);
145 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100146 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100147 new_block->AddSuccessor(successor);
148 if (successor->IsLoopHeader()) {
149 // If we split at a back edge boundary, make the new block the back edge.
150 HLoopInformation* info = successor->GetLoopInformation();
151 if (info->IsBackEdge(block)) {
152 info->RemoveBackEdge(block);
153 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100154 }
155 }
156}
157
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158void HGraph::SimplifyLoop(HBasicBlock* header) {
159 HLoopInformation* info = header->GetLoopInformation();
160
161 // If there are more than one back edge, make them branch to the same block that
162 // will become the only back edge. This simplifies finding natural loops in the
163 // graph.
164 if (info->NumberOfBackEdges() > 1) {
165 HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this);
166 AddBlock(new_back_edge);
167 new_back_edge->AddInstruction(new (arena_) HGoto());
168 for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) {
169 HBasicBlock* back_edge = info->GetBackEdges().Get(pred);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100170 back_edge->ReplaceSuccessor(header, new_back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100171 }
172 info->ClearBackEdges();
173 info->AddBackEdge(new_back_edge);
174 new_back_edge->AddSuccessor(header);
175 }
176
177 // Make sure the loop has only one pre header. This simplifies SSA building by having
178 // to just look at the pre header to know which locals are initialized at entry of the
179 // loop.
180 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
181 if (number_of_incomings != 1) {
182 HBasicBlock* pre_header = new (arena_) HBasicBlock(this);
183 AddBlock(pre_header);
184 pre_header->AddInstruction(new (arena_) HGoto());
185
186 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
187 HBasicBlock* back_edge = info->GetBackEdges().Get(0);
188 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
189 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
190 if (predecessor != back_edge) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100191 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100192 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193 }
194 }
195 pre_header->AddSuccessor(header);
196 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100197
198 // Make sure the second predecessor of a loop header is the back edge.
199 if (header->GetPredecessors().Get(1) != info->GetBackEdges().Get(0)) {
200 header->SwapPredecessors();
201 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100202}
203
204void HGraph::SimplifyCFG() {
205 // Simplify the CFG for future analysis, and code generation:
206 // (1): Split critical edges.
207 // (2): Simplify loops by having only one back edge, and one preheader.
208 for (size_t i = 0; i < blocks_.Size(); ++i) {
209 HBasicBlock* block = blocks_.Get(i);
210 if (block->GetSuccessors().Size() > 1) {
211 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
212 HBasicBlock* successor = block->GetSuccessors().Get(j);
213 if (successor->GetPredecessors().Size() > 1) {
214 SplitCriticalEdge(block, successor);
215 --j;
216 }
217 }
218 }
219 if (block->IsLoopHeader()) {
220 SimplifyLoop(block);
221 }
222 }
223}
224
225bool HGraph::FindNaturalLoops() const {
226 for (size_t i = 0; i < blocks_.Size(); ++i) {
227 HBasicBlock* block = blocks_.Get(i);
228 if (block->IsLoopHeader()) {
229 HLoopInformation* info = block->GetLoopInformation();
230 if (!info->Populate()) {
231 // Abort if the loop is non natural. We currently bailout in such cases.
232 return false;
233 }
234 }
235 }
236 return true;
237}
238
239void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
240 if (blocks_.IsBitSet(block->GetBlockId())) {
241 return;
242 }
243
244 blocks_.SetBit(block->GetBlockId());
245 block->SetInLoop(this);
246 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
247 PopulateRecursive(block->GetPredecessors().Get(i));
248 }
249}
250
251bool HLoopInformation::Populate() {
252 DCHECK_EQ(GetBackEdges().Size(), 1u);
253 HBasicBlock* back_edge = GetBackEdges().Get(0);
254 DCHECK(back_edge->GetDominator() != nullptr);
255 if (!header_->Dominates(back_edge)) {
256 // This loop is not natural. Do not bother going further.
257 return false;
258 }
259
260 // Populate this loop: starting with the back edge, recursively add predecessors
261 // that are not already part of that loop. Set the header as part of the loop
262 // to end the recursion.
263 // This is a recursive implementation of the algorithm described in
264 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
265 blocks_.SetBit(header_->GetBlockId());
266 PopulateRecursive(back_edge);
267 return true;
268}
269
270HBasicBlock* HLoopInformation::GetPreHeader() const {
271 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
272 return header_->GetDominator();
273}
274
275bool HLoopInformation::Contains(const HBasicBlock& block) const {
276 return blocks_.IsBitSet(block.GetBlockId());
277}
278
279bool HLoopInformation::IsIn(const HLoopInformation& other) const {
280 return other.blocks_.IsBitSet(header_->GetBlockId());
281}
282
283bool HBasicBlock::Dominates(HBasicBlock* other) const {
284 // Walk up the dominator tree from `other`, to find out if `this`
285 // is an ancestor.
286 HBasicBlock* current = other;
287 while (current != nullptr) {
288 if (current == this) {
289 return true;
290 }
291 current = current->GetDominator();
292 }
293 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100294}
295
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100296void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
297 DCHECK(cursor->AsPhi() == nullptr);
298 DCHECK(instruction->AsPhi() == nullptr);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100299 DCHECK_EQ(instruction->GetId(), -1);
300 DCHECK_NE(cursor->GetId(), -1);
301 DCHECK_EQ(cursor->GetBlock(), this);
302 DCHECK(!instruction->IsControlFlow());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100303 instruction->next_ = cursor;
304 instruction->previous_ = cursor->previous_;
305 cursor->previous_ = instruction;
306 if (GetFirstInstruction() == cursor) {
307 instructions_.first_instruction_ = instruction;
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100308 } else {
309 instruction->previous_->next_ = instruction;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100310 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100311 instruction->SetBlock(this);
312 instruction->SetId(GetGraph()->GetNextInstructionId());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100313}
314
Roland Levillainccc07a92014-09-16 14:48:16 +0100315void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
316 HInstruction* replacement) {
317 DCHECK(initial->GetBlock() == this);
318 InsertInstructionBefore(replacement, initial);
319 initial->ReplaceWith(replacement);
320 RemoveInstruction(initial);
321}
322
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100323static void Add(HInstructionList* instruction_list,
324 HBasicBlock* block,
325 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000326 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000327 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100328 instruction->SetBlock(block);
329 instruction->SetId(block->GetGraph()->GetNextInstructionId());
330 instruction_list->AddInstruction(instruction);
331}
332
333void HBasicBlock::AddInstruction(HInstruction* instruction) {
334 Add(&instructions_, this, instruction);
335}
336
337void HBasicBlock::AddPhi(HPhi* phi) {
338 Add(&phis_, this, phi);
339}
340
341static void Remove(HInstructionList* instruction_list,
342 HBasicBlock* block,
343 HInstruction* instruction) {
344 DCHECK_EQ(block, instruction->GetBlock());
345 DCHECK(instruction->GetUses() == nullptr);
346 DCHECK(instruction->GetEnvUses() == nullptr);
347 instruction->SetBlock(nullptr);
348 instruction_list->RemoveInstruction(instruction);
349
350 for (size_t i = 0; i < instruction->InputCount(); i++) {
351 instruction->InputAt(i)->RemoveUser(instruction, i);
352 }
353}
354
355void HBasicBlock::RemoveInstruction(HInstruction* instruction) {
356 Remove(&instructions_, this, instruction);
357}
358
359void HBasicBlock::RemovePhi(HPhi* phi) {
360 Remove(&phis_, this, phi);
361}
362
363void HInstruction::RemoveUser(HInstruction* user, size_t input_index) {
364 HUseListNode<HInstruction>* previous = nullptr;
365 HUseListNode<HInstruction>* current = uses_;
366 while (current != nullptr) {
367 if (current->GetUser() == user && current->GetIndex() == input_index) {
368 if (previous == NULL) {
369 uses_ = current->GetTail();
370 } else {
371 previous->SetTail(current->GetTail());
372 }
373 }
374 previous = current;
375 current = current->GetTail();
376 }
377}
378
379void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000380 if (first_instruction_ == nullptr) {
381 DCHECK(last_instruction_ == nullptr);
382 first_instruction_ = last_instruction_ = instruction;
383 } else {
384 last_instruction_->next_ = instruction;
385 instruction->previous_ = last_instruction_;
386 last_instruction_ = instruction;
387 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100388 for (size_t i = 0; i < instruction->InputCount(); i++) {
389 instruction->InputAt(i)->AddUseAt(instruction, i);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000390 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391}
392
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100393void HInstructionList::RemoveInstruction(HInstruction* instruction) {
394 if (instruction->previous_ != nullptr) {
395 instruction->previous_->next_ = instruction->next_;
396 }
397 if (instruction->next_ != nullptr) {
398 instruction->next_->previous_ = instruction->previous_;
399 }
400 if (instruction == first_instruction_) {
401 first_instruction_ = instruction->next_;
402 }
403 if (instruction == last_instruction_) {
404 last_instruction_ = instruction->previous_;
405 }
406}
407
Roland Levillainccc07a92014-09-16 14:48:16 +0100408bool HInstructionList::FoundBefore(const HInstruction* instruction1,
409 const HInstruction* instruction2) const {
410 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
411 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
412 if (it.Current() == instruction1) {
413 return true;
414 }
415 if (it.Current() == instruction2) {
416 return false;
417 }
418 }
419 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
420 return true;
421}
422
423bool HInstruction::Dominates(HInstruction* other_instruction) const {
424 HBasicBlock* block = GetBlock();
425 HBasicBlock* other_block = other_instruction->GetBlock();
426 if (block != other_block) {
427 return GetBlock()->Dominates(other_instruction->GetBlock());
428 } else {
429 // If both instructions are in the same block, ensure this
430 // instruction comes before `other_instruction`.
431 if (IsPhi()) {
432 if (!other_instruction->IsPhi()) {
433 // Phis appear before non phi-instructions so this instruction
434 // dominates `other_instruction`.
435 return true;
436 } else {
437 // There is no order among phis.
438 LOG(FATAL) << "There is no dominance between phis of a same block.";
439 return false;
440 }
441 } else {
442 // `this` is not a phi.
443 if (other_instruction->IsPhi()) {
444 // Phis appear before non phi-instructions so this instruction
445 // does not dominate `other_instruction`.
446 return false;
447 } else {
448 // Check whether this instruction comes before
449 // `other_instruction` in the instruction list.
450 return block->GetInstructions().FoundBefore(this, other_instruction);
451 }
452 }
453 }
454}
455
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100456void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457 DCHECK(other != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100458 for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) {
459 HUseListNode<HInstruction>* current = it.Current();
460 HInstruction* user = current->GetUser();
461 size_t input_index = current->GetIndex();
462 user->SetRawInputAt(input_index, other);
463 other->AddUseAt(user, input_index);
464 }
465
466 for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) {
467 HUseListNode<HEnvironment>* current = it.Current();
468 HEnvironment* user = current->GetUser();
469 size_t input_index = current->GetIndex();
470 user->SetRawEnvAt(input_index, other);
471 other->AddEnvUseAt(user, input_index);
472 }
473
474 uses_ = nullptr;
475 env_uses_ = nullptr;
476}
477
Nicolas Geoffray39468442014-09-02 15:17:15 +0100478size_t HInstruction::EnvironmentSize() const {
479 return HasEnvironment() ? environment_->Size() : 0;
480}
481
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482void HPhi::AddInput(HInstruction* input) {
483 DCHECK(input->GetBlock() != nullptr);
484 inputs_.Add(input);
485 input->AddUseAt(this, inputs_.Size() - 1);
486}
487
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000488#define DEFINE_ACCEPT(name) \
489void H##name::Accept(HGraphVisitor* visitor) { \
490 visitor->Visit##name(this); \
491}
492
493FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
494
495#undef DEFINE_ACCEPT
496
497void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100498 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
499 for (size_t i = 0 ; i < blocks.Size(); i++) {
500 VisitBasicBlock(blocks.Get(i));
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501 }
502}
503
504void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100505 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100506 it.Current()->Accept(this);
507 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100508 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000509 it.Current()->Accept(this);
510 }
511}
512
Roland Levillain556c3d12014-09-18 15:25:07 +0100513HConstant* HBinaryOperation::TryStaticEvaluation(ArenaAllocator* allocator) const {
514 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
515 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
516 GetRight()->AsIntConstant()->GetValue());
517 return new(allocator) HIntConstant(value);
518 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
519 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
520 GetRight()->AsLongConstant()->GetValue());
521 return new(allocator) HLongConstant(value);
522 }
523 return nullptr;
524}
Dave Allison20dfc792014-06-16 20:44:29 -0700525
526bool HCondition::NeedsMaterialization() const {
527 if (!HasOnlyOneUse()) {
528 return true;
529 }
530 HUseListNode<HInstruction>* uses = GetUses();
531 HInstruction* user = uses->GetUser();
532 if (!user->IsIf()) {
533 return true;
534 }
535
536 // TODO: should we allow intervening instructions with no side-effect between this condition
537 // and the If instruction?
538 if (GetNext() != user) {
539 return true;
540 }
541 return false;
542}
543
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100544bool HInstruction::Equals(HInstruction* other) const {
545 if (!InstructionTypeEquals(other)) return false;
546 if (!InstructionDataEquals(other)) return false;
547 if (GetType() != other->GetType()) return false;
548 if (InputCount() != other->InputCount()) return false;
549
550 for (size_t i = 0, e = InputCount(); i < e; ++i) {
551 if (InputAt(i) != other->InputAt(i)) return false;
552 }
553 return true;
554}
555
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000556} // namespace art