| 1 |
// |jit-test| skip-if: !hasDisassembler() || wasmCompileMode() != "ion" || !getBuildConfiguration("x64") || getBuildConfiguration("simulator"); include:codegen-x64-test.js |
| 2 |
|
| 3 |
// There may be some redundant moves to set some operations up on x64 (that's |
| 4 |
// bug 1701164) so we avoid those with no_prefix/no_suffix flags when the issue |
| 5 |
// comes up. |
| 6 |
|
| 7 |
// Test that multiplication by -1 yields negation. |
| 8 |
|
| 9 |
let neg32 = |
| 10 |
`(module |
| 11 |
(func (export "f") (param i32) (result i32) |
| 12 |
(i32.mul (local.get 0) (i32.const -1))))`; |
| 13 |
codegenTestX64_adhoc( |
| 14 |
neg32, |
| 15 |
'f', |
| 16 |
'f7 d8 neg %eax', {no_prefix:true}); |
| 17 |
assertEq(wasmEvalText(neg32).exports.f(-37), 37) |
| 18 |
assertEq(wasmEvalText(neg32).exports.f(42), -42) |
| 19 |
|
| 20 |
let neg64 = |
| 21 |
`(module |
| 22 |
(func (export "f") (param i64) (result i64) |
| 23 |
(i64.mul (local.get 0) (i64.const -1))))`; |
| 24 |
codegenTestX64_adhoc( |
| 25 |
neg64, |
| 26 |
'f', |
| 27 |
'48 f7 d8 neg %rax', {no_prefix:true}); |
| 28 |
assertEq(wasmEvalText(neg64).exports.f(-37000000000n), 37000000000n) |
| 29 |
assertEq(wasmEvalText(neg64).exports.f(42000000000n), -42000000000n) |
| 30 |
|
| 31 |
// Test that multiplication by zero yields zero |
| 32 |
|
| 33 |
let zero32 = |
| 34 |
`(module |
| 35 |
(func (export "f") (param i32) (result i32) |
| 36 |
(i32.mul (local.get 0) (i32.const 0))))`; |
| 37 |
codegenTestX64_adhoc( |
| 38 |
zero32, |
| 39 |
'f', |
| 40 |
'33 c0 xor %eax, %eax', {no_prefix:true}); |
| 41 |
assertEq(wasmEvalText(zero32).exports.f(-37), 0) |
| 42 |
assertEq(wasmEvalText(zero32).exports.f(42), 0) |
| 43 |
|
| 44 |
let zero64 = `(module |
| 45 |
(func (export "f") (param i64) (result i64) |
| 46 |
(i64.mul (local.get 0) (i64.const 0))))` |
| 47 |
codegenTestX64_adhoc( |
| 48 |
zero64, |
| 49 |
'f', |
| 50 |
'48 33 c0 xor %rax, %rax', {no_prefix:true}); |
| 51 |
assertEq(wasmEvalText(zero64).exports.f(-37000000000n), 0n) |
| 52 |
assertEq(wasmEvalText(zero64).exports.f(42000000000n), 0n) |
| 53 |
|
| 54 |
// Test that multiplication by one yields no code |
| 55 |
|
| 56 |
let one32 = |
| 57 |
`(module |
| 58 |
(func (export "f") (param i32) (result i32) |
| 59 |
(i32.mul (local.get 0) (i32.const 1))))`; |
| 60 |
codegenTestX64_adhoc( |
| 61 |
one32, |
| 62 |
'f', |
| 63 |
'', {no_prefix:true}); |
| 64 |
assertEq(wasmEvalText(one32).exports.f(-37), -37) |
| 65 |
assertEq(wasmEvalText(one32).exports.f(42), 42) |
| 66 |
|
| 67 |
let one64 = `(module |
| 68 |
(func (export "f") (param i64) (result i64) |
| 69 |
(i64.mul (local.get 0) (i64.const 1))))` |
| 70 |
codegenTestX64_adhoc( |
| 71 |
one64, |
| 72 |
'f', |
| 73 |
'', {no_prefix:true}); |
| 74 |
assertEq(wasmEvalText(one64).exports.f(-37000000000n), -37000000000n) |
| 75 |
assertEq(wasmEvalText(one64).exports.f(42000000000n), 42000000000n) |
| 76 |
|
| 77 |
// Test that multiplication by two yields an add |
| 78 |
|
| 79 |
let double32 = |
| 80 |
`(module |
| 81 |
(func (export "f") (param i32) (result i32) |
| 82 |
(i32.mul (local.get 0) (i32.const 2))))`; |
| 83 |
codegenTestX64_adhoc( |
| 84 |
double32, |
| 85 |
'f', |
| 86 |
'03 c0 add %eax, %eax', {no_prefix:true}); |
| 87 |
assertEq(wasmEvalText(double32).exports.f(-37), -74) |
| 88 |
assertEq(wasmEvalText(double32).exports.f(42), 84) |
| 89 |
|
| 90 |
let double64 = `(module |
| 91 |
(func (export "f") (param i64) (result i64) |
| 92 |
(i64.mul (local.get 0) (i64.const 2))))` |
| 93 |
codegenTestX64_adhoc( |
| 94 |
double64, |
| 95 |
'f', |
| 96 |
'48 03 c0 add %rax, %rax', {no_prefix:true}); |
| 97 |
assertEq(wasmEvalText(double64).exports.f(-37000000000n), -74000000000n) |
| 98 |
assertEq(wasmEvalText(double64).exports.f(42000000000n), 84000000000n) |
| 99 |
|
| 100 |
// Test that multiplication by four yields a shift |
| 101 |
|
| 102 |
let quad32 = |
| 103 |
`(module |
| 104 |
(func (export "f") (param i32) (result i32) |
| 105 |
(i32.mul (local.get 0) (i32.const 4))))`; |
| 106 |
codegenTestX64_adhoc( |
| 107 |
quad32, |
| 108 |
'f', |
| 109 |
'c1 e0 02 shl \\$0x02, %eax', {no_prefix:true}); |
| 110 |
assertEq(wasmEvalText(quad32).exports.f(-37), -148) |
| 111 |
assertEq(wasmEvalText(quad32).exports.f(42), 168) |
| 112 |
|
| 113 |
let quad64 = `(module |
| 114 |
(func (export "f") (param i64) (result i64) |
| 115 |
(i64.mul (local.get 0) (i64.const 4))))` |
| 116 |
codegenTestX64_adhoc( |
| 117 |
quad64, |
| 118 |
'f', |
| 119 |
'48 c1 e0 02 shl \\$0x02, %rax', {no_prefix:true}); |
| 120 |
assertEq(wasmEvalText(quad64).exports.f(-37000000000n), -148000000000n) |
| 121 |
assertEq(wasmEvalText(quad64).exports.f(42000000000n), 168000000000n) |
| 122 |
|
| 123 |
// Test that multiplication by five yields a multiply |
| 124 |
|
| 125 |
let quint32 = |
| 126 |
`(module |
| 127 |
(func (export "f") (param i32) (result i32) |
| 128 |
(i32.mul (local.get 0) (i32.const 5))))`; |
| 129 |
codegenTestX64_adhoc( |
| 130 |
quint32, |
| 131 |
'f', |
| 132 |
'6b c0 05 imul \\$0x05, %eax, %eax', {no_prefix:true}); |
| 133 |
assertEq(wasmEvalText(quint32).exports.f(-37), -37*5) |
| 134 |
assertEq(wasmEvalText(quint32).exports.f(42), 42*5) |
| 135 |
|
| 136 |
let quint64 = `(module |
| 137 |
(func (export "f") (param i64) (result i64) |
| 138 |
(i64.mul (local.get 0) (i64.const 5))))` |
| 139 |
codegenTestX64_adhoc( |
| 140 |
quint64, |
| 141 |
'f', |
| 142 |
`48 6b c0 05 imul \\$0x05, %rax, %rax`, {no_prefix:true}) |
| 143 |
assertEq(wasmEvalText(quint64).exports.f(-37000000000n), -37000000000n*5n) |
| 144 |
assertEq(wasmEvalText(quint64).exports.f(42000000000n), 42000000000n*5n) |
| 145 |
|
| 146 |
// Test that 0-n yields negation. |
| 147 |
|
| 148 |
let subneg32 = |
| 149 |
`(module |
| 150 |
(func (export "f") (param i32) (result i32) |
| 151 |
(i32.sub (i32.const 0) (local.get 0))))` |
| 152 |
codegenTestX64_adhoc( |
| 153 |
subneg32, |
| 154 |
'f', |
| 155 |
'f7 d8 neg %eax', {no_prefix:true}); |
| 156 |
assertEq(wasmEvalText(subneg32).exports.f(-37), 37) |
| 157 |
assertEq(wasmEvalText(subneg32).exports.f(42), -42) |
| 158 |
|
| 159 |
let subneg64 = |
| 160 |
`(module |
| 161 |
(func (export "f") (param i64) (result i64) |
| 162 |
(i64.sub (i64.const 0) (local.get 0))))` |
| 163 |
codegenTestX64_adhoc( |
| 164 |
subneg64, |
| 165 |
'f', |
| 166 |
'48 f7 d8 neg %rax', {no_prefix:true}); |
| 167 |
assertEq(wasmEvalText(subneg64).exports.f(-37000000000n), 37000000000n) |
| 168 |
assertEq(wasmEvalText(subneg64).exports.f(42000000000n), -42000000000n) |
| 169 |
|
| 170 |
// AND{32,64} followed by `== 0`: check the two operations are merged into a |
| 171 |
// single 'test' insn, and no 'and' insn. The merging isn't done for |
| 172 |
// {OR,XOR}{32,64}. This is for both arguments being non-constant. |
| 173 |
|
| 174 |
for ( [ty, expect_test] of |
| 175 |
[['i32', '85 .. test %e.., %e..'], |
| 176 |
['i64', '48 85 .. test %r.., %r..']] ) { |
| 177 |
codegenTestX64_adhoc( |
| 178 |
`(module |
| 179 |
(func (export "f") (param $p1 ${ty}) (param $p2 ${ty}) (result i32) |
| 180 |
(local $x i32) |
| 181 |
(local.set $x (i32.const 0x4D2)) |
| 182 |
(if (${ty}.eq (${ty}.and (local.get $p1) (local.get $p2)) |
| 183 |
(${ty}.const 0)) |
| 184 |
(then (local.set $x (i32.const 0x11D7))) |
| 185 |
) |
| 186 |
(local.get $x) |
| 187 |
) |
| 188 |
)`, |
| 189 |
'f', |
| 190 |
`${expect_test} |
| 191 |
0f 85 .. 00 00 00 jnz 0x00000000000000.. |
| 192 |
b8 d7 11 00 00 mov \\$0x11D7, %eax |
| 193 |
e9 .. 00 00 00 jmp 0x00000000000000.. |
| 194 |
b8 d2 04 00 00 mov \\$0x4D2, %eax` |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
// AND64 followed by `== 0`, with one of the args being a constant. Depending |
| 199 |
// on the constant we can get one of three forms. |
| 200 |
|
| 201 |
for ( [imm, expect1, expect2] of |
| 202 |
[ // in signed-32 range => imm in insn |
| 203 |
['0x17654321', |
| 204 |
'f7 c. 21 43 65 17 test \\$0x17654321, %e..', // edi or ecx |
| 205 |
''], |
| 206 |
// in unsigned-32 range => imm in reg via movl |
| 207 |
['0x87654321', |
| 208 |
'41 bb 21 43 65 87 mov \\$-0x789ABCDF, %r11d', |
| 209 |
'4c 85 d. test %r11, %r..'], // rdi or rcx |
| 210 |
// not in either range => imm in reg via mov(absq) |
| 211 |
['0x187654321', |
| 212 |
'49 bb 21 43 65 87 01 00 00 00 mov \\$0x187654321, %r11', |
| 213 |
'4c 85 d. test %r11, %r..']] // rdi or rcx |
| 214 |
) { |
| 215 |
codegenTestX64_adhoc( |
| 216 |
`(module |
| 217 |
(func (export "f") (param $p1 i64) (result i32) |
| 218 |
(local $x i32) |
| 219 |
(local.set $x (i32.const 0x4D2)) |
| 220 |
(if (i64.eq (i64.and (i64.const ${imm}) (local.get $p1)) |
| 221 |
(i64.const 0)) |
| 222 |
(then (local.set $x (i32.const 0x11D7))) |
| 223 |
) |
| 224 |
(local.get $x) |
| 225 |
) |
| 226 |
)`, |
| 227 |
'f', |
| 228 |
`${expect1} |
| 229 |
${expect2} |
| 230 |
0f 85 .. 00 00 00 jnz 0x00000000000000.. |
| 231 |
b8 d7 11 00 00 mov \\$0x11D7, %eax |
| 232 |
e9 .. 00 00 00 jmp 0x00000000000000.. |
| 233 |
b8 d2 04 00 00 mov \\$0x4D2, %eax` |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
// For integer comparison followed by select, check that the comparison result |
| 238 |
// isn't materialised into a register, for specific types. |
| 239 |
|
| 240 |
function cmpSel32vs64(cmpTy, cmpOp, selTy) { |
| 241 |
return `(module |
| 242 |
(func (export "f") |
| 243 |
(param $p1 ${cmpTy}) (param $p2 ${cmpTy}) |
| 244 |
(param $p3 ${selTy}) (param $p4 ${selTy}) |
| 245 |
(result ${selTy}) |
| 246 |
(select (local.get $p3) |
| 247 |
(local.get $p4) |
| 248 |
(${cmpTy}.${cmpOp} (local.get $p1) (local.get $p2))) |
| 249 |
) |
| 250 |
)`; |
| 251 |
} |
| 252 |
if (getBuildConfiguration("windows")) { |
| 253 |
for ( [cmpTy, cmpOp, selTy, insn1, insn2, insn3] of |
| 254 |
[ ['i32', 'le_s', 'i32', '8b c3 mov %ebx, %eax', |
| 255 |
'3b ca cmp %edx, %ecx', |
| 256 |
'41 0f 4f c1 cmovnle %r9d, %eax'], |
| 257 |
['i32', 'lt_u', 'i64', '48 89 d8 mov %rbx, %rax', |
| 258 |
'3b ca cmp %edx, %ecx', |
| 259 |
'49 0f 43 c1 cmovnb %r9, %rax'], |
| 260 |
['i64', 'le_s', 'i32', '8b c3 mov %ebx, %eax', |
| 261 |
'48 3b ca cmp %rdx, %rcx', |
| 262 |
'41 0f 4f c1 cmovnle %r9d, %eax'], |
| 263 |
['i64', 'lt_u', 'i64', '48 89 d8 mov %rbx, %rax', |
| 264 |
'48 3b ca cmp %rdx, %rcx', |
| 265 |
'49 0f 43 c1 cmovnb %r9, %rax'] |
| 266 |
] ) { |
| 267 |
codegenTestX64_adhoc(cmpSel32vs64(cmpTy, cmpOp, selTy), 'f', |
| 268 |
`4. (89 c3|8b d8) mov %r8.*, %.bx |
| 269 |
${insn1} |
| 270 |
${insn2} |
| 271 |
${insn3}` |
| 272 |
); |
| 273 |
} |
| 274 |
} else { |
| 275 |
for ( [cmpTy, cmpOp, selTy, insn1, insn2, insn3] of |
| 276 |
[ ['i32', 'le_s', 'i32', '8b c2 mov %edx, %eax', |
| 277 |
'3b fe cmp %esi, %edi', |
| 278 |
'0f 4f c1 cmovnle %ecx, %eax'], |
| 279 |
['i32', 'lt_u', 'i64', '48 89 d0 mov %rdx, %rax', |
| 280 |
'3b fe cmp %esi, %edi', |
| 281 |
'48 0f 43 c1 cmovnb %rcx, %rax'], |
| 282 |
['i64', 'le_s', 'i32', '8b c2 mov %edx, %eax', |
| 283 |
'48 3b fe cmp %rsi, %rdi', |
| 284 |
'0f 4f c1 cmovnle %ecx, %eax'], |
| 285 |
['i64', 'lt_u', 'i64', '48 89 d0 mov %rdx, %rax', |
| 286 |
'48 3b fe cmp %rsi, %rdi', |
| 287 |
'48 0f 43 c1 cmovnb %rcx, %rax'] |
| 288 |
] ) { |
| 289 |
codegenTestX64_adhoc(cmpSel32vs64(cmpTy, cmpOp, selTy), 'f', |
| 290 |
`${insn1} |
| 291 |
${insn2} |
| 292 |
${insn3}` |
| 293 |
); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// For integer comparison followed by select, check correct use of operands in |
| 298 |
// registers vs memory. At least for the 64-bit-cmp/64-bit-sel case. |
| 299 |
|
| 300 |
for ( [pAnyCmp, pAnySel, cmpBytes, cmpArgL, cmovBytes, cmovArgL ] of |
| 301 |
[ // r, r |
| 302 |
['$pReg1', '$pReg2', |
| 303 |
'4. .. ..', '%r.+', '4. .. .. ..', '%r.+'], |
| 304 |
// r, m |
| 305 |
['$pReg1', '$pMem2', |
| 306 |
'4. .. ..', '%r.+', '4. .. .. .. ..', '0x..\\(%rbp\\)'], |
| 307 |
// m, r |
| 308 |
['$pMem1', '$pReg2', |
| 309 |
'4. .. .. ..', '0x..\\(%rbp\\)', '4. .. .. ..', '%r.+'], |
| 310 |
// m, m |
| 311 |
['$pMem1', '$pMem2', |
| 312 |
'4. .. .. ..', '0x..\\(%rbp\\)', '4. .. .. .. ..', '0x..\\(%rbp\\)'] |
| 313 |
] ) { |
| 314 |
codegenTestX64_adhoc( |
| 315 |
`(module |
| 316 |
(func (export "f") |
| 317 |
(param $p1 i64) (param $p2 i64) ;; in regs for both ABIs |
| 318 |
(param $pReg1 i64) (param $pReg2 i64) ;; in regs for both ABIs |
| 319 |
(param i64) (param i64) ;; ignored |
| 320 |
(param $pMem1 i64) (param $pMem2 i64) ;; in mem for both ABIs |
| 321 |
(result i64) |
| 322 |
(select (local.get $p1) |
| 323 |
(local.get ${pAnySel}) |
| 324 |
(i64.eq (local.get $p2) (local.get ${pAnyCmp}))) |
| 325 |
) |
| 326 |
)`, |
| 327 |
'f', |
| 328 |
// On Linux we have an extra move |
| 329 |
(getBuildConfiguration("windows") ? '' : '48 89 .. mov %r.+, %r.+\n') + |
| 330 |
// 'q*' because the disassembler shows 'q' only for the memory cases |
| 331 |
`48 89 .. mov %r.+, %r.+ |
| 332 |
${cmpBytes} cmpq* ${cmpArgL}, %r.+ |
| 333 |
${cmovBytes} cmovnzq* ${cmovArgL}, %r.+` |
| 334 |
); |
| 335 |
} |