mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "hydrogen-dce.h" |
| 29 | #include "v8.h" |
| 30 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 34 | void HDeadCodeEliminationPhase::MarkLive( |
| 35 | HValue* instr, ZoneList<HValue*>* worklist) { |
| 36 | if (instr->CheckFlag(HValue::kIsLive)) return; // Already live. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 37 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 38 | if (FLAG_trace_dead_code_elimination) PrintLive(NULL, instr); |
| 39 | |
| 40 | // Transitively mark all inputs of live instructions live. |
| 41 | worklist->Add(instr, zone()); |
| 42 | while (!worklist->is_empty()) { |
| 43 | HValue* instr = worklist->RemoveLast(); |
| 44 | instr->SetFlag(HValue::kIsLive); |
| 45 | for (int i = 0; i < instr->OperandCount(); ++i) { |
| 46 | HValue* input = instr->OperandAt(i); |
| 47 | if (!input->CheckFlag(HValue::kIsLive)) { |
| 48 | input->SetFlag(HValue::kIsLive); |
| 49 | worklist->Add(input, zone()); |
| 50 | if (FLAG_trace_dead_code_elimination) PrintLive(instr, input); |
| 51 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 52 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 53 | } |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 54 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 55 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 56 | |
| 57 | void HDeadCodeEliminationPhase::PrintLive(HValue* ref, HValue* instr) { |
| 58 | HeapStringAllocator allocator; |
| 59 | StringStream stream(&allocator); |
| 60 | if (ref != NULL) { |
| 61 | ref->PrintTo(&stream); |
| 62 | } else { |
| 63 | stream.Add("root "); |
| 64 | } |
| 65 | stream.Add(" -> "); |
| 66 | instr->PrintTo(&stream); |
| 67 | PrintF("[MarkLive %s]\n", *stream.ToCString()); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
| 71 | void HDeadCodeEliminationPhase::MarkLiveInstructions() { |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 72 | ZoneList<HValue*> worklist(10, zone()); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 73 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 74 | // Transitively mark all live instructions, starting from roots. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 75 | for (int i = 0; i < graph()->blocks()->length(); ++i) { |
| 76 | HBasicBlock* block = graph()->blocks()->at(i); |
| 77 | for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
| 78 | HInstruction* instr = it.Current(); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 79 | if (instr->CannotBeEliminated()) MarkLive(instr, &worklist); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 80 | } |
| 81 | for (int j = 0; j < block->phis()->length(); j++) { |
| 82 | HPhi* phi = block->phis()->at(j); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 83 | if (phi->CannotBeEliminated()) MarkLive(phi, &worklist); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 87 | ASSERT(worklist.is_empty()); // Should have processed everything. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | void HDeadCodeEliminationPhase::RemoveDeadInstructions() { |
| 92 | ZoneList<HPhi*> worklist(graph()->blocks()->length(), zone()); |
| 93 | |
| 94 | // Remove any instruction not marked kIsLive. |
| 95 | for (int i = 0; i < graph()->blocks()->length(); ++i) { |
| 96 | HBasicBlock* block = graph()->blocks()->at(i); |
| 97 | for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
| 98 | HInstruction* instr = it.Current(); |
| 99 | if (!instr->CheckFlag(HValue::kIsLive)) { |
| 100 | // Instruction has not been marked live; assume it is dead and remove. |
| 101 | // TODO(titzer): we don't remove constants because some special ones |
| 102 | // might be used by later phases and are assumed to be in the graph |
| 103 | if (!instr->IsConstant()) instr->DeleteAndReplaceWith(NULL); |
| 104 | } else { |
| 105 | // Clear the liveness flag to leave the graph clean for the next DCE. |
| 106 | instr->ClearFlag(HValue::kIsLive); |
| 107 | } |
| 108 | } |
| 109 | // Collect phis that are dead and remove them in the next pass. |
| 110 | for (int j = 0; j < block->phis()->length(); j++) { |
| 111 | HPhi* phi = block->phis()->at(j); |
| 112 | if (!phi->CheckFlag(HValue::kIsLive)) { |
| 113 | worklist.Add(phi, zone()); |
| 114 | } else { |
| 115 | phi->ClearFlag(HValue::kIsLive); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Process phis separately to avoid simultaneously mutating the phi list. |
| 121 | while (!worklist.is_empty()) { |
| 122 | HPhi* phi = worklist.RemoveLast(); |
| 123 | HBasicBlock* block = phi->block(); |
| 124 | phi->DeleteAndReplaceWith(NULL); |
danno@chromium.org | ad75d6f | 2013-08-12 16:57:59 +0000 | [diff] [blame] | 125 | if (phi->HasMergedIndex()) { |
| 126 | block->RecordDeletedPhi(phi->merged_index()); |
| 127 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | } } // namespace v8::internal |