| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "hydrogen-dce.h" | 28 #include "hydrogen-dce.h" |
| 29 #include "v8.h" | 29 #include "v8.h" |
| 30 | 30 |
| 31 namespace v8 { | 31 namespace v8 { |
| 32 namespace internal { | 32 namespace internal { |
| 33 | 33 |
| 34 bool HDeadCodeEliminationPhase::MarkLive(HValue* ref, HValue* instr) { | 34 void HDeadCodeEliminationPhase::MarkLive( |
| 35 if (instr->CheckFlag(HValue::kIsLive)) return false; | 35 HValue* instr, ZoneList<HValue*>* worklist) { |
| 36 instr->SetFlag(HValue::kIsLive); | 36 if (instr->CheckFlag(HValue::kIsLive)) return; // Already live. |
| 37 | 37 |
| 38 if (FLAG_trace_dead_code_elimination) { | 38 if (FLAG_trace_dead_code_elimination) PrintLive(NULL, instr); |
| 39 HeapStringAllocator allocator; | |
| 40 StringStream stream(&allocator); | |
| 41 if (ref != NULL) { | |
| 42 ref->PrintTo(&stream); | |
| 43 } else { | |
| 44 stream.Add("root "); | |
| 45 } | |
| 46 stream.Add(" -> "); | |
| 47 instr->PrintTo(&stream); | |
| 48 PrintF("[MarkLive %s]\n", *stream.ToCString()); | |
| 49 } | |
| 50 | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 | |
| 55 void HDeadCodeEliminationPhase::MarkLiveInstructions() { | |
| 56 ZoneList<HValue*> worklist(graph()->blocks()->length(), zone()); | |
| 57 | |
| 58 // Mark initial root instructions for dead code elimination. | |
| 59 for (int i = 0; i < graph()->blocks()->length(); ++i) { | |
| 60 HBasicBlock* block = graph()->blocks()->at(i); | |
| 61 for (HInstructionIterator it(block); !it.Done(); it.Advance()) { | |
| 62 HInstruction* instr = it.Current(); | |
| 63 if (instr->CannotBeEliminated() && MarkLive(NULL, instr)) { | |
| 64 worklist.Add(instr, zone()); | |
| 65 } | |
| 66 } | |
| 67 for (int j = 0; j < block->phis()->length(); j++) { | |
| 68 HPhi* phi = block->phis()->at(j); | |
| 69 if (phi->CannotBeEliminated() && MarkLive(NULL, phi)) { | |
| 70 worklist.Add(phi, zone()); | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 | 39 |
| 75 // Transitively mark all inputs of live instructions live. | 40 // Transitively mark all inputs of live instructions live. |
| 76 while (!worklist.is_empty()) { | 41 worklist->Add(instr, zone()); |
| 77 HValue* instr = worklist.RemoveLast(); | 42 while (!worklist->is_empty()) { |
| 43 HValue* instr = worklist->RemoveLast(); |
| 44 instr->SetFlag(HValue::kIsLive); |
| 78 for (int i = 0; i < instr->OperandCount(); ++i) { | 45 for (int i = 0; i < instr->OperandCount(); ++i) { |
| 79 if (MarkLive(instr, instr->OperandAt(i))) { | 46 HValue* input = instr->OperandAt(i); |
| 80 worklist.Add(instr->OperandAt(i), zone()); | 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); |
| 81 } | 51 } |
| 82 } | 52 } |
| 83 } | 53 } |
| 84 } | 54 } |
| 85 | 55 |
| 86 | 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()); |
| 68 } |
| 69 |
| 70 |
| 71 void HDeadCodeEliminationPhase::MarkLiveInstructions() { |
| 72 ZoneList<HValue*> worklist(10, zone()); |
| 73 |
| 74 // Transitively mark all live instructions, starting from roots. |
| 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(); |
| 79 if (instr->CannotBeEliminated()) MarkLive(instr, &worklist); |
| 80 } |
| 81 for (int j = 0; j < block->phis()->length(); j++) { |
| 82 HPhi* phi = block->phis()->at(j); |
| 83 if (phi->CannotBeEliminated()) MarkLive(phi, &worklist); |
| 84 } |
| 85 } |
| 86 |
| 87 ASSERT(worklist.is_empty()); // Should have processed everything. |
| 88 } |
| 89 |
| 90 |
| 87 void HDeadCodeEliminationPhase::RemoveDeadInstructions() { | 91 void HDeadCodeEliminationPhase::RemoveDeadInstructions() { |
| 88 ZoneList<HPhi*> worklist(graph()->blocks()->length(), zone()); | 92 ZoneList<HPhi*> worklist(graph()->blocks()->length(), zone()); |
| 89 | 93 |
| 90 // Remove any instruction not marked kIsLive. | 94 // Remove any instruction not marked kIsLive. |
| 91 for (int i = 0; i < graph()->blocks()->length(); ++i) { | 95 for (int i = 0; i < graph()->blocks()->length(); ++i) { |
| 92 HBasicBlock* block = graph()->blocks()->at(i); | 96 HBasicBlock* block = graph()->blocks()->at(i); |
| 93 for (HInstructionIterator it(block); !it.Done(); it.Advance()) { | 97 for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
| 94 HInstruction* instr = it.Current(); | 98 HInstruction* instr = it.Current(); |
| 95 if (!instr->CheckFlag(HValue::kIsLive)) { | 99 if (!instr->CheckFlag(HValue::kIsLive)) { |
| 96 // Instruction has not been marked live; assume it is dead and remove. | 100 // Instruction has not been marked live, so remove it. |
| 97 // TODO(titzer): we don't remove constants because some special ones | 101 instr->DeleteAndReplaceWith(NULL); |
| 98 // might be used by later phases and are assumed to be in the graph | |
| 99 if (!instr->IsConstant()) instr->DeleteAndReplaceWith(NULL); | |
| 100 } else { | 102 } else { |
| 101 // Clear the liveness flag to leave the graph clean for the next DCE. | 103 // Clear the liveness flag to leave the graph clean for the next DCE. |
| 102 instr->ClearFlag(HValue::kIsLive); | 104 instr->ClearFlag(HValue::kIsLive); |
| 103 } | 105 } |
| 104 } | 106 } |
| 105 // Collect phis that are dead and remove them in the next pass. | 107 // Collect phis that are dead and remove them in the next pass. |
| 106 for (int j = 0; j < block->phis()->length(); j++) { | 108 for (int j = 0; j < block->phis()->length(); j++) { |
| 107 HPhi* phi = block->phis()->at(j); | 109 HPhi* phi = block->phis()->at(j); |
| 108 if (!phi->CheckFlag(HValue::kIsLive)) { | 110 if (!phi->CheckFlag(HValue::kIsLive)) { |
| 109 worklist.Add(phi, zone()); | 111 worklist.Add(phi, zone()); |
| 110 } else { | 112 } else { |
| 111 phi->ClearFlag(HValue::kIsLive); | 113 phi->ClearFlag(HValue::kIsLive); |
| 112 } | 114 } |
| 113 } | 115 } |
| 114 } | 116 } |
| 115 | 117 |
| 116 // Process phis separately to avoid simultaneously mutating the phi list. | 118 // Process phis separately to avoid simultaneously mutating the phi list. |
| 117 while (!worklist.is_empty()) { | 119 while (!worklist.is_empty()) { |
| 118 HPhi* phi = worklist.RemoveLast(); | 120 HPhi* phi = worklist.RemoveLast(); |
| 119 HBasicBlock* block = phi->block(); | 121 HBasicBlock* block = phi->block(); |
| 120 phi->DeleteAndReplaceWith(NULL); | 122 phi->DeleteAndReplaceWith(NULL); |
| 121 if (phi->HasMergedIndex()) { | 123 if (phi->HasMergedIndex()) { |
| 122 block->RecordDeletedPhi(phi->merged_index()); | 124 block->RecordDeletedPhi(phi->merged_index()); |
| 123 } | 125 } |
| 124 } | 126 } |
| 125 } | 127 } |
| 126 | 128 |
| 127 } } // namespace v8::internal | 129 } } // namespace v8::internal |
| OLD | NEW |