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 UnsignedMulHighNode extends BinaryNode implements ArithmeticLIRLowerable {
040
041    public static final NodeClass<UnsignedMulHighNode> TYPE = NodeClass.create(UnsignedMulHighNode.class);
042
043    public UnsignedMulHighNode(ValueNode x, ValueNode y) {
044        this((IntegerStamp) x.stamp().unrestricted(), x, y);
045    }
046
047    public UnsignedMulHighNode(IntegerStamp stamp, ValueNode x, ValueNode y) {
048        super(TYPE, stamp, x, y);
049    }
050
051    /**
052     * Determines the minimum and maximum result of this node for the given inputs and returns the
053     * result of the given BiFunction on the minimum and maximum values. Note that the minima and
054     * maxima are calculated using signed min/max functions, while the values themselves are
055     * unsigned.
056     */
057    private <T> T processExtremes(ValueNode forX, ValueNode forY, BiFunction<Long, Long, T> op) {
058        IntegerStamp xStamp = (IntegerStamp) forX.stamp();
059        IntegerStamp yStamp = (IntegerStamp) forY.stamp();
060
061        Kind kind = getKind();
062        assert kind == Kind.Int || kind == Kind.Long;
063        long[] xExtremes = {xStamp.lowerBound(), xStamp.upperBound()};
064        long[] yExtremes = {yStamp.lowerBound(), yStamp.upperBound()};
065        long min = Long.MAX_VALUE;
066        long max = Long.MIN_VALUE;
067        for (long a : xExtremes) {
068            for (long b : yExtremes) {
069                long result = kind == Kind.Int ? multiplyHighUnsigned((int) a, (int) b) : multiplyHighUnsigned(a, b);
070                min = Math.min(min, result);
071                max = Math.max(max, result);
072            }
073        }
074        return op.apply(min, max);
075    }
076
077    @SuppressWarnings("cast")
078    @Override
079    public boolean inferStamp() {
080        // if min is negative, then the value can reach into the unsigned range
081        return updateStamp(processExtremes(getX(), getY(), (min, max) -> (min == (long) max || min >= 0) ? StampFactory.forInteger(getKind(), min, max) : StampFactory.forKind(getKind())));
082    }
083
084    @SuppressWarnings("cast")
085    @Override
086    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
087        return processExtremes(forX, forY, (min, max) -> min == (long) max ? ConstantNode.forIntegerKind(getKind(), min) : this);
088    }
089
090    @Override
091    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
092        Value a = nodeValueMap.operand(getX());
093        Value b = nodeValueMap.operand(getY());
094        nodeValueMap.setResult(this, gen.emitUMulHigh(a, b));
095    }
096
097    public static int multiplyHighUnsigned(int x, int y) {
098        long xl = x & 0xFFFFFFFFL;
099        long yl = y & 0xFFFFFFFFL;
100        long r = xl * yl;
101        return (int) (r >> 32);
102    }
103
104    public static long multiplyHighUnsigned(long x, long y) {
105        // Checkstyle: stop
106        long x0, y0, z0;
107        long x1, y1, z1, z2, t;
108        // Checkstyle: resume
109
110        x0 = x & 0xFFFFFFFFL;
111        x1 = x >>> 32;
112
113        y0 = y & 0xFFFFFFFFL;
114        y1 = y >>> 32;
115
116        z0 = x0 * y0;
117        t = x1 * y0 + (z0 >>> 32);
118        z1 = t & 0xFFFFFFFFL;
119        z2 = t >>> 32;
120        z1 += x0 * y1;
121
122        return x1 * y1 + z2 + (z1 >>> 32);
123    }
124}