node-matchers.h 26.3 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_COMPILER_NODE_MATCHERS_H_
#define V8_COMPILER_NODE_MATCHERS_H_

8 9
#include <cmath>

10
#include "src/base/compiler-specific.h"
11 12
#include "src/codegen/external-reference.h"
#include "src/common/globals.h"
13
#include "src/compiler/node.h"
14
#include "src/compiler/operator.h"
15
#include "src/numbers/double.h"
16
#include "src/objects/heap-object.h"
17 18 19 20 21

namespace v8 {
namespace internal {
namespace compiler {

22 23
class JSHeapBroker;

24 25 26 27 28
// A pattern matcher for nodes.
struct NodeMatcher {
  explicit NodeMatcher(Node* node) : node_(node) {}

  Node* node() const { return node_; }
29
  const Operator* op() const { return node()->op(); }
30 31 32 33 34 35 36
  IrOpcode::Value opcode() const { return node()->opcode(); }

  bool HasProperty(Operator::Property property) const {
    return op()->HasProperty(property);
  }
  Node* InputAt(int index) const { return node()->InputAt(index); }

37 38
  bool Equals(const Node* node) const { return node_ == node; }

39 40
  bool IsComparison() const;

41 42 43 44 45 46 47 48 49 50 51
#define DEFINE_IS_OPCODE(Opcode) \
  bool Is##Opcode() const { return opcode() == IrOpcode::k##Opcode; }
  ALL_OP_LIST(DEFINE_IS_OPCODE)
#undef DEFINE_IS_OPCODE

 private:
  Node* node_;
};


// A pattern matcher for abitrary value constants.
52
template <typename T, IrOpcode::Value kOpcode>
53
struct ValueMatcher : public NodeMatcher {
54
  using ValueType = T;
55

56
  explicit ValueMatcher(Node* node)
57 58
      : NodeMatcher(node), value_(), has_value_(opcode() == kOpcode) {
    if (has_value_) {
59
      value_ = OpParameter<T>(node->op());
60
    }
61 62 63
  }

  bool HasValue() const { return has_value_; }
64
  const T& Value() const {
65
    DCHECK(HasValue());
66 67 68 69 70 71 72 73 74
    return value_;
  }

 private:
  T value_;
  bool has_value_;
};


75 76 77 78 79 80 81
template <>
inline ValueMatcher<uint32_t, IrOpcode::kInt32Constant>::ValueMatcher(
    Node* node)
    : NodeMatcher(node),
      value_(),
      has_value_(opcode() == IrOpcode::kInt32Constant) {
  if (has_value_) {
82
    value_ = static_cast<uint32_t>(OpParameter<int32_t>(node->op()));
83 84 85 86
  }
}


87 88 89 90
template <>
inline ValueMatcher<int64_t, IrOpcode::kInt64Constant>::ValueMatcher(Node* node)
    : NodeMatcher(node), value_(), has_value_(false) {
  if (opcode() == IrOpcode::kInt32Constant) {
91
    value_ = OpParameter<int32_t>(node->op());
92 93
    has_value_ = true;
  } else if (opcode() == IrOpcode::kInt64Constant) {
94
    value_ = OpParameter<int64_t>(node->op());
95 96 97 98 99 100 101 102 103 104
    has_value_ = true;
  }
}


template <>
inline ValueMatcher<uint64_t, IrOpcode::kInt64Constant>::ValueMatcher(
    Node* node)
    : NodeMatcher(node), value_(), has_value_(false) {
  if (opcode() == IrOpcode::kInt32Constant) {
105
    value_ = static_cast<uint32_t>(OpParameter<int32_t>(node->op()));
106 107
    has_value_ = true;
  } else if (opcode() == IrOpcode::kInt64Constant) {
108
    value_ = static_cast<uint64_t>(OpParameter<int64_t>(node->op()));
109 110 111 112 113
    has_value_ = true;
  }
}


114
// A pattern matcher for integer constants.
115
template <typename T, IrOpcode::Value kOpcode>
116
struct IntMatcher final : public ValueMatcher<T, kOpcode> {
117
  explicit IntMatcher(Node* node) : ValueMatcher<T, kOpcode>(node) {}
118

119 120 121 122 123 124
  bool Is(const T& value) const {
    return this->HasValue() && this->Value() == value;
  }
  bool IsInRange(const T& low, const T& high) const {
    return this->HasValue() && low <= this->Value() && this->Value() <= high;
  }
125 126 127
  bool IsMultipleOf(T n) const {
    return this->HasValue() && (this->Value() % n) == 0;
  }
128 129 130 131
  bool IsPowerOf2() const {
    return this->HasValue() && this->Value() > 0 &&
           (this->Value() & (this->Value() - 1)) == 0;
  }
132 133
  bool IsNegativePowerOf2() const {
    return this->HasValue() && this->Value() < 0 &&
134 135
           ((this->Value() == kMinInt) ||
            (-this->Value() & (-this->Value() - 1)) == 0);
136
  }
137
  bool IsNegative() const { return this->HasValue() && this->Value() < 0; }
138 139
};

140 141 142 143
using Int32Matcher = IntMatcher<int32_t, IrOpcode::kInt32Constant>;
using Uint32Matcher = IntMatcher<uint32_t, IrOpcode::kInt32Constant>;
using Int64Matcher = IntMatcher<int64_t, IrOpcode::kInt64Constant>;
using Uint64Matcher = IntMatcher<uint64_t, IrOpcode::kInt64Constant>;
144
#if V8_HOST_ARCH_32_BIT
145 146
using IntPtrMatcher = Int32Matcher;
using UintPtrMatcher = Uint32Matcher;
147
#else
148 149
using IntPtrMatcher = Int64Matcher;
using UintPtrMatcher = Uint64Matcher;
150
#endif
151 152 153


// A pattern matcher for floating point constants.
154
template <typename T, IrOpcode::Value kOpcode>
155
struct FloatMatcher final : public ValueMatcher<T, kOpcode> {
156
  explicit FloatMatcher(Node* node) : ValueMatcher<T, kOpcode>(node) {}
157

158 159 160 161 162 163
  bool Is(const T& value) const {
    return this->HasValue() && this->Value() == value;
  }
  bool IsInRange(const T& low, const T& high) const {
    return this->HasValue() && low <= this->Value() && this->Value() <= high;
  }
164 165 166
  bool IsMinusZero() const {
    return this->Is(0.0) && std::signbit(this->Value());
  }
167
  bool IsNegative() const { return this->HasValue() && this->Value() < 0.0; }
168
  bool IsNaN() const { return this->HasValue() && std::isnan(this->Value()); }
169
  bool IsZero() const { return this->Is(0.0) && !std::signbit(this->Value()); }
170 171 172
  bool IsNormal() const {
    return this->HasValue() && std::isnormal(this->Value());
  }
173 174 175
  bool IsInteger() const {
    return this->HasValue() && std::nearbyint(this->Value()) == this->Value();
  }
176 177 178 179 180
  bool IsPositiveOrNegativePowerOf2() const {
    if (!this->HasValue() || (this->Value() == 0.0)) {
      return false;
    }
    Double value = Double(this->Value());
181
    return !value.IsInfinite() && base::bits::IsPowerOfTwo(value.Significand());
182
  }
183 184
};

185 186 187
using Float32Matcher = FloatMatcher<float, IrOpcode::kFloat32Constant>;
using Float64Matcher = FloatMatcher<double, IrOpcode::kFloat64Constant>;
using NumberMatcher = FloatMatcher<double, IrOpcode::kNumberConstant>;
188

189
// A pattern matcher for heap object constants.
190
struct HeapObjectMatcher final
191
    : public ValueMatcher<Handle<HeapObject>, IrOpcode::kHeapConstant> {
192
  explicit HeapObjectMatcher(Node* node)
193
      : ValueMatcher<Handle<HeapObject>, IrOpcode::kHeapConstant>(node) {}
194 195 196 197

  bool Is(Handle<HeapObject> const& value) const {
    return this->HasValue() && this->Value().address() == value.address();
  }
198

199 200
  HeapObjectRef Ref(JSHeapBroker* broker) const {
    return HeapObjectRef(broker, this->Value());
201
  }
202 203 204
};


205
// A pattern matcher for external reference constants.
206
struct ExternalReferenceMatcher final
207 208 209
    : public ValueMatcher<ExternalReference, IrOpcode::kExternalConstant> {
  explicit ExternalReferenceMatcher(Node* node)
      : ValueMatcher<ExternalReference, IrOpcode::kExternalConstant>(node) {}
210 211 212
  bool Is(const ExternalReference& value) const {
    return this->HasValue() && this->Value() == value;
  }
213 214 215 216 217 218 219 220 221 222
};


// For shorter pattern matching code, this struct matches the inputs to
// machine-level load operations.
template <typename Object>
struct LoadMatcher : public NodeMatcher {
  explicit LoadMatcher(Node* node)
      : NodeMatcher(node), object_(InputAt(0)), index_(InputAt(1)) {}

223
  using ObjectMatcher = Object;
224 225 226 227 228 229 230 231 232 233

  Object const& object() const { return object_; }
  IntPtrMatcher const& index() const { return index_; }

 private:
  Object const object_;
  IntPtrMatcher const index_;
};


234 235 236 237
// For shorter pattern matching code, this struct matches both the left and
// right hand sides of a binary operation and can put constants on the right
// if they appear on the left hand side of a commutative operation.
template <typename Left, typename Right>
238
struct BinopMatcher : public NodeMatcher {
239 240 241 242
  explicit BinopMatcher(Node* node)
      : NodeMatcher(node), left_(InputAt(0)), right_(InputAt(1)) {
    if (HasProperty(Operator::kCommutative)) PutConstantOnRight();
  }
243 244 245 246
  BinopMatcher(Node* node, bool allow_input_swap)
      : NodeMatcher(node), left_(InputAt(0)), right_(InputAt(1)) {
    if (allow_input_swap) PutConstantOnRight();
  }
247

248 249
  using LeftMatcher = Left;
  using RightMatcher = Right;
250

251 252 253 254 255 256
  const Left& left() const { return left_; }
  const Right& right() const { return right_; }

  bool IsFoldable() const { return left().HasValue() && right().HasValue(); }
  bool LeftEqualsRight() const { return left().node() == right().node(); }

257 258 259 260 261 262 263 264 265
  bool OwnsInput(Node* input) {
    for (Node* use : input->uses()) {
      if (use != node()) {
        return false;
      }
    }
    return true;
  }

266 267 268
 protected:
  void SwapInputs() {
    std::swap(left_, right_);
269 270 271
    // TODO(tebbi): This modification should notify the reducers using
    // BinopMatcher. Alternatively, all reducers (especially value numbering)
    // could ignore the ordering for commutative binops.
272 273 274 275
    node()->ReplaceInput(0, left().node());
    node()->ReplaceInput(1, right().node());
  }

276 277 278
 private:
  void PutConstantOnRight() {
    if (left().HasValue() && !right().HasValue()) {
279
      SwapInputs();
280 281 282 283 284 285 286
    }
  }

  Left left_;
  Right right_;
};

287 288 289 290 291 292 293 294 295 296 297
using Int32BinopMatcher = BinopMatcher<Int32Matcher, Int32Matcher>;
using Uint32BinopMatcher = BinopMatcher<Uint32Matcher, Uint32Matcher>;
using Int64BinopMatcher = BinopMatcher<Int64Matcher, Int64Matcher>;
using Uint64BinopMatcher = BinopMatcher<Uint64Matcher, Uint64Matcher>;
using IntPtrBinopMatcher = BinopMatcher<IntPtrMatcher, IntPtrMatcher>;
using UintPtrBinopMatcher = BinopMatcher<UintPtrMatcher, UintPtrMatcher>;
using Float32BinopMatcher = BinopMatcher<Float32Matcher, Float32Matcher>;
using Float64BinopMatcher = BinopMatcher<Float64Matcher, Float64Matcher>;
using NumberBinopMatcher = BinopMatcher<NumberMatcher, NumberMatcher>;
using HeapObjectBinopMatcher =
    BinopMatcher<HeapObjectMatcher, HeapObjectMatcher>;
298

299 300 301 302 303 304 305
template <class BinopMatcher, IrOpcode::Value kMulOpcode,
          IrOpcode::Value kShiftOpcode>
struct ScaleMatcher {
  explicit ScaleMatcher(Node* node, bool allow_power_of_two_plus_one = false)
      : scale_(-1), power_of_two_plus_one_(false) {
    if (node->InputCount() < 2) return;
    BinopMatcher m(node);
306
    if (node->opcode() == kShiftOpcode) {
307
      if (m.right().HasValue()) {
308 309
        typename BinopMatcher::RightMatcher::ValueType value =
            m.right().Value();
310
        if (value >= 0 && value <= 3) {
311
          scale_ = static_cast<int>(value);
312 313
        }
      }
314
    } else if (node->opcode() == kMulOpcode) {
315
      if (m.right().HasValue()) {
316 317
        typename BinopMatcher::RightMatcher::ValueType value =
            m.right().Value();
318
        if (value == 1) {
319
          scale_ = 0;
320
        } else if (value == 2) {
321
          scale_ = 1;
322
        } else if (value == 4) {
323
          scale_ = 2;
324
        } else if (value == 8) {
325 326 327 328 329 330 331 332 333 334 335 336
          scale_ = 3;
        } else if (allow_power_of_two_plus_one) {
          if (value == 3) {
            scale_ = 1;
            power_of_two_plus_one_ = true;
          } else if (value == 5) {
            scale_ = 2;
            power_of_two_plus_one_ = true;
          } else if (value == 9) {
            scale_ = 3;
            power_of_two_plus_one_ = true;
          }
337 338 339 340 341
        }
      }
    }
  }

342 343 344 345 346 347 348 349 350
  bool matches() const { return scale_ != -1; }
  int scale() const { return scale_; }
  bool power_of_two_plus_one() const { return power_of_two_plus_one_; }

 private:
  int scale_;
  bool power_of_two_plus_one_;
};

351 352 353 354
using Int32ScaleMatcher =
    ScaleMatcher<Int32BinopMatcher, IrOpcode::kInt32Mul, IrOpcode::kWord32Shl>;
using Int64ScaleMatcher =
    ScaleMatcher<Int64BinopMatcher, IrOpcode::kInt64Mul, IrOpcode::kWord64Shl>;
355

356 357 358
template <class BinopMatcher, IrOpcode::Value AddOpcode,
          IrOpcode::Value SubOpcode, IrOpcode::Value kMulOpcode,
          IrOpcode::Value kShiftOpcode>
359
struct AddMatcher : public BinopMatcher {
360 361
  static const IrOpcode::Value kAddOpcode = AddOpcode;
  static const IrOpcode::Value kSubOpcode = SubOpcode;
362
  using Matcher = ScaleMatcher<BinopMatcher, kMulOpcode, kShiftOpcode>;
363

364 365 366 367 368 369
  AddMatcher(Node* node, bool allow_input_swap)
      : BinopMatcher(node, allow_input_swap),
        scale_(-1),
        power_of_two_plus_one_(false) {
    Initialize(node, allow_input_swap);
  }
370
  explicit AddMatcher(Node* node)
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
      : BinopMatcher(node, node->op()->HasProperty(Operator::kCommutative)),
        scale_(-1),
        power_of_two_plus_one_(false) {
    Initialize(node, node->op()->HasProperty(Operator::kCommutative));
  }

  bool HasIndexInput() const { return scale_ != -1; }
  Node* IndexInput() const {
    DCHECK(HasIndexInput());
    return this->left().node()->InputAt(0);
  }
  int scale() const {
    DCHECK(HasIndexInput());
    return scale_;
  }
  bool power_of_two_plus_one() const { return power_of_two_plus_one_; }

 private:
  void Initialize(Node* node, bool allow_input_swap) {
390 391 392 393 394 395 396
    Matcher left_matcher(this->left().node(), true);
    if (left_matcher.matches()) {
      scale_ = left_matcher.scale();
      power_of_two_plus_one_ = left_matcher.power_of_two_plus_one();
      return;
    }

397
    if (!allow_input_swap) {
398 399 400 401 402 403 404 405 406
      return;
    }

    Matcher right_matcher(this->right().node(), true);
    if (right_matcher.matches()) {
      scale_ = right_matcher.scale();
      power_of_two_plus_one_ = right_matcher.power_of_two_plus_one();
      this->SwapInputs();
      return;
407
    }
408

409 410 411 412
    if ((this->left().opcode() != kSubOpcode &&
         this->left().opcode() != kAddOpcode) &&
        (this->right().opcode() == kAddOpcode ||
         this->right().opcode() == kSubOpcode)) {
413
      this->SwapInputs();
414 415 416 417 418
    }
  }

  int scale_;
  bool power_of_two_plus_one_;
419 420
};

421 422 423 424 425 426
using Int32AddMatcher =
    AddMatcher<Int32BinopMatcher, IrOpcode::kInt32Add, IrOpcode::kInt32Sub,
               IrOpcode::kInt32Mul, IrOpcode::kWord32Shl>;
using Int64AddMatcher =
    AddMatcher<Int64BinopMatcher, IrOpcode::kInt64Add, IrOpcode::kInt64Sub,
               IrOpcode::kInt64Mul, IrOpcode::kWord64Shl>;
427

428
enum DisplacementMode { kPositiveDisplacement, kNegativeDisplacement };
429

430 431 432 433 434 435 436
enum class AddressOption : uint8_t {
  kAllowNone = 0u,
  kAllowInputSwap = 1u << 0,
  kAllowScale = 1u << 1,
  kAllowAll = kAllowInputSwap | kAllowScale
};

437
using AddressOptions = base::Flags<AddressOption, uint8_t>;
438
DEFINE_OPERATORS_FOR_FLAGS(AddressOptions)
439

440
template <class AddMatcher>
441
struct BaseWithIndexAndDisplacementMatcher {
442
  BaseWithIndexAndDisplacementMatcher(Node* node, AddressOptions options)
443
      : matches_(false),
444
        index_(nullptr),
445
        scale_(0),
446
        base_(nullptr),
447 448
        displacement_(nullptr),
        displacement_mode_(kPositiveDisplacement) {
449
    Initialize(node, options);
450 451
  }

452
  explicit BaseWithIndexAndDisplacementMatcher(Node* node)
453
      : matches_(false),
454
        index_(nullptr),
455
        scale_(0),
456
        base_(nullptr),
457 458
        displacement_(nullptr),
        displacement_mode_(kPositiveDisplacement) {
459 460 461 462
    Initialize(node, AddressOption::kAllowScale |
                         (node->op()->HasProperty(Operator::kCommutative)
                              ? AddressOption::kAllowInputSwap
                              : AddressOption::kAllowNone));
463 464 465 466 467 468 469
  }

  bool matches() const { return matches_; }
  Node* index() const { return index_; }
  int scale() const { return scale_; }
  Node* base() const { return base_; }
  Node* displacement() const { return displacement_; }
470
  DisplacementMode displacement_mode() const { return displacement_mode_; }
471 472 473 474 475 476 477

 private:
  bool matches_;
  Node* index_;
  int scale_;
  Node* base_;
  Node* displacement_;
478
  DisplacementMode displacement_mode_;
479

480
  void Initialize(Node* node, AddressOptions options) {
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    // The BaseWithIndexAndDisplacementMatcher canonicalizes the order of
    // displacements and scale factors that are used as inputs, so instead of
    // enumerating all possible patterns by brute force, checking for node
    // clusters using the following templates in the following order suffices to
    // find all of the interesting cases (S = index * scale, B = base input, D =
    // displacement input):
    // (S + (B + D))
    // (S + (B + B))
    // (S + D)
    // (S + B)
    // ((S + D) + B)
    // ((S + B) + D)
    // ((B + D) + B)
    // ((B + B) + D)
    // (B + D)
    // (B + B)
497
    if (node->InputCount() < 2) return;
498
    AddMatcher m(node, options & AddressOption::kAllowInputSwap);
499 500
    Node* left = m.left().node();
    Node* right = m.right().node();
501 502 503 504
    Node* displacement = nullptr;
    Node* base = nullptr;
    Node* index = nullptr;
    Node* scale_expression = nullptr;
505
    bool power_of_two_plus_one = false;
506
    DisplacementMode displacement_mode = kPositiveDisplacement;
507
    int scale = 0;
508
    if (m.HasIndexInput() && OwnedByAddressingOperand(left)) {
509 510 511 512
      index = m.IndexInput();
      scale = m.scale();
      scale_expression = left;
      power_of_two_plus_one = m.power_of_two_plus_one();
513
      bool match_found = false;
514
      if (right->opcode() == AddMatcher::kSubOpcode &&
515
          OwnedByAddressingOperand(right)) {
516
        AddMatcher right_matcher(right);
517
        if (right_matcher.right().HasValue()) {
518
          // (S + (B - D))
519 520
          base = right_matcher.left().node();
          displacement = right_matcher.right().node();
521 522 523 524 525
          displacement_mode = kNegativeDisplacement;
          match_found = true;
        }
      }
      if (!match_found) {
526
        if (right->opcode() == AddMatcher::kAddOpcode &&
527
            OwnedByAddressingOperand(right)) {
528 529 530 531 532 533 534 535 536 537 538 539
          AddMatcher right_matcher(right);
          if (right_matcher.right().HasValue()) {
            // (S + (B + D))
            base = right_matcher.left().node();
            displacement = right_matcher.right().node();
          } else {
            // (S + (B + B))
            base = right;
          }
        } else if (m.right().HasValue()) {
          // (S + D)
          displacement = right;
540
        } else {
541
          // (S + B)
542
          base = right;
543 544 545
        }
      }
    } else {
546
      bool match_found = false;
547
      if (left->opcode() == AddMatcher::kSubOpcode &&
548
          OwnedByAddressingOperand(left)) {
549
        AddMatcher left_matcher(left);
550 551
        Node* left_left = left_matcher.left().node();
        Node* left_right = left_matcher.right().node();
552 553 554
        if (left_matcher.right().HasValue()) {
          if (left_matcher.HasIndexInput() && left_left->OwnedBy(left)) {
            // ((S - D) + B)
555 556 557 558 559
            index = left_matcher.IndexInput();
            scale = left_matcher.scale();
            scale_expression = left_left;
            power_of_two_plus_one = left_matcher.power_of_two_plus_one();
            displacement = left_right;
560
            displacement_mode = kNegativeDisplacement;
561
            base = right;
562
          } else {
563
            // ((B - D) + B)
564 565
            index = left_left;
            displacement = left_right;
566
            displacement_mode = kNegativeDisplacement;
567
            base = right;
568 569 570 571 572
          }
          match_found = true;
        }
      }
      if (!match_found) {
573
        if (left->opcode() == AddMatcher::kAddOpcode &&
574
            OwnedByAddressingOperand(left)) {
575 576 577 578 579 580 581 582 583 584 585 586 587
          AddMatcher left_matcher(left);
          Node* left_left = left_matcher.left().node();
          Node* left_right = left_matcher.right().node();
          if (left_matcher.HasIndexInput() && left_left->OwnedBy(left)) {
            if (left_matcher.right().HasValue()) {
              // ((S + D) + B)
              index = left_matcher.IndexInput();
              scale = left_matcher.scale();
              scale_expression = left_left;
              power_of_two_plus_one = left_matcher.power_of_two_plus_one();
              displacement = left_right;
              base = right;
            } else if (m.right().HasValue()) {
588 589 590 591 592 593 594 595 596 597 598 599 600
              if (left->OwnedBy(node)) {
                // ((S + B) + D)
                index = left_matcher.IndexInput();
                scale = left_matcher.scale();
                scale_expression = left_left;
                power_of_two_plus_one = left_matcher.power_of_two_plus_one();
                base = left_right;
                displacement = right;
              } else {
                // (B + D)
                base = left;
                displacement = right;
              }
601 602 603 604 605 606 607 608 609 610 611 612
            } else {
              // (B + B)
              index = left;
              base = right;
            }
          } else {
            if (left_matcher.right().HasValue()) {
              // ((B + D) + B)
              index = left_left;
              displacement = left_right;
              base = right;
            } else if (m.right().HasValue()) {
613 614 615 616 617 618 619 620 621 622
              if (left->OwnedBy(node)) {
                // ((B + B) + D)
                index = left_left;
                base = left_right;
                displacement = right;
              } else {
                // (B + D)
                base = left;
                displacement = right;
              }
623 624 625 626 627 628 629 630 631 632
            } else {
              // (B + B)
              index = left;
              base = right;
            }
          }
        } else {
          if (m.right().HasValue()) {
            // (B + D)
            base = left;
633
            displacement = right;
634
          } else {
635
            // (B + B)
636 637
            base = left;
            index = right;
638 639 640 641
          }
        }
      }
    }
642
    int64_t value = 0;
643
    if (displacement != nullptr) {
644
      switch (displacement->opcode()) {
645
        case IrOpcode::kInt32Constant: {
646
          value = OpParameter<int32_t>(displacement->op());
647 648 649
          break;
        }
        case IrOpcode::kInt64Constant: {
650
          value = OpParameter<int64_t>(displacement->op());
651 652 653 654 655 656 657
          break;
        }
        default:
          UNREACHABLE();
          break;
      }
      if (value == 0) {
658
        displacement = nullptr;
659 660 661
      }
    }
    if (power_of_two_plus_one) {
662
      if (base != nullptr) {
663 664 665 666 667 668 669 670
        // If the scale requires explicitly using the index as the base, but a
        // base is already part of the match, then the (1 << N + 1) scale factor
        // can't be folded into the match and the entire index * scale
        // calculation must be computed separately.
        index = scale_expression;
        scale = 0;
      } else {
        base = index;
671 672
      }
    }
673 674 675 676
    if (!(options & AddressOption::kAllowScale) && scale != 0) {
      index = scale_expression;
      scale = 0;
    }
677 678
    base_ = base;
    displacement_ = displacement;
679
    displacement_mode_ = displacement_mode;
680 681
    index_ = index;
    scale_ = scale;
682 683
    matches_ = true;
  }
684 685 686 687 688 689 690

  static bool OwnedByAddressingOperand(Node* node) {
    for (auto use : node->use_edges()) {
      Node* from = use.from();
      switch (from->opcode()) {
        case IrOpcode::kLoad:
        case IrOpcode::kPoisonedLoad:
691
        case IrOpcode::kProtectedLoad:
692 693 694 695 696
        case IrOpcode::kInt32Add:
        case IrOpcode::kInt64Add:
          // Skip addressing uses.
          break;
        case IrOpcode::kStore:
697
        case IrOpcode::kProtectedStore:
698 699 700 701 702 703 704 705 706 707 708
          // If the stored value is this node, it is not an addressing use.
          if (from->InputAt(2) == node) return false;
          // Otherwise it is used as an address and skipped.
          break;
        default:
          // Non-addressing use found.
          return false;
      }
    }
    return true;
  }
709 710
};

711 712 713 714
using BaseWithIndexAndDisplacement32Matcher =
    BaseWithIndexAndDisplacementMatcher<Int32AddMatcher>;
using BaseWithIndexAndDisplacement64Matcher =
    BaseWithIndexAndDisplacementMatcher<Int64AddMatcher>;
715

716
struct V8_EXPORT_PRIVATE BranchMatcher : public NON_EXPORTED_BASE(NodeMatcher) {
717 718 719 720 721 722 723 724 725 726 727 728 729
  explicit BranchMatcher(Node* branch);

  bool Matched() const { return if_true_ && if_false_; }

  Node* Branch() const { return node(); }
  Node* IfTrue() const { return if_true_; }
  Node* IfFalse() const { return if_false_; }

 private:
  Node* if_true_;
  Node* if_false_;
};

730 731
struct V8_EXPORT_PRIVATE DiamondMatcher
    : public NON_EXPORTED_BASE(NodeMatcher) {
732 733 734 735 736 737 738 739 740 741 742 743
  explicit DiamondMatcher(Node* merge);

  bool Matched() const { return branch_; }
  bool IfProjectionsAreOwned() const {
    return if_true_->OwnedBy(node()) && if_false_->OwnedBy(node());
  }

  Node* Branch() const { return branch_; }
  Node* IfTrue() const { return if_true_; }
  Node* IfFalse() const { return if_false_; }
  Node* Merge() const { return node(); }

744 745 746 747 748 749 750 751 752 753 754 755 756 757
  Node* TrueInputOf(Node* phi) const {
    DCHECK(IrOpcode::IsPhiOpcode(phi->opcode()));
    DCHECK_EQ(3, phi->InputCount());
    DCHECK_EQ(Merge(), phi->InputAt(2));
    return phi->InputAt(if_true_ == Merge()->InputAt(0) ? 0 : 1);
  }

  Node* FalseInputOf(Node* phi) const {
    DCHECK(IrOpcode::IsPhiOpcode(phi->opcode()));
    DCHECK_EQ(3, phi->InputCount());
    DCHECK_EQ(Merge(), phi->InputAt(2));
    return phi->InputAt(if_true_ == Merge()->InputAt(0) ? 1 : 0);
  }

758 759 760 761 762 763
 private:
  Node* branch_;
  Node* if_true_;
  Node* if_false_;
};

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
template <class BinopMatcher, IrOpcode::Value expected_opcode>
struct WasmStackCheckMatcher {
  explicit WasmStackCheckMatcher(Node* compare) : compare_(compare) {}

  bool Matched() {
    if (compare_->opcode() != expected_opcode) return false;
    BinopMatcher m(compare_);
    return MatchedInternal(m.left(), m.right());
  }

 private:
  bool MatchedInternal(const typename BinopMatcher::LeftMatcher& l,
                       const typename BinopMatcher::RightMatcher& r) {
    // In wasm, the stack check is performed by loading the value given by
    // the address of a field stored in the instance object. That object is
    // passed as a parameter.
    if (l.IsLoad() && r.IsLoadStackPointer()) {
      LoadMatcher<LoadMatcher<NodeMatcher>> mleft(l.node());
      if (mleft.object().IsLoad() && mleft.index().Is(0) &&
          mleft.object().object().IsParameter()) {
        return true;
      }
    }
    return false;
  }
  Node* compare_;
};

792 793 794
template <class BinopMatcher, IrOpcode::Value expected_opcode>
struct StackCheckMatcher {
  StackCheckMatcher(Isolate* isolate, Node* compare)
795 796 797
      : isolate_(isolate), compare_(compare) {
    DCHECK_NOT_NULL(isolate);
  }
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  bool Matched() {
    // TODO(jgruber): Ideally, we could be more flexible here and also match the
    // same pattern with switched operands (i.e.: left is LoadStackPointer and
    // right is the js_stack_limit load). But to be correct in all cases, we'd
    // then have to invert the outcome of the stack check comparison.
    if (compare_->opcode() != expected_opcode) return false;
    BinopMatcher m(compare_);
    return MatchedInternal(m.left(), m.right());
  }

 private:
  bool MatchedInternal(const typename BinopMatcher::LeftMatcher& l,
                       const typename BinopMatcher::RightMatcher& r) {
    if (l.IsLoad() && r.IsLoadStackPointer()) {
      LoadMatcher<ExternalReferenceMatcher> mleft(l.node());
      ExternalReference js_stack_limit =
          ExternalReference::address_of_stack_limit(isolate_);
      if (mleft.object().Is(js_stack_limit) && mleft.index().Is(0)) return true;
    }
    return false;
  }

  Isolate* isolate_;
  Node* compare_;
};

824 825 826
}  // namespace compiler
}  // namespace internal
}  // namespace v8
827 828

#endif  // V8_COMPILER_NODE_MATCHERS_H_