Chromium Code Reviews| Index: lib/compiler/implementation/ssa/codegen.dart |
| diff --git a/lib/compiler/implementation/ssa/codegen.dart b/lib/compiler/implementation/ssa/codegen.dart |
| index a7ff2777c7b95b47f546c0068e4b92f052e21dcb..2a5740e0f5c53b5ceb48e51e09232a22f1ea39b3 100644 |
| --- a/lib/compiler/implementation/ssa/codegen.dart |
| +++ b/lib/compiler/implementation/ssa/codegen.dart |
| @@ -486,11 +486,70 @@ class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor { |
| declareVariable(variableNames.getName(instruction)); |
| } |
| + // For simple updates of the form 'i = i op constant' generate |
| + // 'i op= constant' instead. |
| + bool handleSimpleUpdateDefinition(HInstruction instruction, String name) { |
| + // If the variable is not declared the short update syntax cannot |
| + // be used since it is a declaration and not an update. |
| + if (!isVariableDeclared(name)) return false; |
|
ngeoffray
2012/06/04 13:24:00
I think you should check that it's not delayed eit
Mads Ager (google)
2012/06/04 15:27:18
Yes, went for marking a variable declared when we
|
| + |
| + // Extract the operation and whether or not it is commutative. |
|
kasperl
2012/06/04 13:12:56
I guess you considered moving some of this code to
Mads Ager (google)
2012/06/04 13:21:45
Yes, I decided to keep it local since I only consi
|
| + var operation; |
|
ngeoffray
2012/06/04 13:24:00
Please use the Operation class in lib/compiler/imp
Mads Ager (google)
2012/06/04 15:27:18
In principle that would be very nice. In practice
|
| + var isCommutative = instruction is HAdd || instruction is HMultiply; |
|
kasperl
2012/06/04 13:12:56
Make this start out by being false and push the up
Mads Ager (google)
2012/06/04 13:21:45
Yes! Done.
|
| + if (instruction is HAdd) { |
| + operation = '+'; |
| + } else if (instruction is HMultiply) { |
| + operation = '*'; |
| + } else if (instruction is HSubtract) { |
| + operation = '-'; |
| + } else if (instruction is HDivide) { |
| + operation = '/'; |
| + } else { |
| + return false; |
| + } |
| + |
| + // Is it a simple builtin operation involving constant numbers? |
| + if (instruction.builtin && instruction.inputs.length == 3) { |
| + var left = instruction.inputs[1]; |
| + var right = instruction.inputs[2]; |
| + if (left.isConstantNumber() && isCommutative) { |
| + var tmp = right; |
| + right = left; |
| + left = tmp; |
| + } else if (!right.isConstantNumber()) { |
| + return false; |
| + } |
| + // Right is constant number. |
| + var value = right.constant.value; |
| + // Check that left is a phi with the same name as the definition |
| + // and emit the short update definition if it is. |
| + if (left is HPhi && |
|
ngeoffray
2012/06/04 13:24:00
I think you can remove the check. You only need to
Mads Ager (google)
2012/06/04 15:27:18
Great, thanks!
|
| + variableNames.hasName(left) && |
| + variableNames.getName(left) == name) { |
| + if ((operation == '+') && right.constant.value == 1) { |
|
kasperl
2012/06/04 13:12:56
Looks a bit fishy that you have () around one == (
Mads Ager (google)
2012/06/04 13:21:45
Yes, that looks weird. Updated to consistently use
|
| + buffer.add('++'); |
| + declareInstruction(instruction); |
| + } else if ((operation == '-') && right.constant.value == 1) { |
|
kasperl
2012/06/04 13:12:56
Ditto.
Mads Ager (google)
2012/06/04 13:21:45
Thanks. Done.
|
| + buffer.add('--'); |
| + declareInstruction(instruction); |
| + } else { |
| + declareInstruction(instruction); |
| + buffer.add(' ${operation}= ${value}'); |
| + } |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| void define(HInstruction instruction) { |
| if (instruction is !HCheck && variableNames.hasName(instruction)) { |
| - declareInstruction(instruction); |
| - buffer.add(" = "); |
| - visit(instruction, JSPrecedence.ASSIGNMENT_PRECEDENCE); |
| + if (!handleSimpleUpdateDefinition(instruction, |
| + variableNames.getName(instruction))) { |
|
kasperl
2012/06/04 13:12:56
I would throw variableNames.getName(instruction) i
Mads Ager (google)
2012/06/04 13:21:45
Done.
|
| + declareInstruction(instruction); |
| + buffer.add(" = "); |
| + visit(instruction, JSPrecedence.ASSIGNMENT_PRECEDENCE); |
| + } |
| } else { |
| visit(instruction, JSPrecedence.STATEMENT_PRECEDENCE); |
| } |
| @@ -894,7 +953,7 @@ class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor { |
| } |
| void emitAssignment(String destination, String source) { |
| - if (isGeneratingExpression()) { |
| + if (isGeneratingExpression()) { |
| addExpressionSeparator(); |
| } else { |
| addIndentation(); |