Search in sources :

Example 6 with Constant

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

the class ReadNode method canonicalizeRead.

public static ValueNode canonicalizeRead(ValueNode read, AddressNode address, LocationIdentity locationIdentity, CanonicalizerTool tool) {
    NodeView view = NodeView.from(tool);
    MetaAccessProvider metaAccess = tool.getMetaAccess();
    if (tool.canonicalizeReads() && address instanceof OffsetAddressNode) {
        OffsetAddressNode objAddress = (OffsetAddressNode) address;
        ValueNode object = objAddress.getBase();
        if (metaAccess != null && object.isConstant() && !object.isNullConstant() && objAddress.getOffset().isConstant()) {
            long displacement = objAddress.getOffset().asJavaConstant().asLong();
            int stableDimension = ((ConstantNode) object).getStableDimension();
            if (locationIdentity.isImmutable() || stableDimension > 0) {
                Constant constant = read.stamp(view).readConstant(tool.getConstantReflection().getMemoryAccessProvider(), object.asConstant(), displacement);
                boolean isDefaultStable = locationIdentity.isImmutable() || ((ConstantNode) object).isDefaultStable();
                if (constant != null && (isDefaultStable || !constant.isDefaultForKind())) {
                    return ConstantNode.forConstant(read.stamp(view), constant, Math.max(stableDimension - 1, 0), isDefaultStable, metaAccess);
                }
            }
        }
        if (locationIdentity.equals(ARRAY_LENGTH_LOCATION)) {
            ValueNode length = GraphUtil.arrayLength(object);
            if (length != null) {
                return length;
            }
        }
        if (locationIdentity instanceof CanonicalizableLocation) {
            CanonicalizableLocation canonicalize = (CanonicalizableLocation) locationIdentity;
            ValueNode result = canonicalize.canonicalizeRead(read, address, object, tool);
            assert result != null && result.stamp(view).isCompatible(read.stamp(view));
            return result;
        }
    }
    return read;
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) OffsetAddressNode(org.graalvm.compiler.nodes.memory.address.OffsetAddressNode) Constant(jdk.vm.ci.meta.Constant) ValueNode(org.graalvm.compiler.nodes.ValueNode) NodeView(org.graalvm.compiler.nodes.NodeView) CanonicalizableLocation(org.graalvm.compiler.nodes.CanonicalizableLocation) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider)

Example 7 with Constant

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

the class AMD64ArithmeticLIRGenerator method emitStoreConst.

protected void emitStoreConst(AMD64Kind kind, AMD64AddressValue address, ConstantValue value, LIRFrameState state) {
    Constant c = value.getConstant();
    if (JavaConstant.isNull(c)) {
        assert kind == AMD64Kind.DWORD || kind == AMD64Kind.QWORD;
        OperandSize size = kind == AMD64Kind.DWORD ? DWORD : QWORD;
        getLIRGen().append(new AMD64BinaryConsumer.MemoryConstOp(AMD64MIOp.MOV, size, address, 0, state));
        return;
    } else if (c instanceof VMConstant) {
        // only 32-bit constants can be patched
        if (kind == AMD64Kind.DWORD) {
            if (getLIRGen().target().inlineObjects || !(c instanceof JavaConstant)) {
                // if c is a JavaConstant, it's an oop, otherwise it's a metaspace constant
                assert !(c instanceof JavaConstant) || ((JavaConstant) c).getJavaKind() == JavaKind.Object;
                getLIRGen().append(new AMD64BinaryConsumer.MemoryVMConstOp(AMD64MIOp.MOV, address, (VMConstant) c, state));
                return;
            }
        }
    } else {
        JavaConstant jc = (JavaConstant) c;
        assert jc.getJavaKind().isPrimitive();
        AMD64MIOp op = AMD64MIOp.MOV;
        OperandSize size;
        long imm;
        switch(kind) {
            case BYTE:
                op = AMD64MIOp.MOVB;
                size = BYTE;
                imm = jc.asInt();
                break;
            case WORD:
                size = WORD;
                imm = jc.asInt();
                break;
            case DWORD:
                size = DWORD;
                imm = jc.asInt();
                break;
            case QWORD:
                size = QWORD;
                imm = jc.asLong();
                break;
            case SINGLE:
                size = DWORD;
                imm = Float.floatToRawIntBits(jc.asFloat());
                break;
            case DOUBLE:
                size = QWORD;
                imm = Double.doubleToRawLongBits(jc.asDouble());
                break;
            default:
                throw GraalError.shouldNotReachHere("unexpected kind " + kind);
        }
        if (NumUtil.isInt(imm)) {
            getLIRGen().append(new AMD64BinaryConsumer.MemoryConstOp(op, size, address, (int) imm, state));
            return;
        }
    }
    // fallback: load, then store
    emitStore(kind, address, getLIRGen().asAllocatable(value), state);
}
Also used : Constant(jdk.vm.ci.meta.Constant) JavaConstant(jdk.vm.ci.meta.JavaConstant) LIRValueUtil.asJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant) LIRValueUtil.isJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant) VMConstant(jdk.vm.ci.meta.VMConstant) VMConstant(jdk.vm.ci.meta.VMConstant) JavaConstant(jdk.vm.ci.meta.JavaConstant) LIRValueUtil.asJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant) LIRValueUtil.isJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant) AMD64MIOp(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64MIOp) AMD64BinaryConsumer(org.graalvm.compiler.lir.amd64.AMD64BinaryConsumer) OperandSize(org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize)

Example 8 with Constant

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

the class AMD64ArithmeticLIRGenerator method emitCompareOp.

@Override
public void emitCompareOp(AMD64Kind cmpKind, Variable left, Value right) {
    OperandSize size;
    switch(cmpKind) {
        case BYTE:
            size = BYTE;
            break;
        case WORD:
            size = WORD;
            break;
        case DWORD:
            size = DWORD;
            break;
        case QWORD:
            size = QWORD;
            break;
        case SINGLE:
            getLIRGen().append(new AMD64BinaryConsumer.Op(SSEOp.UCOMIS, PS, left, getLIRGen().asAllocatable(right)));
            return;
        case DOUBLE:
            getLIRGen().append(new AMD64BinaryConsumer.Op(SSEOp.UCOMIS, PD, left, getLIRGen().asAllocatable(right)));
            return;
        default:
            throw GraalError.shouldNotReachHere("unexpected kind: " + cmpKind);
    }
    if (isConstantValue(right)) {
        Constant c = LIRValueUtil.asConstant(right);
        if (JavaConstant.isNull(c)) {
            getLIRGen().append(new AMD64BinaryConsumer.Op(TEST, size, left, left));
            return;
        } else if (c instanceof VMConstant) {
            VMConstant vc = (VMConstant) c;
            if (size == DWORD && !GeneratePIC.getValue(getOptions())) {
                getLIRGen().append(new AMD64BinaryConsumer.VMConstOp(CMP.getMIOpcode(DWORD, false), left, vc));
            } else {
                getLIRGen().append(new AMD64BinaryConsumer.DataOp(CMP.getRMOpcode(size), size, left, vc));
            }
            return;
        } else if (c instanceof JavaConstant) {
            JavaConstant jc = (JavaConstant) c;
            if (jc.isDefaultForKind()) {
                AMD64RMOp op = size == BYTE ? TESTB : TEST;
                getLIRGen().append(new AMD64BinaryConsumer.Op(op, size, left, left));
                return;
            } else if (NumUtil.is32bit(jc.asLong())) {
                getLIRGen().append(new AMD64BinaryConsumer.ConstOp(CMP, size, left, (int) jc.asLong()));
                return;
            }
        }
    }
    // fallback: load, then compare
    getLIRGen().append(new AMD64BinaryConsumer.Op(CMP.getRMOpcode(size), size, left, getLIRGen().asAllocatable(right)));
}
Also used : Constant(jdk.vm.ci.meta.Constant) JavaConstant(jdk.vm.ci.meta.JavaConstant) LIRValueUtil.asJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant) LIRValueUtil.isJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant) VMConstant(jdk.vm.ci.meta.VMConstant) VMConstant(jdk.vm.ci.meta.VMConstant) AMD64RMOp(org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64RMOp) JavaConstant(jdk.vm.ci.meta.JavaConstant) LIRValueUtil.asJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant) LIRValueUtil.isJavaConstant(org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant) AMD64BinaryConsumer(org.graalvm.compiler.lir.amd64.AMD64BinaryConsumer) OperandSize(org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize)

Example 9 with Constant

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

the class SubNode method canonical.

private static ValueNode canonical(SubNode subNode, BinaryOp<Sub> op, Stamp stamp, ValueNode forX, ValueNode forY, NodeView view) {
    SubNode self = subNode;
    if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
        Constant zero = op.getZero(forX.stamp(view));
        if (zero != null) {
            return ConstantNode.forPrimitive(stamp, zero);
        }
    }
    boolean associative = op.isAssociative();
    if (associative) {
        if (forX instanceof AddNode) {
            AddNode x = (AddNode) forX;
            if (x.getY() == forY) {
                // (a + b) - b
                return x.getX();
            }
            if (x.getX() == forY) {
                // (a + b) - a
                return x.getY();
            }
        } else if (forX instanceof SubNode) {
            SubNode x = (SubNode) forX;
            if (x.getX() == forY) {
                // (a - b) - a
                return NegateNode.create(x.getY(), view);
            }
        }
        if (forY instanceof AddNode) {
            AddNode y = (AddNode) forY;
            if (y.getX() == forX) {
                // a - (a + b)
                return NegateNode.create(y.getY(), view);
            }
            if (y.getY() == forX) {
                // b - (a + b)
                return NegateNode.create(y.getX(), view);
            }
        } else if (forY instanceof SubNode) {
            SubNode y = (SubNode) forY;
            if (y.getX() == forX) {
                // a - (a - b)
                return y.getY();
            }
        }
    }
    if (forY.isConstant()) {
        Constant c = forY.asConstant();
        if (op.isNeutral(c)) {
            return forX;
        }
        if (associative && self != null) {
            ValueNode reassociated = reassociate(self, ValueNode.isConstantPredicate(), forX, forY, view);
            if (reassociated != self) {
                return reassociated;
            }
        }
        if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) {
            long i = ((PrimitiveConstant) c).asLong();
            if (i < 0 || ((IntegerStamp) StampFactory.forKind(forY.getStackKind())).contains(-i)) {
                // commutative, so prefer add when it fits.
                return BinaryArithmeticNode.add(forX, ConstantNode.forIntegerStamp(stamp, -i), view);
            }
        }
    } else if (forX.isConstant()) {
        Constant c = forX.asConstant();
        if (ArithmeticOpTable.forStamp(stamp).getAdd().isNeutral(c)) {
            /*
                 * Note that for floating point numbers, + and - have different neutral elements. We
                 * have to test for the neutral element of +, because we are doing this
                 * transformation: 0 - x == (-x) + 0 == -x.
                 */
            return NegateNode.create(forY, view);
        }
        if (associative && self != null) {
            return reassociate(self, ValueNode.isConstantPredicate(), forX, forY, view);
        }
    }
    if (forY instanceof NegateNode) {
        return BinaryArithmeticNode.add(forX, ((NegateNode) forY).getValue(), view);
    }
    return self != null ? self : new SubNode(forX, forY);
}
Also used : Constant(jdk.vm.ci.meta.Constant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) ValueNode(org.graalvm.compiler.nodes.ValueNode)

Example 10 with Constant

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

the class OrNode method canonical.

private static ValueNode canonical(OrNode self, BinaryOp<Or> op, Stamp stamp, ValueNode forX, ValueNode forY, NodeView view) {
    if (GraphUtil.unproxify(forX) == GraphUtil.unproxify(forY)) {
        return forX;
    }
    if (forX.isConstant() && !forY.isConstant()) {
        return new OrNode(forY, forX);
    }
    if (forY.isConstant()) {
        Constant c = forY.asConstant();
        if (op.isNeutral(c)) {
            return forX;
        }
        if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getJavaKind().isNumericInteger()) {
            long rawY = ((PrimitiveConstant) c).asLong();
            long mask = CodeUtil.mask(PrimitiveStamp.getBits(stamp));
            if ((rawY & mask) == mask) {
                return ConstantNode.forIntegerStamp(stamp, mask);
            }
        }
        return reassociate(self != null ? self : (OrNode) new OrNode(forX, forY).maybeCommuteInputs(), ValueNode.isConstantPredicate(), forX, forY, view);
    }
    if (forX instanceof NotNode && forY instanceof NotNode) {
        return new NotNode(AndNode.create(((NotNode) forX).getValue(), ((NotNode) forY).getValue(), view));
    }
    return self != null ? self : new OrNode(forX, forY).maybeCommuteInputs();
}
Also used : Constant(jdk.vm.ci.meta.Constant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant) PrimitiveConstant(jdk.vm.ci.meta.PrimitiveConstant)

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