AMD64AddressNode.java revision 13264:48566d838608
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
24package org.graalvm.compiler.core.amd64;
25
26import org.graalvm.compiler.asm.amd64.AMD64Address.Scale;
27import org.graalvm.compiler.core.common.LIRKind;
28import org.graalvm.compiler.graph.NodeClass;
29import org.graalvm.compiler.graph.spi.Simplifiable;
30import org.graalvm.compiler.graph.spi.SimplifierTool;
31import org.graalvm.compiler.lir.amd64.AMD64AddressValue;
32import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
33import org.graalvm.compiler.nodeinfo.NodeInfo;
34import org.graalvm.compiler.nodes.ConstantNode;
35import org.graalvm.compiler.nodes.LoopBeginNode;
36import org.graalvm.compiler.nodes.PhiNode;
37import org.graalvm.compiler.nodes.ValueNode;
38import org.graalvm.compiler.nodes.calc.AddNode;
39import org.graalvm.compiler.nodes.memory.address.AddressNode;
40import org.graalvm.compiler.nodes.spi.LIRLowerable;
41import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
42
43import jdk.vm.ci.meta.AllocatableValue;
44import jdk.vm.ci.meta.Value;
45
46/**
47 * Represents an address of the form [base + index*scale + displacement]. Both base and index are
48 * optional.
49 */
50@NodeInfo
51public class AMD64AddressNode extends AddressNode implements Simplifiable, LIRLowerable {
52
53    public static final NodeClass<AMD64AddressNode> TYPE = NodeClass.create(AMD64AddressNode.class);
54
55    @OptionalInput private ValueNode base;
56
57    @OptionalInput private ValueNode index;
58    private Scale scale;
59
60    private int displacement;
61
62    public AMD64AddressNode(ValueNode base) {
63        this(base, null);
64    }
65
66    public AMD64AddressNode(ValueNode base, ValueNode index) {
67        super(TYPE);
68        this.base = base;
69        this.index = index;
70        this.scale = Scale.Times1;
71    }
72
73    public void canonicalizeIndex(SimplifierTool tool) {
74        if (index instanceof AddNode) {
75            AddNode add = (AddNode) index;
76            ValueNode valX = add.getX();
77            if (valX instanceof PhiNode) {
78                PhiNode phi = (PhiNode) valX;
79                if (phi.merge() instanceof LoopBeginNode) {
80                    LoopBeginNode loopNode = (LoopBeginNode) phi.merge();
81                    if (!loopNode.isSimpleLoop()) {
82                        ValueNode valY = add.getY();
83                        if (valY instanceof ConstantNode) {
84                            int addBy = valY.asJavaConstant().asInt();
85                            displacement = displacement + scale.value * addBy;
86                            replaceFirstInput(index, phi);
87                            tool.addToWorkList(index);
88                        }
89                    }
90                }
91            }
92        }
93    }
94
95    @Override
96    public void generate(NodeLIRBuilderTool gen) {
97        LIRGeneratorTool tool = gen.getLIRGeneratorTool();
98
99        AllocatableValue baseValue = base == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(base));
100        AllocatableValue indexValue = index == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(index));
101
102        AllocatableValue baseReference = LIRKind.derivedBaseFromValue(baseValue);
103        AllocatableValue indexReference;
104        if (index == null) {
105            indexReference = null;
106        } else if (scale.equals(Scale.Times1)) {
107            indexReference = LIRKind.derivedBaseFromValue(indexValue);
108        } else {
109            if (LIRKind.isValue(indexValue)) {
110                indexReference = null;
111            } else {
112                indexReference = Value.ILLEGAL;
113            }
114        }
115
116        LIRKind kind = LIRKind.combineDerived(tool.getLIRKind(stamp()), baseReference, indexReference);
117        gen.setResult(this, new AMD64AddressValue(kind, baseValue, indexValue, scale, displacement));
118    }
119
120    @Override
121    public ValueNode getBase() {
122        return base;
123    }
124
125    public void setBase(ValueNode base) {
126        // allow modification before inserting into the graph
127        if (isAlive()) {
128            updateUsages(this.base, base);
129        }
130        this.base = base;
131    }
132
133    @Override
134    public ValueNode getIndex() {
135        return index;
136    }
137
138    public void setIndex(ValueNode index) {
139        // allow modification before inserting into the graph
140        if (isAlive()) {
141            updateUsages(this.index, index);
142        }
143        this.index = index;
144    }
145
146    public Scale getScale() {
147        return scale;
148    }
149
150    public void setScale(Scale scale) {
151        this.scale = scale;
152    }
153
154    public int getDisplacement() {
155        return displacement;
156    }
157
158    public void setDisplacement(int displacement) {
159        this.displacement = displacement;
160    }
161
162    @Override
163    public long getMaxConstantDisplacement() {
164        return displacement;
165    }
166
167    @Override
168    public void simplify(SimplifierTool tool) {
169        canonicalizeIndex(tool);
170    }
171}
172