Skip to content

Remove constant folding in Painless user tree #52783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public static Class<?> promoteEquality(Class<?> from0, Class<?> from1) {
return Object.class;
}

public static Class<?> promoteConditional(Class<?> from0, Class<?> from1, Object const0, Object const1) {
public static Class<?> promoteConditional(Class<?> from0, Class<?> from1) {
if (from0 == from1) {
return from0;
}
Expand All @@ -529,123 +529,29 @@ public static Class<?> promoteConditional(Class<?> from0, Class<?> from1, Object
return def.class;
}

if (from0.isPrimitive() && from1.isPrimitive()) {
if (from0 == boolean.class && from1 == boolean.class) {
return boolean.class;
}

if (from0.isPrimitive() && from0 != boolean.class && from1.isPrimitive() && from1 != boolean.class) {
if (from0 == double.class || from1 == double.class) {
return double.class;
} else if (from0 == float.class || from1 == float.class) {
return float.class;
} else if (from0 == long.class || from1 == long.class) {
return long.class;
} else if (from0 == int.class || from1 == int.class) {
return int.class;
} else if (from0 == char.class) {
if (from1 == short.class || from1 == byte.class) {
return int.class;
} else {
return null;
}
} else if (from1 == char.class) {
if (from0 == short.class || from0 == byte.class) {
return int.class;
} else {
if (from0 == byte.class) {
if (from1 == byte.class) {
return byte.class;
} else if (from1 == short.class) {
if (const1 != null) {
final short constant = (short)const1;

if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
return byte.class;
}
}

return short.class;
} else if (from1 == char.class) {
return int.class;
} else if (from1 == int.class) {
if (const1 != null) {
final int constant = (int)const1;

if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
return byte.class;
}
}

return int.class;
}
} else if (from0 == short.class) {
if (from1 == byte.class) {
if (const0 != null) {
final short constant = (short)const0;

if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
return byte.class;
}
}

return short.class;
} else if (from1 == short.class) {
return short.class;
} else if (from1 == char.class) {
return int.class;
} else if (from1 == int.class) {
if (const1 != null) {
final int constant = (int)const1;

if (constant <= Short.MAX_VALUE && constant >= Short.MIN_VALUE) {
return short.class;
}
}

return int.class;
}
} else if (from0 == char.class) {
if (from1 == byte.class) {
return int.class;
} else if (from1 == short.class) {
return int.class;
} else if (from1 == char.class) {
return char.class;
} else if (from1 == int.class) {
if (const1 != null) {
final int constant = (int)const1;

if (constant <= Character.MAX_VALUE && constant >= Character.MIN_VALUE) {
return byte.class;
}
}

return int.class;
}
} else if (from0 == int.class) {
if (from1 == byte.class) {
if (const0 != null) {
final int constant = (int)const0;

if (constant <= Byte.MAX_VALUE && constant >= Byte.MIN_VALUE) {
return byte.class;
}
}

return int.class;
} else if (from1 == short.class) {
if (const0 != null) {
final int constant = (int)const0;

if (constant <= Short.MAX_VALUE && constant >= Short.MIN_VALUE) {
return byte.class;
}
}

return int.class;
} else if (from1 == char.class) {
if (const0 != null) {
final int constant = (int)const0;

if (constant <= Character.MAX_VALUE && constant >= Character.MIN_VALUE) {
return byte.class;
}
}

return int.class;
} else if (from1 == int.class) {
return int.class;
}
return null;
}
} else {
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.elasticsearch.painless.ir.ClassNode;
import org.elasticsearch.painless.ir.ExpressionNode;
import org.elasticsearch.painless.lookup.PainlessCast;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.symbol.ScriptRoot;

import java.util.Objects;
Expand Down Expand Up @@ -84,14 +83,6 @@ public abstract class AExpression extends ANode {
*/
boolean internal = false;

/**
* Set to the value of the constant this expression node represents if
* and only if the node represents a constant. If this is not null
* this node will be replaced by an {@link EConstant} during casting
* if it's not already one.
*/
Object constant = null;

/**
* Set to true by {@link ENull} to represent a null value.
*/
Expand Down Expand Up @@ -134,97 +125,14 @@ AExpression cast(ScriptRoot scriptRoot, Scope scope) {
PainlessCast cast = AnalyzerCaster.getLegalCast(location, actual, expected, explicit, internal);

if (cast == null) {
if (constant == null || this instanceof EConstant) {
// For the case where a cast is not required and a constant is not set
// or the node is already an EConstant no changes are required to the tree.

return this;
} else {
// For the case where a cast is not required but a
// constant is set, an EConstant replaces this node
// with the constant copied from this node. Note that
// for constants output data does not need to be copied
// from this node because the output data for the EConstant
// will already be the same.

EConstant econstant = new EConstant(location, constant);
econstant.analyze(scriptRoot, scope);

if (!expected.equals(econstant.actual)) {
throw createError(new IllegalStateException("Illegal tree structure."));
}

return econstant;
}
return this;
} else {
if (constant == null) {
// For the case where a cast is required and a constant is not set.
// Modify the tree to add an ECast between this node and its parent.
// The output data from this node is copied to the ECast for
// further reads done by the parent.

ECast ecast = new ECast(location, this, cast);
ecast.statement = statement;
ecast.actual = expected;
ecast.isNull = isNull;

return ecast;
} else {
if (PainlessLookupUtility.isConstantType(expected)) {
// For the case where a cast is required, a constant is set,
// and the constant can be immediately cast to the expected type.
// An EConstant replaces this node with the constant cast appropriately
// from the constant value defined by this node. Note that
// for constants output data does not need to be copied
// from this node because the output data for the EConstant
// will already be the same.

constant = AnalyzerCaster.constCast(location, constant, cast);

EConstant econstant = new EConstant(location, constant);
econstant.analyze(scriptRoot, scope);

if (!expected.equals(econstant.actual)) {
throw createError(new IllegalStateException("Illegal tree structure."));
}

return econstant;
} else if (this instanceof EConstant) {
// For the case where a cast is required, a constant is set,
// the constant cannot be immediately cast to the expected type,
// and this node is already an EConstant. Modify the tree to add
// an ECast between this node and its parent. Note that
// for constants output data does not need to be copied
// from this node because the output data for the EConstant
// will already be the same.

ECast ecast = new ECast(location, this, cast);
ecast.actual = expected;

return ecast;
} else {
// For the case where a cast is required, a constant is set,
// the constant cannot be immediately cast to the expected type,
// and this node is not an EConstant. Replace this node with
// an Econstant node copying the constant from this node.
// Modify the tree to add an ECast between the EConstant node
// and its parent. Note that for constants output data does not
// need to be copied from this node because the output data for
// the EConstant will already be the same.

EConstant econstant = new EConstant(location, constant);
econstant.analyze(scriptRoot, scope);

if (!actual.equals(econstant.actual)) {
throw createError(new IllegalStateException("Illegal tree structure."));
}

ECast ecast = new ECast(location, econstant, cast);
ecast.actual = expected;

return ecast;
}
}
ECast ecast = new ECast(location, this, cast);
ecast.statement = statement;
ecast.actual = expected;
ecast.isNull = isNull;

return ecast;
}
}
}
Loading