Search in sources :

Example 1 with AbstractDSLExpressionReducer

use of com.oracle.truffle.dsl.processor.expression.DSLExpression.AbstractDSLExpressionReducer in project graal by oracle.

the class GuardExpression method isConstantTrueInSlowPath.

public boolean isConstantTrueInSlowPath(ProcessorContext context) {
    DSLExpression reducedExpression = getExpression().reduce(new AbstractDSLExpressionReducer() {

        @Override
        public DSLExpression visitVariable(Variable binary) {
            // on the slow path we can assume all cache expressions inlined.
            for (CacheExpression cache : source.getCaches()) {
                if (ElementUtils.variableEquals(cache.getParameter().getVariableElement(), binary.getResolvedVariable())) {
                    return cache.getExpression();
                }
            }
            return super.visitVariable(binary);
        }

        @Override
        public DSLExpression visitCall(Call binary) {
            ExecutableElement method = binary.getResolvedMethod();
            if (!method.getSimpleName().toString().equals("equals")) {
                return binary;
            }
            if (method.getModifiers().contains(Modifier.STATIC)) {
                return binary;
            }
            if (!ElementUtils.typeEquals(method.getReturnType(), context.getType(boolean.class))) {
                return binary;
            }
            if (method.getParameters().size() != 1) {
                return binary;
            }
            // signature: receiver.equals(receiver) can be folded to true
            DSLExpression receiver = binary.getReceiver();
            DSLExpression firstArg = binary.getParameters().get(0);
            if (receiver instanceof Variable && firstArg instanceof Variable) {
                if (receiver.equals(firstArg)) {
                    return new BooleanLiteral(true);
                }
            }
            return super.visitCall(binary);
        }

        @Override
        public DSLExpression visitBinary(Binary binary) {
            // signature: value == value can be folded to true
            if (IDENTITY_FOLD_OPERATORS.contains(binary.getOperator())) {
                if (binary.getLeft() instanceof Variable && binary.getRight() instanceof Variable) {
                    Variable leftVar = ((Variable) binary.getLeft());
                    Variable rightVar = ((Variable) binary.getRight());
                    if (leftVar.equals(rightVar)) {
                        // double and float cannot be folded as NaN is never identity equal
                        if (!ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(float.class)) && !ElementUtils.typeEquals(leftVar.getResolvedType(), context.getType(double.class))) {
                            return new BooleanLiteral(true);
                        }
                    }
                }
            }
            return super.visitBinary(binary);
        }
    });
    Object o = reducedExpression.resolveConstant();
    if (o instanceof Boolean) {
        if (((Boolean) o).booleanValue()) {
            return true;
        }
    }
    return false;
}
Also used : Call(com.oracle.truffle.dsl.processor.expression.DSLExpression.Call) Variable(com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable) BooleanLiteral(com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral) AbstractDSLExpressionReducer(com.oracle.truffle.dsl.processor.expression.DSLExpression.AbstractDSLExpressionReducer) ExecutableElement(javax.lang.model.element.ExecutableElement) Binary(com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary) DSLExpression(com.oracle.truffle.dsl.processor.expression.DSLExpression)

Aggregations

DSLExpression (com.oracle.truffle.dsl.processor.expression.DSLExpression)1 AbstractDSLExpressionReducer (com.oracle.truffle.dsl.processor.expression.DSLExpression.AbstractDSLExpressionReducer)1 Binary (com.oracle.truffle.dsl.processor.expression.DSLExpression.Binary)1 BooleanLiteral (com.oracle.truffle.dsl.processor.expression.DSLExpression.BooleanLiteral)1 Call (com.oracle.truffle.dsl.processor.expression.DSLExpression.Call)1 Variable (com.oracle.truffle.dsl.processor.expression.DSLExpression.Variable)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1