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

Unified Diff: lib/compiler/implementation/ssa/optimize.dart

Issue 10825386: Use JavaScript runtime semantics when constant folding. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update status file. Created 8 years, 3 months 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: lib/compiler/implementation/ssa/optimize.dart
diff --git a/lib/compiler/implementation/ssa/optimize.dart b/lib/compiler/implementation/ssa/optimize.dart
index 56c9f66f228068b0c42aac59d554472f23b6806c..22cfb7294e80bc0265e7bedbdea3eaddeb0eb734 100644
--- a/lib/compiler/implementation/ssa/optimize.dart
+++ b/lib/compiler/implementation/ssa/optimize.dart
@@ -27,24 +27,26 @@ class SsaOptimizerTask extends CompilerTask {
}
void optimize(WorkItem work, HGraph graph) {
+ ConstantSystem constantSystem =
+ compiler.constantHandler.constantSystem;
JavaScriptItemCompilationContext context = work.compilationContext;
HTypeMap types = context.types;
measure(() {
List<OptimizationPhase> phases = <OptimizationPhase>[
// Run trivial constant folding first to optimize
// some patterns useful for type conversion.
- new SsaConstantFolder(backend, work, types),
+ new SsaConstantFolder(constantSystem, backend, work, types),
ngeoffray 2012/09/05 11:20:36 If the constantSystem is in the backend, you don't
floitsch 2012/09/05 16:12:01 I prefer being explicit. Otherwise dependencies be
new SsaTypeConversionInserter(compiler),
new SsaTypePropagator(compiler, types),
new SsaCheckInserter(backend, types),
- new SsaConstantFolder(backend, work, types),
+ new SsaConstantFolder(constantSystem, backend, work, types),
new SsaRedundantPhiEliminator(),
new SsaDeadPhiEliminator(),
new SsaGlobalValueNumberer(compiler, types),
new SsaCodeMotion(),
// Previous optimizations may have generated new
// opportunities for constant folding.
- new SsaConstantFolder(backend, work, types),
+ new SsaConstantFolder(constantSystem, backend, work, types),
new SsaDeadCodeEliminator(types),
new SsaRegisterRecompilationCandidates(backend, work, types)];
runPhases(graph, phases);
@@ -106,11 +108,12 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
final String name = "SsaConstantFolder";
final JavaScriptBackend backend;
final WorkItem work;
+ final ConstantSystem constantSystem;
final HTypeMap types;
HGraph graph;
Compiler get compiler => backend.compiler;
- SsaConstantFolder(this.backend, this.work, this.types);
+ SsaConstantFolder(this.constantSystem, this.backend, this.work, this.types);
void visitGraph(HGraph visitee) {
graph = visitee;
@@ -157,7 +160,7 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
// All values !== true are boolified to false.
DartType type = types[input].computeType(compiler);
if (type !== null && type.element !== compiler.boolClass) {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
return node;
}
@@ -169,7 +172,7 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
if (input is HConstant) {
HConstant constant = input;
bool isTrue = constant.constant.isTrue();
- return graph.addConstantBool(!isTrue);
+ return graph.addConstant(constantSystem.createBool(!isTrue));
} else if (input is HNot) {
return input.inputs[0];
}
@@ -179,7 +182,7 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
HInstruction visitInvokeUnary(HInvokeUnary node) {
HInstruction operand = node.operand;
if (operand is HConstant) {
- UnaryOperation operation = node.operation;
+ UnaryOperation operation = node.operation(constantSystem);
HConstant receiver = operand;
Constant folded = operation.fold(receiver.constant);
if (folded !== null) return graph.addConstant(folded);
@@ -193,15 +196,15 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
if (input.isConstantString()) {
HConstant constantInput = input;
StringConstant constant = constantInput.constant;
- return graph.addConstantInt(constant.length);
+ return graph.addConstant(constantSystem.createInt(constant.length));
} else if (input.isConstantList()) {
HConstant constantInput = input;
ListConstant constant = constantInput.constant;
- return graph.addConstantInt(constant.length);
+ return graph.addConstant(constantSystem.createInt(constant.length));
} else if (input.isConstantMap()) {
HConstant constantInput = input;
MapConstant constant = constantInput.constant;
- return graph.addConstantInt(constant.length);
+ return graph.addConstant(constantSystem.createInt(constant.length));
}
}
@@ -303,11 +306,12 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
HInstruction value = node.value;
if (value.isInteger(types)) return value;
if (value.isConstant()) {
- assert((){
- HConstant constantInstruction = value;
- return !constantInstruction.constant.isInt();
- });
- node.alwaysFalse = true;
+ HConstant constantInstruction = value;
+ assert(!constantInstruction.constant.isInt());
+ if (!constantSystem.isInt(constantInstruction.constant)) {
+ // -0.0 is a double but will pass the runtime integer check.
+ node.alwaysFalse = true;
+ }
}
return node;
}
@@ -332,8 +336,8 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
HInstruction visitInvokeBinary(HInvokeBinary node) {
HInstruction left = node.left;
HInstruction right = node.right;
+ BinaryOperation operation = node.operation(constantSystem);
if (left is HConstant && right is HConstant) {
- BinaryOperation operation = node.operation;
HConstant op1 = left;
HConstant op2 = right;
Constant folded = operation.fold(op1.constant, op2.constant);
@@ -341,10 +345,10 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
}
if (!left.canBePrimitive(types)
- && node.operation.isUserDefinable()
+ && operation.isUserDefinable()
// The equals operation is being optimized in visitEquals.
- && node.operation !== const EqualsOperation()) {
- Selector selector = new Selector.binaryOperator(node.operation.name);
+ && node is! HEquals) {
+ Selector selector = new Selector.binaryOperator(operation.name);
return fromInterceptorToDynamicInvocation(node, selector);
}
return node;
@@ -397,7 +401,7 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
// We don't optimize on numbers to preserve the runtime semantics.
if (!(left.isNumber(types) && right.isNumber(types)) &&
leftType.intersection(rightType).isConflicting()) {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
if (left.isConstantBoolean() && right.isBoolean(types)) {
@@ -461,7 +465,7 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
// operator.
return super.visitEquals(node);
} else if (right.isConstantNull()) {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
} else {
// We can just emit an identity check because the type does
// not implement operator=.
@@ -471,7 +475,7 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
if (right.isConstantNull()) {
if (leftType.isPrimitive()) {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
}
@@ -500,47 +504,47 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
HType expressionType = types[node.expression];
if (element === compiler.objectClass
|| element === compiler.dynamicClass) {
- return graph.addConstantBool(true);
+ return graph.addConstant(constantSystem.createBool(true));
} else if (expressionType.isInteger()) {
if (element === compiler.intClass || element === compiler.numClass) {
- return graph.addConstantBool(true);
+ return graph.addConstant(constantSystem.createBool(true));
} else if (element === compiler.doubleClass) {
// We let the JS semantics decide for that check. Currently
// the code we emit will always return true.
return node;
} else {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
} else if (expressionType.isDouble()) {
if (element === compiler.doubleClass || element === compiler.numClass) {
- return graph.addConstantBool(true);
+ return graph.addConstant(constantSystem.createBool(true));
} else if (element === compiler.intClass) {
// We let the JS semantics decide for that check. Currently
// the code we emit will return true for a double that can be
- // represented as a 31-bit integer.
+ // represented as a 31-bit integer and for -0.0.
return node;
} else {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
} else if (expressionType.isNumber()) {
if (element === compiler.numClass) {
- return graph.addConstantBool(true);
+ return graph.addConstant(constantSystem.createBool(true));
}
// We cannot just return false, because the expression may be of
// type int or double.
} else if (expressionType.isString()) {
if (element === compiler.stringClass
|| Elements.isStringSupertype(element, compiler)) {
- return graph.addConstantBool(true);
+ return graph.addConstant(constantSystem.createBool(true));
} else {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
} else if (expressionType.isArray()) {
if (element === compiler.listClass
|| Elements.isListSupertype(element, compiler)) {
- return graph.addConstantBool(true);
+ return graph.addConstant(constantSystem.createBool(true));
} else {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
// TODO(karlklose): remove the hasTypeArguments check.
} else if (expressionType.isUseful()
@@ -549,9 +553,9 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
DartType receiverType = expressionType.computeType(compiler);
if (receiverType !== null) {
if (compiler.types.isSubtype(receiverType, type)) {
- return graph.addConstantBool(true);
+ return graph.addConstant(constantSystem.createBool(true));
} else if (expressionType.isExact()) {
- return graph.addConstantBool(false);
+ return graph.addConstant(constantSystem.createBool(false));
}
}
}
@@ -631,7 +635,7 @@ class SsaConstantFolder extends HBaseVisitor implements OptimizationPhase {
PrimitiveConstant primitive = constant.constant;
folded = new DartString.concat(folded, primitive.toDartString());
}
- return graph.addConstantString(folded, node.node);
+ return graph.addConstant(constantSystem.createString(folded, node.node));
}
}

Powered by Google App Engine
This is Rietveld 408576698