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/v8.h" |
| 6 #include "test/cctest/cctest.h" |
| 7 |
| 8 #include "src/compiler/common-operator.h" |
| 9 #include "src/compiler/graph-inl.h" |
| 10 #include "src/compiler/phi-reducer.h" |
| 11 |
| 12 using namespace v8::internal; |
| 13 using namespace v8::internal::compiler; |
| 14 |
| 15 class PhiReducerTester : HandleAndZoneScope { |
| 16 public: |
| 17 PhiReducerTester() |
| 18 : isolate(main_isolate()), |
| 19 common(main_zone()), |
| 20 graph(main_zone()), |
| 21 self(graph.NewNode(common.Start())), |
| 22 dead(graph.NewNode(common.Dead())) { |
| 23 } |
| 24 |
| 25 Isolate* isolate; |
| 26 CommonOperatorBuilder common; |
| 27 Graph graph; |
| 28 Node* self; |
| 29 Node* dead; |
| 30 |
| 31 void CheckReduce(Node* expect, Node* phi) { |
| 32 PhiReducer reducer; |
| 33 Reduction reduction = reducer.Reduce(phi); |
| 34 if (expect == phi) { |
| 35 CHECK(!reduction.Changed()); |
| 36 } else { |
| 37 CHECK(reduction.Changed()); |
| 38 CHECK_EQ(expect, reduction.replacement()); |
| 39 } |
| 40 } |
| 41 |
| 42 Node* Int32Constant(int32_t val) { |
| 43 return graph.NewNode(common.Int32Constant(val)); |
| 44 } |
| 45 |
| 46 Node* Float64Constant(double val) { |
| 47 return graph.NewNode(common.Float64Constant(val)); |
| 48 } |
| 49 |
| 50 Node* Parameter(int32_t index = 0) { |
| 51 return graph.NewNode(common.Parameter(index)); |
| 52 } |
| 53 |
| 54 Node* Phi(Node* a) { |
| 55 return SetSelfReferences(graph.NewNode(common.Phi(1), a)); |
| 56 } |
| 57 |
| 58 Node* Phi(Node* a, Node* b) { |
| 59 return SetSelfReferences(graph.NewNode(common.Phi(2), a, b)); |
| 60 } |
| 61 |
| 62 Node* Phi(Node* a, Node* b, Node* c) { |
| 63 return SetSelfReferences(graph.NewNode(common.Phi(3), a, b, c)); |
| 64 } |
| 65 |
| 66 Node* Phi(Node* a, Node* b, Node* c, Node* d) { |
| 67 return SetSelfReferences(graph.NewNode(common.Phi(4), a, b, c, d)); |
| 68 } |
| 69 |
| 70 Node* PhiWithControl(Node* a, Node* control) { |
| 71 return SetSelfReferences(graph.NewNode(common.Phi(1), a, control)); |
| 72 } |
| 73 |
| 74 Node* PhiWithControl(Node* a, Node* b, Node* control) { |
| 75 return SetSelfReferences(graph.NewNode(common.Phi(2), a, b, control)); |
| 76 } |
| 77 |
| 78 Node* SetSelfReferences(Node* node) { |
| 79 Node::Inputs inputs = node->inputs(); |
| 80 for (Node::Inputs::iterator iter(inputs.begin()); |
| 81 iter != inputs.end(); ++iter) { |
| 82 Node* input = *iter; |
| 83 if (input == self) node->ReplaceInput(iter.index(), node); |
| 84 } |
| 85 return node; |
| 86 } |
| 87 }; |
| 88 |
| 89 |
| 90 TEST(PhiReduce1) { |
| 91 PhiReducerTester R; |
| 92 Node* zero = R.Int32Constant(0); |
| 93 Node* one = R.Int32Constant(1); |
| 94 Node* oneish = R.Float64Constant(1.1); |
| 95 Node* param = R.Parameter(); |
| 96 |
| 97 Node* singles[] = { zero, one, oneish, param }; |
| 98 for (size_t i = 0; i < ARRAY_SIZE(singles); i++) { |
| 99 R.CheckReduce(singles[i], R.Phi(singles[i])); |
| 100 } |
| 101 } |
| 102 |
| 103 |
| 104 TEST(PhiReduce2) { |
| 105 PhiReducerTester R; |
| 106 Node* zero = R.Int32Constant(0); |
| 107 Node* one = R.Int32Constant(1); |
| 108 Node* oneish = R.Float64Constant(1.1); |
| 109 Node* param = R.Parameter(); |
| 110 |
| 111 Node* singles[] = { zero, one, oneish, param }; |
| 112 for (size_t i = 0; i < ARRAY_SIZE(singles); i++) { |
| 113 Node* a = singles[i]; |
| 114 R.CheckReduce(a, R.Phi(a, a)); |
| 115 } |
| 116 |
| 117 for (size_t i = 0; i < ARRAY_SIZE(singles); i++) { |
| 118 Node* a = singles[i]; |
| 119 R.CheckReduce(a, R.Phi(R.self, a)); |
| 120 R.CheckReduce(a, R.Phi(a, R.self)); |
| 121 } |
| 122 |
| 123 for (size_t i = 1; i < ARRAY_SIZE(singles); i++) { |
| 124 Node* a = singles[i], *b = singles[0]; |
| 125 Node* phi1 = R.Phi(b, a); |
| 126 R.CheckReduce(phi1, phi1); |
| 127 |
| 128 Node* phi2 = R.Phi(a, b); |
| 129 R.CheckReduce(phi2, phi2); |
| 130 } |
| 131 } |
| 132 |
| 133 |
| 134 TEST(PhiReduce3) { |
| 135 PhiReducerTester R; |
| 136 Node* zero = R.Int32Constant(0); |
| 137 Node* one = R.Int32Constant(1); |
| 138 Node* oneish = R.Float64Constant(1.1); |
| 139 Node* param = R.Parameter(); |
| 140 |
| 141 Node* singles[] = { zero, one, oneish, param }; |
| 142 for (size_t i = 0; i < ARRAY_SIZE(singles); i++) { |
| 143 Node* a = singles[i]; |
| 144 R.CheckReduce(a, R.Phi(a, a, a)); |
| 145 } |
| 146 |
| 147 for (size_t i = 0; i < ARRAY_SIZE(singles); i++) { |
| 148 Node* a = singles[i]; |
| 149 R.CheckReduce(a, R.Phi(R.self, a, a)); |
| 150 R.CheckReduce(a, R.Phi(a, R.self, a)); |
| 151 R.CheckReduce(a, R.Phi(a, a, R.self)); |
| 152 } |
| 153 |
| 154 for (size_t i = 1; i < ARRAY_SIZE(singles); i++) { |
| 155 Node* a = singles[i], *b = singles[0]; |
| 156 Node* phi1 = R.Phi(b, a, a); |
| 157 R.CheckReduce(phi1, phi1); |
| 158 |
| 159 Node* phi2 = R.Phi(a, b, a); |
| 160 R.CheckReduce(phi2, phi2); |
| 161 |
| 162 Node* phi3 = R.Phi(a, a, b); |
| 163 R.CheckReduce(phi3, phi3); |
| 164 } |
| 165 } |
| 166 |
| 167 |
| 168 TEST(PhiReduce4) { |
| 169 PhiReducerTester R; |
| 170 Node* zero = R.Int32Constant(0); |
| 171 Node* one = R.Int32Constant(1); |
| 172 Node* oneish = R.Float64Constant(1.1); |
| 173 Node* param = R.Parameter(); |
| 174 |
| 175 Node* singles[] = { zero, one, oneish, param }; |
| 176 for (size_t i = 0; i < ARRAY_SIZE(singles); i++) { |
| 177 Node* a = singles[i]; |
| 178 R.CheckReduce(a, R.Phi(a, a, a, a)); |
| 179 } |
| 180 |
| 181 for (size_t i = 0; i < ARRAY_SIZE(singles); i++) { |
| 182 Node* a = singles[i]; |
| 183 R.CheckReduce(a, R.Phi(R.self, a, a, a)); |
| 184 R.CheckReduce(a, R.Phi(a, R.self, a, a)); |
| 185 R.CheckReduce(a, R.Phi(a, a, R.self, a)); |
| 186 R.CheckReduce(a, R.Phi(a, a, a, R.self)); |
| 187 |
| 188 R.CheckReduce(a, R.Phi(R.self, R.self, a, a)); |
| 189 R.CheckReduce(a, R.Phi(a, R.self, R.self, a)); |
| 190 R.CheckReduce(a, R.Phi(a, a, R.self, R.self)); |
| 191 R.CheckReduce(a, R.Phi(R.self, a, a, R.self)); |
| 192 } |
| 193 |
| 194 for (size_t i = 1; i < ARRAY_SIZE(singles); i++) { |
| 195 Node* a = singles[i], *b = singles[0]; |
| 196 Node* phi1 = R.Phi(b, a, a, a); |
| 197 R.CheckReduce(phi1, phi1); |
| 198 |
| 199 Node* phi2 = R.Phi(a, b, a, a); |
| 200 R.CheckReduce(phi2, phi2); |
| 201 |
| 202 Node* phi3 = R.Phi(a, a, b, a); |
| 203 R.CheckReduce(phi3, phi3); |
| 204 |
| 205 Node* phi4 = R.Phi(a, a, a, b); |
| 206 R.CheckReduce(phi4, phi4); |
| 207 } |
| 208 } |
| 209 |
| 210 |
| 211 TEST(PhiReduceShouldIgnoreControlNodes) { |
| 212 PhiReducerTester R; |
| 213 Node* zero = R.Int32Constant(0); |
| 214 Node* one = R.Int32Constant(1); |
| 215 Node* oneish = R.Float64Constant(1.1); |
| 216 Node* param = R.Parameter(); |
| 217 |
| 218 Node* singles[] = { zero, one, oneish, param }; |
| 219 for (size_t i = 0; i < ARRAY_SIZE(singles); ++i) { |
| 220 R.CheckReduce(singles[i], R.PhiWithControl(singles[i], R.dead)); |
| 221 R.CheckReduce(singles[i], R.PhiWithControl(R.self, singles[i], R.dead)); |
| 222 R.CheckReduce(singles[i], R.PhiWithControl(singles[i], R.self, R.dead)); |
| 223 } |
| 224 } |
OLD | NEW |