V8 Project
hydrogen-representation-changes.cc
Go to the documentation of this file.
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 
6 
7 namespace v8 {
8 namespace internal {
9 
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  new_value = new(graph()->zone()) HChange(
40  value, to, is_truncating_to_smi, is_truncating_to_int);
41  if (!use_value->operand_position(use_index).IsUnknown()) {
42  new_value->set_position(use_value->operand_position(use_index));
43  } else {
44  DCHECK(!FLAG_hydrogen_track_positions ||
45  !graph()->info()->IsOptimizing());
46  }
47  }
48 
49  new_value->InsertBefore(next);
50  use_value->SetOperandAt(use_index, new_value);
51 }
52 
53 
54 static bool IsNonDeoptingIntToSmiChange(HChange* change) {
55  Representation from_rep = change->from();
56  Representation to_rep = change->to();
57  // Flags indicating Uint32 operations are set in a later Hydrogen phase.
58  DCHECK(!change->CheckFlag(HValue::kUint32));
59  return from_rep.IsInteger32() && to_rep.IsSmi() && SmiValuesAre32Bits();
60 }
61 
62 
64  HValue* value) {
65  Representation r = value->representation();
66  if (r.IsNone()) return;
67  if (value->HasNoUses()) {
68  if (value->IsForceRepresentation()) value->DeleteAndReplaceWith(NULL);
69  return;
70  }
71 
72  for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) {
73  HValue* use_value = it.value();
74  int use_index = it.index();
75  Representation req = use_value->RequiredInputRepresentation(use_index);
76  if (req.IsNone() || req.Equals(r)) continue;
77 
78  // If this is an HForceRepresentation instruction, and an HChange has been
79  // inserted above it, examine the input representation of the HChange. If
80  // that's int32, and this HForceRepresentation use is int32, and int32 to
81  // smi changes can't cause deoptimisation, set the input of the use to the
82  // input of the HChange.
83  if (value->IsForceRepresentation()) {
84  HValue* input = HForceRepresentation::cast(value)->value();
85  if (input->IsChange()) {
86  HChange* change = HChange::cast(input);
87  if (change->from().Equals(req) && IsNonDeoptingIntToSmiChange(change)) {
88  use_value->SetOperandAt(use_index, change->value());
89  continue;
90  }
91  }
92  }
93  InsertRepresentationChangeForUse(value, use_value, use_index, req);
94  }
95  if (value->HasNoUses()) {
96  DCHECK(value->IsConstant() || value->IsForceRepresentation());
97  value->DeleteAndReplaceWith(NULL);
98  } else {
99  // The only purpose of a HForceRepresentation is to represent the value
100  // after the (possible) HChange instruction. We make it disappear.
101  if (value->IsForceRepresentation()) {
102  value->DeleteAndReplaceWith(HForceRepresentation::cast(value)->value());
103  }
104  }
105 }
106 
107 
109  // Compute truncation flag for phis: Initially assume that all
110  // int32-phis allow truncation and iteratively remove the ones that
111  // are used in an operation that does not allow a truncating
112  // conversion.
113  ZoneList<HPhi*> int_worklist(8, zone());
114  ZoneList<HPhi*> smi_worklist(8, zone());
115 
116  const ZoneList<HPhi*>* phi_list(graph()->phi_list());
117  for (int i = 0; i < phi_list->length(); i++) {
118  HPhi* phi = phi_list->at(i);
119  if (phi->representation().IsInteger32()) {
120  phi->SetFlag(HValue::kTruncatingToInt32);
121  } else if (phi->representation().IsSmi()) {
122  phi->SetFlag(HValue::kTruncatingToSmi);
123  phi->SetFlag(HValue::kTruncatingToInt32);
124  }
125  }
126 
127  for (int i = 0; i < phi_list->length(); i++) {
128  HPhi* phi = phi_list->at(i);
129  HValue* value = NULL;
130  if (phi->representation().IsSmiOrInteger32() &&
131  !phi->CheckUsesForFlag(HValue::kTruncatingToInt32, &value)) {
132  int_worklist.Add(phi, zone());
133  phi->ClearFlag(HValue::kTruncatingToInt32);
134  if (FLAG_trace_representation) {
135  PrintF("#%d Phi is not truncating Int32 because of #%d %s\n",
136  phi->id(), value->id(), value->Mnemonic());
137  }
138  }
139 
140  if (phi->representation().IsSmi() &&
141  !phi->CheckUsesForFlag(HValue::kTruncatingToSmi, &value)) {
142  smi_worklist.Add(phi, zone());
143  phi->ClearFlag(HValue::kTruncatingToSmi);
144  if (FLAG_trace_representation) {
145  PrintF("#%d Phi is not truncating Smi because of #%d %s\n",
146  phi->id(), value->id(), value->Mnemonic());
147  }
148  }
149  }
150 
151  while (!int_worklist.is_empty()) {
152  HPhi* current = int_worklist.RemoveLast();
153  for (int i = 0; i < current->OperandCount(); ++i) {
154  HValue* input = current->OperandAt(i);
155  if (input->IsPhi() &&
156  input->representation().IsSmiOrInteger32() &&
158  if (FLAG_trace_representation) {
159  PrintF("#%d Phi is not truncating Int32 because of #%d %s\n",
160  input->id(), current->id(), current->Mnemonic());
161  }
163  int_worklist.Add(HPhi::cast(input), zone());
164  }
165  }
166  }
167 
168  while (!smi_worklist.is_empty()) {
169  HPhi* current = smi_worklist.RemoveLast();
170  for (int i = 0; i < current->OperandCount(); ++i) {
171  HValue* input = current->OperandAt(i);
172  if (input->IsPhi() &&
173  input->representation().IsSmi() &&
175  if (FLAG_trace_representation) {
176  PrintF("#%d Phi is not truncating Smi because of #%d %s\n",
177  input->id(), current->id(), current->Mnemonic());
178  }
180  smi_worklist.Add(HPhi::cast(input), zone());
181  }
182  }
183  }
184 
185  const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
186  for (int i = 0; i < blocks->length(); ++i) {
187  // Process phi instructions first.
188  const HBasicBlock* block(blocks->at(i));
189  const ZoneList<HPhi*>* phis = block->phis();
190  for (int j = 0; j < phis->length(); j++) {
192  }
193 
194  // Process normal instructions.
195  for (HInstruction* current = block->first(); current != NULL; ) {
196  HInstruction* next = current->next();
198  current = next;
199  }
200  }
201 }
202 
203 } } // namespace v8::internal
void set_position(HSourcePosition position)
void InsertBefore(HInstruction *next)
HGraph * graph() const
Definition: hydrogen.h:2802
void InsertRepresentationChangeForUse(HValue *value, HValue *use_value, int use_index, Representation to)
static HValue * cast(HValue *value)
virtual HSourcePosition operand_position(int index) const
HBasicBlock * block() const
const char * Mnemonic() const
virtual Representation RequiredInputRepresentation(int index)=0
Representation representation() const
void SetOperandAt(int index, HValue *value)
bool CheckFlag(Flag f) const
HUseIterator uses() const
virtual HValue * OperandAt(int index) const =0
void DeleteAndReplaceWith(HValue *other)
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:17
T & at(int i) const
Definition: list.h:69
bool Equals(const Representation &other) const
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes to(mksnapshot only)") DEFINE_STRING(raw_context_file
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK(condition)
Definition: logging.h:205
static bool SmiValuesAre32Bits()
Definition: v8.h:5808
void PrintF(const char *format,...)
Definition: utils.cc:80
static bool IsNonDeoptingIntToSmiChange(HChange *change)
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
A simple Maybe type, representing an object which may or may not have a value.
Definition: v8.h:890
T value
Definition: v8.h:896
bool has_value
Definition: v8.h:895