1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.replacements.nodes.arithmetic;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
  27 
  28 import java.util.function.BiFunction;
  29 
  30 import org.graalvm.compiler.core.common.type.IntegerStamp;
  31 import org.graalvm.compiler.core.common.type.Stamp;
  32 import org.graalvm.compiler.core.common.type.StampFactory;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.ConstantNode;
  38 import org.graalvm.compiler.nodes.ValueNode;
  39 import org.graalvm.compiler.nodes.calc.BinaryNode;
  40 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
  41 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  42 
  43 import jdk.vm.ci.meta.JavaKind;
  44 import jdk.vm.ci.meta.Value;
  45 
  46 @NodeInfo(shortName = "*H", cycles = CYCLES_2, size = SIZE_2)
  47 public final class IntegerMulHighNode extends BinaryNode implements ArithmeticLIRLowerable {
  48     public static final NodeClass<IntegerMulHighNode> TYPE = NodeClass.create(IntegerMulHighNode.class);
  49 
  50     public IntegerMulHighNode(ValueNode x, ValueNode y) {
  51         this((IntegerStamp) x.stamp().unrestricted(), x, y);
  52     }
  53 
  54     public IntegerMulHighNode(IntegerStamp stamp, ValueNode x, ValueNode y) {
  55         super(TYPE, stamp, x, y);
  56     }
  57 
  58     /**
  59      * Determines the minimum and maximum result of this node for the given inputs and returns the
  60      * result of the given BiFunction on the minimum and maximum values.
  61      */
  62     private <T> T processExtremes(Stamp forX, Stamp forY, BiFunction<Long, Long, T> op) {
  63         IntegerStamp xStamp = (IntegerStamp) forX;
  64         IntegerStamp yStamp = (IntegerStamp) forY;
  65 
  66         JavaKind kind = getStackKind();
  67         assert kind == JavaKind.Int || kind == JavaKind.Long;
  68         long[] xExtremes = {xStamp.lowerBound(), xStamp.upperBound()};
  69         long[] yExtremes = {yStamp.lowerBound(), yStamp.upperBound()};
  70         long min = Long.MAX_VALUE;
  71         long max = Long.MIN_VALUE;
  72         for (long a : xExtremes) {
  73             for (long b : yExtremes) {
  74                 long result = kind == JavaKind.Int ? multiplyHigh((int) a, (int) b) : multiplyHigh(a, b);
  75                 min = Math.min(min, result);
  76                 max = Math.max(max, result);
  77             }
  78         }
  79         return op.apply(min, max);
  80     }
  81 
  82     @Override
  83     public Stamp foldStamp(Stamp stampX, Stamp stampY) {
  84         return processExtremes(stampX, stampY, (min, max) -> StampFactory.forInteger(getStackKind(), min, max));
  85     }
  86 
  87     @SuppressWarnings("cast")
  88     @Override
  89     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
  90         return processExtremes(forX.stamp(), forY.stamp(), (min, max) -> min == (long) max ? ConstantNode.forIntegerKind(getStackKind(), min) : this);
  91     }
  92 
  93     @Override
  94     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
  95         Value a = nodeValueMap.operand(getX());
  96         Value b = nodeValueMap.operand(getY());
  97         nodeValueMap.setResult(this, gen.emitMulHigh(a, b));
  98     }
  99 
 100     public static int multiplyHigh(int x, int y) {
 101         long r = (long) x * (long) y;
 102         return (int) (r >> 32);
 103     }
 104 
 105     public static long multiplyHigh(long x, long y) {
 106         // Checkstyle: stop
 107         long x0, y0, z0;
 108         long x1, y1, z1, z2, t;
 109         // Checkstyle: resume
 110 
 111         x0 = x & 0xFFFFFFFFL;
 112         x1 = x >> 32;
 113 
 114         y0 = y & 0xFFFFFFFFL;
 115         y1 = y >> 32;
 116 
 117         z0 = x0 * y0;
 118         t = x1 * y0 + (z0 >>> 32);
 119         z1 = t & 0xFFFFFFFFL;
 120         z2 = t >> 32;
 121         z1 += x0 * y1;
 122 
 123         return x1 * y1 + z2 + (z1 >> 32);
 124     }
 125 }