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