Skip to content

Commit 6d27c58

Browse files
committed
Hacky pass at lea-ifying add/sub/shl
1 parent 38e19af commit 6d27c58

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

compiler/src/org.graalvm.compiler.core.amd64/src/org/graalvm/compiler/core/amd64/AMD64ArithmeticLIRGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private Variable emitBinaryConst(LIRKind resultKind, AMD64BinaryArithmetic op, O
224224
}
225225
}
226226

227-
getLIRGen().append(new AMD64Binary.ConstOp(op, size, result, a, constant));
227+
getLIRGen().append(new AMD64Binary.ConstOp(op, size, result, a, constant, setFlags));
228228
return result;
229229
} else {
230230
return emitBinaryVar(resultKind, op.getRMOpcode(size), size, commutative, a, getLIRGen().asAllocatable(b));
@@ -695,7 +695,7 @@ private Variable emitShift(AMD64Shift op, OperandSize size, Value a, Value b) {
695695
* c is implicitly masked to 5 or 6 bits by the CPU, so casting it to (int) is
696696
* always correct, even without the NumUtil.is32bit() test.
697697
*/
698-
getLIRGen().append(new AMD64Binary.ConstOp(op.miOp, size, result, input, (int) c.asLong()));
698+
getLIRGen().append(new AMD64Binary.ConstOp(op.miOp, size, result, input, (int) c.asLong(), false));
699699
}
700700
} else {
701701
getLIRGen().emitMove(RCX_I, b);

compiler/src/org.graalvm.compiler.lir.amd64/src/org/graalvm/compiler/lir/amd64/AMD64Binary.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static jdk.vm.ci.code.ValueUtil.isRegister;
3333
import static jdk.vm.ci.code.ValueUtil.isStackSlot;
3434

35+
import jdk.vm.ci.code.Register;
3536
import org.graalvm.compiler.core.common.NumUtil;
3637
import org.graalvm.compiler.asm.amd64.AMD64Address;
3738
import org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic;
@@ -218,23 +219,39 @@ public static class ConstOp extends AMD64LIRInstruction {
218219
@Def({REG, HINT}) protected AllocatableValue result;
219220
@Use({REG}) protected AllocatableValue x;
220221
private final int y;
222+
private final boolean setFlags;
221223

222-
public ConstOp(AMD64BinaryArithmetic opcode, OperandSize size, AllocatableValue result, AllocatableValue x, int y) {
223-
this(opcode.getMIOpcode(size, NumUtil.isByte(y)), size, result, x, y);
224+
public ConstOp(AMD64BinaryArithmetic opcode, OperandSize size, AllocatableValue result, AllocatableValue x, int y, boolean setFlags) {
225+
this(opcode.getMIOpcode(size, NumUtil.isByte(y)), size, result, x, y, setFlags);
224226
}
225227

226-
public ConstOp(AMD64MIOp opcode, OperandSize size, AllocatableValue result, AllocatableValue x, int y) {
228+
public ConstOp(AMD64MIOp opcode, OperandSize size, AllocatableValue result, AllocatableValue x, int y, boolean setFlags) {
227229
super(TYPE);
228230
this.opcode = opcode;
229231
this.size = size;
230232

231233
this.result = result;
232234
this.x = x;
233235
this.y = y;
236+
this.setFlags = setFlags;
234237
}
235238

236239
@Override
237240
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
241+
if (!setFlags && isRegister(result) && isRegister(x) && !result.equals(x)) {
242+
if (opcode.toString().equals("ADD")) {
243+
masm.leaq(asRegister(result), new AMD64Address(asRegister(x), y));
244+
return;
245+
}
246+
if (opcode.toString().equals("SUB")) {
247+
masm.leaq(asRegister(result), new AMD64Address(asRegister(x), -y));
248+
return;
249+
}
250+
if (opcode.toString().equals("SHL") && y >= 1 && y <= 3) {
251+
masm.leaq(asRegister(result), new AMD64Address(Register.None, asRegister(x), AMD64Address.Scale.fromShift(y)));
252+
return;
253+
}
254+
}
238255
AMD64Move.move(crb, masm, result, x);
239256
opcode.emit(masm, size, asRegister(result), y);
240257
}

0 commit comments

Comments
 (0)