Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(541)

Unified Diff: pkg/compiler/lib/src/cps_ir/type_propagation.dart

Issue 1507313006: dart2js cps: Add instruction for null checks. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Remove spurious change Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/cps_ir/type_propagation.dart
diff --git a/pkg/compiler/lib/src/cps_ir/type_propagation.dart b/pkg/compiler/lib/src/cps_ir/type_propagation.dart
index f716f05e023f7479bd72b9f731accc4b9f2a9dcb..3ded6ed5eb104f7d5ab2aca9e0774f1f67996bad 100644
--- a/pkg/compiler/lib/src/cps_ir/type_propagation.dart
+++ b/pkg/compiler/lib/src/cps_ir/type_propagation.dart
@@ -683,6 +683,12 @@ class ConstantPropagationLattice {
if (constantValue != null) return constant(constantValue, mask);
return nonConstant(mask);
}
+
+ AbstractConstantValue nonNullable(AbstractConstantValue value) {
+ if (value.isNullConstant) return nothing;
+ if (!value.isNullable) return value;
+ return nonConstant(value.type.nonNullable());
+ }
}
/**
@@ -1088,19 +1094,19 @@ class TransformingVisitor extends DeepRecursiveVisitor {
return node.dartReceiver;
}
if (!trustPrimitives) {
+ // TODO(asgerf): Perhaps a separate optimization should decide that
+ // the guarded check is better based on the type?
sra1 2015/12/11 03:34:06 We should also consider generating something like
asgerf 2015/12/11 10:15:40 I see. NullCheck would certainly not be an approp
Primitive check = cps.applyBuiltin(guard, [node.dartReceiver]);
- cps.ifFalsy(check)
- ..invokeMethod(node.dartReceiver, node.selector,
- typeSystem.nullType, [])
- ..put(new Unreachable());
+ return cps.letPrim(new NullCheck.guarded(check, node.dartReceiver,
+ node.selector, node.sourceInformation));
+ } else {
+ // Refine the receiver to be non-null for use in the operator.
+ // This restricts code motion and improves the type computed for the
+ // built-in operator that depends on it.
+ // This must be done even if trusting primitives.
+ return cps.letPrim(
+ new Refinement(node.dartReceiver, typeSystem.nonNullType));
}
- // Refine the receiver to be non-null for use in the operator.
- // This restricts code motion and improves the type computed for the
- // built-in operator that depends on it.
- // This must be done even if trusting primitives.
- Primitive refined = cps.letPrim(
- new Refinement(node.dartReceiver, typeSystem.nonNullType));
- return refined;
}
/// Replaces the call with [operator], using the receiver and first argument
@@ -1164,7 +1170,7 @@ class TransformingVisitor extends DeepRecursiveVisitor {
// Try to insert a numeric operator.
BuiltinOperator operator = NumBinaryBuiltins[opname];
if (operator != null) {
- return makeBinary(operator, guard: BuiltinOperator.IsNumber);
+ return makeBinary(operator, guard: BuiltinOperator.IsNotNumber);
}
// Shift operators are not in [NumBinaryBuiltins] because Dart shifts
// behave different to JS shifts, especially in the handling of the
@@ -1174,7 +1180,7 @@ class TransformingVisitor extends DeepRecursiveVisitor {
lattice.isDefinitelyInt(left, allowNull: true) &&
lattice.isDefinitelyIntInRange(right, min: 0, max: 31)) {
return makeBinary(BuiltinOperator.NumShl,
- guard: BuiltinOperator.IsNumber);
+ guard: BuiltinOperator.IsNotNumber);
}
// Try to insert a shift-right operator. JavaScript's right shift is
// consistent with Dart's only for left operands in the unsigned
@@ -1183,7 +1189,7 @@ class TransformingVisitor extends DeepRecursiveVisitor {
lattice.isDefinitelyUint32(left, allowNull: true) &&
lattice.isDefinitelyIntInRange(right, min: 0, max: 31)) {
return makeBinary(BuiltinOperator.NumShr,
- guard: BuiltinOperator.IsNumber);
+ guard: BuiltinOperator.IsNotNumber);
}
// Try to use remainder for '%'. Both operands must be non-negative
// and the divisor must be non-zero.
@@ -1192,14 +1198,14 @@ class TransformingVisitor extends DeepRecursiveVisitor {
lattice.isDefinitelyUint(right) &&
lattice.isDefinitelyIntInRange(right, min: 1)) {
return makeBinary(BuiltinOperator.NumRemainder,
- guard: BuiltinOperator.IsNumber);
+ guard: BuiltinOperator.IsNotNumber);
}
if (opname == '~/' &&
lattice.isDefinitelyUint32(left, allowNull: true) &&
lattice.isDefinitelyIntInRange(right, min: 2)) {
return makeBinary(BuiltinOperator.NumTruncatingDivideToSigned32,
- guard: BuiltinOperator.IsNumber);
+ guard: BuiltinOperator.IsNotNumber);
}
}
if (lattice.isDefinitelyString(left, allowNull: trustPrimitives) &&
@@ -1218,11 +1224,11 @@ class TransformingVisitor extends DeepRecursiveVisitor {
String opname = node.selector.name;
if (opname == '~') {
return makeUnary(BuiltinOperator.NumBitNot,
- guard: BuiltinOperator.IsNumber);
+ guard: BuiltinOperator.IsNotNumber);
}
if (opname == 'unary-') {
return makeUnary(BuiltinOperator.NumNegate,
- guard: BuiltinOperator.IsNumber);
+ guard: BuiltinOperator.IsNotNumber);
}
}
}
@@ -1238,7 +1244,7 @@ class TransformingVisitor extends DeepRecursiveVisitor {
lattice.isDefinitelyInt(argValue) &&
isIntNotZero(argValue)) {
return makeBinary(BuiltinOperator.NumRemainder,
- guard: BuiltinOperator.IsNumber);
+ guard: BuiltinOperator.IsNotNumber);
}
}
} else if (name == 'codeUnitAt') {
@@ -1322,7 +1328,7 @@ class TransformingVisitor extends DeepRecursiveVisitor {
<Primitive>[index, cps.letPrim(new GetLength(list))]);
cps.ifTruthy(isTooLarge).invokeContinuation(fail);
cps.insideContinuation(fail).invokeStaticThrower(
- helpers.throwIndexOutOfBoundsError,
+ helpers.throwIndexOutOfRangeException,
<Primitive>[list, index]);
return cps;
}
@@ -1437,7 +1443,7 @@ class TransformingVisitor extends DeepRecursiveVisitor {
[length, cps.makeZero()]);
CpsFragment fail = cps.ifTruthy(isEmpty);
fail.invokeStaticThrower(
- helpers.throwIndexOutOfBoundsError,
+ helpers.throwIndexOutOfRangeException,
[list, fail.makeConstant(new IntConstantValue(-1))]);
Primitive removedItem = cps.invokeBuiltin(BuiltinMethod.Pop,
list,
@@ -2397,6 +2403,14 @@ class TransformingVisitor extends DeepRecursiveVisitor {
}
return true;
}
+
+ visitNullCheck(NullCheck node) {
+ if (!getValue(node.value.definition).isNullable) {
+ node.replaceUsesWith(node.value.definition);
+ return new CpsFragment();
+ }
+ return null;
+ }
}
/**
@@ -3184,6 +3198,11 @@ class TypePropagationVisitor implements Visitor {
nonConstant(value.type.intersection(node.refineType, classWorld)));
}
}
+
+ @override
+ void visitNullCheck(NullCheck node) {
+ setValue(node, lattice.nonNullable(getValue(node.value.definition)));
+ }
}
/// Represents the abstract value of a primitive value at some point in the

Powered by Google App Engine
This is Rietveld 408576698