-O2优化的printf(QUOT;%S \\ n&QUOT ;, STR)到看跌期权(STR) [英] -O2 optimizes printf("%s\n", str) to puts(str)
问题描述
与玩弄周围铛
,我编译包含此行C程序:
的printf(%S \\ N的argv [0]);
在没有优化的编译,汇编输出名为的printf
设置寄存器后:
MOVQ(RCX%),RSI%
MOVQ%RAX,%RDI
MOVB $ 0%人
callq _printf
我试着用编译铛-O2
。在的printf
调用替换为一个看跌
电话:
MOVQ(%RSI),%RDI
callq _puts
虽然这非常有意义,在这种情况下,提出了两个问题:
- 多久函数调用替换优化的编译发生?这是频繁或标准输入输出异常?
- 我可以写我自己的库编译器优化?我会怎么做呢?
- 多久函数调用替换优化的编译发生?这是频繁或标准输入输出异常?
块引用>替换
的printf
与看跌
的优化LLVM是班上LibCallSimplifier
。你可以看到在头文件<一个href=\"https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Transforms/Utils/SimplifyLibCalls.h\"相对=nofollow> LLVM /有/ LLVM /变换/ utils的/ SimplifyLibCalls.h 和的 LLVM / lib中/变换/ utils的/ SimplifyLibCalls.cpp 。纵观文件将给出一些完成其它库调用替换优化的例子(头文件是可能更容易下手)。当然,还有一些LLVM呢,你可以通过看的 LLVM通过。
<醇开始=2>
我可以写我自己的库编译器优化?我会怎么做呢?
块引用>是的,你可以。 LLVM是非常模块化并在一系列传球执行对红外转换。所以,如果你想添加自定义自己传递了自己的图书馆,你可以这样做(尽管它仍然是了解如何在LLVM编译器流程的工作原理相当数量的工作)。一个很好的起点是文档:写一个LLVM通
Toying around with
clang
, I compiled a C program containing this line:printf("%s\n", argv[0]);
When compiling without optimization, the assembly output called
printf
after setting up the registers:movq (%rcx), %rsi movq %rax, %rdi movb $0, %al callq _printf
I tried compiling with
clang -O2
. Theprintf
call was replaced to aputs
call:movq (%rsi), %rdi callq _puts
While this makes perfect sense in this case, it raises two questions:
- How often does function call substitution happen in optimized compilation? Is this frequent or is stdio an exception?
- Could I write compiler optimizations for my own libraries? How would I do that?
解决方案
- How often does function call substitution happen in optimized compilation? Is this frequent or is stdio an exception?
The optimization that replaces
printf
withputs
in LLVM is in the classLibCallSimplifier
. You can see the header file in llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h and the implementation in llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp. Looking at the files will give an example of some of the other library call replacement optimizations that are done (the header file is probably easier to start with). And of course there are other many other optimizations that LLVM does, you can get an idea of some of them by looking at the list of LLVM passes.
- Could I write compiler optimizations for my own libraries? How would I do that?
Yes you could. LLVM is very modular and performs transformation on the IR in a series of passes. So if you wanted to add a custom pass yourself for your own library you could do so (although it is still a fair amount of work to understand how the LLVM compiler flow works). A good starting point is the document: Writing an LLVM Pass.
这篇关于-O2优化的printf(QUOT;%S \\ n&QUOT ;, STR)到看跌期权(STR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!