OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "src/compiler/change-lowering.h" |
| 6 #include "src/compiler/common-operator.h" |
| 7 #include "src/compiler/graph.h" |
| 8 #include "src/compiler/node-properties-inl.h" |
| 9 #include "src/compiler/simplified-operator.h" |
| 10 #include "src/factory.h" |
| 11 #include "test/compiler-unittests/compiler-unittests.h" |
| 12 #include "test/compiler-unittests/node-matchers.h" |
| 13 #include "testing/gtest-type-names.h" |
| 14 |
| 15 using testing::_; |
| 16 |
| 17 namespace v8 { |
| 18 namespace internal { |
| 19 namespace compiler { |
| 20 |
| 21 template <typename T> |
| 22 class ChangeLoweringTest : public CompilerTest { |
| 23 public: |
| 24 static const size_t kPointerSize = sizeof(T); |
| 25 |
| 26 explicit ChangeLoweringTest(int num_parameters = 1) |
| 27 : graph_(zone()), common_(zone()), simplified_(zone()) { |
| 28 graph()->SetStart(graph()->NewNode(common()->Start(num_parameters))); |
| 29 } |
| 30 virtual ~ChangeLoweringTest() {} |
| 31 |
| 32 protected: |
| 33 Node* Parameter(int32_t index = 0) { |
| 34 return graph()->NewNode(common()->Parameter(index), graph()->start()); |
| 35 } |
| 36 |
| 37 Reduction Reduce(Node* node) { |
| 38 CompilationInfo info(isolate(), zone()); |
| 39 Linkage linkage(&info); |
| 40 ChangeLowering<kPointerSize> reducer(graph(), &linkage); |
| 41 return reducer.Reduce(node); |
| 42 } |
| 43 |
| 44 Graph* graph() { return &graph_; } |
| 45 Factory* factory() const { return isolate()->factory(); } |
| 46 CommonOperatorBuilder* common() { return &common_; } |
| 47 SimplifiedOperatorBuilder* simplified() { return &simplified_; } |
| 48 |
| 49 PrintableUnique<HeapObject> true_unique() { |
| 50 return PrintableUnique<HeapObject>::CreateImmovable( |
| 51 zone(), factory()->true_value()); |
| 52 } |
| 53 PrintableUnique<HeapObject> false_unique() { |
| 54 return PrintableUnique<HeapObject>::CreateImmovable( |
| 55 zone(), factory()->false_value()); |
| 56 } |
| 57 |
| 58 private: |
| 59 Graph graph_; |
| 60 CommonOperatorBuilder common_; |
| 61 SimplifiedOperatorBuilder simplified_; |
| 62 }; |
| 63 |
| 64 |
| 65 typedef ::testing::Types<int32_t, int64_t> ChangeLoweringTypes; |
| 66 TYPED_TEST_CASE(ChangeLoweringTest, ChangeLoweringTypes); |
| 67 |
| 68 |
| 69 TYPED_TEST(ChangeLoweringTest, ChangeBitToBool) { |
| 70 Node* val = this->Parameter(0); |
| 71 Node* node = |
| 72 this->graph()->NewNode(this->simplified()->ChangeBitToBool(), val); |
| 73 Reduction reduction = this->Reduce(node); |
| 74 ASSERT_TRUE(reduction.Changed()); |
| 75 |
| 76 Node* phi = reduction.replacement(); |
| 77 EXPECT_THAT(phi, IsPhi(IsHeapConstant(this->true_unique()), |
| 78 IsHeapConstant(this->false_unique()), _)); |
| 79 |
| 80 Node* merge = NodeProperties::GetControlInput(phi); |
| 81 ASSERT_EQ(IrOpcode::kMerge, merge->opcode()); |
| 82 |
| 83 Node* if_true = NodeProperties::GetControlInput(merge, 0); |
| 84 ASSERT_EQ(IrOpcode::kIfTrue, if_true->opcode()); |
| 85 |
| 86 Node* if_false = NodeProperties::GetControlInput(merge, 1); |
| 87 ASSERT_EQ(IrOpcode::kIfFalse, if_false->opcode()); |
| 88 |
| 89 Node* branch = NodeProperties::GetControlInput(if_true); |
| 90 EXPECT_EQ(branch, NodeProperties::GetControlInput(if_false)); |
| 91 EXPECT_THAT(branch, IsBranch(val, this->graph()->start())); |
| 92 } |
| 93 |
| 94 |
| 95 TYPED_TEST(ChangeLoweringTest, StringAdd) { |
| 96 Node* node = this->graph()->NewNode(this->simplified()->StringAdd(), |
| 97 this->Parameter(0), this->Parameter(1)); |
| 98 Reduction reduction = this->Reduce(node); |
| 99 EXPECT_FALSE(reduction.Changed()); |
| 100 } |
| 101 |
| 102 |
| 103 class ChangeLowering32Test : public ChangeLoweringTest<int32_t> { |
| 104 public: |
| 105 virtual ~ChangeLowering32Test() {} |
| 106 }; |
| 107 |
| 108 |
| 109 TEST_F(ChangeLowering32Test, ChangeBoolToBit) { |
| 110 Node* val = Parameter(0); |
| 111 Node* node = graph()->NewNode(simplified()->ChangeBoolToBit(), val); |
| 112 Reduction reduction = Reduce(node); |
| 113 ASSERT_TRUE(reduction.Changed()); |
| 114 |
| 115 EXPECT_THAT(reduction.replacement(), |
| 116 IsWord32Equal(val, IsHeapConstant(true_unique()))); |
| 117 } |
| 118 |
| 119 |
| 120 TEST_F(ChangeLowering32Test, ChangeInt32ToTagged) { |
| 121 Node* val = Parameter(0); |
| 122 Node* node = graph()->NewNode(simplified()->ChangeInt32ToTagged(), val); |
| 123 Reduction reduction = Reduce(node); |
| 124 ASSERT_TRUE(reduction.Changed()); |
| 125 |
| 126 Node* phi = reduction.replacement(); |
| 127 ASSERT_EQ(IrOpcode::kPhi, phi->opcode()); |
| 128 |
| 129 Node* smi = NodeProperties::GetValueInput(phi, 1); |
| 130 ASSERT_THAT(smi, IsProjection(0, IsInt32AddWithOverflow(val, val))); |
| 131 |
| 132 Node* heap_number = NodeProperties::GetValueInput(phi, 0); |
| 133 ASSERT_EQ(IrOpcode::kCall, heap_number->opcode()); |
| 134 |
| 135 Node* merge = NodeProperties::GetControlInput(phi); |
| 136 ASSERT_EQ(IrOpcode::kMerge, merge->opcode()); |
| 137 |
| 138 const int32_t kValueOffset = HeapNumber::kValueOffset - kHeapObjectTag; |
| 139 EXPECT_THAT(NodeProperties::GetControlInput(merge, 0), |
| 140 IsStore(kMachineFloat64, kNoWriteBarrier, heap_number, |
| 141 IsInt32Constant(kValueOffset), |
| 142 IsChangeInt32ToFloat64(val), _, heap_number)); |
| 143 |
| 144 Node* if_true = NodeProperties::GetControlInput(heap_number); |
| 145 ASSERT_EQ(IrOpcode::kIfTrue, if_true->opcode()); |
| 146 |
| 147 Node* if_false = NodeProperties::GetControlInput(merge, 1); |
| 148 ASSERT_EQ(IrOpcode::kIfFalse, if_false->opcode()); |
| 149 |
| 150 Node* branch = NodeProperties::GetControlInput(if_true); |
| 151 EXPECT_EQ(branch, NodeProperties::GetControlInput(if_false)); |
| 152 EXPECT_THAT(branch, |
| 153 IsBranch(IsProjection(1, IsInt32AddWithOverflow(val, val)), |
| 154 graph()->start())); |
| 155 } |
| 156 |
| 157 |
| 158 TEST_F(ChangeLowering32Test, ChangeTaggedToFloat64) { |
| 159 Node* val = Parameter(0); |
| 160 Node* node = graph()->NewNode(simplified()->ChangeTaggedToFloat64(), val); |
| 161 Reduction reduction = Reduce(node); |
| 162 ASSERT_TRUE(reduction.Changed()); |
| 163 |
| 164 const int32_t kShiftAmount = |
| 165 kSmiTagSize + SmiTagging<kPointerSize>::kSmiShiftSize; |
| 166 const int32_t kValueOffset = HeapNumber::kValueOffset - kHeapObjectTag; |
| 167 Node* phi = reduction.replacement(); |
| 168 ASSERT_THAT( |
| 169 phi, IsPhi(IsLoad(kMachineFloat64, val, IsInt32Constant(kValueOffset), _), |
| 170 IsChangeInt32ToFloat64( |
| 171 IsWord32Sar(val, IsInt32Constant(kShiftAmount))), |
| 172 _)); |
| 173 |
| 174 Node* merge = NodeProperties::GetControlInput(phi); |
| 175 ASSERT_EQ(IrOpcode::kMerge, merge->opcode()); |
| 176 |
| 177 Node* if_true = NodeProperties::GetControlInput(merge, 0); |
| 178 ASSERT_EQ(IrOpcode::kIfTrue, if_true->opcode()); |
| 179 |
| 180 Node* if_false = NodeProperties::GetControlInput(merge, 1); |
| 181 ASSERT_EQ(IrOpcode::kIfFalse, if_false->opcode()); |
| 182 |
| 183 Node* branch = NodeProperties::GetControlInput(if_true); |
| 184 EXPECT_EQ(branch, NodeProperties::GetControlInput(if_false)); |
| 185 STATIC_ASSERT(kSmiTag == 0); |
| 186 STATIC_ASSERT(kSmiTagSize == 1); |
| 187 EXPECT_THAT(branch, IsBranch(IsWord32And(val, IsInt32Constant(kSmiTagMask)), |
| 188 graph()->start())); |
| 189 } |
| 190 |
| 191 |
| 192 class ChangeLowering64Test : public ChangeLoweringTest<int64_t> { |
| 193 public: |
| 194 virtual ~ChangeLowering64Test() {} |
| 195 }; |
| 196 |
| 197 |
| 198 TEST_F(ChangeLowering64Test, ChangeBoolToBit) { |
| 199 Node* val = Parameter(0); |
| 200 Node* node = graph()->NewNode(simplified()->ChangeBoolToBit(), val); |
| 201 Reduction reduction = Reduce(node); |
| 202 ASSERT_TRUE(reduction.Changed()); |
| 203 |
| 204 EXPECT_THAT(reduction.replacement(), |
| 205 IsWord64Equal(val, IsHeapConstant(true_unique()))); |
| 206 } |
| 207 |
| 208 |
| 209 TEST_F(ChangeLowering64Test, ChangeInt32ToTagged) { |
| 210 Node* val = Parameter(0); |
| 211 Node* node = graph()->NewNode(simplified()->ChangeInt32ToTagged(), val); |
| 212 Reduction reduction = Reduce(node); |
| 213 ASSERT_TRUE(reduction.Changed()); |
| 214 |
| 215 const int32_t kShiftAmount = |
| 216 kSmiTagSize + SmiTagging<kPointerSize>::kSmiShiftSize; |
| 217 EXPECT_THAT(reduction.replacement(), |
| 218 IsWord64Shl(val, IsInt32Constant(kShiftAmount))); |
| 219 } |
| 220 |
| 221 |
| 222 TEST_F(ChangeLowering64Test, ChangeTaggedToFloat64) { |
| 223 Node* val = Parameter(0); |
| 224 Node* node = graph()->NewNode(simplified()->ChangeTaggedToFloat64(), val); |
| 225 Reduction reduction = Reduce(node); |
| 226 ASSERT_TRUE(reduction.Changed()); |
| 227 |
| 228 const int32_t kShiftAmount = |
| 229 kSmiTagSize + SmiTagging<kPointerSize>::kSmiShiftSize; |
| 230 const int32_t kValueOffset = HeapNumber::kValueOffset - kHeapObjectTag; |
| 231 Node* phi = reduction.replacement(); |
| 232 ASSERT_THAT( |
| 233 phi, IsPhi(IsLoad(kMachineFloat64, val, IsInt32Constant(kValueOffset), _), |
| 234 IsChangeInt32ToFloat64(IsConvertInt64ToInt32( |
| 235 IsWord64Sar(val, IsInt32Constant(kShiftAmount)))), |
| 236 _)); |
| 237 |
| 238 Node* merge = NodeProperties::GetControlInput(phi); |
| 239 ASSERT_EQ(IrOpcode::kMerge, merge->opcode()); |
| 240 |
| 241 Node* if_true = NodeProperties::GetControlInput(merge, 0); |
| 242 ASSERT_EQ(IrOpcode::kIfTrue, if_true->opcode()); |
| 243 |
| 244 Node* if_false = NodeProperties::GetControlInput(merge, 1); |
| 245 ASSERT_EQ(IrOpcode::kIfFalse, if_false->opcode()); |
| 246 |
| 247 Node* branch = NodeProperties::GetControlInput(if_true); |
| 248 EXPECT_EQ(branch, NodeProperties::GetControlInput(if_false)); |
| 249 STATIC_ASSERT(kSmiTag == 0); |
| 250 STATIC_ASSERT(kSmiTagSize == 1); |
| 251 EXPECT_THAT(branch, IsBranch(IsWord64And(val, IsInt32Constant(kSmiTagMask)), |
| 252 graph()->start())); |
| 253 } |
| 254 |
| 255 } // namespace compiler |
| 256 } // namespace internal |
| 257 } // namespace v8 |
OLD | NEW |