http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56546
Bug #: 56546 Summary: Using the divide operator on unsigned int produces incorrect code on AVR Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Severity: critical Priority: P3 Component: target AssignedTo: unassig...@gcc.gnu.org ReportedBy: k...@free.fr Created attachment 29592 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29592 Sample code to reproduce the issue Using the divide integer on unsigned int variables on an AVR target leads to wrong code being generated. The generated code uses the __umulhisi3 routine from libgcc which is a multiplication routine and the result is always zero. As I'm willing to increase my knowledge about the inner workings of gcc, I tried to start debugging it myself. I found (using -fdump-rtl-all) that the issue was occurring in the RTL part of the compiler where the udiv was at several point transformed into a mult. I tried disabling the first pass that did the transformation (-fdisable-rtl-fwprop1) and the udiv was propagated a little further in the process until the ira pass. I then tried to disable the ira pass using -fdisable-rtl-ira, made gcc crash and discovered it was a really stupid thing to do (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56514). So here I am again but his time leaving compiler debugging to the compiler guys :-) I've come up with a very simple source file that allows to reproduce the issue. By the way I couldn't think of any case where transforming a udiv into a mult operating on integers would make sense and would be glad if someone could give me some hints on this. Here's the command line I used: avr-gcc -O0 -g -Wall -Wextra -save-temps -mmcu=atmega8 -o main.elf main.c And the version of the toolchain components: binutils: efb7cff2df30eb792d30e8afc384aa88c193932b gcc: ef11013858b41453c4953ca8d4c25e3b1668e536 avr-libc: 2ac01d285e23894ef3bcc65c75b39da8157b9fd9 gcc-4.7.2, binutils 2.23.1 and avr-libc 1.8.0 give the same result.