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.nodes.memory.address; 24 25 import org.graalvm.compiler.core.common.type.IntegerStamp; 26 import org.graalvm.compiler.core.common.type.Stamp; 27 import org.graalvm.compiler.graph.Node; 28 import org.graalvm.compiler.graph.NodeClass; 29 import org.graalvm.compiler.graph.spi.Canonicalizable; 30 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 31 import org.graalvm.compiler.nodeinfo.InputType; 32 import org.graalvm.compiler.nodeinfo.NodeInfo; 33 import org.graalvm.compiler.nodes.ValueNode; 34 import org.graalvm.compiler.nodes.calc.AddNode; 35 import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode; 36 37 import jdk.vm.ci.meta.JavaKind; 38 39 /** 40 * Represents an address that is composed of a base and an offset. The base can be either a 41 * {@link JavaKind#Object}, a word-sized integer or another pointer. The offset must be a word-sized 42 * integer. 43 */ 44 @NodeInfo(allowedUsageTypes = InputType.Association) 45 public class OffsetAddressNode extends AddressNode implements Canonicalizable { 46 public static final NodeClass<OffsetAddressNode> TYPE = NodeClass.create(OffsetAddressNode.class); 47 48 @Input ValueNode base; 49 @Input ValueNode offset; 50 51 public OffsetAddressNode(ValueNode base, ValueNode offset) { 52 super(TYPE); 53 this.base = base; 54 this.offset = offset; 55 } 56 57 @Override 58 public ValueNode getBase() { 59 return base; 60 } 61 62 public void setBase(ValueNode base) { 63 updateUsages(this.base, base); 64 this.base = base; 65 } 66 67 public ValueNode getOffset() { 68 return offset; 69 } 70 71 public void setOffset(ValueNode offset) { 72 updateUsages(this.offset, offset); 73 this.offset = offset; 74 } 75 76 @Override 77 public Node canonical(CanonicalizerTool tool) { 78 if (base instanceof RawAddressNode) { 79 // The RawAddressNode is redundant, just directly use its input as base. 80 return new OffsetAddressNode(((RawAddressNode) base).getAddress(), offset); 81 } else if (base instanceof OffsetAddressNode) { 82 // Rewrite (&base[offset1])[offset2] to base[offset1 + offset2]. 83 OffsetAddressNode b = (OffsetAddressNode) base; 84 return new OffsetAddressNode(b.getBase(), BinaryArithmeticNode.add(b.getOffset(), this.getOffset())); 85 } else if (base instanceof AddNode) { 86 AddNode add = (AddNode) base; 87 if (add.getY().isConstant()) { 88 return new OffsetAddressNode(add.getX(), new AddNode(add.getY(), getOffset())); 89 } 90 } 91 return this; 92 } 93 | 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.nodes.memory.address; 24 25 import org.graalvm.compiler.core.common.type.AbstractPointerStamp; 26 import org.graalvm.compiler.core.common.type.IntegerStamp; 27 import org.graalvm.compiler.core.common.type.Stamp; 28 import org.graalvm.compiler.graph.Node; 29 import org.graalvm.compiler.graph.NodeClass; 30 import org.graalvm.compiler.graph.spi.Canonicalizable; 31 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 32 import org.graalvm.compiler.nodeinfo.InputType; 33 import org.graalvm.compiler.nodeinfo.NodeInfo; 34 import org.graalvm.compiler.nodes.ValueNode; 35 import org.graalvm.compiler.nodes.calc.AddNode; 36 import org.graalvm.compiler.nodes.calc.BinaryArithmeticNode; 37 38 import jdk.vm.ci.meta.JavaKind; 39 40 /** 41 * Represents an address that is composed of a base and an offset. The base can be either a 42 * {@link JavaKind#Object}, a word-sized integer or another pointer. The offset must be a word-sized 43 * integer. 44 */ 45 @NodeInfo(allowedUsageTypes = InputType.Association) 46 public class OffsetAddressNode extends AddressNode implements Canonicalizable { 47 public static final NodeClass<OffsetAddressNode> TYPE = NodeClass.create(OffsetAddressNode.class); 48 49 @Input ValueNode base; 50 @Input ValueNode offset; 51 52 public OffsetAddressNode(ValueNode base, ValueNode offset) { 53 super(TYPE); 54 this.base = base; 55 this.offset = offset; 56 57 assert base != null && (base.stamp() instanceof AbstractPointerStamp || IntegerStamp.getBits(base.stamp()) == 64) && 58 offset != null && IntegerStamp.getBits(offset.stamp()) == 64 : "both values must have 64 bits"; 59 } 60 61 @Override 62 public ValueNode getBase() { 63 return base; 64 } 65 66 public void setBase(ValueNode base) { 67 updateUsages(this.base, base); 68 this.base = base; 69 assert base != null && (base.stamp() instanceof AbstractPointerStamp || IntegerStamp.getBits(base.stamp()) == 64); 70 } 71 72 public ValueNode getOffset() { 73 return offset; 74 } 75 76 public void setOffset(ValueNode offset) { 77 updateUsages(this.offset, offset); 78 this.offset = offset; 79 assert offset != null && IntegerStamp.getBits(offset.stamp()) == 64; 80 } 81 82 @Override 83 public Node canonical(CanonicalizerTool tool) { 84 if (base instanceof RawAddressNode) { 85 // The RawAddressNode is redundant, just directly use its input as base. 86 return new OffsetAddressNode(((RawAddressNode) base).getAddress(), offset); 87 } else if (base instanceof OffsetAddressNode) { 88 // Rewrite (&base[offset1])[offset2] to base[offset1 + offset2]. 89 OffsetAddressNode b = (OffsetAddressNode) base; 90 return new OffsetAddressNode(b.getBase(), BinaryArithmeticNode.add(b.getOffset(), this.getOffset())); 91 } else if (base instanceof AddNode) { 92 AddNode add = (AddNode) base; 93 if (add.getY().isConstant()) { 94 return new OffsetAddressNode(add.getX(), new AddNode(add.getY(), getOffset())); 95 } 96 } 97 return this; 98 } 99 |