Page 1 of 1

GCC optimizations deleting my code

Posted: Wed May 23, 2012 5:36 pm
by wolframio74
Hello!

I have a little problem with gcc and it's aggresive optimizations "-O3".
I have an "idt" variable that I set up with the appropiate offset and limit of my idt table and then pass it's address to lidt like this:

Code: Select all

asm volatile ("lidt (%0)" : : "r"(&idt));
When I compile the code with "O3" optimizations GCC assumes that my "idt" variable is not being used anywhere, deletes the assignments to it, moves it's address into eax and outputs "lidt (%eax)", which is a little weird...
I can workaround the problem using a "noinline" load_idt() function but that's not very elegant.
I thought GCC was aware of the stuff I do with "extended asm" but it doesn't seem to be.

Any suggestions?
Thanks!!

Re: GCC optimizations deleting my code

Posted: Wed May 23, 2012 5:49 pm
by wolframio74
That works! thanks! :D

But why does GCC think I'm not using the idt variable when I tell it to pass it's address in a register?

Thanks!

Re: GCC optimizations deleting my code

Posted: Thu May 24, 2012 4:48 am
by Owen
I suspect the real reason is the surrounding code. I get a distinct smell of undefined behavior.

Re: GCC optimizations deleting my code

Posted: Thu May 24, 2012 8:32 am
by gravaera
We have only seen one line of code, and the produced result is consistent with what it is expected to do. I'm not sure how one can now deduce that the surrounding code is shady from having seen only one line of code that produces the expected result...?

--Peace out
gravaera