----- Mail original ----- De : Miles Bader <miles@xxxxxxx> À : Jeff Law <law@xxxxxxxxxx> Cc : Jeff Kenton <jkenton@xxxxxxxxxx>; Ian Lance Taylor <iant@xxxxxxxxxx>; David Brown <david@xxxxxxxxxxxxxxx>; Georg-Johann Lay <avr@xxxxxxxx>; gcc-help@xxxxxxxxxxx; charfiasma@xxxxxxxx Envoyé le : Mardi 27 Septembre 2011 7h12 Objet : Re: Tr : [redundency elimination, code motion, commun expression elimination] GCC optimizations Miles Bader <miles@xxxxxxx> writes: >> Hmm, actually testing with a recent gcc snapshot, it _is_ merging the >> two if-blocks: > ... > > and in fact, gcc 4.4.6, 4.5.3, and 4.6.1 seem to do the same thing! > > So I dunno... what's the complaint again...? I note that this optimization only takes place at -O2 or above; maybe the original poster simply didn't use a high-enough optimization level? -miles -- I'd rather be consing. Hello, When I write the C code I tried to simplify the code cause actually I used nested switch cases and the code is not directly written in the main it is written in another function called in main. this is the simplified code that GCC did not optimize when I compile the C++ code using -Os. (I used GCC: (GNU) 4.6.0 20110218 ) perhaps it deals with GCC intra optimizations ... I am so sorry for the wrong simplification that I did (in the previous post) cause actually, GCC did factorize the common blocs as miles said but it did not factorize this example :( int a,b,c,d; class MyClass { public : int x; public : void foo() { d=10; } public : void foo1() { if (x==1) { a=10; b=11; c=12; foo(); } if (x==2) { a=10; b=11; c=12; foo(); } } }; int main() { MyClass c; c.x=1; c.foo1(); c.foo1(); c.foo1(); return 0; } and this is the assembly code generated from it. .weak _ZN7MyClass4foo1Ev .type _ZN7MyClass4foo1Ev, @function _ZN7MyClass4foo1Ev: .LFB1: cmpl $1, (%rdi) jne .L2 movl $10, a(%rip) movl $11, b(%rip) movl $12, c(%rip) movl $10, d(%rip) .L2: cmpl $2, (%rdi) jne .L1 movl $10, a(%rip) movl $11, b(%rip) movl $12, c(%rip) movl $10, d(%rip) .L1: ret .LFE1: .size _ZN7MyClass4foo1Ev, .-_ZN7MyClass4foo1Ev .section .text.startup,"ax",@progbits .globl main .type main, @function main: .LFB2: subq $24, %rsp .LCFI0: leaq 12(%rsp), %rdi movl $1, 12(%rsp) call _ZN7MyClass4foo1Ev leaq 12(%rsp), %rdi call _ZN7MyClass4foo1Ev leaq 12(%rsp), %rdi call _ZN7MyClass4foo1Ev xorl %eax, %eax addq $24, %rsp .LCFI1: ret .... LEFDE3: .ident "GCC: (GNU) 4.6.0 20110218 (experimental)" .section .note.GNU-stack,"",@progbits