• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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/compiler/load-elimination.h"
6 
7 #include "src/compiler/access-builder.h"
8 #include "src/compiler/common-operator.h"
9 #include "src/compiler/js-graph.h"
10 #include "src/compiler/node-properties.h"
11 #include "src/heap/factory.h"
12 #include "src/objects/objects-inl.h"
13 
14 namespace v8 {
15 namespace internal {
16 namespace compiler {
17 
18 namespace {
19 
IsRename(Node * node)20 bool IsRename(Node* node) {
21   switch (node->opcode()) {
22     case IrOpcode::kCheckHeapObject:
23     case IrOpcode::kFinishRegion:
24     case IrOpcode::kTypeGuard:
25       return !node->IsDead();
26     default:
27       return false;
28   }
29 }
30 
ResolveRenames(Node * node)31 Node* ResolveRenames(Node* node) {
32   while (IsRename(node)) {
33     node = node->InputAt(0);
34   }
35   return node;
36 }
37 
MayAlias(Node * a,Node * b)38 bool MayAlias(Node* a, Node* b) {
39   if (a != b) {
40     if (!NodeProperties::GetType(a).Maybe(NodeProperties::GetType(b))) {
41       return false;
42     } else if (IsRename(b)) {
43       return MayAlias(a, b->InputAt(0));
44     } else if (IsRename(a)) {
45       return MayAlias(a->InputAt(0), b);
46     } else if (b->opcode() == IrOpcode::kAllocate) {
47       switch (a->opcode()) {
48         case IrOpcode::kAllocate:
49         case IrOpcode::kHeapConstant:
50         case IrOpcode::kParameter:
51           return false;
52         default:
53           break;
54       }
55     } else if (a->opcode() == IrOpcode::kAllocate) {
56       switch (b->opcode()) {
57         case IrOpcode::kHeapConstant:
58         case IrOpcode::kParameter:
59           return false;
60         default:
61           break;
62       }
63     }
64   }
65   return true;
66 }
67 
MustAlias(Node * a,Node * b)68 bool MustAlias(Node* a, Node* b) {
69   return ResolveRenames(a) == ResolveRenames(b);
70 }
71 
72 }  // namespace
73 
Reduce(Node * node)74 Reduction LoadElimination::Reduce(Node* node) {
75   if (FLAG_trace_turbo_load_elimination) {
76     if (node->op()->EffectInputCount() > 0) {
77       PrintF(" visit #%d:%s", node->id(), node->op()->mnemonic());
78       if (node->op()->ValueInputCount() > 0) {
79         PrintF("(");
80         for (int i = 0; i < node->op()->ValueInputCount(); ++i) {
81           if (i > 0) PrintF(", ");
82           Node* const value = NodeProperties::GetValueInput(node, i);
83           PrintF("#%d:%s", value->id(), value->op()->mnemonic());
84         }
85         PrintF(")");
86       }
87       PrintF("\n");
88       for (int i = 0; i < node->op()->EffectInputCount(); ++i) {
89         Node* const effect = NodeProperties::GetEffectInput(node, i);
90         if (AbstractState const* const state = node_states_.Get(effect)) {
91           PrintF("  state[%i]: #%d:%s\n", i, effect->id(),
92                  effect->op()->mnemonic());
93           state->Print();
94         } else {
95           PrintF("  no state[%i]: #%d:%s\n", i, effect->id(),
96                  effect->op()->mnemonic());
97         }
98       }
99     }
100   }
101   switch (node->opcode()) {
102     case IrOpcode::kMapGuard:
103       return ReduceMapGuard(node);
104     case IrOpcode::kCheckMaps:
105       return ReduceCheckMaps(node);
106     case IrOpcode::kCompareMaps:
107       return ReduceCompareMaps(node);
108     case IrOpcode::kEnsureWritableFastElements:
109       return ReduceEnsureWritableFastElements(node);
110     case IrOpcode::kMaybeGrowFastElements:
111       return ReduceMaybeGrowFastElements(node);
112     case IrOpcode::kTransitionElementsKind:
113       return ReduceTransitionElementsKind(node);
114     case IrOpcode::kLoadField:
115       return ReduceLoadField(node, FieldAccessOf(node->op()));
116     case IrOpcode::kStoreField:
117       return ReduceStoreField(node, FieldAccessOf(node->op()));
118     case IrOpcode::kLoadElement:
119       return ReduceLoadElement(node);
120     case IrOpcode::kStoreElement:
121       return ReduceStoreElement(node);
122     case IrOpcode::kTransitionAndStoreElement:
123       return ReduceTransitionAndStoreElement(node);
124     case IrOpcode::kStoreTypedElement:
125       return ReduceStoreTypedElement(node);
126     case IrOpcode::kEffectPhi:
127       return ReduceEffectPhi(node);
128     case IrOpcode::kDead:
129       break;
130     case IrOpcode::kStart:
131       return ReduceStart(node);
132     default:
133       return ReduceOtherNode(node);
134   }
135   return NoChange();
136 }
137 
138 namespace {
139 
IsCompatible(MachineRepresentation r1,MachineRepresentation r2)140 bool IsCompatible(MachineRepresentation r1, MachineRepresentation r2) {
141   if (r1 == r2) return true;
142   return IsAnyTagged(r1) && IsAnyTagged(r2);
143 }
144 
145 }  // namespace
146 
147 LoadElimination::AbstractState const
148     LoadElimination::AbstractState::empty_state_;
149 
Lookup(Node * object,Node * index,MachineRepresentation representation) const150 Node* LoadElimination::AbstractElements::Lookup(
151     Node* object, Node* index, MachineRepresentation representation) const {
152   for (Element const element : elements_) {
153     if (element.object == nullptr) continue;
154     DCHECK_NOT_NULL(element.index);
155     DCHECK_NOT_NULL(element.value);
156     if (MustAlias(object, element.object) && MustAlias(index, element.index) &&
157         IsCompatible(representation, element.representation)) {
158       return element.value;
159     }
160   }
161   return nullptr;
162 }
163 
164 LoadElimination::AbstractElements const*
Kill(Node * object,Node * index,Zone * zone) const165 LoadElimination::AbstractElements::Kill(Node* object, Node* index,
166                                         Zone* zone) const {
167   for (Element const element : this->elements_) {
168     if (element.object == nullptr) continue;
169     if (MayAlias(object, element.object)) {
170       AbstractElements* that = zone->New<AbstractElements>(zone);
171       for (Element const element : this->elements_) {
172         if (element.object == nullptr) continue;
173         DCHECK_NOT_NULL(element.index);
174         DCHECK_NOT_NULL(element.value);
175         if (!MayAlias(object, element.object) ||
176             !NodeProperties::GetType(index).Maybe(
177                 NodeProperties::GetType(element.index))) {
178           that->elements_[that->next_index_++] = element;
179         }
180       }
181       that->next_index_ %= arraysize(elements_);
182       return that;
183     }
184   }
185   return this;
186 }
187 
Equals(AbstractElements const * that) const188 bool LoadElimination::AbstractElements::Equals(
189     AbstractElements const* that) const {
190   if (this == that) return true;
191   for (size_t i = 0; i < arraysize(elements_); ++i) {
192     Element this_element = this->elements_[i];
193     if (this_element.object == nullptr) continue;
194     for (size_t j = 0;; ++j) {
195       if (j == arraysize(elements_)) return false;
196       Element that_element = that->elements_[j];
197       if (this_element.object == that_element.object &&
198           this_element.index == that_element.index &&
199           this_element.value == that_element.value) {
200         break;
201       }
202     }
203   }
204   for (size_t i = 0; i < arraysize(elements_); ++i) {
205     Element that_element = that->elements_[i];
206     if (that_element.object == nullptr) continue;
207     for (size_t j = 0;; ++j) {
208       if (j == arraysize(elements_)) return false;
209       Element this_element = this->elements_[j];
210       if (that_element.object == this_element.object &&
211           that_element.index == this_element.index &&
212           that_element.value == this_element.value) {
213         break;
214       }
215     }
216   }
217   return true;
218 }
219 
220 LoadElimination::AbstractElements const*
Merge(AbstractElements const * that,Zone * zone) const221 LoadElimination::AbstractElements::Merge(AbstractElements const* that,
222                                          Zone* zone) const {
223   if (this->Equals(that)) return this;
224   AbstractElements* copy = zone->New<AbstractElements>(zone);
225   for (Element const this_element : this->elements_) {
226     if (this_element.object == nullptr) continue;
227     for (Element const that_element : that->elements_) {
228       if (this_element.object == that_element.object &&
229           this_element.index == that_element.index &&
230           this_element.value == that_element.value) {
231         copy->elements_[copy->next_index_++] = this_element;
232         break;
233       }
234     }
235   }
236   copy->next_index_ %= arraysize(elements_);
237   return copy;
238 }
239 
Print() const240 void LoadElimination::AbstractElements::Print() const {
241   for (Element const& element : elements_) {
242     if (element.object) {
243       PrintF("    #%d:%s @ #%d:%s -> #%d:%s\n", element.object->id(),
244              element.object->op()->mnemonic(), element.index->id(),
245              element.index->op()->mnemonic(), element.value->id(),
246              element.value->op()->mnemonic());
247     }
248   }
249 }
250 
Lookup(Node * object) const251 LoadElimination::FieldInfo const* LoadElimination::AbstractField::Lookup(
252     Node* object) const {
253   for (auto& pair : info_for_node_) {
254     if (pair.first->IsDead()) continue;
255     if (MustAlias(object, pair.first)) return &pair.second;
256   }
257   return nullptr;
258 }
259 
260 namespace {
261 
MayAlias(MaybeHandle<Name> x,MaybeHandle<Name> y)262 bool MayAlias(MaybeHandle<Name> x, MaybeHandle<Name> y) {
263   if (!x.address()) return true;
264   if (!y.address()) return true;
265   if (x.address() != y.address()) return false;
266   return true;
267 }
268 
269 }  // namespace
270 
271 class LoadElimination::AliasStateInfo {
272  public:
AliasStateInfo(const AbstractState * state,Node * object,Handle<Map> map)273   AliasStateInfo(const AbstractState* state, Node* object, Handle<Map> map)
274       : state_(state), object_(object), map_(map) {}
AliasStateInfo(const AbstractState * state,Node * object)275   AliasStateInfo(const AbstractState* state, Node* object)
276       : state_(state), object_(object) {}
277 
278   bool MayAlias(Node* other) const;
279 
280  private:
281   const AbstractState* state_;
282   Node* object_;
283   MaybeHandle<Map> map_;
284 };
285 
KillConst(Node * object,Zone * zone) const286 LoadElimination::AbstractField const* LoadElimination::AbstractField::KillConst(
287     Node* object, Zone* zone) const {
288   for (auto pair : this->info_for_node_) {
289     if (pair.first->IsDead()) continue;
290     // If we previously recorded information about a const store on the given
291     // 'object', we might not have done it on the same node; e.g. we might now
292     // identify the object by a FinishRegion node, whereas the initial const
293     // store was performed on the Allocate node. We therefore remove information
294     // on all nodes that must alias with 'object'.
295     if (MustAlias(object, pair.first)) {
296       AbstractField* that = zone->New<AbstractField>(zone);
297       for (auto pair : this->info_for_node_) {
298         if (!MustAlias(object, pair.first)) {
299           that->info_for_node_.insert(pair);
300         }
301       }
302       return that;
303     }
304   }
305   return this;
306 }
307 
Kill(const AliasStateInfo & alias_info,MaybeHandle<Name> name,Zone * zone) const308 LoadElimination::AbstractField const* LoadElimination::AbstractField::Kill(
309     const AliasStateInfo& alias_info, MaybeHandle<Name> name,
310     Zone* zone) const {
311   for (auto pair : this->info_for_node_) {
312     if (pair.first->IsDead()) continue;
313     if (alias_info.MayAlias(pair.first)) {
314       AbstractField* that = zone->New<AbstractField>(zone);
315       for (auto pair : this->info_for_node_) {
316         if (!alias_info.MayAlias(pair.first) ||
317             !MayAlias(name, pair.second.name)) {
318           that->info_for_node_.insert(pair);
319         }
320       }
321       return that;
322     }
323   }
324   return this;
325 }
326 
Print() const327 void LoadElimination::AbstractField::Print() const {
328   for (auto pair : info_for_node_) {
329     PrintF("    #%d:%s -> #%d:%s [repr=%s]\n", pair.first->id(),
330            pair.first->op()->mnemonic(), pair.second.value->id(),
331            pair.second.value->op()->mnemonic(),
332            MachineReprToString(pair.second.representation));
333   }
334 }
335 
AbstractMaps(Zone * zone)336 LoadElimination::AbstractMaps::AbstractMaps(Zone* zone)
337     : info_for_node_(zone) {}
338 
AbstractMaps(Node * object,ZoneHandleSet<Map> maps,Zone * zone)339 LoadElimination::AbstractMaps::AbstractMaps(Node* object,
340                                             ZoneHandleSet<Map> maps, Zone* zone)
341     : info_for_node_(zone) {
342   object = ResolveRenames(object);
343   info_for_node_.insert(std::make_pair(object, maps));
344 }
345 
Lookup(Node * object,ZoneHandleSet<Map> * object_maps) const346 bool LoadElimination::AbstractMaps::Lookup(
347     Node* object, ZoneHandleSet<Map>* object_maps) const {
348   auto it = info_for_node_.find(ResolveRenames(object));
349   if (it == info_for_node_.end()) return false;
350   *object_maps = it->second;
351   return true;
352 }
353 
Kill(const AliasStateInfo & alias_info,Zone * zone) const354 LoadElimination::AbstractMaps const* LoadElimination::AbstractMaps::Kill(
355     const AliasStateInfo& alias_info, Zone* zone) const {
356   for (auto pair : this->info_for_node_) {
357     if (alias_info.MayAlias(pair.first)) {
358       AbstractMaps* that = zone->New<AbstractMaps>(zone);
359       for (auto pair : this->info_for_node_) {
360         if (!alias_info.MayAlias(pair.first)) that->info_for_node_.insert(pair);
361       }
362       return that;
363     }
364   }
365   return this;
366 }
367 
Merge(AbstractMaps const * that,Zone * zone) const368 LoadElimination::AbstractMaps const* LoadElimination::AbstractMaps::Merge(
369     AbstractMaps const* that, Zone* zone) const {
370   if (this->Equals(that)) return this;
371   AbstractMaps* copy = zone->New<AbstractMaps>(zone);
372   for (auto this_it : this->info_for_node_) {
373     Node* this_object = this_it.first;
374     ZoneHandleSet<Map> this_maps = this_it.second;
375     auto that_it = that->info_for_node_.find(this_object);
376     if (that_it != that->info_for_node_.end() && that_it->second == this_maps) {
377       copy->info_for_node_.insert(this_it);
378     }
379   }
380   return copy;
381 }
382 
Extend(Node * object,ZoneHandleSet<Map> maps,Zone * zone) const383 LoadElimination::AbstractMaps const* LoadElimination::AbstractMaps::Extend(
384     Node* object, ZoneHandleSet<Map> maps, Zone* zone) const {
385   AbstractMaps* that = zone->New<AbstractMaps>(zone);
386   that->info_for_node_ = this->info_for_node_;
387   object = ResolveRenames(object);
388   that->info_for_node_[object] = maps;
389   return that;
390 }
391 
Print() const392 void LoadElimination::AbstractMaps::Print() const {
393   AllowHandleDereference allow_handle_dereference;
394   StdoutStream os;
395   for (auto pair : info_for_node_) {
396     os << "    #" << pair.first->id() << ":" << pair.first->op()->mnemonic()
397        << std::endl;
398     ZoneHandleSet<Map> const& maps = pair.second;
399     for (size_t i = 0; i < maps.size(); ++i) {
400       os << "     - " << Brief(*maps[i]) << std::endl;
401     }
402   }
403 }
404 
FieldsEquals(AbstractFields const & this_fields,AbstractFields const & that_fields) const405 bool LoadElimination::AbstractState::FieldsEquals(
406     AbstractFields const& this_fields,
407     AbstractFields const& that_fields) const {
408   for (size_t i = 0u; i < this_fields.size(); ++i) {
409     AbstractField const* this_field = this_fields[i];
410     AbstractField const* that_field = that_fields[i];
411     if (this_field) {
412       if (!that_field || !that_field->Equals(this_field)) return false;
413     } else if (that_field) {
414       return false;
415     }
416   }
417   return true;
418 }
419 
Equals(AbstractState const * that) const420 bool LoadElimination::AbstractState::Equals(AbstractState const* that) const {
421   if (this->elements_) {
422     if (!that->elements_ || !that->elements_->Equals(this->elements_)) {
423       return false;
424     }
425   } else if (that->elements_) {
426     return false;
427   }
428   if (!FieldsEquals(this->fields_, that->fields_) ||
429       !FieldsEquals(this->const_fields_, that->const_fields_)) {
430     return false;
431   }
432   if (this->maps_) {
433     if (!that->maps_ || !that->maps_->Equals(this->maps_)) {
434       return false;
435     }
436   } else if (that->maps_) {
437     return false;
438   }
439   return true;
440 }
441 
FieldsMerge(AbstractFields * this_fields,AbstractFields const & that_fields,Zone * zone)442 void LoadElimination::AbstractState::FieldsMerge(
443     AbstractFields* this_fields, AbstractFields const& that_fields,
444     Zone* zone) {
445   for (size_t i = 0; i < this_fields->size(); ++i) {
446     AbstractField const*& this_field = (*this_fields)[i];
447     if (this_field) {
448       if (that_fields[i]) {
449         this_field = this_field->Merge(that_fields[i], zone);
450       } else {
451         this_field = nullptr;
452       }
453     }
454   }
455 }
456 
Merge(AbstractState const * that,Zone * zone)457 void LoadElimination::AbstractState::Merge(AbstractState const* that,
458                                            Zone* zone) {
459   // Merge the information we have about the elements.
460   if (this->elements_) {
461     this->elements_ = that->elements_
462                           ? that->elements_->Merge(this->elements_, zone)
463                           : nullptr;
464   }
465 
466   // Merge the information we have about the fields.
467   FieldsMerge(&this->fields_, that->fields_, zone);
468   FieldsMerge(&this->const_fields_, that->const_fields_, zone);
469 
470   // Merge the information we have about the maps.
471   if (this->maps_) {
472     this->maps_ = that->maps_ ? that->maps_->Merge(this->maps_, zone) : nullptr;
473   }
474 }
475 
LookupMaps(Node * object,ZoneHandleSet<Map> * object_map) const476 bool LoadElimination::AbstractState::LookupMaps(
477     Node* object, ZoneHandleSet<Map>* object_map) const {
478   return this->maps_ && this->maps_->Lookup(object, object_map);
479 }
480 
SetMaps(Node * object,ZoneHandleSet<Map> maps,Zone * zone) const481 LoadElimination::AbstractState const* LoadElimination::AbstractState::SetMaps(
482     Node* object, ZoneHandleSet<Map> maps, Zone* zone) const {
483   AbstractState* that = zone->New<AbstractState>(*this);
484   if (that->maps_) {
485     that->maps_ = that->maps_->Extend(object, maps, zone);
486   } else {
487     that->maps_ = zone->New<AbstractMaps>(object, maps, zone);
488   }
489   return that;
490 }
491 
KillMaps(const AliasStateInfo & alias_info,Zone * zone) const492 LoadElimination::AbstractState const* LoadElimination::AbstractState::KillMaps(
493     const AliasStateInfo& alias_info, Zone* zone) const {
494   if (this->maps_) {
495     AbstractMaps const* that_maps = this->maps_->Kill(alias_info, zone);
496     if (this->maps_ != that_maps) {
497       AbstractState* that = zone->New<AbstractState>(*this);
498       that->maps_ = that_maps;
499       return that;
500     }
501   }
502   return this;
503 }
504 
KillMaps(Node * object,Zone * zone) const505 LoadElimination::AbstractState const* LoadElimination::AbstractState::KillMaps(
506     Node* object, Zone* zone) const {
507   AliasStateInfo alias_info(this, object);
508   return KillMaps(alias_info, zone);
509 }
510 
LookupElement(Node * object,Node * index,MachineRepresentation representation) const511 Node* LoadElimination::AbstractState::LookupElement(
512     Node* object, Node* index, MachineRepresentation representation) const {
513   if (this->elements_) {
514     return this->elements_->Lookup(object, index, representation);
515   }
516   return nullptr;
517 }
518 
519 LoadElimination::AbstractState const*
AddElement(Node * object,Node * index,Node * value,MachineRepresentation representation,Zone * zone) const520 LoadElimination::AbstractState::AddElement(Node* object, Node* index,
521                                            Node* value,
522                                            MachineRepresentation representation,
523                                            Zone* zone) const {
524   AbstractState* that = zone->New<AbstractState>(*this);
525   if (that->elements_) {
526     that->elements_ =
527         that->elements_->Extend(object, index, value, representation, zone);
528   } else {
529     that->elements_ =
530         zone->New<AbstractElements>(object, index, value, representation, zone);
531   }
532   return that;
533 }
534 
535 LoadElimination::AbstractState const*
KillElement(Node * object,Node * index,Zone * zone) const536 LoadElimination::AbstractState::KillElement(Node* object, Node* index,
537                                             Zone* zone) const {
538   if (this->elements_) {
539     AbstractElements const* that_elements =
540         this->elements_->Kill(object, index, zone);
541     if (this->elements_ != that_elements) {
542       AbstractState* that = zone->New<AbstractState>(*this);
543       that->elements_ = that_elements;
544       return that;
545     }
546   }
547   return this;
548 }
549 
AddField(Node * object,IndexRange index_range,LoadElimination::FieldInfo info,Zone * zone) const550 LoadElimination::AbstractState const* LoadElimination::AbstractState::AddField(
551     Node* object, IndexRange index_range, LoadElimination::FieldInfo info,
552     Zone* zone) const {
553   AbstractState* that = zone->New<AbstractState>(*this);
554   AbstractFields& fields =
555       info.const_field_info.IsConst() ? that->const_fields_ : that->fields_;
556   for (int index : index_range) {
557     if (fields[index]) {
558       fields[index] = fields[index]->Extend(object, info, zone);
559     } else {
560       fields[index] = zone->New<AbstractField>(object, info, zone);
561     }
562   }
563   return that;
564 }
565 
566 LoadElimination::AbstractState const*
KillConstField(Node * object,IndexRange index_range,Zone * zone) const567 LoadElimination::AbstractState::KillConstField(Node* object,
568                                                IndexRange index_range,
569                                                Zone* zone) const {
570   AliasStateInfo alias_info(this, object);
571   AbstractState* that = nullptr;
572   for (int index : index_range) {
573     if (AbstractField const* this_field = this->const_fields_[index]) {
574       this_field = this_field->KillConst(object, zone);
575       if (this->const_fields_[index] != this_field) {
576         if (!that) that = zone->New<AbstractState>(*this);
577         that->const_fields_[index] = this_field;
578       }
579     }
580   }
581   return that ? that : this;
582 }
583 
KillField(Node * object,IndexRange index_range,MaybeHandle<Name> name,Zone * zone) const584 LoadElimination::AbstractState const* LoadElimination::AbstractState::KillField(
585     Node* object, IndexRange index_range, MaybeHandle<Name> name,
586     Zone* zone) const {
587   AliasStateInfo alias_info(this, object);
588   return KillField(alias_info, index_range, name, zone);
589 }
590 
KillField(const AliasStateInfo & alias_info,IndexRange index_range,MaybeHandle<Name> name,Zone * zone) const591 LoadElimination::AbstractState const* LoadElimination::AbstractState::KillField(
592     const AliasStateInfo& alias_info, IndexRange index_range,
593     MaybeHandle<Name> name, Zone* zone) const {
594   AbstractState* that = nullptr;
595   for (int index : index_range) {
596     if (AbstractField const* this_field = this->fields_[index]) {
597       this_field = this_field->Kill(alias_info, name, zone);
598       if (this->fields_[index] != this_field) {
599         if (!that) that = zone->New<AbstractState>(*this);
600         that->fields_[index] = this_field;
601       }
602     }
603   }
604   return that ? that : this;
605 }
606 
607 LoadElimination::AbstractState const*
KillFields(Node * object,MaybeHandle<Name> name,Zone * zone) const608 LoadElimination::AbstractState::KillFields(Node* object, MaybeHandle<Name> name,
609                                            Zone* zone) const {
610   AliasStateInfo alias_info(this, object);
611   for (size_t i = 0;; ++i) {
612     if (i == fields_.size()) return this;
613     if (AbstractField const* this_field = this->fields_[i]) {
614       AbstractField const* that_field =
615           this_field->Kill(alias_info, name, zone);
616       if (that_field != this_field) {
617         AbstractState* that = zone->New<AbstractState>(*this);
618         that->fields_[i] = that_field;
619         while (++i < fields_.size()) {
620           if (this->fields_[i] != nullptr) {
621             that->fields_[i] = this->fields_[i]->Kill(alias_info, name, zone);
622           }
623         }
624         return that;
625       }
626     }
627   }
628 }
629 
KillAll(Zone * zone) const630 LoadElimination::AbstractState const* LoadElimination::AbstractState::KillAll(
631     Zone* zone) const {
632   // Kill everything except for const fields
633   for (size_t i = 0; i < const_fields_.size(); ++i) {
634     if (const_fields_[i]) {
635       AbstractState* that = zone->New<AbstractState>();
636       that->const_fields_ = const_fields_;
637       return that;
638     }
639   }
640   return LoadElimination::empty_state();
641 }
642 
LookupField(Node * object,IndexRange index_range,ConstFieldInfo const_field_info) const643 LoadElimination::FieldInfo const* LoadElimination::AbstractState::LookupField(
644     Node* object, IndexRange index_range,
645     ConstFieldInfo const_field_info) const {
646   // Check if all the indices in {index_range} contain identical information.
647   // If not, a partially overlapping access has invalidated part of the value.
648   base::Optional<LoadElimination::FieldInfo const*> result;
649   for (int index : index_range) {
650     LoadElimination::FieldInfo const* info = nullptr;
651     if (const_field_info.IsConst()) {
652       if (AbstractField const* this_field = const_fields_[index]) {
653         info = this_field->Lookup(object);
654       }
655       if (!(info && info->const_field_info == const_field_info)) return nullptr;
656     } else {
657       if (AbstractField const* this_field = fields_[index]) {
658         info = this_field->Lookup(object);
659       }
660       if (!info) return nullptr;
661     }
662     if (!result.has_value()) {
663       result = info;
664     } else if (**result != *info) {
665       // We detected inconsistent information for a field here.
666       // This can happen when incomplete alias information makes an unrelated
667       // write invalidate part of a field and then we re-combine this partial
668       // information.
669       // This is probably OK, but since it's rare, we better bail out here.
670       return nullptr;
671     }
672   }
673   return *result;
674 }
675 
MayAlias(Node * other) const676 bool LoadElimination::AliasStateInfo::MayAlias(Node* other) const {
677   // If {object} is being initialized right here (indicated by {object} being
678   // an Allocate node instead of a FinishRegion node), we know that {other}
679   // can only alias with {object} if they refer to exactly the same node.
680   if (object_->opcode() == IrOpcode::kAllocate) {
681     return object_ == other;
682   }
683   // Decide aliasing based on the node kinds.
684   if (!compiler::MayAlias(object_, other)) {
685     return false;
686   }
687   // Decide aliasing based on maps (if available).
688   Handle<Map> map;
689   if (map_.ToHandle(&map)) {
690     ZoneHandleSet<Map> other_maps;
691     if (state_->LookupMaps(other, &other_maps) && other_maps.size() == 1) {
692       if (map.address() != other_maps.at(0).address()) {
693         return false;
694       }
695     }
696   }
697   return true;
698 }
699 
Print() const700 void LoadElimination::AbstractState::Print() const {
701   if (maps_) {
702     PrintF("   maps:\n");
703     maps_->Print();
704   }
705   if (elements_) {
706     PrintF("   elements:\n");
707     elements_->Print();
708   }
709   for (size_t i = 0; i < fields_.size(); ++i) {
710     if (AbstractField const* const field = fields_[i]) {
711       PrintF("   field %zu:\n", i);
712       field->Print();
713     }
714   }
715   for (size_t i = 0; i < const_fields_.size(); ++i) {
716     if (AbstractField const* const const_field = const_fields_[i]) {
717       PrintF("   const field %zu:\n", i);
718       const_field->Print();
719     }
720   }
721 }
722 
723 LoadElimination::AbstractState const*
Get(Node * node) const724 LoadElimination::AbstractStateForEffectNodes::Get(Node* node) const {
725   size_t const id = node->id();
726   if (id < info_for_node_.size()) return info_for_node_[id];
727   return nullptr;
728 }
729 
Set(Node * node,AbstractState const * state)730 void LoadElimination::AbstractStateForEffectNodes::Set(
731     Node* node, AbstractState const* state) {
732   size_t const id = node->id();
733   if (id >= info_for_node_.size()) info_for_node_.resize(id + 1, nullptr);
734   info_for_node_[id] = state;
735 }
736 
ReduceMapGuard(Node * node)737 Reduction LoadElimination::ReduceMapGuard(Node* node) {
738   ZoneHandleSet<Map> const& maps = MapGuardMapsOf(node->op());
739   Node* const object = NodeProperties::GetValueInput(node, 0);
740   Node* const effect = NodeProperties::GetEffectInput(node);
741   AbstractState const* state = node_states_.Get(effect);
742   if (state == nullptr) return NoChange();
743   ZoneHandleSet<Map> object_maps;
744   if (state->LookupMaps(object, &object_maps)) {
745     if (maps.contains(object_maps)) return Replace(effect);
746     // TODO(turbofan): Compute the intersection.
747   }
748   state = state->SetMaps(object, maps, zone());
749   return UpdateState(node, state);
750 }
751 
ReduceCheckMaps(Node * node)752 Reduction LoadElimination::ReduceCheckMaps(Node* node) {
753   ZoneHandleSet<Map> const& maps = CheckMapsParametersOf(node->op()).maps();
754   Node* const object = NodeProperties::GetValueInput(node, 0);
755   Node* const effect = NodeProperties::GetEffectInput(node);
756   AbstractState const* state = node_states_.Get(effect);
757   if (state == nullptr) return NoChange();
758   ZoneHandleSet<Map> object_maps;
759   if (state->LookupMaps(object, &object_maps)) {
760     if (maps.contains(object_maps)) return Replace(effect);
761     // TODO(turbofan): Compute the intersection.
762   }
763   state = state->SetMaps(object, maps, zone());
764   return UpdateState(node, state);
765 }
766 
ReduceCompareMaps(Node * node)767 Reduction LoadElimination::ReduceCompareMaps(Node* node) {
768   ZoneHandleSet<Map> const& maps = CompareMapsParametersOf(node->op());
769   Node* const object = NodeProperties::GetValueInput(node, 0);
770   Node* const effect = NodeProperties::GetEffectInput(node);
771   AbstractState const* state = node_states_.Get(effect);
772   if (state == nullptr) return NoChange();
773   ZoneHandleSet<Map> object_maps;
774   if (state->LookupMaps(object, &object_maps)) {
775     if (maps.contains(object_maps)) {
776       Node* value = jsgraph()->TrueConstant();
777       ReplaceWithValue(node, value, effect);
778       return Replace(value);
779     }
780     // TODO(turbofan): Compute the intersection.
781   }
782   return UpdateState(node, state);
783 }
784 
ReduceEnsureWritableFastElements(Node * node)785 Reduction LoadElimination::ReduceEnsureWritableFastElements(Node* node) {
786   Node* const object = NodeProperties::GetValueInput(node, 0);
787   Node* const elements = NodeProperties::GetValueInput(node, 1);
788   Node* const effect = NodeProperties::GetEffectInput(node);
789   AbstractState const* state = node_states_.Get(effect);
790   if (state == nullptr) return NoChange();
791   // Check if the {elements} already have the fixed array map.
792   ZoneHandleSet<Map> elements_maps;
793   ZoneHandleSet<Map> fixed_array_maps(factory()->fixed_array_map());
794   if (state->LookupMaps(elements, &elements_maps) &&
795       fixed_array_maps.contains(elements_maps)) {
796     ReplaceWithValue(node, elements, effect);
797     return Replace(elements);
798   }
799   // We know that the resulting elements have the fixed array map.
800   state = state->SetMaps(node, fixed_array_maps, zone());
801   // Kill the previous elements on {object}.
802   state = state->KillField(object,
803                            FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
804                            MaybeHandle<Name>(), zone());
805   // Add the new elements on {object}.
806   state = state->AddField(
807       object, FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
808       {node, MachineRepresentation::kTaggedPointer}, zone());
809   return UpdateState(node, state);
810 }
811 
ReduceMaybeGrowFastElements(Node * node)812 Reduction LoadElimination::ReduceMaybeGrowFastElements(Node* node) {
813   GrowFastElementsParameters params = GrowFastElementsParametersOf(node->op());
814   Node* const object = NodeProperties::GetValueInput(node, 0);
815   Node* const effect = NodeProperties::GetEffectInput(node);
816   AbstractState const* state = node_states_.Get(effect);
817   if (state == nullptr) return NoChange();
818   if (params.mode() == GrowFastElementsMode::kDoubleElements) {
819     // We know that the resulting elements have the fixed double array map.
820     state = state->SetMaps(
821         node, ZoneHandleSet<Map>(factory()->fixed_double_array_map()), zone());
822   } else {
823     // We know that the resulting elements have the fixed array map or the COW
824     // version thereof (if we didn't grow and it was already COW before).
825     ZoneHandleSet<Map> fixed_array_maps(factory()->fixed_array_map());
826     fixed_array_maps.insert(factory()->fixed_cow_array_map(), zone());
827     state = state->SetMaps(node, fixed_array_maps, zone());
828   }
829   // Kill the previous elements on {object}.
830   state = state->KillField(object,
831                            FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
832                            MaybeHandle<Name>(), zone());
833   // Add the new elements on {object}.
834   state = state->AddField(
835       object, FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
836       {node, MachineRepresentation::kTaggedPointer}, zone());
837   return UpdateState(node, state);
838 }
839 
ReduceTransitionElementsKind(Node * node)840 Reduction LoadElimination::ReduceTransitionElementsKind(Node* node) {
841   ElementsTransition transition = ElementsTransitionOf(node->op());
842   Node* const object = NodeProperties::GetValueInput(node, 0);
843   Handle<Map> source_map(transition.source());
844   Handle<Map> target_map(transition.target());
845   Node* const effect = NodeProperties::GetEffectInput(node);
846   AbstractState const* state = node_states_.Get(effect);
847   if (state == nullptr) return NoChange();
848   switch (transition.mode()) {
849     case ElementsTransition::kFastTransition:
850       break;
851     case ElementsTransition::kSlowTransition:
852       // Kill the elements as well.
853       AliasStateInfo alias_info(state, object, source_map);
854       state = state->KillField(
855           alias_info, FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
856           MaybeHandle<Name>(), zone());
857       break;
858   }
859   ZoneHandleSet<Map> object_maps;
860   if (state->LookupMaps(object, &object_maps)) {
861     if (ZoneHandleSet<Map>(target_map).contains(object_maps)) {
862       // The {object} already has the {target_map}, so this TransitionElements
863       // {node} is fully redundant (independent of what {source_map} is).
864       return Replace(effect);
865     }
866     if (object_maps.contains(ZoneHandleSet<Map>(source_map))) {
867       object_maps.remove(source_map, zone());
868       object_maps.insert(target_map, zone());
869       AliasStateInfo alias_info(state, object, source_map);
870       state = state->KillMaps(alias_info, zone());
871       state = state->SetMaps(object, object_maps, zone());
872     }
873   } else {
874     AliasStateInfo alias_info(state, object, source_map);
875     state = state->KillMaps(alias_info, zone());
876   }
877   return UpdateState(node, state);
878 }
879 
ReduceTransitionAndStoreElement(Node * node)880 Reduction LoadElimination::ReduceTransitionAndStoreElement(Node* node) {
881   Node* const object = NodeProperties::GetValueInput(node, 0);
882   Handle<Map> double_map(DoubleMapParameterOf(node->op()));
883   Handle<Map> fast_map(FastMapParameterOf(node->op()));
884   Node* const effect = NodeProperties::GetEffectInput(node);
885   AbstractState const* state = node_states_.Get(effect);
886   if (state == nullptr) return NoChange();
887 
888   // We need to add the double and fast maps to the set of possible maps for
889   // this object, because we don't know which of those we'll transition to.
890   // Additionally, we should kill all alias information.
891   ZoneHandleSet<Map> object_maps;
892   if (state->LookupMaps(object, &object_maps)) {
893     object_maps.insert(double_map, zone());
894     object_maps.insert(fast_map, zone());
895     state = state->KillMaps(object, zone());
896     state = state->SetMaps(object, object_maps, zone());
897   }
898   // Kill the elements as well.
899   state = state->KillField(object,
900                            FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
901                            MaybeHandle<Name>(), zone());
902   return UpdateState(node, state);
903 }
904 
ReduceLoadField(Node * node,FieldAccess const & access)905 Reduction LoadElimination::ReduceLoadField(Node* node,
906                                            FieldAccess const& access) {
907   Node* object = NodeProperties::GetValueInput(node, 0);
908   Node* effect = NodeProperties::GetEffectInput(node);
909   Node* control = NodeProperties::GetControlInput(node);
910   AbstractState const* state = node_states_.Get(effect);
911   if (state == nullptr) return NoChange();
912   if (access.offset == HeapObject::kMapOffset &&
913       access.base_is_tagged == kTaggedBase) {
914     DCHECK(IsAnyTagged(access.machine_type.representation()));
915     ZoneHandleSet<Map> object_maps;
916     if (state->LookupMaps(object, &object_maps) && object_maps.size() == 1) {
917       Node* value = jsgraph()->HeapConstant(object_maps[0]);
918       NodeProperties::SetType(value, Type::OtherInternal());
919       ReplaceWithValue(node, value, effect);
920       return Replace(value);
921     }
922   } else {
923     IndexRange field_index = FieldIndexOf(access);
924     if (field_index != IndexRange::Invalid()) {
925       MachineRepresentation representation =
926           access.machine_type.representation();
927       FieldInfo const* lookup_result =
928           state->LookupField(object, field_index, access.const_field_info);
929       if (!lookup_result && access.const_field_info.IsConst()) {
930         // If the access is const and we didn't find anything, also try to look
931         // up information from mutable stores
932         lookup_result =
933             state->LookupField(object, field_index, ConstFieldInfo::None());
934       }
935       if (lookup_result) {
936         // Make sure we don't reuse values that were recorded with a different
937         // representation or resurrect dead {replacement} nodes.
938         Node* replacement = lookup_result->value;
939         if (IsCompatible(representation, lookup_result->representation) &&
940             !replacement->IsDead()) {
941           // Introduce a TypeGuard if the type of the {replacement} node is not
942           // a subtype of the original {node}'s type.
943           if (!NodeProperties::GetType(replacement)
944                    .Is(NodeProperties::GetType(node))) {
945             Type replacement_type = Type::Intersect(
946                 NodeProperties::GetType(node),
947                 NodeProperties::GetType(replacement), graph()->zone());
948             replacement = effect =
949                 graph()->NewNode(common()->TypeGuard(replacement_type),
950                                  replacement, effect, control);
951             NodeProperties::SetType(replacement, replacement_type);
952           }
953           ReplaceWithValue(node, replacement, effect);
954           return Replace(replacement);
955         }
956       }
957       FieldInfo info(node, representation, access.name,
958                      access.const_field_info);
959       state = state->AddField(object, field_index, info, zone());
960     }
961   }
962   Handle<Map> field_map;
963   if (access.map.ToHandle(&field_map)) {
964     state = state->SetMaps(node, ZoneHandleSet<Map>(field_map), zone());
965   }
966   return UpdateState(node, state);
967 }
968 
ReduceStoreField(Node * node,FieldAccess const & access)969 Reduction LoadElimination::ReduceStoreField(Node* node,
970                                             FieldAccess const& access) {
971   Node* const object = NodeProperties::GetValueInput(node, 0);
972   Node* const new_value = NodeProperties::GetValueInput(node, 1);
973   Node* const effect = NodeProperties::GetEffectInput(node);
974   AbstractState const* state = node_states_.Get(effect);
975   if (state == nullptr) return NoChange();
976   if (access.offset == HeapObject::kMapOffset &&
977       access.base_is_tagged == kTaggedBase) {
978     DCHECK(IsAnyTagged(access.machine_type.representation()));
979     // Kill all potential knowledge about the {object}s map.
980     state = state->KillMaps(object, zone());
981     Type const new_value_type = NodeProperties::GetType(new_value);
982     if (new_value_type.IsHeapConstant()) {
983       // Record the new {object} map information.
984       ZoneHandleSet<Map> object_maps(
985           new_value_type.AsHeapConstant()->Ref().AsMap().object());
986       state = state->SetMaps(object, object_maps, zone());
987     }
988   } else {
989     IndexRange field_index = FieldIndexOf(access);
990     if (field_index != IndexRange::Invalid()) {
991       bool is_const_store = access.const_field_info.IsConst();
992       MachineRepresentation representation =
993           access.machine_type.representation();
994       FieldInfo const* lookup_result =
995           state->LookupField(object, field_index, access.const_field_info);
996 
997       if (lookup_result &&
998           (!is_const_store || V8_ENABLE_DOUBLE_CONST_STORE_CHECK_BOOL)) {
999         // At runtime, we should never encounter
1000         // - any store replacing existing info with a different, incompatible
1001         //   representation, nor
1002         // - two consecutive const stores, unless the latter is a store into
1003         //   a literal.
1004         // However, we may see such code statically, so we guard against
1005         // executing it by emitting Unreachable.
1006         // TODO(gsps): Re-enable the double const store check even for
1007         //   non-debug builds once we have identified other FieldAccesses
1008         //   that should be marked mutable instead of const
1009         //   (cf. JSCreateLowering::AllocateFastLiteral).
1010         bool incompatible_representation =
1011             !lookup_result->name.is_null() &&
1012             !IsCompatible(representation, lookup_result->representation);
1013         bool illegal_double_const_store =
1014             is_const_store && !access.is_store_in_literal;
1015         if (incompatible_representation || illegal_double_const_store) {
1016           Node* control = NodeProperties::GetControlInput(node);
1017           Node* unreachable =
1018               graph()->NewNode(common()->Unreachable(), effect, control);
1019           return Replace(unreachable);
1020         }
1021         if (lookup_result->value == new_value) {
1022           // This store is fully redundant.
1023           return Replace(effect);
1024         }
1025       }
1026 
1027       // Kill all potentially aliasing fields and record the new value.
1028       FieldInfo new_info(new_value, representation, access.name,
1029                          access.const_field_info);
1030       if (is_const_store && access.is_store_in_literal) {
1031         // We only kill const information when there is a chance that we
1032         // previously stored information about the given const field (namely,
1033         // when we observe const stores to literals).
1034         state = state->KillConstField(object, field_index, zone());
1035       }
1036       state = state->KillField(object, field_index, access.name, zone());
1037       state = state->AddField(object, field_index, new_info, zone());
1038       if (is_const_store) {
1039         // For const stores, we track information in both the const and the
1040         // mutable world to guard against field accesses that should have
1041         // been marked const, but were not.
1042         new_info.const_field_info = ConstFieldInfo::None();
1043         state = state->AddField(object, field_index, new_info, zone());
1044       }
1045     } else {
1046       // Unsupported StoreField operator.
1047       state = state->KillFields(object, access.name, zone());
1048     }
1049   }
1050   return UpdateState(node, state);
1051 }
1052 
ReduceLoadElement(Node * node)1053 Reduction LoadElimination::ReduceLoadElement(Node* node) {
1054   Node* const object = NodeProperties::GetValueInput(node, 0);
1055   Node* const index = NodeProperties::GetValueInput(node, 1);
1056   Node* const effect = NodeProperties::GetEffectInput(node);
1057   AbstractState const* state = node_states_.Get(effect);
1058   if (state == nullptr) return NoChange();
1059 
1060   // Only handle loads that do not require truncations.
1061   ElementAccess const& access = ElementAccessOf(node->op());
1062   switch (access.machine_type.representation()) {
1063     case MachineRepresentation::kNone:
1064     case MachineRepresentation::kBit:
1065     case MachineRepresentation::kWord8:
1066     case MachineRepresentation::kWord16:
1067     case MachineRepresentation::kWord32:
1068     case MachineRepresentation::kWord64:
1069     case MachineRepresentation::kFloat32:
1070     case MachineRepresentation::kCompressedPointer:
1071     case MachineRepresentation::kCompressed:
1072       // TODO(turbofan): Add support for doing the truncations.
1073       break;
1074     case MachineRepresentation::kFloat64:
1075     case MachineRepresentation::kSimd128:
1076     case MachineRepresentation::kTaggedSigned:
1077     case MachineRepresentation::kTaggedPointer:
1078     case MachineRepresentation::kTagged:
1079       if (Node* replacement = state->LookupElement(
1080               object, index, access.machine_type.representation())) {
1081         // Make sure we don't resurrect dead {replacement} nodes.
1082         // Skip lowering if the type of the {replacement} node is not a subtype
1083         // of the original {node}'s type.
1084         // TODO(tebbi): We should insert a {TypeGuard} for the intersection of
1085         // these two types here once we properly handle {Type::None} everywhere.
1086         if (!replacement->IsDead() && NodeProperties::GetType(replacement)
1087                                           .Is(NodeProperties::GetType(node))) {
1088           ReplaceWithValue(node, replacement, effect);
1089           return Replace(replacement);
1090         }
1091       }
1092       state = state->AddElement(object, index, node,
1093                                 access.machine_type.representation(), zone());
1094       return UpdateState(node, state);
1095   }
1096   return NoChange();
1097 }
1098 
ReduceStoreElement(Node * node)1099 Reduction LoadElimination::ReduceStoreElement(Node* node) {
1100   ElementAccess const& access = ElementAccessOf(node->op());
1101   Node* const object = NodeProperties::GetValueInput(node, 0);
1102   Node* const index = NodeProperties::GetValueInput(node, 1);
1103   Node* const new_value = NodeProperties::GetValueInput(node, 2);
1104   Node* const effect = NodeProperties::GetEffectInput(node);
1105   AbstractState const* state = node_states_.Get(effect);
1106   if (state == nullptr) return NoChange();
1107   Node* const old_value =
1108       state->LookupElement(object, index, access.machine_type.representation());
1109   if (old_value == new_value) {
1110     // This store is fully redundant.
1111     return Replace(effect);
1112   }
1113   // Kill all potentially aliasing elements.
1114   state = state->KillElement(object, index, zone());
1115   // Only record the new value if the store doesn't have an implicit truncation.
1116   switch (access.machine_type.representation()) {
1117     case MachineRepresentation::kNone:
1118     case MachineRepresentation::kBit:
1119     case MachineRepresentation::kWord8:
1120     case MachineRepresentation::kWord16:
1121     case MachineRepresentation::kWord32:
1122     case MachineRepresentation::kWord64:
1123     case MachineRepresentation::kFloat32:
1124     case MachineRepresentation::kCompressedPointer:
1125     case MachineRepresentation::kCompressed:
1126       // TODO(turbofan): Add support for doing the truncations.
1127       break;
1128     case MachineRepresentation::kFloat64:
1129     case MachineRepresentation::kSimd128:
1130     case MachineRepresentation::kTaggedSigned:
1131     case MachineRepresentation::kTaggedPointer:
1132     case MachineRepresentation::kTagged:
1133       state = state->AddElement(object, index, new_value,
1134                                 access.machine_type.representation(), zone());
1135       break;
1136   }
1137   return UpdateState(node, state);
1138 }
1139 
ReduceStoreTypedElement(Node * node)1140 Reduction LoadElimination::ReduceStoreTypedElement(Node* node) {
1141   Node* const effect = NodeProperties::GetEffectInput(node);
1142   AbstractState const* state = node_states_.Get(effect);
1143   if (state == nullptr) return NoChange();
1144   return UpdateState(node, state);
1145 }
1146 
UpdateStateForPhi(AbstractState const * state,Node * effect_phi,Node * phi)1147 LoadElimination::AbstractState const* LoadElimination::UpdateStateForPhi(
1148     AbstractState const* state, Node* effect_phi, Node* phi) {
1149   int predecessor_count = phi->InputCount() - 1;
1150   // TODO(jarin) Consider doing a union here. At the moment, we just keep this
1151   // consistent with AbstractState::Merge.
1152 
1153   // Check if all the inputs have the same maps.
1154   AbstractState const* input_state =
1155       node_states_.Get(NodeProperties::GetEffectInput(effect_phi, 0));
1156   ZoneHandleSet<Map> object_maps;
1157   if (!input_state->LookupMaps(phi->InputAt(0), &object_maps)) return state;
1158   for (int i = 1; i < predecessor_count; i++) {
1159     input_state =
1160         node_states_.Get(NodeProperties::GetEffectInput(effect_phi, i));
1161     ZoneHandleSet<Map> input_maps;
1162     if (!input_state->LookupMaps(phi->InputAt(i), &input_maps)) return state;
1163     if (input_maps != object_maps) return state;
1164   }
1165   return state->SetMaps(phi, object_maps, zone());
1166 }
1167 
ReduceEffectPhi(Node * node)1168 Reduction LoadElimination::ReduceEffectPhi(Node* node) {
1169   Node* const effect0 = NodeProperties::GetEffectInput(node, 0);
1170   Node* const control = NodeProperties::GetControlInput(node);
1171   AbstractState const* state0 = node_states_.Get(effect0);
1172   if (state0 == nullptr) return NoChange();
1173   if (control->opcode() == IrOpcode::kLoop) {
1174     // Here we rely on having only reducible loops:
1175     // The loop entry edge always dominates the header, so we can just take
1176     // the state from the first input, and compute the loop state based on it.
1177     AbstractState const* state = ComputeLoopState(node, state0);
1178     return UpdateState(node, state);
1179   }
1180   DCHECK_EQ(IrOpcode::kMerge, control->opcode());
1181 
1182   // Shortcut for the case when we do not know anything about some input.
1183   int const input_count = node->op()->EffectInputCount();
1184   for (int i = 1; i < input_count; ++i) {
1185     Node* const effect = NodeProperties::GetEffectInput(node, i);
1186     if (node_states_.Get(effect) == nullptr) return NoChange();
1187   }
1188 
1189   // Make a copy of the first input's state and merge with the state
1190   // from other inputs.
1191   AbstractState* state = zone()->New<AbstractState>(*state0);
1192   for (int i = 1; i < input_count; ++i) {
1193     Node* const input = NodeProperties::GetEffectInput(node, i);
1194     state->Merge(node_states_.Get(input), zone());
1195   }
1196 
1197   // For each phi, try to compute the new state for the phi from
1198   // the inputs.
1199   AbstractState const* state_with_phis = state;
1200   for (Node* use : control->uses()) {
1201     if (use->opcode() == IrOpcode::kPhi) {
1202       state_with_phis = UpdateStateForPhi(state_with_phis, node, use);
1203     }
1204   }
1205 
1206   return UpdateState(node, state_with_phis);
1207 }
1208 
ReduceStart(Node * node)1209 Reduction LoadElimination::ReduceStart(Node* node) {
1210   return UpdateState(node, empty_state());
1211 }
1212 
ReduceOtherNode(Node * node)1213 Reduction LoadElimination::ReduceOtherNode(Node* node) {
1214   if (node->op()->EffectInputCount() == 1) {
1215     if (node->op()->EffectOutputCount() == 1) {
1216       Node* const effect = NodeProperties::GetEffectInput(node);
1217       AbstractState const* state = node_states_.Get(effect);
1218       // If we do not know anything about the predecessor, do not propagate
1219       // just yet because we will have to recompute anyway once we compute
1220       // the predecessor.
1221       if (state == nullptr) return NoChange();
1222       // Check if this {node} has some uncontrolled side effects.
1223       if (!node->op()->HasProperty(Operator::kNoWrite)) {
1224         state = state->KillAll(zone());
1225       }
1226       return UpdateState(node, state);
1227     } else {
1228       // Effect terminators should be handled specially.
1229       return NoChange();
1230     }
1231   }
1232   DCHECK_EQ(0, node->op()->EffectInputCount());
1233   DCHECK_EQ(0, node->op()->EffectOutputCount());
1234   return NoChange();
1235 }
1236 
UpdateState(Node * node,AbstractState const * state)1237 Reduction LoadElimination::UpdateState(Node* node, AbstractState const* state) {
1238   AbstractState const* original = node_states_.Get(node);
1239   // Only signal that the {node} has Changed, if the information about {state}
1240   // has changed wrt. the {original}.
1241   if (state != original) {
1242     if (original == nullptr || !state->Equals(original)) {
1243       node_states_.Set(node, state);
1244       return Changed(node);
1245     }
1246   }
1247   return NoChange();
1248 }
1249 
1250 LoadElimination::AbstractState const*
ComputeLoopStateForStoreField(Node * current,LoadElimination::AbstractState const * state,FieldAccess const & access) const1251 LoadElimination::ComputeLoopStateForStoreField(
1252     Node* current, LoadElimination::AbstractState const* state,
1253     FieldAccess const& access) const {
1254   Node* const object = NodeProperties::GetValueInput(current, 0);
1255   if (access.offset == HeapObject::kMapOffset) {
1256     // Invalidate what we know about the {object}s map.
1257     state = state->KillMaps(object, zone());
1258   } else {
1259     IndexRange field_index = FieldIndexOf(access);
1260     if (field_index == IndexRange::Invalid()) {
1261       state = state->KillFields(object, access.name, zone());
1262     } else {
1263       state = state->KillField(object, field_index, access.name, zone());
1264     }
1265   }
1266   return state;
1267 }
1268 
ComputeLoopState(Node * node,AbstractState const * state) const1269 LoadElimination::AbstractState const* LoadElimination::ComputeLoopState(
1270     Node* node, AbstractState const* state) const {
1271   Node* const control = NodeProperties::GetControlInput(node);
1272   struct TransitionElementsKindInfo {
1273     ElementsTransition transition;
1274     Node* object;
1275   };
1276   // Allocate zone data structures in a temporary zone with a lifetime limited
1277   // to this function to avoid blowing up the size of the stage-global zone.
1278   Zone temp_zone(zone()->allocator(), "Temporary scoped zone");
1279   ZoneVector<TransitionElementsKindInfo> element_transitions_(&temp_zone);
1280   ZoneQueue<Node*> queue(&temp_zone);
1281   ZoneSet<Node*> visited(&temp_zone);
1282   visited.insert(node);
1283   for (int i = 1; i < control->InputCount(); ++i) {
1284     queue.push(node->InputAt(i));
1285   }
1286   while (!queue.empty()) {
1287     Node* const current = queue.front();
1288     queue.pop();
1289     if (visited.find(current) == visited.end()) {
1290       visited.insert(current);
1291       if (!current->op()->HasProperty(Operator::kNoWrite)) {
1292         switch (current->opcode()) {
1293           case IrOpcode::kEnsureWritableFastElements: {
1294             Node* const object = NodeProperties::GetValueInput(current, 0);
1295             state = state->KillField(
1296                 object, FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
1297                 MaybeHandle<Name>(), zone());
1298             break;
1299           }
1300           case IrOpcode::kMaybeGrowFastElements: {
1301             Node* const object = NodeProperties::GetValueInput(current, 0);
1302             state = state->KillField(
1303                 object, FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
1304                 MaybeHandle<Name>(), zone());
1305             break;
1306           }
1307           case IrOpcode::kTransitionElementsKind: {
1308             ElementsTransition transition = ElementsTransitionOf(current->op());
1309             Node* const object = NodeProperties::GetValueInput(current, 0);
1310             ZoneHandleSet<Map> object_maps;
1311             if (!state->LookupMaps(object, &object_maps) ||
1312                 !ZoneHandleSet<Map>(transition.target())
1313                      .contains(object_maps)) {
1314               element_transitions_.push_back({transition, object});
1315             }
1316             break;
1317           }
1318           case IrOpcode::kTransitionAndStoreElement: {
1319             Node* const object = NodeProperties::GetValueInput(current, 0);
1320             // Invalidate what we know about the {object}s map.
1321             state = state->KillMaps(object, zone());
1322             // Kill the elements as well.
1323             state = state->KillField(
1324                 object, FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
1325                 MaybeHandle<Name>(), zone());
1326             break;
1327           }
1328           case IrOpcode::kStoreField: {
1329             FieldAccess access = FieldAccessOf(current->op());
1330             state = ComputeLoopStateForStoreField(current, state, access);
1331             break;
1332           }
1333           case IrOpcode::kStoreElement: {
1334             Node* const object = NodeProperties::GetValueInput(current, 0);
1335             Node* const index = NodeProperties::GetValueInput(current, 1);
1336             state = state->KillElement(object, index, zone());
1337             break;
1338           }
1339           case IrOpcode::kStoreTypedElement: {
1340             // Doesn't affect anything we track with the state currently.
1341             break;
1342           }
1343           default:
1344             return state->KillAll(zone());
1345         }
1346       }
1347       for (int i = 0; i < current->op()->EffectInputCount(); ++i) {
1348         queue.push(NodeProperties::GetEffectInput(current, i));
1349       }
1350     }
1351   }
1352 
1353   // Finally, we apply the element transitions. For each transition, we will try
1354   // to only invalidate information about nodes that can have the transition's
1355   // source map. The trouble is that an object can be transitioned by some other
1356   // transition to the source map. In that case, the other transition will
1357   // invalidate the information, so we are mostly fine.
1358   //
1359   // The only bad case is
1360   //
1361   //    mapA   ---fast--->   mapB   ---slow--->   mapC
1362   //
1363   // If we process the slow transition first on an object that has mapA, we will
1364   // ignore the transition because the object does not have its source map
1365   // (mapB). When we later process the fast transition, we invalidate the
1366   // object's map, but we keep the information about the object's elements. This
1367   // is wrong because the elements will be overwritten by the slow transition.
1368   //
1369   // Note that the slow-slow case is fine because either of the slow transition
1370   // will invalidate the elements field, so the processing order does not
1371   // matter.
1372   //
1373   // To handle the bad case properly, we first kill the maps using all
1374   // transitions. We kill the the fields later when all the transitions are
1375   // already reflected in the map information.
1376 
1377   for (const TransitionElementsKindInfo& t : element_transitions_) {
1378     AliasStateInfo alias_info(state, t.object, t.transition.source());
1379     state = state->KillMaps(alias_info, zone());
1380   }
1381   for (const TransitionElementsKindInfo& t : element_transitions_) {
1382     switch (t.transition.mode()) {
1383       case ElementsTransition::kFastTransition:
1384         break;
1385       case ElementsTransition::kSlowTransition: {
1386         AliasStateInfo alias_info(state, t.object, t.transition.source());
1387         state = state->KillField(
1388             alias_info, FieldIndexOf(JSObject::kElementsOffset, kTaggedSize),
1389             MaybeHandle<Name>(), zone());
1390         break;
1391       }
1392     }
1393   }
1394   return state;
1395 }
1396 
1397 // static
FieldIndexOf(int offset,int representation_size)1398 LoadElimination::IndexRange LoadElimination::FieldIndexOf(
1399     int offset, int representation_size) {
1400   DCHECK(IsAligned(offset, kTaggedSize));
1401   int field_index = offset / kTaggedSize - 1;
1402   DCHECK_EQ(0, representation_size % kTaggedSize);
1403   return IndexRange(field_index, representation_size / kTaggedSize);
1404 }
1405 
1406 // static
FieldIndexOf(FieldAccess const & access)1407 LoadElimination::IndexRange LoadElimination::FieldIndexOf(
1408     FieldAccess const& access) {
1409   MachineRepresentation rep = access.machine_type.representation();
1410   switch (rep) {
1411     case MachineRepresentation::kNone:
1412     case MachineRepresentation::kBit:
1413     case MachineRepresentation::kSimd128:
1414       UNREACHABLE();
1415     case MachineRepresentation::kWord8:
1416     case MachineRepresentation::kWord16:
1417     case MachineRepresentation::kFloat32:
1418       // Currently untracked.
1419       return IndexRange::Invalid();
1420     case MachineRepresentation::kFloat64:
1421     case MachineRepresentation::kWord32:
1422     case MachineRepresentation::kWord64:
1423     case MachineRepresentation::kTaggedSigned:
1424     case MachineRepresentation::kTaggedPointer:
1425     case MachineRepresentation::kTagged:
1426     case MachineRepresentation::kCompressedPointer:
1427     case MachineRepresentation::kCompressed:
1428       break;
1429   }
1430   int representation_size = ElementSizeInBytes(rep);
1431   // We currently only track fields that are at least tagged pointer sized.
1432   if (representation_size < kTaggedSize) return IndexRange::Invalid();
1433   DCHECK_EQ(0, representation_size % kTaggedSize);
1434 
1435   if (access.base_is_tagged != kTaggedBase) {
1436     // We currently only track tagged objects.
1437     return IndexRange::Invalid();
1438   }
1439   return FieldIndexOf(access.offset, representation_size);
1440 }
1441 
common() const1442 CommonOperatorBuilder* LoadElimination::common() const {
1443   return jsgraph()->common();
1444 }
1445 
graph() const1446 Graph* LoadElimination::graph() const { return jsgraph()->graph(); }
1447 
isolate() const1448 Isolate* LoadElimination::isolate() const { return jsgraph()->isolate(); }
1449 
factory() const1450 Factory* LoadElimination::factory() const { return jsgraph()->factory(); }
1451 
1452 }  // namespace compiler
1453 }  // namespace internal
1454 }  // namespace v8
1455