Is there a way to stop the compiler optimising out code?
-
- Junior Member
- Posts: 6
- Joined: Thu May 16, 2013 12:29 pm
Is there a way to stop the compiler optimising out code?
When I turn on optimising useful lines of code are removed. Is there a way to stop this?
-
- XCore Expert
- Posts: 844
- Joined: Sun Jul 11, 2010 1:31 am
Then those lines weren't in fact useful, that is, not what the compiler
considers useful! The compiler generates machine code that does
the same thing as your source code does, and it tries to make the
machine code as small and fast as possible. If you wrote code that
the compiler then optimised away, you might have a bug; try -Wall
-Wextra to have the compiler tell you when it thinks you accidentally
wrote useless code.
If you do understand why the compiler removed some code, but you
want to keep it anyway, the standard way to do it is make an empty
asm with the result of your computation as input. A silly example:
which generates as code something like:
In some cases you need to make the variable an output, or both
an input and an output.
But again, first you need to make sure you understand _why_ the
compiler optimised your code the way it did, before you try to
undo that!
considers useful! The compiler generates machine code that does
the same thing as your source code does, and it tries to make the
machine code as small and fast as possible. If you wrote code that
the compiler then optimised away, you might have a bug; try -Wall
-Wextra to have the compiler tell you when it thinks you accidentally
wrote useless code.
If you do understand why the compiler removed some code, but you
want to keep it anyway, the standard way to do it is make an empty
asm with the result of your computation as input. A silly example:
Code: Select all
int f(int a)
{
int j;
for (j = 0; j < 10; j++)
a++;
return a;
}
int g(int a)
{
int j;
for (j = 0; j < 10; j++) {
a++;
asm("" : : "r"(a));
}
return a;
}
Code: Select all
f:
add r0,r0,10
retsp 0
g:
add r2,r0,10
.L4:
add r0,r0,1
sub r1,r0,r2
bt r1,.L4
mov r0,r2
retsp 0
an input and an output.
But again, first you need to make sure you understand _why_ the
compiler optimised your code the way it did, before you try to
undo that!
-
- Junior Member
- Posts: 6
- Joined: Thu May 16, 2013 12:29 pm
Ok, thanks for your reply. I just wondered if there is a simple way to stop the compiler optimising a section of code, say because the timing of it was correct.
-
- Respected Member
- Posts: 296
- Joined: Thu Dec 10, 2009 10:33 pm
Clive,
I would suggest that if the use of optimization upsets the timing of your code and causes it to no longer work correctly for you then you have a problem in your code.
It sounds to me as if you are using "dead code" to create some kind of delay to get the some timing right. When that dead code is optimized away the timing goes wrong.
This is not a good way to do things. As you see it is fragile. Given all the features of these devices to handle time there are better, more robust, ways to do this.
I would suggest that if the use of optimization upsets the timing of your code and causes it to no longer work correctly for you then you have a problem in your code.
It sounds to me as if you are using "dead code" to create some kind of delay to get the some timing right. When that dead code is optimized away the timing goes wrong.
This is not a good way to do things. As you see it is fragile. Given all the features of these devices to handle time there are better, more robust, ways to do this.