verifier.cc 44.9 KB
Newer Older
1 2 3 4 5 6
// 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.

#include "src/compiler/verifier.h"

7
#include <algorithm>
8 9
#include <deque>
#include <queue>
10 11
#include <sstream>
#include <string>
12

13
#include "src/bit-vector.h"
14
#include "src/compiler/all-nodes.h"
15
#include "src/compiler/common-operator.h"
16 17 18 19 20
#include "src/compiler/graph.h"
#include "src/compiler/node.h"
#include "src/compiler/node-properties.h"
#include "src/compiler/opcodes.h"
#include "src/compiler/operator.h"
21
#include "src/compiler/operator-properties.h"
22
#include "src/compiler/schedule.h"
23
#include "src/compiler/simplified-operator.h"
24
#include "src/ostreams.h"
25 26 27 28 29 30 31

namespace v8 {
namespace internal {
namespace compiler {


static bool IsDefUseChainLinkPresent(Node* def, Node* use) {
32 33
  auto const uses = def->uses();
  return std::find(uses.begin(), uses.end(), use) != uses.end();
34 35 36 37
}


static bool IsUseDefChainLinkPresent(Node* def, Node* use) {
38 39
  auto const inputs = use->inputs();
  return std::find(inputs.begin(), inputs.end(), def) != inputs.end();
40 41 42
}


43
class Verifier::Visitor {
44
 public:
45
  Visitor(Zone* z, Typing typed) : zone(z), typing(typed) {}
46

47
  void Check(Node* node);
48

49 50 51 52
  Zone* zone;
  Typing typing;

 private:
53 54 55
  void CheckNotTyped(Node* node) {
    if (NodeProperties::IsTyped(node)) {
      std::ostringstream str;
56 57 58
      str << "TypeError: node #" << node->id() << ":" << *node->op()
          << " should never have a type";
      FATAL(str.str().c_str());
59 60 61
    }
  }
  void CheckUpperIs(Node* node, Type* type) {
62
    if (typing == TYPED && !NodeProperties::GetType(node)->Is(type)) {
63
      std::ostringstream str;
64
      str << "TypeError: node #" << node->id() << ":" << *node->op()
65
          << " type ";
66
      NodeProperties::GetType(node)->PrintTo(str);
67 68
      str << " is not ";
      type->PrintTo(str);
69
      FATAL(str.str().c_str());
70 71 72
    }
  }
  void CheckUpperMaybe(Node* node, Type* type) {
73
    if (typing == TYPED && !NodeProperties::GetType(node)->Maybe(type)) {
74
      std::ostringstream str;
75
      str << "TypeError: node #" << node->id() << ":" << *node->op()
76
          << " type ";
77
      NodeProperties::GetType(node)->PrintTo(str);
78 79
      str << " must intersect ";
      type->PrintTo(str);
80
      FATAL(str.str().c_str());
81 82 83
    }
  }
  void CheckValueInputIs(Node* node, int i, Type* type) {
84 85
    Node* input = NodeProperties::GetValueInput(node, i);
    if (typing == TYPED && !NodeProperties::GetType(input)->Is(type)) {
86
      std::ostringstream str;
87 88
      str << "TypeError: node #" << node->id() << ":" << *node->op()
          << "(input @" << i << " = " << input->opcode() << ":"
89
          << input->op()->mnemonic() << ") type ";
90
      NodeProperties::GetType(input)->PrintTo(str);
91 92
      str << " is not ";
      type->PrintTo(str);
93
      FATAL(str.str().c_str());
94 95
    }
  }
96 97 98 99 100 101 102 103 104
  void CheckOutput(Node* node, Node* use, int count, const char* kind) {
    if (count <= 0) {
      std::ostringstream str;
      str << "GraphError: node #" << node->id() << ":" << *node->op()
          << " does not produce " << kind << " output used by node #"
          << use->id() << ":" << *use->op();
      FATAL(str.str().c_str());
    }
  }
105 106 107
};


108
void Verifier::Visitor::Check(Node* node) {
109
  int value_count = node->op()->ValueInputCount();
110
  int context_count = OperatorProperties::GetContextInputCount(node->op());
111 112
  int frame_state_count =
      OperatorProperties::GetFrameStateInputCount(node->op());
113 114
  int effect_count = node->op()->EffectInputCount();
  int control_count = node->op()->ControlInputCount();
115 116

  // Verify number of inputs matches up.
117 118
  int input_count = value_count + context_count + frame_state_count +
                    effect_count + control_count;
119 120
  CHECK_EQ(input_count, node->InputCount());

121
  // Verify that frame state has been inserted for the nodes that need it.
122 123
  for (int i = 0; i < frame_state_count; i++) {
    Node* frame_state = NodeProperties::GetFrameStateInput(node, i);
124
    CHECK(frame_state->opcode() == IrOpcode::kFrameState ||
125
          // kFrameState uses Start as a sentinel.
126
          (node->opcode() == IrOpcode::kFrameState &&
127
           frame_state->opcode() == IrOpcode::kStart));
128 129 130 131
    CHECK(IsDefUseChainLinkPresent(frame_state, node));
    CHECK(IsUseDefChainLinkPresent(frame_state, node));
  }

132 133 134
  // Verify all value inputs actually produce a value.
  for (int i = 0; i < value_count; ++i) {
    Node* value = NodeProperties::GetValueInput(node, i);
135
    CheckOutput(value, node, value->op()->ValueOutputCount(), "value");
136 137 138 139 140 141 142
    CHECK(IsDefUseChainLinkPresent(value, node));
    CHECK(IsUseDefChainLinkPresent(value, node));
  }

  // Verify all context inputs are value nodes.
  for (int i = 0; i < context_count; ++i) {
    Node* context = NodeProperties::GetContextInput(node);
143
    CheckOutput(context, node, context->op()->ValueOutputCount(), "context");
144 145 146 147 148 149 150
    CHECK(IsDefUseChainLinkPresent(context, node));
    CHECK(IsUseDefChainLinkPresent(context, node));
  }

  // Verify all effect inputs actually have an effect.
  for (int i = 0; i < effect_count; ++i) {
    Node* effect = NodeProperties::GetEffectInput(node);
151
    CheckOutput(effect, node, effect->op()->EffectOutputCount(), "effect");
152 153 154 155 156 157 158
    CHECK(IsDefUseChainLinkPresent(effect, node));
    CHECK(IsUseDefChainLinkPresent(effect, node));
  }

  // Verify all control inputs are control nodes.
  for (int i = 0; i < control_count; ++i) {
    Node* control = NodeProperties::GetControlInput(node, i);
159
    CheckOutput(control, node, control->op()->ControlOutputCount(), "control");
160 161 162 163 164
    CHECK(IsDefUseChainLinkPresent(control, node));
    CHECK(IsUseDefChainLinkPresent(control, node));
  }

  // Verify all successors are projections if multiple value outputs exist.
165
  if (node->op()->ValueOutputCount() > 1) {
danno's avatar
danno committed
166 167 168 169 170
    for (Edge edge : node->use_edges()) {
      Node* use = edge.from();
      CHECK(!NodeProperties::IsValueEdge(edge) ||
            use->opcode() == IrOpcode::kProjection ||
            use->opcode() == IrOpcode::kParameter);
171 172 173
    }
  }

174 175 176 177 178 179 180 181 182 183
  switch (node->opcode()) {
    case IrOpcode::kStart:
      // Start has no inputs.
      CHECK_EQ(0, input_count);
      // Type is a tuple.
      // TODO(rossberg): Multiple outputs are currently typed as Internal.
      CheckUpperIs(node, Type::Internal());
      break;
    case IrOpcode::kEnd:
      // End has no outputs.
184 185 186
      CHECK(node->op()->ValueOutputCount() == 0);
      CHECK(node->op()->EffectOutputCount() == 0);
      CHECK(node->op()->ControlOutputCount() == 0);
187 188 189
      // Type is empty.
      CheckNotTyped(node);
      break;
190
    case IrOpcode::kDead:
191
      // Dead is never connected to the graph.
192
      UNREACHABLE();
193
      break;
194 195 196
    case IrOpcode::kBranch: {
      // Branch uses are IfTrue and IfFalse.
      int count_true = 0, count_false = 0;
197 198 199 200 201
      for (auto use : node->uses()) {
        CHECK(use->opcode() == IrOpcode::kIfTrue ||
              use->opcode() == IrOpcode::kIfFalse);
        if (use->opcode() == IrOpcode::kIfTrue) ++count_true;
        if (use->opcode() == IrOpcode::kIfFalse) ++count_false;
202
      }
203 204
      CHECK_EQ(1, count_true);
      CHECK_EQ(1, count_false);
205 206 207 208 209 210 211 212 213 214 215
      // Type is empty.
      CheckNotTyped(node);
      break;
    }
    case IrOpcode::kIfTrue:
    case IrOpcode::kIfFalse:
      CHECK_EQ(IrOpcode::kBranch,
               NodeProperties::GetControlInput(node, 0)->opcode());
      // Type is empty.
      CheckNotTyped(node);
      break;
216
    case IrOpcode::kIfSuccess: {
217 218 219 220 221 222 223
      // IfSuccess and IfException continuation only on throwing nodes.
      Node* input = NodeProperties::GetControlInput(node, 0);
      CHECK(!input->op()->HasProperty(Operator::kNoThrow));
      // Type is empty.
      CheckNotTyped(node);
      break;
    }
224 225 226 227 228 229 230 231
    case IrOpcode::kIfException: {
      // IfSuccess and IfException continuation only on throwing nodes.
      Node* input = NodeProperties::GetControlInput(node, 0);
      CHECK(!input->op()->HasProperty(Operator::kNoThrow));
      // Type can be anything.
      CheckUpperIs(node, Type::Any());
      break;
    }
232
    case IrOpcode::kSwitch: {
233 234
      // Switch uses are Case and Default.
      int count_case = 0, count_default = 0;
235
      for (auto use : node->uses()) {
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        switch (use->opcode()) {
          case IrOpcode::kIfValue: {
            for (auto user : node->uses()) {
              if (user != use && user->opcode() == IrOpcode::kIfValue) {
                CHECK_NE(OpParameter<int32_t>(use->op()),
                         OpParameter<int32_t>(user->op()));
              }
            }
            ++count_case;
            break;
          }
          case IrOpcode::kIfDefault: {
            ++count_default;
            break;
          }
          default: {
252 253
            V8_Fatal(__FILE__, __LINE__, "Switch #%d illegally used by #%d:%s",
                     node->id(), use->id(), use->op()->mnemonic());
254 255 256
            break;
          }
        }
257
      }
258 259
      CHECK_EQ(1, count_default);
      CHECK_EQ(node->op()->ControlOutputCount(), count_case + count_default);
260 261 262 263
      // Type is empty.
      CheckNotTyped(node);
      break;
    }
264 265
    case IrOpcode::kIfValue:
    case IrOpcode::kIfDefault:
266 267 268 269 270
      CHECK_EQ(IrOpcode::kSwitch,
               NodeProperties::GetControlInput(node)->opcode());
      // Type is empty.
      CheckNotTyped(node);
      break;
271 272 273 274 275 276
    case IrOpcode::kLoop:
    case IrOpcode::kMerge:
      CHECK_EQ(control_count, input_count);
      // Type is empty.
      CheckNotTyped(node);
      break;
277
    case IrOpcode::kDeoptimize:
278
    case IrOpcode::kReturn:
279 280
    case IrOpcode::kThrow:
      // Deoptimize, Return and Throw uses are End.
281 282 283
      for (auto use : node->uses()) {
        CHECK_EQ(IrOpcode::kEnd, use->opcode());
      }
284 285 286
      // Type is empty.
      CheckNotTyped(node);
      break;
287
    case IrOpcode::kTerminate:
288 289 290 291
      // Terminates take one loop and effect.
      CHECK_EQ(1, control_count);
      CHECK_EQ(1, effect_count);
      CHECK_EQ(2, input_count);
292 293
      CHECK_EQ(IrOpcode::kLoop,
               NodeProperties::GetControlInput(node)->opcode());
294 295 296 297
      // Terminate uses are End.
      for (auto use : node->uses()) {
        CHECK_EQ(IrOpcode::kEnd, use->opcode());
      }
298 299 300
      // Type is empty.
      CheckNotTyped(node);
      break;
301 302
    case IrOpcode::kOsrNormalEntry:
    case IrOpcode::kOsrLoopEntry:
303
      // Osr entries take one control and effect.
304
      CHECK_EQ(1, control_count);
305 306
      CHECK_EQ(1, effect_count);
      CHECK_EQ(2, input_count);
307 308 309
      // Type is empty.
      CheckNotTyped(node);
      break;
310 311 312 313 314 315 316

    // Common operators
    // ----------------
    case IrOpcode::kParameter: {
      // Parameters have the start node as inputs.
      CHECK_EQ(1, input_count);
      // Parameter has an input that produces enough values.
317 318 319
      int const index = ParameterIndexOf(node->op());
      Node* const start = NodeProperties::GetValueInput(node, 0);
      CHECK_EQ(IrOpcode::kStart, start->opcode());
320
      // Currently, parameter indices start at -1 instead of 0.
321 322
      CHECK_LE(-1, index);
      CHECK_LT(index + 1, start->op()->ValueOutputCount());
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
      // Type can be anything.
      CheckUpperIs(node, Type::Any());
      break;
    }
    case IrOpcode::kInt32Constant:  // TODO(rossberg): rename Word32Constant?
      // Constants have no inputs.
      CHECK_EQ(0, input_count);
      // Type is a 32 bit integer, signed or unsigned.
      CheckUpperIs(node, Type::Integral32());
      break;
    case IrOpcode::kInt64Constant:
      // Constants have no inputs.
      CHECK_EQ(0, input_count);
      // Type is internal.
      // TODO(rossberg): Introduce proper Int64 type.
      CheckUpperIs(node, Type::Internal());
      break;
    case IrOpcode::kFloat32Constant:
    case IrOpcode::kFloat64Constant:
    case IrOpcode::kNumberConstant:
      // Constants have no inputs.
      CHECK_EQ(0, input_count);
      // Type is a number.
      CheckUpperIs(node, Type::Number());
      break;
    case IrOpcode::kHeapConstant:
      // Constants have no inputs.
      CHECK_EQ(0, input_count);
      // Type can be anything represented as a heap pointer.
352
      CheckUpperIs(node, Type::TaggedPointer());
353 354 355 356 357 358 359
      break;
    case IrOpcode::kExternalConstant:
      // Constants have no inputs.
      CHECK_EQ(0, input_count);
      // Type is considered internal.
      CheckUpperIs(node, Type::Internal());
      break;
360 361 362 363 364 365 366
    case IrOpcode::kOsrValue:
      // OSR values have a value and a control input.
      CHECK_EQ(1, control_count);
      CHECK_EQ(1, input_count);
      // Type is merged from other values in the graph and could be any.
      CheckUpperIs(node, Type::Any());
      break;
367 368
    case IrOpcode::kProjection: {
      // Projection has an input that produces enough values.
369
      int index = static_cast<int>(ProjectionIndexOf(node->op()));
370
      Node* input = NodeProperties::GetValueInput(node, 0);
371
      CHECK_GT(input->op()->ValueOutputCount(), index);
372 373 374 375 376 377
      // Type can be anything.
      // TODO(rossberg): Introduce tuple types for this.
      // TODO(titzer): Convince rossberg not to.
      CheckUpperIs(node, Type::Any());
      break;
    }
378 379 380 381 382 383
    case IrOpcode::kSelect: {
      CHECK_EQ(0, effect_count);
      CHECK_EQ(0, control_count);
      CHECK_EQ(3, value_count);
      break;
    }
384 385 386 387 388
    case IrOpcode::kPhi: {
      // Phi input count matches parent control node.
      CHECK_EQ(0, effect_count);
      CHECK_EQ(1, control_count);
      Node* control = NodeProperties::GetControlInput(node, 0);
389
      CHECK_EQ(value_count, control->op()->ControlInputCount());
390 391 392 393 394
      CHECK_EQ(input_count, 1 + value_count);
      // Type must be subsumed by all input types.
      // TODO(rossberg): for now at least, narrowing does not really hold.
      /*
      for (int i = 0; i < value_count; ++i) {
395
        CHECK(type_of(ValueInput(node, i))->Is(type_of(node)));
396
      }
397 398 399 400 401 402 403 404
      */
      break;
    }
    case IrOpcode::kEffectPhi: {
      // EffectPhi input count matches parent control node.
      CHECK_EQ(0, value_count);
      CHECK_EQ(1, control_count);
      Node* control = NodeProperties::GetControlInput(node, 0);
405
      CHECK_EQ(effect_count, control->op()->ControlInputCount());
406 407 408
      CHECK_EQ(input_count, 1 + effect_count);
      break;
    }
409 410 411 412 413 414
    case IrOpcode::kEffectSet: {
      CHECK_EQ(0, value_count);
      CHECK_EQ(0, control_count);
      CHECK_LT(1, effect_count);
      break;
    }
415 416 417
    case IrOpcode::kGuard:
      // TODO(bmeurer): what are the constraints on these?
      break;
418
    case IrOpcode::kBeginRegion:
419 420
      // TODO(rossberg): what are the constraints on these?
      break;
421
    case IrOpcode::kFinishRegion: {
422 423 424
      // TODO(rossberg): what are the constraints on these?
      // Type must be subsumed by input type.
      if (typing == TYPED) {
425 426
        Node* val = NodeProperties::GetValueInput(node, 0);
        CHECK(NodeProperties::GetType(val)->Is(NodeProperties::GetType(node)));
427
      }
428 429
      break;
    }
430
    case IrOpcode::kFrameState: {
431
      // TODO(jarin): what are the constraints on these?
432 433 434 435
      CHECK_EQ(5, value_count);
      CHECK_EQ(0, control_count);
      CHECK_EQ(0, effect_count);
      CHECK_EQ(6, input_count);
436 437 438 439 440 441
      for (int i = 0; i < 3; ++i) {
        CHECK(NodeProperties::GetValueInput(node, i)->opcode() ==
                  IrOpcode::kStateValues ||
              NodeProperties::GetValueInput(node, i)->opcode() ==
                  IrOpcode::kTypedStateValues);
      }
442
      break;
443
    }
444
    case IrOpcode::kStateValues:
445
    case IrOpcode::kObjectState:
446
    case IrOpcode::kTypedStateValues:
447 448 449 450 451
      // TODO(jarin): what are the constraints on these?
      break;
    case IrOpcode::kCall:
      // TODO(rossberg): what are the constraints on these?
      break;
452 453 454
    case IrOpcode::kTailCall:
      // TODO(bmeurer): what are the constraints on these?
      break;
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

    // JavaScript operators
    // --------------------
    case IrOpcode::kJSEqual:
    case IrOpcode::kJSNotEqual:
    case IrOpcode::kJSStrictEqual:
    case IrOpcode::kJSStrictNotEqual:
    case IrOpcode::kJSLessThan:
    case IrOpcode::kJSGreaterThan:
    case IrOpcode::kJSLessThanOrEqual:
    case IrOpcode::kJSGreaterThanOrEqual:
      // Type is Boolean.
      CheckUpperIs(node, Type::Boolean());
      break;

    case IrOpcode::kJSBitwiseOr:
    case IrOpcode::kJSBitwiseXor:
    case IrOpcode::kJSBitwiseAnd:
    case IrOpcode::kJSShiftLeft:
    case IrOpcode::kJSShiftRight:
    case IrOpcode::kJSShiftRightLogical:
      // Type is 32 bit integral.
      CheckUpperIs(node, Type::Integral32());
      break;
    case IrOpcode::kJSAdd:
      // Type is Number or String.
      CheckUpperIs(node, Type::NumberOrString());
      break;
    case IrOpcode::kJSSubtract:
    case IrOpcode::kJSMultiply:
    case IrOpcode::kJSDivide:
    case IrOpcode::kJSModulus:
      // Type is Number.
      CheckUpperIs(node, Type::Number());
      break;

    case IrOpcode::kJSToBoolean:
      // Type is Boolean.
      CheckUpperIs(node, Type::Boolean());
      break;
    case IrOpcode::kJSToNumber:
      // Type is Number.
      CheckUpperIs(node, Type::Number());
      break;
    case IrOpcode::kJSToString:
      // Type is String.
      CheckUpperIs(node, Type::String());
      break;
    case IrOpcode::kJSToName:
      // Type is Name.
      CheckUpperIs(node, Type::Name());
      break;
    case IrOpcode::kJSToObject:
      // Type is Receiver.
      CheckUpperIs(node, Type::Receiver());
      break;

    case IrOpcode::kJSCreate:
      // Type is Object.
      CheckUpperIs(node, Type::Object());
      break;
516 517 518 519
    case IrOpcode::kJSCreateArguments:
      // Type is OtherObject.
      CheckUpperIs(node, Type::OtherObject());
      break;
520 521 522 523
    case IrOpcode::kJSCreateArray:
      // Type is OtherObject.
      CheckUpperIs(node, Type::OtherObject());
      break;
524 525
    case IrOpcode::kJSCreateClosure:
      // Type is Function.
526
      CheckUpperIs(node, Type::Function());
527
      break;
528 529 530 531
    case IrOpcode::kJSCreateIterResultObject:
      // Type is OtherObject.
      CheckUpperIs(node, Type::OtherObject());
      break;
532 533
    case IrOpcode::kJSCreateLiteralArray:
    case IrOpcode::kJSCreateLiteralObject:
534
    case IrOpcode::kJSCreateLiteralRegExp:
535 536 537
      // Type is OtherObject.
      CheckUpperIs(node, Type::OtherObject());
      break;
538 539
    case IrOpcode::kJSLoadProperty:
    case IrOpcode::kJSLoadNamed:
540
    case IrOpcode::kJSLoadGlobal:
541 542 543 544 545
      // Type can be anything.
      CheckUpperIs(node, Type::Any());
      break;
    case IrOpcode::kJSStoreProperty:
    case IrOpcode::kJSStoreNamed:
546
    case IrOpcode::kJSStoreGlobal:
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
      // Type is empty.
      CheckNotTyped(node);
      break;
    case IrOpcode::kJSDeleteProperty:
    case IrOpcode::kJSHasProperty:
    case IrOpcode::kJSInstanceOf:
      // Type is Boolean.
      CheckUpperIs(node, Type::Boolean());
      break;
    case IrOpcode::kJSTypeOf:
      // Type is String.
      CheckUpperIs(node, Type::String());
      break;

    case IrOpcode::kJSLoadContext:
      // Type can be anything.
      CheckUpperIs(node, Type::Any());
      break;
    case IrOpcode::kJSStoreContext:
      // Type is empty.
      CheckNotTyped(node);
      break;
    case IrOpcode::kJSCreateFunctionContext:
    case IrOpcode::kJSCreateCatchContext:
    case IrOpcode::kJSCreateWithContext:
    case IrOpcode::kJSCreateBlockContext:
    case IrOpcode::kJSCreateModuleContext:
574
    case IrOpcode::kJSCreateScriptContext: {
575 576 577 578 579
      // Type is Context, and operand is Internal.
      Node* context = NodeProperties::GetContextInput(node);
      // TODO(rossberg): This should really be Is(Internal), but the typer
      // currently can't do backwards propagation.
      CheckUpperMaybe(context, Type::Internal());
580
      if (typing == TYPED) CHECK(NodeProperties::GetType(node)->IsContext());
581 582
      break;
    }
583

584
    case IrOpcode::kJSCallConstruct:
585
    case IrOpcode::kJSConvertReceiver:
586 587 588 589 590 591 592 593 594 595
      // Type is Receiver.
      CheckUpperIs(node, Type::Receiver());
      break;
    case IrOpcode::kJSCallFunction:
    case IrOpcode::kJSCallRuntime:
    case IrOpcode::kJSYield:
      // Type can be anything.
      CheckUpperIs(node, Type::Any());
      break;

596 597 598 599 600 601
    case IrOpcode::kJSForInPrepare: {
      // TODO(bmeurer): What are the constraints on thse?
      CheckUpperIs(node, Type::Any());
      break;
    }
    case IrOpcode::kJSForInDone: {
602 603 604
      // TODO(bmeurer): OSR breaks this invariant, although the node is not user
      // visible, so we know it is safe (fullcodegen has an unsigned smi there).
      // CheckValueInputIs(node, 0, Type::UnsignedSmall());
605 606 607
      break;
    }
    case IrOpcode::kJSForInNext: {
608
      CheckUpperIs(node, Type::Union(Type::Name(), Type::Undefined(), zone));
609 610 611
      break;
    }
    case IrOpcode::kJSForInStep: {
612 613 614
      // TODO(bmeurer): OSR breaks this invariant, although the node is not user
      // visible, so we know it is safe (fullcodegen has an unsigned smi there).
      // CheckValueInputIs(node, 0, Type::UnsignedSmall());
615 616 617 618
      CheckUpperIs(node, Type::UnsignedSmall());
      break;
    }

619 620 621 622
    case IrOpcode::kJSLoadMessage:
    case IrOpcode::kJSStoreMessage:
      break;

623 624 625 626 627
    case IrOpcode::kJSStackCheck:
      // Type is empty.
      CheckNotTyped(node);
      break;

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    // Simplified operators
    // -------------------------------
    case IrOpcode::kBooleanNot:
      // Boolean -> Boolean
      CheckValueInputIs(node, 0, Type::Boolean());
      CheckUpperIs(node, Type::Boolean());
      break;
    case IrOpcode::kBooleanToNumber:
      // Boolean -> Number
      CheckValueInputIs(node, 0, Type::Boolean());
      CheckUpperIs(node, Type::Number());
      break;
    case IrOpcode::kNumberEqual:
    case IrOpcode::kNumberLessThan:
    case IrOpcode::kNumberLessThanOrEqual:
      // (Number, Number) -> Boolean
      CheckValueInputIs(node, 0, Type::Number());
      CheckValueInputIs(node, 1, Type::Number());
      CheckUpperIs(node, Type::Boolean());
      break;
    case IrOpcode::kNumberAdd:
    case IrOpcode::kNumberSubtract:
    case IrOpcode::kNumberMultiply:
    case IrOpcode::kNumberDivide:
    case IrOpcode::kNumberModulus:
      // (Number, Number) -> Number
      CheckValueInputIs(node, 0, Type::Number());
      CheckValueInputIs(node, 1, Type::Number());
      // TODO(rossberg): activate once we retype after opcode changes.
      // CheckUpperIs(node, Type::Number());
      break;
659 660 661 662 663 664 665 666
    case IrOpcode::kNumberBitwiseOr:
    case IrOpcode::kNumberBitwiseXor:
    case IrOpcode::kNumberBitwiseAnd:
      // (Signed32, Signed32) -> Signed32
      CheckValueInputIs(node, 0, Type::Signed32());
      CheckValueInputIs(node, 1, Type::Signed32());
      CheckUpperIs(node, Type::Signed32());
      break;
667 668 669 670 671 672 673 674 675 676 677 678 679
    case IrOpcode::kNumberShiftLeft:
    case IrOpcode::kNumberShiftRight:
      // (Signed32, Unsigned32) -> Signed32
      CheckValueInputIs(node, 0, Type::Signed32());
      CheckValueInputIs(node, 1, Type::Unsigned32());
      CheckUpperIs(node, Type::Signed32());
      break;
    case IrOpcode::kNumberShiftRightLogical:
      // (Unsigned32, Unsigned32) -> Unsigned32
      CheckValueInputIs(node, 0, Type::Unsigned32());
      CheckValueInputIs(node, 1, Type::Unsigned32());
      CheckUpperIs(node, Type::Unsigned32());
      break;
680 681 682 683 684 685 686 687 688 689
    case IrOpcode::kNumberToInt32:
      // Number -> Signed32
      CheckValueInputIs(node, 0, Type::Number());
      CheckUpperIs(node, Type::Signed32());
      break;
    case IrOpcode::kNumberToUint32:
      // Number -> Unsigned32
      CheckValueInputIs(node, 0, Type::Number());
      CheckUpperIs(node, Type::Unsigned32());
      break;
690 691 692 693 694
    case IrOpcode::kNumberIsHoleNaN:
      // Number -> Boolean
      CheckValueInputIs(node, 0, Type::Number());
      CheckUpperIs(node, Type::Boolean());
      break;
695 696 697 698 699
    case IrOpcode::kPlainPrimitiveToNumber:
      // PlainPrimitive -> Number
      CheckValueInputIs(node, 0, Type::PlainPrimitive());
      CheckUpperIs(node, Type::Number());
      break;
700 701 702 703 704 705 706 707 708 709 710 711 712
    case IrOpcode::kStringEqual:
    case IrOpcode::kStringLessThan:
    case IrOpcode::kStringLessThanOrEqual:
      // (String, String) -> Boolean
      CheckValueInputIs(node, 0, Type::String());
      CheckValueInputIs(node, 1, Type::String());
      CheckUpperIs(node, Type::Boolean());
      break;
    case IrOpcode::kReferenceEqual: {
      // (Unique, Any) -> Boolean  and
      // (Any, Unique) -> Boolean
      CheckUpperIs(node, Type::Boolean());
      break;
713
    }
714
    case IrOpcode::kObjectIsNumber:
715
    case IrOpcode::kObjectIsReceiver:
716 717 718 719
    case IrOpcode::kObjectIsSmi:
      CheckValueInputIs(node, 0, Type::Any());
      CheckUpperIs(node, Type::Boolean());
      break;
720 721 722 723
    case IrOpcode::kAllocate:
      CheckValueInputIs(node, 0, Type::PlainNumber());
      CheckUpperIs(node, Type::TaggedPointer());
      break;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 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

    case IrOpcode::kChangeTaggedToInt32: {
      // Signed32 /\ Tagged -> Signed32 /\ UntaggedInt32
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from = Type::Intersect(Type::Signed32(), Type::Tagged());
      // Type* to = Type::Intersect(Type::Signed32(), Type::UntaggedInt32());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
      break;
    }
    case IrOpcode::kChangeTaggedToUint32: {
      // Unsigned32 /\ Tagged -> Unsigned32 /\ UntaggedInt32
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from = Type::Intersect(Type::Unsigned32(), Type::Tagged());
      // Type* to =Type::Intersect(Type::Unsigned32(), Type::UntaggedInt32());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
      break;
    }
    case IrOpcode::kChangeTaggedToFloat64: {
      // Number /\ Tagged -> Number /\ UntaggedFloat64
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from = Type::Intersect(Type::Number(), Type::Tagged());
      // Type* to = Type::Intersect(Type::Number(), Type::UntaggedFloat64());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
      break;
    }
    case IrOpcode::kChangeInt32ToTagged: {
      // Signed32 /\ UntaggedInt32 -> Signed32 /\ Tagged
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from =Type::Intersect(Type::Signed32(), Type::UntaggedInt32());
      // Type* to = Type::Intersect(Type::Signed32(), Type::Tagged());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
      break;
    }
    case IrOpcode::kChangeUint32ToTagged: {
      // Unsigned32 /\ UntaggedInt32 -> Unsigned32 /\ Tagged
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from=Type::Intersect(Type::Unsigned32(),Type::UntaggedInt32());
      // Type* to = Type::Intersect(Type::Unsigned32(), Type::Tagged());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
      break;
    }
    case IrOpcode::kChangeFloat64ToTagged: {
      // Number /\ UntaggedFloat64 -> Number /\ Tagged
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from =Type::Intersect(Type::Number(), Type::UntaggedFloat64());
      // Type* to = Type::Intersect(Type::Number(), Type::Tagged());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
      break;
    }
    case IrOpcode::kChangeBoolToBit: {
      // Boolean /\ TaggedPtr -> Boolean /\ UntaggedInt1
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from = Type::Intersect(Type::Boolean(), Type::TaggedPtr());
      // Type* to = Type::Intersect(Type::Boolean(), Type::UntaggedInt1());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
      break;
    }
788 789 790 791 792 793 794
    case IrOpcode::kChangeBitToBool: {
      // Boolean /\ UntaggedInt1 -> Boolean /\ TaggedPtr
      // TODO(neis): Activate once ChangeRepresentation works in typer.
      // Type* from = Type::Intersect(Type::Boolean(), Type::UntaggedInt1());
      // Type* to = Type::Intersect(Type::Boolean(), Type::TaggedPtr());
      // CheckValueInputIs(node, 0, from));
      // CheckUpperIs(node, to));
795 796 797 798 799 800 801
      break;
    }

    case IrOpcode::kLoadField:
      // Object -> fieldtype
      // TODO(rossberg): activate once machine ops are typed.
      // CheckValueInputIs(node, 0, Type::Object());
802
      // CheckUpperIs(node, FieldAccessOf(node->op()).type));
803
      break;
804 805
    case IrOpcode::kLoadBuffer:
      break;
806 807 808 809
    case IrOpcode::kLoadElement:
      // Object -> elementtype
      // TODO(rossberg): activate once machine ops are typed.
      // CheckValueInputIs(node, 0, Type::Object());
810
      // CheckUpperIs(node, ElementAccessOf(node->op()).type));
811 812 813 814 815
      break;
    case IrOpcode::kStoreField:
      // (Object, fieldtype) -> _|_
      // TODO(rossberg): activate once machine ops are typed.
      // CheckValueInputIs(node, 0, Type::Object());
816
      // CheckValueInputIs(node, 1, FieldAccessOf(node->op()).type));
817 818
      CheckNotTyped(node);
      break;
819 820
    case IrOpcode::kStoreBuffer:
      break;
821 822 823 824
    case IrOpcode::kStoreElement:
      // (Object, elementtype) -> _|_
      // TODO(rossberg): activate once machine ops are typed.
      // CheckValueInputIs(node, 0, Type::Object());
825
      // CheckValueInputIs(node, 1, ElementAccessOf(node->op()).type));
826 827 828 829 830 831 832
      CheckNotTyped(node);
      break;

    // Machine operators
    // -----------------------
    case IrOpcode::kLoad:
    case IrOpcode::kStore:
833
    case IrOpcode::kStackSlot:
834 835 836 837 838 839 840 841
    case IrOpcode::kWord32And:
    case IrOpcode::kWord32Or:
    case IrOpcode::kWord32Xor:
    case IrOpcode::kWord32Shl:
    case IrOpcode::kWord32Shr:
    case IrOpcode::kWord32Sar:
    case IrOpcode::kWord32Ror:
    case IrOpcode::kWord32Equal:
842
    case IrOpcode::kWord32Clz:
843
    case IrOpcode::kWord32Ctz:
844
    case IrOpcode::kWord32ReverseBits:
845
    case IrOpcode::kWord32Popcnt:
846 847 848 849 850 851 852
    case IrOpcode::kWord64And:
    case IrOpcode::kWord64Or:
    case IrOpcode::kWord64Xor:
    case IrOpcode::kWord64Shl:
    case IrOpcode::kWord64Shr:
    case IrOpcode::kWord64Sar:
    case IrOpcode::kWord64Ror:
853
    case IrOpcode::kWord64Clz:
854
    case IrOpcode::kWord64Popcnt:
855
    case IrOpcode::kWord64Ctz:
856
    case IrOpcode::kWord64ReverseBits:
857 858 859 860 861 862 863 864 865 866 867 868 869
    case IrOpcode::kWord64Equal:
    case IrOpcode::kInt32Add:
    case IrOpcode::kInt32AddWithOverflow:
    case IrOpcode::kInt32Sub:
    case IrOpcode::kInt32SubWithOverflow:
    case IrOpcode::kInt32Mul:
    case IrOpcode::kInt32MulHigh:
    case IrOpcode::kInt32Div:
    case IrOpcode::kInt32Mod:
    case IrOpcode::kInt32LessThan:
    case IrOpcode::kInt32LessThanOrEqual:
    case IrOpcode::kUint32Div:
    case IrOpcode::kUint32Mod:
870
    case IrOpcode::kUint32MulHigh:
871 872 873
    case IrOpcode::kUint32LessThan:
    case IrOpcode::kUint32LessThanOrEqual:
    case IrOpcode::kInt64Add:
874
    case IrOpcode::kInt64AddWithOverflow:
875
    case IrOpcode::kInt64Sub:
876
    case IrOpcode::kInt64SubWithOverflow:
877 878 879 880 881 882 883 884
    case IrOpcode::kInt64Mul:
    case IrOpcode::kInt64Div:
    case IrOpcode::kInt64Mod:
    case IrOpcode::kInt64LessThan:
    case IrOpcode::kInt64LessThanOrEqual:
    case IrOpcode::kUint64Div:
    case IrOpcode::kUint64Mod:
    case IrOpcode::kUint64LessThan:
885
    case IrOpcode::kUint64LessThanOrEqual:
886 887 888 889 890 891
    case IrOpcode::kFloat32Add:
    case IrOpcode::kFloat32Sub:
    case IrOpcode::kFloat32Mul:
    case IrOpcode::kFloat32Div:
    case IrOpcode::kFloat32Max:
    case IrOpcode::kFloat32Min:
892
    case IrOpcode::kFloat32Abs:
893 894 895 896
    case IrOpcode::kFloat32Sqrt:
    case IrOpcode::kFloat32Equal:
    case IrOpcode::kFloat32LessThan:
    case IrOpcode::kFloat32LessThanOrEqual:
897 898 899 900 901
    case IrOpcode::kFloat64Add:
    case IrOpcode::kFloat64Sub:
    case IrOpcode::kFloat64Mul:
    case IrOpcode::kFloat64Div:
    case IrOpcode::kFloat64Mod:
902 903
    case IrOpcode::kFloat64Max:
    case IrOpcode::kFloat64Min:
904
    case IrOpcode::kFloat64Abs:
905
    case IrOpcode::kFloat64Sqrt:
906
    case IrOpcode::kFloat32RoundDown:
907
    case IrOpcode::kFloat64RoundDown:
908
    case IrOpcode::kFloat32RoundUp:
909
    case IrOpcode::kFloat64RoundUp:
910
    case IrOpcode::kFloat32RoundTruncate:
911 912
    case IrOpcode::kFloat64RoundTruncate:
    case IrOpcode::kFloat64RoundTiesAway:
913
    case IrOpcode::kFloat32RoundTiesEven:
914
    case IrOpcode::kFloat64RoundTiesEven:
915 916 917 918
    case IrOpcode::kFloat64Equal:
    case IrOpcode::kFloat64LessThan:
    case IrOpcode::kFloat64LessThanOrEqual:
    case IrOpcode::kTruncateInt64ToInt32:
919
    case IrOpcode::kRoundInt32ToFloat32:
920
    case IrOpcode::kRoundInt64ToFloat32:
921
    case IrOpcode::kRoundInt64ToFloat64:
922
    case IrOpcode::kRoundUint32ToFloat32:
923
    case IrOpcode::kRoundUint64ToFloat64:
924
    case IrOpcode::kRoundUint64ToFloat32:
925 926
    case IrOpcode::kTruncateFloat64ToFloat32:
    case IrOpcode::kTruncateFloat64ToInt32:
927 928 929 930
    case IrOpcode::kBitcastFloat32ToInt32:
    case IrOpcode::kBitcastFloat64ToInt64:
    case IrOpcode::kBitcastInt32ToFloat32:
    case IrOpcode::kBitcastInt64ToFloat64:
931 932 933 934 935 936 937
    case IrOpcode::kChangeInt32ToInt64:
    case IrOpcode::kChangeUint32ToUint64:
    case IrOpcode::kChangeInt32ToFloat64:
    case IrOpcode::kChangeUint32ToFloat64:
    case IrOpcode::kChangeFloat32ToFloat64:
    case IrOpcode::kChangeFloat64ToInt32:
    case IrOpcode::kChangeFloat64ToUint32:
938
    case IrOpcode::kTruncateFloat32ToInt32:
939
    case IrOpcode::kTruncateFloat32ToUint32:
940
    case IrOpcode::kTryTruncateFloat32ToInt64:
941
    case IrOpcode::kTryTruncateFloat64ToInt64:
942
    case IrOpcode::kTryTruncateFloat32ToUint64:
943
    case IrOpcode::kTryTruncateFloat64ToUint64:
944 945 946 947
    case IrOpcode::kFloat64ExtractLowWord32:
    case IrOpcode::kFloat64ExtractHighWord32:
    case IrOpcode::kFloat64InsertLowWord32:
    case IrOpcode::kFloat64InsertHighWord32:
948
    case IrOpcode::kLoadStackPointer:
949
    case IrOpcode::kLoadFramePointer:
950
    case IrOpcode::kLoadParentFramePointer:
951 952
    case IrOpcode::kCheckedLoad:
    case IrOpcode::kCheckedStore:
953 954
      // TODO(rossberg): Check.
      break;
955
  }
956
}  // NOLINT(readability/fn_size)
957 958


959
void Verifier::Run(Graph* graph, Typing typing) {
960 961
  CHECK_NOT_NULL(graph->start());
  CHECK_NOT_NULL(graph->end());
962 963
  Zone zone;
  Visitor visitor(&zone, typing);
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
  AllNodes all(&zone, graph);
  for (Node* node : all.live) visitor.Check(node);

  // Check the uniqueness of projections.
  for (Node* proj : all.live) {
    if (proj->opcode() != IrOpcode::kProjection) continue;
    Node* node = proj->InputAt(0);
    for (Node* other : node->uses()) {
      if (all.IsLive(other) && other != proj &&
          other->opcode() == IrOpcode::kProjection &&
          ProjectionIndexOf(other->op()) == ProjectionIndexOf(proj->op())) {
        V8_Fatal(__FILE__, __LINE__,
                 "Node #%d:%s has duplicate projections #%d and #%d",
                 node->id(), node->op()->mnemonic(), proj->id(), other->id());
      }
    }
  }
981
}
982 983


984 985
// -----------------------------------------------------------------------------

986 987 988 989 990 991
static bool HasDominatingDef(Schedule* schedule, Node* node,
                             BasicBlock* container, BasicBlock* use_block,
                             int use_pos) {
  BasicBlock* block = use_block;
  while (true) {
    while (use_pos >= 0) {
992
      if (block->NodeAt(use_pos) == node) return true;
993 994
      use_pos--;
    }
995
    block = block->dominator();
996
    if (block == nullptr) break;
997 998
    use_pos = static_cast<int>(block->NodeCount()) - 1;
    if (node == block->control_input()) return true;
999 1000 1001 1002 1003
  }
  return false;
}


1004 1005 1006
static bool Dominates(Schedule* schedule, Node* dominator, Node* dominatee) {
  BasicBlock* dom = schedule->block(dominator);
  BasicBlock* sub = schedule->block(dominatee);
1007
  while (sub != nullptr) {
1008 1009 1010 1011 1012 1013 1014 1015 1016
    if (sub == dom) {
      return true;
    }
    sub = sub->dominator();
  }
  return false;
}


1017 1018
static void CheckInputsDominate(Schedule* schedule, BasicBlock* block,
                                Node* node, int use_pos) {
1019
  for (int j = node->op()->ValueInputCount() - 1; j >= 0; j--) {
1020 1021 1022
    BasicBlock* use_block = block;
    if (node->opcode() == IrOpcode::kPhi) {
      use_block = use_block->PredecessorAt(j);
1023
      use_pos = static_cast<int>(use_block->NodeCount()) - 1;
1024 1025 1026 1027 1028 1029
    }
    Node* input = node->InputAt(j);
    if (!HasDominatingDef(schedule, node->InputAt(j), block, use_block,
                          use_pos)) {
      V8_Fatal(__FILE__, __LINE__,
               "Node #%d:%s in B%d is not dominated by input@%d #%d:%s",
1030
               node->id(), node->op()->mnemonic(), block->rpo_number(), j,
1031
               input->id(), input->op()->mnemonic());
1032
    }
1033 1034 1035 1036
  }
  // Ensure that nodes are dominated by their control inputs;
  // kEnd is an exception, as unreachable blocks resulting from kMerge
  // are not in the RPO.
1037
  if (node->op()->ControlInputCount() == 1 &&
1038 1039 1040 1041 1042
      node->opcode() != IrOpcode::kEnd) {
    Node* ctl = NodeProperties::GetControlInput(node);
    if (!Dominates(schedule, ctl, node)) {
      V8_Fatal(__FILE__, __LINE__,
               "Node #%d:%s in B%d is not dominated by control input #%d:%s",
1043 1044
               node->id(), node->op()->mnemonic(), block->rpo_number(),
               ctl->id(), ctl->op()->mnemonic());
1045
    }
1046 1047 1048 1049 1050
  }
}


void ScheduleVerifier::Run(Schedule* schedule) {
1051
  const size_t count = schedule->BasicBlockCount();
1052
  Zone tmp_zone;
1053 1054 1055 1056 1057
  Zone* zone = &tmp_zone;
  BasicBlock* start = schedule->start();
  BasicBlockVector* rpo_order = schedule->rpo_order();

  // Verify the RPO order contains only blocks from this schedule.
1058
  CHECK_GE(count, rpo_order->size());
1059 1060 1061
  for (BasicBlockVector::iterator b = rpo_order->begin(); b != rpo_order->end();
       ++b) {
    CHECK_EQ((*b), schedule->GetBlockById((*b)->id()));
1062
    // All predecessors and successors should be in rpo and in this schedule.
1063 1064 1065
    for (BasicBlock const* predecessor : (*b)->predecessors()) {
      CHECK_GE(predecessor->rpo_number(), 0);
      CHECK_EQ(predecessor, schedule->GetBlockById(predecessor->id()));
1066
    }
1067 1068 1069
    for (BasicBlock const* successor : (*b)->successors()) {
      CHECK_GE(successor->rpo_number(), 0);
      CHECK_EQ(successor, schedule->GetBlockById(successor->id()));
1070
    }
1071 1072 1073 1074 1075 1076
  }

  // Verify RPO numbers of blocks.
  CHECK_EQ(start, rpo_order->at(0));  // Start should be first.
  for (size_t b = 0; b < rpo_order->size(); b++) {
    BasicBlock* block = rpo_order->at(b);
1077 1078
    CHECK_EQ(static_cast<int>(b), block->rpo_number());
    BasicBlock* dom = block->dominator();
1079 1080
    if (b == 0) {
      // All blocks except start should have a dominator.
1081
      CHECK_NULL(dom);
1082 1083
    } else {
      // Check that the immediate dominator appears somewhere before the block.
1084
      CHECK_NOT_NULL(dom);
1085
      CHECK_LT(dom->rpo_number(), block->rpo_number());
1086 1087 1088 1089
    }
  }

  // Verify that all blocks reachable from start are in the RPO.
1090
  BoolVector marked(static_cast<int>(count), false, zone);
1091
  {
1092
    ZoneQueue<BasicBlock*> queue(zone);
1093
    queue.push(start);
1094
    marked[start->id().ToSize()] = true;
1095 1096 1097
    while (!queue.empty()) {
      BasicBlock* block = queue.front();
      queue.pop();
1098
      for (size_t s = 0; s < block->SuccessorCount(); s++) {
1099
        BasicBlock* succ = block->SuccessorAt(s);
1100 1101
        if (!marked[succ->id().ToSize()]) {
          marked[succ->id().ToSize()] = true;
1102 1103 1104 1105 1106 1107
          queue.push(succ);
        }
      }
    }
  }
  // Verify marked blocks are in the RPO.
1108 1109
  for (size_t i = 0; i < count; i++) {
    BasicBlock* block = schedule->GetBlockById(BasicBlock::Id::FromSize(i));
1110
    if (marked[i]) {
1111 1112
      CHECK_GE(block->rpo_number(), 0);
      CHECK_EQ(block, rpo_order->at(block->rpo_number()));
1113 1114 1115 1116
    }
  }
  // Verify RPO blocks are marked.
  for (size_t b = 0; b < rpo_order->size(); b++) {
1117
    CHECK(marked[rpo_order->at(b)->id().ToSize()]);
1118 1119 1120 1121
  }

  {
    // Verify the dominance relation.
1122
    ZoneVector<BitVector*> dominators(zone);
1123
    dominators.resize(count, nullptr);
1124 1125 1126

    // Compute a set of all the nodes that dominate a given node by using
    // a forward fixpoint. O(n^2).
1127
    ZoneQueue<BasicBlock*> queue(zone);
1128
    queue.push(start);
1129 1130
    dominators[start->id().ToSize()] =
        new (zone) BitVector(static_cast<int>(count), zone);
1131 1132 1133
    while (!queue.empty()) {
      BasicBlock* block = queue.front();
      queue.pop();
1134 1135
      BitVector* block_doms = dominators[block->id().ToSize()];
      BasicBlock* idom = block->dominator();
1136
      if (idom != nullptr && !block_doms->Contains(idom->id().ToInt())) {
1137
        V8_Fatal(__FILE__, __LINE__, "Block B%d is not dominated by B%d",
1138
                 block->rpo_number(), idom->rpo_number());
1139
      }
1140
      for (size_t s = 0; s < block->SuccessorCount(); s++) {
1141
        BasicBlock* succ = block->SuccessorAt(s);
1142
        BitVector* succ_doms = dominators[succ->id().ToSize()];
1143

1144
        if (succ_doms == nullptr) {
1145
          // First time visiting the node. S.doms = B U B.doms
1146
          succ_doms = new (zone) BitVector(static_cast<int>(count), zone);
1147
          succ_doms->CopyFrom(*block_doms);
1148 1149
          succ_doms->Add(block->id().ToInt());
          dominators[succ->id().ToSize()] = succ_doms;
1150 1151
          queue.push(succ);
        } else {
1152
          // Nth time visiting the successor. S.doms = S.doms ^ (B U B.doms)
1153 1154
          bool had = succ_doms->Contains(block->id().ToInt());
          if (had) succ_doms->Remove(block->id().ToInt());
1155
          if (succ_doms->IntersectIsChanged(*block_doms)) queue.push(succ);
1156
          if (had) succ_doms->Add(block->id().ToInt());
1157 1158 1159 1160 1161 1162 1163 1164
        }
      }
    }

    // Verify the immediateness of dominators.
    for (BasicBlockVector::iterator b = rpo_order->begin();
         b != rpo_order->end(); ++b) {
      BasicBlock* block = *b;
1165
      BasicBlock* idom = block->dominator();
1166
      if (idom == nullptr) continue;
1167
      BitVector* block_doms = dominators[block->id().ToSize()];
1168 1169

      for (BitVector::Iterator it(block_doms); !it.Done(); it.Advance()) {
1170 1171 1172 1173
        BasicBlock* dom =
            schedule->GetBlockById(BasicBlock::Id::FromInt(it.Current()));
        if (dom != idom &&
            !dominators[idom->id().ToSize()]->Contains(dom->id().ToInt())) {
1174
          V8_Fatal(__FILE__, __LINE__,
1175
                   "Block B%d is not immediately dominated by B%d",
1176
                   block->rpo_number(), idom->rpo_number());
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
        }
      }
    }
  }

  // Verify phis are placed in the block of their control input.
  for (BasicBlockVector::iterator b = rpo_order->begin(); b != rpo_order->end();
       ++b) {
    for (BasicBlock::const_iterator i = (*b)->begin(); i != (*b)->end(); ++i) {
      Node* phi = *i;
      if (phi->opcode() != IrOpcode::kPhi) continue;
      // TODO(titzer): Nasty special case. Phis from RawMachineAssembler
      // schedules don't have control inputs.
1190
      if (phi->InputCount() > phi->op()->ValueInputCount()) {
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
        Node* control = NodeProperties::GetControlInput(phi);
        CHECK(control->opcode() == IrOpcode::kMerge ||
              control->opcode() == IrOpcode::kLoop);
        CHECK_EQ((*b), schedule->block(control));
      }
    }
  }

  // Verify that all uses are dominated by their definitions.
  for (BasicBlockVector::iterator b = rpo_order->begin(); b != rpo_order->end();
       ++b) {
    BasicBlock* block = *b;

    // Check inputs to control for this block.
1205
    Node* control = block->control_input();
1206
    if (control != nullptr) {
1207 1208
      CHECK_EQ(block, schedule->block(control));
      CheckInputsDominate(schedule, block, control,
1209
                          static_cast<int>(block->NodeCount()) - 1);
1210 1211
    }
    // Check inputs for all nodes in the block.
1212 1213
    for (size_t i = 0; i < block->NodeCount(); i++) {
      Node* node = block->NodeAt(i);
1214 1215 1216 1217
      CheckInputsDominate(schedule, block, node, static_cast<int>(i) - 1);
    }
  }
}
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279


#ifdef DEBUG

// static
void Verifier::VerifyNode(Node* node) {
  CHECK_EQ(OperatorProperties::GetTotalInputCount(node->op()),
           node->InputCount());
  // If this node has no effect or no control outputs,
  // we check that no its uses are effect or control inputs.
  bool check_no_control = node->op()->ControlOutputCount() == 0;
  bool check_no_effect = node->op()->EffectOutputCount() == 0;
  bool check_no_frame_state = node->opcode() != IrOpcode::kFrameState;
  if (check_no_effect || check_no_control) {
    for (Edge edge : node->use_edges()) {
      Node* const user = edge.from();
      CHECK(!user->IsDead());
      if (NodeProperties::IsControlEdge(edge)) {
        CHECK(!check_no_control);
      } else if (NodeProperties::IsEffectEdge(edge)) {
        CHECK(!check_no_effect);
      } else if (NodeProperties::IsFrameStateEdge(edge)) {
        CHECK(!check_no_frame_state);
      }
    }
  }
  // Frame state inputs should be frame states (or sentinels).
  for (int i = 0; i < OperatorProperties::GetFrameStateInputCount(node->op());
       i++) {
    Node* input = NodeProperties::GetFrameStateInput(node, i);
    CHECK(input->opcode() == IrOpcode::kFrameState ||
          input->opcode() == IrOpcode::kStart ||
          input->opcode() == IrOpcode::kDead);
  }
  // Effect inputs should be effect-producing nodes (or sentinels).
  for (int i = 0; i < node->op()->EffectInputCount(); i++) {
    Node* input = NodeProperties::GetEffectInput(node, i);
    CHECK(input->op()->EffectOutputCount() > 0 ||
          input->opcode() == IrOpcode::kDead);
  }
  // Control inputs should be control-producing nodes (or sentinels).
  for (int i = 0; i < node->op()->ControlInputCount(); i++) {
    Node* input = NodeProperties::GetControlInput(node, i);
    CHECK(input->op()->ControlOutputCount() > 0 ||
          input->opcode() == IrOpcode::kDead);
  }
}


void Verifier::VerifyEdgeInputReplacement(const Edge& edge,
                                          const Node* replacement) {
  // Check that the user does not misuse the replacement.
  DCHECK(!NodeProperties::IsControlEdge(edge) ||
         replacement->op()->ControlOutputCount() > 0);
  DCHECK(!NodeProperties::IsEffectEdge(edge) ||
         replacement->op()->EffectOutputCount() > 0);
  DCHECK(!NodeProperties::IsFrameStateEdge(edge) ||
         replacement->opcode() == IrOpcode::kFrameState);
}

#endif  // DEBUG

1280 1281 1282
}  // namespace compiler
}  // namespace internal
}  // namespace v8