Search in sources :

Example 21 with LogicNode

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

the class SubstrateGraphBuilderPlugins method registerClassPlugins.

private static void registerClassPlugins(InvocationPlugins plugins) {
    /*
         * We have our own Java-level implementation of isAssignableFrom() in DynamicHub, so we do
         * not need to intrinsifiy that to a Graal node. Therefore, we overwrite and deactivate the
         * invocation plugin registered in StandardGraphBuilderPlugins.
         *
         * TODO we should remove the implementation from DynamicHub to a lowering of
         * ClassIsAssignableFromNode. Then we can remove this code.
         */
    Registration r = new Registration(plugins, Class.class).setAllowOverwrite(true);
    r.register2("isAssignableFrom", Receiver.class, Class.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver type, ValueNode otherType) {
            return false;
        }
    });
    r.register2("cast", Receiver.class, Object.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
            ValueNode toType = receiver.get();
            LogicNode condition = b.append(InstanceOfDynamicNode.create(b.getAssumptions(), b.getConstantReflection(), toType, object, true));
            if (condition.isTautology()) {
                b.addPush(JavaKind.Object, object);
            } else {
                FixedGuardNode fixedGuard = b.add(new FixedGuardNode(condition, DeoptimizationReason.ClassCastException, DeoptimizationAction.InvalidateReprofile, false));
                b.addPush(JavaKind.Object, DynamicPiNode.create(b.getAssumptions(), b.getConstantReflection(), object, fixedGuard, toType));
            }
            return true;
        }
    });
    r.register2("isInstance", Receiver.class, Object.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object) {
            ValueNode type = receiver.get();
            LogicNode condition = b.append(InstanceOfDynamicNode.create(null, b.getConstantReflection(), type, object, false));
            b.push(JavaKind.Boolean.getStackKind(), b.append(new ConditionalNode(condition).canonical(null)));
            return true;
        }
    });
}
Also used : FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) ConditionalNode(org.graalvm.compiler.nodes.calc.ConditionalNode) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) StackValueNode(com.oracle.svm.core.graal.stackvalue.StackValueNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ConvertUnknownValueNode(com.oracle.graal.pointsto.nodes.ConvertUnknownValueNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) LogicNode(org.graalvm.compiler.nodes.LogicNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 22 with LogicNode

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

the class EffectsClosure method processBlock.

@Override
protected BlockT processBlock(Block block, BlockT state) {
    if (!state.isDead()) {
        GraphEffectList effects = blockEffects.get(block);
        /*
             * If we enter an if branch that is known to be unreachable, we mark it as dead and
             * cease to do any more analysis on it. At merges, these dead branches will be ignored.
             */
        if (block.getBeginNode().predecessor() instanceof IfNode) {
            IfNode ifNode = (IfNode) block.getBeginNode().predecessor();
            LogicNode condition = ifNode.condition();
            Node alias = getScalarAlias(condition);
            if (alias instanceof LogicConstantNode) {
                LogicConstantNode constant = (LogicConstantNode) alias;
                boolean isTrueSuccessor = block.getBeginNode() == ifNode.trueSuccessor();
                if (constant.getValue() != isTrueSuccessor) {
                    state.markAsDead();
                    effects.killIfBranch(ifNode, constant.getValue());
                    return state;
                }
            }
        }
        OptionValues options = block.getBeginNode().getOptions();
        VirtualUtil.trace(options, debug, "\nBlock: %s, preds: %s, succ: %s (", block, block.getPredecessors(), block.getSuccessors());
        // a lastFixedNode is needed in case we want to insert fixed nodes
        FixedWithNextNode lastFixedNode = null;
        Iterable<? extends Node> nodes = schedule != null ? schedule.getBlockToNodesMap().get(block) : block.getNodes();
        for (Node node : nodes) {
            // reset the aliases (may be non-null due to iterative loop processing)
            aliases.set(node, null);
            if (node instanceof LoopExitNode) {
                LoopExitNode loopExit = (LoopExitNode) node;
                for (ProxyNode proxy : loopExit.proxies()) {
                    aliases.set(proxy, null);
                    changed |= processNode(proxy, state, effects, lastFixedNode) && isSignificantNode(node);
                }
                processLoopExit(loopExit, loopEntryStates.get(loopExit.loopBegin()), state, blockEffects.get(block));
            }
            changed |= processNode(node, state, effects, lastFixedNode) && isSignificantNode(node);
            if (node instanceof FixedWithNextNode) {
                lastFixedNode = (FixedWithNextNode) node;
            }
            if (state.isDead()) {
                break;
            }
        }
        VirtualUtil.trace(options, debug, ")\n    end state: %s\n", state);
    }
    return state;
}
Also used : ProxyNode(org.graalvm.compiler.nodes.ProxyNode) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) OptionValues(org.graalvm.compiler.options.OptionValues) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) LogicConstantNode(org.graalvm.compiler.nodes.LogicConstantNode) AllocatedObjectNode(org.graalvm.compiler.nodes.virtual.AllocatedObjectNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) BoxNode(org.graalvm.compiler.nodes.extended.BoxNode) IfNode(org.graalvm.compiler.nodes.IfNode) VirtualObjectNode(org.graalvm.compiler.nodes.virtual.VirtualObjectNode) CommitAllocationNode(org.graalvm.compiler.nodes.virtual.CommitAllocationNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) LoopExitNode(org.graalvm.compiler.nodes.LoopExitNode) Node(org.graalvm.compiler.graph.Node) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) ProxyNode(org.graalvm.compiler.nodes.ProxyNode) LogicConstantNode(org.graalvm.compiler.nodes.LogicConstantNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) IfNode(org.graalvm.compiler.nodes.IfNode)

Example 23 with LogicNode

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

the class JNIJavaCallWrapperMethod method buildGraph.

@Override
public StructuredGraph buildGraph(DebugContext debug, ResolvedJavaMethod method, HostedProviders providers, Purpose purpose) {
    UniverseMetaAccess metaAccess = (UniverseMetaAccess) providers.getMetaAccess();
    JNIGraphKit kit = new JNIGraphKit(debug, providers, method);
    StructuredGraph graph = kit.getGraph();
    FrameStateBuilder state = new FrameStateBuilder(null, method, graph);
    state.initializeForMethodStart(null, true, providers.getGraphBuilderPlugins());
    JavaKind vmThreadKind = metaAccess.lookupJavaType(JNIEnvironment.class).getJavaKind();
    ValueNode vmThread = kit.loadLocal(0, vmThreadKind);
    kit.append(new CEntryPointEnterNode(EnterAction.Enter, vmThread));
    ResolvedJavaMethod invokeMethod = providers.getMetaAccess().lookupJavaMethod(reflectMethod);
    Signature invokeSignature = invokeMethod.getSignature();
    List<Pair<ValueNode, ResolvedJavaType>> argsWithTypes = loadAndUnboxArguments(kit, providers, invokeMethod, invokeSignature);
    JavaKind returnKind = invokeSignature.getReturnKind();
    if (invokeMethod.isConstructor()) {
        // return `this` to implement NewObject
        assert returnKind == JavaKind.Void;
        returnKind = JavaKind.Object;
    }
    IfNode ifNode = kit.startIf(null, BranchProbabilityNode.FAST_PATH_PROBABILITY);
    kit.thenPart();
    LogicNode typeChecks = LogicConstantNode.tautology(kit.getGraph());
    ValueNode[] args = new ValueNode[argsWithTypes.size()];
    for (int i = 0; i < argsWithTypes.size(); i++) {
        ValueNode value = argsWithTypes.get(i).getLeft();
        ResolvedJavaType type = argsWithTypes.get(i).getRight();
        if (!type.isPrimitive() && !type.isJavaLangObject()) {
            TypeReference typeRef = TypeReference.createTrusted(kit.getAssumptions(), type);
            LogicNode instanceOf = kit.unique(InstanceOfNode.createAllowNull(typeRef, value, null, null));
            typeChecks = LogicNode.and(typeChecks, instanceOf, BranchProbabilityNode.FAST_PATH_PROBABILITY);
            FixedGuardNode guard = kit.append(new FixedGuardNode(instanceOf, DeoptimizationReason.ClassCastException, DeoptimizationAction.None, false));
            value = kit.append(PiNode.create(value, StampFactory.object(typeRef), guard));
        }
        args[i] = value;
    }
    // safe because logic nodes are floating
    ifNode.setCondition(typeChecks);
    InvokeKind kind = // 
    invokeMethod.isStatic() ? // 
    InvokeKind.Static : ((nonVirtual || invokeMethod.isConstructor()) ? InvokeKind.Special : InvokeKind.Virtual);
    ValueNode invokeResult = createInvoke(kit, invokeMethod, kind, state, kit.bci(), args);
    if (invokeMethod.isConstructor()) {
        // return `this` to implement NewObject
        invokeResult = args[0];
    }
    // illegal parameter types
    kit.elsePart();
    ConstantNode exceptionObject = kit.createObject(cachedArgumentClassCastException);
    kit.retainPendingException(exceptionObject);
    ValueNode typeMismatchValue = null;
    if (returnKind != JavaKind.Void) {
        typeMismatchValue = kit.unique(ConstantNode.defaultForKind(returnKind.getStackKind()));
    }
    AbstractMergeNode merge = kit.endIf();
    ValueNode returnValue = null;
    if (returnKind != JavaKind.Void) {
        ValueNode[] inputs = { invokeResult, typeMismatchValue };
        returnValue = kit.getGraph().addWithoutUnique(new ValuePhiNode(invokeResult.stamp(NodeView.DEFAULT), merge, inputs));
        state.push(returnKind, returnValue);
    }
    merge.setStateAfter(state.create(kit.bci(), merge));
    if (returnKind != JavaKind.Void) {
        state.pop(returnKind);
        if (returnKind.isObject()) {
            returnValue = kit.boxObjectInLocalHandle(returnValue);
        }
    }
    kit.append(new CEntryPointLeaveNode(LeaveAction.Leave));
    kit.createReturn(returnValue, returnKind);
    assert graph.verify();
    return graph;
}
Also used : UniverseMetaAccess(com.oracle.graal.pointsto.infrastructure.UniverseMetaAccess) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) IfNode(org.graalvm.compiler.nodes.IfNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) InvokeKind(org.graalvm.compiler.nodes.CallTargetNode.InvokeKind) CEntryPointLeaveNode(com.oracle.svm.core.graal.nodes.CEntryPointLeaveNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) LogicConstantNode(org.graalvm.compiler.nodes.LogicConstantNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) JNIEnvironment(com.oracle.svm.jni.nativeapi.JNIEnvironment) CEntryPointEnterNode(com.oracle.svm.core.graal.nodes.CEntryPointEnterNode) Signature(jdk.vm.ci.meta.Signature) ValueNode(org.graalvm.compiler.nodes.ValueNode) FrameStateBuilder(org.graalvm.compiler.java.FrameStateBuilder) LogicNode(org.graalvm.compiler.nodes.LogicNode) TypeReference(org.graalvm.compiler.core.common.type.TypeReference) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) JavaKind(jdk.vm.ci.meta.JavaKind) Pair(org.graalvm.collections.Pair)

Example 24 with LogicNode

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

the class JNINativeCallWrapperMethod method castObject.

private static ValueNode castObject(JNIGraphKit kit, ValueNode object, ResolvedJavaType type) {
    ValueNode casted = object;
    if (!type.isJavaLangObject()) {
        // safe cast to expected type
        TypeReference typeRef = TypeReference.createTrusted(kit.getAssumptions(), type);
        LogicNode condition = kit.append(InstanceOfNode.createAllowNull(typeRef, object, null, null));
        if (!condition.isTautology()) {
            ObjectStamp stamp = StampFactory.object(typeRef, false);
            FixedGuardNode fixedGuard = kit.append(new FixedGuardNode(condition, DeoptimizationReason.ClassCastException, DeoptimizationAction.None, false));
            casted = kit.append(PiNode.create(object, stamp, fixedGuard));
        }
    }
    return casted;
}
Also used : FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) ValueNode(org.graalvm.compiler.nodes.ValueNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) TypeReference(org.graalvm.compiler.core.common.type.TypeReference)

Example 25 with LogicNode

use of org.graalvm.compiler.nodes.LogicNode 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)

Aggregations

LogicNode (org.graalvm.compiler.nodes.LogicNode)48 ValueNode (org.graalvm.compiler.nodes.ValueNode)37 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)26 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)11 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)11 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)10 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)9 Stamp (org.graalvm.compiler.core.common.type.Stamp)9 IfNode (org.graalvm.compiler.nodes.IfNode)9 TypeReference (org.graalvm.compiler.core.common.type.TypeReference)8 LogicConstantNode (org.graalvm.compiler.nodes.LogicConstantNode)7 CompareNode (org.graalvm.compiler.nodes.calc.CompareNode)7 ConditionalNode (org.graalvm.compiler.nodes.calc.ConditionalNode)7 JavaConstant (jdk.vm.ci.meta.JavaConstant)6 AbstractBeginNode (org.graalvm.compiler.nodes.AbstractBeginNode)6 FixedNode (org.graalvm.compiler.nodes.FixedNode)6 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)6 JavaKind (jdk.vm.ci.meta.JavaKind)5 ObjectStamp (org.graalvm.compiler.core.common.type.ObjectStamp)5 Node (org.graalvm.compiler.graph.Node)5