OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 <functional> | 5 #include <functional> |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "graph-tester.h" | 9 #include "graph-tester.h" |
10 #include "src/compiler/node.h" | 10 #include "src/compiler/node.h" |
11 #include "src/compiler/operator.h" | 11 #include "src/compiler/operator.h" |
12 | 12 |
13 using namespace v8::internal; | 13 using namespace v8::internal; |
14 using namespace v8::internal::compiler; | 14 using namespace v8::internal::compiler; |
15 | 15 |
16 static Operator dummy_operator(IrOpcode::kParameter, Operator::kNoWrite, | 16 static Operator dummy_operator(IrOpcode::kParameter, Operator::kNoWrite, |
17 "dummy", 0, 0, 0, 1, 0, 0); | 17 "dummy", 0, 0, 0, 1, 0, 0); |
18 | 18 |
19 TEST(NodeAllocation) { | |
20 GraphTester graph; | |
21 Node* n1 = graph.NewNode(&dummy_operator); | |
22 Node* n2 = graph.NewNode(&dummy_operator); | |
23 CHECK(n2->id() != n1->id()); | |
24 } | |
25 | |
26 | |
27 TEST(NodeWithOpcode) { | |
28 GraphTester graph; | |
29 Node* n1 = graph.NewNode(&dummy_operator); | |
30 Node* n2 = graph.NewNode(&dummy_operator); | |
31 CHECK(n1->op() == &dummy_operator); | |
32 CHECK(n2->op() == &dummy_operator); | |
33 } | |
34 | |
35 | |
36 TEST(NodeInputs1) { | |
37 GraphTester graph; | |
38 Node* n0 = graph.NewNode(&dummy_operator); | |
39 Node* n2 = graph.NewNode(&dummy_operator, n0); | |
40 CHECK_EQ(1, n2->InputCount()); | |
41 CHECK(n0 == n2->InputAt(0)); | |
42 } | |
43 | |
44 | |
45 TEST(NodeInputs2) { | |
46 GraphTester graph; | |
47 Node* n0 = graph.NewNode(&dummy_operator); | |
48 Node* n1 = graph.NewNode(&dummy_operator); | |
49 Node* n2 = graph.NewNode(&dummy_operator, n0, n1); | |
50 CHECK_EQ(2, n2->InputCount()); | |
51 CHECK(n0 == n2->InputAt(0)); | |
52 CHECK(n1 == n2->InputAt(1)); | |
53 } | |
54 | |
55 | |
56 TEST(NodeInputs3) { | |
57 GraphTester graph; | |
58 Node* n0 = graph.NewNode(&dummy_operator); | |
59 Node* n1 = graph.NewNode(&dummy_operator); | |
60 Node* n2 = graph.NewNode(&dummy_operator, n0, n1, n1); | |
61 CHECK_EQ(3, n2->InputCount()); | |
62 CHECK(n0 == n2->InputAt(0)); | |
63 CHECK(n1 == n2->InputAt(1)); | |
64 CHECK(n1 == n2->InputAt(2)); | |
65 } | |
66 | |
67 | |
68 TEST(NodeInputIteratorEmpty) { | |
69 GraphTester graph; | |
70 Node* n1 = graph.NewNode(&dummy_operator); | |
71 Node::Inputs::iterator i(n1->inputs().begin()); | |
72 int input_count = 0; | |
73 for (; i != n1->inputs().end(); ++i) { | |
74 input_count++; | |
75 } | |
76 CHECK_EQ(0, input_count); | |
77 } | |
78 | |
79 | |
80 TEST(NodeInputIteratorOne) { | |
81 GraphTester graph; | |
82 Node* n0 = graph.NewNode(&dummy_operator); | |
83 Node* n1 = graph.NewNode(&dummy_operator, n0); | |
84 Node::Inputs::iterator i(n1->inputs().begin()); | |
85 CHECK_EQ(1, n1->InputCount()); | |
86 CHECK_EQ(n0, *i); | |
87 ++i; | |
88 CHECK(n1->inputs().end() == i); | |
89 } | |
90 | |
91 | |
92 TEST(NodeUseIteratorEmpty) { | |
93 GraphTester graph; | |
94 Node* n1 = graph.NewNode(&dummy_operator); | |
95 int use_count = 0; | |
96 for (Edge const edge : n1->use_edges()) { | |
97 USE(edge); | |
98 use_count++; | |
99 } | |
100 CHECK_EQ(0, use_count); | |
101 } | |
102 | |
103 | |
104 TEST(NodeUseIteratorOne) { | |
105 GraphTester graph; | |
106 Node* n0 = graph.NewNode(&dummy_operator); | |
107 Node* n1 = graph.NewNode(&dummy_operator, n0); | |
108 Node::Uses::iterator i(n0->uses().begin()); | |
109 CHECK_EQ(n1, *i); | |
110 ++i; | |
111 CHECK(n0->uses().end() == i); | |
112 } | |
113 | |
114 | |
115 TEST(NodeUseIteratorReplaceNoUses) { | |
116 GraphTester graph; | |
117 Node* n0 = graph.NewNode(&dummy_operator); | |
118 Node* n1 = graph.NewNode(&dummy_operator); | |
119 Node* n2 = graph.NewNode(&dummy_operator); | |
120 Node* n3 = graph.NewNode(&dummy_operator, n2); | |
121 n0->ReplaceUses(n1); | |
122 CHECK(n0->uses().begin() == n0->uses().end()); | |
123 n0->ReplaceUses(n2); | |
124 CHECK(n0->uses().begin() == n0->uses().end()); | |
125 USE(n3); | |
126 } | |
127 | |
128 | |
129 TEST(NodeUseIteratorReplaceUses) { | 19 TEST(NodeUseIteratorReplaceUses) { |
130 GraphTester graph; | 20 GraphTester graph; |
131 Node* n0 = graph.NewNode(&dummy_operator); | 21 Node* n0 = graph.NewNode(&dummy_operator); |
132 Node* n1 = graph.NewNode(&dummy_operator, n0); | 22 Node* n1 = graph.NewNode(&dummy_operator, n0); |
133 Node* n2 = graph.NewNode(&dummy_operator, n0); | 23 Node* n2 = graph.NewNode(&dummy_operator, n0); |
134 Node* n3 = graph.NewNode(&dummy_operator); | 24 Node* n3 = graph.NewNode(&dummy_operator); |
135 Node::Uses::iterator i1(n0->uses().begin()); | 25 auto i1(n0->uses().begin()); |
136 CHECK_EQ(n1, *i1); | 26 CHECK_EQ(n1, *i1); |
137 ++i1; | 27 ++i1; |
138 CHECK_EQ(n2, *i1); | 28 CHECK_EQ(n2, *i1); |
139 n0->ReplaceUses(n3); | 29 n0->ReplaceUses(n3); |
140 Node::Uses::iterator i2(n3->uses().begin()); | 30 auto i2(n3->uses().begin()); |
141 CHECK_EQ(n1, *i2); | 31 CHECK_EQ(n1, *i2); |
142 ++i2; | 32 ++i2; |
143 CHECK_EQ(n2, *i2); | 33 CHECK_EQ(n2, *i2); |
144 Node::Inputs::iterator i3(n1->inputs().begin()); | 34 auto i3(n1->inputs().begin()); |
145 CHECK_EQ(n3, *i3); | 35 CHECK_EQ(n3, *i3); |
146 ++i3; | 36 ++i3; |
147 CHECK(n1->inputs().end() == i3); | 37 CHECK(n1->inputs().end() == i3); |
148 Node::Inputs::iterator i4(n2->inputs().begin()); | 38 auto i4(n2->inputs().begin()); |
149 CHECK_EQ(n3, *i4); | 39 CHECK_EQ(n3, *i4); |
150 ++i4; | 40 ++i4; |
151 CHECK(n2->inputs().end() == i4); | 41 CHECK(n2->inputs().end() == i4); |
152 } | 42 } |
153 | 43 |
154 | 44 |
155 TEST(NodeUseIteratorReplaceUsesSelf) { | 45 TEST(NodeUseIteratorReplaceUsesSelf) { |
156 GraphTester graph; | 46 GraphTester graph; |
157 Node* n0 = graph.NewNode(&dummy_operator); | 47 Node* n0 = graph.NewNode(&dummy_operator); |
158 Node* n1 = graph.NewNode(&dummy_operator, n0); | 48 Node* n1 = graph.NewNode(&dummy_operator, n0); |
159 Node* n3 = graph.NewNode(&dummy_operator); | 49 Node* n3 = graph.NewNode(&dummy_operator); |
160 | 50 |
161 n1->ReplaceInput(0, n1); // Create self-reference. | 51 n1->ReplaceInput(0, n1); // Create self-reference. |
162 | 52 |
163 Node::Uses::iterator i1(n1->uses().begin()); | 53 auto i1(n1->uses().begin()); |
164 CHECK_EQ(n1, *i1); | 54 CHECK_EQ(n1, *i1); |
165 | 55 |
166 n1->ReplaceUses(n3); | 56 n1->ReplaceUses(n3); |
167 | 57 |
168 CHECK(n1->uses().begin() == n1->uses().end()); | 58 CHECK(n1->uses().begin() == n1->uses().end()); |
169 | 59 |
170 Node::Uses::iterator i2(n3->uses().begin()); | 60 auto i2(n3->uses().begin()); |
171 CHECK_EQ(n1, *i2); | 61 CHECK_EQ(n1, *i2); |
172 ++i2; | 62 ++i2; |
173 CHECK(n1->uses().end() == i2); | 63 CHECK(n1->uses().end() == i2); |
174 } | 64 } |
175 | 65 |
176 | 66 |
177 TEST(ReplaceInput) { | 67 TEST(ReplaceInput) { |
178 GraphTester graph; | 68 GraphTester graph; |
179 Node* n0 = graph.NewNode(&dummy_operator); | 69 Node* n0 = graph.NewNode(&dummy_operator); |
180 Node* n1 = graph.NewNode(&dummy_operator); | 70 Node* n1 = graph.NewNode(&dummy_operator); |
181 Node* n2 = graph.NewNode(&dummy_operator); | 71 Node* n2 = graph.NewNode(&dummy_operator); |
182 Node* n3 = graph.NewNode(&dummy_operator, n0, n1, n2); | 72 Node* n3 = graph.NewNode(&dummy_operator, n0, n1, n2); |
183 Node::Inputs::iterator i1(n3->inputs().begin()); | 73 auto i1(n3->inputs().begin()); |
184 CHECK(n0 == *i1); | 74 CHECK(n0 == *i1); |
185 CHECK_EQ(n0, n3->InputAt(0)); | 75 CHECK_EQ(n0, n3->InputAt(0)); |
186 ++i1; | 76 ++i1; |
187 CHECK_EQ(n1, *i1); | 77 CHECK_EQ(n1, *i1); |
188 CHECK_EQ(n1, n3->InputAt(1)); | 78 CHECK_EQ(n1, n3->InputAt(1)); |
189 ++i1; | 79 ++i1; |
190 CHECK_EQ(n2, *i1); | 80 CHECK_EQ(n2, *i1); |
191 CHECK_EQ(n2, n3->InputAt(2)); | 81 CHECK_EQ(n2, n3->InputAt(2)); |
192 ++i1; | 82 ++i1; |
193 CHECK(i1 == n3->inputs().end()); | 83 CHECK(i1 == n3->inputs().end()); |
194 | 84 |
195 Node::Uses::iterator i2(n1->uses().begin()); | 85 auto i2(n1->uses().begin()); |
196 CHECK_EQ(n3, *i2); | 86 CHECK_EQ(n3, *i2); |
197 ++i2; | 87 ++i2; |
198 CHECK(i2 == n1->uses().end()); | 88 CHECK(i2 == n1->uses().end()); |
199 | 89 |
200 Node* n4 = graph.NewNode(&dummy_operator); | 90 Node* n4 = graph.NewNode(&dummy_operator); |
201 Node::Uses::iterator i3(n4->uses().begin()); | 91 auto i3(n4->uses().begin()); |
202 CHECK(i3 == n4->uses().end()); | 92 CHECK(i3 == n4->uses().end()); |
203 | 93 |
204 n3->ReplaceInput(1, n4); | 94 n3->ReplaceInput(1, n4); |
205 | 95 |
206 Node::Uses::iterator i4(n1->uses().begin()); | 96 auto i4(n1->uses().begin()); |
207 CHECK(i4 == n1->uses().end()); | 97 CHECK(i4 == n1->uses().end()); |
208 | 98 |
209 Node::Uses::iterator i5(n4->uses().begin()); | 99 auto i5(n4->uses().begin()); |
210 CHECK_EQ(n3, *i5); | 100 CHECK_EQ(n3, *i5); |
211 ++i5; | 101 ++i5; |
212 CHECK(i5 == n4->uses().end()); | 102 CHECK(i5 == n4->uses().end()); |
213 | 103 |
214 Node::Inputs::iterator i6(n3->inputs().begin()); | 104 auto i6(n3->inputs().begin()); |
215 CHECK(n0 == *i6); | 105 CHECK(n0 == *i6); |
216 CHECK_EQ(n0, n3->InputAt(0)); | 106 CHECK_EQ(n0, n3->InputAt(0)); |
217 ++i6; | 107 ++i6; |
218 CHECK_EQ(n4, *i6); | 108 CHECK_EQ(n4, *i6); |
219 CHECK_EQ(n4, n3->InputAt(1)); | 109 CHECK_EQ(n4, n3->InputAt(1)); |
220 ++i6; | 110 ++i6; |
221 CHECK_EQ(n2, *i6); | 111 CHECK_EQ(n2, *i6); |
222 CHECK_EQ(n2, n3->InputAt(2)); | 112 CHECK_EQ(n2, n3->InputAt(2)); |
223 ++i6; | 113 ++i6; |
224 CHECK(i6 == n3->inputs().end()); | 114 CHECK(i6 == n3->inputs().end()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 n3->AppendInput(graph.zone(), n4); | 204 n3->AppendInput(graph.zone(), n4); |
315 CHECK_EQ(5, n3->InputCount()); | 205 CHECK_EQ(5, n3->InputCount()); |
316 CHECK(n3->InputAt(0) == n0); | 206 CHECK(n3->InputAt(0) == n0); |
317 CHECK(n3->InputAt(1) == n1); | 207 CHECK(n3->InputAt(1) == n1); |
318 CHECK(n3->InputAt(2) == n2); | 208 CHECK(n3->InputAt(2) == n2); |
319 CHECK(n3->InputAt(3) == n4); | 209 CHECK(n3->InputAt(3) == n4); |
320 CHECK(n3->InputAt(4) == n4); | 210 CHECK(n3->InputAt(4) == n4); |
321 | 211 |
322 // Make sure uses have been hooked op correctly. | 212 // Make sure uses have been hooked op correctly. |
323 Node::Uses uses(n4->uses()); | 213 Node::Uses uses(n4->uses()); |
324 Node::Uses::iterator current = uses.begin(); | 214 auto current = uses.begin(); |
325 CHECK(current != uses.end()); | 215 CHECK(current != uses.end()); |
326 CHECK(*current == n3); | 216 CHECK(*current == n3); |
327 ++current; | 217 ++current; |
328 CHECK(current != uses.end()); | 218 CHECK(current != uses.end()); |
329 CHECK(*current == n5); | 219 CHECK(*current == n5); |
330 ++current; | 220 ++current; |
331 CHECK(current != uses.end()); | 221 CHECK(current != uses.end()); |
332 CHECK(*current == n3); | 222 CHECK(*current == n3); |
333 ++current; | 223 ++current; |
334 CHECK(current == uses.end()); | 224 CHECK(current == uses.end()); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 Node* n3 = graph.NewNode(&dummy_operator); | 333 Node* n3 = graph.NewNode(&dummy_operator); |
444 n2->AppendInput(graph.zone(), n1); | 334 n2->AppendInput(graph.zone(), n1); |
445 n2->AppendInput(graph.zone(), n0); | 335 n2->AppendInput(graph.zone(), n0); |
446 CHECK_EQ(0, n3->UseCount()); | 336 CHECK_EQ(0, n3->UseCount()); |
447 CHECK_EQ(3, n0->UseCount()); | 337 CHECK_EQ(3, n0->UseCount()); |
448 n0->ReplaceUses(n3); | 338 n0->ReplaceUses(n3); |
449 CHECK_EQ(0, n0->UseCount()); | 339 CHECK_EQ(0, n0->UseCount()); |
450 CHECK_EQ(3, n3->UseCount()); | 340 CHECK_EQ(3, n3->UseCount()); |
451 | 341 |
452 Node::Uses uses(n3->uses()); | 342 Node::Uses uses(n3->uses()); |
453 Node::Uses::iterator current = uses.begin(); | 343 auto current = uses.begin(); |
454 CHECK(current != uses.end()); | 344 CHECK(current != uses.end()); |
455 CHECK(*current == n1); | 345 CHECK(*current == n1); |
456 ++current; | 346 ++current; |
457 CHECK(current != uses.end()); | 347 CHECK(current != uses.end()); |
458 CHECK(*current == n2); | 348 CHECK(*current == n2); |
459 ++current; | 349 ++current; |
460 CHECK(current != uses.end()); | 350 CHECK(current != uses.end()); |
461 CHECK(*current == n2); | 351 CHECK(*current == n2); |
462 ++current; | 352 ++current; |
463 CHECK(current == uses.end()); | 353 CHECK(current == uses.end()); |
464 } | 354 } |
465 | 355 |
466 | 356 |
467 template <bool result> | |
468 struct FixedPredicate { | |
469 bool operator()(const Node* node) const { return result; } | |
470 }; | |
471 | |
472 | |
473 TEST(ReplaceUsesIfWithFixedPredicate) { | |
474 GraphTester graph; | |
475 | |
476 Node* n0 = graph.NewNode(&dummy_operator); | |
477 Node* n1 = graph.NewNode(&dummy_operator, n0); | |
478 Node* n2 = graph.NewNode(&dummy_operator, n0); | |
479 Node* n3 = graph.NewNode(&dummy_operator); | |
480 | |
481 CHECK_EQ(0, n2->UseCount()); | |
482 n2->ReplaceUsesIf(FixedPredicate<true>(), n1); | |
483 CHECK_EQ(0, n2->UseCount()); | |
484 n2->ReplaceUsesIf(FixedPredicate<false>(), n1); | |
485 CHECK_EQ(0, n2->UseCount()); | |
486 | |
487 CHECK_EQ(0, n3->UseCount()); | |
488 n3->ReplaceUsesIf(FixedPredicate<true>(), n1); | |
489 CHECK_EQ(0, n3->UseCount()); | |
490 n3->ReplaceUsesIf(FixedPredicate<false>(), n1); | |
491 CHECK_EQ(0, n3->UseCount()); | |
492 | |
493 CHECK_EQ(2, n0->UseCount()); | |
494 CHECK_EQ(0, n1->UseCount()); | |
495 n0->ReplaceUsesIf(FixedPredicate<false>(), n1); | |
496 CHECK_EQ(2, n0->UseCount()); | |
497 CHECK_EQ(0, n1->UseCount()); | |
498 n0->ReplaceUsesIf(FixedPredicate<true>(), n1); | |
499 CHECK_EQ(0, n0->UseCount()); | |
500 CHECK_EQ(2, n1->UseCount()); | |
501 | |
502 n1->AppendInput(graph.zone(), n1); | |
503 CHECK_EQ(3, n1->UseCount()); | |
504 n1->AppendInput(graph.zone(), n3); | |
505 CHECK_EQ(1, n3->UseCount()); | |
506 n3->ReplaceUsesIf(FixedPredicate<true>(), n1); | |
507 CHECK_EQ(4, n1->UseCount()); | |
508 CHECK_EQ(0, n3->UseCount()); | |
509 n1->ReplaceUsesIf(FixedPredicate<false>(), n3); | |
510 CHECK_EQ(4, n1->UseCount()); | |
511 CHECK_EQ(0, n3->UseCount()); | |
512 } | |
513 | |
514 | |
515 TEST(ReplaceUsesIfWithEqualTo) { | |
516 GraphTester graph; | |
517 | |
518 Node* n0 = graph.NewNode(&dummy_operator); | |
519 Node* n1 = graph.NewNode(&dummy_operator, n0); | |
520 Node* n2 = graph.NewNode(&dummy_operator, n0, n1); | |
521 | |
522 CHECK_EQ(0, n2->UseCount()); | |
523 n2->ReplaceUsesIf(std::bind1st(std::equal_to<Node*>(), n1), n0); | |
524 CHECK_EQ(0, n2->UseCount()); | |
525 | |
526 CHECK_EQ(2, n0->UseCount()); | |
527 CHECK_EQ(1, n1->UseCount()); | |
528 n1->ReplaceUsesIf(std::bind1st(std::equal_to<Node*>(), n0), n0); | |
529 CHECK_EQ(2, n0->UseCount()); | |
530 CHECK_EQ(1, n1->UseCount()); | |
531 n0->ReplaceUsesIf(std::bind2nd(std::equal_to<Node*>(), n2), n1); | |
532 CHECK_EQ(1, n0->UseCount()); | |
533 CHECK_EQ(2, n1->UseCount()); | |
534 } | |
535 | |
536 | |
537 TEST(ReplaceInputMultipleUses) { | 357 TEST(ReplaceInputMultipleUses) { |
538 GraphTester graph; | 358 GraphTester graph; |
539 | 359 |
540 Node* n0 = graph.NewNode(&dummy_operator); | 360 Node* n0 = graph.NewNode(&dummy_operator); |
541 Node* n1 = graph.NewNode(&dummy_operator); | 361 Node* n1 = graph.NewNode(&dummy_operator); |
542 Node* n2 = graph.NewNode(&dummy_operator, n0); | 362 Node* n2 = graph.NewNode(&dummy_operator, n0); |
543 n2->ReplaceInput(0, n1); | 363 n2->ReplaceInput(0, n1); |
544 CHECK_EQ(0, n0->UseCount()); | 364 CHECK_EQ(0, n0->UseCount()); |
545 CHECK_EQ(1, n1->UseCount()); | 365 CHECK_EQ(1, n1->UseCount()); |
546 | 366 |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 n1->ReplaceInput(0, n1); // self-reference. | 649 n1->ReplaceInput(0, n1); // self-reference. |
830 | 650 |
831 CHECK_EQ(0, n0->UseCount()); | 651 CHECK_EQ(0, n0->UseCount()); |
832 CHECK_EQ(1, n1->UseCount()); | 652 CHECK_EQ(1, n1->UseCount()); |
833 n1->RemoveAllInputs(); | 653 n1->RemoveAllInputs(); |
834 CHECK_EQ(1, n1->InputCount()); | 654 CHECK_EQ(1, n1->InputCount()); |
835 CHECK_EQ(0, n1->UseCount()); | 655 CHECK_EQ(0, n1->UseCount()); |
836 CHECK_EQ(NULL, n1->InputAt(0)); | 656 CHECK_EQ(NULL, n1->InputAt(0)); |
837 } | 657 } |
838 } | 658 } |
OLD | NEW |