• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 #ifndef V8_COMPILER_ALLOCATION_BUILDER_H_
6 #define V8_COMPILER_ALLOCATION_BUILDER_H_
7 
8 #include "src/compiler/js-graph.h"
9 #include "src/compiler/node.h"
10 #include "src/compiler/simplified-operator.h"
11 
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15 
16 // A helper class to construct inline allocations on the simplified operator
17 // level. This keeps track of the effect chain for initial stores on a newly
18 // allocated object and also provides helpers for commonly allocated objects.
19 class AllocationBuilder final {
20  public:
AllocationBuilder(JSGraph * jsgraph,Node * effect,Node * control)21   AllocationBuilder(JSGraph* jsgraph, Node* effect, Node* control)
22       : jsgraph_(jsgraph),
23         allocation_(nullptr),
24         effect_(effect),
25         control_(control) {}
26 
27   // Primitive allocation of static size.
28   void Allocate(int size, AllocationType allocation = AllocationType::kYoung,
29                 Type type = Type::Any()) {
30     DCHECK_LE(size, Heap::MaxRegularHeapObjectSize(allocation));
31     effect_ = graph()->NewNode(
32         common()->BeginRegion(RegionObservability::kNotObservable), effect_);
33     allocation_ =
34         graph()->NewNode(simplified()->Allocate(type, allocation),
35                          jsgraph()->Constant(size), effect_, control_);
36     effect_ = allocation_;
37   }
38 
39   // Primitive store into a field.
Store(const FieldAccess & access,Node * value)40   void Store(const FieldAccess& access, Node* value) {
41     effect_ = graph()->NewNode(simplified()->StoreField(access), allocation_,
42                                value, effect_, control_);
43   }
44 
45   // Primitive store into an element.
Store(ElementAccess const & access,Node * index,Node * value)46   void Store(ElementAccess const& access, Node* index, Node* value) {
47     effect_ = graph()->NewNode(simplified()->StoreElement(access), allocation_,
48                                index, value, effect_, control_);
49   }
50 
51   // Compound allocation of a context.
52   inline void AllocateContext(int variadic_part_length, MapRef map);
53 
54   // Compound allocation of a FixedArray.
55   inline void AllocateArray(int length, MapRef map,
56                             AllocationType allocation = AllocationType::kYoung);
57 
58   // Compound allocation of a SloppyArgumentsElements
59   inline void AllocateSloppyArgumentElements(
60       int length, MapRef map,
61       AllocationType allocation = AllocationType::kYoung);
62 
63   // Compound store of a constant into a field.
Store(const FieldAccess & access,const ObjectRef & value)64   void Store(const FieldAccess& access, const ObjectRef& value) {
65     Store(access, jsgraph()->Constant(value));
66   }
67 
FinishAndChange(Node * node)68   void FinishAndChange(Node* node) {
69     NodeProperties::SetType(allocation_, NodeProperties::GetType(node));
70     node->ReplaceInput(0, allocation_);
71     node->ReplaceInput(1, effect_);
72     node->TrimInputCount(2);
73     NodeProperties::ChangeOp(node, common()->FinishRegion());
74   }
75 
Finish()76   Node* Finish() {
77     return graph()->NewNode(common()->FinishRegion(), allocation_, effect_);
78   }
79 
80  protected:
jsgraph()81   JSGraph* jsgraph() { return jsgraph_; }
isolate()82   Isolate* isolate() const { return jsgraph_->isolate(); }
graph()83   Graph* graph() { return jsgraph_->graph(); }
common()84   CommonOperatorBuilder* common() { return jsgraph_->common(); }
simplified()85   SimplifiedOperatorBuilder* simplified() { return jsgraph_->simplified(); }
86 
87  private:
88   JSGraph* const jsgraph_;
89   Node* allocation_;
90   Node* effect_;
91   Node* control_;
92 };
93 
94 }  // namespace compiler
95 }  // namespace internal
96 }  // namespace v8
97 
98 #endif  // V8_COMPILER_ALLOCATION_BUILDER_H_
99