Page 1 of 1

XC Compiler Optimizations

Posted: Thu Jul 15, 2010 8:52 pm
by Jerry
I'm building a simple application to "Release". The optimization level is set to -02 (generally the most I consider safe). When looking at the disassembled code in the debugger I see this:

Code: Select all

0x000100b2 <main+6>:  ldw   (ru6)       r0, sp[0x0]
0x000100b4 <main+8>:  setc  (ru6)      res[r0], 0x1 *
0x000100b6 <main+10>: ldw   (ru6)       r0, sp[0x0]
0x000100b8 <main+12>: in    (2r)         r0, res[r0] *
0x000100ba <main+14>: stw   (ru6)       r0, sp[0x1]
0x000100bc <main+16>: ldw   (ru6)       r0, sp[0x1]
0x000100be <main+18>: stw   (ru6)       r0, sp[0x1]
This code corresponds to:

Code: Select all

	timer tmr;
	unsigned t;

	tmr :> t;
Why all the redundant loads and stores, especially the last three?

Re: XC Compiler Optimizations

Posted: Thu Jul 15, 2010 10:49 pm
by segher
The option is -O2 (big letter Oh), not -02 (digit zero).


Segher

Re: XC Compiler Optimizations

Posted: Fri Jul 16, 2010 1:06 am
by Jerry
That was a typo in my post. I do have the optimization setting set to -O2 (capital Oh).

My original question still stands: with this level of optimization turned on, why is the compiler generating so many redundant load/store instructions?

Re: XC Compiler Optimizations

Posted: Fri Jul 16, 2010 9:19 am
by Jamie

Code: Select all

int main() {
    timer tmr;
    unsigned t;
    tmr :> t;
    return 0;
}
Compiling the above with:

Code: Select all

xcc -O2 -S test.xc -o test.s
Gives you:

Code: Select all

main:
          getr      r1, 0x1
.L0:
          setc      res[r1], 0x1
.L4:
          in        r0, res[r1]
.L1:
          ldc       r0, 0x0
          freer     res[r1]
.L3:
          retsp     0x0
Which seems fine to me!

Re: XC Compiler Optimizations

Posted: Fri Jul 16, 2010 4:27 pm
by Jerry
That's what I would have expected. Perhaps I'm not settings optimizations properly. Here's how I'm doing it in the IDE:

1. Right-click on project name in the Project Explorer.
2. Choose Properties from the context menu.
3. Select C/XC Build->Settings
4. Click on XC Compiler->Optimization.
5. In the Optimization Level drop-down box, choose "Optimize more (-O2)"
6. Click "OK"
7. Clean project.
8. Build project (choosing Release rather than Debug).

Re: XC Compiler Optimizations

Posted: Fri Jul 16, 2010 4:30 pm
by tyler
Are you making sure to change optimization settings for the "Release" build configuration, as that is the configuration under which you are building?

Re: XC Compiler Optimizations

Posted: Fri Jul 16, 2010 5:26 pm
by Jerry
When I look into Debug Configurations I see that I have "Build Configuration" set to "Release". Yet if I delete the .xe file from the "Debug" folder, clicking on Debug results in a "Program file does not exist".

Why, if I have the Build Configuration set to Release in the Debug Configuration, does the debugger look for the binary in the Debug folder? Shouldn't it look for in in Release folder?

Re: XC Compiler Optimizations

Posted: Fri Jul 16, 2010 5:40 pm
by tyler
Not answering your question directly -- but I sometimes find that the IDE will run the wrong executable.

For example, my code will be in revision 1.1 and I can compile and run in Debug. Then make changes, say up to version 1.2. Switch to release, compile, and then run. After much confusion, realize that v1.1 debug-compiled code is running.

Cleaning the project does not fix this. I used to clean, then delete the Release and Debug folders within the IDE view, then rebuild. Now, I just never switch from Release to Debug or vice versa, I just pick a configuration and stay with it.

Also, the compiler thinks it is running the correct executable -- and will point to source code as it debugs that has nothing to do with the code actually running (extremely confusing -- as during the line "x++;" you'll notice in the watch window that some other variable has now changed).

Re: XC Compiler Optimizations

Posted: Mon Jul 19, 2010 9:29 am
by paul
tyler wrote:Not answering your question directly -- but I sometimes find that the IDE will run the wrong executable.

For example, my code will be in revision 1.1 and I can compile and run in Debug. Then make changes, say up to version 1.2. Switch to release, compile, and then run. After much confusion, realize that v1.1 debug-compiled code is running.
This is because when you hit "Run" eclipse creates a run configuration for the binary that it finds first and runs it. It then remembers that configuration and will build it whenever you hit run. To ensure you are running a binary in a different configuration you need to create a separate "run configuration" for release and debug.

Check out section 4.3 of the 10.4 tools user guide