Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/hydrogen-dce.cc

Issue 18791002: Turn dead code elimination into a proper HPhase. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
34 void HDeadCodeEliminationPhase::MarkLive(HValue* ref, HValue* instr) {
35 if (!instr->CheckFlag(HValue::kIsLive)) {
36 instr->SetFlag(HValue::kIsLive);
37 worklist_.Add(instr, zone());
38
39 if (FLAG_trace_dead_code_elimination) {
40 HeapStringAllocator allocator;
41 StringStream stream(&allocator);
42 if (ref != NULL) {
43 ref->PrintTo(&stream);
44 } else {
45 stream.Add("root ");
46 }
47 stream.Add(" -> ");
48 instr->PrintTo(&stream);
49 PrintF("[MarkLive %s]\n", *stream.ToCString());
50 }
51 }
52 }
53
54
55 void HDeadCodeEliminationPhase::MarkLiveInstructions() {
56 ASSERT(worklist_.is_empty());
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 }
65 for (int j = 0; j < block->phis()->length(); j++) {
66 HPhi* phi = block->phis()->at(j);
67 if (phi->CannotBeEliminated()) MarkLive(NULL, phi);
68 }
69 }
70
71 // Transitively mark all inputs of live instructions live.
72 while (!worklist_.is_empty()) {
73 HValue* instr = worklist_.RemoveLast();
74 for (int i = 0; i < instr->OperandCount(); ++i) {
75 MarkLive(instr, instr->OperandAt(i));
76 }
77 }
78 }
79
80
81 void HDeadCodeEliminationPhase::RemoveDeadInstructions() {
82 ASSERT(worklist_.is_empty());
83
84 // Remove any instruction not marked kIsLive.
85 for (int i = 0; i < graph()->blocks()->length(); ++i) {
86 HBasicBlock* block = graph()->blocks()->at(i);
87 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
88 HInstruction* instr = it.Current();
89 if (!instr->CheckFlag(HValue::kIsLive)) {
90 // Instruction has not been marked live; assume it is dead and remove.
91 // TODO(titzer): we don't remove constants because some special ones
92 // might be used by later phases and are assumed to be in the graph
93 if (!instr->IsConstant()) instr->DeleteAndReplaceWith(NULL);
94 } else {
95 // Clear the liveness flag to leave the graph clean for the next DCE.
96 instr->ClearFlag(HValue::kIsLive);
97 }
98 }
99 // Collect phis that are dead and remove them in the next pass.
100 for (int j = 0; j < block->phis()->length(); j++) {
101 HPhi* phi = block->phis()->at(j);
102 if (!phi->CheckFlag(HValue::kIsLive)) {
103 worklist_.Add(phi, zone());
104 } else {
105 phi->ClearFlag(HValue::kIsLive);
106 }
107 }
108 }
109
110 // Process phis separately to avoid simultaneously mutating the phi list.
111 while (!worklist_.is_empty()) {
112 HPhi* phi = HPhi::cast(worklist_.RemoveLast());
113 HBasicBlock* block = phi->block();
114 phi->DeleteAndReplaceWith(NULL);
115 block->RecordDeletedPhi(phi->merged_index());
116 }
117 }
118
119 } } // namespace v8::internal
OLDNEW
« src/hydrogen-dce.h ('K') | « src/hydrogen-dce.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698