001/*
002 * Copyright (c) 2011, 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.nodes.calc;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.compiler.common.type.*;
029import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp;
030import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp.Mul;
031import com.oracle.graal.graph.*;
032import com.oracle.graal.graph.spi.Canonicalizable.BinaryCommutative;
033import com.oracle.graal.graph.spi.*;
034import com.oracle.graal.lir.gen.*;
035import com.oracle.graal.nodeinfo.*;
036import com.oracle.graal.nodes.*;
037import com.oracle.graal.nodes.spi.*;
038
039@NodeInfo(shortName = "*")
040public class MulNode extends BinaryArithmeticNode<Mul> implements NarrowableArithmeticNode, BinaryCommutative<ValueNode> {
041
042    public static final NodeClass<MulNode> TYPE = NodeClass.create(MulNode.class);
043
044    public MulNode(ValueNode x, ValueNode y) {
045        this(TYPE, x, y);
046    }
047
048    protected MulNode(NodeClass<? extends MulNode> c, ValueNode x, ValueNode y) {
049        super(c, ArithmeticOpTable::getMul, x, y);
050    }
051
052    public static ValueNode create(ValueNode x, ValueNode y) {
053        BinaryOp<Mul> op = ArithmeticOpTable.forStamp(x.stamp()).getMul();
054        Stamp stamp = op.foldStamp(x.stamp(), y.stamp());
055        ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp);
056        if (tryConstantFold != null) {
057            return tryConstantFold;
058        } else {
059            return new MulNode(x, y).maybeCommuteInputs();
060        }
061    }
062
063    @Override
064    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
065        ValueNode ret = super.canonical(tool, forX, forY);
066        if (ret != this) {
067            return ret;
068        }
069
070        if (forX.isConstant() && !forY.isConstant()) {
071            return new MulNode(forY, forX);
072        }
073        if (forY.isConstant()) {
074            BinaryOp<Mul> op = getOp(forX, forY);
075            Constant c = forY.asConstant();
076            if (op.isNeutral(c)) {
077                return forX;
078            }
079
080            if (c instanceof PrimitiveConstant && ((PrimitiveConstant) c).getKind().isNumericInteger()) {
081                long i = ((PrimitiveConstant) c).asLong();
082                if (i > 0 && CodeUtil.isPowerOf2(i)) {
083                    return new LeftShiftNode(forX, ConstantNode.forInt(CodeUtil.log2(i)));
084                }
085            }
086
087            if (op.isAssociative()) {
088                // canonicalize expressions like "(a * 1) * 2"
089                return reassociate(this, ValueNode.isConstantPredicate(), forX, forY);
090            }
091        }
092        return this;
093    }
094
095    @Override
096    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
097        Value op1 = nodeValueMap.operand(getX());
098        Value op2 = nodeValueMap.operand(getY());
099        if (!getY().isConstant() && !BinaryArithmeticNode.livesLonger(this, getY(), nodeValueMap)) {
100            Value tmp = op1;
101            op1 = op2;
102            op2 = tmp;
103        }
104        nodeValueMap.setResult(this, gen.emitMul(op1, op2, false));
105    }
106}