Search in sources :

Example 71 with ConstantNode

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

the class MethodTypeFlowBuilder method apply.

protected void apply() {
    // assert method.getAnnotation(Fold.class) == null : method;
    if (method.getAnnotation(NodeIntrinsic.class) != null) {
        graph.getDebug().log("apply MethodTypeFlow on node intrinsic %s", method);
        AnalysisType returnType = (AnalysisType) method.getSignature().getReturnType(method.getDeclaringClass());
        if (returnType.getJavaKind() == JavaKind.Object) {
            /*
                 * This is a method used in a snippet, so most likely the return value does not
                 * matter at all. However, some methods return an object, and the snippet continues
                 * to work with the object. So pretend that this method returns an object of the
                 * exact return type.
                 */
            TypeFlow<?> returnTypeFlow = methodFlow.getResultFlow().getDeclaredType().getTypeFlow(this.bb, true);
            returnTypeFlow = new ProxyTypeFlow(null, returnTypeFlow);
            FormalReturnTypeFlow resultFlow = new FormalReturnTypeFlow(null, returnType, method);
            returnTypeFlow.addOriginalUse(this.bb, resultFlow);
            methodFlow.addMiscEntry(returnTypeFlow);
            methodFlow.setResult(resultFlow);
        }
        return;
    }
    if (!parse()) {
        return;
    }
    this.bb.getUnsupportedFeatures().checkMethod(method, graph);
    processedNodes = new NodeBitMap(graph);
    TypeFlowsOfNodes typeFlows = new TypeFlowsOfNodes();
    for (Node n : graph.getNodes()) {
        if (n instanceof ParameterNode) {
            ParameterNode node = (ParameterNode) n;
            if (node.getStackKind() == JavaKind.Object) {
                TypeFlowBuilder<?> paramBuilder = TypeFlowBuilder.create(bb, node, FormalParamTypeFlow.class, () -> {
                    boolean isStatic = Modifier.isStatic(method.getModifiers());
                    int index = node.index();
                    FormalParamTypeFlow parameter;
                    if (!isStatic && index == 0) {
                        AnalysisType paramType = method.getDeclaringClass();
                        parameter = new FormalReceiverTypeFlow(node, paramType, method);
                    } else {
                        int offset = isStatic ? 0 : 1;
                        AnalysisType paramType = (AnalysisType) method.getSignature().getParameterType(index - offset, method.getDeclaringClass());
                        parameter = new FormalParamTypeFlow(node, paramType, method, index);
                    }
                    methodFlow.setParameter(index, parameter);
                    return parameter;
                });
                typeFlowGraphBuilder.checkFormalParameterBuilder(paramBuilder);
                typeFlows.add(node, paramBuilder);
            }
        } else if (n instanceof BoxNode) {
            BoxNode node = (BoxNode) n;
            Object key = uniqueKey(node);
            BytecodeLocation boxSite = bb.analysisPolicy().createAllocationSite(bb, key, methodFlow.getMethod());
            AnalysisType type = (AnalysisType) StampTool.typeOrNull(node);
            TypeFlowBuilder<?> boxBuilder = TypeFlowBuilder.create(bb, node, BoxTypeFlow.class, () -> {
                BoxTypeFlow boxFlow = new BoxTypeFlow(node, type, boxSite);
                methodFlow.addAllocation(boxFlow);
                return boxFlow;
            });
            typeFlows.add(node, boxBuilder);
        }
        for (Node input : n.inputs()) {
            /*
                 * TODO change the handling of constants so that the SourceTypeFlow is created on
                 * demand, with the optimization that only one SourceTypeFlow is created ever for
                 * every distinct object (using, e.g., caching in a global IdentityHashMap).
                 */
            if (input instanceof ConstantNode && !typeFlows.contains((ConstantNode) input)) {
                ConstantNode node = (ConstantNode) input;
                if (node.asJavaConstant().isNull()) {
                    TypeFlowBuilder<SourceTypeFlow> sourceBuilder = TypeFlowBuilder.create(bb, node, SourceTypeFlow.class, () -> {
                        SourceTypeFlow constantSource = new SourceTypeFlow(node, TypeState.forNull());
                        methodFlow.addSource(constantSource);
                        return constantSource;
                    });
                    typeFlows.add(node, sourceBuilder);
                } else if (node.asJavaConstant().getJavaKind() == JavaKind.Object) {
                    /*
                         * TODO a SubstrateObjectConstant wrapping a PrimitiveConstant has kind
                         * equals to Object. Do we care about the effective value of these primitive
                         * constants in the analysis?
                         */
                    assert StampTool.isExactType(node);
                    AnalysisType type = (AnalysisType) StampTool.typeOrNull(node);
                    assert type.isInstantiated();
                    TypeFlowBuilder<SourceTypeFlow> sourceBuilder = TypeFlowBuilder.create(bb, node, SourceTypeFlow.class, () -> {
                        SourceTypeFlow constantSource = new SourceTypeFlow(node, TypeState.forConstant(this.bb, node.asJavaConstant(), type));
                        methodFlow.addSource(constantSource);
                        return constantSource;
                    });
                    typeFlows.add(node, sourceBuilder);
                }
            }
        }
    }
    // Propagate the type flows through the method's graph
    new NodeIterator(graph.start(), typeFlows).apply();
    /* Prune the method graph. Eliminate nodes with no uses. */
    typeFlowGraphBuilder.build();
    /*
         * Make sure that all existing InstanceOfNodes are registered even when only used as an
         * input of a conditional.
         */
    for (Node n : graph.getNodes()) {
        if (n instanceof InstanceOfNode) {
            InstanceOfNode instanceOf = (InstanceOfNode) n;
            markFieldsUsedInComparison(instanceOf.getValue());
        } else if (n instanceof ObjectEqualsNode) {
            ObjectEqualsNode compareNode = (ObjectEqualsNode) n;
            markFieldsUsedInComparison(compareNode.getX());
            markFieldsUsedInComparison(compareNode.getY());
        }
    }
}
Also used : AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) TypeFlowBuilder(com.oracle.graal.pointsto.flow.builder.TypeFlowBuilder) RawLoadNode(org.graalvm.compiler.nodes.extended.RawLoadNode) BeginNode(org.graalvm.compiler.nodes.BeginNode) MethodCallTargetNode(org.graalvm.compiler.nodes.java.MethodCallTargetNode) LoopBeginNode(org.graalvm.compiler.nodes.LoopBeginNode) WordCastNode(org.graalvm.compiler.word.WordCastNode) DynamicNewArrayNode(org.graalvm.compiler.nodes.java.DynamicNewArrayNode) ObjectEqualsNode(org.graalvm.compiler.nodes.calc.ObjectEqualsNode) BasicObjectCloneNode(org.graalvm.compiler.replacements.nodes.BasicObjectCloneNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) InvokeWithExceptionNode(org.graalvm.compiler.nodes.InvokeWithExceptionNode) AtomicReadAndWriteNode(org.graalvm.compiler.nodes.java.AtomicReadAndWriteNode) ConvertUnknownValueNode(com.oracle.graal.pointsto.nodes.ConvertUnknownValueNode) FixedNode(org.graalvm.compiler.nodes.FixedNode) AbstractEndNode(org.graalvm.compiler.nodes.AbstractEndNode) AnalysisUnsafePartitionLoadNode(com.oracle.graal.pointsto.nodes.AnalysisUnsafePartitionLoadNode) LoopEndNode(org.graalvm.compiler.nodes.LoopEndNode) ForeignCallNode(org.graalvm.compiler.nodes.extended.ForeignCallNode) NewInstanceNode(org.graalvm.compiler.nodes.java.NewInstanceNode) PhiNode(org.graalvm.compiler.nodes.PhiNode) AnalysisArraysCopyOfNode(com.oracle.graal.pointsto.nodes.AnalysisArraysCopyOfNode) DynamicNewInstanceNode(org.graalvm.compiler.nodes.java.DynamicNewInstanceNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) NewMultiArrayNode(org.graalvm.compiler.nodes.java.NewMultiArrayNode) LoadIndexedNode(org.graalvm.compiler.nodes.java.LoadIndexedNode) ReturnNode(org.graalvm.compiler.nodes.ReturnNode) BoxNode(org.graalvm.compiler.nodes.extended.BoxNode) BinaryMathIntrinsicNode(org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode) IfNode(org.graalvm.compiler.nodes.IfNode) RawStoreNode(org.graalvm.compiler.nodes.extended.RawStoreNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) IsNullNode(org.graalvm.compiler.nodes.calc.IsNullNode) NewArrayNode(org.graalvm.compiler.nodes.java.NewArrayNode) UnsafeCompareAndSwapNode(org.graalvm.compiler.nodes.java.UnsafeCompareAndSwapNode) LoadFieldNode(org.graalvm.compiler.nodes.java.LoadFieldNode) ExceptionObjectNode(org.graalvm.compiler.nodes.java.ExceptionObjectNode) InstanceOfNode(org.graalvm.compiler.nodes.java.InstanceOfNode) BasicArrayCopyNode(org.graalvm.compiler.replacements.nodes.BasicArrayCopyNode) InvokeNode(org.graalvm.compiler.nodes.InvokeNode) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) UnaryMathIntrinsicNode(org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode) StoreIndexedNode(org.graalvm.compiler.nodes.java.StoreIndexedNode) MonitorEnterNode(org.graalvm.compiler.nodes.java.MonitorEnterNode) GetClassNode(org.graalvm.compiler.nodes.extended.GetClassNode) BytecodeExceptionNode(org.graalvm.compiler.nodes.extended.BytecodeExceptionNode) StoreFieldNode(org.graalvm.compiler.nodes.java.StoreFieldNode) Node(org.graalvm.compiler.graph.Node) EndNode(org.graalvm.compiler.nodes.EndNode) AnalysisUnsafePartitionStoreNode(com.oracle.graal.pointsto.nodes.AnalysisUnsafePartitionStoreNode) BoxNode(org.graalvm.compiler.nodes.extended.BoxNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) BytecodeLocation(com.oracle.graal.pointsto.flow.context.BytecodeLocation) NodeIntrinsic(org.graalvm.compiler.graph.Node.NodeIntrinsic) PostOrderNodeIterator(org.graalvm.compiler.phases.graph.PostOrderNodeIterator) NodeBitMap(org.graalvm.compiler.graph.NodeBitMap) ObjectEqualsNode(org.graalvm.compiler.nodes.calc.ObjectEqualsNode) InstanceOfNode(org.graalvm.compiler.nodes.java.InstanceOfNode)

Example 72 with ConstantNode

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

the class XorNode method create.

public static ValueNode create(ValueNode x, ValueNode y, NodeView view) {
    BinaryOp<Xor> op = ArithmeticOpTable.forStamp(x.stamp(view)).getXor();
    Stamp stamp = op.foldStamp(x.stamp(view), y.stamp(view));
    ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp, view);
    if (tryConstantFold != null) {
        return tryConstantFold;
    }
    return canonical(null, op, stamp, x, y, view);
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) PrimitiveStamp(org.graalvm.compiler.core.common.type.PrimitiveStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) Xor(org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.Xor)

Example 73 with ConstantNode

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

the class SubNode method create.

public static ValueNode create(ValueNode x, ValueNode y, NodeView view) {
    BinaryOp<Sub> op = ArithmeticOpTable.forStamp(x.stamp(view)).getSub();
    Stamp stamp = op.foldStamp(x.stamp(view), y.stamp(view));
    ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp, view);
    if (tryConstantFold != null) {
        return tryConstantFold;
    }
    return canonical(null, op, stamp, x, y, view);
}
Also used : Sub(org.graalvm.compiler.core.common.type.ArithmeticOpTable.BinaryOp.Sub) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) Stamp(org.graalvm.compiler.core.common.type.Stamp) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp)

Example 74 with ConstantNode

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

the class IntegerSwitchNode method tryOptimizeEnumSwitch.

/**
 * For switch statements on enum values, the Java compiler has to generate complicated code:
 * because {@link Enum#ordinal()} can change when recompiling an enum, it cannot be used
 * directly as the value that is switched on. An intermediate int[] array, which is initialized
 * once at run time based on the actual {@link Enum#ordinal()} values, is used.
 *
 * The {@link ConstantFieldProvider} of Graal already detects the int[] arrays and marks them as
 * {@link ConstantNode#isDefaultStable() stable}, i.e., the array elements are constant. The
 * code in this method detects array loads from such a stable array and re-wires the switch to
 * use the keys from the array elements, so that the array load is unnecessary.
 */
private boolean tryOptimizeEnumSwitch(SimplifierTool tool) {
    if (!(value() instanceof LoadIndexedNode)) {
        /* Not the switch pattern we are looking for. */
        return false;
    }
    LoadIndexedNode loadIndexed = (LoadIndexedNode) value();
    if (loadIndexed.usages().count() > 1) {
        /*
             * The array load is necessary for other reasons too, so there is no benefit optimizing
             * the switch.
             */
        return false;
    }
    assert loadIndexed.usages().first() == this;
    ValueNode newValue = loadIndexed.index();
    JavaConstant arrayConstant = loadIndexed.array().asJavaConstant();
    if (arrayConstant == null || ((ConstantNode) loadIndexed.array()).getStableDimension() != 1 || !((ConstantNode) loadIndexed.array()).isDefaultStable()) {
        /*
             * The array is a constant that we can optimize. We require the array elements to be
             * constant too, since we put them as literal constants into the switch keys.
             */
        return false;
    }
    Integer optionalArrayLength = tool.getConstantReflection().readArrayLength(arrayConstant);
    if (optionalArrayLength == null) {
        /* Loading a constant value can be denied by the VM. */
        return false;
    }
    int arrayLength = optionalArrayLength;
    Map<Integer, List<Integer>> reverseArrayMapping = new HashMap<>();
    for (int i = 0; i < arrayLength; i++) {
        JavaConstant elementConstant = tool.getConstantReflection().readArrayElement(arrayConstant, i);
        if (elementConstant == null || elementConstant.getJavaKind() != JavaKind.Int) {
            /* Loading a constant value can be denied by the VM. */
            return false;
        }
        int element = elementConstant.asInt();
        /*
             * The value loaded from the array is the old switch key, the index into the array is
             * the new switch key. We build a mapping from the old switch key to new keys.
             */
        reverseArrayMapping.computeIfAbsent(element, e -> new ArrayList<>()).add(i);
    }
    /* Build high-level representation of new switch keys. */
    List<KeyData> newKeyDatas = new ArrayList<>(arrayLength);
    ArrayList<AbstractBeginNode> newSuccessors = new ArrayList<>(blockSuccessorCount());
    for (int i = 0; i < keys.length; i++) {
        List<Integer> newKeys = reverseArrayMapping.get(keys[i]);
        if (newKeys == null || newKeys.size() == 0) {
            /* The switch case is unreachable, we can ignore it. */
            continue;
        }
        /*
             * We do not have detailed profiling information about the individual new keys, so we
             * have to assume they split the probability of the old key.
             */
        double newKeyProbability = keyProbabilities[i] / newKeys.size();
        int newKeySuccessor = addNewSuccessor(keySuccessor(i), newSuccessors);
        for (int newKey : newKeys) {
            newKeyDatas.add(new KeyData(newKey, newKeyProbability, newKeySuccessor));
        }
    }
    int newDefaultSuccessor = addNewSuccessor(defaultSuccessor(), newSuccessors);
    double newDefaultProbability = keyProbabilities[keyProbabilities.length - 1];
    /*
         * We remove the array load, but we still need to preserve exception semantics by keeping
         * the bounds check. Fortunately the array length is a constant.
         */
    LogicNode boundsCheck = graph().unique(new IntegerBelowNode(newValue, ConstantNode.forInt(arrayLength, graph())));
    graph().addBeforeFixed(this, graph().add(new FixedGuardNode(boundsCheck, DeoptimizationReason.BoundsCheckException, DeoptimizationAction.InvalidateReprofile)));
    /*
         * Build the low-level representation of the new switch keys and replace ourself with a new
         * node.
         */
    doReplace(newValue, newKeyDatas, newSuccessors, newDefaultSuccessor, newDefaultProbability);
    /* The array load is now unnecessary. */
    assert loadIndexed.hasNoUsages();
    GraphUtil.removeFixedWithUnusedInputs(loadIndexed);
    return true;
}
Also used : DeoptimizationReason(jdk.vm.ci.meta.DeoptimizationReason) Arrays(java.util.Arrays) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) DeoptimizationAction(jdk.vm.ci.meta.DeoptimizationAction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeLIRBuilderTool(org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool) SimplifierTool(org.graalvm.compiler.graph.spi.SimplifierTool) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) StampFactory(org.graalvm.compiler.core.common.type.StampFactory) JavaKind(jdk.vm.ci.meta.JavaKind) NodeClass(org.graalvm.compiler.graph.NodeClass) Map(java.util.Map) LoadIndexedNode(org.graalvm.compiler.nodes.java.LoadIndexedNode) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) NodeInfo(org.graalvm.compiler.nodeinfo.NodeInfo) GraphUtil(org.graalvm.compiler.nodes.util.GraphUtil) NodeView(org.graalvm.compiler.nodes.NodeView) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) PrimitiveStamp(org.graalvm.compiler.core.common.type.PrimitiveStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) LIRLowerable(org.graalvm.compiler.nodes.spi.LIRLowerable) JavaConstant(jdk.vm.ci.meta.JavaConstant) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) List(java.util.List) ConstantFieldProvider(org.graalvm.compiler.core.common.spi.ConstantFieldProvider) Simplifiable(org.graalvm.compiler.graph.spi.Simplifiable) Comparator(java.util.Comparator) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JavaConstant(jdk.vm.ci.meta.JavaConstant) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) LoadIndexedNode(org.graalvm.compiler.nodes.java.LoadIndexedNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ArrayList(java.util.ArrayList) List(java.util.List) LogicNode(org.graalvm.compiler.nodes.LogicNode)

Example 75 with ConstantNode

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

the class AddressOfVMThreadLocalNode method lower.

@Override
public void lower(LoweringTool tool) {
    assert threadLocalInfo.offset >= 0;
    ValueNode base = holder;
    if (base.getStackKind() == JavaKind.Object) {
        base = graph().unique(new FloatingWordCastNode(FrameAccess.getWordStamp(), base));
    }
    assert base.getStackKind() == FrameAccess.getWordKind();
    ConstantNode offset = ConstantNode.forIntegerKind(FrameAccess.getWordKind(), threadLocalInfo.offset, graph());
    ValueNode address = graph().unique(new AddNode(base, offset));
    replaceAtUsagesAndDelete(address);
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) FloatingWordCastNode(com.oracle.svm.core.graal.nodes.FloatingWordCastNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) AddNode(org.graalvm.compiler.nodes.calc.AddNode)

Aggregations

ConstantNode (org.graalvm.compiler.nodes.ConstantNode)100 ValueNode (org.graalvm.compiler.nodes.ValueNode)46 JavaConstant (jdk.vm.ci.meta.JavaConstant)32 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)28 Stamp (org.graalvm.compiler.core.common.type.Stamp)23 Test (org.junit.Test)15 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)14 Node (org.graalvm.compiler.graph.Node)14 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)13 ParameterNode (org.graalvm.compiler.nodes.ParameterNode)13 PhiNode (org.graalvm.compiler.nodes.PhiNode)12 LogicNode (org.graalvm.compiler.nodes.LogicNode)11 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)10 ArrayList (java.util.ArrayList)9 JavaKind (jdk.vm.ci.meta.JavaKind)9 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)9 Constant (jdk.vm.ci.meta.Constant)8 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)8 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)7 LogicConstantNode (org.graalvm.compiler.nodes.LogicConstantNode)7