我比较了clang和gcc为arm生成的代码。
不幸的是,gcc的代码使用的指令更少。我只是很好奇-是否可以减少clang生成的代码?也许我应该使用一些选项来做...
请考虑一个非常简单的例子:
> cat test.c
int to_upper(int c)
{
if(c < 'a' || c > 'z') return c;
else return c - ('a' - 'A');
}
> clang -target arm-none-eabi -Oz -c -mthumb -mcpu=cortex-m0 -msoft-float ./test.c -o ./clang_test.o
> /usr/bin/arm-none-eabi-gcc -Os -c -mthumb -mcpu=cortex-m0 -msoft-float ./test.c -o ./gcc_test.o
> /usr/bin/arm-none-eabi-objdump -d ./clang_test.o
./clang_test.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <to_upper>:
0: 4602 mov r2, r0
2: 3a61 subs r2, #97 ; 0x61
4: 4601 mov r1, r0
6: 3920 subs r1, #32
8: 2a19 cmp r2, #25
a: d800 bhi.n e <to_upper+0xe>
c: 4608 mov r0, r1
e: 4770 bx lr
> /usr/bin/arm-none-eabi-objdump -d ./gcc_test.o
./gcc_test.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <to_upper>:
0: 1c03 adds r3, r0, #0
2: 3b61 subs r3, #97 ; 0x61
4: 2b19 cmp r3, #25
6: d800 bhi.n a <to_upper+0xa>
8: 3820 subs r0, #32
a: 4770 bx lr
为什么这么简单的代码有如此大的差异?在这种情况下,clang可以生成更少的代码吗?至少像gcc?
注意:如果我们将cpu更改为-mcpu = cortex-a5(其他选项保持不变),则clang ang gcc会产生绝对相同的代码:
00000000 <to_upper>:
0: f1a0 0361 sub.w r3, r0, #97 ; 0x61
4: 2b19 cmp r3, #25
6: bf98 it ls
8: 3820 subls r0, #32
a: 4770 bx lr
作业系统:Ubuntu 14.04.3
clang版本3.7.1(tags / RELEASE_371 / final)目标:x86_64-unknown-linux-gnu线程模型:posix
arm-none-eabi-gcc(4.8.2-14ubuntu1 + 6)4.8.2
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句