OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 allocation sinking optimization. |
| 5 |
| 6 import 'package:expect/expect.dart'; |
| 7 |
| 8 class Point { |
| 9 var x, y; |
| 10 |
| 11 Point(this.x, this.y); |
| 12 |
| 13 operator * (other) { |
| 14 return x * other.x + y * other.y; |
| 15 } |
| 16 } |
| 17 |
| 18 class C { |
| 19 var p; |
| 20 C(this.p); |
| 21 } |
| 22 |
| 23 class D { |
| 24 var p; |
| 25 D(this.p); |
| 26 } |
| 27 |
| 28 // Class that is used to capture materialized Point object with * operator. |
| 29 class F { |
| 30 var p; |
| 31 var val; |
| 32 |
| 33 F(this.p); |
| 34 |
| 35 operator * (other) { |
| 36 Expect.isTrue(other is Point); |
| 37 Expect.equals(42.0, other.x); |
| 38 Expect.equals(0.5, other.y); |
| 39 |
| 40 if (val == null) { |
| 41 val = other; |
| 42 } else { |
| 43 Expect.isTrue(identical(val, other)); |
| 44 } |
| 45 |
| 46 return this.p * other; |
| 47 } |
| 48 } |
| 49 |
| 50 test1(c, x, y) { |
| 51 var a = new Point(x - 0.5, y + 0.5); |
| 52 var b = new Point(x + 0.5, y + 0.8); |
| 53 var d = new Point(c.p * a, c.p * b); |
| 54 return d * d; |
| 55 } |
| 56 |
| 57 effects() { |
| 58 // This function should not be inlinable. |
| 59 try { } catch (e) { } |
| 60 } |
| 61 |
| 62 testForwardingThroughEffects(c, x, y) { |
| 63 var a = new Point(x - 0.5, y + 0.5); |
| 64 var b = new Point(x - 0.5, y - 0.8); |
| 65 var d = new Point(c.p * a, c.p * b); |
| 66 // Effects can't affect neither a, b, nor d because they do not escape. |
| 67 effects(); |
| 68 effects(); |
| 69 return ((a == null) ? 0.0 : 0.1) + (d * d); |
| 70 } |
| 71 |
| 72 testIdentity(x) { |
| 73 var y = new Point(42.0, 0.5); |
| 74 var z = y; |
| 75 return x * y + x * z; |
| 76 } |
| 77 |
| 78 class PointP<T> { |
| 79 var x, y; |
| 80 |
| 81 PointP(this.x, this.y); |
| 82 |
| 83 operator * (other) { |
| 84 return x * other.x + y * other.y; |
| 85 } |
| 86 } |
| 87 |
| 88 foo2() => new PointP<int>(1, 3) * new PointP<num>(5, 6); |
| 89 |
| 90 main() { |
| 91 var c = new C(new Point(0.1, 0.2)); |
| 92 |
| 93 // Compute initial values. |
| 94 final x0 = test1(c, 11.11, 22.22); |
| 95 final y0 = testForwardingThroughEffects(c, 11.11, 22.22); |
| 96 final z0 = testIdentity(c.p); |
| 97 |
| 98 // Force optimization. |
| 99 for (var i = 0; i < 10000; i++) { |
| 100 test1(c, i.toDouble(), i.toDouble()); |
| 101 testForwardingThroughEffects(c, i.toDouble(), i.toDouble()); |
| 102 testIdentity(c.p); |
| 103 foo2(); |
| 104 } |
| 105 |
| 106 // Test returned value after optimization. |
| 107 final x1 = test1(c, 11.11, 22.22); |
| 108 final y1 = testForwardingThroughEffects(c, 11.11, 22.22); |
| 109 |
| 110 // Test returned value after deopt. |
| 111 final x2 = test1(new D(c.p), 11.11, 22.22); |
| 112 final y2 = testForwardingThroughEffects(new D(c.p), 11.11, 22.22); |
| 113 |
| 114 Expect.equals(6465, (x0 * 100).floor()); |
| 115 Expect.equals(6465, (x1 * 100).floor()); |
| 116 Expect.equals(6465, (x2 * 100).floor()); |
| 117 Expect.equals(x0, x1); |
| 118 Expect.equals(x0, x2); |
| 119 |
| 120 Expect.equals(6008, (y0 * 100).floor()); |
| 121 Expect.equals(6008, (y1 * 100).floor()); |
| 122 Expect.equals(6008, (y2 * 100).floor()); |
| 123 Expect.equals(y0, y1); |
| 124 Expect.equals(y0, y2); |
| 125 |
| 126 // Test that identity of materialized objects is preserved correctly and |
| 127 // no copies are materialized. |
| 128 final z1 = testIdentity(c.p); |
| 129 final z2 = testIdentity(new F(c.p)); |
| 130 Expect.equals(z0, z1); |
| 131 Expect.equals(z0, z2); |
| 132 } |
OLD | NEW |