bug-gnulib
[Top][All Lists]
Advanced

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

Re: GCC optimizes integer overflow: bug or feature?


From: Denis Vlasenko
Subject: Re: GCC optimizes integer overflow: bug or feature?
Date: Tue, 19 Dec 2006 23:39:50 +0100
User-agent: KMail/1.8.2

On Tuesday 19 December 2006 22:46, Paul Brook wrote:
> > Compiler can optimize it any way it wants,
> > as long as result is the same as unoptimized one.
> 
> We have an option for that. It's called -O0.
> 
> Pretty much all optimization will change the behavior of your program.

Even x*2 -> x+x? Or unsigned x/8 -> x>>3 ?

> The  
> important distinction is whether that difference is observable in valid 
> programs. The whole point of langage standards is that they define what 
> constitutes a valid program.
> 
> By your definition all writes to vairables must translate into a write to 
> memory (for "correct" behavior in multithreaded applications and with signal 
> handlers). This implies all variables are effectively volatile.

I am perfectly happy with registers too. Not nesessarily with same size, even.
(Just proprerly autoextend signed values a-la AMD64).

Do not put words in my mouth. I said nothing about volatility
and multithreading.

[Offtopic: even declaring variable "volatile" buys you zero
for multithreaded/SMP/memory-mapped PCI/etc case.
Today's CPUs are too clever these days. OOO loads and stores and all that.
Linus Torvalds recently explained that on l-k in painful details.]

There are a lot of 100.00% safe optimizations which gcc
can do. Value range propagation for bitwise operations, for one:

# cat tt.c
int f(int n)
{
    n = ((n & 0xf0f) | 1);
    if (n == 0x1000) return 2; /* impossible */
    if (n == 0x10) return 1; /* impossible */
    return 0;
}
# gcc -O2 -S -fomit-frame-pointer tt.c
# cat tt.s
        .file   "tt.c"
        .text
        .p2align 2,,3
.globl f
        .type   f, @function
f:
        movl    4(%esp), %eax
        andl    $3854, %eax
        orl     $1, %eax
        cmpl    $4096, %eax
        je      .L7
        cmpl    $16, %eax
        sete    %al
        movzbl  %al, %eax
        ret
        .p2align 2,,3
.L7:
        movw    $2, %ax
        ret
        .size   f, .-f
        .ident  "GCC: (GNU) 4.2.0 20061128 (prerelease)"
        .section        .note.GNU-stack,"",@progbits
--
vda




reply via email to

[Prev in Thread] Current Thread [Next in Thread]