我有一个项目,在这个项目中,我试图为一个微控制器构建一个固件,并试图更好地控制所使用的优化标志。我希望,不要使用-O<number>标志,而是单独指定不同的优化标志。不幸的是,似乎有一些优化魔术发生在-O标志上,我不能用单独的优化标志来再现,我也不明白为什么。
以下是我尝试的和不起作用的东西:
我知道我可以用-O1编译这个项目。因此,我使用-Q和--help标志输出在激活-O1标志时处于活动状态的标志。我使用这些信息在构建过程中手动指定不同的标志,编译工作很好,但是在链接阶段,它失败了,因为.bss部分不再适合我的内存(我只有384 kByte可用)。
当我在我的链接脚本中增加内存大小时,链接工作正常,但是.bss部分的末尾放置在416 kByte,二值图像比直接使用-O1时大75 %。
当我比较gcc报告的标志和参数时,这两个版本没有什么区别,但是没有-O1的版本仍然要大得多。
根据GCC文档(GCC手册),-O标志是否只激活特定的优化标志,因此也应该可以手动(或不?)
下面是gcc的命令:
具有单一优化标志的GCC调用
gcc -std=c99 -msoft-float -fno-inline -fdata-sections -ffunction-sections -Wall -Wextra\
-faggressive-loop-optimizations -fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments\
-fcompare-elim -fcprop-registers -fdce -fdefer-pop -fdelayed-branch -fdelete-null-pointer-checks\
-fdse -fearly-inlining -ffast-math -fforward-propagate -ffp-contract=fast -ffp-int-builtin-inexact\
-ffunction-cse -fgcse-lm -fguess-branch-probability -fhandle-exceptions -fif-conversion -fif-conversion2\
-finline-atomics -finline-functions-called-once -fipa-profile -fipa-pure-const -fipa-reference\
-fira-algorithm=CB -fira-hoist-pressure -fira-share-save-slots -fira-share-spill-slots -fivopts\
-fjump-tables -flifetime-dse -flifetime-dse=2 -fmath-errno -fmove-loop-invariants -fomit-frame-pointer\
-fpeephole -fplt -fprefetch-loop-arrays -fprintf-return-value -frename-registers -freorder-blocks
-frtti -fsched-critical-path-heuristic -fsched-dep-count-heuristic -fsched-group-heuristic\
-fsched-interblock -fsched-last-insn-heuristic -fsched-rank-heuristic -fsched-spec -fsched-spec-insn-heuristic\
-fsched-stalled-insns-dep -fschedule-fusion -fshort-enums -fshrink-wrap -fshrink-wrap-separate\
-fsigned-zeros -fsplit-ivs-in-unroller -fsplit-wide-types -fssa-backprop -fssa-phiopt -fstack-reuse=all\
-fstdarg-opt -fstrict-volatile-bitfields -fno-threadsafe-statics -ftrapping-math -ftree-bit-ccp\
-ftree-builtin-call-dce -ftree-ccp -ftree-ch -ftree-coalesce-vars -ftree-copy-prop -ftree-cselim\
-ftree-dce -ftree-dominator-opts -ftree-dse -ftree-forwprop -ftree-fre -ftree-loop-if-convert -ftree-loop-im\
-ftree-loop-ivcanon -ftree-loop-optimize -ftree-parallelize-loops=1 -ftree-phiprop -ftree-pta\
-ftree-reassoc -ftree-scev-cprop -ftree-sink -ftree-slsr -ftree-sra -ftree-ter -fvar-tracking -fvar-tracking-assignments\
-fweb -fmerge-constants -fno-associative-math -fno-cx-limited-range -fno-exceptions -fno-finite-math-only\
-fno-reciprocal-math -fno-unsafe-math-optimizations -fexcess-precision=standard -qbsp=leon2 -DCPU_FREQ=CPU_FREQ_125MHz\
-fno-builtin-strtok -c -o timer.o timer.c-O1 GCC与
gcc -O1 -std=c99 -msoft-float -qbsp=leon2 -DCPU_FREQ=CPU_FREQ_125MHz -fno-builtin-strtok -c -o timer.o timer.c如果需要的话,我也可以提供gcc的输出,看看哪种旗帜在这两种情况下都是活跃的。我发现的唯一不同是,-fexcess-precision被设置为-O1的“默认”。我尝试了这两种可能性(快速和标准),但这并没有什么区别。
有谁知道-O选项会额外激活什么魔法,我忽略了吗?
发布于 2020-01-27 13:18:55
GCC手册
Most optimizations are only enabled if an -O level is set on the command line.
Otherwise they are disabled, even if individual optimization flags are specified.因此,仅指定优化标志是不够的。例如,这里,您可以看到,只有在同时启用-O和-fweb时才启用某些分析:
class pass_web : public rtl_opt_pass
{
...
virtual bool gate (function *) { return (optimize > 0 && flag_web); }即使指定-O1并有选择地从更高的优化级别启用优化,也不能可靠地工作,因为有些传递显式地依赖于-O值。例如这里您可以看到CSE优化的部分在-O1中被禁用。
else if (tem == 1 || optimize > 1)
cse_cfg_altered |= cleanup_cfg (0);https://stackoverflow.com/questions/59899413
复制相似问题