v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hydrogen-representation-changes.cc
Go to the documentation of this file.
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 
29 
30 namespace v8 {
31 namespace internal {
32 
33 void HRepresentationChangesPhase::InsertRepresentationChangeForUse(
34  HValue* value, HValue* use_value, int use_index, Representation to) {
35  // Insert the representation change right before its use. For phi-uses we
36  // insert at the end of the corresponding predecessor.
37  HInstruction* next = NULL;
38  if (use_value->IsPhi()) {
39  next = use_value->block()->predecessors()->at(use_index)->end();
40  } else {
41  next = HInstruction::cast(use_value);
42  }
43  // For constants we try to make the representation change at compile
44  // time. When a representation change is not possible without loss of
45  // information we treat constants like normal instructions and insert the
46  // change instructions for them.
47  HInstruction* new_value = NULL;
48  bool is_truncating_to_smi = use_value->CheckFlag(HValue::kTruncatingToSmi);
49  bool is_truncating_to_int = use_value->CheckFlag(HValue::kTruncatingToInt32);
50  if (value->IsConstant()) {
51  HConstant* constant = HConstant::cast(value);
52  // Try to create a new copy of the constant with the new representation.
53  if (is_truncating_to_int && to.IsInteger32()) {
54  Maybe<HConstant*> res = constant->CopyToTruncatedInt32(graph()->zone());
55  if (res.has_value) new_value = res.value;
56  } else {
57  new_value = constant->CopyToRepresentation(to, graph()->zone());
58  }
59  }
60 
61  if (new_value == NULL) {
62  new_value = new(graph()->zone()) HChange(
63  value, to, is_truncating_to_smi, is_truncating_to_int);
64  if (!use_value->operand_position(use_index).IsUnknown()) {
65  new_value->set_position(use_value->operand_position(use_index));
66  } else {
67  ASSERT(!FLAG_hydrogen_track_positions ||
68  !graph()->info()->IsOptimizing());
69  }
70  }
71 
72  new_value->InsertBefore(next);
73  use_value->SetOperandAt(use_index, new_value);
74 }
75 
76 
77 void HRepresentationChangesPhase::InsertRepresentationChangesForValue(
78  HValue* value) {
79  Representation r = value->representation();
80  if (r.IsNone()) return;
81  if (value->HasNoUses()) {
82  if (value->IsForceRepresentation()) value->DeleteAndReplaceWith(NULL);
83  return;
84  }
85 
86  for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) {
87  HValue* use_value = it.value();
88  int use_index = it.index();
89  Representation req = use_value->RequiredInputRepresentation(use_index);
90  if (req.IsNone() || req.Equals(r)) continue;
91  InsertRepresentationChangeForUse(value, use_value, use_index, req);
92  }
93  if (value->HasNoUses()) {
94  ASSERT(value->IsConstant());
95  value->DeleteAndReplaceWith(NULL);
96  }
97 
98  // The only purpose of a HForceRepresentation is to represent the value
99  // after the (possible) HChange instruction. We make it disappear.
100  if (value->IsForceRepresentation()) {
101  value->DeleteAndReplaceWith(HForceRepresentation::cast(value)->value());
102  }
103 }
104 
105 
107  // Compute truncation flag for phis: Initially assume that all
108  // int32-phis allow truncation and iteratively remove the ones that
109  // are used in an operation that does not allow a truncating
110  // conversion.
111  ZoneList<HPhi*> int_worklist(8, zone());
112  ZoneList<HPhi*> smi_worklist(8, zone());
113 
114  const ZoneList<HPhi*>* phi_list(graph()->phi_list());
115  for (int i = 0; i < phi_list->length(); i++) {
116  HPhi* phi = phi_list->at(i);
117  if (phi->representation().IsInteger32()) {
118  phi->SetFlag(HValue::kTruncatingToInt32);
119  } else if (phi->representation().IsSmi()) {
120  phi->SetFlag(HValue::kTruncatingToSmi);
121  phi->SetFlag(HValue::kTruncatingToInt32);
122  }
123  }
124 
125  for (int i = 0; i < phi_list->length(); i++) {
126  HPhi* phi = phi_list->at(i);
127  HValue* value = NULL;
128  if (phi->representation().IsSmiOrInteger32() &&
129  !phi->CheckUsesForFlag(HValue::kTruncatingToInt32, &value)) {
130  int_worklist.Add(phi, zone());
131  phi->ClearFlag(HValue::kTruncatingToInt32);
132  if (FLAG_trace_representation) {
133  PrintF("#%d Phi is not truncating Int32 because of #%d %s\n",
134  phi->id(), value->id(), value->Mnemonic());
135  }
136  }
137 
138  if (phi->representation().IsSmi() &&
139  !phi->CheckUsesForFlag(HValue::kTruncatingToSmi, &value)) {
140  smi_worklist.Add(phi, zone());
141  phi->ClearFlag(HValue::kTruncatingToSmi);
142  if (FLAG_trace_representation) {
143  PrintF("#%d Phi is not truncating Smi because of #%d %s\n",
144  phi->id(), value->id(), value->Mnemonic());
145  }
146  }
147  }
148 
149  while (!int_worklist.is_empty()) {
150  HPhi* current = int_worklist.RemoveLast();
151  for (int i = 0; i < current->OperandCount(); ++i) {
152  HValue* input = current->OperandAt(i);
153  if (input->IsPhi() &&
154  input->representation().IsSmiOrInteger32() &&
156  if (FLAG_trace_representation) {
157  PrintF("#%d Phi is not truncating Int32 because of #%d %s\n",
158  input->id(), current->id(), current->Mnemonic());
159  }
161  int_worklist.Add(HPhi::cast(input), zone());
162  }
163  }
164  }
165 
166  while (!smi_worklist.is_empty()) {
167  HPhi* current = smi_worklist.RemoveLast();
168  for (int i = 0; i < current->OperandCount(); ++i) {
169  HValue* input = current->OperandAt(i);
170  if (input->IsPhi() &&
171  input->representation().IsSmi() &&
173  if (FLAG_trace_representation) {
174  PrintF("#%d Phi is not truncating Smi because of #%d %s\n",
175  input->id(), current->id(), current->Mnemonic());
176  }
178  smi_worklist.Add(HPhi::cast(input), zone());
179  }
180  }
181  }
182 
183  const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
184  for (int i = 0; i < blocks->length(); ++i) {
185  // Process phi instructions first.
186  const HBasicBlock* block(blocks->at(i));
187  const ZoneList<HPhi*>* phis = block->phis();
188  for (int j = 0; j < phis->length(); j++) {
189  InsertRepresentationChangesForValue(phis->at(j));
190  }
191 
192  // Process normal instructions.
193  for (HInstruction* current = block->first(); current != NULL; ) {
194  HInstruction* next = current->next();
195  InsertRepresentationChangesForValue(current);
196  current = next;
197  }
198  }
199 }
200 
201 } } // namespace v8::internal
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
void PrintF(const char *format,...)
Definition: v8utils.cc:40
T & at(int i) const
Definition: list.h:90
#define ASSERT(condition)
Definition: checks.h:329
HGraph * graph() const
Definition: hydrogen.h:2695
Representation representation() const
HInstruction * next() const
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function info
Definition: flags.cc:317
bool CheckFlag(Flag f) const
virtual HValue * OperandAt(int index) const =0
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:39
const char * Mnemonic() const
static HValue * cast(HValue *value)