From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 835DB3857013; Wed, 26 Oct 2022 11:57:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 835DB3857013 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666785431; bh=I7FXi1d0o+wfI1I+ZTlXubRqVAKL/Wxv+k/FBEoK8uw=; h=From:To:Subject:Date:From; b=TEPO3DAojKfdIIskgW7iE92T1k6jXuVE8sCyXI54ZhaQ/S4KqeK5bh6nrUgEVG5lL dL0bOvIM2TpcVCSUHwni2t9y8WysQOp/yRbbNKVYqstoEcsl0tSZ6aFvmOxs7NXKa8 yKpTSIlHprxnxZKNluggiJa79VkXvpXRQGqCA0uA= From: "michael.meier at hexagon dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/107415] New: RISCV-gcc: Leaf function compiles as recursive with -O3 Date: Wed, 26 Oct 2022 11:57:09 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: michael.meier at hexagon dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107415 Bug ID: 107415 Summary: RISCV-gcc: Leaf function compiles as recursive with -O3 Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: michael.meier at hexagon dot com Target Milestone: --- Created attachment 53776 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D53776&action=3Dedit The -save-temps output as requested I am using gcc 12.1.0 to compile for RISC-V. Compiling the following leaf function (it's in the .i file in the attachment) results in gcc generating a recursive call to memset itself, resulting in an endless loop. The code is a slightly reduced version function that led me to finding this issue. It does not make sense as memset, but it triggers the issue. void* memset(void* dest, int byte, size_t len) { if ((((uintptr_t)dest | len) & (sizeof(uintptr_t)-1)) =3D=3D 0) { uintptr_t *d =3D dest; *d =3D byte; } else { char *d =3D dest; while (d < (char*)(dest + len)) *d++ =3D byte; } return dest; } First lines of the objdump output of the generated object file: 00000000 : 0: ff010113 addi sp,sp,-16 4: 00812423 sw s0,8(sp) 8: 00912223 sw s1,4(sp) c: 00112623 sw ra,12(sp) 10: 01010413 addi s0,sp,16 14: 00c56733 or a4,a0,a2 18: 00377713 andi a4,a4,3 1c: 00050493 mv s1,a0 20: 02070a63 beqz a4,54 <.L2> 20: R_RISCV_BRANCH .L2 00000024 <.LBB2>: 24: 00c507b3 add a5,a0,a2 28: 00f57a63 bgeu a0,a5,3c <.L4> 28: R_RISCV_BRANCH .L4 2c: 01859593 slli a1,a1,0x18 00000030 <.LVL2>: 30: 4185d593 srai a1,a1,0x18 34: 00000097 auipc ra,0x0 34: R_RISCV_CALL memset 34: R_RISCV_RELAX *ABS* 38: 000080e7 jalr ra # 34 <.LVL2+0x4> At address 38 inside we find a call to memset. This instruction can be jump= ed over if the right conditions on a4 and a0 happen to be true. However running the function up to the jalr memset does not change any of these conditions. After calling memset, the PC will arrive will arrive at the jalr memset aga= in, resulting in an endless loop. Output of gcc -v: Using built-in specs. COLLECT_GCC=3D/home/memi/Downloads/riscv/bin/riscv64-unknown-elf-gcc COLLECT_LTO_WRAPPER=3D/home/memi/Downloads/riscv/bin/../libexec/gcc/riscv64= -unknown-elf/12.1.0/lto-wrapper Target: riscv64-unknown-elf Configured with: /tmp/rv_gcc/riscv-gnu-toolchain/gcc/configure --target=3Driscv64-unknown-elf --prefix=3D/opt/riscv --disable-shared --disable-threads --enable-languages=3Dc,c++ --with-pkgversion=3Dg1ea978e30= 66 --with-system-zlib --enable-tls --with-newlib --with-sysroot=3D/opt/riscv/riscv64-unknown-elf --with-native-system-header-dir=3D/include --disable-libmudflap --disable-l= ibssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=3D/tmp/rv_gcc/riscv-gnu-toolchain/gcc --enable-multilib --with-multilib-generator=3D'rv32i-ilp32--;rv32ic-ilp32--;rv32im-ilp32--;rv= 32imc-ilp32--' --with-abi=3Dlp64d --with-arch=3Drv64imafdc --with-tune=3Drocket --with-isa= -spec=3D2.2 'CFLAGS_FOR_TARGET=3D-Os -mcmodel=3Dmedlow' 'CXXFLAGS_FOR_TARGET=3D-Os=20= =20 -mcmodel=3Dmedlow' Thread model: single Supported LTO compression algorithms: zlib gcc version 12.1.0 (g1ea978e3066)=20 Complete command line that triggers the problem: /home/memi/Downloads/riscv/bin/riscv64-unknown-elf-gcc -c own-memset.c -O3 -DNDEBUG -march=3Drv32im -mabi=3Dilp32 -msmall-data-limit=3D8 -mno-save-res= tore -fmessage-length=3D0 -fsigned-char -ffunction-sections -fdata-sections -g -fno-omit-frame-pointer -std=3Dgnu11 -MD -MT softconsole_oriole_main/CMakeFiles/MIV_Project.dir/ISP_PRO.c.obj -save-tem= ps The compiler output (on the console): None The preprocessed file is attached.=