Line data Source code
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:
21 : AllocationBuilder(JSGraph* jsgraph, Node* effect, Node* control)
22 : : jsgraph_(jsgraph),
23 : allocation_(nullptr),
24 : effect_(effect),
25 121822 : control_(control) {}
26 :
27 : // Primitive allocation of static size.
28 121822 : void Allocate(int size, PretenureFlag pretenure = NOT_TENURED,
29 609110 : Type* type = Type::Any()) {
30 : DCHECK_LE(size, kMaxRegularHeapObjectSize);
31 : effect_ = graph()->NewNode(
32 365466 : common()->BeginRegion(RegionObservability::kNotObservable), effect_);
33 : allocation_ =
34 : graph()->NewNode(simplified()->Allocate(type, pretenure),
35 487288 : jsgraph()->Constant(size), effect_, control_);
36 121822 : effect_ = allocation_;
37 121822 : }
38 :
39 : // Primitive store into a field.
40 2141601 : void Store(const FieldAccess& access, Node* value) {
41 : effect_ = graph()->NewNode(simplified()->StoreField(access), allocation_,
42 2141601 : value, effect_, control_);
43 713867 : }
44 :
45 : // Primitive store into an element.
46 94209 : void Store(ElementAccess const& access, Node* index, Node* value) {
47 : effect_ = graph()->NewNode(simplified()->StoreElement(access), allocation_,
48 94209 : index, value, effect_, control_);
49 31403 : }
50 :
51 : // Compound allocation of a FixedArray.
52 56665 : void AllocateArray(int length, Handle<Map> map,
53 56665 : PretenureFlag pretenure = NOT_TENURED) {
54 : DCHECK(map->instance_type() == FIXED_ARRAY_TYPE ||
55 : map->instance_type() == FIXED_DOUBLE_ARRAY_TYPE);
56 : int size = (map->instance_type() == FIXED_ARRAY_TYPE)
57 : ? FixedArray::SizeFor(length)
58 56665 : : FixedDoubleArray::SizeFor(length);
59 56665 : Allocate(size, pretenure, Type::OtherInternal());
60 56665 : Store(AccessBuilder::ForMap(), map);
61 56665 : Store(AccessBuilder::ForFixedArrayLength(), jsgraph()->Constant(length));
62 56665 : }
63 :
64 : // Compound store of a constant into a field.
65 136796 : void Store(const FieldAccess& access, Handle<Object> value) {
66 136796 : Store(access, jsgraph()->Constant(value));
67 136796 : }
68 :
69 178900 : void FinishAndChange(Node* node) {
70 89450 : NodeProperties::SetType(allocation_, NodeProperties::GetType(node));
71 89450 : node->ReplaceInput(0, allocation_);
72 89450 : node->ReplaceInput(1, effect_);
73 89450 : node->TrimInputCount(2);
74 89450 : NodeProperties::ChangeOp(node, common()->FinishRegion());
75 89450 : }
76 :
77 97116 : Node* Finish() {
78 97116 : return graph()->NewNode(common()->FinishRegion(), allocation_, effect_);
79 : }
80 :
81 : protected:
82 : JSGraph* jsgraph() { return jsgraph_; }
83 1021286 : Graph* graph() { return jsgraph_->graph(); }
84 243644 : CommonOperatorBuilder* common() { return jsgraph_->common(); }
85 867092 : 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_
|