[LLVMdev] C infinite recursion mis-optimized?

Isaac Dupree ml at isaac.cedarswampstudios.org
Sun Feb 28 10:28:41 PST 2010


On 02/28/10 07:39, Duncan Sands wrote:
> Hi Isaac,
>
>> For fun, I made a recursive function, but LLVM optimized it wrong (if
>> I'm understanding C standards correctly).
>>
>> "void f() { f(); }"[see llvm-code in footnote 1]
>> was optimized to be equivalent to "void f() {}"[also 1].  I believe it
>> should either be equivalent in effect to "void f() { while(1){} }"[2],
>> which produces an infinite loop, or perhaps lead to a stack overflow (as
>> GCC might produce)... it's alright if the infinite loop is written using
>> tail-calls. All I expect is that it never actually return when you run it.
>
> this sounds like a manifestation of bug 965, see http://llvm.org/PR965

ah, you are quite right!  I'd searched "recursion" to look for bugs, but 
the problem (as described in #965) is more general than that.  (I'm 
quite familiar with Haskell, in which termination vs. nontermination is 
discussed much more than it is in C.)  The optimization and the 
"willreturn" suggestion make sense to me.  (Is it worth commenting on 
the bug-report? I guess not, since I don't actually have anything to 
add: it's already a clear bug with a clear solution: though perhaps 
having more test-cases for a testsuite is useful to LLVM?)

-Isaac



More information about the llvm-dev mailing list