提交 b96b2dcd 编写于 作者: V vromero

8016267: javac, TypeTag refactoring has provoked performance issues

Reviewed-by: jjg
上级 8c3358a3
...@@ -110,49 +110,27 @@ public class Type implements PrimitiveType { ...@@ -110,49 +110,27 @@ public class Type implements PrimitiveType {
} }
public boolean isNumeric() { public boolean isNumeric() {
switch (tag) { return tag.isNumeric;
case BYTE: case CHAR:
case SHORT:
case INT: case LONG:
case FLOAT: case DOUBLE:
return true;
default:
return false;
}
} }
public boolean isPrimitive() { public boolean isPrimitive() {
return (isNumeric() || tag == BOOLEAN); return tag.isPrimitive;
} }
public boolean isPrimitiveOrVoid() { public boolean isPrimitiveOrVoid() {
return (isPrimitive() || tag == VOID); return tag.isPrimitiveOrVoid;
} }
public boolean isReference() { public boolean isReference() {
switch (tag) { return tag.isReference;
case CLASS:
case ARRAY:
case TYPEVAR:
case WILDCARD:
case ERROR:
return true;
default:
return false;
}
} }
public boolean isNullOrReference() { public boolean isNullOrReference() {
return (tag == BOT || isReference()); return (tag.isReference || tag == BOT);
} }
public boolean isPartial() { public boolean isPartial() {
switch(tag) { return tag.isPartial;
case ERROR: case UNKNOWN: case UNDETVAR:
return true;
default:
return false;
}
} }
/** /**
......
/* /*
* Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -29,6 +29,8 @@ import com.sun.source.tree.Tree.Kind; ...@@ -29,6 +29,8 @@ import com.sun.source.tree.Tree.Kind;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import static com.sun.tools.javac.code.TypeTag.NumericClasses.*;
/** An interface for type tag values, which distinguish between different /** An interface for type tag values, which distinguish between different
* sorts of types. * sorts of types.
* *
...@@ -40,113 +42,170 @@ import javax.lang.model.type.TypeKind; ...@@ -40,113 +42,170 @@ import javax.lang.model.type.TypeKind;
public enum TypeTag { public enum TypeTag {
/** The tag of the basic type `byte'. /** The tag of the basic type `byte'.
*/ */
BYTE(1), BYTE(BYTE_CLASS, BYTE_SUPERCLASSES,
TypeTagKind.PRIMITIVE | TypeTagKind.NUMERIC),
/** The tag of the basic type `char'. /** The tag of the basic type `char'.
*/ */
CHAR(2), CHAR(CHAR_CLASS, CHAR_SUPERCLASSES,
TypeTagKind.PRIMITIVE | TypeTagKind.NUMERIC),
/** The tag of the basic type `short'. /** The tag of the basic type `short'.
*/ */
SHORT(3), SHORT(SHORT_CLASS, SHORT_SUPERCLASSES,
TypeTagKind.PRIMITIVE | TypeTagKind.NUMERIC),
/** The tag of the basic type `int'. /** The tag of the basic type `int'.
*/ */
INT(4), INT(INT_CLASS, INT_SUPERCLASSES,
TypeTagKind.PRIMITIVE | TypeTagKind.NUMERIC),
/** The tag of the basic type `long'. /** The tag of the basic type `long'.
*/ */
LONG(5), LONG(LONG_CLASS, LONG_SUPERCLASSES, TypeTagKind.PRIMITIVE | TypeTagKind.NUMERIC),
/** The tag of the basic type `float'. /** The tag of the basic type `float'.
*/ */
FLOAT(6), FLOAT(FLOAT_CLASS, FLOAT_SUPERCLASSES, TypeTagKind.PRIMITIVE | TypeTagKind.NUMERIC),
/** The tag of the basic type `double'. /** The tag of the basic type `double'.
*/ */
DOUBLE(7), DOUBLE(DOUBLE_CLASS, DOUBLE_CLASS, TypeTagKind.PRIMITIVE | TypeTagKind.NUMERIC),
/** The tag of the basic type `boolean'. /** The tag of the basic type `boolean'.
*/ */
BOOLEAN, BOOLEAN(TypeTagKind.PRIMITIVE),
/** The tag of the type `void'. /** The tag of the type `void'.
*/ */
VOID, VOID(TypeTagKind.VOID),
/** The tag of all class and interface types. /** The tag of all class and interface types.
*/ */
CLASS, CLASS(TypeTagKind.REFERENCE),
/** The tag of all array types. /** The tag of all array types.
*/ */
ARRAY, ARRAY(TypeTagKind.REFERENCE),
/** The tag of all (monomorphic) method types. /** The tag of all (monomorphic) method types.
*/ */
METHOD, METHOD(TypeTagKind.OTHER),
/** The tag of all package "types". /** The tag of all package "types".
*/ */
PACKAGE, PACKAGE(TypeTagKind.OTHER),
/** The tag of all (source-level) type variables. /** The tag of all (source-level) type variables.
*/ */
TYPEVAR, TYPEVAR(TypeTagKind.REFERENCE),
/** The tag of all type arguments. /** The tag of all type arguments.
*/ */
WILDCARD, WILDCARD(TypeTagKind.REFERENCE),
/** The tag of all polymorphic (method-) types. /** The tag of all polymorphic (method-) types.
*/ */
FORALL, FORALL(TypeTagKind.OTHER),
/** The tag of deferred expression types in method context /** The tag of deferred expression types in method context
*/ */
DEFERRED, DEFERRED(TypeTagKind.OTHER),
/** The tag of the bottom type {@code <null>}. /** The tag of the bottom type {@code <null>}.
*/ */
BOT, BOT(TypeTagKind.OTHER),
/** The tag of a missing type. /** The tag of a missing type.
*/ */
NONE, NONE(TypeTagKind.OTHER),
/** The tag of the error type. /** The tag of the error type.
*/ */
ERROR, ERROR(TypeTagKind.REFERENCE | TypeTagKind.PARTIAL),
/** The tag of an unknown type /** The tag of an unknown type
*/ */
UNKNOWN, UNKNOWN(TypeTagKind.PARTIAL),
/** The tag of all instantiatable type variables. /** The tag of all instantiatable type variables.
*/ */
UNDETVAR, UNDETVAR(TypeTagKind.PARTIAL),
/** Pseudo-types, these are special tags /** Pseudo-types, these are special tags
*/ */
UNINITIALIZED_THIS, UNINITIALIZED_THIS(TypeTagKind.OTHER),
UNINITIALIZED_OBJECT; UNINITIALIZED_OBJECT(TypeTagKind.OTHER);
/** This field will only be used for tags related with numeric types for final boolean isPrimitive;
* optimization reasons. final boolean isNumeric;
*/ final boolean isPartial;
private final int order; final boolean isReference;
final boolean isPrimitiveOrVoid;
final int superClasses;
final int numericClass;
private TypeTag(int kind) {
this(0, 0, kind);
}
private TypeTag(int numericClass, int superClasses, int kind) {
isPrimitive = (kind & TypeTagKind.PRIMITIVE) != 0;
isNumeric = (kind & TypeTagKind.NUMERIC) != 0;
isPartial = (kind & TypeTagKind.PARTIAL) != 0;
isReference = (kind & TypeTagKind.REFERENCE) != 0;
isPrimitiveOrVoid = ((kind & TypeTagKind.PRIMITIVE) != 0) ||
((kind & TypeTagKind.VOID) != 0);
this.superClasses = superClasses;
this.numericClass = numericClass;
}
static class TypeTagKind {
static final int PRIMITIVE = 1;
static final int NUMERIC = 2;
static final int REFERENCE = 4;
static final int PARTIAL = 8;
static final int OTHER = 16;
static final int VOID = 32;
}
public static class NumericClasses {
public static final int BYTE_CLASS = 1;
public static final int CHAR_CLASS = 2;
public static final int SHORT_CLASS = 4;
public static final int INT_CLASS = 8;
public static final int LONG_CLASS = 16;
public static final int FLOAT_CLASS = 32;
public static final int DOUBLE_CLASS = 64;
static final int BYTE_SUPERCLASSES = BYTE_CLASS | SHORT_CLASS | INT_CLASS |
LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
static final int CHAR_SUPERCLASSES = CHAR_CLASS | INT_CLASS |
LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
private TypeTag() { static final int SHORT_SUPERCLASSES = SHORT_CLASS | INT_CLASS |
this(0); LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
static final int INT_SUPERCLASSES = INT_CLASS | LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
static final int LONG_SUPERCLASSES = LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
static final int FLOAT_SUPERCLASSES = FLOAT_CLASS | DOUBLE_CLASS;
} }
private TypeTag(int order) { public boolean isStrictSubRangeOf(TypeTag tag) {
this.order = order; /* Please don't change the implementation of this method to call method
* isSubRangeOf. Both methods are called from hotspot code, the current
* implementation is better performance-wise than the commented modification.
*/
return (this.superClasses & tag.numericClass) != 0 && this != tag;
} }
private static final int MIN_NUMERIC_TAG_ORDER = 1; public boolean isSubRangeOf(TypeTag tag) {
private static final int MAX_NUMERIC_TAG_ORDER = 7; return (this.superClasses & tag.numericClass) != 0;
}
/** Returns the number of type tags. /** Returns the number of type tags.
*/ */
...@@ -155,29 +214,6 @@ public enum TypeTag { ...@@ -155,29 +214,6 @@ public enum TypeTag {
return (UNDETVAR.ordinal() + 1); return (UNDETVAR.ordinal() + 1);
} }
public boolean isSubRangeOf(TypeTag range) {
return (this == range) || isStrictSubRangeOf(range);
}
public boolean isStrictSubRangeOf(TypeTag range) {
if (this.order >= MIN_NUMERIC_TAG_ORDER && this.order <= MAX_NUMERIC_TAG_ORDER &&
range.order >= MIN_NUMERIC_TAG_ORDER && this.order <= MAX_NUMERIC_TAG_ORDER) {
if (this == range)
return false;
switch (this) {
case BYTE:
return true;
case CHAR: case SHORT: case INT:
case LONG: case FLOAT:
return this.order < range.order && range.order <= MAX_NUMERIC_TAG_ORDER;
default:
return false;
}
}
else
return false;
}
public Kind getKindLiteral() { public Kind getKindLiteral() {
switch (this) { switch (this) {
case INT: case INT:
......
...@@ -1532,21 +1532,23 @@ public class Attr extends JCTree.Visitor { ...@@ -1532,21 +1532,23 @@ public class Attr extends JCTree.Visitor {
// If one arm has an integer subrange type (i.e., byte, // If one arm has an integer subrange type (i.e., byte,
// short, or char), and the other is an integer constant // short, or char), and the other is an integer constant
// that fits into the subrange, return the subrange type. // that fits into the subrange, return the subrange type.
if (thenUnboxed.getTag().isStrictSubRangeOf(INT) && elseUnboxed.hasTag(INT) && if (thenUnboxed.getTag().isStrictSubRangeOf(INT) &&
types.isAssignable(elseUnboxed, thenUnboxed)) elseUnboxed.hasTag(INT) &&
types.isAssignable(elseUnboxed, thenUnboxed)) {
return thenUnboxed.baseType(); return thenUnboxed.baseType();
if (elseUnboxed.getTag().isStrictSubRangeOf(INT) && thenUnboxed.hasTag(INT) && }
types.isAssignable(thenUnboxed, elseUnboxed)) if (elseUnboxed.getTag().isStrictSubRangeOf(INT) &&
thenUnboxed.hasTag(INT) &&
types.isAssignable(thenUnboxed, elseUnboxed)) {
return elseUnboxed.baseType(); return elseUnboxed.baseType();
}
for (TypeTag tag : TypeTag.values()) { for (TypeTag tag : primitiveTags) {
if (tag.ordinal() >= TypeTag.getTypeTagCount()) break;
Type candidate = syms.typeOfTag[tag.ordinal()]; Type candidate = syms.typeOfTag[tag.ordinal()];
if (candidate != null && if (types.isSubtype(thenUnboxed, candidate) &&
candidate.isPrimitive() && types.isSubtype(elseUnboxed, candidate)) {
types.isSubtype(thenUnboxed, candidate) &&
types.isSubtype(elseUnboxed, candidate))
return candidate; return candidate;
}
} }
} }
...@@ -1575,6 +1577,17 @@ public class Attr extends JCTree.Visitor { ...@@ -1575,6 +1577,17 @@ public class Attr extends JCTree.Visitor {
return types.lub(thentype.baseType(), elsetype.baseType()); return types.lub(thentype.baseType(), elsetype.baseType());
} }
final static TypeTag[] primitiveTags = new TypeTag[]{
BYTE,
CHAR,
SHORT,
INT,
LONG,
FLOAT,
DOUBLE,
BOOLEAN,
};
public void visitIf(JCIf tree) { public void visitIf(JCIf tree) {
attribExpr(tree.cond, env, syms.booleanType); attribExpr(tree.cond, env, syms.booleanType);
attribStat(tree.thenpart, env); attribStat(tree.thenpart, env);
......
...@@ -544,7 +544,7 @@ public class Check { ...@@ -544,7 +544,7 @@ public class Check {
if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) { if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) {
return found; return found;
} else { } else {
if (found.getTag().isSubRangeOf(DOUBLE) && req.getTag().isSubRangeOf(DOUBLE)) { if (found.isNumeric() && req.isNumeric()) {
checkContext.report(pos, diags.fragment("possible.loss.of.precision", found, req)); checkContext.report(pos, diags.fragment("possible.loss.of.precision", found, req));
return types.createErrorType(found); return types.createErrorType(found);
} }
...@@ -754,7 +754,7 @@ public class Check { ...@@ -754,7 +754,7 @@ public class Check {
* @param t The type to be checked. * @param t The type to be checked.
*/ */
Type checkNullOrRefType(DiagnosticPosition pos, Type t) { Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
if (t.isNullOrReference()) if (t.isReference() || t.hasTag(BOT))
return t; return t;
else else
return typeTagError(pos, return typeTagError(pos,
...@@ -3228,7 +3228,7 @@ public class Check { ...@@ -3228,7 +3228,7 @@ public class Check {
void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) { void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) {
if (operand.constValue() != null if (operand.constValue() != null
&& lint.isEnabled(LintCategory.DIVZERO) && lint.isEnabled(LintCategory.DIVZERO)
&& (operand.getTag().isSubRangeOf(LONG)) && operand.getTag().isSubRangeOf(LONG)
&& ((Number) (operand.constValue())).longValue() == 0) { && ((Number) (operand.constValue())).longValue() == 0) {
int opc = ((OperatorSymbol)operator).opcode; int opc = ((OperatorSymbol)operator).opcode;
if (opc == ByteCodes.idiv || opc == ByteCodes.imod if (opc == ByteCodes.idiv || opc == ByteCodes.imod
......
...@@ -952,8 +952,9 @@ public class Infer { ...@@ -952,8 +952,9 @@ public class Infer {
Type solve(UndetVar uv, InferenceContext inferenceContext) { Type solve(UndetVar uv, InferenceContext inferenceContext) {
Infer infer = inferenceContext.infer(); Infer infer = inferenceContext.infer();
List<Type> lobounds = filterBounds(uv, inferenceContext); List<Type> lobounds = filterBounds(uv, inferenceContext);
Type owntype = infer.types.lub(lobounds); //note: lobounds should have at least one element
if (owntype.hasTag(ERROR)) { Type owntype = lobounds.tail.tail == null ? lobounds.head : infer.types.lub(lobounds);
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
throw infer.inferenceException throw infer.inferenceException
.setMessage("no.unique.minimal.instance.exists", .setMessage("no.unique.minimal.instance.exists",
uv.qtype, lobounds); uv.qtype, lobounds);
...@@ -971,8 +972,9 @@ public class Infer { ...@@ -971,8 +972,9 @@ public class Infer {
Type solve(UndetVar uv, InferenceContext inferenceContext) { Type solve(UndetVar uv, InferenceContext inferenceContext) {
Infer infer = inferenceContext.infer(); Infer infer = inferenceContext.infer();
List<Type> hibounds = filterBounds(uv, inferenceContext); List<Type> hibounds = filterBounds(uv, inferenceContext);
Type owntype = infer.types.glb(hibounds); //note: lobounds should have at least one element
if (owntype.isErroneous()) { Type owntype = hibounds.tail.tail == null ? hibounds.head : infer.types.glb(hibounds);
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
throw infer.inferenceException throw infer.inferenceException
.setMessage("no.unique.maximal.instance.exists", .setMessage("no.unique.maximal.instance.exists",
uv.qtype, hibounds); uv.qtype, hibounds);
...@@ -1100,10 +1102,11 @@ public class Infer { ...@@ -1100,10 +1102,11 @@ public class Infer {
} }
} }
//no progress //no progress
throw inferenceException; throw inferenceException.setMessage();
} }
} }
catch (InferenceException ex) { catch (InferenceException ex) {
//did we fail because of interdependent ivars?
inferenceContext.rollback(); inferenceContext.rollback();
instantiateAsUninferredVars(varsToSolve, inferenceContext); instantiateAsUninferredVars(varsToSolve, inferenceContext);
checkWithinBounds(inferenceContext, warn); checkWithinBounds(inferenceContext, warn);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册