Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 1 | /* |
| 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 "dead_code_elimination.h" |
| 18 | |
| 19 | #include "base/bit_vector-inl.h" |
David Brazdil | 84daae5 | 2015-05-18 12:06:52 +0100 | [diff] [blame] | 20 | #include "ssa_phi_elimination.h" |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 24 | static void MarkReachableBlocks(HBasicBlock* block, ArenaBitVector* visited) { |
| 25 | int block_id = block->GetBlockId(); |
| 26 | if (visited->IsBitSet(block_id)) { |
| 27 | return; |
| 28 | } |
| 29 | visited->SetBit(block_id); |
| 30 | |
| 31 | HInstruction* last_instruction = block->GetLastInstruction(); |
| 32 | if (last_instruction->IsIf()) { |
| 33 | HIf* if_instruction = last_instruction->AsIf(); |
| 34 | HInstruction* condition = if_instruction->InputAt(0); |
| 35 | if (!condition->IsIntConstant()) { |
| 36 | MarkReachableBlocks(if_instruction->IfTrueSuccessor(), visited); |
| 37 | MarkReachableBlocks(if_instruction->IfFalseSuccessor(), visited); |
| 38 | } else if (condition->AsIntConstant()->IsOne()) { |
| 39 | MarkReachableBlocks(if_instruction->IfTrueSuccessor(), visited); |
| 40 | } else { |
| 41 | DCHECK(condition->AsIntConstant()->IsZero()); |
| 42 | MarkReachableBlocks(if_instruction->IfFalseSuccessor(), visited); |
| 43 | } |
| 44 | } else { |
| 45 | for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) { |
| 46 | MarkReachableBlocks(block->GetSuccessors().Get(i), visited); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 51 | static void MarkLoopHeadersContaining(const HBasicBlock& block, ArenaBitVector* set) { |
| 52 | for (HLoopInformationOutwardIterator it(block); !it.Done(); it.Advance()) { |
| 53 | set->SetBit(it.Current()->GetHeader()->GetBlockId()); |
| 54 | } |
| 55 | } |
| 56 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 57 | void HDeadCodeElimination::MaybeRecordDeadBlock(HBasicBlock* block) { |
| 58 | if (stats_ != nullptr) { |
| 59 | stats_->RecordStat(MethodCompilationStat::kRemovedDeadInstruction, |
| 60 | block->GetPhis().CountSize() + block->GetInstructions().CountSize()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void HDeadCodeElimination::RemoveDeadBlocks() { |
| 65 | // Classify blocks as reachable/unreachable. |
| 66 | ArenaAllocator* allocator = graph_->GetArena(); |
| 67 | ArenaBitVector live_blocks(allocator, graph_->GetBlocks().Size(), false); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 68 | ArenaBitVector affected_loops(allocator, graph_->GetBlocks().Size(), false); |
| 69 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 70 | MarkReachableBlocks(graph_->GetEntryBlock(), &live_blocks); |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame^] | 71 | bool removed_one_or_more_blocks = false; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 72 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 73 | // Remove all dead blocks. Iterate in post order because removal needs the |
| 74 | // block's chain of dominators and nested loops need to be updated from the |
| 75 | // inside out. |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 76 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 77 | HBasicBlock* block = it.Current(); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 78 | int id = block->GetBlockId(); |
| 79 | if (live_blocks.IsBitSet(id)) { |
| 80 | if (affected_loops.IsBitSet(id)) { |
| 81 | DCHECK(block->IsLoopHeader()); |
| 82 | block->GetLoopInformation()->Update(); |
| 83 | } |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 84 | } else { |
| 85 | MaybeRecordDeadBlock(block); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 86 | MarkLoopHeadersContaining(*block, &affected_loops); |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 87 | block->DisconnectAndDelete(); |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame^] | 88 | removed_one_or_more_blocks = true; |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 89 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame^] | 92 | // If we removed at least one block, we need to recompute the full |
| 93 | // dominator tree. |
| 94 | if (removed_one_or_more_blocks) { |
| 95 | graph_->ClearDominanceInformation(); |
| 96 | graph_->ComputeDominanceInformation(); |
| 97 | } |
| 98 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 99 | // Connect successive blocks created by dead branches. Order does not matter. |
| 100 | for (HReversePostOrderIterator it(*graph_); !it.Done();) { |
| 101 | HBasicBlock* block = it.Current(); |
| 102 | if (block->IsEntryBlock() || block->GetSuccessors().Size() != 1u) { |
| 103 | it.Advance(); |
| 104 | continue; |
| 105 | } |
| 106 | HBasicBlock* successor = block->GetSuccessors().Get(0); |
| 107 | if (successor->IsExitBlock() || successor->GetPredecessors().Size() != 1u) { |
| 108 | it.Advance(); |
| 109 | continue; |
| 110 | } |
| 111 | block->MergeWith(successor); |
| 112 | |
| 113 | // Reiterate on this block in case it can be merged with its new successor. |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void HDeadCodeElimination::RemoveDeadInstructions() { |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 118 | // Process basic blocks in post-order in the dominator tree, so that |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 119 | // a dead instruction depending on another dead instruction is removed. |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 120 | for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) { |
| 121 | HBasicBlock* block = b.Current(); |
| 122 | // Traverse this block's instructions in backward order and remove |
| 123 | // the unused ones. |
| 124 | HBackwardInstructionIterator i(block->GetInstructions()); |
| 125 | // Skip the first iteration, as the last instruction of a block is |
| 126 | // a branching instruction. |
| 127 | DCHECK(i.Current()->IsControlFlow()); |
| 128 | for (i.Advance(); !i.Done(); i.Advance()) { |
| 129 | HInstruction* inst = i.Current(); |
| 130 | DCHECK(!inst->IsControlFlow()); |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 131 | if (!inst->HasSideEffects() |
| 132 | && !inst->CanThrow() |
| 133 | && !inst->IsSuspendCheck() |
Nicolas Geoffray | 839188b | 2015-06-02 10:38:12 +0100 | [diff] [blame] | 134 | // If we added an explicit barrier then we should keep it. |
| 135 | && !inst->IsMemoryBarrier() |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 136 | && !inst->HasUses()) { |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 137 | block->RemoveInstruction(inst); |
Calin Juravle | 8f20bdb | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 138 | MaybeRecordStat(MethodCompilationStat::kRemovedDeadInstruction); |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 144 | void HDeadCodeElimination::Run() { |
| 145 | RemoveDeadBlocks(); |
David Brazdil | 84daae5 | 2015-05-18 12:06:52 +0100 | [diff] [blame] | 146 | SsaRedundantPhiElimination(graph_).Run(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 147 | RemoveDeadInstructions(); |
| 148 | } |
| 149 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 150 | } // namespace art |