| 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 import "package:expect/expect.dart"; | |
| 7 | |
| 8 class SmiCompares { | |
| 9 // Test deoptimization when one argument is known to be Smi. | |
| 10 static bool smiCompareLessThan2(a) { | |
| 11 return a < 2; | |
| 12 } | |
| 13 | |
| 14 // Test deoptimization when one argument is known to be Smi. | |
| 15 static bool smiCompareGreaterThan2(a) { | |
| 16 return 2 < a; | |
| 17 } | |
| 18 | |
| 19 // Test deoptimization when both arguments unknown. | |
| 20 static bool smiCompareLessThan(a, b) { | |
| 21 return a < b; | |
| 22 } | |
| 23 | |
| 24 // Test deoptimization when both arguments unknown. | |
| 25 static bool smiCompareGreaterThan(a, b) { | |
| 26 return a > b; | |
| 27 } | |
| 28 | |
| 29 static smiComparesTest() { | |
| 30 for (int i = 0; i < 2000; i++) { | |
| 31 Expect.equals(true, smiCompareLessThan2(1)); | |
| 32 Expect.equals(false, smiCompareLessThan2(3)); | |
| 33 Expect.equals(false, smiCompareGreaterThan2(1)); | |
| 34 Expect.equals(true, smiCompareGreaterThan2(3)); | |
| 35 Expect.equals(true, smiCompareLessThan(1, 2)); | |
| 36 Expect.equals(false, smiCompareGreaterThan(1, 2)); | |
| 37 } | |
| 38 // Deoptimize by passing a double instead of Smi | |
| 39 Expect.equals(true, smiCompareLessThan2(1.0)); | |
| 40 Expect.equals(false, smiCompareGreaterThan2(1.0)); | |
| 41 Expect.equals(true, smiCompareLessThan(1.0, 2)); | |
| 42 Expect.equals(false, smiCompareGreaterThan(1, 2.0)); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 class SmiBinop { | |
| 47 static subWithLiteral(a) { | |
| 48 return a - 1; | |
| 49 } | |
| 50 | |
| 51 static void smiBinopTest() { | |
| 52 for (int i = 0; i < 2000; i++) { | |
| 53 Expect.equals(2, subWithLiteral(3)); | |
| 54 } | |
| 55 // Deoptimize. | |
| 56 Expect.equals(2.0, subWithLiteral(3.0)); | |
| 57 } | |
| 58 | |
| 59 static mul(x) { | |
| 60 return x * 1024; | |
| 61 } | |
| 62 | |
| 63 static void smiBinopOverflowTest() { | |
| 64 final int big = 536870912; | |
| 65 for (int i = 0; i < 2000; i++) { | |
| 66 Expect.equals(1024, mul(1)); | |
| 67 } | |
| 68 // Deoptimize by overflow. | |
| 69 Expect.equals(1024 * big, mul(big)); | |
| 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) { | |
| 173 return a >> b; | |
| 174 } | |
| 175 | |
| 176 for (int i = 0; i < 2000; i++) { | |
| 177 var r = ShiftRight(10, 2); | |
| 178 Expect.equals(2, r); | |
| 179 } | |
| 180 // ShiftRight is optimized. | |
| 181 Expect.equals(0, ShiftRight(10, 64)); | |
| 182 // Deoptimize ShiftRight because 'a' is a Mint. | |
| 183 var mint = 1 << 63; | |
| 184 Expect.equals(1 << 3, ShiftRight(mint, 60)); | |
| 185 } | |
| 186 | |
| 187 static doubleUnary() { | |
| 188 num unary(num a) { | |
| 189 return -a; | |
| 190 } | |
| 191 | |
| 192 for (int i = 0; i < 2000; i++) { | |
| 193 var r = unary(2.0); | |
| 194 Expect.equals(-2.0, r); | |
| 195 } | |
| 196 var r = unary(5); | |
| 197 Expect.equals(-5, r); | |
| 198 } | |
| 199 | |
| 200 static void testMain() { | |
| 201 test1(); | |
| 202 test2(); | |
| 203 test3(); | |
| 204 test4(); | |
| 205 SmiCompares.smiComparesTest(); | |
| 206 SmiBinop.smiBinopTest(); | |
| 207 SmiBinop.smiBinopOverflowTest(); | |
| 208 ObjectsEquality.objectsEqualityTest(); | |
| 209 smiRightShift(); | |
| 210 doubleUnary(); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 main() { | |
| 215 DeoptimizationTest.testMain(); | |
| 216 } | |
| OLD | NEW |