[llvm-dev] [RFC] Optimizing Comparisons Chains

Clement Courbet via llvm-dev llvm-dev at lists.llvm.org
Wed Jun 7 09:00:40 PDT 2017

Hi all,

I'm working on a new pass to optimize comparison chains.


Clang currently generates inefficient code when dealing with contiguous
member-by-member structural equality. Consider:

struct A {

 bool operator==(const A& o) const { return i == o.i && j == o.j; }

 uint32 i;

 uint32 j;


This generates:

 mov     eax, dword ptr [rdi]

 cmp     eax, dword ptr [rsi]

 jne     .LBB0_1

 mov     eax, dword ptr [rdi + 4]

 cmp     eax, dword ptr [rsi + 4]

 sete    al



 xor     eax, eax


I’ve been working on an LLVM pass that detects this pattern at IR level and
turns it into a memcmp() call. This generates more efficient code:

 mov     rax, qword ptr [rdi]

 cmp     rax, qword ptr [rsi]

 sete    al


And thanks to recent improvements <https://reviews.llvm.org/D28637> in the
memcmp codegen, this can be made to work for all sizes.

Impact of the change

I’ve measured the change on std:pair/std::tuple. The pass typically makes
the code 2-3 times faster with code that’s typically 2-3x times smaller.

A more detailed description can be found here
and a proof of concept can be seen here <https://reviews.llvm.org/D33987>.

Do you see any aspect of this that I may have missed?

For now I’ve implemented this as a separate pass. Would there be a better
way to integrate it?

Thanks !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170607/ce3bb305/attachment.html>

More information about the llvm-dev mailing list