From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DBF583858D37; Thu, 27 Apr 2023 17:13:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DBF583858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682615603; bh=c6LG4/OogskkS2xyYjm3t/LZ3uN+x/5bKP7qJm0knj4=; h=From:To:Subject:Date:From; b=A/hU3NXo0YggXW1rrH+ent5PlYPpnwlzmA81xW0yUhfmOuNgOCe11KFn4rm5Pk+B5 NX02aA11wGfRhBf1mIesLqo9Ryh7eYMByVlBHhFAMQYjabC2BL/uw6EnRBbcI9dGpV FdAe8AWev9HhntbQ90kXgg1p/d79+qp+FiixLIuo= From: "thierer at web dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/109650] New: avr-gcc incorrect code with -Os Date: Thu, 27 Apr 2023 17:13:23 +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.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: thierer at web dot de 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=3D109650 Bug ID: 109650 Summary: avr-gcc incorrect code with -Os Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: thierer at web dot de Target Milestone: --- Created attachment 54942 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D54942&action=3Dedit preprocessed input file The attached file imho creates invalid code with avr-gcc 12.2.0 when compil= ing with "-Os". The same code works with either different optimization settings= or avr-gcc 11.3.0 (haven't tried any other versions). The misbehaving code is this function: static bool test_func(uint8_t p1, uint8_t p2) { if (p1 =3D=3D 0 || p1 > 7) return false; if (p1 < 3) return p2 <=3D 8; else if (p1 < 5) return p2 <=3D 6; else if (p1 < 7) return p2 <=3D 4; else return p2 <=3D 2; } It should return false for all values of p2 if p1 is 0 or > 7 and the result should depend on p2 for inbetween values of p1. The code contains a lot of boilerplate for sending a showcase output over USART0. This is the result for values of 0 <=3D p2 < 10 (columns) for 0 < p= 1 < 9 (rows): 0123456789 0 ?????????? 1 XXXXXXXXX? 2 XXXXXXXXX? 3 XXXXXX???? 4 XXXXXX???? 5 XXXX?????? 6 XXXX?????? 7 XX???????? 8 ?????????? "X" means test_func() returned true, "?" false. The result for p1 in [0,1,2= ,8] is correct, all the other results are off (too low) by 1. For example, for = p1 =3D=3D 3 the function should return true for all p2 <=3D 6, but it only doe= s for <=3D 5. I'm not too familiar with AVR assembly, but the problem seems to be that the comparisons that calculate the result value all use the same brlo (branch if lower) instruction at .L34, but the compiler fails to compensate for the "lower" instead of "lower or equal" for all but the first (<=3D 8) conditio= n: .L21: cpi r17,lo8(7) brsh .L25 cpi r28,lo8(3) brsh .L12 cpi r29,lo8(9) ; this correctly compares to 9 =3D=3D 8+1 .L34: brlo .L27 .L25: ldi r24,lo8(63) .L11: [...] .L12: cpi r28,lo8(5) brsh .L15 cpi r29,lo8(6) ; but this does not (should be 6+1 =3D=3D 7) rjmp .L34 .L15: cpi r28,lo8(7) breq .L17 cpi r29,lo8(4) ; neither does this (should be 4+1 =3D=3D 5) rjmp .L34 .L17: cpi r29,lo8(2) ; nor this (should be 2+1 =3D=3D 3) rjmp .L34 .L27: Tested with the respective Arch Linux x86_64 avr-gcc packages. Output of "avr-gcc -v": > avr-gcc -v -save-temps -Wall -Wextra -mmcu=3Datmega1284p -Os --param=3Dmi= n-pagesize=3D0 avr-bug.c Using built-in specs. Reading specs from /usr/lib/gcc/avr/12.2.0/device-specs/specs-atmega1284p COLLECT_GCC=3Davr-gcc COLLECT_LTO_WRAPPER=3D/usr/lib/gcc/avr/12.2.0/lto-wrapper Target: avr Configured with: /build/avr-gcc/src/gcc-12.2.0/configure --disable-install-libiberty --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-linker-build-id --disable-nls --disable-werror --disable-__cxa_atexit --enable-checking=3Drelease --enable-clocale=3Dgnu --enable-gnu-unique-object --enable-gold --enable-languages=3Dc,c++ --enable-ld=3Ddefault --enable-lto --enable-plug= in --enable-shared --infodir=3D/usr/share/info --libdir=3D/usr/lib --libexecdir=3D/usr/lib --mandir=3D/usr/share/man --prefix=3D/usr --target= =3Davr --with-as=3D/usr/bin/avr-as --with-gnu-as --with-gnu-ld --with-ld=3D/usr/bi= n/avr-ld --with-plugin-ld=3Dld.gold --with-system-zlib --with-isl --enable-gnu-indirect-function Thread model: single Supported LTO compression algorithms: zlib zstd gcc version 12.2.0 (GCC) COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wall' '-Wextra' '-Os' '--param=3Dmin-pagesize=3D0' '-mdouble=3D32' '-mlong-double=3D64' '-specs=3Ddevice-specs/specs-atmega1284p' '-mmcu=3Davr51' '-dumpdir' 'a-' /usr/lib/gcc/avr/12.2.0/cc1 -E -quiet -v -imultilib avr51 -D__AVR_ATmega1284P__ -D__AVR_DEVICE_NAME__=3Datmega1284p avr-bug.c -mn-fla= sh=3D2 -mno-skip-bug -mdouble=3D32 -mlong-double=3D64 -mmcu=3Davr51 -Wall -Wextra = -Os -fpch-preprocess -o a-avr-bug.i ignoring nonexistent directory "/usr/lib/gcc/avr/12.2.0/../../../../avr/sys-include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/avr/12.2.0/include /usr/lib/gcc/avr/12.2.0/include-fixed /usr/lib/gcc/avr/12.2.0/../../../../avr/include End of search list. COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wall' '-Wextra' '-Os' '--param=3Dmin-pagesize=3D0' '-mdouble=3D32' '-mlong-double=3D64' '-specs=3Ddevice-specs/specs-atmega1284p' '-mmcu=3Davr51' '-dumpdir' 'a-' /usr/lib/gcc/avr/12.2.0/cc1 -fpreprocessed a-avr-bug.i -mn-flash=3D2 -mno-skip-bug -quiet -dumpdir a- -dumpbase avr-bug.c -dumpbase-ext .c -mdouble=3D32 -mlong-double=3D64 -mmcu=3Davr51 -Os -Wall -Wextra -version --param=3Dmin-pagesize=3D0 -o a-avr-bug.s GNU C17 (GCC) version 12.2.0 (avr) compiled by GNU C version 12.1.1 20220730, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.26-GMP warning: MPFR header version 4.1.0-p13 differs from library version 4.2.0. warning: MPC header version 1.2.1 differs from library version 1.3.1. GGC heuristics: --param ggc-min-expand=3D100 --param ggc-min-heapsize=3D131= 072 GNU C17 (GCC) version 12.2.0 (avr) compiled by GNU C version 12.1.1 20220730, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.26-GMP warning: MPFR header version 4.1.0-p13 differs from library version 4.2.0. warning: MPC header version 1.2.1 differs from library version 1.3.1. GGC heuristics: --param ggc-min-expand=3D100 --param ggc-min-heapsize=3D131= 072 Compiler executable checksum: 00c69299beaa7f88846ded0b751d0e18 COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wall' '-Wextra' '-Os' '--param=3Dmin-pagesize=3D0' '-mdouble=3D32' '-mlong-double=3D64' '-specs=3Ddevice-specs/specs-atmega1284p' '-mmcu=3Davr51' '-dumpdir' 'a-' /usr/bin/avr-as -v -mmcu=3Davr51 -mgcc-isr -mno-skip-bug -o a-avr-bug.o a-avr-bug.s GNU assembler version 2.40 (avr) using BFD version (GNU Binutils) 2.40 COMPILER_PATH=3D/usr/lib/gcc/avr/12.2.0/:/usr/lib/gcc/avr/12.2.0/:/usr/lib/= gcc/avr/:/usr/lib/gcc/avr/12.2.0/:/usr/lib/gcc/avr/ LIBRARY_PATH=3D/usr/lib/gcc/avr/12.2.0/avr51/:/usr/lib/gcc/avr/12.2.0/../..= /../../avr/lib/avr51/:/usr/lib/gcc/avr/12.2.0/:/usr/lib/gcc/avr/12.2.0/../.= ./../../avr/lib/ COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wall' '-Wextra' '-Os' '--param=3Dmin-pagesize=3D0' '-mdouble=3D32' '-mlong-double=3D64' '-specs=3Ddevice-specs/specs-atmega1284p' '-mmcu=3Davr51' '-dumpdir' 'a.' /usr/lib/gcc/avr/12.2.0/collect2 -plugin /usr/lib/gcc/avr/12.2.0/liblto_plugin.so -plugin-opt=3D/usr/lib/gcc/avr/12.2.0/lto-wrapper -plugin-opt=3D-fresolutio= n=3Da.res -plugin-opt=3D-pass-through=3D-lgcc -plugin-opt=3D-pass-through=3D-lm -plugin-opt=3D-pass-through=3D-lc -plugin-opt=3D-pass-through=3D-latmega128= 4p -mavr51 -Tdata 0x800100 /usr/lib/gcc/avr/12.2.0/../../../../avr/lib/avr51/crtatmega1284p.o -L/usr/lib/gcc/avr/12.2.0/avr51 -L/usr/lib/gcc/avr/12.2.0/../../../../avr/lib/avr51 -L/usr/lib/gcc/avr/12.2= .0 -L/usr/lib/gcc/avr/12.2.0/../../../../avr/lib a-avr-bug.o --start-group -lg= cc -lm -lc -latmega1284p --end-group COLLECT_GCC_OPTIONS=3D'-v' '-save-temps' '-Wall' '-Wextra' '-Os' '--param=3Dmin-pagesize=3D0' '-mdouble=3D32' '-mlong-double=3D64' '-specs=3Ddevice-specs/specs-atmega1284p' '-mmcu=3Davr51' '-dumpdir' 'a.'=