OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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/linkage.h" | 5 #include "src/compiler/linkage.h" |
6 #include "src/compiler/tail-call-optimization.h" | 6 #include "src/compiler/tail-call-optimization.h" |
7 #include "test/unittests/compiler/graph-unittest.h" | 7 #include "test/unittests/compiler/graph-unittest.h" |
8 #include "test/unittests/compiler/node-test-utils.h" | 8 #include "test/unittests/compiler/node-test-utils.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 21 matching lines...) Expand all Loading... |
32 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( | 32 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( |
33 CallDescriptor::kCallCodeObject, MachineType::AnyTagged(), | 33 CallDescriptor::kCallCodeObject, MachineType::AnyTagged(), |
34 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 34 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
35 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, | 35 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, |
36 Operator::kNoProperties, 0, 0, CallDescriptor::kNoFlags); | 36 Operator::kNoProperties, 0, 0, CallDescriptor::kNoFlags); |
37 Node* p0 = Parameter(0); | 37 Node* p0 = Parameter(0); |
38 Node* p1 = Parameter(1); | 38 Node* p1 = Parameter(1); |
39 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, | 39 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, |
40 graph()->start(), graph()->start()); | 40 graph()->start(), graph()->start()); |
41 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); | 41 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); |
42 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 42 Node* ret = graph()->NewNode(common()->Return(), call, call, if_success); |
43 Node* ret = | |
44 graph()->NewNode(common()->Return(), zero, call, call, if_success); | |
45 Reduction r = Reduce(ret); | 43 Reduction r = Reduce(ret); |
46 ASSERT_FALSE(r.Changed()); | 44 ASSERT_FALSE(r.Changed()); |
47 } | 45 } |
48 | 46 |
49 | 47 |
50 TEST_F(TailCallOptimizationTest, CallCodeObject1) { | 48 TEST_F(TailCallOptimizationTest, CallCodeObject1) { |
51 LinkageLocation kLocationSignature[] = { | 49 LinkageLocation kLocationSignature[] = { |
52 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 50 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
53 LinkageLocation::ForRegister(1, MachineType::Pointer())}; | 51 LinkageLocation::ForRegister(1, MachineType::Pointer())}; |
54 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( | 52 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( |
55 CallDescriptor::kCallCodeObject, MachineType::AnyTagged(), | 53 CallDescriptor::kCallCodeObject, MachineType::AnyTagged(), |
56 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 54 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
57 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, | 55 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, |
58 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); | 56 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); |
59 Node* p0 = Parameter(0); | 57 Node* p0 = Parameter(0); |
60 Node* p1 = Parameter(1); | 58 Node* p1 = Parameter(1); |
61 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, | 59 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, |
62 graph()->start(), graph()->start()); | 60 graph()->start(), graph()->start()); |
63 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); | 61 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); |
64 Node* if_exception = graph()->NewNode(common()->IfException(), call, call); | 62 Node* if_exception = graph()->NewNode(common()->IfException(), call, call); |
65 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 63 Node* ret = graph()->NewNode(common()->Return(), call, call, if_success); |
66 Node* ret = | |
67 graph()->NewNode(common()->Return(), zero, call, call, if_success); | |
68 Node* end = graph()->NewNode(common()->End(1), if_exception); | 64 Node* end = graph()->NewNode(common()->End(1), if_exception); |
69 graph()->SetEnd(end); | 65 graph()->SetEnd(end); |
70 Reduction r = Reduce(ret); | 66 Reduction r = Reduce(ret); |
71 ASSERT_FALSE(r.Changed()); | 67 ASSERT_FALSE(r.Changed()); |
72 } | 68 } |
73 | 69 |
74 | 70 |
75 TEST_F(TailCallOptimizationTest, CallCodeObject2) { | 71 TEST_F(TailCallOptimizationTest, CallCodeObject2) { |
76 LinkageLocation kLocationSignature[] = { | 72 LinkageLocation kLocationSignature[] = { |
77 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 73 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
78 LinkageLocation::ForRegister(1, MachineType::Pointer())}; | 74 LinkageLocation::ForRegister(1, MachineType::Pointer())}; |
79 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( | 75 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( |
80 CallDescriptor::kCallCodeObject, MachineType::AnyTagged(), | 76 CallDescriptor::kCallCodeObject, MachineType::AnyTagged(), |
81 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 77 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
82 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, | 78 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, |
83 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); | 79 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); |
84 Node* p0 = Parameter(0); | 80 Node* p0 = Parameter(0); |
85 Node* p1 = Parameter(1); | 81 Node* p1 = Parameter(1); |
86 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, | 82 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, |
87 graph()->start(), graph()->start()); | 83 graph()->start(), graph()->start()); |
88 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); | 84 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); |
89 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 85 Node* ret = graph()->NewNode(common()->Return(), call, call, if_success); |
90 Node* ret = | |
91 graph()->NewNode(common()->Return(), zero, call, call, if_success); | |
92 Reduction r = Reduce(ret); | 86 Reduction r = Reduce(ret); |
93 ASSERT_TRUE(r.Changed()); | 87 ASSERT_TRUE(r.Changed()); |
94 EXPECT_THAT(r.replacement(), IsTailCall(kCallDescriptor, p0, p1, | 88 EXPECT_THAT(r.replacement(), IsTailCall(kCallDescriptor, p0, p1, |
95 graph()->start(), graph()->start())); | 89 graph()->start(), graph()->start())); |
96 } | 90 } |
97 | 91 |
98 | 92 |
99 TEST_F(TailCallOptimizationTest, CallJSFunction0) { | 93 TEST_F(TailCallOptimizationTest, CallJSFunction0) { |
100 LinkageLocation kLocationSignature[] = { | 94 LinkageLocation kLocationSignature[] = { |
101 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 95 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
102 LinkageLocation::ForRegister(1, MachineType::Pointer())}; | 96 LinkageLocation::ForRegister(1, MachineType::Pointer())}; |
103 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( | 97 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( |
104 CallDescriptor::kCallJSFunction, MachineType::AnyTagged(), | 98 CallDescriptor::kCallJSFunction, MachineType::AnyTagged(), |
105 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 99 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
106 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, | 100 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, |
107 Operator::kNoProperties, 0, 0, CallDescriptor::kNoFlags); | 101 Operator::kNoProperties, 0, 0, CallDescriptor::kNoFlags); |
108 Node* p0 = Parameter(0); | 102 Node* p0 = Parameter(0); |
109 Node* p1 = Parameter(1); | 103 Node* p1 = Parameter(1); |
110 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, | 104 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, |
111 graph()->start(), graph()->start()); | 105 graph()->start(), graph()->start()); |
112 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); | 106 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); |
113 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 107 Node* ret = graph()->NewNode(common()->Return(), call, call, if_success); |
114 Node* ret = | |
115 graph()->NewNode(common()->Return(), zero, call, call, if_success); | |
116 Reduction r = Reduce(ret); | 108 Reduction r = Reduce(ret); |
117 ASSERT_FALSE(r.Changed()); | 109 ASSERT_FALSE(r.Changed()); |
118 } | 110 } |
119 | 111 |
120 | 112 |
121 TEST_F(TailCallOptimizationTest, CallJSFunction1) { | 113 TEST_F(TailCallOptimizationTest, CallJSFunction1) { |
122 LinkageLocation kLocationSignature[] = { | 114 LinkageLocation kLocationSignature[] = { |
123 LinkageLocation::ForRegister(0, MachineType::Pointer()), | 115 LinkageLocation::ForRegister(0, MachineType::Pointer()), |
124 LinkageLocation::ForRegister(1, MachineType::Pointer())}; | 116 LinkageLocation::ForRegister(1, MachineType::Pointer())}; |
125 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( | 117 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( |
126 CallDescriptor::kCallJSFunction, MachineType::AnyTagged(), | 118 CallDescriptor::kCallJSFunction, MachineType::AnyTagged(), |
127 LinkageLocation::ForRegister(0), | 119 LinkageLocation::ForRegister(0), |
128 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, | 120 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, |
129 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); | 121 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); |
130 Node* p0 = Parameter(0); | 122 Node* p0 = Parameter(0); |
131 Node* p1 = Parameter(1); | 123 Node* p1 = Parameter(1); |
132 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, | 124 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, |
133 graph()->start(), graph()->start()); | 125 graph()->start(), graph()->start()); |
134 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); | 126 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); |
135 Node* if_exception = graph()->NewNode(common()->IfException(), call, call); | 127 Node* if_exception = graph()->NewNode(common()->IfException(), call, call); |
136 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 128 Node* ret = graph()->NewNode(common()->Return(), call, call, if_success); |
137 Node* ret = | |
138 graph()->NewNode(common()->Return(), zero, call, call, if_success); | |
139 Node* end = graph()->NewNode(common()->End(1), if_exception); | 129 Node* end = graph()->NewNode(common()->End(1), if_exception); |
140 graph()->SetEnd(end); | 130 graph()->SetEnd(end); |
141 Reduction r = Reduce(ret); | 131 Reduction r = Reduce(ret); |
142 ASSERT_FALSE(r.Changed()); | 132 ASSERT_FALSE(r.Changed()); |
143 } | 133 } |
144 | 134 |
145 | 135 |
146 TEST_F(TailCallOptimizationTest, CallJSFunction2) { | 136 TEST_F(TailCallOptimizationTest, CallJSFunction2) { |
147 LinkageLocation kLocationSignature[] = {LinkageLocation::ForRegister(0), | 137 LinkageLocation kLocationSignature[] = {LinkageLocation::ForRegister(0), |
148 LinkageLocation::ForRegister(1)}; | 138 LinkageLocation::ForRegister(1)}; |
149 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( | 139 const CallDescriptor* kCallDescriptor = new (zone()) CallDescriptor( |
150 CallDescriptor::kCallJSFunction, MachineType::AnyTagged(), | 140 CallDescriptor::kCallJSFunction, MachineType::AnyTagged(), |
151 LinkageLocation::ForRegister(0), | 141 LinkageLocation::ForRegister(0), |
152 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, | 142 new (zone()) LocationSignature(1, 1, kLocationSignature), 0, |
153 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); | 143 Operator::kNoProperties, 0, 0, CallDescriptor::kSupportsTailCalls); |
154 Node* p0 = Parameter(0); | 144 Node* p0 = Parameter(0); |
155 Node* p1 = Parameter(1); | 145 Node* p1 = Parameter(1); |
156 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, | 146 Node* call = graph()->NewNode(common()->Call(kCallDescriptor), p0, p1, |
157 graph()->start(), graph()->start()); | 147 graph()->start(), graph()->start()); |
158 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); | 148 Node* if_success = graph()->NewNode(common()->IfSuccess(), call); |
159 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 149 Node* ret = graph()->NewNode(common()->Return(), call, call, if_success); |
160 Node* ret = | |
161 graph()->NewNode(common()->Return(), zero, call, call, if_success); | |
162 Reduction r = Reduce(ret); | 150 Reduction r = Reduce(ret); |
163 ASSERT_TRUE(r.Changed()); | 151 ASSERT_TRUE(r.Changed()); |
164 EXPECT_THAT(r.replacement(), IsTailCall(kCallDescriptor, p0, p1, | 152 EXPECT_THAT(r.replacement(), IsTailCall(kCallDescriptor, p0, p1, |
165 graph()->start(), graph()->start())); | 153 graph()->start(), graph()->start())); |
166 } | 154 } |
167 | 155 |
168 | 156 |
169 } // namespace compiler | 157 } // namespace compiler |
170 } // namespace internal | 158 } // namespace internal |
171 } // namespace v8 | 159 } // namespace v8 |
OLD | NEW |