assignment-deopt.js revision 8b112d2025046f85ef7f6be087c6129c872ebad2
1// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --allow-natives-syntax
29
30// Test deopt with count operation on parameter.
31var max_smi = 1073741823;
32var o = {x:0};
33
34function assign1(x) { x += 1; o.x = x; }
35assign1(max_smi);
36assertEquals(max_smi + 1, o.x);
37
38assign1(1.1);
39assertEquals(2.1, o.x);
40
41
42// Test deopt with count operation on named property.
43function assign2(p) { p.x += 1 }
44
45o.x = "42";
46assign2(o);
47assertEquals("421", o.x);
48
49var s = max_smi - 10;
50o.x = s;
51for(var i = 0; i < 20; i++) {
52  assign2(o);
53  if (i == 4) {
54    %OptimizeFunctionOnNextCall(assign2);
55  }
56}
57assertEquals(max_smi + 10, o.x);
58
59
60// Test deopt with count operation on keyed property.
61function assign3(a, b) { a[b] += 1; }
62
63o = ["42"];
64assign3(o, 0);
65assertEquals("421", o[0]);
66
67var s = max_smi - 10;
68o[0] = s;
69for(var i = 0; i < 20; i++) {
70  assign3(o, 0);
71  if (i == 4) {
72    %OptimizeFunctionOnNextCall(assign3);
73  }
74}
75assertEquals(max_smi + 10, o[0]);
76
77assign3(o, "0");
78
79assertEquals(max_smi + 11, o[0]);
80
81// Test bailout when accessing a non-existing array element.
82o[0] = 0;
83for(var i = 0; i < 5; i++) {
84  assign3(o, 0);
85}
86%OptimizeFunctionOnNextCall(assign3);
87assign3(o, 0);
88assign3(o, 1);
89
90// Test bailout with count operation in a value context.
91function assign5(x,y) { return (x += 1) + y; }
92for (var i = 0; i < 5; ++i) assertEquals(4, assign5(2, 1));
93%OptimizeFunctionOnNextCall(assign5);
94assertEquals(4, assign5(2, 1));
95
96assertEquals(4.1, assign5(2, 1.1));
97assertEquals(4.1, assign5(2.1, 1));
98
99function assign7(o,y) { return (o.x += 1) + y; }
100o = {x:0};
101for (var i = 0; i < 5; ++i) {
102  o.x = 42;
103  assertEquals(44, assign7(o, 1));
104}
105%OptimizeFunctionOnNextCall(assign7);
106o.x = 42;
107assertEquals(44, assign7(o, 1));
108
109o.x = 42;
110assertEquals(44.1, assign7(o, 1.1));
111o.x = 42.1;
112assertEquals(44.1, assign7(o, 1));
113
114function assign9(o,y) { return (o[0] += 1) + y; }
115q = [0];
116for (var i = 0; i < 5; ++i) {
117  q[0] = 42;
118  assertEquals(44, assign9(q, 1));
119}
120%OptimizeFunctionOnNextCall(assign9);
121q[0] = 42;
122assertEquals(44, assign9(q, 1));
123
124q[0] = 42;
125assertEquals(44.1, assign9(q, 1.1));
126q[0] = 42.1;
127assertEquals(44.1, assign9(q, 1));
128
129// Test deopt because of a failed map check on the load.
130function assign10(p) { return p.x += 1 }
131var g1 = {x:0};
132var g2 = {y:0, x:42};
133for (var i = 0; i < 5; ++i) {
134  g1.x = 42;
135  assertEquals(43, assign10(g1));
136  assertEquals(43, g1.x);
137}
138%OptimizeFunctionOnNextCall(assign10);
139g1.x = 42;
140assertEquals(43, assign10(g1));
141assertEquals(43, g1.x);
142
143assertEquals(43, assign10(g2));
144assertEquals(43, g2.x);
145
146// Test deopt because of a failed map check on the store.
147// The binary operation changes the map as a side effect.
148o = {x:0};
149var g3 = { valueOf: function() { o.y = "bar"; return 42; }};
150function assign11(p) { return p.x += 1; }
151
152for (var i = 0; i < 5; i++) {
153  o.x = "a";
154  assign11(o);
155}
156%OptimizeFunctionOnNextCall(assign11);
157o.x = "a";
158assign11(o);
159
160assertEquals("a11", assign11(o));
161o.x = g3;
162assertEquals(43, assign11(o));
163assertEquals("bar", o.y);
164
165o = [0];
166var g4 = { valueOf: function() { o.y = "bar"; return 42; }};
167function assign12(p) { return p[0] += 1; }
168
169for (var i = 0; i < 5; i++) {
170  o[0] = "a";
171  assign12(o);
172}
173%OptimizeFunctionOnNextCall(assign12);
174o[0] = "a";
175assign12(o);
176
177assertEquals("a11", assign12(o));
178o[0] = g4;
179assertEquals(43, assign12(o));
180assertEquals("bar", o.y);
181