test-deoptimization.cc 32 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
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 28 29
// 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.

#include <stdlib.h>

30
#include "src/v8.h"
31

32
#include "src/api.h"
33
#include "src/base/platform/platform.h"
34
#include "src/compilation-cache.h"
35
#include "src/debug/debug.h"
36 37
#include "src/deoptimizer.h"
#include "src/isolate.h"
38
#include "src/objects-inl.h"
39
#include "test/cctest/cctest.h"
40

41
using ::v8::base::OS;
42 43
using ::v8::internal::Deoptimizer;
using ::v8::internal::EmbeddedVector;
44 45 46 47
using ::v8::internal::Handle;
using ::v8::internal::Isolate;
using ::v8::internal::JSFunction;
using ::v8::internal::Object;
48 49 50 51

// Size of temp buffer for formatting small strings.
#define SMALL_STRING_BUFFER_SIZE 80

52 53 54
// Utility class to set the following runtime flags when constructed and return
// to their default state when destroyed:
//   --allow-natives-syntax --always-opt --noturbo-inlining --nouse-inlining
55 56 57 58 59
class AlwaysOptimizeAllowNativesSyntaxNoInlining {
 public:
  AlwaysOptimizeAllowNativesSyntaxNoInlining()
      : always_opt_(i::FLAG_always_opt),
        allow_natives_syntax_(i::FLAG_allow_natives_syntax),
60
        turbo_inlining_(i::FLAG_turbo_inlining),
61 62 63
        use_inlining_(i::FLAG_use_inlining) {
    i::FLAG_always_opt = true;
    i::FLAG_allow_natives_syntax = true;
64
    i::FLAG_turbo_inlining = false;
65 66 67 68 69
    i::FLAG_use_inlining = false;
  }

  ~AlwaysOptimizeAllowNativesSyntaxNoInlining() {
    i::FLAG_always_opt = always_opt_;
70 71
    i::FLAG_allow_natives_syntax = allow_natives_syntax_;
    i::FLAG_turbo_inlining = turbo_inlining_;
72 73 74 75 76 77
    i::FLAG_use_inlining = use_inlining_;
  }

 private:
  bool always_opt_;
  bool allow_natives_syntax_;
78
  bool turbo_inlining_;
79 80 81 82
  bool use_inlining_;
};


83 84 85
// Utility class to set the following runtime flags when constructed and return
// to their default state when destroyed:
//   --allow-natives-syntax --noturbo-inlining --nouse-inlining
86
class AllowNativesSyntaxNoInlining {
87
 public:
88
  AllowNativesSyntaxNoInlining()
89
      : allow_natives_syntax_(i::FLAG_allow_natives_syntax),
90
        turbo_inlining_(i::FLAG_turbo_inlining),
91
        use_inlining_(i::FLAG_use_inlining) {
92
    i::FLAG_allow_natives_syntax = true;
93
    i::FLAG_turbo_inlining = false;
94 95 96
    i::FLAG_use_inlining = false;
  }

97
  ~AllowNativesSyntaxNoInlining() {
98
    i::FLAG_allow_natives_syntax = allow_natives_syntax_;
99
    i::FLAG_turbo_inlining = turbo_inlining_;
100 101 102 103 104
    i::FLAG_use_inlining = use_inlining_;
  }

 private:
  bool allow_natives_syntax_;
105
  bool turbo_inlining_;
106 107 108 109
  bool use_inlining_;
};


110 111
// Abort any ongoing incremental marking to make sure that all weak global
// handle callbacks are processed.
112
static void NonIncrementalGC(i::Isolate* isolate) {
113 114
  isolate->heap()->CollectAllGarbage(i::Heap::kFinalizeIncrementalMarkingMask,
                                     i::GarbageCollectionReason::kTesting);
115 116 117
}


118
static Handle<JSFunction> GetJSFunction(v8::Local<v8::Context> context,
119
                                        const char* property_name) {
120 121
  v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast(
      context->Global()->Get(context, v8_str(property_name)).ToLocalChecked());
122
  return i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*fun));
123 124 125 126
}


TEST(DeoptimizeSimple) {
127
  LocalContext env;
128
  v8::HandleScope scope(env->GetIsolate());
129 130 131 132 133 134 135 136 137

  // Test lazy deoptimization of a simple function.
  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "function h() { %DeoptimizeFunction(f); }"
        "function g() { count++; h(); }"
        "function f() { g(); };"
138
        "f();");
139
  }
140
  NonIncrementalGC(CcTest::i_isolate());
141

142 143 144 145 146 147
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK(!GetJSFunction(env.local(), "f")->IsOptimized());
148
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
149 150 151 152 153 154 155 156 157

  // Test lazy deoptimization of a simple function. Call the function after the
  // deoptimization while it is still activated further down the stack.
  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "function g() { count++; %DeoptimizeFunction(f); f(false); }"
        "function f(x) { if (x) { g(); } else { return } };"
158
        "f(true);");
159
  }
160
  NonIncrementalGC(CcTest::i_isolate());
161

162 163 164 165 166 167
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK(!GetJSFunction(env.local(), "f")->IsOptimized());
168
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
169 170 171 172
}


TEST(DeoptimizeSimpleWithArguments) {
173
  LocalContext env;
174
  v8::HandleScope scope(env->GetIsolate());
175 176 177 178 179 180 181 182 183

  // Test lazy deoptimization of a simple function with some arguments.
  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "function h(x) { %DeoptimizeFunction(f); }"
        "function g(x, y) { count++; h(x); }"
        "function f(x, y, z) { g(1,x); y+z; };"
184
        "f(1, \"2\", false);");
185
  }
186
  NonIncrementalGC(CcTest::i_isolate());
187

188 189 190 191 192 193
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK(!GetJSFunction(env.local(), "f")->IsOptimized());
194
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
195 196 197 198 199 200 201 202 203 204

  // Test lazy deoptimization of a simple function with some arguments. Call the
  // function after the deoptimization while it is still activated further down
  // the stack.
  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "function g(x, y) { count++; %DeoptimizeFunction(f); f(false, 1, y); }"
        "function f(x, y, z) { if (x) { g(x, y); } else { return y + z; } };"
205
        "f(true, 1, \"2\");");
206
  }
207
  NonIncrementalGC(CcTest::i_isolate());
208

209 210 211 212 213 214
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK(!GetJSFunction(env.local(), "f")->IsOptimized());
215
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
216 217 218 219
}


TEST(DeoptimizeSimpleNested) {
220
  LocalContext env;
221
  v8::HandleScope scope(env->GetIsolate());
222 223 224 225 226 227 228 229 230 231 232

  // Test lazy deoptimization of a simple function. Have a nested function call
  // do the deoptimization.
  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "var result = 0;"
        "function h(x, y, z) { return x + y + z; }"
        "function g(z) { count++; %DeoptimizeFunction(f); return z;}"
        "function f(x,y,z) { return h(x, y, g(z)); };"
233
        "result = f(1, 2, 3);");
234
    NonIncrementalGC(CcTest::i_isolate());
235

236 237 238 239 240 241 242 243 244 245 246
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(6, env->Global()
                    ->Get(env.local(), v8_str("result"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK(!GetJSFunction(env.local(), "f")->IsOptimized());
247
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
248 249 250 251 252
  }
}


TEST(DeoptimizeRecursive) {
253
  LocalContext env;
254
  v8::HandleScope scope(env->GetIsolate());
255 256 257 258 259 260 261 262 263 264

  {
    // Test lazy deoptimization of a simple function called recursively. Call
    // the function recursively a number of times before deoptimizing it.
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "var calls = 0;"
        "function g() { count++; %DeoptimizeFunction(f); }"
        "function f(x) { calls++; if (x > 0) { f(x - 1); } else { g(); } };"
265
        "f(10);");
266
  }
267
  NonIncrementalGC(CcTest::i_isolate());
268

269 270 271 272 273 274 275 276 277 278
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK_EQ(11, env->Global()
                   ->Get(env.local(), v8_str("calls"))
                   .ToLocalChecked()
                   ->Int32Value(env.local())
                   .FromJust());
279
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
280

281
  v8::Local<v8::Function> fun = v8::Local<v8::Function>::Cast(
282 283 284
      env->Global()
          ->Get(env.local(), v8_str(CcTest::isolate(), "f"))
          .ToLocalChecked());
285
  CHECK(!fun.IsEmpty());
286 287 288 289
}


TEST(DeoptimizeMultiple) {
290
  LocalContext env;
291
  v8::HandleScope scope(env->GetIsolate());
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "var result = 0;"
        "function g() { count++;"
        "               %DeoptimizeFunction(f1);"
        "               %DeoptimizeFunction(f2);"
        "               %DeoptimizeFunction(f3);"
        "               %DeoptimizeFunction(f4);}"
        "function f4(x) { g(); };"
        "function f3(x, y, z) { f4(); return x + y + z; };"
        "function f2(x, y) { return x + f3(y + 1, y + 1, y + 1) + y; };"
        "function f1(x) { return f2(x + 1, x + 1) + x; };"
307
        "result = f1(1);");
308
  }
309
  NonIncrementalGC(CcTest::i_isolate());
310

311 312 313 314 315 316 317 318 319 320
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK_EQ(14, env->Global()
                   ->Get(env.local(), v8_str("result"))
                   .ToLocalChecked()
                   ->Int32Value(env.local())
                   .FromJust());
321
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
322 323 324 325
}


TEST(DeoptimizeConstructor) {
326
  LocalContext env;
327
  v8::HandleScope scope(env->GetIsolate());
328 329 330 331 332 333 334 335

  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "function g() { count++;"
        "               %DeoptimizeFunction(f); }"
        "function f() {  g(); };"
336
        "result = new f() instanceof f;");
337
  }
338
  NonIncrementalGC(CcTest::i_isolate());
339

340 341 342 343 344 345 346 347 348
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK(env->Global()
            ->Get(env.local(), v8_str("result"))
            .ToLocalChecked()
            ->IsTrue());
349
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
350 351 352 353 354 355 356 357 358 359

  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "var result = 0;"
        "function g() { count++;"
        "               %DeoptimizeFunction(f); }"
        "function f(x, y) { this.x = x; g(); this.y = y; };"
        "result = new f(1, 2);"
360
        "result = result.x + result.y;");
361
  }
362
  NonIncrementalGC(CcTest::i_isolate());
363

364 365 366 367 368 369 370 371 372 373
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK_EQ(3, env->Global()
                  ->Get(env.local(), v8_str("result"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
374
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
375 376 377 378
}


TEST(DeoptimizeConstructorMultiple) {
379
  LocalContext env;
380
  v8::HandleScope scope(env->GetIsolate());
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

  {
    AlwaysOptimizeAllowNativesSyntaxNoInlining options;
    CompileRun(
        "var count = 0;"
        "var result = 0;"
        "function g() { count++;"
        "               %DeoptimizeFunction(f1);"
        "               %DeoptimizeFunction(f2);"
        "               %DeoptimizeFunction(f3);"
        "               %DeoptimizeFunction(f4);}"
        "function f4(x) { this.result = x; g(); };"
        "function f3(x, y, z) { this.result = new f4(x + y + z).result; };"
        "function f2(x, y) {"
        "    this.result = x + new f3(y + 1, y + 1, y + 1).result + y; };"
        "function f1(x) { this.result = new f2(x + 1, x + 1).result + x; };"
397
        "result = new f1(1).result;");
398
  }
399
  NonIncrementalGC(CcTest::i_isolate());
400

401 402 403 404 405 406 407 408 409 410
  CHECK_EQ(1, env->Global()
                  ->Get(env.local(), v8_str("count"))
                  .ToLocalChecked()
                  ->Int32Value(env.local())
                  .FromJust());
  CHECK_EQ(14, env->Global()
                   ->Get(env.local(), v8_str("result"))
                   .ToLocalChecked()
                   ->Int32Value(env.local())
                   .FromJust());
411
  CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(CcTest::i_isolate()));
412 413 414
}


415
UNINITIALIZED_TEST(DeoptimizeBinaryOperationADDString) {
416 417
  i::FLAG_concurrent_recompilation = false;
  AllowNativesSyntaxNoInlining options;
418 419 420
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
421 422
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
423
  {
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());

    const char* f_source = "function f(x, y) { return x + y; };";

    {
      // Compile function f and collect to type feedback to insert binary op
      // stub call in the optimized code.
      i::FLAG_prepare_always_opt = true;
      CompileRun(
          "var count = 0;"
          "var result = 0;"
          "var deopt = false;"
          "function X() { };"
          "X.prototype.toString = function () {"
          "  if (deopt) { count++; %DeoptimizeFunction(f); } return 'an X'"
          "};");
      CompileRun(f_source);
      CompileRun(
          "for (var i = 0; i < 5; i++) {"
          "  f('a+', new X());"
          "};");

      // Compile an optimized version of f.
      i::FLAG_always_opt = true;
      CompileRun(f_source);
      CompileRun("f('a+', new X());");
Mythri's avatar
Mythri committed
451
      CHECK(!i_isolate->use_optimizer() ||
452
            GetJSFunction(env.local(), "f")->IsOptimized());
453 454 455 456 457 458 459

      // Call f and force deoptimization while processing the binary operation.
      CompileRun(
          "deopt = true;"
          "var result = f('a+', new X());");
    }
    NonIncrementalGC(i_isolate);
460

461 462 463 464 465 466 467 468
    CHECK(!GetJSFunction(env.local(), "f")->IsOptimized());
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    v8::Local<v8::Value> result =
        env->Global()->Get(env.local(), v8_str("result")).ToLocalChecked();
469 470
    CHECK(result->IsString());
    v8::String::Utf8Value utf8(result);
471
    CHECK_EQ(0, strcmp("a+an X", *utf8));
472 473 474 475
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(i_isolate));
  }
  isolate->Exit();
  isolate->Dispose();
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
}


static void CompileConstructorWithDeoptimizingValueOf() {
  CompileRun("var count = 0;"
             "var result = 0;"
             "var deopt = false;"
             "function X() { };"
             "X.prototype.valueOf = function () {"
             "  if (deopt) { count++; %DeoptimizeFunction(f); } return 8"
             "};");
}


static void TestDeoptimizeBinaryOpHelper(LocalContext* env,
                                         const char* binary_op) {
492
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>((*env)->GetIsolate());
493
  EmbeddedVector<char, SMALL_STRING_BUFFER_SIZE> f_source_buffer;
494 495 496
  SNPrintF(f_source_buffer,
           "function f(x, y) { return x %s y; };",
           binary_op);
497 498
  char* f_source = f_source_buffer.start();

499
  AllowNativesSyntaxNoInlining options;
500 501 502 503 504 505 506 507 508 509 510 511 512
  // Compile function f and collect to type feedback to insert binary op stub
  // call in the optimized code.
  i::FLAG_prepare_always_opt = true;
  CompileConstructorWithDeoptimizingValueOf();
  CompileRun(f_source);
  CompileRun("for (var i = 0; i < 5; i++) {"
             "  f(8, new X());"
             "};");

  // Compile an optimized version of f.
  i::FLAG_always_opt = true;
  CompileRun(f_source);
  CompileRun("f(7, new X());");
Mythri's avatar
Mythri committed
513
  CHECK(!i_isolate->use_optimizer() ||
514
        GetJSFunction((*env).local(), "f")->IsOptimized());
515 516 517

  // Call f and force deoptimization while processing the binary operation.
  CompileRun("deopt = true;"
518
             "var result = f(7, new X());");
519
  NonIncrementalGC(i_isolate);
520
  CHECK(!GetJSFunction((*env).local(), "f")->IsOptimized());
521 522 523
}


524
UNINITIALIZED_TEST(DeoptimizeBinaryOperationADD) {
525
  i::FLAG_concurrent_recompilation = false;
526 527 528
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
529 530 531 532 533
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
  {
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());
534

535
    TestDeoptimizeBinaryOpHelper(&env, "+");
536

537 538 539 540 541 542 543 544 545 546
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(15, env->Global()
                     ->Get(env.local(), v8_str("result"))
                     .ToLocalChecked()
                     ->Int32Value(env.local())
                     .FromJust());
547 548 549 550
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(i_isolate));
  }
  isolate->Exit();
  isolate->Dispose();
551 552 553
}


554
UNINITIALIZED_TEST(DeoptimizeBinaryOperationSUB) {
555
  i::FLAG_concurrent_recompilation = false;
556 557 558
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
559 560 561 562 563
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
  {
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());
564

565
    TestDeoptimizeBinaryOpHelper(&env, "-");
566

567 568 569 570 571 572 573 574 575 576
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(-1, env->Global()
                     ->Get(env.local(), v8_str("result"))
                     .ToLocalChecked()
                     ->Int32Value(env.local())
                     .FromJust());
577 578 579 580
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(i_isolate));
  }
  isolate->Exit();
  isolate->Dispose();
581 582 583
}


584
UNINITIALIZED_TEST(DeoptimizeBinaryOperationMUL) {
585
  i::FLAG_concurrent_recompilation = false;
586 587 588
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
589 590 591 592 593
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
  {
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());
594

595
    TestDeoptimizeBinaryOpHelper(&env, "*");
596

597 598 599 600 601 602 603 604 605 606
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(56, env->Global()
                     ->Get(env.local(), v8_str("result"))
                     .ToLocalChecked()
                     ->Int32Value(env.local())
                     .FromJust());
607 608 609 610
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(i_isolate));
  }
  isolate->Exit();
  isolate->Dispose();
611 612 613
}


614
UNINITIALIZED_TEST(DeoptimizeBinaryOperationDIV) {
615
  i::FLAG_concurrent_recompilation = false;
616 617 618
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
619 620 621 622 623
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
  {
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());
624

625
    TestDeoptimizeBinaryOpHelper(&env, "/");
626

627 628 629 630 631 632 633 634 635 636
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(0, env->Global()
                    ->Get(env.local(), v8_str("result"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
637 638 639 640
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(i_isolate));
  }
  isolate->Exit();
  isolate->Dispose();
641 642 643
}


644
UNINITIALIZED_TEST(DeoptimizeBinaryOperationMOD) {
645
  i::FLAG_concurrent_recompilation = false;
646 647 648
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
649 650 651 652 653
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
  {
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());
654

655
    TestDeoptimizeBinaryOpHelper(&env, "%");
656

657 658 659 660 661 662 663 664 665 666
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(7, env->Global()
                    ->Get(env.local(), v8_str("result"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
667 668 669 670
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(i_isolate));
  }
  isolate->Exit();
  isolate->Dispose();
671 672 673
}


674
UNINITIALIZED_TEST(DeoptimizeCompare) {
675
  i::FLAG_concurrent_recompilation = false;
676 677 678
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
679 680
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
681
  {
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());

    const char* f_source = "function f(x, y) { return x < y; };";

    {
      AllowNativesSyntaxNoInlining options;
      // Compile function f and collect to type feedback to insert compare ic
      // call in the optimized code.
      i::FLAG_prepare_always_opt = true;
      CompileRun(
          "var count = 0;"
          "var result = 0;"
          "var deopt = false;"
          "function X() { };"
          "X.prototype.toString = function () {"
          "  if (deopt) { count++; %DeoptimizeFunction(f); } return 'b'"
          "};");
      CompileRun(f_source);
      CompileRun(
          "for (var i = 0; i < 5; i++) {"
          "  f('a', new X());"
          "};");

      // Compile an optimized version of f.
      i::FLAG_always_opt = true;
      CompileRun(f_source);
      CompileRun("f('a', new X());");
Mythri's avatar
Mythri committed
710
      CHECK(!i_isolate->use_optimizer() ||
711
            GetJSFunction(env.local(), "f")->IsOptimized());
712 713 714 715 716 717 718

      // Call f and force deoptimization while processing the comparison.
      CompileRun(
          "deopt = true;"
          "var result = f('a', new X());");
    }
    NonIncrementalGC(i_isolate);
719

720 721 722 723 724 725 726 727 728 729 730
    CHECK(!GetJSFunction(env.local(), "f")->IsOptimized());
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(true, env->Global()
                       ->Get(env.local(), v8_str("result"))
                       .ToLocalChecked()
                       ->BooleanValue(env.local())
                       .FromJust());
731 732 733 734
    CHECK_EQ(0, Deoptimizer::GetDeoptimizedCodeCount(i_isolate));
  }
  isolate->Exit();
  isolate->Dispose();
735 736 737
}


738
UNINITIALIZED_TEST(DeoptimizeLoadICStoreIC) {
739
  i::FLAG_concurrent_recompilation = false;
740 741 742
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
743 744
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
745
  {
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());

    // Functions to generate load/store/keyed load/keyed store IC calls.
    const char* f1_source = "function f1(x) { return x.y; };";
    const char* g1_source = "function g1(x) { x.y = 1; };";
    const char* f2_source = "function f2(x, y) { return x[y]; };";
    const char* g2_source = "function g2(x, y) { x[y] = 1; };";

    {
      AllowNativesSyntaxNoInlining options;
      // Compile functions and collect to type feedback to insert ic
      // calls in the optimized code.
      i::FLAG_prepare_always_opt = true;
      CompileRun(
          "var count = 0;"
          "var result = 0;"
          "var deopt = false;"
          "function X() { };"
          "X.prototype.__defineGetter__('y', function () {"
          "  if (deopt) { count++; %DeoptimizeFunction(f1); };"
          "  return 13;"
          "});"
          "X.prototype.__defineSetter__('y', function () {"
          "  if (deopt) { count++; %DeoptimizeFunction(g1); };"
          "});"
          "X.prototype.__defineGetter__('z', function () {"
          "  if (deopt) { count++; %DeoptimizeFunction(f2); };"
          "  return 13;"
          "});"
          "X.prototype.__defineSetter__('z', function () {"
          "  if (deopt) { count++; %DeoptimizeFunction(g2); };"
          "});");
      CompileRun(f1_source);
      CompileRun(g1_source);
      CompileRun(f2_source);
      CompileRun(g2_source);
      CompileRun(
          "for (var i = 0; i < 5; i++) {"
          "  f1(new X());"
          "  g1(new X());"
          "  f2(new X(), 'z');"
          "  g2(new X(), 'z');"
          "};");

      // Compile an optimized version of the functions.
      i::FLAG_always_opt = true;
      CompileRun(f1_source);
      CompileRun(g1_source);
      CompileRun(f2_source);
      CompileRun(g2_source);
      CompileRun("f1(new X());");
      CompileRun("g1(new X());");
      CompileRun("f2(new X(), 'z');");
      CompileRun("g2(new X(), 'z');");
Mythri's avatar
Mythri committed
801
      if (i_isolate->use_optimizer()) {
802 803 804 805
        CHECK(GetJSFunction(env.local(), "f1")->IsOptimized());
        CHECK(GetJSFunction(env.local(), "g1")->IsOptimized());
        CHECK(GetJSFunction(env.local(), "f2")->IsOptimized());
        CHECK(GetJSFunction(env.local(), "g2")->IsOptimized());
806 807 808 809 810 811 812 813 814
      }

      // Call functions and force deoptimization while processing the ics.
      CompileRun(
          "deopt = true;"
          "var result = f1(new X());"
          "g1(new X());"
          "f2(new X(), 'z');"
          "g2(new X(), 'z');");
815
    }
816 817
    NonIncrementalGC(i_isolate);

818 819 820 821 822 823 824 825 826 827 828 829 830 831
    CHECK(!GetJSFunction(env.local(), "f1")->IsOptimized());
    CHECK(!GetJSFunction(env.local(), "g1")->IsOptimized());
    CHECK(!GetJSFunction(env.local(), "f2")->IsOptimized());
    CHECK(!GetJSFunction(env.local(), "g2")->IsOptimized());
    CHECK_EQ(4, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(13, env->Global()
                     ->Get(env.local(), v8_str("result"))
                     .ToLocalChecked()
                     ->Int32Value(env.local())
                     .FromJust());
832
  }
833 834
  isolate->Exit();
  isolate->Dispose();
835 836 837
}


838
UNINITIALIZED_TEST(DeoptimizeLoadICStoreICNested) {
839
  i::FLAG_concurrent_recompilation = false;
840 841 842
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
843 844
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  isolate->Enter();
845
  {
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
    LocalContext env(isolate);
    v8::HandleScope scope(env->GetIsolate());

    // Functions to generate load/store/keyed load/keyed store IC calls.
    const char* f1_source = "function f1(x) { return x.y; };";
    const char* g1_source = "function g1(x) { x.y = 1; };";
    const char* f2_source = "function f2(x, y) { return x[y]; };";
    const char* g2_source = "function g2(x, y) { x[y] = 1; };";

    {
      AllowNativesSyntaxNoInlining options;
      // Compile functions and collect to type feedback to insert ic
      // calls in the optimized code.
      i::FLAG_prepare_always_opt = true;
      CompileRun(
          "var count = 0;"
          "var result = 0;"
          "var deopt = false;"
          "function X() { };"
          "X.prototype.__defineGetter__('y', function () {"
          "  g1(this);"
          "  return 13;"
          "});"
          "X.prototype.__defineSetter__('y', function () {"
          "  f2(this, 'z');"
          "});"
          "X.prototype.__defineGetter__('z', function () {"
          "  g2(this, 'z');"
          "});"
          "X.prototype.__defineSetter__('z', function () {"
          "  if (deopt) {"
          "    count++;"
          "    %DeoptimizeFunction(f1);"
          "    %DeoptimizeFunction(g1);"
          "    %DeoptimizeFunction(f2);"
          "    %DeoptimizeFunction(g2); };"
          "});");
      CompileRun(f1_source);
      CompileRun(g1_source);
      CompileRun(f2_source);
      CompileRun(g2_source);
      CompileRun(
          "for (var i = 0; i < 5; i++) {"
          "  f1(new X());"
          "  g1(new X());"
          "  f2(new X(), 'z');"
          "  g2(new X(), 'z');"
          "};");

      // Compile an optimized version of the functions.
      i::FLAG_always_opt = true;
      CompileRun(f1_source);
      CompileRun(g1_source);
      CompileRun(f2_source);
      CompileRun(g2_source);
      CompileRun("f1(new X());");
      CompileRun("g1(new X());");
      CompileRun("f2(new X(), 'z');");
      CompileRun("g2(new X(), 'z');");
Mythri's avatar
Mythri committed
905
      if (i_isolate->use_optimizer()) {
906 907 908 909
        CHECK(GetJSFunction(env.local(), "f1")->IsOptimized());
        CHECK(GetJSFunction(env.local(), "g1")->IsOptimized());
        CHECK(GetJSFunction(env.local(), "f2")->IsOptimized());
        CHECK(GetJSFunction(env.local(), "g2")->IsOptimized());
910 911 912 913 914 915
      }

      // Call functions and force deoptimization while processing the ics.
      CompileRun(
          "deopt = true;"
          "var result = f1(new X());");
916
    }
917
    NonIncrementalGC(i_isolate);
918

919 920 921 922 923 924 925 926 927 928 929 930 931 932
    CHECK(!GetJSFunction(env.local(), "f1")->IsOptimized());
    CHECK(!GetJSFunction(env.local(), "g1")->IsOptimized());
    CHECK(!GetJSFunction(env.local(), "f2")->IsOptimized());
    CHECK(!GetJSFunction(env.local(), "g2")->IsOptimized());
    CHECK_EQ(1, env->Global()
                    ->Get(env.local(), v8_str("count"))
                    .ToLocalChecked()
                    ->Int32Value(env.local())
                    .FromJust());
    CHECK_EQ(13, env->Global()
                     ->Get(env.local(), v8_str("result"))
                     .ToLocalChecked()
                     ->Int32Value(env.local())
                     .FromJust());
933
  }
934 935
  isolate->Exit();
  isolate->Dispose();
936
}