X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f From: cbramix AT libero DOT it Newsgroups: comp.os.msdos.djgpp Subject: missing optimization? Date: 4 Nov 2005 04:02:39 -0800 Organization: http://groups.google.com Lines: 92 Message-ID: <1131105759.132511.231360@g47g2000cwa.googlegroups.com> NNTP-Posting-Host: 151.25.138.102 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1131105764 9772 127.0.0.1 (4 Nov 2005 12:02:44 GMT) X-Complaints-To: groups-abuse AT google DOT com NNTP-Posting-Date: Fri, 4 Nov 2005 12:02:44 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98),gzip(gfe),gzip(gfe) Complaints-To: groups-abuse AT google DOT com Injection-Info: g47g2000cwa.googlegroups.com; posting-host=151.25.138.102; posting-account=2QKFyAwAAACmXX-HdPjfH6A4KB1YwZKs To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Hello, I discovered that GCC makes a strange thing when compiling the sources for my embedded application. I attached a very simple C source for demonstrating the fact. ------------------------------------ #include typedef struct { int entry; } data_t; data_t data; void function1(int val) { if ((data.entry -= val) < 0) { printf("<"); } } void function2(int val) { int v = data.entry; if ( (v -= val) < 0) { data.entry = v; printf("<"); } else { data.entry = v; } } ------------------------------------ 'function1' and 'function2' make the same thing. But the generated assembly isn't exacly what I wanted... Here I just put the code of the functions: _function1: movl _data, %eax subl 4(%esp), %eax testl %eax, %eax movl %eax, _data js L5 ret .p2align 4,,15 L5: movl $60, 4(%esp) jmp _putchar _function2: movl _data, %eax subl 4(%esp), %eax js L9 movl %eax, _data ret .p2align 4,,15 L9: movl %eax, _data movl $60, 4(%esp) jmp _putchar Into the code of 'function1' the instruction 'subl 4(%esp), %eax' already changes the status of the FLAGS. So I can't understand why it makes an additional 'testl %eax, %eax'. When I copy my value into a local variable like I did into 'function2', it works as I expected. Unfortunately, with this solution I must save the modified value with the 'data.entry=v;' into both directions, while it could be safely placed between the SUB and JS opcodes. If compiler is *newer* than 2.95, then it puts the TEST opcode. Older versions work fine when accessing the structure directly too. I compiled the source with: gcc -march=i486 -mtune=i486 -mpreferred-stack-boundary=2 -Wall demo.c -S -fomit-frame-pointer -O2 I tried to increase the optimization level but it didn't change. Changing the 'i486' to another microprocessor didn't change too. The application seems to run without problem. However, those optimizations are missed in several critical points. I would feel better if the code into these points could be reduced to the minimum. Do you have some good idea? What am I doing wrong in your opinion? I would like to avoid changes into this old source, if it's possible... Sincerely, Carlo Bramini