Search in sources :

Example 26 with Constant

use of jdk.vm.ci.meta.Constant in project graal by oracle.

the class HotSpotResolvedObjectTypeTest method testKlassLayoutHelper.

@Test
public void testKlassLayoutHelper() {
    Constant klass = HotSpotResolvedObjectType.fromObjectClass(this.getClass()).klass();
    MemoryAccessProvider memoryAccess = getProviders().getConstantReflection().getMemoryAccessProvider();
    GraalHotSpotVMConfig config = runtime().getVMConfig();
    Constant c = StampFactory.forKind(JavaKind.Int).readConstant(memoryAccess, klass, config.klassLayoutHelperOffset);
    assertTrue(c.toString(), c.getClass() == PrimitiveConstant.class);
    PrimitiveConstant pc = (PrimitiveConstant) c;
    assertTrue(pc.toString(), pc.getJavaKind() == JavaKind.Int);
}
Also used : MemoryAccessProvider(jdk.vm.ci.meta.MemoryAccessProvider) Constant(jdk.vm.ci.meta.Constant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) GraalHotSpotVMConfig(org.graalvm.compiler.hotspot.GraalHotSpotVMConfig) Test(org.junit.Test)

Example 27 with Constant

use of jdk.vm.ci.meta.Constant in project graal by oracle.

the class KlassLayoutHelperNode method canonical.

private static ValueNode canonical(KlassLayoutHelperNode klassLayoutHelperNode, GraalHotSpotVMConfig config, ValueNode klass, Stamp stamp, ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess) {
    KlassLayoutHelperNode self = klassLayoutHelperNode;
    if (klass.isConstant()) {
        if (!klass.asConstant().isDefaultForKind()) {
            Constant constant = stamp.readConstant(constantReflection.getMemoryAccessProvider(), klass.asConstant(), config.klassLayoutHelperOffset);
            return ConstantNode.forConstant(stamp, constant, metaAccess);
        }
    }
    if (klass instanceof LoadHubNode) {
        LoadHubNode hub = (LoadHubNode) klass;
        Stamp hubStamp = hub.getValue().stamp(NodeView.DEFAULT);
        if (hubStamp instanceof ObjectStamp) {
            ObjectStamp ostamp = (ObjectStamp) hubStamp;
            HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) ostamp.type();
            if (type != null && type.isArray() && !type.getComponentType().isPrimitive()) {
                // The layout for all object arrays is the same.
                Constant constant = stamp.readConstant(constantReflection.getMemoryAccessProvider(), type.klass(), config.klassLayoutHelperOffset);
                return ConstantNode.forConstant(stamp, constant, metaAccess);
            }
        }
    }
    if (self == null) {
        self = new KlassLayoutHelperNode(config, klass);
    }
    return self;
}
Also used : LoadHubNode(org.graalvm.compiler.nodes.extended.LoadHubNode) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) Constant(jdk.vm.ci.meta.Constant) HotSpotResolvedObjectType(jdk.vm.ci.hotspot.HotSpotResolvedObjectType)

Example 28 with Constant

use of jdk.vm.ci.meta.Constant in project graal by oracle.

the class TypeSwitchNode method simplify.

@Override
public void simplify(SimplifierTool tool) {
    NodeView view = NodeView.from(tool);
    if (value() instanceof ConstantNode) {
        Constant constant = value().asConstant();
        int survivingEdge = keySuccessorIndex(keyCount());
        for (int i = 0; i < keyCount(); i++) {
            Constant typeHub = keyAt(i);
            Boolean equal = tool.getConstantReflection().constantEquals(constant, typeHub);
            if (equal == null) {
                /* We don't know if this key is a match or not, so we cannot simplify. */
                return;
            } else if (equal.booleanValue()) {
                survivingEdge = keySuccessorIndex(i);
            }
        }
        killOtherSuccessors(tool, survivingEdge);
    }
    if (value() instanceof LoadHubNode && ((LoadHubNode) value()).getValue().stamp(view) instanceof ObjectStamp) {
        ObjectStamp objectStamp = (ObjectStamp) ((LoadHubNode) value()).getValue().stamp(view);
        if (objectStamp.type() != null) {
            int validKeys = 0;
            for (int i = 0; i < keyCount(); i++) {
                if (objectStamp.type().isAssignableFrom(keys[i])) {
                    validKeys++;
                }
            }
            if (validKeys == 0) {
                tool.addToWorkList(defaultSuccessor());
                graph().removeSplitPropagate(this, defaultSuccessor());
            } else if (validKeys != keys.length) {
                ArrayList<AbstractBeginNode> newSuccessors = new ArrayList<>(blockSuccessorCount());
                ResolvedJavaType[] newKeys = new ResolvedJavaType[validKeys];
                int[] newKeySuccessors = new int[validKeys + 1];
                double[] newKeyProbabilities = new double[validKeys + 1];
                double totalProbability = 0;
                int current = 0;
                for (int i = 0; i < keyCount() + 1; i++) {
                    if (i == keyCount() || objectStamp.type().isAssignableFrom(keys[i])) {
                        int index = newSuccessors.indexOf(keySuccessor(i));
                        if (index == -1) {
                            index = newSuccessors.size();
                            newSuccessors.add(keySuccessor(i));
                        }
                        newKeySuccessors[current] = index;
                        if (i < keyCount()) {
                            newKeys[current] = keys[i];
                        }
                        newKeyProbabilities[current] = keyProbability(i);
                        totalProbability += keyProbability(i);
                        current++;
                    }
                }
                if (totalProbability > 0) {
                    for (int i = 0; i < current; i++) {
                        newKeyProbabilities[i] /= totalProbability;
                    }
                } else {
                    for (int i = 0; i < current; i++) {
                        newKeyProbabilities[i] = 1.0 / current;
                    }
                }
                for (int i = 0; i < blockSuccessorCount(); i++) {
                    AbstractBeginNode successor = blockSuccessor(i);
                    if (!newSuccessors.contains(successor)) {
                        tool.deleteBranch(successor);
                    }
                    setBlockSuccessor(i, null);
                }
                AbstractBeginNode[] successorsArray = newSuccessors.toArray(new AbstractBeginNode[newSuccessors.size()]);
                TypeSwitchNode newSwitch = graph().add(new TypeSwitchNode(value(), successorsArray, newKeys, newKeyProbabilities, newKeySuccessors, tool.getConstantReflection()));
                ((FixedWithNextNode) predecessor()).setNext(newSwitch);
                GraphUtil.killWithUnusedFloatingInputs(this);
            }
        }
    }
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) LoadHubNode(org.graalvm.compiler.nodes.extended.LoadHubNode) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) Constant(jdk.vm.ci.meta.Constant) ArrayList(java.util.ArrayList) NodeView(org.graalvm.compiler.nodes.NodeView) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode)

Example 29 with Constant

use of jdk.vm.ci.meta.Constant in project graal by oracle.

the class ClassCastBytecodeExceptionTest method registerInvocationPlugins.

@Override
protected void registerInvocationPlugins(InvocationPlugins invocationPlugins) {
    invocationPlugins.register(new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode obj, ValueNode classNode) {
            ResolvedJavaType type = b.getConstantReflection().asJavaType(classNode.asConstant());
            Constant hub = b.getConstantReflection().asObjectHub(type);
            Stamp hubStamp = b.getStampProvider().createHubStamp(StampFactory.object(TypeReference.createExactTrusted(type)));
            ConstantNode hubConst = b.add(ConstantNode.forConstant(hubStamp, hub, b.getMetaAccess()));
            return throwBytecodeException(b, ClassCastException.class, obj, hubConst);
        }
    }, Exceptions.class, "throwClassCast", Object.class, Class.class);
    super.registerInvocationPlugins(invocationPlugins);
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) Stamp(org.graalvm.compiler.core.common.type.Stamp) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Constant(jdk.vm.ci.meta.Constant) ValueNode(org.graalvm.compiler.nodes.ValueNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Example 30 with Constant

use of jdk.vm.ci.meta.Constant in project graal by oracle.

the class IfNode method checkForUnsignedCompare.

/**
 * Recognize a couple patterns that can be merged into an unsigned compare.
 *
 * @param tool
 * @return true if a replacement was done.
 */
private boolean checkForUnsignedCompare(SimplifierTool tool) {
    assert trueSuccessor().hasNoUsages() && falseSuccessor().hasNoUsages();
    if (condition() instanceof IntegerLessThanNode) {
        NodeView view = NodeView.from(tool);
        IntegerLessThanNode lessThan = (IntegerLessThanNode) condition();
        Constant y = lessThan.getY().stamp(view).asConstant();
        if (y instanceof PrimitiveConstant && ((PrimitiveConstant) y).asLong() == 0 && falseSuccessor().next() instanceof IfNode) {
            IfNode ifNode2 = (IfNode) falseSuccessor().next();
            if (ifNode2.condition() instanceof IntegerLessThanNode) {
                IntegerLessThanNode lessThan2 = (IntegerLessThanNode) ifNode2.condition();
                AbstractBeginNode falseSucc = ifNode2.falseSuccessor();
                AbstractBeginNode trueSucc = ifNode2.trueSuccessor();
                IntegerBelowNode below = null;
                /*
                     * Convert x >= 0 && x < positive which is represented as !(x < 0) && x <
                     * <positive> into an unsigned compare.
                     */
                if (lessThan2.getX() == lessThan.getX() && lessThan2.getY().stamp(view) instanceof IntegerStamp && ((IntegerStamp) lessThan2.getY().stamp(view)).isPositive() && sameDestination(trueSuccessor(), ifNode2.falseSuccessor)) {
                    below = graph().unique(new IntegerBelowNode(lessThan2.getX(), lessThan2.getY()));
                    // swap direction
                    AbstractBeginNode tmp = falseSucc;
                    falseSucc = trueSucc;
                    trueSucc = tmp;
                } else if (lessThan2.getY() == lessThan.getX() && sameDestination(trueSuccessor(), ifNode2.trueSuccessor)) {
                    /*
                         * Convert x >= 0 && x <= positive which is represented as !(x < 0) &&
                         * !(<positive> > x), into x <| positive + 1. This can only be done for
                         * constants since there isn't a IntegerBelowEqualThanNode but that doesn't
                         * appear to be interesting.
                         */
                    JavaConstant positive = lessThan2.getX().asJavaConstant();
                    if (positive != null && positive.asLong() > 0 && positive.asLong() < positive.getJavaKind().getMaxValue()) {
                        ConstantNode newLimit = ConstantNode.forIntegerStamp(lessThan2.getX().stamp(view), positive.asLong() + 1, graph());
                        below = graph().unique(new IntegerBelowNode(lessThan.getX(), newLimit));
                    }
                }
                if (below != null) {
                    ifNode2.setTrueSuccessor(null);
                    ifNode2.setFalseSuccessor(null);
                    IfNode newIfNode = graph().add(new IfNode(below, falseSucc, trueSucc, 1 - trueSuccessorProbability));
                    // Remove the < 0 test.
                    tool.deleteBranch(trueSuccessor);
                    graph().removeSplit(this, falseSuccessor);
                    // Replace the second test with the new one.
                    ifNode2.predecessor().replaceFirstSuccessor(ifNode2, newIfNode);
                    ifNode2.safeDelete();
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) Constant(jdk.vm.ci.meta.Constant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) JavaConstant(jdk.vm.ci.meta.JavaConstant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode) JavaConstant(jdk.vm.ci.meta.JavaConstant)

Aggregations

Constant (jdk.vm.ci.meta.Constant)31 JavaConstant (jdk.vm.ci.meta.JavaConstant)15 PrimitiveConstant (jdk.vm.ci.meta.PrimitiveConstant)11 ValueNode (org.graalvm.compiler.nodes.ValueNode)9 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)6 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)5 Stamp (org.graalvm.compiler.core.common.type.Stamp)5 ArrayList (java.util.ArrayList)4 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)4 AllocatableValue (jdk.vm.ci.meta.AllocatableValue)3 LIRValueUtil.asJavaConstant (org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant)3 LIRValueUtil.isJavaConstant (org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant)3 RegisterValue (jdk.vm.ci.code.RegisterValue)2 HotSpotCompressedNullConstant (jdk.vm.ci.hotspot.HotSpotCompressedNullConstant)2 HotSpotObjectConstant (jdk.vm.ci.hotspot.HotSpotObjectConstant)2 MetaAccessProvider (jdk.vm.ci.meta.MetaAccessProvider)2 SerializableConstant (jdk.vm.ci.meta.SerializableConstant)2 VMConstant (jdk.vm.ci.meta.VMConstant)2 Value (jdk.vm.ci.meta.Value)2 OperandSize (org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize)2