load-elimination.h 11.3 KB
Newer Older
1
// Copyright 2016 the V8 project authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_LOAD_ELIMINATION_H_
#define V8_COMPILER_LOAD_ELIMINATION_H_

8
#include "src/base/compiler-specific.h"
9
#include "src/compiler/graph-reducer.h"
10
#include "src/globals.h"
11
#include "src/machine-type.h"
12
#include "src/zone/zone-handle-set.h"
13

14 15
namespace v8 {
namespace internal {
16 17 18 19

// Forward declarations.
class Factory;

20 21
namespace compiler {

22
// Forward declarations.
23
class CommonOperatorBuilder;
24
struct FieldAccess;
25
class Graph;
26
class JSGraph;
27

28 29
class V8_EXPORT_PRIVATE LoadElimination final
    : public NON_EXPORTED_BASE(AdvancedReducer) {
30
 public:
31 32
  LoadElimination(Editor* editor, JSGraph* jsgraph, Zone* zone)
      : AdvancedReducer(editor), node_states_(zone), jsgraph_(jsgraph) {}
33
  ~LoadElimination() final {}
34

35 36
  const char* reducer_name() const override { return "LoadElimination"; }

37
  Reduction Reduce(Node* node) final;
38 39

 private:
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  static const size_t kMaxTrackedChecks = 8;

  // Abstract state to approximate the current state of checks that are
  // only invalidated by calls, i.e. array buffer neutering checks, along
  // the effect paths through the graph.
  class AbstractChecks final : public ZoneObject {
   public:
    explicit AbstractChecks(Zone* zone) {
      for (size_t i = 0; i < arraysize(nodes_); ++i) {
        nodes_[i] = nullptr;
      }
    }
    AbstractChecks(Node* node, Zone* zone) : AbstractChecks(zone) {
      nodes_[next_index_++] = node;
    }

    AbstractChecks const* Extend(Node* node, Zone* zone) const {
      AbstractChecks* that = new (zone) AbstractChecks(*this);
      that->nodes_[that->next_index_] = node;
      that->next_index_ = (that->next_index_ + 1) % arraysize(nodes_);
      return that;
    }
    Node* Lookup(Node* node) const;
    bool Equals(AbstractChecks const* that) const;
    AbstractChecks const* Merge(AbstractChecks const* that, Zone* zone) const;

66 67
    void Print() const;

68 69 70 71 72
   private:
    Node* nodes_[kMaxTrackedChecks];
    size_t next_index_ = 0;
  };

73 74 75 76 77 78 79 80 81 82 83
  static const size_t kMaxTrackedElements = 8;

  // Abstract state to approximate the current state of an element along the
  // effect paths through the graph.
  class AbstractElements final : public ZoneObject {
   public:
    explicit AbstractElements(Zone* zone) {
      for (size_t i = 0; i < arraysize(elements_); ++i) {
        elements_[i] = Element();
      }
    }
84 85
    AbstractElements(Node* object, Node* index, Node* value,
                     MachineRepresentation representation, Zone* zone)
86
        : AbstractElements(zone) {
87
      elements_[next_index_++] = Element(object, index, value, representation);
88 89 90
    }

    AbstractElements const* Extend(Node* object, Node* index, Node* value,
91
                                   MachineRepresentation representation,
92 93
                                   Zone* zone) const {
      AbstractElements* that = new (zone) AbstractElements(*this);
94 95
      that->elements_[that->next_index_] =
          Element(object, index, value, representation);
96 97 98
      that->next_index_ = (that->next_index_ + 1) % arraysize(elements_);
      return that;
    }
99 100
    Node* Lookup(Node* object, Node* index,
                 MachineRepresentation representation) const;
101 102 103 104 105
    AbstractElements const* Kill(Node* object, Node* index, Zone* zone) const;
    bool Equals(AbstractElements const* that) const;
    AbstractElements const* Merge(AbstractElements const* that,
                                  Zone* zone) const;

106 107
    void Print() const;

108 109 110
   private:
    struct Element {
      Element() {}
111 112 113 114 115 116
      Element(Node* object, Node* index, Node* value,
              MachineRepresentation representation)
          : object(object),
            index(index),
            value(value),
            representation(representation) {}
117 118 119 120

      Node* object = nullptr;
      Node* index = nullptr;
      Node* value = nullptr;
121
      MachineRepresentation representation = MachineRepresentation::kNone;
122 123 124 125 126 127
    };

    Element elements_[kMaxTrackedElements];
    size_t next_index_ = 0;
  };

128 129 130 131 132
  // Information we use to resolve object aliasing. Currently, we consider
  // object not aliased if they have different maps or if the nodes may
  // not alias.
  class AliasStateInfo;

133 134 135 136 137
  // Abstract state to approximate the current state of a certain field along
  // the effect paths through the graph.
  class AbstractField final : public ZoneObject {
   public:
    explicit AbstractField(Zone* zone) : info_for_node_(zone) {}
138
    AbstractField(Node* object, Node* value, MaybeHandle<Name> name, Zone* zone)
139
        : info_for_node_(zone) {
140
      info_for_node_.insert(std::make_pair(object, Field(value, name)));
141 142
    }

143 144
    AbstractField const* Extend(Node* object, Node* value,
                                MaybeHandle<Name> name, Zone* zone) const {
145 146
      AbstractField* that = new (zone) AbstractField(zone);
      that->info_for_node_ = this->info_for_node_;
147
      that->info_for_node_.insert(std::make_pair(object, Field(value, name)));
148 149 150
      return that;
    }
    Node* Lookup(Node* object) const;
151 152
    AbstractField const* Kill(const AliasStateInfo& alias_info,
                              MaybeHandle<Name> name, Zone* zone) const;
153 154 155 156 157 158 159 160
    bool Equals(AbstractField const* that) const {
      return this == that || this->info_for_node_ == that->info_for_node_;
    }
    AbstractField const* Merge(AbstractField const* that, Zone* zone) const {
      if (this->Equals(that)) return this;
      AbstractField* copy = new (zone) AbstractField(zone);
      for (auto this_it : this->info_for_node_) {
        Node* this_object = this_it.first;
161
        Field this_second = this_it.second;
162 163
        auto that_it = that->info_for_node_.find(this_object);
        if (that_it != that->info_for_node_.end() &&
164
            that_it->second == this_second) {
165 166 167 168 169 170
          copy->info_for_node_.insert(this_it);
        }
      }
      return copy;
    }

171 172
    void Print() const;

173
   private:
174 175 176 177 178 179 180 181 182 183 184 185 186
    struct Field {
      Field() {}
      Field(Node* value, MaybeHandle<Name> name) : value(value), name(name) {}

      bool operator==(const Field& other) const {
        return value == other.value && name.address() == other.name.address();
      }

      Node* value = nullptr;
      MaybeHandle<Name> name;
    };

    ZoneMap<Node*, Field> info_for_node_;
187 188
  };

189
  static size_t const kMaxTrackedFields = 32;
190

191 192 193 194
  // Abstract state to approximate the current map of an object along the
  // effect paths through the graph.
  class AbstractMaps final : public ZoneObject {
   public:
195
    explicit AbstractMaps(Zone* zone);
196
    AbstractMaps(Node* object, ZoneHandleSet<Map> maps, Zone* zone);
197

198
    AbstractMaps const* Extend(Node* object, ZoneHandleSet<Map> maps,
199
                               Zone* zone) const;
200
    bool Lookup(Node* object, ZoneHandleSet<Map>* object_maps) const;
201 202
    AbstractMaps const* Kill(const AliasStateInfo& alias_info,
                             Zone* zone) const;
203 204 205
    bool Equals(AbstractMaps const* that) const {
      return this == that || this->info_for_node_ == that->info_for_node_;
    }
206
    AbstractMaps const* Merge(AbstractMaps const* that, Zone* zone) const;
207 208 209 210

    void Print() const;

   private:
211
    ZoneMap<Node*, ZoneHandleSet<Map>> info_for_node_;
212 213
  };

214 215
  class AbstractState final : public ZoneObject {
   public:
216 217 218 219 220 221
    AbstractState() {
      for (size_t i = 0; i < arraysize(fields_); ++i) {
        fields_[i] = nullptr;
      }
    }

222 223 224
    bool Equals(AbstractState const* that) const;
    void Merge(AbstractState const* that, Zone* zone);

225
    AbstractState const* SetMaps(Node* object, ZoneHandleSet<Map> maps,
226 227
                                 Zone* zone) const;
    AbstractState const* KillMaps(Node* object, Zone* zone) const;
228 229
    AbstractState const* KillMaps(const AliasStateInfo& alias_info,
                                  Zone* zone) const;
230 231
    bool LookupMaps(Node* object, ZoneHandleSet<Map>* object_maps) const;

232
    AbstractState const* AddField(Node* object, size_t index, Node* value,
233
                                  MaybeHandle<Name> name, Zone* zone) const;
234 235 236
    AbstractState const* KillField(const AliasStateInfo& alias_info,
                                   size_t index, MaybeHandle<Name> name,
                                   Zone* zone) const;
237
    AbstractState const* KillField(Node* object, size_t index,
238 239 240
                                   MaybeHandle<Name> name, Zone* zone) const;
    AbstractState const* KillFields(Node* object, MaybeHandle<Name> name,
                                    Zone* zone) const;
241 242 243
    Node* LookupField(Node* object, size_t index) const;

    AbstractState const* AddElement(Node* object, Node* index, Node* value,
244
                                    MachineRepresentation representation,
245 246 247
                                    Zone* zone) const;
    AbstractState const* KillElement(Node* object, Node* index,
                                     Zone* zone) const;
248 249
    Node* LookupElement(Node* object, Node* index,
                        MachineRepresentation representation) const;
250

251 252 253
    AbstractState const* AddCheck(Node* node, Zone* zone) const;
    Node* LookupCheck(Node* node) const;

254 255
    void Print() const;

256
   private:
257
    AbstractChecks const* checks_ = nullptr;
258 259
    AbstractElements const* elements_ = nullptr;
    AbstractField const* fields_[kMaxTrackedFields];
260
    AbstractMaps const* maps_ = nullptr;
261 262 263 264 265 266 267 268 269 270 271 272 273 274
  };

  class AbstractStateForEffectNodes final : public ZoneObject {
   public:
    explicit AbstractStateForEffectNodes(Zone* zone) : info_for_node_(zone) {}
    AbstractState const* Get(Node* node) const;
    void Set(Node* node, AbstractState const* state);

    Zone* zone() const { return info_for_node_.get_allocator().zone(); }

   private:
    ZoneVector<AbstractState const*> info_for_node_;
  };

275
  Reduction ReduceArrayBufferWasNeutered(Node* node);
276
  Reduction ReduceCheckMaps(Node* node);
277
  Reduction ReduceCompareMaps(Node* node);
278
  Reduction ReduceMapGuard(Node* node);
279
  Reduction ReduceEnsureWritableFastElements(Node* node);
280
  Reduction ReduceMaybeGrowFastElements(Node* node);
281
  Reduction ReduceTransitionElementsKind(Node* node);
282 283 284 285
  Reduction ReduceLoadField(Node* node);
  Reduction ReduceStoreField(Node* node);
  Reduction ReduceLoadElement(Node* node);
  Reduction ReduceStoreElement(Node* node);
286
  Reduction ReduceTransitionAndStoreElement(Node* node);
287
  Reduction ReduceStoreTypedElement(Node* node);
288 289 290 291 292 293 294 295
  Reduction ReduceEffectPhi(Node* node);
  Reduction ReduceStart(Node* node);
  Reduction ReduceOtherNode(Node* node);

  Reduction UpdateState(Node* node, AbstractState const* state);

  AbstractState const* ComputeLoopState(Node* node,
                                        AbstractState const* state) const;
296 297
  AbstractState const* UpdateStateForPhi(AbstractState const* state,
                                         Node* effect_phi, Node* phi);
298

299
  static int FieldIndexOf(int offset);
300 301
  static int FieldIndexOf(FieldAccess const& access);

302
  CommonOperatorBuilder* common() const;
303
  AbstractState const* empty_state() const { return &empty_state_; }
304
  Factory* factory() const;
305
  Graph* graph() const;
306
  JSGraph* jsgraph() const { return jsgraph_; }
307 308 309 310
  Zone* zone() const { return node_states_.zone(); }

  AbstractState const empty_state_;
  AbstractStateForEffectNodes node_states_;
311
  JSGraph* const jsgraph_;
312

313
  DISALLOW_COPY_AND_ASSIGN(LoadElimination);
314 315 316 317 318 319 320
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_LOAD_ELIMINATION_H_