Search in sources :

Example 1 with RightShiftNode

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

the class StandardGraphBuilderPlugins method registerCharacterPlugins.

private static void registerCharacterPlugins(InvocationPlugins plugins) {
    Registration r = new Registration(plugins, Character.class);
    r.register1("reverseBytes", char.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
            // return (char) (Integer.reverse(i) >> 16);
            ReverseBytesNode reverse = b.add(new ReverseBytesNode(value));
            RightShiftNode rightShift = b.add(new RightShiftNode(reverse, b.add(ConstantNode.forInt(16))));
            ZeroExtendNode charCast = b.add(new ZeroExtendNode(b.add(new NarrowNode(rightShift, 16)), 32));
            b.push(JavaKind.Char, b.append(charCast.canonical(null)));
            return true;
        }
    });
}
Also used : GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) ValueNode(org.graalvm.compiler.nodes.ValueNode) RightShiftNode(org.graalvm.compiler.nodes.calc.RightShiftNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) ReverseBytesNode(org.graalvm.compiler.replacements.nodes.ReverseBytesNode) NarrowNode(org.graalvm.compiler.nodes.calc.NarrowNode) ZeroExtendNode(org.graalvm.compiler.nodes.calc.ZeroExtendNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 2 with RightShiftNode

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

the class DefaultJavaLoweringProvider method reconstructArrayIndex.

@Override
public ValueNode reconstructArrayIndex(JavaKind elementKind, AddressNode address) {
    StructuredGraph graph = address.graph();
    ValueNode offset = ((OffsetAddressNode) address).getOffset();
    int base = arrayBaseOffset(elementKind);
    ValueNode scaledIndex = graph.unique(new SubNode(offset, ConstantNode.forIntegerStamp(offset.stamp(NodeView.DEFAULT), base, graph)));
    int shift = CodeUtil.log2(arrayScalingFactor(elementKind));
    ValueNode ret = graph.unique(new RightShiftNode(scaledIndex, ConstantNode.forInt(shift, graph)));
    return IntegerConvertNode.convert(ret, StampFactory.forKind(JavaKind.Int), graph, NodeView.DEFAULT);
}
Also used : StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) OffsetAddressNode(org.graalvm.compiler.nodes.memory.address.OffsetAddressNode) SubNode(org.graalvm.compiler.nodes.calc.SubNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) RightShiftNode(org.graalvm.compiler.nodes.calc.RightShiftNode)

Example 3 with RightShiftNode

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

the class CInterfaceInvocationPlugin method replaceBitfieldAccessor.

private boolean replaceBitfieldAccessor(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args, StructBitfieldInfo bitfieldInfo, AccessorInfo accessorInfo) {
    int byteOffset = bitfieldInfo.getByteOffsetInfo().getProperty();
    int startBit = bitfieldInfo.getStartBitInfo().getProperty();
    int endBit = bitfieldInfo.getEndBitInfo().getProperty();
    boolean isUnsigned = bitfieldInfo.isUnsigned();
    assert byteOffset >= 0 && byteOffset < ((SizableInfo) bitfieldInfo.getParent()).getSizeInfo().getProperty();
    assert startBit >= 0 && startBit < 8;
    assert endBit >= startBit && endBit < 64;
    /*
         * The startBit is always in the first byte. Therefore, the endBit tells us how many bytes
         * we actually have to read and write.
         */
    JavaKind memoryKind;
    if (endBit < 8) {
        memoryKind = JavaKind.Byte;
    } else if (endBit < 16) {
        memoryKind = JavaKind.Short;
    } else if (endBit < 32) {
        memoryKind = JavaKind.Int;
    } else {
        memoryKind = JavaKind.Long;
    }
    int numBytes = memoryKind.getByteCount();
    /*
         * Try to align the byteOffset to be a multiple of numBytes. That should always be possible,
         * but we don't trust the C compiler and memory layout enough to make it an assertion.
         */
    int alignmentCorrection = byteOffset % numBytes;
    if (alignmentCorrection > 0 && endBit + alignmentCorrection * 8 < numBytes * 8) {
        byteOffset -= alignmentCorrection;
        startBit += alignmentCorrection * 8;
        endBit += alignmentCorrection * 8;
    }
    assert byteOffset >= 0 && byteOffset < ((SizableInfo) bitfieldInfo.getParent()).getSizeInfo().getProperty();
    assert startBit >= 0 && startBit < numBytes * 8;
    assert endBit >= startBit && endBit < numBytes * 8;
    int numBits = endBit - startBit + 1;
    assert numBits > 0 && numBits <= numBytes * 8;
    /*
         * The bit-operations on the value are either performed on Int or Long. We do not perform 8
         * or 16 bit arithmetic operations.
         */
    JavaKind computeKind = memoryKind.getStackKind();
    Stamp computeStamp = StampFactory.forKind(computeKind);
    int computeBits = computeKind.getBitCount();
    assert startBit >= 0 && startBit < computeBits;
    assert endBit >= startBit && endBit < computeBits;
    assert computeBits >= numBits;
    assert args.length == accessorInfo.parameterCount(true);
    ValueNode base = args[accessorInfo.baseParameterNumber(true)];
    StructuredGraph graph = b.getGraph();
    /*
         * Read the memory location. This is also necessary for writes, since we need to keep the
         * bits around the written bitfield unchanged.
         */
    ValueNode address = makeAddress(graph, args, accessorInfo, base, byteOffset, -1);
    LocationIdentity locationIdentity = makeLocationIdentity(b, method, args, accessorInfo);
    Stamp stamp = StampFactory.forInteger(memoryKind.getBitCount());
    ValueNode cur = readOp(b, address, locationIdentity, stamp, accessorInfo);
    cur = adaptPrimitiveType(graph, cur, memoryKind, computeKind, true);
    switch(accessorInfo.getAccessorKind()) {
        case GETTER:
            {
                if (isUnsigned) {
                    /*
                     * Unsigned reads: shift the bitfield to the right and mask out the unnecessary
                     * high-order bits.
                     */
                    cur = graph.unique(new RightShiftNode(cur, ConstantNode.forInt(startBit, graph)));
                    cur = graph.unique(new AndNode(cur, ConstantNode.forIntegerStamp(computeStamp, (1L << numBits) - 1, graph)));
                } else {
                    /*
                     * Signed reads: shift the bitfield to the right end to get the sign bit in
                     * place, then do a signed left shift to have a proper sign extension.
                     */
                    cur = graph.unique(new LeftShiftNode(cur, ConstantNode.forInt(computeBits - endBit - 1, graph)));
                    cur = graph.unique(new RightShiftNode(cur, ConstantNode.forInt(computeBits - numBits, graph)));
                }
                JavaKind resultKind = wordTypes.asKind(b.getInvokeReturnType());
                b.push(pushKind(method), adaptPrimitiveType(graph, cur, computeKind, resultKind == JavaKind.Boolean ? resultKind : resultKind.getStackKind(), isUnsigned));
                return true;
            }
        case SETTER:
            {
                /* Zero out the bits of our bitfields, i.e., the bits we are going to change. */
                long mask = ~(((1L << numBits) - 1) << startBit);
                cur = graph.unique(new AndNode(cur, ConstantNode.forIntegerStamp(computeStamp, mask, graph)));
                /*
                 * Mask the unnecessary high-order bits of the value to be written, and shift it to
                 * its place.
                 */
                ValueNode value = args[accessorInfo.valueParameterNumber(true)];
                value = adaptPrimitiveType(graph, value, value.getStackKind(), computeKind, isUnsigned);
                value = graph.unique(new AndNode(value, ConstantNode.forIntegerStamp(computeStamp, (1L << numBits) - 1, graph)));
                value = graph.unique(new LeftShiftNode(value, ConstantNode.forInt(startBit, graph)));
                /* Combine the leftover bits of the original memory word with the new value. */
                cur = graph.unique(new OrNode(cur, value));
                /* Narrow value to the number of bits we need to write. */
                cur = adaptPrimitiveType(graph, cur, computeKind, memoryKind, true);
                /* Perform the write (bitcount is taken from the stamp of the written value). */
                writeOp(b, address, locationIdentity, cur, accessorInfo);
                return true;
            }
        default:
            throw shouldNotReachHere();
    }
}
Also used : Stamp(org.graalvm.compiler.core.common.type.Stamp) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ValueNode(org.graalvm.compiler.nodes.ValueNode) RightShiftNode(org.graalvm.compiler.nodes.calc.RightShiftNode) LocationIdentity(org.graalvm.word.LocationIdentity) CInterfaceLocationIdentity(com.oracle.svm.core.c.struct.CInterfaceLocationIdentity) AndNode(org.graalvm.compiler.nodes.calc.AndNode) LeftShiftNode(org.graalvm.compiler.nodes.calc.LeftShiftNode) JavaKind(jdk.vm.ci.meta.JavaKind) OrNode(org.graalvm.compiler.nodes.calc.OrNode)

Example 4 with RightShiftNode

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

the class StandardGraphBuilderPlugins method registerShortPlugins.

private static void registerShortPlugins(InvocationPlugins plugins) {
    Registration r = new Registration(plugins, Short.class);
    r.register1("reverseBytes", short.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value) {
            // return (short) (Integer.reverse(i) >> 16);
            ReverseBytesNode reverse = b.add(new ReverseBytesNode(value));
            RightShiftNode rightShift = b.add(new RightShiftNode(reverse, b.add(ConstantNode.forInt(16))));
            SignExtendNode charCast = b.add(new SignExtendNode(b.add(new NarrowNode(rightShift, 16)), 32));
            b.push(JavaKind.Short, b.append(charCast.canonical(null)));
            return true;
        }
    });
}
Also used : SignExtendNode(org.graalvm.compiler.nodes.calc.SignExtendNode) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) ValueNode(org.graalvm.compiler.nodes.ValueNode) RightShiftNode(org.graalvm.compiler.nodes.calc.RightShiftNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) ReverseBytesNode(org.graalvm.compiler.replacements.nodes.ReverseBytesNode) NarrowNode(org.graalvm.compiler.nodes.calc.NarrowNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

ValueNode (org.graalvm.compiler.nodes.ValueNode)4 RightShiftNode (org.graalvm.compiler.nodes.calc.RightShiftNode)4 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)2 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)2 NarrowNode (org.graalvm.compiler.nodes.calc.NarrowNode)2 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)2 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)2 Receiver (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver)2 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)2 ReverseBytesNode (org.graalvm.compiler.replacements.nodes.ReverseBytesNode)2 CInterfaceLocationIdentity (com.oracle.svm.core.c.struct.CInterfaceLocationIdentity)1 JavaKind (jdk.vm.ci.meta.JavaKind)1 Stamp (org.graalvm.compiler.core.common.type.Stamp)1 AndNode (org.graalvm.compiler.nodes.calc.AndNode)1 LeftShiftNode (org.graalvm.compiler.nodes.calc.LeftShiftNode)1 OrNode (org.graalvm.compiler.nodes.calc.OrNode)1 SignExtendNode (org.graalvm.compiler.nodes.calc.SignExtendNode)1 SubNode (org.graalvm.compiler.nodes.calc.SubNode)1 ZeroExtendNode (org.graalvm.compiler.nodes.calc.ZeroExtendNode)1 OffsetAddressNode (org.graalvm.compiler.nodes.memory.address.OffsetAddressNode)1