blob: 72c5834a8ce5dab5426571faaea19ae26aca3063 [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()) {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100127 block->GetDominator()->AddDominatedBlock(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100128 reverse_post_order_.Add(block);
129 for (size_t i = 0; i < block->GetSuccessors().Size(); i++) {
130 VisitBlockForDominatorTree(block->GetSuccessors().Get(i), block, visits);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000131 }
132 }
133}
134
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100135void HGraph::TransformToSSA() {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100136 DCHECK(!reverse_post_order_.IsEmpty());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100137 SsaBuilder ssa_builder(this);
138 ssa_builder.BuildSsa();
139}
140
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100141void HGraph::SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor) {
142 // Insert a new node between `block` and `successor` to split the
143 // critical edge.
144 HBasicBlock* new_block = new (arena_) HBasicBlock(this);
145 AddBlock(new_block);
146 new_block->AddInstruction(new (arena_) HGoto());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100147 block->ReplaceSuccessor(successor, new_block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100148 new_block->AddSuccessor(successor);
149 if (successor->IsLoopHeader()) {
150 // If we split at a back edge boundary, make the new block the back edge.
151 HLoopInformation* info = successor->GetLoopInformation();
152 if (info->IsBackEdge(block)) {
153 info->RemoveBackEdge(block);
154 info->AddBackEdge(new_block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155 }
156 }
157}
158
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100159void HGraph::SimplifyLoop(HBasicBlock* header) {
160 HLoopInformation* info = header->GetLoopInformation();
161
162 // If there are more than one back edge, make them branch to the same block that
163 // will become the only back edge. This simplifies finding natural loops in the
164 // graph.
165 if (info->NumberOfBackEdges() > 1) {
166 HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this);
167 AddBlock(new_back_edge);
168 new_back_edge->AddInstruction(new (arena_) HGoto());
169 for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) {
170 HBasicBlock* back_edge = info->GetBackEdges().Get(pred);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100171 back_edge->ReplaceSuccessor(header, new_back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172 }
173 info->ClearBackEdges();
174 info->AddBackEdge(new_back_edge);
175 new_back_edge->AddSuccessor(header);
176 }
177
178 // Make sure the loop has only one pre header. This simplifies SSA building by having
179 // to just look at the pre header to know which locals are initialized at entry of the
180 // loop.
181 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
182 if (number_of_incomings != 1) {
183 HBasicBlock* pre_header = new (arena_) HBasicBlock(this);
184 AddBlock(pre_header);
185 pre_header->AddInstruction(new (arena_) HGoto());
186
187 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
188 HBasicBlock* back_edge = info->GetBackEdges().Get(0);
189 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
190 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
191 if (predecessor != back_edge) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100192 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194 }
195 }
196 pre_header->AddSuccessor(header);
197 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100198
199 // Make sure the second predecessor of a loop header is the back edge.
200 if (header->GetPredecessors().Get(1) != info->GetBackEdges().Get(0)) {
201 header->SwapPredecessors();
202 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100203}
204
205void HGraph::SimplifyCFG() {
206 // Simplify the CFG for future analysis, and code generation:
207 // (1): Split critical edges.
208 // (2): Simplify loops by having only one back edge, and one preheader.
209 for (size_t i = 0; i < blocks_.Size(); ++i) {
210 HBasicBlock* block = blocks_.Get(i);
211 if (block->GetSuccessors().Size() > 1) {
212 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
213 HBasicBlock* successor = block->GetSuccessors().Get(j);
214 if (successor->GetPredecessors().Size() > 1) {
215 SplitCriticalEdge(block, successor);
216 --j;
217 }
218 }
219 }
220 if (block->IsLoopHeader()) {
221 SimplifyLoop(block);
222 }
223 }
224}
225
226bool HGraph::FindNaturalLoops() const {
227 for (size_t i = 0; i < blocks_.Size(); ++i) {
228 HBasicBlock* block = blocks_.Get(i);
229 if (block->IsLoopHeader()) {
230 HLoopInformation* info = block->GetLoopInformation();
231 if (!info->Populate()) {
232 // Abort if the loop is non natural. We currently bailout in such cases.
233 return false;
234 }
235 }
236 }
237 return true;
238}
239
240void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
241 if (blocks_.IsBitSet(block->GetBlockId())) {
242 return;
243 }
244
245 blocks_.SetBit(block->GetBlockId());
246 block->SetInLoop(this);
247 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
248 PopulateRecursive(block->GetPredecessors().Get(i));
249 }
250}
251
252bool HLoopInformation::Populate() {
253 DCHECK_EQ(GetBackEdges().Size(), 1u);
254 HBasicBlock* back_edge = GetBackEdges().Get(0);
255 DCHECK(back_edge->GetDominator() != nullptr);
256 if (!header_->Dominates(back_edge)) {
257 // This loop is not natural. Do not bother going further.
258 return false;
259 }
260
261 // Populate this loop: starting with the back edge, recursively add predecessors
262 // that are not already part of that loop. Set the header as part of the loop
263 // to end the recursion.
264 // This is a recursive implementation of the algorithm described in
265 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
266 blocks_.SetBit(header_->GetBlockId());
267 PopulateRecursive(back_edge);
268 return true;
269}
270
271HBasicBlock* HLoopInformation::GetPreHeader() const {
272 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
273 return header_->GetDominator();
274}
275
276bool HLoopInformation::Contains(const HBasicBlock& block) const {
277 return blocks_.IsBitSet(block.GetBlockId());
278}
279
280bool HLoopInformation::IsIn(const HLoopInformation& other) const {
281 return other.blocks_.IsBitSet(header_->GetBlockId());
282}
283
284bool HBasicBlock::Dominates(HBasicBlock* other) const {
285 // Walk up the dominator tree from `other`, to find out if `this`
286 // is an ancestor.
287 HBasicBlock* current = other;
288 while (current != nullptr) {
289 if (current == this) {
290 return true;
291 }
292 current = current->GetDominator();
293 }
294 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100295}
296
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100297void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
298 DCHECK(cursor->AsPhi() == nullptr);
299 DCHECK(instruction->AsPhi() == nullptr);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100300 DCHECK_EQ(instruction->GetId(), -1);
301 DCHECK_NE(cursor->GetId(), -1);
302 DCHECK_EQ(cursor->GetBlock(), this);
303 DCHECK(!instruction->IsControlFlow());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100304 instruction->next_ = cursor;
305 instruction->previous_ = cursor->previous_;
306 cursor->previous_ = instruction;
307 if (GetFirstInstruction() == cursor) {
308 instructions_.first_instruction_ = instruction;
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100309 } else {
310 instruction->previous_->next_ = instruction;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100311 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100312 instruction->SetBlock(this);
313 instruction->SetId(GetGraph()->GetNextInstructionId());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100314}
315
Roland Levillainccc07a92014-09-16 14:48:16 +0100316void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
317 HInstruction* replacement) {
318 DCHECK(initial->GetBlock() == this);
319 InsertInstructionBefore(replacement, initial);
320 initial->ReplaceWith(replacement);
321 RemoveInstruction(initial);
322}
323
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100324static void Add(HInstructionList* instruction_list,
325 HBasicBlock* block,
326 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000327 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000328 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100329 instruction->SetBlock(block);
330 instruction->SetId(block->GetGraph()->GetNextInstructionId());
331 instruction_list->AddInstruction(instruction);
332}
333
334void HBasicBlock::AddInstruction(HInstruction* instruction) {
335 Add(&instructions_, this, instruction);
336}
337
338void HBasicBlock::AddPhi(HPhi* phi) {
339 Add(&phis_, this, phi);
340}
341
342static void Remove(HInstructionList* instruction_list,
343 HBasicBlock* block,
344 HInstruction* instruction) {
345 DCHECK_EQ(block, instruction->GetBlock());
346 DCHECK(instruction->GetUses() == nullptr);
347 DCHECK(instruction->GetEnvUses() == nullptr);
348 instruction->SetBlock(nullptr);
349 instruction_list->RemoveInstruction(instruction);
350
351 for (size_t i = 0; i < instruction->InputCount(); i++) {
352 instruction->InputAt(i)->RemoveUser(instruction, i);
353 }
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100354
355 HEnvironment* environment = instruction->GetEnvironment();
356 if (environment != nullptr) {
357 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
358 HInstruction* vreg = environment->GetInstructionAt(i);
359 if (vreg != nullptr) {
360 vreg->RemoveEnvironmentUser(environment, i);
361 }
362 }
363 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100364}
365
366void HBasicBlock::RemoveInstruction(HInstruction* instruction) {
367 Remove(&instructions_, this, instruction);
368}
369
370void HBasicBlock::RemovePhi(HPhi* phi) {
371 Remove(&phis_, this, phi);
372}
373
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100374template <typename T>
375static void RemoveFromUseList(T* user,
376 size_t input_index,
377 HUseListNode<T>** list) {
378 HUseListNode<T>* previous = nullptr;
379 HUseListNode<T>* current = *list;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100380 while (current != nullptr) {
381 if (current->GetUser() == user && current->GetIndex() == input_index) {
382 if (previous == NULL) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100383 *list = current->GetTail();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100384 } else {
385 previous->SetTail(current->GetTail());
386 }
387 }
388 previous = current;
389 current = current->GetTail();
390 }
391}
392
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100393void HInstruction::RemoveUser(HInstruction* user, size_t input_index) {
394 RemoveFromUseList(user, input_index, &uses_);
395}
396
397void HInstruction::RemoveEnvironmentUser(HEnvironment* user, size_t input_index) {
398 RemoveFromUseList(user, input_index, &env_uses_);
399}
400
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100401void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402 if (first_instruction_ == nullptr) {
403 DCHECK(last_instruction_ == nullptr);
404 first_instruction_ = last_instruction_ = instruction;
405 } else {
406 last_instruction_->next_ = instruction;
407 instruction->previous_ = last_instruction_;
408 last_instruction_ = instruction;
409 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 for (size_t i = 0; i < instruction->InputCount(); i++) {
411 instruction->InputAt(i)->AddUseAt(instruction, i);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000412 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413}
414
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100415void HInstructionList::RemoveInstruction(HInstruction* instruction) {
416 if (instruction->previous_ != nullptr) {
417 instruction->previous_->next_ = instruction->next_;
418 }
419 if (instruction->next_ != nullptr) {
420 instruction->next_->previous_ = instruction->previous_;
421 }
422 if (instruction == first_instruction_) {
423 first_instruction_ = instruction->next_;
424 }
425 if (instruction == last_instruction_) {
426 last_instruction_ = instruction->previous_;
427 }
428}
429
Roland Levillainccc07a92014-09-16 14:48:16 +0100430bool HInstructionList::FoundBefore(const HInstruction* instruction1,
431 const HInstruction* instruction2) const {
432 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
433 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
434 if (it.Current() == instruction1) {
435 return true;
436 }
437 if (it.Current() == instruction2) {
438 return false;
439 }
440 }
441 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
442 return true;
443}
444
445bool HInstruction::Dominates(HInstruction* other_instruction) const {
446 HBasicBlock* block = GetBlock();
447 HBasicBlock* other_block = other_instruction->GetBlock();
448 if (block != other_block) {
449 return GetBlock()->Dominates(other_instruction->GetBlock());
450 } else {
451 // If both instructions are in the same block, ensure this
452 // instruction comes before `other_instruction`.
453 if (IsPhi()) {
454 if (!other_instruction->IsPhi()) {
455 // Phis appear before non phi-instructions so this instruction
456 // dominates `other_instruction`.
457 return true;
458 } else {
459 // There is no order among phis.
460 LOG(FATAL) << "There is no dominance between phis of a same block.";
461 return false;
462 }
463 } else {
464 // `this` is not a phi.
465 if (other_instruction->IsPhi()) {
466 // Phis appear before non phi-instructions so this instruction
467 // does not dominate `other_instruction`.
468 return false;
469 } else {
470 // Check whether this instruction comes before
471 // `other_instruction` in the instruction list.
472 return block->GetInstructions().FoundBefore(this, other_instruction);
473 }
474 }
475 }
476}
477
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100478void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100479 DCHECK(other != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) {
481 HUseListNode<HInstruction>* current = it.Current();
482 HInstruction* user = current->GetUser();
483 size_t input_index = current->GetIndex();
484 user->SetRawInputAt(input_index, other);
485 other->AddUseAt(user, input_index);
486 }
487
488 for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) {
489 HUseListNode<HEnvironment>* current = it.Current();
490 HEnvironment* user = current->GetUser();
491 size_t input_index = current->GetIndex();
492 user->SetRawEnvAt(input_index, other);
493 other->AddEnvUseAt(user, input_index);
494 }
495
496 uses_ = nullptr;
497 env_uses_ = nullptr;
498}
499
Nicolas Geoffray39468442014-09-02 15:17:15 +0100500size_t HInstruction::EnvironmentSize() const {
501 return HasEnvironment() ? environment_->Size() : 0;
502}
503
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504void HPhi::AddInput(HInstruction* input) {
505 DCHECK(input->GetBlock() != nullptr);
506 inputs_.Add(input);
507 input->AddUseAt(this, inputs_.Size() - 1);
508}
509
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000510#define DEFINE_ACCEPT(name) \
511void H##name::Accept(HGraphVisitor* visitor) { \
512 visitor->Visit##name(this); \
513}
514
515FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
516
517#undef DEFINE_ACCEPT
518
519void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100520 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
521 for (size_t i = 0 ; i < blocks.Size(); i++) {
522 VisitBasicBlock(blocks.Get(i));
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000523 }
524}
525
526void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100527 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100528 it.Current()->Accept(this);
529 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100530 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000531 it.Current()->Accept(this);
532 }
533}
534
Roland Levillain556c3d12014-09-18 15:25:07 +0100535HConstant* HBinaryOperation::TryStaticEvaluation(ArenaAllocator* allocator) const {
536 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
537 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
538 GetRight()->AsIntConstant()->GetValue());
539 return new(allocator) HIntConstant(value);
540 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
541 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
542 GetRight()->AsLongConstant()->GetValue());
543 return new(allocator) HLongConstant(value);
544 }
545 return nullptr;
546}
Dave Allison20dfc792014-06-16 20:44:29 -0700547
548bool HCondition::NeedsMaterialization() const {
549 if (!HasOnlyOneUse()) {
550 return true;
551 }
552 HUseListNode<HInstruction>* uses = GetUses();
553 HInstruction* user = uses->GetUser();
554 if (!user->IsIf()) {
555 return true;
556 }
557
558 // TODO: should we allow intervening instructions with no side-effect between this condition
559 // and the If instruction?
560 if (GetNext() != user) {
561 return true;
562 }
563 return false;
564}
565
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100566bool HInstruction::Equals(HInstruction* other) const {
567 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100568 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100569 if (!InstructionDataEquals(other)) return false;
570 if (GetType() != other->GetType()) return false;
571 if (InputCount() != other->InputCount()) return false;
572
573 for (size_t i = 0, e = InputCount(); i < e; ++i) {
574 if (InputAt(i) != other->InputAt(i)) return false;
575 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100576 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100577 return true;
578}
579
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000580} // namespace art