43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, 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 |
|
|
24 |
package org.graalvm.compiler.core.amd64;
|
|
25 |
|
47798
|
26 |
import org.graalvm.compiler.asm.amd64.AMD64Address.Scale;
|
46344
|
27 |
import org.graalvm.compiler.core.common.NumUtil;
|
48190
|
28 |
import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
|
43972
|
29 |
import org.graalvm.compiler.core.common.type.IntegerStamp;
|
46640
|
30 |
import org.graalvm.compiler.debug.DebugContext;
|
48190
|
31 |
import org.graalvm.compiler.nodes.NodeView;
|
47798
|
32 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
43972
|
33 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
34 |
import org.graalvm.compiler.nodes.calc.AddNode;
|
|
35 |
import org.graalvm.compiler.nodes.calc.LeftShiftNode;
|
47798
|
36 |
import org.graalvm.compiler.nodes.calc.NegateNode;
|
43972
|
37 |
import org.graalvm.compiler.nodes.memory.address.AddressNode;
|
|
38 |
import org.graalvm.compiler.phases.common.AddressLoweringPhase.AddressLowering;
|
|
39 |
|
47798
|
40 |
import jdk.vm.ci.meta.JavaConstant;
|
|
41 |
|
43972
|
42 |
public class AMD64AddressLowering extends AddressLowering {
|
48190
|
43 |
private static final int ADDRESS_BITS = 64;
|
43972
|
44 |
|
|
45 |
@Override
|
|
46 |
public AddressNode lower(ValueNode base, ValueNode offset) {
|
|
47 |
AMD64AddressNode ret = new AMD64AddressNode(base, offset);
|
47798
|
48 |
StructuredGraph graph = base.graph();
|
|
49 |
|
43972
|
50 |
boolean changed;
|
|
51 |
do {
|
47798
|
52 |
changed = improve(graph, base.getDebug(), ret, false, false);
|
43972
|
53 |
} while (changed);
|
47798
|
54 |
|
48190
|
55 |
assert checkAddressBitWidth(ret.getBase());
|
|
56 |
assert checkAddressBitWidth(ret.getIndex());
|
47798
|
57 |
return graph.unique(ret);
|
43972
|
58 |
}
|
|
59 |
|
48190
|
60 |
private static boolean checkAddressBitWidth(ValueNode value) {
|
|
61 |
return value == null || value.stamp(NodeView.DEFAULT) instanceof AbstractPointerStamp || IntegerStamp.getBits(value.stamp(NodeView.DEFAULT)) == ADDRESS_BITS;
|
|
62 |
}
|
|
63 |
|
46640
|
64 |
/**
|
47798
|
65 |
* Tries to optimize addresses so that they match the AMD64-specific addressing mode better
|
|
66 |
* (base + index * scale + displacement).
|
|
67 |
*
|
|
68 |
* @param graph the current graph
|
|
69 |
* @param debug the current debug context
|
|
70 |
* @param ret the address that should be optimized
|
|
71 |
* @param isBaseNegated determines if the address base is negated. if so, all values that are
|
|
72 |
* extracted from the base will be negated as well
|
|
73 |
* @param isIndexNegated determines if the index is negated. if so, all values that are
|
|
74 |
* extracted from the index will be negated as well
|
|
75 |
* @return true if the address was modified
|
46640
|
76 |
*/
|
47798
|
77 |
protected boolean improve(StructuredGraph graph, DebugContext debug, AMD64AddressNode ret, boolean isBaseNegated, boolean isIndexNegated) {
|
|
78 |
ValueNode newBase = improveInput(ret, ret.getBase(), 0, isBaseNegated);
|
43972
|
79 |
if (newBase != ret.getBase()) {
|
|
80 |
ret.setBase(newBase);
|
|
81 |
return true;
|
|
82 |
}
|
|
83 |
|
47798
|
84 |
ValueNode newIdx = improveInput(ret, ret.getIndex(), ret.getScale().log2, isIndexNegated);
|
43972
|
85 |
if (newIdx != ret.getIndex()) {
|
|
86 |
ret.setIndex(newIdx);
|
|
87 |
return true;
|
|
88 |
}
|
|
89 |
|
|
90 |
if (ret.getIndex() instanceof LeftShiftNode) {
|
|
91 |
LeftShiftNode shift = (LeftShiftNode) ret.getIndex();
|
|
92 |
if (shift.getY().isConstant()) {
|
|
93 |
int amount = ret.getScale().log2 + shift.getY().asJavaConstant().asInt();
|
|
94 |
Scale scale = Scale.fromShift(amount);
|
|
95 |
if (scale != null) {
|
|
96 |
ret.setIndex(shift.getX());
|
|
97 |
ret.setScale(scale);
|
|
98 |
return true;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
if (ret.getScale() == Scale.Times1) {
|
47798
|
104 |
if (ret.getIndex() == null && ret.getBase() instanceof AddNode) {
|
|
105 |
AddNode add = (AddNode) ret.getBase();
|
|
106 |
ret.setBase(add.getX());
|
|
107 |
ret.setIndex(considerNegation(graph, add.getY(), isBaseNegated));
|
|
108 |
return true;
|
48861
|
109 |
}
|
|
110 |
|
|
111 |
if (ret.getBase() == null && ret.getIndex() instanceof AddNode) {
|
47798
|
112 |
AddNode add = (AddNode) ret.getIndex();
|
|
113 |
ret.setBase(considerNegation(graph, add.getX(), isIndexNegated));
|
|
114 |
ret.setIndex(add.getY());
|
|
115 |
return true;
|
43972
|
116 |
}
|
|
117 |
|
|
118 |
if (ret.getBase() instanceof LeftShiftNode && !(ret.getIndex() instanceof LeftShiftNode)) {
|
|
119 |
ValueNode tmp = ret.getBase();
|
47798
|
120 |
ret.setBase(considerNegation(graph, ret.getIndex(), isIndexNegated != isBaseNegated));
|
|
121 |
ret.setIndex(considerNegation(graph, tmp, isIndexNegated != isBaseNegated));
|
43972
|
122 |
return true;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
47798
|
126 |
return improveNegation(graph, debug, ret, isBaseNegated, isIndexNegated);
|
|
127 |
}
|
|
128 |
|
|
129 |
private boolean improveNegation(StructuredGraph graph, DebugContext debug, AMD64AddressNode ret, boolean originalBaseNegated, boolean originalIndexNegated) {
|
|
130 |
boolean baseNegated = originalBaseNegated;
|
|
131 |
boolean indexNegated = originalIndexNegated;
|
|
132 |
|
|
133 |
ValueNode originalBase = ret.getBase();
|
|
134 |
ValueNode originalIndex = ret.getIndex();
|
|
135 |
|
|
136 |
if (ret.getBase() instanceof NegateNode) {
|
|
137 |
NegateNode negate = (NegateNode) ret.getBase();
|
|
138 |
ret.setBase(negate.getValue());
|
|
139 |
baseNegated = !baseNegated;
|
|
140 |
}
|
|
141 |
|
|
142 |
if (ret.getIndex() instanceof NegateNode) {
|
|
143 |
NegateNode negate = (NegateNode) ret.getIndex();
|
|
144 |
ret.setIndex(negate.getValue());
|
|
145 |
indexNegated = !indexNegated;
|
|
146 |
}
|
|
147 |
|
|
148 |
if (baseNegated != originalBaseNegated || indexNegated != originalIndexNegated) {
|
|
149 |
ValueNode base = ret.getBase();
|
|
150 |
ValueNode index = ret.getIndex();
|
|
151 |
|
|
152 |
boolean improved = improve(graph, debug, ret, baseNegated, indexNegated);
|
|
153 |
if (baseNegated != originalBaseNegated) {
|
|
154 |
if (base == ret.getBase()) {
|
|
155 |
ret.setBase(originalBase);
|
|
156 |
} else if (ret.getBase() != null) {
|
48190
|
157 |
ret.setBase(graph.maybeAddOrUnique(NegateNode.create(ret.getBase(), NodeView.DEFAULT)));
|
47798
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
if (indexNegated != originalIndexNegated) {
|
|
162 |
if (index == ret.getIndex()) {
|
|
163 |
ret.setIndex(originalIndex);
|
|
164 |
} else if (ret.getIndex() != null) {
|
48190
|
165 |
ret.setIndex(graph.maybeAddOrUnique(NegateNode.create(ret.getIndex(), NodeView.DEFAULT)));
|
47798
|
166 |
}
|
|
167 |
}
|
|
168 |
return improved;
|
|
169 |
} else {
|
|
170 |
assert ret.getBase() == originalBase && ret.getIndex() == originalIndex;
|
|
171 |
}
|
43972
|
172 |
return false;
|
|
173 |
}
|
|
174 |
|
47798
|
175 |
private static ValueNode considerNegation(StructuredGraph graph, ValueNode value, boolean negate) {
|
|
176 |
if (negate && value != null) {
|
48190
|
177 |
return graph.maybeAddOrUnique(NegateNode.create(value, NodeView.DEFAULT));
|
47798
|
178 |
}
|
|
179 |
return value;
|
|
180 |
}
|
|
181 |
|
48190
|
182 |
private static ValueNode improveInput(AMD64AddressNode address, ValueNode node, int shift, boolean negateExtractedDisplacement) {
|
43972
|
183 |
if (node == null) {
|
|
184 |
return null;
|
|
185 |
}
|
|
186 |
|
|
187 |
JavaConstant c = node.asJavaConstant();
|
|
188 |
if (c != null) {
|
47798
|
189 |
return improveConstDisp(address, node, c, null, shift, negateExtractedDisplacement);
|
43972
|
190 |
} else {
|
48190
|
191 |
if (node.stamp(NodeView.DEFAULT) instanceof IntegerStamp) {
|
48861
|
192 |
assert IntegerStamp.getBits(node.stamp(NodeView.DEFAULT)) == ADDRESS_BITS;
|
48190
|
193 |
|
|
194 |
/*
|
|
195 |
* we can't swallow zero-extends because of multiple reasons:
|
|
196 |
*
|
|
197 |
* a) we might encounter something like the following: ZeroExtend(Add(negativeValue,
|
|
198 |
* positiveValue)). if we swallow the zero-extend in this case and subsequently
|
|
199 |
* optimize the add, we might end up with a negative value that has less than 64
|
|
200 |
* bits in base or index. such a value would require sign extension instead of
|
|
201 |
* zero-extension but the backend can only do (implicit) zero-extension by using a
|
|
202 |
* larger register (e.g., rax instead of eax).
|
|
203 |
*
|
|
204 |
* b) our backend does not guarantee that the upper half of a 64-bit register equals
|
|
205 |
* 0 if a 32-bit value is stored in there.
|
|
206 |
*
|
|
207 |
* c) we also can't swallow zero-extends with less than 32 bits as most of these
|
|
208 |
* values are immediately sign-extended to 32 bit by the backend (therefore, the
|
|
209 |
* subsequent implicit zero-extension to 64 bit won't do what we expect).
|
|
210 |
*/
|
47798
|
211 |
|
|
212 |
if (node instanceof AddNode) {
|
43972
|
213 |
AddNode add = (AddNode) node;
|
|
214 |
if (add.getX().isConstant()) {
|
47798
|
215 |
return improveConstDisp(address, node, add.getX().asJavaConstant(), add.getY(), shift, negateExtractedDisplacement);
|
43972
|
216 |
} else if (add.getY().isConstant()) {
|
47798
|
217 |
return improveConstDisp(address, node, add.getY().asJavaConstant(), add.getX(), shift, negateExtractedDisplacement);
|
43972
|
218 |
}
|
|
219 |
}
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
return node;
|
|
224 |
}
|
|
225 |
|
47798
|
226 |
private static ValueNode improveConstDisp(AMD64AddressNode address, ValueNode original, JavaConstant c, ValueNode other, int shift, boolean negateExtractedDisplacement) {
|
43972
|
227 |
if (c.getJavaKind().isNumericInteger()) {
|
47798
|
228 |
long delta = c.asLong() << shift;
|
|
229 |
if (updateDisplacement(address, delta, negateExtractedDisplacement)) {
|
43972
|
230 |
return other;
|
|
231 |
}
|
|
232 |
}
|
|
233 |
return original;
|
|
234 |
}
|
47798
|
235 |
|
|
236 |
protected static boolean updateDisplacement(AMD64AddressNode address, long displacementDelta, boolean negateDelta) {
|
|
237 |
long sign = negateDelta ? -1 : 1;
|
|
238 |
long disp = address.getDisplacement() + displacementDelta * sign;
|
|
239 |
if (NumUtil.isInt(disp)) {
|
|
240 |
address.setDisplacement((int) disp);
|
|
241 |
return true;
|
|
242 |
}
|
|
243 |
return false;
|
|
244 |
}
|
43972
|
245 |
}
|