OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 | 28 // Flags: --allow-natives-syntax |
29 | 29 |
30 // Test usage of static type information for loads that would otherwise | 30 // Presents negative opportunities for dead loop removal. |
31 // turn into polymorphic or generic loads. | |
32 | 31 |
33 // Prepare a highly polymorphic load to be used by all tests. | 32 function loop1() { |
34 Object.prototype.load = function() { return this.property; }; | 33 while (true) return; |
35 Object.prototype.load.call({ A:0, property:10 }); | 34 } |
36 Object.prototype.load.call({ A:0, B:0, property:11 }); | |
37 Object.prototype.load.call({ A:0, B:0, C:0, property:12 }); | |
38 Object.prototype.load.call({ A:0, B:0, C:0, D:0, property:13 }); | |
39 Object.prototype.load.call({ A:0, B:0, C:0, D:0, E:0, property:14 }); | |
40 Object.prototype.load.call({ A:0, B:0, C:0, D:0, E:0, F:0, property:15 }); | |
41 | 35 |
42 // Test for object literals. | 36 function loop2() { |
43 (function() { | 37 var i = 0; |
44 function f(x) { | 38 while (i++ < 10) ; |
45 var object = { property:x }; | 39 return i; // value of {i} escapes. |
46 return object.load(); | 40 // can only remove the loop with induction variable analysis. |
| 41 } |
| 42 |
| 43 function loop3() { |
| 44 var i = 0; |
| 45 for (; i < 10; i++) ; |
| 46 return i; // value of {i} escapes. |
| 47 // can only remove the loop with induction variable analysis. |
| 48 } |
| 49 |
| 50 function loop4() { |
| 51 var a = 0; |
| 52 for (var i = 0; i < 10; i++) a++; |
| 53 return a; // value of {a} escapes. |
| 54 // can only remove the loop with induction variable analysis. |
| 55 } |
| 56 |
| 57 function loop5() { |
| 58 var a = new Int32Array(4), sum = 0; |
| 59 for (var i = 0; i < a.length; i++) { |
| 60 sum += a[i]; |
47 } | 61 } |
| 62 return sum; // {sum} escapes. |
| 63 // can only remove the loop by figuring out that all elements of {a} are 0. |
| 64 } |
48 | 65 |
49 assertSame(1, f(1)); | 66 function loop6(a) { |
50 assertSame(2, f(2)); | 67 for (var i = 0; i < a; i++) ; // implicit a.valueOf(). |
| 68 // can only remove the loop by guarding on the type of a. |
| 69 } |
| 70 |
| 71 function loop7(a) { |
| 72 for (var i = 0; i < 10; i++) a.toString(); // unknown side-effect on a. |
| 73 // can only remove the loop by guarding on the type of a. |
| 74 } |
| 75 |
| 76 function loop8(a) { |
| 77 for (var i = 0; i < 10; i++) a.valueOf(); // unknown side-effect on a. |
| 78 // can only remove the loop by guarding on the type of a. |
| 79 } |
| 80 |
| 81 var no_params_loops = [loop1, loop2, loop3, loop4, loop5, loop6]; |
| 82 var params_loops = [loop6, loop7, loop8]; |
| 83 |
| 84 for (var i = 0; i < no_params_loops.length; i++) { |
| 85 var f = no_params_loops[i]; |
| 86 f(); |
| 87 f(); |
| 88 f(); |
51 %OptimizeFunctionOnNextCall(f); | 89 %OptimizeFunctionOnNextCall(f); |
52 assertSame(3, f(3)); | 90 f(); |
53 })(); | 91 } |
54 | 92 |
55 // Test for inlined constructors. | 93 for (var i = 0; i < params_loops.length; i++) { |
56 (function() { | 94 var f = params_loops[i]; |
57 function c(x) { | 95 f(3); |
58 this.property = x; | 96 f(7); |
59 } | 97 f(11); |
60 function f(x) { | |
61 var object = new c(x); | |
62 return object.load(); | |
63 } | |
64 | |
65 assertSame(1, f(1)); | |
66 assertSame(2, f(2)); | |
67 %OptimizeFunctionOnNextCall(f); | 98 %OptimizeFunctionOnNextCall(f); |
68 assertSame(3, f(3)); | 99 f(9); |
69 })(); | 100 } |
OLD | NEW |