Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
| 30 | var a = new Int32Array(1024); |
| 31 | |
| 32 | // Test that we do not assert if the accessed index has not an int32 rep. |
| 33 | var v = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 34 | function 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 | } |
| 48 | test_do_not_assert_on_non_int32(v,1); |
| 49 | test_do_not_assert_on_non_int32(v,1); |
| 50 | test_do_not_assert_on_non_int32(v,"a"); |
| 51 | test_do_not_assert_on_non_int32(v,"a"); |
| 52 | %OptimizeFunctionOnNextCall(test_do_not_assert_on_non_int32); |
| 53 | test_do_not_assert_on_non_int32(v,0); |
| 54 | |
| 55 | function 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 | |
| 76 | function 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 | |
| 90 | test_base(a, 1, true); |
| 91 | test_base(a, 2, true); |
| 92 | test_base(a, 1, false); |
| 93 | test_base(a, 2, false); |
| 94 | %OptimizeFunctionOnNextCall(test_base); |
| 95 | test_base(a, 3, true); |
| 96 | check_test_base(a, 3, true); |
| 97 | test_base(a, 3, false); |
| 98 | check_test_base(a, 3, false); |
| 99 | |
| 100 | // Test that we deopt on failed bounds checks. |
| 101 | var dictionary_map_array = new Int32Array(128); |
| 102 | test_base(dictionary_map_array, 5, true); |
| 103 | test_base(dictionary_map_array, 6, true); |
| 104 | test_base(dictionary_map_array, 5, false); |
| 105 | test_base(dictionary_map_array, 6, false); |
| 106 | %OptimizeFunctionOnNextCall(test_base); |
| 107 | test_base(dictionary_map_array, -2, true); |
| 108 | assertUnoptimized(test_base); |
| 109 | |
| 110 | // Forget about the dictionary_map_array's map. |
| 111 | %ClearFunctionTypeFeedback(test_base); |
| 112 | |
| 113 | test_base(a, 5, true); |
| 114 | test_base(a, 6, true); |
| 115 | test_base(a, 5, false); |
| 116 | test_base(a, 6, false); |
| 117 | %OptimizeFunctionOnNextCall(test_base); |
| 118 | test_base(a, 2048, true); |
| 119 | assertUnoptimized(test_base); |
| 120 | |
| 121 | function 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 | |
| 142 | function 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 | |
| 156 | test_minus(5,true); |
| 157 | test_minus(6,true); |
| 158 | %OptimizeFunctionOnNextCall(test_minus); |
| 159 | test_minus(7,true); |
| 160 | check_test_minus(7,true); |
| 161 | test_minus(7,false); |
| 162 | check_test_minus(7,false); |
| 163 | |
| 164 | // Specific test on negative offsets. |
| 165 | var short_a = new Array(100); |
| 166 | for (var i = 0; i < short_a.length; i++) short_a[i] = 0; |
| 167 | function short_test(a, i) { |
| 168 | a[i + 9] = 0; |
| 169 | a[i - 10] = 0; |
| 170 | } |
| 171 | short_test(short_a, 50); |
| 172 | short_test(short_a, 50); |
| 173 | %OptimizeFunctionOnNextCall(short_test); |
| 174 | short_a.length = 10; |
| 175 | short_test(short_a, 0); |
| 176 | assertUnoptimized(test_base); |
| 177 | |
| 178 | |
| 179 | // A test for when we would modify a phi index. |
| 180 | var data_phi = [0, 1, 2, 3, 4, 5, 6, 7, 8]; |
| 181 | function 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 | } |
| 193 | var result_phi = 0; |
| 194 | result_phi = test_phi(data_phi, 3, true); |
| 195 | assertEquals(12, result_phi); |
| 196 | result_phi = test_phi(data_phi, 3, true); |
| 197 | assertEquals(12, result_phi); |
| 198 | %OptimizeFunctionOnNextCall(test_phi); |
| 199 | result_phi = test_phi(data_phi, 3, true); |
| 200 | assertEquals(12, result_phi); |
| 201 | |
| 202 | |
| 203 | // A test for recursive decomposition |
| 204 | var data_composition_long = [0, 1, 2, 3, 4, 5, 6, 7, 8]; |
| 205 | var data_composition_short = [0, 1, 2, 3, 4]; |
| 206 | function 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 | } |
| 221 | var result_composition = 0; |
| 222 | result_composition = test_composition(data_composition_long, 2); |
| 223 | assertEquals(19, result_composition); |
| 224 | result_composition = test_composition(data_composition_long, 2); |
| 225 | assertEquals(19, result_composition); |
| 226 | %OptimizeFunctionOnNextCall(test_composition); |
| 227 | result_composition = test_composition(data_composition_short, 2); |
| 228 | assertEquals(NaN, result_composition); |
| 229 | |
| 230 | |
| 231 | gc(); |