blob: 8cb2ef6de8b720b410847ed8e2a6fd53e219dd84 [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.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 HBasicBlock* new_block = new (arena_) HBasicBlock(this, successor->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100145 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.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 // Also, if the loop is a do/while (that is the back edge is an if), change the
166 // back edge to be a goto. This simplifies code generation of suspend cheks.
167 if (info->NumberOfBackEdges() > 1 || info->GetBackEdges().Get(0)->GetLastInstruction()->IsIf()) {
168 HBasicBlock* new_back_edge = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100169 AddBlock(new_back_edge);
170 new_back_edge->AddInstruction(new (arena_) HGoto());
171 for (size_t pred = 0, e = info->GetBackEdges().Size(); pred < e; ++pred) {
172 HBasicBlock* back_edge = info->GetBackEdges().Get(pred);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100173 back_edge->ReplaceSuccessor(header, new_back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100174 }
175 info->ClearBackEdges();
176 info->AddBackEdge(new_back_edge);
177 new_back_edge->AddSuccessor(header);
178 }
179
180 // Make sure the loop has only one pre header. This simplifies SSA building by having
181 // to just look at the pre header to know which locals are initialized at entry of the
182 // loop.
183 size_t number_of_incomings = header->GetPredecessors().Size() - info->NumberOfBackEdges();
184 if (number_of_incomings != 1) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 HBasicBlock* pre_header = new (arena_) HBasicBlock(this, header->GetDexPc());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100186 AddBlock(pre_header);
187 pre_header->AddInstruction(new (arena_) HGoto());
188
189 ArenaBitVector back_edges(arena_, GetBlocks().Size(), false);
190 HBasicBlock* back_edge = info->GetBackEdges().Get(0);
191 for (size_t pred = 0; pred < header->GetPredecessors().Size(); ++pred) {
192 HBasicBlock* predecessor = header->GetPredecessors().Get(pred);
193 if (predecessor != back_edge) {
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100194 predecessor->ReplaceSuccessor(header, pre_header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100195 pred--;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100196 }
197 }
198 pre_header->AddSuccessor(header);
199 }
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100200
201 // Make sure the second predecessor of a loop header is the back edge.
202 if (header->GetPredecessors().Get(1) != info->GetBackEdges().Get(0)) {
203 header->SwapPredecessors();
204 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100205
206 // Place the suspend check at the beginning of the header, so that live registers
207 // will be known when allocating registers. Note that code generation can still
208 // generate the suspend check at the back edge, but needs to be careful with
209 // loop phi spill slots (which are not written to at back edge).
210 HInstruction* first_instruction = header->GetFirstInstruction();
211 if (!first_instruction->IsSuspendCheck()) {
212 HSuspendCheck* check = new (arena_) HSuspendCheck(header->GetDexPc());
213 header->InsertInstructionBefore(check, first_instruction);
214 first_instruction = check;
215 }
216 info->SetSuspendCheck(first_instruction->AsSuspendCheck());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100217}
218
219void HGraph::SimplifyCFG() {
220 // Simplify the CFG for future analysis, and code generation:
221 // (1): Split critical edges.
222 // (2): Simplify loops by having only one back edge, and one preheader.
223 for (size_t i = 0; i < blocks_.Size(); ++i) {
224 HBasicBlock* block = blocks_.Get(i);
225 if (block->GetSuccessors().Size() > 1) {
226 for (size_t j = 0; j < block->GetSuccessors().Size(); ++j) {
227 HBasicBlock* successor = block->GetSuccessors().Get(j);
228 if (successor->GetPredecessors().Size() > 1) {
229 SplitCriticalEdge(block, successor);
230 --j;
231 }
232 }
233 }
234 if (block->IsLoopHeader()) {
235 SimplifyLoop(block);
236 }
237 }
238}
239
240bool HGraph::FindNaturalLoops() const {
241 for (size_t i = 0; i < blocks_.Size(); ++i) {
242 HBasicBlock* block = blocks_.Get(i);
243 if (block->IsLoopHeader()) {
244 HLoopInformation* info = block->GetLoopInformation();
245 if (!info->Populate()) {
246 // Abort if the loop is non natural. We currently bailout in such cases.
247 return false;
248 }
249 }
250 }
251 return true;
252}
253
254void HLoopInformation::PopulateRecursive(HBasicBlock* block) {
255 if (blocks_.IsBitSet(block->GetBlockId())) {
256 return;
257 }
258
259 blocks_.SetBit(block->GetBlockId());
260 block->SetInLoop(this);
261 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
262 PopulateRecursive(block->GetPredecessors().Get(i));
263 }
264}
265
266bool HLoopInformation::Populate() {
267 DCHECK_EQ(GetBackEdges().Size(), 1u);
268 HBasicBlock* back_edge = GetBackEdges().Get(0);
269 DCHECK(back_edge->GetDominator() != nullptr);
270 if (!header_->Dominates(back_edge)) {
271 // This loop is not natural. Do not bother going further.
272 return false;
273 }
274
275 // Populate this loop: starting with the back edge, recursively add predecessors
276 // that are not already part of that loop. Set the header as part of the loop
277 // to end the recursion.
278 // This is a recursive implementation of the algorithm described in
279 // "Advanced Compiler Design & Implementation" (Muchnick) p192.
280 blocks_.SetBit(header_->GetBlockId());
281 PopulateRecursive(back_edge);
282 return true;
283}
284
285HBasicBlock* HLoopInformation::GetPreHeader() const {
286 DCHECK_EQ(header_->GetPredecessors().Size(), 2u);
287 return header_->GetDominator();
288}
289
290bool HLoopInformation::Contains(const HBasicBlock& block) const {
291 return blocks_.IsBitSet(block.GetBlockId());
292}
293
294bool HLoopInformation::IsIn(const HLoopInformation& other) const {
295 return other.blocks_.IsBitSet(header_->GetBlockId());
296}
297
298bool HBasicBlock::Dominates(HBasicBlock* other) const {
299 // Walk up the dominator tree from `other`, to find out if `this`
300 // is an ancestor.
301 HBasicBlock* current = other;
302 while (current != nullptr) {
303 if (current == this) {
304 return true;
305 }
306 current = current->GetDominator();
307 }
308 return false;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100309}
310
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100311static void UpdateInputsUsers(HInstruction* instruction) {
312 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
313 instruction->InputAt(i)->AddUseAt(instruction, i);
314 }
315 // Environment should be created later.
316 DCHECK(!instruction->HasEnvironment());
317}
318
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100319void HBasicBlock::InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor) {
Roland Levillain476df552014-10-09 17:51:36 +0100320 DCHECK(!cursor->IsPhi());
321 DCHECK(!instruction->IsPhi());
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100322 DCHECK_EQ(instruction->GetId(), -1);
323 DCHECK_NE(cursor->GetId(), -1);
324 DCHECK_EQ(cursor->GetBlock(), this);
325 DCHECK(!instruction->IsControlFlow());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100326 instruction->next_ = cursor;
327 instruction->previous_ = cursor->previous_;
328 cursor->previous_ = instruction;
329 if (GetFirstInstruction() == cursor) {
330 instructions_.first_instruction_ = instruction;
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100331 } else {
332 instruction->previous_->next_ = instruction;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100333 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100334 instruction->SetBlock(this);
335 instruction->SetId(GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100336 UpdateInputsUsers(instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100337}
338
Roland Levillainccc07a92014-09-16 14:48:16 +0100339void HBasicBlock::ReplaceAndRemoveInstructionWith(HInstruction* initial,
340 HInstruction* replacement) {
341 DCHECK(initial->GetBlock() == this);
342 InsertInstructionBefore(replacement, initial);
343 initial->ReplaceWith(replacement);
344 RemoveInstruction(initial);
345}
346
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100347static void Add(HInstructionList* instruction_list,
348 HBasicBlock* block,
349 HInstruction* instruction) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000350 DCHECK(instruction->GetBlock() == nullptr);
Nicolas Geoffray43c86422014-03-18 11:58:24 +0000351 DCHECK_EQ(instruction->GetId(), -1);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100352 instruction->SetBlock(block);
353 instruction->SetId(block->GetGraph()->GetNextInstructionId());
Nicolas Geoffray191c4b12014-10-07 14:14:27 +0100354 UpdateInputsUsers(instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355 instruction_list->AddInstruction(instruction);
356}
357
358void HBasicBlock::AddInstruction(HInstruction* instruction) {
359 Add(&instructions_, this, instruction);
360}
361
362void HBasicBlock::AddPhi(HPhi* phi) {
363 Add(&phis_, this, phi);
364}
365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100366void HBasicBlock::InsertPhiAfter(HPhi* phi, HPhi* cursor) {
367 DCHECK_EQ(phi->GetId(), -1);
368 DCHECK_NE(cursor->GetId(), -1);
369 DCHECK_EQ(cursor->GetBlock(), this);
370 if (cursor->next_ == nullptr) {
371 cursor->next_ = phi;
372 phi->previous_ = cursor;
373 DCHECK(phi->next_ == nullptr);
374 } else {
375 phi->next_ = cursor->next_;
376 phi->previous_ = cursor;
377 cursor->next_ = phi;
378 phi->next_->previous_ = phi;
379 }
380 phi->SetBlock(this);
381 phi->SetId(GetGraph()->GetNextInstructionId());
382 UpdateInputsUsers(phi);
383}
384
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100385static void Remove(HInstructionList* instruction_list,
386 HBasicBlock* block,
387 HInstruction* instruction) {
388 DCHECK_EQ(block, instruction->GetBlock());
389 DCHECK(instruction->GetUses() == nullptr);
390 DCHECK(instruction->GetEnvUses() == nullptr);
391 instruction->SetBlock(nullptr);
392 instruction_list->RemoveInstruction(instruction);
393
394 for (size_t i = 0; i < instruction->InputCount(); i++) {
395 instruction->InputAt(i)->RemoveUser(instruction, i);
396 }
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100397
398 HEnvironment* environment = instruction->GetEnvironment();
399 if (environment != nullptr) {
400 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
401 HInstruction* vreg = environment->GetInstructionAt(i);
402 if (vreg != nullptr) {
403 vreg->RemoveEnvironmentUser(environment, i);
404 }
405 }
406 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407}
408
409void HBasicBlock::RemoveInstruction(HInstruction* instruction) {
410 Remove(&instructions_, this, instruction);
411}
412
413void HBasicBlock::RemovePhi(HPhi* phi) {
414 Remove(&phis_, this, phi);
415}
416
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100417template <typename T>
418static void RemoveFromUseList(T* user,
419 size_t input_index,
420 HUseListNode<T>** list) {
421 HUseListNode<T>* previous = nullptr;
422 HUseListNode<T>* current = *list;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100423 while (current != nullptr) {
424 if (current->GetUser() == user && current->GetIndex() == input_index) {
425 if (previous == NULL) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100426 *list = current->GetTail();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100427 } else {
428 previous->SetTail(current->GetTail());
429 }
430 }
431 previous = current;
432 current = current->GetTail();
433 }
434}
435
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100436void HInstruction::RemoveUser(HInstruction* user, size_t input_index) {
437 RemoveFromUseList(user, input_index, &uses_);
438}
439
440void HInstruction::RemoveEnvironmentUser(HEnvironment* user, size_t input_index) {
441 RemoveFromUseList(user, input_index, &env_uses_);
442}
443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100444void HInstructionList::AddInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000445 if (first_instruction_ == nullptr) {
446 DCHECK(last_instruction_ == nullptr);
447 first_instruction_ = last_instruction_ = instruction;
448 } else {
449 last_instruction_->next_ = instruction;
450 instruction->previous_ = last_instruction_;
451 last_instruction_ = instruction;
452 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000453}
454
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455void HInstructionList::RemoveInstruction(HInstruction* instruction) {
456 if (instruction->previous_ != nullptr) {
457 instruction->previous_->next_ = instruction->next_;
458 }
459 if (instruction->next_ != nullptr) {
460 instruction->next_->previous_ = instruction->previous_;
461 }
462 if (instruction == first_instruction_) {
463 first_instruction_ = instruction->next_;
464 }
465 if (instruction == last_instruction_) {
466 last_instruction_ = instruction->previous_;
467 }
468}
469
Roland Levillain6b469232014-09-25 10:10:38 +0100470bool HInstructionList::Contains(HInstruction* instruction) const {
471 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
472 if (it.Current() == instruction) {
473 return true;
474 }
475 }
476 return false;
477}
478
Roland Levillainccc07a92014-09-16 14:48:16 +0100479bool HInstructionList::FoundBefore(const HInstruction* instruction1,
480 const HInstruction* instruction2) const {
481 DCHECK_EQ(instruction1->GetBlock(), instruction2->GetBlock());
482 for (HInstructionIterator it(*this); !it.Done(); it.Advance()) {
483 if (it.Current() == instruction1) {
484 return true;
485 }
486 if (it.Current() == instruction2) {
487 return false;
488 }
489 }
490 LOG(FATAL) << "Did not find an order between two instructions of the same block.";
491 return true;
492}
493
Roland Levillain6c82d402014-10-13 16:10:27 +0100494bool HInstruction::StrictlyDominates(HInstruction* other_instruction) const {
495 if (other_instruction == this) {
496 // An instruction does not strictly dominate itself.
497 return false;
498 }
Roland Levillainccc07a92014-09-16 14:48:16 +0100499 HBasicBlock* block = GetBlock();
500 HBasicBlock* other_block = other_instruction->GetBlock();
501 if (block != other_block) {
502 return GetBlock()->Dominates(other_instruction->GetBlock());
503 } else {
504 // If both instructions are in the same block, ensure this
505 // instruction comes before `other_instruction`.
506 if (IsPhi()) {
507 if (!other_instruction->IsPhi()) {
508 // Phis appear before non phi-instructions so this instruction
509 // dominates `other_instruction`.
510 return true;
511 } else {
512 // There is no order among phis.
513 LOG(FATAL) << "There is no dominance between phis of a same block.";
514 return false;
515 }
516 } else {
517 // `this` is not a phi.
518 if (other_instruction->IsPhi()) {
519 // Phis appear before non phi-instructions so this instruction
520 // does not dominate `other_instruction`.
521 return false;
522 } else {
523 // Check whether this instruction comes before
524 // `other_instruction` in the instruction list.
525 return block->GetInstructions().FoundBefore(this, other_instruction);
526 }
527 }
528 }
529}
530
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100531void HInstruction::ReplaceWith(HInstruction* other) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100532 DCHECK(other != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100533 for (HUseIterator<HInstruction> it(GetUses()); !it.Done(); it.Advance()) {
534 HUseListNode<HInstruction>* current = it.Current();
535 HInstruction* user = current->GetUser();
536 size_t input_index = current->GetIndex();
537 user->SetRawInputAt(input_index, other);
538 other->AddUseAt(user, input_index);
539 }
540
541 for (HUseIterator<HEnvironment> it(GetEnvUses()); !it.Done(); it.Advance()) {
542 HUseListNode<HEnvironment>* current = it.Current();
543 HEnvironment* user = current->GetUser();
544 size_t input_index = current->GetIndex();
545 user->SetRawEnvAt(input_index, other);
546 other->AddEnvUseAt(user, input_index);
547 }
548
549 uses_ = nullptr;
550 env_uses_ = nullptr;
551}
552
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100553void HInstruction::ReplaceInput(HInstruction* replacement, size_t index) {
554 InputAt(index)->RemoveUser(this, index);
555 SetRawInputAt(index, replacement);
556 replacement->AddUseAt(this, index);
557}
558
Nicolas Geoffray39468442014-09-02 15:17:15 +0100559size_t HInstruction::EnvironmentSize() const {
560 return HasEnvironment() ? environment_->Size() : 0;
561}
562
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100563void HPhi::AddInput(HInstruction* input) {
564 DCHECK(input->GetBlock() != nullptr);
565 inputs_.Add(input);
566 input->AddUseAt(this, inputs_.Size() - 1);
567}
568
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100569#define DEFINE_ACCEPT(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000570void H##name::Accept(HGraphVisitor* visitor) { \
571 visitor->Visit##name(this); \
572}
573
574FOR_EACH_INSTRUCTION(DEFINE_ACCEPT)
575
576#undef DEFINE_ACCEPT
577
578void HGraphVisitor::VisitInsertionOrder() {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100579 const GrowableArray<HBasicBlock*>& blocks = graph_->GetBlocks();
580 for (size_t i = 0 ; i < blocks.Size(); i++) {
581 VisitBasicBlock(blocks.Get(i));
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000582 }
583}
584
Roland Levillain633021e2014-10-01 14:12:25 +0100585void HGraphVisitor::VisitReversePostOrder() {
586 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
587 VisitBasicBlock(it.Current());
588 }
589}
590
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000591void HGraphVisitor::VisitBasicBlock(HBasicBlock* block) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100592 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100593 it.Current()->Accept(this);
594 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100595 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000596 it.Current()->Accept(this);
597 }
598}
599
Roland Levillain9240d6a2014-10-20 16:47:04 +0100600HConstant* HUnaryOperation::TryStaticEvaluation() const {
601 if (GetInput()->IsIntConstant()) {
602 int32_t value = Evaluate(GetInput()->AsIntConstant()->GetValue());
603 return new(GetBlock()->GetGraph()->GetArena()) HIntConstant(value);
604 } else if (GetInput()->IsLongConstant()) {
Roland Levillainb762d2e2014-10-22 10:11:06 +0100605 // TODO: Implement static evaluation of long unary operations.
606 //
607 // Do not exit with a fatal condition here. Instead, simply
608 // return `nullptr' to notify the caller that this instruction
609 // cannot (yet) be statically evaluated.
Roland Levillain9240d6a2014-10-20 16:47:04 +0100610 return nullptr;
611 }
612 return nullptr;
613}
614
615HConstant* HBinaryOperation::TryStaticEvaluation() const {
Roland Levillain556c3d12014-09-18 15:25:07 +0100616 if (GetLeft()->IsIntConstant() && GetRight()->IsIntConstant()) {
617 int32_t value = Evaluate(GetLeft()->AsIntConstant()->GetValue(),
618 GetRight()->AsIntConstant()->GetValue());
Roland Levillain9240d6a2014-10-20 16:47:04 +0100619 return new(GetBlock()->GetGraph()->GetArena()) HIntConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100620 } else if (GetLeft()->IsLongConstant() && GetRight()->IsLongConstant()) {
621 int64_t value = Evaluate(GetLeft()->AsLongConstant()->GetValue(),
622 GetRight()->AsLongConstant()->GetValue());
Roland Levillain9240d6a2014-10-20 16:47:04 +0100623 return new(GetBlock()->GetGraph()->GetArena()) HLongConstant(value);
Roland Levillain556c3d12014-09-18 15:25:07 +0100624 }
625 return nullptr;
626}
Dave Allison20dfc792014-06-16 20:44:29 -0700627
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100628bool HCondition::IsBeforeWhenDisregardMoves(HIf* if_) const {
629 HInstruction* previous = if_->GetPrevious();
630 while (previous != nullptr && previous->IsParallelMove()) {
631 previous = previous->GetPrevious();
632 }
633 return previous == this;
634}
635
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100636bool HInstruction::Equals(HInstruction* other) const {
637 if (!InstructionTypeEquals(other)) return false;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100638 DCHECK_EQ(GetKind(), other->GetKind());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100639 if (!InstructionDataEquals(other)) return false;
640 if (GetType() != other->GetType()) return false;
641 if (InputCount() != other->InputCount()) return false;
642
643 for (size_t i = 0, e = InputCount(); i < e; ++i) {
644 if (InputAt(i) != other->InputAt(i)) return false;
645 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100646 DCHECK_EQ(ComputeHashCode(), other->ComputeHashCode());
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100647 return true;
648}
649
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700650std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs) {
651#define DECLARE_CASE(type, super) case HInstruction::k##type: os << #type; break;
652 switch (rhs) {
653 FOR_EACH_INSTRUCTION(DECLARE_CASE)
654 default:
655 os << "Unknown instruction kind " << static_cast<int>(rhs);
656 break;
657 }
658#undef DECLARE_CASE
659 return os;
660}
661
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662} // namespace art