| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --allow-natives-syntax --smi-only-arrays --expose-gc | 28 // Flags: --allow-natives-syntax --expose-gc |
| 29 // Flags: --noalways-opt | 29 // Flags: --noalways-opt |
| 30 | 30 |
| 31 // Test element kind of objects. | 31 // Test element kind of objects. |
| 32 // Since --smi-only-arrays affects builtins, its default setting at compile | |
| 33 // time sticks if built with snapshot. If --smi-only-arrays is deactivated | |
| 34 // by default, only a no-snapshot build actually has smi-only arrays enabled | |
| 35 // in this test case. Depending on whether smi-only arrays are actually | |
| 36 // enabled, this test takes the appropriate code path to check smi-only arrays. | |
| 37 | |
| 38 // support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8)); | |
| 39 support_smi_only_arrays = true; | |
| 40 | |
| 41 if (support_smi_only_arrays) { | |
| 42 print("Tests include smi-only arrays."); | |
| 43 } else { | |
| 44 print("Tests do NOT include smi-only arrays."); | |
| 45 } | |
| 46 | 32 |
| 47 var elements_kind = { | 33 var elements_kind = { |
| 48 fast_smi_only : 'fast smi only elements', | 34 fast_smi_only : 'fast smi only elements', |
| 49 fast : 'fast elements', | 35 fast : 'fast elements', |
| 50 fast_double : 'fast double elements', | 36 fast_double : 'fast double elements', |
| 51 dictionary : 'dictionary elements', | 37 dictionary : 'dictionary elements', |
| 52 external_byte : 'external byte elements', | 38 external_byte : 'external byte elements', |
| 53 external_unsigned_byte : 'external unsigned byte elements', | 39 external_unsigned_byte : 'external unsigned byte elements', |
| 54 external_short : 'external short elements', | 40 external_short : 'external short elements', |
| 55 external_unsigned_short : 'external unsigned short elements', | 41 external_unsigned_short : 'external unsigned short elements', |
| (...skipping 10 matching lines...) Expand all Loading... |
| 66 if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; | 52 if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; |
| 67 if (%HasDictionaryElements(obj)) return elements_kind.dictionary; | 53 if (%HasDictionaryElements(obj)) return elements_kind.dictionary; |
| 68 } | 54 } |
| 69 | 55 |
| 70 function isHoley(obj) { | 56 function isHoley(obj) { |
| 71 if (%HasFastHoleyElements(obj)) return true; | 57 if (%HasFastHoleyElements(obj)) return true; |
| 72 return false; | 58 return false; |
| 73 } | 59 } |
| 74 | 60 |
| 75 function assertKind(expected, obj, name_opt) { | 61 function assertKind(expected, obj, name_opt) { |
| 76 if (!support_smi_only_arrays && | |
| 77 expected == elements_kind.fast_smi_only) { | |
| 78 expected = elements_kind.fast; | |
| 79 } | |
| 80 assertEquals(expected, getKind(obj), name_opt); | 62 assertEquals(expected, getKind(obj), name_opt); |
| 81 } | 63 } |
| 82 | 64 |
| 83 if (support_smi_only_arrays) { | 65 // Test: If a call site goes megamorphic, it retains the ability to |
| 66 // use allocation site feedback (if FLAG_allocation_site_pretenuring |
| 67 // is on). |
| 68 (function() { |
| 69 function bar(t, len) { |
| 70 return new t(len); |
| 71 } |
| 84 | 72 |
| 85 // Test: If a call site goes megamorphic, it retains the ability to | 73 a = bar(Array, 10); |
| 86 // use allocation site feedback (if FLAG_allocation_site_pretenuring | 74 a[0] = 3.5; |
| 87 // is on). | 75 b = bar(Array, 1); |
| 88 (function() { | 76 assertKind(elements_kind.fast_double, b); |
| 89 function bar(t, len) { | 77 c = bar(Object, 3); |
| 90 return new t(len); | 78 b = bar(Array, 10); |
| 91 } | 79 // TODO(mvstanton): re-enable when FLAG_allocation_site_pretenuring |
| 92 | 80 // is on in the build. |
| 93 a = bar(Array, 10); | 81 // assertKind(elements_kind.fast_double, b); |
| 94 a[0] = 3.5; | 82 })(); |
| 95 b = bar(Array, 1); | |
| 96 assertKind(elements_kind.fast_double, b); | |
| 97 c = bar(Object, 3); | |
| 98 b = bar(Array, 10); | |
| 99 // TODO(mvstanton): re-enable when FLAG_allocation_site_pretenuring | |
| 100 // is on in the build. | |
| 101 // assertKind(elements_kind.fast_double, b); | |
| 102 })(); | |
| 103 | 83 |
| 104 | 84 |
| 105 // Test: ensure that crankshafted array constructor sites are deopted | 85 // Test: ensure that crankshafted array constructor sites are deopted |
| 106 // if another function is used. | 86 // if another function is used. |
| 107 (function() { | 87 (function() { |
| 108 function bar0(t) { | 88 function bar0(t) { |
| 109 return new t(); | 89 return new t(); |
| 110 } | 90 } |
| 111 a = bar0(Array); | 91 a = bar0(Array); |
| 112 a[0] = 3.5; | 92 a[0] = 3.5; |
| 113 b = bar0(Array); | 93 b = bar0(Array); |
| 114 assertKind(elements_kind.fast_double, b); | 94 assertKind(elements_kind.fast_double, b); |
| 115 %OptimizeFunctionOnNextCall(bar0); | 95 %OptimizeFunctionOnNextCall(bar0); |
| 116 b = bar0(Array); | 96 b = bar0(Array); |
| 117 assertKind(elements_kind.fast_double, b); | 97 assertKind(elements_kind.fast_double, b); |
| 98 assertOptimized(bar0); |
| 99 // bar0 should deopt |
| 100 b = bar0(Object); |
| 101 assertUnoptimized(bar0) |
| 102 // When it's re-optimized, we should call through the full stub |
| 103 bar0(Array); |
| 104 %OptimizeFunctionOnNextCall(bar0); |
| 105 b = bar0(Array); |
| 106 // This only makes sense to test if we allow crankshafting |
| 107 if (4 != %GetOptimizationStatus(bar0)) { |
| 108 // We also lost our ability to record kind feedback, as the site |
| 109 // is megamorphic now. |
| 110 assertKind(elements_kind.fast_smi_only, b); |
| 118 assertOptimized(bar0); | 111 assertOptimized(bar0); |
| 119 // bar0 should deopt | 112 b[0] = 3.5; |
| 120 b = bar0(Object); | 113 c = bar0(Array); |
| 121 assertUnoptimized(bar0) | 114 assertKind(elements_kind.fast_smi_only, c); |
| 122 // When it's re-optimized, we should call through the full stub | 115 } |
| 123 bar0(Array); | 116 })(); |
| 124 %OptimizeFunctionOnNextCall(bar0); | |
| 125 b = bar0(Array); | |
| 126 // This only makes sense to test if we allow crankshafting | |
| 127 if (4 != %GetOptimizationStatus(bar0)) { | |
| 128 // We also lost our ability to record kind feedback, as the site | |
| 129 // is megamorphic now. | |
| 130 assertKind(elements_kind.fast_smi_only, b); | |
| 131 assertOptimized(bar0); | |
| 132 b[0] = 3.5; | |
| 133 c = bar0(Array); | |
| 134 assertKind(elements_kind.fast_smi_only, c); | |
| 135 } | |
| 136 })(); | |
| 137 | 117 |
| 138 | 118 |
| 139 // Test: Ensure that inlined array calls in crankshaft learn from deopts | 119 // Test: Ensure that inlined array calls in crankshaft learn from deopts |
| 140 // based on the move to a dictionary for the array. | 120 // based on the move to a dictionary for the array. |
| 141 (function() { | 121 (function() { |
| 142 function bar(len) { | 122 function bar(len) { |
| 143 return new Array(len); | 123 return new Array(len); |
| 144 } | 124 } |
| 145 a = bar(10); | 125 a = bar(10); |
| 146 a[0] = "a string"; | 126 a[0] = "a string"; |
| 147 a = bar(10); | 127 a = bar(10); |
| 148 assertKind(elements_kind.fast, a); | 128 assertKind(elements_kind.fast, a); |
| 149 %OptimizeFunctionOnNextCall(bar); | 129 %OptimizeFunctionOnNextCall(bar); |
| 150 a = bar(10); | 130 a = bar(10); |
| 151 assertKind(elements_kind.fast, a); | 131 assertKind(elements_kind.fast, a); |
| 152 assertOptimized(bar); | 132 assertOptimized(bar); |
| 153 a = bar(100000); | 133 a = bar(100000); |
| 154 assertKind(elements_kind.dictionary, a); | 134 assertKind(elements_kind.dictionary, a); |
| 155 assertOptimized(bar); | 135 assertOptimized(bar); |
| 156 | 136 |
| 157 // If the argument isn't a smi, things should still work. | 137 // If the argument isn't a smi, things should still work. |
| 158 a = bar("oops"); | 138 a = bar("oops"); |
| 159 assertOptimized(bar); | 139 assertOptimized(bar); |
| 160 assertKind(elements_kind.fast, a); | 140 assertKind(elements_kind.fast, a); |
| 161 | 141 |
| 162 function barn(one, two, three) { | 142 function barn(one, two, three) { |
| 163 return new Array(one, two, three); | 143 return new Array(one, two, three); |
| 164 } | 144 } |
| 165 | 145 |
| 166 barn(1, 2, 3); | 146 barn(1, 2, 3); |
| 167 barn(1, 2, 3); | 147 barn(1, 2, 3); |
| 168 %OptimizeFunctionOnNextCall(barn); | 148 %OptimizeFunctionOnNextCall(barn); |
| 169 barn(1, 2, 3); | 149 barn(1, 2, 3); |
| 170 assertOptimized(barn); | 150 assertOptimized(barn); |
| 171 a = barn(1, "oops", 3); | 151 a = barn(1, "oops", 3); |
| 172 assertOptimized(barn); | 152 assertOptimized(barn); |
| 173 })(); | 153 })(); |
| 174 | 154 |
| 175 | 155 |
| 176 // Test: When a method with array constructor is crankshafted, the type | 156 // Test: When a method with array constructor is crankshafted, the type |
| 177 // feedback for elements kind is baked in. Verify that transitions don't | 157 // feedback for elements kind is baked in. Verify that transitions don't |
| 178 // change it anymore | 158 // change it anymore |
| 179 (function() { | 159 (function() { |
| 180 function bar() { | 160 function bar() { |
| 181 return new Array(); | 161 return new Array(); |
| 182 } | 162 } |
| 183 a = bar(); | 163 a = bar(); |
| 184 bar(); | 164 bar(); |
| 185 %OptimizeFunctionOnNextCall(bar); | 165 %OptimizeFunctionOnNextCall(bar); |
| 186 b = bar(); | 166 b = bar(); |
| 187 // This only makes sense to test if we allow crankshafting | 167 // This only makes sense to test if we allow crankshafting |
| 188 if (4 != %GetOptimizationStatus(bar)) { | 168 if (4 != %GetOptimizationStatus(bar)) { |
| 189 assertOptimized(bar); | 169 assertOptimized(bar); |
| 190 %DebugPrint(3); | 170 %DebugPrint(3); |
| 191 b[0] = 3.5; | 171 b[0] = 3.5; |
| 192 c = bar(); | 172 c = bar(); |
| 193 assertKind(elements_kind.fast_smi_only, c); | 173 assertKind(elements_kind.fast_smi_only, c); |
| 194 assertOptimized(bar); | 174 assertOptimized(bar); |
| 195 } | 175 } |
| 196 })(); | 176 })(); |
| 197 | 177 |
| 198 | 178 |
| 199 // Test: create arrays in two contexts, verifying that the correct | 179 // Test: create arrays in two contexts, verifying that the correct |
| 200 // map for Array in that context will be used. | 180 // map for Array in that context will be used. |
| 201 (function() { | 181 (function() { |
| 202 function bar() { return new Array(); } | 182 function bar() { return new Array(); } |
| 203 bar(); | 183 bar(); |
| 204 bar(); | 184 bar(); |
| 205 %OptimizeFunctionOnNextCall(bar); | 185 %OptimizeFunctionOnNextCall(bar); |
| 206 a = bar(); | 186 a = bar(); |
| 207 assertTrue(a instanceof Array); | 187 assertTrue(a instanceof Array); |
| 208 | 188 |
| 209 var contextB = Realm.create(); | 189 var contextB = Realm.create(); |
| 210 Realm.eval(contextB, "function bar2() { return new Array(); };"); | 190 Realm.eval(contextB, "function bar2() { return new Array(); };"); |
| 211 Realm.eval(contextB, "bar2(); bar2();"); | 191 Realm.eval(contextB, "bar2(); bar2();"); |
| 212 Realm.eval(contextB, "%OptimizeFunctionOnNextCall(bar2);"); | 192 Realm.eval(contextB, "%OptimizeFunctionOnNextCall(bar2);"); |
| 213 Realm.eval(contextB, "bar2();"); | 193 Realm.eval(contextB, "bar2();"); |
| 214 assertFalse(Realm.eval(contextB, "bar2();") instanceof Array); | 194 assertFalse(Realm.eval(contextB, "bar2();") instanceof Array); |
| 215 assertTrue(Realm.eval(contextB, "bar2() instanceof Array")); | 195 assertTrue(Realm.eval(contextB, "bar2() instanceof Array")); |
| 216 })(); | 196 })(); |
| 217 | 197 |
| 218 // Test: create array with packed feedback, then optimize function, which | 198 // Test: create array with packed feedback, then optimize function, which |
| 219 // should deal with arguments that create holey arrays. | 199 // should deal with arguments that create holey arrays. |
| 220 (function() { | 200 (function() { |
| 221 function bar(len) { return new Array(len); } | 201 function bar(len) { return new Array(len); } |
| 222 bar(0); | 202 bar(0); |
| 223 bar(0); | 203 bar(0); |
| 224 %OptimizeFunctionOnNextCall(bar); | 204 %OptimizeFunctionOnNextCall(bar); |
| 225 a = bar(0); | 205 a = bar(0); |
| 226 assertOptimized(bar); | 206 assertOptimized(bar); |
| 207 assertFalse(isHoley(a)); |
| 208 a = bar(1); // ouch! |
| 209 assertOptimized(bar); |
| 210 assertTrue(isHoley(a)); |
| 211 a = bar(100); |
| 212 assertTrue(isHoley(a)); |
| 213 a = bar(0); |
| 214 assertOptimized(bar); |
| 215 // Crankshafted functions don't use mementos, so feedback still |
| 216 // indicates a packed array is desired. (unless --nocrankshaft is in use). |
| 217 if (4 != %GetOptimizationStatus(bar)) { |
| 227 assertFalse(isHoley(a)); | 218 assertFalse(isHoley(a)); |
| 228 a = bar(1); // ouch! | 219 } |
| 229 assertOptimized(bar); | 220 })(); |
| 230 assertTrue(isHoley(a)); | |
| 231 a = bar(100); | |
| 232 assertTrue(isHoley(a)); | |
| 233 a = bar(0); | |
| 234 assertOptimized(bar); | |
| 235 // Crankshafted functions don't use mementos, so feedback still | |
| 236 // indicates a packed array is desired. (unless --nocrankshaft is in use). | |
| 237 if (4 != %GetOptimizationStatus(bar)) { | |
| 238 assertFalse(isHoley(a)); | |
| 239 } | |
| 240 })(); | |
| 241 } | |
| OLD | NEW |