Home | History | Annotate | Download | only in compiler
      1 // Copyright 2013 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 // Presents negative opportunities for dead loop removal.
     31 
     32 function loop1() {
     33   while (true) return;
     34 }
     35 
     36 function loop2() {
     37   var i = 0;
     38   while (i++ < 10) ;
     39   return i;  // value of {i} escapes.
     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];
     61   }
     62   return sum;  // {sum} escapes.
     63   // can only remove the loop by figuring out that all elements of {a} are 0.
     64 }
     65 
     66 function loop6(a) {
     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();
     89   %OptimizeFunctionOnNextCall(f);
     90   f();
     91 }
     92 
     93 for (var i = 0; i < params_loops.length; i++) {
     94   var f = params_loops[i];
     95   f(3);
     96   f(7);
     97   f(11);
     98   %OptimizeFunctionOnNextCall(f);
     99   f(9);
    100 }
    101