OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // Test deoptimization. | |
5 | |
6 class SmiCompares { | |
7 // Test deoptimization when one argument is known to be Smi. | |
8 static bool smiCompareLessThan2(a) { | |
9 return a < 2; | |
10 } | |
11 | |
12 // Test deoptimization when one argument is known to be Smi. | |
13 static bool smiCompareGreaterThan2(a) { | |
14 return 2 < a; | |
15 } | |
16 | |
17 // Test deoptimization when both arguments unknown. | |
18 static bool smiCompareLessThan(a, b) { | |
19 return a < b; | |
20 } | |
21 | |
22 // Test deoptimization when both arguments unknown. | |
23 static bool smiCompareGreaterThan(a, b) { | |
24 return a > b; | |
25 } | |
26 | |
27 static smiComparesTest() { | |
28 for (int i = 0; i < 2000; i++) { | |
29 Expect.equals(true, smiCompareLessThan2(1)); | |
30 Expect.equals(false, smiCompareLessThan2(3)); | |
31 Expect.equals(false, smiCompareGreaterThan2(1)); | |
32 Expect.equals(true, smiCompareGreaterThan2(3)); | |
33 Expect.equals(true, smiCompareLessThan(1, 2)); | |
34 Expect.equals(false, smiCompareGreaterThan(1, 2)); | |
35 } | |
36 // Deoptimize by passing a double instead of Smi | |
37 Expect.equals(true, smiCompareLessThan2(1.0)); | |
38 Expect.equals(false, smiCompareGreaterThan2(1.0)); | |
39 Expect.equals(true, smiCompareLessThan(1.0, 2)); | |
40 Expect.equals(false, smiCompareGreaterThan(1, 2.0)); | |
41 } | |
42 } | |
43 | |
44 | |
45 class SmiBinop { | |
46 static subWithLiteral(a) { | |
47 return a - 1; | |
48 } | |
49 | |
50 static void smiBinopTest() { | |
51 for (int i = 0; i < 2000; i++) { | |
52 Expect.equals(2, subWithLiteral(3)); | |
53 } | |
54 // Deoptimize. | |
55 Expect.equals(2.0, subWithLiteral(3.0)); | |
56 } | |
57 | |
58 static mul(x) { | |
59 return x * 1024; | |
60 } | |
61 | |
62 static void smiBinopOverflowTest() { | |
63 final int big = 536870912; | |
64 for (int i = 0; i < 2000; i++) { | |
65 Expect.equals(1024, mul(1)); | |
66 } | |
67 // Deoptimize by overflow. | |
68 Expect.equals(1024 * big, mul(big)); | |
69 } | |
70 } | |
71 | |
72 | |
73 class ObjectsEquality { | |
74 static bool compareEqual(a, b) { | |
75 return a == b; | |
76 } | |
77 | |
78 static bool compareNotEqual(a, b) { | |
79 return a != b; | |
80 } | |
81 | |
82 // Use only Object.==. | |
83 static void objectsEqualityTest() { | |
84 var a = new ObjectsEquality(); | |
85 var b = new ObjectsEquality(); | |
86 final nan = 0.0/0.0; | |
87 for (int i = 0; i < 1000; i++) { | |
88 Expect.equals(true, compareEqual(a, a)); | |
89 Expect.equals(true, compareEqual(null, null)); | |
90 Expect.equals(false, compareEqual(null, a)); | |
91 Expect.equals(false, compareEqual(a, null)); | |
92 Expect.equals(true, compareEqual(b, b)); | |
93 Expect.equals(false, compareEqual(a, b)); | |
94 | |
95 Expect.equals(false, compareNotEqual(a, a)); | |
96 Expect.equals(false, compareNotEqual(null, null)); | |
97 Expect.equals(true, compareNotEqual(null, a)); | |
98 Expect.equals(true, compareNotEqual(a, null)); | |
99 Expect.equals(false, compareNotEqual(b, b)); | |
100 Expect.equals(true, compareNotEqual(a, b)); | |
101 } | |
102 var c = new SmiBinop(); | |
103 // Deoptimize. | |
104 Expect.equals(true, compareEqual(c, c)); | |
105 Expect.equals(false, compareEqual(c, null)); | |
106 Expect.equals(false, compareNotEqual(c, c)); | |
107 Expect.equals(true, compareNotEqual(c, null)); | |
108 } | |
109 } | |
110 | |
111 class DeoptimizationTest { | |
112 static foo(a, b) { | |
113 return a - b; | |
114 } | |
115 | |
116 static test1() { | |
117 for (int i = 0; i < 2000; i++) { | |
118 Expect.equals(2, foo(3, 1)); // <-- Optimizes 'foo', | |
119 } | |
120 Expect.equals(2.2, foo(1.2, -1.0)); // <-- Deoptimizes 'foo'. | |
121 for (int i = 0; i < 10000; i++) { | |
122 Expect.equals(2, foo(3, 1)); // <-- Optimizes 'foo'. | |
123 } | |
124 Expect.equals(2.2, foo(1.2, -1)); // <-- Deoptimizes 'foo'. | |
125 } | |
126 | |
127 static moo(n) { | |
128 return ++n; | |
129 } | |
130 | |
131 static test2() { | |
132 for (int i = 0; i < 2000; i++) { | |
133 Expect.equals(4, moo(3)); // <-- Optimizes 'moo', | |
134 } | |
135 Expect.equals(2.2, moo(1.2)); // <-- Deoptimizes 'moo'. | |
136 for (int i = 0; i < 10000; i++) { | |
137 Expect.equals(4, moo(3)); // <-- Optimizes 'moo'. | |
138 } | |
139 Expect.equals(2.2, moo(1.2)); // <-- Deoptimizes 'moo'. | |
140 } | |
141 | |
142 static test3() { | |
143 for (int i = 0; i < 2000; i++) { | |
144 Expect.equals(2.0, foo(3.0, 1.0)); // <-- Optimizes 'foo', | |
145 } | |
146 Expect.equals(2, foo(1, -1)); // <-- Deoptimizes 'foo'. | |
147 for (int i = 0; i < 2000; i++) { | |
148 Expect.equals(2.0, foo(3.0, 1.0)); // <-- Optimizes 'foo', | |
149 } | |
150 Expect.equals(2.2, moo(1.2)); // <-- Deoptimizes 'moo'. | |
151 } | |
152 | |
153 static bool compareInt(a, b) { | |
154 return a < b; | |
155 } | |
156 | |
157 static bool compareDouble(a, b) { | |
158 return a < b; | |
159 } | |
160 | |
161 static test4() { | |
162 for (int i = 0; i < 2000; i++) { | |
163 Expect.equals(true, compareInt(1, 2)); | |
164 Expect.equals(true, compareDouble(1.0, 2.0)); | |
165 } | |
166 // Trigger deoptimization in compareInt and compareDouble. | |
167 Expect.equals(true, compareInt(1, 2.0)); | |
168 Expect.equals(true, compareDouble(1.0, 2)); | |
169 } | |
170 | |
171 static smiRightShift() { | |
172 int ShiftRight(int a, int b) { return a >> b; } | |
173 | |
174 for (int i = 0; i < 2000; i++) { | |
175 var r = ShiftRight(10, 2); | |
176 Expect.equals(2, r); | |
177 } | |
178 // ShiftRight is optimized. | |
179 Expect.equals(0, ShiftRight(10, 64)); | |
180 // Deoptimize ShiftRight because 'a' is a Mint. | |
181 var mint = 1 << 63; | |
182 Expect.equals(1 << 3, ShiftRight(mint, 60)); | |
183 } | |
184 | |
185 static doubleUnary() { | |
186 num unary(num a) { return -a; } | |
187 | |
188 for (int i = 0; i < 2000; i++) { | |
189 var r = unary(2.0); | |
190 Expect.equals(-2.0, r); | |
191 } | |
192 var r = unary(5); | |
193 Expect.equals(-5, r); | |
194 } | |
195 | |
196 static void testMain() { | |
197 test1(); | |
198 test2(); | |
199 test3(); | |
200 test4(); | |
201 SmiCompares.smiComparesTest(); | |
202 SmiBinop.smiBinopTest(); | |
203 SmiBinop.smiBinopOverflowTest(); | |
204 ObjectsEquality.objectsEqualityTest(); | |
205 smiRightShift(); | |
206 doubleUnary(); | |
207 } | |
208 } | |
209 | |
210 main() { | |
211 DeoptimizationTest.testMain(); | |
212 } | |
OLD | NEW |