inline-arguments.js 8.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Flags: --allow-natives-syntax
29

30 31
function A() {
}
32

33 34 35 36 37 38
A.prototype.X = function (a, b, c) {
  assertTrue(this instanceof A);
  assertEquals(1, a);
  assertEquals(2, b);
  assertEquals(3, c);
};
39

40 41 42 43 44 45 46 47 48
A.prototype.Y = function () {
  this.X.apply(this, arguments);
};

A.prototype.Z = function () {
  this.Y(1,2,3);
};

var a = new A();
49
%PrepareFunctionForOptimization(a.Z);
50 51 52 53 54 55 56 57
a.Z(4,5,6);
a.Z(4,5,6);
%OptimizeFunctionOnNextCall(a.Z);
a.Z(4,5,6);
A.prototype.X.apply = function (receiver, args) {
  return Function.prototype.apply.call(this, receiver, args);
};
a.Z(4,5,6);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78


// Ensure that HArgumentsObject is inserted in a correct place
// and dominates all uses.
function F1() { }
function F2() { F1.apply(this, arguments); }
function F3(x, y) {
  if (x) {
    F2(y);
  }
}

function F31() {
  return F1.apply(this, arguments);
}

function F4() {
  F3(true, false);
  return F31(1);
}

79
%PrepareFunctionForOptimization(F4);
80 81 82 83 84
F4(1);
F4(1);
F4(1);
%OptimizeFunctionOnNextCall(F4);
F4(1);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112


// Test correct adapation of arguments.
// Strict mode prevents arguments object from shadowing parameters.
(function () {
  "use strict";

  function G2() {
    assertArrayEquals([1,2], arguments);
  }

  function G4() {
    assertArrayEquals([1,2,3,4], arguments);
  }

  function adapt2to4(a, b, c, d) {
    G2.apply(this, arguments);
  }

  function adapt4to2(a, b) {
    G4.apply(this, arguments);
  }

  function test_adaptation() {
    adapt2to4(1, 2);
    adapt4to2(1, 2, 3, 4);
  }

113
  %PrepareFunctionForOptimization(test_adaptation);
114 115 116 117 118
  test_adaptation();
  test_adaptation();
  %OptimizeFunctionOnNextCall(test_adaptation);
  test_adaptation();
})();
119 120

// Test arguments access from the inlined function.
121
%NeverOptimizeFunction(uninlinable);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
function uninlinable(v) {
  assertEquals(0, v);
  return 0;
}

function toarr_inner() {
  var a = arguments;
  var marker = a[0];
  uninlinable(uninlinable(0, 0), marker.x);

  var r = new Array();
  for (var i = a.length - 1; i >= 1; i--) {
    r.push(a[i]);
  }

  return r;
}

function toarr1(marker, a, b, c) {
  return toarr_inner(marker, a / 2, b / 2, c / 2);
}

function toarr2(marker, a, b, c) {
  var x = 0;
  return uninlinable(uninlinable(0, 0),
                     x = toarr_inner(marker, a / 2, b / 2, c / 2)), x;
}

function test_toarr(toarr) {
  var marker = { x: 0 };
152
  %PrepareFunctionForOptimization(toarr);
153 154 155 156 157 158 159 160 161 162 163
  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
  %OptimizeFunctionOnNextCall(toarr);
  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
  delete marker.x;
  assertArrayEquals([3, 2, 1], toarr(marker, 2, 4, 6));
}

test_toarr(toarr1);
test_toarr(toarr2);

164

165
// Test that arguments access from inlined function uses correct values.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
(function () {
  function inner(x, y) {
    "use strict";
    x = 10;
    y = 20;
    for (var i = 0; i < 1; i++) {
      for (var j = 1; j <= arguments.length; j++) {
        return arguments[arguments.length - j];
      }
    }
  }

  function outer(x, y) {
    return inner(x, y);
  }

182
  %PrepareFunctionForOptimization(outer);
183
  %OptimizeFunctionOnNextCall(outer);
184 185
  assertEquals(2, outer(1, 2));
  %PrepareFunctionForOptimization(inner);
186 187 188 189 190
  %OptimizeFunctionOnNextCall(inner);
  assertEquals(2, outer(1, 2));
})();


191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
(function () {
  function inner(x, y) {
    "use strict";
    x = 10;
    y = 20;
    for (var i = 0; i < 1; i++) {
      for (var j = 1; j <= arguments.length; j++) {
        return arguments[arguments.length - j];
      }
    }
  }

  function outer(x, y) {
    return inner(x, y);
  }

207
  %PrepareFunctionForOptimization(outer);
208 209 210 211 212 213
  assertEquals(2, outer(1, 2));
  assertEquals(2, outer(1, 2));
  assertEquals(2, outer(1, 2));
  %OptimizeFunctionOnNextCall(outer);
  assertEquals(2, outer(1, 2));
})();
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252


// Test inlining and deoptimization of functions accessing and modifying
// the arguments object in strict mode with mismatched arguments count.
(function () {
  "use strict";
  function test(outerCount, middleCount, innerCount) {
    var forceDeopt = { deopt:false };
    function inner(x,y) {
      x = 0; y = 0;
      forceDeopt.deopt;
      assertSame(innerCount, arguments.length);
      for (var i = 0; i < arguments.length; i++) {
        assertSame(30 + i, arguments[i]);
      }
    }

    function middle(x,y) {
      x = 0; y = 0;
      if (innerCount == 1) inner(30);
      if (innerCount == 2) inner(30, 31);
      if (innerCount == 3) inner(30, 31, 32);
      assertSame(middleCount, arguments.length);
      for (var i = 0; i < arguments.length; i++) {
        assertSame(20 + i, arguments[i]);
      }
    }

    function outer(x,y) {
      x = 0; y = 0;
      if (middleCount == 1) middle(20);
      if (middleCount == 2) middle(20, 21);
      if (middleCount == 3) middle(20, 21, 22);
      assertSame(outerCount, arguments.length);
      for (var i = 0; i < arguments.length; i++) {
        assertSame(10 + i, arguments[i]);
      }
    }

253
    %PrepareFunctionForOptimization(outer);
254 255 256 257 258 259 260 261 262 263 264
    for (var step = 0; step < 4; step++) {
      if (outerCount == 1) outer(10);
      if (outerCount == 2) outer(10, 11);
      if (outerCount == 3) outer(10, 11, 12);
      if (step == 1) %OptimizeFunctionOnNextCall(outer);
      if (step == 2) delete forceDeopt.deopt;
    }

    %DeoptimizeFunction(outer);
    %DeoptimizeFunction(middle);
    %DeoptimizeFunction(inner);
265 266 267
    %ClearFunctionFeedback(outer);
    %ClearFunctionFeedback(middle);
    %ClearFunctionFeedback(inner);
268 269 270 271 272 273 274 275 276 277
  }

  for (var a = 1; a <= 3; a++) {
    for (var b = 1; b <= 3; b++) {
      for (var c = 1; c <= 3; c++) {
        test(a,b,c);
      }
    }
  }
})();
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313


// Test materialization of arguments object with values in registers.
(function () {
  "use strict";
  var forceDeopt = { deopt:false };
  function inner(a,b,c,d,e,f,g,h,i,j) {
    var args = arguments;
    forceDeopt.deopt;
    assertSame(10, args.length);
    assertSame(a, args[0]);
    assertSame(b, args[1]);
    assertSame(c, args[2]);
    assertSame(d, args[3]);
    assertSame(e, args[4]);
    assertSame(f, args[5]);
    assertSame(g, args[6]);
    assertSame(h, args[7]);
    assertSame(i, args[8]);
    assertSame(j, args[9]);
  }

  var a = 0.5;
  var b = 1.7;
  var c = 123;
  function outer() {
    inner(
      a - 0.3,  // double in double register
      b + 2.3,  // integer in double register
      c + 321,  // integer in general register
      c - 456,  // integer in stack slot
      a + 0.1, a + 0.2, a + 0.3, a + 0.4, a + 0.5,
      a + 0.6   // double in stack slot
    );
  }

314
  %PrepareFunctionForOptimization(outer);
315 316 317 318 319 320 321
  outer();
  outer();
  %OptimizeFunctionOnNextCall(outer);
  outer();
  delete forceDeopt.deopt;
  outer();
})();