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" |
| 20 | |
| 21 | namespace art { |
| 22 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 23 | void HDeadCodeElimination::Run() { |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 24 | // Process basic blocks in post-order in the dominator tree, so that |
| 25 | // a dead instruction depending on another dead instruction is |
| 26 | // removed. |
| 27 | for (HPostOrderIterator b(*graph_); !b.Done(); b.Advance()) { |
| 28 | HBasicBlock* block = b.Current(); |
| 29 | // Traverse this block's instructions in backward order and remove |
| 30 | // the unused ones. |
| 31 | HBackwardInstructionIterator i(block->GetInstructions()); |
| 32 | // Skip the first iteration, as the last instruction of a block is |
| 33 | // a branching instruction. |
| 34 | DCHECK(i.Current()->IsControlFlow()); |
| 35 | for (i.Advance(); !i.Done(); i.Advance()) { |
| 36 | HInstruction* inst = i.Current(); |
| 37 | DCHECK(!inst->IsControlFlow()); |
Roland Levillain | e161a2a | 2014-10-03 12:45:18 +0100 | [diff] [blame] | 38 | if (!inst->HasSideEffects() |
| 39 | && !inst->CanThrow() |
| 40 | && !inst->IsSuspendCheck() |
| 41 | && !inst->HasUses()) { |
Roland Levillain | 72bceff | 2014-09-15 18:29:00 +0100 | [diff] [blame] | 42 | block->RemoveInstruction(inst); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | } // namespace art |