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.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 
 100     @NodeIntrinsic
 101     public static native Address address(Object base, long offset);
 102 
 103     @Override
 104     public long getMaxConstantDisplacement() {
 105         Stamp curStamp = offset.stamp();
 106         if (curStamp instanceof IntegerStamp) {
 107             IntegerStamp integerStamp = (IntegerStamp) curStamp;
 108             if (integerStamp.lowerBound() >= 0) {
 109                 return integerStamp.upperBound();
 110             }
 111         }
 112         return Long.MAX_VALUE;
 113     }
 114 
 115     @Override
 116     public ValueNode getIndex() {
 117         return null;
 118     }
 119 }