1// Copyright 2011 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 --expose-gc
29
30var a = new Int32Array(1024);
31
32// Test that we do not assert if the accessed index has not an int32 rep.
33var v = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
34function test_do_not_assert_on_non_int32(vector, base) {
35  var r = 0;
36  var a1 = base + 1;
37  var a2 = base + 2;
38  var a3 = base + 3;
39  var a4 = base + 4;
40  if (a1 == 2) {
41    r += vector[a1];
42    r += vector[a4];
43    r += vector[a2];
44    r += vector[a3];
45  }
46  return r;
47}
48test_do_not_assert_on_non_int32(v,1);
49test_do_not_assert_on_non_int32(v,1);
50test_do_not_assert_on_non_int32(v,"a");
51test_do_not_assert_on_non_int32(v,"a");
52%OptimizeFunctionOnNextCall(test_do_not_assert_on_non_int32);
53test_do_not_assert_on_non_int32(v,0);
54
55function test_base(a, base, condition) {
56  a[base + 1] = 1;
57  a[base + 4] = 2;
58  a[base + 3] = 3;
59  a[base + 2] = 4;
60  a[base + 4] = base + 4;
61  if (condition) {
62    a[base + 1] = 1;
63    a[base + 2] = 2;
64    a[base + 2] = 3;
65    a[base + 2] = 4;
66    a[base + 4] = base + 4;
67  } else {
68    a[base + 6] = 1;
69    a[base + 4] = 2;
70    a[base + 3] = 3;
71    a[base + 2] = 4;
72    a[base + 4] = base - 4;
73  }
74}
75
76function check_test_base(a, base, condition) {
77  if (condition) {
78    assertEquals(1, a[base + 1]);
79    assertEquals(4, a[base + 2]);
80    assertEquals(base + 4, a[base + 4]);
81  } else {
82    assertEquals(1, a[base + 6]);
83    assertEquals(3, a[base + 3]);
84    assertEquals(4, a[base + 2]);
85    assertEquals(base - 4, a[base + 4]);
86  }
87}
88
89
90test_base(a, 1, true);
91test_base(a, 2, true);
92test_base(a, 1, false);
93test_base(a, 2, false);
94%OptimizeFunctionOnNextCall(test_base);
95test_base(a, 3, true);
96check_test_base(a, 3, true);
97test_base(a, 3, false);
98check_test_base(a, 3, false);
99
100// Test that we deopt on failed bounds checks.
101var dictionary_map_array = new Int32Array(128);
102test_base(dictionary_map_array, 5, true);
103test_base(dictionary_map_array, 6, true);
104test_base(dictionary_map_array, 5, false);
105test_base(dictionary_map_array, 6, false);
106%OptimizeFunctionOnNextCall(test_base);
107test_base(dictionary_map_array, -2, true);
108assertUnoptimized(test_base);
109
110// Forget about the dictionary_map_array's map.
111%ClearFunctionTypeFeedback(test_base);
112
113test_base(a, 5, true);
114test_base(a, 6, true);
115test_base(a, 5, false);
116test_base(a, 6, false);
117%OptimizeFunctionOnNextCall(test_base);
118test_base(a, 2048, true);
119assertUnoptimized(test_base);
120
121function test_minus(base,cond) {
122  a[base - 1] = 1;
123  a[base - 2] = 2;
124  a[base + 4] = 3;
125  a[base] = 4;
126  a[base + 4] = base + 4;
127  if (cond) {
128    a[base - 4] = 1;
129    a[base + 5] = 2;
130    a[base + 3] = 3;
131    a[base + 2] = 4;
132    a[base + 4] = base + 4;
133  } else {
134    a[base + 6] = 1;
135    a[base + 4] = 2;
136    a[base + 3] = 3;
137    a[base + 2] = 4;
138    a[base + 4] = base - 4;
139  }
140}
141
142function check_test_minus(base,cond) {
143  if (cond) {
144    assertEquals(2, a[base + 5]);
145    assertEquals(3, a[base + 3]);
146    assertEquals(4, a[base + 2]);
147    assertEquals(base + 4, a[base + 4]);
148  } else {
149    assertEquals(1, a[base + 6]);
150    assertEquals(3, a[base + 3]);
151    assertEquals(4, a[base + 2]);
152    assertEquals(base - 4, a[base + 4]);
153  }
154}
155
156test_minus(5,true);
157test_minus(6,true);
158%OptimizeFunctionOnNextCall(test_minus);
159test_minus(7,true);
160check_test_minus(7,true);
161test_minus(7,false);
162check_test_minus(7,false);
163
164// Specific test on negative offsets.
165var short_a = new Array(100);
166for (var i = 0; i < short_a.length; i++) short_a[i] = 0;
167function short_test(a, i) {
168  a[i + 9] = 0;
169  a[i - 10] = 0;
170}
171short_test(short_a, 50);
172short_test(short_a, 50);
173%OptimizeFunctionOnNextCall(short_test);
174short_a.length = 10;
175short_test(short_a, 0);
176assertUnoptimized(test_base);
177
178
179// A test for when we would modify a phi index.
180var data_phi = [0, 1, 2, 3, 4, 5, 6, 7, 8];
181function test_phi(a, base, check) {
182  var index;
183  if (check) {
184    index = base + 1;
185  } else {
186    index = base + 2;
187  }
188  var result = a[index];
189  result += a[index + 1];
190  result += a[index - 1];
191  return result;
192}
193var result_phi = 0;
194result_phi = test_phi(data_phi, 3,  true);
195assertEquals(12, result_phi);
196result_phi = test_phi(data_phi, 3,  true);
197assertEquals(12, result_phi);
198%OptimizeFunctionOnNextCall(test_phi);
199result_phi = test_phi(data_phi, 3,  true);
200assertEquals(12, result_phi);
201
202
203// A test for recursive decomposition
204var data_composition_long = [0, 1, 2, 3, 4, 5, 6, 7, 8];
205var data_composition_short = [0, 1, 2, 3, 4];
206function test_composition(a, base0, check) {
207  var base1 = ((base0 + 2));
208  var base2 = ((base1 + 8) >> 2);
209  var base3 = ((base2 + 6) >> 1);
210  var base4 = ((base3 + 8) >> 1);
211
212  var result = 0;
213  result += a[base0];
214  result += a[base1];
215  result += a[base2];
216  result += a[base3];
217  result += a[base4];
218
219  return result;
220}
221var result_composition = 0;
222result_composition = test_composition(data_composition_long, 2);
223assertEquals(19, result_composition);
224result_composition = test_composition(data_composition_long, 2);
225assertEquals(19, result_composition);
226%OptimizeFunctionOnNextCall(test_composition);
227result_composition = test_composition(data_composition_short, 2);
228assertEquals(NaN, result_composition);
229
230
231gc();
232