GCC 优化:使用 ARM 条件指令?
我正在查看 XCode 中为 iOS 编译的一些代码(因此使用 gcc 为 ARM 编译),据我所知,编译器从未使用过 ARM 允许任意指令附加条件的功能,而是总是使用根据英特尔和其他架构的情况进行分支。
这是否只是 GCC 的限制(我可以理解,这可能是:“条件 = 分支”在编译器体系结构中嵌入在太高的级别以允许其他情况),或者是否需要一个特定的优化标志打开以允许编译条件指令?
(显然,我很感激我对“应该”使用条件指令的位置做出了很大的假设,并且实际上是一种优化,但我有对早期 ARM 芯片进行编程以及使用和分析 Acorn 原始 ARM C 编译器的输出的经验,所以我有一个粗略的想法。)
更新:通过以下信息对此进行了更多研究,结果表明:
- XCode 在 Thumb-2 模式下编译,在该模式下,任意指令的条件执行不是可用的;
- 但在某些情况下,它确实使用 ITE(if-then-else)指令来有效地生成具有条件执行的指令。
I'm looking at some code compiled for iOS in XCode (so compiled for ARM with gcc) and as far as I can see, the compiler has never used ARM's feature of allowing arbitrary instructions to have a condition attached to them, but instead always branches on a condition as would be the case on Intel and other architectures.
Is this simply a restriction of GCC (I can understand that it might be: that "condition = branch" is embedded at a too high a level in the compiler architecture to allow otherwise), or is there a particular optimisation flag that needs to be turned on to allow compilation of conditional instructions?
(Obviously I appreciate I'm making big assumptions about where use of conditional instructions "ought" to be used and would actually be an optimisation, but I have experience of programming earlier ARM chips and using and analysing the output of Acorn's original ARM C compiler, so I have a rough idea.)
Update: Having investigated this more thanks to the information below, it turns out that:
- XCode compiles in Thumb-2 mode, in which conditional execution of arbitrary instructions is not available;
- Under some circumstances, it does however use the ITE (if-then-else) instruction to effectively produce instructions with conditional execution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看到一些实际的汇编会让事情变得清楚,但我怀疑 iOS 编译的默认设置更喜欢生成 Thumb 代码而不是 ARM,以获得更好的代码密度。虽然 Thumb32 又名 Thumb-2 中有伪条件指令(通过 IT 指令在 ARMv7 架构中受支持),但原始 Thumb16 仅具有条件分支。此外,即使在 ARM 模式下,也有一些指令不能是有条件的(例如,许多 NEON 指令使用条件字段设置为 NV 的扩展操作码空间)。
Seeing some actual assembly would make things clear, but I suspect that the default settings for iOS compilation prefer generation of Thumb code instead of ARM for better code density. While there are pseudo-conditional instructions in Thumb32 aka Thumb-2 (supported in ARMv7 architecture via the IT instruction), the original Thumb16 only has conditional branches. Also, even in ARM mode there are some instructions that cannot be conditional (e.g. many NEON instructions use the extended opcode space with condition field set to NV).
是的,gcc 并没有真正产生最优化的代码 WRT 条件指令。它在最简单的情况下运行良好,但实际代码会遭受一些无意义的减慢,而这些减慢可以在手工编码的 Arm ASM 中避免。只是为了给您一个粗略的了解,通过在 ARM asm(而不是 gcc 发出的 C 代码)中执行读/写和复制逻辑,我能够为非常低级别的图形 blit 方法获得 2 倍的加速。但是,请记住,只有对于代码中使用最频繁的部分,这种优化才值得。编写优化良好的 ARM asm 需要做大量的工作,所以不要尝试它,除非优化确实有好处。
首先要记住的是 xcode 默认使用 Thumb 模式,因此为了生成 ARM asm,您需要将 -mno-thumb 选项添加到将包含 ARM 的特定 .c 文件的模块特定选项中汇编。一旦发出 ARM asm,您将需要有条件地编译 asm 语句,如以下问题的答案所示:
ARM asm条件编译问题
Yes, gcc does not really produce the most optimal code WRT conditional instructions. It works well in the most simple cases, but real code suffers from some pointless slowdowns that can be avoided in hand coded arm ASM. Just to give you a rough idea, I was able to get a 2x speedup for a very low level graphics blit method by doing the read/write and copy logic in ARM asm instead of the C code emitted by gcc. But, keep in mind that this optimization is only worth it for the most heavily used parts of your code. It takes a lot of work to write well optimized ARM asm, so don't even attempt it unless there is a real benefit in the optimization.
The first thing to keep in mind is that xcode uses Thumb mode by default, so in order to generate ARM asm you will need to add the -mno-thumb option to the module specific options for the specific .c file that will contain the ARM asm. Once the ARM asm is getting emitted, you will want to conditionally compile asm statements as indicated in the answer to the following question:
ARM asm conditional compilation question