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.meta.*; 026 027import com.oracle.graal.compiler.common.type.*; 028import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp; 029import com.oracle.graal.compiler.common.type.ArithmeticOpTable.BinaryOp.Add; 030import com.oracle.graal.graph.*; 031import com.oracle.graal.graph.spi.Canonicalizable.BinaryCommutative; 032import com.oracle.graal.graph.spi.*; 033import com.oracle.graal.lir.gen.*; 034import com.oracle.graal.nodeinfo.*; 035import com.oracle.graal.nodes.*; 036import com.oracle.graal.nodes.spi.*; 037 038@NodeInfo(shortName = "+") 039public class AddNode extends BinaryArithmeticNode<Add> implements NarrowableArithmeticNode, BinaryCommutative<ValueNode> { 040 041 public static final NodeClass<AddNode> TYPE = NodeClass.create(AddNode.class); 042 043 public AddNode(ValueNode x, ValueNode y) { 044 this(TYPE, x, y); 045 } 046 047 protected AddNode(NodeClass<? extends AddNode> c, ValueNode x, ValueNode y) { 048 super(c, ArithmeticOpTable::getAdd, x, y); 049 } 050 051 public static ValueNode create(ValueNode x, ValueNode y) { 052 BinaryOp<Add> op = ArithmeticOpTable.forStamp(x.stamp()).getAdd(); 053 Stamp stamp = op.foldStamp(x.stamp(), y.stamp()); 054 ConstantNode tryConstantFold = tryConstantFold(op, x, y, stamp); 055 if (tryConstantFold != null) { 056 return tryConstantFold; 057 } else { 058 return new AddNode(x, y).maybeCommuteInputs(); 059 } 060 } 061 062 @Override 063 public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) { 064 ValueNode ret = super.canonical(tool, forX, forY); 065 if (ret != this) { 066 return ret; 067 } 068 069 if (forX.isConstant() && !forY.isConstant()) { 070 return new AddNode(forY, forX); 071 } 072 BinaryOp<Add> op = getOp(forX, forY); 073 boolean associative = op.isAssociative(); 074 if (associative) { 075 if (forX instanceof SubNode) { 076 SubNode sub = (SubNode) forX; 077 if (sub.getY() == forY) { 078 // (a - b) + b 079 return sub.getX(); 080 } 081 } 082 if (forY instanceof SubNode) { 083 SubNode sub = (SubNode) forY; 084 if (sub.getY() == forX) { 085 // b + (a - b) 086 return sub.getX(); 087 } 088 } 089 } 090 if (forY.isConstant()) { 091 Constant c = forY.asConstant(); 092 if (op.isNeutral(c)) { 093 return forX; 094 } 095 if (associative) { 096 // canonicalize expressions like "(a + 1) + 2" 097 BinaryNode reassociated = reassociate(this, ValueNode.isConstantPredicate(), forX, forY); 098 if (reassociated != this) { 099 return reassociated; 100 } 101 } 102 } 103 if (forX instanceof NegateNode) { 104 return BinaryArithmeticNode.sub(forY, ((NegateNode) forX).getValue()); 105 } else if (forY instanceof NegateNode) { 106 return BinaryArithmeticNode.sub(forX, ((NegateNode) forY).getValue()); 107 } 108 return this; 109 } 110 111 @Override 112 public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) { 113 Value op1 = nodeValueMap.operand(getX()); 114 assert op1 != null : getX() + ", this=" + this; 115 Value op2 = nodeValueMap.operand(getY()); 116 if (!getY().isConstant() && !BinaryArithmeticNode.livesLonger(this, getY(), nodeValueMap)) { 117 Value tmp = op1; 118 op1 = op2; 119 op2 = tmp; 120 } 121 nodeValueMap.setResult(this, gen.emitAdd(op1, op2, false)); 122 } 123}