gpt4 book ai didi

c - 嵌入式 GCC 优化魔法

转载 作者:行者123 更新时间:2023-12-04 13:16:34 27 4
gpt4 key购买 nike

我有一个项目,我尝试为微 Controller 构建固件并尝试更好地控制所使用的优化标志。我想,而不是使用 -O<number> flag 分别指定不同的优化标志。不幸的是,-O 似乎发生了一些优化魔法。我无法使用单个优化标志重现的标志,我不明白为什么。
这是我尝试的方法和不起作用的方法:
我知道我可以用 -O1 编译项目.所以我使用了 -Q--help标志来输出当我激活 -O1 时处于事件状态的标志旗帜。我使用此信息在我的构建过程中手动指定不同的标志,并且编译工作正常,但在链接阶段它失败了,因为 .bss 部分不再适合我的 RAM(我只有 384 KB 可用)。
当我在链接器脚本中增加 RAM 大小时,链接工作正常,但 .bss 部分的末尾放置在 416 kByte 处,并且二进制图像比使用 -O1 时大 75%直接地。
当我比较 gcc 报告的标志和参数时,两个版本之间没有区别,但没有 -O1 的版本之间没有区别。还是大得多。
根据 GCC 文档( GCC Manual )做 -O flag 仅激活特定的优化标志,因此也应该可以手动执行此操作(或不可以?)
这是我的 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 -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选项还激活了我忽略的?

最佳答案

根据 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.

因此仅指定优化标志是不够的。例如 here您可以看到只有在 -O 时才启用某些分析和 -fweb已启用:
class pass_web : public rtl_opt_pass
{
...
virtual bool gate (function *) { return (optimize > 0 && flag_web); }

甚至指定 -O1并且从更高的优化级别选择性地启用优化将无法可靠地工作,因为某些传递明确依赖于 -O值(value)。例如。 here您可以看到在 -O1 处禁用了部分 CSE 优化。 :
else if (tem == 1 || optimize > 1)
cse_cfg_altered |= cleanup_cfg (0);

关于c - 嵌入式 GCC 优化魔法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59899413/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com