GCC Optimzations

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
spix
Member
Member
Posts: 128
Joined: Mon Jun 26, 2006 8:41 am
Location: Millicent, South Australia
Contact:

GCC Optimzations

Post by spix »

Hi There

I'm in the process of debugging some of my code, which I discovered works sometimes it is compiled and other times it doesn't. Anyway, I got side tracked and experimented with gcc optimizations on my kernel. Using GCC 4.1, -O worked fine, -O2 and -O3 the keyboard stopped working. So I am wondering, when optimizations break my code does that mean that there is some bug I haven't discovered yet in my code, or it is optimizing something it shouldn't and I shouldn't play with optimizations in the first place?

Andrew
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GCC Optimzations

Post by Solar »

Generally, GCC is very well-tested. Actually, it is better tested with optimizations than without.

Chances you did soemthing fishy in your code: 99.5%
Chances GCC optimization broke: 0.5%

My first guess would be you forgot "volatile" in important places, or did some strange polling things that GCC optimized away.
Every good solution is obvious once you've found it.
User avatar
spix
Member
Member
Posts: 128
Joined: Mon Jun 26, 2006 8:41 am
Location: Millicent, South Australia
Contact:

Re:GCC Optimzations

Post by spix »

Thanks for that, you're probably right with the volatile, I've missed those in the past.

I was wondering about the optimizations because sometimes I've read posts on other boards with regards to linux and how compiling with too much optimizations can be bad - I think it was a gentoo forum, and also there was a lot of code that needed to be fixed to work in gcc 4.0 when it came out.

I guess I was wondering mostly because, I can use this as a debugging tool, if it breaks with some optimization but usually works, there must be a hidden bug.

Anyway, thanks for that
Andrew
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:GCC Optimzations

Post by Solar »

spix wrote: I was wondering about the optimizations because sometimes I've read posts on other boards with regards to linux and how compiling with too much optimizations can be bad - I think it was a gentoo forum, and also there was a lot of code that needed to be fixed to work in gcc 4.0 when it came out.
Correct. That is because most of the software available for Linux is pretty sh*tty. Gee, mommy, look at all those compiler warnings... needless to say, if your code is badly written, running an aggressive optimizer on it won't improve things.

But that's not GCC's fault...
I guess I was wondering mostly because, I can use this as a debugging tool, if it breaks with some optimization but usually works, there must be a hidden bug.
Start with enabling as many compiler warnings as you can find, and also do -Werror so you don't get lazy with warnings. Then make sure you understood the language details (like 'volatile'). The optimizer is about the last tool I would trust to find my bugs for me. ;)
Every good solution is obvious once you've found it.
Post Reply