summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/common-operator-reducer.cc
blob: b07383d21c5ed7b554273d24f98d933ab8808d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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/common-operator-reducer.h"

#include <algorithm>

#include "src/compiler/common-operator.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/node.h"
#include "src/compiler/node-matchers.h"

namespace v8 {
namespace internal {
namespace compiler {

Reduction CommonOperatorReducer::Reduce(Node* node) {
  switch (node->opcode()) {
    case IrOpcode::kEffectPhi:
      return ReduceEffectPhi(node);
    case IrOpcode::kPhi:
      return ReducePhi(node);
    case IrOpcode::kSelect:
      return ReduceSelect(node);
    default:
      break;
  }
  return NoChange();
}


Reduction CommonOperatorReducer::ReduceEffectPhi(Node* node) {
  DCHECK_EQ(IrOpcode::kEffectPhi, node->opcode());
  int const input_count = node->InputCount();
  if (input_count > 1) {
    Node* const replacement = node->InputAt(0);
    for (int i = 1; i < input_count - 1; ++i) {
      if (node->InputAt(i) != replacement) return NoChange();
    }
    return Replace(replacement);
  }
  return NoChange();
}


Reduction CommonOperatorReducer::ReducePhi(Node* node) {
  DCHECK_EQ(IrOpcode::kPhi, node->opcode());
  int const input_count = node->InputCount();
  if (input_count == 3) {
    Node* vtrue = NodeProperties::GetValueInput(node, 0);
    Node* vfalse = NodeProperties::GetValueInput(node, 1);
    Node* merge = NodeProperties::GetControlInput(node);
    Node* if_true = NodeProperties::GetControlInput(merge, 0);
    Node* if_false = NodeProperties::GetControlInput(merge, 1);
    if (if_true->opcode() != IrOpcode::kIfTrue) {
      std::swap(if_true, if_false);
      std::swap(vtrue, vfalse);
    }
    if (if_true->opcode() == IrOpcode::kIfTrue &&
        if_false->opcode() == IrOpcode::kIfFalse &&
        if_true->InputAt(0) == if_false->InputAt(0)) {
      Node* branch = if_true->InputAt(0);
      Node* cond = branch->InputAt(0);
      if (cond->opcode() == IrOpcode::kFloat64LessThan) {
        if (cond->InputAt(0) == vtrue && cond->InputAt(1) == vfalse &&
            machine()->HasFloat64Min()) {
          node->set_op(machine()->Float64Min());
          node->ReplaceInput(0, vtrue);
          node->ReplaceInput(1, vfalse);
          node->TrimInputCount(2);
          return Changed(node);
        } else if (cond->InputAt(0) == vfalse && cond->InputAt(1) == vtrue &&
                   machine()->HasFloat64Max()) {
          node->set_op(machine()->Float64Max());
          node->ReplaceInput(0, vtrue);
          node->ReplaceInput(1, vfalse);
          node->TrimInputCount(2);
          return Changed(node);
        }
      }
    }
  }
  if (input_count > 1) {
    Node* const replacement = node->InputAt(0);
    for (int i = 1; i < input_count - 1; ++i) {
      if (node->InputAt(i) != replacement) return NoChange();
    }
    return Replace(replacement);
  }
  return NoChange();
}


Reduction CommonOperatorReducer::ReduceSelect(Node* node) {
  DCHECK_EQ(IrOpcode::kSelect, node->opcode());
  Node* cond = NodeProperties::GetValueInput(node, 0);
  Node* vtrue = NodeProperties::GetValueInput(node, 1);
  Node* vfalse = NodeProperties::GetValueInput(node, 2);
  if (vtrue == vfalse) return Replace(vtrue);
  if (cond->opcode() == IrOpcode::kFloat64LessThan) {
    if (cond->InputAt(0) == vtrue && cond->InputAt(1) == vfalse &&
        machine()->HasFloat64Min()) {
      node->set_op(machine()->Float64Min());
      node->ReplaceInput(0, vtrue);
      node->ReplaceInput(1, vfalse);
      node->TrimInputCount(2);
      return Changed(node);
    } else if (cond->InputAt(0) == vfalse && cond->InputAt(1) == vtrue &&
               machine()->HasFloat64Max()) {
      node->set_op(machine()->Float64Max());
      node->ReplaceInput(0, vtrue);
      node->ReplaceInput(1, vfalse);
      node->TrimInputCount(2);
      return Changed(node);
    }
  }
  return NoChange();
}


CommonOperatorBuilder* CommonOperatorReducer::common() const {
  return jsgraph()->common();
}


Graph* CommonOperatorReducer::graph() const { return jsgraph()->graph(); }


MachineOperatorBuilder* CommonOperatorReducer::machine() const {
  return jsgraph()->machine();
}

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