43972
|
1 |
/*
|
59095
|
2 |
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.lir;
|
|
26 |
|
46640
|
27 |
import static jdk.vm.ci.code.ValueUtil.isStackSlot;
|
43972
|
28 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.COMPOSITE;
|
|
29 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST;
|
|
30 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.HINT;
|
|
31 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
|
|
32 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.OUTGOING;
|
|
33 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
|
|
34 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
|
|
35 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.UNINITIALIZED;
|
|
36 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandMode.ALIVE;
|
|
37 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandMode.DEF;
|
|
38 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandMode.TEMP;
|
|
39 |
import static org.graalvm.compiler.lir.LIRValueUtil.isVirtualStackSlot;
|
|
40 |
|
|
41 |
import java.lang.annotation.ElementType;
|
|
42 |
import java.lang.annotation.Retention;
|
|
43 |
import java.lang.annotation.RetentionPolicy;
|
|
44 |
import java.lang.annotation.Target;
|
|
45 |
import java.util.Arrays;
|
|
46 |
import java.util.EnumMap;
|
|
47 |
import java.util.EnumSet;
|
|
48 |
|
|
49 |
import org.graalvm.compiler.graph.NodeSourcePosition;
|
46459
|
50 |
import org.graalvm.compiler.lir.StandardOp.LoadConstantOp;
|
|
51 |
import org.graalvm.compiler.lir.StandardOp.MoveOp;
|
|
52 |
import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
|
43972
|
53 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
46536
|
54 |
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
|
43972
|
55 |
|
|
56 |
import jdk.vm.ci.code.RegisterValue;
|
|
57 |
import jdk.vm.ci.code.StackSlot;
|
|
58 |
import jdk.vm.ci.meta.JavaConstant;
|
|
59 |
import jdk.vm.ci.meta.Value;
|
|
60 |
|
|
61 |
/**
|
|
62 |
* The base class for an {@code LIRInstruction}.
|
|
63 |
*/
|
|
64 |
public abstract class LIRInstruction {
|
|
65 |
/**
|
|
66 |
* Constants denoting how a LIR instruction uses an operand.
|
|
67 |
*/
|
|
68 |
public enum OperandMode {
|
|
69 |
/**
|
|
70 |
* The value must have been defined before. It is alive before the instruction until the
|
|
71 |
* beginning of the instruction, but not necessarily throughout the instruction. A register
|
|
72 |
* assigned to it can also be assigned to a {@link #TEMP} or {@link #DEF} operand. The value
|
|
73 |
* can be used again after the instruction, so the instruction must not modify the register.
|
|
74 |
*/
|
|
75 |
USE,
|
|
76 |
|
|
77 |
/**
|
|
78 |
* The value must have been defined before. It is alive before the instruction and
|
|
79 |
* throughout the instruction. A register assigned to it cannot be assigned to a
|
|
80 |
* {@link #TEMP} or {@link #DEF} operand. The value can be used again after the instruction,
|
|
81 |
* so the instruction must not modify the register.
|
|
82 |
*/
|
|
83 |
ALIVE,
|
|
84 |
|
|
85 |
/**
|
|
86 |
* The value must not have been defined before, and must not be used after the instruction.
|
|
87 |
* The instruction can do whatever it wants with the register assigned to it (or not use it
|
|
88 |
* at all).
|
|
89 |
*/
|
|
90 |
TEMP,
|
|
91 |
|
|
92 |
/**
|
|
93 |
* The value must not have been defined before. The instruction has to assign a value to the
|
|
94 |
* register. The value can (and most likely will) be used after the instruction.
|
|
95 |
*/
|
|
96 |
DEF,
|
|
97 |
}
|
|
98 |
|
|
99 |
@Retention(RetentionPolicy.RUNTIME)
|
|
100 |
@Target(ElementType.FIELD)
|
|
101 |
public static @interface Use {
|
|
102 |
|
|
103 |
OperandFlag[] value() default OperandFlag.REG;
|
|
104 |
}
|
|
105 |
|
|
106 |
@Retention(RetentionPolicy.RUNTIME)
|
|
107 |
@Target(ElementType.FIELD)
|
|
108 |
public static @interface Alive {
|
|
109 |
|
|
110 |
OperandFlag[] value() default OperandFlag.REG;
|
|
111 |
}
|
|
112 |
|
|
113 |
@Retention(RetentionPolicy.RUNTIME)
|
|
114 |
@Target(ElementType.FIELD)
|
|
115 |
public static @interface Temp {
|
|
116 |
|
|
117 |
OperandFlag[] value() default OperandFlag.REG;
|
|
118 |
}
|
|
119 |
|
|
120 |
@Retention(RetentionPolicy.RUNTIME)
|
|
121 |
@Target(ElementType.FIELD)
|
|
122 |
public static @interface Def {
|
|
123 |
|
|
124 |
OperandFlag[] value() default OperandFlag.REG;
|
|
125 |
}
|
|
126 |
|
|
127 |
@Retention(RetentionPolicy.RUNTIME)
|
|
128 |
@Target(ElementType.FIELD)
|
|
129 |
public static @interface State {
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Flags for an operand.
|
|
134 |
*/
|
|
135 |
public enum OperandFlag {
|
|
136 |
/**
|
|
137 |
* The value can be a {@link RegisterValue}.
|
|
138 |
*/
|
|
139 |
REG,
|
|
140 |
|
|
141 |
/**
|
|
142 |
* The value can be a {@link StackSlot}.
|
|
143 |
*/
|
|
144 |
STACK,
|
|
145 |
|
|
146 |
/**
|
|
147 |
* The value can be a {@link CompositeValue}.
|
|
148 |
*/
|
|
149 |
COMPOSITE,
|
|
150 |
|
|
151 |
/**
|
|
152 |
* The value can be a {@link JavaConstant}.
|
|
153 |
*/
|
|
154 |
CONST,
|
|
155 |
|
|
156 |
/**
|
|
157 |
* The value can be {@link Value#ILLEGAL}.
|
|
158 |
*/
|
|
159 |
ILLEGAL,
|
|
160 |
|
|
161 |
/**
|
|
162 |
* The register allocator should try to assign a certain register to improve code quality.
|
|
163 |
* Use {@link LIRInstruction#forEachRegisterHint} to access the register hints.
|
|
164 |
*/
|
|
165 |
HINT,
|
|
166 |
|
|
167 |
/**
|
|
168 |
* The value can be uninitialized, e.g., a stack slot that has not written to before. This
|
|
169 |
* is only used to avoid false positives in verification code.
|
|
170 |
*/
|
|
171 |
UNINITIALIZED,
|
|
172 |
|
|
173 |
/** Outgoing block value. */
|
|
174 |
OUTGOING,
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* For validity checking of the operand flags defined by instruction subclasses.
|
|
179 |
*/
|
|
180 |
protected static final EnumMap<OperandMode, EnumSet<OperandFlag>> ALLOWED_FLAGS;
|
|
181 |
|
|
182 |
static {
|
|
183 |
ALLOWED_FLAGS = new EnumMap<>(OperandMode.class);
|
|
184 |
ALLOWED_FLAGS.put(OperandMode.USE, EnumSet.of(REG, STACK, COMPOSITE, CONST, ILLEGAL, HINT, UNINITIALIZED));
|
|
185 |
ALLOWED_FLAGS.put(ALIVE, EnumSet.of(REG, STACK, COMPOSITE, CONST, ILLEGAL, HINT, UNINITIALIZED, OUTGOING));
|
|
186 |
ALLOWED_FLAGS.put(TEMP, EnumSet.of(REG, STACK, COMPOSITE, ILLEGAL, HINT));
|
|
187 |
ALLOWED_FLAGS.put(DEF, EnumSet.of(REG, STACK, COMPOSITE, ILLEGAL, HINT));
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* The flags of the base and index value of an address.
|
|
192 |
*/
|
|
193 |
protected static final EnumSet<OperandFlag> ADDRESS_FLAGS = EnumSet.of(REG, ILLEGAL);
|
|
194 |
|
|
195 |
private final LIRInstructionClass<?> instructionClass;
|
|
196 |
|
|
197 |
/**
|
|
198 |
* Instruction id for register allocation.
|
|
199 |
*/
|
|
200 |
private int id;
|
|
201 |
|
|
202 |
/**
|
|
203 |
* The source position of the code that generated this instruction.
|
|
204 |
*/
|
|
205 |
private NodeSourcePosition position;
|
|
206 |
|
|
207 |
/**
|
|
208 |
* Constructs a new LIR instruction.
|
|
209 |
*/
|
|
210 |
public LIRInstruction(LIRInstructionClass<? extends LIRInstruction> c) {
|
|
211 |
instructionClass = c;
|
|
212 |
assert c.getClazz() == this.getClass();
|
|
213 |
id = -1;
|
|
214 |
}
|
|
215 |
|
|
216 |
public abstract void emitCode(CompilationResultBuilder crb);
|
|
217 |
|
|
218 |
public final int id() {
|
|
219 |
return id;
|
|
220 |
}
|
|
221 |
|
|
222 |
public final void setId(int id) {
|
|
223 |
this.id = id;
|
|
224 |
}
|
|
225 |
|
|
226 |
public final NodeSourcePosition getPosition() {
|
|
227 |
return position;
|
|
228 |
}
|
|
229 |
|
|
230 |
public final void setPosition(NodeSourcePosition position) {
|
|
231 |
this.position = position;
|
|
232 |
}
|
|
233 |
|
|
234 |
public final String name() {
|
|
235 |
return instructionClass.getOpcode(this);
|
|
236 |
}
|
|
237 |
|
|
238 |
public final boolean hasOperands() {
|
|
239 |
return instructionClass.hasOperands() || hasState() || destroysCallerSavedRegisters();
|
|
240 |
}
|
|
241 |
|
|
242 |
public final boolean hasState() {
|
|
243 |
return instructionClass.hasState(this);
|
|
244 |
}
|
|
245 |
|
|
246 |
public boolean destroysCallerSavedRegisters() {
|
|
247 |
return false;
|
|
248 |
}
|
|
249 |
|
|
250 |
// InstructionValueProcedures
|
|
251 |
public final void forEachInput(InstructionValueProcedure proc) {
|
|
252 |
instructionClass.forEachUse(this, proc);
|
|
253 |
}
|
|
254 |
|
|
255 |
public final void forEachAlive(InstructionValueProcedure proc) {
|
|
256 |
instructionClass.forEachAlive(this, proc);
|
|
257 |
}
|
|
258 |
|
|
259 |
public final void forEachTemp(InstructionValueProcedure proc) {
|
|
260 |
instructionClass.forEachTemp(this, proc);
|
|
261 |
}
|
|
262 |
|
|
263 |
public final void forEachOutput(InstructionValueProcedure proc) {
|
|
264 |
instructionClass.forEachDef(this, proc);
|
|
265 |
}
|
|
266 |
|
|
267 |
public final void forEachState(InstructionValueProcedure proc) {
|
|
268 |
instructionClass.forEachState(this, proc);
|
|
269 |
}
|
|
270 |
|
|
271 |
// ValueProcedures
|
|
272 |
public final void forEachInput(ValueProcedure proc) {
|
|
273 |
instructionClass.forEachUse(this, proc);
|
|
274 |
}
|
|
275 |
|
|
276 |
public final void forEachAlive(ValueProcedure proc) {
|
|
277 |
instructionClass.forEachAlive(this, proc);
|
|
278 |
}
|
|
279 |
|
|
280 |
public final void forEachTemp(ValueProcedure proc) {
|
|
281 |
instructionClass.forEachTemp(this, proc);
|
|
282 |
}
|
|
283 |
|
|
284 |
public final void forEachOutput(ValueProcedure proc) {
|
|
285 |
instructionClass.forEachDef(this, proc);
|
|
286 |
}
|
|
287 |
|
|
288 |
public final void forEachState(ValueProcedure proc) {
|
|
289 |
instructionClass.forEachState(this, proc);
|
|
290 |
}
|
|
291 |
|
|
292 |
// States
|
|
293 |
public final void forEachState(InstructionStateProcedure proc) {
|
|
294 |
instructionClass.forEachState(this, proc);
|
|
295 |
}
|
|
296 |
|
|
297 |
public final void forEachState(StateProcedure proc) {
|
|
298 |
instructionClass.forEachState(this, proc);
|
|
299 |
}
|
|
300 |
|
|
301 |
// InstructionValueConsumers
|
|
302 |
public final void visitEachInput(InstructionValueConsumer proc) {
|
|
303 |
instructionClass.visitEachUse(this, proc);
|
|
304 |
}
|
|
305 |
|
|
306 |
public final void visitEachAlive(InstructionValueConsumer proc) {
|
|
307 |
instructionClass.visitEachAlive(this, proc);
|
|
308 |
}
|
|
309 |
|
|
310 |
public final void visitEachTemp(InstructionValueConsumer proc) {
|
|
311 |
instructionClass.visitEachTemp(this, proc);
|
|
312 |
}
|
|
313 |
|
|
314 |
public final void visitEachOutput(InstructionValueConsumer proc) {
|
|
315 |
instructionClass.visitEachDef(this, proc);
|
|
316 |
}
|
|
317 |
|
|
318 |
public final void visitEachState(InstructionValueConsumer proc) {
|
|
319 |
instructionClass.visitEachState(this, proc);
|
|
320 |
}
|
|
321 |
|
|
322 |
// ValueConsumers
|
|
323 |
public final void visitEachInput(ValueConsumer proc) {
|
|
324 |
instructionClass.visitEachUse(this, proc);
|
|
325 |
}
|
|
326 |
|
|
327 |
public final void visitEachAlive(ValueConsumer proc) {
|
|
328 |
instructionClass.visitEachAlive(this, proc);
|
|
329 |
}
|
|
330 |
|
|
331 |
public final void visitEachTemp(ValueConsumer proc) {
|
|
332 |
instructionClass.visitEachTemp(this, proc);
|
|
333 |
}
|
|
334 |
|
|
335 |
public final void visitEachOutput(ValueConsumer proc) {
|
|
336 |
instructionClass.visitEachDef(this, proc);
|
|
337 |
}
|
|
338 |
|
|
339 |
public final void visitEachState(ValueConsumer proc) {
|
|
340 |
instructionClass.visitEachState(this, proc);
|
|
341 |
}
|
|
342 |
|
|
343 |
@SuppressWarnings("unused")
|
|
344 |
public final Value forEachRegisterHint(Value value, OperandMode mode, InstructionValueProcedure proc) {
|
|
345 |
return instructionClass.forEachRegisterHint(this, mode, proc);
|
|
346 |
}
|
|
347 |
|
|
348 |
@SuppressWarnings("unused")
|
|
349 |
public final Value forEachRegisterHint(Value value, OperandMode mode, ValueProcedure proc) {
|
|
350 |
return instructionClass.forEachRegisterHint(this, mode, proc);
|
|
351 |
}
|
|
352 |
|
46459
|
353 |
// Checkstyle: stop
|
|
354 |
/**
|
|
355 |
* Returns {@code true} if the instruction is a {@link MoveOp}.
|
|
356 |
*
|
|
357 |
* This function is preferred to {@code instanceof MoveOp} since the type check is more
|
|
358 |
* expensive than reading a field from {@link LIRInstructionClass}.
|
|
359 |
*/
|
|
360 |
public final boolean isMoveOp() {
|
|
361 |
return instructionClass.isMoveOp();
|
|
362 |
}
|
|
363 |
|
|
364 |
/**
|
|
365 |
* Returns {@code true} if the instruction is a {@link ValueMoveOp}.
|
|
366 |
*
|
|
367 |
* This function is preferred to {@code instanceof ValueMoveOp} since the type check is more
|
|
368 |
* expensive than reading a field from {@link LIRInstructionClass}.
|
|
369 |
*/
|
|
370 |
public final boolean isValueMoveOp() {
|
|
371 |
return instructionClass.isValueMoveOp();
|
|
372 |
}
|
|
373 |
|
|
374 |
/**
|
|
375 |
* Returns {@code true} if the instruction is a {@link LoadConstantOp}.
|
|
376 |
*
|
|
377 |
* This function is preferred to {@code instanceof LoadConstantOp} since the type check is more
|
|
378 |
* expensive than reading a field from {@link LIRInstructionClass}.
|
|
379 |
*/
|
|
380 |
public final boolean isLoadConstantOp() {
|
|
381 |
return instructionClass.isLoadConstantOp();
|
|
382 |
}
|
|
383 |
// Checkstyle: resume
|
|
384 |
|
43972
|
385 |
/**
|
|
386 |
* Utility method to add stack arguments to a list of temporaries. Useful for modeling calling
|
|
387 |
* conventions that kill outgoing argument space.
|
|
388 |
*
|
|
389 |
* @return additional temporaries
|
|
390 |
*/
|
|
391 |
protected static Value[] addStackSlotsToTemporaries(Value[] parameters, Value[] temporaries) {
|
|
392 |
int extraTemps = 0;
|
|
393 |
for (Value p : parameters) {
|
|
394 |
if (isStackSlot(p)) {
|
|
395 |
extraTemps++;
|
|
396 |
}
|
|
397 |
assert !isVirtualStackSlot(p) : "only real stack slots in calling convention";
|
|
398 |
}
|
|
399 |
if (extraTemps != 0) {
|
|
400 |
int index = temporaries.length;
|
|
401 |
Value[] newTemporaries = Arrays.copyOf(temporaries, temporaries.length + extraTemps);
|
|
402 |
for (Value p : parameters) {
|
|
403 |
if (isStackSlot(p)) {
|
|
404 |
newTemporaries[index++] = p;
|
|
405 |
}
|
|
406 |
}
|
|
407 |
return newTemporaries;
|
|
408 |
}
|
|
409 |
return temporaries;
|
|
410 |
}
|
|
411 |
|
|
412 |
public void verify() {
|
|
413 |
}
|
|
414 |
|
46536
|
415 |
/**
|
|
416 |
* Adds a comment to this instruction.
|
|
417 |
*/
|
|
418 |
public final void setComment(LIRGenerationResult res, String comment) {
|
|
419 |
res.setComment(this, comment);
|
|
420 |
}
|
|
421 |
|
|
422 |
/**
|
|
423 |
* Gets the comment attached to this instruction.
|
|
424 |
*/
|
|
425 |
public final String getComment(LIRGenerationResult res) {
|
|
426 |
return res.getComment(this);
|
|
427 |
}
|
|
428 |
|
43972
|
429 |
public final String toStringWithIdPrefix() {
|
|
430 |
if (id != -1) {
|
|
431 |
return String.format("%4d %s", id, toString());
|
|
432 |
}
|
|
433 |
return " " + toString();
|
|
434 |
}
|
|
435 |
|
|
436 |
@Override
|
|
437 |
public String toString() {
|
|
438 |
return instructionClass.toString(this);
|
|
439 |
}
|
|
440 |
|
46536
|
441 |
public String toString(LIRGenerationResult res) {
|
|
442 |
String toString = toString();
|
|
443 |
if (res == null) {
|
|
444 |
return toString;
|
|
445 |
}
|
|
446 |
String comment = getComment(res);
|
|
447 |
if (comment == null) {
|
|
448 |
return toString;
|
|
449 |
}
|
|
450 |
return String.format("%s // %s", toString, comment);
|
|
451 |
}
|
|
452 |
|
43972
|
453 |
public LIRInstructionClass<?> getLIRInstructionClass() {
|
|
454 |
return instructionClass;
|
|
455 |
}
|
46344
|
456 |
|
|
457 |
@Override
|
|
458 |
public int hashCode() {
|
|
459 |
return id;
|
|
460 |
}
|
59095
|
461 |
|
|
462 |
public boolean needsClearUpperVectorRegisters() {
|
|
463 |
return false;
|
|
464 |
}
|
43972
|
465 |
}
|