Home | History | Annotate | Download | only in compiler
      1 // Copyright 2014 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 --noalways-opt
     29 
     30 var global = this;
     31 
     32 Array.prototype.f = function() {
     33   return 0;
     34 };
     35 
     36 (function() {
     37   var called = 0;
     38 
     39   function g(x, y, called) {
     40     return called + 1;
     41   }
     42 
     43   function f(deopt, called) {
     44     return g([].f.call({}), deopt + 1, called);
     45   }
     46 
     47   called = f(0, called);
     48   called = f(0, called);
     49   %OptimizeFunctionOnNextCall(f);
     50   called = f(0, called);
     51   assertOptimized(f);
     52   called = f({}, called);
     53   assertUnoptimized(f);
     54   assertEquals(4, called);
     55 })();
     56 
     57 (function() {
     58   // The array built-ins are only inlined if the receiver is a
     59   // HConstant, this seems to require a *unique* global identifier
     60   // each time.
     61   global.a1 = [1,2,3,4];
     62   var obj = {value: 3};
     63 
     64   function f(b) {
     65     return [].pop.call(a1) + b.value;
     66   }
     67 
     68   assertEquals(7, f(obj));
     69   assertEquals(6, f(obj));
     70   %OptimizeFunctionOnNextCall(f);
     71   assertEquals(5, f(obj));
     72   assertOptimized(f);
     73   assertEquals(4, f({d: 0, value: 3}));
     74   assertUnoptimized(f);
     75   assertEquals(0, a1.length);
     76 })();
     77 
     78 
     79 (function() {
     80   global.a2 = [1,2,3,4];
     81   var obj = {value: 3};
     82 
     83   function f(b) {
     84     return [].shift.call(a2) + b.value;
     85   }
     86 
     87   assertEquals(4, f(obj));
     88   assertEquals(5, f(obj));
     89   %OptimizeFunctionOnNextCall(f);
     90   assertEquals(6, f(obj));
     91   assertOptimized(f);
     92   assertEquals(7, f({d: 0, value: 3}));
     93   assertUnoptimized(f);
     94   assertEquals(0, a2.length);
     95 })();
     96 
     97 (function() {
     98   global.a3 = [1,2,3,4];
     99   var obj = {value: 3};
    100 
    101   function f(b) {
    102     return [].push.call(a3, b.value);
    103   }
    104 
    105   assertEquals(5, f(obj));
    106   assertEquals(6, f(obj));
    107   %OptimizeFunctionOnNextCall(f);
    108   assertEquals(7, f(obj));
    109   assertOptimized(f);
    110   assertEquals(8, f({d: 0, value: 3}));
    111   assertUnoptimized(f);
    112   assertEquals(8, a3.length);
    113   assertEquals(3, a3[7]);
    114 })();
    115 
    116 (function() {
    117   global.a4 = [1,2,3,4];
    118   var obj = {value: 3};
    119 
    120   function f(b) {
    121     return [].indexOf.call(a4, b.value);
    122   }
    123 
    124   f(obj);
    125   f(obj);
    126   %OptimizeFunctionOnNextCall(f);
    127   var index1 = f(obj);
    128   assertOptimized(f);
    129   var index2 = f({d: 0, value: 3});
    130   assertUnoptimized(f);
    131 
    132   assertEquals(2, index1);
    133   assertEquals(index1, index2);
    134 })();
    135 
    136 (function() {
    137   global.a5 = [1,2,3,4];
    138   var obj = {value: 3};
    139 
    140   function f(b) {
    141     return [].lastIndexOf.call(a5, b.value);
    142   }
    143 
    144   f(obj);
    145   f(obj);
    146   %OptimizeFunctionOnNextCall(f);
    147   var index1 = f(obj);
    148   assertOptimized(f);
    149   var index2 = f({d: 0, value: 3});
    150   assertUnoptimized(f);
    151 
    152   assertEquals(2, index1);
    153   assertEquals(index1, index2);
    154 })();
    155