Re: Question regarding C code generation for a compound condition

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Jun 7, 2011 at 15:59, Cedric Roux <cedric.roux@xxxxxxxxxx> wrote:
> On 06/07/2011 03:47 PM, Cedric Roux wrote:
>>>
>>> Or compile with-fno-delete-null-pointer-checks
>>
>> forget about that. Sorry...
>
> might be that after all!
> (really sorry for the noise)

No problems: you pinpointed it . Adding -fno- will indeed generate
code for both sides of the '||':

  if ( (g->a==0) || (g==NULL) )
   0:	e59f3028 	ldr	r3, [pc, #40]	; 30 <test_function+0x30>
   4:	e5933000 	ldr	r3, [r3]
   8:	e5d32000 	ldrb	r2, [r3]
   c:	e3520000 	cmp	r2, #0
  10:	0a000003 	beq	24 <test_function+0x24>
  14:	e3530000 	cmp	r3, #0

(this time r3 is tested against 0, as the C code says)

And indeed this explains the issue: -O2 enables
-fdelete-null-pointer-checks, which is described as follows:

"Assume that programs cannot safely dereference null pointers, and
that no code or data element resides there. This enables simple
constant folding optimizations at all optimization levels. In
addition, other optimization passes in GCC use this flag to control
global dataflow analyses that eliminate useless checks for null
pointers; these assume that if a pointer is checked after it has
already been dereferenced, it cannot be null."

So indeed, with -fdelete-null-pointer-checks *and* additional passes,
the left-hand dereferences makes gcc consider g non-null, causing the
right-hand test to become useless and deleting it.

@Andrew: so this was not an undefined behavior case; rather, it was
merely a case of not having read the gcc docs deep enough, for which I
apologize.

Thanks again!
-- 
Albert ARIBAUD - Motorola Mobility France SAS


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux