summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/compiler/load-elimination-const-field.js
blob: fb227dd93dca6a52a1c470a6705ee9f60e491076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2019 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.

// Flags: --allow-natives-syntax

// Check that load elimination on const-marked fields works
(function() {
    function maybe_sideeffect(b) { return 42; }

    %NeverOptimizeFunction(maybe_sideeffect);

    class B {
        constructor(x) {
            this.value = x;
        }
    }
    %EnsureFeedbackVectorForFunction(B);


    function lit_const_smi() {
        let b = { value: 123 };
        maybe_sideeffect(b);
        let v1 = b.value;
        maybe_sideeffect(b);
        let v2 = b.value;
        %TurbofanStaticAssert(Object.is(v1, v2));
        %TurbofanStaticAssert(Object.is(v2, 123));
    }

    %PrepareFunctionForOptimization(lit_const_smi);
    lit_const_smi(); lit_const_smi();
    %OptimizeFunctionOnNextCall(lit_const_smi); lit_const_smi();


    function lit_const_object() {
        let o = {x: 123};
        let b = { value: o };
        maybe_sideeffect(b);
        let v1 = b.value;
        maybe_sideeffect(b);
        let v2 = b.value;
        %TurbofanStaticAssert(Object.is(v1, v2));
        %TurbofanStaticAssert(Object.is(v2, o));
    }

    %PrepareFunctionForOptimization(lit_const_object);
    lit_const_object(); lit_const_object();
    %OptimizeFunctionOnNextCall(lit_const_object); lit_const_object();


    function lit_computed_smi(k) {
        let kk = 2 * k;
        let b = { value: kk };
        maybe_sideeffect(b);
        let v1 = b.value;
        maybe_sideeffect(b);
        let v2 = b.value;
        %TurbofanStaticAssert(Object.is(v1, v2));
        %TurbofanStaticAssert(Object.is(v2, kk));
    }

    %PrepareFunctionForOptimization(lit_computed_smi);
    lit_computed_smi(1); lit_computed_smi(2);
    %OptimizeFunctionOnNextCall(lit_computed_smi); lit_computed_smi(3);

    // TODO(bmeurer): Fix const tracking for double fields in object literals
    // lit_computed_smi(1.1); lit_computed_smi(2.2);
    // %OptimizeFunctionOnNextCall(lit_computed_smi); lit_computed_smi(3.3);


    function lit_param_object(k) {
        let b = { value: k };
        maybe_sideeffect(b);
        let v1 = b.value;
        maybe_sideeffect(b);
        let v2 = b.value;
        %TurbofanStaticAssert(Object.is(v1, v2));
        %TurbofanStaticAssert(Object.is(v2, k));
    }

    %PrepareFunctionForOptimization(lit_param_object);
    lit_param_object({x: 1}); lit_param_object({x: 2});
    %OptimizeFunctionOnNextCall(lit_param_object); lit_param_object({x: 3});


    function nested_lit_param(k) {
        let b = { x: { value: k } };
        maybe_sideeffect(b);
        let v1 = b.x.value;
        maybe_sideeffect(b);
        let v2 = b.x.value;
        %TurbofanStaticAssert(Object.is(v1, v2));
        %TurbofanStaticAssert(Object.is(v2, k));
    }

    %PrepareFunctionForOptimization(nested_lit_param);
    nested_lit_param(1); nested_lit_param(2);
    %OptimizeFunctionOnNextCall(nested_lit_param); nested_lit_param(3);

    // TODO(bmeurer): Fix const tracking for double fields in object literals
    // nested_lit_param(1.1); nested_lit_param(2.2);
    // %OptimizeFunctionOnNextCall(nested_lit_param); nested_lit_param(3.3);


    function nested_lit_param_object(k) {
        let b = { x: { value: k } };
        maybe_sideeffect(b);
        let v1 = b.x.value;
        maybe_sideeffect(b);
        let v2 = b.x.value;
        %TurbofanStaticAssert(Object.is(v1, v2));
        %TurbofanStaticAssert(Object.is(v2, k));
    }

    %PrepareFunctionForOptimization(nested_lit_param_object);
    nested_lit_param_object({x: 1}); nested_lit_param_object({x: 2});
    %OptimizeFunctionOnNextCall(nested_lit_param_object);
    nested_lit_param_object({x: 3});


    function inst_param(k) {
        let b = new B(k);
        maybe_sideeffect(b);
        let v1 = b.value;
        maybe_sideeffect(b);
        let v2 = b.value;
        if (!%IsDictPropertyConstTrackingEnabled()) {
          // TODO(v8:11457) If v8_dict_property_const_tracking is enabled, then
          // b has a dictionary mode prototype and the load elimination doesn't
          // work, yet.
          %TurbofanStaticAssert(Object.is(v1, v2));
          %TurbofanStaticAssert(Object.is(v2, k));
        }
    }

    %EnsureFeedbackVectorForFunction(B);
    %PrepareFunctionForOptimization(inst_param);
    inst_param(1); inst_param(2);
    %OptimizeFunctionOnNextCall(inst_param); inst_param(3);

    // TODO(gsps): Reenable once we fully support const field information
    //   tracking in the presence of pointer compression.
    // inst_param(1.1); inst_param(2.2);
    // %OptimizeFunctionOnNextCall(inst_param); inst_param(3.3);

    %PrepareFunctionForOptimization(inst_param);
    inst_param({x: 1}); inst_param({x: 2});
    %OptimizeFunctionOnNextCall(inst_param); inst_param({x: 3});


    function inst_computed(k) {
        let kk = 2 * k;
        let b = new B(kk);
        maybe_sideeffect(b);
        let v1 = b.value;
        maybe_sideeffect(b);
        let v2 = b.value;
        if (!%IsDictPropertyConstTrackingEnabled()) {
          // TODO(v8:11457) If v8_dict_property_const_tracking is enabled, then
          // b has a dictionary mode prototype and the load elimination doesn't
          // work, yet.
          %TurbofanStaticAssert(Object.is(v1, v2));
          %TurbofanStaticAssert(Object.is(v2, kk));
        }
    }

    %EnsureFeedbackVectorForFunction(B);
    %PrepareFunctionForOptimization(inst_computed);
    inst_computed(1); inst_computed(2);
    %OptimizeFunctionOnNextCall(inst_computed); inst_computed(3);

    %PrepareFunctionForOptimization(inst_computed);
    inst_computed(1.1); inst_computed(2.2);
    %OptimizeFunctionOnNextCall(inst_computed); inst_computed(3.3);
})();