Skip to content

Missed folding memset(len) to memset(len + 1) of zeros #141160

Open
@BreadTom

Description

@BreadTom

Godbolt
GCC bug

Compiling the C code attachment shows f0_slow() is not optimize like f0() with flags -O3. Could be similar to #139810 (comment)

void f0(unsigned char * restrict arg0, unsigned long len){
    __builtin_memset(arg0, 0, len + 1);
}

void f0_slow(unsigned char * restrict arg0, unsigned long len){
        __builtin_memset(arg0, 0, len);
        arg0[len] = '\0';
}
define dso_local void @f0(ptr noalias noundef writeonly captures(none) %arg0, i64 noundef %len) local_unnamed_addr {
entry:
  %add = add i64 %len, 1
  tail call void @llvm.memset.p0.i64(ptr align 1 %arg0, i8 0, i64 %add, i1 false)
  ret void
}

declare void @llvm.memset.p0.i64(ptr writeonly captures(none), i8, i64, i1 immarg) #1

define dso_local void @f0_slow(ptr noalias noundef writeonly captures(none) %arg0, i64 noundef %len) local_unnamed_addr {
entry:
  tail call void @llvm.memset.p0.i64(ptr align 1 %arg0, i8 0, i64 %len, i1 false)
  %arrayidx = getelementptr inbounds nuw i8, ptr %arg0, i64 %len
  store i8 0, ptr %arrayidx, align 1
  ret void
}

clang -v

clang version 21.0.0git (https://github.com/llvm/llvm-project.git 12c62ebcb2008086ad597fffa0f4daf6fe79fe5e)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-trunk-20250522/bin
Found candidate GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0
Selected GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
 (in-process)
 "/opt/compiler-explorer/clang-trunk-20250522/bin/clang-21" -cc1 -triple x86_64-unknown-linux-gnu -S -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name example.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -fno-verbose-asm -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/app -v -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-trunk-20250522/lib/clang/21 -internal-isystem /opt/compiler-explorer/clang-trunk-20250522/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -mllvm --x86-asm-syntax=intel -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /app/output.s -x c <source>
clang -cc1 version 21.0.0git based upon LLVM 21.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/compiler-explorer/clang-trunk-20250522/lib/clang/21/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
Compiler returned: 0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions