1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/compiler/node-matchers.h"
6
7namespace v8 {
8namespace internal {
9namespace compiler {
10
11bool NodeMatcher::IsComparison() const {
12  return IrOpcode::IsComparisonOpcode(opcode());
13}
14
15
16BranchMatcher::BranchMatcher(Node* branch)
17    : NodeMatcher(branch), if_true_(nullptr), if_false_(nullptr) {
18  if (branch->opcode() != IrOpcode::kBranch) return;
19  for (Node* use : branch->uses()) {
20    if (use->opcode() == IrOpcode::kIfTrue) {
21      DCHECK_NULL(if_true_);
22      if_true_ = use;
23    } else if (use->opcode() == IrOpcode::kIfFalse) {
24      DCHECK_NULL(if_false_);
25      if_false_ = use;
26    }
27  }
28}
29
30
31DiamondMatcher::DiamondMatcher(Node* merge)
32    : NodeMatcher(merge),
33      branch_(nullptr),
34      if_true_(nullptr),
35      if_false_(nullptr) {
36  if (merge->InputCount() != 2) return;
37  if (merge->opcode() != IrOpcode::kMerge) return;
38  Node* input0 = merge->InputAt(0);
39  if (input0->InputCount() != 1) return;
40  Node* input1 = merge->InputAt(1);
41  if (input1->InputCount() != 1) return;
42  Node* branch = input0->InputAt(0);
43  if (branch != input1->InputAt(0)) return;
44  if (branch->opcode() != IrOpcode::kBranch) return;
45  if (input0->opcode() == IrOpcode::kIfTrue &&
46      input1->opcode() == IrOpcode::kIfFalse) {
47    branch_ = branch;
48    if_true_ = input0;
49    if_false_ = input1;
50  } else if (input0->opcode() == IrOpcode::kIfFalse &&
51             input1->opcode() == IrOpcode::kIfTrue) {
52    branch_ = branch;
53    if_true_ = input1;
54    if_false_ = input0;
55  }
56}
57
58}  // namespace compiler
59}  // namespace internal
60}  // namespace v8
61