This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

When does arm-none-eabi-gcc optimize away empty loops?

I am used to gcc optimizing away the sort of "for (i=0; i<DELAYCOUNT; i++) ;" loops that people sometimes try to use for delays.

But arm gcc seems to be very inconsistent in this area.

the following code, compiled with arm-gcc version 5.4, 6, 8, 9, or 10 and -Os, -O2, or -O3 will optimize away the loop in delay(), but NOT the for loop in main() ??

Fullscreen
1
2
3
4
5
6
7
8
9
void delay() {
for (int i=0; i < 9000000; i++) {}
}
int main() {
while(1) {
for(int i=0; i<9000000; i++){} //Run a few cycles doing nothing
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

arm gcc 7 optimizes away both loops. g++ optimizes away both loops.

 

from gcc 10:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/Downloads/gcc-arm-10/bin/arm-none-eabi-gcc -mcpu=cortex-m0 -mthumb -g -Os -Wall -Wextra loop.c -c; arm-objdump -S loop.o
loop.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <delay>:
void delay() {
for (int i=0; i < 9000000; i++) {}
}
0: 4770 bx lr
Disassembly of section .text.startup:
00000000 <main>:
int main() {
0: 4b02 ldr r3, [pc, #8] ; (c <main+0xc>)
while(1) {
for(int i=0; i<9000000; i++){} //Run a few cycles doing nothing
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

(I'm not happy about the extra "cmp" instruction, either.  The subs will have set the flags.  with cpu=cortex-m4 it does better.)

0