Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cranelift/assembler-x64/meta/src/generate/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ impl dsl::Format {
f.empty_line();
f.comment("Emit ModR/M byte.");
}
let bytes_at_end = match self.operands_by_kind().as_slice() {
[.., Imm(imm)] => imm.bytes(),
_ => 0,
};

match self.operands_by_kind().as_slice() {
[FixedReg(_)] | [FixedReg(_), FixedReg(_)] | [FixedReg(_), Imm(_)] => {
Expand All @@ -163,7 +167,10 @@ impl dsl::Format {
| [FixedReg(_), FixedReg(_), RegMem(mem)] => {
let digit = rex.digit.unwrap();
fmtln!(f, "let digit = 0x{digit:x};");
fmtln!(f, "self.{mem}.encode_rex_suffixes(buf, off, digit, 0);");
fmtln!(
f,
"self.{mem}.encode_rex_suffixes(buf, off, digit, {bytes_at_end});"
);
}
[Reg(reg), RegMem(mem)]
| [Reg(reg), RegMem(mem), Imm(_)]
Expand All @@ -172,7 +179,10 @@ impl dsl::Format {
| [RegMem(mem), Reg(reg), Imm(_)]
| [RegMem(mem), Reg(reg), FixedReg(_)] => {
fmtln!(f, "let reg = self.{reg}.enc();");
fmtln!(f, "self.{mem}.encode_rex_suffixes(buf, off, reg, 0);");
fmtln!(
f,
"self.{mem}.encode_rex_suffixes(buf, off, reg, {bytes_at_end});"
);
}
unknown => unimplemented!("unknown pattern: {unknown:?}"),
}
Expand Down
64 changes: 64 additions & 0 deletions cranelift/filetests/filetests/isa/x64/mul.clif
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,67 @@ block0(v0: i8, v1: i8):
; popq %rbp
; retq

function %imul_and_constant_pool_small_immediate() -> i32 {
block0:
v0 = iconst.i32 7
v1 = iconst.i32 0x22222222
v2 = imul v0, v1
return v2
}

; VCode:
; pushq %rbp
; movq %rsp, %rbp
; block0:
; imull $0x7, (%rip), %eax
; movq %rbp, %rsp
; popq %rbp
; ret
;
; Disassembled:
; block0: ; offset 0x0
; pushq %rbp
; movq %rsp, %rbp
; block1: ; offset 0x4
; imull $7, 5(%rip), %eax
; movq %rbp, %rsp
; popq %rbp
; retq
; andb (%rdx), %ah
; andb (%rdx), %ah
; addb %al, (%rax)
; addb %al, (%rax)

function %imul_and_constant_pool_big_immediate() -> i32 {
block0:
v0 = iconst.i32 0x11111111
v1 = iconst.i32 0x22222222
v2 = imul v0, v1
return v2
}

; VCode:
; pushq %rbp
; movq %rsp, %rbp
; block0:
; imull $0x11111111, (%rip), %eax
; movq %rbp, %rsp
; popq %rbp
; ret
;
; Disassembled:
; block0: ; offset 0x0
; pushq %rbp
; movq %rsp, %rbp
; block1: ; offset 0x4
; imull $0x11111111, 0xa(%rip), %eax
; movq %rbp, %rsp
; popq %rbp
; retq
; addb %al, (%rax)
; addb %al, (%rax)
; addb %ah, (%rdx)
; andb (%rdx), %ah
; andb (%rax), %al
; addb %al, (%rax)

18 changes: 18 additions & 0 deletions cranelift/filetests/filetests/runtests/arithmetic.clif
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,21 @@ block0(v0: i8):
; run: %udiv_i8_const(0) == 0
; run: %udiv_i8_const(-1) == 1
; run: %udiv_i8_const(0xFE) == 1

function %imul_small_constant() -> i32 {
block0:
v0 = iconst.i32 7
v1 = iconst.i32 0x22222222
v2 = imul v0, v1
return v2
}
; run: %imul_small_constant() == -286331154

function %imul_big_constant() -> i32 {
block0:
v0 = iconst.i32 0x11111111
v1 = iconst.i32 0x22222222
v2 = imul v0, v1
return v2
}
; run: %imul_big_constant() == 248153666
Loading