alexcrichton transferred Issue #873 (assigned to sstangl):
I thought I'd look into codegen. Consider the following test of a simple branch, where compilers were asked to generate the best code:
int five(int a) { return a > 5 ? 8 : a; }LLVM generates:
0000000000000000 <five>: 0: 83 ff 06 cmp $0x6,%edi 3: b8 05 00 00 00 mov $0x5,%eax 8: 0f 4c c7 cmovl %edi,%eax b: c3 retqGCC generates:
0000000000000000 <five>: 0: 89 f8 mov %edi,%eax 2: 83 ff 06 cmp $0x6,%edi 5: ba 08 00 00 00 mov $0x8,%edx a: 0f 4d c2 cmovge %edx,%eax d: c3 retqAnd Cranelift generates (via clang-compiled wasm):
Disassembly of 22 bytes: 0: 55 push rbp 1: 48 89 e5 mov rbp, rsp 4: b8 08 00 00 00 mov eax, 8 9: 83 ff 05 cmp edi, 5 c: 7f 04 jg 0x12 e: 89 f8 mov eax, edi 10: eb 02 jmp 0x14 12: eb fc jmp 0x10 14: 5d pop rbp 15: c3 retThere are some obvious things to fix up:
- [ ] Constructing/destructing stack frames can be skipped if the stack is unused.
- [x]
0xc
jumps conditionally to0x12
, which is an unconditional jump to0x10
, which is an unconditional jump to0x14
, which is an unconditional jump to the end of the function.- [ ] Simple branches can be folded into
cmov
on x86. In this case we havecmp
..jg
..mov
..jmp
which is exactly what acmov
does.
Last updated: Dec 23 2024 at 14:03 UTC