This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Undefined behavior or gcc is doing additional good job?
- From: "Bin.Cheng" <amker dot cheng at gmail dot com>
- To: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Fri, 3 Jan 2014 16:12:19 +0800
- Subject: Undefined behavior or gcc is doing additional good job?
- Authentication-results: sourceware.org; auth=none
Hi, For below simple example:
#include <stdint.h>
extern uint32_t __bss_start[];
extern uint32_t __data_start[];
void Reset_Handler(void)
{
/* Clear .bss section (initialize with zeros) */
for (uint32_t* bss_ptr = __bss_start; bss_ptr != __data_start; ++bss_ptr) {
*bss_ptr = 0;
}
}
One snapshot of our branch generates:
Reset_Handler:
@ args = 0, pretend = 0, frame = 0
@ frame_needed = 0, uses_anonymous_args = 0
@ link register save eliminated.
ldr r2, .L6
ldr r1, .L6+4
subs r1, r1, r2
bic r1, r1, #3
movs r3, #0
.L2:
cmp r3, r1
beq .L5
movs r0, #0
str r0, [r2, r3]
adds r3, r3, #4
b .L2
.L5:
bx lr
.L7:
.align 2
.L6:
.word __bss_start
.word __data_start
.size Reset_Handler, .-Reset_Handler
I know the IVOPT chooses wrong candidate here, but what I am not sure about is:
0) the original code is not safe. It could result in infinite loop if
there is any alignment issue of __bss_ptr and __data_start.
1) GCC explicitly clears the two lower bits of (__bss_ptr -
__data_start). This makes the loop safe (from infinite loop).
My question is, is it intended for GCC to do such transformation?
Thanks,
bin
--
Best Regards.