This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt][builtins] Simplify __ucmpdi2
Needs ReviewPublic

Authored by Kayjukh on Jun 27 2021, 1:31 AM.

Details

Reviewers
cjdb
efriedma
Summary

When plugged into the current version of clang at optimization level O3,
the initial code produces an assembly output that looks like the
following on an x86 target:

__ucmpdi2:
  mov     rcx, rdi
  shr     rcx, 32
  mov     rdx, rsi
  shr     rdx, 32
  xor     eax, eax
  cmp     ecx, edx
  jb      .LBB0_4
  mov     eax, 2
  ja      .LBB0_4
  mov     eax, 0
  cmp     edi, esi
  jb      .LBB0_4
  cmp     esi, edi
  mov     eax, 1
  adc     eax, 0
.LBB0_4:
  ret

In constrast, the new version produces

__ucmpdi2:
  xor     eax, eax
  cmp     rdi, rsi
  seta    al
  sbb     eax, 0
  add     eax, 1
  ret

We can safely use arithmetic on booleans here, since both the C++ and C
standard guarantee that a bool to int conversion will produce either
zero (for false) or one (for true).

Diff Detail

Event Timeline

Kayjukh created this revision.Jun 27 2021, 1:31 AM
Kayjukh requested review of this revision.Jun 27 2021, 1:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2021, 1:31 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript

LLVM never generates calls to __ucmpdi2, as far as I can tell. Do you have some other use-case?

I don't have any specific use case. I think it may still be worth simplifying the function if it ever happens to be emitted by something.