Files
sdk/pkg/compiler/test/codegen/gvn_test.dart
T
Lasse R.H. Nielsen f8086c81ae Collect all test-related files in package:expect.
Collects files from `package:async_helper` and `tests/language`
that are generally useful, so that all test-related helpers are
in `package:expect`.

Moves the two libraries from `package:async_helper` into `package:expect`,
and the `tests/language/static_type_helper.dart` file too.

Deprecates `async_minitest.dart`, to follow `minitest.dart`,
expecting the Flutter use of it to have been fixed to not break
on deprecation (I believe Flutter no longer breaks builds on deprecations at all).

Patch 1 is the actual change.
Patch 2+4+8 is changing all existing references to the files.
Patch 6 ignores deprecation in files still using `async_minitest.dart`.

3+5+7+9 are updating this text to make the numbers match.
Then it's just test-expectations and small tweaks from there.

Change-Id: I1b665135b5fef9b9a0c3b340ffe9daf874d0174c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373120
Reviewed-by: Nate Bosch <nbosch@google.com>
Reviewed-by: Devon Carew <devoncarew@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2024-10-11 16:53:52 +00:00

145 lines
3.5 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:expect/async_helper.dart';
import 'package:expect/expect.dart';
import '../helpers/compiler_helper.dart';
const String TEST_ONE = r"""
void foo(bar) {
for (int i = 0; i < 1; i++) {
print(1 + bar);
print(1 + bar);
}
}
""";
// Check that modulo does not have any side effect and we are
// GVN'ing the length of [:list:].
const String TEST_TWO = r"""
void foo(a) {
var list = <int>[];
list[0] = list[0 % a as int];
list[1] = list[1 % a as int];
}
""";
// Check that is checks get GVN'ed.
const String TEST_THREE = r"""
void foo(a) {
print(42); // Make sure numbers are used.
print(a is num);
print(a is num);
}
""";
// Check that instructions that don't have a builtin equivalent can
// still be GVN'ed.
const String TEST_FOUR = r"""
void foo(a) {
print(1 >> a);
print(1 >> a);
}
""";
// Check that [HCheck] instructions do not prevent GVN.
const String TEST_FIVE = r"""
class A {
final int foo;
A(this.foo);
}
class B {}
main() {
helper([new A(32), A(21), B(), null][0] as A);
}
helper(A a) {
var b = a.foo;
var c = a.foo;
if (a is B) {
c = (a as dynamic).foo;
}
return b + c;
}
""";
// Check that a gvn'able instruction in the loop header gets hoisted.
const String TEST_SIX = r"""
class A {
@pragma('dart2js:noElision')
final field = 54;
}
main() {
dynamic a = A();
while (a.field == 54) { a.field = 42; }
}
""";
// Check that a gvn'able instruction that may throw in the loop header
// gets hoisted.
const String TEST_SEVEN = r"""
class A {
final field;
A() : field = null;
A.bar() : field = 42;
}
main() {
dynamic a = A();
dynamic b = A.bar();
while (a.field == 54) { a.field = 42; b.field = 42; }
}
""";
// Check that a check in a loop header gets hoisted.
const String TEST_EIGHT = r"""
class A {
final field;
A() : field = null;
A.bar() : field = 42;
}
main() {
dynamic a = A();
dynamic b = A.bar();
for (int i = 0; i < a.field!; i++) { a.field = 42; b.field = 42; }
}
""";
main() {
asyncTest(() async {
await compile(TEST_ONE, entry: 'foo', check: (String generated) {
RegExp regexp = RegExp(r"1 \+ [a-z]+");
checkNumberOfMatches(regexp.allMatches(generated).iterator, 1);
});
await compile(TEST_TWO, entry: 'foo', check: (String generated) {
checkNumberOfMatches(RegExp("length").allMatches(generated).iterator, 1);
});
await compile(TEST_THREE, entry: 'foo', check: (String generated) {
checkNumberOfMatches(RegExp("number").allMatches(generated).iterator, 1);
});
await compile(TEST_FOUR, entry: 'foo', check: (String generated) {
checkNumberOfMatches(RegExp("shr").allMatches(generated).iterator, 1);
});
await compileAll(TEST_FIVE).then((generated) {
checkNumberOfMatches(RegExp(r"\.foo;").allMatches(generated).iterator, 1);
checkNumberOfMatches(
RegExp(r"get\$foo\(").allMatches(generated).iterator, 0);
});
await compileAll(TEST_SIX).then((generated) {
Expect.isTrue(generated.contains('for (t1 = a.field === 54; t1;)'));
});
await compileAll(TEST_SEVEN).then((generated) {
Expect.isTrue(generated.contains('for (t1 = a.field === 54; t1;)'));
});
await compileAll(TEST_EIGHT).then((generated) {
Expect.isTrue(generated.contains('for (; i < t1; ++i)'));
});
});
}