count-deopt.js 5.14 KB
Newer Older
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
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
// Flags: --allow-natives-syntax

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// Test deopt with count operation on parameter.
var max_smi = 1073741823;
var o = {x:0};

function inc1(x) { x++; o.x = x; }
inc1(max_smi);
assertEquals(max_smi + 1, o.x);

inc1(1.1);
assertEquals(2.1, o.x);


// Test deopt with count operation on named property.
function inc2(p) { p.x++ }

45 46
%PrepareFunctionForOptimization(inc2);

47 48 49 50
o.x = "42";
inc2(o);
assertEquals(43, o.x);

51
var s = max_smi - 10;
52
o.x = s;
53
for(var i = 0; i < 20; i++) {
54
  inc2(o);
55 56 57
  if (i == 4) {
    %OptimizeFunctionOnNextCall(inc2);
  }
58
}
59
assertEquals(max_smi + 10, o.x);
60 61 62 63 64


// Test deopt with count operation on keyed property.
function inc3(a, b) { a[b]++; }

65 66
%PrepareFunctionForOptimization(inc3);

67 68 69 70
o = ["42"];
inc3(o, 0);
assertEquals(43, o[0]);

71
var s = max_smi - 10;
72
o[0] = s;
73
for(var i = 0; i < 20; i++) {
74
  inc3(o, 0);
75 76 77
  if (i == 4) {
    %OptimizeFunctionOnNextCall(inc3);
  }
78
}
79
assertEquals(max_smi + 10, o[0]);
80 81 82

inc3(o,"0");

83
assertEquals(max_smi + 11, o[0]);
84 85 86

// Test bailout when accessing a non-existing array element.
o[0] = 0;
87
for(var i = 0; i < 5; i++) {
88 89
  inc3(o, 0);
}
90
%PrepareFunctionForOptimization(inc3);
91 92 93
%OptimizeFunctionOnNextCall(inc3);
inc3(o, 0);
inc3(o, 1);
94 95 96

// Test bailout with count operation in a value context.
function inc4(x,y) { return (x++) + y; }
97
%PrepareFunctionForOptimization(inc4);
98 99 100
for (var i = 0; i < 5; ++i) assertEquals(3, inc4(2, 1));
%OptimizeFunctionOnNextCall(inc4);
inc4(2, 1);
101 102 103
assertEquals(3.1, inc4(2, 1.1));

function inc5(x,y) { return (++x) + y; }
104
%PrepareFunctionForOptimization(inc5);
105 106 107
for (var i = 0; i < 5; ++i) assertEquals(4, inc5(2, 1));
%OptimizeFunctionOnNextCall(inc5);
assertEquals(4, inc5(2, 1));
108 109 110 111
assertEquals(4.1, inc5(2, 1.1));
assertEquals(4.1, inc5(2.1, 1));

function inc6(o,y) { return (o.x++) + y; }
112
%PrepareFunctionForOptimization(inc6);
113
o = {x:0};
114
for (var i = 0; i < 5; ++i) {
115 116 117
  o.x = 42;
  assertEquals(43, inc6(o, 1));
}
118 119 120
%OptimizeFunctionOnNextCall(inc6);
o.x = 42;
assertEquals(43, inc6(o, 1));
121 122 123 124 125 126
o.x = 42;
assertEquals(43.1, inc6(o, 1.1));
o.x = 42.1;
assertEquals(43.1, inc6(o, 1));

function inc7(o,y) { return (++o.x) + y; }
127
%PrepareFunctionForOptimization(inc7);
128
o = {x:0};
129
for (var i = 0; i < 5; ++i) {
130 131 132
  o.x = 42;
  assertEquals(44, inc7(o, 1));
}
133 134 135
%OptimizeFunctionOnNextCall(inc7);
o.x = 42;
assertEquals(44, inc7(o, 1));
136 137 138 139 140 141
o.x = 42;
assertEquals(44.1, inc7(o, 1.1));
o.x = 42.1;
assertEquals(44.1, inc7(o, 1));

function inc8(o,y) { return (o[0]++) + y; }
142
%PrepareFunctionForOptimization(inc8);
143
var q = [0];
144
for (var i = 0; i < 5; ++i) {
145 146 147
  q[0] = 42;
  assertEquals(43, inc8(q, 1));
}
148 149 150
%OptimizeFunctionOnNextCall(inc8);
q[0] = 42;
assertEquals(43, inc8(q, 1));
151 152 153 154 155 156
q[0] = 42;
assertEquals(43.1, inc8(q, 1.1));
q[0] = 42.1;
assertEquals(43.1, inc8(q, 1));

function inc9(o,y) { return (++o[0]) + y; }
157
%PrepareFunctionForOptimization(inc9);
158
q = [0];
159
for (var i = 0; i < 5; ++i) {
160 161 162
  q[0] = 42;
  assertEquals(44, inc9(q, 1));
}
163 164 165
%OptimizeFunctionOnNextCall(inc9);
q[0] = 42;
assertEquals(44, inc9(q, 1));
166 167 168 169 170 171 172
q[0] = 42;
assertEquals(44.1, inc9(q, 1.1));
q[0] = 42.1;
assertEquals(44.1, inc9(q, 1));

// Test deopt because of a failed map check.
function inc10(p) { return p.x++ }
173
%PrepareFunctionForOptimization(inc10);
174 175
var g1 = {x:0};
var g2 = {y:0, x:42}
176
for (var i = 0; i < 5; ++i) {
177 178 179 180
  g1.x = 42;
  assertEquals(42, inc10(g1));
  assertEquals(43, g1.x);
}
181 182 183 184
%OptimizeFunctionOnNextCall(inc10);
g1.x = 42;
assertEquals(42, inc10(g1));
assertEquals(43, g1.x);
185 186 187 188 189 190 191 192
assertEquals(42, inc10(g2));
assertEquals(43, g2.x);

// Test deoptimization with postfix operation in a value context.
function inc11(a) { return a[this.x++]; }
var g3 = {x:null, f:inc11};
var g4 = [42];
assertEquals(42, g3.f(g4));