[PATCH] D35067: [x86, CGP] increase memcmp() expansion up to 4 load pairs

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 6 09:50:41 PDT 2017


spatel created this revision.
Herald added a subscriber: mcrosier.

It should be a win to avoid going out to the system lib for all small memcmp() calls using scalar ops. For x86 32-bit, this means most everything up to 16 bytes. For 64-bit, that doubles because we can do 8-byte loads.

Notes:

1. I don't have a strong opinion about the -Os behavior. Is 4 loads too much code for that case? It's effectively a question of how much do we trust the system implementation. Linux and macOS (and Windows I assume, but did not test) have optimized memcmp() code for x86, so it's probably not bad either way? PPC is using 8/4 for defaults on these. We do not expand at all for -Oz.

2. There are still potential improvements to make for the CGP expansion IR and/or lowering such as avoiding select-of-constants (https://reviews.llvm.org/D34904) and not doing zexts to the max load type before doing a compare.

3. We have special-case SSE/AVX codegen for (memcmp(x, y, 16/32) == 0) that will no longer be produced after this patch. I've shown the experimental justification for that change in PR33329:

https://bugs.llvm.org/show_bug.cgi?id=33329#c12
TLDR: While the vector code is a likely winner, we can't guarantee that it's a winner in all cases on all CPUs, so I'm willing to sacrifice it for the greater good of expanding all small memcmp(). If we want to resurrect that codegen, it can be done by adjusting the CGP params or poking a hole to let those fall-through the CGP expansion.

4. I added the cmov attribute to the 32-bit codegen test because it removes some noise for that file. I think the intent for the SSE vs no-SSE runs is to show the potential difference for the 16 and 32 byte cases rather than the lack of cmov (which has been available for all CPUs since SSE1, so that's why it shows up automatically with -mattr=sse2).


https://reviews.llvm.org/D35067

Files:
  lib/Target/X86/X86ISelLowering.cpp
  test/CodeGen/X86/memcmp.ll
  test/Transforms/CodeGenPrepare/X86/memcmp.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35067.105447.patch
Type: text/x-patch
Size: 129884 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170706/312ec96b/attachment-0001.bin>


More information about the llvm-commits mailing list