001/*
002 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.nodes.arithmetic;
024
025import java.util.function.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.lir.gen.*;
033import com.oracle.graal.nodeinfo.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.calc.*;
036import com.oracle.graal.nodes.spi.*;
037
038@NodeInfo(shortName = "*H")
039public final class IntegerMulHighNode extends BinaryNode implements ArithmeticLIRLowerable {
040    public static final NodeClass<IntegerMulHighNode> TYPE = NodeClass.create(IntegerMulHighNode.class);
041
042    public IntegerMulHighNode(ValueNode x, ValueNode y) {
043        this((IntegerStamp) x.stamp().unrestricted(), x, y);
044    }
045
046    public IntegerMulHighNode(IntegerStamp stamp, ValueNode x, ValueNode y) {
047        super(TYPE, stamp, x, y);
048    }
049
050    /**
051     * Determines the minimum and maximum result of this node for the given inputs and returns the
052     * result of the given BiFunction on the minimum and maximum values.
053     */
054    private <T> T processExtremes(ValueNode forX, ValueNode forY, BiFunction<Long, Long, T> op) {
055        IntegerStamp xStamp = (IntegerStamp) forX.stamp();
056        IntegerStamp yStamp = (IntegerStamp) forY.stamp();
057
058        Kind kind = getKind();
059        assert kind == Kind.Int || kind == Kind.Long;
060        long[] xExtremes = {xStamp.lowerBound(), xStamp.upperBound()};
061        long[] yExtremes = {yStamp.lowerBound(), yStamp.upperBound()};
062        long min = Long.MAX_VALUE;
063        long max = Long.MIN_VALUE;
064        for (long a : xExtremes) {
065            for (long b : yExtremes) {
066                long result = kind == Kind.Int ? multiplyHigh((int) a, (int) b) : multiplyHigh(a, b);
067                min = Math.min(min, result);
068                max = Math.max(max, result);
069            }
070        }
071        return op.apply(min, max);
072    }
073
074    @Override
075    public boolean inferStamp() {
076        return updateStamp(processExtremes(getX(), getY(), (min, max) -> StampFactory.forInteger(getKind(), min, max)));
077    }
078
079    @SuppressWarnings("cast")
080    @Override
081    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
082        return processExtremes(forX, forY, (min, max) -> min == (long) max ? ConstantNode.forIntegerKind(getKind(), min) : this);
083    }
084
085    @Override
086    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
087        Value a = nodeValueMap.operand(getX());
088        Value b = nodeValueMap.operand(getY());
089        nodeValueMap.setResult(this, gen.emitMulHigh(a, b));
090    }
091
092    public static int multiplyHigh(int x, int y) {
093        long r = (long) x * (long) y;
094        return (int) (r >> 32);
095    }
096
097    public static long multiplyHigh(long x, long y) {
098        // Checkstyle: stop
099        long x0, y0, z0;
100        long x1, y1, z1, z2, t;
101        // Checkstyle: resume
102
103        x0 = x & 0xFFFFFFFFL;
104        x1 = x >> 32;
105
106        y0 = y & 0xFFFFFFFFL;
107        y1 = y >> 32;
108
109        z0 = x0 * y0;
110        t = x1 * y0 + (z0 >>> 32);
111        z1 = t & 0xFFFFFFFFL;
112        z2 = t >> 32;
113        z1 += x0 * y1;
114
115        return x1 * y1 + z2 + (z1 >> 32);
116    }
117}