Stream: git-cranelift

Topic: cranelift / Issue #873 Fix codegen on a simple branching ...


view this post on Zulip GitHub (Feb 28 2020 at 23:26):

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                      retq

GCC 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                      retq

And 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                      ret

There are some obvious things to fix up:


Last updated: Dec 23 2024 at 14:03 UTC