001/*
002 * Copyright (c) 2009, 2014, 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 java.io.*;
026import java.util.function.*;
027
028import jdk.internal.jvmci.common.*;
029import jdk.internal.jvmci.meta.*;
030
031import com.oracle.graal.compiler.common.type.*;
032import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp;
033import com.oracle.graal.graph.*;
034import com.oracle.graal.graph.iterators.*;
035import com.oracle.graal.graph.spi.*;
036import com.oracle.graal.nodeinfo.*;
037import com.oracle.graal.nodes.*;
038import com.oracle.graal.nodes.spi.*;
039
040@NodeInfo
041public abstract class BinaryArithmeticNode<OP> extends BinaryNode implements ArithmeticLIRLowerable, Canonicalizable.Binary<ValueNode> {
042
043    @SuppressWarnings("rawtypes") public static final NodeClass<BinaryArithmeticNode> TYPE = NodeClass.create(BinaryArithmeticNode.class);
044
045    protected interface SerializableBinaryFunction<T> extends Function<ArithmeticOpTable, BinaryOp<T>>, Serializable {
046    }
047
048    protected final SerializableBinaryFunction<OP> getOp;
049
050    protected BinaryArithmeticNode(NodeClass<? extends BinaryArithmeticNode<OP>> c, SerializableBinaryFunction<OP> getOp, ValueNode x, ValueNode y) {
051        super(c, getOp.apply(ArithmeticOpTable.forStamp(x.stamp())).foldStamp(x.stamp(), y.stamp()), x, y);
052        this.getOp = getOp;
053    }
054
055    protected final BinaryOp<OP> getOp(ValueNode forX, ValueNode forY) {
056        ArithmeticOpTable table = ArithmeticOpTable.forStamp(forX.stamp());
057        assert table.equals(ArithmeticOpTable.forStamp(forY.stamp()));
058        return getOp.apply(table);
059    }
060
061    public boolean isAssociative() {
062        return getOp(getX(), getY()).isAssociative();
063    }
064
065    @Override
066    public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
067        ValueNode result = tryConstantFold(getOp(forX, forY), forX, forY, stamp());
068        if (result != null) {
069            return result;
070        }
071        return this;
072    }
073
074    public static <OP> ConstantNode tryConstantFold(BinaryOp<OP> op, ValueNode forX, ValueNode forY, Stamp stamp) {
075        if (forX.isConstant() && forY.isConstant()) {
076            Constant ret = op.foldConstant(forX.asConstant(), forY.asConstant());
077            return ConstantNode.forPrimitive(stamp, ret);
078        }
079        return null;
080    }
081
082    @Override
083    public boolean inferStamp() {
084        return updateStamp(getOp(getX(), getY()).foldStamp(getX().stamp(), getY().stamp()));
085    }
086
087    public static AddNode add(StructuredGraph graph, ValueNode v1, ValueNode v2) {
088        return graph.unique(new AddNode(v1, v2));
089    }
090
091    public static AddNode add(ValueNode v1, ValueNode v2) {
092        return new AddNode(v1, v2);
093    }
094
095    public static MulNode mul(StructuredGraph graph, ValueNode v1, ValueNode v2) {
096        return graph.unique(new MulNode(v1, v2));
097    }
098
099    public static MulNode mul(ValueNode v1, ValueNode v2) {
100        return new MulNode(v1, v2);
101    }
102
103    public static SubNode sub(StructuredGraph graph, ValueNode v1, ValueNode v2) {
104        return graph.unique(new SubNode(v1, v2));
105    }
106
107    public static SubNode sub(ValueNode v1, ValueNode v2) {
108        return new SubNode(v1, v2);
109    }
110
111    private enum ReassociateMatch {
112        x,
113        y;
114
115        public ValueNode getValue(BinaryNode binary) {
116            switch (this) {
117                case x:
118                    return binary.getX();
119                case y:
120                    return binary.getY();
121                default:
122                    throw JVMCIError.shouldNotReachHere();
123            }
124        }
125
126        public ValueNode getOtherValue(BinaryNode binary) {
127            switch (this) {
128                case x:
129                    return binary.getY();
130                case y:
131                    return binary.getX();
132                default:
133                    throw JVMCIError.shouldNotReachHere();
134            }
135        }
136    }
137
138    private static ReassociateMatch findReassociate(BinaryNode binary, NodePredicate criterion) {
139        boolean resultX = criterion.apply(binary.getX());
140        boolean resultY = criterion.apply(binary.getY());
141        if (resultX && !resultY) {
142            return ReassociateMatch.x;
143        }
144        if (!resultX && resultY) {
145            return ReassociateMatch.y;
146        }
147        return null;
148    }
149
150    //@formatter:off
151    /*
152     * In reassociate, complexity comes from the handling of IntegerSub (non commutative) which can
153     * be mixed with IntegerAdd. It first tries to find m1, m2 which match the criterion :
154     * (a o m2) o m1
155     * (m2 o a) o m1
156     * m1 o (a o m2)
157     * m1 o (m2 o a)
158     * It then produces 4 boolean for the -/+ cases:
159     * invertA : should the final expression be like *-a (rather than a+*)
160     * aSub : should the final expression be like a-* (rather than a+*)
161     * invertM1 : should the final expression contain -m1
162     * invertM2 : should the final expression contain -m2
163     *
164     */
165    //@formatter:on
166    /**
167     * Tries to re-associate values which satisfy the criterion. For example with a constantness
168     * criterion: {@code (a + 2) + 1 => a + (1 + 2)}
169     * <p>
170     * This method accepts only {@linkplain BinaryOp#isAssociative() associative} operations such as
171     * +, -, *, &amp;, | and ^
172     *
173     * @param forY
174     * @param forX
175     */
176    public static BinaryArithmeticNode<?> reassociate(BinaryArithmeticNode<?> node, NodePredicate criterion, ValueNode forX, ValueNode forY) {
177        assert node.getOp(forX, forY).isAssociative();
178        ReassociateMatch match1 = findReassociate(node, criterion);
179        if (match1 == null) {
180            return node;
181        }
182        ValueNode otherValue = match1.getOtherValue(node);
183        boolean addSub = false;
184        boolean subAdd = false;
185        if (otherValue.getClass() != node.getClass()) {
186            if (node instanceof AddNode && otherValue instanceof SubNode) {
187                addSub = true;
188            } else if (node instanceof SubNode && otherValue instanceof AddNode) {
189                subAdd = true;
190            } else {
191                return node;
192            }
193        }
194        BinaryNode other = (BinaryNode) otherValue;
195        ReassociateMatch match2 = findReassociate(other, criterion);
196        if (match2 == null) {
197            return node;
198        }
199        boolean invertA = false;
200        boolean aSub = false;
201        boolean invertM1 = false;
202        boolean invertM2 = false;
203        if (addSub) {
204            invertM2 = match2 == ReassociateMatch.y;
205            invertA = !invertM2;
206        } else if (subAdd) {
207            invertA = invertM2 = match1 == ReassociateMatch.x;
208            invertM1 = !invertM2;
209        } else if (node instanceof SubNode && other instanceof SubNode) {
210            invertA = match1 == ReassociateMatch.x ^ match2 == ReassociateMatch.x;
211            aSub = match1 == ReassociateMatch.y && match2 == ReassociateMatch.y;
212            invertM1 = match1 == ReassociateMatch.y && match2 == ReassociateMatch.x;
213            invertM2 = match1 == ReassociateMatch.x && match2 == ReassociateMatch.x;
214        }
215        assert !(invertM1 && invertM2) && !(invertA && aSub);
216        ValueNode m1 = match1.getValue(node);
217        ValueNode m2 = match2.getValue(other);
218        ValueNode a = match2.getOtherValue(other);
219        if (node instanceof AddNode || node instanceof SubNode) {
220            BinaryNode associated;
221            if (invertM1) {
222                associated = BinaryArithmeticNode.sub(m2, m1);
223            } else if (invertM2) {
224                associated = BinaryArithmeticNode.sub(m1, m2);
225            } else {
226                associated = BinaryArithmeticNode.add(m1, m2);
227            }
228            if (invertA) {
229                return BinaryArithmeticNode.sub(associated, a);
230            }
231            if (aSub) {
232                return BinaryArithmeticNode.sub(a, associated);
233            }
234            return BinaryArithmeticNode.add(a, associated);
235        } else if (node instanceof MulNode) {
236            return BinaryArithmeticNode.mul(a, AddNode.mul(m1, m2));
237        } else if (node instanceof AndNode) {
238            return new AndNode(a, new AndNode(m1, m2));
239        } else if (node instanceof OrNode) {
240            return new OrNode(a, new OrNode(m1, m2));
241        } else if (node instanceof XorNode) {
242            return new XorNode(a, new XorNode(m1, m2));
243        } else {
244            throw JVMCIError.shouldNotReachHere();
245        }
246    }
247
248    protected static boolean livesLonger(ValueNode after, ValueNode value, NodeValueMap nodeValueMap) {
249        for (Node usage : value.usages()) {
250            if (usage != after && usage instanceof ValueNode && nodeValueMap.hasOperand(usage)) {
251                return true;
252            }
253        }
254        return false;
255    }
256
257    /**
258     * Ensure a canonical ordering of inputs for commutative nodes to improve GVN results. Order the
259     * inputs by increasing {@link Node#id} and call {@link Graph#findDuplicate(Node)} on the node
260     * if it's currently in a graph. It's assumed that if there was a constant on the left it's been
261     * moved to the right by other code and that ordering is left alone.
262     *
263     * @return the original node or another node with the same input ordering
264     */
265    @SuppressWarnings("deprecation")
266    public BinaryNode maybeCommuteInputs() {
267        assert this instanceof BinaryCommutative;
268        if (!y.isConstant() && x.getId() > y.getId()) {
269            ValueNode tmp = x;
270            x = y;
271            y = tmp;
272            if (graph() != null) {
273                // See if this node already exists
274                BinaryNode duplicate = graph().findDuplicate(this);
275                if (duplicate != null) {
276                    return duplicate;
277                }
278            }
279        }
280        return this;
281    }
282}