Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5 | #include "src/compiler/simplified-operator-reducer.h" |
| 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/compiler/js-graph.h" |
| 8 | #include "src/compiler/machine-operator.h" |
| 9 | #include "src/compiler/node-matchers.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | #include "src/compiler/operator-properties.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 11 | #include "src/compiler/simplified-operator.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | #include "src/conversions-inl.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 13 | #include "src/type-cache.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | namespace compiler { |
| 18 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 19 | SimplifiedOperatorReducer::SimplifiedOperatorReducer(JSGraph* jsgraph) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 20 | : jsgraph_(jsgraph), type_cache_(TypeCache::Get()) {} |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 21 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | SimplifiedOperatorReducer::~SimplifiedOperatorReducer() {} |
| 23 | |
| 24 | |
| 25 | Reduction SimplifiedOperatorReducer::Reduce(Node* node) { |
| 26 | switch (node->opcode()) { |
| 27 | case IrOpcode::kBooleanNot: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | HeapObjectMatcher m(node->InputAt(0)); |
| 29 | if (m.HasValue()) { |
| 30 | return Replace(jsgraph()->BooleanConstant(!m.Value()->BooleanValue())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | if (m.IsBooleanNot()) return Replace(m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | break; |
| 34 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 35 | case IrOpcode::kChangeBitToTagged: { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | Int32Matcher m(node->InputAt(0)); |
| 37 | if (m.Is(0)) return Replace(jsgraph()->FalseConstant()); |
| 38 | if (m.Is(1)) return Replace(jsgraph()->TrueConstant()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 39 | if (m.IsChangeTaggedToBit()) return Replace(m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | break; |
| 41 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 42 | case IrOpcode::kChangeTaggedToBit: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 43 | HeapObjectMatcher m(node->InputAt(0)); |
| 44 | if (m.HasValue()) return ReplaceInt32(m.Value()->BooleanValue()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 45 | if (m.IsChangeBitToTagged()) return Replace(m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | case IrOpcode::kChangeFloat64ToTagged: { |
| 49 | Float64Matcher m(node->InputAt(0)); |
| 50 | if (m.HasValue()) return ReplaceNumber(m.Value()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 51 | if (m.IsChangeTaggedToFloat64()) return Replace(m.node()->InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | break; |
| 53 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 54 | case IrOpcode::kChangeInt31ToTaggedSigned: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | case IrOpcode::kChangeInt32ToTagged: { |
| 56 | Int32Matcher m(node->InputAt(0)); |
| 57 | if (m.HasValue()) return ReplaceNumber(m.Value()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 58 | if (m.IsChangeTaggedToInt32() || m.IsChangeTaggedSignedToInt32()) { |
| 59 | return Replace(m.InputAt(0)); |
| 60 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | break; |
| 62 | } |
| 63 | case IrOpcode::kChangeTaggedToFloat64: { |
| 64 | NumberMatcher m(node->InputAt(0)); |
| 65 | if (m.HasValue()) return ReplaceFloat64(m.Value()); |
| 66 | if (m.IsChangeFloat64ToTagged()) return Replace(m.node()->InputAt(0)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 67 | if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | return Change(node, machine()->ChangeInt32ToFloat64(), m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | } |
| 70 | if (m.IsChangeUint32ToTagged()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 71 | return Change(node, machine()->ChangeUint32ToFloat64(), m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | } |
| 73 | break; |
| 74 | } |
| 75 | case IrOpcode::kChangeTaggedToInt32: { |
| 76 | NumberMatcher m(node->InputAt(0)); |
| 77 | if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value())); |
| 78 | if (m.IsChangeFloat64ToTagged()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 79 | return Change(node, machine()->ChangeFloat64ToInt32(), m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 81 | if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged()) { |
| 82 | return Replace(m.InputAt(0)); |
| 83 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | case IrOpcode::kChangeTaggedToUint32: { |
| 87 | NumberMatcher m(node->InputAt(0)); |
| 88 | if (m.HasValue()) return ReplaceUint32(DoubleToUint32(m.Value())); |
| 89 | if (m.IsChangeFloat64ToTagged()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 90 | return Change(node, machine()->ChangeFloat64ToUint32(), m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 91 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | if (m.IsChangeUint32ToTagged()) return Replace(m.InputAt(0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | case IrOpcode::kChangeUint32ToTagged: { |
| 96 | Uint32Matcher m(node->InputAt(0)); |
| 97 | if (m.HasValue()) return ReplaceNumber(FastUI2D(m.Value())); |
| 98 | break; |
| 99 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 100 | case IrOpcode::kTruncateTaggedToWord32: { |
| 101 | NumberMatcher m(node->InputAt(0)); |
| 102 | if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value())); |
| 103 | if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged() || |
| 104 | m.IsChangeUint32ToTagged()) { |
| 105 | return Replace(m.InputAt(0)); |
| 106 | } |
| 107 | if (m.IsChangeFloat64ToTagged()) { |
| 108 | return Change(node, machine()->TruncateFloat64ToWord32(), m.InputAt(0)); |
| 109 | } |
| 110 | break; |
| 111 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 112 | case IrOpcode::kNumberCeil: |
| 113 | case IrOpcode::kNumberFloor: |
| 114 | case IrOpcode::kNumberRound: |
| 115 | case IrOpcode::kNumberTrunc: { |
| 116 | Node* const input = NodeProperties::GetValueInput(node, 0); |
| 117 | Type* const input_type = NodeProperties::GetType(input); |
| 118 | if (input_type->Is(type_cache_.kIntegerOrMinusZeroOrNaN)) { |
| 119 | return Replace(input); |
| 120 | } |
| 121 | break; |
| 122 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 123 | case IrOpcode::kReferenceEqual: |
| 124 | return ReduceReferenceEqual(node); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 125 | case IrOpcode::kTypeGuard: |
| 126 | return ReduceTypeGuard(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | return NoChange(); |
| 131 | } |
| 132 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 133 | Reduction SimplifiedOperatorReducer::ReduceReferenceEqual(Node* node) { |
| 134 | DCHECK_EQ(IrOpcode::kReferenceEqual, node->opcode()); |
| 135 | Node* const left = NodeProperties::GetValueInput(node, 0); |
| 136 | Node* const right = NodeProperties::GetValueInput(node, 1); |
| 137 | HeapObjectMatcher match_left(left); |
| 138 | HeapObjectMatcher match_right(right); |
| 139 | if (match_left.HasValue() && match_right.HasValue()) { |
| 140 | if (match_left.Value().is_identical_to(match_right.Value())) { |
| 141 | return Replace(jsgraph()->TrueConstant()); |
| 142 | } else { |
| 143 | return Replace(jsgraph()->FalseConstant()); |
| 144 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 145 | } |
| 146 | return NoChange(); |
| 147 | } |
| 148 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 149 | Reduction SimplifiedOperatorReducer::ReduceTypeGuard(Node* node) { |
| 150 | DCHECK_EQ(IrOpcode::kTypeGuard, node->opcode()); |
| 151 | Node* const input = NodeProperties::GetValueInput(node, 0); |
| 152 | Type* const input_type = NodeProperties::GetTypeOrAny(input); |
| 153 | Type* const guard_type = TypeOf(node->op()); |
| 154 | if (input_type->Is(guard_type)) return Replace(input); |
| 155 | return NoChange(); |
| 156 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 157 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | Reduction SimplifiedOperatorReducer::Change(Node* node, const Operator* op, |
| 159 | Node* a) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 160 | DCHECK_EQ(node->InputCount(), OperatorProperties::GetTotalInputCount(op)); |
| 161 | DCHECK_LE(1, node->InputCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 162 | node->ReplaceInput(0, a); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 163 | NodeProperties::ChangeOp(node, op); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | return Changed(node); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | Reduction SimplifiedOperatorReducer::ReplaceFloat64(double value) { |
| 169 | return Replace(jsgraph()->Float64Constant(value)); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | Reduction SimplifiedOperatorReducer::ReplaceInt32(int32_t value) { |
| 174 | return Replace(jsgraph()->Int32Constant(value)); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | Reduction SimplifiedOperatorReducer::ReplaceNumber(double value) { |
| 179 | return Replace(jsgraph()->Constant(value)); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | Reduction SimplifiedOperatorReducer::ReplaceNumber(int32_t value) { |
| 184 | return Replace(jsgraph()->Constant(value)); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | Graph* SimplifiedOperatorReducer::graph() const { return jsgraph()->graph(); } |
| 189 | |
| 190 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 191 | MachineOperatorBuilder* SimplifiedOperatorReducer::machine() const { |
| 192 | return jsgraph()->machine(); |
| 193 | } |
| 194 | |
| 195 | } // namespace compiler |
| 196 | } // namespace internal |
| 197 | } // namespace v8 |