2008年6月20日星期五

GCC的优化导致的问题(keil + winarm)

一个跑马灯程序,调了N久,还是不行。症状是:所有LED都亮。在汇编窗口下调试,发现延时函数不见了。最后发现竟然是被优化掉了,去掉优化选项,通过了。

 

/****************************************************************************

* 名称:DelayNS()

* 功能:长软件延时

* 入口参数:dly          延时参数,值越大,延时越久

* 出口参数:无

****************************************************************************/

void  DelayNS(uint32  dly)

{  uint32  i;

 

   for(; dly>0; dly--)

      for(i=0; i<5000; i++);

}

 

这样的函数在默认的优化等级下(一级优化),竟然被优化掉,真惊了!!!!

这样就OK了:

 

void  DelayNS(uint32  dly)
{  
   //uint32  i;
   volatile uint32  i;
   for(; dly>0; dly--) 
      for(i=0; i<5000; i++);
}

 

网上搜到了关于GCC优化的文章:

 

GCC各级优化的内容:(http://hi.baidu.com/hilyjiang/blog/item/f8e249f0b7833ac27831aa01.html )
1. -O0:不做任何优化处理,default
2. -O1:一级优化
defer-pop:Defer popping function args from stack until necessary.
thread-jumps: Perform jump threading optimizations (to avoid jumps to jumps).
branch-probabilities: Use branch profiling to optimize branches.
cprop-registers: Perform a register copy-propagation optimization pass.
guess-branch-probability:Enable guessing of branch probabilities.
omit-frame-pointer: Do not generate stack-frames (if possible).
3.-O2:二级优化(速度优先)
二级优化包含一级优化中不损失速度的部分.
align-loops : Align the start of loops.
align-jumps : Align the labels that are only reachable by jumps.
align-labels : Align all labels.
align-functions : Align the beginning of functions.
optimize-sibling-calls : Optimize sibling and tail recursive calls.
cse-follow-jumps : When performing CSE, follow jumps to their targets.
cse-skip-blocks : When performing CSE, follow conditional jumps.
gcse : Perform global common subexpression elimination.
expensive-optimizations : Perform a set of expensive optimizations.
strength-reduce : Perform strength reduction optimizations.
rerun-cse-after-loop : Rerun CSE after loop optimizations.
rerun-loop-opt : Rerun the loop optimizer twice.
caller-saves : Enable register saving around function calls.
force-mem : Copy memory operands into registers before using.
peephole2 : Enable an rtl peephole pass before sched2.
regmove : Enable register move optimizations.
strict-aliasing : Assume that strict aliasing rules apply.
delete-null-pointer-checks : Delete useless null pointer checks.
reorder-blocks : Reorder basic blocks to improve code placement.
schedule-insns : Reschedule instructions before register allocation.
schedule-insns2 : Reschedule instructions after register allocation
4. -Os:尺寸优化(尺寸优先)
尺寸优化包含二级优化中不损失尺寸的部分.同时不支持二级优化中的下列内容:
falign-labels,falign-jumps,falign-labels, falign-functions.
5. -O3:三级优化(速度优先)
finline-functions : Inline simple functions into the calling function.
frename-registers : Optimize register allocation for architectures with large numbers of registers (makes debugging difficult).

推荐使用二级优化.

 
2008-06-20

于士友

没有评论: