1 /* 2 * Copyright (c) 2015, 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.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 94 @NodeIntrinsic 95 public static native Address address(Object base, long offset); 96 97 @Override 98 public long getMaxConstantDisplacement() { 99 Stamp curStamp = offset.stamp(); 100 if (curStamp instanceof IntegerStamp) { 101 IntegerStamp integerStamp = (IntegerStamp) curStamp; 102 if (integerStamp.lowerBound() >= 0) { 103 return integerStamp.upperBound(); 104 } 105 } 106 return Long.MAX_VALUE; 107 } 108 109 @Override 110 public ValueNode getIndex() { 111 return null; 112 } 113 }