[PATCH] D154725: [SimplifyLibCalls] Fold strcmp for short string literals
Maksim Kita via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 8 10:33:01 PDT 2023
kitaisreal added a comment.
For such program.
#include <cstring>
bool testFunction1_1(const char* c)
{
return strcmp(c, "0") == 0;
}
GCC emits:
.type _Z15testFunction1_1PKc, @function
_Z15testFunction1_1PKc:
.LFB27:
.cfi_startproc
endbr64
movzbl (%rdi), %eax
subl $48, %eax
jne .L2
movzbl 1(%rdi), %eax
.L2:
testl %eax, %eax
sete %al
ret
.cfi_endproc
.LFE27:
.size _Z15testFunction1_1PKc, .-_Z15testFunction1_1PKc
.p2align 4
.globl _Z15testFunction1_2PKc
.type _Z15testFunction1_2PKc, @function
After changes clang emits:
.type _Z15testFunction1_1PKc, at function
_Z15testFunction1_1PKc: # @_Z15testFunction1_1PKc
.cfi_startproc
# %bb.0: # %entry
movsbl (%rdi), %eax
addl $-48, %eax
je .LBB0_1
# %bb.2: # %strcmp_fold_sub_join
testl %eax, %eax
sete %al
retq
.LBB0_1: # %strcmp_fold_sub_is_zero
movsbl 1(%rdi), %eax
testl %eax, %eax
sete %al
retq
.Lfunc_end0:
.size _Z15testFunction1_1PKc, .Lfunc_end0-_Z15testFunction1_1PKc
.cfi_endproc
================
Comment at: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp:612
if (!HasStr1 && HasStr2) {
if (canTransformToMemCmp(CI, Str1P, Len2, DL))
return copyFlags(
----------------
efriedma wrote:
> nikic wrote:
> > This is the transform that implements what you're trying to do, but taking dereferenceability into account.
> https://github.com/llvm/llvm-project/issues/58003 is requesting that we generate control flow for small strings. For example, transforming `strcmp(c, "x") == 0` to `c[0] == 'x' && c[1] == '\0'`. This is distinct from transforming it to a memcmp. (Of course, the current version of this patch doesn't implement that.)
I updated implementation for single character str2 constant. If current approach is valid I will implement it for two characters str2 constant as well.
I am not sure if current approach with changing control flow inside this optimization is valid and safe, because it seems that other SimplifyLibCalls optimizations does not change control flow.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D154725/new/
https://reviews.llvm.org/D154725
More information about the llvm-commits
mailing list