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

Side by Side Diff: src/hydrogen-infer-representation.cc

Issue 18054002: Refactor HInferRepresentation into an HPhase and use the phase zone. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rename hydrogen-phase.{cc,h} to hydrogen-infer-representation.{cc,h} 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
« no previous file with comments | « src/hydrogen-infer-representation.h ('k') | src/hydrogen-instructions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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-infer-representation.h"
29
30 namespace v8 {
31 namespace internal {
32
33 void HInferRepresentationPhase::AddToWorklist(HValue* current) {
34 if (current->representation().IsTagged()) return;
35 if (!current->CheckFlag(HValue::kFlexibleRepresentation)) return;
36 if (in_worklist_.Contains(current->id())) return;
37 worklist_.Add(current, zone());
38 in_worklist_.Add(current->id());
39 }
40
41
42 void HInferRepresentationPhase::Run() {
43 // (1) Initialize bit vectors and count real uses. Each phi gets a
44 // bit-vector of length <number of phis>.
45 const ZoneList<HPhi*>* phi_list = graph()->phi_list();
46 int phi_count = phi_list->length();
47 ZoneList<BitVector*> connected_phis(phi_count, zone());
48 for (int i = 0; i < phi_count; ++i) {
49 phi_list->at(i)->InitRealUses(i);
50 BitVector* connected_set = new(zone()) BitVector(phi_count, zone());
51 connected_set->Add(i);
52 connected_phis.Add(connected_set, zone());
53 }
54
55 // (2) Do a fixed point iteration to find the set of connected phis. A
56 // phi is connected to another phi if its value is used either directly or
57 // indirectly through a transitive closure of the def-use relation.
58 bool change = true;
59 while (change) {
60 change = false;
61 // We normally have far more "forward edges" than "backward edges",
62 // so we terminate faster when we walk backwards.
63 for (int i = phi_count - 1; i >= 0; --i) {
64 HPhi* phi = phi_list->at(i);
65 for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) {
66 HValue* use = it.value();
67 if (use->IsPhi()) {
68 int id = HPhi::cast(use)->phi_id();
69 if (connected_phis[i]->UnionIsChanged(*connected_phis[id]))
70 change = true;
71 }
72 }
73 }
74 }
75
76 // Set truncation flags for groups of connected phis. This is a conservative
77 // approximation; the flag will be properly re-computed after representations
78 // have been determined.
79 if (phi_count > 0) {
80 BitVector done(phi_count, zone());
81 for (int i = 0; i < phi_count; ++i) {
82 if (done.Contains(i)) continue;
83
84 // Check if all uses of all connected phis in this group are truncating.
85 bool all_uses_everywhere_truncating = true;
86 for (BitVector::Iterator it(connected_phis[i]);
87 !it.Done();
88 it.Advance()) {
89 int index = it.Current();
90 all_uses_everywhere_truncating &=
91 phi_list->at(index)->CheckFlag(HInstruction::kTruncatingToInt32);
92 done.Add(index);
93 }
94 if (all_uses_everywhere_truncating) {
95 continue; // Great, nothing to do.
96 }
97 // Clear truncation flag of this group of connected phis.
98 for (BitVector::Iterator it(connected_phis[i]);
99 !it.Done();
100 it.Advance()) {
101 int index = it.Current();
102 phi_list->at(index)->ClearFlag(HInstruction::kTruncatingToInt32);
103 }
104 }
105 }
106
107 // Simplify constant phi inputs where possible.
108 // This step uses kTruncatingToInt32 flags of phis.
109 for (int i = 0; i < phi_count; ++i) {
110 phi_list->at(i)->SimplifyConstantInputs();
111 }
112
113 // Use the phi reachability information from step 2 to
114 // sum up the non-phi use counts of all connected phis.
115 for (int i = 0; i < phi_count; ++i) {
116 HPhi* phi = phi_list->at(i);
117 for (BitVector::Iterator it(connected_phis[i]);
118 !it.Done();
119 it.Advance()) {
120 int index = it.Current();
121 HPhi* it_use = phi_list->at(index);
122 if (index != i) phi->AddNonPhiUsesFrom(it_use); // Don't count twice.
123 }
124 }
125
126 // Initialize work list
127 for (int i = 0; i < graph()->blocks()->length(); ++i) {
128 HBasicBlock* block = graph()->blocks()->at(i);
129 const ZoneList<HPhi*>* phis = block->phis();
130 for (int j = 0; j < phis->length(); ++j) {
131 AddToWorklist(phis->at(j));
132 }
133
134 HInstruction* current = block->first();
135 while (current != NULL) {
136 AddToWorklist(current);
137 current = current->next();
138 }
139 }
140
141 // Do a fixed point iteration, trying to improve representations
142 while (!worklist_.is_empty()) {
143 HValue* current = worklist_.RemoveLast();
144 in_worklist_.Remove(current->id());
145 current->InferRepresentation(this);
146 }
147
148 // Lastly: any instruction that we don't have representation information
149 // for defaults to Tagged.
150 for (int i = 0; i < graph()->blocks()->length(); ++i) {
151 HBasicBlock* block = graph()->blocks()->at(i);
152 const ZoneList<HPhi*>* phis = block->phis();
153 for (int j = 0; j < phis->length(); ++j) {
154 HPhi* phi = phis->at(j);
155 if (phi->representation().IsNone()) {
156 phi->ChangeRepresentation(Representation::Tagged());
157 }
158 }
159 for (HInstruction* current = block->first();
160 current != NULL; current = current->next()) {
161 if (current->representation().IsNone() &&
162 current->CheckFlag(HInstruction::kFlexibleRepresentation)) {
163 if (current->CheckFlag(HInstruction::kCannotBeTagged)) {
164 current->ChangeRepresentation(Representation::Double());
165 } else {
166 current->ChangeRepresentation(Representation::Tagged());
167 }
168 }
169 }
170 }
171 }
172
173 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/hydrogen-infer-representation.h ('k') | src/hydrogen-instructions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698