简体   繁体   English

lang无限尾递归优化

[英]clang infinite tail recursion optimization

#include <iostream> 

int foo(int i){ 
     return foo(i + 1);
} 

int main(int argc,char * argv[]){ 
     if(argc != 2){ 
         return 1; 
     } 
     std::cout << foo(std::atoi(argv[1])) << std::endl; 
} 

% clang++ -O2 test.cc %clang ++ -O2 test.cc

% time ./a.out 42 %时间./a.out 42

1490723512 1490723512

./a.out 42 0.00s user 0.00s system 69% cpu 0.004 total ./a.out 42 0.00s用户0.00s系统69%cpu 0.004总计

% time ./a.out 42 %时间./a.out 42

1564058296 1564058296

./a.out 42 0.00s user 0.00s system 56% cpu 0.006 total ./a.out 42 0.00s用户0.00s系统56%cpu 0.006总计

% g++ -O2 test.cc %g ++ -O2 test.cc

% ./a.out 42 #infinte recursion %./a.out 42#无限递归

^C ^ C

% clang++ --version 
clang version 3.3 (tags/RELEASE_33/final) 
Target: x86_64-apple-darwin12.4.0 
Thread model: posix 
% g++ --version 
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) 
Copyright (C) 2007 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions.  There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

So is it a bug or a feature of clang++? 那是clang ++的错误还是功能?

While both g++ and clang++ are able to compile C++98 and C++11 code, clang++ was designed from the start as a C++11 compiler and has some C++11 behaviors embedded in its DNA (so to speak). 虽然g ++和clang ++都能够编译C ++ 98和C ++ 11代码,但是clang ++最初是作为C ++ 11编译器设计的,并且在其DNA中嵌入了一些C ++ 11行为(可以这么说) 。

With C++11 the C++ standard became thread aware, and that means that now there are some specific thread behavior. 有了C ++ 11,C ++标准就可以识别线程了,这意味着现在有了一些特定的线程行为。 In particular 1.10/24 states: 特别是1.10 / 24指出:

The implementation may assume that any thread will eventually do one of the following: 该实现可以假定任何线程最终都将执行以下操作之一:

— terminate, -终止,

— make a call to a library I/O function, —调用库I / O函数,

— access or modify a volatile object, or —访问或修改易失性对象,或

— perform a synchronization operation or an atomic operation. —执行同步操作或原子操作。

[Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. [注:这旨在允许编译器转换,例如删除空循环,即使无法证明终止也是如此。 — end note ] —尾注]

And that is precisely what clang++ is doing when optimizing. 而这正是clang ++在优化时所做的。 It sees that the function has no side effects and removes it even if it does not terminate. 可以看到该函数没有副作用, 即使没有终止也将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM