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); |
| 71 | |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 72 | // Remove all dead blocks. Iterate in post order because removal needs the |
| 73 | // block's chain of dominators and nested loops need to be updated from the |
| 74 | // inside out. |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 75 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 76 | HBasicBlock* block = it.Current(); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 77 | int id = block->GetBlockId(); |
| 78 | if (live_blocks.IsBitSet(id)) { |
| 79 | if (affected_loops.IsBitSet(id)) { |
| 80 | DCHECK(block->IsLoopHeader()); |
| 81 | block->GetLoopInformation()->Update(); |
| 82 | } |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 83 | } else { |
| 84 | MaybeRecordDeadBlock(block); |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 85 | MarkLoopHeadersContaining(*block, &affected_loops); |
David Brazdil | 69a2804 | 2015-04-29 17:16:07 +0100 | [diff] [blame] | 86 | block->DisconnectAndDelete(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 87 | } |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Connect successive blocks created by dead branches. Order does not matter. |
| 91 | for (HReversePostOrderIterator it(*graph_); !it.Done();) { |
| 92 | HBasicBlock* block = it.Current(); |
| 93 | if (block->IsEntryBlock() || block->GetSuccessors().Size() != 1u) { |
| 94 | it.Advance(); |
| 95 | continue; |
| 96 | } |
| 97 | HBasicBlock* successor = block->GetSuccessors().Get(0); |
| 98 | if (successor->IsExitBlock() || successor->GetPredecessors().Size() != 1u) { |
| 99 | it.Advance(); |
| 100 | continue; |
| 101 | } |
| 102 | block->MergeWith(successor); |
| 103 | |
| 104 | // Reiterate on this block in case it can be merged with its new successor. |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void HDeadCodeElimination::RemoveDeadInstructions() { |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 109 | // Process basic blocks in post-order in the dominator tree, so that |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 110 | // a dead instruction depending on another dead instruction is removed. |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 111 | for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) { |
| 112 | HBasicBlock* block = b.Current(); |
| 113 | // Traverse this block's instructions in backward order and remove |
| 114 | // the unused ones. |
| 115 | HBackwardInstructionIterator i(block->GetInstructions()); |
| 116 | // Skip the first iteration, as the last instruction of a block is |
| 117 | // a branching instruction. |
| 118 | DCHECK(i.Current()->IsControlFlow()); |
| 119 | for (i.Advance(); !i.Done(); i.Advance()) { |
| 120 | HInstruction* inst = i.Current(); |
| 121 | DCHECK(!inst->IsControlFlow()); |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 122 | if (!inst->HasSideEffects() |
| 123 | && !inst->CanThrow() |
| 124 | && !inst->IsSuspendCheck() |
Nicolas Geoffray | 839188b | 2015-06-02 10:38:12 +0100 | [diff] [blame] | 125 | // If we added an explicit barrier then we should keep it. |
| 126 | && !inst->IsMemoryBarrier() |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 127 | && !inst->HasUses()) { |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 128 | block->RemoveInstruction(inst); |
Calin Juravle | 8f20bdb | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 129 | MaybeRecordStat(MethodCompilationStat::kRemovedDeadInstruction); |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 135 | void HDeadCodeElimination::Run() { |
| 136 | RemoveDeadBlocks(); |
David Brazdil | 84daae5 | 2015-05-18 12:06:52 +0100 | [diff] [blame] | 137 | SsaRedundantPhiElimination(graph_).Run(); |
David Brazdil | 2d7352b | 2015-04-20 14:52:42 +0100 | [diff] [blame] | 138 | RemoveDeadInstructions(); |
| 139 | } |
| 140 | |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 141 | } // namespace art |