public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Undefined behavior or gcc is doing additional good job?
@ 2014-01-03  8:24 Bin.Cheng
  2014-01-03  8:44 ` Jakub Jelinek
  0 siblings, 1 reply; 8+ messages in thread
From: Bin.Cheng @ 2014-01-03  8:24 UTC (permalink / raw)
  To: gcc

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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-01-07  8:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-03  8:24 Undefined behavior or gcc is doing additional good job? Bin.Cheng
2014-01-03  8:44 ` Jakub Jelinek
2014-01-03  9:17   ` Bin.Cheng
2014-01-03  9:44     ` Jakub Jelinek
2014-01-03 14:42       ` Joseph S. Myers
2014-01-07  8:11       ` Bin.Cheng
2014-01-07  8:19         ` Jakub Jelinek
2014-01-07 10:05           ` Bin.Cheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).