access-info.h 10.6 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_COMPILER_ACCESS_INFO_H_
#define V8_COMPILER_ACCESS_INFO_H_
7 8 9

#include <iosfwd>

10
#include "src/codegen/machine-type.h"
11
#include "src/compiler/types.h"
12
#include "src/objects/feedback-vector.h"
13
#include "src/objects/field-index.h"
14
#include "src/objects/map.h"
15
#include "src/objects/objects.h"
16
#include "src/zone/zone-containers.h"
17 18 19 20 21 22 23 24 25

namespace v8 {
namespace internal {

// Forward declarations.
class Factory;

namespace compiler {

26
// Forward declarations.
27 28
class CompilationDependencies;
class CompilationDependency;
29
class ElementAccessFeedback;
30
class JSHeapBroker;
31
class MinimorphicLoadPropertyAccessFeedback;
32
class TypeCache;
33
struct ConstFieldInfo;
34

35 36 37 38 39
std::ostream& operator<<(std::ostream&, AccessMode);

// This class encapsulates all information required to access a certain element.
class ElementAccessInfo final {
 public:
40 41
  ElementAccessInfo(ZoneVector<Handle<Map>>&& receiver_maps,
                    ElementsKind elements_kind, Zone* zone);
42 43

  ElementsKind elements_kind() const { return elements_kind_; }
44 45 46 47 48 49
  ZoneVector<Handle<Map>> const& receiver_maps() const {
    return receiver_maps_;
  }
  ZoneVector<Handle<Map>> const& transition_sources() const {
    return transition_sources_;
  }
50 51 52 53 54

  void AddTransitionSource(Handle<Map> map) {
    CHECK_EQ(receiver_maps_.size(), 1);
    transition_sources_.push_back(map);
  }
55 56 57

 private:
  ElementsKind elements_kind_;
58 59
  ZoneVector<Handle<Map>> receiver_maps_;
  ZoneVector<Handle<Map>> transition_sources_;
60
};
61 62 63 64 65

// This class encapsulates all information required to access a certain
// object property, either on the object itself or on the prototype chain.
class PropertyAccessInfo final {
 public:
66 67 68 69
  enum Kind {
    kInvalid,
    kNotFound,
    kDataField,
70
    kDataConstant,
71
    kAccessorConstant,
72 73
    kModuleExport,
    kStringLength
74
  };
75

76
  static PropertyAccessInfo NotFound(Zone* zone, Handle<Map> receiver_map,
77
                                     MaybeHandle<JSObject> holder);
78
  static PropertyAccessInfo DataField(
79
      Zone* zone, Handle<Map> receiver_map,
80
      ZoneVector<CompilationDependency const*>&& unrecorded_dependencies,
81
      FieldIndex field_index, Representation field_representation,
82 83
      Type field_type, Handle<Map> field_owner_map,
      MaybeHandle<Map> field_map = MaybeHandle<Map>(),
84
      MaybeHandle<JSObject> holder = MaybeHandle<JSObject>(),
85
      MaybeHandle<Map> transition_map = MaybeHandle<Map>());
86
  static PropertyAccessInfo DataConstant(
87
      Zone* zone, Handle<Map> receiver_map,
88
      ZoneVector<CompilationDependency const*>&& unrecorded_dependencies,
89
      FieldIndex field_index, Representation field_representation,
90 91
      Type field_type, Handle<Map> field_owner_map, MaybeHandle<Map> field_map,
      MaybeHandle<JSObject> holder,
92
      MaybeHandle<Map> transition_map = MaybeHandle<Map>());
93 94
  static PropertyAccessInfo AccessorConstant(Zone* zone,
                                             Handle<Map> receiver_map,
95 96
                                             Handle<Object> constant,
                                             MaybeHandle<JSObject> holder);
97
  static PropertyAccessInfo ModuleExport(Zone* zone, Handle<Map> receiver_map,
98
                                         Handle<Cell> cell);
99 100
  static PropertyAccessInfo StringLength(Zone* zone, Handle<Map> receiver_map);
  static PropertyAccessInfo Invalid(Zone* zone);
101

102
  bool Merge(PropertyAccessInfo const* that, AccessMode access_mode,
103
             Zone* zone) V8_WARN_UNUSED_RESULT;
104

105 106
  void RecordDependencies(CompilationDependencies* dependencies);

107
  bool IsInvalid() const { return kind() == kInvalid; }
108
  bool IsNotFound() const { return kind() == kNotFound; }
109
  bool IsDataField() const { return kind() == kDataField; }
110
  bool IsDataConstant() const { return kind() == kDataConstant; }
111
  bool IsAccessorConstant() const { return kind() == kAccessorConstant; }
112
  bool IsModuleExport() const { return kind() == kModuleExport; }
113
  bool IsStringLength() const { return kind() == kStringLength; }
114

115
  bool HasTransitionMap() const { return !transition_map().is_null(); }
116
  ConstFieldInfo GetConstFieldInfo() const;
117

118
  Kind kind() const { return kind_; }
119
  MaybeHandle<JSObject> holder() const {
120 121 122
    // TODO(neis): There was a CHECK here that tries to protect against
    // using the access info without recording its dependencies first.
    // Find a more suitable place for it.
123 124
    return holder_;
  }
125 126 127
  MaybeHandle<Map> transition_map() const { return transition_map_; }
  Handle<Object> constant() const { return constant_; }
  FieldIndex field_index() const { return field_index_; }
128
  Type field_type() const { return field_type_; }
129
  Representation field_representation() const { return field_representation_; }
130
  MaybeHandle<Map> field_map() const { return field_map_; }
131 132 133
  ZoneVector<Handle<Map>> const& receiver_maps() const {
    return receiver_maps_;
  }
134 135

 private:
136 137 138 139 140 141
  explicit PropertyAccessInfo(Zone* zone);
  PropertyAccessInfo(Zone* zone, Kind kind, MaybeHandle<JSObject> holder,
                     ZoneVector<Handle<Map>>&& receiver_maps);
  PropertyAccessInfo(Zone* zone, Kind kind, MaybeHandle<JSObject> holder,
                     Handle<Object> constant,
                     ZoneVector<Handle<Map>>&& receiver_maps);
142 143 144
  PropertyAccessInfo(Kind kind, MaybeHandle<JSObject> holder,
                     MaybeHandle<Map> transition_map, FieldIndex field_index,
                     Representation field_representation, Type field_type,
145
                     Handle<Map> field_owner_map, MaybeHandle<Map> field_map,
146 147
                     ZoneVector<Handle<Map>>&& receiver_maps,
                     ZoneVector<CompilationDependency const*>&& dependencies);
148 149

  Kind kind_;
150
  ZoneVector<Handle<Map>> receiver_maps_;
151
  ZoneVector<CompilationDependency const*> unrecorded_dependencies_;
152 153 154 155
  Handle<Object> constant_;
  MaybeHandle<Map> transition_map_;
  MaybeHandle<JSObject> holder_;
  FieldIndex field_index_;
156
  Representation field_representation_;
157
  Type field_type_;
158
  MaybeHandle<Map> field_owner_map_;
159
  MaybeHandle<Map> field_map_;
160 161
};

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
// This class encapsulates information required to generate load properties
// by only using the information from handlers. This information is used with
// dynamic map checks.
class MinimorphicLoadPropertyAccessInfo final {
 public:
  enum Kind { kInvalid, kDataField };
  static MinimorphicLoadPropertyAccessInfo DataField(
      int offset, bool is_inobject, Representation field_representation,
      Type field_type);
  static MinimorphicLoadPropertyAccessInfo Invalid();

  bool IsInvalid() const { return kind_ == kInvalid; }
  bool IsDataField() const { return kind_ == kDataField; }
  int offset() const { return offset_; }
  int is_inobject() const { return is_inobject_; }
  Type field_type() const { return field_type_; }
  Representation field_representation() const { return field_representation_; }

 private:
  MinimorphicLoadPropertyAccessInfo(Kind kind, int offset, bool is_inobject,
                                    Representation field_representation,
                                    Type field_type);

  Kind kind_;
  bool is_inobject_;
  int offset_;
  Representation field_representation_;
  Type field_type_;
};
191

192 193
// Factory class for {ElementAccessInfo}s and {PropertyAccessInfo}s.
class AccessInfoFactory final {
194
 public:
195
  AccessInfoFactory(JSHeapBroker* broker, CompilationDependencies* dependencies,
196
                    Zone* zone);
197

198 199
  base::Optional<ElementAccessInfo> ComputeElementAccessInfo(
      Handle<Map> map, AccessMode access_mode) const;
200
  bool ComputeElementAccessInfos(
201
      ElementAccessFeedback const& feedback,
202 203
      ZoneVector<ElementAccessInfo>* access_infos) const;

204 205 206
  PropertyAccessInfo ComputePropertyAccessInfo(Handle<Map> map,
                                               Handle<Name> name,
                                               AccessMode access_mode) const;
207

208 209 210
  MinimorphicLoadPropertyAccessInfo ComputePropertyAccessInfo(
      MinimorphicLoadPropertyAccessFeedback const& feedback) const;

211
  // Convenience wrapper around {ComputePropertyAccessInfo} for multiple maps.
212
  void ComputePropertyAccessInfos(
213 214
      MapHandles const& maps, Handle<Name> name, AccessMode access_mode,
      ZoneVector<PropertyAccessInfo>* access_infos) const;
215

216 217 218 219
  // Merge as many of the given {infos} as possible and record any dependencies.
  // Return false iff any of them was invalid, in which case no dependencies are
  // recorded.
  // TODO(neis): Make access_mode part of access info?
220 221 222 223
  bool FinalizePropertyAccessInfos(
      ZoneVector<PropertyAccessInfo> infos, AccessMode access_mode,
      ZoneVector<PropertyAccessInfo>* result) const;

224 225 226 227 228 229
  // Merge the given {infos} to a single one and record any dependencies. If the
  // merge is not possible, the result has kind {kInvalid} and no dependencies
  // are recorded.
  PropertyAccessInfo FinalizePropertyAccessInfosAsOne(
      ZoneVector<PropertyAccessInfo> infos, AccessMode access_mode) const;

230
 private:
231
  base::Optional<ElementAccessInfo> ConsolidateElementLoad(
232
      ElementAccessFeedback const& feedback) const;
233 234 235 236 237 238 239
  PropertyAccessInfo LookupSpecialFieldAccessor(Handle<Map> map,
                                                Handle<Name> name) const;
  PropertyAccessInfo LookupTransition(Handle<Map> map, Handle<Name> name,
                                      MaybeHandle<JSObject> holder) const;
  PropertyAccessInfo ComputeDataFieldAccessInfo(Handle<Map> receiver_map,
                                                Handle<Map> map,
                                                MaybeHandle<JSObject> holder,
240
                                                InternalIndex descriptor,
241 242
                                                AccessMode access_mode) const;
  PropertyAccessInfo ComputeAccessorDescriptorAccessInfo(
243
      Handle<Map> receiver_map, Handle<Name> name, Handle<Map> map,
244
      MaybeHandle<JSObject> holder, InternalIndex descriptor,
245
      AccessMode access_mode) const;
246

247 248 249 250
  void MergePropertyAccessInfos(ZoneVector<PropertyAccessInfo> infos,
                                AccessMode access_mode,
                                ZoneVector<PropertyAccessInfo>* result) const;

251
  CompilationDependencies* dependencies() const { return dependencies_; }
252
  JSHeapBroker* broker() const { return broker_; }
253
  Isolate* isolate() const;
254 255
  Zone* zone() const { return zone_; }

256
  JSHeapBroker* const broker_;
257
  CompilationDependencies* const dependencies_;
258
  TypeCache const* const type_cache_;
259 260
  Zone* const zone_;

261
  DISALLOW_COPY_AND_ASSIGN(AccessInfoFactory);
262 263 264 265 266 267
};

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

268
#endif  // V8_COMPILER_ACCESS_INFO_H_