Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / v8 / src / hydrogen-representation-changes.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/hydrogen-representation-changes.h"
6
7 namespace v8 {
8 namespace internal {
9
10 void HRepresentationChangesPhase::InsertRepresentationChangeForUse(
11     HValue* value, HValue* use_value, int use_index, Representation to) {
12   // Insert the representation change right before its use. For phi-uses we
13   // insert at the end of the corresponding predecessor.
14   HInstruction* next = NULL;
15   if (use_value->IsPhi()) {
16     next = use_value->block()->predecessors()->at(use_index)->end();
17   } else {
18     next = HInstruction::cast(use_value);
19   }
20   // For constants we try to make the representation change at compile
21   // time. When a representation change is not possible without loss of
22   // information we treat constants like normal instructions and insert the
23   // change instructions for them.
24   HInstruction* new_value = NULL;
25   bool is_truncating_to_smi = use_value->CheckFlag(HValue::kTruncatingToSmi);
26   bool is_truncating_to_int = use_value->CheckFlag(HValue::kTruncatingToInt32);
27   if (value->IsConstant()) {
28     HConstant* constant = HConstant::cast(value);
29     // Try to create a new copy of the constant with the new representation.
30     if (is_truncating_to_int && to.IsInteger32()) {
31       Maybe<HConstant*> res = constant->CopyToTruncatedInt32(graph()->zone());
32       if (res.has_value) new_value = res.value;
33     } else {
34       new_value = constant->CopyToRepresentation(to, graph()->zone());
35     }
36   }
37
38   if (new_value == NULL) {
39     if (((to.IsFloat32x4() || to.IsFloat64x2() || to.IsInt32x4()) &&
40          !value->representation().IsTagged()) ||
41         ((value->representation().IsFloat32x4() ||
42           value->representation().IsFloat64x2() ||
43           value->representation().IsInt32x4()) &&
44          !to.IsTagged())) {
45       new_value = HUnarySIMDOperation::New(graph()->zone(),
46           graph()->entry_block()->last_environment()->context(),
47           value, kSIMD128Change, to);
48     } else {
49       new_value = new(graph()->zone()) HChange(
50           value, to, is_truncating_to_smi, is_truncating_to_int);
51     }
52     if (!use_value->operand_position(use_index).IsUnknown()) {
53       new_value->set_position(use_value->operand_position(use_index));
54     } else {
55       DCHECK(!FLAG_hydrogen_track_positions ||
56              !graph()->info()->IsOptimizing());
57     }
58   }
59
60   new_value->InsertBefore(next);
61   use_value->SetOperandAt(use_index, new_value);
62 }
63
64
65 static bool IsNonDeoptingIntToSmiChange(HChange* change) {
66   Representation from_rep = change->from();
67   Representation to_rep = change->to();
68   // Flags indicating Uint32 operations are set in a later Hydrogen phase.
69   DCHECK(!change->CheckFlag(HValue::kUint32));
70   return from_rep.IsInteger32() && to_rep.IsSmi() && SmiValuesAre32Bits();
71 }
72
73
74 void HRepresentationChangesPhase::InsertRepresentationChangesForValue(
75     HValue* value) {
76   Representation r = value->representation();
77   if (r.IsNone()) return;
78   if (value->HasNoUses()) {
79     if (value->IsForceRepresentation()) value->DeleteAndReplaceWith(NULL);
80     return;
81   }
82
83   for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) {
84     HValue* use_value = it.value();
85     int use_index = it.index();
86     Representation req = use_value->RequiredInputRepresentation(use_index);
87     if (req.IsNone() || req.Equals(r)) continue;
88
89     // If this is an HForceRepresentation instruction, and an HChange has been
90     // inserted above it, examine the input representation of the HChange. If
91     // that's int32, and this HForceRepresentation use is int32, and int32 to
92     // smi changes can't cause deoptimisation, set the input of the use to the
93     // input of the HChange.
94     if (value->IsForceRepresentation()) {
95       HValue* input = HForceRepresentation::cast(value)->value();
96       if (input->IsChange()) {
97         HChange* change = HChange::cast(input);
98         if (change->from().Equals(req) && IsNonDeoptingIntToSmiChange(change)) {
99           use_value->SetOperandAt(use_index, change->value());
100           continue;
101         }
102       }
103     }
104     InsertRepresentationChangeForUse(value, use_value, use_index, req);
105   }
106   if (value->HasNoUses()) {
107     DCHECK(value->IsConstant() || value->IsForceRepresentation());
108     value->DeleteAndReplaceWith(NULL);
109   } else {
110     // The only purpose of a HForceRepresentation is to represent the value
111     // after the (possible) HChange instruction.  We make it disappear.
112     if (value->IsForceRepresentation()) {
113       value->DeleteAndReplaceWith(HForceRepresentation::cast(value)->value());
114     }
115   }
116 }
117
118
119 void HRepresentationChangesPhase::Run() {
120   // Compute truncation flag for phis: Initially assume that all
121   // int32-phis allow truncation and iteratively remove the ones that
122   // are used in an operation that does not allow a truncating
123   // conversion.
124   ZoneList<HPhi*> int_worklist(8, zone());
125   ZoneList<HPhi*> smi_worklist(8, zone());
126
127   const ZoneList<HPhi*>* phi_list(graph()->phi_list());
128   for (int i = 0; i < phi_list->length(); i++) {
129     HPhi* phi = phi_list->at(i);
130     if (phi->representation().IsInteger32()) {
131       phi->SetFlag(HValue::kTruncatingToInt32);
132     } else if (phi->representation().IsSmi()) {
133       phi->SetFlag(HValue::kTruncatingToSmi);
134       phi->SetFlag(HValue::kTruncatingToInt32);
135     }
136   }
137
138   for (int i = 0; i < phi_list->length(); i++) {
139     HPhi* phi = phi_list->at(i);
140     HValue* value = NULL;
141     if (phi->representation().IsSmiOrInteger32() &&
142         !phi->CheckUsesForFlag(HValue::kTruncatingToInt32, &value)) {
143       int_worklist.Add(phi, zone());
144       phi->ClearFlag(HValue::kTruncatingToInt32);
145       if (FLAG_trace_representation) {
146         PrintF("#%d Phi is not truncating Int32 because of #%d %s\n",
147                phi->id(), value->id(), value->Mnemonic());
148       }
149     }
150
151     if (phi->representation().IsSmi() &&
152         !phi->CheckUsesForFlag(HValue::kTruncatingToSmi, &value)) {
153       smi_worklist.Add(phi, zone());
154       phi->ClearFlag(HValue::kTruncatingToSmi);
155       if (FLAG_trace_representation) {
156         PrintF("#%d Phi is not truncating Smi because of #%d %s\n",
157                phi->id(), value->id(), value->Mnemonic());
158       }
159     }
160   }
161
162   while (!int_worklist.is_empty()) {
163     HPhi* current = int_worklist.RemoveLast();
164     for (int i = 0; i < current->OperandCount(); ++i) {
165       HValue* input = current->OperandAt(i);
166       if (input->IsPhi() &&
167           input->representation().IsSmiOrInteger32() &&
168           input->CheckFlag(HValue::kTruncatingToInt32)) {
169         if (FLAG_trace_representation) {
170           PrintF("#%d Phi is not truncating Int32 because of #%d %s\n",
171                  input->id(), current->id(), current->Mnemonic());
172         }
173         input->ClearFlag(HValue::kTruncatingToInt32);
174         int_worklist.Add(HPhi::cast(input), zone());
175       }
176     }
177   }
178
179   while (!smi_worklist.is_empty()) {
180     HPhi* current = smi_worklist.RemoveLast();
181     for (int i = 0; i < current->OperandCount(); ++i) {
182       HValue* input = current->OperandAt(i);
183       if (input->IsPhi() &&
184           input->representation().IsSmi() &&
185           input->CheckFlag(HValue::kTruncatingToSmi)) {
186         if (FLAG_trace_representation) {
187           PrintF("#%d Phi is not truncating Smi because of #%d %s\n",
188                  input->id(), current->id(), current->Mnemonic());
189         }
190         input->ClearFlag(HValue::kTruncatingToSmi);
191         smi_worklist.Add(HPhi::cast(input), zone());
192       }
193     }
194   }
195
196   const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
197   for (int i = 0; i < blocks->length(); ++i) {
198     // Process phi instructions first.
199     const HBasicBlock* block(blocks->at(i));
200     const ZoneList<HPhi*>* phis = block->phis();
201     for (int j = 0; j < phis->length(); j++) {
202       InsertRepresentationChangesForValue(phis->at(j));
203     }
204
205     // Process normal instructions.
206     for (HInstruction* current = block->first(); current != NULL; ) {
207       HInstruction* next = current->next();
208       InsertRepresentationChangesForValue(current);
209       current = next;
210     }
211   }
212 }
213
214 } }  // namespace v8::internal