Following test case generates wrong code with optimization option -O2 or O3. Details are as under. h8300-elf-gcc -v ---------------- Configured with: /home/kpit/fsfsrc/downloads/gcc-3.5-20040711/configure -- enable-languages=c,c++ --target=h8300-elf --with-newlib -- prefix=/home/nitins2/intmdt_share/gnuh8300_v0403_elf-1 Thread model: single gcc version 3.5-GNUH8_v0403 ---------------------------------------------- Commandline: h8300-elf-gcc test.c -ms -m2600s -S -O2 //test.c void theLoop( void ) { unsigned long int cnt; cnt = 0L; do cnt++; while (cnt < 1000000L); } //test.c //test.s .file "test.c" .h8300s .section .text .align 2 .align 1 .global _theLoop _theLoop: mov.l er6,@-er7 mov.l er7,er6 mov.l #999999,er2 .L2: sub.l #50,er2 !This should be 1 mov.l er2,er2 bge .L2 mov.l @er7+,er6 rts .size _theLoop, .-_theLoop .end //test.s Additional Info: This bug comes with target options -mh,-ms and -ms2600 only.
Hi, The workaround to avoid bug is to pass either of following option along with -O2 , -fno-loop-optimize -fno-strength-reduce This bug is not target dependent as was first impression. Since counting variable is of type unsigned long, the number of instructions in loop for plain H8/300 are 7. So problem doesn't crop up for plain H8/300. If we change data type of counting variable to unsigned int (16 bits) and value also within range of 16 bits ( less than 65535) then bug can be seen in case of plain H8/300. Bug is not observed in following program code, //test.c void theLoop( void ) { unsigned long int cnt; cnt = 0L; do{ cnt++; asm("nop"); } while (cnt < 1000000L); } //test.c Compiled with -S -O2 -ms options. The number of instructions in loop becomes 4 in above case. Hence bug is not observed. I tracked the problem to loop.c, line no. 5358 (gcc 3.5 snapshot dated 25 July 2004). Following call doesn't take place for insn_count more than 4 since "unrolled_insn_copies <= insn_count" this condition is dissatisfied. unroll_loop (loop, insn_count, 1); In case of H8S target and insn_count 3, call takes place and the magic number 50 appears instead of 1. This magic number is also dependent on number of iterations of loop. Will it be ok to add additional condition as insn_count > 4 while calling unroll_loop at line 5358 ? Please comment. Regards, Anil Paranjpe KPIT Cummins InfoSystems Ltd. Pune, India ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Free download of GNU based tool-chains for Renesas' SH and H8 Series. The following site also offers free technical support to its users. Visit http://www.kpitgnutools.com for details. Latest versions of KPIT GNU tools were released on June 1, 2004. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I doubt there is anything in the C language specification that prohibits the compiler from removing or shortening these empty loops. To guarantee the presevation of the loop iterations, you need to use asm ("nop") as you have demonstrated already.