Search in sources :

Example 6 with ZeroExtendNode

use of org.graalvm.compiler.nodes.calc.ZeroExtendNode in project graal by oracle.

the class CInterfaceInvocationPlugin method adaptPrimitiveType.

static ValueNode adaptPrimitiveType(StructuredGraph graph, ValueNode value, JavaKind fromKind, JavaKind toKind, boolean isUnsigned) {
    if (fromKind == toKind) {
        return value;
    }
    assert fromKind.isNumericFloat() == toKind.isNumericFloat();
    int fromBits = fromKind.getBitCount();
    int toBits = toKind.getBitCount();
    if (fromBits == toBits) {
        return value;
    } else if (fromKind.isNumericFloat()) {
        FloatConvert op;
        if (fromKind == JavaKind.Float && toKind == JavaKind.Double) {
            op = FloatConvert.F2D;
        } else if (fromKind == JavaKind.Double && toKind == JavaKind.Float) {
            op = FloatConvert.D2F;
        } else {
            throw shouldNotReachHere();
        }
        return graph.unique(new FloatConvertNode(op, value));
    } else if (toKind == JavaKind.Boolean) {
        JavaKind computeKind = fromKind == JavaKind.Long ? JavaKind.Long : JavaKind.Int;
        LogicNode comparison = graph.unique(new IntegerEqualsNode(adaptPrimitiveType(graph, value, fromKind, computeKind, true), ConstantNode.forIntegerKind(computeKind, 0, graph)));
        return graph.unique(new ConditionalNode(comparison, ConstantNode.forBoolean(false, graph), ConstantNode.forBoolean(true, graph)));
    } else if (fromBits > toBits) {
        return graph.unique(new NarrowNode(value, toBits));
    } else if (isUnsigned) {
        return graph.unique(new ZeroExtendNode(value, toBits));
    } else {
        return graph.unique(new SignExtendNode(value, toBits));
    }
}
Also used : ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) FloatConvertNode(org.graalvm.compiler.nodes.calc.FloatConvertNode) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) FloatConvert(org.graalvm.compiler.core.common.calc.FloatConvert) LogicNode(org.graalvm.compiler.nodes.LogicNode) NarrowNode(org.graalvm.compiler.nodes.calc.NarrowNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 7 with ZeroExtendNode

use of org.graalvm.compiler.nodes.calc.ZeroExtendNode in project graal by oracle.

the class LoopEx method findInductionVariables.

/**
 * Collect all the basic induction variables for the loop and the find any induction variables
 * which are derived from the basic ones.
 *
 * @param loop
 * @return a map from node to induction variable
 */
private static EconomicMap<Node, InductionVariable> findInductionVariables(LoopEx loop) {
    EconomicMap<Node, InductionVariable> ivs = EconomicMap.create(Equivalence.IDENTITY);
    Queue<InductionVariable> scanQueue = new LinkedList<>();
    LoopBeginNode loopBegin = loop.loopBegin();
    AbstractEndNode forwardEnd = loopBegin.forwardEnd();
    for (PhiNode phi : loopBegin.valuePhis()) {
        ValueNode backValue = phi.singleBackValueOrThis();
        if (backValue == phi) {
            continue;
        }
        ValueNode stride = addSub(loop, backValue, phi);
        if (stride != null) {
            BasicInductionVariable biv = new BasicInductionVariable(loop, (ValuePhiNode) phi, phi.valueAt(forwardEnd), stride, (BinaryArithmeticNode<?>) backValue);
            ivs.put(phi, biv);
            scanQueue.add(biv);
        }
    }
    while (!scanQueue.isEmpty()) {
        InductionVariable baseIv = scanQueue.remove();
        ValueNode baseIvNode = baseIv.valueNode();
        for (ValueNode op : baseIvNode.usages().filter(ValueNode.class)) {
            if (loop.isOutsideLoop(op)) {
                continue;
            }
            if (op.usages().count() == 1 && op.usages().first() == baseIvNode) {
                /*
                     * This is just the base induction variable increment with no other uses so
                     * don't bother reporting it.
                     */
                continue;
            }
            InductionVariable iv = null;
            ValueNode offset = addSub(loop, op, baseIvNode);
            ValueNode scale;
            if (offset != null) {
                iv = new DerivedOffsetInductionVariable(loop, baseIv, offset, (BinaryArithmeticNode<?>) op);
            } else if (op instanceof NegateNode) {
                iv = new DerivedScaledInductionVariable(loop, baseIv, (NegateNode) op);
            } else if ((scale = mul(loop, op, baseIvNode)) != null) {
                iv = new DerivedScaledInductionVariable(loop, baseIv, scale, op);
            } else {
                boolean isValidConvert = op instanceof PiNode || op instanceof SignExtendNode;
                if (!isValidConvert && op instanceof ZeroExtendNode) {
                    ZeroExtendNode zeroExtendNode = (ZeroExtendNode) op;
                    isValidConvert = zeroExtendNode.isInputAlwaysPositive() || ((IntegerStamp) zeroExtendNode.stamp(NodeView.DEFAULT)).isPositive();
                }
                if (isValidConvert) {
                    iv = new DerivedConvertedInductionVariable(loop, baseIv, op.stamp(NodeView.DEFAULT), op);
                }
            }
            if (iv != null) {
                ivs.put(op, iv);
                scanQueue.offer(iv);
            }
        }
    }
    return ivs;
}
Also used : ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) ValueAnchorNode(org.graalvm.compiler.nodes.extended.ValueAnchorNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode) NegateNode(org.graalvm.compiler.nodes.calc.NegateNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) IfNode(org.graalvm.compiler.nodes.IfNode) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) PiNode(org.graalvm.compiler.nodes.PiNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) BinaryArithmeticNode(org.graalvm.compiler.nodes.calc.BinaryArithmeticNode) FullInfopointNode(org.graalvm.compiler.nodes.FullInfopointNode) MulNode(org.graalvm.compiler.nodes.calc.MulNode) CompareNode(org.graalvm.compiler.nodes.calc.CompareNode) LeftShiftNode(org.graalvm.compiler.nodes.calc.LeftShiftNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) IntegerEqualsNode(org.graalvm.compiler.nodes.calc.IntegerEqualsNode) AddNode(org.graalvm.compiler.nodes.calc.AddNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) SubNode(org.graalvm.compiler.nodes.calc.SubNode) Node(org.graalvm.compiler.graph.Node) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) PiNode(org.graalvm.compiler.nodes.PiNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) BinaryArithmeticNode(org.graalvm.compiler.nodes.calc.BinaryArithmeticNode) LinkedList(java.util.LinkedList) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) ValueNode(org.graalvm.compiler.nodes.ValueNode) NegateNode(org.graalvm.compiler.nodes.calc.NegateNode)

Example 8 with ZeroExtendNode

use of org.graalvm.compiler.nodes.calc.ZeroExtendNode in project graal by oracle.

the class CEntryPointCallStubMethod method generateExceptionHandler.

private void generateExceptionHandler(HostedProviders providers, SubstrateGraphKit kit, ExceptionObjectNode exception, JavaKind returnKind) {
    if (entryPointData.getExceptionHandler() == CEntryPointOptions.FatalExceptionHandler.class) {
        kit.append(new CEntryPointLeaveNode(LeaveAction.ExceptionAbort, exception));
        kit.append(new DeadEndNode());
    } else {
        ResolvedJavaType throwable = providers.getMetaAccess().lookupJavaType(Throwable.class);
        ResolvedJavaType handler = providers.getMetaAccess().lookupJavaType(entryPointData.getExceptionHandler());
        ResolvedJavaMethod[] handlerMethods = handler.getDeclaredMethods();
        UserError.guarantee(handlerMethods.length == 1 && handlerMethods[0].isStatic(), "Exception handler class must declare exactly one static method: " + targetMethod.format("%H.%n(%p)") + " -> " + handler.toJavaName());
        JavaType[] handlerParameterTypes = handlerMethods[0].toParameterTypes();
        UserError.guarantee(handlerParameterTypes.length == 1 && ((ResolvedJavaType) handlerParameterTypes[0]).isAssignableFrom(throwable), "Exception handler method must have exactly one parameter of type Throwable: " + targetMethod.format("%H.%n(%p)") + " -> " + handlerMethods[0].format("%H.%n(%p)"));
        int handlerExceptionBci = kit.bci();
        InvokeWithExceptionNode handlerInvoke = kit.startInvokeWithException(handlerMethods[0], InvokeKind.Static, kit.getFrameState(), kit.bci(), handlerExceptionBci, exception);
        kit.noExceptionPart();
        ValueNode returnValue = handlerInvoke;
        if (handlerInvoke.getStackKind() != returnKind) {
            JavaKind fromKind = handlerInvoke.getStackKind();
            if (fromKind == JavaKind.Float && returnKind == JavaKind.Double) {
                returnValue = kit.unique(new FloatConvertNode(FloatConvert.F2D, returnValue));
            } else if (fromKind.isUnsigned() && returnKind.isNumericInteger() && returnKind.getBitCount() > fromKind.getBitCount()) {
                returnValue = kit.unique(new ZeroExtendNode(returnValue, returnKind.getBitCount()));
            } else if (fromKind.isNumericInteger() && returnKind.isNumericInteger() && returnKind.getBitCount() > fromKind.getBitCount()) {
                returnValue = kit.unique(new SignExtendNode(returnValue, returnKind.getBitCount()));
            } else {
                throw UserError.abort("Exception handler method return type must be assignable to entry point method return type: " + targetMethod.format("%H.%n(%p)") + " -> " + handlerMethods[0].format("%H.%n(%p)"));
            }
        }
        kit.createReturn(returnValue, returnValue.getStackKind());
        // fail-safe for exceptions in exception handler
        kit.exceptionPart();
        kit.append(new CEntryPointLeaveNode(LeaveAction.ExceptionAbort, kit.exceptionObject()));
        kit.append(new DeadEndNode());
        kit.endInvokeWithException();
    }
}
Also used : CEntryPointOptions(com.oracle.svm.core.c.function.CEntryPointOptions) SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) CEntryPointLeaveNode(com.oracle.svm.core.graal.nodes.CEntryPointLeaveNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JavaType(jdk.vm.ci.meta.JavaType) FloatConvertNode(org.graalvm.compiler.nodes.calc.FloatConvertNode) InvokeWithExceptionNode(org.graalvm.compiler.nodes.InvokeWithExceptionNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) DeadEndNode(com.oracle.svm.core.graal.nodes.DeadEndNode) JavaKind(jdk.vm.ci.meta.JavaKind)

Aggregations

ZeroExtendNode (org.graalvm.compiler.nodes.calc.ZeroExtendNode)8 ValueNode (org.graalvm.compiler.nodes.ValueNode)7 SignExtendNode (org.graalvm.compiler.nodes.calc.SignExtendNode)7 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)4 JavaKind (jdk.vm.ci.meta.JavaKind)3 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)3 Node (org.graalvm.compiler.graph.Node)3 PhiNode (org.graalvm.compiler.nodes.PhiNode)3 AddNode (org.graalvm.compiler.nodes.calc.AddNode)3 FloatConvertNode (org.graalvm.compiler.nodes.calc.FloatConvertNode)3 OffsetAddressNode (org.graalvm.compiler.nodes.memory.address.OffsetAddressNode)3 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)2 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)2 AMD64AddressNode (org.graalvm.compiler.core.amd64.AMD64AddressNode)2 Stamp (org.graalvm.compiler.core.common.type.Stamp)2 GraalHotSpotVMConfigNode (org.graalvm.compiler.hotspot.nodes.GraalHotSpotVMConfigNode)2 BasicInductionVariable (org.graalvm.compiler.loop.BasicInductionVariable)2 DerivedInductionVariable (org.graalvm.compiler.loop.DerivedInductionVariable)2 InductionVariable (org.graalvm.compiler.loop.InductionVariable)2 LogicNode (org.graalvm.compiler.nodes.LogicNode)2