KT-3002 optimized ==/!=0

Optimize byte-code generation for integer comparisons != and ==.
Work done in Hackergarten Vienna.
Co-authored-by: Kilian Matt <kilian.matt@gmail.com>
This commit is contained in:
Rafael Cordones
2012-10-29 22:06:42 +01:00
committed by Alexander Udalov
parent d28c9e0eef
commit cb99f26807
2 changed files with 45 additions and 0 deletions
@@ -2362,6 +2362,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return genCmpWithNull(left, leftType, opToken);
}
if (isIntZero(left, leftType)) {
return genCmpWithZero(right, rightType, opToken);
}
if (isIntZero(right, rightType)) {
return genCmpWithZero(left, leftType, opToken);
}
if (isPrimitive(leftType) != isPrimitive(rightType)) {
gen(left, leftType);
StackValue.valueOf(v, leftType);
@@ -2384,6 +2392,27 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
genEqualsForExpressionsOnStack(v, opToken, leftType, rightType, leftJetType.isNullable(), rightJetType.isNullable());
}
private boolean isIntZero(JetExpression expr, Type exprType) {
CompileTimeConstant<?> exprValue = bindingContext.get(BindingContext.COMPILE_TIME_VALUE, expr);
return isIntPrimitive(exprType) && exprValue != null && exprValue.getValue().equals(0);
}
private StackValue genCmpWithZero(JetExpression exp, Type expType, IElementType opToken) {
v.iconst(1);
gen(exp, expType);
Label ok = new Label();
if (JetTokens.EQEQ == opToken || JetTokens.EQEQEQ == opToken) {
v.ifeq(ok);
}
else {
v.ifne(ok);
}
v.pop();
v.iconst(0);
v.mark(ok);
return StackValue.onStack(Type.BOOLEAN_TYPE);
}
private StackValue genCmpWithNull(JetExpression exp, Type expType, IElementType opToken) {
v.iconst(1);
gen(exp, boxType(expType));