| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/compiler/change-lowering.h" | 5 #include "src/compiler/change-lowering.h" |
| 6 | 6 |
| 7 #include "src/compiler/js-graph.h" | 7 #include "src/compiler/js-graph.h" |
| 8 | 8 |
| 9 namespace v8 { | 9 namespace v8 { |
| 10 namespace internal { | 10 namespace internal { |
| 11 namespace compiler { | 11 namespace compiler { |
| 12 | 12 |
| 13 ChangeLowering::~ChangeLowering() {} | 13 ChangeLowering::~ChangeLowering() {} |
| 14 | 14 |
| 15 | 15 |
| 16 Reduction ChangeLowering::Reduce(Node* node) { | 16 Reduction ChangeLowering::Reduce(Node* node) { |
| 17 Node* control = graph()->start(); | 17 Node* control = graph()->start(); |
| 18 Node* effect = control; | |
| 19 switch (node->opcode()) { | 18 switch (node->opcode()) { |
| 20 case IrOpcode::kChangeBitToBool: | 19 case IrOpcode::kChangeBitToBool: |
| 21 return ChangeBitToBool(node->InputAt(0), control); | 20 return ChangeBitToBool(node->InputAt(0), control); |
| 22 case IrOpcode::kChangeBoolToBit: | 21 case IrOpcode::kChangeBoolToBit: |
| 23 return ChangeBoolToBit(node->InputAt(0)); | 22 return ChangeBoolToBit(node->InputAt(0)); |
| 23 case IrOpcode::kChangeFloat64ToTagged: |
| 24 return ChangeFloat64ToTagged(node->InputAt(0), control); |
| 24 case IrOpcode::kChangeInt32ToTagged: | 25 case IrOpcode::kChangeInt32ToTagged: |
| 25 return ChangeInt32ToTagged(node->InputAt(0), effect, control); | 26 return ChangeInt32ToTagged(node->InputAt(0), control); |
| 26 case IrOpcode::kChangeTaggedToFloat64: | 27 case IrOpcode::kChangeTaggedToFloat64: |
| 27 return ChangeTaggedToFloat64(node->InputAt(0), effect, control); | 28 return ChangeTaggedToFloat64(node->InputAt(0), control); |
| 29 case IrOpcode::kChangeTaggedToInt32: |
| 30 return ChangeTaggedToInt32(node->InputAt(0), control); |
| 28 default: | 31 default: |
| 29 return NoChange(); | 32 return NoChange(); |
| 30 } | 33 } |
| 31 UNREACHABLE(); | 34 UNREACHABLE(); |
| 32 return NoChange(); | 35 return NoChange(); |
| 33 } | 36 } |
| 34 | 37 |
| 35 | 38 |
| 36 Node* ChangeLowering::HeapNumberValueIndexConstant() { | 39 Node* ChangeLowering::HeapNumberValueIndexConstant() { |
| 37 STATIC_ASSERT(HeapNumber::kValueOffset % kPointerSize == 0); | 40 STATIC_ASSERT(HeapNumber::kValueOffset % kPointerSize == 0); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return Replace(phi); | 73 return Replace(phi); |
| 71 } | 74 } |
| 72 | 75 |
| 73 | 76 |
| 74 Reduction ChangeLowering::ChangeBoolToBit(Node* val) { | 77 Reduction ChangeLowering::ChangeBoolToBit(Node* val) { |
| 75 return Replace( | 78 return Replace( |
| 76 graph()->NewNode(machine()->WordEqual(), val, jsgraph()->TrueConstant())); | 79 graph()->NewNode(machine()->WordEqual(), val, jsgraph()->TrueConstant())); |
| 77 } | 80 } |
| 78 | 81 |
| 79 | 82 |
| 80 Reduction ChangeLowering::ChangeInt32ToTagged(Node* val, Node* effect, | 83 Reduction ChangeLowering::ChangeFloat64ToTagged(Node* val, Node* control) { |
| 81 Node* control) { | 84 return Replace(AllocateHeapNumberWithValue(val, control)); |
| 85 } |
| 86 |
| 87 |
| 88 Reduction ChangeLowering::ChangeInt32ToTagged(Node* val, Node* control) { |
| 82 if (machine()->is64()) { | 89 if (machine()->is64()) { |
| 83 return Replace( | 90 return Replace( |
| 84 graph()->NewNode(machine()->WordShl(), val, SmiShiftBitsConstant())); | 91 graph()->NewNode(machine()->WordShl(), val, SmiShiftBitsConstant())); |
| 85 } | 92 } |
| 86 | 93 |
| 87 Node* context = jsgraph()->SmiConstant(0); | |
| 88 | |
| 89 Node* add = graph()->NewNode(machine()->Int32AddWithOverflow(), val, val); | 94 Node* add = graph()->NewNode(machine()->Int32AddWithOverflow(), val, val); |
| 90 Node* ovf = graph()->NewNode(common()->Projection(1), add); | 95 Node* ovf = graph()->NewNode(common()->Projection(1), add); |
| 91 | 96 |
| 92 Node* branch = graph()->NewNode(common()->Branch(), ovf, control); | 97 Node* branch = graph()->NewNode(common()->Branch(), ovf, control); |
| 93 | 98 |
| 94 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); | 99 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
| 95 Node* number = graph()->NewNode(machine()->ChangeInt32ToFloat64(), val); | 100 Node* heap_number = AllocateHeapNumberWithValue( |
| 96 | 101 graph()->NewNode(machine()->ChangeInt32ToFloat64(), val), if_true); |
| 97 const Runtime::Function* fn = | |
| 98 Runtime::FunctionForId(Runtime::kAllocateHeapNumber); | |
| 99 DCHECK_EQ(0, fn->nargs); | |
| 100 CallDescriptor* desc = linkage()->GetRuntimeCallDescriptor( | |
| 101 fn->function_id, 0, Operator::kNoProperties); | |
| 102 Node* heap_number = graph()->NewNode( | |
| 103 common()->Call(desc), jsgraph()->CEntryStubConstant(), | |
| 104 jsgraph()->ExternalConstant(ExternalReference(fn, isolate())), | |
| 105 jsgraph()->Int32Constant(fn->nargs), context, effect, if_true); | |
| 106 Node* store = graph()->NewNode( | |
| 107 machine()->Store(kMachFloat64, kNoWriteBarrier), heap_number, | |
| 108 HeapNumberValueIndexConstant(), number, heap_number, if_true); | |
| 109 Node* finish = graph()->NewNode(common()->Finish(1), heap_number, store); | |
| 110 | 102 |
| 111 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | 103 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
| 112 Node* smi = graph()->NewNode(common()->Projection(0), add); | 104 Node* smi = graph()->NewNode(common()->Projection(0), add); |
| 113 | 105 |
| 114 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); | 106 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); |
| 115 Node* phi = graph()->NewNode(common()->Phi(2), finish, smi, merge); | 107 Node* phi = graph()->NewNode(common()->Phi(2), heap_number, smi, merge); |
| 116 | 108 |
| 117 return Replace(phi); | 109 return Replace(phi); |
| 118 } | 110 } |
| 119 | 111 |
| 120 | 112 |
| 121 Reduction ChangeLowering::ChangeTaggedToFloat64(Node* val, Node* effect, | 113 Reduction ChangeLowering::ChangeTaggedToInt32(Node* val, Node* control) { |
| 122 Node* control) { | 114 STATIC_ASSERT(kSmiTag == 0); |
| 123 STATIC_ASSERT(kSmiTagMask == 1); | 115 STATIC_ASSERT(kSmiTagMask == 1); |
| 124 | 116 |
| 125 Node* tag = graph()->NewNode(machine()->WordAnd(), val, | 117 Node* tag = graph()->NewNode(machine()->WordAnd(), val, |
| 118 jsgraph()->Int32Constant(kSmiTagMask)); |
| 119 Node* branch = graph()->NewNode(common()->Branch(), tag, control); |
| 120 |
| 121 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
| 122 Node* load = graph()->NewNode( |
| 123 machine()->Load(kMachFloat64), val, HeapNumberValueIndexConstant(), |
| 124 graph()->NewNode(common()->ControlEffect(), if_true)); |
| 125 Node* change = graph()->NewNode(machine()->ChangeFloat64ToInt32(), load); |
| 126 |
| 127 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
| 128 Node* integer = |
| 129 graph()->NewNode(machine()->WordSar(), val, SmiShiftBitsConstant()); |
| 130 Node* number = |
| 131 machine()->is64() |
| 132 ? graph()->NewNode(machine()->ConvertInt64ToInt32(), integer) |
| 133 : integer; |
| 134 |
| 135 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); |
| 136 Node* phi = graph()->NewNode(common()->Phi(2), change, number, merge); |
| 137 |
| 138 return Replace(phi); |
| 139 } |
| 140 |
| 141 |
| 142 Reduction ChangeLowering::ChangeTaggedToFloat64(Node* val, Node* control) { |
| 143 STATIC_ASSERT(kSmiTag == 0); |
| 144 STATIC_ASSERT(kSmiTagMask == 1); |
| 145 |
| 146 Node* tag = graph()->NewNode(machine()->WordAnd(), val, |
| 126 jsgraph()->Int32Constant(kSmiTagMask)); | 147 jsgraph()->Int32Constant(kSmiTagMask)); |
| 127 Node* branch = graph()->NewNode(common()->Branch(), tag, control); | 148 Node* branch = graph()->NewNode(common()->Branch(), tag, control); |
| 128 | 149 |
| 129 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); | 150 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
| 130 Node* load = graph()->NewNode( | 151 Node* load = graph()->NewNode( |
| 131 machine()->Load(kMachFloat64), val, HeapNumberValueIndexConstant(), | 152 machine()->Load(kMachFloat64), val, HeapNumberValueIndexConstant(), |
| 132 graph()->NewNode(common()->ControlEffect(), if_true)); | 153 graph()->NewNode(common()->ControlEffect(), if_true)); |
| 133 | 154 |
| 134 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | 155 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
| 135 Node* integer = | 156 Node* integer = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 150 Isolate* ChangeLowering::isolate() const { return jsgraph()->isolate(); } | 171 Isolate* ChangeLowering::isolate() const { return jsgraph()->isolate(); } |
| 151 | 172 |
| 152 | 173 |
| 153 Graph* ChangeLowering::graph() const { return jsgraph()->graph(); } | 174 Graph* ChangeLowering::graph() const { return jsgraph()->graph(); } |
| 154 | 175 |
| 155 | 176 |
| 156 CommonOperatorBuilder* ChangeLowering::common() const { | 177 CommonOperatorBuilder* ChangeLowering::common() const { |
| 157 return jsgraph()->common(); | 178 return jsgraph()->common(); |
| 158 } | 179 } |
| 159 | 180 |
| 181 |
| 182 Node* ChangeLowering::AllocateHeapNumberWithValue(Node* value, Node* control) { |
| 183 // The AllocateHeapNumber() runtime function does not use the context, so we |
| 184 // can safely pass in Smi zero here. |
| 185 Node* context = jsgraph()->ZeroConstant(); |
| 186 Node* effect = graph()->NewNode(common()->ValueEffect(1), value); |
| 187 const Runtime::Function* function = |
| 188 Runtime::FunctionForId(Runtime::kAllocateHeapNumber); |
| 189 DCHECK_EQ(0, function->nargs); |
| 190 CallDescriptor* desc = linkage()->GetRuntimeCallDescriptor( |
| 191 function->function_id, 0, Operator::kNoProperties); |
| 192 Node* heap_number = graph()->NewNode( |
| 193 common()->Call(desc), jsgraph()->CEntryStubConstant(), |
| 194 jsgraph()->ExternalConstant(ExternalReference(function, isolate())), |
| 195 jsgraph()->Int32Constant(function->nargs), context, effect, control); |
| 196 Node* store = graph()->NewNode( |
| 197 machine()->Store(kMachFloat64, kNoWriteBarrier), heap_number, |
| 198 HeapNumberValueIndexConstant(), value, heap_number, control); |
| 199 return graph()->NewNode(common()->Finish(1), heap_number, store); |
| 200 } |
| 201 |
| 160 } // namespace compiler | 202 } // namespace compiler |
| 161 } // namespace internal | 203 } // namespace internal |
| 162 } // namespace v8 | 204 } // namespace v8 |
| OLD | NEW |