Bug 59394 - Unused code generated
Summary: Unused code generated
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.8.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2013-12-05 11:06 UTC by smalcom
Modified: 2021-12-23 08:04 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
source code (591 bytes, text/x-c)
2013-12-05 11:06 UTC, smalcom
Details
generated assembly (2.71 KB, text/plain)
2013-12-05 11:07 UTC, smalcom
Details
C++ source. (455 bytes, text/plain)
2013-12-22 15:11 UTC, Georg-Johann Lay
Details
Generated assembly when using "gc-section". avr-gcc-10.2 (1.95 KB, text/plain)
2021-12-23 08:02 UTC, smalcom
Details
Generated assembly when using "LTO". avr-gcc-10.2 (1.89 KB, text/plain)
2021-12-23 08:04 UTC, smalcom
Details

Note You need to log in before you can comment on or make changes to this bug.
Description smalcom 2013-12-05 11:06:41 UTC
Created attachment 31385 [details]
source code

GCC:
avr-gcc -v
Using built-in specs.
COLLECT_GCC=avr-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/avr/4.8.2/lto-wrapper
Target: avr
Configured with: /build/avr-gcc/src/gcc-4.8.2/configure --disable-cloog-version-check --disable-install-libiberty --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-linker-build-id --disable-nls --disable-werror --enable-__cxa_atexit --enable-checking=release --enable-clocale=gnu --enable-cloog-backend=isl --enable-gnu-unique-object --enable-gold --enable-languages=c,c++ --enable-ld=default --enable-lto --enable-plugin --enable-shared --infodir=/usr/share/info --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --prefix=/usr --target=avr --with-as=/usr/bin/avr-as --with-gnu-as --with-gnu-ld --with-ld=/usr/bin/avr-ld --with-plugin-ld=ld.gold --with-system-zlib
Thread model: single
gcc version 4.8.2 (GCC) 

OS:
Arch Linux 
Linux 3.12.0-1-ARCH #1 SMP PREEMPT Wed Nov 6 09:06:27 CET 2013 x86_64 GNU/Linux

Command line:
avr-gcc -Wall -mmcu=atxmega64a3 -DF_CPU=16000000UL  -mno-interrupts -Os -pedantic-errors -pedantic -std=c++11 -Wfatal-errors -Wall    -I/usr/avr/include  -c main.cpp -o obj/Release/main.o
main.cpp: In function 'int main()':
main.cpp:55:21: warning: variable 'tval32' set but not used [-Wunused-but-set-variable]
   volatile uint32_t tval32;
                     ^
avr-g++  -o bin/Release/lambda_test.elf obj/Release/main.o   -mmcu=atxmega64a3 -Wl,-Map=bin/Release/lambda_test.map,--cref  
Output size is 7,92 KB
Running project post-build steps
avr-size bin/Release/lambda_test.elf
   text	   data	    bss	    dec	    hex	filename
    850	      0	   2400	   3250	    cb2	bin/Release/lambda_test.elf
avr-objdump -h -S bin/Release/lambda_test.elf > bin/Release/lambda_test.lss
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 1 warnings (0 minutes, 0 seconds)

Source file: attached main.cpp
Generated assembly: attached lambda_test.lss

Problem.
A. As you can see in generated assembly:
1. function Sort_OldStyle(_Z13Sort_OldStylev) contain(as inline) functions Sort and Sort_OldStyle_Internal
2. function Sort_NewStyle(_Z13Sort_NewStylev) contain(as inline) functions Sort and lambda-expression

B. But also generated code contain unneded:
1. function Sort(_Z4SortPV5SPairS1_PFvRS0_E)
2. function Sort_OldStyle_Internal(_Z22Sort_OldStyle_InternalRV5SPair)
3. lambda-expression(_ZZ13Sort_NewStylevENUlRV5SPairE_4_FUNES1_)

Why gcc include functions from B-list if they already exist in functions of list A? Also why gcc use inline for function Sort and don't use call with -Os used?
Comment 1 smalcom 2013-12-05 11:07:51 UTC
Created attachment 31386 [details]
generated assembly
Comment 2 Georg-Johann Lay 2013-12-22 15:11:50 UTC
Created attachment 31500 [details]
C++ source.

Please follow the bug reporting instructions and don't include headers, especcialy ones that the common GCC developer does not have.

Attached a source file that compiles fine without headers.

And there is nothing avr specific here.
Comment 3 Georg-Johann Lay 2013-12-22 16:43:38 UTC
Some more notes:

If you don't want that small functions are being inlined, consider -fno-inline-small-functions.

If a function is being inlined then it's body is still implemented except in the case where GCC can prove the body is never needed, for example if the function is static.

If you implement two functions that result in the same code, GCC won't reuse the code for you.  If you have redundant code, it is up to you to reuse it.
Comment 4 Andrew Pinski 2021-12-23 07:00:09 UTC
If I use -Wl,--gc-sections -ffunction-sections, then all of the unused (non-static) functions are removed.

Also -flto is able to optimize it too.
Comment 5 smalcom 2021-12-23 08:02:52 UTC
Created attachment 52049 [details]
Generated assembly when using "gc-section". avr-gcc-10.2
Comment 6 smalcom 2021-12-23 08:04:47 UTC
Created attachment 52050 [details]
Generated assembly when using "LTO". avr-gcc-10.2

(In reply to Andrew Pinski from comment #4)
> If I use -Wl,--gc-sections -ffunction-sections, then all of the unused
> (non-static) functions are removed.
> 
> Also -flto is able to optimize it too.

I confirm that:
1. With set "-Wl,--gc-sections -ffunction-sections" output file do not contain unused functions (attached gcsec.lss).
2. With "-flto" all functions are inlined and present only used code (attached lto.lss).

Tested using gcc-10:

avr-gcc -v
Using built-in specs.
Reading specs from /mnt/stor5/opt/сборка/avr-10.2.0/bin/../lib/gcc/avr/10.2.0/device-specs/specs-avr2
COLLECT_GCC=avr-gcc
COLLECT_LTO_WRAPPER=/mnt/stor5/opt/сборка/avr-10.2.0/bin/../libexec/gcc/avr/10.2.0/lto-wrapper
Target: avr
Configured with: ../configure --prefix=/opt/toolchain/avr-10.2.0 --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp --with-dwarf2
Thread model: single
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC) 

Great. Thanks!