test-js-context-specialization.cc 24.8 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/codegen/tick-counter.h"
6
#include "src/compiler/compiler-source-position-table.h"
7
#include "src/compiler/js-context-specialization.h"
8
#include "src/compiler/js-graph.h"
9
#include "src/compiler/js-heap-broker.h"
10 11
#include "src/compiler/js-operator.h"
#include "src/compiler/node-matchers.h"
12
#include "src/compiler/node-properties.h"
13
#include "src/compiler/simplified-operator.h"
14
#include "src/heap/factory.h"
15
#include "src/objects/objects-inl.h"
16
#include "src/objects/property.h"
17 18 19
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/function-tester.h"

20 21 22
namespace v8 {
namespace internal {
namespace compiler {
23

24
class ContextSpecializationTester : public HandleAndZoneScope {
25
 public:
26
  explicit ContextSpecializationTester(Maybe<OuterContext> context)
27 28
      : canonical_(main_isolate()),
        graph_(new (main_zone()) Graph(main_zone())),
29 30
        common_(main_zone()),
        javascript_(main_zone()),
31
        machine_(main_zone()),
32
        simplified_(main_zone()),
33 34
        jsgraph_(main_isolate(), graph(), common(), &javascript_, &simplified_,
                 &machine_),
35
        reducer_(main_zone(), graph(), &tick_counter_),
36
        js_heap_broker_(main_isolate(), main_zone()),
37 38
        spec_(&reducer_, jsgraph(), &js_heap_broker_, context,
              MaybeHandle<JSFunction>()) {}
39

40
  JSContextSpecialization* spec() { return &spec_; }
41 42 43 44 45
  Factory* factory() { return main_isolate()->factory(); }
  CommonOperatorBuilder* common() { return &common_; }
  JSOperatorBuilder* javascript() { return &javascript_; }
  SimplifiedOperatorBuilder* simplified() { return &simplified_; }
  JSGraph* jsgraph() { return &jsgraph_; }
46
  Graph* graph() { return graph_; }
47

48 49 50 51 52 53 54
  void CheckChangesToValue(Node* node, Handle<HeapObject> expected_value);
  void CheckContextInputAndDepthChanges(
      Node* node, Handle<Context> expected_new_context_object,
      size_t expected_new_depth);
  void CheckContextInputAndDepthChanges(Node* node, Node* expected_new_context,
                                        size_t expected_new_depth);

55 56
  JSHeapBroker* broker() { return &js_heap_broker_; }

57
 private:
58
  TickCounter tick_counter_;
59
  CanonicalHandleScope canonical_;
60
  Graph* graph_;
61 62
  CommonOperatorBuilder common_;
  JSOperatorBuilder javascript_;
sigurds@chromium.org's avatar
sigurds@chromium.org committed
63
  MachineOperatorBuilder machine_;
64 65
  SimplifiedOperatorBuilder simplified_;
  JSGraph jsgraph_;
66
  GraphReducer reducer_;
67
  JSHeapBroker js_heap_broker_;
68
  JSContextSpecialization spec_;
69 70
};

71 72 73 74 75 76 77 78 79 80 81 82
void ContextSpecializationTester::CheckChangesToValue(
    Node* node, Handle<HeapObject> expected_value) {
  Reduction r = spec()->Reduce(node);
  CHECK(r.Changed());
  HeapObjectMatcher match(r.replacement());
  CHECK(match.HasValue());
  CHECK_EQ(*match.Value(), *expected_value);
}

void ContextSpecializationTester::CheckContextInputAndDepthChanges(
    Node* node, Handle<Context> expected_new_context_object,
    size_t expected_new_depth) {
83
  ContextAccess access = ContextAccessOf(node->op());
84 85 86 87 88 89
  Reduction r = spec()->Reduce(node);
  CHECK(r.Changed());

  Node* new_context = NodeProperties::GetContextInput(r.replacement());
  CHECK_EQ(IrOpcode::kHeapConstant, new_context->opcode());
  HeapObjectMatcher match(new_context);
90
  CHECK_EQ(Context::cast(*match.Value()), *expected_new_context_object);
91

92
  ContextAccess new_access = ContextAccessOf(r.replacement()->op());
93 94 95 96 97 98 99
  CHECK_EQ(new_access.depth(), expected_new_depth);
  CHECK_EQ(new_access.index(), access.index());
  CHECK_EQ(new_access.immutable(), access.immutable());
}

void ContextSpecializationTester::CheckContextInputAndDepthChanges(
    Node* node, Node* expected_new_context, size_t expected_new_depth) {
100
  ContextAccess access = ContextAccessOf(node->op());
101 102 103 104 105
  Reduction r = spec()->Reduce(node);
  CHECK(r.Changed());

  Node* new_context = NodeProperties::GetContextInput(r.replacement());
  CHECK_EQ(new_context, expected_new_context);
106

107
  ContextAccess new_access = ContextAccessOf(r.replacement()->op());
108 109 110 111 112
  CHECK_EQ(new_access.depth(), expected_new_depth);
  CHECK_EQ(new_access.index(), access.index());
  CHECK_EQ(new_access.immutable(), access.immutable());
}

113
static const int slot_index = Context::PREVIOUS_INDEX;
114 115

TEST(ReduceJSLoadContext0) {
116
  ContextSpecializationTester t(Nothing<OuterContext>());
117

118
  Node* start = t.graph()->NewNode(t.common()->Start(0));
119 120 121 122
  t.graph()->SetStart(start);

  // Make a context and initialize it a bit for this test.
  Handle<Context> native = t.factory()->NewNativeContext();
123 124 125 126
  Handle<Context> subcontext1 = t.factory()->NewNativeContext();
  Handle<Context> subcontext2 = t.factory()->NewNativeContext();
  subcontext2->set_previous(*subcontext1);
  subcontext1->set_previous(*native);
127
  Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!");
128
  const int slot = Context::PREVIOUS_INDEX;
129 130
  native->set(slot, *expected);

131 132 133
  Node* const_context = t.jsgraph()->Constant(ObjectRef(t.broker(), native));
  Node* deep_const_context =
      t.jsgraph()->Constant(ObjectRef(t.broker(), subcontext2));
134
  Node* param_context = t.graph()->NewNode(t.common()->Parameter(0), start);
135 136 137

  {
    // Mutable slot, constant context, depth = 0 => do nothing.
138
    Node* load = t.graph()->NewNode(t.javascript()->LoadContext(0, 0, false),
139
                                    const_context, start);
140
    Reduction r = t.spec()->Reduce(load);
141 142 143 144 145
    CHECK(!r.Changed());
  }

  {
    // Mutable slot, non-constant context, depth = 0 => do nothing.
146
    Node* load = t.graph()->NewNode(t.javascript()->LoadContext(0, 0, false),
147
                                    param_context, start);
148
    Reduction r = t.spec()->Reduce(load);
149 150 151 152
    CHECK(!r.Changed());
  }

  {
153
    // Mutable slot, constant context, depth > 0 => fold-in parent context.
154
    Node* load = t.graph()->NewNode(
155
        t.javascript()->LoadContext(2, Context::GLOBAL_EVAL_FUN_INDEX, false),
156
        deep_const_context, start);
157
    Reduction r = t.spec()->Reduce(load);
158
    CHECK(r.Changed());
159
    Node* new_context_input = NodeProperties::GetContextInput(r.replacement());
160
    CHECK_EQ(IrOpcode::kHeapConstant, new_context_input->opcode());
161
    HeapObjectMatcher match(new_context_input);
162
    CHECK_EQ(*native, Context::cast(*match.Value()));
163
    ContextAccess access = ContextAccessOf(r.replacement()->op());
164 165
    CHECK_EQ(Context::GLOBAL_EVAL_FUN_INDEX, static_cast<int>(access.index()));
    CHECK_EQ(0, static_cast<int>(access.depth()));
166 167 168 169
    CHECK_EQ(false, access.immutable());
  }

  {
170
    // Immutable slot, constant context, depth = 0 => specialize.
171
    Node* load = t.graph()->NewNode(t.javascript()->LoadContext(0, slot, true),
172
                                    const_context, start);
173
    Reduction r = t.spec()->Reduce(load);
174 175 176
    CHECK(r.Changed());
    CHECK(r.replacement() != load);

177
    HeapObjectMatcher match(r.replacement());
178
    CHECK(match.HasValue());
179
    CHECK_EQ(*expected, *match.Value());
180
  }
181 182 183

  // Clean up so that verifiers don't complain.
  native->set(slot, Smi::zero());
184 185 186 187 188 189 190
}

TEST(ReduceJSLoadContext1) {
  // The graph's context chain ends in the incoming context parameter:
  //
  //   context2 <-- context1 <-- context0 (= Parameter(0))

191
  ContextSpecializationTester t(Nothing<OuterContext>());
192 193 194

  Node* start = t.graph()->NewNode(t.common()->Start(0));
  t.graph()->SetStart(start);
195
  Handle<ScopeInfo> empty(ScopeInfo::Empty(t.main_isolate()), t.main_isolate());
196
  const i::compiler::Operator* create_function_context =
197
      t.javascript()->CreateFunctionContext(empty, 42, FUNCTION_SCOPE);
198 199

  Node* context0 = t.graph()->NewNode(t.common()->Parameter(0), start);
200 201 202 203
  Node* context1 =
      t.graph()->NewNode(create_function_context, context0, start, start);
  Node* context2 =
      t.graph()->NewNode(create_function_context, context1, start, start);
204 205 206 207 208 209 210 211 212 213 214 215

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(0, slot_index, false), context2, start);
    CHECK(!t.spec()->Reduce(load).Changed());
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(0, slot_index, true), context2, start);
    CHECK(!t.spec()->Reduce(load).Changed());
  }
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
  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(1, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context1, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(1, slot_index, true), context2, start);
    t.CheckContextInputAndDepthChanges(load, context1, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(2, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context0, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(2, slot_index, true), context2, start);
    t.CheckContextInputAndDepthChanges(load, context0, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(3, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context0, 1);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(3, slot_index, true), context2, start);
    t.CheckContextInputAndDepthChanges(load, context0, 1);
  }
252 253
}

254 255
TEST(ReduceJSLoadContext2) {
  // The graph's context chain ends in a constant context (context_object1),
256
  // which has another outer context (context_object0).
257 258 259
  //
  //   context2 <-- context1 <-- context0 (= HeapConstant(context_object1))
  //   context_object1 <~~ context_object0
260

261
  ContextSpecializationTester t(Nothing<OuterContext>());
262 263 264

  Node* start = t.graph()->NewNode(t.common()->Start(0));
  t.graph()->SetStart(start);
265
  Handle<ScopeInfo> empty(ScopeInfo::Empty(t.main_isolate()), t.main_isolate());
266
  const i::compiler::Operator* create_function_context =
267
      t.javascript()->CreateFunctionContext(empty, 42, FUNCTION_SCOPE);
268 269 270 271 272 273 274

  Handle<HeapObject> slot_value0 = t.factory()->InternalizeUtf8String("0");
  Handle<HeapObject> slot_value1 = t.factory()->InternalizeUtf8String("1");

  Handle<Context> context_object0 = t.factory()->NewNativeContext();
  Handle<Context> context_object1 = t.factory()->NewNativeContext();
  context_object1->set_previous(*context_object0);
275 276
  context_object0->set(Context::EXTENSION_INDEX, *slot_value0);
  context_object1->set(Context::EXTENSION_INDEX, *slot_value1);
277

278 279
  Node* context0 =
      t.jsgraph()->Constant(ObjectRef(t.broker(), context_object1));
280 281 282 283
  Node* context1 =
      t.graph()->NewNode(create_function_context, context0, start, start);
  Node* context2 =
      t.graph()->NewNode(create_function_context, context1, start, start);
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 314 315 316

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(0, slot_index, false), context2, start);
    CHECK(!t.spec()->Reduce(load).Changed());
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(0, slot_index, true), context2, start);
    CHECK(!t.spec()->Reduce(load).Changed());
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(1, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context1, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(1, slot_index, true), context2, start);
    t.CheckContextInputAndDepthChanges(load, context1, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(2, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context0, 0);
  }

  {
    Node* load = t.graph()->NewNode(
317 318
        t.javascript()->LoadContext(2, Context::EXTENSION_INDEX, true),
        context2, start);
319 320 321 322 323 324 325 326 327 328 329
    t.CheckChangesToValue(load, slot_value1);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(3, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context_object0, 0);
  }

  {
    Node* load = t.graph()->NewNode(
330 331
        t.javascript()->LoadContext(3, Context::EXTENSION_INDEX, true),
        context2, start);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    t.CheckChangesToValue(load, slot_value0);
  }
}

TEST(ReduceJSLoadContext3) {
  // Like in ReduceJSLoadContext1, the graph's context chain ends in the
  // incoming context parameter.  However, this time we provide a concrete
  // context for this parameter as the "specialization context".  We choose
  // context_object2 from ReduceJSLoadContext2 for this, so almost all test
  // expectations are the same as in ReduceJSLoadContext2.

  HandleAndZoneScope handle_zone_scope;
  auto factory = handle_zone_scope.main_isolate()->factory();

  Handle<HeapObject> slot_value0 = factory->InternalizeUtf8String("0");
  Handle<HeapObject> slot_value1 = factory->InternalizeUtf8String("1");

  Handle<Context> context_object0 = factory->NewNativeContext();
  Handle<Context> context_object1 = factory->NewNativeContext();
  context_object1->set_previous(*context_object0);
352 353
  context_object0->set(Context::EXTENSION_INDEX, *slot_value0);
  context_object1->set(Context::EXTENSION_INDEX, *slot_value1);
354

355
  ContextSpecializationTester t(Just(OuterContext(context_object1, 0)));
356 357 358

  Node* start = t.graph()->NewNode(t.common()->Start(2));
  t.graph()->SetStart(start);
359 360
  Handle<ScopeInfo> empty(ScopeInfo::Empty(t.main_isolate()),
                          handle_zone_scope.main_isolate());
361
  const i::compiler::Operator* create_function_context =
362
      t.javascript()->CreateFunctionContext(empty, 42, FUNCTION_SCOPE);
363 364

  Node* context0 = t.graph()->NewNode(t.common()->Parameter(0), start);
365 366 367 368
  Node* context1 =
      t.graph()->NewNode(create_function_context, context0, start, start);
  Node* context2 =
      t.graph()->NewNode(create_function_context, context1, start, start);
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(0, slot_index, false), context2, start);
    CHECK(!t.spec()->Reduce(load).Changed());
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(0, slot_index, true), context2, start);
    CHECK(!t.spec()->Reduce(load).Changed());
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(1, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context1, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(1, slot_index, true), context2, start);
    t.CheckContextInputAndDepthChanges(load, context1, 0);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(2, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context_object1, 0);
  }

  {
    Node* load = t.graph()->NewNode(
402 403
        t.javascript()->LoadContext(2, Context::EXTENSION_INDEX, true),
        context2, start);
404 405 406 407 408 409 410 411 412 413 414
    t.CheckChangesToValue(load, slot_value1);
  }

  {
    Node* load = t.graph()->NewNode(
        t.javascript()->LoadContext(3, slot_index, false), context2, start);
    t.CheckContextInputAndDepthChanges(load, context_object0, 0);
  }

  {
    Node* load = t.graph()->NewNode(
415 416
        t.javascript()->LoadContext(3, Context::EXTENSION_INDEX, true),
        context2, start);
417 418 419 420 421
    t.CheckChangesToValue(load, slot_value0);
  }
}

TEST(ReduceJSStoreContext0) {
422
  ContextSpecializationTester t(Nothing<OuterContext>());
423

424
  Node* start = t.graph()->NewNode(t.common()->Start(0));
425 426 427 428 429 430 431 432 433
  t.graph()->SetStart(start);

  // Make a context and initialize it a bit for this test.
  Handle<Context> native = t.factory()->NewNativeContext();
  Handle<Context> subcontext1 = t.factory()->NewNativeContext();
  Handle<Context> subcontext2 = t.factory()->NewNativeContext();
  subcontext2->set_previous(*subcontext1);
  subcontext1->set_previous(*native);
  Handle<Object> expected = t.factory()->InternalizeUtf8String("gboy!");
434
  const int slot = Context::PREVIOUS_INDEX;
435 436
  native->set(slot, *expected);

437 438 439
  Node* const_context = t.jsgraph()->Constant(ObjectRef(t.broker(), native));
  Node* deep_const_context =
      t.jsgraph()->Constant(ObjectRef(t.broker(), subcontext2));
440
  Node* param_context = t.graph()->NewNode(t.common()->Parameter(0), start);
441

442
  {
443
    // Mutable slot, constant context, depth = 0 => do nothing.
444 445
    Node* load = t.graph()->NewNode(t.javascript()->StoreContext(0, 0),
                                    const_context, const_context, start, start);
446
    Reduction r = t.spec()->Reduce(load);
447 448
    CHECK(!r.Changed());
  }
449

450 451
  {
    // Mutable slot, non-constant context, depth = 0 => do nothing.
452 453
    Node* load = t.graph()->NewNode(t.javascript()->StoreContext(0, 0),
                                    param_context, param_context, start, start);
454
    Reduction r = t.spec()->Reduce(load);
455
    CHECK(!r.Changed());
456 457
  }

458 459
  {
    // Immutable slot, constant context, depth = 0 => do nothing.
460 461
    Node* load = t.graph()->NewNode(t.javascript()->StoreContext(0, slot),
                                    const_context, const_context, start, start);
462
    Reduction r = t.spec()->Reduce(load);
463 464 465 466 467
    CHECK(!r.Changed());
  }

  {
    // Mutable slot, constant context, depth > 0 => fold-in parent context.
468
    Node* load = t.graph()->NewNode(
469
        t.javascript()->StoreContext(2, Context::GLOBAL_EVAL_FUN_INDEX),
470
        deep_const_context, deep_const_context, start, start);
471
    Reduction r = t.spec()->Reduce(load);
472
    CHECK(r.Changed());
473
    Node* new_context_input = NodeProperties::GetContextInput(r.replacement());
474
    CHECK_EQ(IrOpcode::kHeapConstant, new_context_input->opcode());
475
    HeapObjectMatcher match(new_context_input);
476
    CHECK_EQ(*native, Context::cast(*match.Value()));
477
    ContextAccess access = ContextAccessOf(r.replacement()->op());
478 479
    CHECK_EQ(Context::GLOBAL_EVAL_FUN_INDEX, static_cast<int>(access.index()));
    CHECK_EQ(0, static_cast<int>(access.depth()));
480 481
    CHECK_EQ(false, access.immutable());
  }
482 483 484

  // Clean up so that verifiers don't complain.
  native->set(slot, Smi::zero());
485 486
}

487
TEST(ReduceJSStoreContext1) {
488
  ContextSpecializationTester t(Nothing<OuterContext>());
489 490 491

  Node* start = t.graph()->NewNode(t.common()->Start(0));
  t.graph()->SetStart(start);
492
  Handle<ScopeInfo> empty(ScopeInfo::Empty(t.main_isolate()), t.main_isolate());
493
  const i::compiler::Operator* create_function_context =
494
      t.javascript()->CreateFunctionContext(empty, 42, FUNCTION_SCOPE);
495 496

  Node* context0 = t.graph()->NewNode(t.common()->Parameter(0), start);
497 498 499 500
  Node* context1 =
      t.graph()->NewNode(create_function_context, context0, start, start);
  Node* context2 =
      t.graph()->NewNode(create_function_context, context1, start, start);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

  {
    Node* store =
        t.graph()->NewNode(t.javascript()->StoreContext(0, slot_index),
                           context2, context2, start, start);
    CHECK(!t.spec()->Reduce(store).Changed());
  }

  {
    Node* store =
        t.graph()->NewNode(t.javascript()->StoreContext(1, slot_index),
                           context2, context2, start, start);
    t.CheckContextInputAndDepthChanges(store, context1, 0);
  }

  {
    Node* store =
        t.graph()->NewNode(t.javascript()->StoreContext(2, slot_index),
                           context2, context2, start, start);
    t.CheckContextInputAndDepthChanges(store, context0, 0);
  }

  {
    Node* store =
        t.graph()->NewNode(t.javascript()->StoreContext(3, slot_index),
                           context2, context2, start, start);
    t.CheckContextInputAndDepthChanges(store, context0, 1);
  }
}

TEST(ReduceJSStoreContext2) {
532
  ContextSpecializationTester t(Nothing<OuterContext>());
533 534 535

  Node* start = t.graph()->NewNode(t.common()->Start(0));
  t.graph()->SetStart(start);
536
  Handle<ScopeInfo> empty(ScopeInfo::Empty(t.main_isolate()), t.main_isolate());
537
  const i::compiler::Operator* create_function_context =
538
      t.javascript()->CreateFunctionContext(empty, 42, FUNCTION_SCOPE);
539 540 541 542 543 544 545

  Handle<HeapObject> slot_value0 = t.factory()->InternalizeUtf8String("0");
  Handle<HeapObject> slot_value1 = t.factory()->InternalizeUtf8String("1");

  Handle<Context> context_object0 = t.factory()->NewNativeContext();
  Handle<Context> context_object1 = t.factory()->NewNativeContext();
  context_object1->set_previous(*context_object0);
546 547
  context_object0->set(Context::EXTENSION_INDEX, *slot_value0);
  context_object1->set(Context::EXTENSION_INDEX, *slot_value1);
548

549 550
  Node* context0 =
      t.jsgraph()->Constant(ObjectRef(t.broker(), context_object1));
551 552 553 554
  Node* context1 =
      t.graph()->NewNode(create_function_context, context0, start, start);
  Node* context2 =
      t.graph()->NewNode(create_function_context, context1, start, start);
555 556

  {
557 558 559
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(0, Context::EXTENSION_INDEX), context2,
        context2, start, start);
560 561 562 563
    CHECK(!t.spec()->Reduce(store).Changed());
  }

  {
564 565 566
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(1, Context::EXTENSION_INDEX), context2,
        context2, start, start);
567 568 569 570
    t.CheckContextInputAndDepthChanges(store, context1, 0);
  }

  {
571 572 573
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(2, Context::EXTENSION_INDEX), context2,
        context2, start, start);
574 575 576 577
    t.CheckContextInputAndDepthChanges(store, context0, 0);
  }

  {
578 579 580
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(3, Context::EXTENSION_INDEX), context2,
        context2, start, start);
581 582 583 584 585 586 587 588 589 590 591 592 593 594
    t.CheckContextInputAndDepthChanges(store, context_object0, 0);
  }
}

TEST(ReduceJSStoreContext3) {
  HandleAndZoneScope handle_zone_scope;
  auto factory = handle_zone_scope.main_isolate()->factory();

  Handle<HeapObject> slot_value0 = factory->InternalizeUtf8String("0");
  Handle<HeapObject> slot_value1 = factory->InternalizeUtf8String("1");

  Handle<Context> context_object0 = factory->NewNativeContext();
  Handle<Context> context_object1 = factory->NewNativeContext();
  context_object1->set_previous(*context_object0);
595 596
  context_object0->set(Context::EXTENSION_INDEX, *slot_value0);
  context_object1->set(Context::EXTENSION_INDEX, *slot_value1);
597

598
  ContextSpecializationTester t(Just(OuterContext(context_object1, 0)));
599 600 601

  Node* start = t.graph()->NewNode(t.common()->Start(2));
  t.graph()->SetStart(start);
602 603
  Handle<ScopeInfo> empty(ScopeInfo::Empty(t.main_isolate()),
                          handle_zone_scope.main_isolate());
604
  const i::compiler::Operator* create_function_context =
605
      t.javascript()->CreateFunctionContext(empty, 42, FUNCTION_SCOPE);
606 607

  Node* context0 = t.graph()->NewNode(t.common()->Parameter(0), start);
608 609 610 611
  Node* context1 =
      t.graph()->NewNode(create_function_context, context0, start, start);
  Node* context2 =
      t.graph()->NewNode(create_function_context, context1, start, start);
612 613

  {
614 615 616
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(0, Context::EXTENSION_INDEX), context2,
        context2, start, start);
617 618 619 620
    CHECK(!t.spec()->Reduce(store).Changed());
  }

  {
621 622 623
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(1, Context::EXTENSION_INDEX), context2,
        context2, start, start);
624 625 626 627
    t.CheckContextInputAndDepthChanges(store, context1, 0);
  }

  {
628 629 630
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(2, Context::EXTENSION_INDEX), context2,
        context2, start, start);
631 632 633 634
    t.CheckContextInputAndDepthChanges(store, context_object1, 0);
  }

  {
635 636 637
    Node* store = t.graph()->NewNode(
        t.javascript()->StoreContext(3, Context::EXTENSION_INDEX), context2,
        context2, start, start);
638 639 640
    t.CheckContextInputAndDepthChanges(store, context_object0, 0);
  }
}
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

TEST(SpecializeJSFunction_ToConstant1) {
  FunctionTester T(
      "(function() { var x = 1; function inc(a)"
      " { return a + x; } return inc; })()");

  T.CheckCall(1.0, 0.0, 0.0);
  T.CheckCall(2.0, 1.0, 0.0);
  T.CheckCall(2.1, 1.1, 0.0);
}


TEST(SpecializeJSFunction_ToConstant2) {
  FunctionTester T(
      "(function() { var x = 1.5; var y = 2.25; var z = 3.75;"
      " function f(a) { return a - x + y - z; } return f; })()");

  T.CheckCall(-3.0, 0.0, 0.0);
  T.CheckCall(-2.0, 1.0, 0.0);
  T.CheckCall(-1.9, 1.1, 0.0);
}


TEST(SpecializeJSFunction_ToConstant3) {
  FunctionTester T(
      "(function() { var x = -11.5; function inc()"
      " { return (function(a) { return a + x; }); }"
      " return inc(); })()");

  T.CheckCall(-11.5, 0.0, 0.0);
  T.CheckCall(-10.5, 1.0, 0.0);
  T.CheckCall(-10.4, 1.1, 0.0);
}


TEST(SpecializeJSFunction_ToConstant_uninit) {
  {
    FunctionTester T(
        "(function() { if (false) { var x = 1; } function inc(a)"
        " { return x; } return inc; })()");  // x is undefined!
681 682 683 684 685 686 687 688
    i::Isolate* isolate = CcTest::i_isolate();
    CHECK(
        T.Call(T.Val(0.0), T.Val(0.0)).ToHandleChecked()->IsUndefined(isolate));
    CHECK(
        T.Call(T.Val(2.0), T.Val(0.0)).ToHandleChecked()->IsUndefined(isolate));
    CHECK(T.Call(T.Val(-2.1), T.Val(0.0))
              .ToHandleChecked()
              ->IsUndefined(isolate));
689 690 691 692 693 694 695 696 697 698 699 700
  }

  {
    FunctionTester T(
        "(function() { if (false) { var x = 1; } function inc(a)"
        " { return a + x; } return inc; })()");  // x is undefined!

    CHECK(T.Call(T.Val(0.0), T.Val(0.0)).ToHandleChecked()->IsNaN());
    CHECK(T.Call(T.Val(2.0), T.Val(0.0)).ToHandleChecked()->IsNaN());
    CHECK(T.Call(T.Val(-2.1), T.Val(0.0)).ToHandleChecked()->IsNaN());
  }
}
701 702 703 704

}  // namespace compiler
}  // namespace internal
}  // namespace v8